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
b6607597bb9970cd6e3fef63181d496233f431b1
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/seq/wseq.lean
9d375e3e4b6a39a52560d5e592f9251210490f98
[ "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
55,303
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.seq.seq import data.dlist open function universes u v w /- coinductive wseq (α : Type u) : Type u | nil : wseq α | cons : α → wseq α → wseq α | think : wseq α → wseq α -/ /-- Weak sequences. While the `seq` structure allows for lists which may not be finite, a weak sequence also allows the computation of each element to involve an indeterminate amount of computation, including possibly an infinite loop. This is represented as a regular `seq` interspersed with `none` elements to indicate that computation is ongoing. This model is appropriate for Haskell style lazy lists, and is closed under most interesting computation patterns on infinite lists, but conversely it is difficult to extract elements from it. -/ def wseq (α) := seq (option α) namespace wseq variables {α : Type u} {β : Type v} {γ : Type w} /-- Turn a sequence into a weak sequence -/ def of_seq : seq α → wseq α := (<$>) some /-- Turn a list into a weak sequence -/ def of_list (l : list α) : wseq α := of_seq l /-- Turn a stream into a weak sequence -/ def of_stream (l : stream α) : wseq α := of_seq l instance coe_seq : has_coe (seq α) (wseq α) := ⟨of_seq⟩ instance coe_list : has_coe (list α) (wseq α) := ⟨of_list⟩ instance coe_stream : has_coe (stream α) (wseq α) := ⟨of_stream⟩ /-- The empty weak sequence -/ def nil : wseq α := seq.nil instance : inhabited (wseq α) := ⟨nil⟩ /-- Prepend an element to a weak sequence -/ def cons (a : α) : wseq α → wseq α := seq.cons (some a) /-- Compute for one tick, without producing any elements -/ def think : wseq α → wseq α := seq.cons none /-- Destruct a weak sequence, to (eventually possibly) produce either `none` for `nil` or `some (a, s)` if an element is produced. -/ def destruct : wseq α → computation (option (α × wseq α)) := computation.corec (λs, match seq.destruct s with | none := sum.inl none | some (none, s') := sum.inr s' | some (some a, s') := sum.inl (some (a, s')) end) def cases_on {C : wseq α → Sort v} (s : wseq α) (h1 : C nil) (h2 : ∀ x s, C (cons x s)) (h3 : ∀ s, C (think s)) : C s := seq.cases_on s h1 (λ o, option.cases_on o h3 h2) protected def mem (a : α) (s : wseq α) := seq.mem (some a) s instance : has_mem α (wseq α) := ⟨wseq.mem⟩ theorem not_mem_nil (a : α) : a ∉ @nil α := seq.not_mem_nil a /-- Get the head of a weak sequence. This involves a possibly infinite computation. -/ def head (s : wseq α) : computation (option α) := computation.map ((<$>) prod.fst) (destruct s) /-- Encode a computation yielding a weak sequence into additional `think` constructors in a weak sequence -/ def flatten : computation (wseq α) → wseq α := seq.corec (λc, match computation.destruct c with | sum.inl s := seq.omap return (seq.destruct s) | sum.inr c' := some (none, c') end) /-- Get the tail of a weak sequence. This doesn't need a `computation` wrapper, unlike `head`, because `flatten` allows us to hide this in the construction of the weak sequence itself. -/ def tail (s : wseq α) : wseq α := flatten $ (λo, option.rec_on o nil prod.snd) <$> destruct s /-- drop the first `n` elements from `s`. -/ def drop (s : wseq α) : ℕ → wseq α | 0 := s | (n+1) := tail (drop n) attribute [simp] drop /-- Get the nth element of `s`. -/ def nth (s : wseq α) (n : ℕ) : computation (option α) := head (drop s n) /-- Convert `s` to a list (if it is finite and completes in finite time). -/ def to_list (s : wseq α) : computation (list α) := @computation.corec (list α) (list α × wseq α) (λ⟨l, s⟩, match seq.destruct s with | none := sum.inl l.reverse | some (none, s') := sum.inr (l, s') | some (some a, s') := sum.inr (a::l, s') end) ([], s) /-- Get the length of `s` (if it is finite and completes in finite time). -/ def length (s : wseq α) : computation ℕ := @computation.corec ℕ (ℕ × wseq α) (λ⟨n, s⟩, match seq.destruct s with | none := sum.inl n | some (none, s') := sum.inr (n, s') | some (some a, s') := sum.inr (n+1, s') end) (0, s) /-- A weak sequence is finite if `to_list s` terminates. Equivalently, it is a finite number of `think` and `cons` applied to `nil`. -/ class is_finite (s : wseq α) : Prop := (out : (to_list s).terminates) instance to_list_terminates (s : wseq α) [h : is_finite s] : (to_list s).terminates := h.out /-- Get the list corresponding to a finite weak sequence. -/ def get (s : wseq α) [is_finite s] : list α := (to_list s).get /-- A weak sequence is *productive* if it never stalls forever - there are always a finite number of `think`s between `cons` constructors. The sequence itself is allowed to be infinite though. -/ class productive (s : wseq α) : Prop := (nth_terminates : ∀ n, (nth s n).terminates) theorem productive_iff (s : wseq α) : productive s ↔ ∀ n, (nth s n).terminates := ⟨λ h, h.1, λ h, ⟨h⟩⟩ instance nth_terminates (s : wseq α) [h : productive s] : ∀ n, (nth s n).terminates := h.nth_terminates instance head_terminates (s : wseq α) [productive s] : (head s).terminates := s.nth_terminates 0 /-- Replace the `n`th element of `s` with `a`. -/ def update_nth (s : wseq α) (n : ℕ) (a : α) : wseq α := @seq.corec (option α) (ℕ × wseq α) (λ⟨n, s⟩, match seq.destruct s, n with | none, n := none | some (none, s'), n := some (none, n, s') | some (some a', s'), 0 := some (some a', 0, s') | some (some a', s'), 1 := some (some a, 0, s') | some (some a', s'), (n+2) := some (some a', n+1, s') end) (n+1, s) /-- Remove the `n`th element of `s`. -/ def remove_nth (s : wseq α) (n : ℕ) : wseq α := @seq.corec (option α) (ℕ × wseq α) (λ⟨n, s⟩, match seq.destruct s, n with | none, n := none | some (none, s'), n := some (none, n, s') | some (some a', s'), 0 := some (some a', 0, s') | some (some a', s'), 1 := some (none, 0, s') | some (some a', s'), (n+2) := some (some a', n+1, s') end) (n+1, s) /-- Map the elements of `s` over `f`, removing any values that yield `none`. -/ def filter_map (f : α → option β) : wseq α → wseq β := seq.corec (λs, match seq.destruct s with | none := none | some (none, s') := some (none, s') | some (some a, s') := some (f a, s') end) /-- Select the elements of `s` that satisfy `p`. -/ def filter (p : α → Prop) [decidable_pred p] : wseq α → wseq α := filter_map (λa, if p a then some a else none) -- example of infinite list manipulations /-- Get the first element of `s` satisfying `p`. -/ def find (p : α → Prop) [decidable_pred p] (s : wseq α) : computation (option α) := head $ filter p s /-- Zip a function over two weak sequences -/ def zip_with (f : α → β → γ) (s1 : wseq α) (s2 : wseq β) : wseq γ := @seq.corec (option γ) (wseq α × wseq β) (λ⟨s1, s2⟩, match seq.destruct s1, seq.destruct s2 with | some (none, s1'), some (none, s2') := some (none, s1', s2') | some (some a1, s1'), some (none, s2') := some (none, s1, s2') | some (none, s1'), some (some a2, s2') := some (none, s1', s2) | some (some a1, s1'), some (some a2, s2') := some (some (f a1 a2), s1', s2') | _, _ := none end) (s1, s2) /-- Zip two weak sequences into a single sequence of pairs -/ def zip : wseq α → wseq β → wseq (α × β) := zip_with prod.mk /-- Get the list of indexes of elements of `s` satisfying `p` -/ def find_indexes (p : α → Prop) [decidable_pred p] (s : wseq α) : wseq ℕ := (zip s (stream.nats : wseq ℕ)).filter_map (λ ⟨a, n⟩, if p a then some n else none) /-- Get the index of the first element of `s` satisfying `p` -/ def find_index (p : α → Prop) [decidable_pred p] (s : wseq α) : computation ℕ := (λ o, option.get_or_else o 0) <$> head (find_indexes p s) /-- Get the index of the first occurrence of `a` in `s` -/ def index_of [decidable_eq α] (a : α) : wseq α → computation ℕ := find_index (eq a) /-- Get the indexes of occurrences of `a` in `s` -/ def indexes_of [decidable_eq α] (a : α) : wseq α → wseq ℕ := find_indexes (eq a) /-- `union s1 s2` is a weak sequence which interleaves `s1` and `s2` in some order (nondeterministically). -/ def union (s1 s2 : wseq α) : wseq α := @seq.corec (option α) (wseq α × wseq α) (λ⟨s1, s2⟩, match seq.destruct s1, seq.destruct s2 with | none, none := none | some (a1, s1'), none := some (a1, s1', nil) | none, some (a2, s2') := some (a2, nil, s2') | some (none, s1'), some (none, s2') := some (none, s1', s2') | some (some a1, s1'), some (none, s2') := some (some a1, s1', s2') | some (none, s1'), some (some a2, s2') := some (some a2, s1', s2') | some (some a1, s1'), some (some a2, s2') := some (some a1, cons a2 s1', s2') end) (s1, s2) /-- Returns `tt` if `s` is `nil` and `ff` if `s` has an element -/ def is_empty (s : wseq α) : computation bool := computation.map option.is_none $ head s /-- Calculate one step of computation -/ def compute (s : wseq α) : wseq α := match seq.destruct s with | some (none, s') := s' | _ := s end /-- Get the first `n` elements of a weak sequence -/ def take (s : wseq α) (n : ℕ) : wseq α := @seq.corec (option α) (ℕ × wseq α) (λ⟨n, s⟩, match n, seq.destruct s with | 0, _ := none | m+1, none := none | m+1, some (none, s') := some (none, m+1, s') | m+1, some (some a, s') := some (some a, m, s') end) (n, s) /-- Split the sequence at position `n` into a finite initial segment and the weak sequence tail -/ def split_at (s : wseq α) (n : ℕ) : computation (list α × wseq α) := @computation.corec (list α × wseq α) (ℕ × list α × wseq α) (λ⟨n, l, s⟩, match n, seq.destruct s with | 0, _ := sum.inl (l.reverse, s) | m+1, none := sum.inl (l.reverse, s) | m+1, some (none, s') := sum.inr (n, l, s') | m+1, some (some a, s') := sum.inr (m, a::l, s') end) (n, [], s) /-- Returns `tt` if any element of `s` satisfies `p` -/ def any (s : wseq α) (p : α → bool) : computation bool := computation.corec (λs : wseq α, match seq.destruct s with | none := sum.inl ff | some (none, s') := sum.inr s' | some (some a, s') := if p a then sum.inl tt else sum.inr s' end) s /-- Returns `tt` if every element of `s` satisfies `p` -/ def all (s : wseq α) (p : α → bool) : computation bool := computation.corec (λs : wseq α, match seq.destruct s with | none := sum.inl tt | some (none, s') := sum.inr s' | some (some a, s') := if p a then sum.inr s' else sum.inl ff end) s /-- Apply a function to the elements of the sequence to produce a sequence of partial results. (There is no `scanr` because this would require working from the end of the sequence, which may not exist.) -/ def scanl (f : α → β → α) (a : α) (s : wseq β) : wseq α := cons a $ @seq.corec (option α) (α × wseq β) (λ⟨a, s⟩, match seq.destruct s with | none := none | some (none, s') := some (none, a, s') | some (some b, s') := let a' := f a b in some (some a', a', s') end) (a, s) /-- Get the weak sequence of initial segments of the input sequence -/ def inits (s : wseq α) : wseq (list α) := cons [] $ @seq.corec (option (list α)) (dlist α × wseq α) (λ ⟨l, s⟩, match seq.destruct s with | none := none | some (none, s') := some (none, l, s') | some (some a, s') := let l' := l.concat a in some (some l'.to_list, l', s') end) (dlist.empty, s) /-- Like take, but does not wait for a result. Calculates `n` steps of computation and returns the sequence computed so far -/ def collect (s : wseq α) (n : ℕ) : list α := (seq.take n s).filter_map id /-- Append two weak sequences. As with `seq.append`, this may not use the second sequence if the first one takes forever to compute -/ def append : wseq α → wseq α → wseq α := seq.append /-- Map a function over a weak sequence -/ def map (f : α → β) : wseq α → wseq β := seq.map (option.map f) /-- Flatten a sequence of weak sequences. (Note that this allows empty sequences, unlike `seq.join`.) -/ def join (S : wseq (wseq α)) : wseq α := seq.join ((λo : option (wseq α), match o with | none := seq1.ret none | some s := (none, s) end) <$> S) /-- Monadic bind operator for weak sequences -/ def bind (s : wseq α) (f : α → wseq β) : wseq β := join (map f s) @[simp] def lift_rel_o (R : α → β → Prop) (C : wseq α → wseq β → Prop) : option (α × wseq α) → option (β × wseq β) → Prop | none none := true | (some (a, s)) (some (b, t)) := R a b ∧ C s t | _ _ := false theorem lift_rel_o.imp {R S : α → β → Prop} {C D : wseq α → wseq β → Prop} (H1 : ∀ a b, R a b → S a b) (H2 : ∀ s t, C s t → D s t) : ∀ {o p}, lift_rel_o R C o p → lift_rel_o S D o p | none none h := trivial | (some (a, s)) (some (b, t)) h := and.imp (H1 _ _) (H2 _ _) h | none (some _) h := false.elim h | (some (_, _)) none h := false.elim h theorem lift_rel_o.imp_right (R : α → β → Prop) {C D : wseq α → wseq β → Prop} (H : ∀ s t, C s t → D s t) {o p} : lift_rel_o R C o p → lift_rel_o R D o p := lift_rel_o.imp (λ _ _, id) H @[simp] def bisim_o (R : wseq α → wseq α → Prop) : option (α × wseq α) → option (α × wseq α) → Prop := lift_rel_o (=) R theorem bisim_o.imp {R S : wseq α → wseq α → Prop} (H : ∀ s t, R s t → S s t) {o p} : bisim_o R o p → bisim_o S o p := lift_rel_o.imp_right _ H /-- Two weak sequences are `lift_rel R` related if they are either both empty, or they are both nonempty and the heads are `R` related and the tails are `lift_rel R` related. (This is a coinductive definition.) -/ def lift_rel (R : α → β → Prop) (s : wseq α) (t : wseq β) : Prop := ∃ C : wseq α → wseq β → Prop, C s t ∧ ∀ {s t}, C s t → computation.lift_rel (lift_rel_o R C) (destruct s) (destruct t) /-- If two sequences are equivalent, then they have the same values and the same computational behavior (i.e. if one loops forever then so does the other), although they may differ in the number of `think`s needed to arrive at the answer. -/ def equiv : wseq α → wseq α → Prop := lift_rel (=) theorem lift_rel_destruct {R : α → β → Prop} {s : wseq α} {t : wseq β} : lift_rel R s t → computation.lift_rel (lift_rel_o R (lift_rel R)) (destruct s) (destruct t) | ⟨R, h1, h2⟩ := by refine computation.lift_rel.imp _ _ _ (h2 h1); apply lift_rel_o.imp_right; exact λ s' t' h', ⟨R, h', @h2⟩ theorem lift_rel_destruct_iff {R : α → β → Prop} {s : wseq α} {t : wseq β} : lift_rel R s t ↔ computation.lift_rel (lift_rel_o R (lift_rel R)) (destruct s) (destruct t) := ⟨lift_rel_destruct, λ h, ⟨λ s t, lift_rel R s t ∨ computation.lift_rel (lift_rel_o R (lift_rel R)) (destruct s) (destruct t), or.inr h, λ s t h, begin have h : computation.lift_rel (lift_rel_o R (lift_rel R)) (destruct s) (destruct t), { cases h with h h, exact lift_rel_destruct h, assumption }, apply computation.lift_rel.imp _ _ _ h, intros a b, apply lift_rel_o.imp_right, intros s t, apply or.inl end⟩⟩ infix ` ~ `:50 := equiv theorem destruct_congr {s t : wseq α} : s ~ t → computation.lift_rel (bisim_o (~)) (destruct s) (destruct t) := lift_rel_destruct theorem destruct_congr_iff {s t : wseq α} : s ~ t ↔ computation.lift_rel (bisim_o (~)) (destruct s) (destruct t) := lift_rel_destruct_iff theorem lift_rel.refl (R : α → α → Prop) (H : reflexive R) : reflexive (lift_rel R) := λ s, begin refine ⟨(=), rfl, λ s t (h : s = t), _⟩, rw ←h, apply computation.lift_rel.refl, intro a, cases a with a, simp, cases a; simp, apply H end theorem lift_rel_o.swap (R : α → β → Prop) (C) : swap (lift_rel_o R C) = lift_rel_o (swap R) (swap C) := by funext x y; cases x with x; [skip, cases x]; { cases y with y; [skip, cases y]; refl } theorem lift_rel.swap_lem {R : α → β → Prop} {s1 s2} (h : lift_rel R s1 s2) : lift_rel (swap R) s2 s1 := begin refine ⟨swap (lift_rel R), h, λ s t (h : lift_rel R t s), _⟩, rw [←lift_rel_o.swap, computation.lift_rel.swap], apply lift_rel_destruct h end theorem lift_rel.swap (R : α → β → Prop) : swap (lift_rel R) = lift_rel (swap R) := funext $ λ x, funext $ λ y, propext ⟨lift_rel.swap_lem, lift_rel.swap_lem⟩ theorem lift_rel.symm (R : α → α → Prop) (H : symmetric R) : symmetric (lift_rel R) := λ s1 s2 (h : swap (lift_rel R) s2 s1), by rwa [lift_rel.swap, show swap R = R, from funext $ λ a, funext $ λ b, propext $ by constructor; apply H] at h theorem lift_rel.trans (R : α → α → Prop) (H : transitive R) : transitive (lift_rel R) := λ s t u h1 h2, begin refine ⟨λ s u, ∃ t, lift_rel R s t ∧ lift_rel R t u, ⟨t, h1, h2⟩, λ s u h, _⟩, rcases h with ⟨t, h1, h2⟩, have h1 := lift_rel_destruct h1, have h2 := lift_rel_destruct h2, refine computation.lift_rel_def.2 ⟨(computation.terminates_of_lift_rel h1).trans (computation.terminates_of_lift_rel h2), λ a c ha hc, _⟩, rcases h1.left ha with ⟨b, hb, t1⟩, have t2 := computation.rel_of_lift_rel h2 hb hc, cases a with a; cases c with c, { trivial }, { cases b, {cases t2}, {cases t1} }, { cases a, cases b with b, {cases t1}, {cases b, cases t2} }, { cases a with a s, cases b with b, {cases t1}, cases b with b t, cases c with c u, cases t1 with ab st, cases t2 with bc tu, exact ⟨H ab bc, t, st, tu⟩ } end theorem lift_rel.equiv (R : α → α → Prop) : equivalence R → equivalence (lift_rel R) | ⟨refl, symm, trans⟩ := ⟨lift_rel.refl R refl, lift_rel.symm R symm, lift_rel.trans R trans⟩ @[refl] theorem equiv.refl : ∀ (s : wseq α), s ~ s := lift_rel.refl (=) eq.refl @[symm] theorem equiv.symm : ∀ {s t : wseq α}, s ~ t → t ~ s := lift_rel.symm (=) (@eq.symm _) @[trans] theorem equiv.trans : ∀ {s t u : wseq α}, s ~ t → t ~ u → s ~ u := lift_rel.trans (=) (@eq.trans _) theorem equiv.equivalence : equivalence (@equiv α) := ⟨@equiv.refl _, @equiv.symm _, @equiv.trans _⟩ open computation local notation `return` := computation.return @[simp] theorem destruct_nil : destruct (nil : wseq α) = return none := computation.destruct_eq_ret rfl @[simp] theorem destruct_cons (a : α) (s) : destruct (cons a s) = return (some (a, s)) := computation.destruct_eq_ret $ by simp [destruct, cons, computation.rmap] @[simp] theorem destruct_think (s : wseq α) : destruct (think s) = (destruct s).think := computation.destruct_eq_think $ by simp [destruct, think, computation.rmap] @[simp] theorem seq_destruct_nil : seq.destruct (nil : wseq α) = none := seq.destruct_nil @[simp] theorem seq_destruct_cons (a : α) (s) : seq.destruct (cons a s) = some (some a, s) := seq.destruct_cons _ _ @[simp] theorem seq_destruct_think (s : wseq α) : seq.destruct (think s) = some (none, s) := seq.destruct_cons _ _ @[simp] theorem head_nil : head (nil : wseq α) = return none := by simp [head]; refl @[simp] theorem head_cons (a : α) (s) : head (cons a s) = return (some a) := by simp [head]; refl @[simp] theorem head_think (s : wseq α) : head (think s) = (head s).think := by simp [head]; refl @[simp] theorem flatten_ret (s : wseq α) : flatten (return s) = s := begin refine seq.eq_of_bisim (λs1 s2, flatten (return s2) = s1) _ rfl, intros s' s h, rw ←h, simp [flatten], cases seq.destruct s, { simp }, { cases val with o s', simp } end @[simp] theorem flatten_think (c : computation (wseq α)) : flatten c.think = think (flatten c) := seq.destruct_eq_cons $ by simp [flatten, think] @[simp] theorem destruct_flatten (c : computation (wseq α)) : destruct (flatten c) = c >>= destruct := begin refine computation.eq_of_bisim (λc1 c2, c1 = c2 ∨ ∃ c, c1 = destruct (flatten c) ∧ c2 = computation.bind c destruct) _ (or.inr ⟨c, rfl, rfl⟩), intros c1 c2 h, exact match c1, c2, h with | _, _, (or.inl $ eq.refl c) := by cases c.destruct; simp | _, _, (or.inr ⟨c, rfl, rfl⟩) := begin apply c.cases_on (λa, _) (λc', _); repeat {simp}, { cases (destruct a).destruct; simp }, { exact or.inr ⟨c', rfl, rfl⟩ } end end end theorem head_terminates_iff (s : wseq α) : terminates (head s) ↔ terminates (destruct s) := terminates_map_iff _ (destruct s) @[simp] theorem tail_nil : tail (nil : wseq α) = nil := by simp [tail] @[simp] theorem tail_cons (a : α) (s) : tail (cons a s) = s := by simp [tail] @[simp] theorem tail_think (s : wseq α) : tail (think s) = (tail s).think := by simp [tail] @[simp] theorem dropn_nil (n) : drop (nil : wseq α) n = nil := by induction n; simp [*, drop] @[simp] theorem dropn_cons (a : α) (s) (n) : drop (cons a s) (n+1) = drop s n := by induction n; simp [*, drop] @[simp] theorem dropn_think (s : wseq α) (n) : drop (think s) n = (drop s n).think := by induction n; simp [*, drop] theorem dropn_add (s : wseq α) (m) : ∀ n, drop s (m + n) = drop (drop s m) n | 0 := rfl | (n+1) := congr_arg tail (dropn_add n) theorem dropn_tail (s : wseq α) (n) : drop (tail s) n = drop s (n + 1) := by rw add_comm; symmetry; apply dropn_add theorem nth_add (s : wseq α) (m n) : nth s (m + n) = nth (drop s m) n := congr_arg head (dropn_add _ _ _) theorem nth_tail (s : wseq α) (n) : nth (tail s) n = nth s (n + 1) := congr_arg head (dropn_tail _ _) @[simp] theorem join_nil : join nil = (nil : wseq α) := seq.join_nil @[simp] theorem join_think (S : wseq (wseq α)) : join (think S) = think (join S) := by { simp [think, join], unfold functor.map, simp [join, seq1.ret] } @[simp] theorem join_cons (s : wseq α) (S) : join (cons s S) = think (append s (join S)) := by { simp [think, join], unfold functor.map, simp [join, cons, append] } @[simp] theorem nil_append (s : wseq α) : append nil s = s := seq.nil_append _ @[simp] theorem cons_append (a : α) (s t) : append (cons a s) t = cons a (append s t) := seq.cons_append _ _ _ @[simp] theorem think_append (s t : wseq α) : append (think s) t = think (append s t) := seq.cons_append _ _ _ @[simp] theorem append_nil (s : wseq α) : append s nil = s := seq.append_nil _ @[simp] theorem append_assoc (s t u : wseq α) : append (append s t) u = append s (append t u) := seq.append_assoc _ _ _ @[simp] def tail.aux : option (α × wseq α) → computation (option (α × wseq α)) | none := return none | (some (a, s)) := destruct s theorem destruct_tail (s : wseq α) : destruct (tail s) = destruct s >>= tail.aux := begin simp [tail], rw [← bind_pure_comp_eq_map, is_lawful_monad.bind_assoc], apply congr_arg, ext1 (_|⟨a, s⟩); apply (@pure_bind computation _ _ _ _ _ _).trans _; simp end @[simp] def drop.aux : ℕ → option (α × wseq α) → computation (option (α × wseq α)) | 0 := return | (n+1) := λ a, tail.aux a >>= drop.aux n theorem drop.aux_none : ∀ n, @drop.aux α n none = return none | 0 := rfl | (n+1) := show computation.bind (return none) (drop.aux n) = return none, by rw [ret_bind, drop.aux_none] theorem destruct_dropn : ∀ (s : wseq α) n, destruct (drop s n) = destruct s >>= drop.aux n | s 0 := (bind_ret' _).symm | s (n+1) := by rw [← dropn_tail, destruct_dropn _ n, destruct_tail, is_lawful_monad.bind_assoc]; refl theorem head_terminates_of_head_tail_terminates (s : wseq α) [T : terminates (head (tail s))] : terminates (head s) := (head_terminates_iff _).2 $ begin rcases (head_terminates_iff _).1 T with ⟨⟨a, h⟩⟩, simp [tail] at h, rcases exists_of_mem_bind h with ⟨s', h1, h2⟩, unfold functor.map at h1, exact let ⟨t, h3, h4⟩ := exists_of_mem_map h1 in terminates_of_mem h3 end theorem destruct_some_of_destruct_tail_some {s : wseq α} {a} (h : some a ∈ destruct (tail s)) : ∃ a', some a' ∈ destruct s := begin unfold tail functor.map at h, simp at h, rcases exists_of_mem_bind h with ⟨t, tm, td⟩, clear h, rcases exists_of_mem_map tm with ⟨t', ht', ht2⟩, clear tm, cases t' with t'; rw ←ht2 at td; simp at td, { have := mem_unique td (ret_mem _), contradiction }, { exact ⟨_, ht'⟩ } end theorem head_some_of_head_tail_some {s : wseq α} {a} (h : some a ∈ head (tail s)) : ∃ a', some a' ∈ head s := begin unfold head at h, rcases exists_of_mem_map h with ⟨o, md, e⟩, clear h, cases o with o; injection e with h', clear e h', cases destruct_some_of_destruct_tail_some md with a am, exact ⟨_, mem_map ((<$>) (@prod.fst α (wseq α))) am⟩ end theorem head_some_of_nth_some {s : wseq α} {a n} (h : some a ∈ nth s n) : ∃ a', some a' ∈ head s := begin revert a, induction n with n IH; intros, exacts [⟨_, h⟩, let ⟨a', h'⟩ := head_some_of_head_tail_some h in IH h'] end instance productive_tail (s : wseq α) [productive s] : productive (tail s) := ⟨λ n, by rw [nth_tail]; apply_instance⟩ instance productive_dropn (s : wseq α) [productive s] (n) : productive (drop s n) := ⟨λ m, by rw [←nth_add]; apply_instance⟩ /-- Given a productive weak sequence, we can collapse all the `think`s to produce a sequence. -/ def to_seq (s : wseq α) [productive s] : seq α := ⟨λ n, (nth s n).get, λn h, begin cases e : computation.get (nth s (n + 1)), {assumption}, have := mem_of_get_eq _ e, simp [nth] at this h, cases head_some_of_head_tail_some this with a' h', have := mem_unique h' (@mem_of_get_eq _ _ _ _ h), contradiction end⟩ theorem nth_terminates_le {s : wseq α} {m n} (h : m ≤ n) : terminates (nth s n) → terminates (nth s m) := by induction h with m' h IH; [exact id, exact λ T, IH (@head_terminates_of_head_tail_terminates _ _ T)] theorem head_terminates_of_nth_terminates {s : wseq α} {n} : terminates (nth s n) → terminates (head s) := nth_terminates_le (nat.zero_le n) theorem destruct_terminates_of_nth_terminates {s : wseq α} {n} (T : terminates (nth s n)) : terminates (destruct s) := (head_terminates_iff _).1 $ head_terminates_of_nth_terminates T theorem mem_rec_on {C : wseq α → Prop} {a s} (M : a ∈ s) (h1 : ∀ b s', (a = b ∨ C s') → C (cons b s')) (h2 : ∀ s, C s → C (think s)) : C s := begin apply seq.mem_rec_on M, intros o s' h, cases o with b, { apply h2, cases h, {contradiction}, {assumption} }, { apply h1, apply or.imp_left _ h, intro h, injection h } end @[simp] theorem mem_think (s : wseq α) (a) : a ∈ think s ↔ a ∈ s := begin cases s with f al, change some (some a) ∈ some none :: f ↔ some (some a) ∈ f, constructor; intro h, { apply (stream.eq_or_mem_of_mem_cons h).resolve_left, intro, injections }, { apply stream.mem_cons_of_mem _ h } end theorem eq_or_mem_iff_mem {s : wseq α} {a a' s'} : some (a', s') ∈ destruct s → (a ∈ s ↔ a = a' ∨ a ∈ s') := begin generalize e : destruct s = c, intro h, revert s, apply computation.mem_rec_on h _ (λ c IH, _); intro s; apply s.cases_on _ (λ x s, _) (λ s, _); intros m; have := congr_arg computation.destruct m; simp at this; cases this with i1 i2, { rw [i1, i2], cases s' with f al, unfold cons has_mem.mem wseq.mem seq.mem seq.cons, simp, have h_a_eq_a' : a = a' ↔ some (some a) = some (some a'), {simp}, rw [h_a_eq_a'], refine ⟨stream.eq_or_mem_of_mem_cons, λo, _⟩, { cases o with e m, { rw e, apply stream.mem_cons }, { exact stream.mem_cons_of_mem _ m } } }, { simp, exact IH this } end @[simp] theorem mem_cons_iff (s : wseq α) (b) {a} : a ∈ cons b s ↔ a = b ∨ a ∈ s := eq_or_mem_iff_mem $ by simp [ret_mem] theorem mem_cons_of_mem {s : wseq α} (b) {a} (h : a ∈ s) : a ∈ cons b s := (mem_cons_iff _ _).2 (or.inr h) theorem mem_cons (s : wseq α) (a) : a ∈ cons a s := (mem_cons_iff _ _).2 (or.inl rfl) theorem mem_of_mem_tail {s : wseq α} {a} : a ∈ tail s → a ∈ s := begin intro h, have := h, cases h with n e, revert s, simp [stream.nth], induction n with n IH; intro s; apply s.cases_on _ (λx s, _) (λ s, _); repeat{simp}; intros m e; injections, { exact or.inr m }, { exact or.inr m }, { apply IH m, rw e, cases tail s, refl } end theorem mem_of_mem_dropn {s : wseq α} {a} : ∀ {n}, a ∈ drop s n → a ∈ s | 0 h := h | (n+1) h := @mem_of_mem_dropn n (mem_of_mem_tail h) theorem nth_mem {s : wseq α} {a n} : some a ∈ nth s n → a ∈ s := begin revert s, induction n with n IH; intros s h, { rcases exists_of_mem_map h with ⟨o, h1, h2⟩, cases o with o; injection h2 with h', cases o with a' s', exact (eq_or_mem_iff_mem h1).2 (or.inl h'.symm) }, { have := @IH (tail s), rw nth_tail at this, exact mem_of_mem_tail (this h) } end theorem exists_nth_of_mem {s : wseq α} {a} (h : a ∈ s) : ∃ n, some a ∈ nth s n := begin apply mem_rec_on h, { intros a' s' h, cases h with h h, { existsi 0, simp [nth], rw h, apply ret_mem }, { cases h with n h, existsi n+1, simp [nth], exact h } }, { intros s' h, cases h with n h, existsi n, simp [nth], apply think_mem h } end theorem exists_dropn_of_mem {s : wseq α} {a} (h : a ∈ s) : ∃ n s', some (a, s') ∈ destruct (drop s n) := let ⟨n, h⟩ := exists_nth_of_mem h in ⟨n, begin rcases (head_terminates_iff _).1 ⟨⟨_, h⟩⟩ with ⟨⟨o, om⟩⟩, have := mem_unique (mem_map _ om) h, cases o with o; injection this with i, cases o with a' s', dsimp at i, rw i at om, exact ⟨_, om⟩ end⟩ theorem lift_rel_dropn_destruct {R : α → β → Prop} {s t} (H : lift_rel R s t) : ∀ n, computation.lift_rel (lift_rel_o R (lift_rel R)) (destruct (drop s n)) (destruct (drop t n)) | 0 := lift_rel_destruct H | (n+1) := begin simp [destruct_tail], apply lift_rel_bind, apply lift_rel_dropn_destruct n, exact λ a b o, match a, b, o with | none, none, _ := by simp | some (a, s), some (b, t), ⟨h1, h2⟩ := by simp [tail.aux]; apply lift_rel_destruct h2 end end theorem exists_of_lift_rel_left {R : α → β → Prop} {s t} (H : lift_rel R s t) {a} (h : a ∈ s) : ∃ {b}, b ∈ t ∧ R a b := let ⟨n, h⟩ := exists_nth_of_mem h, ⟨some (._, s'), sd, rfl⟩ := exists_of_mem_map h, ⟨some (b, t'), td, ⟨ab, _⟩⟩ := (lift_rel_dropn_destruct H n).left sd in ⟨b, nth_mem (mem_map ((<$>) prod.fst.{v v}) td), ab⟩ theorem exists_of_lift_rel_right {R : α → β → Prop} {s t} (H : lift_rel R s t) {b} (h : b ∈ t) : ∃ {a}, a ∈ s ∧ R a b := by rw ←lift_rel.swap at H; exact exists_of_lift_rel_left H h theorem head_terminates_of_mem {s : wseq α} {a} (h : a ∈ s) : terminates (head s) := let ⟨n, h⟩ := exists_nth_of_mem h in head_terminates_of_nth_terminates ⟨⟨_, h⟩⟩ theorem of_mem_append {s₁ s₂ : wseq α} {a : α} : a ∈ append s₁ s₂ → a ∈ s₁ ∨ a ∈ s₂ := seq.of_mem_append theorem mem_append_left {s₁ s₂ : wseq α} {a : α} : a ∈ s₁ → a ∈ append s₁ s₂ := seq.mem_append_left theorem exists_of_mem_map {f} {b : β} : ∀ {s : wseq α}, b ∈ map f s → ∃ a, a ∈ s ∧ f a = b | ⟨g, al⟩ h := let ⟨o, om, oe⟩ := seq.exists_of_mem_map h in by cases o with a; injection oe with h'; exact ⟨a, om, h'⟩ @[simp] theorem lift_rel_nil (R : α → β → Prop) : lift_rel R nil nil := by rw [lift_rel_destruct_iff]; simp @[simp] theorem lift_rel_cons (R : α → β → Prop) (a b s t) : lift_rel R (cons a s) (cons b t) ↔ R a b ∧ lift_rel R s t := by rw [lift_rel_destruct_iff]; simp @[simp] theorem lift_rel_think_left (R : α → β → Prop) (s t) : lift_rel R (think s) t ↔ lift_rel R s t := by rw [lift_rel_destruct_iff, lift_rel_destruct_iff]; simp @[simp] theorem lift_rel_think_right (R : α → β → Prop) (s t) : lift_rel R s (think t) ↔ lift_rel R s t := by rw [lift_rel_destruct_iff, lift_rel_destruct_iff]; simp theorem cons_congr {s t : wseq α} (a : α) (h : s ~ t) : cons a s ~ cons a t := by unfold equiv; simp; exact h theorem think_equiv (s : wseq α) : think s ~ s := by unfold equiv; simp; apply equiv.refl theorem think_congr {s t : wseq α} (a : α) (h : s ~ t) : think s ~ think t := by unfold equiv; simp; exact h theorem head_congr : ∀ {s t : wseq α}, s ~ t → head s ~ head t := suffices ∀ {s t : wseq α}, s ~ t → ∀ {o}, o ∈ head s → o ∈ head t, from λ s t h o, ⟨this h, this h.symm⟩, begin intros s t h o ho, rcases @computation.exists_of_mem_map _ _ _ _ (destruct s) ho with ⟨ds, dsm, dse⟩, rw ←dse, cases destruct_congr h with l r, rcases l dsm with ⟨dt, dtm, dst⟩, cases ds with a; cases dt with b, { apply mem_map _ dtm }, { cases b, cases dst }, { cases a, cases dst }, { cases a with a s', cases b with b t', rw dst.left, exact @mem_map _ _ (@functor.map _ _ (α × wseq α) _ prod.fst) _ (destruct t) dtm } end theorem flatten_equiv {c : computation (wseq α)} {s} (h : s ∈ c) : flatten c ~ s := begin apply computation.mem_rec_on h, { simp }, { intro s', apply equiv.trans, simp [think_equiv] } end theorem lift_rel_flatten {R : α → β → Prop} {c1 : computation (wseq α)} {c2 : computation (wseq β)} (h : c1.lift_rel (lift_rel R) c2) : lift_rel R (flatten c1) (flatten c2) := let S := λ s t, ∃ c1 c2, s = flatten c1 ∧ t = flatten c2 ∧ computation.lift_rel (lift_rel R) c1 c2 in ⟨S, ⟨c1, c2, rfl, rfl, h⟩, λ s t h, match s, t, h with ._, ._, ⟨c1, c2, rfl, rfl, h⟩ := begin simp, apply lift_rel_bind _ _ h, intros a b ab, apply computation.lift_rel.imp _ _ _ (lift_rel_destruct ab), intros a b, apply lift_rel_o.imp_right, intros s t h, refine ⟨return s, return t, _, _, _⟩; simp [h] end end⟩ theorem flatten_congr {c1 c2 : computation (wseq α)} : computation.lift_rel equiv c1 c2 → flatten c1 ~ flatten c2 := lift_rel_flatten theorem tail_congr {s t : wseq α} (h : s ~ t) : tail s ~ tail t := begin apply flatten_congr, unfold functor.map, rw [←bind_ret, ←bind_ret], apply lift_rel_bind _ _ (destruct_congr h), intros a b h, simp, cases a with a; cases b with b, { trivial }, { cases h }, { cases a, cases h }, { cases a with a s', cases b with b t', exact h.right } end theorem dropn_congr {s t : wseq α} (h : s ~ t) (n) : drop s n ~ drop t n := by induction n; simp [*, tail_congr] theorem nth_congr {s t : wseq α} (h : s ~ t) (n) : nth s n ~ nth t n := head_congr (dropn_congr h _) theorem mem_congr {s t : wseq α} (h : s ~ t) (a) : a ∈ s ↔ a ∈ t := suffices ∀ {s t : wseq α}, s ~ t → a ∈ s → a ∈ t, from ⟨this h, this h.symm⟩, λ s t h as, let ⟨n, hn⟩ := exists_nth_of_mem as in nth_mem ((nth_congr h _ _).1 hn) theorem productive_congr {s t : wseq α} (h : s ~ t) : productive s ↔ productive t := by simp only [productive_iff]; exact forall_congr (λ n, terminates_congr $ nth_congr h _) theorem equiv.ext {s t : wseq α} (h : ∀ n, nth s n ~ nth t n) : s ~ t := ⟨λ s t, ∀ n, nth s n ~ nth t n, h, λs t h, begin refine lift_rel_def.2 ⟨_, _⟩, { rw [←head_terminates_iff, ←head_terminates_iff], exact terminates_congr (h 0) }, { intros a b ma mb, cases a with a; cases b with b, { trivial }, { injection mem_unique (mem_map _ ma) ((h 0 _).2 (mem_map _ mb)) }, { injection mem_unique (mem_map _ ma) ((h 0 _).2 (mem_map _ mb)) }, { cases a with a s', cases b with b t', injection mem_unique (mem_map _ ma) ((h 0 _).2 (mem_map _ mb)) with ab, refine ⟨ab, λ n, _⟩, refine (nth_congr (flatten_equiv (mem_map _ ma)) n).symm.trans ((_ : nth (tail s) n ~ nth (tail t) n).trans (nth_congr (flatten_equiv (mem_map _ mb)) n)), rw [nth_tail, nth_tail], apply h } } end⟩ theorem length_eq_map (s : wseq α) : length s = computation.map list.length (to_list s) := begin refine eq_of_bisim (λ c1 c2, ∃ (l : list α) (s : wseq α), c1 = corec length._match_2 (l.length, s) ∧ c2 = computation.map list.length (corec to_list._match_2 (l, s))) _ ⟨[], s, rfl, rfl⟩, intros s1 s2 h, rcases h with ⟨l, s, h⟩, rw [h.left, h.right], apply s.cases_on _ (λ a s, _) (λ s, _); repeat {simp [to_list, nil, cons, think, length]}, { refine ⟨a::l, s, _, _⟩; simp }, { refine ⟨l, s, _, _⟩; simp } end @[simp] theorem of_list_nil : of_list [] = (nil : wseq α) := rfl @[simp] theorem of_list_cons (a : α) (l) : of_list (a :: l) = cons a (of_list l) := show seq.map some (seq.of_list (a :: l)) = seq.cons (some a) (seq.map some (seq.of_list l)), by simp @[simp] theorem to_list'_nil (l : list α) : corec to_list._match_2 (l, nil) = return l.reverse := destruct_eq_ret rfl @[simp] theorem to_list'_cons (l : list α) (s : wseq α) (a : α) : corec to_list._match_2 (l, cons a s) = (corec to_list._match_2 (a::l, s)).think := destruct_eq_think $ by simp [to_list, cons] @[simp] theorem to_list'_think (l : list α) (s : wseq α) : corec to_list._match_2 (l, think s) = (corec to_list._match_2 (l, s)).think := destruct_eq_think $ by simp [to_list, think] theorem to_list'_map (l : list α) (s : wseq α) : corec to_list._match_2 (l, s) = ((++) l.reverse) <$> to_list s := begin refine eq_of_bisim (λ c1 c2, ∃ (l' : list α) (s : wseq α), c1 = corec to_list._match_2 (l' ++ l, s) ∧ c2 = computation.map ((++) l.reverse) (corec to_list._match_2 (l', s))) _ ⟨[], s, rfl, rfl⟩, intros s1 s2 h, rcases h with ⟨l', s, h⟩, rw [h.left, h.right], apply s.cases_on _ (λ a s, _) (λ s, _); repeat {simp [to_list, nil, cons, think, length]}, { refine ⟨a::l', s, _, _⟩; simp }, { refine ⟨l', s, _, _⟩; simp } end @[simp] theorem to_list_cons (a : α) (s) : to_list (cons a s) = (list.cons a <$> to_list s).think := destruct_eq_think $ by unfold to_list; simp; rw to_list'_map; simp; refl @[simp] theorem to_list_nil : to_list (nil : wseq α) = return [] := destruct_eq_ret rfl theorem to_list_of_list (l : list α) : l ∈ to_list (of_list l) := by induction l with a l IH; simp [ret_mem]; exact think_mem (mem_map _ IH) @[simp] theorem destruct_of_seq (s : seq α) : destruct (of_seq s) = return (s.head.map $ λ a, (a, of_seq s.tail)) := destruct_eq_ret $ begin simp [of_seq, head, destruct, seq.destruct, seq.head], rw [show seq.nth (some <$> s) 0 = some <$> seq.nth s 0, by apply seq.map_nth], cases seq.nth s 0 with a, { refl }, unfold functor.map, simp [destruct] end @[simp] theorem head_of_seq (s : seq α) : head (of_seq s) = return s.head := by simp [head]; cases seq.head s; refl @[simp] theorem tail_of_seq (s : seq α) : tail (of_seq s) = of_seq s.tail := begin simp [tail], apply s.cases_on _ (λ x s, _); simp [of_seq], {refl}, rw [seq.head_cons, seq.tail_cons], refl end @[simp] theorem dropn_of_seq (s : seq α) : ∀ n, drop (of_seq s) n = of_seq (s.drop n) | 0 := rfl | (n+1) := by dsimp [drop]; rw [dropn_of_seq, tail_of_seq] theorem nth_of_seq (s : seq α) (n) : nth (of_seq s) n = return (seq.nth s n) := by dsimp [nth]; rw [dropn_of_seq, head_of_seq, seq.head_dropn] instance productive_of_seq (s : seq α) : productive (of_seq s) := ⟨λ n, by rw nth_of_seq; apply_instance⟩ theorem to_seq_of_seq (s : seq α) : to_seq (of_seq s) = s := begin apply subtype.eq, funext n, dsimp [to_seq], apply get_eq_of_mem, rw nth_of_seq, apply ret_mem end /-- The monadic `return a` is a singleton list containing `a`. -/ def ret (a : α) : wseq α := of_list [a] @[simp] theorem map_nil (f : α → β) : map f nil = nil := rfl @[simp] theorem map_cons (f : α → β) (a s) : map f (cons a s) = cons (f a) (map f s) := seq.map_cons _ _ _ @[simp] theorem map_think (f : α → β) (s) : map f (think s) = think (map f s) := seq.map_cons _ _ _ @[simp] theorem map_id (s : wseq α) : map id s = s := by simp [map] @[simp] theorem map_ret (f : α → β) (a) : map f (ret a) = ret (f a) := by simp [ret] @[simp] theorem map_append (f : α → β) (s t) : map f (append s t) = append (map f s) (map f t) := seq.map_append _ _ _ theorem map_comp (f : α → β) (g : β → γ) (s : wseq α) : map (g ∘ f) s = map g (map f s) := begin dsimp [map], rw ←seq.map_comp, apply congr_fun, apply congr_arg, ext ⟨⟩; refl end theorem mem_map (f : α → β) {a : α} {s : wseq α} : a ∈ s → f a ∈ map f s := seq.mem_map (option.map f) -- The converse is not true without additional assumptions theorem exists_of_mem_join {a : α} : ∀ {S : wseq (wseq α)}, a ∈ join S → ∃ s, s ∈ S ∧ a ∈ s := suffices ∀ ss : wseq α, a ∈ ss → ∀ s S, append s (join S) = ss → a ∈ append s (join S) → a ∈ s ∨ ∃ s, s ∈ S ∧ a ∈ s, from λ S h, (this _ h nil S (by simp) (by simp [h])).resolve_left (not_mem_nil _), begin intros ss h, apply mem_rec_on h (λ b ss o, _) (λ ss IH, _); intros s S, { refine s.cases_on (S.cases_on _ (λ s S, _) (λ S, _)) (λ b' s, _) (λ s, _); intros ej m; simp at ej; have := congr_arg seq.destruct ej; simp at this; try {cases this}; try {contradiction}, substs b' ss, simp at m ⊢, cases o with e IH, { simp [e] }, cases m with e m, { simp [e] }, exact or.imp_left or.inr (IH _ _ rfl m) }, { refine s.cases_on (S.cases_on _ (λ s S, _) (λ S, _)) (λ b' s, _) (λ s, _); intros ej m; simp at ej; have := congr_arg seq.destruct ej; simp at this; try { try {have := this.1}, contradiction }; subst ss, { apply or.inr, simp at m ⊢, cases IH s S rfl m with as ex, { exact ⟨s, or.inl rfl, as⟩ }, { rcases ex with ⟨s', sS, as⟩, exact ⟨s', or.inr sS, as⟩ } }, { apply or.inr, simp at m, rcases (IH nil S (by simp) (by simp [m])).resolve_left (not_mem_nil _) with ⟨s, sS, as⟩, exact ⟨s, by simp [sS], as⟩ }, { simp at m IH ⊢, apply IH _ _ rfl m } } end theorem exists_of_mem_bind {s : wseq α} {f : α → wseq β} {b} (h : b ∈ bind s f) : ∃ a ∈ s, b ∈ f a := let ⟨t, tm, bt⟩ := exists_of_mem_join h, ⟨a, as, e⟩ := exists_of_mem_map tm in ⟨a, as, by rwa e⟩ theorem destruct_map (f : α → β) (s : wseq α) : destruct (map f s) = computation.map (option.map (prod.map f (map f))) (destruct s) := begin apply eq_of_bisim (λ c1 c2, ∃ s, c1 = destruct (map f s) ∧ c2 = computation.map (option.map (prod.map f (map f))) (destruct s)), { intros c1 c2 h, cases h with s h, rw [h.left, h.right], apply s.cases_on _ (λ a s, _) (λ s, _); simp, exact ⟨s, rfl, rfl⟩ }, { exact ⟨s, rfl, rfl⟩ } end theorem lift_rel_map {δ} (R : α → β → Prop) (S : γ → δ → Prop) {s1 : wseq α} {s2 : wseq β} {f1 : α → γ} {f2 : β → δ} (h1 : lift_rel R s1 s2) (h2 : ∀ {a b}, R a b → S (f1 a) (f2 b)) : lift_rel S (map f1 s1) (map f2 s2) := ⟨λ s1 s2, ∃ s t, s1 = map f1 s ∧ s2 = map f2 t ∧ lift_rel R s t, ⟨s1, s2, rfl, rfl, h1⟩, λ s1 s2 h, match s1, s2, h with ._, ._, ⟨s, t, rfl, rfl, h⟩ := begin simp [destruct_map], apply computation.lift_rel_map _ _ (lift_rel_destruct h), intros o p h, cases o with a; cases p with b; simp, { cases b; cases h }, { cases a; cases h }, { cases a with a s; cases b with b t, cases h with r h, exact ⟨h2 r, s, rfl, t, rfl, h⟩ } end end⟩ theorem map_congr (f : α → β) {s t : wseq α} (h : s ~ t) : map f s ~ map f t := lift_rel_map _ _ h (λ _ _, congr_arg _) @[simp] def destruct_append.aux (t : wseq α) : option (α × wseq α) → computation (option (α × wseq α)) | none := destruct t | (some (a, s)) := return (some (a, append s t)) theorem destruct_append (s t : wseq α) : destruct (append s t) = (destruct s).bind (destruct_append.aux t) := begin apply eq_of_bisim (λ c1 c2, ∃ s t, c1 = destruct (append s t) ∧ c2 = (destruct s).bind (destruct_append.aux t)) _ ⟨s, t, rfl, rfl⟩, intros c1 c2 h, rcases h with ⟨s, t, h⟩, rw [h.left, h.right], apply s.cases_on _ (λ a s, _) (λ s, _); simp, { apply t.cases_on _ (λ b t, _) (λ t, _); simp, { refine ⟨nil, t, _, _⟩; simp } }, { exact ⟨s, t, rfl, rfl⟩ } end @[simp] def destruct_join.aux : option (wseq α × wseq (wseq α)) → computation (option (α × wseq α)) | none := return none | (some (s, S)) := (destruct (append s (join S))).think theorem destruct_join (S : wseq (wseq α)) : destruct (join S) = (destruct S).bind destruct_join.aux := begin apply eq_of_bisim (λ c1 c2, c1 = c2 ∨ ∃ S, c1 = destruct (join S) ∧ c2 = (destruct S).bind destruct_join.aux) _ (or.inr ⟨S, rfl, rfl⟩), intros c1 c2 h, exact match c1, c2, h with | _, _, (or.inl $ eq.refl c) := by cases c.destruct; simp | _, _, or.inr ⟨S, rfl, rfl⟩ := begin apply S.cases_on _ (λ s S, _) (λ S, _); simp, { refine or.inr ⟨S, rfl, rfl⟩ } end end end theorem lift_rel_append (R : α → β → Prop) {s1 s2 : wseq α} {t1 t2 : wseq β} (h1 : lift_rel R s1 t1) (h2 : lift_rel R s2 t2) : lift_rel R (append s1 s2) (append t1 t2) := ⟨λ s t, lift_rel R s t ∨ ∃ s1 t1, s = append s1 s2 ∧ t = append t1 t2 ∧ lift_rel R s1 t1, or.inr ⟨s1, t1, rfl, rfl, h1⟩, λ s t h, match s, t, h with | s, t, or.inl h := begin apply computation.lift_rel.imp _ _ _ (lift_rel_destruct h), intros a b, apply lift_rel_o.imp_right, intros s t, apply or.inl end | ._, ._, or.inr ⟨s1, t1, rfl, rfl, h⟩ := begin simp [destruct_append], apply computation.lift_rel_bind _ _ (lift_rel_destruct h), intros o p h, cases o with a; cases p with b, { simp, apply computation.lift_rel.imp _ _ _ (lift_rel_destruct h2), intros a b, apply lift_rel_o.imp_right, intros s t, apply or.inl }, { cases b; cases h }, { cases a; cases h }, { cases a with a s; cases b with b t, cases h with r h, simp, exact ⟨r, or.inr ⟨s, rfl, t, rfl, h⟩⟩ } end end⟩ theorem lift_rel_join.lem (R : α → β → Prop) {S T} {U : wseq α → wseq β → Prop} (ST : lift_rel (lift_rel R) S T) (HU : ∀ s1 s2, (∃ s t S T, s1 = append s (join S) ∧ s2 = append t (join T) ∧ lift_rel R s t ∧ lift_rel (lift_rel R) S T) → U s1 s2) {a} (ma : a ∈ destruct (join S)) : ∃ {b}, b ∈ destruct (join T) ∧ lift_rel_o R U a b := begin cases exists_results_of_mem ma with n h, clear ma, revert a S T, apply nat.strong_induction_on n _, intros n IH a S T ST ra, simp [destruct_join] at ra, exact let ⟨o, m, k, rs1, rs2, en⟩ := of_results_bind ra, ⟨p, mT, rop⟩ := computation.exists_of_lift_rel_left (lift_rel_destruct ST) rs1.mem in by exact match o, p, rop, rs1, rs2, mT with | none, none, _, rs1, rs2, mT := by simp only [destruct_join]; exact ⟨none, mem_bind mT (ret_mem _), by rw eq_of_ret_mem rs2.mem; trivial⟩ | some (s, S'), some (t, T'), ⟨st, ST'⟩, rs1, rs2, mT := by simp [destruct_append] at rs2; exact let ⟨k1, rs3, ek⟩ := of_results_think rs2, ⟨o', m1, n1, rs4, rs5, ek1⟩ := of_results_bind rs3, ⟨p', mt, rop'⟩ := computation.exists_of_lift_rel_left (lift_rel_destruct st) rs4.mem in by exact match o', p', rop', rs4, rs5, mt with | none, none, _, rs4, rs5', mt := have n1 < n, begin rw [en, ek, ek1], apply lt_of_lt_of_le _ (nat.le_add_right _ _), apply nat.lt_succ_of_le (nat.le_add_right _ _) end, let ⟨ob, mb, rob⟩ := IH _ this ST' rs5' in by refine ⟨ob, _, rob⟩; { simp [destruct_join], apply mem_bind mT, simp [destruct_append], apply think_mem, apply mem_bind mt, exact mb } | some (a, s'), some (b, t'), ⟨ab, st'⟩, rs4, rs5, mt := begin simp at rs5, refine ⟨some (b, append t' (join T')), _, _⟩, { simp [destruct_join], apply mem_bind mT, simp [destruct_append], apply think_mem, apply mem_bind mt, apply ret_mem }, rw eq_of_ret_mem rs5.mem, exact ⟨ab, HU _ _ ⟨s', t', S', T', rfl, rfl, st', ST'⟩⟩ end end end end theorem lift_rel_join (R : α → β → Prop) {S : wseq (wseq α)} {T : wseq (wseq β)} (h : lift_rel (lift_rel R) S T) : lift_rel R (join S) (join T) := ⟨λ s1 s2, ∃ s t S T, s1 = append s (join S) ∧ s2 = append t (join T) ∧ lift_rel R s t ∧ lift_rel (lift_rel R) S T, ⟨nil, nil, S, T, by simp, by simp, by simp, h⟩, λs1 s2 ⟨s, t, S, T, h1, h2, st, ST⟩, begin clear _fun_match _x, rw [h1, h2], rw [destruct_append, destruct_append], apply computation.lift_rel_bind _ _ (lift_rel_destruct st), exact λ o p h, match o, p, h with | some (a, s), some (b, t), ⟨h1, h2⟩ := by simp; exact ⟨h1, s, t, S, rfl, T, rfl, h2, ST⟩ | none, none, _ := begin dsimp [destruct_append.aux, computation.lift_rel], constructor, { intro, apply lift_rel_join.lem _ ST (λ _ _, id) }, { intros b mb, rw [←lift_rel_o.swap], apply lift_rel_join.lem (swap R), { rw [←lift_rel.swap R, ←lift_rel.swap], apply ST }, { rw [←lift_rel.swap R, ←lift_rel.swap (lift_rel R)], exact λ s1 s2 ⟨s, t, S, T, h1, h2, st, ST⟩, ⟨t, s, T, S, h2, h1, st, ST⟩ }, { exact mb } } end end end⟩ theorem join_congr {S T : wseq (wseq α)} (h : lift_rel equiv S T) : join S ~ join T := lift_rel_join _ h theorem lift_rel_bind {δ} (R : α → β → Prop) (S : γ → δ → Prop) {s1 : wseq α} {s2 : wseq β} {f1 : α → wseq γ} {f2 : β → wseq δ} (h1 : lift_rel R s1 s2) (h2 : ∀ {a b}, R a b → lift_rel S (f1 a) (f2 b)) : lift_rel S (bind s1 f1) (bind s2 f2) := lift_rel_join _ (lift_rel_map _ _ h1 @h2) theorem bind_congr {s1 s2 : wseq α} {f1 f2 : α → wseq β} (h1 : s1 ~ s2) (h2 : ∀ a, f1 a ~ f2 a) : bind s1 f1 ~ bind s2 f2 := lift_rel_bind _ _ h1 (λ a b h, by rw h; apply h2) @[simp] theorem join_ret (s : wseq α) : join (ret s) ~ s := by simp [ret]; apply think_equiv @[simp] theorem join_map_ret (s : wseq α) : join (map ret s) ~ s := begin refine ⟨λ s1 s2, join (map ret s2) = s1, rfl, _⟩, intros s' s h, rw ←h, apply lift_rel_rec (λ c1 c2, ∃ s, c1 = destruct (join (map ret s)) ∧ c2 = destruct s), { exact λ c1 c2 h, match c1, c2, h with | ._, ._, ⟨s, rfl, rfl⟩ := begin clear h _match, have : ∀ s, ∃ s' : wseq α, (map ret s).join.destruct = (map ret s').join.destruct ∧ destruct s = s'.destruct, from λ s, ⟨s, rfl, rfl⟩, apply s.cases_on _ (λ a s, _) (λ s, _); simp [ret, ret_mem, this, option.exists] end end }, { exact ⟨s, rfl, rfl⟩ } end @[simp] theorem join_append (S T : wseq (wseq α)) : join (append S T) ~ append (join S) (join T) := begin refine ⟨λ s1 s2, ∃ s S T, s1 = append s (join (append S T)) ∧ s2 = append s (append (join S) (join T)), ⟨nil, S, T, by simp, by simp⟩, _⟩, intros s1 s2 h, apply lift_rel_rec (λ c1 c2, ∃ (s : wseq α) S T, c1 = destruct (append s (join (append S T))) ∧ c2 = destruct (append s (append (join S) (join T)))) _ _ _ (let ⟨s, S, T, h1, h2⟩ := h in ⟨s, S, T, congr_arg destruct h1, congr_arg destruct h2⟩), intros c1 c2 h, exact match c1, c2, h with ._, ._, ⟨s, S, T, rfl, rfl⟩ := begin clear _match h h, apply wseq.cases_on s _ (λ a s, _) (λ s, _); simp, { apply wseq.cases_on S _ (λ s S, _) (λ S, _); simp, { apply wseq.cases_on T _ (λ s T, _) (λ T, _); simp, { refine ⟨s, nil, T, _, _⟩; simp }, { refine ⟨nil, nil, T, _, _⟩; simp } }, { exact ⟨s, S, T, rfl, rfl⟩ }, { refine ⟨nil, S, T, _, _⟩; simp } }, { exact ⟨s, S, T, rfl, rfl⟩ }, { exact ⟨s, S, T, rfl, rfl⟩ } end end end @[simp] theorem bind_ret (f : α → β) (s) : bind s (ret ∘ f) ~ map f s := begin dsimp [bind], change (λx, ret (f x)) with (ret ∘ f), rw [map_comp], apply join_map_ret end @[simp] theorem ret_bind (a : α) (f : α → wseq β) : bind (ret a) f ~ f a := by simp [bind] @[simp] theorem map_join (f : α → β) (S) : map f (join S) = join (map (map f) S) := begin apply seq.eq_of_bisim (λs1 s2, ∃ s S, s1 = append s (map f (join S)) ∧ s2 = append s (join (map (map f) S))), { intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, S, rfl, rfl⟩ := begin apply wseq.cases_on s _ (λ a s, _) (λ s, _); simp, { apply wseq.cases_on S _ (λ s S, _) (λ S, _); simp, { exact ⟨map f s, S, rfl, rfl⟩ }, { refine ⟨nil, S, _, _⟩; simp } }, { exact ⟨_, _, rfl, rfl⟩ }, { exact ⟨_, _, rfl, rfl⟩ } end end }, { refine ⟨nil, S, _, _⟩; simp } end @[simp] theorem join_join (SS : wseq (wseq (wseq α))) : join (join SS) ~ join (map join SS) := begin refine ⟨λ s1 s2, ∃ s S SS, s1 = append s (join (append S (join SS))) ∧ s2 = append s (append (join S) (join (map join SS))), ⟨nil, nil, SS, by simp, by simp⟩, _⟩, intros s1 s2 h, apply lift_rel_rec (λ c1 c2, ∃ s S SS, c1 = destruct (append s (join (append S (join SS)))) ∧ c2 = destruct (append s (append (join S) (join (map join SS))))) _ (destruct s1) (destruct s2) (let ⟨s, S, SS, h1, h2⟩ := h in ⟨s, S, SS, by simp [h1], by simp [h2]⟩), intros c1 c2 h, exact match c1, c2, h with ._, ._, ⟨s, S, SS, rfl, rfl⟩ := begin clear _match h h, apply wseq.cases_on s _ (λ a s, _) (λ s, _); simp, { apply wseq.cases_on S _ (λ s S, _) (λ S, _); simp, { apply wseq.cases_on SS _ (λ S SS, _) (λ SS, _); simp, { refine ⟨nil, S, SS, _, _⟩; simp }, { refine ⟨nil, nil, SS, _, _⟩; simp } }, { exact ⟨s, S, SS, rfl, rfl⟩ }, { refine ⟨nil, S, SS, _, _⟩; simp } }, { exact ⟨s, S, SS, rfl, rfl⟩ }, { exact ⟨s, S, SS, rfl, rfl⟩ } end end end @[simp] theorem bind_assoc (s : wseq α) (f : α → wseq β) (g : β → wseq γ) : bind (bind s f) g ~ bind s (λ (x : α), bind (f x) g) := begin simp [bind], rw [← map_comp f (map g), map_comp (map g ∘ f) join], apply join_join end instance : monad wseq := { map := @map, pure := @ret, bind := @bind } /- Unfortunately, wseq is not a lawful monad, because it does not satisfy the monad laws exactly, only up to sequence equivalence. Furthermore, even quotienting by the equivalence is not sufficient, because the join operation involves lists of quotient elements, with a lifted equivalence relation, and pure quotients cannot handle this type of construction. instance : is_lawful_monad wseq := { id_map := @map_id, bind_pure_comp_eq_map := @bind_ret, pure_bind := @ret_bind, bind_assoc := @bind_assoc } -/ end wseq
2da2336e020dc5e6568e50cd550bf7092b421770
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/topology/algebra/quotient.lean
c60edc9ff7a9550a76145c93494abe1b83d5681c
[ "Apache-2.0" ]
permissive
johoelzl/mathlib
253f46daa30b644d011e8e119025b01ad69735c4
592e3c7a2dfbd5826919b4605559d35d4d75938f
refs/heads/master
1,625,657,216,488
1,551,374,946,000
1,551,374,946,000
98,915,829
0
0
Apache-2.0
1,522,917,267,000
1,501,524,499,000
Lean
UTF-8
Lean
false
false
8,280
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 Topological structures on quotient rings and groups. -/ import topology.uniform_space.completion import group_theory.quotient_group @[simp] lemma {u} eq_mpr_heq {α β : Sort u} (h : β = α) (x : α) : eq.mpr h x == x := by subst h; refl open function set variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} -- variables [topological_space β] [topological_space γ] [topological_space δ] namespace quot protected def map {α} (r r' : α → α → Prop) (h : ∀a b, r a b → r' a b) (a : quot r) : quot r' := quot.hrec_on a (quot.mk r') $ assume a b hab, by rw [quot.sound (h a b hab)] /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr {α} {r r' : α → α → Prop} (eq : ∀a b, r a b ↔ r' a b) : quot r ≃ quot r' := ⟨quot.map r r' (assume a b, (eq a b).1), quot.map r' r (assume a b, (eq a b).2), by rintros ⟨a⟩; refl, by rintros ⟨a⟩; refl⟩ end quot namespace quotient protected def congr {α} {r r' : setoid α} (eq : ∀a b, @setoid.r α r a b ↔ @setoid.r α r' a b) : quotient r ≃ quotient r' := quot.congr eq end quotient /- namespace comm_ring open equiv protected def map {α β} (e : α ≃ β) (r : comm_ring α) : comm_ring β := { zero := e r.zero, one := e r.one, neg := arrow_congr e e r.neg, add := arrow_congr e (arrow_congr e e) r.add, mul := arrow_congr e (arrow_congr e e) r.mul } def comm_ring_congr {α β} (e : α ≃ β) : comm_ring α ≃ comm_ring β := {} end comm_ring -/ section topological_group variables [topological_space α] [group α] [topological_group α] (N : set α) [normal_subgroup N] @[to_additive quotient_add_group.quotient.topological_space] instance : topological_space (quotient_group.quotient N) := by dunfold quotient_group.quotient; apply_instance attribute [instance] quotient_add_group.quotient.topological_space open quotient_group @[to_additive quotient_add_group_saturate] lemma quotient_group_saturate (s : set α) : (coe : α → quotient N) ⁻¹' ((coe : α → quotient N) '' s) = (⋃ x : N, (λ y, y*x.1) '' s) := begin ext x, simp only [mem_preimage_eq, mem_image, mem_Union, quotient_group.eq], split, { exact assume ⟨a, a_in, h⟩, ⟨⟨_, h⟩, a, a_in, mul_inv_cancel_left _ _⟩ }, { exact assume ⟨⟨i, hi⟩, a, ha, eq⟩, ⟨a, ha, by simp only [eq.symm, (mul_assoc _ _ _).symm, inv_mul_cancel_left, hi]⟩ } end @[to_additive quotient_add_group.open_coe] lemma quotient_group.open_coe : is_open_map (coe : α → quotient N) := begin intros s s_op, change is_open ((coe : α → quotient N) ⁻¹' (coe '' s)), rw quotient_group_saturate N s, apply is_open_Union, rintro ⟨n, _⟩, exact is_open_map_mul_right n s s_op end @[to_additive topological_add_group_quotient] instance topological_group_quotient : topological_group (quotient N) := { continuous_mul := begin have cont : continuous ((coe : α → quotient N) ∘ (λ (p : α × α), p.fst * p.snd)) := continuous.comp continuous_mul' continuous_quot_mk, have quot : quotient_map (λ p : α × α, ((p.1:quotient N), (p.2:quotient N))), { apply is_open_map.to_quotient_map, { exact is_open_map.prod (quotient_group.open_coe N) (quotient_group.open_coe N) }, { apply continuous.prod_mk, { exact continuous.comp continuous_fst continuous_quot_mk }, { exact continuous.comp continuous_snd continuous_quot_mk } }, { rintro ⟨⟨x⟩, ⟨y⟩⟩, exact ⟨(x, y), rfl⟩ } }, exact (quotient_map.continuous_iff quot).2 cont, end, continuous_inv := begin apply continuous_quotient_lift, change continuous ((coe : α → quotient N) ∘ (λ (a : α), a⁻¹)), exact continuous.comp continuous_inv' continuous_quot_mk end } attribute [instance] topological_add_group_quotient end topological_group section ideal_is_add_subgroup variables [comm_ring α] {M : Type*} [add_comm_group M] [module α M] (N : submodule α M) instance submodule_is_add_subgroup : is_add_subgroup ↑N := { zero_mem := N.zero, add_mem := N.add, neg_mem := λ _, N.neg_mem } end ideal_is_add_subgroup @[to_additive normal_add_subgroup_of_comm] instance normal_subgroup_of_comm [comm_group α] (s : set α) [hs : is_subgroup s] : normal_subgroup s := { normal := assume a hn b, by rwa [mul_comm b, mul_inv_cancel_right] } attribute [instance] normal_add_subgroup_of_comm section topological_ring variables [topological_space α] [comm_ring α] [topological_ring α] (N : ideal α) open ideal.quotient instance topological_ring_quotient_topology : topological_space N.quotient := by dunfold ideal.quotient submodule.quotient; apply_instance -- this should be quotient_add_saturate ... lemma quotient_ring_saturate (s : set α) : mk N ⁻¹' (mk N '' s) = (⋃ x : N, (λ y, x.1 + y) '' s) := begin ext x, simp only [mem_preimage_eq, mem_image, mem_Union, ideal.quotient.eq], split, { exact assume ⟨a, a_in, h⟩, ⟨⟨_, N.neg_mem h⟩, a, a_in, by simp⟩ }, { exact assume ⟨⟨i, hi⟩, a, ha, eq⟩, ⟨a, ha, by rw [← eq, sub_add_eq_sub_sub_swap, sub_self, zero_sub]; exact N.neg_mem hi⟩ } end lemma quotient_ring.is_open_map_coe : is_open_map (mk N) := begin assume s s_op, show is_open (mk N ⁻¹' (mk N '' s)), rw quotient_ring_saturate N s, exact is_open_Union (assume ⟨n, _⟩, is_open_map_add_left n s s_op) end lemma quotient_ring.quotient_map_coe_coe : quotient_map (λ p : α × α, (mk N p.1, mk N p.2)) := begin apply is_open_map.to_quotient_map, { exact is_open_map.prod (quotient_ring.is_open_map_coe N) (quotient_ring.is_open_map_coe N) }, { apply continuous.prod_mk, { exact continuous.comp continuous_fst continuous_quot_mk }, { exact continuous.comp continuous_snd continuous_quot_mk } }, { rintro ⟨⟨x⟩, ⟨y⟩⟩, exact ⟨(x, y), rfl⟩ } end instance topological_ring_quotient : topological_ring N.quotient := { continuous_add := have cont : continuous (mk N ∘ (λ (p : α × α), p.fst + p.snd)) := continuous.comp continuous_add' continuous_quot_mk, (quotient_map.continuous_iff (quotient_ring.quotient_map_coe_coe N)).2 cont, continuous_neg := continuous_quotient_lift _ (continuous.comp continuous_neg' continuous_quot_mk), continuous_mul := have cont : continuous (mk N ∘ (λ (p : α × α), p.fst * p.snd)) := continuous.comp continuous_mul' continuous_quot_mk, (quotient_map.continuous_iff (quotient_ring.quotient_map_coe_coe N)).2 cont } end topological_ring namespace uniform_space lemma ring_sep_rel (α) [comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] : separation_setoid α = submodule.quotient_rel (ideal.closure ⊥) := setoid.ext $ assume x y, group_separation_rel x y lemma ring_sep_quot (α) [r : comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] : quotient (separation_setoid α) = (⊥ : ideal α).closure.quotient := by rw [@ring_sep_rel α r]; refl def sep_quot_equiv_ring_quot (α) [r : comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] : quotient (separation_setoid α) ≃ (⊥ : ideal α).closure.quotient := quotient.congr $ assume x y, group_separation_rel x y /- TODO: use a form of transport a.k.a. lift definition a.k.a. transfer -/ instance [comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] : comm_ring (quotient (separation_setoid α)) := by rw ring_sep_quot α; apply_instance instance [comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] : topological_ring (quotient (separation_setoid α)) := begin convert topological_ring_quotient (⊥ : ideal α).closure, { apply ring_sep_rel }, { dsimp [topological_ring_quotient_topology, quotient.topological_space, to_topological_space], congr, apply ring_sep_rel, apply ring_sep_rel }, { apply ring_sep_rel }, { simp [uniform_space.comm_ring] }, end end uniform_space
f9731ba467a37af215a5ec1e99e389ca8ec83ea2
7f15a2b0775be8a2fcb4936c6b0f127f683c4666
/library/init/meta/tactic.lean
4cc3291318212a8b2e24d45ced563b94dc8dc2eb
[ "Apache-2.0" ]
permissive
mwillsey/lean
0b90b5016a3158490d7c9cf8b836e8fe7bb3df81
8621be63353b299b0da9106f4e7c8ca5e271941d
refs/heads/master
1,630,587,469,685
1,514,924,859,000
1,514,925,180,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
50,842
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.function init.data.option.basic init.util import init.category.combinators init.category.monad init.category.alternative init.category.monad_fail import init.data.nat.div init.meta.exceptional init.meta.format init.meta.environment import init.meta.pexpr init.data.repr init.data.string.basic init.meta.interaction_monad meta constant tactic_state : Type universes u v namespace tactic_state /-- Create a tactic state with an empty local context and a dummy goal. -/ meta constant mk_empty : environment → options → tactic_state meta constant env : tactic_state → environment /-- Format the given tactic state. If `target_lhs_only` is true and the target is of the form `lhs ~ rhs`, where `~` is a simplification relation, then only the `lhs` is displayed. Remark: the parameter `target_lhs_only` is a temporary hack used to implement the `conv` monad. It will be removed in the future. -/ meta constant to_format (s : tactic_state) (target_lhs_only : bool := ff) : format /-- Format expression with respect to the main goal in the tactic state. If the tactic state does not contain any goals, then format expression using an empty local context. -/ meta constant format_expr : tactic_state → expr → format meta constant get_options : tactic_state → options meta constant set_options : tactic_state → options → tactic_state end tactic_state meta instance : has_to_format tactic_state := ⟨tactic_state.to_format⟩ meta instance : has_to_string tactic_state := ⟨λ s, (to_fmt s).to_string s.get_options⟩ @[reducible] meta def tactic := interaction_monad tactic_state @[reducible] meta def tactic_result := interaction_monad.result tactic_state namespace tactic export interaction_monad (hiding failed fail) meta def failed {α : Type} : tactic α := interaction_monad.failed meta def fail {α : Type u} {β : Type v} [has_to_format β] (msg : β) : tactic α := interaction_monad.fail msg end tactic namespace tactic_result export interaction_monad.result end tactic_result open tactic open tactic_result infixl ` >>=[tactic] `:2 := interaction_monad_bind infixl ` >>[tactic] `:2 := interaction_monad_seq meta instance : alternative tactic := { failure := @interaction_monad.failed _, orelse := @interaction_monad_orelse _, ..interaction_monad.monad } meta def {u₁ u₂} tactic.up {α : Type u₂} (t : tactic α) : tactic (ulift.{u₁} α) := λ s, match t s with | success a s' := success (ulift.up a) s' | exception t ref s := exception t ref s end meta def {u₁ u₂} tactic.down {α : Type u₂} (t : tactic (ulift.{u₁} α)) : tactic α := λ s, match t s with | success (ulift.up a) s' := success a s' | exception t ref s := exception t ref s end namespace tactic variables {α : Type u} meta def try_core (t : tactic α) : tactic (option α) := λ s, result.cases_on (t s) (λ a, success (some a)) (λ e ref s', success none s) meta def skip : tactic unit := success () meta def try (t : tactic α) : tactic unit := try_core t >>[tactic] skip meta def try_lst : list (tactic unit) → tactic unit | [] := failed | (tac :: tacs) := λ s, match tac s with | result.success _ s' := try (try_lst tacs) s' | result.exception e p s' := match try_lst tacs s' with | result.exception _ _ _ := result.exception e p s' | r := r end end meta def fail_if_success {α : Type u} (t : tactic α) : tactic unit := λ s, result.cases_on (t s) (λ a s, mk_exception "fail_if_success combinator failed, given tactic succeeded" none s) (λ e ref s', success () s) meta def success_if_fail {α : Type u} (t : tactic α) : tactic unit := λ s, match t s with | (interaction_monad.result.exception _ _ s') := success () s | (interaction_monad.result.success a s) := mk_exception "success_if_fail combinator failed, given tactic succeeded" none s end open nat /-- (iterate_at_most n t): repeat the given tactic at most n times or until t fails -/ meta def iterate_at_most : nat → tactic unit → tactic unit | 0 t := skip | (succ n) t := (do t, iterate_at_most n t) <|> skip /-- (iterate_exactly n t) : execute t n times -/ meta def iterate_exactly : nat → tactic unit → tactic unit | 0 t := skip | (succ n) t := do t, iterate_exactly n t meta def iterate : tactic unit → tactic unit := iterate_at_most 100000 meta def returnopt (e : option α) : tactic α := λ s, match e with | (some a) := success a s | none := mk_exception "failed" none s end meta instance opt_to_tac : has_coe (option α) (tactic α) := ⟨returnopt⟩ /-- Decorate t's exceptions with msg -/ meta def decorate_ex (msg : format) (t : tactic α) : tactic α := λ s, result.cases_on (t s) success (λ opt_thunk, match opt_thunk with | some e := exception (some (λ u, msg ++ format.nest 2 (format.line ++ e u))) | none := exception none end) @[inline] meta def write (s' : tactic_state) : tactic unit := λ s, success () s' @[inline] meta def read : tactic tactic_state := λ s, success s s meta def get_options : tactic options := do s ← read, return s.get_options meta def set_options (o : options) : tactic unit := do s ← read, write (s.set_options o) meta def save_options {α : Type} (t : tactic α) : tactic α := do o ← get_options, a ← t, set_options o, return a meta def returnex {α : Type} (e : exceptional α) : tactic α := λ s, match e with | exceptional.success a := success a s | exceptional.exception ._ f := match get_options s with | success opt _ := exception (some (λ u, f opt)) none s | exception _ _ _ := exception (some (λ u, f options.mk)) none s end end meta instance ex_to_tac {α : Type} : has_coe (exceptional α) (tactic α) := ⟨returnex⟩ end tactic meta def tactic_format_expr (e : expr) : tactic format := do s ← tactic.read, return (tactic_state.format_expr s e) meta class has_to_tactic_format (α : Type u) := (to_tactic_format : α → tactic format) meta instance : has_to_tactic_format expr := ⟨tactic_format_expr⟩ meta def tactic.pp {α : Type u} [has_to_tactic_format α] : α → tactic format := has_to_tactic_format.to_tactic_format open tactic format meta instance {α : Type u} [has_to_tactic_format α] : has_to_tactic_format (list α) := ⟨has_map.map to_fmt ∘ monad.mapm pp⟩ meta instance (α : Type u) (β : Type v) [has_to_tactic_format α] [has_to_tactic_format β] : has_to_tactic_format (α × β) := ⟨λ ⟨a, b⟩, to_fmt <$> (prod.mk <$> pp a <*> pp b)⟩ meta def option_to_tactic_format {α : Type u} [has_to_tactic_format α] : option α → tactic format | (some a) := do fa ← pp a, return (to_fmt "(some " ++ fa ++ ")") | none := return "none" meta instance {α : Type u} [has_to_tactic_format α] : has_to_tactic_format (option α) := ⟨option_to_tactic_format⟩ meta instance {α} (a : α) : has_to_tactic_format (reflected a) := ⟨λ h, pp h.to_expr⟩ @[priority 10] meta instance has_to_format_to_has_to_tactic_format (α : Type) [has_to_format α] : has_to_tactic_format α := ⟨(λ x, return x) ∘ to_fmt⟩ namespace tactic open tactic_state meta def get_env : tactic environment := do s ← read, return $ env s meta def get_decl (n : name) : tactic declaration := do s ← read, (env s).get n meta def trace {α : Type u} [has_to_tactic_format α] (a : α) : tactic unit := do fmt ← pp a, return $ _root_.trace_fmt fmt (λ u, ()) meta def trace_call_stack : tactic unit := assume state, _root_.trace_call_stack (success () state) meta def timetac {α : Type u} (desc : string) (t : thunk (tactic α)) : tactic α := λ s, timeit desc (t () s) meta def trace_state : tactic unit := do s ← read, trace $ to_fmt s inductive transparency | all | semireducible | instances | reducible | none export transparency (reducible semireducible) /-- (eval_expr α e) evaluates 'e' IF 'e' has type 'α'. -/ meta constant eval_expr (α : Type u) [reflected α] : expr → tactic α /-- Return the partial term/proof constructed so far. Note that the resultant expression may contain variables that are not declarate in the current main goal. -/ meta constant result : tactic expr /-- Display the partial term/proof constructed so far. This tactic is *not* equivalent to `do { r ← result, s ← read, return (format_expr s r) }` because this one will format the result with respect to the current goal, and trace_result will do it with respect to the initial goal. -/ meta constant format_result : tactic format /-- Return target type of the main goal. Fail if tactic_state does not have any goal left. -/ meta constant target : tactic expr meta constant intro_core : name → tactic expr meta constant intron : nat → tactic unit /-- Clear the given local constant. The tactic fails if the given expression is not a local constant. -/ meta constant clear : expr → tactic unit meta constant revert_lst : list expr → tactic nat /-- Return `e` in weak head normal form with respect to the given transparency setting. If `unfold_ginductive` is `tt`, then nested and/or mutually recursive inductive datatype constructors and types are unfolded. Recall that nested and mutually recursive inductive datatype declarations are compiled into primitive datatypes accepted by the Kernel. -/ meta constant whnf (e : expr) (md := semireducible) (unfold_ginductive := tt) : tactic expr /-- (head) eta expand the given expression -/ meta constant head_eta_expand : expr → tactic expr /-- (head) beta reduction -/ meta constant head_beta : expr → tactic expr /-- (head) zeta reduction -/ meta constant head_zeta : expr → tactic expr /-- zeta reduction -/ meta constant zeta : expr → tactic expr /-- (head) eta reduction -/ meta constant head_eta : expr → tactic expr /-- Succeeds if `t` and `s` can be unified using the given transparency setting. -/ meta constant unify (t s : expr) (md := semireducible) : tactic unit /-- Similar to `unify`, but it treats metavariables as constants. -/ meta constant is_def_eq (t s : expr) (md := semireducible) : tactic unit /-- Infer the type of the given expression. Remark: transparency does not affect type inference -/ meta constant infer_type : expr → tactic expr meta constant get_local : name → tactic expr /-- Resolve a name using the current local context, environment, aliases, etc. -/ meta constant resolve_name : name → tactic pexpr /-- Return the hypothesis in the main goal. Fail if tactic_state does not have any goal left. -/ meta constant local_context : tactic (list expr) meta constant get_unused_name (n : name) (i : option nat := none) : tactic name /-- Helper tactic for creating simple applications where some arguments are inferred using type inference. Example, given ``` rel.{l_1 l_2} : Pi (α : Type.{l_1}) (β : α -> Type.{l_2}), (Pi x : α, β x) -> (Pi x : α, β x) -> , Prop nat : Type real : Type vec.{l} : Pi (α : Type l) (n : nat), Type.{l1} f g : Pi (n : nat), vec real n ``` then ``` mk_app_core semireducible "rel" [f, g] ``` returns the application ``` rel.{1 2} nat (fun n : nat, vec real n) f g ``` The unification constraints due to type inference are solved using the transparency `md`. -/ meta constant mk_app (fn : name) (args : list expr) (md := semireducible) : tactic expr /-- Similar to `mk_app`, but allows to specify which arguments are explicit/implicit. Example, given `(a b : nat)` then ``` mk_mapp "ite" [some (a > b), none, none, some a, some b] ``` returns the application ``` @ite.{1} (a > b) (nat.decidable_gt a b) nat a b ``` -/ meta constant mk_mapp (fn : name) (args : list (option expr)) (md := semireducible) : tactic expr /-- (mk_congr_arg h₁ h₂) is a more efficient version of (mk_app `congr_arg [h₁, h₂]) -/ meta constant mk_congr_arg : expr → expr → tactic expr /-- (mk_congr_fun h₁ h₂) is a more efficient version of (mk_app `congr_fun [h₁, h₂]) -/ meta constant mk_congr_fun : expr → expr → tactic expr /-- (mk_congr h₁ h₂) is a more efficient version of (mk_app `congr [h₁, h₂]) -/ meta constant mk_congr : expr → expr → tactic expr /-- (mk_eq_refl h) is a more efficient version of (mk_app `eq.refl [h]) -/ meta constant mk_eq_refl : expr → tactic expr /-- (mk_eq_symm h) is a more efficient version of (mk_app `eq.symm [h]) -/ meta constant mk_eq_symm : expr → tactic expr /-- (mk_eq_trans h₁ h₂) is a more efficient version of (mk_app `eq.trans [h₁, h₂]) -/ meta constant mk_eq_trans : expr → expr → tactic expr /-- (mk_eq_mp h₁ h₂) is a more efficient version of (mk_app `eq.mp [h₁, h₂]) -/ meta constant mk_eq_mp : expr → expr → tactic expr /-- (mk_eq_mpr h₁ h₂) is a more efficient version of (mk_app `eq.mpr [h₁, h₂]) -/ meta constant mk_eq_mpr : expr → expr → tactic expr /- Given a local constant t, if t has type (lhs = rhs) apply substitution. Otherwise, try to find a local constant that has type of the form (t = t') or (t' = t). The tactic fails if the given expression is not a local constant. -/ meta constant subst : expr → tactic unit /-- Close the current goal using `e`. Fail is the type of `e` is not definitionally equal to the target type. -/ meta constant exact (e : expr) (md := semireducible) : tactic unit /-- Elaborate the given quoted expression with respect to the current main goal. If `allow_mvars` is tt, then metavariables are tolerated and become new goals if `subgoals` is tt. -/ meta constant to_expr (q : pexpr) (allow_mvars := tt) (subgoals := tt) : tactic expr /-- Return true if the given expression is a type class. -/ meta constant is_class : expr → tactic bool /-- Try to create an instance of the given type class. -/ meta constant mk_instance : expr → tactic expr /-- Change the target of the main goal. The input expression must be definitionally equal to the current target. If `check` is `ff`, then the tactic does not check whether `e` is definitionally equal to the current target. If it is not, then the error will only be detected by the kernel type checker. -/ meta constant change (e : expr) (check : bool := tt): tactic unit /-- `assert_core H T`, adds a new goal for T, and change target to `T -> target`. -/ meta constant assert_core : name → expr → tactic unit /-- `assertv_core H T P`, change target to (T -> target) if P has type T. -/ meta constant assertv_core : name → expr → expr → tactic unit /-- `define_core H T`, adds a new goal for T, and change target to `let H : T := ?M in target` in the current goal. -/ meta constant define_core : name → expr → tactic unit /-- `definev_core H T P`, change target to `let H : T := P in target` if P has type T. -/ meta constant definev_core : name → expr → expr → tactic unit /-- rotate goals to the left -/ meta constant rotate_left : nat → tactic unit meta constant get_goals : tactic (list expr) meta constant set_goals : list expr → tactic unit inductive new_goals | non_dep_first | non_dep_only | all /-- Configuration options for the `apply` tactic. -/ structure apply_cfg := (md := semireducible) (approx := tt) (new_goals := new_goals.non_dep_first) (instances := tt) (auto_param := tt) (opt_param := tt) /-- Apply the expression `e` to the main goal, the unification is performed using the transparency mode in `cfg`. If `cfg.approx` is `tt`, then fallback to first-order unification, and approximate context during unification. `cfg.new_goals` specifies which unassigned metavariables become new goals, and their order. If `cfg.instances` is `tt`, then use type class resolution to instantiate unassigned meta-variables. The fields `cfg.auto_param` and `cfg.opt_param` are ignored by this tactic (See `tactic.apply`). It returns a list of all introduced meta variables and the parameter name associated with them, even the assigned ones. -/ meta constant apply_core (e : expr) (cfg : apply_cfg := {}) : tactic (list (name × expr)) /- Create a fresh meta universe variable. -/ meta constant mk_meta_univ : tactic level /- Create a fresh meta-variable with the given type. The scope of the new meta-variable is the local context of the main goal. -/ meta constant mk_meta_var : expr → tactic expr /-- Return the value assigned to the given universe meta-variable. Fail if argument is not an universe meta-variable or if it is not assigned. -/ meta constant get_univ_assignment : level → tactic level /-- Return the value assigned to the given meta-variable. Fail if argument is not a meta-variable or if it is not assigned. -/ meta constant get_assignment : expr → tactic expr /-- Return true if the given meta-variable is assigned. Fail if argument is not a meta-variable. -/ meta constant is_assigned : expr → tactic bool meta constant mk_fresh_name : tactic name /-- Return a hash code for expr that ignores inst_implicit arguments, and proofs. -/ meta constant abstract_hash : expr → tactic nat /-- Return the "weight" of the given expr while ignoring inst_implicit arguments, and proofs. -/ meta constant abstract_weight : expr → tactic nat meta constant abstract_eq : expr → expr → tactic bool /-- Induction on `h` using recursor `rec`, names for the new hypotheses are retrieved from `ns`. If `ns` does not have sufficient names, then use the internal binder names in the recursor. It returns for each new goal the name of the constructor (if `rec_name` is a builtin recursor), a list of new hypotheses, and a list of substitutions for hypotheses depending on `h`. The substitutions map internal names to their replacement terms. If the replacement is again a hypothesis the user name stays the same. The internal names are only valid in the original goal, not in the type context of the new goal. Remark: if `rec_name` is not a builtin recursor, we use parameter names of `rec_name` instead of constructor names. If `rec` is none, then the type of `h` is inferred, if it is of the form `C ...`, tactic uses `C.rec` -/ meta constant induction (h : expr) (ns : list name := []) (rec : option name := none) (md := semireducible) : tactic (list (name × list expr × list (name × expr))) /-- Apply `cases_on` recursor, names for the new hypotheses are retrieved from `ns`. `h` must be a local constant. It returns for each new goal the name of the constructor, a list of new hypotheses, and a list of substitutions for hypotheses depending on `h`. The number of new goals may be smaller than the number of constructors. Some goals may be discarded when the indices to not match. See `induction` for information on the list of substitutions. The `cases` tactic is implemented using this one, and it relaxes the restriction of `h`. -/ meta constant cases_core (h : expr) (ns : list name := []) (md := semireducible) : tactic (list (name × list expr × list (name × expr))) /-- Similar to cases tactic, but does not revert/intro/clear hypotheses. -/ meta constant destruct (e : expr) (md := semireducible) : tactic unit /-- Generalizes the target with respect to `e`. -/ meta constant generalize (e : expr) (n : name := `_x) (md := semireducible) : tactic unit /-- instantiate assigned metavariables in the given expression -/ meta constant instantiate_mvars : expr → tactic expr /-- Add the given declaration to the environment -/ meta constant add_decl : declaration → tactic unit /-- Changes the environment to the `new_env`. `new_env` needs to be a descendant from the current environment. -/ meta constant set_env : environment → tactic unit /-- (doc_string env d k) return the doc string for d (if available) -/ meta constant doc_string : name → tactic string meta constant add_doc_string : name → string → tactic unit /-- Create an auxiliary definition with name `c` where `type` and `value` may contain local constants and meta-variables. This function collects all dependencies (universe parameters, universe metavariables, local constants (aka hypotheses) and metavariables). It updates the environment in the tactic_state, and returns an expression of the form (c.{l_1 ... l_n} a_1 ... a_m) where l_i's and a_j's are the collected dependencies. -/ meta constant add_aux_decl (c : name) (type : expr) (val : expr) (is_lemma : bool) : tactic expr meta constant module_doc_strings : tactic (list (option name × string)) /-- Set attribute `attr_name` for constant `c_name` with the given priority. If the priority is none, then use default -/ meta constant set_basic_attribute (attr_name : name) (c_name : name) (persistent := ff) (prio : option nat := none) : tactic unit /-- `unset_attribute attr_name c_name` -/ meta constant unset_attribute : name → name → tactic unit /-- `has_attribute attr_name c_name` succeeds if the declaration `decl_name` has the attribute `attr_name`. The result is the priority. -/ meta constant has_attribute : name → name → tactic nat /-- `copy_attribute attr_name c_name d_name` copy attribute `attr_name` from `src` to `tgt` if it is defined for `src` -/ meta def copy_attribute (attr_name : name) (src : name) (p : bool) (tgt : name) : tactic unit := try $ do prio ← has_attribute attr_name src, set_basic_attribute attr_name tgt p (some prio) /-- Name of the declaration currently being elaborated. -/ meta constant decl_name : tactic name /-- `save_type_info e ref` save (typeof e) at position associated with ref -/ meta constant save_type_info {elab : bool} : expr → expr elab → tactic unit meta constant save_info_thunk : pos → (unit → format) → tactic unit /-- Return list of currently open namespaces -/ meta constant open_namespaces : tactic (list name) /-- Return tt iff `t` "occurs" in `e`. The occurrence checking is performed using keyed matching with the given transparency setting. We say `t` occurs in `e` by keyed matching iff there is a subterm `s` s.t. `t` and `s` have the same head, and `is_def_eq t s md` The main idea is to minimize the number of `is_def_eq` checks performed. -/ meta constant kdepends_on (e t : expr) (md := reducible) : tactic bool /-- Abstracts all occurrences of the term `t` in `e` using keyed matching. -/ meta constant kabstract (e t : expr) (md := reducible) : tactic expr /-- Blocks the execution of the current thread for at least `msecs` milliseconds. This tactic is used mainly for debugging purposes. -/ meta constant sleep (msecs : nat) : tactic unit /-- Type check `e` with respect to the current goal. Fails if `e` is not type correct. -/ meta constant type_check (e : expr) (md := semireducible) : tactic unit open list nat /-- Goals can be tagged using a list of names. -/ def tag : Type := list name /-- Enable/disable goal tagging -/ meta constant enable_tags (b : bool) : tactic unit /-- Return tt iff goal tagging is enabled. -/ meta constant tags_enabled : tactic bool /-- Tag goal `g` with tag `t`. It does nothing is goal tagging is disabled. Remark: `set_goal g []` removes the tag -/ meta constant set_tag (g : expr) (t : tag) : tactic unit /-- Return tag associated with `g`. Return `[]` if there is no tag. -/ meta constant get_tag (g : expr) : tactic tag meta def induction' (h : expr) (ns : list name := []) (rec : option name := none) (md := semireducible) : tactic unit := induction h ns rec md >> return () /-- Remark: set_goals will erase any solved goal -/ meta def cleanup : tactic unit := get_goals >>= set_goals /-- Auxiliary definition used to implement begin ... end blocks -/ meta def step {α : Type u} (t : tactic α) : tactic unit := t >>[tactic] cleanup meta def istep {α : Type u} (line0 col0 : ℕ) (line col : ℕ) (t : tactic α) : tactic unit := λ s, (@scope_trace _ line col (λ _, step t s)).clamp_pos line0 line col meta def is_prop (e : expr) : tactic bool := do t ← infer_type e, return (t = `(Prop)) /-- Return true iff n is the name of declaration that is a proposition. -/ meta def is_prop_decl (n : name) : tactic bool := do env ← get_env, d ← env.get n, t ← return $ d.type, is_prop t meta def is_proof (e : expr) : tactic bool := infer_type e >>= is_prop meta def whnf_no_delta (e : expr) : tactic expr := whnf e transparency.none /-- Return `e` in weak head normal form with respect to the given transparency setting, or `e` head is a generalized constructor or inductive datatype. -/ meta def whnf_ginductive (e : expr) (md := semireducible) : tactic expr := whnf e md ff meta def whnf_target : tactic unit := target >>= whnf >>= change meta def unsafe_change (e : expr) : tactic unit := change e ff meta def intro (n : name) : tactic expr := do t ← target, if expr.is_pi t ∨ expr.is_let t then intro_core n else whnf_target >> intro_core n meta def intro1 : tactic expr := intro `_ meta def intros : tactic (list expr) := do t ← target, match t with | expr.pi _ _ _ _ := do H ← intro1, Hs ← intros, return (H :: Hs) | expr.elet _ _ _ _ := do H ← intro1, Hs ← intros, return (H :: Hs) | _ := return [] end meta def intro_lst : list name → tactic (list expr) | [] := return [] | (n::ns) := do H ← intro n, Hs ← intro_lst ns, return (H :: Hs) /-- Introduces new hypotheses with forward dependencies -/ meta def intros_dep : tactic (list expr) := do t ← target, let proc (b : expr) := if b.has_var_idx 0 then do h ← intro1, hs ← intros_dep, return (h::hs) else -- body doesn't depend on new hypothesis return [], match t with | expr.pi _ _ _ b := proc b | expr.elet _ _ _ b := proc b | _ := return [] end meta def introv : list name → tactic (list expr) | [] := intros_dep | (n::ns) := do hs ← intros_dep, h ← intro n, hs' ← introv ns, return (hs ++ h :: hs') /-- Returns n fully qualified if it refers to a constant, or else fails. -/ meta def resolve_constant (n : name) : tactic name := do (expr.const n _) ← resolve_name n, pure n meta def to_expr_strict (q : pexpr) : tactic expr := to_expr q meta def revert (l : expr) : tactic nat := revert_lst [l] meta def clear_lst : list name → tactic unit | [] := skip | (n::ns) := do H ← get_local n, clear H, clear_lst ns meta def match_not (e : expr) : tactic expr := match (expr.is_not e) with | (some a) := return a | none := fail "expression is not a negation" end meta def match_and (e : expr) : tactic (expr × expr) := match (expr.is_and e) with | (some (α, β)) := return (α, β) | none := fail "expression is not a conjunction" end meta def match_or (e : expr) : tactic (expr × expr) := match (expr.is_or e) with | (some (α, β)) := return (α, β) | none := fail "expression is not a disjunction" end meta def match_iff (e : expr) : tactic (expr × expr) := match (expr.is_iff e) with | (some (lhs, rhs)) := return (lhs, rhs) | none := fail "expression is not an iff" end meta def match_eq (e : expr) : tactic (expr × expr) := match (expr.is_eq e) with | (some (lhs, rhs)) := return (lhs, rhs) | none := fail "expression is not an equality" end meta def match_ne (e : expr) : tactic (expr × expr) := match (expr.is_ne e) with | (some (lhs, rhs)) := return (lhs, rhs) | none := fail "expression is not a disequality" end meta def match_heq (e : expr) : tactic (expr × expr × expr × expr) := do match (expr.is_heq e) with | (some (α, lhs, β, rhs)) := return (α, lhs, β, rhs) | none := fail "expression is not a heterogeneous equality" end meta def match_refl_app (e : expr) : tactic (name × expr × expr) := do env ← get_env, match (environment.is_refl_app env e) with | (some (R, lhs, rhs)) := return (R, lhs, rhs) | none := fail "expression is not an application of a reflexive relation" end meta def match_app_of (e : expr) (n : name) : tactic (list expr) := guard (expr.is_app_of e n) >> return e.get_app_args meta def get_local_type (n : name) : tactic expr := get_local n >>= infer_type meta def trace_result : tactic unit := format_result >>= trace meta def rexact (e : expr) : tactic unit := exact e reducible meta def any_hyp_aux {α : Type} (f : expr → tactic α) : list expr → tactic α | [] := failed | (h :: hs) := f h <|> any_hyp_aux hs meta def any_hyp {α : Type} (f : expr → tactic α) : tactic α := local_context >>= any_hyp_aux f /-- `find_same_type t es` tries to find in es an expression with type definitionally equal to t -/ meta def find_same_type : expr → list expr → tactic expr | e [] := failed | e (H :: Hs) := do t ← infer_type H, (unify e t >> return H) <|> find_same_type e Hs meta def find_assumption (e : expr) : tactic expr := do ctx ← local_context, find_same_type e ctx meta def assumption : tactic unit := do { ctx ← local_context, t ← target, H ← find_same_type t ctx, exact H } <|> fail "assumption tactic failed" meta def save_info (p : pos) : tactic unit := do s ← read, tactic.save_info_thunk p (λ _, tactic_state.to_format s) notation `‹` p `›` := (by assumption : p) /-- Swap first two goals, do nothing if tactic state does not have at least two goals. -/ meta def swap : tactic unit := do gs ← get_goals, match gs with | (g₁ :: g₂ :: rs) := set_goals (g₂ :: g₁ :: rs) | e := skip end /-- `assert h t`, adds a new goal for t, and the hypothesis `h : t` in the current goal. -/ meta def assert (h : name) (t : expr) : tactic expr := do assert_core h t, swap, e ← intro h, swap, return e /-- `assertv h t v`, adds the hypothesis `h : t` in the current goal if v has type t. -/ meta def assertv (h : name) (t : expr) (v : expr) : tactic expr := assertv_core h t v >> intro h /-- `define h t`, adds a new goal for t, and the hypothesis `h : t := ?M` in the current goal. -/ meta def define (h : name) (t : expr) : tactic expr := do define_core h t, swap, e ← intro h, swap, return e /-- `definev h t v`, adds the hypothesis (h : t := v) in the current goal if v has type t. -/ meta def definev (h : name) (t : expr) (v : expr) : tactic expr := definev_core h t v >> intro h /-- Add `h : t := pr` to the current goal -/ meta def pose (h : name) (t : option expr := none) (pr : expr) : tactic expr := let dv := λt, definev h t pr in option.cases_on t (infer_type pr >>= dv) dv /-- Add `h : t` to the current goal, given a proof `pr : t` -/ meta def note (h : name) (t : option expr := none) (pr : expr) : tactic expr := let dv := λt, assertv h t pr in option.cases_on t (infer_type pr >>= dv) dv /-- Return the number of goals that need to be solved -/ meta def num_goals : tactic nat := do gs ← get_goals, return (length gs) /-- We have to provide the instance argument `[has_mod nat]` because mod for nat was not defined yet -/ meta def rotate_right (n : nat) [has_mod nat] : tactic unit := do ng ← num_goals, if ng = 0 then skip else rotate_left (ng - n % ng) meta def rotate : nat → tactic unit := rotate_left private meta def repeat_aux (t : tactic unit) : list expr → list expr → tactic unit | [] r := set_goals r.reverse | (g::gs) r := do ok ← try_core (set_goals [g] >> t), match ok with | none := repeat_aux gs (g::r) | _ := do gs' ← get_goals, repeat_aux (gs' ++ gs) r end /-- This tactic is applied 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` never fails. -/ meta def repeat (t : tactic unit) : tactic unit := do gs ← get_goals, repeat_aux t gs [] /-- `first [t_1, ..., t_n]` applies the first tactic that doesn't fail. The tactic fails if all t_i's fail. -/ meta def first {α : Type u} : list (tactic α) → tactic α | [] := fail "first tactic failed, no more alternatives" | (t::ts) := t <|> first ts /-- Applies the given tactic to the main goal and fails if it is not solved. -/ meta def solve1 (tac : tactic unit) : tactic unit := do gs ← get_goals, match gs with | [] := fail "solve1 tactic failed, there isn't any goal left to focus" | (g::rs) := do set_goals [g], tac, gs' ← get_goals, match gs' with | [] := set_goals rs | gs := fail "solve1 tactic failed, focused goal has not been solved" end end /-- `solve [t_1, ... t_n]` applies the first tactic that solves the main goal. -/ meta def solve (ts : list (tactic unit)) : tactic unit := first $ map solve1 ts private meta def focus_aux : list (tactic unit) → list expr → list expr → tactic unit | [] [] rs := set_goals rs | (t::ts) [] rs := fail "focus tactic failed, insufficient number of goals" | tts (g::gs) rs := mcond (is_assigned g) (focus_aux tts gs rs) $ do set_goals [g], t::ts ← pure tts | fail "focus tactic failed, insufficient number of tactics", t, rs' ← get_goals, focus_aux ts gs (rs ++ rs') /-- `focus [t_1, ..., t_n]` applies t_i to the i-th goal. Fails if the number of goals is not n. -/ meta def focus (ts : list (tactic unit)) : tactic unit := do gs ← get_goals, focus_aux ts gs [] meta def focus1 {α} (tac : tactic α) : tactic α := do g::gs ← get_goals, match gs with | [] := tac | _ := do set_goals [g], a ← tac, gs' ← get_goals, set_goals (gs' ++ gs), return a end private meta def all_goals_core (tac : tactic unit) : list expr → list expr → tactic unit | [] ac := set_goals ac | (g :: gs) ac := mcond (is_assigned g) (all_goals_core gs ac) $ do set_goals [g], tac, new_gs ← get_goals, all_goals_core gs (ac ++ new_gs) /-- Apply the given tactic to all goals. -/ meta def all_goals (tac : tactic unit) : tactic unit := do gs ← get_goals, all_goals_core tac gs [] private meta def any_goals_core (tac : tactic unit) : list expr → list expr → bool → tactic unit | [] ac progress := guard progress >> set_goals ac | (g :: gs) ac progress := mcond (is_assigned g) (any_goals_core gs ac progress) $ do set_goals [g], succeeded ← try_core tac, new_gs ← get_goals, any_goals_core gs (ac ++ new_gs) (succeeded.is_some || progress) /-- Apply the given tactic to any goal where it succeeds. The tactic succeeds only if tac succeeds for at least one goal. -/ meta def any_goals (tac : tactic unit) : tactic unit := do gs ← get_goals, any_goals_core tac gs [] ff /-- LCF-style AND_THEN tactic. It applies tac1, and if succeed applies tac2 to each subgoal produced by tac1 -/ meta def seq (tac1 : tactic unit) (tac2 : tactic unit) : tactic unit := do g::gs ← get_goals, set_goals [g], tac1, all_goals tac2, gs' ← get_goals, set_goals (gs' ++ gs) meta def seq_focus (tac1 : tactic unit) (tacs2 : list (tactic unit)) : tactic unit := do g::gs ← get_goals, set_goals [g], tac1, focus tacs2, gs' ← get_goals, set_goals (gs' ++ gs) meta instance andthen_seq : has_andthen (tactic unit) (tactic unit) (tactic unit) := ⟨seq⟩ meta instance andthen_seq_focus : has_andthen (tactic unit) (list (tactic unit)) (tactic unit) := ⟨seq_focus⟩ meta constant is_trace_enabled_for : name → bool /-- Execute tac only if option trace.n is set to true. -/ meta def when_tracing (n : name) (tac : tactic unit) : tactic unit := when (is_trace_enabled_for n = tt) tac /-- Fail if there are no remaining goals. -/ meta def fail_if_no_goals : tactic unit := do n ← num_goals, when (n = 0) (fail "tactic failed, there are no goals to be solved") /-- Fail if there are unsolved goals. -/ meta def done : tactic unit := do n ← num_goals, when (n ≠ 0) (fail "done tactic failed, there are unsolved goals") meta def apply_opt_param : tactic unit := do `(opt_param %%t %%v) ← target, exact v meta def apply_auto_param : tactic unit := do `(auto_param %%type %%tac_name_expr) ← target, change type, tac_name ← eval_expr name tac_name_expr, tac ← eval_expr (tactic unit) (expr.const tac_name []), tac meta def has_opt_auto_param (ms : list expr) : tactic bool := ms.mfoldl (λ r m, do type ← infer_type m, return $ r || type.is_napp_of `opt_param 2 || type.is_napp_of `auto_param 2) ff meta def try_apply_opt_auto_param (cfg : apply_cfg) (ms : list expr) : tactic unit := when (cfg.auto_param || cfg.opt_param) $ mwhen (has_opt_auto_param ms) $ do gs ← get_goals, ms.mmap' (λ m, mwhen (bnot <$> is_assigned m) $ set_goals [m] >> when cfg.opt_param (try apply_opt_param) >> when cfg.auto_param (try apply_auto_param)), set_goals gs meta def has_opt_auto_param_for_apply (ms : list (name × expr)) : tactic bool := ms.mfoldl (λ r m, do type ← infer_type m.2, return $ r || type.is_napp_of `opt_param 2 || type.is_napp_of `auto_param 2) ff meta def try_apply_opt_auto_param_for_apply (cfg : apply_cfg) (ms : list (name × expr)) : tactic unit := mwhen (has_opt_auto_param_for_apply ms) $ do gs ← get_goals, ms.mmap' (λ m, mwhen (bnot <$> (is_assigned m.2)) $ set_goals [m.2] >> when cfg.opt_param (try apply_opt_param) >> when cfg.auto_param (try apply_auto_param)), set_goals gs meta def apply (e : expr) (cfg : apply_cfg := {}) : tactic (list (name × expr)) := do r ← apply_core e cfg, try_apply_opt_auto_param_for_apply cfg r, return r meta def fapply (e : expr) : tactic (list (name × expr)) := apply e {new_goals := new_goals.all} meta def eapply (e : expr) : tactic (list (name × expr)) := apply e {new_goals := new_goals.non_dep_only} /-- Try to solve the main goal using type class resolution. -/ meta def apply_instance : tactic unit := do tgt ← target >>= instantiate_mvars, b ← is_class tgt, if b then mk_instance tgt >>= exact else fail "apply_instance tactic fail, target is not a type class" /-- Create a list of universe meta-variables of the given size. -/ meta def mk_num_meta_univs : nat → tactic (list level) | 0 := return [] | (succ n) := do l ← mk_meta_univ, ls ← mk_num_meta_univs n, return (l::ls) /-- Return `expr.const c [l_1, ..., l_n]` where l_i's are fresh universe meta-variables. -/ meta def mk_const (c : name) : tactic expr := do env ← get_env, decl ← env.get c, let num := decl.univ_params.length, ls ← mk_num_meta_univs num, return (expr.const c ls) /-- Apply the constant `c` -/ meta def applyc (c : name) : tactic unit := do c ← mk_const c, apply c, skip meta def eapplyc (c : name) : tactic unit := do c ← mk_const c, eapply c, skip meta def save_const_type_info (n : name) {elab : bool} (ref : expr elab) : tactic unit := try (do c ← mk_const n, save_type_info c ref) /-- Create a fresh universe `?u`, a metavariable `?T : Type.{?u}`, and return metavariable `?M : ?T`. This action can be used to create a meta-variable when we don't know its type at creation time -/ meta def mk_mvar : tactic expr := do u ← mk_meta_univ, t ← mk_meta_var (expr.sort u), mk_meta_var t /-- Makes a sorry macro with a meta-variable as its type. -/ meta def mk_sorry : tactic expr := do u ← mk_meta_univ, t ← mk_meta_var (expr.sort u), return $ expr.mk_sorry t /-- Closes the main goal using sorry. -/ meta def admit : tactic unit := target >>= exact ∘ expr.mk_sorry meta def mk_local' (pp_name : name) (bi : binder_info) (type : expr) : tactic expr := do uniq_name ← mk_fresh_name, return $ expr.local_const uniq_name pp_name bi type meta def mk_local_def (pp_name : name) (type : expr) : tactic expr := mk_local' pp_name binder_info.default type meta def mk_local_pis : expr → tactic (list expr × expr) | (expr.pi n bi d b) := do p ← mk_local' n bi d, (ps, r) ← mk_local_pis (expr.instantiate_var b p), return ((p :: ps), r) | e := return ([], e) private meta def get_pi_arity_aux : expr → tactic nat | (expr.pi n bi d b) := do m ← mk_fresh_name, let l := expr.local_const m n bi d, new_b ← whnf (expr.instantiate_var b l), r ← get_pi_arity_aux new_b, return (r + 1) | e := return 0 /-- Compute the arity of the given (Pi-)type -/ meta def get_pi_arity (type : expr) : tactic nat := whnf type >>= get_pi_arity_aux /-- Compute the arity of the given function -/ meta def get_arity (fn : expr) : tactic nat := infer_type fn >>= get_pi_arity meta def triv : tactic unit := mk_const `trivial >>= exact notation `dec_trivial` := of_as_true (by tactic.triv) meta def by_contradiction (H : option name := none) : tactic expr := do tgt : expr ← target, (match_not tgt >> return ()) <|> (mk_mapp `decidable.by_contradiction [some tgt, none] >>= eapply >> skip) <|> fail "tactic by_contradiction failed, target is not a negation nor a decidable proposition (remark: when 'local attribute classical.prop_decidable [instance]' is used all propositions are decidable)", match H with | some n := intro n | none := intro1 end private meta def generalizes_aux (md : transparency) : list expr → tactic unit | [] := skip | (e::es) := generalize e `x md >> generalizes_aux es meta def generalizes (es : list expr) (md := semireducible) : tactic unit := generalizes_aux md es private meta def kdependencies_core (e : expr) (md : transparency) : list expr → list expr → tactic (list expr) | [] r := return r | (h::hs) r := do type ← infer_type h, d ← kdepends_on type e md, if d then kdependencies_core hs (h::r) else kdependencies_core hs r /-- Return all hypotheses that depends on `e` The dependency test is performed using `kdepends_on` with the given transparency setting. -/ meta def kdependencies (e : expr) (md := reducible) : tactic (list expr) := do ctx ← local_context, kdependencies_core e md ctx [] /-- Revert all hypotheses that depend on `e` -/ meta def revert_kdependencies (e : expr) (md := reducible) : tactic nat := kdependencies e md >>= revert_lst meta def revert_kdeps (e : expr) (md := reducible) := revert_kdependencies e md /-- Similar to `cases_core`, but `e` doesn't need to be a hypothesis. Remark, it reverts dependencies using `revert_kdeps`. Two different transparency modes are used `md` and `dmd`. The mode `md` is used with `cases_core` and `dmd` with `generalize` and `revert_kdeps`. It returns the constructor names associated with each new goal. -/ meta def cases (e : expr) (ids : list name := []) (md := semireducible) (dmd := semireducible) : tactic (list name) := if e.is_local_constant then do r ← cases_core e ids md, return $ r.map (λ t, t.1) else do x ← mk_fresh_name, n ← revert_kdependencies e dmd, (tactic.generalize e x dmd) <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, return ()), h ← tactic.intro1, focus1 (do r ← cases_core h ids md, all_goals (intron n), return $ r.map (λ t, t.1)) meta def refine (e : pexpr) : tactic unit := do tgt : expr ← target, to_expr ``(%%e : %%tgt) tt >>= exact meta def by_cases (e : expr) (h : name) : tactic unit := do dec_e ← (mk_app `decidable [e] <|> fail "by_cases tactic failed, type is not a proposition"), inst ← (mk_instance dec_e <|> fail "by_cases tactic failed, type of given expression is not decidable"), t ← target, tm ← mk_mapp `dite [some e, some inst, some t], seq (apply tm >> skip) (intro h >> skip) meta def funext_core : list name → bool → tactic unit | [] tt := return () | ids only_ids := try $ do some (lhs, rhs) ← expr.is_eq <$> (target >>= whnf), applyc `funext, id ← if ids.empty ∨ ids.head = `_ then do (expr.lam n _ _ _) ← whnf lhs, return n else return ids.head, intro id, funext_core ids.tail only_ids meta def funext : tactic unit := funext_core [] ff meta def funext_lst (ids : list name) : tactic unit := funext_core ids tt private meta def get_undeclared_const (env : environment) (base : name) : ℕ → name | i := let n := base <.> ("_aux_" ++ repr i) in if ¬env.contains n then n else get_undeclared_const (i+1) meta def new_aux_decl_name : tactic name := do env ← get_env, n ← decl_name, return $ get_undeclared_const env n 1 private meta def mk_aux_decl_name : option name → tactic name | none := new_aux_decl_name | (some suffix) := do p ← decl_name, return $ p ++ suffix meta def abstract (tac : tactic unit) (suffix : option name := none) (zeta_reduce := tt) : tactic unit := do fail_if_no_goals, gs ← get_goals, type ← if zeta_reduce then target >>= zeta else target, is_lemma ← is_prop type, m ← mk_meta_var type, set_goals [m], tac, n ← num_goals, when (n ≠ 0) (fail "abstract tactic failed, there are unsolved goals"), set_goals gs, val ← instantiate_mvars m, val ← if zeta_reduce then zeta val else return val, c ← mk_aux_decl_name suffix, e ← add_aux_decl c type val is_lemma, exact e /-- `solve_aux type tac` synthesize an element of 'type' using tactic 'tac' -/ meta def solve_aux {α : Type} (type : expr) (tac : tactic α) : tactic (α × expr) := do m ← mk_meta_var type, gs ← get_goals, set_goals [m], a ← tac, set_goals gs, return (a, m) /-- Return tt iff 'd' is a declaration in one of the current open namespaces -/ meta def in_open_namespaces (d : name) : tactic bool := do ns ← open_namespaces, env ← get_env, return $ ns.any (λ n, n.is_prefix_of d) && env.contains d /-- Execute tac for 'max' "heartbeats". The heartbeat is approx. the maximum number of memory allocations (in thousands) performed by 'tac'. This is a deterministic way of interrupting long running tactics. -/ meta def try_for {α} (max : nat) (tac : tactic α) : tactic α := λ s, match _root_.try_for max (tac s) with | some r := r | none := mk_exception "try_for tactic failed, timeout" none s end meta def updateex_env (f : environment → exceptional environment) : tactic unit := do env ← get_env, env ← returnex $ f env, set_env env /- Add a new inductive datatype to the environment name, universe parameters, number of parameters, type, constructors (name and type), is_meta -/ meta def add_inductive (n : name) (ls : list name) (p : nat) (ty : expr) (is : list (name × expr)) (is_meta : bool := ff) : tactic unit := updateex_env $ λe, e.add_inductive n ls p ty is is_meta meta def add_meta_definition (n : name) (lvls : list name) (type value : expr) : tactic unit := add_decl (declaration.defn n lvls type value reducibility_hints.abbrev ff) meta def rename (curr : name) (new : name) : tactic unit := do h ← get_local curr, n ← revert h, intro new, intron (n - 1) /-- "Replace" hypothesis `h : type` with `h : new_type` where `eq_pr` is a proof that (type = new_type). The tactic actually creates a new hypothesis with the same user facing name, and (tries to) clear `h`. The `clear` step fails if `h` has forward dependencies. In this case, the old `h` will remain in the local context. The tactic returns the new hypothesis. -/ meta def replace_hyp (h : expr) (new_type : expr) (eq_pr : expr) : tactic expr := do h_type ← infer_type h, new_h ← assert h.local_pp_name new_type, mk_eq_mp eq_pr h >>= exact, try $ clear h, return new_h meta def main_goal : tactic expr := do g::gs ← get_goals, return g /- Goal tagging support -/ meta def with_enable_tags {α : Type} (t : tactic α) (b := tt) : tactic α := do old ← tags_enabled, enable_tags b, r ← t, enable_tags old, return r meta def get_main_tag : tactic tag := main_goal >>= get_tag meta def set_main_tag (t : tag) : tactic unit := do g ← main_goal, set_tag g t end tactic notation [parsing_only] `command`:max := tactic unit open tactic namespace list meta def for_each {α} : list α → (α → tactic unit) → tactic unit | [] fn := skip | (e::es) fn := do fn e, for_each es fn meta def any_of {α β} : list α → (α → tactic β) → tactic β | [] fn := failed | (e::es) fn := do opt_b ← try_core (fn e), match opt_b with | some b := return b | none := any_of es fn end end list /- Define id_rhs using meta-programming because we don't have syntax for setting reducibility_hints. See module init.meta.declaration. Remark: id_rhs is used in the equation compiler to address performance issues when proving equational lemmas. -/ run_cmd do let l := level.param `l, let Ty : pexpr := expr.sort l, type ← to_expr ``(Π (α : %%Ty), α → α), val ← to_expr ``(λ (α : %%Ty) (a : α), a), add_decl (declaration.defn `id_rhs [`l] type val reducibility_hints.abbrev tt) attribute [reducible, inline] id_rhs /- Install monad laws tactic and use it to prove some instances. -/ meta def control_laws_tac := whnf_target >> intros >> to_expr ``(rfl) >>= exact meta def order_laws_tac := whnf_target >> intros >> to_expr ``(iff.refl _) >>= exact meta def unsafe_monad_from_pure_bind {m : Type u → Type v} (pure : Π {α : Type u}, α → m α) (bind : Π {α β : Type u}, m α → (α → m β) → m β) : monad m := {pure := @pure, bind := @bind, id_map := undefined, pure_bind := undefined, bind_assoc := undefined} meta instance : monad task := {map := @task.map, bind := @task.bind, pure := @task.pure, id_map := undefined, pure_bind := undefined, bind_assoc := undefined, bind_pure_comp_eq_map := undefined} namespace tactic meta def mk_id_proof (prop : expr) (pr : expr) : expr := expr.app (expr.app (expr.const ``id [level.zero]) prop) pr meta def mk_id_eq (lhs : expr) (rhs : expr) (pr : expr) : tactic expr := do prop ← mk_app `eq [lhs, rhs], return $ mk_id_proof prop pr meta def replace_target (new_target : expr) (pr : expr) : tactic unit := do t ← target, assert `htarget new_target, swap, ht ← get_local `htarget, locked_pr ← mk_id_eq t new_target pr, mk_eq_mpr locked_pr ht >>= exact end tactic
3162b3155e1f5e5b47e48e3771cae940c68ddfc6
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/measure_theory/constructions/pi.lean
33632d0bbee4374af6b200a3874fb4c3fefe6df3
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
28,297
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 /-! # Product measures In this file we define and prove properties about finite products of measures (and at some point, countable products of measures). ## Main definition * `measure_theory.measure.pi`: The product of finitely many σ-finite measures. Given `μ : Π i : ι, measure (α i)` for `[fintype ι]` it has type `measure (Π i : ι, α i)`. To apply Fubini along some subset of the variables, use `measure_theory.measure.map_pi_equiv_pi_subtype_prod` to reduce to the situation of a product of two measures: this lemma states that the bijection `equiv.pi_equiv_pi_subtype_prod p α` between `(Π i : ι, α i)` and `(Π i : {i // p i}, α i) × (Π i : {i // ¬ p i}, α i)` maps a product measure to a direct product of product measures, to which one can apply the usual Fubini for direct product of measures. ## Implementation Notes We define `measure_theory.outer_measure.pi`, the product of finitely many outer measures, as the maximal outer measure `n` with the property that `n (pi univ s) ≤ ∏ i, m i (s i)`, where `pi univ s` is the product of the sets `{s i | i : ι}`. We then show that this induces a product of measures, called `measure_theory.measure.pi`. For a collection of σ-finite measures `μ` and a collection of measurable sets `s` we show that `measure.pi μ (pi univ s) = ∏ i, m i (s i)`. To do this, we follow the following steps: * We know that there is some ordering on `ι`, given by an element of `[encodable ι]`. * Using this, we have an equivalence `measurable_equiv.pi_measurable_equiv_tprod` between `Π ι, α i` and an iterated product of `α i`, called `list.tprod α l` for some list `l`. * On this iterated product we can easily define a product measure `measure_theory.measure.tprod` by iterating `measure_theory.measure.prod` * Using the previous two steps we construct `measure_theory.measure.pi'` on `Π ι, α i` for encodable `ι`. * We know that `measure_theory.measure.pi'` sends products of sets to products of measures, and since `measure_theory.measure.pi` is the maximal such measure (or at least, it comes from an outer measure which is the maximal such outer measure), we get the same rule for `measure_theory.measure.pi`. ## Tags finitary product measure -/ noncomputable theory open function set measure_theory.outer_measure filter measurable_space encodable open_locale classical big_operators topological_space ennreal universes u v variables {ι ι' : Type*} {α : ι → Type*} /-! We start with some measurability properties -/ /-- Boxes formed by π-systems form a π-system. -/ lemma is_pi_system.pi {C : Π i, set (set (α i))} (hC : ∀ i, is_pi_system (C i)) : is_pi_system (pi univ '' pi univ C) := begin rintro _ ⟨s₁, hs₁, rfl⟩ _ ⟨s₂, hs₂, rfl⟩ hst, rw [← pi_inter_distrib] at hst ⊢, rw [univ_pi_nonempty_iff] at hst, exact mem_image_of_mem _ (λ i _, hC i _ (hs₁ i (mem_univ i)) _ (hs₂ i (mem_univ i)) (hst i)) end /-- Boxes form a π-system. -/ lemma is_pi_system_pi [Π i, measurable_space (α i)] : is_pi_system (pi univ '' pi univ (λ i, {s : set (α i) | measurable_set s})) := is_pi_system.pi (λ i, is_pi_system_measurable_set) variables [fintype ι] [fintype ι'] /-- Boxes of countably spanning sets are countably spanning. -/ lemma is_countably_spanning.pi {C : Π i, set (set (α i))} (hC : ∀ i, is_countably_spanning (C i)) : is_countably_spanning (pi univ '' pi univ C) := begin choose s h1s h2s using hC, haveI := fintype.encodable ι, let e : ℕ → (ι → ℕ) := λ n, (decode (ι → ℕ) n).iget, refine ⟨λ n, pi univ (λ i, s i (e n i)), λ n, mem_image_of_mem _ (λ i _, h1s i _), _⟩, simp_rw [(surjective_decode_iget (ι → ℕ)).Union_comp (λ x, pi univ (λ i, s i (x i))), Union_univ_pi s, h2s, pi_univ] end /-- The product of generated σ-algebras is the one generated by boxes, if both generating sets are countably spanning. -/ lemma generate_from_pi_eq {C : Π i, set (set (α i))} (hC : ∀ i, is_countably_spanning (C i)) : @measurable_space.pi _ _ (λ i, generate_from (C i)) = generate_from (pi univ '' pi univ C) := begin haveI := fintype.encodable ι, apply le_antisymm, { refine supr_le _, intro i, rw [comap_generate_from], apply generate_from_le, rintro _ ⟨s, hs, rfl⟩, dsimp, choose t h1t h2t using hC, simp_rw [eval_preimage, ← h2t], rw [← @Union_const _ ℕ _ s], have : (pi univ (update (λ (i' : ι), Union (t i')) i (⋃ (i' : ℕ), s))) = (pi univ (λ k, ⋃ j : ℕ, @update ι (λ i', set (α i')) _ (λ i', t i' j) i s k)), { ext, simp_rw [mem_univ_pi], apply forall_congr, intro i', by_cases (i' = i), { subst h, simp }, { rw [← ne.def] at h, simp [h] }}, rw [this, ← Union_univ_pi], apply measurable_set.Union, intro n, apply measurable_set_generate_from, apply mem_image_of_mem, intros j _, dsimp only, by_cases h: j = i, subst h, rwa [update_same], rw [update_noteq h], apply h1t }, { apply generate_from_le, rintro _ ⟨s, hs, rfl⟩, rw [univ_pi_eq_Inter], apply measurable_set.Inter, intro i, apply measurable_pi_apply, exact measurable_set_generate_from (hs i (mem_univ i)) } end /-- If `C` and `D` generate the σ-algebras on `α` resp. `β`, then rectangles formed by `C` and `D` generate the σ-algebra on `α × β`. -/ lemma generate_from_eq_pi [h : Π i, measurable_space (α i)] {C : Π i, set (set (α i))} (hC : ∀ i, generate_from (C i) = h i) (h2C : ∀ i, is_countably_spanning (C i)) : generate_from (pi univ '' pi univ C) = measurable_space.pi := by rw [← funext hC, generate_from_pi_eq h2C] /-- The product σ-algebra is generated from boxes, i.e. `s ×ˢ t` for sets `s : set α` and `t : set β`. -/ lemma generate_from_pi [Π i, measurable_space (α i)] : generate_from (pi univ '' pi univ (λ i, { s : set (α i) | measurable_set s})) = measurable_space.pi := generate_from_eq_pi (λ i, generate_from_measurable_set) (λ i, is_countably_spanning_measurable_set) namespace measure_theory variables {m : Π i, outer_measure (α i)} /-- An upper bound for the measure in a finite product space. It is defined to by taking the image of the set under all projections, and taking the product of the measures of these images. For measurable boxes it is equal to the correct measure. -/ @[simp] def pi_premeasure (m : Π i, outer_measure (α i)) (s : set (Π i, α i)) : ℝ≥0∞ := ∏ i, m i (eval i '' s) lemma pi_premeasure_pi {s : Π i, set (α i)} (hs : (pi univ s).nonempty) : pi_premeasure m (pi univ s) = ∏ i, m i (s i) := by simp [hs] lemma pi_premeasure_pi' [nonempty ι] {s : Π i, set (α i)} : pi_premeasure m (pi univ s) = ∏ i, m i (s i) := begin cases (pi univ s).eq_empty_or_nonempty with h h, { rcases univ_pi_eq_empty_iff.mp h with ⟨i, hi⟩, have : ∃ i, m i (s i) = 0 := ⟨i, by simp [hi]⟩, simpa [h, finset.card_univ, zero_pow (fintype.card_pos_iff.mpr ‹_›), @eq_comm _ (0 : ℝ≥0∞), finset.prod_eq_zero_iff] }, { simp [h] } end lemma pi_premeasure_pi_mono {s t : set (Π i, α i)} (h : s ⊆ t) : pi_premeasure m s ≤ pi_premeasure m t := finset.prod_le_prod' (λ i _, (m i).mono' (image_subset _ h)) lemma pi_premeasure_pi_eval [nonempty ι] {s : set (Π i, α i)} : pi_premeasure m (pi univ (λ i, eval i '' s)) = pi_premeasure m s := by simp [pi_premeasure_pi'] namespace outer_measure /-- `outer_measure.pi m` is the finite product of the outer measures `{m i | i : ι}`. It is defined to be the maximal outer measure `n` with the property that `n (pi univ s) ≤ ∏ i, m i (s i)`, where `pi univ s` is the product of the sets `{s i | i : ι}`. -/ protected def pi (m : Π i, outer_measure (α i)) : outer_measure (Π i, α i) := bounded_by (pi_premeasure m) lemma pi_pi_le (m : Π i, outer_measure (α i)) (s : Π i, set (α i)) : outer_measure.pi m (pi univ s) ≤ ∏ i, m i (s i) := by { cases (pi univ s).eq_empty_or_nonempty with h h, simp [h], exact (bounded_by_le _).trans_eq (pi_premeasure_pi h) } lemma le_pi {m : Π i, outer_measure (α i)} {n : outer_measure (Π i, α i)} : n ≤ outer_measure.pi m ↔ ∀ (s : Π i, set (α i)), (pi univ s).nonempty → n (pi univ s) ≤ ∏ i, m i (s i) := begin rw [outer_measure.pi, le_bounded_by'], split, { intros h s hs, refine (h _ hs).trans_eq (pi_premeasure_pi hs) }, { intros h s hs, refine le_trans (n.mono $ subset_pi_eval_image univ s) (h _ _), simp [univ_pi_nonempty_iff, hs] } end end outer_measure namespace measure variables [Π i, measurable_space (α i)] (μ : Π i, measure (α i)) section tprod open list variables {δ : Type*} {π : δ → Type*} [∀ x, measurable_space (π x)] /-- A product of measures in `tprod α l`. -/ -- for some reason the equation compiler doesn't like this definition protected def tprod (l : list δ) (μ : Π i, measure (π i)) : measure (tprod π l) := by { induction l with i l ih, exact dirac punit.star, exact (μ i).prod ih } @[simp] lemma tprod_nil (μ : Π i, measure (π i)) : measure.tprod [] μ = dirac punit.star := rfl @[simp] lemma tprod_cons (i : δ) (l : list δ) (μ : Π i, measure (π i)) : measure.tprod (i :: l) μ = (μ i).prod (measure.tprod l μ) := rfl instance sigma_finite_tprod (l : list δ) (μ : Π i, measure (π i)) [∀ i, sigma_finite (μ i)] : sigma_finite (measure.tprod l μ) := begin induction l with i l ih, { rw [tprod_nil], apply_instance }, { rw [tprod_cons], resetI, apply_instance } end lemma tprod_tprod (l : list δ) (μ : Π i, measure (π i)) [∀ i, sigma_finite (μ i)] (s : Π i, set (π i)) : measure.tprod l μ (set.tprod l s) = (l.map (λ i, (μ i) (s i))).prod := begin induction l with i l ih, { simp }, rw [tprod_cons, set.tprod, prod_prod, map_cons, prod_cons, ih] end end tprod section encodable open list measurable_equiv variables [encodable ι] /-- The product measure on an encodable finite type, defined by mapping `measure.tprod` along the equivalence `measurable_equiv.pi_measurable_equiv_tprod`. The definition `measure_theory.measure.pi` should be used instead of this one. -/ def pi' : measure (Π i, α i) := measure.map (tprod.elim' mem_sorted_univ) (measure.tprod (sorted_univ ι) μ) lemma pi'_pi [∀ i, sigma_finite (μ i)] (s : Π i, set (α i)) : pi' μ (pi univ s) = ∏ i, μ i (s i) := by rw [pi', ← measurable_equiv.pi_measurable_equiv_tprod_symm_apply, measurable_equiv.map_apply, measurable_equiv.pi_measurable_equiv_tprod_symm_apply, elim_preimage_pi, tprod_tprod _ μ, ← list.prod_to_finset, sorted_univ_to_finset]; exact sorted_univ_nodup ι end encodable lemma pi_caratheodory : measurable_space.pi ≤ (outer_measure.pi (λ i, (μ i).to_outer_measure)).caratheodory := begin refine supr_le _, intros i s hs, rw [measurable_space.comap] at hs, rcases hs with ⟨s, hs, rfl⟩, apply bounded_by_caratheodory, intro t, simp_rw [pi_premeasure], refine finset.prod_add_prod_le' (finset.mem_univ i) _ _ _, { simp [image_inter_preimage, image_diff_preimage, measure_inter_add_diff _ hs, le_refl] }, { rintro j - hj, apply mono', apply image_subset, apply inter_subset_left }, { rintro j - hj, apply mono', apply image_subset, apply diff_subset } end /-- `measure.pi μ` is the finite product of the measures `{μ i | i : ι}`. It is defined to be measure corresponding to `measure_theory.outer_measure.pi`. -/ @[irreducible] protected def pi : measure (Π i, α i) := to_measure (outer_measure.pi (λ i, (μ i).to_outer_measure)) (pi_caratheodory μ) lemma pi_pi_aux [∀ i, sigma_finite (μ i)] (s : Π i, set (α i)) (hs : ∀ i, measurable_set (s i)) : measure.pi μ (pi univ s) = ∏ i, μ i (s i) := begin refine le_antisymm _ _, { rw [measure.pi, to_measure_apply _ _ (measurable_set.pi_fintype (λ i _, hs i))], apply outer_measure.pi_pi_le }, { haveI : encodable ι := fintype.encodable ι, rw [← pi'_pi μ s], simp_rw [← pi'_pi μ s, measure.pi, to_measure_apply _ _ (measurable_set.pi_fintype (λ i _, hs i)), ← to_outer_measure_apply], suffices : (pi' μ).to_outer_measure ≤ outer_measure.pi (λ i, (μ i).to_outer_measure), { exact this _ }, clear hs s, rw [outer_measure.le_pi], intros s hs, simp_rw [to_outer_measure_apply], exact (pi'_pi μ s).le } end variable {μ} /-- `measure.pi μ` has finite spanning sets in rectangles of finite spanning sets. -/ def finite_spanning_sets_in.pi {C : Π i, set (set (α i))} (hμ : ∀ i, (μ i).finite_spanning_sets_in (C i)) : (measure.pi μ).finite_spanning_sets_in (pi univ '' pi univ C) := begin haveI := λ i, (hμ i).sigma_finite, haveI := fintype.encodable ι, let e : ℕ → (ι → ℕ) := λ n, (decode (ι → ℕ) n).iget, refine ⟨λ n, pi univ (λ i, (hμ i).set (e n i)), λ n, _, λ n, _, _⟩, { refine mem_image_of_mem _ (λ i _, (hμ i).set_mem _) }, { calc measure.pi μ (pi univ (λ i, (hμ i).set (e n i))) ≤ measure.pi μ (pi univ (λ i, to_measurable (μ i) ((hμ i).set (e n i)))) : measure_mono (pi_mono $ λ i hi, subset_to_measurable _ _) ... = ∏ i, μ i (to_measurable (μ i) ((hμ i).set (e n i))) : pi_pi_aux μ _ (λ i, measurable_set_to_measurable _ _) ... = ∏ i, μ i ((hμ i).set (e n i)) : by simp only [measure_to_measurable] ... < ∞ : ennreal.prod_lt_top (λ i hi, ((hμ i).finite _).ne) }, { simp_rw [(surjective_decode_iget (ι → ℕ)).Union_comp (λ x, pi univ (λ i, (hμ i).set (x i))), Union_univ_pi (λ i, (hμ i).set), (hμ _).spanning, set.pi_univ] } end /-- A measure on a finite product space equals the product measure if they are equal on rectangles with as sides sets that generate the corresponding σ-algebras. -/ lemma pi_eq_generate_from {C : Π i, set (set (α i))} (hC : ∀ i, generate_from (C i) = _inst_3 i) (h2C : ∀ i, is_pi_system (C i)) (h3C : ∀ i, (μ i).finite_spanning_sets_in (C i)) {μν : measure (Π i, α i)} (h₁ : ∀ s : Π i, set (α i), (∀ i, s i ∈ C i) → μν (pi univ s) = ∏ i, μ i (s i)) : measure.pi μ = μν := begin have h4C : ∀ i (s : set (α i)), s ∈ C i → measurable_set s, { intros i s hs, rw [← hC], exact measurable_set_generate_from hs }, refine (finite_spanning_sets_in.pi h3C).ext (generate_from_eq_pi hC (λ i, (h3C i).is_countably_spanning)).symm (is_pi_system.pi h2C) _, rintro _ ⟨s, hs, rfl⟩, rw [mem_univ_pi] at hs, haveI := λ i, (h3C i).sigma_finite, simp_rw [h₁ s hs, pi_pi_aux μ s (λ i, h4C i _ (hs i))] end variables [∀ i, sigma_finite (μ i)] /-- A measure on a finite product space equals the product measure if they are equal on rectangles. -/ lemma pi_eq {μ' : measure (Π i, α i)} (h : ∀ s : Π i, set (α i), (∀ i, measurable_set (s i)) → μ' (pi univ s) = ∏ i, μ i (s i)) : measure.pi μ = μ' := pi_eq_generate_from (λ i, generate_from_measurable_set) (λ i, is_pi_system_measurable_set) (λ i, (μ i).to_finite_spanning_sets_in) h variables (μ) lemma pi'_eq_pi [encodable ι] : pi' μ = measure.pi μ := eq.symm $ pi_eq $ λ s hs, pi'_pi μ s @[simp] lemma pi_pi (s : Π i, set (α i)) : measure.pi μ (pi univ s) = ∏ i, μ i (s i) := begin haveI : encodable ι := fintype.encodable ι, rw [← pi'_eq_pi, pi'_pi] end lemma pi_univ : measure.pi μ univ = ∏ i, μ i univ := by rw [← pi_univ, pi_pi μ] lemma pi_ball [∀ i, metric_space (α i)] (x : Π i, α i) {r : ℝ} (hr : 0 < r) : measure.pi μ (metric.ball x r) = ∏ i, μ i (metric.ball (x i) r) := by rw [ball_pi _ hr, pi_pi] lemma pi_closed_ball [∀ i, metric_space (α i)] (x : Π i, α i) {r : ℝ} (hr : 0 ≤ r) : measure.pi μ (metric.closed_ball x r) = ∏ i, μ i (metric.closed_ball (x i) r) := by rw [closed_ball_pi _ hr, pi_pi] instance pi.sigma_finite : sigma_finite (measure.pi μ) := (finite_spanning_sets_in.pi (λ i, (μ i).to_finite_spanning_sets_in)).sigma_finite lemma pi_of_empty {α : Type*} [is_empty α] {β : α → Type*} {m : Π a, measurable_space (β a)} (μ : Π a : α, measure (β a)) (x : Π a, β a := is_empty_elim) : measure.pi μ = dirac x := begin haveI : ∀ a, sigma_finite (μ a) := is_empty_elim, refine pi_eq (λ s hs, _), rw [fintype.prod_empty, dirac_apply_of_mem], exact is_empty_elim end lemma pi_eval_preimage_null {i : ι} {s : set (α i)} (hs : μ i s = 0) : measure.pi μ (eval i ⁻¹' s) = 0 := begin /- WLOG, `s` is measurable -/ rcases exists_measurable_superset_of_null hs with ⟨t, hst, htm, hμt⟩, suffices : measure.pi μ (eval i ⁻¹' t) = 0, from measure_mono_null (preimage_mono hst) this, clear_dependent s, /- Now rewrite it as `set.pi`, and apply `pi_pi` -/ rw [← univ_pi_update_univ, pi_pi], apply finset.prod_eq_zero (finset.mem_univ i), simp [hμt] end lemma pi_hyperplane (i : ι) [has_no_atoms (μ i)] (x : α i) : measure.pi μ {f : Π i, α i | f i = x} = 0 := show measure.pi μ (eval i ⁻¹' {x}) = 0, from pi_eval_preimage_null _ (measure_singleton x) lemma ae_eval_ne (i : ι) [has_no_atoms (μ i)] (x : α i) : ∀ᵐ y : Π i, α i ∂measure.pi μ, y i ≠ x := compl_mem_ae_iff.2 (pi_hyperplane μ i x) variable {μ} lemma tendsto_eval_ae_ae {i : ι} : tendsto (eval i) (measure.pi μ).ae (μ i).ae := λ s hs, pi_eval_preimage_null μ hs lemma ae_pi_le_pi : (measure.pi μ).ae ≤ filter.pi (λ i, (μ i).ae) := le_infi $ λ i, tendsto_eval_ae_ae.le_comap lemma ae_eq_pi {β : ι → Type*} {f f' : Π i, α i → β i} (h : ∀ i, f i =ᵐ[μ i] f' i) : (λ (x : Π i, α i) i, f i (x i)) =ᵐ[measure.pi μ] (λ x i, f' i (x i)) := (eventually_all.2 (λ i, tendsto_eval_ae_ae.eventually (h i))).mono $ λ x hx, funext hx lemma ae_le_pi {β : ι → Type*} [Π i, preorder (β i)] {f f' : Π i, α i → β i} (h : ∀ i, f i ≤ᵐ[μ i] f' i) : (λ (x : Π i, α i) i, f i (x i)) ≤ᵐ[measure.pi μ] (λ x i, f' i (x i)) := (eventually_all.2 (λ i, tendsto_eval_ae_ae.eventually (h i))).mono $ λ x hx, hx lemma ae_le_set_pi {I : set ι} {s t : Π i, set (α i)} (h : ∀ i ∈ I, s i ≤ᵐ[μ i] t i) : (set.pi I s) ≤ᵐ[measure.pi μ] (set.pi I t) := ((eventually_all_finite (finite.of_fintype I)).2 (λ i hi, tendsto_eval_ae_ae.eventually (h i hi))).mono $ λ x hst hx i hi, hst i hi $ hx i hi lemma ae_eq_set_pi {I : set ι} {s t : Π i, set (α i)} (h : ∀ i ∈ I, s i =ᵐ[μ i] t i) : (set.pi I s) =ᵐ[measure.pi μ] (set.pi I t) := (ae_le_set_pi (λ i hi, (h i hi).le)).antisymm (ae_le_set_pi (λ i hi, (h i hi).symm.le)) section intervals variables {μ} [Π i, partial_order (α i)] [∀ i, has_no_atoms (μ i)] lemma pi_Iio_ae_eq_pi_Iic {s : set ι} {f : Π i, α i} : pi s (λ i, Iio (f i)) =ᵐ[measure.pi μ] pi s (λ i, Iic (f i)) := ae_eq_set_pi $ λ i hi, Iio_ae_eq_Iic lemma pi_Ioi_ae_eq_pi_Ici {s : set ι} {f : Π i, α i} : pi s (λ i, Ioi (f i)) =ᵐ[measure.pi μ] pi s (λ i, Ici (f i)) := ae_eq_set_pi $ λ i hi, Ioi_ae_eq_Ici lemma univ_pi_Iio_ae_eq_Iic {f : Π i, α i} : pi univ (λ i, Iio (f i)) =ᵐ[measure.pi μ] Iic f := by { rw ← pi_univ_Iic, exact pi_Iio_ae_eq_pi_Iic } lemma univ_pi_Ioi_ae_eq_Ici {f : Π i, α i} : pi univ (λ i, Ioi (f i)) =ᵐ[measure.pi μ] Ici f := by { rw ← pi_univ_Ici, exact pi_Ioi_ae_eq_pi_Ici } lemma pi_Ioo_ae_eq_pi_Icc {s : set ι} {f g : Π i, α i} : pi s (λ i, Ioo (f i) (g i)) =ᵐ[measure.pi μ] pi s (λ i, Icc (f i) (g i)) := ae_eq_set_pi $ λ i hi, Ioo_ae_eq_Icc lemma pi_Ioo_ae_eq_pi_Ioc {s : set ι} {f g : Π i, α i} : pi s (λ i, Ioo (f i) (g i)) =ᵐ[measure.pi μ] pi s (λ i, Ioc (f i) (g i)) := ae_eq_set_pi $ λ i hi, Ioo_ae_eq_Ioc lemma univ_pi_Ioo_ae_eq_Icc {f g : Π i, α i} : pi univ (λ i, Ioo (f i) (g i)) =ᵐ[measure.pi μ] Icc f g := by { rw ← pi_univ_Icc, exact pi_Ioo_ae_eq_pi_Icc } lemma pi_Ioc_ae_eq_pi_Icc {s : set ι} {f g : Π i, α i} : pi s (λ i, Ioc (f i) (g i)) =ᵐ[measure.pi μ] pi s (λ i, Icc (f i) (g i)) := ae_eq_set_pi $ λ i hi, Ioc_ae_eq_Icc lemma univ_pi_Ioc_ae_eq_Icc {f g : Π i, α i} : pi univ (λ i, Ioc (f i) (g i)) =ᵐ[measure.pi μ] Icc f g := by { rw ← pi_univ_Icc, exact pi_Ioc_ae_eq_pi_Icc } lemma pi_Ico_ae_eq_pi_Icc {s : set ι} {f g : Π i, α i} : pi s (λ i, Ico (f i) (g i)) =ᵐ[measure.pi μ] pi s (λ i, Icc (f i) (g i)) := ae_eq_set_pi $ λ i hi, Ico_ae_eq_Icc lemma univ_pi_Ico_ae_eq_Icc {f g : Π i, α i} : pi univ (λ i, Ico (f i) (g i)) =ᵐ[measure.pi μ] Icc f g := by { rw ← pi_univ_Icc, exact pi_Ico_ae_eq_pi_Icc } end intervals /-- If one of the measures `μ i` has no atoms, them `measure.pi µ` has no atoms. The instance below assumes that all `μ i` have no atoms. -/ lemma pi_has_no_atoms (i : ι) [has_no_atoms (μ i)] : has_no_atoms (measure.pi μ) := ⟨λ x, flip measure_mono_null (pi_hyperplane μ i (x i)) (singleton_subset_iff.2 rfl)⟩ instance [h : nonempty ι] [∀ i, has_no_atoms (μ i)] : has_no_atoms (measure.pi μ) := h.elim $ λ i, pi_has_no_atoms i instance [Π i, topological_space (α i)] [∀ i, is_locally_finite_measure (μ i)] : is_locally_finite_measure (measure.pi μ) := begin refine ⟨λ x, _⟩, choose s hxs ho hμ using λ i, (μ i).exists_is_open_measure_lt_top (x i), refine ⟨pi univ s, set_pi_mem_nhds finite_univ (λ i hi, is_open.mem_nhds (ho i) (hxs i)), _⟩, rw [pi_pi], exact ennreal.prod_lt_top (λ i _, (hμ i).ne) end variable (μ) /-- Separating the indices into those that satisfy a predicate `p` and those that don't maps a product measure to a product of product measures. This is useful to apply Fubini to some subset of the variables. The converse is `measure_theory.measure.map_pi_equiv_pi_subtype_prod`. -/ lemma map_pi_equiv_pi_subtype_prod_symm (p : ι → Prop) [decidable_pred p] : map (equiv.pi_equiv_pi_subtype_prod p α).symm (measure.prod (measure.pi (λ i, μ i)) (measure.pi (λ i, μ i))) = measure.pi μ := begin refine (measure.pi_eq (λ s hs, _)).symm, have A : (equiv.pi_equiv_pi_subtype_prod p α).symm ⁻¹' (set.pi set.univ (λ (i : ι), s i)) = (set.pi set.univ (λ i : {i // p i}, s i)) ×ˢ (set.pi set.univ (λ i : {i // ¬p i}, s i)), { ext x, simp only [equiv.pi_equiv_pi_subtype_prod_symm_apply, mem_prod, mem_univ_pi, mem_preimage, subtype.forall], split, { exact λ h, ⟨λ i hi, by simpa [dif_pos hi] using h i, λ i hi, by simpa [dif_neg hi] using h i⟩ }, { assume h i, by_cases hi : p i, { simpa only [dif_pos hi] using h.1 i hi }, {simpa only [dif_neg hi] using h.2 i hi } } }, rw [measure.map_apply (measurable_pi_equiv_pi_subtype_prod_symm _ p) (measurable_set.univ_pi_fintype hs), A, measure.prod_prod, pi_pi, pi_pi, ← fintype.prod_subtype_mul_prod_subtype p (λ i, μ i (s i))], end lemma map_pi_equiv_pi_subtype_prod (p : ι → Prop) [decidable_pred p] : map (equiv.pi_equiv_pi_subtype_prod p α) (measure.pi μ) = measure.prod (measure.pi (λ i, μ i)) (measure.pi (λ i, μ i)) := begin rw [← map_pi_equiv_pi_subtype_prod_symm μ p, measure.map_map (measurable_pi_equiv_pi_subtype_prod _ p) (measurable_pi_equiv_pi_subtype_prod_symm _ p)], simp only [equiv.self_comp_symm, map_id] end end measure instance measure_space.pi [Π i, measure_space (α i)] : measure_space (Π i, α i) := ⟨measure.pi (λ i, volume)⟩ lemma volume_pi [Π i, measure_space (α i)] : (volume : measure (Π i, α i)) = measure.pi (λ i, volume) := rfl lemma volume_pi_pi [Π i, measure_space (α i)] [∀ i, sigma_finite (volume : measure (α i))] (s : Π i, set (α i)) : volume (pi univ s) = ∏ i, volume (s i) := measure.pi_pi (λ i, volume) s lemma volume_pi_ball [Π i, measure_space (α i)] [∀ i, sigma_finite (volume : measure (α i))] [∀ i, metric_space (α i)] (x : Π i, α i) {r : ℝ} (hr : 0 < r) : volume (metric.ball x r) = ∏ i, volume (metric.ball (x i) r) := measure.pi_ball _ _ hr lemma volume_pi_closed_ball [Π i, measure_space (α i)] [∀ i, sigma_finite (volume : measure (α i))] [∀ i, metric_space (α i)] (x : Π i, α i) {r : ℝ} (hr : 0 ≤ r) : volume (metric.closed_ball x r) = ∏ i, volume (metric.closed_ball (x i) r) := measure.pi_closed_ball _ _ hr /-! ### Measure preserving equivalences In this section we prove that some measurable equivalences (e.g., between `fin 1 → α` and `α` or between `fin 2 → α` and `α × α`) preserve measure or volume. These lemmas can be used to prove that measures of corresponding sets (images or preimages) have equal measures and functions `f ∘ e` and `f` have equal integrals, see lemmas in the `measure_theory.measure_preserving` prefix. -/ section measure_preserving lemma measure_preserving_fun_unique {β : Type u} {m : measurable_space β} (μ : measure β) (α : Type v) [unique α] : measure_preserving (measurable_equiv.fun_unique α β) (measure.pi (λ a : α, μ)) μ := begin set e := measurable_equiv.fun_unique α β, have : pi_premeasure (λ _ : α, μ.to_outer_measure) = measure.map e.symm μ, { ext1 s, rw [pi_premeasure, fintype.prod_unique, to_outer_measure_apply, e.symm.map_apply], congr' 1, exact e.to_equiv.image_eq_preimage s }, simp only [measure.pi, outer_measure.pi, this, bounded_by_measure, to_outer_measure_to_measure], exact ((measurable_equiv.fun_unique α β).symm.measurable.measure_preserving _).symm end lemma volume_preserving_fun_unique (α : Type u) (β : Type v) [unique α] [measure_space β] : measure_preserving (measurable_equiv.fun_unique α β) volume volume := measure_preserving_fun_unique volume α lemma measure_preserving_pi_fin_two {α : fin 2 → Type u} {m : Π i, measurable_space (α i)} (μ : Π i, measure (α i)) [∀ i, sigma_finite (μ i)] : measure_preserving (measurable_equiv.pi_fin_two α) (measure.pi μ) ((μ 0).prod (μ 1)) := begin refine ⟨measurable_equiv.measurable _, (measure.prod_eq $ λ s t hs ht, _).symm⟩, rw [measurable_equiv.map_apply, measurable_equiv.pi_fin_two_apply, fin.preimage_apply_01_prod, measure.pi_pi, fin.prod_univ_two], refl end lemma volume_preserving_pi_fin_two (α : fin 2 → Type u) [Π i, measure_space (α i)] [∀ i, sigma_finite (volume : measure (α i))] : measure_preserving (measurable_equiv.pi_fin_two α) volume volume := measure_preserving_pi_fin_two _ lemma measure_preserving_fin_two_arrow_vec {α : Type u} {m : measurable_space α} (μ ν : measure α) [sigma_finite μ] [sigma_finite ν] : measure_preserving measurable_equiv.fin_two_arrow (measure.pi ![μ, ν]) (μ.prod ν) := begin haveI : ∀ i, sigma_finite (![μ, ν] i) := fin.forall_fin_two.2 ⟨‹_›, ‹_›⟩, exact measure_preserving_pi_fin_two _ end lemma measure_preserving_fin_two_arrow {α : Type u} {m : measurable_space α} (μ : measure α) [sigma_finite μ] : measure_preserving measurable_equiv.fin_two_arrow (measure.pi (λ _, μ)) (μ.prod μ) := by simpa only [matrix.vec_single_eq_const, matrix.vec_cons_const] using measure_preserving_fin_two_arrow_vec μ μ lemma volume_preserving_fin_two_arrow (α : Type u) [measure_space α] [sigma_finite (volume : measure α)] : measure_preserving (@measurable_equiv.fin_two_arrow α _) volume volume := measure_preserving_fin_two_arrow volume lemma measure_preserving_pi_empty {ι : Type u} {α : ι → Type v} [is_empty ι] {m : Π i, measurable_space (α i)} (μ : Π i, measure (α i)) : measure_preserving (measurable_equiv.of_unique_of_unique (Π i, α i) unit) (measure.pi μ) (measure.dirac ()) := begin set e := (measurable_equiv.of_unique_of_unique (Π i, α i) unit), refine ⟨e.measurable, _⟩, rw [measure.pi_of_empty, measure.map_dirac e.measurable], refl end lemma volume_preserving_pi_empty {ι : Type u} (α : ι → Type v) [is_empty ι] [Π i, measure_space (α i)] : measure_preserving (measurable_equiv.of_unique_of_unique (Π i, α i) unit) volume volume := measure_preserving_pi_empty (λ _, volume) end measure_preserving end measure_theory
42f0abc6a286b4b931b3f85bacc1ee12d3c92285
2fbe653e4bc441efde5e5d250566e65538709888
/src/algebra/module.lean
83baeed93867b561901c1b357e0d068d018d3365
[ "Apache-2.0" ]
permissive
aceg00/mathlib
5e15e79a8af87ff7eb8c17e2629c442ef24e746b
8786ea6d6d46d6969ac9a869eb818bf100802882
refs/heads/master
1,649,202,698,930
1,580,924,783,000
1,580,924,783,000
149,197,272
0
0
Apache-2.0
1,537,224,208,000
1,537,224,207,000
null
UTF-8
Lean
false
false
18,192
lean
/- Copyright (c) 2015 Nathaniel Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro Modules over a ring. ## Implementation notes Throughout the `linear_map` section implicit `{}` brackets are often used instead of type class `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type `linear_map`. When they can be inferred from the type it is faster to use this method than to use type class inference -/ import algebra.ring algebra.big_operators group_theory.subgroup group_theory.group_action open function universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} -- /-- Typeclass for types with a scalar multiplication operation, denoted `•` (`\bu`) -/ -- class has_scalar (α : Type u) (γ : Type v) := (smul : α → γ → γ) -- infixr ` • `:73 := has_scalar.smul section prio set_option default_priority 100 -- see Note [default priority] /-- A semimodule is a generalization of vector spaces to a scalar semiring. It consists of a scalar semiring `α` and an additive monoid of "vectors" `β`, connected by a "scalar multiplication" operation `r • x : β` (where `r : α` and `x : β`) with some natural associativity and distributivity axioms similar to those on a ring. -/ class semimodule (α : Type u) (β : Type v) [semiring α] [add_comm_monoid β] extends distrib_mul_action α β := (add_smul : ∀(r s : α) (x : β), (r + s) • x = r • x + s • x) (zero_smul : ∀x : β, (0 : α) • x = 0) end prio section semimodule variables [R:semiring α] [add_comm_monoid β] [semimodule α β] (r s : α) (x y : β) include R theorem add_smul : (r + s) • x = r • x + s • x := semimodule.add_smul r s x variables (α) @[simp] theorem zero_smul : (0 : α) • x = 0 := semimodule.zero_smul α x variable {α} lemma semimodule.eq_zero_of_zero_eq_one (zero_eq_one : (0 : α) = 1) : x = 0 := by rw [←one_smul α x, ←zero_eq_one, zero_smul] instance smul.is_add_monoid_hom (x : β) : is_add_monoid_hom (λ r:α, r • x) := { map_zero := zero_smul _ x, map_add := λ r₁ r₂, add_smul r₁ r₂ x } lemma list.sum_smul {l : list α} {x : β} : l.sum • x = (l.map (λ r, r • x)).sum := show (λ r, r • x) l.sum = (l.map (λ r, r • x)).sum, from (list.sum_hom _ _).symm lemma multiset.sum_smul {l : multiset α} {x : β} : l.sum • x = (l.map (λ r, r • x)).sum := show (λ r, r • x) l.sum = (l.map (λ r, r • x)).sum, from (multiset.sum_hom _ _).symm lemma finset.sum_smul {f : γ → α} {s : finset γ} {x : β} : s.sum f • x = s.sum (λ r, (f r) • x) := show (λ r, r • x) (s.sum f) = s.sum (λ r, (f r) • x), from (finset.sum_hom _ _).symm end semimodule section prio set_option default_priority 100 -- see Note [default priority] /-- A module is a generalization of vector spaces to a scalar ring. It consists of a scalar ring `α` and an additive group of "vectors" `β`, connected by a "scalar multiplication" operation `r • x : β` (where `r : α` and `x : β`) with some natural associativity and distributivity axioms similar to those on a ring. -/ class module (α : Type u) (β : Type v) [ring α] [add_comm_group β] extends semimodule α β end prio structure module.core (α β) [ring α] [add_comm_group β] extends has_scalar α β := (smul_add : ∀(r : α) (x y : β), r • (x + y) = r • x + r • y) (add_smul : ∀(r s : α) (x : β), (r + s) • x = r • x + s • x) (mul_smul : ∀(r s : α) (x : β), (r * s) • x = r • s • x) (one_smul : ∀x : β, (1 : α) • x = x) def module.of_core {α β} [ring α] [add_comm_group β] (M : module.core α β) : module α β := by letI := M.to_has_scalar; exact { zero_smul := λ x, have (0 : α) • x + (0 : α) • x = (0 : α) • x + 0, by rw ← M.add_smul; simp, add_left_cancel this, smul_zero := λ r, have r • (0:β) + r • 0 = r • 0 + 0, by rw ← M.smul_add; simp, add_left_cancel this, ..M } section module variables [ring α] [add_comm_group β] [module α β] (r s : α) (x y : β) @[simp] theorem neg_smul : -r • x = - (r • x) := eq_neg_of_add_eq_zero (by rw [← add_smul, add_left_neg, zero_smul]) variables (α) theorem neg_one_smul (x : β) : (-1 : α) • x = -x := by simp variables {α} @[simp] theorem smul_neg : r • (-x) = -(r • x) := by rw [← neg_one_smul α, ← mul_smul, mul_neg_one, neg_smul] theorem smul_sub (r : α) (x y : β) : r • (x - y) = r • x - r • y := by simp [smul_add]; rw smul_neg theorem sub_smul (r s : α) (y : β) : (r - s) • y = r • y - s • y := by simp [add_smul] end module instance semiring.to_semimodule [r : semiring α] : semimodule α α := { smul := (*), smul_add := mul_add, add_smul := add_mul, mul_smul := mul_assoc, one_smul := one_mul, zero_smul := zero_mul, smul_zero := mul_zero, ..r } @[simp] lemma smul_eq_mul [semiring α] {a a' : α} : a • a' = a * a' := rfl instance ring.to_module [r : ring α] : module α α := { ..semiring.to_semimodule } def is_ring_hom.to_module [ring α] [ring β] (f : α → β) [h : is_ring_hom f] : module α β := module.of_core { smul := λ r x, f r * x, smul_add := λ r x y, by unfold has_scalar.smul; rw [mul_add], add_smul := λ r s x, by unfold has_scalar.smul; rw [h.map_add, add_mul], mul_smul := λ r s x, by unfold has_scalar.smul; rw [h.map_mul, mul_assoc], one_smul := λ x, show f 1 * x = _, by rw [h.map_one, one_mul] } class is_linear_map (α : Type u) {β : Type v} {γ : Type w} [ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] (f : β → γ) : Prop := (add : ∀x y, f (x + y) = f x + f y) (smul : ∀(c : α) x, f (c • x) = c • f x) structure linear_map (α : Type u) (β : Type v) (γ : Type w) [ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] := (to_fun : β → γ) (add : ∀x y, to_fun (x + y) = to_fun x + to_fun y) (smul : ∀(c : α) x, to_fun (c • x) = c • to_fun x) infixr ` →ₗ `:25 := linear_map _ notation β ` →ₗ[`:25 α:25 `] `:0 γ:0 := linear_map α β γ namespace linear_map variables {rα : ring α} {gβ : add_comm_group β} {gγ : add_comm_group γ} {gδ : add_comm_group δ} variables {mβ : module α β} {mγ : module α γ} {mδ : module α δ} variables (f g : β →ₗ[α] γ) include α mβ mγ instance : has_coe_to_fun (β →ₗ[α] γ) := ⟨_, to_fun⟩ @[simp] lemma coe_mk (f : β → γ) (h₁ h₂) : ((linear_map.mk f h₁ h₂ : β →ₗ[α] γ) : β → γ) = f := rfl theorem is_linear : is_linear_map α f := {..f} @[ext] theorem ext {f g : β →ₗ[α] γ} (H : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext H theorem ext_iff {f g : β →ₗ[α] γ} : f = g ↔ ∀ x, f x = g x := ⟨by rintro rfl; simp, ext⟩ @[simp] lemma map_add (x y : β) : f (x + y) = f x + f y := f.add x y @[simp] lemma map_smul (c : α) (x : β) : f (c • x) = c • f x := f.smul c x @[simp] lemma map_zero : f 0 = 0 := by rw [← zero_smul α, map_smul f 0 0, zero_smul] instance : is_add_group_hom f := { map_add := map_add f } @[simp] lemma map_neg (x : β) : f (- x) = - f x := by rw [← neg_one_smul α, map_smul, neg_one_smul] @[simp] lemma map_sub (x y : β) : f (x - y) = f x - f y := by simp [map_neg, map_add] @[simp] lemma map_sum {ι} {t : finset ι} {g : ι → β} : f (t.sum g) = t.sum (λi, f (g i)) := (t.sum_hom f).symm include mδ def comp (f : γ →ₗ[α] δ) (g : β →ₗ[α] γ) : β →ₗ[α] δ := ⟨f ∘ g, by simp, by simp⟩ @[simp] lemma comp_apply (f : γ →ₗ[α] δ) (g : β →ₗ[α] γ) (x : β) : f.comp g x = f (g x) := rfl omit mγ mδ variables [rα] [gβ] [mβ] def id : β →ₗ[α] β := ⟨id, by simp, by simp⟩ @[simp] lemma id_apply (x : β) : @id α β _ _ _ x = x := rfl end linear_map namespace is_linear_map variables [ring α] [add_comm_group β] [add_comm_group γ] variables [module α β] [module α γ] include α def mk' (f : β → γ) (H : is_linear_map α f) : β →ₗ γ := {to_fun := f, ..H} @[simp] theorem mk'_apply {f : β → γ} (H : is_linear_map α f) (x : β) : mk' f H x = f x := rfl lemma is_linear_map_neg : is_linear_map α (λ (z : β), -z) := is_linear_map.mk neg_add (λ x y, (smul_neg x y).symm) lemma is_linear_map_smul {α R : Type*} [add_comm_group α] [comm_ring R] [module R α] (c : R) : is_linear_map R (λ (z : α), c • z) := begin refine is_linear_map.mk (smul_add c) _, intros _ _, simp [smul_smul], ac_refl end --TODO: move lemma is_linear_map_smul' {α R : Type*} [add_comm_group α] [ring R] [module R α] (a : α) : is_linear_map R (λ (c : R), c • a) := begin refine is_linear_map.mk (λ x y, add_smul x y a) _, intros _ _, simp [smul_smul] end variables {f : β → γ} (lin : is_linear_map α f) include β γ lin @[simp] lemma map_zero : f (0 : β) = (0 : γ) := by rw [← zero_smul α (0 : β), lin.smul, zero_smul] @[simp] lemma map_add (x y : β) : f (x + y) = f x + f y := by rw [lin.add] @[simp] lemma map_neg (x : β) : f (- x) = - f x := by rw [← neg_one_smul α, lin.smul, neg_one_smul] @[simp] lemma map_sub (x y : β) : f (x - y) = f x - f y := by simp [lin.map_neg, lin.map_add] end is_linear_map abbreviation module.End (R : Type u) (M : Type v) [comm_ring R] [add_comm_group M] [module R M] := M →ₗ[R] M /-- A submodule of a module is one which is closed under vector operations. This is a sufficient condition for the subset of vectors in the submodule to themselves form a module. -/ structure submodule (α : Type u) (β : Type v) [ring α] [add_comm_group β] [module α β] : Type v := (carrier : set β) (zero : (0:β) ∈ carrier) (add : ∀ {x y}, x ∈ carrier → y ∈ carrier → x + y ∈ carrier) (smul : ∀ (c:α) {x}, x ∈ carrier → c • x ∈ carrier) namespace submodule variables [ring α] [add_comm_group β] [add_comm_group γ] variables [module α β] [module α γ] variables (p p' : submodule α β) variables {r : α} {x y : β} instance : has_coe (submodule α β) (set β) := ⟨submodule.carrier⟩ instance : has_mem β (submodule α β) := ⟨λ x p, x ∈ (p : set β)⟩ @[simp] theorem mem_coe : x ∈ (p : set β) ↔ x ∈ p := iff.rfl theorem ext' {s t : submodule α β} (h : (s : set β) = t) : s = t := by cases s; cases t; congr' protected theorem ext'_iff {s t : submodule α β} : (s : set β) = t ↔ s = t := ⟨ext', λ h, h ▸ rfl⟩ @[ext] theorem ext {s t : submodule α β} (h : ∀ x, x ∈ s ↔ x ∈ t) : s = t := ext' $ set.ext h @[simp] lemma zero_mem : (0 : β) ∈ p := p.zero lemma add_mem (h₁ : x ∈ p) (h₂ : y ∈ p) : x + y ∈ p := p.add h₁ h₂ lemma smul_mem (r : α) (h : x ∈ p) : r • x ∈ p := p.smul r h lemma neg_mem (hx : x ∈ p) : -x ∈ p := by rw ← neg_one_smul α; exact p.smul_mem _ hx lemma sub_mem (hx : x ∈ p) (hy : y ∈ p) : x - y ∈ p := p.add_mem hx (p.neg_mem hy) lemma neg_mem_iff : -x ∈ p ↔ x ∈ p := ⟨λ h, by simpa using neg_mem p h, neg_mem p⟩ lemma add_mem_iff_left (h₁ : y ∈ p) : x + y ∈ p ↔ x ∈ p := ⟨λ h₂, by simpa using sub_mem _ h₂ h₁, λ h₂, add_mem _ h₂ h₁⟩ lemma add_mem_iff_right (h₁ : x ∈ p) : x + y ∈ p ↔ y ∈ p := ⟨λ h₂, by simpa using sub_mem _ h₂ h₁, add_mem _ h₁⟩ lemma sum_mem {ι : Type w} [decidable_eq ι] {t : finset ι} {f : ι → β} : (∀c∈t, f c ∈ p) → t.sum f ∈ p := finset.induction_on t (by simp [p.zero_mem]) (by simp [p.add_mem] {contextual := tt}) instance : has_add p := ⟨λx y, ⟨x.1 + y.1, add_mem _ x.2 y.2⟩⟩ instance : has_zero p := ⟨⟨0, zero_mem _⟩⟩ instance : inhabited p := ⟨0⟩ instance : has_neg p := ⟨λx, ⟨-x.1, neg_mem _ x.2⟩⟩ instance : has_scalar α p := ⟨λ c x, ⟨c • x.1, smul_mem _ c x.2⟩⟩ @[simp, move_cast] lemma coe_add (x y : p) : (↑(x + y) : β) = ↑x + ↑y := rfl @[simp, elim_cast] lemma coe_zero : ((0 : p) : β) = 0 := rfl @[simp, move_cast] lemma coe_neg (x : p) : ((-x : p) : β) = -x := rfl @[simp, move_cast] lemma coe_smul (r : α) (x : p) : ((r • x : p) : β) = r • ↑x := rfl instance : add_comm_group p := by refine {add := (+), zero := 0, neg := has_neg.neg, ..}; { intros, apply set_coe.ext, simp } instance submodule_is_add_subgroup : is_add_subgroup (p : set β) := { zero_mem := p.zero, add_mem := p.add, neg_mem := λ _, p.neg_mem } @[move_cast] lemma coe_sub (x y : p) : (↑(x - y) : β) = ↑x - ↑y := by simp instance : module α p := by refine {smul := (•), ..}; { intros, apply set_coe.ext, simp [smul_add, add_smul, mul_smul] } protected def subtype : p →ₗ[α] β := by refine {to_fun := coe, ..}; simp [coe_smul] @[simp] theorem subtype_apply (x : p) : p.subtype x = x := rfl lemma subtype_eq_val (p : submodule α β) : ((submodule.subtype p) : p → β) = subtype.val := rfl end submodule @[reducible] def ideal (α : Type u) [comm_ring α] := submodule α α namespace ideal variables [comm_ring α] (I : ideal α) {a b : α} protected lemma zero_mem : (0 : α) ∈ I := I.zero_mem protected lemma add_mem : a ∈ I → b ∈ I → a + b ∈ I := I.add_mem lemma neg_mem_iff : -a ∈ I ↔ a ∈ I := I.neg_mem_iff lemma add_mem_iff_left : b ∈ I → (a + b ∈ I ↔ a ∈ I) := I.add_mem_iff_left lemma add_mem_iff_right : a ∈ I → (a + b ∈ I ↔ b ∈ I) := I.add_mem_iff_right protected lemma sub_mem : a ∈ I → b ∈ I → a - b ∈ I := I.sub_mem lemma mul_mem_left : b ∈ I → a * b ∈ I := I.smul_mem _ lemma mul_mem_right (h : a ∈ I) : a * b ∈ I := mul_comm b a ▸ I.mul_mem_left h end ideal library_note "vector space definition" "Vector spaces are defined as an `abbreviation` for modules, if the base ring is a field. (A previous definition made `vector_space` a structure defined to be `module`.) This has as advantage that vector spaces are completely transparant for type class inference, which means that all instances for modules are immediately picked up for vector spaces as well. A cosmetic disadvantage is that one can not extend vector spaces an sich, in definitions such as `normed_space`. The solution is to extend `module` instead." /-- A vector space is the same as a module, except the scalar ring is actually a field. (This adds commutativity of the multiplication and existence of inverses.) This is the traditional generalization of spaces like `ℝ^n`, which have a natural addition operation and a way to multiply them by real numbers, but no multiplication operation between vectors. -/ abbreviation vector_space (α : Type u) (β : Type v) [discrete_field α] [add_comm_group β] := module α β instance discrete_field.to_vector_space {α : Type*} [discrete_field α] : vector_space α α := { .. ring.to_module } /-- Subspace of a vector space. Defined to equal `submodule`. -/ @[reducible] def subspace (α : Type u) (β : Type v) [discrete_field α] [add_comm_group β] [vector_space α β] : Type v := submodule α β instance subspace.vector_space {α β} {f : discrete_field α} [add_comm_group β] [vector_space α β] (p : subspace α β) : vector_space α p := {..submodule.module p} namespace submodule variables {R:discrete_field α} [add_comm_group β] [add_comm_group γ] variables [vector_space α β] [vector_space α γ] variables (p p' : submodule α β) variables {r : α} {x y : β} include R set_option class.instance_max_depth 36 theorem smul_mem_iff (r0 : r ≠ 0) : r • x ∈ p ↔ x ∈ p := ⟨λ h, by simpa [smul_smul, inv_mul_cancel r0] using p.smul_mem (r⁻¹) h, p.smul_mem r⟩ end submodule namespace add_comm_monoid open add_monoid variables {M : Type*} [add_comm_monoid M] instance : semimodule ℕ M := { smul := smul, smul_add := λ _ _ _, smul_add _ _ _, add_smul := λ _ _ _, add_smul _ _ _, mul_smul := λ _ _ _, mul_smul _ _ _, one_smul := one_smul, zero_smul := zero_smul, smul_zero := smul_zero } end add_comm_monoid namespace add_comm_group variables {M : Type*} [add_comm_group M] instance : module ℤ M := { smul := gsmul, smul_add := λ _ _ _, gsmul_add _ _ _, add_smul := λ _ _ _, add_gsmul _ _ _, mul_smul := λ _ _ _, gsmul_mul _ _ _, one_smul := one_gsmul, zero_smul := zero_gsmul, smul_zero := gsmul_zero } end add_comm_group lemma gsmul_eq_smul {M : Type*} [add_comm_group M] (n : ℤ) (x : M) : gsmul n x = n • x := rfl def is_add_group_hom.to_linear_map [add_comm_group α] [add_comm_group β] (f : α → β) [is_add_group_hom f] : α →ₗ[ℤ] β := { to_fun := f, add := is_add_hom.map_add f, smul := λ i x, int.induction_on i (by rw [zero_smul, zero_smul, is_add_group_hom.map_zero f]) (λ i ih, by rw [add_smul, add_smul, is_add_hom.map_add f, ih, one_smul, one_smul]) (λ i ih, by rw [sub_smul, sub_smul, is_add_group_hom.map_sub f, ih, one_smul, one_smul]) } lemma module.smul_eq_smul {R : Type*} [ring R] {β : Type*} [add_comm_group β] [module R β] (n : ℕ) (b : β) : n • b = (n : R) • b := begin induction n with n ih, { rw [nat.cast_zero, zero_smul, zero_smul] }, { change (n + 1) • b = (n + 1 : R) • b, rw [add_smul, add_smul, one_smul, ih, one_smul] } end lemma nat.smul_def {M : Type*} [add_comm_monoid M] (n : ℕ) (x : M) : n • x = add_monoid.smul n x := rfl namespace finset lemma sum_const' {α : Type*} (R : Type*) [ring R] {β : Type*} [add_comm_group β] [module R β] {s : finset α} (b : β) : finset.sum s (λ (a : α), b) = (finset.card s : R) • b := by rw [finset.sum_const, ← module.smul_eq_smul]; refl variables {M : Type*} [decidable_linear_ordered_cancel_comm_monoid M] {s : finset α} (f : α → M) theorem exists_card_smul_le_sum (hs : s.nonempty) : ∃ i ∈ s, s.card • f i ≤ s.sum f := exists_le_of_sum_le hs $ by rw [sum_const, ← nat.smul_def, smul_sum] theorem exists_card_smul_ge_sum (hs : s.nonempty) : ∃ i ∈ s, s.sum f ≤ s.card • f i := exists_le_of_sum_le hs $ by rw [sum_const, ← nat.smul_def, smul_sum] end finset
cda3c3152e041b0317eb282ff5da145986890369
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/topology/continuous_function/bounded.lean
0b0cf93d3dbb6b5b00eb0fbf57ee06ab870fd094
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
42,479
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Mario Carneiro, Yury Kudryashov, Heather Macbeth -/ import analysis.normed_space.operator_norm import topology.continuous_function.algebra /-! # Bounded continuous functions The type of bounded continuous functions taking values in a metric space, with the uniform distance. -/ noncomputable theory open_locale topological_space classical nnreal open set filter metric universes u v w variables {α : Type u} {β : Type v} {γ : Type w} /-- The type of bounded continuous functions from a topological space to a metric space -/ structure bounded_continuous_function (α : Type u) (β : Type v) [topological_space α] [metric_space β] extends continuous_map α β : Type (max u v) := (bounded' : ∃C, ∀x y:α, dist (to_fun x) (to_fun y) ≤ C) localized "infixr ` →ᵇ `:25 := bounded_continuous_function" in bounded_continuous_function namespace bounded_continuous_function section basics variables [topological_space α] [metric_space β] [metric_space γ] variables {f g : α →ᵇ β} {x : α} {C : ℝ} instance : has_coe_to_fun (α →ᵇ β) := ⟨_, λ f, f.to_fun⟩ protected lemma bounded (f : α →ᵇ β) : ∃C, ∀ x y : α, dist (f x) (f y) ≤ C := f.bounded' @[continuity] protected lemma continuous (f : α →ᵇ β) : continuous f := f.to_continuous_map.continuous @[ext] lemma ext (H : ∀x, f x = g x) : f = g := by { cases f, cases g, congr, ext, exact H x, } lemma ext_iff : f = g ↔ ∀ x, f x = g x := ⟨λ h, λ x, h ▸ rfl, ext⟩ lemma bounded_range : bounded (range f) := bounded_range_iff.2 f.bounded /-- A continuous function with an explicit bound is a bounded continuous function. -/ def mk_of_bound (f : C(α, β)) (C : ℝ) (h : ∀ x y : α, dist (f x) (f y) ≤ C) : α →ᵇ β := ⟨f, ⟨C, h⟩⟩ @[simp] lemma mk_of_bound_coe {f} {C} {h} : (mk_of_bound f C h : α → β) = (f : α → β) := rfl /-- A continuous function on a compact space is automatically a bounded continuous function. -/ def mk_of_compact [compact_space α] (f : C(α, β)) : α →ᵇ β := ⟨f, bounded_range_iff.1 (is_compact_range f.continuous).bounded⟩ @[simp] lemma mk_of_compact_apply [compact_space α] (f : C(α, β)) (a : α) : mk_of_compact f a = f a := rfl /-- If a function is bounded on a discrete space, it is automatically continuous, and therefore gives rise to an element of the type of bounded continuous functions -/ def mk_of_discrete [discrete_topology α] (f : α → β) (C : ℝ) (h : ∀ x y : α, dist (f x) (f y) ≤ C) : α →ᵇ β := ⟨⟨f, continuous_of_discrete_topology⟩, ⟨C, h⟩⟩ @[simp] lemma mk_of_discrete_apply [discrete_topology α] (f : α → β) (C) (h) (a : α) : mk_of_discrete f C h a = f a := rfl section variables (α β) /-- The map forgetting that a bounded continuous function is bounded. -/ def forget_boundedness : (α →ᵇ β) → C(α, β) := λ f, f.1 @[simp] lemma forget_boundedness_coe (f : α →ᵇ β) : (forget_boundedness α β f : α → β) = f := rfl end /-- The uniform distance between two bounded continuous functions -/ instance : has_dist (α →ᵇ β) := ⟨λf g, Inf {C | 0 ≤ C ∧ ∀ x : α, dist (f x) (g x) ≤ C}⟩ lemma dist_eq : dist f g = Inf {C | 0 ≤ C ∧ ∀ x : α, dist (f x) (g x) ≤ C} := rfl lemma dist_set_exists : ∃ C, 0 ≤ C ∧ ∀ x : α, dist (f x) (g x) ≤ C := begin refine if h : nonempty α then _ else ⟨0, le_refl _, λ x, h.elim ⟨x⟩⟩, cases h with x, rcases f.bounded with ⟨Cf, hCf : ∀ x y, dist (f x) (f y) ≤ Cf⟩, rcases g.bounded with ⟨Cg, hCg : ∀ x y, dist (g x) (g y) ≤ Cg⟩, let C := max 0 (dist (f x) (g x) + (Cf + Cg)), refine ⟨C, le_max_left _ _, λ y, _⟩, calc dist (f y) (g y) ≤ dist (f x) (g x) + (dist (f x) (f y) + dist (g x) (g y)) : dist_triangle4_left _ _ _ _ ... ≤ dist (f x) (g x) + (Cf + Cg) : by mono* ... ≤ C : le_max_right _ _ end /-- The pointwise distance is controlled by the distance between functions, by definition. -/ lemma dist_coe_le_dist (x : α) : dist (f x) (g x) ≤ dist f g := le_cInf dist_set_exists $ λb hb, hb.2 x /- This lemma will be needed in the proof of the metric space instance, but it will become useless afterwards as it will be superseded by the general result that the distance is nonnegative in metric spaces. -/ private lemma dist_nonneg' : 0 ≤ dist f g := le_cInf dist_set_exists (λ C, and.left) /-- The distance between two functions is controlled by the supremum of the pointwise distances -/ lemma dist_le (C0 : (0 : ℝ) ≤ C) : dist f g ≤ C ↔ ∀x:α, dist (f x) (g x) ≤ C := ⟨λ h x, le_trans (dist_coe_le_dist x) h, λ H, cInf_le ⟨0, λ C, and.left⟩ ⟨C0, H⟩⟩ lemma dist_le_iff_of_nonempty [nonempty α] : dist f g ≤ C ↔ ∀ x, dist (f x) (g x) ≤ C := ⟨λ h x, le_trans (dist_coe_le_dist x) h, λ w, (dist_le (le_trans dist_nonneg (w (nonempty.some ‹_›)))).mpr w⟩ lemma dist_lt_of_nonempty_compact [nonempty α] [compact_space α] (w : ∀x:α, dist (f x) (g x) < C) : dist f g < C := begin have c : continuous (λ x, dist (f x) (g x)), { continuity, }, obtain ⟨x, -, le⟩ := is_compact.exists_forall_ge compact_univ set.univ_nonempty (continuous.continuous_on c), exact lt_of_le_of_lt (dist_le_iff_of_nonempty.mpr (λ y, le y trivial)) (w x), end lemma dist_lt_iff_of_compact [compact_space α] (C0 : (0 : ℝ) < C) : dist f g < C ↔ ∀x:α, dist (f x) (g x) < C := begin fsplit, { intros w x, exact lt_of_le_of_lt (dist_coe_le_dist x) w, }, { by_cases h : nonempty α, { resetI, exact dist_lt_of_nonempty_compact, }, { rintro -, convert C0, apply le_antisymm _ dist_nonneg', rw [dist_eq], exact cInf_le ⟨0, λ C, and.left⟩ ⟨le_refl _, λ x, false.elim (h (nonempty.intro x))⟩, }, }, end lemma dist_lt_iff_of_nonempty_compact [nonempty α] [compact_space α] : dist f g < C ↔ ∀x:α, dist (f x) (g x) < C := ⟨λ w x, lt_of_le_of_lt (dist_coe_le_dist x) w, dist_lt_of_nonempty_compact⟩ /-- On an empty space, bounded continuous functions are at distance 0 -/ lemma dist_zero_of_empty [is_empty α] : dist f g = 0 := le_antisymm ((dist_le (le_refl _)).2 is_empty_elim) dist_nonneg' /-- The type of bounded continuous functions, with the uniform distance, is a metric space. -/ instance : metric_space (α →ᵇ β) := { dist_self := λ f, le_antisymm ((dist_le (le_refl _)).2 $ λ x, by simp) dist_nonneg', eq_of_dist_eq_zero := λ f g hfg, by ext x; exact eq_of_dist_eq_zero (le_antisymm (hfg ▸ dist_coe_le_dist _) dist_nonneg), dist_comm := λ f g, by simp [dist_eq, dist_comm], dist_triangle := λ f g h, (dist_le (add_nonneg dist_nonneg' dist_nonneg')).2 $ λ x, le_trans (dist_triangle _ _ _) (add_le_add (dist_coe_le_dist _) (dist_coe_le_dist _)) } variables (α) {β} /-- Constant as a continuous bounded function. -/ def const (b : β) : α →ᵇ β := ⟨continuous_map.const b, 0, by simp [le_refl]⟩ variable {α} @[simp] lemma coe_const (b : β) : ⇑(const α b) = function.const α b := rfl lemma const_apply (a : α) (b : β) : (const α b : α → β) a = b := rfl /-- If the target space is inhabited, so is the space of bounded continuous functions -/ instance [inhabited β] : inhabited (α →ᵇ β) := ⟨const α (default β)⟩ /-- The evaluation map is continuous, as a joint function of `u` and `x` -/ @[continuity] theorem continuous_eval : continuous (λ p : (α →ᵇ β) × α, p.1 p.2) := continuous_iff'.2 $ λ ⟨f, x⟩ ε ε0, /- use the continuity of `f` to find a neighborhood of `x` where it varies at most by ε/2 -/ have Hs : _ := continuous_iff'.1 f.continuous x (ε/2) (half_pos ε0), mem_of_superset (prod_is_open.mem_nhds (ball_mem_nhds _ (half_pos ε0)) Hs) $ λ ⟨g, y⟩ ⟨hg, hy⟩, calc dist (g y) (f x) ≤ dist (g y) (f y) + dist (f y) (f x) : dist_triangle _ _ _ ... < ε/2 + ε/2 : add_lt_add (lt_of_le_of_lt (dist_coe_le_dist _) hg) hy ... = ε : add_halves _ /-- In particular, when `x` is fixed, `f → f x` is continuous -/ @[continuity] theorem continuous_evalx {x : α} : continuous (λ f : α →ᵇ β, f x) := continuous_eval.comp (continuous_id.prod_mk continuous_const) /-- Bounded continuous functions taking values in a complete space form a complete space. -/ instance [complete_space β] : complete_space (α →ᵇ β) := complete_of_cauchy_seq_tendsto $ λ (f : ℕ → α →ᵇ β) (hf : cauchy_seq f), begin /- We have to show that `f n` converges to a bounded continuous function. For this, we prove pointwise convergence to define the limit, then check it is a continuous bounded function, and then check the norm convergence. -/ rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩, have f_bdd := λx n m N hn hm, le_trans (dist_coe_le_dist x) (b_bound n m N hn hm), have fx_cau : ∀x, cauchy_seq (λn, f n x) := λx, cauchy_seq_iff_le_tendsto_0.2 ⟨b, b0, f_bdd x, b_lim⟩, choose F hF using λx, cauchy_seq_tendsto_of_complete (fx_cau x), /- F : α → β, hF : ∀ (x : α), tendsto (λ (n : ℕ), f n x) at_top (𝓝 (F x)) `F` is the desired limit function. Check that it is uniformly approximated by `f N` -/ have fF_bdd : ∀x N, dist (f N x) (F x) ≤ b N := λ x N, le_of_tendsto (tendsto_const_nhds.dist (hF x)) (filter.eventually_at_top.2 ⟨N, λn hn, f_bdd x N n N (le_refl N) hn⟩), refine ⟨⟨⟨F, _⟩, _⟩, _⟩, { /- Check that `F` is continuous, as a uniform limit of continuous functions -/ have : tendsto_uniformly (λn x, f n x) F at_top, { refine metric.tendsto_uniformly_iff.2 (λ ε ε0, _), refine ((tendsto_order.1 b_lim).2 ε ε0).mono (λ n hn x, _), rw dist_comm, exact lt_of_le_of_lt (fF_bdd x n) hn }, exact this.continuous (λN, (f N).continuous) }, { /- Check that `F` is bounded -/ rcases (f 0).bounded with ⟨C, hC⟩, refine ⟨C + (b 0 + b 0), λ x y, _⟩, calc dist (F x) (F y) ≤ dist (f 0 x) (f 0 y) + (dist (f 0 x) (F x) + dist (f 0 y) (F y)) : dist_triangle4_left _ _ _ _ ... ≤ C + (b 0 + b 0) : by mono* }, { /- Check that `F` is close to `f N` in distance terms -/ refine tendsto_iff_dist_tendsto_zero.2 (squeeze_zero (λ _, dist_nonneg) _ b_lim), exact λ N, (dist_le (b0 _)).2 (λx, fF_bdd x N) } end /-- Composition (in the target) of a bounded continuous function with a Lipschitz map again gives a bounded continuous function -/ def comp (G : β → γ) {C : ℝ≥0} (H : lipschitz_with C G) (f : α →ᵇ β) : α →ᵇ γ := ⟨⟨λx, G (f x), H.continuous.comp f.continuous⟩, let ⟨D, hD⟩ := f.bounded in ⟨max C 0 * D, λ x y, calc dist (G (f x)) (G (f y)) ≤ C * dist (f x) (f y) : H.dist_le_mul _ _ ... ≤ max C 0 * dist (f x) (f y) : mul_le_mul_of_nonneg_right (le_max_left C 0) dist_nonneg ... ≤ max C 0 * D : mul_le_mul_of_nonneg_left (hD _ _) (le_max_right C 0)⟩⟩ /-- The composition operator (in the target) with a Lipschitz map is Lipschitz -/ lemma lipschitz_comp {G : β → γ} {C : ℝ≥0} (H : lipschitz_with C G) : lipschitz_with C (comp G H : (α →ᵇ β) → α →ᵇ γ) := lipschitz_with.of_dist_le_mul $ λ f g, (dist_le (mul_nonneg C.2 dist_nonneg)).2 $ λ x, calc dist (G (f x)) (G (g x)) ≤ C * dist (f x) (g x) : H.dist_le_mul _ _ ... ≤ C * dist f g : mul_le_mul_of_nonneg_left (dist_coe_le_dist _) C.2 /-- The composition operator (in the target) with a Lipschitz map is uniformly continuous -/ lemma uniform_continuous_comp {G : β → γ} {C : ℝ≥0} (H : lipschitz_with C G) : uniform_continuous (comp G H : (α →ᵇ β) → α →ᵇ γ) := (lipschitz_comp H).uniform_continuous /-- The composition operator (in the target) with a Lipschitz map is continuous -/ lemma continuous_comp {G : β → γ} {C : ℝ≥0} (H : lipschitz_with C G) : continuous (comp G H : (α →ᵇ β) → α →ᵇ γ) := (lipschitz_comp H).continuous /-- Restriction (in the target) of a bounded continuous function taking values in a subset -/ def cod_restrict (s : set β) (f : α →ᵇ β) (H : ∀x, f x ∈ s) : α →ᵇ s := ⟨⟨s.cod_restrict f H, continuous_subtype_mk _ f.continuous⟩, f.bounded⟩ end basics section arzela_ascoli variables [topological_space α] [compact_space α] [metric_space β] variables {f g : α →ᵇ β} {x : α} {C : ℝ} /- Arzela-Ascoli theorem asserts that, on a compact space, a set of functions sharing a common modulus of continuity and taking values in a compact set forms a compact subset for the topology of uniform convergence. In this section, we prove this theorem and several useful variations around it. -/ /-- First version, with pointwise equicontinuity and range in a compact space -/ theorem arzela_ascoli₁ [compact_space β] (A : set (α →ᵇ β)) (closed : is_closed A) (H : ∀ (x:α) (ε > 0), ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : is_compact A := begin refine compact_of_totally_bounded_is_closed _ closed, refine totally_bounded_of_finite_discretization (λ ε ε0, _), rcases exists_between ε0 with ⟨ε₁, ε₁0, εε₁⟩, let ε₂ := ε₁/2/2, /- We have to find a finite discretization of `u`, i.e., finite information that is sufficient to reconstruct `u` up to ε. This information will be provided by the values of `u` on a sufficiently dense set tα, slightly translated to fit in a finite ε₂-dense set tβ in the image. Such sets exist by compactness of the source and range. Then, to check that these data determine the function up to ε, one uses the control on the modulus of continuity to extend the closeness on tα to closeness everywhere. -/ have ε₂0 : ε₂ > 0 := half_pos (half_pos ε₁0), have : ∀x:α, ∃U, x ∈ U ∧ is_open U ∧ ∀ (y z ∈ U) {f : α →ᵇ β}, f ∈ A → dist (f y) (f z) < ε₂ := λ x, let ⟨U, nhdsU, hU⟩ := H x _ ε₂0, ⟨V, VU, openV, xV⟩ := _root_.mem_nhds_iff.1 nhdsU in ⟨V, xV, openV, λy z hy hz f hf, hU y z (VU hy) (VU hz) f hf⟩, choose U hU using this, /- For all x, the set hU x is an open set containing x on which the elements of A fluctuate by at most ε₂. We extract finitely many of these sets that cover the whole space, by compactness -/ rcases compact_univ.elim_finite_subcover_image (λx _, (hU x).2.1) (λx hx, mem_bUnion (mem_univ _) (hU x).1) with ⟨tα, _, ⟨_⟩, htα⟩, /- tα : set α, htα : univ ⊆ ⋃x ∈ tα, U x -/ rcases @finite_cover_balls_of_compact β _ _ compact_univ _ ε₂0 with ⟨tβ, _, ⟨_⟩, htβ⟩, resetI, /- tβ : set β, htβ : univ ⊆ ⋃y ∈ tβ, ball y ε₂ -/ /- Associate to every point `y` in the space a nearby point `F y` in tβ -/ choose F hF using λy, show ∃z∈tβ, dist y z < ε₂, by simpa using htβ (mem_univ y), /- F : β → β, hF : ∀ (y : β), F y ∈ tβ ∧ dist y (F y) < ε₂ -/ /- Associate to every function a discrete approximation, mapping each point in `tα` to a point in `tβ` close to its true image by the function. -/ refine ⟨tα → tβ, by apply_instance, λ f a, ⟨F (f a), (hF (f a)).1⟩, _⟩, rintro ⟨f, hf⟩ ⟨g, hg⟩ f_eq_g, /- If two functions have the same approximation, then they are within distance ε -/ refine lt_of_le_of_lt ((dist_le $ le_of_lt ε₁0).2 (λ x, _)) εε₁, obtain ⟨x', x'tα, hx'⟩ : ∃x' ∈ tα, x ∈ U x' := mem_bUnion_iff.1 (htα (mem_univ x)), refine calc dist (f x) (g x) ≤ dist (f x) (f x') + dist (g x) (g x') + dist (f x') (g x') : dist_triangle4_right _ _ _ _ ... ≤ ε₂ + ε₂ + ε₁/2 : le_of_lt (add_lt_add (add_lt_add _ _) _) ... = ε₁ : by rw [add_halves, add_halves], { exact (hU x').2.2 _ _ hx' ((hU x').1) hf }, { exact (hU x').2.2 _ _ hx' ((hU x').1) hg }, { have F_f_g : F (f x') = F (g x') := (congr_arg (λ f:tα → tβ, (f ⟨x', x'tα⟩ : β)) f_eq_g : _), calc dist (f x') (g x') ≤ dist (f x') (F (f x')) + dist (g x') (F (f x')) : dist_triangle_right _ _ _ ... = dist (f x') (F (f x')) + dist (g x') (F (g x')) : by rw F_f_g ... < ε₂ + ε₂ : add_lt_add (hF (f x')).2 (hF (g x')).2 ... = ε₁/2 : add_halves _ } end /-- Second version, with pointwise equicontinuity and range in a compact subset -/ theorem arzela_ascoli₂ (s : set β) (hs : is_compact s) (A : set (α →ᵇ β)) (closed : is_closed A) (in_s : ∀(f : α →ᵇ β) (x : α), f ∈ A → f x ∈ s) (H : ∀(x:α) (ε > 0), ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : is_compact A := /- This version is deduced from the previous one by restricting to the compact type in the target, using compactness there and then lifting everything to the original space. -/ begin have M : lipschitz_with 1 coe := lipschitz_with.subtype_coe s, let F : (α →ᵇ s) → α →ᵇ β := comp coe M, refine compact_of_is_closed_subset ((_ : is_compact (F ⁻¹' A)).image (continuous_comp M)) closed (λ f hf, _), { haveI : compact_space s := is_compact_iff_compact_space.1 hs, refine arzela_ascoli₁ _ (continuous_iff_is_closed.1 (continuous_comp M) _ closed) (λ x ε ε0, bex.imp_right (λ U U_nhds hU y z hy hz f hf, _) (H x ε ε0)), calc dist (f y) (f z) = dist (F f y) (F f z) : rfl ... < ε : hU y z hy hz (F f) hf }, { let g := cod_restrict s f (λx, in_s f x hf), rw [show f = F g, by ext; refl] at hf ⊢, exact ⟨g, hf, rfl⟩ } end /-- Third (main) version, with pointwise equicontinuity and range in a compact subset, but without closedness. The closure is then compact -/ theorem arzela_ascoli (s : set β) (hs : is_compact s) (A : set (α →ᵇ β)) (in_s : ∀(f : α →ᵇ β) (x : α), f ∈ A → f x ∈ s) (H : ∀(x:α) (ε > 0), ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε) : is_compact (closure A) := /- This version is deduced from the previous one by checking that the closure of A, in addition to being closed, still satisfies the properties of compact range and equicontinuity -/ arzela_ascoli₂ s hs (closure A) is_closed_closure (λ f x hf, (mem_of_closed' hs.is_closed).2 $ λ ε ε0, let ⟨g, gA, dist_fg⟩ := metric.mem_closure_iff.1 hf ε ε0 in ⟨g x, in_s g x gA, lt_of_le_of_lt (dist_coe_le_dist _) dist_fg⟩) (λ x ε ε0, show ∃ U ∈ 𝓝 x, ∀ y z ∈ U, ∀ (f : α →ᵇ β), f ∈ closure A → dist (f y) (f z) < ε, begin refine bex.imp_right (λ U U_set hU y z hy hz f hf, _) (H x (ε/2) (half_pos ε0)), rcases metric.mem_closure_iff.1 hf (ε/2/2) (half_pos (half_pos ε0)) with ⟨g, gA, dist_fg⟩, replace dist_fg := λ x, lt_of_le_of_lt (dist_coe_le_dist x) dist_fg, calc dist (f y) (f z) ≤ dist (f y) (g y) + dist (f z) (g z) + dist (g y) (g z) : dist_triangle4_right _ _ _ _ ... < ε/2/2 + ε/2/2 + ε/2 : add_lt_add (add_lt_add (dist_fg y) (dist_fg z)) (hU y z hy hz g gA) ... = ε : by rw [add_halves, add_halves] end) /- To apply the previous theorems, one needs to check the equicontinuity. An important instance is when the source space is a metric space, and there is a fixed modulus of continuity for all the functions in the set A -/ lemma equicontinuous_of_continuity_modulus {α : Type u} [metric_space α] (b : ℝ → ℝ) (b_lim : tendsto b (𝓝 0) (𝓝 0)) (A : set (α →ᵇ β)) (H : ∀(x y:α) (f : α →ᵇ β), f ∈ A → dist (f x) (f y) ≤ b (dist x y)) (x:α) (ε : ℝ) (ε0 : 0 < ε) : ∃U ∈ 𝓝 x, ∀ (y z ∈ U) (f : α →ᵇ β), f ∈ A → dist (f y) (f z) < ε := begin rcases tendsto_nhds_nhds.1 b_lim ε ε0 with ⟨δ, δ0, hδ⟩, refine ⟨ball x (δ/2), ball_mem_nhds x (half_pos δ0), λ y z hy hz f hf, _⟩, have : dist y z < δ := calc dist y z ≤ dist y x + dist z x : dist_triangle_right _ _ _ ... < δ/2 + δ/2 : add_lt_add hy hz ... = δ : add_halves _, calc dist (f y) (f z) ≤ b (dist y z) : H y z f hf ... ≤ |b (dist y z)| : le_abs_self _ ... = dist (b (dist y z)) 0 : by simp [real.dist_eq] ... < ε : hδ (by simpa [real.dist_eq] using this), end end arzela_ascoli section has_lipschitz_add /- In this section, if `β` is an `add_monoid` whose addition operation is Lipschitz, then we show that the space of bounded continuous functions from `α` to `β` inherits a topological `add_monoid` structure, by using pointwise operations and checking that they are compatible with the uniform distance. Implementation note: The material in this section could have been written for `has_lipschitz_mul` and transported by `@[to_additive]`. We choose not to do this because this causes a few lemma names (for example, `coe_mul`) to conflict with later lemma names for normed rings; this is only a trivial inconvenience, but in any case there are no obvious applications of the multiplicative version. -/ variables [topological_space α] [metric_space β] [add_monoid β] instance : has_zero (α →ᵇ β) := ⟨const α 0⟩ @[simp] lemma coe_zero : ((0 : α →ᵇ β) : α → β) = 0 := rfl lemma forall_coe_zero_iff_zero (f : α →ᵇ β) : (∀x, f x = 0) ↔ f = 0 := (@ext_iff _ _ _ _ f 0).symm variables [has_lipschitz_add β] variables (f g : α →ᵇ β) {x : α} {C : ℝ} /-- The pointwise sum of two bounded continuous functions is again bounded continuous. -/ instance : has_add (α →ᵇ β) := { add := λ f g, bounded_continuous_function.mk_of_bound (f.to_continuous_map + g.to_continuous_map) (↑(has_lipschitz_add.C β) * max (classical.some f.bounded) (classical.some g.bounded)) begin intros x y, refine le_trans (lipschitz_with_lipschitz_const_add ⟨f x, g x⟩ ⟨f y, g y⟩) _, rw prod.dist_eq, refine mul_le_mul_of_nonneg_left _ (has_lipschitz_add.C β).coe_nonneg, apply max_le_max, exact classical.some_spec f.bounded x y, exact classical.some_spec g.bounded x y, end } @[simp] lemma coe_add : ⇑(f + g) = f + g := rfl lemma add_apply : (f + g) x = f x + g x := rfl instance : add_monoid (α →ᵇ β) := { add_assoc := assume f g h, by ext; simp [add_assoc], zero_add := assume f, by ext; simp, add_zero := assume f, by ext; simp, .. bounded_continuous_function.has_add, .. bounded_continuous_function.has_zero } instance : has_lipschitz_add (α →ᵇ β) := { lipschitz_add := ⟨has_lipschitz_add.C β, begin have C_nonneg := (has_lipschitz_add.C β).coe_nonneg, rw lipschitz_with_iff_dist_le_mul, rintros ⟨f₁, g₁⟩ ⟨f₂, g₂⟩, rw dist_le (mul_nonneg C_nonneg dist_nonneg), intros x, refine le_trans (lipschitz_with_lipschitz_const_add ⟨f₁ x, g₁ x⟩ ⟨f₂ x, g₂ x⟩) _, refine mul_le_mul_of_nonneg_left _ C_nonneg, apply max_le_max; exact dist_coe_le_dist x, end⟩ } /-- Coercion of a `normed_group_hom` is an `add_monoid_hom`. Similar to `add_monoid_hom.coe_fn` -/ @[simps] def coe_fn_add_hom : (α →ᵇ β) →+ (α → β) := { to_fun := coe_fn, map_zero' := coe_zero, map_add' := coe_add } variables (α β) /-- The additive map forgetting that a bounded continuous function is bounded. -/ @[simps] def forget_boundedness_add_hom : (α →ᵇ β) →+ C(α, β) := { to_fun := forget_boundedness α β, map_zero' := by { ext, simp, }, map_add' := by { intros, ext, simp, }, } end has_lipschitz_add section comm_has_lipschitz_add variables [topological_space α] [metric_space β] [add_comm_monoid β] [has_lipschitz_add β] @[to_additive] instance : add_comm_monoid (α →ᵇ β) := { add_comm := assume f g, by ext; simp [add_comm], .. bounded_continuous_function.add_monoid } open_locale big_operators @[simp] lemma coe_sum {ι : Type*} (s : finset ι) (f : ι → (α →ᵇ β)) : ⇑(∑ i in s, f i) = (∑ i in s, (f i : α → β)) := (@coe_fn_add_hom α β _ _ _ _).map_sum f s lemma sum_apply {ι : Type*} (s : finset ι) (f : ι → (α →ᵇ β)) (a : α) : (∑ i in s, f i) a = (∑ i in s, f i a) := by simp end comm_has_lipschitz_add section normed_group /- In this section, if β is a normed group, then we show that the space of bounded continuous functions from α to β inherits a normed group structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space α] [normed_group β] variables (f g : α →ᵇ β) {x : α} {C : ℝ} instance : has_norm (α →ᵇ β) := ⟨λu, dist u 0⟩ lemma norm_def : ∥f∥ = dist f 0 := rfl /-- The norm of a bounded continuous function is the supremum of `∥f x∥`. We use `Inf` to ensure that the definition works if `α` has no elements. -/ lemma norm_eq (f : α →ᵇ β) : ∥f∥ = Inf {C : ℝ | 0 ≤ C ∧ ∀ (x : α), ∥f x∥ ≤ C} := by simp [norm_def, bounded_continuous_function.dist_eq] /-- When the domain is non-empty, we do not need the `0 ≤ C` condition in the formula for ∥f∥ as an `Inf`. -/ lemma norm_eq_of_nonempty [h : nonempty α] : ∥f∥ = Inf {C : ℝ | ∀ (x : α), ∥f x∥ ≤ C} := begin unfreezingI { obtain ⟨a⟩ := h, }, rw norm_eq, congr, ext, simp only [and_iff_right_iff_imp], exact λ h', le_trans (norm_nonneg (f a)) (h' a), end @[simp] lemma norm_eq_zero_of_empty [h : is_empty α] : ∥f∥ = 0 := begin have h' : ∀ (C : ℝ) (x : α), ∥f x∥ ≤ C, { intros, exfalso, apply h.false, use x, }, simp only [norm_eq, h', and_true, implies_true_iff], exact cInf_Ici, end lemma norm_coe_le_norm (x : α) : ∥f x∥ ≤ ∥f∥ := calc ∥f x∥ = dist (f x) ((0 : α →ᵇ β) x) : by simp [dist_zero_right] ... ≤ ∥f∥ : dist_coe_le_dist _ lemma dist_le_two_norm' {f : γ → β} {C : ℝ} (hC : ∀ x, ∥f x∥ ≤ C) (x y : γ) : dist (f x) (f y) ≤ 2 * C := calc dist (f x) (f y) ≤ ∥f x∥ + ∥f y∥ : dist_le_norm_add_norm _ _ ... ≤ C + C : add_le_add (hC x) (hC y) ... = 2 * C : (two_mul _).symm /-- Distance between the images of any two points is at most twice the norm of the function. -/ lemma dist_le_two_norm (x y : α) : dist (f x) (f y) ≤ 2 * ∥f∥ := dist_le_two_norm' f.norm_coe_le_norm x y variable {f} /-- The norm of a function is controlled by the supremum of the pointwise norms -/ lemma norm_le (C0 : (0 : ℝ) ≤ C) : ∥f∥ ≤ C ↔ ∀x:α, ∥f x∥ ≤ C := by simpa using @dist_le _ _ _ _ f 0 _ C0 lemma norm_le_of_nonempty [nonempty α] {f : α →ᵇ β} {M : ℝ} : ∥f∥ ≤ M ↔ ∀ x, ∥f x∥ ≤ M := begin simp_rw [norm_def, ←dist_zero_right], exact dist_le_iff_of_nonempty, end lemma norm_lt_iff_of_compact [compact_space α] {f : α →ᵇ β} {M : ℝ} (M0 : 0 < M) : ∥f∥ < M ↔ ∀ x, ∥f x∥ < M := begin simp_rw [norm_def, ←dist_zero_right], exact dist_lt_iff_of_compact M0, end lemma norm_lt_iff_of_nonempty_compact [nonempty α] [compact_space α] {f : α →ᵇ β} {M : ℝ} : ∥f∥ < M ↔ ∀ x, ∥f x∥ < M := begin simp_rw [norm_def, ←dist_zero_right], exact dist_lt_iff_of_nonempty_compact, end variable (f) /-- Norm of `const α b` is less than or equal to `∥b∥`. If `α` is nonempty, then it is equal to `∥b∥`. -/ lemma norm_const_le (b : β) : ∥const α b∥ ≤ ∥b∥ := (norm_le (norm_nonneg b)).2 $ λ x, le_refl _ @[simp] lemma norm_const_eq [h : nonempty α] (b : β) : ∥const α b∥ = ∥b∥ := le_antisymm (norm_const_le b) $ h.elim $ λ x, (const α b).norm_coe_le_norm x /-- Constructing a bounded continuous function from a uniformly bounded continuous function taking values in a normed group. -/ def of_normed_group {α : Type u} {β : Type v} [topological_space α] [normed_group β] (f : α → β) (Hf : continuous f) (C : ℝ) (H : ∀x, ∥f x∥ ≤ C) : α →ᵇ β := ⟨⟨λn, f n, Hf⟩, ⟨_, dist_le_two_norm' H⟩⟩ @[simp] lemma coe_of_normed_group {α : Type u} {β : Type v} [topological_space α] [normed_group β] (f : α → β) (Hf : continuous f) (C : ℝ) (H : ∀x, ∥f x∥ ≤ C) : (of_normed_group f Hf C H : α → β) = f := rfl lemma norm_of_normed_group_le {f : α → β} (hfc : continuous f) {C : ℝ} (hC : 0 ≤ C) (hfC : ∀ x, ∥f x∥ ≤ C) : ∥of_normed_group f hfc C hfC∥ ≤ C := (norm_le hC).2 hfC /-- Constructing a bounded continuous function from a uniformly bounded function on a discrete space, taking values in a normed group -/ def of_normed_group_discrete {α : Type u} {β : Type v} [topological_space α] [discrete_topology α] [normed_group β] (f : α → β) (C : ℝ) (H : ∀x, norm (f x) ≤ C) : α →ᵇ β := of_normed_group f continuous_of_discrete_topology C H @[simp] lemma coe_of_normed_group_discrete {α : Type u} {β : Type v} [topological_space α] [discrete_topology α] [normed_group β] (f : α → β) (C : ℝ) (H : ∀x, ∥f x∥ ≤ C) : (of_normed_group_discrete f C H : α → β) = f := rfl /-- Taking the pointwise norm of a bounded continuous function with values in a `normed_group`, yields a bounded continuous function with values in ℝ. -/ def norm_comp : α →ᵇ ℝ := of_normed_group (norm ∘ f) (by continuity) ∥f∥ (λ x, by simp only [f.norm_coe_le_norm, norm_norm]) @[simp] lemma coe_norm_comp : (f.norm_comp : α → ℝ) = norm ∘ f := rfl @[simp] lemma norm_norm_comp : ∥f.norm_comp∥ = ∥f∥ := by simp only [norm_eq, coe_norm_comp, norm_norm] lemma bdd_above_range_norm_comp : bdd_above $ set.range $ norm ∘ f := (real.bounded_iff_bdd_below_bdd_above.mp $ @bounded_range _ _ _ _ f.norm_comp).2 lemma norm_eq_supr_norm : ∥f∥ = ⨆ x : α, ∥f x∥ := begin casesI is_empty_or_nonempty α with hα _, { suffices : range (norm ∘ f) = ∅, { rw [f.norm_eq_zero_of_empty, supr, this, real.Sup_empty], }, simp only [hα, range_eq_empty, not_nonempty_iff], }, { rw [norm_eq_of_nonempty, supr, ← cInf_upper_bounds_eq_cSup f.bdd_above_range_norm_comp (range_nonempty _)], congr, ext, simp only [forall_apply_eq_imp_iff', mem_range, exists_imp_distrib], }, end /-- The pointwise opposite of a bounded continuous function is again bounded continuous. -/ instance : has_neg (α →ᵇ β) := ⟨λf, of_normed_group (-f) f.continuous.neg ∥f∥ $ λ x, trans_rel_right _ (norm_neg _) (f.norm_coe_le_norm x)⟩ /-- The pointwise difference of two bounded continuous functions is again bounded continuous. -/ instance : has_sub (α →ᵇ β) := ⟨λf g, of_normed_group (f - g) (f.continuous.sub g.continuous) (∥f∥ + ∥g∥) $ λ x, by { simp only [sub_eq_add_neg], exact le_trans (norm_add_le _ _) (add_le_add (f.norm_coe_le_norm x) $ trans_rel_right _ (norm_neg _) (g.norm_coe_le_norm x)) }⟩ @[simp] lemma coe_neg : ⇑(-f) = -f := rfl lemma neg_apply : (-f) x = -f x := rfl instance : add_comm_group (α →ᵇ β) := { add_left_neg := assume f, by ext; simp, add_comm := assume f g, by ext; simp [add_comm], sub_eq_add_neg := assume f g, by { ext, apply sub_eq_add_neg }, ..bounded_continuous_function.add_monoid, ..bounded_continuous_function.has_neg, ..bounded_continuous_function.has_sub } @[simp] lemma coe_sub : ⇑(f - g) = f - g := rfl lemma sub_apply : (f - g) x = f x - g x := rfl instance : normed_group (α →ᵇ β) := { dist_eq := λ f g, by simp only [norm_eq, dist_eq, dist_eq_norm, sub_apply] } lemma abs_diff_coe_le_dist : ∥f x - g x∥ ≤ dist f g := by { rw dist_eq_norm, exact (f - g).norm_coe_le_norm x } lemma coe_le_coe_add_dist {f g : α →ᵇ ℝ} : f x ≤ g x + dist f g := sub_le_iff_le_add'.1 $ (abs_le.1 $ @dist_coe_le_dist _ _ _ _ f g x).2 end normed_group section has_bounded_smul /-! ### `has_bounded_smul` (in particular, topological module) structure In this section, if `β` is a metric space and a `𝕜`-module whose addition and scalar multiplication are compatible with the metric structure, then we show that the space of bounded continuous functions from `α` to `β` inherits a so-called `has_bounded_smul` structure (in particular, a `has_continuous_mul` structure, which is the mathlib formulation of being a topological module), by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables {𝕜 : Type*} [metric_space 𝕜] [semiring 𝕜] variables [topological_space α] [metric_space β] [add_comm_monoid β] [module 𝕜 β] [has_bounded_smul 𝕜 β] variables {f g : α →ᵇ β} {x : α} {C : ℝ} instance : has_scalar 𝕜 (α →ᵇ β) := ⟨λ c f, bounded_continuous_function.mk_of_bound (c • f.to_continuous_map) (dist c 0 * (classical.some f.bounded)) begin intros x y, refine (dist_smul_pair c (f x) (f y)).trans _, refine mul_le_mul_of_nonneg_left _ dist_nonneg, exact classical.some_spec f.bounded x y end ⟩ @[simp] lemma coe_smul (c : 𝕜) (f : α →ᵇ β) : ⇑(c • f) = λ x, c • (f x) := rfl lemma smul_apply (c : 𝕜) (f : α →ᵇ β) (x : α) : (c • f) x = c • f x := rfl instance : has_bounded_smul 𝕜 (α →ᵇ β) := { dist_smul_pair' := λ c f₁ f₂, begin rw dist_le (mul_nonneg dist_nonneg dist_nonneg), intros x, refine (dist_smul_pair c (f₁ x) (f₂ x)).trans _, exact mul_le_mul_of_nonneg_left (dist_coe_le_dist x) dist_nonneg end, dist_pair_smul' := λ c₁ c₂ f, begin rw dist_le (mul_nonneg dist_nonneg dist_nonneg), intros x, refine (dist_pair_smul c₁ c₂ (f x)).trans _, convert mul_le_mul_of_nonneg_left (dist_coe_le_dist x) dist_nonneg, simp end } variables [has_lipschitz_add β] instance : module 𝕜 (α →ᵇ β) := { smul := (•), smul_add := λ c f g, ext $ λ x, smul_add c (f x) (g x), add_smul := λ c₁ c₂ f, ext $ λ x, add_smul c₁ c₂ (f x), mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul c₁ c₂ (f x), one_smul := λ f, ext $ λ x, one_smul 𝕜 (f x), smul_zero := λ c, ext $ λ x, smul_zero c, zero_smul := λ f, ext $ λ x, zero_smul 𝕜 (f x), .. bounded_continuous_function.add_comm_monoid } variables (𝕜) /-- The evaluation at a point, as a continuous linear map from `α →ᵇ β` to `β`. -/ def eval_clm (x : α) : (α →ᵇ β) →L[𝕜] β := { to_fun := λ f, f x, map_add' := λ f g, by simp only [pi.add_apply, coe_add], map_smul' := λ c f, by simp only [coe_smul, ring_hom.id_apply] } @[simp] lemma eval_clm_apply (x : α) (f : α →ᵇ β) : eval_clm 𝕜 x f = f x := rfl variables (α β) /-- The linear map forgetting that a bounded continuous function is bounded. -/ @[simps] def forget_boundedness_linear_map : (α →ᵇ β) →ₗ[𝕜] C(α, β) := { to_fun := forget_boundedness α β, map_smul' := by { intros, ext, simp, }, map_add' := by { intros, ext, simp, }, } end has_bounded_smul section normed_space /-! ### Normed space structure In this section, if `β` is a normed space, then we show that the space of bounded continuous functions from `α` to `β` inherits a normed space structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables {𝕜 : Type*} variables [topological_space α] [normed_group β] variables {f g : α →ᵇ β} {x : α} {C : ℝ} instance [normed_field 𝕜] [normed_space 𝕜 β] : normed_space 𝕜 (α →ᵇ β) := ⟨λ c f, begin refine norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _, exact (λ x, trans_rel_right _ (norm_smul _ _) (mul_le_mul_of_nonneg_left (f.norm_coe_le_norm _) (norm_nonneg _))) end⟩ variables [nondiscrete_normed_field 𝕜] [normed_space 𝕜 β] variables [normed_group γ] [normed_space 𝕜 γ] variables (α) -- TODO does this work in the `has_bounded_smul` setting, too? /-- Postcomposition of bounded continuous functions into a normed module by a continuous linear map is a continuous linear map. Upgraded version of `continuous_linear_map.comp_left_continuous`, similar to `linear_map.comp_left`. -/ protected def _root_.continuous_linear_map.comp_left_continuous_bounded (g : β →L[𝕜] γ) : (α →ᵇ β) →L[𝕜] (α →ᵇ γ) := linear_map.mk_continuous { to_fun := λ f, of_normed_group (g ∘ f) (g.continuous.comp f.continuous) (∥g∥ * ∥f∥) (λ x, (g.le_op_norm_of_le (f.norm_coe_le_norm x))), map_add' := λ f g, by ext; simp, map_smul' := λ c f, by ext; simp } ∥g∥ (λ f, norm_of_normed_group_le _ (mul_nonneg (norm_nonneg g) (norm_nonneg f)) _) @[simp] lemma _root_.continuous_linear_map.comp_left_continuous_bounded_apply (g : β →L[𝕜] γ) (f : α →ᵇ β) (x : α) : (g.comp_left_continuous_bounded α f) x = g (f x) := rfl end normed_space section normed_ring /-! ### Normed ring structure In this section, if `R` is a normed ring, then we show that the space of bounded continuous functions from `α` to `R` inherits a normed ring structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space α] {R : Type*} [normed_ring R] instance : ring (α →ᵇ R) := { one := const α 1, mul := λ f g, of_normed_group (f * g) (f.continuous.mul g.continuous) (∥f∥ * ∥g∥) $ λ x, le_trans (normed_ring.norm_mul (f x) (g x)) $ mul_le_mul (f.norm_coe_le_norm x) (g.norm_coe_le_norm x) (norm_nonneg _) (norm_nonneg _), one_mul := λ f, ext $ λ x, one_mul (f x), mul_one := λ f, ext $ λ x, mul_one (f x), mul_assoc := λ f₁ f₂ f₃, ext $ λ x, mul_assoc _ _ _, left_distrib := λ f₁ f₂ f₃, ext $ λ x, left_distrib _ _ _, right_distrib := λ f₁ f₂ f₃, ext $ λ x, right_distrib _ _ _, .. bounded_continuous_function.add_comm_group } @[simp] lemma coe_mul (f g : α →ᵇ R) : ⇑(f * g) = f * g := rfl lemma mul_apply (f g : α →ᵇ R) (x : α) : (f * g) x = f x * g x := rfl instance : normed_ring (α →ᵇ R) := { norm_mul := λ f g, norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _, .. bounded_continuous_function.normed_group } end normed_ring section normed_comm_ring /-! ### Normed commutative ring structure In this section, if `R` is a normed commutative ring, then we show that the space of bounded continuous functions from `α` to `R` inherits a normed commutative ring structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space α] {R : Type*} [normed_comm_ring R] instance : comm_ring (α →ᵇ R) := { mul_comm := λ f₁ f₂, ext $ λ x, mul_comm _ _, .. bounded_continuous_function.ring } instance : normed_comm_ring (α →ᵇ R) := { .. bounded_continuous_function.comm_ring, .. bounded_continuous_function.normed_group } end normed_comm_ring section normed_algebra /-! ### Normed algebra structure In this section, if `γ` is a normed algebra, then we show that the space of bounded continuous functions from `α` to `γ` inherits a normed algebra structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables {𝕜 : Type*} [normed_field 𝕜] variables [topological_space α] [normed_group β] [normed_space 𝕜 β] variables [normed_ring γ] [normed_algebra 𝕜 γ] variables {f g : α →ᵇ γ} {x : α} {c : 𝕜} /-- `bounded_continuous_function.const` as a `ring_hom`. -/ def C : 𝕜 →+* (α →ᵇ γ) := { to_fun := λ (c : 𝕜), const α ((algebra_map 𝕜 γ) c), map_one' := ext $ λ x, (algebra_map 𝕜 γ).map_one, map_mul' := λ c₁ c₂, ext $ λ x, (algebra_map 𝕜 γ).map_mul _ _, map_zero' := ext $ λ x, (algebra_map 𝕜 γ).map_zero, map_add' := λ c₁ c₂, ext $ λ x, (algebra_map 𝕜 γ).map_add _ _ } instance : algebra 𝕜 (α →ᵇ γ) := { to_ring_hom := C, commutes' := λ c f, ext $ λ x, algebra.commutes' _ _, smul_def' := λ c f, ext $ λ x, algebra.smul_def' _ _, ..bounded_continuous_function.module, ..bounded_continuous_function.ring } @[simp] lemma algebra_map_apply (k : 𝕜) (a : α) : algebra_map 𝕜 (α →ᵇ γ) k a = k • 1 := by { rw algebra.algebra_map_eq_smul_one, refl, } instance [nonempty α] : normed_algebra 𝕜 (α →ᵇ γ) := { norm_algebra_map_eq := λ c, begin calc ∥ (algebra_map 𝕜 (α →ᵇ γ)).to_fun c∥ = ∥(algebra_map 𝕜 γ) c∥ : _ ... = ∥c∥ : norm_algebra_map_eq _ _, apply norm_const_eq ((algebra_map 𝕜 γ) c), assumption, end, ..bounded_continuous_function.algebra } /-! ### Structure as normed module over scalar functions If `β` is a normed `𝕜`-space, then we show that the space of bounded continuous functions from `α` to `β` is naturally a module over the algebra of bounded continuous functions from `α` to `𝕜`. -/ instance has_scalar' : has_scalar (α →ᵇ 𝕜) (α →ᵇ β) := ⟨λ (f : α →ᵇ 𝕜) (g : α →ᵇ β), of_normed_group (λ x, (f x) • (g x)) (f.continuous.smul g.continuous) (∥f∥ * ∥g∥) (λ x, calc ∥f x • g x∥ ≤ ∥f x∥ * ∥g x∥ : normed_space.norm_smul_le _ _ ... ≤ ∥f∥ * ∥g∥ : mul_le_mul (f.norm_coe_le_norm _) (g.norm_coe_le_norm _) (norm_nonneg _) (norm_nonneg _)) ⟩ instance module' : module (α →ᵇ 𝕜) (α →ᵇ β) := module.of_core $ { smul := (•), smul_add := λ c f₁ f₂, ext $ λ x, smul_add _ _ _, add_smul := λ c₁ c₂ f, ext $ λ x, add_smul _ _ _, mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul _ _ _, one_smul := λ f, ext $ λ x, one_smul 𝕜 (f x) } lemma norm_smul_le (f : α →ᵇ 𝕜) (g : α →ᵇ β) : ∥f • g∥ ≤ ∥f∥ * ∥g∥ := norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _ /- TODO: When `normed_module` has been added to `normed_space.basic`, the above facts show that the space of bounded continuous functions from `α` to `β` is naturally a normed module over the algebra of bounded continuous functions from `α` to `𝕜`. -/ end normed_algebra end bounded_continuous_function
a46981a6f0918f5a6bb7e3523dd618bf60d25aab
137c667471a40116a7afd7261f030b30180468c2
/src/algebra/invertible.lean
4cc9950dc3c5231e84f458ccb8f61430c771251e
[ "Apache-2.0" ]
permissive
bragadeesh153/mathlib
46bf814cfb1eecb34b5d1549b9117dc60f657792
b577bb2cd1f96eb47031878256856020b76f73cd
refs/heads/master
1,687,435,188,334
1,626,384,207,000
1,626,384,207,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,076
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import algebra.group.units import algebra.ring.basic /-! # Invertible elements This file defines a typeclass `invertible a` for elements `a` with a two-sided multiplicative inverse. The intent of the typeclass is to provide a way to write e.g. `⅟2` in a ring like `ℤ[1/2]` where some inverses exist but there is no general `⁻¹` operator; or to specify that a field has characteristic `≠ 2`. It is the `Type`-valued analogue to the `Prop`-valued `is_unit`. For constructions of the invertible element given a characteristic, see `algebra/char_p/invertible` and other lemmas in that file. ## Notation * `⅟a` is `invertible.inv_of a`, the inverse of `a` ## Implementation notes The `invertible` class lives in `Type`, not `Prop`, to make computation easier. If multiplication is associative, `invertible` is a subsingleton anyway. The `simp` normal form tries to normalize `⅟a` to `a ⁻¹`. Otherwise, it pushes `⅟` inside the expression as much as possible. ## Tags invertible, inverse element, inv_of, a half, one half, a third, one third, ½, ⅓ -/ universes u variables {α : Type u} /-- `invertible a` gives a two-sided multiplicative inverse of `a`. -/ class invertible [has_mul α] [has_one α] (a : α) : Type u := (inv_of : α) (inv_of_mul_self : inv_of * a = 1) (mul_inv_of_self : a * inv_of = 1) -- This notation has the same precedence as `has_inv.inv`. notation `⅟`:1034 := invertible.inv_of @[simp] lemma inv_of_mul_self [has_mul α] [has_one α] (a : α) [invertible a] : ⅟a * a = 1 := invertible.inv_of_mul_self @[simp] lemma mul_inv_of_self [has_mul α] [has_one α] (a : α) [invertible a] : a * ⅟a = 1 := invertible.mul_inv_of_self @[simp] lemma inv_of_mul_self_assoc [monoid α] (a b : α) [invertible a] : ⅟a * (a * b) = b := by rw [←mul_assoc, inv_of_mul_self, one_mul] @[simp] lemma mul_inv_of_self_assoc [monoid α] (a b : α) [invertible a] : a * (⅟a * b) = b := by rw [←mul_assoc, mul_inv_of_self, one_mul] @[simp] lemma mul_inv_of_mul_self_cancel [monoid α] (a b : α) [invertible b] : a * ⅟b * b = a := by simp [mul_assoc] @[simp] lemma mul_mul_inv_of_self_cancel [monoid α] (a b : α) [invertible b] : a * b * ⅟b = a := by simp [mul_assoc] lemma inv_of_eq_right_inv [monoid α] {a b : α} [invertible a] (hac : a * b = 1) : ⅟a = b := left_inv_eq_right_inv (inv_of_mul_self _) hac lemma inv_of_eq_left_inv [monoid α] {a b : α} [invertible a] (hac : b * a = 1) : ⅟a = b := (left_inv_eq_right_inv hac (mul_inv_of_self _)).symm lemma invertible_unique {α : Type u} [monoid α] (a b : α) (h : a = b) [invertible a] [invertible b] : ⅟a = ⅟b := by { apply inv_of_eq_right_inv, rw [h, mul_inv_of_self], } instance [monoid α] (a : α) : subsingleton (invertible a) := ⟨ λ ⟨b, hba, hab⟩ ⟨c, hca, hac⟩, by { congr, exact left_inv_eq_right_inv hba hac } ⟩ /-- If `r` is invertible and `s = r`, then `s` is invertible. -/ def invertible.copy [monoid α] {r : α} (hr : invertible r) (s : α) (hs : s = r) : invertible s := { inv_of := ⅟r, inv_of_mul_self := by rw [hs, inv_of_mul_self], mul_inv_of_self := by rw [hs, mul_inv_of_self] } /-- An `invertible` element is a unit. -/ @[simps] def unit_of_invertible [monoid α] (a : α) [invertible a] : units α := { val := a, inv := ⅟a, val_inv := by simp, inv_val := by simp, } lemma is_unit_of_invertible [monoid α] (a : α) [invertible a] : is_unit a := ⟨unit_of_invertible a, rfl⟩ /-- Units are invertible in their associated monoid. -/ def units.invertible [monoid α] (u : units α) : invertible (u : α) := { inv_of := ↑(u⁻¹), inv_of_mul_self := u.inv_mul, mul_inv_of_self := u.mul_inv } @[simp] lemma inv_of_units [monoid α] (u : units α) [invertible (u : α)] : ⅟(u : α) = ↑(u⁻¹) := inv_of_eq_right_inv u.mul_inv lemma is_unit.nonempty_invertible [monoid α] {a : α} (h : is_unit a) : nonempty (invertible a) := let ⟨x, hx⟩ := h in ⟨x.invertible.copy _ hx.symm⟩ /-- Convert `is_unit` to `invertible` using `classical.choice`. Prefer `casesI h.nonempty_invertible` over `letI := h.invertible` if you want to avoid choice. -/ noncomputable def is_unit.invertible [monoid α] {a : α} (h : is_unit a) : invertible a := classical.choice h.nonempty_invertible @[simp] lemma nonempty_invertible_iff_is_unit [monoid α] (a : α) : nonempty (invertible a) ↔ is_unit a := ⟨nonempty.rec $ @is_unit_of_invertible _ _ _, is_unit.nonempty_invertible⟩ /-- Each element of a group is invertible. -/ def invertible_of_group [group α] (a : α) : invertible a := ⟨a⁻¹, inv_mul_self a, mul_inv_self a⟩ @[simp] lemma inv_of_eq_group_inv [group α] (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_self a) /-- `1` is the inverse of itself -/ def invertible_one [monoid α] : invertible (1 : α) := ⟨1, mul_one _, one_mul _⟩ @[simp] lemma inv_of_one [monoid α] [invertible (1 : α)] : ⅟(1 : α) = 1 := inv_of_eq_right_inv (mul_one _) /-- `-⅟a` is the inverse of `-a` -/ def invertible_neg [ring α] (a : α) [invertible a] : invertible (-a) := ⟨ -⅟a, by simp, by simp ⟩ @[simp] lemma inv_of_neg [ring α] (a : α) [invertible a] [invertible (-a)] : ⅟(-a) = -⅟a := inv_of_eq_right_inv (by simp) @[simp] lemma one_sub_inv_of_two [ring α] [invertible (2:α)] : 1 - (⅟2:α) = ⅟2 := (is_unit_of_invertible (2:α)).mul_right_inj.1 $ by rw [mul_sub, mul_inv_of_self, mul_one, bit0, add_sub_cancel] /-- `a` is the inverse of `⅟a`. -/ instance invertible_inv_of [has_one α] [has_mul α] {a : α} [invertible a] : invertible (⅟a) := ⟨ a, mul_inv_of_self a, inv_of_mul_self a ⟩ @[simp] lemma inv_of_inv_of [monoid α] {a : α} [invertible a] [invertible (⅟a)] : ⅟(⅟a) = a := inv_of_eq_right_inv (inv_of_mul_self _) /-- `⅟b * ⅟a` is the inverse of `a * b` -/ def invertible_mul [monoid α] (a b : α) [invertible a] [invertible b] : invertible (a * b) := ⟨ ⅟b * ⅟a, by simp [←mul_assoc], by simp [←mul_assoc] ⟩ @[simp] lemma inv_of_mul [monoid α] (a b : α) [invertible a] [invertible b] [invertible (a * b)] : ⅟(a * b) = ⅟b * ⅟a := inv_of_eq_right_inv (by simp [←mul_assoc]) theorem commute.inv_of_right [monoid α] {a b : α} [invertible b] (h : commute a b) : commute a (⅟b) := calc a * (⅟b) = (⅟b) * (b * a * (⅟b)) : by simp [mul_assoc] ... = (⅟b) * (a * b * ((⅟b))) : by rw h.eq ... = (⅟b) * a : by simp [mul_assoc] theorem commute.inv_of_left [monoid α] {a b : α} [invertible b] (h : commute b a) : commute (⅟b) a := calc (⅟b) * a = (⅟b) * (a * b * (⅟b)) : by simp [mul_assoc] ... = (⅟b) * (b * a * (⅟b)) : by rw h.eq ... = a * (⅟b) : by simp [mul_assoc] lemma commute_inv_of {M : Type*} [has_one M] [has_mul M] (m : M) [invertible m] : commute m (⅟m) := calc m * ⅟m = 1 : mul_inv_of_self m ... = ⅟ m * m : (inv_of_mul_self m).symm section group_with_zero variable [group_with_zero α] lemma nonzero_of_invertible (a : α) [invertible a] : a ≠ 0 := λ ha, zero_ne_one $ calc 0 = ⅟a * a : by simp [ha] ... = 1 : inv_of_mul_self a /-- `a⁻¹` is an inverse of `a` if `a ≠ 0` -/ def invertible_of_nonzero {a : α} (h : a ≠ 0) : invertible a := ⟨ a⁻¹, inv_mul_cancel h, mul_inv_cancel h ⟩ @[simp] lemma inv_of_eq_inv (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_cancel (nonzero_of_invertible a)) @[simp] lemma inv_mul_cancel_of_invertible (a : α) [invertible a] : a⁻¹ * a = 1 := inv_mul_cancel (nonzero_of_invertible a) @[simp] lemma mul_inv_cancel_of_invertible (a : α) [invertible a] : a * a⁻¹ = 1 := mul_inv_cancel (nonzero_of_invertible a) @[simp] lemma div_mul_cancel_of_invertible (a b : α) [invertible b] : a / b * b = a := div_mul_cancel a (nonzero_of_invertible b) @[simp] lemma mul_div_cancel_of_invertible (a b : α) [invertible b] : a * b / b = a := mul_div_cancel a (nonzero_of_invertible b) @[simp] lemma div_self_of_invertible (a : α) [invertible a] : a / a = 1 := div_self (nonzero_of_invertible a) /-- `b / a` is the inverse of `a / b` -/ def invertible_div (a b : α) [invertible a] [invertible b] : invertible (a / b) := ⟨b / a, by simp [←mul_div_assoc], by simp [←mul_div_assoc]⟩ @[simp] lemma inv_of_div (a b : α) [invertible a] [invertible b] [invertible (a / b)] : ⅟(a / b) = b / a := inv_of_eq_right_inv (by simp [←mul_div_assoc]) /-- `a` is the inverse of `a⁻¹` -/ def invertible_inv {a : α} [invertible a] : invertible (a⁻¹) := ⟨ a, by simp, by simp ⟩ end group_with_zero /-- Monoid homs preserve invertibility. -/ def invertible.map {R : Type*} {S : Type*} [monoid R] [monoid S] (f : R →* S) (r : R) [invertible r] : invertible (f r) := { inv_of := f (⅟r), inv_of_mul_self := by rw [← f.map_mul, inv_of_mul_self, f.map_one], mul_inv_of_self := by rw [← f.map_mul, mul_inv_of_self, f.map_one] }
6070728461e2274e3c13fcd0eee81ef22fcb92ff
1d265c7dd8cb3d0e1d645a19fd6157a2084c3921
/src/chapter_exercises/chap3_exercises.lean
647098bdcdbb045501512a6a6af24c8694a7770e
[ "MIT" ]
permissive
hanzhi713/lean-proofs
de432372f220d302be09b5ca4227f8986567e4fd
4d8356a878645b9ba7cb036f87737f3f1e68ede5
refs/heads/master
1,585,580,245,658
1,553,646,623,000
1,553,646,623,000
151,342,188
0
1
null
null
null
null
UTF-8
Lean
false
false
11,137
lean
variables p q r s : Prop -- commutativity of ∧ and ∨ example : p ∧ q ↔ q ∧ p := begin apply iff.intro, assume pq, show q ∧ p, from and.intro pq.2 pq.1, assume qp, show p ∧ q, from and.intro qp.2 qp.1 end example : p ∨ q ↔ q ∨ p := begin apply iff.intro, assume pq, cases pq with pfp pfq, show q ∨ p, from or.inr pfp, show q ∨ p, from or.inl pfq, assume qp, cases qp with pfq pfp, show p ∨ q, from or.inr pfq, show p ∨ q, from or.inl pfp end -- associativity of ∧ and ∨ example : (p ∧ q) ∧ r ↔ p ∧ (q ∧ r) := begin apply iff.intro, assume pqr, show p ∧ (q ∧ r), from and.intro pqr.1.1 (and.intro pqr.1.2 pqr.2), assume pqr, show (p ∧ q) ∧ r, from and.intro (and.intro pqr.1 pqr.2.1) pqr.2.2 end example : (p ∨ q) ∨ r ↔ p ∨ (q ∨ r) := begin apply iff.intro, assume pqr, cases pqr with pq pfr, cases pq with pfp pfq, show p ∨ (q ∨ r), from or.inl pfp, show p ∨ (q ∨ r), from or.inr (or.inl pfq), show p ∨ (q ∨ r), from or.inr (or.inr pfr), assume pqr, cases pqr with pfp qr, show (p ∨ q) ∨ r, from or.inl (or.inl pfp), cases qr with pfq pfr, show (p ∨ q) ∨ r, from or.inl (or.inr pfq), show (p ∨ q) ∨ r, from or.inr pfr end -- distributivity example : p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) := begin apply iff.intro, assume pqr, have pfp := pqr.1, have qr := pqr.2, cases qr with pfq pfr, show (p ∧ q) ∨ (p ∧ r), from or.inl (and.intro pfp pfq), show (p ∧ q) ∨ (p ∧ r), from or.inr (and.intro pfp pfr), assume pqpr, cases pqpr with pq pr, have pfp := pq.1, have right := or.inl pq.2, show p ∧ (q ∨ r), from and.intro pfp right, have pfp := pr.1, have right := or.inr pr.2, show p ∧ (q ∨ r), from and.intro pfp right end example : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (p ∨ r) := begin apply iff.intro, assume pqr, cases pqr with pfp qr, show (p ∨ q) ∧ (p ∨ r), from and.intro (or.inl pfp) (or.inl pfp), have pfq := qr.1, have pfr := qr.2, show (p ∨ q) ∧ (p ∨ r), from and.intro (or.inr pfq) (or.inr pfr), assume pqpr, have pq := pqpr.1, have pr := pqpr.2, cases pq with pfp pfq, show p ∨ (q ∧ r), from or.inl pfp, cases pr with pfp pfr, show p ∨ (q ∧ r), from or.inl pfp, show p ∨ (q ∧ r), from or.inr (and.intro pfq pfr) end -- other properties example : (p → (q → r)) ↔ (p ∧ q → r) := begin apply iff.intro, assume pqr, assume pq, show r, from pqr pq.1 pq.2, assume pqr, assume p q, show r, from pqr (and.intro p q), end example : ((p ∨ q) → r) ↔ (p → r) ∧ (q → r) := begin apply iff.intro, assume pqr, apply and.intro, assume p, show r, from pqr (or.inl p), assume q, show r, from pqr (or.inr q), assume pqr, have pr := pqr.1, have qr := pqr.2, assume pq, cases pq with pfp pfq, show r, from pr pfp, show r, from qr pfq end -- First Demorgan's law example : ¬(p ∨ q) ↔ ¬p ∧ ¬q := begin apply iff.intro, assume npq, apply and.intro, assume p, show false, from npq (or.inl p), assume q, show false, from npq (or.inr q), assume npnq, assume porq, cases porq with pfp pfq, show false, from npnq.1 pfp, show false, from npnq.2 pfq end -- Second Demorgan's law -- This direction does not require classical reasoning example : ¬p ∨ ¬q → ¬(p ∧ q) := begin assume npornq, assume pandq, cases npornq with np nq, show false, from np pandq.1, show false, from nq pandq.2 end example : ¬(p ∧ ¬p) := begin assume h, show false, from h.2 h.1 end example : p ∧ ¬q → ¬(p → q) := begin assume pandnotq, assume ptoq, have q := ptoq pandnotq.1, show false, from pandnotq.2 q end example : ¬p → (p → q) := begin assume notp, assume p, show q, from false.elim (notp p) end example : (¬p ∨ q) → (p → q) := begin assume notporq, assume p, cases notporq with notp pfq, show q, from false.elim (notp p), show q, from pfq end example : p ∨ false ↔ p := begin apply iff.intro, assume pfalse, cases pfalse with pfp pff, show p, from pfp, show p, from false.elim pff, assume pfp, show p ∨ false, from or.inl pfp end example : p ∧ false ↔ false := begin apply iff.intro, assume pf, show false, from pf.2, assume f, show p ∧ false, from and.intro (false.elim f) f end example : ¬(p ↔ ¬p) := begin assume h, have forward := iff.elim_left h, have backward := iff.elim_right h, have np : ¬p := assume p, (forward p) p, show false, from np (backward np) end theorem modus_tollens : (p → q) → (¬q → ¬p) := begin assume pq, assume notq, assume pfp, have pfq : q := pq pfp, show false, from notq pfq end -- an alternative proof to ¬(p ↔ ¬p) theorem piffnpf : ¬(p ↔ ¬p) := begin apply iff.elim, assume pnp, assume npp, apply modus_tollens, exact npp, assume p, show false, from (pnp p) p, assume p, show false, from (pnp p) p end -- these require classical reasoning open classical theorem double_neg_elim: ∀ { P }, ¬¬P → P := begin assume P : Prop, assume pfNotNotP : ¬¬P, cases em P with pfP pfnP, show P, from pfP, have f: false := pfNotNotP pfnP, show P, from false.elim f end example : (p → r ∨ s) → ((p → r) ∨ (p → s)) := begin assume prs, cases em p with pfp pfnp, have rs := prs pfp, cases rs with pfr pfs, apply or.inl, show p → r, from assume pfp, pfr, apply or.inr, show p → s, from assume pfp, pfs, apply or.inl, assume p, show r, from false.elim (pfnp p) end -- Second Demorgan's law, -- the direction that requires classical reasoning example : ¬(p ∧ q) → ¬p ∨ ¬q := begin assume notPandQ, cases em p with pfP pfnotP, cases em q with pfQ pfnotQ, show ¬p ∨ ¬q, from false.elim (notPandQ (and.intro pfP pfQ)), show ¬p ∨ ¬q, from or.inr pfnotQ, show ¬p ∨ ¬q, from or.inl pfnotP, end -- I don't use "example" here because it's used in following proofs theorem pf_by_contrapositive: (¬q → ¬p) → (p → q) := begin assume nqnp : ¬q → ¬p, assume pfP : p, have nnq : ¬q → false := begin assume nq : ¬q, have np : ¬p := nqnp nq, show false, from np pfP end, show q, from double_neg_elim nnq end #check pf_by_contrapositive example : ¬(p → q) → p ∧ ¬q := begin assume notpq, apply and.intro, show ¬q, --note: "show" can be used to switch the order of goals assume pfq, have pq : p → q := assume pfp, pfq, show false, from notpq pq, cases em p with pfp pfnp, assumption, have pq : p → q := assume pfp, false.elim (pfnp pfp), show p, from false.elim (notpq pq) end -- an alternative proof to the previous one example : ¬(p → q) → p ∧ ¬q := begin assume notpq, cases em p with pfp pfnp, cases em q with pfq pfnq, have pq : p → q := assume p, pfq, show p ∧ ¬q, from false.elim (notpq pq), show p ∧ ¬q, from and.intro pfp pfnq, have nqnp : ¬q → ¬p := assume nq, pfnp, have pq : p → q := pf_by_contrapositive p q nqnp, show p ∧ ¬q, from false.elim (notpq pq) end -- an alternative proof to the previous one example : ¬(p → q) → p ∧ ¬q := begin assume notpq, apply and.intro, cases em p with pfp pfnp, cases em q with pfq pfnq, show p, from pfp, show p, from pfp, have pq : p → q := assume p, false.elim (pfnp p), show p, from false.elim (notpq pq), assume pfq, have pq : p → q := assume p, pfq, show false, from notpq pq end -- an alternative proof to the previous one example : ¬(p → q) → p ∧ ¬q := begin assume notpq, apply and.intro, cases em p with pfp pfnp, show p, from pfp, have nqnp : ¬q → ¬p := assume nq, pfnp, have pq : p → q := pf_by_contrapositive p q nqnp, show p, from false.elim (notpq pq), assume pfq, have pq : p → q := assume p, pfq, show false, from notpq pq end example : (p → q) → (¬p ∨ q) := begin assume pq, cases em p with pfp pfnp, show ¬p ∨ q, from or.inr (pq pfp), show ¬p ∨ q, from or.inl pfnp end example : p ∨ ¬p := begin apply em end example : (((p → q) → p) → p) := begin assume pqp, cases em p with pfp pfnp, show p, from pfp, have n: ¬q → ¬p := assume nq, pfnp, have pq : p → q := pf_by_contrapositive p q n, show p, from pqp pq end -- an alternative proof to the previous one example : (((p → q) → p) → p) := begin assume pqp, cases em p with pfp pfnp, show p, from pfp, have pq: p → q := assume pfp, false.elim (pfnp pfp), show p, from pqp pq end -- Proof that double negation elimination implies axiom of excluded middle -- First prove that ∀ P, ¬¬(P ∨ ¬P). -- This proof makes use of the property that ¬(p ∨ q) ↔ ¬p ∧ ¬q lemma notnotem: ∀ P, ¬¬(P ∨ ¬P) := begin assume P, assume npornp, have np: ¬P := assume p, npornp (or.inl p), have nnp: ¬¬P := assume np, npornp (or.inr np), show false, from nnp np end #check notnotem #check non_contradictory_em -- notnotem is actually a built-in lemma theorem DNEtoEM : (∀ P, ¬¬P → P) → (∀ P, P ∨ ¬P) := begin assume notnotPtoP P, have duo_neg_elim_em : ¬¬(P ∨ ¬P) → P ∨ ¬P := notnotPtoP (P ∨ ¬P), show P ∨ ¬P, from duo_neg_elim_em (notnotem P) end
8b60fd59a6becb72fcb3880452462a9341b290b4
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/functor.lean
b7e0fa13610d0e3386fdf8bc223706f2ef733aff
[ "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
3,696
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison Defines a functor between categories. (As it is a 'bundled' object rather than the `is_functorial` typeclass parametrised by the underlying function on objects, the name is capitalised.) Introduces notations `C ⥤ D` for the type of all functors from `C` to `D`. (I would like a better arrow here, unfortunately ⇒ (`\functor`) is taken by core.) -/ import tactic.reassoc_axiom import tactic.monotonicity namespace category_theory -- declare the `v`'s first; see `category_theory.category` for an explanation universes v v₁ v₂ v₃ u u₁ u₂ u₃ section set_option old_structure_cmd true /-- `functor C D` represents a functor between categories `C` and `D`. To apply a functor `F` to an object use `F.obj X`, and to a morphism use `F.map f`. The axiom `map_id` expresses preservation of identities, and `map_comp` expresses functoriality. See https://stacks.math.columbia.edu/tag/001B. -/ structure functor (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] extends prefunctor C D : Type (max v₁ v₂ u₁ u₂) := (map_id' : ∀ (X : C), map (𝟙 X) = 𝟙 (obj X) . obviously) (map_comp' : ∀ {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z), map (f ≫ g) = (map f) ≫ (map g) . obviously) /-- The prefunctor between the underlying quivers. -/ add_decl_doc functor.to_prefunctor end -- A functor is basically a function, so give ⥤ a similar precedence to → (25). -- For example, `C × D ⥤ E` should parse as `(C × D) ⥤ E` not `C × (D ⥤ E)`. infixr ` ⥤ `:26 := functor -- type as \func -- restate_axiom functor.map_id' attribute [simp] functor.map_id restate_axiom functor.map_comp' attribute [reassoc, simp] functor.map_comp namespace functor section variables (C : Type u₁) [category.{v₁} C] /-- `𝟭 C` is the identity functor on a category `C`. -/ protected def id : C ⥤ C := { obj := λ X, X, map := λ _ _ f, f } notation `𝟭` := functor.id -- Type this as `\sb1` instance : inhabited (C ⥤ C) := ⟨functor.id C⟩ variable {C} @[simp] lemma id_obj (X : C) : (𝟭 C).obj X = X := rfl @[simp] lemma id_map {X Y : C} (f : X ⟶ Y) : (𝟭 C).map f = f := rfl end section variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] {E : Type u₃} [category.{v₃} E] /-- `F ⋙ G` is the composition of a functor `F` and a functor `G` (`F` first, then `G`). -/ def comp (F : C ⥤ D) (G : D ⥤ E) : C ⥤ E := { obj := λ X, G.obj (F.obj X), map := λ _ _ f, G.map (F.map f) } infixr ` ⋙ `:80 := comp @[simp] lemma comp_obj (F : C ⥤ D) (G : D ⥤ E) (X : C) : (F ⋙ G).obj X = G.obj (F.obj X) := rfl @[simp] lemma comp_map (F : C ⥤ D) (G : D ⥤ E) {X Y : C} (f : X ⟶ Y) : (F ⋙ G).map f = G.map (F.map f) := rfl -- These are not simp lemmas because rewriting along equalities between functors -- is not necessarily a good idea. -- Natural isomorphisms are also provided in `whiskering.lean`. protected lemma comp_id (F : C ⥤ D) : F ⋙ (𝟭 D) = F := by cases F; refl protected lemma id_comp (F : C ⥤ D) : (𝟭 C) ⋙ F = F := by cases F; refl @[simp] lemma map_dite (F : C ⥤ D) {X Y : C} {P : Prop} [decidable P] (f : P → (X ⟶ Y)) (g : ¬P → (X ⟶ Y)) : F.map (if h : P then f h else g h) = if h : P then F.map (f h) else F.map (g h) := by { split_ifs; refl, } end @[mono] lemma monotone {α β : Type*} [preorder α] [preorder β] (F : α ⥤ β) : monotone F.obj := λ a b h, (F.map h.hom).le end functor end category_theory
4f30f470ad7437e0172fc33e92792a0e8cc2060c
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/data/set.lean
34a13a4b056de1b710991e62be3422bc9f287bca
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
1,132
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.interactive import init.control.lawful universes u v /-- A set of elements of type `α`; implemented as a predicate `α → Prop`. -/ def set (α : Type u) := α → Prop /-- The set `{x | p x}` of elements satisfying the predicate `p`. -/ def set_of {α : Type u} (p : α → Prop) : set α := p namespace set variables {α : Type u} instance has_mem : has_mem α (set α) := ⟨λ x s, s x⟩ @[simp] lemma mem_set_of_eq {x : α} {p : α → Prop} : x ∈ {y | p y} = p x := rfl instance : has_emptyc (set α) := ⟨{x | false}⟩ /-- The set that contains all elements of a type. -/ def univ : set α := {x | true} instance : has_insert α (set α) := ⟨λ a s, {b | b = a ∨ b ∈ s}⟩ instance : has_singleton α (set α) := ⟨λ a, {b | b = a}⟩ instance : has_sep α (set α) := ⟨λ p s, {x | x ∈ s ∧ p x}⟩ instance : is_lawful_singleton α (set α) := ⟨λ a, funext $ λ b, propext $ or_false _⟩ end set
ec2ffb3f8a26eb2531221625e8f12da3222897d9
26ac254ecb57ffcb886ff709cf018390161a9225
/src/category_theory/groupoid.lean
c003975c04a95ced169de074755cba71d541614a
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
2,202
lean
/- Copyright (c) 2018 Reid Barton All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Scott Morrison, David Wärn -/ import category_theory.category import category_theory.epi_mono namespace category_theory universes v v₂ u u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation section prio set_option default_priority 100 -- see Note [default priority] /-- A `groupoid` is a category such that all morphisms are isomorphisms. -/ class groupoid (obj : Type u) extends category.{v} obj : Type (max u (v+1)) := (inv : Π {X Y : obj}, (X ⟶ Y) → (Y ⟶ X)) (inv_comp' : ∀ {X Y : obj} (f : X ⟶ Y), comp (inv f) f = id Y . obviously) (comp_inv' : ∀ {X Y : obj} (f : X ⟶ Y), comp f (inv f) = id X . obviously) end prio restate_axiom groupoid.inv_comp' restate_axiom groupoid.comp_inv' attribute [simp] groupoid.inv_comp groupoid.comp_inv abbreviation large_groupoid (C : Type (u+1)) : Type (u+1) := groupoid.{u} C abbreviation small_groupoid (C : Type u) : Type (u+1) := groupoid.{u} C section variables {C : Type u} [groupoid.{v} C] {X Y : C} @[priority 100] -- see Note [lower instance priority] instance is_iso.of_groupoid (f : X ⟶ Y) : is_iso f := { inv := groupoid.inv f } variables (X Y) /-- In a groupoid, isomorphisms are equivalent to morphisms. -/ def groupoid.iso_equiv_hom : (X ≅ Y) ≃ (X ⟶ Y) := { to_fun := iso.hom, inv_fun := λ f, as_iso f, left_inv := λ i, iso.ext rfl, right_inv := λ f, rfl } end section variables {C : Type u} [category.{v} C] /-- A category where every morphism `is_iso` is a groupoid. -/ def groupoid.of_is_iso (all_is_iso : ∀ {X Y : C} (f : X ⟶ Y), is_iso f) : groupoid.{v} C := { inv := λ X Y f, (all_is_iso f).inv } /-- A category where every morphism has a `trunc` retraction is computably a groupoid. -/ def groupoid.of_trunc_split_mono (all_split_mono : ∀ {X Y : C} (f : X ⟶ Y), trunc (split_mono f)) : groupoid.{v} C := begin apply groupoid.of_is_iso, intros X Y f, trunc_cases all_split_mono f, trunc_cases all_split_mono (retraction f), apply is_iso.of_mono_retraction, end end end category_theory
8361f9f23c36ef55cc53c5ff6da2cd3c28f48236
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/category_theory/comma.lean
a924907f8bd00efb4105d69167d38bb4641ab919
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
11,331
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johan Commelin -/ import category_theory.isomorphism import category_theory.punit namespace category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {A : Type u₁} [𝒜 : category.{v₁} A] variables {B : Type u₂} [ℬ : category.{v₂} B] variables {T : Type u₃} [𝒯 : category.{v₃} T] include 𝒜 ℬ 𝒯 structure comma (L : A ⥤ T) (R : B ⥤ T) : Type (max u₁ u₂ v₃) := (left : A . obviously) (right : B . obviously) (hom : L.obj left ⟶ R.obj right) variables {L : A ⥤ T} {R : B ⥤ T} structure comma_morphism (X Y : comma L R) := (left : X.left ⟶ Y.left . obviously) (right : X.right ⟶ Y.right . obviously) (w' : L.map left ≫ Y.hom = X.hom ≫ R.map right . obviously) restate_axiom comma_morphism.w' attribute [simp] comma_morphism.w namespace comma_morphism @[extensionality] lemma ext {X Y : comma L R} {f g : comma_morphism X Y} (l : f.left = g.left) (r : f.right = g.right) : f = g := begin cases f, cases g, congr; assumption end end comma_morphism instance comma_category : category (comma L R) := { hom := comma_morphism, id := λ X, { left := 𝟙 X.left, right := 𝟙 X.right }, comp := λ X Y Z f g, { left := f.left ≫ g.left, right := f.right ≫ g.right, w' := begin rw [functor.map_comp, category.assoc, g.w, ←category.assoc, f.w, functor.map_comp, category.assoc], end }} namespace comma section variables {X Y Z : comma L R} {f : X ⟶ Y} {g : Y ⟶ Z} @[simp] lemma comp_left : (f ≫ g).left = f.left ≫ g.left := rfl @[simp] lemma comp_right : (f ≫ g).right = f.right ≫ g.right := rfl end variables (L) (R) def fst : comma L R ⥤ A := { obj := λ X, X.left, map := λ _ _ f, f.left } def snd : comma L R ⥤ B := { obj := λ X, X.right, map := λ _ _ f, f.right } @[simp] lemma fst_obj {X : comma L R} : (fst L R).obj X = X.left := rfl @[simp] lemma snd_obj {X : comma L R} : (snd L R).obj X = X.right := rfl @[simp] lemma fst_map {X Y : comma L R} {f : X ⟶ Y} : (fst L R).map f = f.left := rfl @[simp] lemma snd_map {X Y : comma L R} {f : X ⟶ Y} : (snd L R).map f = f.right := rfl def nat_trans : fst L R ⋙ L ⟶ snd L R ⋙ R := { app := λ X, X.hom } section variables {L₁ L₂ L₃ : A ⥤ T} {R₁ R₂ R₃ : B ⥤ T} def map_left (l : L₁ ⟶ L₂) : comma L₂ R ⥤ comma L₁ R := { obj := λ X, { left := X.left, right := X.right, hom := l.app X.left ≫ X.hom }, map := λ X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←category.assoc, l.naturality f.left, category.assoc]; tidy } } section variables {X Y : comma L₂ R} {f : X ⟶ Y} {l : L₁ ⟶ L₂} @[simp] lemma map_left_obj_left : ((map_left R l).obj X).left = X.left := rfl @[simp] lemma map_left_obj_right : ((map_left R l).obj X).right = X.right := rfl @[simp] lemma map_left_obj_hom : ((map_left R l).obj X).hom = l.app X.left ≫ X.hom := rfl @[simp] lemma map_left_map_left : ((map_left R l).map f).left = f.left := rfl @[simp] lemma map_left_map_right : ((map_left R l).map f).right = f.right := rfl end def map_left_id : map_left R (𝟙 L) ≅ 𝟭 _ := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R} @[simp] lemma map_left_id_hom_app_left : (((map_left_id L R).hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_id_hom_app_right : (((map_left_id L R).hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_left_id_inv_app_left : (((map_left_id L R).inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_id_inv_app_right : (((map_left_id L R).inv).app X).right = 𝟙 (X.right) := rfl end def map_left_comp (l : L₁ ⟶ L₂) (l' : L₂ ⟶ L₃) : (map_left R (l ≫ l')) ≅ (map_left R l') ⋙ (map_left R l) := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L₃ R} {l : L₁ ⟶ L₂} {l' : L₂ ⟶ L₃} @[simp] lemma map_left_comp_hom_app_left : (((map_left_comp R l l').hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_comp_hom_app_right : (((map_left_comp R l l').hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_left_comp_inv_app_left : (((map_left_comp R l l').inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_comp_inv_app_right : (((map_left_comp R l l').inv).app X).right = 𝟙 (X.right) := rfl end def map_right (r : R₁ ⟶ R₂) : comma L R₁ ⥤ comma L R₂ := { obj := λ X, { left := X.left, right := X.right, hom := X.hom ≫ r.app X.right }, map := λ X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←r.naturality f.right, ←category.assoc]; tidy } } section variables {X Y : comma L R₁} {f : X ⟶ Y} {r : R₁ ⟶ R₂} @[simp] lemma map_right_obj_left : ((map_right L r).obj X).left = X.left := rfl @[simp] lemma map_right_obj_right : ((map_right L r).obj X).right = X.right := rfl @[simp] lemma map_right_obj_hom : ((map_right L r).obj X).hom = X.hom ≫ r.app X.right := rfl @[simp] lemma map_right_map_left : ((map_right L r).map f).left = f.left := rfl @[simp] lemma map_right_map_right : ((map_right L r).map f).right = f.right := rfl end def map_right_id : map_right L (𝟙 R) ≅ 𝟭 _ := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R} @[simp] lemma map_right_id_hom_app_left : (((map_right_id L R).hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_id_hom_app_right : (((map_right_id L R).hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_right_id_inv_app_left : (((map_right_id L R).inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_id_inv_app_right : (((map_right_id L R).inv).app X).right = 𝟙 (X.right) := rfl end def map_right_comp (r : R₁ ⟶ R₂) (r' : R₂ ⟶ R₃) : (map_right L (r ≫ r')) ≅ (map_right L r) ⋙ (map_right L r') := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R₁} {r : R₁ ⟶ R₂} {r' : R₂ ⟶ R₃} @[simp] lemma map_right_comp_hom_app_left : (((map_right_comp L r r').hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_comp_hom_app_right : (((map_right_comp L r r').hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_right_comp_inv_app_left : (((map_right_comp L r r').inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_comp_inv_app_right : (((map_right_comp L r r').inv).app X).right = 𝟙 (X.right) := rfl end end end comma omit 𝒜 ℬ @[derive category] def over (X : T) := comma.{v₃ 0 v₃} (𝟭 T) (functor.of.obj X) namespace over variables {X : T} @[extensionality] lemma over_morphism.ext {X : T} {U V : over X} {f g : U ⟶ V} (h : f.left = g.left) : f = g := by tidy @[simp] lemma over_right (U : over X) : U.right = punit.star := by tidy @[simp] lemma over_morphism_right {U V : over X} (f : U ⟶ V) : f.right = 𝟙 punit.star := by tidy @[simp] lemma id_left (U : over X) : comma_morphism.left (𝟙 U) = 𝟙 U.left := rfl @[simp] lemma comp_left (a b c : over X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).left = f.left ≫ g.left := rfl @[simp] lemma w {A B : over X} (f : A ⟶ B) : f.left ≫ B.hom = A.hom := by have := f.w; tidy def mk {X Y : T} (f : Y ⟶ X) : over X := { left := Y, hom := f } @[simp] lemma mk_left {X Y : T} (f : Y ⟶ X) : (mk f).left = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : Y ⟶ X) : (mk f).hom = f := rfl def hom_mk {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom . obviously) : U ⟶ V := { left := f } @[simp] lemma hom_mk_left {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom) : (hom_mk f).left = f := rfl def forget : (over X) ⥤ T := comma.fst _ _ @[simp] lemma forget_obj {U : over X} : forget.obj U = U.left := rfl @[simp] lemma forget_map {U V : over X} {f : U ⟶ V} : forget.map f = f.left := rfl def map {Y : T} (f : X ⟶ Y) : over X ⥤ over Y := comma.map_right _ $ functor.of.map f section variables {Y : T} {f : X ⟶ Y} {U V : over X} {g : U ⟶ V} @[simp] lemma map_obj_left : ((map f).obj U).left = U.left := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = U.hom ≫ f := rfl @[simp] lemma map_map_left : ((map f).map g).left = g.left := rfl end section variables {D : Type u₃} [𝒟 : category.{v₃} D] include 𝒟 def post (F : T ⥤ D) : over X ⥤ over (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { left := F.map f.left, w' := by tidy; erw [← F.map_comp, w] } } end end over @[derive category] def under (X : T) := comma.{0 v₃ v₃} (functor.of.obj X) (𝟭 T) namespace under variables {X : T} @[extensionality] lemma under_morphism.ext {X : T} {U V : under X} {f g : U ⟶ V} (h : f.right = g.right) : f = g := by tidy @[simp] lemma under_left (U : under X) : U.left = punit.star := by tidy @[simp] lemma under_morphism_left {U V : under X} (f : U ⟶ V) : f.left = 𝟙 punit.star := by tidy @[simp] lemma id_right (U : under X) : comma_morphism.right (𝟙 U) = 𝟙 U.right := rfl @[simp] lemma comp_right (a b c : under X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).right = f.right ≫ g.right := rfl @[simp] lemma w {A B : under X} (f : A ⟶ B) : A.hom ≫ f.right = B.hom := by have := f.w; tidy def mk {X Y : T} (f : X ⟶ Y) : under X := { right := Y, hom := f } @[simp] lemma mk_right {X Y : T} (f : X ⟶ Y) : (mk f).right = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : X ⟶ Y) : (mk f).hom = f := rfl def hom_mk {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom . obviously) : U ⟶ V := { right := f } @[simp] lemma hom_mk_right {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom) : (hom_mk f).right = f := rfl def forget : (under X) ⥤ T := comma.snd _ _ @[simp] lemma forget_obj {U : under X} : forget.obj U = U.right := rfl @[simp] lemma forget_map {U V : under X} {f : U ⟶ V} : forget.map f = f.right := rfl def map {Y : T} (f : X ⟶ Y) : under Y ⥤ under X := comma.map_left _ $ functor.of.map f section variables {Y : T} {f : X ⟶ Y} {U V : under Y} {g : U ⟶ V} @[simp] lemma map_obj_right : ((map f).obj U).right = U.right := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = f ≫ U.hom := rfl @[simp] lemma map_map_right : ((map f).map g).right = g.right := rfl end section variables {D : Type u₃} [𝒟 : category.{v₃} D] include 𝒟 def post {X : T} (F : T ⥤ D) : under X ⥤ under (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { right := F.map f.right, w' := by tidy; erw [← F.map_comp, w] } } end end under end category_theory
6e2fcaa958950e3a37caae0b95079addd52e86d8
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/src/Lean/Elab/Attributes.lean
aaf8b61cb3335240cbea97c25e9511695c13e9a8
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,049
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.Parser.Basic import Lean.Attributes import Lean.MonadEnv namespace Lean namespace Elab structure Attribute := (name : Name) (args : Syntax := Syntax.missing) instance Attribute.hasFormat : HasFormat Attribute := ⟨fun attr => Format.bracket "@[" (toString attr.name ++ (if attr.args.isMissing then "" else toString attr.args)) "]"⟩ instance Attribute.inhabited : Inhabited Attribute := ⟨{ name := arbitrary _ }⟩ def elabAttr {m} [Monad m] [MonadEnv m] [MonadError m] (stx : Syntax) : m Attribute := do -- rawIdent >> many attrArg let nameStx := stx.getArg 0; attrName ← match nameStx.isIdOrAtom? with | none => withRef nameStx $ throwError "identifier expected" | some str => pure $ mkNameSimple str; env ← getEnv; unless (isAttribute env attrName) $ throwError ("unknown attribute [" ++ attrName ++ "]"); let args := stx.getArg 1; -- the old frontend passes Syntax.missing for empty args, for reasons let args := if args.getNumArgs == 0 then Syntax.missing else args; pure { name := attrName, args := args } def elabAttrs {m} [Monad m] [MonadEnv m] [MonadError m] (stx : Syntax) : m (Array Attribute) := (stx.getArg 1).foldSepArgsM (fun stx attrs => do attr ← elabAttr stx; pure $ attrs.push attr) #[] def applyAttributesImp (declName : Name) (attrs : Array Attribute) (applicationTime : AttributeApplicationTime) : CoreM Unit := attrs.forM $ fun attr => do env ← getEnv; match getAttributeImpl env attr.name with | Except.error errMsg => throwError errMsg | Except.ok attrImpl => when (attrImpl.applicationTime == applicationTime) do attrImpl.add declName attr.args true def applyAttributes {m} [MonadLiftT CoreM m] (declName : Name) (attrs : Array Attribute) (applicationTime : AttributeApplicationTime) : m Unit := liftM $ applyAttributesImp declName attrs applicationTime end Elab end Lean
a1f484dc9d3727575dab9f89fa22338d1dbf33e8
5d166a16ae129621cb54ca9dde86c275d7d2b483
/library/init/native/result.lean
5bedf464d8435b718d87995a0d4eb784e364293d
[ "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
2,411
lean
/- Copyright (c) 2016 Jared Roesch. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jared Roesch -/ prelude import init.meta.interactive namespace native inductive result (E : Type) (R : Type) : Type | err {} : E → result | ok {} : R → result def unwrap_or {E T : Type} : result E T → T → T | (result.err _) default := default | (result.ok t) _ := t def result.and_then {E T U : Type} : result E T → (T → result E U) → result E U | (result.err e) _ := result.err e | (result.ok t) f := f t instance result_monad (E : Type) : monad (result E) := {pure := @result.ok E, bind := @result.and_then E, id_map := by intros; cases x; dsimp [result.and_then]; apply rfl, pure_bind := by intros; apply rfl, bind_assoc := by intros; cases x; simp [result.and_then]} inductive resultT (M : Type → Type) (E : Type) (A : Type) : Type | run : M (result E A) → resultT section resultT variable {M : Type → Type} def resultT.pure [monad : monad M] {E A : Type} (x : A) : resultT M E A := resultT.run $ return (result.ok x) def resultT.and_then [monad : monad M] {E A B : Type} : resultT M E A → (A → resultT M E B) → resultT M E B | (resultT.run action) f := resultT.run (do res_a ← action, -- a little ugly with this match match res_a with | result.err e := return (result.err e) | result.ok a := let (resultT.run actionB) := f a in actionB end) instance resultT_monad [m : monad M] (E : Type) : monad (resultT M E) := {pure := @resultT.pure M m E, bind := @resultT.and_then M m E, id_map := begin intros, cases x, dsimp [resultT.and_then], assert h : @resultT.and_then._match_1 _ m E α _ resultT.pure = pure, { apply funext, intro x, cases x; simp [resultT.and_then, resultT.pure, resultT.and_then] }, { rw [h, @monad.bind_pure _ (result E α) _] }, end, pure_bind := begin intros, dsimp [resultT.pure, resultT.and_then], rw [monad.pure_bind], dsimp [resultT.and_then], cases f x, dsimp [resultT.and_then], apply rfl, end, bind_assoc := begin intros, cases x, dsimp [resultT.and_then], apply congr_arg, rw [monad.bind_assoc], apply congr_arg, apply funext, intro, cases x with e a; dsimp [resultT.and_then], { rw [monad.pure_bind], apply rfl }, { cases f a, apply rfl }, end} end resultT end native
e87342782fb147ce9d9bb9ac27027479dcebf678
4f643cce24b2d005aeeb5004c2316a8d6cc7f3b1
/src/o_minimal/definable.lean
e69524deeaa26478bc99a6571499b08dea61ab1b
[]
no_license
rwbarton/lean-omin
da209ed061d64db65a8f7f71f198064986f30eb9
fd733c6d95ef6f4743aae97de5e15df79877c00e
refs/heads/master
1,674,408,673,325
1,607,343,535,000
1,607,343,535,000
285,150,399
9
0
null
null
null
null
UTF-8
Lean
false
false
16,451
lean
import category_theory.category import o_minimal.structure import o_minimal.coordinates universe u namespace o_minimal open set -- Carrier for the (o-minimal) structure. variables {R : Type u} -- A structure on `R`. variables (S : struc R) /-- A type with coordinates valued in R is *definable* with respect to a given structure S on R if the corresponding subset of Rⁿ is definable according to S. -/ class is_definable (X : Type*) [has_coordinates R X] : Prop := (is_definable [] : S.definable (coordinate_image R X)) instance is_definable.self : is_definable S R := begin constructor, convert S.definable_univ 1, ext, simp [coordinate_image], use (x 0), ext i, have : i = 0 := by cc, subst i end -- TODO: Generalize to [is_definable S X] : is_definable S (finvec n X) instance is_definable.rn {n : ℕ} : is_definable S (finvec n R) := begin constructor, convert S.definable_univ n, ext, simp [coordinate_image] end variables {X : Type*} [has_coordinates R X] [is_definable S X] variables {Y : Type*} [has_coordinates R Y] [is_definable S Y] variables {Z : Type*} [has_coordinates R Z] [is_definable S Z] variables {W : Type*} [has_coordinates R W] [is_definable S W] instance is_definable.prod : is_definable S (X × Y) := begin constructor, rw coordinate_image_prod, exact S.definable_external_prod (is_definable.is_definable S X) (is_definable.is_definable S Y) end -- TODO: instances matching the rest of has_coordinates section definable_set /- We first discuss what it means for a subset `s : set X` to be definable when `X` is a definable type. -/ def def_set (s : set X) : Prop := S.definable (coords R '' s) variables {S} /-- The subtype of a definable type determined by a definable set is definable. Unfortunately, this can't be a global instance because of the hypothesis `hs`. -/ def is_definable.subtype {s : set X} (hs : def_set S s) : is_definable S s := { is_definable := begin unfold def_set at hs, convert hs, ext x, simp [coordinate_image] end } variables (X) lemma def_set_empty : def_set S (∅ : set X) := begin convert S.definable_empty _, simp [def_set] end lemma def_set_univ : def_set S (set.univ : set X) := by simpa [def_set] using is_definable.is_definable S X variable {X} lemma def_set.inter {s t : set X} (hs : def_set S s) (ht : def_set S t) : def_set S (s ∩ t) := begin convert S.definable_inter hs ht, simp [def_set, image_inter (injective_coords X)] end lemma def_set.union {s t : set X} (hs : def_set S s) (ht : def_set S t) : def_set S (s ∪ t) := begin convert S.definable_union hs ht, simp [def_set, image_union] end lemma def_set.diff {s t : set X} (hs : def_set S s) (ht : def_set S t) : def_set S (s \ t) := begin convert S.definable_diff hs ht, simp [def_set, image_diff (injective_coords X)], end lemma def_set.compl {s : set X} (hs : def_set S s) : def_set S (sᶜ) := by { rw compl_eq_univ_diff, exact (def_set_univ _).diff hs } lemma def_set.compl' {s : set X} (hs : def_set S sᶜ) : def_set S s := by { rw ←compl_compl s, exact hs.compl } lemma def_set.iff {s t : set X} (hs : def_set S s) (ht : def_set S t) : def_set S {x : X | x ∈ s ↔ x ∈ t} := begin simp only [iff_iff_and_or_not_and_not], exact (hs.inter ht).union (hs.compl.inter ht.compl) end lemma def_set_Inter {ι : Type*} [fintype ι] {t : ι → set X} (ht : ∀ i, def_set S (t i)) : def_set S (⋂ i, t i) := suffices ∀ {s : set ι}, set.finite s → def_set S (⋂ i ∈ s, t i), by { convert this finite_univ, simp }, λ s hs, finite.induction_on hs (by { convert def_set_univ _, simp, apply_instance }) -- why is apply_instance needed here? (λ i s _ _ IH, by { convert (ht i).inter IH, simp }) lemma def_set_Union {ι : Type*} [fintype ι] {t : ι → set X} (ht : ∀ i, def_set S (t i)) : def_set S (⋃ i, t i) := begin apply def_set.compl', rw compl_Union, exact def_set_Inter (λ i, (ht i).compl) end lemma def_set.or {s t : X → Prop} (hs : def_set S {x : X | s x}) (ht : def_set S {x : X | t x}) : def_set S {x | s x ∨ t x} := hs.union ht lemma def_set.and {s t : X → Prop} (hs : def_set S {x : X | s x}) (ht : def_set S {x : X | t x}) : def_set S {x | s x ∧ t x} := hs.inter ht lemma def_set.not {s : X → Prop} (hs : def_set S {x : X | s x}) : def_set S {x | ¬ s x} := hs.compl lemma def_set.imp {s t : X → Prop} (hs : def_set S {x : X | s x}) (ht : def_set S {x : X | t x}) : def_set S {x | s x → t x} := begin simp [imp_iff_not_or], -- classical! exact hs.not.or ht end lemma def_set.forall_fintype {ι : Type*} [fintype ι] {t : ι → set X} (ht : ∀ i, def_set S (t i)) : def_set S {x | ∀ i, x ∈ t i} := begin convert def_set_Inter ht using 1, ext x, simp end lemma def_set.exists_fintype {ι : Type*} [fintype ι] {t : ι → set X} (ht : ∀ i, def_set S (t i)) : def_set S {x | ∃ i, x ∈ t i} := begin convert def_set_Union ht using 1, ext x, simp end lemma def_set.proj {s : set (X × Y)} (hs : def_set S s) : def_set S (prod.fst '' s) := begin unfold def_set, convert S.definable_proj hs using 1, ext z, rw [image_image, image_image], simp only [has_coordinates.prod_coords, finvec.left_append] end -- Is it better to use `{p : Y × X | s p.2 p.1}`? -- After all, Lean prefers to form `Z × Y × X = Z × (Y × X)` lemma def_set.exists {s : X → Y → Prop} (hs : def_set S {p : X × Y | s p.1 p.2}) : def_set S {x | ∃ y, s x y} := begin convert def_set.proj hs, ext, simp end lemma def_set.forall {s : X → Y → Prop} (hs : def_set S {p : X × Y | s p.1 p.2}) : def_set S {x | ∀ y, s x y} := begin -- classical!! have : ∀ (s : X → Y → Prop) (hs : def_set S {p : X × Y | s p.1 p.2}), def_set S {x | ∀ y, ¬ s x y}, { intros t ht, simpa using ht.exists.not }, simpa using this (λ x y, ¬ s x y) hs.not, end lemma def_set.reindex {f : X → Y} (hf : is_reindexing R f) {s : set Y} (hs : def_set S s) : def_set S (f ⁻¹' s) := begin cases hf with fσ hf, unfold def_set, -- The preimage f ⁻¹' s, as a subset of the Rⁿ in which X lives, -- is the intersection of X with the preimage of s under the reindexing. convert S.definable_inter (is_definable.is_definable S X) (S.definable_reindex fσ hs), ext z, suffices : (∃ (x : X), f x ∈ s ∧ coords R x = z) ↔ z ∈ range (coords R) ∧ ∃ (y : Y), y ∈ s ∧ coords R y = z ∘ fσ, { simpa }, -- TODO: funext'd version of `is_reindexing.hf` replace hf : ∀ (x : X), coords R x ∘ fσ = coords R (f x) := λ x, funext (λ i, (hf x i)), split, { rintro ⟨x, hfx, rfl⟩, refine ⟨mem_range_self _, f x, hfx, (hf x).symm⟩ }, { rintro ⟨⟨x, rfl⟩, y, hy, H⟩, rw hf x at H, replace hf := injective_coords _ H, subst y, exact ⟨x, hy, rfl⟩ } end lemma def_set_diag : def_set S {p : X × X | p.1 = p.2} := begin unfold def_set, -- The image of the diagonal of X in Rⁿ × Rⁿ -- is the diagonal of Rⁿ intersected with X × X. convert S.definable_inter (S.definable_external_prod (is_definable.is_definable S X) (is_definable.is_definable S X)) S.definable_diag_rn, ext z, rw [mem_inter_iff, finvec.mem_prod_iff], change _ ↔ _ ∧ finvec.left z = finvec.right z, split, { rintro ⟨⟨x, y⟩, h, rfl⟩, change x = y at h, simp [coordinate_image, h] }, { rintro ⟨⟨hz₁, _⟩, hz₂⟩, rcases hz₁ with ⟨x, hx⟩, refine ⟨⟨x, x⟩, rfl, _⟩, convert finvec.left_append_right _, refine finvec.append.inj_iff.mpr _, simp [hx, hz₂] } end lemma def_set.prod_univ {s : set X} (hs : def_set S s) : def_set S {p : X × Y | p.1 ∈ s} := def_set.reindex is_reindexing.fst hs lemma def_set.univ_prod {t : set Y} (ht : def_set S t) : def_set S {p : X × Y | p.2 ∈ t} := def_set.reindex is_reindexing.snd ht lemma def_set.prod {s : set X} (hs : def_set S s) {t : set Y} (ht : def_set S t) : def_set S (s.prod t) := hs.prod_univ.inter ht.univ_prod end definable_set section definable_fun -- Now we introduce definable functions between definable types. -- They are the functions whose graphs are definable sets. /-- A function f : X → Y is definable if its graph is a definable set. -/ def def_fun (f : X → Y) : Prop := def_set S {p : X × Y | f p.1 = p.2} variables {S} lemma def_fun.id : def_fun S (id : X → X) := def_set_diag lemma def_fun.comp {g : Y → Z} (hg : def_fun S g) {f : X → Y} (hf : def_fun S f) : def_fun S (g ∘ f) := begin suffices : def_set S {p : X × Z | ∃ y, f p.1 = y ∧ g y = p.2}, { unfold def_fun, convert this, ext ⟨x, z⟩, simp }, apply def_set.exists, apply def_set.and, { have : is_reindexing R (λ p : (X × Z) × Y, (p.1.1, p.2)), { apply_rules [is_reindexing.prod, is_reindexing.fst, is_reindexing.snd, is_reindexing.comp] }, exact def_set.reindex this hf }, { have : is_reindexing R (λ p : (X × Z) × Y, (p.2, p.1.2)), { apply_rules [is_reindexing.prod, is_reindexing.fst, is_reindexing.snd, is_reindexing.comp] }, exact def_set.reindex this hg } end lemma is_reindexing.def_fun {f : X → Y} (hf : is_reindexing R f) : def_fun S f := begin cases hf with fσ hf, unfold def_fun, unfold def_set, convert S.definable_inter (S.definable_prod_rn (is_definable.is_definable S X)) (S.definable_reindex_aux fσ (def_set_univ Y)), ext z, split, { rintro ⟨⟨x, y⟩, h, rfl⟩, change f x = y at h, subst y, show _ ∧ _ ∧ _, simp only [mem_range_self, and_true, image_univ, has_coordinates.prod_coords, finvec.left_append, finvec.right_append, finvec.append_mem_prod_univ_iff], refine ⟨⟨x, _⟩, _⟩, { simp }, { ext i, apply hf, }, }, { rintro ⟨⟨x, hx⟩, ⟨hz, ⟨y, ⟨⟩, hy⟩⟩⟩, simp only [mem_image, mem_set_of_eq], use [(x,y)], split, { apply @injective_coords R, ext i, show coords R (f x) i = coords R y i, rw ← hf, rw [← hx, ← hy] at hz, exact congr_fun hz i }, { simp [hx, hy] } } end lemma def_fun.preimage {f : X → Y} (hf : def_fun S f) {s : set Y} (hs : def_set S s) : def_set S (f ⁻¹' s) := begin -- f ⁻¹' s = {x | ∃ (p : X × Y), p.1 = x ∧ p ∈ Γ(f)} convert def_set.proj (hf.inter ((def_set_univ _).prod hs)) using 1, ext, simp end lemma def_fun.coords : def_fun S (λ x : X, coords R x) := is_reindexing.def_fun is_reindexing.coords lemma def_fun.coord (i : fin (has_coordinates.ambdim R X)) : def_fun S (λ x : X, coords R x i) := is_reindexing.def_fun (is_reindexing.coord i) lemma def_fun.coord_rn {n : ℕ} (i : fin n) : def_fun S (λ x : finvec n R, x i) := def_fun.coord i lemma def_fun.fst : def_fun S (prod.fst : X × Y → X) := is_reindexing.def_fun is_reindexing.fst lemma def_fun.snd : def_fun S (prod.snd : X × Y → Y) := is_reindexing.def_fun is_reindexing.snd lemma def_fun.prod' {f : X → Y} {g : X → Z} (hf : def_fun S f) (hg : def_fun S g) : def_fun S (λ x, (f x, g x)) := begin unfold def_fun, let p1 : X × (Y × Z) → X × Y := λ p, (p.1, p.2.1), have hp1 : def_fun S p1, { apply is_reindexing.def_fun, apply is_reindexing.fst.prod (is_reindexing.fst.comp is_reindexing.snd) }, let p2 : X × (Y × Z) → X × Z := λ p, (p.1, p.2.2), have hp2 : def_fun S p2, { apply is_reindexing.def_fun, apply is_reindexing.fst.prod (is_reindexing.snd.comp is_reindexing.snd) }, convert (hp1.preimage hf).inter (hp2.preimage hg), ext ⟨x,y,z⟩, show (f x, g x) = (y,z) ↔ _, simp only [mem_inter_eq, prod.mk.inj_iff, mem_set_of_eq, preimage_set_of_eq], end lemma def_fun.prod {f : X → Z} {g : Y → W} (hf : def_fun S f) (hg : def_fun S g) : def_fun S (prod.map f g) := (hf.comp def_fun.fst).prod' (hg.comp def_fun.snd) lemma def_fun_subtype_val {s : set X} {hs : def_set S s} : by haveI := is_definable.subtype hs; exact def_fun S (subtype.val : s → X) := by haveI := is_definable.subtype hs; exact is_reindexing.subtype.val.def_fun lemma def_fun.finvec.left {n m : ℕ} : def_fun S (λ x : finvec (n+m) R, x.left) := is_reindexing.finvec.left.def_fun lemma def_fun.finvec.right {n m : ℕ} : def_fun S (λ x : finvec (n+m) R, x.right) := is_reindexing.finvec.right.def_fun lemma def_fun.finvec.init {n : ℕ} : def_fun S (λ x : finvec (n+1) R, x.init) := is_reindexing.finvec.init.def_fun lemma def_fun.finvec.snoc' {n : ℕ} : def_fun S (λ p : finvec n R × R, p.1.snoc p.2) := is_reindexing.finvec.snoc.def_fun lemma def_fun.finvec.snoc {n : ℕ} {f : X → finvec n R} (hf : def_fun S f) {g : X → R} (hg : def_fun S g) : def_fun S (λ x, (f x).snoc (g x)) := def_fun.finvec.snoc'.comp (hf.prod' hg) lemma def_set_eq {f g : X → Y} (hf : def_fun S f) (hg : def_fun S g) : def_set S {x | f x = g x} := (hf.prod' hg).preimage def_set_diag lemma def_fun.cancel {g : Y → Z} (dg : def_fun S g) (hg : function.injective g) {f : X → Y} (h : def_fun S (g ∘ f)) : def_fun S f := begin unfold def_fun, suffices : def_set S {p : X × Y | (g ∘ f) p.fst = g p.snd}, { convert ←this, ext, apply hg.eq_iff }, apply def_set_eq, { exact h.comp def_fun.fst }, { exact dg.comp def_fun.snd } end lemma def_fun_subtype_mk {s : set X} {hs : def_set S s} {f : Y → X} (df : def_fun S f) (h : ∀ y, f y ∈ s) : by haveI := is_definable.subtype hs; exact def_fun S (λ y, (⟨f y, h y⟩ : s)) := by haveI := is_definable.subtype hs; exact def_fun.cancel def_fun_subtype_val subtype.val_injective df lemma def_fun_subtype_iff {s : set X} {ds : def_set S s} {f : Y → s} : by haveI := is_definable.subtype ds; exact def_fun S f ↔ def_fun S (subtype.val ∘ f) := by haveI := is_definable.subtype ds; exact ⟨λ h, def_fun_subtype_val.comp h, λ h, def_fun_subtype_val.cancel subtype.val_injective h⟩ lemma def_fun.image {f : X → Y} (hf : def_fun S f) {s : set X} (hs : def_set S s) : def_set S (f '' s) := show def_set S {y | ∃ x, x ∈ s ∧ f x = y}, from def_set.exists $ (def_fun.preimage def_fun.snd hs).and (def_set_eq (hf.comp def_fun.snd) (def_fun.fst)) lemma def_fun.range {f : X → Y} (hf : def_fun S f) : def_set S (range f) := by { rw ←image_univ, exact hf.image (def_set_univ _) } end definable_fun section definable_val -- Finally, a "value" (element) of X is definable -- if the corresponding singleton set is definable. -- -- This notion is mostly used for bootstrapping -- because in the o-minimal project we're only interested in -- structures S on R in which every r ∈ R is definable, -- which forces every value of every definable type to be definable. /-- A value `x : X` is definable if `{x}` is definable. -/ def def_val (x : X) : Prop := def_set S ({x} : set X) variables (S) /-- A structure `S` on `R` has *definable constants* if every `r : R` is definable. -/ class definable_constants : Prop := (definable_val : ∀ (r : R), def_val S r) variables {S} -- These primed lemmas take `def_val` arguments -- and have unprimed variants which use a `definable_constants S` assumption. lemma def_set_eq_const' {f : X → Y} (hf : def_fun S f) {y : Y} (hy : def_val S y) : def_set S {x | f x = y} := show def_set S (f ⁻¹' {y}), from hf.preimage hy lemma def_set_const_eq' {f : X → Y} (hf : def_fun S f) {y : Y} (hy : def_val S y) : def_set S {x | y = f x} := by { convert def_set_eq_const' hf hy, simp_rw [eq_comm] } lemma def_fun_const' {y : Y} (hy : def_val S y) : def_fun S (λ (x : X), y) := def_set_const_eq' def_fun.snd hy lemma def_val_const [definable_constants S] {x : X} : def_val S x := begin unfold def_val, have : {x} = ⋂ i, {x' : X | coords R x' i = coords R x i}, { ext x', rw [mem_singleton_iff, mem_Inter], exact (@injective_coords R X _).eq_iff.symm.trans function.funext_iff }, rw this, apply def_set_Inter, intro i, exact def_set_eq_const' (def_fun.coord i) (definable_constants.definable_val _) end lemma def_set_eq_const [definable_constants S] {f : X → Y} (hf : def_fun S f) (y : Y) : def_set S {x | f x = y} := def_set_eq_const' hf def_val_const lemma def_set_const_eq [definable_constants S] {f : X → Y} (hf : def_fun S f) (y : Y) : def_set S {x | y = f x} := def_set_const_eq' hf def_val_const lemma def_fun_const [definable_constants S] {y : Y} : def_fun S (λ (x : X), y) := def_fun_const' def_val_const -- TODO: more lemmas as needed. end definable_val end o_minimal
3c368faf98b2d131e0121f787ea88ec7e117482b
f3a5af2927397cf346ec0e24312bfff077f00425
/src/game/world6/level1.lean
9ff64e24578a8df02d896cf58b7670e9211f0c45
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/natural_number_game
05c39e1586408cfb563d1a12e1085a90726ab655
f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd
refs/heads/master
1,688,570,964,990
1,636,908,242,000
1,636,908,242,000
195,403,790
277
84
Apache-2.0
1,694,547,955,000
1,562,328,792,000
Lean
UTF-8
Lean
false
false
3,043
lean
/- # Proposition world. A Proposition is a true/false statement, like `2 + 2 = 4` or `2 + 2 = 5`. Just like we can have concrete sets in Lean like `mynat`, and abstract sets called things like `X`, we can also have concrete propositions like `2 + 2 = 5` and abstract propositions called things like `P`. Mathematicians are very good at conflating a theorem with its proof. They might say "now use theorem 12 and we're done". What they really mean is "now use the proof of theorem 12..." (i.e. the fact that we proved it already). Particularly problematic is the fact that mathematicians use the word Proposition to mean "a relatively straightforward statement which is true" and computer scientists use it to mean "a statement of arbitrary complexity, which might be true or false". Computer scientists are far more careful about distinguishing between a proposition and a proof. For example: `x + 0 = x` is a proposition, and `add_zero x` is its proof. The convention we'll use is capital letters for propositions and small letters for proofs. In this world you will see the local context in the following kind of state: ``` P : Prop p : P ``` Here `P` is the true/false statement (the statement of proposition), and `p` is its proof. It's like `P` being the set and `p` being the element. In fact computer scientists sometimes think about the following analogy: propositions are like sets, and their proofs are like their elements. ## What's going on in this world? We're going to learn about manipulating propositions and proofs. Fortunately, we don't need to learn a bunch of new tactics -- the ones we just learnt (`exact`, `intro`, `have`, `apply`) will be perfect. The levels in proposition world are "back to normal", we're proving theorems, not constructing elements of sets. Or are we? If you delete the sorry below then your local context will look like this: ``` P Q : Prop, p : P, h : P → Q ⊢ Q ``` In this situation, we have true/false statements $P$ and $Q$, a proof $p$ of $P$, and $h$ is the hypothesis that $P\implies Q$. Our goal is to construct a proof of $Q$. It's clear what to do *mathematically* to solve this goal, $P$ is true and $P$ implies $Q$ so $Q$ is true. But how to do it in Lean? Adopting a point of view wholly unfamiliar to many mathematicians, Lean interprets the hypothesis $h$ as a function from proofs of $P$ to proofs of $Q$, so the rather surprising approach `exact h(p),` works to close the goal. Note that `exact h(P),` (with a capital P) won't work; this is a common error I see from beginners. "We're trying to solve `P` so it's exactly `P`". The goal states the *theorem*, your job is to construct the *proof*. $P$ is not a proof of $P$, it's $p$ that is a proof of $P$. In Lean, Propositions, like sets, are types, and proofs, like elements of sets, are terms. ## Level 1: the `exact` tactic. -/ /- Lemma : no-side-bar If $P$ is true, and $P\implies Q$ is also true, then $Q$ is true. -/ example (P Q : Prop) (p : P) (h : P → Q) : Q := begin exact h(p), end
8dcf5d562711eb6a5b5696cfae47ae0869109e42
4f643cce24b2d005aeeb5004c2316a8d6cc7f3b1
/src/for_mathlib/finite.lean
cc938b425291f7c99017a59d3cced895eae02ad1
[]
no_license
rwbarton/lean-omin
da209ed061d64db65a8f7f71f198064986f30eb9
fd733c6d95ef6f4743aae97de5e15df79877c00e
refs/heads/master
1,674,408,673,325
1,607,343,535,000
1,607,343,535,000
285,150,399
9
0
null
null
null
null
UTF-8
Lean
false
false
2,351
lean
import data.set.finite import logic.function.iterate variables {α : Type*} {β : Type*} @[simp] lemma finset.not_nonempty (s : finset α) : ¬s.nonempty ↔ s = ∅ := begin classical, rw [finset.nonempty_iff_ne_empty, not_not], end namespace set lemma finite_of_finite_image_fibers {s : set α} {f : α → β} (himg : finite (f '' s)) (hfib : ∀ b, finite {a ∈ s | f a = b}) : finite s := begin have : s = ⋃ (b ∈ f '' s), {a ∈ s | f a = b}, { ext a, rw mem_bUnion_iff, split, { intro h, exact ⟨f a, mem_image_of_mem _ h, ⟨h, rfl⟩⟩ }, { rintro ⟨_, _, ⟨h, _⟩⟩, exact h } }, rw this, exact finite.bUnion himg (λ b _, hfib b) end lemma infinite_of_subset {s₁ s₂ : set α} (h : s₁ ⊆ s₂) (h₁ : s₁.infinite) : s₂.infinite := begin rw ← infinite_coe_iff at h₁ ⊢, resetI, apply infinite.of_injective _ (set.inclusion_injective h), end instance Ioo.densely_ordered [preorder α] [densely_ordered α] (a b : α) : densely_ordered (Ioo a b) := begin constructor, rintros ⟨x, hx⟩ ⟨y, hy⟩ h, change x < y at h, choose z hz using exists_between h, exact ⟨⟨z, lt_trans hx.1 hz.1, lt_trans hz.2 hy.2⟩, hz.1, hz.2⟩, end lemma Ioo.infinite [preorder α] [densely_ordered α] {x y : α} (h : x < y) : infinite (Ioo x y) := begin obtain ⟨c, hc₁, hc₂⟩ : ∃ c : α, x < c ∧ c < y := exists_between h, intro f, letI := f.fintype, have : well_founded ((<) : Ioo x y → Ioo x y → Prop) := fintype.well_founded_of_trans_of_irrefl _, obtain ⟨m, _, hm⟩ : ∃ (m : Ioo x y) _, ∀ d ∈ univ, ¬ d < m := this.has_min univ ⟨⟨c, hc₁, hc₂⟩, trivial⟩, obtain ⟨z, hz₁, hz₂⟩ : ∃ (z : α), x < z ∧ z < m := exists_between m.2.1, refine hm ⟨z, hz₁, lt_trans hz₂ m.2.2⟩ trivial hz₂ end lemma Ico.infinite [preorder α] [densely_ordered α] {a b : α} (h : a < b) : infinite (Ico a b) := set.infinite_of_subset Ioo_subset_Ico_self (Ioo.infinite h) lemma Ioc.infinite [preorder α] [densely_ordered α] {a b : α} (h : a < b) : infinite (Ioc a b) := set.infinite_of_subset Ioo_subset_Ioc_self (Ioo.infinite h) lemma Icc.infinite [preorder α] [densely_ordered α] {a b : α} (h : a < b) : infinite (Icc a b) := set.infinite_of_subset Ioo_subset_Icc_self (Ioo.infinite h) end set
25f326d987dbea599490c3b4b2c259966d0182e8
18425d4bab0b5e4677ef791d0065e16639493248
/data/fintype.lean
ae8823f1fd43368c484fd27259a51e33b4a87813
[ "MIT" ]
permissive
tizmd/lean-finitary
5feb94a2474b55c0f23e4b61b9ac42403bf222f7
8958fdb3fa3d9fcc304e116fd339448875025e95
refs/heads/master
1,611,397,193,043
1,497,517,825,000
1,497,517,825,000
93,048,842
0
0
null
null
null
null
UTF-8
Lean
false
false
1,768
lean
import finitary data.fin.misc universe u class fintype (α : Type u) := (size : ℕ) (finitary : finitary α size) namespace fintype variables {α : Type u}[fintype α] @[reducible] def enum : α → fin (size α) := (finitary α).map @[reducible] def index : fin (size α) → α := (finitary α).bijection.inv def elems : list α := list.map (@index α _) (fin.elems (size α)) theorem mem_elems : ∀ a : α, a ∈ (elems : list α) := begin intro a, assert H : index (enum a) ∈ (elems : list α), { simp [elems], apply list.mem_map, apply fin.mem_elems }, simp [enum,index] at H, rw (finitary α).bijection.left_inverse_of_inv at H, assumption end end fintype def fsubtype {α : Type u}{l : list α} (prop : list.nodup l) := { a : α // a ∈ l } namespace fsubtype variables {α : Type u}[decidable_eq α]{l : list α} {prop : list.nodup l} def index : fsubtype prop → fin l.length := take ⟨a, hmem⟩, fin.left_index hmem def enum : fin l.length → fsubtype prop := take i, ⟨fin.nth l i, fin.mem_nth _ _⟩ def enum_index_inverse : function.left_inverse enum (@index _ _ _ prop) := take ⟨a, hmem⟩, subtype.eq $ fin.nth_left_index_left_inverse hmem def index_enum_inverse : function.right_inverse enum (@index _ _ _ prop) := take i, fin.left_index_mem_nth_left_inverse_of_nodup prop i end fsubtype instance {α : Type u}[decidable_eq α]{l : list α} {prop : list.nodup l} : fintype (fsubtype prop) := { size := l.length, finitary := { map := fsubtype.index, bijection := { inv := fsubtype.enum, left_inverse_of_inv := fsubtype.enum_index_inverse, right_inverse_of_inv := fsubtype.index_enum_inverse } } }
97a20335ae63332bae69b11bc1021a746386d3bd
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/deprecated/subgroup.lean
e6eddcd5c7c44cdb8f747137e8c291cefed2b245
[]
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
24,573
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, Mitchell Rowett, Scott Morrison, Johan Commelin, Mario Carneiro, Michael Howes -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.group_theory.subgroup import Mathlib.deprecated.submonoid import Mathlib.PostPort universes u_3 l u_1 u_2 u_4 namespace Mathlib /-- `s` is an additive subgroup: a set containing 0 and closed under addition and negation. -/ class is_add_subgroup {A : Type u_3} [add_group A] (s : set A) extends is_add_submonoid s where neg_mem : ∀ {a : A}, a ∈ s → -a ∈ s /-- `s` is a subgroup: a set containing 1 and closed under multiplication and inverse. -/ class is_subgroup {G : Type u_1} [group G] (s : set G) extends is_submonoid s where inv_mem : ∀ {a : G}, a ∈ s → a⁻¹ ∈ s theorem is_subgroup.div_mem {G : Type u_1} [group G] {s : set G} [is_subgroup s] {x : G} {y : G} (hx : x ∈ s) (hy : y ∈ s) : x / y ∈ s := sorry theorem additive.is_add_subgroup {G : Type u_1} [group G] (s : set G) [is_subgroup s] : is_add_subgroup s := is_add_subgroup.mk is_subgroup.inv_mem theorem additive.is_add_subgroup_iff {G : Type u_1} [group G] {s : set G} : is_add_subgroup s ↔ is_subgroup s := sorry theorem multiplicative.is_subgroup {A : Type u_3} [add_group A] (s : set A) [is_add_subgroup s] : is_subgroup s := is_subgroup.mk is_add_subgroup.neg_mem theorem multiplicative.is_subgroup_iff {A : Type u_3} [add_group A] {s : set A} : is_subgroup s ↔ is_add_subgroup s := sorry /-- The group structure on a subgroup coerced to a type. -/ def subtype.group {G : Type u_1} [group G] {s : set G} [is_subgroup s] : group ↥s := group.mk monoid.mul sorry monoid.one sorry sorry (fun (x : ↥s) => { val := ↑x⁻¹, property := sorry }) (fun (x y : ↥s) => { val := ↑x / ↑y, property := sorry }) sorry /-- The commutative group structure on a commutative subgroup coerced to a type. -/ def subtype.comm_group {G : Type u_1} [comm_group G] {s : set G} [is_subgroup s] : comm_group ↥s := comm_group.mk group.mul sorry group.one sorry sorry group.inv group.div sorry sorry @[simp] theorem is_subgroup.coe_inv {G : Type u_1} [group G] {s : set G} [is_subgroup s] (a : ↥s) : ↑(a⁻¹) = (↑a⁻¹) := rfl @[simp] theorem is_subgroup.coe_gpow {G : Type u_1} [group G] {s : set G} [is_subgroup s] (a : ↥s) (n : ℤ) : ↑(a ^ n) = ↑a ^ n := sorry @[simp] theorem is_add_subgroup.gsmul_coe {A : Type u_3} [add_group A] {s : set A} [is_add_subgroup s] (a : ↥s) (n : ℤ) : ↑(n •ℤ a) = n •ℤ ↑a := sorry theorem is_add_subgroup.of_add_neg {G : Type u_1} [add_group G] (s : set G) (one_mem : 0 ∈ s) (div_mem : ∀ {a b : G}, a ∈ s → b ∈ s → a + -b ∈ s) : is_add_subgroup s := sorry theorem is_add_subgroup.of_sub {A : Type u_3} [add_group A] (s : set A) (zero_mem : 0 ∈ s) (sub_mem : ∀ {a b : A}, a ∈ s → b ∈ s → a - b ∈ s) : is_add_subgroup s := sorry protected instance is_add_subgroup.inter {G : Type u_1} [add_group G] (s₁ : set G) (s₂ : set G) [is_add_subgroup s₁] [is_add_subgroup s₂] : is_add_subgroup (s₁ ∩ s₂) := is_add_subgroup.mk fun (x : G) (hx : x ∈ s₁ ∩ s₂) => { left := is_add_subgroup.neg_mem (and.left hx), right := is_add_subgroup.neg_mem (and.right hx) } protected instance is_add_subgroup.Inter {G : Type u_1} [add_group G] {ι : Sort u_2} (s : ι → set G) [h : ∀ (y : ι), is_add_subgroup (s y)] : is_add_subgroup (set.Inter s) := is_add_subgroup.mk fun (x : G) (h_1 : x ∈ set.Inter s) => iff.mpr set.mem_Inter fun (y : ι) => is_add_subgroup.neg_mem (iff.mp set.mem_Inter h_1 y) theorem is_add_subgroup_Union_of_directed {G : Type u_1} [add_group G] {ι : Type u_2} [hι : Nonempty ι] (s : ι → set G) [∀ (i : ι), is_add_subgroup (s i)] (directed : ∀ (i j : ι), ∃ (k : ι), s i ⊆ s k ∧ s j ⊆ s k) : is_add_subgroup (set.Union fun (i : ι) => s i) := sorry def gpowers {G : Type u_1} [group G] (x : G) : set G := set.range (pow x) def gmultiples {A : Type u_3} [add_group A] (x : A) : set A := set.range fun (i : ℤ) => i •ℤ x protected instance gpowers.is_subgroup {G : Type u_1} [group G] (x : G) : is_subgroup (gpowers x) := is_subgroup.mk fun (x₀ : G) (_x : x₀ ∈ gpowers x) => sorry protected instance gmultiples.is_add_subgroup {A : Type u_3} [add_group A] (x : A) : is_add_subgroup (gmultiples x) := iff.mp multiplicative.is_subgroup_iff (gpowers.is_subgroup x) theorem is_subgroup.gpow_mem {G : Type u_1} [group G] {a : G} {s : set G} [is_subgroup s] (h : a ∈ s) {i : ℤ} : a ^ i ∈ s := int.cases_on i (fun (i : ℕ) => idRhs (a ^ i ∈ s) (is_submonoid.pow_mem h)) fun (i : ℕ) => idRhs (a ^ Nat.succ i⁻¹ ∈ s) (is_subgroup.inv_mem (is_submonoid.pow_mem h)) theorem is_add_subgroup.gsmul_mem {A : Type u_3} [add_group A] {a : A} {s : set A} [is_add_subgroup s] : a ∈ s → ∀ {i : ℤ}, i •ℤ a ∈ s := is_subgroup.gpow_mem theorem gpowers_subset {G : Type u_1} [group G] {a : G} {s : set G} [is_subgroup s] (h : a ∈ s) : gpowers a ⊆ s := sorry theorem gmultiples_subset {A : Type u_3} [add_group A] {a : A} {s : set A} [is_add_subgroup s] (h : a ∈ s) : gmultiples a ⊆ s := gpowers_subset h theorem mem_gpowers {G : Type u_1} [group G] {a : G} : a ∈ gpowers a := sorry theorem mem_gmultiples {A : Type u_3} [add_group A] {a : A} : a ∈ gmultiples a := sorry namespace is_subgroup theorem inv_mem_iff {G : Type u_1} {a : G} [group G] (s : set G) [is_subgroup s] : a⁻¹ ∈ s ↔ a ∈ s := sorry theorem Mathlib.is_add_subgroup.add_mem_cancel_right {G : Type u_1} {a : G} {b : G} [add_group G] (s : set G) [is_add_subgroup s] (h : a ∈ s) : b + a ∈ s ↔ b ∈ s := sorry theorem Mathlib.is_add_subgroup.add_mem_cancel_left {G : Type u_1} {a : G} {b : G} [add_group G] (s : set G) [is_add_subgroup s] (h : a ∈ s) : a + b ∈ s ↔ b ∈ s := sorry end is_subgroup class normal_add_subgroup {A : Type u_3} [add_group A] (s : set A) extends is_add_subgroup s where normal : ∀ (n : A), n ∈ s → ∀ (g : A), g + n + -g ∈ s class normal_subgroup {G : Type u_1} [group G] (s : set G) extends is_subgroup s where normal : ∀ (n : G), n ∈ s → ∀ (g : G), g * n * (g⁻¹) ∈ s theorem normal_add_subgroup_of_add_comm_group {G : Type u_1} [add_comm_group G] (s : set G) [hs : is_add_subgroup s] : normal_add_subgroup s := sorry theorem additive.normal_add_subgroup {G : Type u_1} [group G] (s : set G) [normal_subgroup s] : normal_add_subgroup s := normal_add_subgroup.mk normal_subgroup.normal theorem additive.normal_add_subgroup_iff {G : Type u_1} [group G] {s : set G} : normal_add_subgroup s ↔ normal_subgroup s := sorry theorem multiplicative.normal_subgroup {A : Type u_3} [add_group A] (s : set A) [normal_add_subgroup s] : normal_subgroup s := normal_subgroup.mk normal_add_subgroup.normal theorem multiplicative.normal_subgroup_iff {A : Type u_3} [add_group A] {s : set A} : normal_subgroup s ↔ normal_add_subgroup s := sorry namespace is_subgroup -- Normal subgroup properties theorem mem_norm_comm {G : Type u_1} [group G] {s : set G} [normal_subgroup s] {a : G} {b : G} (hab : a * b ∈ s) : b * a ∈ s := sorry theorem Mathlib.is_add_subgroup.mem_norm_comm_iff {G : Type u_1} [add_group G] {s : set G} [normal_add_subgroup s] {a : G} {b : G} : a + b ∈ s ↔ b + a ∈ s := { mp := is_add_subgroup.mem_norm_comm, mpr := is_add_subgroup.mem_norm_comm } /-- The trivial subgroup -/ def trivial (G : Type u_1) [group G] : set G := singleton 1 @[simp] theorem Mathlib.is_add_subgroup.mem_trivial {G : Type u_1} [add_group G] {g : G} : g ∈ is_add_subgroup.trivial G ↔ g = 0 := set.mem_singleton_iff protected instance Mathlib.is_add_subgroup.trivial_normal {G : Type u_1} [add_group G] : normal_add_subgroup (is_add_subgroup.trivial G) := sorry theorem Mathlib.is_add_subgroup.eq_trivial_iff {G : Type u_1} [add_group G] {s : set G} [is_add_subgroup s] : s = is_add_subgroup.trivial G ↔ ∀ (x : G), x ∈ s → x = 0 := sorry protected instance univ_subgroup {G : Type u_1} [group G] : normal_subgroup set.univ := normal_subgroup.mk (eq.mpr (id (Eq.trans (forall_congr_eq fun (n : G) => Eq.trans (imp_congr_eq (propext ((fun {α : Type u_1} (x : α) => iff_true_intro (set.mem_univ x)) n)) (Eq.trans (forall_congr_eq fun (g : G) => propext ((fun {α : Type u_1} (x : α) => iff_true_intro (set.mem_univ x)) (g * n * (g⁻¹)))) (propext (forall_const G)))) (propext (forall_prop_of_true True.intro))) (propext (forall_const G)))) trivial) def Mathlib.is_add_subgroup.add_center (G : Type u_1) [add_group G] : set G := set_of fun (z : G) => ∀ (g : G), g + z = z + g theorem mem_center {G : Type u_1} [group G] {a : G} : a ∈ center G ↔ ∀ (g : G), g * a = a * g := iff.rfl protected instance center_normal {G : Type u_1} [group G] : normal_subgroup (center G) := sorry def Mathlib.is_add_subgroup.add_normalizer {G : Type u_1} [add_group G] (s : set G) : set G := set_of fun (g : G) => ∀ (n : G), n ∈ s ↔ g + n + -g ∈ s protected instance Mathlib.is_add_subgroup.normalizer_is_add_subgroup {G : Type u_1} [add_group G] (s : set G) : is_add_subgroup (is_add_subgroup.add_normalizer s) := sorry theorem Mathlib.is_add_subgroup.subset_add_normalizer {G : Type u_1} [add_group G] (s : set G) [is_add_subgroup s] : s ⊆ is_add_subgroup.add_normalizer s := sorry /-- Every subgroup is a normal subgroup of its normalizer -/ protected instance Mathlib.is_add_subgroup.add_normal_in_add_normalizer {G : Type u_1} [add_group G] (s : set G) [is_add_subgroup s] : normal_add_subgroup (subtype.val ⁻¹' s) := normal_add_subgroup.mk fun (a : ↥(is_add_subgroup.add_normalizer s)) (ha : a ∈ subtype.val ⁻¹' s) (_x : ↥(is_add_subgroup.add_normalizer s)) => sorry end is_subgroup -- Homomorphism subgroups namespace is_group_hom def ker {G : Type u_1} {H : Type u_2} [group H] (f : G → H) : set G := f ⁻¹' is_subgroup.trivial H theorem Mathlib.is_add_group_hom.mem_ker {G : Type u_1} {H : Type u_2} [add_group H] (f : G → H) {x : G} : x ∈ is_add_group_hom.ker f ↔ f x = 0 := is_add_subgroup.mem_trivial theorem Mathlib.is_add_group_hom.zero_ker_neg {G : Type u_1} {H : Type u_2} [add_group G] [add_group H] (f : G → H) [is_add_group_hom f] {a : G} {b : G} (h : f (a + -b) = 0) : f a = f b := sorry theorem one_ker_inv' {G : Type u_1} {H : Type u_2} [group G] [group H] (f : G → H) [is_group_hom f] {a : G} {b : G} (h : f (a⁻¹ * b) = 1) : f a = f b := sorry theorem Mathlib.is_add_group_hom.neg_ker_zero {G : Type u_1} {H : Type u_2} [add_group G] [add_group H] (f : G → H) [is_add_group_hom f] {a : G} {b : G} (h : f a = f b) : f (a + -b) = 0 := sorry theorem inv_ker_one' {G : Type u_1} {H : Type u_2} [group G] [group H] (f : G → H) [is_group_hom f] {a : G} {b : G} (h : f a = f b) : f (a⁻¹ * b) = 1 := sorry theorem Mathlib.is_add_group_hom.zero_iff_ker_neg {G : Type u_1} {H : Type u_2} [add_group G] [add_group H] (f : G → H) [is_add_group_hom f] (a : G) (b : G) : f a = f b ↔ f (a + -b) = 0 := { mp := is_add_group_hom.neg_ker_zero f, mpr := is_add_group_hom.zero_ker_neg f } theorem one_iff_ker_inv' {G : Type u_1} {H : Type u_2} [group G] [group H] (f : G → H) [is_group_hom f] (a : G) (b : G) : f a = f b ↔ f (a⁻¹ * b) = 1 := { mp := inv_ker_one' f, mpr := one_ker_inv' f } theorem inv_iff_ker {G : Type u_1} {H : Type u_2} [group G] [group H] (f : G → H) [w : is_group_hom f] (a : G) (b : G) : f a = f b ↔ a * (b⁻¹) ∈ ker f := eq.mpr (id (Eq._oldrec (Eq.refl (f a = f b ↔ a * (b⁻¹) ∈ ker f)) (propext (mem_ker f)))) (one_iff_ker_inv f a b) theorem inv_iff_ker' {G : Type u_1} {H : Type u_2} [group G] [group H] (f : G → H) [w : is_group_hom f] (a : G) (b : G) : f a = f b ↔ a⁻¹ * b ∈ ker f := eq.mpr (id (Eq._oldrec (Eq.refl (f a = f b ↔ a⁻¹ * b ∈ ker f)) (propext (mem_ker f)))) (one_iff_ker_inv' f a b) protected instance Mathlib.is_add_group_hom.image_add_subgroup {G : Type u_1} {H : Type u_2} [add_group G] [add_group H] (f : G → H) [is_add_group_hom f] (s : set G) [is_add_subgroup s] : is_add_subgroup (f '' s) := is_add_subgroup.mk fun (a : H) (_x : a ∈ f '' s) => sorry protected instance range_subgroup {G : Type u_1} {H : Type u_2} [group G] [group H] (f : G → H) [is_group_hom f] : is_subgroup (set.range f) := set.image_univ ▸ is_group_hom.image_subgroup f set.univ protected instance preimage {G : Type u_1} {H : Type u_2} [group G] [group H] (f : G → H) [is_group_hom f] (s : set H) [is_subgroup s] : is_subgroup (f ⁻¹' s) := is_subgroup.mk (eq.mpr (id (Eq.trans (forall_congr_eq fun (a : G) => Eq.trans (imp_congr_ctx_eq (propext set.mem_preimage) fun (_h : f a ∈ s) => Eq.trans (Eq.trans (propext set.mem_preimage) ((fun (ᾰ ᾰ_1 : H) (e_2 : ᾰ = ᾰ_1) (ᾰ_2 ᾰ_3 : set H) (e_3 : ᾰ_2 = ᾰ_3) => congr (congr_arg has_mem.mem e_2) e_3) (f (a⁻¹)) (f a⁻¹) (map_inv f a) s s (Eq.refl s))) (propext ((fun [c : is_subgroup s] {a : H} (ᾰ : a ∈ s) => iff_true_intro (is_subgroup.inv_mem ᾰ)) (iff.mpr (iff_true_intro _h) True.intro)))) (propext forall_true_iff)) (propext (forall_const G)))) trivial) protected instance preimage_normal {G : Type u_1} {H : Type u_2} [group G] [group H] (f : G → H) [is_group_hom f] (s : set H) [normal_subgroup s] : normal_subgroup (f ⁻¹' s) := sorry protected instance normal_subgroup_ker {G : Type u_1} {H : Type u_2} [group G] [group H] (f : G → H) [is_group_hom f] : normal_subgroup (ker f) := is_group_hom.preimage_normal f (is_subgroup.trivial H) theorem Mathlib.is_add_group_hom.injective_of_trivial_ker {G : Type u_1} {H : Type u_2} [add_group G] [add_group H] (f : G → H) [is_add_group_hom f] (h : is_add_group_hom.ker f = is_add_subgroup.trivial G) : function.injective f := sorry theorem trivial_ker_of_injective {G : Type u_1} {H : Type u_2} [group G] [group H] (f : G → H) [is_group_hom f] (h : function.injective f) : ker f = is_subgroup.trivial G := sorry theorem Mathlib.is_add_group_hom.injective_iff_trivial_ker {G : Type u_1} {H : Type u_2} [add_group G] [add_group H] (f : G → H) [is_add_group_hom f] : function.injective f ↔ is_add_group_hom.ker f = is_add_subgroup.trivial G := { mp := is_add_group_hom.trivial_ker_of_injective f, mpr := is_add_group_hom.injective_of_trivial_ker f } theorem Mathlib.is_add_group_hom.trivial_ker_iff_eq_zero {G : Type u_1} {H : Type u_2} [add_group G] [add_group H] (f : G → H) [is_add_group_hom f] : is_add_group_hom.ker f = is_add_subgroup.trivial G ↔ ∀ (x : G), f x = 0 → x = 0 := sorry end is_group_hom protected instance subtype_val.is_add_group_hom {G : Type u_1} [add_group G] {s : set G} [is_add_subgroup s] : is_add_group_hom subtype.val := is_add_group_hom.mk protected instance coe.is_add_group_hom {G : Type u_1} [add_group G] {s : set G} [is_add_subgroup s] : is_add_group_hom coe := is_add_group_hom.mk protected instance subtype_mk.is_group_hom {G : Type u_1} {H : Type u_2} [group G] [group H] {s : set G} [is_subgroup s] (f : H → G) [is_group_hom f] (h : ∀ (x : H), f x ∈ s) : is_group_hom fun (x : H) => { val := f x, property := h x } := is_group_hom.mk protected instance set_inclusion.is_group_hom {G : Type u_1} [group G] {s : set G} {t : set G} [is_subgroup s] [is_subgroup t] (h : s ⊆ t) : is_group_hom (set.inclusion h) := subtype_mk.is_group_hom (fun (x : ↥s) => ↑x) fun (x : ↥s) => set.inclusion._proof_1 h x /-- `subtype.val : set.range f → H` as a monoid homomorphism, when `f` is a monoid homomorphism. -/ def monoid_hom.range_subtype_val {G : Type u_1} {H : Type u_2} [monoid G] [monoid H] (f : G →* H) : ↥(set.range ⇑f) →* H := monoid_hom.of subtype.val /-- `set.range_factorization f : G → set.range f` as a monoid homomorphism, when `f` is a monoid homomorphism. -/ def add_monoid_hom.range_factorization {G : Type u_1} {H : Type u_2} [add_monoid G] [add_monoid H] (f : G →+ H) : G →+ ↥(set.range ⇑f) := add_monoid_hom.mk (set.range_factorization ⇑f) sorry sorry namespace add_group inductive in_closure {A : Type u_3} [add_group A] (s : set A) : A → Prop where | basic : ∀ {a : A}, a ∈ s → in_closure s a | zero : in_closure s 0 | neg : ∀ {a : A}, in_closure s a → in_closure s (-a) | add : ∀ {a b : A}, in_closure s a → in_closure s b → in_closure s (a + b) end add_group namespace group inductive in_closure {G : Type u_1} [group G] (s : set G) : G → Prop where | basic : ∀ {a : G}, a ∈ s → in_closure s a | one : in_closure s 1 | inv : ∀ {a : G}, in_closure s a → in_closure s (a⁻¹) | mul : ∀ {a b : G}, in_closure s a → in_closure s b → in_closure s (a * b) /-- `group.closure s` is the subgroup closed over `s`, i.e. the smallest subgroup containg s. -/ def Mathlib.add_group.closure {G : Type u_1} [add_group G] (s : set G) : set G := set_of fun (a : G) => add_group.in_closure s a theorem Mathlib.add_group.mem_closure {G : Type u_1} [add_group G] {s : set G} {a : G} : a ∈ s → a ∈ add_group.closure s := add_group.in_closure.basic protected instance closure.is_subgroup {G : Type u_1} [group G] (s : set G) : is_subgroup (closure s) := is_subgroup.mk fun (a : G) => in_closure.inv theorem Mathlib.add_group.subset_closure {G : Type u_1} [add_group G] {s : set G} : s ⊆ add_group.closure s := fun (a : G) => add_group.mem_closure theorem Mathlib.add_group.closure_subset {G : Type u_1} [add_group G] {s : set G} {t : set G} [is_add_subgroup t] (h : s ⊆ t) : add_group.closure s ⊆ t := sorry theorem Mathlib.add_group.closure_subset_iff {G : Type u_1} [add_group G] (s : set G) (t : set G) [is_add_subgroup t] : add_group.closure s ⊆ t ↔ s ⊆ t := { mp := fun (h : add_group.closure s ⊆ t) (b : G) (ha : b ∈ s) => h (add_group.mem_closure ha), mpr := fun (h : s ⊆ t) (b : G) (ha : b ∈ add_group.closure s) => add_group.closure_subset h ha } theorem Mathlib.add_group.closure_mono {G : Type u_1} [add_group G] {s : set G} {t : set G} (h : s ⊆ t) : add_group.closure s ⊆ add_group.closure t := add_group.closure_subset (set.subset.trans h add_group.subset_closure) @[simp] theorem closure_subgroup {G : Type u_1} [group G] (s : set G) [is_subgroup s] : closure s = s := set.subset.antisymm (closure_subset (set.subset.refl s)) subset_closure theorem Mathlib.add_group.exists_list_of_mem_closure {G : Type u_1} [add_group G] {s : set G} {a : G} (h : a ∈ add_group.closure s) : ∃ (l : List G), (∀ (x : G), x ∈ l → x ∈ s ∨ -x ∈ s) ∧ list.sum l = a := sorry theorem Mathlib.add_group.image_closure {G : Type u_1} {H : Type u_2} [add_group G] [add_group H] (f : G → H) [is_add_group_hom f] (s : set G) : f '' add_group.closure s = add_group.closure (f '' s) := sorry theorem Mathlib.add_group.mclosure_subset {G : Type u_1} [add_group G] {s : set G} : add_monoid.closure s ⊆ add_group.closure s := add_monoid.closure_subset add_group.subset_closure theorem Mathlib.add_group.mclosure_neg_subset {G : Type u_1} [add_group G] {s : set G} : add_monoid.closure (Neg.neg ⁻¹' s) ⊆ add_group.closure s := add_monoid.closure_subset fun (x : G) (hx : x ∈ Neg.neg ⁻¹' s) => neg_neg x ▸ is_add_subgroup.neg_mem (add_group.subset_closure hx) theorem Mathlib.add_group.closure_eq_mclosure {G : Type u_1} [add_group G] {s : set G} : add_group.closure s = add_monoid.closure (s ∪ Neg.neg ⁻¹' s) := sorry theorem Mathlib.add_group.mem_closure_union_iff {G : Type u_1} [add_comm_group G] {s : set G} {t : set G} {x : G} : x ∈ add_group.closure (s ∪ t) ↔ ∃ (y : G), ∃ (H : y ∈ add_group.closure s), ∃ (z : G), ∃ (H : z ∈ add_group.closure t), y + z = x := sorry theorem gpowers_eq_closure {G : Type u_1} [group G] {a : G} : gpowers a = closure (singleton a) := sorry end group namespace is_subgroup theorem Mathlib.is_add_subgroup.trivial_eq_closure {G : Type u_1} [add_group G] : is_add_subgroup.trivial G = add_group.closure ∅ := sorry end is_subgroup /-The normal closure of a set s is the subgroup closure of all the conjugates of elements of s. It is the smallest normal subgroup containing s. -/ namespace group theorem conjugates_subset {G : Type u_1} [group G] {t : set G} [normal_subgroup t] {a : G} (h : a ∈ t) : conjugates a ⊆ t := sorry theorem conjugates_of_set_subset' {G : Type u_1} [group G] {s : set G} {t : set G} [normal_subgroup t] (h : s ⊆ t) : conjugates_of_set s ⊆ t := set.bUnion_subset fun (x : G) (H : x ∈ s) => conjugates_subset (h H) /-- The normal closure of a set s is the subgroup closure of all the conjugates of elements of s. It is the smallest normal subgroup containing s. -/ def normal_closure {G : Type u_1} [group G] (s : set G) : set G := closure (conjugates_of_set s) theorem conjugates_of_set_subset_normal_closure {G : Type u_1} {s : set G} [group G] : conjugates_of_set s ⊆ normal_closure s := subset_closure theorem subset_normal_closure {G : Type u_1} {s : set G} [group G] : s ⊆ normal_closure s := set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure /-- The normal closure of a set is a subgroup. -/ protected instance normal_closure.is_subgroup {G : Type u_1} [group G] (s : set G) : is_subgroup (normal_closure s) := closure.is_subgroup (conjugates_of_set s) /-- The normal closure of s is a normal subgroup. -/ protected instance normal_closure.is_normal {G : Type u_1} {s : set G} [group G] : normal_subgroup (normal_closure s) := sorry /-- The normal closure of s is the smallest normal subgroup containing s. -/ theorem normal_closure_subset {G : Type u_1} [group G] {s : set G} {t : set G} [normal_subgroup t] (h : s ⊆ t) : normal_closure s ⊆ t := sorry theorem normal_closure_subset_iff {G : Type u_1} [group G] {s : set G} {t : set G} [normal_subgroup t] : s ⊆ t ↔ normal_closure s ⊆ t := { mp := normal_closure_subset, mpr := set.subset.trans subset_normal_closure } theorem normal_closure_mono {G : Type u_1} [group G] {s : set G} {t : set G} : s ⊆ t → normal_closure s ⊆ normal_closure t := fun (h : s ⊆ t) => normal_closure_subset (set.subset.trans h subset_normal_closure) end group class simple_group (G : Type u_4) [group G] where simple : ∀ (N : set G) [_inst_1_1 : normal_subgroup N], N = is_subgroup.trivial G ∨ N = set.univ class simple_add_group (A : Type u_4) [add_group A] where simple : ∀ (N : set A) [_inst_1_1 : normal_add_subgroup N], N = is_add_subgroup.trivial A ∨ N = set.univ theorem additive.simple_add_group_iff {G : Type u_1} [group G] : simple_add_group (additive G) ↔ simple_group G := sorry protected instance additive.simple_add_group {G : Type u_1} [group G] [simple_group G] : simple_add_group (additive G) := iff.mpr additive.simple_add_group_iff _inst_2 theorem multiplicative.simple_group_iff {A : Type u_3} [add_group A] : simple_group (multiplicative A) ↔ simple_add_group A := sorry protected instance multiplicative.simple_group {A : Type u_3} [add_group A] [simple_add_group A] : simple_group (multiplicative A) := iff.mpr multiplicative.simple_group_iff _inst_2 theorem simple_add_group_of_surjective {G : Type u_1} {H : Type u_2} [add_group G] [add_group H] [simple_add_group G] (f : G → H) [is_add_group_hom f] (hf : function.surjective f) : simple_add_group H := sorry /-- Create a bundled subgroup from a set `s` and `[is_subroup s]`. -/ def subgroup.of {G : Type u_1} [group G] (s : set G) [h : is_subgroup s] : subgroup G := subgroup.mk s sorry sorry is_subgroup.inv_mem protected instance subgroup.is_subgroup {G : Type u_1} [group G] (K : subgroup G) : is_subgroup ↑K := is_subgroup.mk (subgroup.inv_mem' K) protected instance subgroup.of_normal {G : Type u_1} [group G] (s : set G) [h : is_subgroup s] [n : normal_subgroup s] : subgroup.normal (subgroup.of s) := subgroup.normal.mk normal_subgroup.normal
45daafd319042e1aeef59b2b1dc52f718a05c56f
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/stage0/src/Lean/Modifiers.lean
66d926c28f92fd02c26b22c2a86d428b7c8cef14
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
2,491
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.Environment namespace Lean builtin_initialize protectedExt : TagDeclarationExtension ← mkTagDeclarationExtension `protected @[export lean_add_protected] def addProtected (env : Environment) (n : Name) : Environment := protectedExt.tag env n @[export lean_is_protected] def isProtected (env : Environment) (n : Name) : Bool := protectedExt.isTagged env n builtin_initialize privateExt : EnvExtension Nat ← registerEnvExtension (pure 1) /- Private name support. Suppose the user marks as declaration `n` as private. Then, we create the name: `_private.<module_name>.<index> ++ n`. We say `_private.<module_name>.<index>` is the "private prefix" where `<index>` comes from the environment extension `privateExt`. We assume that `n` is a valid user name and does not contain `Name.num` constructors. Thus, we can easily convert from private internal name to user given name. -/ def privateHeader : Name := `_private @[export lean_mk_private_prefix] def mkUniquePrivatePrefix (env : Environment) : Environment × Name := let idx := privateExt.getState env let p := Name.mkNum (privateHeader ++ env.mainModule) idx let env := privateExt.setState env (idx+1) (env, p) @[export lean_mk_private_name] def mkUniquePrivateName (env : Environment) (n : Name) : Environment × Name := let (env, p) := mkUniquePrivatePrefix env (env, p ++ n) def mkPrivateName (env : Environment) (n : Name) : Name := Name.mkNum (privateHeader ++ env.mainModule) 0 ++ n def isPrivateName : Name → Bool | n@(Name.str p _ _) => n == privateHeader || isPrivateName p | Name.num p _ _ => isPrivateName p | _ => false @[export lean_is_private_name] def isPrivateNameExport (n : Name) : Bool := isPrivateName n private def privateToUserNameAux : Name → Name | Name.str p s _ => Name.mkStr (privateToUserNameAux p) s | _ => Name.anonymous @[export lean_private_to_user_name] def privateToUserName? (n : Name) : Option Name := if isPrivateName n then privateToUserNameAux n else none private def privatePrefixAux : Name → Name | Name.str p _ _ => privatePrefixAux p | n => n @[export lean_private_prefix] def privatePrefix (n : Name) : Option Name := if isPrivateName n then privatePrefixAux n else none end Lean
7a1de0545a5638bdefc2287cd3d7d2ad69c9b8bd
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/default.lean
394413d005f4c70b3df0ee6fc87232767eb02401
[]
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
1,326
lean
/- This file imports many useful tactics ("the kitchen sink"). You can use `import tactic` at the beginning of your file to get everything. (Although you may want to strip things down when you're polishing.) Because this file imports some complicated tactics, it has many transitive dependencies (which of course may not use `import tactic`, and must import selectively). As (non-exhaustive) examples, these includes things like: * algebra.group_power * algebra.ordered_ring * data.rat * data.nat.prime * data.list.perm * data.set.lattice * data.equiv.encodable.basic * order.complete_lattice -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.basic import Mathlib.tactic.abel import Mathlib.tactic.ring_exp import Mathlib.tactic.noncomm_ring import Mathlib.tactic.linarith.default import Mathlib.tactic.omega.default import Mathlib.tactic.tfae import Mathlib.tactic.apply_fun import Mathlib.tactic.interval_cases import Mathlib.tactic.reassoc_axiom import Mathlib.tactic.slice import Mathlib.tactic.subtype_instance import Mathlib.tactic.derive_fintype import Mathlib.tactic.group import Mathlib.tactic.cancel_denoms import Mathlib.tactic.zify import Mathlib.tactic.transport import Mathlib.tactic.unfold_cases import Mathlib.tactic.field_simp import Mathlib.PostPort namespace Mathlib
a73383bf1a8493497807b97c80fc1e6e7d305c35
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/topology/local_extr.lean
cbab9367d8934648ed9e43a6854300af4404c653
[ "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
14,833
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import order.filter.extr topology.continuous_on /-! # Local extrema of functions on topological spaces ## Main definitions This file defines special versions of `is_*_filter f a l`, `*=min/max/extr`, from `order/filter/extr` for two kinds of filters: `nhds_within` and `nhds`. These versions are called `is_local_*_on` and `is_local_*`, respectively. ## Main statements Many lemmas in this file restate those from `order/filter/extr`, and you can find a detailed documentation there. These convenience lemmas are provided only to make the dot notation return propositions of expected types, not just `is_*_filter`. Here is the list of statements specific to these two types of filters: * `is_local_*.on`, `is_local_*_on.on_subset`: restrict to a subset; * `is_local_*_on.inter` : intersect the set with another one; * `is_*_on.localize` : a global extremum is a local extremum too. * `is_[local_]*_on.is_local_*` : if we have `is_local_*_on f s a` and `s ∈ 𝓝 a`, then we have `is_local_* f a`. -/ universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} [topological_space α] open set filter open_locale topological_space section preorder variables [preorder β] [preorder γ] (f : α → β) (s : set α) (a : α) /-- `is_local_min_on f s a` means that `f a ≤ f x` for all `x ∈ s` in some neighborhood of `a`. -/ def is_local_min_on := is_min_filter f (nhds_within a s) a /-- `is_local_max_on f s a` means that `f x ≤ f a` for all `x ∈ s` in some neighborhood of `a`. -/ def is_local_max_on := is_max_filter f (nhds_within a s) a /-- `is_local_extr_on f s a` means `is_local_min_on f s a ∨ is_local_max_on f s a`. -/ def is_local_extr_on := is_extr_filter f (nhds_within a s) a /-- `is_local_min f a` means that `f a ≤ f x` for all `x` in some neighborhood of `a`. -/ def is_local_min := is_min_filter f (𝓝 a) a /-- `is_local_max f a` means that `f x ≤ f a` for all `x ∈ s` in some neighborhood of `a`. -/ def is_local_max := is_max_filter f (𝓝 a) a /-- `is_local_extr_on f s a` means `is_local_min_on f s a ∨ is_local_max_on f s a`. -/ def is_local_extr := is_extr_filter f (𝓝 a) a variables {f s a} lemma is_local_extr_on.elim {p : Prop} : is_local_extr_on f s a → (is_local_min_on f s a → p) → (is_local_max_on f s a → p) → p := or.elim lemma is_local_extr.elim {p : Prop} : is_local_extr f a → (is_local_min f a → p) → (is_local_max f a → p) → p := or.elim /-! ### Restriction to (sub)sets -/ lemma is_local_min.on (h : is_local_min f a) (s) : is_local_min_on f s a := h.filter_inf _ lemma is_local_max.on (h : is_local_max f a) (s) : is_local_max_on f s a := h.filter_inf _ lemma is_local_extr.on (h : is_local_extr f a) (s) : is_local_extr_on f s a := h.filter_inf _ lemma is_local_min_on.on_subset {t : set α} (hf : is_local_min_on f t a) (h : s ⊆ t) : is_local_min_on f s a := hf.filter_mono $ nhds_within_mono a h lemma is_local_max_on.on_subset {t : set α} (hf : is_local_max_on f t a) (h : s ⊆ t) : is_local_max_on f s a := hf.filter_mono $ nhds_within_mono a h lemma is_local_extr_on.on_subset {t : set α} (hf : is_local_extr_on f t a) (h : s ⊆ t) : is_local_extr_on f s a := hf.filter_mono $ nhds_within_mono a h lemma is_local_min_on.inter (hf : is_local_min_on f s a) (t) : is_local_min_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) lemma is_local_max_on.inter (hf : is_local_max_on f s a) (t) : is_local_max_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) lemma is_local_extr_on.inter (hf : is_local_extr_on f s a) (t) : is_local_extr_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) lemma is_min_on.localize (hf : is_min_on f s a) : is_local_min_on f s a := hf.filter_mono $ inf_le_right lemma is_max_on.localize (hf : is_max_on f s a) : is_local_max_on f s a := hf.filter_mono $ inf_le_right lemma is_extr_on.localize (hf : is_extr_on f s a) : is_local_extr_on f s a := hf.filter_mono $ inf_le_right lemma is_local_min_on.is_local_min (hf : is_local_min_on f s a) (hs : s ∈ 𝓝 a) : is_local_min f a := have 𝓝 a ≤ principal s, from le_principal_iff.2 hs, hf.filter_mono $ le_inf (le_refl _) this lemma is_local_max_on.is_local_max (hf : is_local_max_on f s a) (hs : s ∈ 𝓝 a) : is_local_max f a := have 𝓝 a ≤ principal s, from le_principal_iff.2 hs, hf.filter_mono $ le_inf (le_refl _) this lemma is_local_extr_on.is_local_extr (hf : is_local_extr_on f s a) (hs : s ∈ 𝓝 a) : is_local_extr f a := hf.elim (λ hf, (hf.is_local_min hs).is_extr) (λ hf, (hf.is_local_max hs).is_extr) lemma is_min_on.is_local_min (hf : is_min_on f s a) (hs : s ∈ 𝓝 a) : is_local_min f a := hf.localize.is_local_min hs lemma is_max_on.is_local_max (hf : is_max_on f s a) (hs : s ∈ 𝓝 a) : is_local_max f a := hf.localize.is_local_max hs lemma is_extr_on.is_local_extr (hf : is_extr_on f s a) (hs : s ∈ 𝓝 a) : is_local_extr f a := hf.localize.is_local_extr hs /-! ### Constant -/ lemma is_local_min_on_const {b : β} : is_local_min_on (λ _, b) s a := is_min_filter_const lemma is_local_max_on_const {b : β} : is_local_max_on (λ _, b) s a := is_max_filter_const lemma is_local_extr_on_const {b : β} : is_local_extr_on (λ _, b) s a := is_extr_filter_const lemma is_local_min_const {b : β} : is_local_min (λ _, b) a := is_min_filter_const lemma is_local_max_const {b : β} : is_local_max (λ _, b) a := is_max_filter_const lemma is_local_extr_const {b : β} : is_local_extr (λ _, b) a := is_extr_filter_const /-! ### Composition with (anti)monotone functions -/ lemma is_local_min.comp_mono (hf : is_local_min f a) {g : β → γ} (hg : monotone g) : is_local_min (g ∘ f) a := hf.comp_mono hg lemma is_local_max.comp_mono (hf : is_local_max f a) {g : β → γ} (hg : monotone g) : is_local_max (g ∘ f) a := hf.comp_mono hg lemma is_local_extr.comp_mono (hf : is_local_extr f a) {g : β → γ} (hg : monotone g) : is_local_extr (g ∘ f) a := hf.comp_mono hg lemma is_local_min.comp_antimono (hf : is_local_min f a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_max (g ∘ f) a := hf.comp_antimono hg lemma is_local_max.comp_antimono (hf : is_local_max f a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_min (g ∘ f) a := hf.comp_antimono hg lemma is_local_extr.comp_antimono (hf : is_local_extr f a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_extr (g ∘ f) a := hf.comp_antimono hg lemma is_local_min_on.comp_mono (hf : is_local_min_on f s a) {g : β → γ} (hg : monotone g) : is_local_min_on (g ∘ f) s a := hf.comp_mono hg lemma is_local_max_on.comp_mono (hf : is_local_max_on f s a) {g : β → γ} (hg : monotone g) : is_local_max_on (g ∘ f) s a := hf.comp_mono hg lemma is_local_extr_on.comp_mono (hf : is_local_extr_on f s a) {g : β → γ} (hg : monotone g) : is_local_extr_on (g ∘ f) s a := hf.comp_mono hg lemma is_local_min_on.comp_antimono (hf : is_local_min_on f s a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_max_on (g ∘ f) s a := hf.comp_antimono hg lemma is_local_max_on.comp_antimono (hf : is_local_max_on f s a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_min_on (g ∘ f) s a := hf.comp_antimono hg lemma is_local_extr_on.comp_antimono (hf : is_local_extr_on f s a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_extr_on (g ∘ f) s a := hf.comp_antimono hg lemma is_local_min.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_min f a) {g : α → γ} (hg : is_local_min g a) : is_local_min (λ x, op (f x) (g x)) a := hf.bicomp_mono hop hg lemma is_local_max.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_max f a) {g : α → γ} (hg : is_local_max g a) : is_local_max (λ x, op (f x) (g x)) a := hf.bicomp_mono hop hg -- No `extr` version because we need `hf` and `hg` to be of the same kind lemma is_local_min_on.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_min_on f s a) {g : α → γ} (hg : is_local_min_on g s a) : is_local_min_on (λ x, op (f x) (g x)) s a := hf.bicomp_mono hop hg lemma is_local_max_on.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_max_on f s a) {g : α → γ} (hg : is_local_max_on g s a) : is_local_max_on (λ x, op (f x) (g x)) s a := hf.bicomp_mono hop hg /-! ### Composition with `continuous_at` -/ lemma is_local_min.comp_continuous [topological_space δ] {g : δ → α} {b : δ} (hf : is_local_min f (g b)) (hg : continuous_at g b) : is_local_min (f ∘ g) b := hg hf lemma is_local_max.comp_continuous [topological_space δ] {g : δ → α} {b : δ} (hf : is_local_max f (g b)) (hg : continuous_at g b) : is_local_max (f ∘ g) b := hg hf lemma is_local_extr.comp_continuous [topological_space δ] {g : δ → α} {b : δ} (hf : is_local_extr f (g b)) (hg : continuous_at g b) : is_local_extr (f ∘ g) b := hf.comp_tendsto hg lemma is_local_min.comp_continuous_on [topological_space δ] {s : set δ} {g : δ → α} {b : δ} (hf : is_local_min f (g b)) (hg : continuous_on g s) (hb : b ∈ s) : is_local_min_on (f ∘ g) s b := hf.comp_tendsto (hg b hb) lemma is_local_max.comp_continuous_on [topological_space δ] {s : set δ} {g : δ → α} {b : δ} (hf : is_local_max f (g b)) (hg : continuous_on g s) (hb : b ∈ s) : is_local_max_on (f ∘ g) s b := hf.comp_tendsto (hg b hb) lemma is_local_extr.comp_continuous_on [topological_space δ] {s : set δ} (g : δ → α) {b : δ} (hf : is_local_extr f (g b)) (hg : continuous_on g s) (hb : b ∈ s) : is_local_extr_on (f ∘ g) s b := hf.elim (λ hf, (hf.comp_continuous_on hg hb).is_extr) (λ hf, (is_local_max.comp_continuous_on hf hg hb).is_extr) end preorder /-! ### Pointwise addition -/ section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.add (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, f x + g x) a := hf.add hg lemma is_local_max.add (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, f x + g x) a := hf.add hg lemma is_local_min_on.add (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, f x + g x) s a := hf.add hg lemma is_local_max_on.add (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, f x + g x) s a := hf.add hg end ordered_add_comm_monoid /-! ### Pointwise negation and subtraction -/ section ordered_add_comm_group variables [ordered_add_comm_group β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.neg (hf : is_local_min f a) : is_local_max (λ x, -f x) a := hf.neg lemma is_local_max.neg (hf : is_local_max f a) : is_local_min (λ x, -f x) a := hf.neg lemma is_local_extr.neg (hf : is_local_extr f a) : is_local_extr (λ x, -f x) a := hf.neg lemma is_local_min_on.neg (hf : is_local_min_on f s a) : is_local_max_on (λ x, -f x) s a := hf.neg lemma is_local_max_on.neg (hf : is_local_max_on f s a) : is_local_min_on (λ x, -f x) s a := hf.neg lemma is_local_extr_on.neg (hf : is_local_extr_on f s a) : is_local_extr_on (λ x, -f x) s a := hf.neg lemma is_local_min.sub (hf : is_local_min f a) (hg : is_local_max g a) : is_local_min (λ x, f x - g x) a := hf.sub hg lemma is_local_max.sub (hf : is_local_max f a) (hg : is_local_min g a) : is_local_max (λ x, f x - g x) a := hf.sub hg lemma is_local_min_on.sub (hf : is_local_min_on f s a) (hg : is_local_max_on g s a) : is_local_min_on (λ x, f x - g x) s a := hf.sub hg lemma is_local_max_on.sub (hf : is_local_max_on f s a) (hg : is_local_min_on g s a) : is_local_max_on (λ x, f x - g x) s a := hf.sub hg end ordered_add_comm_group /-! ### Pointwise `sup`/`inf` -/ section semilattice_sup variables [semilattice_sup β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.sup (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, f x ⊔ g x) a := hf.sup hg lemma is_local_max.sup (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, f x ⊔ g x) a := hf.sup hg lemma is_local_min_on.sup (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, f x ⊔ g x) s a := hf.sup hg lemma is_local_max_on.sup (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, f x ⊔ g x) s a := hf.sup hg end semilattice_sup section semilattice_inf variables [semilattice_inf β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.inf (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, f x ⊓ g x) a := hf.inf hg lemma is_local_max.inf (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, f x ⊓ g x) a := hf.inf hg lemma is_local_min_on.inf (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, f x ⊓ g x) s a := hf.inf hg lemma is_local_max_on.inf (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, f x ⊓ g x) s a := hf.inf hg end semilattice_inf /-! ### Pointwise `min`/`max` -/ section decidable_linear_order variables [decidable_linear_order β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.min (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, min (f x) (g x)) a := hf.min hg lemma is_local_max.min (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, min (f x) (g x)) a := hf.min hg lemma is_local_min_on.min (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, min (f x) (g x)) s a := hf.min hg lemma is_local_max_on.min (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, min (f x) (g x)) s a := hf.min hg lemma is_local_min.max (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, max (f x) (g x)) a := hf.max hg lemma is_local_max.max (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, max (f x) (g x)) a := hf.max hg lemma is_local_min_on.max (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, max (f x) (g x)) s a := hf.max hg lemma is_local_max_on.max (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, max (f x) (g x)) s a := hf.max hg end decidable_linear_order
c1d61b99b299058b579c23c63064275a79949257
64874bd1010548c7f5a6e3e8902efa63baaff785
/library/data/prod.lean
c1e5e5a7cb15fc16f8d9c73190043d137fc1021c
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,900
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: data.prod Author: Leonardo de Moura, Jeremy Avigad -/ import logic.eq open inhabited decidable eq.ops namespace prod variables {A B : Type} {a₁ a₂ : A} {b₁ b₂ : B} {u : A × B} theorem pair_eq : a₁ = a₂ → b₁ = b₂ → (a₁, b₁) = (a₂, b₂) := assume H1 H2, H1 ▸ H2 ▸ rfl protected theorem equal {p₁ p₂ : prod A B} : pr₁ p₁ = pr₁ p₂ → pr₂ p₁ = pr₂ p₂ → p₁ = p₂ := destruct p₁ (take a₁ b₁, destruct p₂ (take a₂ b₂ H₁ H₂, pair_eq H₁ H₂)) protected definition is_inhabited [instance] : inhabited A → inhabited B → inhabited (prod A B) := take (H₁ : inhabited A) (H₂ : inhabited B), inhabited.destruct H₁ (λa, inhabited.destruct H₂ (λb, inhabited.mk (pair a b))) protected definition has_decidable_eq [instance] : decidable_eq A → decidable_eq B → decidable_eq (A × B) := take (H₁ : decidable_eq A) (H₂ : decidable_eq B) (u v : A × B), have H₃ : u = v ↔ (pr₁ u = pr₁ v) ∧ (pr₂ u = pr₂ v), from iff.intro (assume H, H ▸ and.intro rfl rfl) (assume H, and.elim H (assume H₄ H₅, equal H₄ H₅)), decidable_of_decidable_of_iff _ (iff.symm H₃) -- ### flip operation definition flip (a : A × B) : B × A := pair (pr2 a) (pr1 a) theorem flip_def (a : A × B) : flip a = pair (pr2 a) (pr1 a) := rfl theorem flip_pair (a : A) (b : B) : flip (pair a b) = pair b a := rfl theorem flip_pr1 (a : A × B) : pr1 (flip a) = pr2 a := rfl theorem flip_pr2 (a : A × B) : pr2 (flip a) = pr1 a := rfl theorem flip_flip (a : A × B) : flip (flip a) = a := destruct a (take x y, rfl) theorem P_flip {P : A → B → Prop} (a : A × B) (H : P (pr1 a) (pr2 a)) : P (pr2 (flip a)) (pr1 (flip a)) := (flip_pr1 a)⁻¹ ▸ (flip_pr2 a)⁻¹ ▸ H theorem flip_inj {a b : A × B} (H : flip a = flip b) : a = b := have H2 : flip (flip a) = flip (flip b), from congr_arg flip H, show a = b, from (flip_flip a) ▸ (flip_flip b) ▸ H2 -- ### coordinatewise unary maps definition map_pair (f : A → B) (a : A × A) : B × B := pair (f (pr1 a)) (f (pr2 a)) theorem map_pair_def (f : A → B) (a : A × A) : map_pair f a = pair (f (pr1 a)) (f (pr2 a)) := rfl theorem map_pair_pair (f : A → B) (a a' : A) : map_pair f (pair a a') = pair (f a) (f a') := (pr1.mk a a') ▸ (pr2.mk a a') ▸ rfl theorem map_pair_pr1 (f : A → B) (a : A × A) : pr1 (map_pair f a) = f (pr1 a) := !pr1.mk theorem map_pair_pr2 (f : A → B) (a : A × A) : pr2 (map_pair f a) = f (pr2 a) := !pr2.mk -- ### coordinatewise binary maps definition map_pair2 {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) : C × C := pair (f (pr1 a) (pr1 b)) (f (pr2 a) (pr2 b)) theorem map_pair2_def {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) : map_pair2 f a b = pair (f (pr1 a) (pr1 b)) (f (pr2 a) (pr2 b)) := rfl theorem map_pair2_pair {A B C : Type} (f : A → B → C) (a a' : A) (b b' : B) : map_pair2 f (pair a a') (pair b b') = pair (f a b) (f a' b') := calc map_pair2 f (pair a a') (pair b b') = pair (f (pr1 (pair a a')) b) (f (pr2 (pair a a')) (pr2 (pair b b'))) : {pr1.mk b b'} ... = pair (f (pr1 (pair a a')) b) (f (pr2 (pair a a')) b') : {pr2.mk b b'} ... = pair (f (pr1 (pair a a')) b) (f a' b') : {pr2.mk a a'} ... = pair (f a b) (f a' b') : {pr1.mk a a'} theorem map_pair2_pr1 {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) : pr1 (map_pair2 f a b) = f (pr1 a) (pr1 b) := !pr1.mk theorem map_pair2_pr2 {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) : pr2 (map_pair2 f a b) = f (pr2 a) (pr2 b) := !pr2.mk theorem map_pair2_flip {A B C : Type} (f : A → B → C) (a : A × A) (b : B × B) : flip (map_pair2 f a b) = map_pair2 f (flip a) (flip b) := have Hx : pr1 (flip (map_pair2 f a b)) = pr1 (map_pair2 f (flip a) (flip b)), from calc pr1 (flip (map_pair2 f a b)) = pr2 (map_pair2 f a b) : flip_pr1 _ ... = f (pr2 a) (pr2 b) : map_pair2_pr2 f a b ... = f (pr1 (flip a)) (pr2 b) : {(flip_pr1 a)⁻¹} ... = f (pr1 (flip a)) (pr1 (flip b)) : {(flip_pr1 b)⁻¹} ... = pr1 (map_pair2 f (flip a) (flip b)) : (map_pair2_pr1 f _ _)⁻¹, have Hy : pr2 (flip (map_pair2 f a b)) = pr2 (map_pair2 f (flip a) (flip b)), from calc pr2 (flip (map_pair2 f a b)) = pr1 (map_pair2 f a b) : flip_pr2 _ ... = f (pr1 a) (pr1 b) : map_pair2_pr1 f a b ... = f (pr2 (flip a)) (pr1 b) : {flip_pr2 a} ... = f (pr2 (flip a)) (pr2 (flip b)) : {flip_pr2 b} ... = pr2 (map_pair2 f (flip a) (flip b)) : (map_pair2_pr2 f _ _)⁻¹, pair_eq Hx Hy end prod
c24e063035891a66d64edfe47f3699ce0dd556b3
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/stage0/src/Lean/Compiler/IR.lean
e9412fed3b81f5177ea45ef94e41f760bb0d2123
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,693
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.Compiler.IR.Basic import Lean.Compiler.IR.Format import Lean.Compiler.IR.CompilerM import Lean.Compiler.IR.PushProj import Lean.Compiler.IR.ElimDeadVars import Lean.Compiler.IR.SimpCase import Lean.Compiler.IR.ResetReuse import Lean.Compiler.IR.NormIds import Lean.Compiler.IR.Checker import Lean.Compiler.IR.Borrow import Lean.Compiler.IR.Boxing import Lean.Compiler.IR.RC import Lean.Compiler.IR.ExpandResetReuse import Lean.Compiler.IR.UnboxResult import Lean.Compiler.IR.ElimDeadBranches import Lean.Compiler.IR.EmitC import Lean.Compiler.IR.CtorLayout namespace Lean.IR private def compileAux (decls : Array Decl) : CompilerM Unit := do logDecls `init decls checkDecls decls let decls ← elimDeadBranches decls logDecls `elim_dead_branches decls let decls := decls.map Decl.pushProj logDecls `push_proj decls let decls := decls.map Decl.insertResetReuse logDecls `reset_reuse decls let decls := decls.map Decl.elimDead logDecls `elim_dead decls let decls := decls.map Decl.simpCase logDecls `simp_case decls let decls := decls.map Decl.normalizeIds let decls ← inferBorrow decls logDecls `borrow decls let decls ← explicitBoxing decls logDecls `boxing decls let decls ← explicitRC decls logDecls `rc decls let decls := decls.map Decl.expandResetReuse logDecls `expand_reset_reuse decls let decls := decls.map Decl.pushProj logDecls `push_proj decls logDecls `result decls checkDecls decls addDecls decls @[export lean_ir_compile] def compile (env : Environment) (opts : Options) (decls : Array Decl) : Log × (Except String Environment) := match (compileAux decls opts).run { env := env } with | EStateM.Result.ok _ s => (s.log, Except.ok s.env) | EStateM.Result.error msg s => (s.log, Except.error msg) def addBoxedVersionAux (decl : Decl) : CompilerM Unit := do let env ← getEnv if !ExplicitBoxing.requiresBoxedVersion env decl then pure () else let decl := ExplicitBoxing.mkBoxedVersion decl let decls : Array Decl := #[decl] let decls ← explicitRC decls decls.forM fun decl => modifyEnv $ fun env => addDeclAux env decl pure () -- Remark: we are ignoring the `Log` here. This should be fine. @[export lean_ir_add_boxed_version] def addBoxedVersion (env : Environment) (decl : Decl) : Except String Environment := match (addBoxedVersionAux decl Options.empty).run { env := env } with | EStateM.Result.ok _ s => Except.ok s.env | EStateM.Result.error msg s => Except.error msg end Lean.IR
9ae3b72a1fb3d86cbf272f66e391f8d54fdedbee
4727251e0cd73359b15b664c3170e5d754078599
/src/order/category/Preorder.lean
b28dd988e00b257e72cde0ab7d6285252c0edb9d
[ "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,577
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 category_theory.concrete_category.bundled_hom import algebra.punit_instances import order.hom.basic import category_theory.category.Cat import category_theory.category.preorder /-! # Category of preorders This defines `Preorder`, the category of preorders with monotone maps. -/ universe u open category_theory /-- The category of preorders. -/ def Preorder := bundled preorder namespace Preorder instance : bundled_hom @order_hom := { to_fun := @order_hom.to_fun, id := @order_hom.id, comp := @order_hom.comp, hom_ext := @order_hom.ext } attribute [derive [large_category, concrete_category]] Preorder instance : has_coe_to_sort Preorder Type* := bundled.has_coe_to_sort /-- Construct a bundled Preorder from the underlying type and typeclass. -/ def of (α : Type*) [preorder α] : Preorder := bundled.of α @[simp] lemma coe_of (α : Type*) [preorder α] : ↥(of α) = α := rfl instance : inhabited Preorder := ⟨of punit⟩ instance (α : Preorder) : preorder α := α.str /-- Constructs an equivalence between preorders from an order isomorphism between them. -/ @[simps] def iso.mk {α β : Preorder.{u}} (e : α ≃o β) : α ≅ β := { hom := e, inv := e.symm, hom_inv_id' := by { ext, exact e.symm_apply_apply x }, inv_hom_id' := by { ext, exact e.apply_symm_apply x } } /-- `order_dual` as a functor. -/ @[simps] def dual : Preorder ⥤ Preorder := { obj := λ X, of Xᵒᵈ, map := λ X Y, order_hom.dual } /-- The equivalence between `Preorder` and itself induced by `order_dual` both ways. -/ @[simps functor inverse] def dual_equiv : Preorder ≌ Preorder := equivalence.mk dual dual (nat_iso.of_components (λ X, iso.mk $ order_iso.dual_dual X) $ λ X Y f, rfl) (nat_iso.of_components (λ X, iso.mk $ order_iso.dual_dual X) $ λ X Y f, rfl) end Preorder /-- The embedding of `Preorder` into `Cat`. -/ @[simps] def Preorder_to_Cat : Preorder.{u} ⥤ Cat := { obj := λ X, Cat.of X.1, map := λ X Y f, f.monotone.functor, map_id' := λ X, begin apply category_theory.functor.ext, tidy end, map_comp' := λ X Y Z f g, begin apply category_theory.functor.ext, tidy end } instance : faithful Preorder_to_Cat.{u} := { map_injective' := λ X Y f g h, begin ext x, exact functor.congr_obj h x end } instance : full Preorder_to_Cat.{u} := { preimage := λ X Y f, ⟨f.obj, f.monotone⟩, witness' := λ X Y f, begin apply category_theory.functor.ext, tidy end }
3ac46ebe7e9207e5ddb9a124b272118b77e840a5
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/support.lean
35b0b9e55261e6d7f583d5682b14d70f4fea7a9a
[ "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
12,035
lean
/- Copyright (c) 2022 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Patrick Massot -/ import topology.separation /-! # The topological support of a function In this file we define the topological support of a function `f`, `tsupport f`, as the closure of the support of `f`. Furthermore, we say that `f` has compact support if the topological support of `f` is compact. ## Main definitions * `function.mul_tsupport` & `function.tsupport` * `function.has_compact_mul_support` & `function.has_compact_support` ## Implementation Notes * We write all lemmas for multiplicative functions, and use `@[to_additive]` to get the more common additive versions. * We do not put the definitions in the `function` namespace, following many other topological definitions that are in the root namespace (compare `embedding` vs `function.embedding`). -/ open function set filter open_locale topological_space variables {X α α' β γ δ M E R : Type*} section one variables [has_one α] variables [topological_space X] /-- The topological support of a function is the closure of its support, i.e. the closure of the set of all elements where the function is not equal to 1. -/ @[to_additive /-" The topological support of a function is the closure of its support. i.e. the closure of the set of all elements where the function is nonzero. "-/] def mul_tsupport (f : X → α) : set X := closure (mul_support f) @[to_additive] lemma subset_mul_tsupport (f : X → α) : mul_support f ⊆ mul_tsupport f := subset_closure @[to_additive] lemma is_closed_mul_tsupport (f : X → α) : is_closed (mul_tsupport f) := is_closed_closure @[to_additive] lemma mul_tsupport_eq_empty_iff {f : X → α} : mul_tsupport f = ∅ ↔ f = 1 := by rw [mul_tsupport, closure_empty_iff, mul_support_eq_empty_iff] @[to_additive] lemma image_eq_zero_of_nmem_mul_tsupport {f : X → α} {x : X} (hx : x ∉ mul_tsupport f) : f x = 1 := mul_support_subset_iff'.mp (subset_mul_tsupport f) x hx @[to_additive] lemma range_subset_insert_image_mul_tsupport (f : X → α) : range f ⊆ insert 1 (f '' mul_tsupport f) := (range_subset_insert_image_mul_support f).trans $ insert_subset_insert $ image_subset _ subset_closure @[to_additive] lemma range_eq_image_mul_tsupport_or (f : X → α) : range f = f '' mul_tsupport f ∨ range f = insert 1 (f '' mul_tsupport f) := (wcovby_insert _ _).eq_or_eq (image_subset_range _ _) (range_subset_insert_image_mul_tsupport f) lemma tsupport_mul_subset_left {α : Type*} [mul_zero_class α] {f g : X → α} : tsupport (λ x, f x * g x) ⊆ tsupport f := closure_mono (support_mul_subset_left _ _) lemma tsupport_mul_subset_right {α : Type*} [mul_zero_class α] {f g : X → α} : tsupport (λ x, f x * g x) ⊆ tsupport g := closure_mono (support_mul_subset_right _ _) end one lemma tsupport_smul_subset_left {M α} [topological_space X] [has_zero M] [has_zero α] [smul_with_zero M α] (f : X → M) (g : X → α) : tsupport (λ x, f x • g x) ⊆ tsupport f := closure_mono $ support_smul_subset_left f g section variables [topological_space α] [topological_space α'] variables [has_one β] [has_one γ] [has_one δ] variables {g : β → γ} {f : α → β} {f₂ : α → γ} {m : β → γ → δ} {x : α} @[to_additive] lemma not_mem_mul_tsupport_iff_eventually_eq : x ∉ mul_tsupport f ↔ f =ᶠ[𝓝 x] 1 := by simp_rw [mul_tsupport, mem_closure_iff_nhds, not_forall, not_nonempty_iff_eq_empty, ← disjoint_iff_inter_eq_empty, disjoint_mul_support_iff, eventually_eq_iff_exists_mem] @[to_additive] lemma continuous_of_mul_tsupport [topological_space β] {f : α → β} (hf : ∀ x ∈ mul_tsupport f, continuous_at f x) : continuous f := continuous_iff_continuous_at.2 $ λ x, (em _).elim (hf x) $ λ hx, (@continuous_at_const _ _ _ _ _ 1).congr (not_mem_mul_tsupport_iff_eventually_eq.mp hx).symm /-- A function `f` *has compact multiplicative support* or is *compactly supported* if the closure of the multiplicative support of `f` is compact. In a T₂ space this is equivalent to `f` being equal to `1` outside a compact set. -/ @[to_additive /-" A function `f` *has compact support* or is *compactly supported* if the closure of the support of `f` is compact. In a T₂ space this is equivalent to `f` being equal to `0` outside a compact set. "-/] def has_compact_mul_support (f : α → β) : Prop := is_compact (mul_tsupport f) @[to_additive] lemma has_compact_mul_support_def : has_compact_mul_support f ↔ is_compact (closure (mul_support f)) := by refl @[to_additive] lemma exists_compact_iff_has_compact_mul_support [t2_space α] : (∃ K : set α, is_compact K ∧ ∀ x ∉ K, f x = 1) ↔ has_compact_mul_support f := by simp_rw [← nmem_mul_support, ← mem_compl_iff, ← subset_def, compl_subset_compl, has_compact_mul_support_def, exists_compact_superset_iff] @[to_additive] lemma has_compact_mul_support.intro [t2_space α] {K : set α} (hK : is_compact K) (hfK : ∀ x ∉ K, f x = 1) : has_compact_mul_support f := exists_compact_iff_has_compact_mul_support.mp ⟨K, hK, hfK⟩ @[to_additive] lemma has_compact_mul_support.is_compact (hf : has_compact_mul_support f) : is_compact (mul_tsupport f) := hf @[to_additive] lemma has_compact_mul_support_iff_eventually_eq : has_compact_mul_support f ↔ f =ᶠ[coclosed_compact α] 1 := ⟨ λ h, mem_coclosed_compact.mpr ⟨mul_tsupport f, is_closed_mul_tsupport _, h, λ x, not_imp_comm.mpr $ λ hx, subset_mul_tsupport f hx⟩, λ h, let ⟨C, hC⟩ := mem_coclosed_compact'.mp h in is_compact_of_is_closed_subset hC.2.1 (is_closed_mul_tsupport _) (closure_minimal hC.2.2 hC.1)⟩ @[to_additive] lemma has_compact_mul_support.is_compact_range [topological_space β] (h : has_compact_mul_support f) (hf : continuous f) : is_compact (range f) := begin cases range_eq_image_mul_tsupport_or f with h2 h2; rw [h2], exacts [h.image hf, (h.image hf).insert 1] end @[to_additive] lemma has_compact_mul_support.mono' {f' : α → γ} (hf : has_compact_mul_support f) (hff' : mul_support f' ⊆ mul_tsupport f) : has_compact_mul_support f' := is_compact_of_is_closed_subset hf is_closed_closure $ closure_minimal hff' is_closed_closure @[to_additive] lemma has_compact_mul_support.mono {f' : α → γ} (hf : has_compact_mul_support f) (hff' : mul_support f' ⊆ mul_support f) : has_compact_mul_support f' := hf.mono' $ hff'.trans subset_closure @[to_additive] lemma has_compact_mul_support.comp_left (hf : has_compact_mul_support f) (hg : g 1 = 1) : has_compact_mul_support (g ∘ f) := hf.mono $ mul_support_comp_subset hg f @[to_additive] lemma has_compact_mul_support_comp_left (hg : ∀ {x}, g x = 1 ↔ x = 1) : has_compact_mul_support (g ∘ f) ↔ has_compact_mul_support f := by simp_rw [has_compact_mul_support_def, mul_support_comp_eq g @hg f] @[to_additive] lemma has_compact_mul_support.comp_closed_embedding (hf : has_compact_mul_support f) {g : α' → α} (hg : closed_embedding g) : has_compact_mul_support (f ∘ g) := begin rw [has_compact_mul_support_def, function.mul_support_comp_eq_preimage], refine is_compact_of_is_closed_subset (hg.is_compact_preimage hf) is_closed_closure _, rw [hg.to_embedding.closure_eq_preimage_closure_image], exact preimage_mono (closure_mono $ image_preimage_subset _ _) end @[to_additive] lemma has_compact_mul_support.comp₂_left (hf : has_compact_mul_support f) (hf₂ : has_compact_mul_support f₂) (hm : m 1 1 = 1) : has_compact_mul_support (λ x, m (f x) (f₂ x)) := begin rw [has_compact_mul_support_iff_eventually_eq] at hf hf₂ ⊢, filter_upwards [hf, hf₂] using λ x hx hx₂, by simp_rw [hx, hx₂, pi.one_apply, hm] end end section monoid variables [topological_space α] [monoid β] variables {f f' : α → β} {x : α} @[to_additive] lemma has_compact_mul_support.mul (hf : has_compact_mul_support f) (hf' : has_compact_mul_support f') : has_compact_mul_support (f * f') := by apply hf.comp₂_left hf' (mul_one 1) -- `by apply` speeds up elaboration end monoid section distrib_mul_action variables [topological_space α] [monoid_with_zero R] [add_monoid M] [distrib_mul_action R M] variables {f : α → R} {f' : α → M} {x : α} lemma has_compact_support.smul_left (hf : has_compact_support f') : has_compact_support (f • f') := begin rw [has_compact_support_iff_eventually_eq] at hf ⊢, refine hf.mono (λ x hx, by simp_rw [pi.smul_apply', hx, pi.zero_apply, smul_zero]) end end distrib_mul_action section smul_with_zero variables [topological_space α] [has_zero R] [has_zero M] [smul_with_zero R M] variables {f : α → R} {f' : α → M} {x : α} lemma has_compact_support.smul_right (hf : has_compact_support f) : has_compact_support (f • f') := begin rw [has_compact_support_iff_eventually_eq] at hf ⊢, refine hf.mono (λ x hx, by simp_rw [pi.smul_apply', hx, pi.zero_apply, zero_smul]) end lemma has_compact_support.smul_left' (hf : has_compact_support f') : has_compact_support (f • f') := begin rw [has_compact_support_iff_eventually_eq] at hf ⊢, refine hf.mono (λ x hx, by simp_rw [pi.smul_apply', hx, pi.zero_apply, smul_zero]) end end smul_with_zero section mul_zero_class variables [topological_space α] [mul_zero_class β] variables {f f' : α → β} {x : α} lemma has_compact_support.mul_right (hf : has_compact_support f) : has_compact_support (f * f') := begin rw [has_compact_support_iff_eventually_eq] at hf ⊢, refine hf.mono (λ x hx, by simp_rw [pi.mul_apply, hx, pi.zero_apply, zero_mul]) end lemma has_compact_support.mul_left (hf : has_compact_support f') : has_compact_support (f * f') := begin rw [has_compact_support_iff_eventually_eq] at hf ⊢, refine hf.mono (λ x hx, by simp_rw [pi.mul_apply, hx, pi.zero_apply, mul_zero]) end end mul_zero_class namespace locally_finite variables {ι : Type*} {U : ι → set X} [topological_space X] [has_one R] /-- If a family of functions `f` has locally-finite multiplicative support, subordinate to a family of open sets, then for any point we can find a neighbourhood on which only finitely-many members of `f` are not equal to 1. -/ @[to_additive /-" If a family of functions `f` has locally-finite support, subordinate to a family of open sets, then for any point we can find a neighbourhood on which only finitely-many members of `f` are non-zero. "-/] lemma exists_finset_nhd_mul_support_subset {f : ι → X → R} (hlf : locally_finite (λ i, mul_support (f i))) (hso : ∀ i, mul_tsupport (f i) ⊆ U i) (ho : ∀ i, is_open (U i)) (x : X) : ∃ (is : finset ι) {n : set X} (hn₁ : n ∈ 𝓝 x) (hn₂ : n ⊆ ⋂ i ∈ is, U i), ∀ (z ∈ n), mul_support (λ i, f i z) ⊆ is := begin obtain ⟨n, hn, hnf⟩ := hlf x, classical, let is := hnf.to_finset.filter (λ i, x ∈ U i), let js := hnf.to_finset.filter (λ j, x ∉ U j), refine ⟨is, n ∩ (⋂ j ∈ js, (mul_tsupport (f j))ᶜ) ∩ (⋂ i ∈ is, U i), inter_mem (inter_mem hn _) _, inter_subset_right _ _, λ z hz, _⟩, { exact (bInter_finset_mem js).mpr (λ j hj, is_closed.compl_mem_nhds (is_closed_mul_tsupport _) (set.not_mem_subset (hso j) (finset.mem_filter.mp hj).2)), }, { exact (bInter_finset_mem is).mpr (λ i hi, (ho i).mem_nhds (finset.mem_filter.mp hi).2) }, { have hzn : z ∈ n, { rw inter_assoc at hz, exact mem_of_mem_inter_left hz, }, replace hz := mem_of_mem_inter_right (mem_of_mem_inter_left hz), simp only [finset.mem_filter, finite.mem_to_finset, mem_set_of_eq, mem_Inter, and_imp] at hz, suffices : mul_support (λ i, f i z) ⊆ hnf.to_finset, { refine hnf.to_finset.subset_coe_filter_of_subset_forall _ this (λ i hi, _), specialize hz i ⟨z, ⟨hi, hzn⟩⟩, contrapose hz, simp [hz, subset_mul_tsupport (f i) hi], }, intros i hi, simp only [finite.coe_to_finset, mem_set_of_eq], exact ⟨z, ⟨hi, hzn⟩⟩, }, end end locally_finite
6710f359e00d48dce00c8691c1cf4414a131a2c7
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/geometry/manifold/mfderiv.lean
4bc9632fe1624f3c418d9c5291a51a1b2ddc3f5a
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
65,745
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 geometry.manifold.basic_smooth_bundle /-! # The derivative of functions between smooth manifolds Let `M` and `M'` be two smooth manifolds with corners over a field `𝕜` (with respective models with corners `I` on `(E, H)` and `I'` on `(E', H')`), and let `f : M → M'`. We define the derivative of the function at a point, within a set or along the whole space, mimicking the API for (Fréchet) derivatives. It is denoted by `mfderiv I I' f x`, where "m" stands for "manifold" and "f" for "Fréchet" (as in the usual derivative `fderiv 𝕜 f x`). ## Main definitions * `unique_mdiff_on I s` : predicate saying that, at each point of the set `s`, a function can have at most one derivative. This technical condition is important when we define `mfderiv_within` below, as otherwise there is an arbitrary choice in the derivative, and many properties will fail (for instance the chain rule). This is analogous to `unique_diff_on 𝕜 s` in a vector space. Let `f` be a map between smooth manifolds. The following definitions follow the `fderiv` API. * `mfderiv I I' f x` : the derivative of `f` at `x`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. If the map is not differentiable, this is `0`. * `mfderiv_within I I' f s x` : the derivative of `f` at `x` within `s`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. If the map is not differentiable within `s`, this is `0`. * `mdifferentiable_at I I' f x` : Prop expressing whether `f` is differentiable at `x`. * `mdifferentiable_within_at 𝕜 f s x` : Prop expressing whether `f` is differentiable within `s` at `x`. * `has_mfderiv_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative at `x`. * `has_mfderiv_within_at I I' f s x f'` : Prop expressing whether `f` has `f'` as a derivative within `s` at `x`. * `mdifferentiable_on I I' f s` : Prop expressing that `f` is differentiable on the set `s`. * `mdifferentiable I I' f` : Prop expressing that `f` is differentiable everywhere. * `tangent_map I I' f` : the derivative of `f`, as a map from the tangent bundle of `M` to the tangent bundle of `M'`. We also establish results on the differential of the identity, constant functions, charts, extended charts. For functions between vector spaces, we show that the usual notions and the manifold notions coincide. ## Implementation notes The tangent bundle is constructed using the machinery of topological fiber bundles, for which one can define bundled morphisms and construct canonically maps from the total space of one bundle to the total space of another one. One could use this mechanism to construct directly the derivative of a smooth map. However, we want to define the derivative of any map (and let it be zero if the map is not differentiable) to avoid proof arguments everywhere. This means we have to go back to the details of the definition of the total space of a fiber bundle constructed from core, to cook up a suitable definition of the derivative. It is the following: at each point, we have a preferred chart (used to identify the fiber above the point with the model vector space in fiber bundles). Then one should read the function using these preferred charts at `x` and `f x`, and take the derivative of `f` in these charts. Due to the fact that we are working in a model with corners, with an additional embedding `I` of the model space `H` in the model vector space `E`, the charts taking values in `E` are not the original charts of the manifold, but those ones composed with `I`, called extended charts. We define `written_in_ext_chart I I' x f` for the function `f` written in the preferred extended charts. Then the manifold derivative of `f`, at `x`, is just the usual derivative of `written_in_ext_chart I I' x f`, at the point `(ext_chart_at I x) x`. There is a subtelty with respect to continuity: if the function is not continuous, then the image of a small open set around `x` will not be contained in the source of the preferred chart around `f x`, which means that when reading `f` in the chart one is losing some information. To avoid this, we include continuity in the definition of differentiablity (which is reasonable since with any definition, differentiability implies continuity). *Warning*: the derivative (even within a subset) is a linear map on the whole tangent space. Suppose that one is given a smooth submanifold `N`, and a function which is smooth on `N` (i.e., its restriction to the subtype `N` is smooth). Then, in the whole manifold `M`, the property `mdifferentiable_on I I' f N` holds. However, `mfderiv_within I I' f N` is not uniquely defined (what values would one choose for vectors that are transverse to `N`?), which can create issues down the road. The problem here is that knowing the value of `f` along `N` does not determine the differential of `f` in all directions. This is in contrast to the case where `N` would be an open subset, or a submanifold with boundary of maximal dimension, where this issue does not appear. The predicate `unique_mdiff_on I N` indicates that the derivative along `N` is unique if it exists, and is an assumption in most statements requiring a form of uniqueness. On a vector space, the manifold derivative and the usual derivative are equal. This means in particular that they live on the same space, i.e., the tangent space is defeq to the original vector space. To get this property is a motivation for our definition of the tangent space as a single copy of the vector space, instead of more usual definitions such as the space of derivations, or the space of equivalence classes of smooth curves in the manifold. ## Tags Derivative, manifold -/ noncomputable theory open_locale classical topological_space manifold open set universe u section derivatives_definitions /-! ### Derivative of maps between manifolds The derivative of a smooth map `f` between smooth manifold `M` and `M'` at `x` is a bounded linear map from the tangent space to `M` at `x`, to the tangent space to `M'` at `f x`. Since we defined the tangent space using one specific chart, the formula for the derivative is written in terms of this specific chart. We use the names `mdifferentiable` and `mfderiv`, where the prefix letter `m` means "manifold". -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H') {M' : Type*} [topological_space M'] [charted_space H' M'] /-- Predicate ensuring that, at a point and within a set, a function can have at most one derivative. This is expressed using the preferred chart at the considered point. -/ def unique_mdiff_within_at (s : set M) (x : M) := unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) /-- Predicate ensuring that, at all points of a set, a function can have at most one derivative. -/ def unique_mdiff_on (s : set M) := ∀x∈s, unique_mdiff_within_at I s x /-- Conjugating a function to write it in the preferred charts around `x`. The manifold derivative of `f` will just be the derivative of this conjugated function. -/ @[simp, mfld_simps] def written_in_ext_chart_at (x : M) (f : M → M') : E → E' := (ext_chart_at I' (f x)) ∘ f ∘ (ext_chart_at I x).symm /-- `mdifferentiable_within_at I I' f s x` indicates that the function `f` between manifolds has a derivative at the point `x` within the set `s`. This is a generalization of `differentiable_within_at` to manifolds. We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def mdifferentiable_within_at (f : M → M') (s : set M) (x : M) := continuous_within_at f s x ∧ differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) /-- `mdifferentiable_at I I' f x` indicates that the function `f` between manifolds has a derivative at the point `x`. This is a generalization of `differentiable_at` to manifolds. We require continuity in the definition, as otherwise points close to `x` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def mdifferentiable_at (f : M → M') (x : M) := continuous_at f x ∧ differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) (range I) ((ext_chart_at I x) x) /-- `mdifferentiable_on I I' f s` indicates that the function `f` between manifolds has a derivative within `s` at all points of `s`. This is a generalization of `differentiable_on` to manifolds. -/ def mdifferentiable_on (f : M → M') (s : set M) := ∀x ∈ s, mdifferentiable_within_at I I' f s x /-- `mdifferentiable I I' f` indicates that the function `f` between manifolds has a derivative everywhere. This is a generalization of `differentiable` to manifolds. -/ def mdifferentiable (f : M → M') := ∀x, mdifferentiable_at I I' f x /-- Prop registering if a local homeomorphism is a local diffeomorphism on its source -/ def local_homeomorph.mdifferentiable (f : local_homeomorph M M') := (mdifferentiable_on I I' f f.source) ∧ (mdifferentiable_on I' I f.symm f.target) variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M'] /-- `has_mfderiv_within_at I I' f s x f'` indicates that the function `f` between manifolds has, at the point `x` and within the set `s`, the derivative `f'`. Here, `f'` is a continuous linear map from the tangent space at `x` to the tangent space at `f x`. This is a generalization of `has_fderiv_within_at` to manifolds (as indicated by the prefix `m`). The order of arguments is changed as the type of the derivative `f'` depends on the choice of `x`. We require continuity in the definition, as otherwise points close to `x` in `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def has_mfderiv_within_at (f : M → M') (s : set M) (x : M) (f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) := continuous_within_at f s x ∧ has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f' ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) /-- `has_mfderiv_at I I' f x f'` indicates that the function `f` between manifolds has, at the point `x`, the derivative `f'`. Here, `f'` is a continuous linear map from the tangent space at `x` to the tangent space at `f x`. We require continuity in the definition, as otherwise points close to `x` `s` could be sent by `f` outside of the chart domain around `f x`. Then the chart could do anything to the image points, and in particular by coincidence `written_in_ext_chart_at I I' x f` could be differentiable, while this would not mean anything relevant. -/ def has_mfderiv_at (f : M → M') (x : M) (f' : tangent_space I x →L[𝕜] tangent_space I' (f x)) := continuous_at f x ∧ has_fderiv_within_at (written_in_ext_chart_at I I' x f : E → E') f' (range I) ((ext_chart_at I x) x) /-- Let `f` be a function between two smooth manifolds. Then `mfderiv_within I I' f s x` is the derivative of `f` at `x` within `s`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. -/ def mfderiv_within (f : M → M') (s : set M) (x : M) : tangent_space I x →L[𝕜] tangent_space I' (f x) := if h : mdifferentiable_within_at I I' f s x then (fderiv_within 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) : _) else 0 /-- Let `f` be a function between two smooth manifolds. Then `mfderiv I I' f x` is the derivative of `f` at `x`, as a continuous linear map from the tangent space at `x` to the tangent space at `f x`. -/ def mfderiv (f : M → M') (x : M) : tangent_space I x →L[𝕜] tangent_space I' (f x) := if h : mdifferentiable_at I I' f x then (fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : E → E') (range I) ((ext_chart_at I x) x) : _) else 0 /-- The derivative within a set, as a map between the tangent bundles -/ def tangent_map_within (f : M → M') (s : set M) : tangent_bundle I M → tangent_bundle I' M' := λp, ⟨f p.1, (mfderiv_within I I' f s p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩ /-- The derivative, as a map between the tangent bundles -/ def tangent_map (f : M → M') : tangent_bundle I M → tangent_bundle I' M' := λp, ⟨f p.1, (mfderiv I I' f p.1 : tangent_space I p.1 → tangent_space I' (f p.1)) p.2⟩ end derivatives_definitions section derivatives_properties /-! ### Unique differentiability sets in manifolds -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] -- {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} {M' : Type*} [topological_space M'] [charted_space H' M'] {E'' : Type*} [normed_group E''] [normed_space 𝕜 E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''} {M'' : Type*} [topological_space M''] [charted_space H'' M''] {f f₀ f₁ : M → M'} {x : M} {s t : set M} {g : M' → M''} {u : set M'} lemma unique_mdiff_within_at_univ : unique_mdiff_within_at I univ x := begin unfold unique_mdiff_within_at, simp only [preimage_univ, univ_inter], exact I.unique_diff _ (mem_range_self _) end variable {I} lemma unique_mdiff_within_at_iff {s : set M} {x : M} : unique_mdiff_within_at I s x ↔ unique_diff_within_at 𝕜 ((ext_chart_at I x).symm ⁻¹' s ∩ (ext_chart_at I x).target) ((ext_chart_at I x) x) := begin apply unique_diff_within_at_congr, rw [nhds_within_inter, nhds_within_inter, nhds_within_ext_chart_target_eq] end lemma unique_mdiff_within_at.mono (h : unique_mdiff_within_at I s x) (st : s ⊆ t) : unique_mdiff_within_at I t x := unique_diff_within_at.mono h $ inter_subset_inter (preimage_mono st) (subset.refl _) lemma unique_mdiff_within_at.inter' (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝[s] x) : unique_mdiff_within_at I (s ∩ t) x := begin rw [unique_mdiff_within_at, ext_chart_preimage_inter_eq], exact unique_diff_within_at.inter' hs (ext_chart_preimage_mem_nhds_within I x ht) end lemma unique_mdiff_within_at.inter (hs : unique_mdiff_within_at I s x) (ht : t ∈ 𝓝 x) : unique_mdiff_within_at I (s ∩ t) x := begin rw [unique_mdiff_within_at, ext_chart_preimage_inter_eq], exact unique_diff_within_at.inter hs (ext_chart_preimage_mem_nhds I x ht) end lemma is_open.unique_mdiff_within_at (xs : x ∈ s) (hs : is_open s) : unique_mdiff_within_at I s x := begin have := unique_mdiff_within_at.inter (unique_mdiff_within_at_univ I) (mem_nhds_sets hs xs), rwa univ_inter at this end lemma unique_mdiff_on.inter (hs : unique_mdiff_on I s) (ht : is_open t) : unique_mdiff_on I (s ∩ t) := λx hx, unique_mdiff_within_at.inter (hs _ hx.1) (mem_nhds_sets ht hx.2) lemma is_open.unique_mdiff_on (hs : is_open s) : unique_mdiff_on I s := λx hx, is_open.unique_mdiff_within_at hx hs lemma unique_mdiff_on_univ : unique_mdiff_on I (univ : set M) := is_open_univ.unique_mdiff_on /- We name the typeclass variables related to `smooth_manifold_with_corners` structure as they are necessary in lemmas mentioning the derivative, but not in lemmas about differentiability, so we want to include them or omit them when necessary. -/ variables [Is : smooth_manifold_with_corners I M] [I's : smooth_manifold_with_corners I' M'] [I''s : smooth_manifold_with_corners I'' M''] {f' f₀' f₁' : tangent_space I x →L[𝕜] tangent_space I' (f x)} {g' : tangent_space I' (f x) →L[𝕜] tangent_space I'' (g (f x))} /-- `unique_mdiff_within_at` achieves its goal: it implies the uniqueness of the derivative. -/ theorem unique_mdiff_within_at.eq (U : unique_mdiff_within_at I s x) (h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') : f' = f₁' := U.eq h.2 h₁.2 theorem unique_mdiff_on.eq (U : unique_mdiff_on I s) (hx : x ∈ s) (h : has_mfderiv_within_at I I' f s x f') (h₁ : has_mfderiv_within_at I I' f s x f₁') : f' = f₁' := unique_mdiff_within_at.eq (U _ hx) h h₁ /-! ### General lemmas on derivatives of functions between manifolds We mimick the API for functions between vector spaces -/ lemma mdifferentiable_within_at_iff {f : M → M'} {s : set M} {x : M} : mdifferentiable_within_at I I' f s x ↔ continuous_within_at f s x ∧ differentiable_within_at 𝕜 (written_in_ext_chart_at I I' x f) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' s) ((ext_chart_at I x) x) := begin refine and_congr iff.rfl (exists_congr $ λ f', _), rw [inter_comm], simp only [has_fderiv_within_at, nhds_within_inter, nhds_within_ext_chart_target_eq] end include Is I's lemma mfderiv_within_zero_of_not_mdifferentiable_within_at (h : ¬ mdifferentiable_within_at I I' f s x) : mfderiv_within I I' f s x = 0 := by simp only [mfderiv_within, h, dif_neg, not_false_iff] lemma mfderiv_zero_of_not_mdifferentiable_at (h : ¬ mdifferentiable_at I I' f x) : mfderiv I I' f x = 0 := by simp only [mfderiv, h, dif_neg, not_false_iff] theorem has_mfderiv_within_at.mono (h : has_mfderiv_within_at I I' f t x f') (hst : s ⊆ t) : has_mfderiv_within_at I I' f s x f' := ⟨ continuous_within_at.mono h.1 hst, has_fderiv_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩ theorem has_mfderiv_at.has_mfderiv_within_at (h : has_mfderiv_at I I' f x f') : has_mfderiv_within_at I I' f s x f' := ⟨ continuous_at.continuous_within_at h.1, has_fderiv_within_at.mono h.2 (inter_subset_right _ _) ⟩ lemma has_mfderiv_within_at.mdifferentiable_within_at (h : has_mfderiv_within_at I I' f s x f') : mdifferentiable_within_at I I' f s x := ⟨h.1, ⟨f', h.2⟩⟩ lemma has_mfderiv_at.mdifferentiable_at (h : has_mfderiv_at I I' f x f') : mdifferentiable_at I I' f x := ⟨h.1, ⟨f', h.2⟩⟩ @[simp, mfld_simps] lemma has_mfderiv_within_at_univ : has_mfderiv_within_at I I' f univ x f' ↔ has_mfderiv_at I I' f x f' := by simp only [has_mfderiv_within_at, has_mfderiv_at, continuous_within_at_univ] with mfld_simps theorem has_mfderiv_at_unique (h₀ : has_mfderiv_at I I' f x f₀') (h₁ : has_mfderiv_at I I' f x f₁') : f₀' = f₁' := begin rw ← has_mfderiv_within_at_univ at h₀ h₁, exact (unique_mdiff_within_at_univ I).eq h₀ h₁ end lemma has_mfderiv_within_at_inter' (h : t ∈ 𝓝[s] x) : has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' := begin rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_preimage_inter_eq, has_fderiv_within_at_inter', continuous_within_at_inter' h], exact ext_chart_preimage_mem_nhds_within I x h, end lemma has_mfderiv_within_at_inter (h : t ∈ 𝓝 x) : has_mfderiv_within_at I I' f (s ∩ t) x f' ↔ has_mfderiv_within_at I I' f s x f' := begin rw [has_mfderiv_within_at, has_mfderiv_within_at, ext_chart_preimage_inter_eq, has_fderiv_within_at_inter, continuous_within_at_inter h], exact ext_chart_preimage_mem_nhds I x h, end lemma has_mfderiv_within_at.union (hs : has_mfderiv_within_at I I' f s x f') (ht : has_mfderiv_within_at I I' f t x f') : has_mfderiv_within_at I I' f (s ∪ t) x f' := begin split, { exact continuous_within_at.union hs.1 ht.1 }, { convert has_fderiv_within_at.union hs.2 ht.2, simp only [union_inter_distrib_right, preimage_union] } end lemma has_mfderiv_within_at.nhds_within (h : has_mfderiv_within_at I I' f s x f') (ht : s ∈ 𝓝[t] x) : has_mfderiv_within_at I I' f t x f' := (has_mfderiv_within_at_inter' ht).1 (h.mono (inter_subset_right _ _)) lemma has_mfderiv_within_at.has_mfderiv_at (h : has_mfderiv_within_at I I' f s x f') (hs : s ∈ 𝓝 x) : has_mfderiv_at I I' f x f' := by rwa [← univ_inter s, has_mfderiv_within_at_inter hs, has_mfderiv_within_at_univ] at h lemma mdifferentiable_within_at.has_mfderiv_within_at (h : mdifferentiable_within_at I I' f s x) : has_mfderiv_within_at I I' f s x (mfderiv_within I I' f s x) := begin refine ⟨h.1, _⟩, simp only [mfderiv_within, h, dif_pos] with mfld_simps, exact differentiable_within_at.has_fderiv_within_at h.2 end lemma mdifferentiable_within_at.mfderiv_within (h : mdifferentiable_within_at I I' f s x) : (mfderiv_within I I' f s x) = fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ((ext_chart_at I x) x) := by simp only [mfderiv_within, h, dif_pos] lemma mdifferentiable_at.has_mfderiv_at (h : mdifferentiable_at I I' f x) : has_mfderiv_at I I' f x (mfderiv I I' f x) := begin refine ⟨h.1, _⟩, simp only [mfderiv, h, dif_pos] with mfld_simps, exact differentiable_within_at.has_fderiv_within_at h.2 end lemma mdifferentiable_at.mfderiv (h : mdifferentiable_at I I' f x) : (mfderiv I I' f x) = fderiv_within 𝕜 (written_in_ext_chart_at I I' x f : _) (range I) ((ext_chart_at I x) x) := by simp only [mfderiv, h, dif_pos] lemma has_mfderiv_at.mfderiv (h : has_mfderiv_at I I' f x f') : mfderiv I I' f x = f' := by { ext, rw has_mfderiv_at_unique h h.mdifferentiable_at.has_mfderiv_at } lemma has_mfderiv_within_at.mfderiv_within (h : has_mfderiv_within_at I I' f s x f') (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' f s x = f' := by { ext, rw hxs.eq h h.mdifferentiable_within_at.has_mfderiv_within_at } lemma mdifferentiable.mfderiv_within (h : mdifferentiable_at I I' f x) (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' f s x = mfderiv I I' f x := begin apply has_mfderiv_within_at.mfderiv_within _ hxs, exact h.has_mfderiv_at.has_mfderiv_within_at end lemma mfderiv_within_subset (st : s ⊆ t) (hs : unique_mdiff_within_at I s x) (h : mdifferentiable_within_at I I' f t x) : mfderiv_within I I' f s x = mfderiv_within I I' f t x := ((mdifferentiable_within_at.has_mfderiv_within_at h).mono st).mfderiv_within hs omit Is I's lemma mdifferentiable_within_at.mono (hst : s ⊆ t) (h : mdifferentiable_within_at I I' f t x) : mdifferentiable_within_at I I' f s x := ⟨ continuous_within_at.mono h.1 hst, differentiable_within_at.mono h.2 (inter_subset_inter (preimage_mono hst) (subset.refl _)) ⟩ lemma mdifferentiable_within_at_univ : mdifferentiable_within_at I I' f univ x ↔ mdifferentiable_at I I' f x := by simp only [mdifferentiable_within_at, mdifferentiable_at, continuous_within_at_univ] with mfld_simps lemma mdifferentiable_within_at_inter (ht : t ∈ 𝓝 x) : mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x := begin rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_preimage_inter_eq, differentiable_within_at_inter, continuous_within_at_inter ht], exact ext_chart_preimage_mem_nhds I x ht end lemma mdifferentiable_within_at_inter' (ht : t ∈ 𝓝[s] x) : mdifferentiable_within_at I I' f (s ∩ t) x ↔ mdifferentiable_within_at I I' f s x := begin rw [mdifferentiable_within_at, mdifferentiable_within_at, ext_chart_preimage_inter_eq, differentiable_within_at_inter', continuous_within_at_inter' ht], exact ext_chart_preimage_mem_nhds_within I x ht end lemma mdifferentiable_at.mdifferentiable_within_at (h : mdifferentiable_at I I' f x) : mdifferentiable_within_at I I' f s x := mdifferentiable_within_at.mono (subset_univ _) (mdifferentiable_within_at_univ.2 h) lemma mdifferentiable_within_at.mdifferentiable_at (h : mdifferentiable_within_at I I' f s x) (hs : s ∈ 𝓝 x) : mdifferentiable_at I I' f x := begin have : s = univ ∩ s, by rw univ_inter, rwa [this, mdifferentiable_within_at_inter hs, mdifferentiable_within_at_univ] at h, end lemma mdifferentiable_on.mono (h : mdifferentiable_on I I' f t) (st : s ⊆ t) : mdifferentiable_on I I' f s := λx hx, (h x (st hx)).mono st lemma mdifferentiable_on_univ : mdifferentiable_on I I' f univ ↔ mdifferentiable I I' f := by { simp only [mdifferentiable_on, mdifferentiable_within_at_univ] with mfld_simps, refl } lemma mdifferentiable.mdifferentiable_on (h : mdifferentiable I I' f) : mdifferentiable_on I I' f s := (mdifferentiable_on_univ.2 h).mono (subset_univ _) lemma mdifferentiable_on_of_locally_mdifferentiable_on (h : ∀x∈s, ∃u, is_open u ∧ x ∈ u ∧ mdifferentiable_on I I' f (s ∩ u)) : mdifferentiable_on I I' f s := begin assume x xs, rcases h x xs with ⟨t, t_open, xt, ht⟩, exact (mdifferentiable_within_at_inter (mem_nhds_sets t_open xt)).1 (ht x ⟨xs, xt⟩) end include Is I's @[simp, mfld_simps] lemma mfderiv_within_univ : mfderiv_within I I' f univ = mfderiv I I' f := begin ext x : 1, simp only [mfderiv_within, mfderiv] with mfld_simps, rw mdifferentiable_within_at_univ end lemma mfderiv_within_inter (ht : t ∈ 𝓝 x) (hs : unique_mdiff_within_at I s x) : mfderiv_within I I' f (s ∩ t) x = mfderiv_within I I' f s x := by rw [mfderiv_within, mfderiv_within, ext_chart_preimage_inter_eq, mdifferentiable_within_at_inter ht, fderiv_within_inter (ext_chart_preimage_mem_nhds I x ht) hs] omit Is I's /-! ### Deriving continuity from differentiability on manifolds -/ theorem has_mfderiv_within_at.continuous_within_at (h : mdifferentiable_within_at I I' f s x) : continuous_within_at f s x := h.1 theorem has_mfderiv_at.continuous_at (h : has_mfderiv_at I I' f x f') : continuous_at f x := h.1 lemma mdifferentiable_within_at.continuous_within_at (h : mdifferentiable_within_at I I' f s x) : continuous_within_at f s x := h.1 lemma mdifferentiable_at.continuous_at (h : mdifferentiable_at I I' f x) : continuous_at f x := h.1 lemma mdifferentiable_on.continuous_on (h : mdifferentiable_on I I' f s) : continuous_on f s := λx hx, (h x hx).continuous_within_at lemma mdifferentiable.continuous (h : mdifferentiable I I' f) : continuous f := continuous_iff_continuous_at.2 $ λx, (h x).continuous_at include Is I's lemma tangent_map_within_subset {p : tangent_bundle I M} (st : s ⊆ t) (hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_within_at I I' f t p.1) : tangent_map_within I I' f s p = tangent_map_within I I' f t p := begin simp only [tangent_map_within] with mfld_simps, rw mfderiv_within_subset st hs h, end lemma tangent_map_within_univ : tangent_map_within I I' f univ = tangent_map I I' f := by { ext p : 1, simp only [tangent_map_within, tangent_map] with mfld_simps } lemma tangent_map_within_eq_tangent_map {p : tangent_bundle I M} (hs : unique_mdiff_within_at I s p.1) (h : mdifferentiable_at I I' f p.1) : tangent_map_within I I' f s p = tangent_map I I' f p := begin rw ← mdifferentiable_within_at_univ at h, rw ← tangent_map_within_univ, exact tangent_map_within_subset (subset_univ _) hs h, end @[simp, mfld_simps] lemma tangent_map_within_tangent_bundle_proj {p : tangent_bundle I M} : tangent_bundle.proj I' M' (tangent_map_within I I' f s p) = f (tangent_bundle.proj I M p) := rfl @[simp, mfld_simps] lemma tangent_map_within_proj {p : tangent_bundle I M} : (tangent_map_within I I' f s p).1 = f p.1 := rfl @[simp, mfld_simps] lemma tangent_map_tangent_bundle_proj {p : tangent_bundle I M} : tangent_bundle.proj I' M' (tangent_map I I' f p) = f (tangent_bundle.proj I M p) := rfl @[simp, mfld_simps] lemma tangent_map_proj {p : tangent_bundle I M} : (tangent_map I I' f p).1 = f p.1 := rfl omit Is I's /-! ### Congruence lemmas for derivatives on manifolds -/ lemma has_mfderiv_within_at.congr_of_eventually_eq (h : has_mfderiv_within_at I I' f s x f') (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : has_mfderiv_within_at I I' f₁ s x f' := begin refine ⟨continuous_within_at.congr_of_eventually_eq h.1 h₁ hx, _⟩, apply has_fderiv_within_at.congr_of_eventually_eq h.2, { have : (ext_chart_at I x).symm ⁻¹' {y | f₁ y = f y} ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := ext_chart_preimage_mem_nhds_within I x h₁, apply filter.mem_sets_of_superset this (λy, _), simp only [hx] with mfld_simps {contextual := tt} }, { simp only [hx] with mfld_simps }, end lemma has_mfderiv_within_at.congr_mono (h : has_mfderiv_within_at I I' f s x f') (ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : has_mfderiv_within_at I I' f₁ t x f' := (h.mono h₁).congr_of_eventually_eq (filter.mem_inf_sets_of_right ht) hx lemma has_mfderiv_at.congr_of_eventually_eq (h : has_mfderiv_at I I' f x f') (h₁ : f₁ =ᶠ[𝓝 x] f) : has_mfderiv_at I I' f₁ x f' := begin rw ← has_mfderiv_within_at_univ at ⊢ h, apply h.congr_of_eventually_eq _ (mem_of_nhds h₁ : _), rwa nhds_within_univ end include Is I's lemma mdifferentiable_within_at.congr_of_eventually_eq (h : mdifferentiable_within_at I I' f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x := (h.has_mfderiv_within_at.congr_of_eventually_eq h₁ hx).mdifferentiable_within_at variables (I I') lemma filter.eventually_eq.mdifferentiable_within_at_iff (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f s x ↔ mdifferentiable_within_at I I' f₁ s x := begin split, { assume h, apply h.congr_of_eventually_eq h₁ hx }, { assume h, apply h.congr_of_eventually_eq _ hx.symm, apply h₁.mono, intro y, apply eq.symm } end variables {I I'} lemma mdifferentiable_within_at.congr_mono (h : mdifferentiable_within_at I I' f s x) (ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : mdifferentiable_within_at I I' f₁ t x := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx h₁).mdifferentiable_within_at lemma mdifferentiable_within_at.congr (h : mdifferentiable_within_at I I' f s x) (ht : ∀x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : mdifferentiable_within_at I I' f₁ s x := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at ht hx (subset.refl _)).mdifferentiable_within_at lemma mdifferentiable_on.congr_mono (h : mdifferentiable_on I I' f s) (h' : ∀x ∈ t, f₁ x = f x) (h₁ : t ⊆ s) : mdifferentiable_on I I' f₁ t := λ x hx, (h x (h₁ hx)).congr_mono h' (h' x hx) h₁ lemma mdifferentiable_at.congr_of_eventually_eq (h : mdifferentiable_at I I' f x) (hL : f₁ =ᶠ[𝓝 x] f) : mdifferentiable_at I I' f₁ x := ((h.has_mfderiv_at).congr_of_eventually_eq hL).mdifferentiable_at lemma mdifferentiable_within_at.mfderiv_within_congr_mono (h : mdifferentiable_within_at I I' f s x) (hs : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (hxt : unique_mdiff_within_at I t x) (h₁ : t ⊆ s) : mfderiv_within I I' f₁ t x = (mfderiv_within I I' f s x : _) := (has_mfderiv_within_at.congr_mono h.has_mfderiv_within_at hs hx h₁).mfderiv_within hxt lemma filter.eventually_eq.mfderiv_within_eq (hs : unique_mdiff_within_at I s x) (hL : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) := begin by_cases h : mdifferentiable_within_at I I' f s x, { exact ((h.has_mfderiv_within_at).congr_of_eventually_eq hL hx).mfderiv_within hs }, { unfold mfderiv_within, rw [dif_neg h, dif_neg], rwa ← hL.mdifferentiable_within_at_iff I I' hx } end lemma mfderiv_within_congr (hs : unique_mdiff_within_at I s x) (hL : ∀ x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : mfderiv_within I I' f₁ s x = (mfderiv_within I I' f s x : _) := filter.eventually_eq.mfderiv_within_eq hs (filter.eventually_eq_of_mem (self_mem_nhds_within) hL) hx lemma tangent_map_within_congr (h : ∀ x ∈ s, f x = f₁ x) (p : tangent_bundle I M) (hp : p.1 ∈ s) (hs : unique_mdiff_within_at I s p.1) : tangent_map_within I I' f s p = tangent_map_within I I' f₁ s p := begin simp only [tangent_map_within, h p.fst hp, true_and, eq_self_iff_true, heq_iff_eq, sigma.mk.inj_iff], congr' 1, exact mfderiv_within_congr hs h (h _ hp) end lemma filter.eventually_eq.mfderiv_eq (hL : f₁ =ᶠ[𝓝 x] f) : mfderiv I I' f₁ x = (mfderiv I I' f x : _) := begin have A : f₁ x = f x := (mem_of_nhds hL : _), rw [← mfderiv_within_univ, ← mfderiv_within_univ], rw ← nhds_within_univ at hL, exact hL.mfderiv_within_eq (unique_mdiff_within_at_univ I) A end /-! ### Composition lemmas -/ omit Is I's lemma written_in_ext_chart_comp (h : continuous_within_at f s x) : {y | written_in_ext_chart_at I I'' x (g ∘ f) y = ((written_in_ext_chart_at I' I'' (f x) g) ∘ (written_in_ext_chart_at I I' x f)) y} ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := begin apply @filter.mem_sets_of_superset _ _ ((f ∘ (ext_chart_at I x).symm)⁻¹' (ext_chart_at I' (f x)).source) _ (ext_chart_preimage_mem_nhds_within I x (h.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))), mfld_set_tac, end variable (x) include Is I's I''s theorem has_mfderiv_within_at.comp (hg : has_mfderiv_within_at I' I'' g u (f x) g') (hf : has_mfderiv_within_at I I' f s x f') (hst : s ⊆ f ⁻¹' u) : has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') := begin refine ⟨continuous_within_at.comp hg.1 hf.1 hst, _⟩, have A : has_fderiv_within_at ((written_in_ext_chart_at I' I'' (f x) g) ∘ (written_in_ext_chart_at I I' x f)) (continuous_linear_map.comp g' f' : E →L[𝕜] E'') ((ext_chart_at I x).symm ⁻¹' s ∩ range (I)) ((ext_chart_at I x) x), { have : (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' (f x)).source) ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := (ext_chart_preimage_mem_nhds_within I x (hf.1.preimage_mem_nhds_within (ext_chart_at_source_mem_nhds _ _))), unfold has_mfderiv_within_at at *, rw [← has_fderiv_within_at_inter' this, ← ext_chart_preimage_inter_eq] at hf ⊢, have : written_in_ext_chart_at I I' x f ((ext_chart_at I x) x) = (ext_chart_at I' (f x)) (f x), by simp only with mfld_simps, rw ← this at hg, apply has_fderiv_within_at.comp ((ext_chart_at I x) x) hg.2 hf.2 _, assume y hy, simp only with mfld_simps at hy, have : f (((chart_at H x).symm : H → M) (I.symm y)) ∈ u := hst hy.1.1, simp only [hy, this] with mfld_simps }, apply A.congr_of_eventually_eq (written_in_ext_chart_comp hf.1), simp only with mfld_simps end /-- The chain rule. -/ theorem has_mfderiv_at.comp (hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_at I I' f x f') : has_mfderiv_at I I'' (g ∘ f) x (g'.comp f') := begin rw ← has_mfderiv_within_at_univ at *, exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ end theorem has_mfderiv_at.comp_has_mfderiv_within_at (hg : has_mfderiv_at I' I'' g (f x) g') (hf : has_mfderiv_within_at I I' f s x f') : has_mfderiv_within_at I I'' (g ∘ f) s x (g'.comp f') := begin rw ← has_mfderiv_within_at_univ at *, exact has_mfderiv_within_at.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ end lemma mdifferentiable_within_at.comp (hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x) (h : s ⊆ f ⁻¹' u) : mdifferentiable_within_at I I'' (g ∘ f) s x := begin rcases hf.2 with ⟨f', hf'⟩, have F : has_mfderiv_within_at I I' f s x f' := ⟨hf.1, hf'⟩, rcases hg.2 with ⟨g', hg'⟩, have G : has_mfderiv_within_at I' I'' g u (f x) g' := ⟨hg.1, hg'⟩, exact (has_mfderiv_within_at.comp x G F h).mdifferentiable_within_at end lemma mdifferentiable_at.comp (hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) : mdifferentiable_at I I'' (g ∘ f) x := (hg.has_mfderiv_at.comp x hf.has_mfderiv_at).mdifferentiable_at lemma mfderiv_within_comp (hg : mdifferentiable_within_at I' I'' g u (f x)) (hf : mdifferentiable_within_at I I' f s x) (h : s ⊆ f ⁻¹' u) (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I'' (g ∘ f) s x = (mfderiv_within I' I'' g u (f x)).comp (mfderiv_within I I' f s x) := begin apply has_mfderiv_within_at.mfderiv_within _ hxs, exact has_mfderiv_within_at.comp x hg.has_mfderiv_within_at hf.has_mfderiv_within_at h end lemma mfderiv_comp (hg : mdifferentiable_at I' I'' g (f x)) (hf : mdifferentiable_at I I' f x) : mfderiv I I'' (g ∘ f) x = (mfderiv I' I'' g (f x)).comp (mfderiv I I' f x) := begin apply has_mfderiv_at.mfderiv, exact has_mfderiv_at.comp x hg.has_mfderiv_at hf.has_mfderiv_at end lemma mdifferentiable_on.comp (hg : mdifferentiable_on I' I'' g u) (hf : mdifferentiable_on I I' f s) (st : s ⊆ f ⁻¹' u) : mdifferentiable_on I I'' (g ∘ f) s := λx hx, mdifferentiable_within_at.comp x (hg (f x) (st hx)) (hf x hx) st lemma mdifferentiable.comp (hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) : mdifferentiable I I'' (g ∘ f) := λx, mdifferentiable_at.comp x (hg (f x)) (hf x) lemma tangent_map_within_comp_at (p : tangent_bundle I M) (hg : mdifferentiable_within_at I' I'' g u (f p.1)) (hf : mdifferentiable_within_at I I' f s p.1) (h : s ⊆ f ⁻¹' u) (hps : unique_mdiff_within_at I s p.1) : tangent_map_within I I'' (g ∘ f) s p = tangent_map_within I' I'' g u (tangent_map_within I I' f s p) := begin simp only [tangent_map_within] with mfld_simps, rw mfderiv_within_comp p.1 hg hf h hps, refl end lemma tangent_map_comp_at (p : tangent_bundle I M) (hg : mdifferentiable_at I' I'' g (f p.1)) (hf : mdifferentiable_at I I' f p.1) : tangent_map I I'' (g ∘ f) p = tangent_map I' I'' g (tangent_map I I' f p) := begin simp only [tangent_map] with mfld_simps, rw mfderiv_comp p.1 hg hf, refl end lemma tangent_map_comp (hg : mdifferentiable I' I'' g) (hf : mdifferentiable I I' f) : tangent_map I I'' (g ∘ f) = (tangent_map I' I'' g) ∘ (tangent_map I I' f) := by { ext p : 1, exact tangent_map_comp_at _ (hg _) (hf _) } end derivatives_properties section specific_functions /-! ### Differentiability of specific functions -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] {s : set M} {x : M} section id /-! #### Identity -/ lemma has_mfderiv_at_id (x : M) : has_mfderiv_at I I (@_root_.id M) x (continuous_linear_map.id 𝕜 (tangent_space I x)) := begin refine ⟨continuous_id.continuous_at, _⟩, have : ∀ᶠ y in 𝓝[range I] ((ext_chart_at I x) x), ((ext_chart_at I x) ∘ (ext_chart_at I x).symm) y = id y, { apply filter.mem_sets_of_superset (ext_chart_at_target_mem_nhds_within I x), mfld_set_tac }, apply has_fderiv_within_at.congr_of_eventually_eq (has_fderiv_within_at_id _ _) this, simp only with mfld_simps end theorem has_mfderiv_within_at_id (s : set M) (x : M) : has_mfderiv_within_at I I (@_root_.id M) s x (continuous_linear_map.id 𝕜 (tangent_space I x)) := (has_mfderiv_at_id I x).has_mfderiv_within_at lemma mdifferentiable_at_id : mdifferentiable_at I I (@_root_.id M) x := (has_mfderiv_at_id I x).mdifferentiable_at lemma mdifferentiable_within_at_id : mdifferentiable_within_at I I (@_root_.id M) s x := (mdifferentiable_at_id I).mdifferentiable_within_at lemma mdifferentiable_id : mdifferentiable I I (@_root_.id M) := λx, mdifferentiable_at_id I lemma mdifferentiable_on_id : mdifferentiable_on I I (@_root_.id M) s := (mdifferentiable_id I).mdifferentiable_on @[simp, mfld_simps] lemma mfderiv_id : mfderiv I I (@_root_.id M) x = (continuous_linear_map.id 𝕜 (tangent_space I x)) := has_mfderiv_at.mfderiv (has_mfderiv_at_id I x) lemma mfderiv_within_id (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I (@_root_.id M) s x = (continuous_linear_map.id 𝕜 (tangent_space I x)) := begin rw mdifferentiable.mfderiv_within (mdifferentiable_at_id I) hxs, exact mfderiv_id I end @[simp, mfld_simps] lemma tangent_map_id : tangent_map I I (id : M → M) = id := by { ext1 ⟨x, v⟩, simp [tangent_map] } lemma tangent_map_within_id {p : tangent_bundle I M} (hs : unique_mdiff_within_at I s (tangent_bundle.proj I M p)) : tangent_map_within I I (id : M → M) s p = p := begin simp only [tangent_map_within, id.def], rw mfderiv_within_id, { rcases p, refl }, { exact hs } end end id section const /-! #### Constants -/ variables {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H') {M' : Type*} [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M'] {c : M'} lemma has_mfderiv_at_const (c : M') (x : M) : has_mfderiv_at I I' (λy : M, c) x (0 : tangent_space I x →L[𝕜] tangent_space I' c) := begin refine ⟨continuous_const.continuous_at, _⟩, have : (ext_chart_at I' c) ∘ (λ (y : M), c) ∘ (ext_chart_at I x).symm = (λy, (ext_chart_at I' c) c) := rfl, rw [written_in_ext_chart_at, this], apply has_fderiv_within_at_const end theorem has_mfderiv_within_at_const (c : M') (s : set M) (x : M) : has_mfderiv_within_at I I' (λy : M, c) s x (0 : tangent_space I x →L[𝕜] tangent_space I' c) := (has_mfderiv_at_const I I' c x).has_mfderiv_within_at lemma mdifferentiable_at_const : mdifferentiable_at I I' (λy : M, c) x := (has_mfderiv_at_const I I' c x).mdifferentiable_at lemma mdifferentiable_within_at_const : mdifferentiable_within_at I I' (λy : M, c) s x := (mdifferentiable_at_const I I').mdifferentiable_within_at lemma mdifferentiable_const : mdifferentiable I I' (λy : M, c) := λx, mdifferentiable_at_const I I' lemma mdifferentiable_on_const : mdifferentiable_on I I' (λy : M, c) s := (mdifferentiable_const I I').mdifferentiable_on @[simp, mfld_simps] lemma mfderiv_const : mfderiv I I' (λy : M, c) x = (0 : tangent_space I x →L[𝕜] tangent_space I' c) := has_mfderiv_at.mfderiv (has_mfderiv_at_const I I' c x) lemma mfderiv_within_const (hxs : unique_mdiff_within_at I s x) : mfderiv_within I I' (λy : M, c) s x = (0 : tangent_space I x →L[𝕜] tangent_space I' c) := begin rw mdifferentiable.mfderiv_within (mdifferentiable_at_const I I') hxs, { exact mfderiv_const I I' }, { apply_instance } end end const section model_with_corners /-! #### Model with corners -/ lemma model_with_corners.mdifferentiable : mdifferentiable I (model_with_corners_self 𝕜 E) I := begin simp only [mdifferentiable, mdifferentiable_at] with mfld_simps, assume x, refine ⟨I.continuous.continuous_at, _⟩, have : differentiable_within_at 𝕜 id (range I) (I x) := differentiable_at_id.differentiable_within_at, apply this.congr, { simp only with mfld_simps {contextual := tt} }, { simp only with mfld_simps } end lemma model_with_corners.mdifferentiable_on_symm : mdifferentiable_on (model_with_corners_self 𝕜 E) I I.symm (range I) := begin simp only [mdifferentiable_on, mdifferentiable_within_at] with mfld_simps, assume x hx, refine ⟨I.continuous_symm.continuous_at.continuous_within_at, _⟩, have : differentiable_within_at 𝕜 id (range I) x := differentiable_at_id.differentiable_within_at, apply this.congr, { simp only with mfld_simps {contextual := tt} }, { simp only [hx] with mfld_simps } end end model_with_corners section charts variable {e : local_homeomorph M H} lemma mdifferentiable_at_atlas (h : e ∈ atlas H M) {x : M} (hx : x ∈ e.source) : mdifferentiable_at I I e x := begin refine ⟨(e.continuous_on x hx).continuous_at (mem_nhds_sets e.open_source hx), _⟩, have mem : I ((chart_at H x : M → H) x) ∈ I.symm ⁻¹' ((chart_at H x).symm ≫ₕ e).source ∩ range I, by simp only [hx] with mfld_simps, have : (chart_at H x).symm.trans e ∈ times_cont_diff_groupoid ∞ I := has_groupoid.compatible _ (chart_mem_atlas H x) h, have A : times_cont_diff_on 𝕜 ∞ (I ∘ ((chart_at H x).symm.trans e) ∘ I.symm) (I.symm ⁻¹' ((chart_at H x).symm.trans e).source ∩ range I) := this.1, have B := A.differentiable_on le_top (I ((chart_at H x : M → H) x)) mem, simp only with mfld_simps at B, rw [inter_comm, differentiable_within_at_inter] at B, { simpa only with mfld_simps }, { apply mem_nhds_sets (I.continuous_symm _ (local_homeomorph.open_source _)) mem.1 } end lemma mdifferentiable_on_atlas (h : e ∈ atlas H M) : mdifferentiable_on I I e e.source := λx hx, (mdifferentiable_at_atlas I h hx).mdifferentiable_within_at lemma mdifferentiable_at_atlas_symm (h : e ∈ atlas H M) {x : H} (hx : x ∈ e.target) : mdifferentiable_at I I e.symm x := begin refine ⟨(e.continuous_on_symm x hx).continuous_at (mem_nhds_sets e.open_target hx), _⟩, have mem : I x ∈ I.symm ⁻¹' (e.symm ≫ₕ chart_at H (e.symm x)).source ∩ range (I), by simp only [hx] with mfld_simps, have : e.symm.trans (chart_at H (e.symm x)) ∈ times_cont_diff_groupoid ∞ I := has_groupoid.compatible _ h (chart_mem_atlas H _), have A : times_cont_diff_on 𝕜 ∞ (I ∘ (e.symm.trans (chart_at H (e.symm x))) ∘ I.symm) (I.symm ⁻¹' (e.symm.trans (chart_at H (e.symm x))).source ∩ range I) := this.1, have B := A.differentiable_on le_top (I x) mem, simp only with mfld_simps at B, rw [inter_comm, differentiable_within_at_inter] at B, { simpa only with mfld_simps }, { apply (mem_nhds_sets (I.continuous_symm _ (local_homeomorph.open_source _)) mem.1) } end lemma mdifferentiable_on_atlas_symm (h : e ∈ atlas H M) : mdifferentiable_on I I e.symm e.target := λx hx, (mdifferentiable_at_atlas_symm I h hx).mdifferentiable_within_at lemma mdifferentiable_of_mem_atlas (h : e ∈ atlas H M) : e.mdifferentiable I I := ⟨mdifferentiable_on_atlas I h, mdifferentiable_on_atlas_symm I h⟩ lemma mdifferentiable_chart (x : M) : (chart_at H x).mdifferentiable I I := mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _) /-- The derivative of the chart at a base point is the chart of the tangent bundle, composed with the identification between the tangent bundle of the model space and the product space. -/ lemma tangent_map_chart {p q : tangent_bundle I M} (h : q.1 ∈ (chart_at H p.1).source) : tangent_map I I (chart_at H p.1) q = (equiv.sigma_equiv_prod _ _).symm ((chart_at (model_prod H E) p : tangent_bundle I M → model_prod H E) q) := begin dsimp [tangent_map], rw mdifferentiable_at.mfderiv, { refl }, { exact mdifferentiable_at_atlas _ (chart_mem_atlas _ _) h } end /-- The derivative of the inverse of the chart at a base point is the inverse of the chart of the tangent bundle, composed with the identification between the tangent bundle of the model space and the product space. -/ lemma tangent_map_chart_symm {p : tangent_bundle I M} {q : tangent_bundle I H} (h : q.1 ∈ (chart_at H p.1).target) : tangent_map I I (chart_at H p.1).symm q = ((chart_at (model_prod H E) p).symm : model_prod H E → tangent_bundle I M) ((equiv.sigma_equiv_prod H E) q) := begin dsimp only [tangent_map], rw mdifferentiable_at.mfderiv (mdifferentiable_at_atlas_symm _ (chart_mem_atlas _ _) h), -- a trivial instance is needed after the rewrite, handle it right now. rotate, { apply_instance }, simp only [chart_at, basic_smooth_bundle_core.chart, topological_fiber_bundle_core.local_triv, basic_smooth_bundle_core.to_topological_fiber_bundle_core, topological_fiber_bundle_core.local_triv', tangent_bundle_core, h, equiv.sigma_equiv_prod_apply, subtype.coe_mk] with mfld_simps, end end charts end specific_functions section mfderiv_fderiv /-! ### Relations between vector space derivative and manifold derivative The manifold derivative `mfderiv`, when considered on the model vector space with its trivial manifold structure, coincides with the usual Frechet derivative `fderiv`. In this section, we prove this and related statements. -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {f : E → E'} {s : set E} {x : E} lemma unique_mdiff_within_at_iff_unique_diff_within_at : unique_mdiff_within_at (model_with_corners_self 𝕜 E) s x ↔ unique_diff_within_at 𝕜 s x := by simp only [unique_mdiff_within_at] with mfld_simps lemma unique_mdiff_on_iff_unique_diff_on : unique_mdiff_on (model_with_corners_self 𝕜 E) s ↔ unique_diff_on 𝕜 s := by simp [unique_mdiff_on, unique_diff_on, unique_mdiff_within_at_iff_unique_diff_within_at] @[simp, mfld_simps] lemma written_in_ext_chart_model_space : written_in_ext_chart_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') x f = f := by { ext y, simp only with mfld_simps } /-- For maps between vector spaces, `mdifferentiable_within_at` and `fdifferentiable_within_at` coincide -/ theorem mdifferentiable_within_at_iff_differentiable_within_at : mdifferentiable_within_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s x ↔ differentiable_within_at 𝕜 f s x := begin simp only [mdifferentiable_within_at] with mfld_simps, exact ⟨λH, H.2, λH, ⟨H.continuous_within_at, H⟩⟩ end /-- For maps between vector spaces, `mdifferentiable_at` and `differentiable_at` coincide -/ theorem mdifferentiable_at_iff_differentiable_at : mdifferentiable_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f x ↔ differentiable_at 𝕜 f x := begin simp only [mdifferentiable_at, differentiable_within_at_univ] with mfld_simps, exact ⟨λH, H.2, λH, ⟨H.continuous_at, H⟩⟩ end /-- For maps between vector spaces, `mdifferentiable_on` and `differentiable_on` coincide -/ theorem mdifferentiable_on_iff_differentiable_on : mdifferentiable_on (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s ↔ differentiable_on 𝕜 f s := by simp only [mdifferentiable_on, differentiable_on, mdifferentiable_within_at_iff_differentiable_within_at] /-- For maps between vector spaces, `mdifferentiable` and `differentiable` coincide -/ theorem mdifferentiable_iff_differentiable : mdifferentiable (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f ↔ differentiable 𝕜 f := by simp only [mdifferentiable, differentiable, mdifferentiable_at_iff_differentiable_at] /-- For maps between vector spaces, `mfderiv_within` and `fderiv_within` coincide -/ theorem mfderiv_within_eq_fderiv_within : mfderiv_within (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s x = fderiv_within 𝕜 f s x := begin by_cases h : mdifferentiable_within_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f s x, { simp only [mfderiv_within, h, dif_pos] with mfld_simps }, { simp only [mfderiv_within, h, dif_neg, not_false_iff], rw [mdifferentiable_within_at_iff_differentiable_within_at, differentiable_within_at] at h, change ¬(∃(f' : tangent_space (model_with_corners_self 𝕜 E) x →L[𝕜] tangent_space (model_with_corners_self 𝕜 E') (f x)), has_fderiv_within_at f f' s x) at h, simp only [fderiv_within, h, dif_neg, not_false_iff] } end /-- For maps between vector spaces, `mfderiv` and `fderiv` coincide -/ theorem mfderiv_eq_fderiv : mfderiv (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') f x = fderiv 𝕜 f x := begin rw [← mfderiv_within_univ, ← fderiv_within_univ], exact mfderiv_within_eq_fderiv_within end end mfderiv_fderiv /-! ### Differentiable local homeomorphisms -/ namespace local_homeomorph.mdifferentiable variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} {M : Type*} [topological_space M] [charted_space H M] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} {M' : Type*} [topological_space M'] [charted_space H' M'] {E'' : Type*} [normed_group E''] [normed_space 𝕜 E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''} {M'' : Type*} [topological_space M''] [charted_space H'' M''] {e : local_homeomorph M M'} (he : e.mdifferentiable I I') {e' : local_homeomorph M' M''} include he lemma symm : e.symm.mdifferentiable I' I := ⟨he.2, he.1⟩ protected lemma mdifferentiable_at {x : M} (hx : x ∈ e.source) : mdifferentiable_at I I' e x := (he.1 x hx).mdifferentiable_at (mem_nhds_sets e.open_source hx) lemma mdifferentiable_at_symm {x : M'} (hx : x ∈ e.target) : mdifferentiable_at I' I e.symm x := (he.2 x hx).mdifferentiable_at (mem_nhds_sets e.open_target hx) variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners I' M'] [smooth_manifold_with_corners I'' M''] lemma symm_comp_deriv {x : M} (hx : x ∈ e.source) : (mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) = continuous_linear_map.id 𝕜 (tangent_space I x) := begin have : (mfderiv I I (e.symm ∘ e) x) = (mfderiv I' I e.symm (e x)).comp (mfderiv I I' e x) := mfderiv_comp x (he.mdifferentiable_at_symm (e.map_source hx)) (he.mdifferentiable_at hx), rw ← this, have : mfderiv I I (_root_.id : M → M) x = continuous_linear_map.id _ _ := mfderiv_id I, rw ← this, apply filter.eventually_eq.mfderiv_eq, have : e.source ∈ 𝓝 x := mem_nhds_sets e.open_source hx, exact filter.mem_sets_of_superset this (by mfld_set_tac) end lemma comp_symm_deriv {x : M'} (hx : x ∈ e.target) : (mfderiv I I' e (e.symm x)).comp (mfderiv I' I e.symm x) = continuous_linear_map.id 𝕜 (tangent_space I' x) := he.symm.symm_comp_deriv hx /-- The derivative of a differentiable local homeomorphism, as a continuous linear equivalence between the tangent spaces at `x` and `e x`. -/ protected def mfderiv {x : M} (hx : x ∈ e.source) : tangent_space I x ≃L[𝕜] tangent_space I' (e x) := { inv_fun := (mfderiv I' I e.symm (e x)), continuous_to_fun := (mfderiv I I' e x).cont, continuous_inv_fun := (mfderiv I' I e.symm (e x)).cont, left_inv := λy, begin have : (continuous_linear_map.id _ _ : tangent_space I x →L[𝕜] tangent_space I x) y = y := rfl, conv_rhs { rw [← this, ← he.symm_comp_deriv hx] }, refl end, right_inv := λy, begin have : (continuous_linear_map.id 𝕜 _ : tangent_space I' (e x) →L[𝕜] tangent_space I' (e x)) y = y := rfl, conv_rhs { rw [← this, ← he.comp_symm_deriv (e.map_source hx)] }, rw e.left_inv hx, refl end, .. mfderiv I I' e x } lemma range_mfderiv_eq_univ {x : M} (hx : x ∈ e.source) : range (mfderiv I I' e x) = univ := (he.mfderiv hx).to_linear_equiv.to_equiv.range_eq_univ lemma trans (he': e'.mdifferentiable I' I'') : (e.trans e').mdifferentiable I I'' := begin split, { assume x hx, simp only with mfld_simps at hx, exact ((he'.mdifferentiable_at hx.2).comp _ (he.mdifferentiable_at hx.1)).mdifferentiable_within_at }, { assume x hx, simp only with mfld_simps at hx, exact ((he.symm.mdifferentiable_at hx.2).comp _ (he'.symm.mdifferentiable_at hx.1)).mdifferentiable_within_at } end end local_homeomorph.mdifferentiable /-! ### Unique derivative sets in manifolds -/ section unique_mdiff variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} {M' : Type*} [topological_space M'] [charted_space H' M'] {s : set M} /-- If a set has the unique differential property, then its image under a local diffeomorphism also has the unique differential property. -/ lemma unique_mdiff_on.unique_mdiff_on_preimage [smooth_manifold_with_corners I' M'] (hs : unique_mdiff_on I s) {e : local_homeomorph M M'} (he : e.mdifferentiable I I') : unique_mdiff_on I' (e.target ∩ e.symm ⁻¹' s) := begin /- Start from a point `x` in the image, and let `z` be its preimage. Then the unique derivative property at `x` is expressed through `ext_chart_at I' x`, and the unique derivative property at `z` is expressed through `ext_chart_at I z`. We will argue that the composition of these two charts with `e` is a local diffeomorphism in vector spaces, and therefore preserves the unique differential property thanks to lemma `has_fderiv_within_at.unique_diff_within_at`, saying that a differentiable function with onto derivative preserves the unique derivative property.-/ assume x hx, let z := e.symm x, have z_source : z ∈ e.source, by simp only [hx.1] with mfld_simps, have zx : e z = x, by simp only [z, hx.1] with mfld_simps, let F := ext_chart_at I z, -- the unique derivative property at `z` is expressed through its preferred chart, that we call `F`. have B : unique_diff_within_at 𝕜 (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z), { have : unique_mdiff_within_at I s z := hs _ hx.2, have S : e.source ∩ e ⁻¹' ((ext_chart_at I' x).source) ∈ 𝓝 z, { apply mem_nhds_sets, apply e.continuous_on.preimage_open_of_open e.open_source (ext_chart_at_open_source I' x), simp only [z_source, zx] with mfld_simps }, have := this.inter S, rw [unique_mdiff_within_at_iff] at this, exact this }, -- denote by `G` the change of coordinate, i.e., the composition of the two extended charts and -- of `e` let G := F.symm ≫ e.to_local_equiv ≫ (ext_chart_at I' x), -- `G` is differentiable have Diff : ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).mdifferentiable I I', { have A := mdifferentiable_of_mem_atlas I (chart_mem_atlas H z), have B := mdifferentiable_of_mem_atlas I' (chart_mem_atlas H' x), exact A.symm.trans (he.trans B) }, have Mmem : (chart_at H z : M → H) z ∈ ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)).source, by simp only [z_source, zx] with mfld_simps, have A : differentiable_within_at 𝕜 G (range I) (F z), { refine (Diff.mdifferentiable_at Mmem).2.congr (λp hp, _) _; simp only [G, F] with mfld_simps }, -- let `G'` be its derivative let G' := fderiv_within 𝕜 G (range I) (F z), have D₁ : has_fderiv_within_at G G' (range I) (F z) := A.has_fderiv_within_at, have D₂ : has_fderiv_within_at G G' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) (F z) := D₁.mono (by mfld_set_tac), -- The derivative `G'` is onto, as it is the derivative of a local diffeomorphism, the composition -- of the two charts and of `e`. have C₁ : range (G' : E → E') = univ, { have : G' = mfderiv I I' ((chart_at H z).symm ≫ₕ e ≫ₕ (chart_at H' x)) ((chart_at H z : M → H) z), by { rw (Diff.mdifferentiable_at Mmem).mfderiv, refl }, rw this, exact Diff.range_mfderiv_eq_univ Mmem }, have C₂ : closure (range (G' : E → E')) = univ, by rw [C₁, closure_univ], -- key step: thanks to what we have proved about it, `G` preserves the unique derivative property have key : unique_diff_within_at 𝕜 (G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target)) (G (F z)) := D₂.unique_diff_within_at B C₂, have : G (F z) = (ext_chart_at I' x) x, by { dsimp [G, F], simp only [hx.1] with mfld_simps }, rw this at key, apply key.mono, show G '' (F.symm ⁻¹' (s ∩ (e.source ∩ e ⁻¹' ((ext_chart_at I' x).source))) ∩ F.target) ⊆ (ext_chart_at I' x).symm ⁻¹' e.target ∩ (ext_chart_at I' x).symm ⁻¹' (e.symm ⁻¹' s) ∩ range (I'), rw image_subset_iff, mfld_set_tac end /-- If a set in a manifold has the unique derivative property, then its pullback by any extended chart, in the vector space, also has the unique derivative property. -/ lemma unique_mdiff_on.unique_diff_on (hs : unique_mdiff_on I s) (x : M) : unique_diff_on 𝕜 ((ext_chart_at I x).target ∩ ((ext_chart_at I x).symm ⁻¹' s)) := begin -- this is just a reformulation of `unique_mdiff_on.unique_mdiff_on_preimage`, using as `e` -- the local chart at `x`. assume z hz, simp only with mfld_simps at hz, have : (chart_at H x).mdifferentiable I I := mdifferentiable_chart _ _, have T := (hs.unique_mdiff_on_preimage this) (I.symm z), simp only [hz.left.left, hz.left.right, hz.right, unique_mdiff_within_at] with mfld_simps at ⊢ T, convert T using 1, rw @preimage_comp _ _ _ _ (chart_at H x).symm, mfld_set_tac end /-- When considering functions between manifolds, this statement shows up often. It entails the unique differential of the pullback in extended charts of the set where the function can be read in the charts. -/ lemma unique_mdiff_on.unique_diff_on_inter_preimage (hs : unique_mdiff_on I s) (x : M) (y : M') {f : M → M'} (hf : continuous_on f s) : unique_diff_on 𝕜 ((ext_chart_at I x).target ∩ ((ext_chart_at I x).symm ⁻¹' (s ∩ f⁻¹' (ext_chart_at I' y).source))) := begin have : unique_mdiff_on I (s ∩ f ⁻¹' (ext_chart_at I' y).source), { assume z hz, apply (hs z hz.1).inter', apply (hf z hz.1).preimage_mem_nhds_within, exact mem_nhds_sets (ext_chart_at_open_source I' y) hz.2 }, exact this.unique_diff_on _ end variables {F : Type*} [normed_group F] [normed_space 𝕜 F] (Z : basic_smooth_bundle_core I M F) /-- In a smooth fiber bundle constructed from core, the preimage under the projection of a set with unique differential in the basis also has unique differential. -/ lemma unique_mdiff_on.smooth_bundle_preimage (hs : unique_mdiff_on I s) : unique_mdiff_on (I.prod (model_with_corners_self 𝕜 F)) (Z.to_topological_fiber_bundle_core.proj ⁻¹' s) := begin /- Using a chart (and the fact that unique differentiability is invariant under charts), we reduce the situation to the model space, where we can use the fact that products respect unique differentiability. -/ assume p hp, replace hp : p.fst ∈ s, by simpa only with mfld_simps using hp, let e₀ := chart_at H p.1, let e := chart_at (model_prod H F) p, -- It suffices to prove unique differentiability in a chart suffices h : unique_mdiff_on (I.prod (model_with_corners_self 𝕜 F)) (e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s)), { have A : unique_mdiff_on (I.prod (model_with_corners_self 𝕜 F)) (e.symm.target ∩ e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s))), { apply h.unique_mdiff_on_preimage, exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)).symm, apply_instance }, have : p ∈ e.symm.target ∩ e.symm.symm ⁻¹' (e.target ∩ e.symm⁻¹' (Z.to_topological_fiber_bundle_core.proj ⁻¹' s)), by simp only [e, hp] with mfld_simps, apply (A _ this).mono, assume q hq, simp only [e, local_homeomorph.left_inv _ hq.1] with mfld_simps at hq, simp only [hq] with mfld_simps }, -- rewrite the relevant set in the chart as a direct product have : (λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' e.target ∩ (λ (p : E × F), (I.symm p.1, p.snd)) ⁻¹' (e.symm ⁻¹' (sigma.fst ⁻¹' s)) ∩ ((range I).prod univ) = set.prod (I.symm ⁻¹' (e₀.target ∩ e₀.symm⁻¹' s) ∩ range I) univ, by mfld_set_tac, assume q hq, replace hq : q.1 ∈ (chart_at H p.1).target ∧ ((chart_at H p.1).symm : H → M) q.1 ∈ s, by simpa only with mfld_simps using hq, simp only [unique_mdiff_within_at, model_with_corners.prod, preimage_inter, this] with mfld_simps, -- apply unique differentiability of products to conclude apply unique_diff_on.prod _ unique_diff_on_univ, { simp only [hq] with mfld_simps }, { assume x hx, have A : unique_mdiff_on I (e₀.target ∩ e₀.symm⁻¹' s), { apply hs.unique_mdiff_on_preimage, exact (mdifferentiable_of_mem_atlas _ (chart_mem_atlas _ _)), apply_instance }, simp only [unique_mdiff_on, unique_mdiff_within_at, preimage_inter] with mfld_simps at A, have B := A (I.symm x) hx.1.1 hx.1.2, rwa [← preimage_inter, model_with_corners.right_inv _ hx.2] at B } end lemma unique_mdiff_on.tangent_bundle_proj_preimage (hs : unique_mdiff_on I s): unique_mdiff_on I.tangent ((tangent_bundle.proj I M) ⁻¹' s) := hs.smooth_bundle_preimage _ end unique_mdiff
288b29a1bd720f2e5997b23992564f9027d8925a
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/linear_algebra/tensor_product.lean
f47ad291d0a071d39a0913478a493bc2177153d1
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
28,956
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro -/ import group_theory.congruence import linear_algebra.basic /-! # Tensor product of semimodules over commutative semirings. This file constructs the tensor product of semimodules over commutative semirings. Given a semiring `R` and semimodules over it `M` and `N`, the standard construction of the tensor product is `tensor_product R M N`. It is also a semimodule over `R`. It comes with a canonical bilinear map `M → N → tensor_product R M N`. Given any bilinear map `M → N → P`, there is a unique linear map `tensor_product R M N → P` whose composition with the canonical bilinear map `M → N → tensor_product R M N` is the given bilinear map `M → N → P`. We start by proving basic lemmas about bilinear maps. ## Notations This file uses the localized notation `M ⊗ N` and `M ⊗[R] N` for `tensor_product R M N`, as well as `m ⊗ₜ n` and `m ⊗ₜ[R] n` for `tensor_product.tmul R m n`. ## Tags bilinear, tensor, tensor product -/ namespace linear_map variables {R : Type*} [comm_semiring R] variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*} variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q] [add_comm_monoid S] variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] [semimodule R S] include R variables (R) /-- Create a bilinear map from a function that is linear in each component. -/ def mk₂ (f : M → N → P) (H1 : ∀ m₁ m₂ n, f (m₁ + m₂) n = f m₁ n + f m₂ n) (H2 : ∀ (c:R) m n, f (c • m) n = c • f m n) (H3 : ∀ m n₁ n₂, f m (n₁ + n₂) = f m n₁ + f m n₂) (H4 : ∀ (c:R) m n, f m (c • n) = c • f m n) : M →ₗ N →ₗ P := ⟨λ m, ⟨f m, H3 m, λ c, H4 c m⟩, λ m₁ m₂, linear_map.ext $ H1 m₁ m₂, λ c m, linear_map.ext $ H2 c m⟩ variables {R} @[simp] theorem mk₂_apply (f : M → N → P) {H1 H2 H3 H4} (m : M) (n : N) : (mk₂ R f H1 H2 H3 H4 : M →ₗ[R] N →ₗ P) m n = f m n := rfl theorem ext₂ {f g : M →ₗ[R] N →ₗ[R] P} (H : ∀ m n, f m n = g m n) : f = g := linear_map.ext (λ m, linear_map.ext $ λ n, H m n) /-- Given a linear map from `M` to linear maps from `N` to `P`, i.e., a bilinear map from `M × N` to `P`, change the order of variables and get a linear map from `N` to linear maps from `M` to `P`. -/ def flip (f : M →ₗ[R] N →ₗ[R] P) : N →ₗ M →ₗ P := mk₂ R (λ n m, f m n) (λ n₁ n₂ m, (f m).map_add _ _) (λ c n m, (f m).map_smul _ _) (λ n m₁ m₂, by rw f.map_add; refl) (λ c n m, by rw f.map_smul; refl) variable (f : M →ₗ[R] N →ₗ[R] P) @[simp] theorem flip_apply (m : M) (n : N) : flip f n m = f m n := rfl variables {R} theorem flip_inj {f g : M →ₗ[R] N →ₗ P} (H : flip f = flip g) : f = g := ext₂ $ λ m n, show flip f n m = flip g n m, by rw H variables (R M N P) /-- Given a linear map from `M` to linear maps from `N` to `P`, i.e., a bilinear map `M → N → P`, change the order of variables and get a linear map from `N` to linear maps from `M` to `P`. -/ def lflip : (M →ₗ[R] N →ₗ P) →ₗ[R] N →ₗ M →ₗ P := ⟨flip, λ _ _, rfl, λ _ _, rfl⟩ variables {R M N P} @[simp] theorem lflip_apply (m : M) (n : N) : lflip R M N P f n m = f m n := rfl theorem map_zero₂ (y) : f 0 y = 0 := (flip f y).map_zero theorem map_neg₂ {R : Type*} [comm_ring R] {M N P : Type*} [add_comm_group M] [add_comm_group N] [add_comm_group P] [module R M] [module R N] [module R P] (f : M →ₗ[R] N →ₗ[R] P) (x y) : f (-x) y = -f x y := (flip f y).map_neg _ theorem map_add₂ (x₁ x₂ y) : f (x₁ + x₂) y = f x₁ y + f x₂ y := (flip f y).map_add _ _ theorem map_smul₂ (r:R) (x y) : f (r • x) y = r • f x y := (flip f y).map_smul _ _ variables (R P) /-- Composing a linear map `M → N` and a linear map `N → P` to form a linear map `M → P`. -/ def lcomp (f : M →ₗ[R] N) : (N →ₗ[R] P) →ₗ[R] M →ₗ[R] P := flip $ linear_map.comp (flip id) f variables {R P} @[simp] theorem lcomp_apply (f : M →ₗ[R] N) (g : N →ₗ P) (x : M) : lcomp R P f g x = g (f x) := rfl variables (R M N P) /-- Composing a linear map `M → N` and a linear map `N → P` to form a linear map `M → P`. -/ def llcomp : (N →ₗ[R] P) →ₗ[R] (M →ₗ[R] N) →ₗ M →ₗ P := flip ⟨lcomp R P, λ f f', ext₂ $ λ g x, g.map_add _ _, λ c f, ext₂ $ λ g x, g.map_smul _ _⟩ variables {R M N P} section @[simp] theorem llcomp_apply (f : N →ₗ[R] P) (g : M →ₗ[R] N) (x : M) : llcomp R M N P f g x = f (g x) := rfl end /-- Composing a linear map `Q → N` and a bilinear map `M → N → P` to form a bilinear map `M → Q → P`. -/ def compl₂ (g : Q →ₗ N) : M →ₗ Q →ₗ P := (lcomp R _ g).comp f @[simp] theorem compl₂_apply (g : Q →ₗ[R] N) (m : M) (q : Q) : f.compl₂ g m q = f m (g q) := rfl /-- Composing a linear map `P → Q` and a bilinear map `M × N → P` to form a bilinear map `M → N → Q`. -/ def compr₂ (g : P →ₗ Q) : M →ₗ N →ₗ Q := linear_map.comp (llcomp R N P Q g) f @[simp] theorem compr₂_apply (g : P →ₗ[R] Q) (m : M) (n : N) : f.compr₂ g m n = g (f m n) := rfl variables (R M) /-- Scalar multiplication as a bilinear map `R → M → M`. -/ def lsmul : R →ₗ M →ₗ M := mk₂ R (•) add_smul (λ _ _ _, mul_smul _ _ _) smul_add (λ r s m, by simp only [smul_smul, smul_eq_mul, mul_comm]) variables {R M} @[simp] theorem lsmul_apply (r : R) (m : M) : lsmul R M r m = r • m := rfl end linear_map section semiring variables {R : Type*} [comm_semiring R] variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*} variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q] [add_comm_monoid S] variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] [semimodule R S] include R variables (M N) namespace tensor_product section -- open free_add_monoid variables (R) /-- The relation on `free_add_monoid (M × N)` that generates a congruence whose quotient is the tensor product. -/ inductive eqv : free_add_monoid (M × N) → free_add_monoid (M × N) → Prop | of_zero_left : ∀ n : N, eqv (free_add_monoid.of (0, n)) 0 | of_zero_right : ∀ m : M, eqv (free_add_monoid.of (m, 0)) 0 | of_add_left : ∀ (m₁ m₂ : M) (n : N), eqv (free_add_monoid.of (m₁, n) + free_add_monoid.of (m₂, n)) (free_add_monoid.of (m₁ + m₂, n)) | of_add_right : ∀ (m : M) (n₁ n₂ : N), eqv (free_add_monoid.of (m, n₁) + free_add_monoid.of (m, n₂)) (free_add_monoid.of (m, n₁ + n₂)) | of_smul : ∀ (r : R) (m : M) (n : N), eqv (free_add_monoid.of (r • m, n)) (free_add_monoid.of (m, r • n)) | add_comm : ∀ x y, eqv (x + y) (y + x) end end tensor_product variables (R) /-- The tensor product of two semimodules `M` and `N` over the same commutative semiring `R`. The localized notations are `M ⊗ N` and `M ⊗[R] N`, accessed by `open_locale tensor_product`. -/ def tensor_product : Type* := (add_con_gen (tensor_product.eqv R M N)).quotient variables {R} localized "infix ` ⊗ `:100 := tensor_product _" in tensor_product localized "notation M ` ⊗[`:100 R `] ` N:100 := tensor_product R M N" in tensor_product namespace tensor_product section module instance : add_comm_monoid (M ⊗[R] N) := { add_comm := λ x y, add_con.induction_on₂ x y $ λ x y, quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.add_comm _ _, .. (add_con_gen (tensor_product.eqv R M N)).add_monoid } instance : inhabited (M ⊗[R] N) := ⟨0⟩ variables (R) {M N} /-- The canonical function `M → N → M ⊗ N`. The localized notations are `m ⊗ₜ n` and `m ⊗ₜ[R] n`, accessed by `open_locale tensor_product`. -/ def tmul (m : M) (n : N) : M ⊗[R] N := add_con.mk' _ $ free_add_monoid.of (m, n) variables {R} infix ` ⊗ₜ `:100 := tmul _ notation x ` ⊗ₜ[`:100 R `] ` y := tmul R x y @[elab_as_eliminator] protected theorem induction_on {C : (M ⊗[R] N) → Prop} (z : M ⊗[R] N) (C0 : C 0) (C1 : ∀ {x y}, C $ x ⊗ₜ[R] y) (Cp : ∀ {x y}, C x → C y → C (x + y)) : C z := add_con.induction_on z $ λ x, free_add_monoid.rec_on x C0 $ λ ⟨m, n⟩ y ih, by { rw add_con.coe_add, exact Cp C1 ih } variables (M) @[simp] lemma zero_tmul (n : N) : (0 ⊗ₜ n : M ⊗[R] N) = 0 := quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_left _ variables {M} lemma add_tmul (m₁ m₂ : M) (n : N) : (m₁ + m₂) ⊗ₜ n = m₁ ⊗ₜ n + m₂ ⊗ₜ[R] n := eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_left _ _ _ variables (N) @[simp] lemma tmul_zero (m : M) : (m ⊗ₜ 0 : M ⊗[R] N) = 0 := quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_right _ variables {N} lemma tmul_add (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ + n₂) = m ⊗ₜ n₁ + m ⊗ₜ[R] n₂ := eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_right _ _ _ lemma smul_tmul (r : R) (m : M) (n : N) : (r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n) := quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_smul _ _ _ /-- Auxiliary function to defining scalar multiplication on tensor product. -/ def smul.aux (r : R) : free_add_monoid (M × N) →+ M ⊗[R] N := free_add_monoid.lift $ λ p : M × N, (r • p.1) ⊗ₜ p.2 theorem smul.aux_of (r : R) (m : M) (n : N) : smul.aux r (free_add_monoid.of (m, n)) = (r • m) ⊗ₜ n := rfl instance : has_scalar R (M ⊗[R] N) := ⟨λ r, (add_con_gen (tensor_product.eqv R M N)).lift (smul.aux r) $ add_con.add_con_gen_le $ λ x y hxy, match x, y, hxy with | _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_zero, smul.aux_of, smul_zero, zero_tmul] | _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_zero, smul.aux_of, tmul_zero] | _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, smul.aux_of, smul_add, add_tmul] | _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, smul.aux_of, tmul_add] | _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $ by simp_rw [smul.aux_of, smul_tmul, smul_smul, mul_comm] | _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, add_comm] end⟩ protected theorem smul_zero (r : R) : (r • 0 : M ⊗[R] N) = 0 := add_monoid_hom.map_zero _ protected theorem smul_add (r : R) (x y : M ⊗[R] N) : r • (x + y) = r • x + r • y := add_monoid_hom.map_add _ _ _ theorem smul_tmul' (r : R) (m : M) (n : N) : r • (m ⊗ₜ n : M ⊗[R] N) = (r • m) ⊗ₜ n := rfl instance : semimodule R (M ⊗[R] N) := { smul := (•), smul_add := λ r x y, tensor_product.smul_add r x y, mul_smul := λ r s x, tensor_product.induction_on x (by simp_rw tensor_product.smul_zero) (λ m n, by simp_rw [smul_tmul', mul_smul]) (λ x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy] }), one_smul := λ x, tensor_product.induction_on x (by rw tensor_product.smul_zero) (λ m n, by rw [smul_tmul', one_smul]) (λ x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy]), add_smul := λ r s x, tensor_product.induction_on x (by simp_rw [tensor_product.smul_zero, add_zero]) (λ m n, by simp_rw [smul_tmul', add_smul, add_tmul]) (λ x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy, add_add_add_comm] }), smul_zero := λ r, tensor_product.smul_zero r, zero_smul := λ x, tensor_product.induction_on x (by rw tensor_product.smul_zero) (λ m n, by rw [smul_tmul', zero_smul, zero_tmul]) (λ x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy, add_zero]) } @[simp] lemma tmul_smul (r : R) (x : M) (y : N) : x ⊗ₜ (r • y) = r • (x ⊗ₜ[R] y) := (smul_tmul _ _ _).symm variables (R M N) /-- The canonical bilinear map `M → N → M ⊗[R] N`. -/ def mk : M →ₗ N →ₗ M ⊗[R] N := linear_map.mk₂ R (⊗ₜ) add_tmul (λ c m n, by rw [smul_tmul, tmul_smul]) tmul_add tmul_smul variables {R M N} @[simp] lemma mk_apply (m : M) (n : N) : mk R M N m n = m ⊗ₜ n := rfl lemma ite_tmul (x₁ : M) (x₂ : N) (P : Prop) [decidable P] : ((if P then x₁ else 0) ⊗ₜ[R] x₂) = if P then (x₁ ⊗ₜ x₂) else 0 := by { split_ifs; simp } lemma tmul_ite (x₁ : M) (x₂ : N) (P : Prop) [decidable P] : (x₁ ⊗ₜ[R] (if P then x₂ else 0)) = if P then (x₁ ⊗ₜ x₂) else 0 := by { split_ifs; simp } section open_locale big_operators lemma sum_tmul {α : Type*} (s : finset α) (m : α → M) (n : N) : ((∑ a in s, m a) ⊗ₜ[R] n) = ∑ a in s, m a ⊗ₜ[R] n := begin classical, induction s using finset.induction with a s has ih h, { simp, }, { simp [finset.sum_insert has, add_tmul, ih], }, end lemma tmul_sum (m : M) {α : Type*} (s : finset α) (n : α → N) : (m ⊗ₜ[R] (∑ a in s, n a)) = ∑ a in s, m ⊗ₜ[R] n a := begin classical, induction s using finset.induction with a s has ih h, { simp, }, { simp [finset.sum_insert has, tmul_add, ih], }, end end end module section UMP variables {M N P Q} variables (f : M →ₗ[R] N →ₗ[R] P) /-- Auxiliary function to constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is the given bilinear map `M → N → P`. -/ def lift_aux : (M ⊗[R] N) →+ P := (add_con_gen (tensor_product.eqv R M N)).lift (free_add_monoid.lift $ λ p : M × N, f p.1 p.2) $ add_con.add_con_gen_le $ λ x y hxy, match x, y, hxy with | _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, f.map_zero₂] | _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, (f m).map_zero] | _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, f.map_add₂] | _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, (f m).map_add] | _, _, (eqv.of_smul r m n) := (add_con.ker_rel _).2 $ by simp_rw [free_add_monoid.lift_eval_of, f.map_smul₂, (f m).map_smul] | _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, add_comm] end lemma lift_aux_tmul (m n) : lift_aux f (m ⊗ₜ n) = f m n := zero_add _ variable {f} @[simp] lemma lift_aux.smul (r : R) (x) : lift_aux f (r • x) = r • lift_aux f x := tensor_product.induction_on x (smul_zero _).symm (λ p q, by rw [← tmul_smul, lift_aux_tmul, lift_aux_tmul, (f p).map_smul]) (λ p q ih1 ih2, by rw [smul_add, (lift_aux f).map_add, ih1, ih2, (lift_aux f).map_add, smul_add]) variable (f) /-- Constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is the given bilinear map `M → N → P`. -/ def lift : M ⊗ N →ₗ P := { map_smul' := lift_aux.smul, .. lift_aux f } variable {f} @[simp] lemma lift.tmul (x y) : lift f (x ⊗ₜ y) = f x y := zero_add _ @[simp] lemma lift.tmul' (x y) : (lift f).1 (x ⊗ₜ y) = f x y := lift.tmul _ _ @[ext] theorem ext {g h : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = h (x ⊗ₜ y)) : g = h := linear_map.ext $ λ z, tensor_product.induction_on z (by simp_rw linear_map.map_zero) H $ λ x y ihx ihy, by rw [g.map_add, h.map_add, ihx, ihy] theorem lift.unique {g : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = f x y) : g = lift f := ext $ λ m n, by rw [H, lift.tmul] theorem lift_mk : lift (mk R M N) = linear_map.id := eq.symm $ lift.unique $ λ x y, rfl theorem lift_compr₂ (g : P →ₗ Q) : lift (f.compr₂ g) = g.comp (lift f) := eq.symm $ lift.unique $ λ x y, by simp theorem lift_mk_compr₂ (f : M ⊗ N →ₗ P) : lift ((mk R M N).compr₂ f) = f := by rw [lift_compr₂ f, lift_mk, linear_map.comp_id] theorem mk_compr₂_inj {g h : M ⊗ N →ₗ P} (H : (mk R M N).compr₂ g = (mk R M N).compr₂ h) : g = h := by rw [← lift_mk_compr₂ g, H, lift_mk_compr₂] example : M → N → (M → N → P) → P := λ m, flip $ λ f, f m variables (R M N P) /-- Linearly constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is the given bilinear map `M → N → P`. -/ def uncurry : (M →ₗ[R] N →ₗ[R] P) →ₗ[R] M ⊗[R] N →ₗ[R] P := linear_map.flip $ lift $ (linear_map.lflip _ _ _ _).comp (linear_map.flip linear_map.id) variables {R M N P} @[simp] theorem uncurry_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) : uncurry R M N P f (m ⊗ₜ n) = f m n := by rw [uncurry, linear_map.flip_apply, lift.tmul]; refl variables (R M N P) /-- A linear equivalence constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is the given bilinear map `M → N → P`. -/ def lift.equiv : (M →ₗ N →ₗ P) ≃ₗ (M ⊗ N →ₗ P) := { inv_fun := λ f, (mk R M N).compr₂ f, left_inv := λ f, linear_map.ext₂ $ λ m n, lift.tmul _ _, right_inv := λ f, ext $ λ m n, lift.tmul _ _, .. uncurry R M N P } /-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to form a bilinear map `M → N → P`. -/ def lcurry : (M ⊗[R] N →ₗ[R] P) →ₗ[R] M →ₗ[R] N →ₗ[R] P := (lift.equiv R M N P).symm variables {R M N P} @[simp] theorem lcurry_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) : lcurry R M N P f m n = f (m ⊗ₜ n) := rfl /-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to form a bilinear map `M → N → P`. -/ def curry (f : M ⊗ N →ₗ P) : M →ₗ N →ₗ P := lcurry R M N P f @[simp] theorem curry_apply (f : M ⊗ N →ₗ[R] P) (m : M) (n : N) : curry f m n = f (m ⊗ₜ n) := rfl theorem ext_threefold {g h : (M ⊗[R] N) ⊗[R] P →ₗ[R] Q} (H : ∀ x y z, g ((x ⊗ₜ y) ⊗ₜ z) = h ((x ⊗ₜ y) ⊗ₜ z)) : g = h := begin let e := linear_equiv.to_equiv (lift.equiv R (M ⊗[R] N) P Q), apply e.symm.injective, refine ext _, intros x y, ext z, exact H x y z end -- We'll need this one for checking the pentagon identity! theorem ext_fourfold {g h : ((M ⊗[R] N) ⊗[R] P) ⊗[R] Q →ₗ[R] S} (H : ∀ w x y z, g (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z) = h (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z)) : g = h := begin let e := linear_equiv.to_equiv (lift.equiv R ((M ⊗[R] N) ⊗[R] P) Q S), apply e.symm.injective, refine ext_threefold _, intros x y z, ext w, exact H x y z w, end end UMP variables {M N} section variables (R M) /-- The base ring is a left identity for the tensor product of modules, up to linear equivalence. -/ protected def lid : R ⊗ M ≃ₗ M := linear_equiv.of_linear (lift $ linear_map.lsmul R M) (mk R R M 1) (linear_map.ext $ λ _, by simp) (ext $ λ r m, by simp; rw [← tmul_smul, ← smul_tmul, smul_eq_mul, mul_one]) end @[simp] theorem lid_tmul (m : M) (r : R) : ((tensor_product.lid R M) : (R ⊗ M → M)) (r ⊗ₜ m) = r • m := begin dsimp [tensor_product.lid], simp, end @[simp] lemma lid_symm_apply (m : M) : (tensor_product.lid R M).symm m = 1 ⊗ₜ m := rfl section variables (R M N) /-- The tensor product of modules is commutative, up to linear equivalence. -/ protected def comm : M ⊗ N ≃ₗ N ⊗ M := linear_equiv.of_linear (lift (mk R N M).flip) (lift (mk R M N).flip) (ext $ λ m n, rfl) (ext $ λ m n, rfl) @[simp] theorem comm_tmul (m : M) (n : N) : (tensor_product.comm R M N) (m ⊗ₜ n) = n ⊗ₜ m := rfl @[simp] theorem comm_symm_tmul (m : M) (n : N) : (tensor_product.comm R M N).symm (n ⊗ₜ m) = m ⊗ₜ n := rfl end section variables (R M) /-- The base ring is a right identity for the tensor product of modules, up to linear equivalence. -/ protected def rid : M ⊗[R] R ≃ₗ M := linear_equiv.trans (tensor_product.comm R M R) (tensor_product.lid R M) end @[simp] theorem rid_tmul (m : M) (r : R) : (tensor_product.rid R M) (m ⊗ₜ r) = r • m := begin dsimp [tensor_product.rid, tensor_product.comm, tensor_product.lid], simp, end @[simp] lemma rid_symm_apply (m : M) : (tensor_product.rid R M).symm m = m ⊗ₜ 1 := rfl open linear_map section variables (R M N P) /-- The associator for tensor product of R-modules, as a linear equivalence. -/ protected def assoc : (M ⊗[R] N) ⊗[R] P ≃ₗ[R] M ⊗[R] (N ⊗[R] P) := begin refine linear_equiv.of_linear (lift $ lift $ comp (lcurry R _ _ _) $ mk _ _ _) (lift $ comp (uncurry R _ _ _) $ curry $ mk _ _ _) (mk_compr₂_inj $ linear_map.ext $ λ m, ext $ λ n p, _) (mk_compr₂_inj $ flip_inj $ linear_map.ext $ λ p, ext $ λ m n, _); repeat { rw lift.tmul <|> rw compr₂_apply <|> rw comp_apply <|> rw mk_apply <|> rw flip_apply <|> rw lcurry_apply <|> rw uncurry_apply <|> rw curry_apply <|> rw id_apply } end end @[simp] theorem assoc_tmul (m : M) (n : N) (p : P) : (tensor_product.assoc R M N P) ((m ⊗ₜ n) ⊗ₜ p) = m ⊗ₜ (n ⊗ₜ p) := rfl @[simp] theorem assoc_symm_tmul (m : M) (n : N) (p : P) : (tensor_product.assoc R M N P).symm (m ⊗ₜ (n ⊗ₜ p)) = (m ⊗ₜ n) ⊗ₜ p := rfl /-- The tensor product of a pair of linear maps between modules. -/ def map (f : M →ₗ[R] P) (g : N →ₗ Q) : M ⊗ N →ₗ[R] P ⊗ Q := lift $ comp (compl₂ (mk _ _ _) g) f @[simp] theorem map_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (m : M) (n : N) : map f g (m ⊗ₜ n) = f m ⊗ₜ g n := rfl section variables {P' Q' : Type*} variables [add_comm_monoid P'] [semimodule R P'] variables [add_comm_monoid Q'] [semimodule R Q'] lemma map_comp (f₂ : P →ₗ[R] P') (f₁ : M →ₗ[R] P) (g₂ : Q →ₗ[R] Q') (g₁ : N →ₗ[R] Q) : map (f₂.comp f₁) (g₂.comp g₁) = (map f₂ g₂).comp (map f₁ g₁) := by { ext1, simp only [linear_map.comp_apply, map_tmul] } lemma lift_comp_map (i : P →ₗ[R] Q →ₗ[R] Q') (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : (lift i).comp (map f g) = lift ((i.comp f).compl₂ g) := by { ext1, simp only [lift.tmul, map_tmul, linear_map.compl₂_apply, linear_map.comp_apply] } end /-- If `M` and `P` are linearly equivalent and `N` and `Q` are linearly equivalent then `M ⊗ N` and `P ⊗ Q` are linearly equivalent. -/ def congr (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) : M ⊗ N ≃ₗ[R] P ⊗ Q := linear_equiv.of_linear (map f g) (map f.symm g.symm) (ext $ λ m n, by simp; simp only [linear_equiv.apply_symm_apply]) (ext $ λ m n, by simp; simp only [linear_equiv.symm_apply_apply]) @[simp] theorem congr_tmul (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) (m : M) (n : N) : congr f g (m ⊗ₜ n) = f m ⊗ₜ g n := rfl @[simp] theorem congr_symm_tmul (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) (p : P) (q : Q) : (congr f g).symm (p ⊗ₜ q) = f.symm p ⊗ₜ g.symm q := rfl end tensor_product namespace linear_map variables {R} (M) {N P Q} /-- `ltensor M f : M ⊗ N →ₗ M ⊗ P` is the natural linear map induced by `f : N →ₗ P`. -/ def ltensor (f : N →ₗ[R] P) : M ⊗ N →ₗ[R] M ⊗ P := tensor_product.map id f /-- `rtensor f M : N₁ ⊗ M →ₗ N₂ ⊗ M` is the natural linear map induced by `f : N₁ →ₗ N₂`. -/ def rtensor (f : N →ₗ[R] P) : N ⊗ M →ₗ[R] P ⊗ M := tensor_product.map f id variables (g : P →ₗ[R] Q) (f : N →ₗ[R] P) @[simp] lemma ltensor_tmul (m : M) (n : N) : f.ltensor M (m ⊗ₜ n) = m ⊗ₜ (f n) := rfl @[simp] lemma rtensor_tmul (m : M) (n : N) : f.rtensor M (n ⊗ₜ m) = (f n) ⊗ₜ m := rfl open tensor_product /-- `ltensor_hom M` is the natural linear map that sends a linear map `f : N →ₗ P` to `M ⊗ f`. -/ def ltensor_hom : (N →ₗ[R] P) →ₗ[R] (M ⊗[R] N →ₗ[R] M ⊗[R] P) := { to_fun := ltensor M, map_add' := λ f g, by { ext x y, simp only [add_apply, ltensor_tmul, tmul_add] }, map_smul' := λ r f, by { ext x y, simp only [tmul_smul, smul_apply, ltensor_tmul] } } /-- `rtensor_hom M` is the natural linear map that sends a linear map `f : N →ₗ P` to `M ⊗ f`. -/ def rtensor_hom : (N →ₗ[R] P) →ₗ[R] (N ⊗[R] M →ₗ[R] P ⊗[R] M) := { to_fun := λ f, f.rtensor M, map_add' := λ f g, by { ext x y, simp only [add_apply, rtensor_tmul, add_tmul] }, map_smul' := λ r f, by { ext x y, simp only [smul_tmul, tmul_smul, smul_apply, rtensor_tmul] } } @[simp] lemma coe_ltensor_hom : (ltensor_hom M : (N →ₗ[R] P) → (M ⊗[R] N →ₗ[R] M ⊗[R] P)) = ltensor M := rfl @[simp] lemma coe_rtensor_hom : (rtensor_hom M : (N →ₗ[R] P) → (N ⊗[R] M →ₗ[R] P ⊗[R] M)) = rtensor M := rfl @[simp] lemma ltensor_add (f g : N →ₗ[R] P) : (f + g).ltensor M = f.ltensor M + g.ltensor M := (ltensor_hom M).map_add f g @[simp] lemma rtensor_add (f g : N →ₗ[R] P) : (f + g).rtensor M = f.rtensor M + g.rtensor M := (rtensor_hom M).map_add f g @[simp] lemma ltensor_zero : ltensor M (0 : N →ₗ[R] P) = 0 := (ltensor_hom M).map_zero @[simp] lemma rtensor_zero : rtensor M (0 : N →ₗ[R] P) = 0 := (rtensor_hom M).map_zero @[simp] lemma ltensor_smul (r : R) (f : N →ₗ[R] P) : (r • f).ltensor M = r • (f.ltensor M) := (ltensor_hom M).map_smul r f @[simp] lemma rtensor_smul (r : R) (f : N →ₗ[R] P) : (r • f).rtensor M = r • (f.rtensor M) := (rtensor_hom M).map_smul r f lemma ltensor_comp : (g.comp f).ltensor M = (g.ltensor M).comp (f.ltensor M) := by { ext m n, simp only [comp_apply, ltensor_tmul] } lemma rtensor_comp : (g.comp f).rtensor M = (g.rtensor M).comp (f.rtensor M) := by { ext m n, simp only [comp_apply, rtensor_tmul] } variables (N) @[simp] lemma ltensor_id : (id : N →ₗ[R] N).ltensor M = id := by { ext m n, simp only [id_coe, id.def, ltensor_tmul] } @[simp] lemma rtensor_id : (id : N →ₗ[R] N).rtensor M = id := by { ext m n, simp only [id_coe, id.def, rtensor_tmul] } variables {N} @[simp] lemma ltensor_comp_rtensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : (g.ltensor P).comp (f.rtensor N) = map f g := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] @[simp] lemma rtensor_comp_ltensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : (f.rtensor Q).comp (g.ltensor M) = map f g := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] @[simp] lemma map_comp_rtensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (f' : S →ₗ[R] M) : (map f g).comp (f'.rtensor _) = map (f.comp f') g := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] @[simp] lemma map_comp_ltensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (g' : S →ₗ[R] N) : (map f g).comp (g'.ltensor _) = map f (g.comp g') := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] @[simp] lemma rtensor_comp_map (f' : P →ₗ[R] S) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : (f'.rtensor _).comp (map f g) = map (f'.comp f) g := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] @[simp] lemma ltensor_comp_map (g' : Q →ₗ[R] S) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) : (g'.ltensor _).comp (map f g) = map f (g'.comp g) := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] end linear_map end semiring section ring variables {R : Type*} [comm_ring R] variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*} variables [add_comm_group M] [add_comm_group N] [add_comm_group P] [add_comm_group Q] [add_comm_group S] variables [module R M] [module R N] [module R P] [module R Q] [module R S] namespace tensor_product open_locale tensor_product open linear_map instance : add_comm_group (M ⊗[R] N) := semimodule.add_comm_monoid_to_add_comm_group R lemma neg_tmul (m : M) (n : N) : (-m) ⊗ₜ n = -(m ⊗ₜ[R] n) := (mk R M N).map_neg₂ _ _ lemma tmul_neg (m : M) (n : N) : m ⊗ₜ (-n) = -(m ⊗ₜ[R] n) := (mk R M N _).map_neg _ end tensor_product namespace linear_map @[simp] lemma ltensor_sub (f g : N →ₗ[R] P) : (f - g).ltensor M = f.ltensor M - g.ltensor M := by simp only [← coe_ltensor_hom, map_sub] @[simp] lemma rtensor_sub (f g : N →ₗ[R] P) : (f - g).rtensor M = f.rtensor M - g.rtensor M := by simp only [← coe_rtensor_hom, map_sub] @[simp] lemma ltensor_neg (f : N →ₗ[R] P) : (-f).ltensor M = -(f.ltensor M) := by simp only [← coe_ltensor_hom, map_neg] @[simp] lemma rtensor_neg (f : N →ₗ[R] P) : (-f).rtensor M = -(f.rtensor M) := by simp only [← coe_rtensor_hom, map_neg] end linear_map end ring
e44d627040a2646596e33c5f0934e76fb9f1c656
367134ba5a65885e863bdc4507601606690974c1
/src/tactic/omega/nat/form.lean
2c9899c5bdb2dbe4f4de20e7c64397d7e2f897fe
[ "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
5,766
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek -/ /- Linear natural number arithmetic preformulas in pre-normalized preform. -/ import tactic.omega.nat.preterm namespace omega namespace nat /-- Intermediate shadow syntax for LNA formulas that includes unreified exprs -/ meta inductive exprform | eq : exprterm → exprterm → exprform | le : exprterm → exprterm → exprform | not : exprform → exprform | or : exprform → exprform → exprform | and : exprform → exprform → exprform /-- Intermediate shadow syntax for LNA formulas that includes non-canonical terms -/ @[derive has_reflect, derive inhabited] inductive preform | eq : preterm → preterm → preform | le : preterm → preterm → preform | not : preform → preform | or : preform → preform → preform | and : preform → preform → preform localized "notation x ` =* ` y := omega.nat.preform.eq x y" in omega.nat localized "notation x ` ≤* ` y := omega.nat.preform.le x y" in omega.nat localized "notation `¬* ` p := omega.nat.preform.not p" in omega.nat localized "notation p ` ∨* ` q := omega.nat.preform.or p q" in omega.nat localized "notation p ` ∧* ` q := omega.nat.preform.and p q" in omega.nat namespace preform /-- Evaluate a preform into prop using the valuation `v`. -/ @[simp] def holds (v : nat → nat) : preform → Prop | (t =* s) := t.val v = s.val v | (t ≤* s) := t.val v ≤ s.val v | (¬* p) := ¬ p.holds | (p ∨* q) := p.holds ∨ q.holds | (p ∧* q) := p.holds ∧ q.holds end preform /-- `univ_close p n` := `p` closed by prepending `n` universal quantifiers -/ @[simp] def univ_close (p : preform) : (nat → nat) → nat → Prop | v 0 := p.holds v | v (k+1) := ∀ i : nat, univ_close (update_zero i v) k namespace preform /-- Argument is free of negations -/ def neg_free : preform → Prop | (t =* s) := true | (t ≤* s) := true | (p ∨* q) := neg_free p ∧ neg_free q | (p ∧* q) := neg_free p ∧ neg_free q | _ := false /-- Return expr of proof that argument is free of subtractions -/ def sub_free : preform → Prop | (t =* s) := t.sub_free ∧ s.sub_free | (t ≤* s) := t.sub_free ∧ s.sub_free | (¬* p) := p.sub_free | (p ∨* q) := p.sub_free ∧ q.sub_free | (p ∧* q) := p.sub_free ∧ q.sub_free /-- Fresh de Brujin index not used by any variable in argument -/ def fresh_index : preform → nat | (t =* s) := max t.fresh_index s.fresh_index | (t ≤* s) := max t.fresh_index s.fresh_index | (¬* p) := p.fresh_index | (p ∨* q) := max p.fresh_index q.fresh_index | (p ∧* q) := max p.fresh_index q.fresh_index lemma holds_constant {v w : nat → nat} : ∀ p : preform, ( (∀ x < p.fresh_index, v x = w x) → (p.holds v ↔ p.holds w) ) | (t =* s) h1 := begin simp only [holds], apply pred_mono_2; apply preterm.val_constant; intros x h2; apply h1 _ (lt_of_lt_of_le h2 _), apply le_max_left, apply le_max_right end | (t ≤* s) h1 := begin simp only [holds], apply pred_mono_2; apply preterm.val_constant; intros x h2; apply h1 _ (lt_of_lt_of_le h2 _), apply le_max_left, apply le_max_right end | (¬* p) h1 := begin apply not_iff_not_of_iff, apply holds_constant p h1 end | (p ∨* q) h1 := begin simp only [holds], apply pred_mono_2'; apply holds_constant; intros x h2; apply h1 _ (lt_of_lt_of_le h2 _), apply le_max_left, apply le_max_right end | (p ∧* q) h1 := begin simp only [holds], apply pred_mono_2'; apply holds_constant; intros x h2; apply h1 _ (lt_of_lt_of_le h2 _), apply le_max_left, apply le_max_right end /-- All valuations satisfy argument -/ def valid (p : preform) : Prop := ∀ v, holds v p /-- There exists some valuation that satisfies argument -/ def sat (p : preform) : Prop := ∃ v, holds v p /-- `implies p q` := under any valuation, `q` holds if `p` holds -/ def implies (p q : preform) : Prop := ∀ v, (holds v p → holds v q) /-- `equiv p q` := under any valuation, `p` holds iff `q` holds -/ def equiv (p q : preform) : Prop := ∀ v, (holds v p ↔ holds v q) lemma sat_of_implies_of_sat {p q : preform} : implies p q → sat p → sat q := begin intros h1 h2, apply exists_imp_exists h1 h2 end lemma sat_or {p q : preform} : sat (p ∨* q) ↔ sat p ∨ sat q := begin constructor; intro h1, { cases h1 with v h1, cases h1 with h1 h1; [left,right]; refine ⟨v,_⟩; assumption }, { cases h1 with h1 h1; cases h1 with v h1; refine ⟨v,_⟩; [left,right]; assumption } end /-- There does not exist any valuation that satisfies argument -/ def unsat (p : preform) : Prop := ¬ sat p def repr : preform → string | (t =* s) := "(" ++ t.repr ++ " = " ++ s.repr ++ ")" | (t ≤* s) := "(" ++ t.repr ++ " ≤ " ++ s.repr ++ ")" | (¬* p) := "¬" ++ p.repr | (p ∨* q) := "(" ++ p.repr ++ " ∨ " ++ q.repr ++ ")" | (p ∧* q) := "(" ++ p.repr ++ " ∧ " ++ q.repr ++ ")" instance has_repr : has_repr preform := ⟨repr⟩ meta instance has_to_format : has_to_format preform := ⟨λ x, x.repr⟩ end preform lemma univ_close_of_valid {p : preform} : ∀ {m : nat} {v : nat → nat}, p.valid → univ_close p v m | 0 v h1 := h1 _ | (m+1) v h1 := λ i, univ_close_of_valid h1 lemma valid_of_unsat_not {p : preform} : (¬*p).unsat → p.valid := begin simp only [preform.sat, preform.unsat, preform.valid, preform.holds], rw not_exists_not, intro h, assumption end /-- Tactic for setting up proof by induction over preforms. -/ meta def preform.induce (t : tactic unit := tactic.skip) : tactic unit := `[ intro p, induction p with t s t s p ih p q ihp ihq p q ihp ihq; t ] end nat end omega
76de8ee49c523d8058005ed5afaa173ed1724334
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/limits/opposites.lean
8c0e25b9d42b078c9e75dda19176c7553a404e12
[ "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
21,869
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Floris van Doorn -/ import category_theory.limits.shapes.finite_products import category_theory.discrete_category import tactic.equiv_rw /-! # Limits in `C` give colimits in `Cᵒᵖ`. We also give special cases for (co)products, (co)equalizers, and pullbacks / pushouts. -/ universes v₁ v₂ u₁ u₂ noncomputable theory open category_theory open category_theory.functor open opposite namespace category_theory.limits variables {C : Type u₁} [category.{v₁} C] variables {J : Type u₂} [category.{v₂} J] /-- Turn a colimit for `F : J ⥤ C` into a limit for `F.op : Jᵒᵖ ⥤ Cᵒᵖ`. -/ @[simps] def is_limit_cocone_op (F : J ⥤ C) {c : cocone F} (hc : is_colimit c) : is_limit c.op := { lift := λ s, (hc.desc s.unop).op, fac' := λ s j, quiver.hom.unop_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.unop_inj (hc.hom_ext (λ j, quiver.hom.op_inj _)), simpa only [quiver.hom.unop_op, is_colimit.fac] using w (op j) end } /-- Turn a limit for `F : J ⥤ C` into a colimit for `F.op : Jᵒᵖ ⥤ Cᵒᵖ`. -/ @[simps] def is_colimit_cone_op (F : J ⥤ C) {c : cone F} (hc : is_limit c) : is_colimit c.op := { desc := λ s, (hc.lift s.unop).op, fac' := λ s j, quiver.hom.unop_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.unop_inj (hc.hom_ext (λ j, quiver.hom.op_inj _)), simpa only [quiver.hom.unop_op, is_limit.fac] using w (op j) end } /-- Turn a colimit for `F : J ⥤ Cᵒᵖ` into a limit for `F.left_op : Jᵒᵖ ⥤ C`. -/ @[simps] def is_limit_cone_left_op_of_cocone (F : J ⥤ Cᵒᵖ) {c : cocone F} (hc : is_colimit c) : is_limit (cone_left_op_of_cocone c) := { lift := λ s, (hc.desc (cocone_of_cone_left_op s)).unop, fac' := λ s j, quiver.hom.op_inj $ by simpa only [cone_left_op_of_cocone_π_app, op_comp, quiver.hom.op_unop, is_colimit.fac, cocone_of_cone_left_op_ι_app], uniq' := λ s m w, begin refine quiver.hom.op_inj (hc.hom_ext (λ j, quiver.hom.unop_inj _)), simpa only [quiver.hom.op_unop, is_colimit.fac, cocone_of_cone_left_op_ι_app] using w (op j) end } /-- Turn a limit of `F : J ⥤ Cᵒᵖ` into a colimit of `F.left_op : Jᵒᵖ ⥤ C`. -/ @[simps] def is_colimit_cocone_left_op_of_cone (F : J ⥤ Cᵒᵖ) {c : cone F} (hc : is_limit c) : is_colimit (cocone_left_op_of_cone c) := { desc := λ s, (hc.lift (cone_of_cocone_left_op s)).unop, fac' := λ s j, quiver.hom.op_inj $ by simpa only [cocone_left_op_of_cone_ι_app, op_comp, quiver.hom.op_unop, is_limit.fac, cone_of_cocone_left_op_π_app], uniq' := λ s m w, begin refine quiver.hom.op_inj (hc.hom_ext (λ j, quiver.hom.unop_inj _)), simpa only [quiver.hom.op_unop, is_limit.fac, cone_of_cocone_left_op_π_app] using w (op j) end } /-- Turn a colimit for `F : Jᵒᵖ ⥤ C` into a limit for `F.right_op : J ⥤ Cᵒᵖ`. -/ @[simps] def is_limit_cone_right_op_of_cocone (F : Jᵒᵖ ⥤ C) {c : cocone F} (hc : is_colimit c) : is_limit (cone_right_op_of_cocone c) := { lift := λ s, (hc.desc (cocone_of_cone_right_op s)).op, fac' := λ s j, quiver.hom.unop_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.unop_inj (hc.hom_ext (λ j, quiver.hom.op_inj _)), simpa only [quiver.hom.unop_op, is_colimit.fac] using w (unop j) end } /-- Turn a limit for `F : Jᵒᵖ ⥤ C` into a colimit for `F.right_op : J ⥤ Cᵒᵖ`. -/ @[simps] def is_colimit_cocone_right_op_of_cone (F : Jᵒᵖ ⥤ C) {c : cone F} (hc : is_limit c) : is_colimit (cocone_right_op_of_cone c) := { desc := λ s, (hc.lift (cone_of_cocone_right_op s)).op, fac' := λ s j, quiver.hom.unop_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.unop_inj (hc.hom_ext (λ j, quiver.hom.op_inj _)), simpa only [quiver.hom.unop_op, is_limit.fac] using w (unop j) end } /-- Turn a colimit for `F : Jᵒᵖ ⥤ Cᵒᵖ` into a limit for `F.unop : J ⥤ C`. -/ @[simps] def is_limit_cone_unop_of_cocone (F : Jᵒᵖ ⥤ Cᵒᵖ) {c : cocone F} (hc : is_colimit c) : is_limit (cone_unop_of_cocone c) := { lift := λ s, (hc.desc (cocone_of_cone_unop s)).unop, fac' := λ s j, quiver.hom.op_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.op_inj (hc.hom_ext (λ j, quiver.hom.unop_inj _)), simpa only [quiver.hom.op_unop, is_colimit.fac] using w (unop j) end } /-- Turn a limit of `F : Jᵒᵖ ⥤ Cᵒᵖ` into a colimit of `F.unop : J ⥤ C`. -/ @[simps] def is_colimit_cocone_unop_of_cone (F : Jᵒᵖ ⥤ Cᵒᵖ) {c : cone F} (hc : is_limit c) : is_colimit (cocone_unop_of_cone c) := { desc := λ s, (hc.lift (cone_of_cocone_unop s)).unop, fac' := λ s j, quiver.hom.op_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.op_inj (hc.hom_ext (λ j, quiver.hom.unop_inj _)), simpa only [quiver.hom.op_unop, is_limit.fac] using w (unop j) end } /-- Turn a colimit for `F.op : Jᵒᵖ ⥤ Cᵒᵖ` into a limit for `F : J ⥤ C`. -/ @[simps] def is_limit_cocone_unop (F : J ⥤ C) {c : cocone F.op} (hc : is_colimit c) : is_limit c.unop := { lift := λ s, (hc.desc s.op).unop, fac' := λ s j, quiver.hom.op_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.op_inj (hc.hom_ext (λ j, quiver.hom.unop_inj _)), simpa only [quiver.hom.op_unop, is_colimit.fac] using w (unop j) end } /-- Turn a limit for `F.op : Jᵒᵖ ⥤ Cᵒᵖ` into a colimit for `F : J ⥤ C`. -/ @[simps] def is_colimit_cone_unop (F : J ⥤ C) {c : cone F.op} (hc : is_limit c) : is_colimit c.unop := { desc := λ s, (hc.lift s.op).unop, fac' := λ s j, quiver.hom.op_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.op_inj (hc.hom_ext (λ j, quiver.hom.unop_inj _)), simpa only [quiver.hom.op_unop, is_limit.fac] using w (unop j) end } /-- Turn a colimit for `F.left_op : Jᵒᵖ ⥤ C` into a limit for `F : J ⥤ Cᵒᵖ`. -/ @[simps] def is_limit_cone_of_cocone_left_op (F : J ⥤ Cᵒᵖ) {c : cocone F.left_op} (hc : is_colimit c) : is_limit (cone_of_cocone_left_op c) := { lift := λ s, (hc.desc (cocone_left_op_of_cone s)).op, fac' := λ s j, quiver.hom.unop_inj $ by simpa only [cone_of_cocone_left_op_π_app, unop_comp, quiver.hom.unop_op, is_colimit.fac, cocone_left_op_of_cone_ι_app], uniq' := λ s m w, begin refine quiver.hom.unop_inj (hc.hom_ext (λ j, quiver.hom.op_inj _)), simpa only [quiver.hom.unop_op, is_colimit.fac, cone_of_cocone_left_op_π_app] using w (unop j) end } /-- Turn a limit of `F.left_op : Jᵒᵖ ⥤ C` into a colimit of `F : J ⥤ Cᵒᵖ`. -/ @[simps] def is_colimit_cocone_of_cone_left_op (F : J ⥤ Cᵒᵖ) {c : cone (F.left_op)} (hc : is_limit c) : is_colimit (cocone_of_cone_left_op c) := { desc := λ s, (hc.lift (cone_left_op_of_cocone s)).op, fac' := λ s j, quiver.hom.unop_inj $ by simpa only [cocone_of_cone_left_op_ι_app, unop_comp, quiver.hom.unop_op, is_limit.fac, cone_left_op_of_cocone_π_app], uniq' := λ s m w, begin refine quiver.hom.unop_inj (hc.hom_ext (λ j, quiver.hom.op_inj _)), simpa only [quiver.hom.unop_op, is_limit.fac, cocone_of_cone_left_op_ι_app] using w (unop j) end } /-- Turn a colimit for `F.right_op : J ⥤ Cᵒᵖ` into a limit for `F : Jᵒᵖ ⥤ C`. -/ @[simps] def is_limit_cone_of_cocone_right_op (F : Jᵒᵖ ⥤ C) {c : cocone F.right_op} (hc : is_colimit c) : is_limit (cone_of_cocone_right_op c) := { lift := λ s, (hc.desc (cocone_right_op_of_cone s)).unop, fac' := λ s j, quiver.hom.op_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.op_inj (hc.hom_ext (λ j, quiver.hom.unop_inj _)), simpa only [quiver.hom.op_unop, is_colimit.fac] using w (op j) end } /-- Turn a limit for `F.right_op : J ⥤ Cᵒᵖ` into a limit for `F : Jᵒᵖ ⥤ C`. -/ @[simps] def is_colimit_cocone_of_cone_right_op (F : Jᵒᵖ ⥤ C) {c : cone F.right_op} (hc : is_limit c) : is_colimit (cocone_of_cone_right_op c) := { desc := λ s, (hc.lift (cone_right_op_of_cocone s)).unop, fac' := λ s j, quiver.hom.op_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.op_inj (hc.hom_ext (λ j, quiver.hom.unop_inj _)), simpa only [quiver.hom.op_unop, is_limit.fac] using w (op j) end } /-- Turn a colimit for `F.unop : J ⥤ C` into a limit for `F : Jᵒᵖ ⥤ Cᵒᵖ`. -/ @[simps] def is_limit_cone_of_cocone_unop (F : Jᵒᵖ ⥤ Cᵒᵖ) {c : cocone F.unop} (hc : is_colimit c) : is_limit (cone_of_cocone_unop c) := { lift := λ s, (hc.desc (cocone_unop_of_cone s)).op, fac' := λ s j, quiver.hom.unop_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.unop_inj (hc.hom_ext (λ j, quiver.hom.op_inj _)), simpa only [quiver.hom.unop_op, is_colimit.fac] using w (op j) end } /-- Turn a limit for `F.unop : J ⥤ C` into a colimit for `F : Jᵒᵖ ⥤ Cᵒᵖ`. -/ @[simps] def is_colimit_cone_of_cocone_unop (F : Jᵒᵖ ⥤ Cᵒᵖ) {c : cone F.unop} (hc : is_limit c) : is_colimit (cocone_of_cone_unop c) := { desc := λ s, (hc.lift (cone_unop_of_cocone s)).op, fac' := λ s j, quiver.hom.unop_inj (by simpa), uniq' := λ s m w, begin refine quiver.hom.unop_inj (hc.hom_ext (λ j, quiver.hom.op_inj _)), simpa only [quiver.hom.unop_op, is_limit.fac] using w (op j) end } /-- If `F.left_op : Jᵒᵖ ⥤ C` has a colimit, we can construct a limit for `F : J ⥤ Cᵒᵖ`. -/ lemma has_limit_of_has_colimit_left_op (F : J ⥤ Cᵒᵖ) [has_colimit F.left_op] : has_limit F := has_limit.mk { cone := cone_of_cocone_left_op (colimit.cocone F.left_op), is_limit := is_limit_cone_of_cocone_left_op _ (colimit.is_colimit _) } /-- If `C` has colimits of shape `Jᵒᵖ`, we can construct limits in `Cᵒᵖ` of shape `J`. -/ lemma has_limits_of_shape_op_of_has_colimits_of_shape [has_colimits_of_shape Jᵒᵖ C] : has_limits_of_shape J Cᵒᵖ := { has_limit := λ F, has_limit_of_has_colimit_left_op F } local attribute [instance] has_limits_of_shape_op_of_has_colimits_of_shape /-- If `C` has colimits, we can construct limits for `Cᵒᵖ`. -/ lemma has_limits_op_of_has_colimits [has_colimits C] : has_limits Cᵒᵖ := ⟨infer_instance⟩ /-- If `F.left_op : Jᵒᵖ ⥤ C` has a limit, we can construct a colimit for `F : J ⥤ Cᵒᵖ`. -/ lemma has_colimit_of_has_limit_left_op (F : J ⥤ Cᵒᵖ) [has_limit F.left_op] : has_colimit F := has_colimit.mk { cocone := cocone_of_cone_left_op (limit.cone F.left_op), is_colimit := is_colimit_cocone_of_cone_left_op _ (limit.is_limit _) } /-- If `C` has colimits of shape `Jᵒᵖ`, we can construct limits in `Cᵒᵖ` of shape `J`. -/ lemma has_colimits_of_shape_op_of_has_limits_of_shape [has_limits_of_shape Jᵒᵖ C] : has_colimits_of_shape J Cᵒᵖ := { has_colimit := λ F, has_colimit_of_has_limit_left_op F } local attribute [instance] has_colimits_of_shape_op_of_has_limits_of_shape /-- If `C` has limits, we can construct colimits for `Cᵒᵖ`. -/ lemma has_colimits_op_of_has_limits [has_limits C] : has_colimits Cᵒᵖ := ⟨infer_instance⟩ variables (X : Type v₁) /-- If `C` has products indexed by `X`, then `Cᵒᵖ` has coproducts indexed by `X`. -/ lemma has_coproducts_opposite [has_products_of_shape X C] : has_coproducts_of_shape X Cᵒᵖ := begin haveI : has_limits_of_shape (discrete X)ᵒᵖ C := has_limits_of_shape_of_equivalence (discrete.opposite X).symm, apply_instance end /-- If `C` has coproducts indexed by `X`, then `Cᵒᵖ` has products indexed by `X`. -/ lemma has_products_opposite [has_coproducts_of_shape X C] : has_products_of_shape X Cᵒᵖ := begin haveI : has_colimits_of_shape (discrete X)ᵒᵖ C := has_colimits_of_shape_of_equivalence (discrete.opposite X).symm, apply_instance end lemma has_finite_coproducts_opposite [has_finite_products C] : has_finite_coproducts Cᵒᵖ := { out := λ J 𝒟, begin resetI, haveI : has_limits_of_shape (discrete J)ᵒᵖ C := has_limits_of_shape_of_equivalence (discrete.opposite J).symm, apply_instance, end } lemma has_finite_products_opposite [has_finite_coproducts C] : has_finite_products Cᵒᵖ := { out := λ J 𝒟, begin resetI, haveI : has_colimits_of_shape (discrete J)ᵒᵖ C := has_colimits_of_shape_of_equivalence (discrete.opposite J).symm, apply_instance, end } lemma has_equalizers_opposite [has_coequalizers C] : has_equalizers Cᵒᵖ := begin haveI : has_colimits_of_shape walking_parallel_pairᵒᵖ C := has_colimits_of_shape_of_equivalence walking_parallel_pair_op_equiv, apply_instance end lemma has_coequalizers_opposite [has_equalizers C] : has_coequalizers Cᵒᵖ := begin haveI : has_limits_of_shape walking_parallel_pairᵒᵖ C := has_limits_of_shape_of_equivalence walking_parallel_pair_op_equiv, apply_instance end lemma has_finite_colimits_opposite [has_finite_limits C] : has_finite_colimits Cᵒᵖ := { out := λ J 𝒟 𝒥, by { resetI, apply_instance, }, } lemma has_finite_limits_opposite [has_finite_colimits C] : has_finite_limits Cᵒᵖ := { out := λ J 𝒟 𝒥, by { resetI, apply_instance, }, } lemma has_pullbacks_opposite [has_pushouts C] : has_pullbacks Cᵒᵖ := begin haveI : has_colimits_of_shape walking_cospanᵒᵖ C := has_colimits_of_shape_of_equivalence walking_cospan_op_equiv.symm, apply has_limits_of_shape_op_of_has_colimits_of_shape, end lemma has_pushouts_opposite [has_pullbacks C] : has_pushouts Cᵒᵖ := begin haveI : has_limits_of_shape walking_spanᵒᵖ C := has_limits_of_shape_of_equivalence walking_span_op_equiv.symm, apply has_colimits_of_shape_op_of_has_limits_of_shape, end /-- The canonical isomorphism relating `span f.op g.op` and `(cospan f g).op` -/ @[simps] def span_op {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : span f.op g.op ≅ walking_cospan_op_equiv.inverse ⋙ (cospan f g).op := nat_iso.of_components (by { rintro (_|_|_); refl, }) (by { rintros (_|_|_) (_|_|_) f; cases f; tidy, }) /-- The canonical isomorphism relating `(cospan f g).op` and `span f.op g.op` -/ @[simps] def op_cospan {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : (cospan f g).op ≅ walking_cospan_op_equiv.functor ⋙ span f.op g.op := calc (cospan f g).op ≅ 𝟭 _ ⋙ (cospan f g).op : by refl ... ≅ (walking_cospan_op_equiv.functor ⋙ walking_cospan_op_equiv.inverse) ⋙ (cospan f g).op : iso_whisker_right walking_cospan_op_equiv.unit_iso _ ... ≅ walking_cospan_op_equiv.functor ⋙ (walking_cospan_op_equiv.inverse ⋙ (cospan f g).op) : functor.associator _ _ _ ... ≅ walking_cospan_op_equiv.functor ⋙ span f.op g.op : iso_whisker_left _ (span_op f g).symm /-- The canonical isomorphism relating `cospan f.op g.op` and `(span f g).op` -/ @[simps] def cospan_op {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : cospan f.op g.op ≅ walking_span_op_equiv.inverse ⋙ (span f g).op := nat_iso.of_components (by { rintro (_|_|_); refl, }) (by { rintros (_|_|_) (_|_|_) f; cases f; tidy, }) /-- The canonical isomorphism relating `(span f g).op` and `cospan f.op g.op` -/ @[simps] def op_span {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) : (span f g).op ≅ walking_span_op_equiv.functor ⋙ cospan f.op g.op := calc (span f g).op ≅ 𝟭 _ ⋙ (span f g).op : by refl ... ≅ (walking_span_op_equiv.functor ⋙ walking_span_op_equiv.inverse) ⋙ (span f g).op : iso_whisker_right walking_span_op_equiv.unit_iso _ ... ≅ walking_span_op_equiv.functor ⋙ (walking_span_op_equiv.inverse ⋙ (span f g).op) : functor.associator _ _ _ ... ≅ walking_span_op_equiv.functor ⋙ cospan f.op g.op : iso_whisker_left _ (cospan_op f g).symm namespace pushout_cocone /-- The obvious map `pushout_cocone f g → pullback_cone f.unop g.unop` -/ @[simps] def unop {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : X ⟶ Z} (c : pushout_cocone f g) : pullback_cone f.unop g.unop := cocone.unop ((cocones.precompose (op_cospan f.unop g.unop).hom).obj (cocone.whisker walking_cospan_op_equiv.functor c)) @[simp] lemma unop_fst {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : X ⟶ Z} (c : pushout_cocone f g) : c.unop.fst = c.inl.unop := by { change (_ : limits.cone _).π.app _ = _, tidy, } @[simp] lemma unop_snd {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : X ⟶ Z} (c : pushout_cocone f g) : c.unop.snd = c.inr.unop := by { change (_ : limits.cone _).π.app _ = _, tidy, } /-- The obvious map `pushout_cocone f.op g.op → pullback_cone f g` -/ @[simps] def op {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} (c : pushout_cocone f g) : pullback_cone f.op g.op := (cones.postcompose ((cospan_op f g).symm).hom).obj (cone.whisker walking_span_op_equiv.inverse (cocone.op c)) @[simp] lemma op_fst {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} (c : pushout_cocone f g) : c.op.fst = c.inl.op := by { change (_ : limits.cone _).π.app _ = _, apply category.comp_id, } @[simp] lemma op_snd {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} (c : pushout_cocone f g) : c.op.snd = c.inr.op := by { change (_ : limits.cone _).π.app _ = _, apply category.comp_id, } end pushout_cocone namespace pullback_cone /-- The obvious map `pullback_cone f g → pushout_cocone f.unop g.unop` -/ @[simps] def unop {X Y Z : Cᵒᵖ} {f : X ⟶ Z} {g : Y ⟶ Z} (c : pullback_cone f g) : pushout_cocone f.unop g.unop := cone.unop ((cones.postcompose (op_span f.unop g.unop).symm.hom).obj (cone.whisker walking_span_op_equiv.functor c)) @[simp] lemma unop_inl {X Y Z : Cᵒᵖ} {f : X ⟶ Z} {g : Y ⟶ Z} (c : pullback_cone f g) : c.unop.inl = c.fst.unop := begin change ((_ : limits.cocone _).ι.app _) = _, dsimp only [unop, op_span], simp, dsimp, simp, dsimp, simp end @[simp] lemma unop_inr {X Y Z : Cᵒᵖ} {f : X ⟶ Z} {g : Y ⟶ Z} (c : pullback_cone f g) : c.unop.inr = c.snd.unop := begin change ((_ : limits.cocone _).ι.app _) = _, apply quiver.hom.op_inj, dsimp, simp, dsimp, simp, apply category.comp_id, end /-- The obvious map `pullback_cone f g → pushout_cocone f.op g.op` -/ @[simps] def op {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (c : pullback_cone f g) : pushout_cocone f.op g.op := (cocones.precompose (span_op f g).hom).obj (cocone.whisker walking_cospan_op_equiv.inverse (cone.op c)) @[simp] lemma op_inl {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (c : pullback_cone f g) : c.op.inl = c.fst.op := by { change (_ : limits.cocone _).ι.app _ = _, apply category.id_comp, } @[simp] lemma op_inr {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (c : pullback_cone f g) : c.op.inr = c.snd.op := by { change (_ : limits.cocone _).ι.app _ = _, apply category.id_comp, } /-- If `c` is a pullback cone, then `c.op.unop` is isomorphic to `c`. -/ def op_unop {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (c : pullback_cone f g) : c.op.unop ≅ c := pullback_cone.ext (iso.refl _) (by simp) (by simp) /-- If `c` is a pullback cone in `Cᵒᵖ`, then `c.unop.op` is isomorphic to `c`. -/ def unop_op {X Y Z : Cᵒᵖ} {f : X ⟶ Z} {g : Y ⟶ Z} (c : pullback_cone f g) : c.unop.op ≅ c := pullback_cone.ext (iso.refl _) (by simp) (by simp) end pullback_cone namespace pushout_cocone /-- If `c` is a pushout cocone, then `c.op.unop` is isomorphic to `c`. -/ def op_unop {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} (c : pushout_cocone f g) : c.op.unop ≅ c := pushout_cocone.ext (iso.refl _) (by simp) (by simp) /-- If `c` is a pushout cocone in `Cᵒᵖ`, then `c.unop.op` is isomorphic to `c`. -/ def unop_op {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : X ⟶ Z} (c : pushout_cocone f g) : c.unop.op ≅ c := pushout_cocone.ext (iso.refl _) (by simp) (by simp) /-- A pushout cone is a colimit cocone if and only if the corresponding pullback cone in the opposite category is a limit cone. -/ def is_colimit_equiv_is_limit_op {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} (c : pushout_cocone f g) : is_colimit c ≃ is_limit c.op := begin apply equiv_of_subsingleton_of_subsingleton, { intro h, equiv_rw is_limit.postcompose_hom_equiv _ _, equiv_rw (is_limit.whisker_equivalence_equiv walking_span_op_equiv.symm).symm, exact is_limit_cocone_op _ h, }, { intro h, equiv_rw is_colimit.equiv_iso_colimit c.op_unop.symm, apply is_colimit_cone_unop, equiv_rw is_limit.postcompose_hom_equiv _ _, equiv_rw (is_limit.whisker_equivalence_equiv _).symm, exact h, } end /-- A pushout cone is a colimit cocone in `Cᵒᵖ` if and only if the corresponding pullback cone in `C` is a limit cone. -/ def is_colimit_equiv_is_limit_unop {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : X ⟶ Z} (c : pushout_cocone f g) : is_colimit c ≃ is_limit c.unop := begin apply equiv_of_subsingleton_of_subsingleton, { intro h, apply is_limit_cocone_unop, equiv_rw is_colimit.precompose_hom_equiv _ _, equiv_rw (is_colimit.whisker_equivalence_equiv _).symm, exact h, }, { intro h, equiv_rw is_colimit.equiv_iso_colimit c.unop_op.symm, equiv_rw is_colimit.precompose_hom_equiv _ _, equiv_rw (is_colimit.whisker_equivalence_equiv walking_cospan_op_equiv.symm).symm, exact is_colimit_cone_op _ h, }, end end pushout_cocone namespace pullback_cone /-- A pullback cone is a limit cone if and only if the corresponding pushout cocone in the opposite category is a colimit cocone. -/ def is_limit_equiv_is_colimit_op {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (c : pullback_cone f g) : is_limit c ≃ is_colimit c.op := (is_limit.equiv_iso_limit c.op_unop).symm.trans c.op.is_colimit_equiv_is_limit_unop.symm /-- A pullback cone is a limit cone in `Cᵒᵖ` if and only if the corresponding pushout cocone in `C` is a colimit cocone. -/ def is_limit_equiv_is_colimit_unop {X Y Z : Cᵒᵖ} {f : X ⟶ Z} {g : Y ⟶ Z} (c : pullback_cone f g) : is_limit c ≃ is_colimit c.unop := (is_limit.equiv_iso_limit c.unop_op).symm.trans c.unop.is_colimit_equiv_is_limit_op.symm end pullback_cone end category_theory.limits
9f05a8a819699cd1b023214199cb04b65e4a6821
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/group_theory/free_group.lean
18153bb9dd8727523ec480462156f74b447d8bf6
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
32,883
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import data.fintype.basic import group_theory.subgroup.basic /-! # Free groups This file defines free groups over a type. Furthermore, it is shown that the free group construction is an instance of a monad. For the result that `free_group` is the left adjoint to the forgetful functor from groups to types, see `algebra/category/Group/adjunctions`. ## Main definitions * `free_group`: the free group associated to a type `α` defined as the words over `a : α × bool` modulo the relation `a * x * x⁻¹ * b = a * b`. * `free_group.mk`: the canonical quotient map `list (α × bool) → free_group α`. * `free_group.of`: the canoical injection `α → free_group α`. * `free_group.lift f`: the canonical group homomorphism `free_group α →* G` given a group `G` and a function `f : α → G`. ## Main statements * `free_group.church_rosser`: The Church-Rosser theorem for word reduction (also known as Newman's diamond lemma). * `free_group.free_group_unit_equiv_int`: The free group over the one-point type is isomorphic to the integers. * The free group construction is an instance of a monad. ## Implementation details First we introduce the one step reduction relation `free_group.red.step`: `w * x * x⁻¹ * v ~> w * v`, its reflexive transitive closure `free_group.red.trans` and prove that its join is an equivalence relation. Then we introduce `free_group α` as a quotient over `free_group.red.step`. ## Tags free group, Newman's diamond lemma, Church-Rosser theorem -/ open relation universes u v w variables {α : Type u} local attribute [simp] list.append_eq_has_append namespace free_group variables {L L₁ L₂ L₃ L₄ : list (α × bool)} /-- Reduction step: `w * x * x⁻¹ * v ~> w * v` -/ inductive red.step : list (α × bool) → list (α × bool) → Prop | bnot {L₁ L₂ x b} : red.step (L₁ ++ (x, b) :: (x, bnot b) :: L₂) (L₁ ++ L₂) attribute [simp] red.step.bnot /-- Reflexive-transitive closure of red.step -/ def red : list (α × bool) → list (α × bool) → Prop := refl_trans_gen red.step @[refl] lemma red.refl : red L L := refl_trans_gen.refl @[trans] lemma red.trans : red L₁ L₂ → red L₂ L₃ → red L₁ L₃ := refl_trans_gen.trans namespace red /-- Predicate asserting that word `w₁` can be reduced to `w₂` in one step, i.e. there are words `w₃ w₄` and letter `x` such that `w₁ = w₃xx⁻¹w₄` and `w₂ = w₃w₄` -/ theorem step.length : ∀ {L₁ L₂ : list (α × bool)}, step L₁ L₂ → L₂.length + 2 = L₁.length | _ _ (@red.step.bnot _ L1 L2 x b) := by rw [list.length_append, list.length_append]; refl @[simp] lemma step.bnot_rev {x b} : step (L₁ ++ (x, bnot b) :: (x, b) :: L₂) (L₁ ++ L₂) := by cases b; from step.bnot @[simp] lemma step.cons_bnot {x b} : red.step ((x, b) :: (x, bnot b) :: L) L := @step.bnot _ [] _ _ _ @[simp] lemma step.cons_bnot_rev {x b} : red.step ((x, bnot b) :: (x, b) :: L) L := @red.step.bnot_rev _ [] _ _ _ theorem step.append_left : ∀ {L₁ L₂ L₃ : list (α × bool)}, step L₂ L₃ → step (L₁ ++ L₂) (L₁ ++ L₃) | _ _ _ red.step.bnot := by rw [← list.append_assoc, ← list.append_assoc]; constructor theorem step.cons {x} (H : red.step L₁ L₂) : red.step (x :: L₁) (x :: L₂) := @step.append_left _ [x] _ _ H theorem step.append_right : ∀ {L₁ L₂ L₃ : list (α × bool)}, step L₁ L₂ → step (L₁ ++ L₃) (L₂ ++ L₃) | _ _ _ red.step.bnot := by simp lemma not_step_nil : ¬ step [] L := begin generalize h' : [] = L', assume h, cases h with L₁ L₂, simp [list.nil_eq_append_iff] at h', contradiction end lemma step.cons_left_iff {a : α} {b : bool} : step ((a, b) :: L₁) L₂ ↔ (∃L, step L₁ L ∧ L₂ = (a, b) :: L) ∨ (L₁ = (a, bnot b)::L₂) := begin split, { generalize hL : ((a, b) :: L₁ : list _) = L, assume h, rcases h with ⟨_ | ⟨p, s'⟩, e, a', b'⟩, { simp at hL, simp [*] }, { simp at hL, rcases hL with ⟨rfl, rfl⟩, refine or.inl ⟨s' ++ e, step.bnot, _⟩, simp } }, { assume h, rcases h with ⟨L, h, rfl⟩ | rfl, { exact step.cons h }, { exact step.cons_bnot } } end lemma not_step_singleton : ∀ {p : α × bool}, ¬ step [p] L | (a, b) := by simp [step.cons_left_iff, not_step_nil] lemma step.cons_cons_iff : ∀{p : α × bool}, step (p :: L₁) (p :: L₂) ↔ step L₁ L₂ := by simp [step.cons_left_iff, iff_def, or_imp_distrib] {contextual := tt} lemma step.append_left_iff : ∀L, step (L ++ L₁) (L ++ L₂) ↔ step L₁ L₂ | [] := by simp | (p :: l) := by simp [step.append_left_iff l, step.cons_cons_iff] private theorem step.diamond_aux : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)} {x1 b1 x2 b2}, L₁ ++ (x1, b1) :: (x1, bnot b1) :: L₂ = L₃ ++ (x2, b2) :: (x2, bnot b2) :: L₄ → L₁ ++ L₂ = L₃ ++ L₄ ∨ ∃ L₅, red.step (L₁ ++ L₂) L₅ ∧ red.step (L₃ ++ L₄) L₅ | [] _ [] _ _ _ _ _ H := by injections; subst_vars; simp | [] _ [(x3,b3)] _ _ _ _ _ H := by injections; subst_vars; simp | [(x3,b3)] _ [] _ _ _ _ _ H := by injections; subst_vars; simp | [] _ ((x3,b3)::(x4,b4)::tl) _ _ _ _ _ H := by injections; subst_vars; simp; right; exact ⟨_, red.step.bnot, red.step.cons_bnot⟩ | ((x3,b3)::(x4,b4)::tl) _ [] _ _ _ _ _ H := by injections; subst_vars; simp; right; exact ⟨_, red.step.cons_bnot, red.step.bnot⟩ | ((x3,b3)::tl) _ ((x4,b4)::tl2) _ _ _ _ _ H := let ⟨H1, H2⟩ := list.cons.inj H in match step.diamond_aux H2 with | or.inl H3 := or.inl $ by simp [H1, H3] | or.inr ⟨L₅, H3, H4⟩ := or.inr ⟨_, step.cons H3, by simpa [H1] using step.cons H4⟩ end theorem step.diamond : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)}, red.step L₁ L₃ → red.step L₂ L₄ → L₁ = L₂ → L₃ = L₄ ∨ ∃ L₅, red.step L₃ L₅ ∧ red.step L₄ L₅ | _ _ _ _ red.step.bnot red.step.bnot H := step.diamond_aux H lemma step.to_red : step L₁ L₂ → red L₁ L₂ := refl_trans_gen.single /-- **Church-Rosser theorem** for word reduction: If `w1 w2 w3` are words such that `w1` reduces to `w2` and `w3` respectively, then there is a word `w4` such that `w2` and `w3` reduce to `w4` respectively. This is also known as Newman's diamond lemma. -/ theorem church_rosser : red L₁ L₂ → red L₁ L₃ → join red L₂ L₃ := relation.church_rosser (assume a b c hab hac, match b, c, red.step.diamond hab hac rfl with | b, _, or.inl rfl := ⟨b, by refl, by refl⟩ | b, c, or.inr ⟨d, hbd, hcd⟩ := ⟨d, refl_gen.single hbd, hcd.to_red⟩ end) lemma cons_cons {p} : red L₁ L₂ → red (p :: L₁) (p :: L₂) := refl_trans_gen.lift (list.cons p) (assume a b, step.cons) lemma cons_cons_iff (p) : red (p :: L₁) (p :: L₂) ↔ red L₁ L₂ := iff.intro begin generalize eq₁ : (p :: L₁ : list _) = LL₁, generalize eq₂ : (p :: L₂ : list _) = LL₂, assume h, induction h using relation.refl_trans_gen.head_induction_on with L₁ L₂ h₁₂ h ih generalizing L₁ L₂, { subst_vars, cases eq₂, constructor }, { subst_vars, cases p with a b, rw [step.cons_left_iff] at h₁₂, rcases h₁₂ with ⟨L, h₁₂, rfl⟩ | rfl, { exact (ih rfl rfl).head h₁₂ }, { exact (cons_cons h).tail step.cons_bnot_rev } } end cons_cons lemma append_append_left_iff : ∀L, red (L ++ L₁) (L ++ L₂) ↔ red L₁ L₂ | [] := iff.rfl | (p :: L) := by simp [append_append_left_iff L, cons_cons_iff] lemma append_append (h₁ : red L₁ L₃) (h₂ : red L₂ L₄) : red (L₁ ++ L₂) (L₃ ++ L₄) := (h₁.lift (λL, L ++ L₂) (assume a b, step.append_right)).trans ((append_append_left_iff _).2 h₂) lemma to_append_iff : red L (L₁ ++ L₂) ↔ (∃L₃ L₄, L = L₃ ++ L₄ ∧ red L₃ L₁ ∧ red L₄ L₂) := iff.intro begin generalize eq : L₁ ++ L₂ = L₁₂, assume h, induction h with L' L₁₂ hLL' h ih generalizing L₁ L₂, { exact ⟨_, _, eq.symm, by refl, by refl⟩ }, { cases h with s e a b, rcases list.append_eq_append_iff.1 eq with ⟨s', rfl, rfl⟩ | ⟨e', rfl, rfl⟩, { have : L₁ ++ (s' ++ ((a, b) :: (a, bnot b) :: e)) = (L₁ ++ s') ++ ((a, b) :: (a, bnot b) :: e), { simp }, rcases ih this with ⟨w₁, w₂, rfl, h₁, h₂⟩, exact ⟨w₁, w₂, rfl, h₁, h₂.tail step.bnot⟩ }, { have : (s ++ ((a, b) :: (a, bnot b) :: e')) ++ L₂ = s ++ ((a, b) :: (a, bnot b) :: (e' ++ L₂)), { simp }, rcases ih this with ⟨w₁, w₂, rfl, h₁, h₂⟩, exact ⟨w₁, w₂, rfl, h₁.tail step.bnot, h₂⟩ }, } end (assume ⟨L₃, L₄, eq, h₃, h₄⟩, eq.symm ▸ append_append h₃ h₄) /-- The empty word `[]` only reduces to itself. -/ theorem nil_iff : red [] L ↔ L = [] := refl_trans_gen_iff_eq (assume l, red.not_step_nil) /-- A letter only reduces to itself. -/ theorem singleton_iff {x} : red [x] L₁ ↔ L₁ = [x] := refl_trans_gen_iff_eq (assume l, not_step_singleton) /-- If `x` is a letter and `w` is a word such that `xw` reduces to the empty word, then `w` reduces to `x⁻¹` -/ theorem cons_nil_iff_singleton {x b} : red ((x, b) :: L) [] ↔ red L [(x, bnot b)] := iff.intro (assume h, have h₁ : red ((x, bnot b) :: (x, b) :: L) [(x, bnot b)], from cons_cons h, have h₂ : red ((x, bnot b) :: (x, b) :: L) L, from refl_trans_gen.single step.cons_bnot_rev, let ⟨L', h₁, h₂⟩ := church_rosser h₁ h₂ in by rw [singleton_iff] at h₁; subst L'; assumption) (assume h, (cons_cons h).tail step.cons_bnot) theorem red_iff_irreducible {x1 b1 x2 b2} (h : (x1, b1) ≠ (x2, b2)) : red [(x1, bnot b1), (x2, b2)] L ↔ L = [(x1, bnot b1), (x2, b2)] := begin apply refl_trans_gen_iff_eq, generalize eq : [(x1, bnot b1), (x2, b2)] = L', assume L h', cases h', simp [list.cons_eq_append_iff, list.nil_eq_append_iff] at eq, rcases eq with ⟨rfl, ⟨rfl, rfl⟩, ⟨rfl, rfl⟩, rfl⟩, subst_vars, simp at h, contradiction end /-- If `x` and `y` are distinct letters and `w₁ w₂` are words such that `xw₁` reduces to `yw₂`, then `w₁` reduces to `x⁻¹yw₂`. -/ theorem inv_of_red_of_ne {x1 b1 x2 b2} (H1 : (x1, b1) ≠ (x2, b2)) (H2 : red ((x1, b1) :: L₁) ((x2, b2) :: L₂)) : red L₁ ((x1, bnot b1) :: (x2, b2) :: L₂) := begin have : red ((x1, b1) :: L₁) ([(x2, b2)] ++ L₂), from H2, rcases to_append_iff.1 this with ⟨_ | ⟨p, L₃⟩, L₄, eq, h₁, h₂⟩, { simp [nil_iff] at h₁, contradiction }, { cases eq, show red (L₃ ++ L₄) ([(x1, bnot b1), (x2, b2)] ++ L₂), apply append_append _ h₂, have h₁ : red ((x1, bnot b1) :: (x1, b1) :: L₃) [(x1, bnot b1), (x2, b2)], { exact cons_cons h₁ }, have h₂ : red ((x1, bnot b1) :: (x1, b1) :: L₃) L₃, { exact step.cons_bnot_rev.to_red }, rcases church_rosser h₁ h₂ with ⟨L', h₁, h₂⟩, rw [red_iff_irreducible H1] at h₁, rwa [h₁] at h₂ } end theorem step.sublist (H : red.step L₁ L₂) : L₂ <+ L₁ := by cases H; simp; constructor; constructor; refl /-- If `w₁ w₂` are words such that `w₁` reduces to `w₂`, then `w₂` is a sublist of `w₁`. -/ theorem sublist : red L₁ L₂ → L₂ <+ L₁ := refl_trans_gen_of_transitive_reflexive (λl, list.sublist.refl l) (λa b c hab hbc, list.sublist.trans hbc hab) (λa b, red.step.sublist) theorem sizeof_of_step : ∀ {L₁ L₂ : list (α × bool)}, step L₁ L₂ → L₂.sizeof < L₁.sizeof | _ _ (@step.bnot _ L1 L2 x b) := begin induction L1 with hd tl ih, case list.nil { dsimp [list.sizeof], have H : 1 + sizeof (x, b) + (1 + sizeof (x, bnot b) + list.sizeof L2) = (list.sizeof L2 + 1) + (sizeof (x, b) + sizeof (x, bnot b) + 1), { ac_refl }, rw H, exact nat.le_add_right _ _ }, case list.cons { dsimp [list.sizeof], exact nat.add_lt_add_left ih _ } end theorem length (h : red L₁ L₂) : ∃ n, L₁.length = L₂.length + 2 * n := begin induction h with L₂ L₃ h₁₂ h₂₃ ih, { exact ⟨0, rfl⟩ }, { rcases ih with ⟨n, eq⟩, existsi (1 + n), simp [mul_add, eq, (step.length h₂₃).symm, add_assoc] } end theorem antisymm (h₁₂ : red L₁ L₂) : red L₂ L₁ → L₁ = L₂ := match L₁, h₁₂.cases_head with | _, or.inl rfl := assume h, rfl | L₁, or.inr ⟨L₃, h₁₃, h₃₂⟩ := assume h₂₁, let ⟨n, eq⟩ := length (h₃₂.trans h₂₁) in have list.length L₃ + 0 = list.length L₃ + (2 * n + 2), by simpa [(step.length h₁₃).symm, add_comm, add_assoc] using eq, (nat.no_confusion $ nat.add_left_cancel this) end end red theorem equivalence_join_red : equivalence (join (@red α)) := equivalence_join_refl_trans_gen $ assume a b c hab hac, (match b, c, red.step.diamond hab hac rfl with | b, _, or.inl rfl := ⟨b, by refl, by refl⟩ | b, c, or.inr ⟨d, hbd, hcd⟩ := ⟨d, refl_gen.single hbd, refl_trans_gen.single hcd⟩ end) theorem join_red_of_step (h : red.step L₁ L₂) : join red L₁ L₂ := join_of_single reflexive_refl_trans_gen h.to_red theorem eqv_gen_step_iff_join_red : eqv_gen red.step L₁ L₂ ↔ join red L₁ L₂ := iff.intro (assume h, have eqv_gen (join red) L₁ L₂ := h.mono (assume a b, join_red_of_step), equivalence_join_red.eqv_gen_iff.1 this) (join_of_equivalence (eqv_gen.is_equivalence _) $ assume a b, refl_trans_gen_of_equivalence (eqv_gen.is_equivalence _) eqv_gen.rel) end free_group /-- The free group over a type, i.e. the words formed by the elements of the type and their formal inverses, quotient by one step reduction. -/ def free_group (α : Type u) : Type u := quot $ @free_group.red.step α namespace free_group variables {α} {L L₁ L₂ L₃ L₄ : list (α × bool)} /-- The canonical map from `list (α × bool)` to the free group on `α`. -/ def mk (L) : free_group α := quot.mk red.step L @[simp] lemma quot_mk_eq_mk : quot.mk red.step L = mk L := rfl @[simp] lemma quot_lift_mk (β : Type v) (f : list (α × bool) → β) (H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) : quot.lift f H (mk L) = f L := rfl @[simp] lemma quot_lift_on_mk (β : Type v) (f : list (α × bool) → β) (H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) : quot.lift_on (mk L) f H = f L := rfl @[simp] lemma quot_map_mk (β : Type v) (f : list (α × bool) → list (β × bool)) (H : (red.step ⇒ red.step) f f) : quot.map f H (mk L) = mk (f L) := rfl instance : has_one (free_group α) := ⟨mk []⟩ lemma one_eq_mk : (1 : free_group α) = mk [] := rfl instance : inhabited (free_group α) := ⟨1⟩ instance : has_mul (free_group α) := ⟨λ x y, quot.lift_on x (λ L₁, quot.lift_on y (λ L₂, mk $ L₁ ++ L₂) (λ L₂ L₃ H, quot.sound $ red.step.append_left H)) (λ L₁ L₂ H, quot.induction_on y $ λ L₃, quot.sound $ red.step.append_right H)⟩ @[simp] lemma mul_mk : mk L₁ * mk L₂ = mk (L₁ ++ L₂) := rfl instance : has_inv (free_group α) := ⟨λx, quot.lift_on x (λ L, mk (L.map $ λ x : α × bool, (x.1, bnot x.2)).reverse) (assume a b h, quot.sound $ by cases h; simp)⟩ @[simp] lemma inv_mk : (mk L)⁻¹ = mk (L.map $ λ x : α × bool, (x.1, bnot x.2)).reverse := rfl instance : group (free_group α) := { mul := (*), one := 1, inv := has_inv.inv, mul_assoc := by rintros ⟨L₁⟩ ⟨L₂⟩ ⟨L₃⟩; simp, one_mul := by rintros ⟨L⟩; refl, mul_one := by rintros ⟨L⟩; simp [one_eq_mk], mul_left_inv := by rintros ⟨L⟩; exact (list.rec_on L rfl $ λ ⟨x, b⟩ tl ih, eq.trans (quot.sound $ by simp [one_eq_mk]) ih) } /-- `of` is the canonical injection from the type to the free group over that type by sending each element to the equivalence class of the letter that is the element. -/ def of (x : α) : free_group α := mk [(x, tt)] theorem red.exact : mk L₁ = mk L₂ ↔ join red L₁ L₂ := calc (mk L₁ = mk L₂) ↔ eqv_gen red.step L₁ L₂ : iff.intro (quot.exact _) quot.eqv_gen_sound ... ↔ join red L₁ L₂ : eqv_gen_step_iff_join_red /-- The canonical injection from the type to the free group is an injection. -/ theorem of_injective : function.injective (@of α) := λ _ _ H, let ⟨L₁, hx, hy⟩ := red.exact.1 H in by simp [red.singleton_iff] at hx hy; cc section lift variables {β : Type v} [group β] (f : α → β) {x y : free_group α} /-- Given `f : α → β` with `β` a group, the canonical map `list (α × bool) → β` -/ def lift.aux : list (α × bool) → β := λ L, list.prod $ L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹ theorem red.step.lift {f : α → β} (H : red.step L₁ L₂) : lift.aux f L₁ = lift.aux f L₂ := by cases H with _ _ _ b; cases b; simp [lift.aux] /-- If `β` is a group, then any function from `α` to `β` extends uniquely to a group homomorphism from the free group over `α` to `β` -/ @[simps symm_apply] def lift : (α → β) ≃ (free_group α →* β) := { to_fun := λ f, monoid_hom.mk' (quot.lift (lift.aux f) $ λ L₁ L₂, red.step.lift) $ begin rintros ⟨L₁⟩ ⟨L₂⟩, simp [lift.aux], end, inv_fun := λ g, g ∘ of, left_inv := λ f, one_mul _, right_inv := λ g, monoid_hom.ext $ begin rintros ⟨L⟩, apply list.rec_on L, { exact g.map_one.symm, }, { rintros ⟨x, _ | _⟩ t (ih : _ = g (mk t)), { show _ = g ((of x)⁻¹ * mk t), simpa [lift.aux] using ih }, { show _ = g (of x * mk t), simpa [lift.aux] using ih }, }, end } variable {f} @[simp] lemma lift.mk : lift f (mk L) = list.prod (L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹) := rfl @[simp] lemma lift.of {x} : lift f (of x) = f x := one_mul _ theorem lift.unique (g : free_group α →* β) (hg : ∀ x, g (of x) = f x) : ∀{x}, g x = lift f x := monoid_hom.congr_fun $ (lift.symm_apply_eq).mp (funext hg : g ∘ of = f) /-- Two homomorphisms out of a free group are equal if they are equal on generators. See note [partially-applied ext lemmas]. -/ @[ext] lemma ext_hom {G : Type*} [group G] (f g : free_group α →* G) (h : ∀ a, f (of a) = g (of a)) : f = g := lift.symm.injective $ funext h theorem lift.of_eq (x : free_group α) : lift of x = x := monoid_hom.congr_fun (lift.apply_symm_apply (monoid_hom.id _)) x theorem lift.range_subset {s : subgroup β} (H : set.range f ⊆ s) : set.range (lift f) ⊆ s := by rintros _ ⟨⟨L⟩, rfl⟩; exact list.rec_on L s.one_mem (λ ⟨x, b⟩ tl ih, bool.rec_on b (by simp at ih ⊢; from s.mul_mem (s.inv_mem $ H ⟨x, rfl⟩) ih) (by simp at ih ⊢; from s.mul_mem (H ⟨x, rfl⟩) ih)) theorem closure_subset {G : Type*} [group G] {s : set G} {t : subgroup G} (h : s ⊆ t) : subgroup.closure s ≤ t := begin simp only [h, subgroup.closure_le], end theorem lift.range_eq_closure : set.range (lift f) = subgroup.closure (set.range f) := set.subset.antisymm (lift.range_subset subgroup.subset_closure) begin suffices : (subgroup.closure (set.range f)) ≤ monoid_hom.range (lift f), simpa, rw subgroup.closure_le, rintros y ⟨x, hx⟩, exact ⟨of x, by simpa⟩ end end lift section map variables {β : Type v} (f : α → β) {x y : free_group α} /-- Any function from `α` to `β` extends uniquely to a group homomorphism from the free group ver `α` to the free group over `β`. -/ def map : free_group α →* free_group β := monoid_hom.mk' (quot.map (list.map $ λ x, (f x.1, x.2)) $ λ L₁ L₂ H, by cases H; simp) (by { rintros ⟨L₁⟩ ⟨L₂⟩, simp }) variable {f} @[simp] lemma map.mk : map f (mk L) = mk (L.map (λ x, (f x.1, x.2))) := rfl @[simp] lemma map.id (x : free_group α) : map id x = x := by rcases x with ⟨L⟩; simp [list.map_id'] @[simp] lemma map.id' (x : free_group α) : map (λ z, z) x = x := map.id x theorem map.comp {γ : Type w} (f : α → β) (g : β → γ) (x) : map g (map f x) = map (g ∘ f) x := by rcases x with ⟨L⟩; simp @[simp] lemma map.of {x} : map f (of x) = of (f x) := rfl theorem map.unique (g : free_group α →* free_group β) (hg : ∀ x, g (of x) = of (f x)) : ∀{x}, g x = map f x := by rintros ⟨L⟩; exact list.rec_on L g.map_one (λ ⟨x, b⟩ t (ih : g (mk t) = map f (mk t)), bool.rec_on b (show g ((of x)⁻¹ * mk t) = map f ((of x)⁻¹ * mk t), by simp [g.map_mul, g.map_inv, hg, ih]) (show g (of x * mk t) = map f (of x * mk t), by simp [g.map_mul, hg, ih])) theorem map_eq_lift : map f x = lift (of ∘ f) x := eq.symm $ map.unique _ $ λ x, by simp /-- Equivalent types give rise to multiplicatively equivalent free groups. The converse can be found in `group_theory.free_abelian_group_finsupp`, as `equiv.of_free_group_equiv` -/ @[simps apply] def free_group_congr {α β} (e : α ≃ β) : free_group α ≃* free_group β := { to_fun := map e, inv_fun := map e.symm, left_inv := λ x, by simp [function.comp, map.comp], right_inv := λ x, by simp [function.comp, map.comp], map_mul' := monoid_hom.map_mul _ } @[simp] lemma free_group_congr_refl : free_group_congr (equiv.refl α) = mul_equiv.refl _ := mul_equiv.ext map.id @[simp] lemma free_group_congr_symm {α β} (e : α ≃ β) : (free_group_congr e).symm = free_group_congr e.symm := rfl lemma free_group_congr_trans {α β γ} (e : α ≃ β) (f : β ≃ γ) : (free_group_congr e).trans (free_group_congr f) = free_group_congr (e.trans f) := mul_equiv.ext $ map.comp _ _ end map section prod variables [group α] (x y : free_group α) /-- If `α` is a group, then any function from `α` to `α` extends uniquely to a homomorphism from the free group over `α` to `α`. This is the multiplicative version of `sum`. -/ def prod : free_group α →* α := lift id variables {x y} @[simp] lemma prod_mk : prod (mk L) = list.prod (L.map $ λ x, cond x.2 x.1 x.1⁻¹) := rfl @[simp] lemma prod.of {x : α} : prod (of x) = x := lift.of lemma prod.unique (g : free_group α →* α) (hg : ∀ x, g (of x) = x) {x} : g x = prod x := lift.unique g hg end prod theorem lift_eq_prod_map {β : Type v} [group β] {f : α → β} {x} : lift f x = prod (map f x) := begin rw ←lift.unique (prod.comp (map f)), { refl }, { simp } end section sum variables [add_group α] (x y : free_group α) /-- If `α` is a group, then any function from `α` to `α` extends uniquely to a homomorphism from the free group over `α` to `α`. This is the additive version of `prod`. -/ def sum : α := @prod (multiplicative _) _ x variables {x y} @[simp] lemma sum_mk : sum (mk L) = list.sum (L.map $ λ x, cond x.2 x.1 (-x.1)) := rfl @[simp] lemma sum.of {x : α} : sum (of x) = x := prod.of -- note: there are no bundled homs with different notation in the domain and codomain, so we copy -- these manually @[simp] lemma sum.map_mul : sum (x * y) = sum x + sum y := (@prod (multiplicative _) _).map_mul _ _ @[simp] lemma sum.map_one : sum (1:free_group α) = 0 := (@prod (multiplicative _) _).map_one @[simp] lemma sum.map_inv : sum x⁻¹ = -sum x := (@prod (multiplicative _) _).map_inv _ end sum /-- The bijection between the free group on the empty type, and a type with one element. -/ def free_group_empty_equiv_unit : free_group empty ≃ unit := { to_fun := λ _, (), inv_fun := λ _, 1, left_inv := by rintros ⟨_ | ⟨⟨⟨⟩, _⟩, _⟩⟩; refl, right_inv := λ ⟨⟩, rfl } /-- The bijection between the free group on a singleton, and the integers. -/ def free_group_unit_equiv_int : free_group unit ≃ ℤ := { to_fun := λ x, sum begin revert x, apply monoid_hom.to_fun, apply map (λ _, (1 : ℤ)), end, inv_fun := λ x, of () ^ x, left_inv := begin rintros ⟨L⟩, refine list.rec_on L rfl _, exact (λ ⟨⟨⟩, b⟩ tl ih, by cases b; simp [zpow_add] at ih ⊢; rw ih; refl), end, right_inv := λ x, int.induction_on x (by simp) (λ i ih, by simp at ih; simp [zpow_add, ih]) (λ i ih, by simp at ih; simp [zpow_add, ih, sub_eq_add_neg, -int.add_neg_one]) } section category variables {β : Type u} instance : monad free_group.{u} := { pure := λ α, of, map := λ α β f, (map f), bind := λ α β x f, lift f x } @[elab_as_eliminator] protected theorem induction_on {C : free_group α → Prop} (z : free_group α) (C1 : C 1) (Cp : ∀ x, C $ pure x) (Ci : ∀ x, C (pure x) → C (pure x)⁻¹) (Cm : ∀ x y, C x → C y → C (x * y)) : C z := quot.induction_on z $ λ L, list.rec_on L C1 $ λ ⟨x, b⟩ tl ih, bool.rec_on b (Cm _ _ (Ci _ $ Cp x) ih) (Cm _ _ (Cp x) ih) @[simp] lemma map_pure (f : α → β) (x : α) : f <$> (pure x : free_group α) = pure (f x) := map.of @[simp] lemma map_one (f : α → β) : f <$> (1 : free_group α) = 1 := (map f).map_one @[simp] lemma map_mul (f : α → β) (x y : free_group α) : f <$> (x * y) = f <$> x * f <$> y := (map f).map_mul x y @[simp] lemma map_inv (f : α → β) (x : free_group α) : f <$> (x⁻¹) = (f <$> x)⁻¹ := (map f).map_inv x @[simp] lemma pure_bind (f : α → free_group β) (x) : pure x >>= f = f x := lift.of @[simp] lemma one_bind (f : α → free_group β) : 1 >>= f = 1 := (lift f).map_one @[simp] lemma mul_bind (f : α → free_group β) (x y : free_group α) : x * y >>= f = (x >>= f) * (y >>= f) := (lift f).map_mul _ _ @[simp] lemma inv_bind (f : α → free_group β) (x : free_group α) : x⁻¹ >>= f = (x >>= f)⁻¹ := (lift f).map_inv _ instance : is_lawful_monad free_group.{u} := { id_map := λ α x, free_group.induction_on x (map_one id) (λ x, map_pure id x) (λ x ih, by rw [map_inv, ih]) (λ x y ihx ihy, by rw [map_mul, ihx, ihy]), pure_bind := λ α β x f, pure_bind f x, bind_assoc := λ α β γ x f g, free_group.induction_on x (by iterate 3 { rw one_bind }) (λ x, by iterate 2 { rw pure_bind }) (λ x ih, by iterate 3 { rw inv_bind }; rw ih) (λ x y ihx ihy, by iterate 3 { rw mul_bind }; rw [ihx, ihy]), bind_pure_comp_eq_map := λ α β f x, free_group.induction_on x (by rw [one_bind, map_one]) (λ x, by rw [pure_bind, map_pure]) (λ x ih, by rw [inv_bind, map_inv, ih]) (λ x y ihx ihy, by rw [mul_bind, map_mul, ihx, ihy]) } end category section reduce variable [decidable_eq α] /-- The maximal reduction of a word. It is computable iff `α` has decidable equality. -/ def reduce (L : list (α × bool)) : list (α × bool) := list.rec_on L [] $ λ hd1 tl1 ih, list.cases_on ih [hd1] $ λ hd2 tl2, if hd1.1 = hd2.1 ∧ hd1.2 = bnot hd2.2 then tl2 else hd1 :: hd2 :: tl2 @[simp] lemma reduce.cons (x) : reduce (x :: L) = list.cases_on (reduce L) [x] (λ hd tl, if x.1 = hd.1 ∧ x.2 = bnot hd.2 then tl else x :: hd :: tl) := rfl /-- The first theorem that characterises the function `reduce`: a word reduces to its maximal reduction. -/ theorem reduce.red : red L (reduce L) := begin induction L with hd1 tl1 ih, case list.nil { constructor }, case list.cons { dsimp, revert ih, generalize htl : reduce tl1 = TL, intro ih, cases TL with hd2 tl2, case list.nil { exact red.cons_cons ih }, case list.cons { dsimp, by_cases h : hd1.fst = hd2.fst ∧ hd1.snd = bnot (hd2.snd), { rw [if_pos h], transitivity, { exact red.cons_cons ih }, { cases hd1, cases hd2, cases h, dsimp at *, subst_vars, exact red.step.cons_bnot_rev.to_red } }, { rw [if_neg h], exact red.cons_cons ih } } } end theorem reduce.not {p : Prop} : ∀ {L₁ L₂ L₃ : list (α × bool)} {x b}, reduce L₁ = L₂ ++ (x, b) :: (x, bnot b) :: L₃ → p | [] L2 L3 _ _ := λ h, by cases L2; injections | ((x,b)::L1) L2 L3 x' b' := begin dsimp, cases r : reduce L1, { dsimp, intro h, have := congr_arg list.length h, simp [-add_comm] at this, exact absurd this dec_trivial }, cases hd with y c, by_cases x = y ∧ b = bnot c; simp [h]; intro H, { rw H at r, exact @reduce.not L1 ((y,c)::L2) L3 x' b' r }, rcases L2 with _|⟨a, L2⟩, { injections, subst_vars, simp at h, cc }, { refine @reduce.not L1 L2 L3 x' b' _, injection H with _ H, rw [r, H], refl } end /-- The second theorem that characterises the function `reduce`: the maximal reduction of a word only reduces to itself. -/ theorem reduce.min (H : red (reduce L₁) L₂) : reduce L₁ = L₂ := begin induction H with L1 L' L2 H1 H2 ih, { refl }, { cases H1 with L4 L5 x b, exact reduce.not H2 } end /-- `reduce` is idempotent, i.e. the maximal reduction of the maximal reduction of a word is the maximal reduction of the word. -/ theorem reduce.idem : reduce (reduce L) = reduce L := eq.symm $ reduce.min reduce.red theorem reduce.step.eq (H : red.step L₁ L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, HR13, HR23⟩ := red.church_rosser reduce.red (reduce.red.head H) in (reduce.min HR13).trans (reduce.min HR23).symm /-- If a word reduces to another word, then they have a common maximal reduction. -/ theorem reduce.eq_of_red (H : red L₁ L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, HR13, HR23⟩ := red.church_rosser reduce.red (red.trans H reduce.red) in (reduce.min HR13).trans (reduce.min HR23).symm /-- If two words correspond to the same element in the free group, then they have a common maximal reduction. This is the proof that the function that sends an element of the free group to its maximal reduction is well-defined. -/ theorem reduce.sound (H : mk L₁ = mk L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, H13, H23⟩ := red.exact.1 H in (reduce.eq_of_red H13).trans (reduce.eq_of_red H23).symm /-- If two words have a common maximal reduction, then they correspond to the same element in the free group. -/ theorem reduce.exact (H : reduce L₁ = reduce L₂) : mk L₁ = mk L₂ := red.exact.2 ⟨reduce L₂, H ▸ reduce.red, reduce.red⟩ /-- A word and its maximal reduction correspond to the same element of the free group. -/ theorem reduce.self : mk (reduce L) = mk L := reduce.exact reduce.idem /-- If words `w₁ w₂` are such that `w₁` reduces to `w₂`, then `w₂` reduces to the maximal reduction of `w₁`. -/ theorem reduce.rev (H : red L₁ L₂) : red L₂ (reduce L₁) := (reduce.eq_of_red H).symm ▸ reduce.red /-- The function that sends an element of the free group to its maximal reduction. -/ def to_word : free_group α → list (α × bool) := quot.lift reduce $ λ L₁ L₂ H, reduce.step.eq H lemma to_word.mk : ∀{x : free_group α}, mk (to_word x) = x := by rintros ⟨L⟩; exact reduce.self lemma to_word.inj : ∀(x y : free_group α), to_word x = to_word y → x = y := by rintros ⟨L₁⟩ ⟨L₂⟩; exact reduce.exact /-- Constructive Church-Rosser theorem (compare `church_rosser`). -/ def reduce.church_rosser (H12 : red L₁ L₂) (H13 : red L₁ L₃) : { L₄ // red L₂ L₄ ∧ red L₃ L₄ } := ⟨reduce L₁, reduce.rev H12, reduce.rev H13⟩ instance : decidable_eq (free_group α) := function.injective.decidable_eq to_word.inj instance red.decidable_rel : decidable_rel (@red α) | [] [] := is_true red.refl | [] (hd2::tl2) := is_false $ λ H, list.no_confusion (red.nil_iff.1 H) | ((x,b)::tl) [] := match red.decidable_rel tl [(x, bnot b)] with | is_true H := is_true $ red.trans (red.cons_cons H) $ (@red.step.bnot _ [] [] _ _).to_red | is_false H := is_false $ λ H2, H $ red.cons_nil_iff_singleton.1 H2 end | ((x1,b1)::tl1) ((x2,b2)::tl2) := if h : (x1, b1) = (x2, b2) then match red.decidable_rel tl1 tl2 with | is_true H := is_true $ h ▸ red.cons_cons H | is_false H := is_false $ λ H2, H $ h ▸ (red.cons_cons_iff _).1 $ H2 end else match red.decidable_rel tl1 ((x1,bnot b1)::(x2,b2)::tl2) with | is_true H := is_true $ (red.cons_cons H).tail red.step.cons_bnot | is_false H := is_false $ λ H2, H $ red.inv_of_red_of_ne h H2 end /-- A list containing every word that `w₁` reduces to. -/ def red.enum (L₁ : list (α × bool)) : list (list (α × bool)) := list.filter (λ L₂, red L₁ L₂) (list.sublists L₁) theorem red.enum.sound (H : L₂ ∈ red.enum L₁) : red L₁ L₂ := list.of_mem_filter H theorem red.enum.complete (H : red L₁ L₂) : L₂ ∈ red.enum L₁ := list.mem_filter_of_mem (list.mem_sublists.2 $ red.sublist H) H instance : fintype { L₂ // red L₁ L₂ } := fintype.subtype (list.to_finset $ red.enum L₁) $ λ L₂, ⟨λ H, red.enum.sound $ list.mem_to_finset.1 H, λ H, list.mem_to_finset.2 $ red.enum.complete H⟩ end reduce end free_group
9243667591d6b7fb47bddaaa4137affc6fc12c37
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/linear_algebra/matrix/to_lin.lean
72c3054c71bf5f49a30984a9eae2f2457c1da702
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
28,661
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, Patrick Massot, Casper Putz, Anne Baanen -/ import data.matrix.block import linear_algebra.matrix.finite_dimensional import linear_algebra.std_basis import ring_theory.algebra_tower /-! # Linear maps and matrices This file defines the maps to send matrices to a linear map, and to send linear maps between modules with a finite bases to matrices. This defines a linear equivalence between linear maps between finite-dimensional vector spaces and matrices indexed by the respective bases. ## Main definitions In the list below, and in all this file, `R` is a commutative ring (semiring is sometimes enough), `M` and its variations are `R`-modules, `ι`, `κ`, `n` and `m` are finite types used for indexing. * `linear_map.to_matrix`: given bases `v₁ : ι → M₁` and `v₂ : κ → M₂`, the `R`-linear equivalence from `M₁ →ₗ[R] M₂` to `matrix κ ι R` * `matrix.to_lin`: the inverse of `linear_map.to_matrix` * `linear_map.to_matrix'`: the `R`-linear equivalence from `(n → R) →ₗ[R] (m → R)` to `matrix n m R` (with the standard basis on `n → R` and `m → R`) * `matrix.to_lin'`: the inverse of `linear_map.to_matrix'` * `alg_equiv_matrix`: given a basis indexed by `n`, the `R`-algebra equivalence between `R`-endomorphisms of `M` and `matrix n n R` ## Tags linear_map, matrix, linear_equiv, diagonal, det, trace -/ noncomputable theory open linear_map matrix set submodule open_locale big_operators open_locale matrix universes u v w section to_matrix' instance {n m} [fintype m] [decidable_eq m] [fintype n] [decidable_eq n] (R) [fintype R] : fintype (matrix m n R) := by unfold matrix; apply_instance variables {R : Type*} [comm_ring R] variables {l m n : Type*} /-- `matrix.mul_vec M` is a linear map. -/ def matrix.mul_vec_lin [fintype n] (M : matrix m n R) : (n → R) →ₗ[R] (m → R) := { to_fun := M.mul_vec, map_add' := λ v w, funext (λ i, dot_product_add _ _ _), map_smul' := λ c v, funext (λ i, dot_product_smul _ _ _) } @[simp] lemma matrix.mul_vec_lin_apply [fintype n] (M : matrix m n R) (v : n → R) : matrix.mul_vec_lin M v = M.mul_vec v := rfl variables [fintype n] [decidable_eq n] @[simp] lemma matrix.mul_vec_std_basis (M : matrix m n R) (i j) : M.mul_vec (std_basis R (λ _, R) j 1) i = M i j := begin have : (∑ j', M i j' * if j = j' then 1 else 0) = M i j, { simp_rw [mul_boole, finset.sum_ite_eq, finset.mem_univ, if_true] }, convert this, ext, split_ifs with h; simp only [std_basis_apply], { rw [h, function.update_same] }, { rw [function.update_noteq (ne.symm h), pi.zero_apply] } end /-- Linear maps `(n → R) →ₗ[R] (m → R)` are linearly equivalent to `matrix m n R`. -/ def linear_map.to_matrix' : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] matrix m n R := { to_fun := λ f i j, f (std_basis R (λ _, R) j 1) i, inv_fun := matrix.mul_vec_lin, right_inv := λ M, by { ext i j, simp only [matrix.mul_vec_std_basis, matrix.mul_vec_lin_apply] }, left_inv := λ f, begin apply (pi.basis_fun R n).ext, intro j, ext i, simp only [pi.basis_fun_apply, matrix.mul_vec_std_basis, matrix.mul_vec_lin_apply] end, map_add' := λ f g, by { ext i j, simp only [pi.add_apply, linear_map.add_apply] }, map_smul' := λ c f, by { ext i j, simp only [pi.smul_apply, linear_map.smul_apply, ring_hom.id_apply] } } /-- A `matrix m n R` is linearly equivalent to a linear map `(n → R) →ₗ[R] (m → R)`. -/ def matrix.to_lin' : matrix m n R ≃ₗ[R] ((n → R) →ₗ[R] (m → R)) := linear_map.to_matrix'.symm @[simp] lemma linear_map.to_matrix'_symm : (linear_map.to_matrix'.symm : matrix m n R ≃ₗ[R] _) = matrix.to_lin' := rfl @[simp] lemma matrix.to_lin'_symm : (matrix.to_lin'.symm : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] _) = linear_map.to_matrix' := rfl @[simp] lemma linear_map.to_matrix'_to_lin' (M : matrix m n R) : linear_map.to_matrix' (matrix.to_lin' M) = M := linear_map.to_matrix'.apply_symm_apply M @[simp] lemma matrix.to_lin'_to_matrix' (f : (n → R) →ₗ[R] (m → R)) : matrix.to_lin' (linear_map.to_matrix' f) = f := matrix.to_lin'.apply_symm_apply f @[simp] lemma linear_map.to_matrix'_apply (f : (n → R) →ₗ[R] (m → R)) (i j) : linear_map.to_matrix' f i j = f (λ j', if j' = j then 1 else 0) i := begin simp only [linear_map.to_matrix', linear_equiv.coe_mk], congr, ext j', split_ifs with h, { rw [h, std_basis_same] }, apply std_basis_ne _ _ _ _ h end @[simp] lemma matrix.to_lin'_apply (M : matrix m n R) (v : n → R) : matrix.to_lin' M v = M.mul_vec v := rfl @[simp] lemma matrix.to_lin'_one : matrix.to_lin' (1 : matrix n n R) = id := by { ext, simp [linear_map.one_apply, std_basis_apply] } @[simp] lemma linear_map.to_matrix'_id : (linear_map.to_matrix' (linear_map.id : (n → R) →ₗ[R] (n → R))) = 1 := by { ext, rw [matrix.one_apply, linear_map.to_matrix'_apply, id_apply] } @[simp] lemma matrix.to_lin'_mul [fintype m] [decidable_eq m] (M : matrix l m R) (N : matrix m n R) : matrix.to_lin' (M ⬝ N) = (matrix.to_lin' M).comp (matrix.to_lin' N) := by { ext, simp } /-- Shortcut lemma for `matrix.to_lin'_mul` and `linear_map.comp_apply` -/ lemma matrix.to_lin'_mul_apply [fintype m] [decidable_eq m] (M : matrix l m R) (N : matrix m n R) (x) : matrix.to_lin' (M ⬝ N) x = (matrix.to_lin' M (matrix.to_lin' N x)) := by rw [matrix.to_lin'_mul, linear_map.comp_apply] lemma linear_map.to_matrix'_comp [fintype l] [decidable_eq l] (f : (n → R) →ₗ[R] (m → R)) (g : (l → R) →ₗ[R] (n → R)) : (f.comp g).to_matrix' = f.to_matrix' ⬝ g.to_matrix' := suffices (f.comp g) = (f.to_matrix' ⬝ g.to_matrix').to_lin', by rw [this, linear_map.to_matrix'_to_lin'], by rw [matrix.to_lin'_mul, matrix.to_lin'_to_matrix', matrix.to_lin'_to_matrix'] lemma linear_map.to_matrix'_mul [fintype m] [decidable_eq m] (f g : (m → R) →ₗ[R] (m → R)) : (f * g).to_matrix' = f.to_matrix' ⬝ g.to_matrix' := linear_map.to_matrix'_comp f g @[simp] lemma linear_map.to_matrix'_algebra_map (x : R) : linear_map.to_matrix' (algebra_map R (module.End R (n → R)) x) = scalar n x := by simp [module.algebra_map_End_eq_smul_id] lemma matrix.ker_to_lin'_eq_bot_iff {M : matrix n n R} : M.to_lin'.ker = ⊥ ↔ ∀ v, M.mul_vec v = 0 → v = 0 := by simp only [submodule.eq_bot_iff, linear_map.mem_ker, matrix.to_lin'_apply] /-- If `M` and `M'` are each other's inverse matrices, they provide an equivalence between `m → A` and `n → A` corresponding to `M.mul_vec` and `M'.mul_vec`. -/ @[simps] def matrix.to_lin'_of_inv [fintype m] [decidable_eq m] {M : matrix m n R} {M' : matrix n m R} (hMM' : M ⬝ M' = 1) (hM'M : M' ⬝ M = 1) : (m → R) ≃ₗ[R] (n → R) := { to_fun := matrix.to_lin' M', inv_fun := M.to_lin', left_inv := λ x, by rw [← matrix.to_lin'_mul_apply, hMM', matrix.to_lin'_one, id_apply], right_inv := λ x, by rw [← matrix.to_lin'_mul_apply, hM'M, matrix.to_lin'_one, id_apply], .. matrix.to_lin' M' } /-- Linear maps `(n → R) →ₗ[R] (n → R)` are algebra equivalent to `matrix n n R`. -/ def linear_map.to_matrix_alg_equiv' : ((n → R) →ₗ[R] (n → R)) ≃ₐ[R] matrix n n R := alg_equiv.of_linear_equiv linear_map.to_matrix' linear_map.to_matrix'_mul linear_map.to_matrix'_algebra_map /-- A `matrix n n R` is algebra equivalent to a linear map `(n → R) →ₗ[R] (n → R)`. -/ def matrix.to_lin_alg_equiv' : matrix n n R ≃ₐ[R] ((n → R) →ₗ[R] (n → R)) := linear_map.to_matrix_alg_equiv'.symm @[simp] lemma linear_map.to_matrix_alg_equiv'_symm : (linear_map.to_matrix_alg_equiv'.symm : matrix n n R ≃ₐ[R] _) = matrix.to_lin_alg_equiv' := rfl @[simp] lemma matrix.to_lin_alg_equiv'_symm : (matrix.to_lin_alg_equiv'.symm : ((n → R) →ₗ[R] (n → R)) ≃ₐ[R] _) = linear_map.to_matrix_alg_equiv' := rfl @[simp] lemma linear_map.to_matrix_alg_equiv'_to_lin_alg_equiv' (M : matrix n n R) : linear_map.to_matrix_alg_equiv' (matrix.to_lin_alg_equiv' M) = M := linear_map.to_matrix_alg_equiv'.apply_symm_apply M @[simp] lemma matrix.to_lin_alg_equiv'_to_matrix_alg_equiv' (f : (n → R) →ₗ[R] (n → R)) : matrix.to_lin_alg_equiv' (linear_map.to_matrix_alg_equiv' f) = f := matrix.to_lin_alg_equiv'.apply_symm_apply f @[simp] lemma linear_map.to_matrix_alg_equiv'_apply (f : (n → R) →ₗ[R] (n → R)) (i j) : linear_map.to_matrix_alg_equiv' f i j = f (λ j', if j' = j then 1 else 0) i := by simp [linear_map.to_matrix_alg_equiv'] @[simp] lemma matrix.to_lin_alg_equiv'_apply (M : matrix n n R) (v : n → R) : matrix.to_lin_alg_equiv' M v = M.mul_vec v := rfl @[simp] lemma matrix.to_lin_alg_equiv'_one : matrix.to_lin_alg_equiv' (1 : matrix n n R) = id := by { ext, simp [matrix.one_apply, std_basis_apply] } @[simp] lemma linear_map.to_matrix_alg_equiv'_id : (linear_map.to_matrix_alg_equiv' (linear_map.id : (n → R) →ₗ[R] (n → R))) = 1 := by { ext, rw [matrix.one_apply, linear_map.to_matrix_alg_equiv'_apply, id_apply] } @[simp] lemma matrix.to_lin_alg_equiv'_mul (M N : matrix n n R) : matrix.to_lin_alg_equiv' (M ⬝ N) = (matrix.to_lin_alg_equiv' M).comp (matrix.to_lin_alg_equiv' N) := by { ext, simp } lemma linear_map.to_matrix_alg_equiv'_comp (f g : (n → R) →ₗ[R] (n → R)) : (f.comp g).to_matrix_alg_equiv' = f.to_matrix_alg_equiv' ⬝ g.to_matrix_alg_equiv' := suffices (f.comp g) = (f.to_matrix_alg_equiv' ⬝ g.to_matrix_alg_equiv').to_lin_alg_equiv', by rw [this, linear_map.to_matrix_alg_equiv'_to_lin_alg_equiv'], by rw [matrix.to_lin_alg_equiv'_mul, matrix.to_lin_alg_equiv'_to_matrix_alg_equiv', matrix.to_lin_alg_equiv'_to_matrix_alg_equiv'] lemma linear_map.to_matrix_alg_equiv'_mul (f g : (n → R) →ₗ[R] (n → R)) : (f * g).to_matrix_alg_equiv' = f.to_matrix_alg_equiv' ⬝ g.to_matrix_alg_equiv' := linear_map.to_matrix_alg_equiv'_comp f g lemma matrix.rank_vec_mul_vec {K m n : Type u} [field K] [fintype n] [decidable_eq n] (w : m → K) (v : n → K) : rank (vec_mul_vec w v).to_lin' ≤ 1 := begin rw [vec_mul_vec_eq, matrix.to_lin'_mul], refine le_trans (rank_comp_le1 _ _) _, refine (rank_le_domain _).trans_eq _, rw [dim_fun', fintype.card_unit, nat.cast_one] end end to_matrix' section to_matrix variables {R : Type*} [comm_ring R] variables {l m n : Type*} [fintype n] [fintype m] [decidable_eq n] variables {M₁ M₂ : Type*} [add_comm_group M₁] [add_comm_group M₂] [module R M₁] [module R M₂] variables (v₁ : basis n R M₁) (v₂ : basis m R M₂) /-- Given bases of two modules `M₁` and `M₂` over a commutative ring `R`, we get a linear equivalence between linear maps `M₁ →ₗ M₂` and matrices over `R` indexed by the bases. -/ def linear_map.to_matrix : (M₁ →ₗ[R] M₂) ≃ₗ[R] matrix m n R := linear_equiv.trans (linear_equiv.arrow_congr v₁.equiv_fun v₂.equiv_fun) linear_map.to_matrix' /-- `linear_map.to_matrix'` is a particular case of `linear_map.to_matrix`, for the standard basis `pi.basis_fun R n`. -/ lemma linear_map.to_matrix_eq_to_matrix' : linear_map.to_matrix (pi.basis_fun R n) (pi.basis_fun R n) = linear_map.to_matrix' := rfl /-- Given bases of two modules `M₁` and `M₂` over a commutative ring `R`, we get a linear equivalence between matrices over `R` indexed by the bases and linear maps `M₁ →ₗ M₂`. -/ def matrix.to_lin : matrix m n R ≃ₗ[R] (M₁ →ₗ[R] M₂) := (linear_map.to_matrix v₁ v₂).symm /-- `matrix.to_lin'` is a particular case of `matrix.to_lin`, for the standard basis `pi.basis_fun R n`. -/ lemma matrix.to_lin_eq_to_lin' : matrix.to_lin (pi.basis_fun R n) (pi.basis_fun R m) = matrix.to_lin' := rfl @[simp] lemma linear_map.to_matrix_symm : (linear_map.to_matrix v₁ v₂).symm = matrix.to_lin v₁ v₂ := rfl @[simp] lemma matrix.to_lin_symm : (matrix.to_lin v₁ v₂).symm = linear_map.to_matrix v₁ v₂ := rfl @[simp] lemma matrix.to_lin_to_matrix (f : M₁ →ₗ[R] M₂) : matrix.to_lin v₁ v₂ (linear_map.to_matrix v₁ v₂ f) = f := by rw [← matrix.to_lin_symm, linear_equiv.apply_symm_apply] @[simp] lemma linear_map.to_matrix_to_lin (M : matrix m n R) : linear_map.to_matrix v₁ v₂ (matrix.to_lin v₁ v₂ M) = M := by rw [← matrix.to_lin_symm, linear_equiv.symm_apply_apply] lemma linear_map.to_matrix_apply (f : M₁ →ₗ[R] M₂) (i : m) (j : n) : linear_map.to_matrix v₁ v₂ f i j = v₂.repr (f (v₁ j)) i := begin rw [linear_map.to_matrix, linear_equiv.trans_apply, linear_map.to_matrix'_apply, linear_equiv.arrow_congr_apply, basis.equiv_fun_symm_apply, finset.sum_eq_single j, if_pos rfl, one_smul, basis.equiv_fun_apply], { intros j' _ hj', rw [if_neg hj', zero_smul] }, { intro hj, have := finset.mem_univ j, contradiction } end lemma linear_map.to_matrix_transpose_apply (f : M₁ →ₗ[R] M₂) (j : n) : (linear_map.to_matrix v₁ v₂ f)ᵀ j = v₂.repr (f (v₁ j)) := funext $ λ i, f.to_matrix_apply _ _ i j lemma linear_map.to_matrix_apply' (f : M₁ →ₗ[R] M₂) (i : m) (j : n) : linear_map.to_matrix v₁ v₂ f i j = v₂.repr (f (v₁ j)) i := linear_map.to_matrix_apply v₁ v₂ f i j lemma linear_map.to_matrix_transpose_apply' (f : M₁ →ₗ[R] M₂) (j : n) : (linear_map.to_matrix v₁ v₂ f)ᵀ j = v₂.repr (f (v₁ j)) := linear_map.to_matrix_transpose_apply v₁ v₂ f j lemma matrix.to_lin_apply (M : matrix m n R) (v : M₁) : matrix.to_lin v₁ v₂ M v = ∑ j, M.mul_vec (v₁.repr v) j • v₂ j := show v₂.equiv_fun.symm (matrix.to_lin' M (v₁.repr v)) = _, by rw [matrix.to_lin'_apply, v₂.equiv_fun_symm_apply] @[simp] lemma matrix.to_lin_self (M : matrix m n R) (i : n) : matrix.to_lin v₁ v₂ M (v₁ i) = ∑ j, M j i • v₂ j := begin rw [matrix.to_lin_apply, finset.sum_congr rfl (λ j hj, _)], rw [basis.repr_self, matrix.mul_vec, dot_product, finset.sum_eq_single i, finsupp.single_eq_same, mul_one], { intros i' _ i'_ne, rw [finsupp.single_eq_of_ne i'_ne.symm, mul_zero] }, { intros, have := finset.mem_univ i, contradiction }, end /-- This will be a special case of `linear_map.to_matrix_id_eq_basis_to_matrix`. -/ lemma linear_map.to_matrix_id : linear_map.to_matrix v₁ v₁ id = 1 := begin ext i j, simp [linear_map.to_matrix_apply, matrix.one_apply, finsupp.single, eq_comm] end lemma linear_map.to_matrix_one : linear_map.to_matrix v₁ v₁ 1 = 1 := linear_map.to_matrix_id v₁ @[simp] lemma matrix.to_lin_one : matrix.to_lin v₁ v₁ 1 = id := by rw [← linear_map.to_matrix_id v₁, matrix.to_lin_to_matrix] theorem linear_map.to_matrix_reindex_range [decidable_eq M₁] [decidable_eq M₂] (f : M₁ →ₗ[R] M₂) (k : m) (i : n) : linear_map.to_matrix v₁.reindex_range v₂.reindex_range f ⟨v₂ k, mem_range_self k⟩ ⟨v₁ i, mem_range_self i⟩ = linear_map.to_matrix v₁ v₂ f k i := by simp_rw [linear_map.to_matrix_apply, basis.reindex_range_self, basis.reindex_range_repr] variables {M₃ : Type*} [add_comm_group M₃] [module R M₃] (v₃ : basis l R M₃) lemma linear_map.to_matrix_comp [fintype l] [decidable_eq m] (f : M₂ →ₗ[R] M₃) (g : M₁ →ₗ[R] M₂) : linear_map.to_matrix v₁ v₃ (f.comp g) = linear_map.to_matrix v₂ v₃ f ⬝ linear_map.to_matrix v₁ v₂ g := by simp_rw [linear_map.to_matrix, linear_equiv.trans_apply, linear_equiv.arrow_congr_comp _ v₂.equiv_fun, linear_map.to_matrix'_comp] lemma linear_map.to_matrix_mul (f g : M₁ →ₗ[R] M₁) : linear_map.to_matrix v₁ v₁ (f * g) = linear_map.to_matrix v₁ v₁ f ⬝ linear_map.to_matrix v₁ v₁ g := by { rw [show (@has_mul.mul (M₁ →ₗ[R] M₁) _) = linear_map.comp, from rfl, linear_map.to_matrix_comp v₁ v₁ v₁ f g] } @[simp] lemma linear_map.to_matrix_algebra_map (x : R) : linear_map.to_matrix v₁ v₁ (algebra_map R (module.End R M₁) x) = scalar n x := by simp [module.algebra_map_End_eq_smul_id, linear_map.to_matrix_id] lemma linear_map.to_matrix_mul_vec_repr (f : M₁ →ₗ[R] M₂) (x : M₁) : (linear_map.to_matrix v₁ v₂ f).mul_vec (v₁.repr x) = v₂.repr (f x) := by { ext i, rw [← matrix.to_lin'_apply, linear_map.to_matrix, linear_equiv.trans_apply, matrix.to_lin'_to_matrix', linear_equiv.arrow_congr_apply, v₂.equiv_fun_apply], congr, exact v₁.equiv_fun.symm_apply_apply x } lemma matrix.to_lin_mul [fintype l] [decidable_eq m] (A : matrix l m R) (B : matrix m n R) : matrix.to_lin v₁ v₃ (A ⬝ B) = (matrix.to_lin v₂ v₃ A).comp (matrix.to_lin v₁ v₂ B) := begin apply (linear_map.to_matrix v₁ v₃).injective, haveI : decidable_eq l := λ _ _, classical.prop_decidable _, rw linear_map.to_matrix_comp v₁ v₂ v₃, repeat { rw linear_map.to_matrix_to_lin }, end /-- Shortcut lemma for `matrix.to_lin_mul` and `linear_map.comp_apply`. -/ lemma matrix.to_lin_mul_apply [fintype l] [decidable_eq m] (A : matrix l m R) (B : matrix m n R) (x) : matrix.to_lin v₁ v₃ (A ⬝ B) x = (matrix.to_lin v₂ v₃ A) (matrix.to_lin v₁ v₂ B x) := by rw [matrix.to_lin_mul v₁ v₂, linear_map.comp_apply] /-- If `M` and `M` are each other's inverse matrices, `matrix.to_lin M` and `matrix.to_lin M'` form a linear equivalence. -/ @[simps] def matrix.to_lin_of_inv [decidable_eq m] {M : matrix m n R} {M' : matrix n m R} (hMM' : M ⬝ M' = 1) (hM'M : M' ⬝ M = 1) : M₁ ≃ₗ[R] M₂ := { to_fun := matrix.to_lin v₁ v₂ M, inv_fun := matrix.to_lin v₂ v₁ M', left_inv := λ x, by rw [← matrix.to_lin_mul_apply, hM'M, matrix.to_lin_one, id_apply], right_inv := λ x, by rw [← matrix.to_lin_mul_apply, hMM', matrix.to_lin_one, id_apply], .. matrix.to_lin v₁ v₂ M } /-- Given a basis of a module `M₁` over a commutative ring `R`, we get an algebra equivalence between linear maps `M₁ →ₗ M₁` and square matrices over `R` indexed by the basis. -/ def linear_map.to_matrix_alg_equiv : (M₁ →ₗ[R] M₁) ≃ₐ[R] matrix n n R := alg_equiv.of_linear_equiv (linear_map.to_matrix v₁ v₁) (linear_map.to_matrix_mul v₁) (linear_map.to_matrix_algebra_map v₁) /-- Given a basis of a module `M₁` over a commutative ring `R`, we get an algebra equivalence between square matrices over `R` indexed by the basis and linear maps `M₁ →ₗ M₁`. -/ def matrix.to_lin_alg_equiv : matrix n n R ≃ₐ[R] (M₁ →ₗ[R] M₁) := (linear_map.to_matrix_alg_equiv v₁).symm @[simp] lemma linear_map.to_matrix_alg_equiv_symm : (linear_map.to_matrix_alg_equiv v₁).symm = matrix.to_lin_alg_equiv v₁ := rfl @[simp] lemma matrix.to_lin_alg_equiv_symm : (matrix.to_lin_alg_equiv v₁).symm = linear_map.to_matrix_alg_equiv v₁ := rfl @[simp] lemma matrix.to_lin_alg_equiv_to_matrix_alg_equiv (f : M₁ →ₗ[R] M₁) : matrix.to_lin_alg_equiv v₁ (linear_map.to_matrix_alg_equiv v₁ f) = f := by rw [← matrix.to_lin_alg_equiv_symm, alg_equiv.apply_symm_apply] @[simp] lemma linear_map.to_matrix_alg_equiv_to_lin_alg_equiv (M : matrix n n R) : linear_map.to_matrix_alg_equiv v₁ (matrix.to_lin_alg_equiv v₁ M) = M := by rw [← matrix.to_lin_alg_equiv_symm, alg_equiv.symm_apply_apply] lemma linear_map.to_matrix_alg_equiv_apply (f : M₁ →ₗ[R] M₁) (i j : n) : linear_map.to_matrix_alg_equiv v₁ f i j = v₁.repr (f (v₁ j)) i := by simp [linear_map.to_matrix_alg_equiv, linear_map.to_matrix_apply] lemma linear_map.to_matrix_alg_equiv_transpose_apply (f : M₁ →ₗ[R] M₁) (j : n) : (linear_map.to_matrix_alg_equiv v₁ f)ᵀ j = v₁.repr (f (v₁ j)) := funext $ λ i, f.to_matrix_apply _ _ i j lemma linear_map.to_matrix_alg_equiv_apply' (f : M₁ →ₗ[R] M₁) (i j : n) : linear_map.to_matrix_alg_equiv v₁ f i j = v₁.repr (f (v₁ j)) i := linear_map.to_matrix_alg_equiv_apply v₁ f i j lemma linear_map.to_matrix_alg_equiv_transpose_apply' (f : M₁ →ₗ[R] M₁) (j : n) : (linear_map.to_matrix_alg_equiv v₁ f)ᵀ j = v₁.repr (f (v₁ j)) := linear_map.to_matrix_alg_equiv_transpose_apply v₁ f j lemma matrix.to_lin_alg_equiv_apply (M : matrix n n R) (v : M₁) : matrix.to_lin_alg_equiv v₁ M v = ∑ j, M.mul_vec (v₁.repr v) j • v₁ j := show v₁.equiv_fun.symm (matrix.to_lin_alg_equiv' M (v₁.repr v)) = _, by rw [matrix.to_lin_alg_equiv'_apply, v₁.equiv_fun_symm_apply] @[simp] lemma matrix.to_lin_alg_equiv_self (M : matrix n n R) (i : n) : matrix.to_lin_alg_equiv v₁ M (v₁ i) = ∑ j, M j i • v₁ j := matrix.to_lin_self _ _ _ _ lemma linear_map.to_matrix_alg_equiv_id : linear_map.to_matrix_alg_equiv v₁ id = 1 := by simp_rw [linear_map.to_matrix_alg_equiv, alg_equiv.of_linear_equiv_apply, linear_map.to_matrix_id] @[simp] lemma matrix.to_lin_alg_equiv_one : matrix.to_lin_alg_equiv v₁ 1 = id := by rw [← linear_map.to_matrix_alg_equiv_id v₁, matrix.to_lin_alg_equiv_to_matrix_alg_equiv] theorem linear_map.to_matrix_alg_equiv_reindex_range [decidable_eq M₁] (f : M₁ →ₗ[R] M₁) (k i : n) : linear_map.to_matrix_alg_equiv v₁.reindex_range f ⟨v₁ k, mem_range_self k⟩ ⟨v₁ i, mem_range_self i⟩ = linear_map.to_matrix_alg_equiv v₁ f k i := by simp_rw [linear_map.to_matrix_alg_equiv_apply, basis.reindex_range_self, basis.reindex_range_repr] lemma linear_map.to_matrix_alg_equiv_comp (f g : M₁ →ₗ[R] M₁) : linear_map.to_matrix_alg_equiv v₁ (f.comp g) = linear_map.to_matrix_alg_equiv v₁ f ⬝ linear_map.to_matrix_alg_equiv v₁ g := by simp [linear_map.to_matrix_alg_equiv, linear_map.to_matrix_comp v₁ v₁ v₁ f g] lemma linear_map.to_matrix_alg_equiv_mul (f g : M₁ →ₗ[R] M₁) : linear_map.to_matrix_alg_equiv v₁ (f * g) = linear_map.to_matrix_alg_equiv v₁ f ⬝ linear_map.to_matrix_alg_equiv v₁ g := by { rw [show (@has_mul.mul (M₁ →ₗ[R] M₁) _) = linear_map.comp, from rfl, linear_map.to_matrix_alg_equiv_comp v₁ f g] } lemma matrix.to_lin_alg_equiv_mul (A B : matrix n n R) : matrix.to_lin_alg_equiv v₁ (A ⬝ B) = (matrix.to_lin_alg_equiv v₁ A).comp (matrix.to_lin_alg_equiv v₁ B) := by convert matrix.to_lin_mul v₁ v₁ v₁ A B end to_matrix namespace algebra section lmul variables {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T] variables [algebra R S] [algebra S T] [algebra R T] [is_scalar_tower R S T] variables {m n : Type*} [fintype m] [decidable_eq m] [decidable_eq n] variables (b : basis m R S) (c : basis n S T) open algebra lemma to_matrix_lmul' (x : S) (i j) : linear_map.to_matrix b b (lmul R S x) i j = b.repr (x * b j) i := by rw [linear_map.to_matrix_apply', lmul_apply] @[simp] lemma to_matrix_lsmul (x : R) (i j) : linear_map.to_matrix b b (algebra.lsmul R S x) i j = if i = j then x else 0 := by { rw [linear_map.to_matrix_apply', algebra.lsmul_coe, linear_equiv.map_smul, finsupp.smul_apply, b.repr_self_apply, smul_eq_mul, mul_boole], congr' 1; simp only [eq_comm] } /-- `left_mul_matrix b x` is the matrix corresponding to the linear map `λ y, x * y`. `left_mul_matrix_eq_repr_mul` gives a formula for the entries of `left_mul_matrix`. This definition is useful for doing (more) explicit computations with `algebra.lmul`, such as the trace form or norm map for algebras. -/ noncomputable def left_mul_matrix : S →ₐ[R] matrix m m R := { to_fun := λ x, linear_map.to_matrix b b (algebra.lmul R S x), map_zero' := by rw [alg_hom.map_zero, linear_equiv.map_zero], map_one' := by rw [alg_hom.map_one, linear_map.to_matrix_one], map_add' := λ x y, by rw [alg_hom.map_add, linear_equiv.map_add], map_mul' := λ x y, by rw [alg_hom.map_mul, linear_map.to_matrix_mul, matrix.mul_eq_mul], commutes' := λ r, by { ext, rw [lmul_algebra_map, to_matrix_lsmul, algebra_map_matrix_apply, id.map_eq_self] } } lemma left_mul_matrix_apply (x : S) : left_mul_matrix b x = linear_map.to_matrix b b (lmul R S x) := rfl lemma left_mul_matrix_eq_repr_mul (x : S) (i j) : left_mul_matrix b x i j = b.repr (x * b j) i := -- This is defeq to just `to_matrix_lmul' b x i j`, -- but the unfolding goes a lot faster with this explicit `rw`. by rw [left_mul_matrix_apply, to_matrix_lmul' b x i j] lemma left_mul_matrix_mul_vec_repr (x y : S) : (left_mul_matrix b x).mul_vec (b.repr y) = b.repr (x * y) := linear_map.to_matrix_mul_vec_repr b b (algebra.lmul R S x) y @[simp] lemma to_matrix_lmul_eq (x : S) : linear_map.to_matrix b b (lmul R S x) = left_mul_matrix b x := rfl lemma left_mul_matrix_injective : function.injective (left_mul_matrix b) := λ x x' h, calc x = algebra.lmul R S x 1 : (mul_one x).symm ... = algebra.lmul R S x' 1 : by rw (linear_map.to_matrix b b).injective h ... = x' : mul_one x' variable [fintype n] lemma smul_left_mul_matrix (x) (ik jk) : left_mul_matrix (b.smul c) x ik jk = left_mul_matrix b (left_mul_matrix c x ik.2 jk.2) ik.1 jk.1 := by simp only [left_mul_matrix_apply, linear_map.to_matrix_apply, mul_comm, basis.smul_apply, basis.smul_repr, finsupp.smul_apply, algebra.lmul_apply, id.smul_eq_mul, linear_equiv.map_smul, mul_smul_comm] lemma smul_left_mul_matrix_algebra_map (x : S) : left_mul_matrix (b.smul c) (algebra_map _ _ x) = block_diagonal (λ k, left_mul_matrix b x) := begin ext ⟨i, k⟩ ⟨j, k'⟩, rw [smul_left_mul_matrix, alg_hom.commutes, block_diagonal_apply, algebra_map_matrix_apply], split_ifs with h; simp [h], end lemma smul_left_mul_matrix_algebra_map_eq (x : S) (i j k) : left_mul_matrix (b.smul c) (algebra_map _ _ x) (i, k) (j, k) = left_mul_matrix b x i j := by rw [smul_left_mul_matrix_algebra_map, block_diagonal_apply_eq] lemma smul_left_mul_matrix_algebra_map_ne (x : S) (i j) {k k'} (h : k ≠ k') : left_mul_matrix (b.smul c) (algebra_map _ _ x) (i, k) (j, k') = 0 := by rw [smul_left_mul_matrix_algebra_map, block_diagonal_apply_ne _ _ _ h] end lmul end algebra namespace linear_map section finite_dimensional open_locale classical variables {K : Type*} [field K] variables {V : Type*} [add_comm_group V] [module K V] [finite_dimensional K V] variables {W : Type*} [add_comm_group W] [module K W] [finite_dimensional K W] instance : finite_dimensional K (V →ₗ[K] W) := linear_equiv.finite_dimensional (linear_map.to_matrix (basis.of_vector_space K V) (basis.of_vector_space K W)).symm /-- The dimension of the space of linear transformations is the product of the dimensions of the domain and codomain. -/ @[simp] lemma finrank_linear_map : finite_dimensional.finrank K (V →ₗ[K] W) = (finite_dimensional.finrank K V) * (finite_dimensional.finrank K W) := begin let hbV := basis.of_vector_space K V, let hbW := basis.of_vector_space K W, rw [linear_equiv.finrank_eq (linear_map.to_matrix hbV hbW), matrix.finrank_matrix, finite_dimensional.finrank_eq_card_basis hbV, finite_dimensional.finrank_eq_card_basis hbW, mul_comm], end end finite_dimensional end linear_map section variables {R : Type v} [comm_ring R] {n : Type*} [decidable_eq n] variables {M M₁ M₂ : Type*} [add_comm_group M] [module R M] variables [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] /-- The natural equivalence between linear endomorphisms of finite free modules and square matrices is compatible with the algebra structures. -/ def alg_equiv_matrix' [fintype n] : module.End R (n → R) ≃ₐ[R] matrix n n R := { map_mul' := linear_map.to_matrix'_comp, map_add' := linear_map.to_matrix'.map_add, commutes' := λ r, by { change (r • (linear_map.id : module.End R _)).to_matrix' = r • 1, rw ←linear_map.to_matrix'_id, refl, apply_instance }, ..linear_map.to_matrix' } /-- A linear equivalence of two modules induces an equivalence of algebras of their endomorphisms. -/ def linear_equiv.alg_conj (e : M₁ ≃ₗ[R] M₂) : module.End R M₁ ≃ₐ[R] module.End R M₂ := { map_mul' := λ f g, by apply e.arrow_congr_comp, map_add' := e.conj.map_add, commutes' := λ r, by { change e.conj (r • linear_map.id) = r • linear_map.id, rw [linear_equiv.map_smul, linear_equiv.conj_id], }, ..e.conj } /-- A basis of a module induces an equivalence of algebras from the endomorphisms of the module to square matrices. -/ def alg_equiv_matrix [fintype n] (h : basis n R M) : module.End R M ≃ₐ[R] matrix n n R := h.equiv_fun.alg_conj.trans alg_equiv_matrix' end
40594fc53fcb0246b0e8fb4a42c3609f6cdf7485
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/abelian/exact.lean
47df0b5e3ea1b312fa2f08d9825d8dbeb999ae6b
[ "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
4,387
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.abelian.basic import algebra.homology.exact import tactic.tfae /-! # Exact sequences in abelian categories In an abelian category, we get several interesting results related to exactness which are not true in more general settings. ## Main results * `(f, g)` is exact if and only if `f ≫ g = 0` and `kernel.ι g ≫ cokernel.π f = 0`. This characterisation tends to be less cumbersome to work with than the original definition involving the comparison map `image f ⟶ kernel g`. * If `(f, g)` is exact, then `image.ι f` has the universal property of the kernel of `g`. * `f` is a monomorphism iff `kernel.ι f = 0` iff `exact 0 f`, and `f` is an epimorphism iff `cokernel.π = 0` iff `exact f 0`. -/ universes v u noncomputable theory open category_theory open category_theory.limits open category_theory.preadditive variables {C : Type u} [category.{v} C] [abelian C] namespace category_theory.abelian variables {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) local attribute [instance] has_equalizers_of_has_kernels theorem exact_iff : exact f g ↔ f ≫ g = 0 ∧ kernel.ι g ≫ cokernel.π f = 0 := begin split, { introI h, exact ⟨h.1, kernel_comp_cokernel f g⟩ }, { refine λ h, ⟨h.1, _⟩, suffices hl : is_limit (kernel_fork.of_ι (image.ι f) (image_ι_comp_eq_zero h.1)), { have : image_to_kernel_map f g h.1 = (is_limit.cone_point_unique_up_to_iso hl (limit.is_limit _)).hom, { ext, simp }, rw this, apply_instance }, refine is_limit.of_ι _ _ _ _ _, { refine λ W u hu, kernel.lift (cokernel.π f) u _ ≫ (image_iso_image f).hom, rw [←kernel.lift_ι g u hu, category.assoc, h.2, has_zero_morphisms.comp_zero] }, { tidy }, { intros, simp [w, ←cancel_mono (image.ι f)] } } end theorem exact_iff' {cg : kernel_fork g} (hg : is_limit cg) {cf : cokernel_cofork f} (hf : is_colimit cf) : exact f g ↔ f ≫ g = 0 ∧ cg.ι ≫ cf.π = 0 := begin split, { introI h, exact ⟨h.1, fork_ι_comp_cofork_π f g cg cf⟩ }, { rw exact_iff, refine λ h, ⟨h.1, _⟩, apply zero_of_epi_comp (is_limit.cone_point_unique_up_to_iso hg (limit.is_limit _)).hom, apply zero_of_comp_mono (is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit _) hf).hom, simp [h.2] } end /-- If `(f, g)` is exact, then `images.image.ι f` is a kernel of `g`. -/ def is_limit_image [h : exact f g] : is_limit (kernel_fork.of_ι (images.image.ι f) (images.image_ι_comp_eq_zero h.1) : kernel_fork g) := begin rw exact_iff at h, refine is_limit.of_ι _ _ _ _ _, { refine λ W u hu, kernel.lift (cokernel.π f) u _, rw [←kernel.lift_ι g u hu, category.assoc, h.2, has_zero_morphisms.comp_zero] }, tidy end /-- If `(f, g)` is exact, then `image.ι f` is a kernel of `g`. -/ def is_limit_image' [h : exact f g] : is_limit (kernel_fork.of_ι (image.ι f) (image_ι_comp_eq_zero h.1)) := is_kernel.iso_kernel _ _ (is_limit_image f g) (image_iso_image f).symm $ is_image.lift_fac _ _ lemma exact_cokernel : exact f (cokernel.π f) := by { rw exact_iff, tidy } section variables (Z) lemma tfae_mono : tfae [mono f, kernel.ι f = 0, exact (0 : Z ⟶ X) f] := begin tfae_have : 3 → 2, { introsI, exact kernel_ι_eq_zero_of_exact_zero_left Z }, tfae_have : 1 → 3, { introsI, exact exact_zero_left_of_mono Z }, tfae_have : 2 → 1, { exact mono_of_kernel_ι_eq_zero _ }, tfae_finish end lemma mono_iff_exact_zero_left : mono f ↔ exact (0 : Z ⟶ X) f := (tfae_mono Z f).out 0 2 lemma mono_iff_kernel_ι_eq_zero : mono f ↔ kernel.ι f = 0 := (tfae_mono X f).out 0 1 lemma tfae_epi : tfae [epi f, cokernel.π f = 0, exact f (0 : Y ⟶ Z)] := begin tfae_have : 3 → 2, { rw exact_iff, rintro ⟨-, h⟩, exact zero_of_epi_comp _ h }, tfae_have : 1 → 3, { rw exact_iff, introI, exact ⟨by simp, by simp [cokernel.π_of_epi]⟩ }, tfae_have : 2 → 1, { exact epi_of_cokernel_π_eq_zero _ }, tfae_finish end lemma epi_iff_exact_zero_right : epi f ↔ exact f (0 : Y ⟶ Z) := (tfae_epi Z f).out 0 2 lemma epi_iff_cokernel_π_eq_zero : epi f ↔ cokernel.π f = 0 := (tfae_epi X f).out 0 1 end end category_theory.abelian
de108f392eb40b098e40e162f3bef8735c9b0299
367134ba5a65885e863bdc4507601606690974c1
/src/algebra/opposites.lean
2ed464573724bbbee6dd71277e05ef7c14d347e5
[ "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
10,750
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import data.opposite import algebra.field import algebra.group.commute import group_theory.group_action.defs import data.equiv.mul_add /-! # Algebraic operations on `αᵒᵖ` This file records several basic facts about the opposite of an algebraic structure, e.g. the opposite of a ring is a ring (with multiplication `x * y = yx`). Use is made of the identity functions `op : α → αᵒᵖ` and `unop : αᵒᵖ → α`. -/ namespace opposite universes u variables (α : Type u) instance [has_add α] : has_add (opposite α) := { add := λ x y, op (unop x + unop y) } instance [has_sub α] : has_sub (opposite α) := { sub := λ x y, op (unop x - unop y) } instance [add_semigroup α] : add_semigroup (opposite α) := { add_assoc := λ x y z, unop_injective $ add_assoc (unop x) (unop y) (unop z), .. opposite.has_add α } instance [add_left_cancel_semigroup α] : add_left_cancel_semigroup (opposite α) := { add_left_cancel := λ x y z H, unop_injective $ add_left_cancel $ op_injective H, .. opposite.add_semigroup α } instance [add_right_cancel_semigroup α] : add_right_cancel_semigroup (opposite α) := { add_right_cancel := λ x y z H, unop_injective $ add_right_cancel $ op_injective H, .. opposite.add_semigroup α } instance [add_comm_semigroup α] : add_comm_semigroup (opposite α) := { add_comm := λ x y, unop_injective $ add_comm (unop x) (unop y), .. opposite.add_semigroup α } instance [has_zero α] : has_zero (opposite α) := { zero := op 0 } instance [nontrivial α] : nontrivial (opposite α) := let ⟨x, y, h⟩ := exists_pair_ne α in nontrivial_of_ne (op x) (op y) (op_injective.ne h) section local attribute [reducible] opposite @[simp] lemma unop_eq_zero_iff [has_zero α] (a : αᵒᵖ) : a.unop = (0 : α) ↔ a = (0 : αᵒᵖ) := iff.refl _ @[simp] lemma op_eq_zero_iff [has_zero α] (a : α) : op a = (0 : αᵒᵖ) ↔ a = (0 : α) := iff.refl _ end instance [add_monoid α] : add_monoid (opposite α) := { zero_add := λ x, unop_injective $ zero_add $ unop x, add_zero := λ x, unop_injective $ add_zero $ unop x, .. opposite.add_semigroup α, .. opposite.has_zero α } instance [add_comm_monoid α] : add_comm_monoid (opposite α) := { .. opposite.add_monoid α, .. opposite.add_comm_semigroup α } instance [has_neg α] : has_neg (opposite α) := { neg := λ x, op $ -(unop x) } instance [add_group α] : add_group (opposite α) := { add_left_neg := λ x, unop_injective $ add_left_neg $ unop x, sub_eq_add_neg := λ x y, unop_injective $ sub_eq_add_neg (unop x) (unop y), .. opposite.add_monoid α, .. opposite.has_neg α, .. opposite.has_sub α } instance [add_comm_group α] : add_comm_group (opposite α) := { .. opposite.add_group α, .. opposite.add_comm_monoid α } instance [has_mul α] : has_mul (opposite α) := { mul := λ x y, op (unop y * unop x) } instance [semigroup α] : semigroup (opposite α) := { mul_assoc := λ x y z, unop_injective $ eq.symm $ mul_assoc (unop z) (unop y) (unop x), .. opposite.has_mul α } instance [right_cancel_semigroup α] : left_cancel_semigroup (opposite α) := { mul_left_cancel := λ x y z H, unop_injective $ mul_right_cancel $ op_injective H, .. opposite.semigroup α } instance [left_cancel_semigroup α] : right_cancel_semigroup (opposite α) := { mul_right_cancel := λ x y z H, unop_injective $ mul_left_cancel $ op_injective H, .. opposite.semigroup α } instance [comm_semigroup α] : comm_semigroup (opposite α) := { mul_comm := λ x y, unop_injective $ mul_comm (unop y) (unop x), .. opposite.semigroup α } instance [has_one α] : has_one (opposite α) := { one := op 1 } section local attribute [reducible] opposite @[simp] lemma unop_eq_one_iff [has_one α] (a : αᵒᵖ) : a.unop = 1 ↔ a = 1 := iff.refl _ @[simp] lemma op_eq_one_iff [has_one α] (a : α) : op a = 1 ↔ a = 1 := iff.refl _ end instance [monoid α] : monoid (opposite α) := { one_mul := λ x, unop_injective $ mul_one $ unop x, mul_one := λ x, unop_injective $ one_mul $ unop x, .. opposite.semigroup α, .. opposite.has_one α } instance [comm_monoid α] : comm_monoid (opposite α) := { .. opposite.monoid α, .. opposite.comm_semigroup α } instance [has_inv α] : has_inv (opposite α) := { inv := λ x, op $ (unop x)⁻¹ } instance [group α] : group (opposite α) := { mul_left_inv := λ x, unop_injective $ mul_inv_self $ unop x, .. opposite.monoid α, .. opposite.has_inv α } instance [comm_group α] : comm_group (opposite α) := { .. opposite.group α, .. opposite.comm_monoid α } instance [distrib α] : distrib (opposite α) := { left_distrib := λ x y z, unop_injective $ add_mul (unop y) (unop z) (unop x), right_distrib := λ x y z, unop_injective $ mul_add (unop z) (unop x) (unop y), .. opposite.has_add α, .. opposite.has_mul α } instance [semiring α] : semiring (opposite α) := { zero_mul := λ x, unop_injective $ mul_zero $ unop x, mul_zero := λ x, unop_injective $ zero_mul $ unop x, .. opposite.add_comm_monoid α, .. opposite.monoid α, .. opposite.distrib α } instance [ring α] : ring (opposite α) := { .. opposite.add_comm_group α, .. opposite.monoid α, .. opposite.semiring α } instance [comm_ring α] : comm_ring (opposite α) := { .. opposite.ring α, .. opposite.comm_semigroup α } instance [has_zero α] [has_mul α] [no_zero_divisors α] : no_zero_divisors (opposite α) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y (H : op (_ * _) = op (0:α)), or.cases_on (eq_zero_or_eq_zero_of_mul_eq_zero $ op_injective H) (λ hy, or.inr $ unop_injective $ hy) (λ hx, or.inl $ unop_injective $ hx), } instance [integral_domain α] : integral_domain (opposite α) := { .. opposite.no_zero_divisors α, .. opposite.comm_ring α, .. opposite.nontrivial α } instance [field α] : field (opposite α) := { mul_inv_cancel := λ x hx, unop_injective $ inv_mul_cancel $ λ hx', hx $ unop_injective hx', inv_zero := unop_injective inv_zero, .. opposite.comm_ring α, .. opposite.has_inv α, .. opposite.nontrivial α } instance (R : Type*) [has_scalar R α] : has_scalar R (opposite α) := { smul := λ c x, op (c • unop x) } instance (R : Type*) [monoid R] [mul_action R α] : mul_action R (opposite α) := { one_smul := λ x, unop_injective $ one_smul R (unop x), mul_smul := λ r₁ r₂ x, unop_injective $ mul_smul r₁ r₂ (unop x), ..opposite.has_scalar α R } instance (R : Type*) [monoid R] [add_monoid α] [distrib_mul_action R α] : distrib_mul_action R (opposite α) := { smul_add := λ r x₁ x₂, unop_injective $ smul_add r (unop x₁) (unop x₂), smul_zero := λ r, unop_injective $ smul_zero r, ..opposite.mul_action α R } @[simp] lemma op_zero [has_zero α] : op (0 : α) = 0 := rfl @[simp] lemma unop_zero [has_zero α] : unop (0 : αᵒᵖ) = 0 := rfl @[simp] lemma op_one [has_one α] : op (1 : α) = 1 := rfl @[simp] lemma unop_one [has_one α] : unop (1 : αᵒᵖ) = 1 := rfl variable {α} @[simp] lemma op_add [has_add α] (x y : α) : op (x + y) = op x + op y := rfl @[simp] lemma unop_add [has_add α] (x y : αᵒᵖ) : unop (x + y) = unop x + unop y := rfl @[simp] lemma op_neg [has_neg α] (x : α) : op (-x) = -op x := rfl @[simp] lemma unop_neg [has_neg α] (x : αᵒᵖ) : unop (-x) = -unop x := rfl @[simp] lemma op_mul [has_mul α] (x y : α) : op (x * y) = op y * op x := rfl @[simp] lemma unop_mul [has_mul α] (x y : αᵒᵖ) : unop (x * y) = unop y * unop x := rfl @[simp] lemma op_inv [has_inv α] (x : α) : op (x⁻¹) = (op x)⁻¹ := rfl @[simp] lemma unop_inv [has_inv α] (x : αᵒᵖ) : unop (x⁻¹) = (unop x)⁻¹ := rfl @[simp] lemma op_sub [add_group α] (x y : α) : op (x - y) = op x - op y := rfl @[simp] lemma unop_sub [add_group α] (x y : αᵒᵖ) : unop (x - y) = unop x - unop y := rfl @[simp] lemma op_smul {R : Type*} [has_scalar R α] (c : R) (a : α) : op (c • a) = c • op a := rfl @[simp] lemma unop_smul {R : Type*} [has_scalar R α] (c : R) (a : αᵒᵖ) : unop (c • a) = c • unop a := rfl lemma semiconj_by.op [has_mul α] {a x y : α} (h : semiconj_by a x y) : semiconj_by (op a) (op y) (op x) := begin dunfold semiconj_by, rw [← op_mul, ← op_mul, h.eq] end lemma semiconj_by.unop [has_mul α] {a x y : αᵒᵖ} (h : semiconj_by a x y) : semiconj_by (unop a) (unop y) (unop x) := begin dunfold semiconj_by, rw [← unop_mul, ← unop_mul, h.eq] end @[simp] lemma semiconj_by_op [has_mul α] {a x y : α} : semiconj_by (op a) (op y) (op x) ↔ semiconj_by a x y := begin split, { intro h, rw [← unop_op a, ← unop_op x, ← unop_op y], exact semiconj_by.unop h }, { intro h, exact semiconj_by.op h } end @[simp] lemma semiconj_by_unop [has_mul α] {a x y : αᵒᵖ} : semiconj_by (unop a) (unop y) (unop x) ↔ semiconj_by a x y := by conv_rhs { rw [← op_unop a, ← op_unop x, ← op_unop y, semiconj_by_op] } lemma commute.op [has_mul α] {x y : α} (h : commute x y) : commute (op x) (op y) := begin dunfold commute at h ⊢, exact semiconj_by.op h end lemma commute.unop [has_mul α] {x y : αᵒᵖ} (h : commute x y) : commute (unop x) (unop y) := begin dunfold commute at h ⊢, exact semiconj_by.unop h end @[simp] lemma commute_op [has_mul α] {x y : α} : commute (op x) (op y) ↔ commute x y := begin dunfold commute, rw semiconj_by_op end @[simp] lemma commute_unop [has_mul α] {x y : αᵒᵖ} : commute (unop x) (unop y) ↔ commute x y := begin dunfold commute, rw semiconj_by_unop end /-- The function `op` is an additive equivalence. -/ def op_add_equiv [has_add α] : α ≃+ αᵒᵖ := { map_add' := λ a b, rfl, .. equiv_to_opposite } @[simp] lemma coe_op_add_equiv [has_add α] : (op_add_equiv : α → αᵒᵖ) = op := rfl @[simp] lemma coe_op_add_equiv_symm [has_add α] : (op_add_equiv.symm : αᵒᵖ → α) = unop := rfl @[simp] lemma op_add_equiv_to_equiv [has_add α] : (op_add_equiv : α ≃+ αᵒᵖ).to_equiv = equiv_to_opposite := rfl end opposite open opposite /-- A ring homomorphism `f : R →+* S` such that `f x` commutes with `f y` for all `x, y` defines a ring homomorphism to `Sᵒᵖ`. -/ def ring_hom.to_opposite {R S : Type*} [semiring R] [semiring S] (f : R →+* S) (hf : ∀ x y, commute (f x) (f y)) : R →+* Sᵒᵖ := { map_one' := congr_arg op f.map_one, map_mul' := λ x y, by simp [(hf x y).eq], .. (opposite.op_add_equiv : S ≃+ Sᵒᵖ).to_add_monoid_hom.comp ↑f } @[simp] lemma ring_hom.coe_to_opposite {R S : Type*} [semiring R] [semiring S] (f : R →+* S) (hf : ∀ x y, commute (f x) (f y)) : ⇑(f.to_opposite hf) = op ∘ f := rfl
47e09537539d2dc16176a77230e08527475c17ec
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/comp_val2.lean
322f8efea1e11ae0cecdb32b5ace13637974f576
[ "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
477
lean
open tactic example : 'a' ≠ 'b' := by comp_val example : '0' ≠ 'a' := by comp_val example : "hello worlg" ≠ "hhello world" := by comp_val example : "hello world" ≠ "hhello world" := by comp_val example : "abc" ≠ "cde" := by comp_val example : "abc" ≠ "" := by comp_val example : "" ≠ "cde" := by comp_val example : @fin.mk 5 3 dec_trivial ≠ @fin.mk 5 4 dec_trivial := by comp_val example : @fin.mk 5 4 dec_trivial ≠ @fin.mk 5 1 dec_trivial := by comp_val
3d9c32ba4405b0522dbd5f2e1472ae219f75b9ce
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/jason1.lean
a4b58feb6cfaa37ad3a54bd0680df1dc64b1c2e2
[ "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
215
lean
structure Name where name : Unit inductive Foo (Name : Type) where | foo (x : Name) def bar : Foo Name → Type | Foo.foo (Name.mk n) => Nat def bar' : Foo Name → Type | Foo.foo (_root_.Name.mk n) => Nat
71a3976d95216b5a4f629eca564afee8e73b5139
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/topology/stone_cech.lean
ef0343a9885f86d621aa2e20952b047e3a96620a
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
11,323
lean
/- Copyright (c) 2018 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton -/ import topology.bases import topology.dense_embedding /-! # Stone-Čech compactification Construction of the Stone-Čech compactification using ultrafilters. Parts of the formalization are based on "Ultrafilters and Topology" by Marius Stekelenburg, particularly section 5. -/ noncomputable theory open filter set open_locale topological_space universes u v section ultrafilter /- The set of ultrafilters on α carries a natural topology which makes it the Stone-Čech compactification of α (viewed as a discrete space). -/ /-- Basis for the topology on `ultrafilter α`. -/ def ultrafilter_basis (α : Type u) : set (set (ultrafilter α)) := {t | ∃ (s : set α), t = {u | s ∈ u.val}} variables {α : Type u} instance : topological_space (ultrafilter α) := topological_space.generate_from (ultrafilter_basis α) lemma ultrafilter_basis_is_basis : topological_space.is_topological_basis (ultrafilter_basis α) := ⟨begin rintros _ ⟨a, rfl⟩ _ ⟨b, rfl⟩ u ⟨ua, ub⟩, refine ⟨_, ⟨a ∩ b, rfl⟩, u.val.inter_sets ua ub, assume v hv, ⟨_, _⟩⟩; apply v.val.sets_of_superset hv; simp end, eq_univ_of_univ_subset $ subset_sUnion_of_mem $ ⟨univ, eq.symm (eq_univ_of_forall (λ u, u.val.univ_sets))⟩, rfl⟩ /-- The basic open sets for the topology on ultrafilters are open. -/ lemma ultrafilter_is_open_basic (s : set α) : is_open {u : ultrafilter α | s ∈ u.val} := topological_space.is_open_of_is_topological_basis ultrafilter_basis_is_basis ⟨s, rfl⟩ /-- The basic open sets for the topology on ultrafilters are also closed. -/ lemma ultrafilter_is_closed_basic (s : set α) : is_closed {u : ultrafilter α | s ∈ u.val} := begin change is_open _ᶜ, convert ultrafilter_is_open_basic sᶜ, ext u, exact (ultrafilter_iff_compl_mem_iff_not_mem.mp u.property s).symm end /-- Every ultrafilter `u` on `ultrafilter α` converges to a unique point of `ultrafilter α`, namely `mjoin u`. -/ lemma ultrafilter_converges_iff {u : ultrafilter (ultrafilter α)} {x : ultrafilter α} : u.val ≤ 𝓝 x ↔ x = mjoin u := begin rw [eq_comm, ultrafilter.eq_iff_val_le_val], change u.val ≤ 𝓝 x ↔ x.val.sets ⊆ {a | {v : ultrafilter α | a ∈ v.val} ∈ u.val}, simp only [topological_space.nhds_generate_from, le_infi_iff, ultrafilter_basis, le_principal_iff], split; intro h, { intros a ha, exact h _ ⟨ha, a, rfl⟩ }, { rintros _ ⟨xi, a, rfl⟩, exact h xi } end instance ultrafilter_compact : compact_space (ultrafilter α) := ⟨compact_iff_ultrafilter_le_nhds.mpr $ assume f uf _, ⟨mjoin ⟨f, uf⟩, trivial, ultrafilter_converges_iff.mpr rfl⟩⟩ instance ultrafilter.t2_space : t2_space (ultrafilter α) := t2_iff_ultrafilter.mpr $ assume f x y u fx fy, have hx : x = mjoin ⟨f, u⟩, from ultrafilter_converges_iff.mp fx, have hy : y = mjoin ⟨f, u⟩, from ultrafilter_converges_iff.mp fy, hx.trans hy.symm lemma ultrafilter_comap_pure_nhds (b : ultrafilter α) : comap pure (𝓝 b) ≤ b.val := begin rw topological_space.nhds_generate_from, simp only [comap_infi, comap_principal], intros s hs, rw ←le_principal_iff, refine infi_le_of_le {u | s ∈ u.val} _, refine infi_le_of_le ⟨hs, ⟨s, rfl⟩⟩ _, exact principal_mono.2 (λ a, id) end section embedding lemma ultrafilter_pure_injective : function.injective (pure : α → ultrafilter α) := begin intros x y h, have : {x} ∈ (pure x : ultrafilter α).val := singleton_mem_pure_sets, rw h at this, exact (mem_singleton_iff.mp (mem_pure_sets.mp this)).symm end open topological_space /-- `pure : α → ultrafilter α` defines a dense inducing of `α` in `ultrafilter α`. -/ lemma dense_inducing_pure : @dense_inducing _ _ ⊥ _ (pure : α → ultrafilter α) := by letI : topological_space α := ⊥; exact dense_inducing.mk' pure continuous_bot (assume x, mem_closure_iff_ultrafilter.mpr ⟨x.map ultrafilter.pure, range_mem_map, ultrafilter_converges_iff.mpr (bind_pure x).symm⟩) (assume a s as, ⟨{u | s ∈ u.val}, mem_nhds_sets (ultrafilter_is_open_basic s) (mem_of_nhds as : a ∈ s), assume b hb, mem_pure_sets.mp hb⟩) -- The following refined version will never be used /-- `pure : α → ultrafilter α` defines a dense embedding of `α` in `ultrafilter α`. -/ lemma dense_embedding_pure : @dense_embedding _ _ ⊥ _ (pure : α → ultrafilter α) := by letI : topological_space α := ⊥ ; exact { inj := ultrafilter_pure_injective, ..dense_inducing_pure } end embedding section extension /- Goal: Any function `α → γ` to a compact Hausdorff space `γ` has a unique extension to a continuous function `ultrafilter α → γ`. We already know it must be unique because `α → ultrafilter α` is a dense embedding and `γ` is Hausdorff. For existence, we will invoke `dense_embedding.continuous_extend`. -/ variables {γ : Type*} [topological_space γ] /-- The extension of a function `α → γ` to a function `ultrafilter α → γ`. When `γ` is a compact Hausdorff space it will be continuous. -/ def ultrafilter.extend (f : α → γ) : ultrafilter α → γ := by letI : topological_space α := ⊥; exact dense_inducing_pure.extend f variables [t2_space γ] lemma ultrafilter_extend_extends (f : α → γ) : ultrafilter.extend f ∘ pure = f := begin letI : topological_space α := ⊥, letI : discrete_topology α := ⟨rfl⟩, exact funext (dense_inducing_pure.extend_eq continuous_of_discrete_topology) end variables [compact_space γ] lemma continuous_ultrafilter_extend (f : α → γ) : continuous (ultrafilter.extend f) := have ∀ (b : ultrafilter α), ∃ c, tendsto f (comap ultrafilter.pure (𝓝 b)) (𝓝 c) := assume b, -- b.map f is an ultrafilter on γ, which is compact, so it converges to some c in γ. let ⟨c, _, h⟩ := compact_iff_ultrafilter_le_nhds.mp compact_univ (b.map f).val (b.map f).property (by rw [le_principal_iff]; exact univ_mem_sets) in ⟨c, le_trans (map_mono (ultrafilter_comap_pure_nhds _)) h⟩, begin letI : topological_space α := ⊥, letI : normal_space γ := normal_of_compact_t2, exact dense_inducing_pure.continuous_extend this end /-- The value of `ultrafilter.extend f` on an ultrafilter `b` is the unique limit of the ultrafilter `b.map f` in `γ`. -/ lemma ultrafilter_extend_eq_iff {f : α → γ} {b : ultrafilter α} {c : γ} : ultrafilter.extend f b = c ↔ b.val.map f ≤ 𝓝 c := ⟨assume h, begin -- Write b as an ultrafilter limit of pure ultrafilters, and use -- the facts that ultrafilter.extend is a continuous extension of f. let b' : ultrafilter (ultrafilter α) := b.map pure, have t : b'.val ≤ 𝓝 b, from ultrafilter_converges_iff.mpr (bind_pure _).symm, rw ←h, have := (continuous_ultrafilter_extend f).tendsto b, refine le_trans _ (le_trans (map_mono t) this), change _ ≤ map (ultrafilter.extend f ∘ pure) b.val, rw ultrafilter_extend_extends, exact le_refl _ end, assume h, by letI : topological_space α := ⊥; exact dense_inducing_pure.extend_eq_of_tendsto (le_trans (map_mono (ultrafilter_comap_pure_nhds _)) h)⟩ end extension end ultrafilter section stone_cech /- Now, we start with a (not necessarily discrete) topological space α and we want to construct its Stone-Čech compactification. We can build it as a quotient of `ultrafilter α` by the relation which identifies two points if the extension of every continuous function α → γ to a compact Hausdorff space sends the two points to the same point of γ. -/ variables (α : Type u) [topological_space α] instance stone_cech_setoid : setoid (ultrafilter α) := { r := λ x y, ∀ (γ : Type u) [topological_space γ], by exactI ∀ [t2_space γ] [compact_space γ] (f : α → γ) (hf : continuous f), ultrafilter.extend f x = ultrafilter.extend f y, iseqv := ⟨assume x γ tγ h₁ h₂ f hf, rfl, assume x y xy γ tγ h₁ h₂ f hf, by exactI (xy γ f hf).symm, assume x y z xy yz γ tγ h₁ h₂ f hf, by exactI (xy γ f hf).trans (yz γ f hf)⟩ } /-- The Stone-Čech compactification of a topological space. -/ def stone_cech : Type u := quotient (stone_cech_setoid α) variables {α} instance : topological_space (stone_cech α) := by unfold stone_cech; apply_instance instance [inhabited α] : inhabited (stone_cech α) := by unfold stone_cech; apply_instance /-- The natural map from α to its Stone-Čech compactification. -/ def stone_cech_unit (x : α) : stone_cech α := ⟦pure x⟧ /-- The image of stone_cech_unit is dense. (But stone_cech_unit need not be an embedding, for example if α is not Hausdorff.) -/ lemma stone_cech_unit_dense : closure (range (@stone_cech_unit α _)) = univ := begin convert quotient_dense_of_dense (eq_univ_iff_forall.mp dense_inducing_pure.closure_range), rw [←range_comp], refl end section extension variables {γ : Type u} [topological_space γ] [t2_space γ] [compact_space γ] variables {f : α → γ} (hf : continuous f) local attribute [elab_with_expected_type] quotient.lift /-- The extension of a continuous function from α to a compact Hausdorff space γ to the Stone-Čech compactification of α. -/ def stone_cech_extend : stone_cech α → γ := quotient.lift (ultrafilter.extend f) (λ x y xy, xy γ f hf) lemma stone_cech_extend_extends : stone_cech_extend hf ∘ stone_cech_unit = f := ultrafilter_extend_extends f lemma continuous_stone_cech_extend : continuous (stone_cech_extend hf) := continuous_quot_lift _ (continuous_ultrafilter_extend f) end extension lemma convergent_eqv_pure {u : ultrafilter α} {x : α} (ux : u.val ≤ 𝓝 x) : u ≈ pure x := assume γ tγ h₁ h₂ f hf, begin resetI, transitivity f x, swap, symmetry, all_goals { refine ultrafilter_extend_eq_iff.mpr (le_trans (map_mono _) (hf.tendsto _)) }, { apply pure_le_nhds }, { exact ux } end lemma continuous_stone_cech_unit : continuous (stone_cech_unit : α → stone_cech α) := continuous_iff_ultrafilter.mpr $ λ x g u gx, let g' : ultrafilter α := ⟨g, u⟩ in have (g'.map ultrafilter.pure).val ≤ 𝓝 g', by rw ultrafilter_converges_iff; exact (bind_pure _).symm, have (g'.map stone_cech_unit).val ≤ 𝓝 ⟦g'⟧, from (continuous_at_iff_ultrafilter g').mp (continuous_quotient_mk.tendsto g') _ (ultrafilter_map u) this, by rwa (show ⟦g'⟧ = ⟦pure x⟧, from quotient.sound $ convergent_eqv_pure gx) at this instance stone_cech.t2_space : t2_space (stone_cech α) := begin rw t2_iff_ultrafilter, rintros g ⟨x⟩ ⟨y⟩ u gx gy, apply quotient.sound, intros γ tγ h₁ h₂ f hf, resetI, let ff := stone_cech_extend hf, change ff ⟦x⟧ = ff ⟦y⟧, have lim : ∀ z : ultrafilter α, g ≤ 𝓝 ⟦z⟧ → tendsto ff g (𝓝 (ff ⟦z⟧)) := assume z gz, calc map ff g ≤ map ff (𝓝 ⟦z⟧) : map_mono gz ... ≤ 𝓝 (ff ⟦z⟧) : (continuous_stone_cech_extend hf).tendsto _, haveI := u.1, exact tendsto_nhds_unique (lim x gx) (lim y gy) end instance stone_cech.compact_space : compact_space (stone_cech α) := quotient.compact_space end stone_cech
abf1c0eefecb9e284e5cd62a90720e618fd220cb
c777c32c8e484e195053731103c5e52af26a25d1
/src/ring_theory/tensor_product.lean
203263beb8cd15729db4ba326b54cbad4bf0bd7c
[ "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
37,410
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johan Commelin -/ import linear_algebra.finite_dimensional import ring_theory.adjoin.basic import linear_algebra.direct_sum.finsupp /-! # The tensor product of R-algebras Let `R` be a (semi)ring and `A` an `R`-algebra. In this file we: - Define the `A`-module structure on `A ⊗ M`, for an `R`-module `M`. - Define the `R`-algebra structure on `A ⊗ B`, for another `R`-algebra `B`. and provide the structure isomorphisms * `R ⊗[R] A ≃ₐ[R] A` * `A ⊗[R] R ≃ₐ[R] A` * `A ⊗[R] B ≃ₐ[R] B ⊗[R] A` * `((A ⊗[R] B) ⊗[R] C) ≃ₐ[R] (A ⊗[R] (B ⊗[R] C))` ## Main declaration - `linear_map.base_change A f` is the `A`-linear map `A ⊗ f`, for an `R`-linear map `f`. ## Implementation notes The heterobasic definitions below such as: * `tensor_product.algebra_tensor_module.curry` * `tensor_product.algebra_tensor_module.uncurry` * `tensor_product.algebra_tensor_module.lcurry` * `tensor_product.algebra_tensor_module.lift` * `tensor_product.algebra_tensor_module.lift.equiv` * `tensor_product.algebra_tensor_module.mk` * `tensor_product.algebra_tensor_module.assoc` are just more general versions of the definitions already in `linear_algebra/tensor_product`. We could thus consider replacing the less general definitions with these ones. If we do this, we probably should still implement the less general ones as abbreviations to the more general ones with fewer type arguments. -/ universes u v₁ v₂ v₃ v₄ open_locale tensor_product open tensor_product namespace tensor_product variables {R A M N P : Type*} /-! ### The `A`-module structure on `A ⊗[R] M` -/ open linear_map open algebra (lsmul) namespace algebra_tensor_module section semiring variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M] variables [add_comm_monoid N] [module R N] variables [add_comm_monoid P] [module R P] [module A P] [is_scalar_tower R A P] lemma smul_eq_lsmul_rtensor (a : A) (x : M ⊗[R] N) : a • x = (lsmul R M a).rtensor N x := rfl /-- Heterobasic version of `tensor_product.curry`: Given a linear map `M ⊗[R] N →[A] P`, compose it with the canonical bilinear map `M →[A] N →[R] M ⊗[R] N` to form a bilinear map `M →[A] N →[R] P`. -/ @[simps] def curry (f : (M ⊗[R] N) →ₗ[A] P) : M →ₗ[A] (N →ₗ[R] P) := { to_fun := curry (f.restrict_scalars R), map_smul' := λ c x, linear_map.ext $ λ y, f.map_smul c (x ⊗ₜ y), .. curry (f.restrict_scalars R) } lemma restrict_scalars_curry (f : (M ⊗[R] N) →ₗ[A] P) : restrict_scalars R (curry f) = curry (f.restrict_scalars R) := rfl /-- Just as `tensor_product.ext` is marked `ext` instead of `tensor_product.ext'`, this is a better `ext` lemma than `tensor_product.algebra_tensor_module.ext` below. See note [partially-applied ext lemmas]. -/ @[ext] lemma curry_injective : function.injective (curry : (M ⊗ N →ₗ[A] P) → (M →ₗ[A] N →ₗ[R] P)) := λ x y h, linear_map.restrict_scalars_injective R $ curry_injective $ (congr_arg (linear_map.restrict_scalars R) h : _) theorem ext {g h : (M ⊗[R] N) →ₗ[A] P} (H : ∀ x y, g (x ⊗ₜ y) = h (x ⊗ₜ y)) : g = h := curry_injective $ linear_map.ext₂ H end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] [algebra R A] variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M] variables [add_comm_monoid N] [module R N] variables [add_comm_monoid P] [module R P] [module A P] [is_scalar_tower R A P] /-- Heterobasic version of `tensor_product.lift`: Constructing a linear map `M ⊗[R] N →[A] P` given a bilinear map `M →[A] N →[R] P` with the property that its composition with the canonical bilinear map `M →[A] N →[R] M ⊗[R] N` is the given bilinear map `M →[A] N →[R] P`. -/ @[simps] def lift (f : M →ₗ[A] (N →ₗ[R] P)) : (M ⊗[R] N) →ₗ[A] P := { map_smul' := λ c, show ∀ x : M ⊗[R] N, (lift (f.restrict_scalars R)).comp (lsmul R _ c) x = (lsmul R _ c).comp (lift (f.restrict_scalars R)) x, from ext_iff.1 $ tensor_product.ext' $ λ x y, by simp only [comp_apply, algebra.lsmul_coe, smul_tmul', lift.tmul, coe_restrict_scalars_eq_coe, f.map_smul, smul_apply], .. lift (f.restrict_scalars R) } @[simp] lemma lift_tmul (f : M →ₗ[A] (N →ₗ[R] P)) (x : M) (y : N) : lift f (x ⊗ₜ y) = f x y := rfl variables (R A M N P) /-- Heterobasic version of `tensor_product.uncurry`: Linearly constructing a linear map `M ⊗[R] N →[A] P` given a bilinear map `M →[A] N →[R] P` with the property that its composition with the canonical bilinear map `M →[A] N →[R] M ⊗[R] N` is the given bilinear map `M →[A] N →[R] P`. -/ @[simps] def uncurry : (M →ₗ[A] (N →ₗ[R] P)) →ₗ[A] ((M ⊗[R] N) →ₗ[A] P) := { to_fun := lift, map_add' := λ f g, ext $ λ x y, by simp only [lift_tmul, add_apply], map_smul' := λ c f, ext $ λ x y, by simp only [lift_tmul, smul_apply, ring_hom.id_apply] } /-- Heterobasic version of `tensor_product.lcurry`: Given a linear map `M ⊗[R] N →[A] P`, compose it with the canonical bilinear map `M →[A] N →[R] M ⊗[R] N` to form a bilinear map `M →[A] N →[R] P`. -/ @[simps] def lcurry : ((M ⊗[R] N) →ₗ[A] P) →ₗ[A] (M →ₗ[A] (N →ₗ[R] P)) := { to_fun := curry, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } /-- Heterobasic version of `tensor_product.lift.equiv`: A linear equivalence constructing a linear map `M ⊗[R] N →[A] P` given a bilinear map `M →[A] N →[R] P` with the property that its composition with the canonical bilinear map `M →[A] N →[R] M ⊗[R] N` is the given bilinear map `M →[A] N →[R] P`. -/ def lift.equiv : (M →ₗ[A] (N →ₗ[R] P)) ≃ₗ[A] ((M ⊗[R] N) →ₗ[A] P) := linear_equiv.of_linear (uncurry R A M N P) (lcurry R A M N P) (linear_map.ext $ λ f, ext $ λ x y, lift_tmul _ x y) (linear_map.ext $ λ f, linear_map.ext $ λ x, linear_map.ext $ λ y, lift_tmul f x y) variables (R A M N P) /-- Heterobasic version of `tensor_product.mk`: The canonical bilinear map `M →[A] N →[R] M ⊗[R] N`. -/ @[simps] def mk : M →ₗ[A] N →ₗ[R] M ⊗[R] N := { map_smul' := λ c x, rfl, .. mk R M N } local attribute [ext] tensor_product.ext /-- Heterobasic version of `tensor_product.assoc`: Linear equivalence between `(M ⊗[A] N) ⊗[R] P` and `M ⊗[A] (N ⊗[R] P)`. -/ def assoc : ((M ⊗[A] P) ⊗[R] N) ≃ₗ[A] (M ⊗[A] (P ⊗[R] N)) := linear_equiv.of_linear (lift $ tensor_product.uncurry A _ _ _ $ comp (lcurry R A _ _ _) $ tensor_product.mk A M (P ⊗[R] N)) (tensor_product.uncurry A _ _ _ $ comp (uncurry R A _ _ _) $ by { apply tensor_product.curry, exact (mk R A _ _) }) (by { ext, refl, }) (by { ext, simp only [curry_apply, tensor_product.curry_apply, mk_apply, tensor_product.mk_apply, uncurry_apply, tensor_product.uncurry_apply, id_apply, lift_tmul, compr₂_apply, restrict_scalars_apply, function.comp_app, to_fun_eq_coe, lcurry_apply, linear_map.comp_apply] }) end comm_semiring end algebra_tensor_module end tensor_product namespace linear_map open tensor_product /-! ### The base-change of a linear map of `R`-modules to a linear map of `A`-modules -/ section semiring variables {R A B M N : Type*} [comm_semiring R] variables [semiring A] [algebra R A] [semiring B] [algebra R B] variables [add_comm_monoid M] [module R M] [add_comm_monoid N] [module R N] variables (r : R) (f g : M →ₗ[R] N) variables (A) /-- `base_change A f` for `f : M →ₗ[R] N` is the `A`-linear map `A ⊗[R] M →ₗ[A] A ⊗[R] N`. -/ def base_change (f : M →ₗ[R] N) : A ⊗[R] M →ₗ[A] A ⊗[R] N := { to_fun := f.ltensor A, map_add' := (f.ltensor A).map_add, map_smul' := λ a x, show (f.ltensor A) (rtensor M (linear_map.mul R A a) x) = (rtensor N ((linear_map.mul R A) a)) ((ltensor A f) x), by { rw [← comp_apply, ← comp_apply], simp only [ltensor_comp_rtensor, rtensor_comp_ltensor] } } variables {A} @[simp] lemma base_change_tmul (a : A) (x : M) : f.base_change A (a ⊗ₜ x) = a ⊗ₜ (f x) := rfl lemma base_change_eq_ltensor : (f.base_change A : A ⊗ M → A ⊗ N) = f.ltensor A := rfl @[simp] lemma base_change_add : (f + g).base_change A = f.base_change A + g.base_change A := by { ext, simp [base_change_eq_ltensor], } @[simp] lemma base_change_zero : base_change A (0 : M →ₗ[R] N) = 0 := by { ext, simp [base_change_eq_ltensor], } @[simp] lemma base_change_smul : (r • f).base_change A = r • (f.base_change A) := by { ext, simp [base_change_tmul], } variables (R A M N) /-- `base_change` as a linear map. -/ @[simps] def base_change_hom : (M →ₗ[R] N) →ₗ[R] A ⊗[R] M →ₗ[A] A ⊗[R] N := { to_fun := base_change A, map_add' := base_change_add, map_smul' := base_change_smul } end semiring section ring variables {R A B M N : Type*} [comm_ring R] variables [ring A] [algebra R A] [ring B] [algebra R B] variables [add_comm_group M] [module R M] [add_comm_group N] [module R N] variables (f g : M →ₗ[R] N) @[simp] lemma base_change_sub : (f - g).base_change A = f.base_change A - g.base_change A := by { ext, simp [base_change_eq_ltensor], } @[simp] lemma base_change_neg : (-f).base_change A = -(f.base_change A) := by { ext, simp [base_change_eq_ltensor], } end ring end linear_map namespace algebra namespace tensor_product section semiring variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type v₂} [semiring B] [algebra R B] /-! ### The `R`-algebra structure on `A ⊗[R] B` -/ /-- (Implementation detail) The multiplication map on `A ⊗[R] B`, for a fixed pure tensor in the first argument, as an `R`-linear map. -/ def mul_aux (a₁ : A) (b₁ : B) : (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) := tensor_product.map (linear_map.mul_left R a₁) (linear_map.mul_left R b₁) @[simp] lemma mul_aux_apply (a₁ a₂ : A) (b₁ b₂ : B) : (mul_aux a₁ b₁) (a₂ ⊗ₜ[R] b₂) = (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂) := rfl /-- (Implementation detail) The multiplication map on `A ⊗[R] B`, as an `R`-bilinear map. -/ def mul : (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) := tensor_product.lift $ linear_map.mk₂ R mul_aux (λ x₁ x₂ y, tensor_product.ext' $ λ x' y', by simp only [mul_aux_apply, linear_map.add_apply, add_mul, add_tmul]) (λ c x y, tensor_product.ext' $ λ x' y', by simp only [mul_aux_apply, linear_map.smul_apply, smul_tmul', smul_mul_assoc]) (λ x y₁ y₂, tensor_product.ext' $ λ x' y', by simp only [mul_aux_apply, linear_map.add_apply, add_mul, tmul_add]) (λ c x y, tensor_product.ext' $ λ x' y', by simp only [mul_aux_apply, linear_map.smul_apply, smul_tmul, smul_tmul', smul_mul_assoc]) @[simp] lemma mul_apply (a₁ a₂ : A) (b₁ b₂ : B) : mul (a₁ ⊗ₜ[R] b₁) (a₂ ⊗ₜ[R] b₂) = (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂) := rfl lemma mul_assoc' (mul : (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) →ₗ[R] (A ⊗[R] B)) (h : ∀ (a₁ a₂ a₃ : A) (b₁ b₂ b₃ : B), mul (mul (a₁ ⊗ₜ[R] b₁) (a₂ ⊗ₜ[R] b₂)) (a₃ ⊗ₜ[R] b₃) = mul (a₁ ⊗ₜ[R] b₁) (mul (a₂ ⊗ₜ[R] b₂) (a₃ ⊗ₜ[R] b₃))) : ∀ (x y z : A ⊗[R] B), mul (mul x y) z = mul x (mul y z) := begin intros, apply tensor_product.induction_on x, { simp only [linear_map.map_zero, linear_map.zero_apply], }, apply tensor_product.induction_on y, { simp only [linear_map.map_zero, forall_const, linear_map.zero_apply], }, apply tensor_product.induction_on z, { simp only [linear_map.map_zero, forall_const], }, { intros, simp only [h], }, { intros, simp only [linear_map.map_add, *], }, { intros, simp only [linear_map.map_add, *, linear_map.add_apply], }, { intros, simp only [linear_map.map_add, *, linear_map.add_apply], }, end lemma mul_assoc (x y z : A ⊗[R] B) : mul (mul x y) z = mul x (mul y z) := mul_assoc' mul (by { intros, simp only [mul_apply, mul_assoc], }) x y z lemma one_mul (x : A ⊗[R] B) : mul (1 ⊗ₜ 1) x = x := begin apply tensor_product.induction_on x; simp {contextual := tt}, end lemma mul_one (x : A ⊗[R] B) : mul x (1 ⊗ₜ 1) = x := begin apply tensor_product.induction_on x; simp {contextual := tt}, end instance : has_one (A ⊗[R] B) := { one := 1 ⊗ₜ 1 } instance : add_monoid_with_one (A ⊗[R] B) := add_monoid_with_one.unary instance : semiring (A ⊗[R] B) := { zero := 0, add := (+), one := 1, mul := λ a b, mul a b, one_mul := one_mul, mul_one := mul_one, mul_assoc := mul_assoc, zero_mul := by simp, mul_zero := by simp, left_distrib := by simp, right_distrib := by simp, .. (by apply_instance : add_monoid_with_one (A ⊗[R] B)), .. (by apply_instance : add_comm_monoid (A ⊗[R] B)) }. lemma one_def : (1 : A ⊗[R] B) = (1 : A) ⊗ₜ (1 : B) := rfl @[simp] lemma tmul_mul_tmul (a₁ a₂ : A) (b₁ b₂ : B) : (a₁ ⊗ₜ[R] b₁) * (a₂ ⊗ₜ[R] b₂) = (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂) := rfl @[simp] lemma tmul_pow (a : A) (b : B) (k : ℕ) : (a ⊗ₜ[R] b)^k = (a^k) ⊗ₜ[R] (b^k) := begin induction k with k ih, { simp [one_def], }, { simp [pow_succ, ih], } end /-- The ring morphism `A →+* A ⊗[R] B` sending `a` to `a ⊗ₜ 1`. -/ @[simps] def include_left_ring_hom : A →+* A ⊗[R] B := { to_fun := λ a, a ⊗ₜ 1, map_zero' := by simp, map_add' := by simp [add_tmul], map_one' := rfl, map_mul' := by simp } variables {S : Type*} [comm_semiring S] [algebra R S] [algebra S A] [is_scalar_tower R S A] instance left_algebra : algebra S (A ⊗[R] B) := { commutes' := λ r x, begin apply tensor_product.induction_on x, { simp, }, { intros a b, dsimp, rw [algebra.commutes, _root_.mul_one, _root_.one_mul], }, { intros y y' h h', dsimp at h h' ⊢, simp only [mul_add, add_mul, h, h'], }, end, smul_def' := λ r x, begin apply tensor_product.induction_on x, { simp [smul_zero], }, { intros a b, dsimp, rw [tensor_product.smul_tmul', algebra.smul_def r a, _root_.one_mul] }, { intros, dsimp, simp [smul_add, mul_add, *], }, end, .. tensor_product.include_left_ring_hom.comp (algebra_map S A), .. (by apply_instance : module S (A ⊗[R] B)) }. -- This is for the `undergrad.yaml` list. /-- The tensor product of two `R`-algebras is an `R`-algebra. -/ instance : algebra R (A ⊗[R] B) := infer_instance @[simp] lemma algebra_map_apply (r : S) : (algebra_map S (A ⊗[R] B)) r = ((algebra_map S A) r) ⊗ₜ 1 := rfl instance : is_scalar_tower R S (A ⊗[R] B) := ⟨λ a b c, by simp⟩ variables {C : Type v₃} [semiring C] [algebra R C] @[ext] theorem ext {g h : (A ⊗[R] B) →ₐ[R] C} (H : ∀ a b, g (a ⊗ₜ b) = h (a ⊗ₜ b)) : g = h := begin apply @alg_hom.to_linear_map_injective R (A ⊗[R] B) C _ _ _ _ _ _ _ _, ext, simp [H], end /-- The `R`-algebra morphism `A →ₐ[R] A ⊗[R] B` sending `a` to `a ⊗ₜ 1`. -/ def include_left : A →ₐ[R] A ⊗[R] B := { commutes' := by simp, ..include_left_ring_hom } @[simp] lemma include_left_apply (a : A) : (include_left : A →ₐ[R] A ⊗[R] B) a = a ⊗ₜ 1 := rfl /-- The algebra morphism `B →ₐ[R] A ⊗[R] B` sending `b` to `1 ⊗ₜ b`. -/ def include_right : B →ₐ[R] A ⊗[R] B := { to_fun := λ b, 1 ⊗ₜ b, map_zero' := by simp, map_add' := by simp [tmul_add], map_one' := rfl, map_mul' := by simp, commutes' := λ r, begin simp only [algebra_map_apply], transitivity r • ((1 : A) ⊗ₜ[R] (1 : B)), { rw [←tmul_smul, algebra.smul_def], simp, }, { simp [algebra.smul_def], }, end, } @[simp] lemma include_right_apply (b : B) : (include_right : B →ₐ[R] A ⊗[R] B) b = 1 ⊗ₜ b := rfl lemma include_left_comp_algebra_map {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T] [algebra R S] [algebra R T] : (include_left.to_ring_hom.comp (algebra_map R S) : R →+* S ⊗[R] T) = include_right.to_ring_hom.comp (algebra_map R T) := by { ext, simp } end semiring section ring variables {R : Type u} [comm_ring R] variables {A : Type v₁} [ring A] [algebra R A] variables {B : Type v₂} [ring B] [algebra R B] instance : ring (A ⊗[R] B) := { .. (by apply_instance : add_comm_group (A ⊗[R] B)), .. (by apply_instance : semiring (A ⊗[R] B)) }. end ring section comm_ring variables {R : Type u} [comm_ring R] variables {A : Type v₁} [comm_ring A] [algebra R A] variables {B : Type v₂} [comm_ring B] [algebra R B] instance : comm_ring (A ⊗[R] B) := { mul_comm := λ x y, begin apply tensor_product.induction_on x, { simp, }, { intros a₁ b₁, apply tensor_product.induction_on y, { simp, }, { intros a₂ b₂, simp [mul_comm], }, { intros a₂ b₂ ha hb, simp [mul_add, add_mul, ha, hb], }, }, { intros x₁ x₂ h₁ h₂, simp [mul_add, add_mul, h₁, h₂], }, end .. (by apply_instance : ring (A ⊗[R] B)) }. section right_algebra /-- `S ⊗[R] T` has a `T`-algebra structure. This is not a global instance or else the action of `S` on `S ⊗[R] S` would be ambiguous. -/ @[reducible] def right_algebra : algebra B (A ⊗[R] B) := (algebra.tensor_product.include_right.to_ring_hom : B →+* A ⊗[R] B).to_algebra local attribute [instance] tensor_product.right_algebra instance right_is_scalar_tower : is_scalar_tower R B (A ⊗[R] B) := is_scalar_tower.of_algebra_map_eq (λ r, (algebra.tensor_product.include_right.commutes r).symm) end right_algebra end comm_ring /-- Verify that typeclass search finds the ring structure on `A ⊗[ℤ] B` when `A` and `B` are merely rings, by treating both as `ℤ`-algebras. -/ example {A : Type v₁} [ring A] {B : Type v₂} [ring B] : ring (A ⊗[ℤ] B) := by apply_instance /-- Verify that typeclass search finds the comm_ring structure on `A ⊗[ℤ] B` when `A` and `B` are merely comm_rings, by treating both as `ℤ`-algebras. -/ example {A : Type v₁} [comm_ring A] {B : Type v₂} [comm_ring B] : comm_ring (A ⊗[ℤ] B) := by apply_instance /-! We now build the structure maps for the symmetric monoidal category of `R`-algebras. -/ section monoidal section variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type v₂} [semiring B] [algebra R B] variables {C : Type v₃} [semiring C] [algebra R C] variables {D : Type v₄} [semiring D] [algebra R D] /-- Build an algebra morphism from a linear map out of a tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_hom_of_linear_map_tensor_product (f : A ⊗[R] B →ₗ[R] C) (w₁ : ∀ (a₁ a₂ : A) (b₁ b₂ : B), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂)) = f (a₁ ⊗ₜ b₁) * f (a₂ ⊗ₜ b₂)) (w₂ : ∀ r, f ((algebra_map R A) r ⊗ₜ[R] 1) = (algebra_map R C) r): A ⊗[R] B →ₐ[R] C := { map_one' := by rw [←(algebra_map R C).map_one, ←w₂, (algebra_map R A).map_one]; refl, map_zero' := by rw [linear_map.to_fun_eq_coe, map_zero], map_mul' := λ x y, by { rw linear_map.to_fun_eq_coe, apply tensor_product.induction_on x, { rw [zero_mul, map_zero, zero_mul] }, { intros a₁ b₁, apply tensor_product.induction_on y, { rw [mul_zero, map_zero, mul_zero] }, { intros a₂ b₂, rw [tmul_mul_tmul, w₁] }, { intros x₁ x₂ h₁ h₂, rw [mul_add, map_add, map_add, mul_add, h₁, h₂] } }, { intros x₁ x₂ h₁ h₂, rw [add_mul, map_add, map_add, add_mul, h₁, h₂] } }, commutes' := λ r, by rw [linear_map.to_fun_eq_coe, algebra_map_apply, w₂], .. f } @[simp] lemma alg_hom_of_linear_map_tensor_product_apply (f w₁ w₂ x) : (alg_hom_of_linear_map_tensor_product f w₁ w₂ : A ⊗[R] B →ₐ[R] C) x = f x := rfl /-- Build an algebra equivalence from a linear equivalence out of a tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_equiv_of_linear_equiv_tensor_product (f : A ⊗[R] B ≃ₗ[R] C) (w₁ : ∀ (a₁ a₂ : A) (b₁ b₂ : B), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂)) = f (a₁ ⊗ₜ b₁) * f (a₂ ⊗ₜ b₂)) (w₂ : ∀ r, f ((algebra_map R A) r ⊗ₜ[R] 1) = (algebra_map R C) r): A ⊗[R] B ≃ₐ[R] C := { .. alg_hom_of_linear_map_tensor_product (f : A ⊗[R] B →ₗ[R] C) w₁ w₂, .. f } @[simp] lemma alg_equiv_of_linear_equiv_tensor_product_apply (f w₁ w₂ x) : (alg_equiv_of_linear_equiv_tensor_product f w₁ w₂ : A ⊗[R] B ≃ₐ[R] C) x = f x := rfl /-- Build an algebra equivalence from a linear equivalence out of a triple tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_equiv_of_linear_equiv_triple_tensor_product (f : ((A ⊗[R] B) ⊗[R] C) ≃ₗ[R] D) (w₁ : ∀ (a₁ a₂ : A) (b₁ b₂ : B) (c₁ c₂ : C), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂) ⊗ₜ (c₁ * c₂)) = f (a₁ ⊗ₜ b₁ ⊗ₜ c₁) * f (a₂ ⊗ₜ b₂ ⊗ₜ c₂)) (w₂ : ∀ r, f (((algebra_map R A) r ⊗ₜ[R] (1 : B)) ⊗ₜ[R] (1 : C)) = (algebra_map R D) r) : (A ⊗[R] B) ⊗[R] C ≃ₐ[R] D := { to_fun := f, map_mul' := λ x y, begin apply tensor_product.induction_on x, { simp only [map_zero, zero_mul] }, { intros ab₁ c₁, apply tensor_product.induction_on y, { simp only [map_zero, mul_zero] }, { intros ab₂ c₂, apply tensor_product.induction_on ab₁, { simp only [zero_tmul, map_zero, zero_mul] }, { intros a₁ b₁, apply tensor_product.induction_on ab₂, { simp only [zero_tmul, map_zero, mul_zero] }, { intros, simp only [tmul_mul_tmul, w₁] }, { intros x₁ x₂ h₁ h₂, simp only [tmul_mul_tmul] at h₁ h₂, simp only [tmul_mul_tmul, mul_add, add_tmul, map_add, h₁, h₂] } }, { intros x₁ x₂ h₁ h₂, simp only [tmul_mul_tmul] at h₁ h₂, simp only [tmul_mul_tmul, add_mul, add_tmul, map_add, h₁, h₂] } }, { intros x₁ x₂ h₁ h₂, simp only [tmul_mul_tmul, map_add, mul_add, add_mul, h₁, h₂], }, }, { intros x₁ x₂ h₁ h₂, simp only [tmul_mul_tmul, map_add, mul_add, add_mul, h₁, h₂], } end, commutes' := λ r, by simp [w₂], .. f } @[simp] lemma alg_equiv_of_linear_equiv_triple_tensor_product_apply (f w₁ w₂ x) : (alg_equiv_of_linear_equiv_triple_tensor_product f w₁ w₂ : (A ⊗[R] B) ⊗[R] C ≃ₐ[R] D) x = f x := rfl end variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type v₂} [semiring B] [algebra R B] variables {C : Type v₃} [semiring C] [algebra R C] variables {D : Type v₄} [semiring D] [algebra R D] section variables (R A) /-- The base ring is a left identity for the tensor product of algebra, up to algebra isomorphism. -/ protected def lid : R ⊗[R] A ≃ₐ[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.lid R A) (by simp [mul_smul]) (by simp [algebra.smul_def]) @[simp] theorem lid_tmul (r : R) (a : A) : (tensor_product.lid R A : (R ⊗ A → A)) (r ⊗ₜ a) = r • a := by simp [tensor_product.lid] /-- The base ring is a right identity for the tensor product of algebra, up to algebra isomorphism. -/ protected def rid : A ⊗[R] R ≃ₐ[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.rid R A) (by simp [mul_smul]) (by simp [algebra.smul_def]) @[simp] theorem rid_tmul (r : R) (a : A) : (tensor_product.rid R A : (A ⊗ R → A)) (a ⊗ₜ r) = r • a := by simp [tensor_product.rid] section variables (R A B) /-- The tensor product of R-algebras is commutative, up to algebra isomorphism. -/ protected def comm : A ⊗[R] B ≃ₐ[R] B ⊗[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.comm R A B) (by simp) (λ r, begin transitivity r • ((1 : B) ⊗ₜ[R] (1 : A)), { rw [←tmul_smul, algebra.smul_def], simp, }, { simp [algebra.smul_def], }, end) @[simp] theorem comm_tmul (a : A) (b : B) : (tensor_product.comm R A B : (A ⊗[R] B → B ⊗[R] A)) (a ⊗ₜ b) = (b ⊗ₜ a) := by simp [tensor_product.comm] lemma adjoin_tmul_eq_top : adjoin R {t : A ⊗[R] B | ∃ a b, a ⊗ₜ[R] b = t} = ⊤ := top_le_iff.mp $ (top_le_iff.mpr $ span_tmul_eq_top R A B).trans (span_le_adjoin R _) end section variables {R A B C} lemma assoc_aux_1 (a₁ a₂ : A) (b₁ b₂ : B) (c₁ c₂ : C) : (tensor_product.assoc R A B C) (((a₁ * a₂) ⊗ₜ[R] (b₁ * b₂)) ⊗ₜ[R] (c₁ * c₂)) = (tensor_product.assoc R A B C) ((a₁ ⊗ₜ[R] b₁) ⊗ₜ[R] c₁) * (tensor_product.assoc R A B C) ((a₂ ⊗ₜ[R] b₂) ⊗ₜ[R] c₂) := rfl lemma assoc_aux_2 (r : R) : (tensor_product.assoc R A B C) (((algebra_map R A) r ⊗ₜ[R] 1) ⊗ₜ[R] 1) = (algebra_map R (A ⊗ (B ⊗ C))) r := rfl variables (R A B C) /-- The associator for tensor product of R-algebras, as an algebra isomorphism. -/ protected def assoc : ((A ⊗[R] B) ⊗[R] C) ≃ₐ[R] (A ⊗[R] (B ⊗[R] C)) := alg_equiv_of_linear_equiv_triple_tensor_product (tensor_product.assoc.{u v₁ v₂ v₃} R A B C : (A ⊗ B ⊗ C) ≃ₗ[R] (A ⊗ (B ⊗ C))) (@algebra.tensor_product.assoc_aux_1.{u v₁ v₂ v₃} R _ A _ _ B _ _ C _ _) (@algebra.tensor_product.assoc_aux_2.{u v₁ v₂ v₃} R _ A _ _ B _ _ C _ _) variables {R A B C} @[simp] theorem assoc_tmul (a : A) (b : B) (c : C) : ((tensor_product.assoc R A B C) : (A ⊗[R] B) ⊗[R] C → A ⊗[R] (B ⊗[R] C)) ((a ⊗ₜ b) ⊗ₜ c) = a ⊗ₜ (b ⊗ₜ c) := rfl end variables {R A B C D} /-- The tensor product of a pair of algebra morphisms. -/ def map (f : A →ₐ[R] B) (g : C →ₐ[R] D) : A ⊗[R] C →ₐ[R] B ⊗[R] D := alg_hom_of_linear_map_tensor_product (tensor_product.map f.to_linear_map g.to_linear_map) (by simp) (by simp [alg_hom.commutes]) @[simp] theorem map_tmul (f : A →ₐ[R] B) (g : C →ₐ[R] D) (a : A) (c : C) : map f g (a ⊗ₜ c) = f a ⊗ₜ g c := rfl @[simp] lemma map_comp_include_left (f : A →ₐ[R] B) (g : C →ₐ[R] D) : (map f g).comp include_left = include_left.comp f := alg_hom.ext $ by simp @[simp] lemma map_comp_include_right (f : A →ₐ[R] B) (g : C →ₐ[R] D) : (map f g).comp include_right = include_right.comp g := alg_hom.ext $ by simp lemma map_range (f : A →ₐ[R] B) (g : C →ₐ[R] D) : (map f g).range = (include_left.comp f).range ⊔ (include_right.comp g).range := begin apply le_antisymm, { rw [←map_top, ←adjoin_tmul_eq_top, ←adjoin_image, adjoin_le_iff], rintros _ ⟨_, ⟨a, b, rfl⟩, rfl⟩, rw [map_tmul, ←_root_.mul_one (f a), ←_root_.one_mul (g b), ←tmul_mul_tmul], exact mul_mem_sup (alg_hom.mem_range_self _ a) (alg_hom.mem_range_self _ b) }, { rw [←map_comp_include_left f g, ←map_comp_include_right f g], exact sup_le (alg_hom.range_comp_le_range _ _) (alg_hom.range_comp_le_range _ _) }, end /-- Construct an isomorphism between tensor products of R-algebras from isomorphisms between the tensor factors. -/ def congr (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) : A ⊗[R] C ≃ₐ[R] B ⊗[R] D := alg_equiv.of_alg_hom (map f g) (map f.symm g.symm) (ext $ λ b d, by simp) (ext $ λ a c, by simp) @[simp] lemma congr_apply (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) (x) : congr f g x = (map (f : A →ₐ[R] B) (g : C →ₐ[R] D)) x := rfl @[simp] lemma congr_symm_apply (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) (x) : (congr f g).symm x = (map (f.symm : B →ₐ[R] A) (g.symm : D →ₐ[R] C)) x := rfl end end monoidal section variables {R A B S : Type*} [comm_semiring R] [semiring A] [semiring B] [comm_semiring S] variables [algebra R A] [algebra R B] [algebra R S] variables (f : A →ₐ[R] S) (g : B →ₐ[R] S) variables (R) /-- `linear_map.mul'` is an alg_hom on commutative rings. -/ def lmul' : S ⊗[R] S →ₐ[R] S := alg_hom_of_linear_map_tensor_product (linear_map.mul' R S) (λ a₁ a₂ b₁ b₂, by simp only [linear_map.mul'_apply, mul_mul_mul_comm]) (λ r, by simp only [linear_map.mul'_apply, _root_.mul_one]) variables {R} lemma lmul'_to_linear_map : (lmul' R : _ →ₐ[R] S).to_linear_map = linear_map.mul' R S := rfl @[simp] lemma lmul'_apply_tmul (a b : S) : lmul' R (a ⊗ₜ[R] b) = a * b := rfl @[simp] lemma lmul'_comp_include_left : (lmul' R : _ →ₐ[R] S).comp include_left = alg_hom.id R S := alg_hom.ext $ _root_.mul_one @[simp] lemma lmul'_comp_include_right : (lmul' R : _ →ₐ[R] S).comp include_right = alg_hom.id R S := alg_hom.ext $ _root_.one_mul /-- If `S` is commutative, for a pair of morphisms `f : A →ₐ[R] S`, `g : B →ₐ[R] S`, We obtain a map `A ⊗[R] B →ₐ[R] S` that commutes with `f`, `g` via `a ⊗ b ↦ f(a) * g(b)`. -/ def product_map : A ⊗[R] B →ₐ[R] S := (lmul' R).comp (tensor_product.map f g) @[simp] lemma product_map_apply_tmul (a : A) (b : B) : product_map f g (a ⊗ₜ b) = f a * g b := by { unfold product_map lmul', simp } lemma product_map_left_apply (a : A) : product_map f g ((include_left : A →ₐ[R] A ⊗ B) a) = f a := by simp @[simp] lemma product_map_left : (product_map f g).comp include_left = f := alg_hom.ext $ by simp lemma product_map_right_apply (b : B) : product_map f g (include_right b) = g b := by simp @[simp] lemma product_map_right : (product_map f g).comp include_right = g := alg_hom.ext $ by simp lemma product_map_range : (product_map f g).range = f.range ⊔ g.range := by rw [product_map, alg_hom.range_comp, map_range, map_sup, ←alg_hom.range_comp, ←alg_hom.range_comp, ←alg_hom.comp_assoc, ←alg_hom.comp_assoc, lmul'_comp_include_left, lmul'_comp_include_right, alg_hom.id_comp, alg_hom.id_comp] end section variables {R A A' B S : Type*} variables [comm_semiring R] [comm_semiring A] [semiring A'] [semiring B] [comm_semiring S] variables [algebra R A] [algebra R A'] [algebra A A'] [is_scalar_tower R A A'] [algebra R B] variables [algebra R S] [algebra A S] [is_scalar_tower R A S] /-- If `A`, `B` are `R`-algebras, `A'` is an `A`-algebra, then the product map of `f : A' →ₐ[A] S` and `g : B →ₐ[R] S` is an `A`-algebra homomorphism. -/ @[simps] def product_left_alg_hom (f : A' →ₐ[A] S) (g : B →ₐ[R] S) : A' ⊗[R] B →ₐ[A] S := { commutes' := λ r, by { dsimp, simp }, ..(product_map (f.restrict_scalars R) g).to_ring_hom } end section basis variables {k : Type*} [comm_ring k] (R : Type*) [ring R] [algebra k R] {M : Type*} [add_comm_monoid M] [module k M] {ι : Type*} (b : basis ι k M) /-- Given a `k`-algebra `R` and a `k`-basis of `M,` this is a `k`-linear isomorphism `R ⊗[k] M ≃ (ι →₀ R)` (which is in fact `R`-linear). -/ noncomputable def basis_aux : R ⊗[k] M ≃ₗ[k] (ι →₀ R) := (_root_.tensor_product.congr (finsupp.linear_equiv.finsupp_unique k R punit).symm b.repr) ≪≫ₗ (finsupp_tensor_finsupp k R k punit ι).trans (finsupp.lcongr (equiv.unique_prod ι punit) (_root_.tensor_product.rid k R)) variables {R} lemma basis_aux_tmul (r : R) (m : M) : basis_aux R b (r ⊗ₜ m) = r • (finsupp.map_range (algebra_map k R) (map_zero _) (b.repr m)) := begin ext, simp [basis_aux, ←algebra.commutes, algebra.smul_def], end lemma basis_aux_map_smul (r : R) (x : R ⊗[k] M) : basis_aux R b (r • x) = r • basis_aux R b x := tensor_product.induction_on x (by simp) (λ x y, by simp only [tensor_product.smul_tmul', basis_aux_tmul, smul_assoc]) (λ x y hx hy, by simp [hx, hy]) variables (R) /-- Given a `k`-algebra `R`, this is the `R`-basis of `R ⊗[k] M` induced by a `k`-basis of `M`. -/ noncomputable def basis : basis ι R (R ⊗[k] M) := { repr := { map_smul' := basis_aux_map_smul b, .. basis_aux R b } } variables {R} @[simp] lemma basis_repr_tmul (r : R) (m : M) : (basis R b).repr (r ⊗ₜ m) = r • (finsupp.map_range (algebra_map k R) (map_zero _) (b.repr m)) := basis_aux_tmul _ _ _ @[simp] lemma basis_repr_symm_apply (r : R) (i : ι) : (basis R b).repr.symm (finsupp.single i r) = r ⊗ₜ b.repr.symm (finsupp.single i 1) := by simp [basis, equiv.unique_prod_symm_apply, basis_aux] end basis end tensor_product end algebra namespace module variables {R M N : Type*} [comm_semiring R] variables [add_comm_monoid M] [add_comm_monoid N] variables [module R M] [module R N] /-- The algebra homomorphism from `End M ⊗ End N` to `End (M ⊗ N)` sending `f ⊗ₜ g` to the `tensor_product.map f g`, the tensor product of the two maps. -/ def End_tensor_End_alg_hom : (End R M) ⊗[R] (End R N) →ₐ[R] End R (M ⊗[R] N) := begin refine algebra.tensor_product.alg_hom_of_linear_map_tensor_product (hom_tensor_hom_map R M N M N) _ _, { intros f₁ f₂ g₁ g₂, simp only [hom_tensor_hom_map_apply, tensor_product.map_mul] }, { intro r, simp only [hom_tensor_hom_map_apply], ext m n, simp [smul_tmul] } end lemma End_tensor_End_alg_hom_apply (f : End R M) (g : End R N) : End_tensor_End_alg_hom (f ⊗ₜ[R] g) = tensor_product.map f g := by simp only [End_tensor_End_alg_hom, algebra.tensor_product.alg_hom_of_linear_map_tensor_product_apply, hom_tensor_hom_map_apply] end module lemma subalgebra.finite_dimensional_sup {K L : Type*} [field K] [comm_ring L] [algebra K L] (E1 E2 : subalgebra K L) [finite_dimensional K E1] [finite_dimensional K E2] : finite_dimensional K ↥(E1 ⊔ E2) := begin rw [←E1.range_val, ←E2.range_val, ←algebra.tensor_product.product_map_range], exact (algebra.tensor_product.product_map E1.val E2.val).to_linear_map.finite_dimensional_range, end namespace tensor_product.algebra variables {R A B M : Type*} variables [comm_semiring R] [add_comm_monoid M] [module R M] variables [semiring A] [semiring B] [module A M] [module B M] variables [algebra R A] [algebra R B] variables [is_scalar_tower R A M] [is_scalar_tower R B M] /-- An auxiliary definition, used for constructing the `module (A ⊗[R] B) M` in `tensor_product.algebra.module` below. -/ def module_aux : A ⊗[R] B →ₗ[R] M →ₗ[R] M := tensor_product.lift { to_fun := λ a, a • (algebra.lsmul R M : B →ₐ[R] module.End R M).to_linear_map, map_add' := λ r t, by { ext, simp only [add_smul, linear_map.add_apply] }, map_smul' := λ n r, by { ext, simp only [ring_hom.id_apply, linear_map.smul_apply, smul_assoc] } } lemma module_aux_apply (a : A) (b : B) (m : M) : module_aux (a ⊗ₜ[R] b) m = a • b • m := rfl variables [smul_comm_class A B M] /-- If `M` is a representation of two different `R`-algebras `A` and `B` whose actions commute, then it is a representation the `R`-algebra `A ⊗[R] B`. An important example arises from a semiring `S`; allowing `S` to act on itself via left and right multiplication, the roles of `R`, `A`, `B`, `M` are played by `ℕ`, `S`, `Sᵐᵒᵖ`, `S`. This example is important because a submodule of `S` as a `module` over `S ⊗[ℕ] Sᵐᵒᵖ` is a two-sided ideal. NB: This is not an instance because in the case `B = A` and `M = A ⊗[R] A` we would have a diamond of `smul` actions. Furthermore, this would not be a mere definitional diamond but a true mathematical diamond in which `A ⊗[R] A` had two distinct scalar actions on itself: one from its multiplication, and one from this would-be instance. Arguably we could live with this but in any case the real fix is to address the ambiguity in notation, probably along the lines outlined here: https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.234773.20base.20change/near/240929258 -/ protected def module : module (A ⊗[R] B) M := { smul := λ x m, module_aux x m, zero_smul := λ m, by simp only [map_zero, linear_map.zero_apply], smul_zero := λ x, by simp only [map_zero], smul_add := λ x m₁ m₂, by simp only [map_add], add_smul := λ x y m, by simp only [map_add, linear_map.add_apply], one_smul := λ m, by simp only [module_aux_apply, algebra.tensor_product.one_def, one_smul], mul_smul := λ x y m, begin apply tensor_product.induction_on x; apply tensor_product.induction_on y, { simp only [mul_zero, map_zero, linear_map.zero_apply], }, { intros a b, simp only [zero_mul, map_zero, linear_map.zero_apply], }, { intros z w hz hw, simp only [zero_mul, map_zero, linear_map.zero_apply], }, { intros a b, simp only [mul_zero, map_zero, linear_map.zero_apply], }, { intros a₁ b₁ a₂ b₂, simp only [module_aux_apply, mul_smul, smul_comm a₁ b₂, algebra.tensor_product.tmul_mul_tmul, linear_map.mul_apply], }, { intros z w hz hw a b, simp only at hz hw, simp only [mul_add, hz, hw, map_add, linear_map.add_apply], }, { intros z w hz hw, simp only [mul_zero, map_zero, linear_map.zero_apply], }, { intros a b z w hz hw, simp only at hz hw, simp only [map_add, add_mul, linear_map.add_apply, hz, hw], }, { intros u v hu hv z w hz hw, simp only at hz hw, simp only [add_mul, hz, hw, map_add, linear_map.add_apply], }, end } local attribute [instance] tensor_product.algebra.module lemma smul_def (a : A) (b : B) (m : M) : (a ⊗ₜ[R] b) • m = a • b • m := rfl end tensor_product.algebra
0075e4b78c8b273c0bb3dabb6207263d5bc874db
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/box_integral/basic.lean
a02b18afc77bdf520763efc17291387c3531c8a9
[ "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
43,394
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.box_integral.partition.filter import analysis.box_integral.partition.measure import topology.uniform_space.compact /-! # Integrals of Riemann, Henstock-Kurzweil, and McShane > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define the integral of a function over a box in `ℝⁿ. The same definition works for Riemann, Henstock-Kurzweil, and McShane integrals. As usual, we represent `ℝⁿ` as the type of functions `ι → ℝ` for some finite type `ι`. A rectangular box `(l, u]` in `ℝⁿ` is defined to be the set `{x : ι → ℝ | ∀ i, l i < x i ∧ x i ≤ u i}`, see `box_integral.box`. Let `vol` be a box-additive function on boxes in `ℝⁿ` with codomain `E →L[ℝ] F`. Given a function `f : ℝⁿ → E`, a box `I` and a tagged partition `π` of this box, the *integral sum* of `f` over `π` with respect to the volume `vol` is the sum of `vol J (f (π.tag J))` over all boxes of `π`. Here `π.tag J` is the point (tag) in `ℝⁿ` associated with the box `J`. The integral is defined as the limit of integral sums along a filter. Different filters correspond to different integration theories. In order to avoid code duplication, all our definitions and theorems take an argument `l : box_integral.integration_params`. This is a type that holds three boolean values, and encodes eight filters including those corresponding to Riemann, Henstock-Kurzweil, and McShane integrals. Following the design of infinite sums (see `has_sum` and `tsum`), we define a predicate `box_integral.has_integral` and a function `box_integral.integral` that returns a vector satisfying the predicate or zero if the function is not integrable. Then we prove some basic properties of box integrals (linearity, a formula for the integral of a constant). We also prove a version of the Henstock-Sacks inequality (see `box_integral.integrable.dist_integral_sum_le_of_mem_base_set` and `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq`), prove integrability of continuous functions, and provide a criterion for integrability w.r.t. a non-Riemann filter (e.g., Henstock-Kurzweil and McShane). ## Notation - `ℝⁿ`: local notation for `ι → ℝ` ## Tags integral -/ open_locale big_operators classical topology nnreal filter uniformity box_integral open set finset function filter metric box_integral.integration_params noncomputable theory namespace box_integral universes u v w variables {ι : Type u} {E : Type v} {F : Type w} [normed_add_comm_group E] [normed_space ℝ E] [normed_add_comm_group F] [normed_space ℝ F] {I J : box ι} {π : tagged_prepartition I} open tagged_prepartition local notation `ℝⁿ` := ι → ℝ /-! ### Integral sum and its basic properties -/ /-- The integral sum of `f : ℝⁿ → E` over a tagged prepartition `π` w.r.t. box-additive volume `vol` with codomain `E →L[ℝ] F` is the sum of `vol J (f (π.tag J))` over all boxes of `π`. -/ def integral_sum (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : F := ∑ J in π.boxes, vol J (f (π.tag J)) lemma integral_sum_bUnion_tagged (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : prepartition I) (πi : Π J, tagged_prepartition J) : integral_sum f vol (π.bUnion_tagged πi) = ∑ J in π.boxes, integral_sum f vol (πi J) := begin refine (π.sum_bUnion_boxes _ _).trans (sum_congr rfl $ λ J hJ, sum_congr rfl $ λ J' hJ', _), rw π.tag_bUnion_tagged hJ hJ' end lemma integral_sum_bUnion_partition (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) (πi : Π J, prepartition J) (hπi : ∀ J ∈ π, (πi J).is_partition) : integral_sum f vol (π.bUnion_prepartition πi) = integral_sum f vol π := begin refine (π.to_prepartition.sum_bUnion_boxes _ _).trans (sum_congr rfl $ λ J hJ, _), calc ∑ J' in (πi J).boxes, vol J' (f (π.tag $ π.to_prepartition.bUnion_index πi J')) = ∑ J' in (πi J).boxes, vol J' (f (π.tag J)) : sum_congr rfl (λ J' hJ', by rw [prepartition.bUnion_index_of_mem _ hJ hJ']) ... = vol J (f (π.tag J)) : (vol.map ⟨λ g : E →L[ℝ] F, g (f (π.tag J)), rfl, λ _ _, rfl⟩).sum_partition_boxes le_top (hπi J hJ) end lemma integral_sum_inf_partition (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) {π' : prepartition I} (h : π'.is_partition) : integral_sum f vol (π.inf_prepartition π') = integral_sum f vol π := integral_sum_bUnion_partition f vol π _ $ λ J hJ, h.restrict (prepartition.le_of_mem _ hJ) lemma integral_sum_fiberwise {α} (g : box ι → α) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : ∑ y in π.boxes.image g, integral_sum f vol (π.filter (λ x, g x = y)) = integral_sum f vol π := π.to_prepartition.sum_fiberwise g (λ J, vol J (f $ π.tag J)) lemma integral_sum_sub_partitions (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) {π₁ π₂ : tagged_prepartition I} (h₁ : π₁.is_partition) (h₂ : π₂.is_partition) : integral_sum f vol π₁ - integral_sum f vol π₂ = ∑ J in (π₁.to_prepartition ⊓ π₂.to_prepartition).boxes, (vol J (f $ (π₁.inf_prepartition π₂.to_prepartition).tag J) - vol J (f $ (π₂.inf_prepartition π₁.to_prepartition).tag J)) := begin rw [← integral_sum_inf_partition f vol π₁ h₂, ← integral_sum_inf_partition f vol π₂ h₁, integral_sum, integral_sum, finset.sum_sub_distrib], simp only [inf_prepartition_to_prepartition, _root_.inf_comm] end @[simp] lemma integral_sum_disj_union (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) {π₁ π₂ : tagged_prepartition I} (h : disjoint π₁.Union π₂.Union) : integral_sum f vol (π₁.disj_union π₂ h) = integral_sum f vol π₁ + integral_sum f vol π₂ := begin refine (prepartition.sum_disj_union_boxes h _).trans (congr_arg2 (+) (sum_congr rfl $ λ J hJ, _) (sum_congr rfl $ λ J hJ, _)), { rw disj_union_tag_of_mem_left _ hJ }, { rw disj_union_tag_of_mem_right _ hJ } end @[simp] lemma integral_sum_add (f g : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : integral_sum (f + g) vol π = integral_sum f vol π + integral_sum g vol π := by simp only [integral_sum, pi.add_apply, (vol _).map_add, finset.sum_add_distrib] @[simp] lemma integral_sum_neg (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : integral_sum (-f) vol π = -integral_sum f vol π := by simp only [integral_sum, pi.neg_apply, (vol _).map_neg, finset.sum_neg_distrib] @[simp] lemma integral_sum_smul (c : ℝ) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : integral_sum (c • f) vol π = c • integral_sum f vol π := by simp only [integral_sum, finset.smul_sum, pi.smul_apply, continuous_linear_map.map_smul] variables [fintype ι] /-! ### Basic integrability theory -/ /-- The predicate `has_integral I l f vol y` says that `y` is the integral of `f` over `I` along `l` w.r.t. volume `vol`. This means that integral sums of `f` tend to `𝓝 y` along `box_integral.integration_params.to_filter_Union I ⊤`. -/ def has_integral (I : box ι) (l : integration_params) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (y : F) : Prop := tendsto (integral_sum f vol) (l.to_filter_Union I ⊤) (𝓝 y) /-- A function is integrable if there exists a vector that satisfies the `has_integral` predicate. -/ def integrable (I : box ι) (l : integration_params) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) := ∃ y, has_integral I l f vol y /-- The integral of a function `f` over a box `I` along a filter `l` w.r.t. a volume `vol`. Returns zero on non-integrable functions. -/ def integral (I : box ι) (l : integration_params) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) := if h : integrable I l f vol then h.some else 0 variables {l : integration_params} {f g : ℝⁿ → E} {vol : ι →ᵇᵃ (E →L[ℝ] F)} {y y' : F} /-- Reinterpret `box_integral.has_integral` as `filter.tendsto`, e.g., dot-notation theorems that are shadowed in the `box_integral.has_integral` namespace. -/ lemma has_integral.tendsto (h : has_integral I l f vol y) : tendsto (integral_sum f vol) (l.to_filter_Union I ⊤) (𝓝 y) := h /-- The `ε`-`δ` definition of `box_integral.has_integral`. -/ lemma has_integral_iff : has_integral I l f vol y ↔ ∀ ε > (0 : ℝ), ∃ r : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ), (∀ c, l.r_cond (r c)) ∧ ∀ c π, l.mem_base_set I c (r c) π → is_partition π → dist (integral_sum f vol π) y ≤ ε := ((l.has_basis_to_filter_Union_top I).tendsto_iff nhds_basis_closed_ball).trans $ by simp [@forall_swap ℝ≥0 (tagged_prepartition I)] /-- Quite often it is more natural to prove an estimate of the form `a * ε`, not `ε` in the RHS of `box_integral.has_integral_iff`, so we provide this auxiliary lemma. -/ lemma has_integral_of_mul (a : ℝ) (h : ∀ ε : ℝ, 0 < ε → ∃ r: ℝ≥0 → ℝⁿ → Ioi (0 : ℝ), (∀ c, l.r_cond (r c)) ∧ ∀ c π, l.mem_base_set I c (r c) π → is_partition π → dist (integral_sum f vol π) y ≤ a * ε) : has_integral I l f vol y := begin refine has_integral_iff.2 (λ ε hε, _), rcases exists_pos_mul_lt hε a with ⟨ε', hε', ha⟩, rcases h ε' hε' with ⟨r, hr, H⟩, exact ⟨r, hr, λ c π hπ hπp, (H c π hπ hπp).trans ha.le⟩ end lemma integrable_iff_cauchy [complete_space F] : integrable I l f vol ↔ cauchy ((l.to_filter_Union I ⊤).map (integral_sum f vol)) := cauchy_map_iff_exists_tendsto.symm /-- In a complete space, a function is integrable if and only if its integral sums form a Cauchy net. Here we restate this fact in terms of `∀ ε > 0, ∃ r, ...`. -/ lemma integrable_iff_cauchy_basis [complete_space F] : integrable I l f vol ↔ ∀ ε > (0 : ℝ), ∃ r : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ), (∀ c, l.r_cond (r c)) ∧ ∀ c₁ c₂ π₁ π₂, l.mem_base_set I c₁ (r c₁) π₁ → π₁.is_partition → l.mem_base_set I c₂ (r c₂) π₂ → π₂.is_partition → dist (integral_sum f vol π₁) (integral_sum f vol π₂) ≤ ε := begin rw [integrable_iff_cauchy, cauchy_map_iff', (l.has_basis_to_filter_Union_top _).prod_self.tendsto_iff uniformity_basis_dist_le], refine forall₂_congr (λ ε ε0, exists_congr $ λ r, _), simp only [exists_prop, prod.forall, set.mem_Union, exists_imp_distrib, prod_mk_mem_set_prod_eq, and_imp, mem_inter_iff, mem_set_of_eq], exact and_congr iff.rfl ⟨λ H c₁ c₂ π₁ π₂ h₁ hU₁ h₂ hU₂, H π₁ π₂ c₁ h₁ hU₁ c₂ h₂ hU₂, λ H π₁ π₂ c₁ h₁ hU₁ c₂ h₂ hU₂, H c₁ c₂ π₁ π₂ h₁ hU₁ h₂ hU₂⟩ end lemma has_integral.mono {l₁ l₂ : integration_params} (h : has_integral I l₁ f vol y) (hl : l₂ ≤ l₁) : has_integral I l₂ f vol y := h.mono_left $ integration_params.to_filter_Union_mono _ hl _ protected lemma integrable.has_integral (h : integrable I l f vol) : has_integral I l f vol (integral I l f vol) := by { rw [integral, dif_pos h], exact classical.some_spec h } lemma integrable.mono {l'} (h : integrable I l f vol) (hle : l' ≤ l) : integrable I l' f vol := ⟨_, h.has_integral.mono hle⟩ lemma has_integral.unique (h : has_integral I l f vol y) (h' : has_integral I l f vol y') : y = y' := tendsto_nhds_unique h h' lemma has_integral.integrable (h : has_integral I l f vol y) : integrable I l f vol := ⟨_, h⟩ lemma has_integral.integral_eq (h : has_integral I l f vol y) : integral I l f vol = y := h.integrable.has_integral.unique h lemma has_integral.add (h : has_integral I l f vol y) (h' : has_integral I l g vol y') : has_integral I l (f + g) vol (y + y') := by simpa only [has_integral, ← integral_sum_add] using h.add h' lemma integrable.add (hf : integrable I l f vol) (hg : integrable I l g vol) : integrable I l (f + g) vol := (hf.has_integral.add hg.has_integral).integrable lemma integral_add (hf : integrable I l f vol) (hg : integrable I l g vol) : integral I l (f + g) vol = integral I l f vol + integral I l g vol := (hf.has_integral.add hg.has_integral).integral_eq lemma has_integral.neg (hf : has_integral I l f vol y) : has_integral I l (-f) vol (-y) := by simpa only [has_integral, ← integral_sum_neg] using hf.neg lemma integrable.neg (hf : integrable I l f vol) : integrable I l (-f) vol := hf.has_integral.neg.integrable lemma integrable.of_neg (hf : integrable I l (-f) vol) : integrable I l f vol := neg_neg f ▸ hf.neg @[simp] lemma integrable_neg : integrable I l (-f) vol ↔ integrable I l f vol := ⟨λ h, h.of_neg, λ h, h.neg⟩ @[simp] lemma integral_neg : integral I l (-f) vol = -integral I l f vol := if h : integrable I l f vol then h.has_integral.neg.integral_eq else by rw [integral, integral, dif_neg h, dif_neg (mt integrable.of_neg h), neg_zero] lemma has_integral.sub (h : has_integral I l f vol y) (h' : has_integral I l g vol y') : has_integral I l (f - g) vol (y - y') := by simpa only [sub_eq_add_neg] using h.add h'.neg lemma integrable.sub (hf : integrable I l f vol) (hg : integrable I l g vol) : integrable I l (f - g) vol := (hf.has_integral.sub hg.has_integral).integrable lemma integral_sub (hf : integrable I l f vol) (hg : integrable I l g vol) : integral I l (f - g) vol = integral I l f vol - integral I l g vol := (hf.has_integral.sub hg.has_integral).integral_eq lemma has_integral_const (c : E) : has_integral I l (λ _, c) vol (vol I c) := tendsto_const_nhds.congr' $ (l.eventually_is_partition I).mono $ λ π hπ, ((vol.map ⟨λ g : E →L[ℝ] F, g c, rfl, λ _ _, rfl⟩).sum_partition_boxes le_top hπ).symm @[simp] lemma integral_const (c : E) : integral I l (λ _, c) vol = vol I c := (has_integral_const c).integral_eq lemma integrable_const (c : E) : integrable I l (λ _, c) vol := ⟨_, has_integral_const c⟩ lemma has_integral_zero : has_integral I l (λ _, (0:E)) vol 0 := by simpa only [← (vol I).map_zero] using has_integral_const (0 : E) lemma integrable_zero : integrable I l (λ _, (0:E)) vol := ⟨0, has_integral_zero⟩ lemma integral_zero : integral I l (λ _, (0:E)) vol = 0 := has_integral_zero.integral_eq lemma has_integral_sum {α : Type*} {s : finset α} {f : α → ℝⁿ → E} {g : α → F} (h : ∀ i ∈ s, has_integral I l (f i) vol (g i)) : has_integral I l (λ x, ∑ i in s, f i x) vol (∑ i in s, g i) := begin induction s using finset.induction_on with a s ha ihs, { simp [has_integral_zero] }, simp only [finset.sum_insert ha], rw finset.forall_mem_insert at h, exact h.1.add (ihs h.2) end lemma has_integral.smul (hf : has_integral I l f vol y) (c : ℝ) : has_integral I l (c • f) vol (c • y) := by simpa only [has_integral, ← integral_sum_smul] using (tendsto_const_nhds : tendsto _ _ (𝓝 c)).smul hf lemma integrable.smul (hf : integrable I l f vol) (c : ℝ) : integrable I l (c • f) vol := (hf.has_integral.smul c).integrable lemma integrable.of_smul {c : ℝ} (hf : integrable I l (c • f) vol) (hc : c ≠ 0) : integrable I l f vol := by { convert hf.smul c⁻¹, ext x, simp only [pi.smul_apply, inv_smul_smul₀ hc] } @[simp] lemma integral_smul (c : ℝ) : integral I l (λ x, c • f x) vol = c • integral I l f vol := begin rcases eq_or_ne c 0 with rfl | hc, { simp only [zero_smul, integral_zero] }, by_cases hf : integrable I l f vol, { exact (hf.has_integral.smul c).integral_eq }, { have : ¬integrable I l (λ x, c • f x) vol, from mt (λ h, h.of_smul hc) hf, rw [integral, integral, dif_neg hf, dif_neg this, smul_zero] } end open measure_theory /-- The integral of a nonnegative function w.r.t. a volume generated by a locally-finite measure is nonnegative. -/ lemma integral_nonneg {g : ℝⁿ → ℝ} (hg : ∀ x ∈ I.Icc, 0 ≤ g x) (μ : measure ℝⁿ) [is_locally_finite_measure μ] : 0 ≤ integral I l g μ.to_box_additive.to_smul := begin by_cases hgi : integrable I l g μ.to_box_additive.to_smul, { refine ge_of_tendsto' hgi.has_integral (λ π, sum_nonneg $ λ J hJ, _), exact mul_nonneg ennreal.to_real_nonneg (hg _ $ π.tag_mem_Icc _) }, { rw [integral, dif_neg hgi] } end /-- If `‖f x‖ ≤ g x` on `[l, u]` and `g` is integrable, then the norm of the integral of `f` is less than or equal to the integral of `g`. -/ lemma norm_integral_le_of_norm_le {g : ℝⁿ → ℝ} (hle : ∀ x ∈ I.Icc, ‖f x‖ ≤ g x) (μ : measure ℝⁿ) [is_locally_finite_measure μ] (hg : integrable I l g μ.to_box_additive.to_smul) : ‖(integral I l f μ.to_box_additive.to_smul : E)‖ ≤ integral I l g μ.to_box_additive.to_smul := begin by_cases hfi : integrable.{u v v} I l f μ.to_box_additive.to_smul, { refine le_of_tendsto_of_tendsto' hfi.has_integral.norm hg.has_integral (λ π, _), refine norm_sum_le_of_le _ (λ J hJ, _), simp only [box_additive_map.to_smul_apply, norm_smul, smul_eq_mul, real.norm_eq_abs, μ.to_box_additive_apply, abs_of_nonneg ennreal.to_real_nonneg], exact mul_le_mul_of_nonneg_left (hle _ $ π.tag_mem_Icc _) ennreal.to_real_nonneg }, { rw [integral, dif_neg hfi, norm_zero], exact integral_nonneg (λ x hx, (norm_nonneg _).trans (hle x hx)) μ } end lemma norm_integral_le_of_le_const {c : ℝ} (hc : ∀ x ∈ I.Icc, ‖f x‖ ≤ c) (μ : measure ℝⁿ) [is_locally_finite_measure μ] : ‖(integral I l f μ.to_box_additive.to_smul : E)‖ ≤ (μ I).to_real * c := by simpa only [integral_const] using norm_integral_le_of_norm_le hc μ (integrable_const c) /-! # Henstock-Sacks inequality and integrability on subboxes Henstock-Sacks inequality for Henstock-Kurzweil integral says the following. Let `f` be a function integrable on a box `I`; let `r : ℝⁿ → (0, ∞)` be a function such that for any tagged partition of `I` subordinate to `r`, the integral sum over this partition is `ε`-close to the integral. Then for any tagged prepartition (i.e. a finite collections of pairwise disjoint subboxes of `I` with tagged points) `π`, the integral sum over `π` differs from the integral of `f` over the part of `I` covered by `π` by at most `ε`. The actual statement in the library is a bit more complicated to make it work for any `box_integral.integration_params`. We formalize several versions of this inequality in `box_integral.integrable.dist_integral_sum_le_of_mem_base_set`, `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq`, and `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set`. Instead of using predicate assumptions on `r`, we define `box_integral.integrable.convergence_r (h : integrable I l f vol) (ε : ℝ) (c : ℝ≥0) : ℝⁿ → (0, ∞)` to be a function `r` such that - if `l.bRiemann`, then `r` is a constant; - if `ε > 0`, then for any tagged partition `π` of `I` subordinate to `r` (more precisely, satisfying the predicate `l.mem_base_set I c r`), the integral sum of `f` over `π` differs from the integral of `f` over `I` by at most `ε`. The proof is mostly based on [Russel A. Gordon, *The integrals of Lebesgue, Denjoy, Perron, and Henstock*][Gordon55]. -/ namespace integrable /-- If `ε > 0`, then `box_integral.integrable.convergence_r` is a function `r : ℝ≥0 → ℝⁿ → (0, ∞)` such that for every `c : ℝ≥0`, for every tagged partition `π` subordinate to `r` (and satisfying additional distortion estimates if `box_integral.integration_params.bDistortion l = tt`), the corresponding integral sum is `ε`-close to the integral. If `box.integral.integration_params.bRiemann = tt`, then `r c x` does not depend on `x`. If `ε ≤ 0`, then we use `r c x = 1`. -/ def convergence_r (h : integrable I l f vol) (ε : ℝ) : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ) := if hε : 0 < ε then (has_integral_iff.1 h.has_integral ε hε).some else λ _ _, ⟨1, set.mem_Ioi.2 zero_lt_one⟩ variables {c c₁ c₂ : ℝ≥0} {ε ε₁ ε₂ : ℝ} {π₁ π₂ : tagged_prepartition I} lemma convergence_r_cond (h : integrable I l f vol) (ε : ℝ) (c : ℝ≥0) : l.r_cond (h.convergence_r ε c) := begin rw convergence_r, split_ifs with h₀, exacts [(has_integral_iff.1 h.has_integral ε h₀).some_spec.1 _, λ _ x, rfl] end lemma dist_integral_sum_integral_le_of_mem_base_set (h : integrable I l f vol) (h₀ : 0 < ε) (hπ : l.mem_base_set I c (h.convergence_r ε c) π) (hπp : π.is_partition) : dist (integral_sum f vol π) (integral I l f vol) ≤ ε := begin rw [convergence_r, dif_pos h₀] at hπ, exact (has_integral_iff.1 h.has_integral ε h₀).some_spec.2 c _ hπ hπp end /-- **Henstock-Sacks inequality**. Let `r₁ r₂ : ℝⁿ → (0, ∞)` be function such that for any tagged *partition* of `I` subordinate to `rₖ`, `k=1,2`, the integral sum of `f` over this partition differs from the integral of `f` by at most `εₖ`. Then for any two tagged *prepartition* `π₁ π₂` subordinate to `r₁` and `r₂` respectively and covering the same part of `I`, the integral sums of `f` over these prepartitions differ from each other by at most `ε₁ + ε₂`. The actual statement - uses `box_integral.integrable.convergence_r` instead of a predicate assumption on `r`; - uses `box_integral.integration_params.mem_base_set` instead of “subordinate to `r`” to account for additional requirements like being a Henstock partition or having a bounded distortion. See also `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq` and `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set`. -/ lemma dist_integral_sum_le_of_mem_base_set (h : integrable I l f vol) (hpos₁ : 0 < ε₁) (hpos₂ : 0 < ε₂) (h₁ : l.mem_base_set I c₁ (h.convergence_r ε₁ c₁) π₁) (h₂ : l.mem_base_set I c₂ (h.convergence_r ε₂ c₂) π₂) (HU : π₁.Union = π₂.Union) : dist (integral_sum f vol π₁) (integral_sum f vol π₂) ≤ ε₁ + ε₂ := begin rcases h₁.exists_common_compl h₂ HU with ⟨π, hπU, hπc₁, hπc₂⟩, set r : ℝⁿ → Ioi (0 : ℝ) := λ x, min (h.convergence_r ε₁ c₁ x) (h.convergence_r ε₂ c₂ x), have hr : l.r_cond r := (h.convergence_r_cond _ c₁).min (h.convergence_r_cond _ c₂), set πr := π.to_subordinate r, have H₁ : dist (integral_sum f vol (π₁.union_compl_to_subordinate π hπU r)) (integral I l f vol) ≤ ε₁, from h.dist_integral_sum_integral_le_of_mem_base_set hpos₁ (h₁.union_compl_to_subordinate (λ _ _, min_le_left _ _) hπU hπc₁) (is_partition_union_compl_to_subordinate _ _ _ _), rw HU at hπU, have H₂ : dist (integral_sum f vol (π₂.union_compl_to_subordinate π hπU r)) (integral I l f vol) ≤ ε₂, from h.dist_integral_sum_integral_le_of_mem_base_set hpos₂ (h₂.union_compl_to_subordinate (λ _ _, min_le_right _ _) hπU hπc₂) (is_partition_union_compl_to_subordinate _ _ _ _), simpa [union_compl_to_subordinate] using (dist_triangle_right _ _ _).trans (add_le_add H₁ H₂) end /-- If `f` is integrable on `I` along `l`, then for two sufficiently fine tagged prepartitions (in the sense of the filter `box_integral.integration_params.to_filter l I`) such that they cover the same part of `I`, the integral sums of `f` over `π₁` and `π₂` are very close to each other. -/ lemma tendsto_integral_sum_to_filter_prod_self_inf_Union_eq_uniformity (h : integrable I l f vol) : tendsto (λ π : tagged_prepartition I × tagged_prepartition I, (integral_sum f vol π.1, integral_sum f vol π.2)) ((l.to_filter I ×ᶠ l.to_filter I) ⊓ 𝓟 {π | π.1.Union = π.2.Union}) (𝓤 F) := begin refine (((l.has_basis_to_filter I).prod_self.inf_principal _).tendsto_iff uniformity_basis_dist_le).2 (λ ε ε0, _), replace ε0 := half_pos ε0, use [h.convergence_r (ε / 2), h.convergence_r_cond (ε / 2)], rintro ⟨π₁, π₂⟩ ⟨⟨h₁, h₂⟩, hU⟩, rw ← add_halves ε, exact h.dist_integral_sum_le_of_mem_base_set ε0 ε0 h₁.some_spec h₂.some_spec hU end /-- If `f` is integrable on a box `I` along `l`, then for any fixed subset `s` of `I` that can be represented as a finite union of boxes, the integral sums of `f` over tagged prepartitions that cover exactly `s` form a Cauchy “sequence” along `l`. -/ lemma cauchy_map_integral_sum_to_filter_Union (h : integrable I l f vol) (π₀ : prepartition I) : cauchy ((l.to_filter_Union I π₀).map (integral_sum f vol)) := begin refine ⟨infer_instance, _⟩, rw [prod_map_map_eq, ← to_filter_inf_Union_eq, ← prod_inf_prod, prod_principal_principal], exact h.tendsto_integral_sum_to_filter_prod_self_inf_Union_eq_uniformity.mono_left (inf_le_inf_left _ $ principal_mono.2 $ λ π h, h.1.trans h.2.symm) end variable [complete_space F] lemma to_subbox_aux (h : integrable I l f vol) (hJ : J ≤ I) : ∃ y : F, has_integral J l f vol y ∧ tendsto (integral_sum f vol) (l.to_filter_Union I (prepartition.single I J hJ)) (𝓝 y) := begin refine (cauchy_map_iff_exists_tendsto.1 (h.cauchy_map_integral_sum_to_filter_Union (prepartition.single I J hJ))).imp (λ y hy, ⟨_, hy⟩), convert hy.comp (l.tendsto_embed_box_to_filter_Union_top hJ) -- faster than `exact` here end /-- If `f` is integrable on a box `I`, then it is integrable on any subbox of `I`. -/ lemma to_subbox (h : integrable I l f vol) (hJ : J ≤ I) : integrable J l f vol := (h.to_subbox_aux hJ).imp $ λ y, and.left /-- If `f` is integrable on a box `I`, then integral sums of `f` over tagged prepartitions that cover exactly a subbox `J ≤ I` tend to the integral of `f` over `J` along `l`. -/ lemma tendsto_integral_sum_to_filter_Union_single (h : integrable I l f vol) (hJ : J ≤ I) : tendsto (integral_sum f vol) (l.to_filter_Union I (prepartition.single I J hJ)) (𝓝 $ integral J l f vol) := let ⟨y, h₁, h₂⟩ := h.to_subbox_aux hJ in h₁.integral_eq.symm ▸ h₂ /-- **Henstock-Sacks inequality**. Let `r : ℝⁿ → (0, ∞)` be a function such that for any tagged *partition* of `I` subordinate to `r`, the integral sum of `f` over this partition differs from the integral of `f` by at most `ε`. Then for any tagged *prepartition* `π` subordinate to `r`, the integral sum of `f` over this prepartition differs from the integral of `f` over the part of `I` covered by `π` by at most `ε`. The actual statement - uses `box_integral.integrable.convergence_r` instead of a predicate assumption on `r`; - uses `box_integral.integration_params.mem_base_set` instead of “subordinate to `r`” to account for additional requirements like being a Henstock partition or having a bounded distortion; - takes an extra argument `π₀ : prepartition I` and an assumption `π.Union = π₀.Union` instead of using `π.to_prepartition`. -/ lemma dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq (h : integrable I l f vol) (h0 : 0 < ε) (hπ : l.mem_base_set I c (h.convergence_r ε c) π) {π₀ : prepartition I} (hU : π.Union = π₀.Union) : dist (integral_sum f vol π) (∑ J in π₀.boxes, integral J l f vol) ≤ ε := begin /- Let us prove that the distance is less than or equal to `ε + δ` for all positive `δ`. -/ refine le_of_forall_pos_le_add (λ δ δ0, _), /- First we choose some constants. -/ set δ' : ℝ := δ / (π₀.boxes.card + 1), have H0 : 0 < (π₀.boxes.card + 1 : ℝ) := nat.cast_add_one_pos _, have δ'0 : 0 < δ' := div_pos δ0 H0, set C := max π₀.distortion π₀.compl.distortion, /- Next we choose a tagged partition of each `J ∈ π₀` such that the integral sum of `f` over this partition is `δ'`-close to the integral of `f` over `J`. -/ have : ∀ J ∈ π₀, ∃ πi : tagged_prepartition J, πi.is_partition ∧ dist (integral_sum f vol πi) (integral J l f vol) ≤ δ' ∧ l.mem_base_set J C (h.convergence_r δ' C) πi, { intros J hJ, have Hle : J ≤ I := π₀.le_of_mem hJ, have HJi : integrable J l f vol := h.to_subbox Hle, set r := λ x, min (h.convergence_r δ' C x) (HJi.convergence_r δ' C x), have hr : l.r_cond r, from (h.convergence_r_cond _ C).min (HJi.convergence_r_cond _ C), have hJd : J.distortion ≤ C, from le_trans (finset.le_sup hJ) (le_max_left _ _), rcases l.exists_mem_base_set_is_partition J hJd r with ⟨πJ, hC, hp⟩, have hC₁ : l.mem_base_set J C (HJi.convergence_r δ' C) πJ, { refine hC.mono J le_rfl le_rfl (λ x hx, _), exact min_le_right _ _ }, have hC₂ : l.mem_base_set J C (h.convergence_r δ' C) πJ, { refine hC.mono J le_rfl le_rfl (λ x hx, _), exact min_le_left _ _ }, exact ⟨πJ, hp, HJi.dist_integral_sum_integral_le_of_mem_base_set δ'0 hC₁ hp, hC₂⟩ }, /- Now we combine these tagged partitions into a tagged prepartition of `I` that covers the same part of `I` as `π₀` and apply `box_integral.dist_integral_sum_le_of_mem_base_set` to `π` and this prepartition. -/ choose! πi hπip hπiδ' hπiC, have : l.mem_base_set I C (h.convergence_r δ' C) (π₀.bUnion_tagged πi), from bUnion_tagged_mem_base_set hπiC hπip (λ _, le_max_right _ _), have hU' : π.Union = (π₀.bUnion_tagged πi).Union, from hU.trans (prepartition.Union_bUnion_partition _ hπip).symm, have := h.dist_integral_sum_le_of_mem_base_set h0 δ'0 hπ this hU', rw integral_sum_bUnion_tagged at this, calc dist (integral_sum f vol π) (∑ J in π₀.boxes, integral J l f vol) ≤ dist (integral_sum f vol π) (∑ J in π₀.boxes, integral_sum f vol (πi J)) + dist (∑ J in π₀.boxes, integral_sum f vol (πi J)) (∑ J in π₀.boxes, integral J l f vol) : dist_triangle _ _ _ ... ≤ (ε + δ') + ∑ J in π₀.boxes, δ' : add_le_add this (dist_sum_sum_le_of_le _ hπiδ') ... = ε + δ : by { field_simp [H0.ne'], ring } end /-- **Henstock-Sacks inequality**. Let `r : ℝⁿ → (0, ∞)` be a function such that for any tagged *partition* of `I` subordinate to `r`, the integral sum of `f` over this partition differs from the integral of `f` by at most `ε`. Then for any tagged *prepartition* `π` subordinate to `r`, the integral sum of `f` over this prepartition differs from the integral of `f` over the part of `I` covered by `π` by at most `ε`. The actual statement - uses `box_integral.integrable.convergence_r` instead of a predicate assumption on `r`; - uses `box_integral.integration_params.mem_base_set` instead of “subordinate to `r`” to account for additional requirements like being a Henstock partition or having a bounded distortion; -/ lemma dist_integral_sum_sum_integral_le_of_mem_base_set (h : integrable I l f vol) (h0 : 0 < ε) (hπ : l.mem_base_set I c (h.convergence_r ε c) π) : dist (integral_sum f vol π) (∑ J in π.boxes, integral J l f vol) ≤ ε := h.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq h0 hπ rfl /-- Integral sum of `f` over a tagged prepartition `π` such that `π.Union = π₀.Union` tends to the sum of integrals of `f` over the boxes of `π₀`. -/ lemma tendsto_integral_sum_sum_integral (h : integrable I l f vol) (π₀ : prepartition I) : tendsto (integral_sum f vol) (l.to_filter_Union I π₀) (𝓝 $ ∑ J in π₀.boxes, integral J l f vol) := begin refine ((l.has_basis_to_filter_Union I π₀).tendsto_iff nhds_basis_closed_ball).2 (λ ε ε0, _), refine ⟨h.convergence_r ε, h.convergence_r_cond ε, _⟩, simp only [mem_inter_iff, set.mem_Union, mem_set_of_eq], rintro π ⟨c, hc, hU⟩, exact h.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq ε0 hc hU end /-- If `f` is integrable on `I`, then `λ J, integral J l f vol` is box-additive on subboxes of `I`: if `π₁`, `π₂` are two prepartitions of `I` covering the same part of `I`, then the sum of integrals of `f` over the boxes of `π₁` is equal to the sum of integrals of `f` over the boxes of `π₂`. See also `box_integral.integrable.to_box_additive` for a bundled version. -/ lemma sum_integral_congr (h : integrable I l f vol) {π₁ π₂ : prepartition I} (hU : π₁.Union = π₂.Union) : ∑ J in π₁.boxes, integral J l f vol = ∑ J in π₂.boxes, integral J l f vol := begin refine tendsto_nhds_unique (h.tendsto_integral_sum_sum_integral π₁) _, rw l.to_filter_Union_congr _ hU, exact h.tendsto_integral_sum_sum_integral π₂ end /-- If `f` is integrable on `I`, then `λ J, integral J l f vol` is box-additive on subboxes of `I`: if `π₁`, `π₂` are two prepartitions of `I` covering the same part of `I`, then the sum of integrals of `f` over the boxes of `π₁` is equal to the sum of integrals of `f` over the boxes of `π₂`. See also `box_integral.integrable.sum_integral_congr` for an unbundled version. -/ @[simps] def to_box_additive (h : integrable I l f vol) : ι →ᵇᵃ[I] F := { to_fun := λ J, integral J l f vol, sum_partition_boxes' := λ J hJ π hπ, begin replace hπ := hπ.Union_eq, rw ← prepartition.Union_top at hπ, rw [(h.to_subbox (with_top.coe_le_coe.1 hJ)).sum_integral_congr hπ, prepartition.top_boxes, sum_singleton] end } end integrable open measure_theory /-! ### Integrability conditions -/ variable (l) /-- A continuous function is box-integrable with respect to any locally finite measure. This is true for any volume with bounded variation. -/ lemma integrable_of_continuous_on [complete_space E] {I : box ι} {f : ℝⁿ → E} (hc : continuous_on f I.Icc) (μ : measure ℝⁿ) [is_locally_finite_measure μ] : integrable.{u v v} I l f μ.to_box_additive.to_smul := begin have huc := I.is_compact_Icc.uniform_continuous_on_of_continuous hc, rw metric.uniform_continuous_on_iff_le at huc, refine integrable_iff_cauchy_basis.2 (λ ε ε0, _), rcases exists_pos_mul_lt ε0 (μ.to_box_additive I) with ⟨ε', ε0', hε⟩, rcases huc ε' ε0' with ⟨δ, δ0 : 0 < δ, Hδ⟩, refine ⟨λ _ _, ⟨δ / 2, half_pos δ0⟩, λ _ _ _, rfl, λ c₁ c₂ π₁ π₂ h₁ h₁p h₂ h₂p, _⟩, simp only [dist_eq_norm, integral_sum_sub_partitions _ _ h₁p h₂p, box_additive_map.to_smul_apply, ← smul_sub], have : ∀ J ∈ π₁.to_prepartition ⊓ π₂.to_prepartition, ‖μ.to_box_additive J • (f ((π₁.inf_prepartition π₂.to_prepartition).tag J) - f ((π₂.inf_prepartition π₁.to_prepartition).tag J))‖ ≤ μ.to_box_additive J * ε', { intros J hJ, have : 0 ≤ μ.to_box_additive J, from ennreal.to_real_nonneg, rw [norm_smul, real.norm_eq_abs, abs_of_nonneg this, ← dist_eq_norm], refine mul_le_mul_of_nonneg_left _ this, refine Hδ _ (tagged_prepartition.tag_mem_Icc _ _) _ (tagged_prepartition.tag_mem_Icc _ _) _, rw [← add_halves δ], refine (dist_triangle_left _ _ J.upper).trans (add_le_add (h₁.1 _ _ _) (h₂.1 _ _ _)), { exact prepartition.bUnion_index_mem _ hJ }, { exact box.le_iff_Icc.1 (prepartition.le_bUnion_index _ hJ) J.upper_mem_Icc }, { rw _root_.inf_comm at hJ, exact prepartition.bUnion_index_mem _ hJ }, { rw _root_.inf_comm at hJ, exact box.le_iff_Icc.1 (prepartition.le_bUnion_index _ hJ) J.upper_mem_Icc } }, refine (norm_sum_le_of_le _ this).trans _, rw [← finset.sum_mul, μ.to_box_additive.sum_partition_boxes le_top (h₁p.inf h₂p)], exact hε.le end variable {l} /-- This is an auxiliary lemma used to prove two statements at once. Use one of the next two lemmas instead. -/ lemma has_integral_of_bRiemann_eq_ff_of_forall_is_o (hl : l.bRiemann = ff) (B : ι →ᵇᵃ[I] ℝ) (hB0 : ∀ J, 0 ≤ B J) (g : ι →ᵇᵃ[I] F) (s : set ℝⁿ) (hs : s.countable) (hlH : s.nonempty → l.bHenstock = tt) (H₁ : ∀ (c : ℝ≥0) (x ∈ I.Icc ∩ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → x ∈ J.Icc → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε) (H₂ : ∀ (c : ℝ≥0) (x ∈ I.Icc \ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → (l.bHenstock → x ∈ J.Icc) → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε * B J) : has_integral I l f vol (g I) := begin /- We choose `r x` differently for `x ∈ s` and `x ∉ s`. For `x ∈ s`, we choose `εs` such that `∑' x : s, εs x < ε / 2 / 2 ^ #ι`, then choose `r x` so that `dist (vol J (f x)) (g J) ≤ εs x` for `J` in the `r x`-neighborhood of `x`. This guarantees that the sum of these distances over boxes `J` such that `π.tag J ∈ s` is less than `ε / 2`. We need an additional multiplier `2 ^ #ι` because different boxes can have the same tag. For `x ∉ s`, we choose `r x` so that `dist (vol (J (f x))) (g J) ≤ (ε / 2 / B I) * B J` for a box `J` in the `δ`-neighborhood of `x`. -/ refine ((l.has_basis_to_filter_Union_top _).tendsto_iff metric.nhds_basis_closed_ball).2 _, intros ε ε0, simp only [subtype.exists'] at H₁ H₂, choose! δ₁ Hδ₁ using H₁, choose! δ₂ Hδ₂ using H₂, have ε0' := half_pos ε0, have H0 : 0 < (2 ^ fintype.card ι : ℝ), from pow_pos zero_lt_two _, rcases hs.exists_pos_forall_sum_le (div_pos ε0' H0) with ⟨εs, hεs0, hεs⟩, simp only [le_div_iff' H0, mul_sum] at hεs, rcases exists_pos_mul_lt ε0' (B I) with ⟨ε', ε'0, hεI⟩, set δ : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ) := λ c x, if x ∈ s then δ₁ c x (εs x) else (δ₂ c) x ε', refine ⟨δ, λ c, l.r_cond_of_bRiemann_eq_ff hl, _⟩, simp only [set.mem_Union, mem_inter_iff, mem_set_of_eq], rintro π ⟨c, hπδ, hπp⟩, /- Now we split the sum into two parts based on whether `π.tag J` belongs to `s` or not. -/ rw [← g.sum_partition_boxes le_rfl hπp, mem_closed_ball, integral_sum, ← sum_filter_add_sum_filter_not π.boxes (λ J, π.tag J ∈ s), ← sum_filter_add_sum_filter_not π.boxes (λ J, π.tag J ∈ s), ← add_halves ε], refine dist_add_add_le_of_le _ _, { unfreezingI { rcases s.eq_empty_or_nonempty with rfl|hsne }, { simp [ε0'.le] }, /- For the boxes such that `π.tag J ∈ s`, we use the fact that at most `2 ^ #ι` boxes have the same tag. -/ specialize hlH hsne, have : ∀ J ∈ π.boxes.filter (λ J, π.tag J ∈ s), dist (vol J (f $ π.tag J)) (g J) ≤ εs (π.tag J), { intros J hJ, rw finset.mem_filter at hJ, cases hJ with hJ hJs, refine Hδ₁ c _ ⟨π.tag_mem_Icc _, hJs⟩ _ (hεs0 _) _ (π.le_of_mem' _ hJ) _ (hπδ.2 hlH J hJ) (λ hD, (finset.le_sup hJ).trans (hπδ.3 hD)), convert hπδ.1 J hJ, exact (dif_pos hJs).symm }, refine (dist_sum_sum_le_of_le _ this).trans _, rw sum_comp, refine (sum_le_sum _).trans (hεs _ _), { rintro b -, rw [← nat.cast_two, ← nat.cast_pow, ← nsmul_eq_mul], refine nsmul_le_nsmul (hεs0 _).le _, refine (finset.card_le_of_subset _).trans ((hπδ.is_Henstock hlH).card_filter_tag_eq_le b), exact filter_subset_filter _ (filter_subset _ _) }, { rw [finset.coe_image, set.image_subset_iff], exact λ J hJ, (finset.mem_filter.1 hJ).2 } }, /- Now we deal with boxes such that `π.tag J ∉ s`. In this case the estimate is straightforward. -/ have H₂ : ∀ J ∈ π.boxes.filter (λ J, π.tag J ∉ s), dist (vol J (f $ π.tag J)) (g J) ≤ ε' * B J, { intros J hJ, rw finset.mem_filter at hJ, cases hJ with hJ hJs, refine Hδ₂ c _ ⟨π.tag_mem_Icc _, hJs⟩ _ ε'0 _ (π.le_of_mem' _ hJ) _ (λ hH, hπδ.2 hH J hJ) (λ hD, (finset.le_sup hJ).trans (hπδ.3 hD)), convert hπδ.1 J hJ, exact (dif_neg hJs).symm }, refine (dist_sum_sum_le_of_le _ H₂).trans ((sum_le_sum_of_subset_of_nonneg (filter_subset _ _) _).trans _), { exact λ _ _ _, mul_nonneg ε'0.le (hB0 _) }, { rw [← mul_sum, B.sum_partition_boxes le_rfl hπp, mul_comm], exact hεI.le } end /-- A function `f` has Henstock (or `⊥`) integral over `I` is equal to the value of a box-additive function `g` on `I` provided that `vol J (f x)` is sufficiently close to `g J` for sufficiently small boxes `J ∋ x`. This lemma is useful to prove, e.g., to prove the Divergence theorem for integral along `⊥`. Let `l` be either `box_integral.integration_params.Henstock` or `⊥`. Let `g` a box-additive function on subboxes of `I`. Suppose that there exists a nonnegative box-additive function `B` and a countable set `s` with the following property. For every `c : ℝ≥0`, a point `x ∈ I.Icc`, and a positive `ε` there exists `δ > 0` such that for any box `J ≤ I` such that - `x ∈ J.Icc ⊆ metric.closed_ball x δ`; - if `l.bDistortion` (i.e., `l = ⊥`), then the distortion of `J` is less than or equal to `c`, the distance between the term `vol J (f x)` of an integral sum corresponding to `J` and `g J` is less than or equal to `ε` if `x ∈ s` and is less than or equal to `ε * B J` otherwise. Then `f` is integrable on `I along `l` with integral `g I`. -/ lemma has_integral_of_le_Henstock_of_forall_is_o (hl : l ≤ Henstock) (B : ι →ᵇᵃ[I] ℝ) (hB0 : ∀ J, 0 ≤ B J) (g : ι →ᵇᵃ[I] F) (s : set ℝⁿ) (hs : s.countable) (H₁ : ∀ (c : ℝ≥0) (x ∈ I.Icc ∩ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → x ∈ J.Icc → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε) (H₂ : ∀ (c : ℝ≥0) (x ∈ I.Icc \ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → x ∈ J.Icc → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε * B J) : has_integral I l f vol (g I) := have A : l.bHenstock, from hl.2.1.resolve_left dec_trivial, has_integral_of_bRiemann_eq_ff_of_forall_is_o (hl.1.resolve_right dec_trivial) B hB0 _ s hs (λ _, A) H₁ $ by simpa only [A, true_implies_iff] using H₂ /-- Suppose that there exists a nonnegative box-additive function `B` with the following property. For every `c : ℝ≥0`, a point `x ∈ I.Icc`, and a positive `ε` there exists `δ > 0` such that for any box `J ≤ I` such that - `J.Icc ⊆ metric.closed_ball x δ`; - if `l.bDistortion` (i.e., `l = ⊥`), then the distortion of `J` is less than or equal to `c`, the distance between the term `vol J (f x)` of an integral sum corresponding to `J` and `g J` is less than or equal to `ε * B J`. Then `f` is McShane integrable on `I` with integral `g I`. -/ lemma has_integral_McShane_of_forall_is_o (B : ι →ᵇᵃ[I] ℝ) (hB0 : ∀ J, 0 ≤ B J) (g : ι →ᵇᵃ[I] F) (H : ∀ (c : ℝ≥0) (x ∈ I.Icc) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → dist (vol J (f x)) (g J) ≤ ε * B J) : has_integral I McShane f vol (g I) := has_integral_of_bRiemann_eq_ff_of_forall_is_o rfl B hB0 g ∅ countable_empty (λ ⟨x, hx⟩, hx.elim) (λ c x hx, hx.2.elim) $ by simpa only [McShane, coe_sort_ff, false_implies_iff, true_implies_iff, diff_empty] using H end box_integral
ed033a6621b5116fa4f0424b5b6f24e7fdd94227
dc253be9829b840f15d96d986e0c13520b085033
/algebra/submodule.hlean
a325b3541fb58d8a86a0745d6636148f0cc548c2
[ "Apache-2.0" ]
permissive
cmu-phil/Spectral
4ce68e5c1ef2a812ffda5260e9f09f41b85ae0ea
3b078f5f1de251637decf04bd3fc8aa01930a6b3
refs/heads/master
1,685,119,195,535
1,684,169,772,000
1,684,169,772,000
46,450,197
42
13
null
1,505,516,767,000
1,447,883,921,000
Lean
UTF-8
Lean
false
false
26,188
hlean
/- submodules and quotient modules -/ -- Authors: Floris van Doorn, Jeremy Avigad import .left_module .quotient_group .module_chain_complex open algebra eq group sigma sigma.ops is_trunc function trunc equiv is_equiv property universe variable u namespace left_module /- submodules -/ variables {R : Ring} {M M₁ M₁' M₂ M₂' M₃ M₃' : LeftModule R} {m m₁ m₂ : M} structure is_submodule [class] (M : LeftModule R) (S : property M) : Type := (zero_mem : 0 ∈ S) (add_mem : Π⦃g h⦄, g ∈ S → h ∈ S → g + h ∈ S) (smul_mem : Π⦃g⦄ (r : R), g ∈ S → r • g ∈ S) definition zero_mem {R : Ring} {M : LeftModule R} (S : property M) [is_submodule M S] := is_submodule.zero_mem S definition add_mem {R : Ring} {M : LeftModule R} (S : property M) [is_submodule M S] := @is_submodule.add_mem R M S definition smul_mem {R : Ring} {M : LeftModule R} (S : property M) [is_submodule M S] := @is_submodule.smul_mem R M S theorem neg_mem (S : property M) [is_submodule M S] ⦃m⦄ (H : m ∈ S) : -m ∈ S := transport (λx, x ∈ S) (neg_one_smul m) (smul_mem S (- 1) H) theorem is_normal_submodule (S : property M) [is_submodule M S] ⦃m₁ m₂⦄ (H : S m₁) : S (m₂ + m₁ + (-m₂)) := transport (λx, S x) (by rewrite [add.comm, neg_add_cancel_left]) H -- open is_submodule variables {S : property M} [is_submodule M S] {S₂ : property M₂} [is_submodule M₂ S₂] definition is_subgroup_of_is_submodule [instance] (S : property M) [is_submodule M S] : is_subgroup (AddGroup_of_AddAbGroup M) S := is_subgroup.mk (zero_mem S) (add_mem S) (neg_mem S) definition is_subgroup_of_is_submodule' [instance] (S : property M) [is_submodule M S] : is_subgroup (Group_of_AbGroup (AddAbGroup_of_LeftModule M)) S := is_subgroup.mk (zero_mem S) (add_mem S) (neg_mem S) definition submodule' (S : property M) [is_submodule M S] : AddAbGroup := ab_subgroup S -- (subgroup_rel_of_submodule_rel S) definition submodule_smul [constructor] (S : property M) [is_submodule M S] (r : R) : submodule' S →a submodule' S := ab_subgroup_functor (smul_homomorphism M r) (λg, smul_mem S r) definition submodule_smul_right_distrib (r s : R) (n : submodule' S) : submodule_smul S (r + s) n = submodule_smul S r n + submodule_smul S s n := begin refine subgroup_functor_homotopy _ _ _ n ⬝ !subgroup_functor_mul⁻¹ᵖ, intro m, exact to_smul_right_distrib r s m end definition submodule_mul_smul' (r s : R) (n : submodule' S) : submodule_smul S (r * s) n = (submodule_smul S r ∘g submodule_smul S s) n := begin refine subgroup_functor_homotopy _ _ _ n ⬝ (subgroup_functor_compose _ _ _ _ n)⁻¹ᵖ, intro m, exact to_mul_smul r s m end definition submodule_mul_smul (r s : R) (n : submodule' S) : submodule_smul S (r * s) n = submodule_smul S r (submodule_smul S s n) := by rexact submodule_mul_smul' r s n definition submodule_one_smul (n : submodule' S) : submodule_smul S (1 : R) n = n := begin refine subgroup_functor_homotopy _ _ _ n ⬝ !subgroup_functor_gid, intro m, exact to_one_smul m end definition submodule (S : property M) [is_submodule M S] : LeftModule R := LeftModule_of_AddAbGroup (submodule' S) (submodule_smul S) (λr, homomorphism.addstruct (submodule_smul S r)) submodule_smul_right_distrib submodule_mul_smul submodule_one_smul definition submodule_incl [constructor] (S : property M) [is_submodule M S] : submodule S →lm M := lm_homomorphism_of_group_homomorphism (incl_of_subgroup _) begin intro r m, induction m with m hm, reflexivity end definition hom_lift [constructor] {K : property M₂} [is_submodule M₂ K] (φ : M₁ →lm M₂) (h : Π (m : M₁), φ m ∈ K) : M₁ →lm submodule K := lm_homomorphism_of_group_homomorphism (hom_lift (group_homomorphism_of_lm_homomorphism φ) _ h) begin intro r g, exact subtype_eq (to_respect_smul φ r g) end definition submodule_functor [constructor] {S : property M₁} [is_submodule M₁ S] {K : property M₂} [is_submodule M₂ K] (φ : M₁ →lm M₂) (h : Π (m : M₁), m ∈ S → φ m ∈ K) : submodule S →lm submodule K := hom_lift (φ ∘lm submodule_incl S) (by intro m; exact h m.1 m.2) definition hom_lift_compose {K : property M₃} [is_submodule M₃ K] (φ : M₂ →lm M₃) (h : Π (m : M₂), φ m ∈ K) (ψ : M₁ →lm M₂) : hom_lift φ h ∘lm ψ ~ hom_lift (φ ∘lm ψ) proof (λm, h (ψ m)) qed := by reflexivity definition hom_lift_homotopy {K : property M₂} [is_submodule M₂ K] {φ : M₁ →lm M₂} {h : Π (m : M₁), φ m ∈ K} {φ' : M₁ →lm M₂} {h' : Π (m : M₁), φ' m ∈ K} (p : φ ~ φ') : hom_lift φ h ~ hom_lift φ' h' := λg, subtype_eq (p g) definition incl_smul (S : property M) [is_submodule M S] (r : R) (m : M) (h : S m) : r • ⟨m, h⟩ = ⟨_, smul_mem S r h⟩ :> submodule S := by reflexivity definition property_submodule (S₁ S₂ : property M) [is_submodule M S₁] [is_submodule M S₂] : property (submodule S₁) := {m | submodule_incl S₁ m ∈ S₂} definition is_submodule_property_submodule [instance] (S₁ S₂ : property M) [is_submodule M S₁] [is_submodule M S₂] : is_submodule (submodule S₁) (property_submodule S₁ S₂) := is_submodule.mk (mem_property_of (zero_mem S₂)) (λm n p q, mem_property_of (add_mem S₂ (of_mem_property_of p) (of_mem_property_of q))) begin intro m r p, induction m with m hm, apply mem_property_of, apply smul_mem S₂, exact (of_mem_property_of p) end definition eq_zero_of_mem_property_submodule_trivial [constructor] {S₁ S₂ : property M} [is_submodule M S₁] [is_submodule M S₂] (h : Π⦃m⦄, m ∈ S₂ → m = 0) ⦃m : submodule S₁⦄ (Sm : m ∈ property_submodule S₁ S₂) : m = 0 := begin fapply subtype_eq, apply h (of_mem_property_of Sm) end definition is_contr_submodule (S : property M) [is_submodule M S] (H : is_contr M) : is_contr (submodule S) := have is_prop M, from _, have is_prop (submodule S), from @is_trunc_sigma _ _ _ this _, is_contr_of_inhabited_prop 0 this definition submodule_isomorphism [constructor] (S : property M) [is_submodule M S] (h : Πg, g ∈ S) : submodule S ≃lm M := isomorphism.mk (submodule_incl S) (is_equiv_incl_of_subgroup S h) definition submodule_isomorphism_submodule [constructor] {S : property M₁} [is_submodule M₁ S] {K : property M₂} [is_submodule M₂ K] (φ : M₁ ≃lm M₂) (h : Π (m : M₁), m ∈ S ↔ φ m ∈ K) : submodule S ≃lm submodule K := lm_isomorphism_of_group_isomorphism (subgroup_isomorphism_subgroup (group_isomorphism_of_lm_isomorphism φ) h) (by rexact to_respect_smul (submodule_functor φ (λg, iff.mp (h g)))) /- quotient modules -/ definition quotient_module' (S : property M) [is_submodule M S] : AddAbGroup := quotient_ab_group S -- (subgroup_rel_of_submodule_rel S) definition quotient_module_smul [constructor] (S : property M) [is_submodule M S] (r : R) : quotient_module' S →a quotient_module' S := quotient_ab_group_functor (smul_homomorphism M r) (λg, smul_mem S r) definition quotient_module_smul_right_distrib (r s : R) (n : quotient_module' S) : quotient_module_smul S (r + s) n = quotient_module_smul S r n + quotient_module_smul S s n := begin refine quotient_ab_group_functor_homotopy _ _ _ n ⬝ !quotient_ab_group_functor_mul⁻¹, intro m, exact to_smul_right_distrib r s m end definition quotient_module_mul_smul' (r s : R) (n : quotient_module' S) : quotient_module_smul S (r * s) n = (quotient_module_smul S r ∘g quotient_module_smul S s) n := begin apply eq.symm, apply eq.trans (quotient_ab_group_functor_compose _ _ _ _ n), apply quotient_ab_group_functor_homotopy, intro m, exact eq.symm (to_mul_smul r s m) end -- previous proof: -- refine quotient_ab_group_functor_homotopy _ _ _ n ⬝ -- (quotient_ab_group_functor_compose (quotient_module_smul S r) (quotient_module_smul S s) _ _ n)⁻¹ᵖ, -- intro m, to_mul_smul r s m definition quotient_module_mul_smul (r s : R) (n : quotient_module' S) : quotient_module_smul S (r * s) n = quotient_module_smul S r (quotient_module_smul S s n) := by rexact quotient_module_mul_smul' r s n definition quotient_module_one_smul (n : quotient_module' S) : quotient_module_smul S (1 : R) n = n := begin refine quotient_ab_group_functor_homotopy _ _ _ n ⬝ !quotient_ab_group_functor_gid, intro m, exact to_one_smul m end variable (S) definition quotient_module (S : property M) [is_submodule M S] : LeftModule R := LeftModule_of_AddAbGroup (quotient_module' S) (quotient_module_smul S) (λr, homomorphism.addstruct (quotient_module_smul S r)) quotient_module_smul_right_distrib quotient_module_mul_smul quotient_module_one_smul definition quotient_map [constructor] : M →lm quotient_module S := lm_homomorphism_of_group_homomorphism (ab_qg_map _) (λr g, idp) definition quotient_map_eq_zero (m : M) (H : S m) : quotient_map S m = 0 := @ab_qg_map_eq_one _ _ _ _ H definition rel_of_quotient_map_eq_zero (m : M) (H : quotient_map S m = 0) : S m := @rel_of_qg_map_eq_one _ _ _ m H variable {S} definition respect_smul_quotient_elim [constructor] (φ : M →lm M₂) (H : Π⦃m⦄, m ∈ S → φ m = 0) (r : R) (m : quotient_module S) : quotient_ab_group_elim (group_homomorphism_of_lm_homomorphism φ) H (@has_scalar.smul _ (quotient_module S) _ r m) = r • quotient_ab_group_elim (group_homomorphism_of_lm_homomorphism φ) H m := begin revert m, refine @set_quotient.rec_prop _ _ _ (λ x, !is_trunc_eq) _, intro m, exact to_respect_smul φ r m end definition quotient_elim [constructor] (φ : M →lm M₂) (H : Π⦃m⦄, m ∈ S → φ m = 0) : quotient_module S →lm M₂ := lm_homomorphism_of_group_homomorphism (quotient_ab_group_elim (group_homomorphism_of_lm_homomorphism φ) H) (respect_smul_quotient_elim φ H) definition is_prop_quotient_module (S : property M) [is_submodule M S] [H : is_prop M] : is_prop (quotient_module S) := begin apply @set_quotient.is_trunc_set_quotient, exact H end definition is_contr_quotient_module [instance] (S : property M) [is_submodule M S] (H : is_contr M) : is_contr (quotient_module S) := have is_prop M, from _, have is_prop (quotient_module S), from @set_quotient.is_trunc_set_quotient _ _ _ this, is_contr_of_inhabited_prop 0 this definition rel_of_is_contr_quotient_module (S : property M) [is_submodule M S] (H : is_contr (quotient_module S)) (m : M) : S m := rel_of_quotient_map_eq_zero S m (@eq_of_is_contr _ H _ _) definition quotient_module_isomorphism [constructor] (S : property M) [is_submodule M S] (h : Π⦃m⦄, S m → m = 0) : quotient_module S ≃lm M := (isomorphism.mk (quotient_map S) (is_equiv_ab_qg_map S h))⁻¹ˡᵐ definition quotient_module_functor [constructor] (φ : M →lm M₂) (h : Πg, g ∈ S → φ g ∈ S₂) : quotient_module S →lm quotient_module S₂ := quotient_elim (quotient_map S₂ ∘lm φ) begin intros m Hm, rexact quotient_map_eq_zero S₂ (φ m) (h m Hm) end definition quotient_module_isomorphism_quotient_module [constructor] (φ : M ≃lm M₂) (h : Πm, m ∈ S ↔ φ m ∈ S₂) : quotient_module S ≃lm quotient_module S₂ := lm_isomorphism_of_group_isomorphism (quotient_ab_group_isomorphism_quotient_ab_group (group_isomorphism_of_lm_isomorphism φ) h) (to_respect_smul (quotient_module_functor φ (λg, iff.mp (h g)))) /- specific submodules -/ definition has_scalar_image (φ : M₁ →lm M₂) ⦃m : M₂⦄ (r : R) (h : image φ m) : image φ (r • m) := begin induction h with m' p, apply image.mk (r • m'), refine to_respect_smul φ r m' ⬝ ap (λx, r • x) p, end definition is_submodule_image [instance] (φ : M₁ →lm M₂) : is_submodule M₂ (image φ) := is_submodule.mk (show 0 ∈ image (group_homomorphism_of_lm_homomorphism φ), begin apply is_subgroup.one_mem, apply is_subgroup_image end) (λ g₁ g₂ hg₁ hg₂, show g₁ + g₂ ∈ image (group_homomorphism_of_lm_homomorphism φ), begin apply @is_subgroup.mul_mem, apply is_subgroup_image, exact hg₁, exact hg₂ end) (has_scalar_image φ) /- definition image_rel [constructor] (φ : M₁ →lm M₂) : submodule_rel M₂ := submodule_rel_of_subgroup_rel (image_subgroup (group_homomorphism_of_lm_homomorphism φ)) (has_scalar_image φ) -/ definition image_trivial (φ : M₁ →lm M₂) [H : is_contr M₁] ⦃m : M₂⦄ (h : m ∈ image φ) : m = 0 := begin refine image.rec _ h, intro x p, refine p⁻¹ ⬝ ap φ _ ⬝ to_respect_zero φ, apply @is_prop.elim, apply is_trunc_succ, exact H end definition image_module [constructor] (φ : M₁ →lm M₂) : LeftModule R := submodule (image φ) -- unfortunately this is note definitionally equal: -- definition foo (φ : M₁ →lm M₂) : -- (image_module φ : AddAbGroup) = image (group_homomorphism_of_lm_homomorphism φ) := -- by reflexivity definition image_lift [constructor] (φ : M₁ →lm M₂) : M₁ →lm image_module φ := hom_lift φ (λm, image.mk m idp) definition is_surjective_image_lift (φ : M₁ →lm M₂) : is_surjective (image_lift φ) := begin refine total_image.rec _, intro m, exact image.mk m (subtype_eq idp) end variables {ψ : M₂ →lm M₃} {φ : M₁ →lm M₂} {θ : M₁ →lm M₃} {ψ' : M₂' →lm M₃'} {φ' : M₁' →lm M₂'} definition image_elim [constructor] (θ : M₁ →lm M₃) (h : Π⦃g⦄, φ g = 0 → θ g = 0) : image_module φ →lm M₃ := begin fapply homomorphism.mk, change Image (group_homomorphism_of_lm_homomorphism φ) → M₃, exact image_elim (group_homomorphism_of_lm_homomorphism θ) h, split, { exact homomorphism.struct (image_elim (group_homomorphism_of_lm_homomorphism θ) _) }, { intro r, refine @total_image.rec _ _ _ _ (λx, !is_trunc_eq) _, intro g, apply to_respect_smul } end definition image_elim_compute (h : Π⦃g⦄, φ g = 0 → θ g = 0) : image_elim θ h ∘lm image_lift φ ~ θ := begin reflexivity end -- definition image_elim_hom_lift (ψ : M →lm M₂) (h : Π⦃g⦄, φ g = 0 → θ g = 0) : -- image_elim θ h ∘lm hom_lift ψ _ ~ _ := -- begin -- reflexivity -- end definition is_contr_image_module [instance] (φ : M₁ →lm M₂) (H : is_contr M₂) : is_contr (image_module φ) := is_contr_submodule _ _ definition is_contr_image_module_of_is_contr_dom (φ : M₁ →lm M₂) (H : is_contr M₁) : is_contr (image_module φ) := is_contr.mk 0 begin have Π(x : image_module φ), is_prop (0 = x), from _, apply @total_image.rec, exact this, intro m, have h : is_contr (LeftModule.carrier M₁), from H, induction (eq_of_is_contr 0 m), apply subtype_eq, exact (to_respect_zero φ)⁻¹ end definition image_module_isomorphism [constructor] (φ : M₁ →lm M₂) (H : is_surjective φ) : image_module φ ≃lm M₂ := submodule_isomorphism _ H definition has_scalar_kernel (φ : M₁ →lm M₂) ⦃m : M₁⦄ (r : R) (p : φ m = 0) : φ (r • m) = 0 := begin refine to_respect_smul φ r m ⬝ ap (λx, r • x) p ⬝ smul_zero r, end definition lm_kernel [reducible] (φ : M₁ →lm M₂) : property M₁ := kernel (group_homomorphism_of_lm_homomorphism φ) definition is_submodule_kernel [instance] (φ : M₁ →lm M₂) : is_submodule M₁ (lm_kernel φ) := is_submodule.mk (show 0 ∈ kernel (group_homomorphism_of_lm_homomorphism φ), begin apply is_subgroup.one_mem, apply is_subgroup_kernel end) (λ g₁ g₂ hg₁ hg₂, show g₁ + g₂ ∈ kernel (group_homomorphism_of_lm_homomorphism φ), begin apply @is_subgroup.mul_mem, apply is_subgroup_kernel, exact hg₁, exact hg₂ end) (has_scalar_kernel φ) definition kernel_full (φ : M₁ →lm M₂) (H : is_contr M₂) (m : M₁) : m ∈ lm_kernel φ := !is_prop.elim definition kernel_module [reducible] (φ : M₁ →lm M₂) : LeftModule R := submodule (lm_kernel φ) definition is_contr_kernel_module [instance] (φ : M₁ →lm M₂) (H : is_contr M₁) : is_contr (kernel_module φ) := is_contr_submodule _ _ definition kernel_module_isomorphism [constructor] (φ : M₁ →lm M₂) (H : is_contr M₂) : kernel_module φ ≃lm M₁ := submodule_isomorphism _ (kernel_full φ _) definition homology_quotient_property (ψ : M₂ →lm M₃) (φ : M₁ →lm M₂) : property (kernel_module ψ) := property_submodule (lm_kernel ψ) (image (homomorphism_fn φ)) definition is_submodule_homology_property [instance] (ψ : M₂ →lm M₃) (φ : M₁ →lm M₂) : is_submodule (kernel_module ψ) (homology_quotient_property ψ φ) := (is_submodule_property_submodule _ (image φ)) definition homology (ψ : M₂ →lm M₃) (φ : M₁ →lm M₂) : LeftModule R := quotient_module (homology_quotient_property ψ φ) definition homology.mk (φ : M₁ →lm M₂) (m : M₂) (h : ψ m = 0) : homology ψ φ := quotient_map (homology_quotient_property ψ φ) ⟨m, h⟩ definition homology_eq0 {m : M₂} {hm : ψ m = 0} (h : image φ m) : homology.mk φ m hm = 0 := ab_qg_map_eq_one _ h definition homology_eq0' {m : M₂} {hm : ψ m = 0} (h : image φ m): homology.mk φ m hm = homology.mk φ 0 (to_respect_zero ψ) := ab_qg_map_eq_one _ h definition homology_eq {m n : M₂} {hm : ψ m = 0} {hn : ψ n = 0} (h : image φ (m - n)) : homology.mk φ m hm = homology.mk φ n hn := eq_of_sub_eq_zero (homology_eq0 h) definition homology_elim [constructor] (θ : M₂ →lm M) (H : Πm, θ (φ m) = 0) : homology ψ φ →lm M := quotient_elim (θ ∘lm submodule_incl _) begin intro m x, induction m with m h, esimp at *, induction x with v, exact ap θ p⁻¹ ⬝ H v -- m' end definition is_contr_homology [instance] (ψ : M₂ →lm M₃) (φ : M₁ →lm M₂) (H : is_contr M₂) : is_contr (homology ψ φ) := is_contr_quotient_module _ (is_contr_kernel_module _ _) definition homology_isomorphism [constructor] (ψ : M₂ →lm M₃) (φ : M₁ →lm M₂) (H₁ : is_contr M₁) (H₃ : is_contr M₃) : homology ψ φ ≃lm M₂ := (quotient_module_isomorphism (homology_quotient_property ψ φ) (eq_zero_of_mem_property_submodule_trivial (image_trivial _))) ⬝lm (kernel_module_isomorphism ψ _) definition homology_functor [constructor] (α₁ : M₁ →lm M₁') (α₂ : M₂ →lm M₂') (α₃ : M₃ →lm M₃') (p : hsquare ψ ψ' α₂ α₃) (q : hsquare φ φ' α₁ α₂) : homology ψ φ →lm homology ψ' φ' := begin fapply quotient_module_functor, { apply submodule_functor α₂, intro m pm, refine (p m)⁻¹ ⬝ ap α₃ pm ⬝ to_respect_zero α₃ }, { intro m pm, induction pm with m' pm', refine image.mk (α₁ m') ((q m')⁻¹ ⬝ _), exact ap α₂ pm' } end definition homology_isomorphism_homology [constructor] (α₁ : M₁ ≃lm M₁') (α₂ : M₂ ≃lm M₂') (α₃ : M₃ ≃lm M₃') (p : hsquare ψ ψ' α₂ α₃) (q : hsquare φ φ' α₁ α₂) : homology ψ φ ≃lm homology ψ' φ' := begin fapply quotient_module_isomorphism_quotient_module, { fapply submodule_isomorphism_submodule α₂, intro m, exact iff.intro (λpm, (p m)⁻¹ ⬝ ap α₃ pm ⬝ to_respect_zero α₃) (λpm, inj (equiv_of_isomorphism α₃) (p m ⬝ pm ⬝ (to_respect_zero α₃)⁻¹)) }, { intro m, apply iff.intro, { intro pm, induction pm with m' pm', refine image.mk (α₁ m') ((q m')⁻¹ ⬝ _), exact ap α₂ pm' }, { intro pm, induction pm with m' pm', refine image.mk (α₁⁻¹ˡᵐ m') _, refine (hvinverse' (equiv_of_isomorphism α₁) (equiv_of_isomorphism α₂) q m')⁻¹ ⬝ _, exact ap α₂⁻¹ˡᵐ pm' ⬝ to_left_inv (equiv_of_isomorphism α₂) m.1 }} end definition ker_in_im_of_is_contr_homology (ψ : M₂ →lm M₃) {φ : M₁ →lm M₂} (H₁ : is_contr (homology ψ φ)) {m : M₂} (p : ψ m = 0) : image φ m := rel_of_is_contr_quotient_module _ H₁ ⟨m, p⟩ definition is_embedding_of_is_contr_homology_of_constant {ψ : M₂ →lm M₃} (φ : M₁ →lm M₂) (H₁ : is_contr (homology ψ φ)) (H₂ : Πm, φ m = 0) : is_embedding ψ := begin apply to_is_embedding_homomorphism (group_homomorphism_of_lm_homomorphism ψ), intro m p, note H := rel_of_is_contr_quotient_module _ H₁ ⟨m, p⟩, induction H with n q, exact q⁻¹ ⬝ H₂ n end definition is_embedding_of_is_contr_homology_of_is_contr {ψ : M₂ →lm M₃} (φ : M₁ →lm M₂) (H₁ : is_contr (homology ψ φ)) (H₂ : is_contr M₁) : is_embedding ψ := is_embedding_of_is_contr_homology_of_constant φ H₁ (λm, ap φ (@eq_of_is_contr _ H₂ _ _) ⬝ respect_zero φ) definition is_surjective_of_is_contr_homology_of_constant (ψ : M₂ →lm M₃) {φ : M₁ →lm M₂} (H₁ : is_contr (homology ψ φ)) (H₂ : Πm, ψ m = 0) : is_surjective φ := λm, ker_in_im_of_is_contr_homology ψ H₁ (H₂ m) definition is_surjective_of_is_contr_homology_of_is_contr (ψ : M₂ →lm M₃) {φ : M₁ →lm M₂} (H₁ : is_contr (homology ψ φ)) (H₂ : is_contr M₃) : is_surjective φ := is_surjective_of_is_contr_homology_of_constant ψ H₁ (λm, @eq_of_is_contr _ H₂ _ _) definition homology_isomorphism_kernel_module (ψ : M₂ →lm M₃) (φ : M₁ →lm M₂) (H : Πm, image φ m → m = 0) : homology ψ φ ≃lm kernel_module ψ := begin apply quotient_module_isomorphism, intro m h, apply subtype_eq, apply H, exact h end definition cokernel_module (φ : M₁ →lm M₂) : LeftModule R := quotient_module (image φ) definition homology_isomorphism_cokernel_module (ψ : M₂ →lm M₃) (φ : M₁ →lm M₂) (H : Πm, ψ m = 0) : homology ψ φ ≃lm cokernel_module φ := quotient_module_isomorphism_quotient_module (submodule_isomorphism _ H) begin intro m, reflexivity end open chain_complex fin nat definition LES_of_SESs {N : succ_str} (A B C : N → LeftModule.{_ u} R) (φ : Πn, A n →lm B n) (ses : Πn : N, short_exact_mod (cokernel_module (φ (succ_str.S n))) (C n) (kernel_module (φ n))) : module_chain_complex.{_ _ u} R (stratified N 2) := begin fapply module_chain_complex.mk, { intro x, induction x with n k, induction k with k H, do 3 (cases k with k; rotate 1), { /-k≥3-/ exfalso, apply lt_le_antisymm H, apply le_add_left}, { /-k=0-/ exact B n }, { /-k=1-/ exact A n }, { /-k=2-/ exact C n }}, { intro x, induction x with n k, induction k with k H, do 3 (cases k with k; rotate 1), { /-k≥3-/ exfalso, apply lt_le_antisymm H, apply le_add_left}, { /-k=0-/ exact φ n }, { /-k=1-/ exact submodule_incl _ ∘lm short_exact_mod.g (ses n) }, { /-k=2-/ change B (succ_str.S n) →lm C n, exact short_exact_mod.f (ses n) ∘lm !quotient_map }}, { intros x m, induction x with n k, induction k with k H, do 3 (cases k with k; rotate 1), { exfalso, apply lt_le_antisymm H, apply le_add_left}, { exact (short_exact_mod.g (ses n) m).2 }, { note h := is_short_exact.im_in_ker (short_exact_mod.h (ses n)) (quotient_map _ m), exact ap pr1 h }, { refine _ ⬝ to_respect_zero (short_exact_mod.f (ses n)), rexact ap (short_exact_mod.f (ses n)) (quotient_map_eq_zero _ _ (image.mk m idp)) }} end open prod definition LES_of_SESs_zero {N : succ_str} {A B C : N → LeftModule.{_ u} R} (φ : Πn, A n →lm B n) (ses : Πn : N, short_exact_mod (cokernel_module (φ (succ_str.S n))) (C n) (kernel_module (φ n))) (n : N) : LES_of_SESs A B C φ ses (n, 0) ≃lm B n := by reflexivity definition LES_of_SESs_one {N : succ_str} {A B C : N → LeftModule.{_ u} R} (φ : Πn, A n →lm B n) (ses : Πn : N, short_exact_mod (cokernel_module (φ (succ_str.S n))) (C n) (kernel_module (φ n))) (n : N) : LES_of_SESs A B C φ ses (n, 1) ≃lm A n := by reflexivity definition LES_of_SESs_two {N : succ_str} {A B C : N → LeftModule.{_ u} R} (φ : Πn, A n →lm B n) (ses : Πn : N, short_exact_mod (cokernel_module (φ (succ_str.S n))) (C n) (kernel_module (φ n))) (n : N) : LES_of_SESs A B C φ ses (n, 2) ≃lm C n := by reflexivity definition is_exact_LES_of_SESs {N : succ_str} {A B C : N → LeftModule.{_ u} R} (φ : Πn, A n →lm B n) (ses : Πn : N, short_exact_mod (cokernel_module (φ (succ_str.S n))) (C n) (kernel_module (φ n))) : is_exact_m (LES_of_SESs A B C φ ses) := begin intros x m p, induction x with n k, induction k with k H, do 3 (cases k with k; rotate 1), { exfalso, apply lt_le_antisymm H, apply le_add_left}, { induction is_short_exact.is_surj (short_exact_mod.h (ses n)) ⟨m, p⟩ with m' q, exact image.mk m' (ap pr1 q) }, { induction is_short_exact.ker_in_im (short_exact_mod.h (ses n)) m (subtype_eq p) with m' q, induction m' using set_quotient.rec_prop with m', exact image.mk m' q }, { apply rel_of_quotient_map_eq_zero (image (φ (succ_str.S n))) m, apply @is_injective_of_is_embedding _ _ _ (is_short_exact.is_emb (short_exact_mod.h (ses n))), exact p ⬝ (to_respect_zero (short_exact_mod.f (ses n)))⁻¹ } end -- remove: -- definition homology.rec (P : homology ψ φ → Type) -- [H : Πx, is_set (P x)] (h₀ : Π(m : M₂) (h : ψ m = 0), P (homology.mk m h)) -- (h₁ : Π(m : M₂) (h : ψ m = 0) (k : image φ m), h₀ m h =[homology_eq0' k] h₀ 0 (to_respect_zero ψ)) -- : Πx, P x := -- begin -- refine @set_quotient.rec _ _ _ H _ _, -- { intro v, induction v with m h, exact h₀ m h }, -- { intro v v', induction v with m hm, induction v' with n hn, -- intro h, -- note x := h₁ (m - n) _ h, -- esimp, -- exact change_path _ _, -- } -- end -- definition quotient.rec (P : quotient_group N → Type) -- [H : Πx, is_set (P x)] (h₀ : Π(g : G), P (qg_map N g)) -- -- (h₀_mul : Π(g h : G), h₀ (g * h)) -- (h₁ : Π(g : G) (h : N g), h₀ g =[qg_map_eq_one g h] h₀ 1) -- : Πx, P x := -- begin -- refine @set_quotient.rec _ _ _ H _ _, -- { intro g, exact h₀ g }, -- { intro g g' h, -- note x := h₁ (g * g'⁻¹) h, -- } -- -- { intro v, induction }, -- -- { intro v v', induction v with m hm, induction v' with n hn, -- -- intro h, -- -- note x := h₁ (m - n) _ h, -- -- esimp, -- -- exact change_path _ _, -- -- } -- end end left_module
9a0708c337b290b2b307449d739e4a60ffafd110
8b9f17008684d796c8022dab552e42f0cb6fb347
/hott/init/relation.hlean
e68f545cbc1deee56732a7c6080ea2db86f6b5d8
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,466
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: init.relation Authors: Leonardo de Moura -/ prelude import init.logic -- TODO(Leo): remove duplication between this file and algebra/relation.lean -- We need some of the following definitions asap when "initializing" Lean. variables {A B : Type} (R : B → B → Type) local infix `≺`:50 := R definition reflexive := ∀x, x ≺ x definition symmetric := ∀⦃x y⦄, x ≺ y → y ≺ x definition transitive := ∀⦃x y z⦄, x ≺ y → y ≺ z → x ≺ z definition irreflexive := ∀x, ¬ x ≺ x definition anti_symmetric := ∀⦃x y⦄, x ≺ y → y ≺ x → x = y definition empty_relation := λa₁ a₂ : A, empty definition subrelation (Q R : B → B → Type) := ∀⦃x y⦄, Q x y → R x y definition inv_image (f : A → B) : A → A → Type := λa₁ a₂, f a₁ ≺ f a₂ definition inv_image.trans (f : A → B) (H : transitive R) : transitive (inv_image R f) := λ (a₁ a₂ a₃ : A) (H₁ : inv_image R f a₁ a₂) (H₂ : inv_image R f a₂ a₃), H H₁ H₂ definition inv_image.irreflexive (f : A → B) (H : irreflexive R) : irreflexive (inv_image R f) := λ (a : A) (H₁ : inv_image R f a a), H (f a) H₁ inductive tc {A : Type} (R : A → A → Type) : A → A → Type := | base : ∀a b, R a b → tc R a b | trans : ∀a b c, tc R a b → tc R b c → tc R a c
f5afc0c8fa8eb7ec253f3a5880b385e5bf4dc341
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/liminf_limsup.lean
5b3381d8bc7139845818d628723ca0254e17f5cc
[]
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
28,927
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Johannes Hölzl -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.filter.partial import Mathlib.order.filter.at_top_bot import Mathlib.PostPort universes u_1 u_2 u_3 namespace Mathlib /-! # liminfs and limsups of functions and filters Defines the Liminf/Limsup of a function taking values in a conditionally complete lattice, with respect to an arbitrary filter. We define `f.Limsup` (`f.Liminf`) where `f` is a filter taking values in a conditionally complete lattice. `f.Limsup` is the smallest element `a` such that, eventually, `u ≤ a` (and vice versa for `f.Liminf`). To work with the Limsup along a function `u` use `(f.map u).Limsup`. Usually, one defines the Limsup as `Inf (Sup s)` where the Inf is taken over all sets in the filter. For instance, in ℕ along a function `u`, this is `Inf_n (Sup_{k ≥ n} u k)` (and the latter quantity decreases with `n`, so this is in fact a limit.). There is however a difficulty: it is well possible that `u` is not bounded on the whole space, only eventually (think of `Limsup (λx, 1/x)` on ℝ. Then there is no guarantee that the quantity above really decreases (the value of the `Sup` beforehand is not really well defined, as one can not use ∞), so that the Inf could be anything. So one can not use this `Inf Sup ...` definition in conditionally complete lattices, and one has to use a less tractable definition. In conditionally complete lattices, the definition is only useful for filters which are eventually bounded above (otherwise, the Limsup would morally be +∞, which does not belong to the space) and which are frequently bounded below (otherwise, the Limsup would morally be -∞, which is not in the space either). We start with definitions of these concepts for arbitrary filters, before turning to the definitions of Limsup and Liminf. In complete lattices, however, it coincides with the `Inf Sup` definition. -/ namespace filter /-- `f.is_bounded (≺)`: the filter `f` is eventually bounded w.r.t. the relation `≺`, i.e. eventually, it is bounded by some uniform bound. `r` will be usually instantiated with `≤` or `≥`. -/ def is_bounded {α : Type u_1} (r : α → α → Prop) (f : filter α) := ∃ (b : α), filter.eventually (fun (x : α) => r x b) f /-- `f.is_bounded_under (≺) u`: the image of the filter `f` under `u` is eventually bounded w.r.t. the relation `≺`, i.e. eventually, it is bounded by some uniform bound. -/ def is_bounded_under {α : Type u_1} {β : Type u_2} (r : α → α → Prop) (f : filter β) (u : β → α) := is_bounded r (map u f) /-- `f` is eventually bounded if and only if, there exists an admissible set on which it is bounded. -/ theorem is_bounded_iff {α : Type u_1} {r : α → α → Prop} {f : filter α} : is_bounded r f ↔ ∃ (s : set α), ∃ (H : s ∈ sets f), ∃ (b : α), s ⊆ set_of fun (x : α) => r x b := sorry /-- A bounded function `u` is in particular eventually bounded. -/ theorem is_bounded_under_of {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {f : filter β} {u : β → α} : (∃ (b : α), ∀ (x : β), r (u x) b) → is_bounded_under r f u := sorry theorem is_bounded_bot {α : Type u_1} {r : α → α → Prop} : is_bounded r ⊥ ↔ Nonempty α := sorry theorem is_bounded_top {α : Type u_1} {r : α → α → Prop} : is_bounded r ⊤ ↔ ∃ (t : α), ∀ (x : α), r x t := sorry theorem is_bounded_principal {α : Type u_1} {r : α → α → Prop} (s : set α) : is_bounded r (principal s) ↔ ∃ (t : α), ∀ (x : α), x ∈ s → r x t := sorry theorem is_bounded_sup {α : Type u_1} {r : α → α → Prop} {f : filter α} {g : filter α} [is_trans α r] (hr : ∀ (b₁ b₂ : α), ∃ (b : α), r b₁ b ∧ r b₂ b) : is_bounded r f → is_bounded r g → is_bounded r (f ⊔ g) := sorry theorem is_bounded.mono {α : Type u_1} {r : α → α → Prop} {f : filter α} {g : filter α} (h : f ≤ g) : is_bounded r g → is_bounded r f := sorry theorem is_bounded_under.mono {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {f : filter β} {g : filter β} {u : β → α} (h : f ≤ g) : is_bounded_under r g u → is_bounded_under r f u := fun (hg : is_bounded_under r g u) => is_bounded.mono (map_mono h) hg theorem is_bounded.is_bounded_under {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {f : filter α} {q : β → β → Prop} {u : α → β} (hf : ∀ (a₀ a₁ : α), r a₀ a₁ → q (u a₀) (u a₁)) : is_bounded r f → is_bounded_under q f u := sorry /-- `is_cobounded (≺) f` states that the filter `f` does not tend to infinity w.r.t. `≺`. This is also called frequently bounded. Will be usually instantiated with `≤` or `≥`. There is a subtlety in this definition: we want `f.is_cobounded` to hold for any `f` in the case of complete lattices. This will be relevant to deduce theorems on complete lattices from their versions on conditionally complete lattices with additional assumptions. We have to be careful in the edge case of the trivial filter containing the empty set: the other natural definition `¬ ∀ a, ∀ᶠ n in f, a ≤ n` would not work as well in this case. -/ def is_cobounded {α : Type u_1} (r : α → α → Prop) (f : filter α) := ∃ (b : α), ∀ (a : α), filter.eventually (fun (x : α) => r x a) f → r b a /-- `is_cobounded_under (≺) f u` states that the image of the filter `f` under the map `u` does not tend to infinity w.r.t. `≺`. This is also called frequently bounded. Will be usually instantiated with `≤` or `≥`. -/ def is_cobounded_under {α : Type u_1} {β : Type u_2} (r : α → α → Prop) (f : filter β) (u : β → α) := is_cobounded r (map u f) /-- To check that a filter is frequently bounded, it suffices to have a witness which bounds `f` at some point for every admissible set. This is only an implication, as the other direction is wrong for the trivial filter.-/ theorem is_cobounded.mk {α : Type u_1} {r : α → α → Prop} {f : filter α} [is_trans α r] (a : α) (h : ∀ (s : set α) (H : s ∈ f), ∃ (x : α), ∃ (H : x ∈ s), r a x) : is_cobounded r f := sorry /-- A filter which is eventually bounded is in particular frequently bounded (in the opposite direction). At least if the filter is not trivial. -/ theorem is_bounded.is_cobounded_flip {α : Type u_1} {r : α → α → Prop} {f : filter α} [is_trans α r] [ne_bot f] : is_bounded r f → is_cobounded (flip r) f := sorry theorem is_cobounded_bot {α : Type u_1} {r : α → α → Prop} : is_cobounded r ⊥ ↔ ∃ (b : α), ∀ (x : α), r b x := sorry theorem is_cobounded_top {α : Type u_1} {r : α → α → Prop} : is_cobounded r ⊤ ↔ Nonempty α := sorry theorem is_cobounded_principal {α : Type u_1} {r : α → α → Prop} (s : set α) : is_cobounded r (principal s) ↔ ∃ (b : α), ∀ (a : α), (∀ (x : α), x ∈ s → r x a) → r b a := sorry theorem is_cobounded.mono {α : Type u_1} {r : α → α → Prop} {f : filter α} {g : filter α} (h : f ≤ g) : is_cobounded r f → is_cobounded r g := sorry theorem is_cobounded_le_of_bot {α : Type u_1} [order_bot α] {f : filter α} : is_cobounded LessEq f := Exists.intro ⊥ fun (a : α) (h : filter.eventually (fun (x : α) => x ≤ a) f) => bot_le theorem is_cobounded_ge_of_top {α : Type u_1} [order_top α] {f : filter α} : is_cobounded ge f := Exists.intro ⊤ fun (a : α) (h : filter.eventually (fun (x : α) => x ≥ a) f) => le_top theorem is_bounded_le_of_top {α : Type u_1} [order_top α] {f : filter α} : is_bounded LessEq f := Exists.intro ⊤ (eventually_of_forall fun (_x : α) => le_top) theorem is_bounded_ge_of_bot {α : Type u_1} [order_bot α] {f : filter α} : is_bounded ge f := Exists.intro ⊥ (eventually_of_forall fun (_x : α) => bot_le) theorem is_bounded_under_sup {α : Type u_1} {β : Type u_2} [semilattice_sup α] {f : filter β} {u : β → α} {v : β → α} : is_bounded_under LessEq f u → is_bounded_under LessEq f v → is_bounded_under LessEq f fun (a : β) => u a ⊔ v a := sorry theorem is_bounded_under_inf {α : Type u_1} {β : Type u_2} [semilattice_inf α] {f : filter β} {u : β → α} {v : β → α} : is_bounded_under ge f u → is_bounded_under ge f v → is_bounded_under ge f fun (a : β) => u a ⊓ v a := sorry /-- Filters are automatically bounded or cobounded in complete lattices. To use the same statements in complete and conditionally complete lattices but let automation fill automatically the boundedness proofs in complete lattices, we use the tactic `is_bounded_default` in the statements, in the form `(hf : f.is_bounded (≥) . is_bounded_default)`. -/ /-- The `Limsup` of a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `x ≤ a`. -/ def Limsup {α : Type u_1} [conditionally_complete_lattice α] (f : filter α) : α := Inf (set_of fun (a : α) => filter.eventually (fun (n : α) => n ≤ a) f) /-- The `Liminf` of a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `x ≥ a`. -/ def Liminf {α : Type u_1} [conditionally_complete_lattice α] (f : filter α) : α := Sup (set_of fun (a : α) => filter.eventually (fun (n : α) => a ≤ n) f) /-- The `limsup` of a function `u` along a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `u x ≤ a`. -/ def limsup {α : Type u_1} {β : Type u_2} [conditionally_complete_lattice α] (f : filter β) (u : β → α) : α := Limsup (map u f) /-- The `liminf` of a function `u` along a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `u x ≥ a`. -/ def liminf {α : Type u_1} {β : Type u_2} [conditionally_complete_lattice α] (f : filter β) (u : β → α) : α := Liminf (map u f) theorem limsup_eq {α : Type u_1} {β : Type u_2} [conditionally_complete_lattice α] {f : filter β} {u : β → α} : limsup f u = Inf (set_of fun (a : α) => filter.eventually (fun (n : β) => u n ≤ a) f) := rfl theorem liminf_eq {α : Type u_1} {β : Type u_2} [conditionally_complete_lattice α] {f : filter β} {u : β → α} : liminf f u = Sup (set_of fun (a : α) => filter.eventually (fun (n : β) => a ≤ u n) f) := rfl theorem Limsup_le_of_le {α : Type u_1} [conditionally_complete_lattice α] {f : filter α} {a : α} (hf : autoParam (is_cobounded LessEq f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (h : filter.eventually (fun (n : α) => n ≤ a) f) : Limsup f ≤ a := cInf_le hf h theorem le_Liminf_of_le {α : Type u_1} [conditionally_complete_lattice α] {f : filter α} {a : α} (hf : autoParam (is_cobounded ge f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (h : filter.eventually (fun (n : α) => a ≤ n) f) : a ≤ Liminf f := le_cSup hf h theorem le_Limsup_of_le {α : Type u_1} [conditionally_complete_lattice α] {f : filter α} {a : α} (hf : autoParam (is_bounded LessEq f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (h : ∀ (b : α), filter.eventually (fun (n : α) => n ≤ b) f → a ≤ b) : a ≤ Limsup f := le_cInf hf h theorem Liminf_le_of_le {α : Type u_1} [conditionally_complete_lattice α] {f : filter α} {a : α} (hf : autoParam (is_bounded ge f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (h : ∀ (b : α), filter.eventually (fun (n : α) => b ≤ n) f → b ≤ a) : Liminf f ≤ a := cSup_le hf h theorem Liminf_le_Limsup {α : Type u_1} [conditionally_complete_lattice α] {f : filter α} [ne_bot f] (h₁ : autoParam (is_bounded LessEq f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (h₂ : autoParam (is_bounded ge f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : Liminf f ≤ Limsup f := sorry theorem Liminf_le_Liminf {α : Type u_1} [conditionally_complete_lattice α] {f : filter α} {g : filter α} (hf : autoParam (is_bounded ge f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hg : autoParam (is_cobounded ge g) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (h : ∀ (a : α), filter.eventually (fun (n : α) => a ≤ n) f → filter.eventually (fun (n : α) => a ≤ n) g) : Liminf f ≤ Liminf g := cSup_le_cSup hg hf h theorem Limsup_le_Limsup {α : Type u_1} [conditionally_complete_lattice α] {f : filter α} {g : filter α} (hf : autoParam (is_cobounded LessEq f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hg : autoParam (is_bounded LessEq g) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (h : ∀ (a : α), filter.eventually (fun (n : α) => n ≤ a) g → filter.eventually (fun (n : α) => n ≤ a) f) : Limsup f ≤ Limsup g := cInf_le_cInf hf hg h theorem Limsup_le_Limsup_of_le {α : Type u_1} [conditionally_complete_lattice α] {f : filter α} {g : filter α} (h : f ≤ g) (hf : autoParam (is_cobounded LessEq f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hg : autoParam (is_bounded LessEq g) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : Limsup f ≤ Limsup g := Limsup_le_Limsup fun (a : α) (ha : filter.eventually (fun (n : α) => n ≤ a) g) => h ha theorem Liminf_le_Liminf_of_le {α : Type u_1} [conditionally_complete_lattice α] {f : filter α} {g : filter α} (h : g ≤ f) (hf : autoParam (is_bounded ge f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hg : autoParam (is_cobounded ge g) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : Liminf f ≤ Liminf g := Liminf_le_Liminf fun (a : α) (ha : filter.eventually (fun (n : α) => a ≤ n) f) => h ha theorem limsup_le_limsup {β : Type u_2} {α : Type u_1} [conditionally_complete_lattice β] {f : filter α} {u : α → β} {v : α → β} (h : eventually_le f u v) (hu : autoParam (is_cobounded_under LessEq f u) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hv : autoParam (is_bounded_under LessEq f v) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : limsup f u ≤ limsup f v := Limsup_le_Limsup fun (b : β) => eventually_le.trans h theorem liminf_le_liminf {β : Type u_2} {α : Type u_1} [conditionally_complete_lattice β] {f : filter α} {u : α → β} {v : α → β} (h : filter.eventually (fun (a : α) => u a ≤ v a) f) (hu : autoParam (is_bounded_under ge f u) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hv : autoParam (is_cobounded_under ge f v) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : liminf f u ≤ liminf f v := limsup_le_limsup h theorem Limsup_principal {α : Type u_1} [conditionally_complete_lattice α] {s : set α} (h : bdd_above s) (hs : set.nonempty s) : Limsup (principal s) = Sup s := sorry theorem Liminf_principal {α : Type u_1} [conditionally_complete_lattice α] {s : set α} (h : bdd_below s) (hs : set.nonempty s) : Liminf (principal s) = Inf s := Limsup_principal h hs theorem limsup_congr {β : Type u_2} {α : Type u_1} [conditionally_complete_lattice β] {f : filter α} {u : α → β} {v : α → β} (h : filter.eventually (fun (a : α) => u a = v a) f) : limsup f u = limsup f v := sorry theorem liminf_congr {β : Type u_2} {α : Type u_1} [conditionally_complete_lattice β] {f : filter α} {u : α → β} {v : α → β} (h : filter.eventually (fun (a : α) => u a = v a) f) : liminf f u = liminf f v := limsup_congr h theorem limsup_const {β : Type u_2} {α : Type u_1} [conditionally_complete_lattice β] {f : filter α} [ne_bot f] (b : β) : (limsup f fun (x : α) => b) = b := sorry theorem liminf_const {β : Type u_2} {α : Type u_1} [conditionally_complete_lattice β] {f : filter α} [ne_bot f] (b : β) : (liminf f fun (x : α) => b) = b := limsup_const b @[simp] theorem Limsup_bot {α : Type u_1} [complete_lattice α] : Limsup ⊥ = ⊥ := sorry @[simp] theorem Liminf_bot {α : Type u_1} [complete_lattice α] : Liminf ⊥ = ⊤ := sorry @[simp] theorem Limsup_top {α : Type u_1} [complete_lattice α] : Limsup ⊤ = ⊤ := sorry @[simp] theorem Liminf_top {α : Type u_1} [complete_lattice α] : Liminf ⊤ = ⊥ := sorry /-- Same as limsup_const applied to `⊥` but without the `ne_bot f` assumption -/ theorem limsup_const_bot {α : Type u_1} {β : Type u_2} [complete_lattice α] {f : filter β} : (limsup f fun (x : β) => ⊥) = ⊥ := sorry /-- Same as limsup_const applied to `⊤` but without the `ne_bot f` assumption -/ theorem liminf_const_top {α : Type u_1} {β : Type u_2} [complete_lattice α] {f : filter β} : (liminf f fun (x : β) => ⊤) = ⊤ := limsup_const_bot theorem liminf_le_limsup {α : Type u_1} {β : Type u_2} [complete_lattice α] {f : filter β} [ne_bot f] {u : β → α} : liminf f u ≤ limsup f u := Liminf_le_Limsup theorem has_basis.Limsup_eq_infi_Sup {α : Type u_1} [complete_lattice α] {ι : Type u_2} {p : ι → Prop} {s : ι → set α} {f : filter α} (h : has_basis f p s) : Limsup f = infi fun (i : ι) => infi fun (hi : p i) => Sup (s i) := sorry theorem has_basis.Liminf_eq_supr_Inf {α : Type u_1} {ι : Type u_3} [complete_lattice α] {p : ι → Prop} {s : ι → set α} {f : filter α} (h : has_basis f p s) : Liminf f = supr fun (i : ι) => supr fun (hi : p i) => Inf (s i) := has_basis.Limsup_eq_infi_Sup h theorem Limsup_eq_infi_Sup {α : Type u_1} [complete_lattice α] {f : filter α} : Limsup f = infi fun (s : set α) => infi fun (H : s ∈ f) => Sup s := has_basis.Limsup_eq_infi_Sup (basis_sets f) theorem Liminf_eq_supr_Inf {α : Type u_1} [complete_lattice α] {f : filter α} : Liminf f = supr fun (s : set α) => supr fun (H : s ∈ f) => Inf s := Limsup_eq_infi_Sup /-- In a complete lattice, the limsup of a function is the infimum over sets `s` in the filter of the supremum of the function over `s` -/ theorem limsup_eq_infi_supr {α : Type u_1} {β : Type u_2} [complete_lattice α] {f : filter β} {u : β → α} : limsup f u = infi fun (s : set β) => infi fun (H : s ∈ f) => supr fun (a : β) => supr fun (H : a ∈ s) => u a := sorry theorem limsup_eq_infi_supr_of_nat {α : Type u_1} [complete_lattice α] {u : ℕ → α} : limsup at_top u = infi fun (n : ℕ) => supr fun (i : ℕ) => supr fun (H : i ≥ n) => u i := sorry theorem limsup_eq_infi_supr_of_nat' {α : Type u_1} [complete_lattice α] {u : ℕ → α} : limsup at_top u = infi fun (n : ℕ) => supr fun (i : ℕ) => u (i + n) := sorry theorem has_basis.limsup_eq_infi_supr {α : Type u_1} {β : Type u_2} {ι : Type u_3} [complete_lattice α] {p : ι → Prop} {s : ι → set β} {f : filter β} {u : β → α} (h : has_basis f p s) : limsup f u = infi fun (i : ι) => infi fun (hi : p i) => supr fun (a : β) => supr fun (H : a ∈ s i) => u a := sorry /-- In a complete lattice, the liminf of a function is the infimum over sets `s` in the filter of the supremum of the function over `s` -/ theorem liminf_eq_supr_infi {α : Type u_1} {β : Type u_2} [complete_lattice α] {f : filter β} {u : β → α} : liminf f u = supr fun (s : set β) => supr fun (H : s ∈ f) => infi fun (a : β) => infi fun (H : a ∈ s) => u a := limsup_eq_infi_supr theorem liminf_eq_supr_infi_of_nat {α : Type u_1} [complete_lattice α] {u : ℕ → α} : liminf at_top u = supr fun (n : ℕ) => infi fun (i : ℕ) => infi fun (H : i ≥ n) => u i := limsup_eq_infi_supr_of_nat theorem liminf_eq_supr_infi_of_nat' {α : Type u_1} [complete_lattice α] {u : ℕ → α} : liminf at_top u = supr fun (n : ℕ) => infi fun (i : ℕ) => u (i + n) := limsup_eq_infi_supr_of_nat' theorem has_basis.liminf_eq_supr_infi {α : Type u_1} {β : Type u_2} {ι : Type u_3} [complete_lattice α] {p : ι → Prop} {s : ι → set β} {f : filter β} {u : β → α} (h : has_basis f p s) : liminf f u = supr fun (i : ι) => supr fun (hi : p i) => infi fun (a : β) => infi fun (H : a ∈ s i) => u a := has_basis.limsup_eq_infi_supr h theorem eventually_lt_of_lt_liminf {α : Type u_1} {β : Type u_2} {f : filter α} [conditionally_complete_linear_order β] {u : α → β} {b : β} (h : b < liminf f u) (hu : autoParam (is_bounded_under ge f u) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : filter.eventually (fun (a : α) => b < u a) f := sorry theorem eventually_lt_of_limsup_lt {α : Type u_1} {β : Type u_2} {f : filter α} [conditionally_complete_linear_order β] {u : α → β} {b : β} (h : limsup f u < b) (hu : autoParam (is_bounded_under LessEq f u) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : filter.eventually (fun (a : α) => u a < b) f := eventually_lt_of_lt_liminf h theorem le_limsup_of_frequently_le {α : Type u_1} {β : Type u_2} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu_le : filter.frequently (fun (x : α) => b ≤ u x) f) (hu : autoParam (is_bounded_under LessEq f u) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : b ≤ limsup f u := sorry theorem liminf_le_of_frequently_le {α : Type u_1} {β : Type u_2} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu_le : filter.frequently (fun (x : α) => u x ≤ b) f) (hu : autoParam (is_bounded_under ge f u) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : liminf f u ≤ b := le_limsup_of_frequently_le hu_le end filter theorem galois_connection.l_limsup_le {α : Type u_1} {β : Type u_2} {γ : Type u_3} [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {v : α → β} {l : β → γ} {u : γ → β} (gc : galois_connection l u) (hlv : autoParam (filter.is_bounded_under LessEq f fun (x : α) => l (v x)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hv_co : autoParam (filter.is_cobounded_under LessEq f v) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : l (filter.limsup f v) ≤ filter.limsup f fun (x : α) => l (v x) := sorry theorem order_iso.limsup_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {u : α → β} (g : β ≃o γ) (hu : autoParam (filter.is_bounded_under LessEq f u) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hu_co : autoParam (filter.is_cobounded_under LessEq f u) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hgu : autoParam (filter.is_bounded_under LessEq f fun (x : α) => coe_fn g (u x)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hgu_co : autoParam (filter.is_cobounded_under LessEq f fun (x : α) => coe_fn g (u x)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : coe_fn g (filter.limsup f u) = filter.limsup f fun (x : α) => coe_fn g (u x) := sorry theorem order_iso.liminf_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {u : α → β} (g : β ≃o γ) (hu : autoParam (filter.is_bounded_under ge f u) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hu_co : autoParam (filter.is_cobounded_under ge f u) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hgu : autoParam (filter.is_bounded_under ge f fun (x : α) => coe_fn g (u x)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) (hgu_co : autoParam (filter.is_cobounded_under ge f fun (x : α) => coe_fn g (u x)) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.filter.is_bounded_default") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "filter") "is_bounded_default") [])) : coe_fn g (filter.liminf f u) = filter.liminf f fun (x : α) => coe_fn g (u x) := order_iso.limsup_apply (order_iso.dual g)
f841b3dbfdc61e4b7247ec15122cc730c67ab322
ebb7367fa8ab324601b5abf705720fd4cc0e8598
/homotopy/cohomology.hlean
a0ea06071a1130632f84b7b510991fc0b42e17db
[ "Apache-2.0" ]
permissive
radams78/Spectral
3e34916d9bbd0939ee6a629e36744827ff27bfc2
c8145341046cfa2b4960ef3cc5a1117d12c43f63
refs/heads/master
1,610,421,583,830
1,481,232,014,000
1,481,232,014,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,673
hlean
/- Copyright (c) 2016 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Reduced cohomology -/ import algebra.arrow_group .spectrum homotopy.EM open eq spectrum int trunc pointed EM group algebra circle sphere nat EM.ops definition EM_spectrum /-[constructor]-/ (G : AbGroup) : spectrum := spectrum.Mk (K G) (λn, (loop_EM G n)⁻¹ᵉ*) definition cohomology (X : Type*) (Y : spectrum) (n : ℤ) : AbGroup := AbGroup_pmap X (πag[2] (Y (2+n))) definition ordinary_cohomology [reducible] (X : Type*) (G : AbGroup) (n : ℤ) : AbGroup := cohomology X (EM_spectrum G) n definition ordinary_cohomology_Z [reducible] (X : Type*) (n : ℤ) : AbGroup := ordinary_cohomology X agℤ n notation `H^` n `[`:0 X:0 `, ` Y:0 `]`:0 := cohomology X Y n notation `H^` n `[`:0 X:0 `]`:0 := ordinary_cohomology_Z X n -- check H^3[S¹*,EM_spectrum agℤ] -- check H^3[S¹*] definition unpointed_cohomology (X : Type) (Y : spectrum) (n : ℤ) : AbGroup := cohomology X₊ Y n definition cohomology_homomorphism [constructor] {X X' : Type*} (f : X' →* X) (Y : spectrum) (n : ℤ) : cohomology X Y n →g cohomology X' Y n := Group_pmap_homomorphism f (πag[2] (Y (2+n))) definition cohomology_homomorphism_id (X : Type*) (Y : spectrum) (n : ℤ) (f : H^n[X, Y]) : cohomology_homomorphism (pid X) Y n f ~* f := !pcompose_pid definition cohomology_homomorphism_compose {X X' X'' : Type*} (g : X'' →* X') (f : X' →* X) (Y : spectrum) (n : ℤ) (h : H^n[X, Y]) : cohomology_homomorphism (f ∘* g) Y n h ~* cohomology_homomorphism g Y n (cohomology_homomorphism f Y n h) := !passoc⁻¹*
b09acaeae05802b5714de3a99edad7fd340229c9
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/functions/composition_injective.lean
be2c9f1f07ebd32f9928bc40a71ba7ef032a89fd
[ "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
526
lean
import data.real.basic open function /- # Chapter 6 : Functions ## Level 5 A classical result in composition of functions. Now going the other way around. -/ /- Lemma If composition of $f$ and $g$ is injective, then $f$ is injective. -/ theorem composition_injective (X Y Z : set ℝ) (f : X → Y) (g : Y → Z) : injective (g ∘ f) → injective f := begin intros h a b ha, have applyg : g (f a) = g (f a), refl, rw ha at applyg {occs := occurrences.pos [2]}, apply h, exact applyg, done end
eb183067058fcf83fcf8254db358037bd0db8676
46ee5dc7248ccc9ee615639c0c003c05f58975cd
/src/completeness.lean
ae6e710d5ef282e842aab02345f02229fadced23
[ "Apache-2.0" ]
permissive
m4lvin/tablean
e61d593b4dde6512245192c577edeb36c48f63c0
836202612fc2bfacb5545696412e7d27f7704141
refs/heads/main
1,685,613,112,076
1,676,755,678,000
1,676,755,678,000
454,064,275
8
1
null
null
null
null
UTF-8
Lean
false
false
8,597
lean
-- COMPLETENESS import syntax import tableau import soundness -- import modelgraphs open has_sat -- Each local rule preserves truth "upwards" lemma localRuleTruth {W : Type} {M : kripkeModel W} {w : W} {X : finset formula} {B : finset (finset formula)} : localRule X B → (∃ Y ∈ B, (M,w) ⊨ Y) → (M,w) ⊨ X := begin intro r, cases r, case localRule.bot { simp, }, case localRule.not { simp, }, case localRule.neg : a f notnotf_in_a { intro hyp, rcases hyp with ⟨b, b_in_B, w_sat_b⟩, intros phi phi_in_a, have b_is_af : b = insert f (a \ {~~f}), { simp at *, subst b_in_B, }, have phi_in_b_or_is_f : phi ∈ b ∨ phi = ~~f, { rw b_is_af, simp, finish, }, cases phi_in_b_or_is_f with phi_in_b phi_is_notnotf, { apply w_sat_b, exact phi_in_b, }, { rw phi_is_notnotf, unfold evaluate at *, simp, -- this removes double negation apply w_sat_b, rw b_is_af , simp at *, }, }, case localRule.con : a f g fandg_in_a { intro hyp, rcases hyp with ⟨b, b_in_B, w_sat_b⟩, intros phi phi_in_a, simp at b_in_B, have b_is_fga : b = insert f (insert g (a \ {f⋏g})), { subst b_in_B, ext1, simp, }, have phi_in_b_or_is_fandg : phi ∈ b ∨ phi = f⋏g, { rw b_is_fga, simp, finish, }, cases phi_in_b_or_is_fandg with phi_in_b phi_is_fandg, { apply w_sat_b, exact phi_in_b, }, { rw phi_is_fandg, unfold evaluate at *, split, { apply w_sat_b, rw b_is_fga, simp, }, { apply w_sat_b, rw b_is_fga, simp, }, }, }, case localRule.nCo : a f g not_fandg_in_a { intro hyp, rcases hyp with ⟨b, b_in_B, w_sat_b⟩, intros phi phi_in_a, simp at *, have phi_in_b_or_is_notfandg : phi ∈ b ∨ phi = ~(f⋏g), { cases b_in_B ; { rw b_in_B, simp, finish, }, }, cases b_in_B, { -- b contains ~f cases phi_in_b_or_is_notfandg with phi_in_b phi_def, { exact w_sat_b phi phi_in_b, }, { rw phi_def, unfold evaluate, rw b_in_B at w_sat_b, specialize w_sat_b (~f), rw not_and_distrib, left, unfold evaluate at w_sat_b, apply w_sat_b, finish, }, }, { -- b contains ~g cases phi_in_b_or_is_notfandg with phi_in_b phi_def, { exact w_sat_b phi phi_in_b, }, { rw phi_def, unfold evaluate, rw b_in_B at w_sat_b, specialize w_sat_b (~g), rw not_and_distrib, right, unfold evaluate at w_sat_b, apply w_sat_b, finish, }, }, }, end -- Each local rule is "complete", i.e. preserves satisfiability "upwards" lemma localRuleCompleteness {X : finset formula} { B : finset (finset formula) } : localRule X B → (∃ Y ∈ B, satisfiable Y) → satisfiable X := begin intro lr, intro sat_B, rcases sat_B with ⟨Y, Y_in_B, sat_Y⟩, unfold satisfiable at *, rcases sat_Y with ⟨W,M,w,w_sat_Y⟩, use [W,M,w], apply localRuleTruth lr, tauto, end -- Lemma 11 (but rephrased to be about local tableau!?) lemma inconsUpwards {X} {ltX : localTableau X} : (Π en ∈ endNodesOf ⟨X, ltX⟩, inconsistent en) → inconsistent X := begin intro lhs, unfold inconsistent at *, let leafTableaus : Π en ∈ endNodesOf ⟨X, ltX⟩, closedTableau en := λ Y YinEnds, (lhs Y YinEnds).some, split, exact closedTableau.loc ltX leafTableaus, end -- Converse of Lemma 11 lemma consToEndNodes {X} {ltX : localTableau X} : consistent X → (∃ en ∈ endNodesOf ⟨X, ltX⟩, consistent en) := begin intro consX, unfold consistent at *, have claim := not.imp consX (@inconsUpwards X ltX), simp at claim, tauto, end def projOfConsSimpIsCons : Π {X ϕ}, consistent X → simple X → ~□ϕ ∈ X → consistent (projection X ∪ {~ϕ}) := begin intros X ϕ consX simpX notBoxPhi_in_X, unfold consistent at *, unfold inconsistent at *, by_contradiction h, simp at *, cases h with ctProj, have ctX : closedTableau X, { apply closedTableau.atm notBoxPhi_in_X simpX, simp, exact ctProj, }, cases consX, tauto, end lemma locTabEndSatThenSat {X Y} (ltX : localTableau X) (Y_endOf_X : Y ∈ endNodesOf ⟨X, ltX⟩) : satisfiable Y → satisfiable X := begin intro satY, induction ltX, case byLocalRule : X B lr next IH { apply localRuleCompleteness lr, cases lr, case localRule.bot : W bot_in_W { simp at *, exact Y_endOf_X, }, case localRule.not : _ ϕ h { simp at *, exact Y_endOf_X, }, case localRule.neg : Z ϕ notNotPhi_inX { simp at *, specialize IH (insert ϕ (Z.erase (~~ϕ))), simp at IH, apply IH, rcases Y_endOf_X with ⟨W, W_def, Y_endOf_W⟩, subst W_def, exact Y_endOf_W, }, case localRule.con : Z ϕ ψ _ { simp at *, specialize IH (insert ϕ (insert ψ (Z.erase (ϕ⋏ψ)))), simp at IH, apply IH, rcases Y_endOf_X with ⟨W, W_def, Y_endOf_W⟩, subst W_def, exact Y_endOf_W, }, case localRule.nCo : Z ϕ ψ _ { simp at *, rcases Y_endOf_X with ⟨W, W_def, Y_endOf_W⟩, cases W_def, all_goals { subst W_def, }, { specialize IH (insert (~ϕ) (Z.erase (~(ϕ⋏ψ)))), simp at IH, use (insert (~ϕ) (Z.erase (~(ϕ⋏ψ)))), split, { left, refl, }, { apply IH, exact Y_endOf_W, } }, { specialize IH (insert (~ψ) (Z.erase (~(ϕ⋏ψ)))), simp at IH, use (insert (~ψ) (Z.erase (~(ϕ⋏ψ)))), split, { right, refl, }, { apply IH, exact Y_endOf_W, } }, }, }, case sim : X simpX { finish, }, end lemma almostCompleteness : Π n X, lengthOfSet X = n → consistent X → satisfiable X := begin intro n, apply nat.strong_induction_on n, intros n IH, intros X lX_is_n consX, refine if simpX : simple X then _ else _, -- CASE 1: X is simple rw Lemma1_simple_sat_iff_all_projections_sat simpX, split, { -- show that X is not closed by_contradiction h, unfold consistent at consX, unfold inconsistent at consX, simp at consX, cases consX, apply consX, unfold closed at h, refine if botInX : ⊥ ∈ X then _ else _, { apply closedTableau.loc, rotate, apply localTableau.byLocalRule, exact localRule.bot botInX, intros Y YinEmpty, cases YinEmpty, rw botNoEndNodes, intros Y YinEmpty, cases YinEmpty, }, { have f1 : ∃ (f : formula) (H : f ∈ X), ~f ∈ X := by tauto, have : nonempty (closedTableau X), { rcases f1 with ⟨f, f_in_X, notf_in_X⟩, fconstructor, apply closedTableau.loc, rotate, apply localTableau.byLocalRule, apply localRule.not ⟨f_in_X , notf_in_X⟩, intros Y YinEmpty, cases YinEmpty, rw notNoEndNodes, intros Y YinEmpty, cases YinEmpty, }, exact classical.choice this, }, }, { -- show that all projections are sat intros R notBoxR_in_X, apply IH (lengthOfSet (projection X ∪ {~R})), { -- projection decreases lengthOfSet subst lX_is_n, exact atmRuleDecreasesLength notBoxR_in_X, }, { refl, }, { exact projOfConsSimpIsCons consX simpX notBoxR_in_X, }, }, -- CASE 2: X is not simple rename simpX notSimpX, rcases notSimpleThenLocalRule notSimpX with ⟨B,lrExists⟩, have lr := classical.choice lrExists, have rest : Π (Y : finset formula), Y ∈ B → localTableau Y, { intros Y Y_in_B, set N := hasLength.lengthOf Y, exact classical.choice (existsLocalTableauFor N Y (by refl)) }, rcases @consToEndNodes X (localTableau.byLocalRule lr rest) consX with ⟨E, E_endOf_X, consE⟩, have satE : satisfiable E, { apply IH (lengthOfSet E), { -- end Node of local rule is LT subst lX_is_n, apply endNodesOfLocalRuleLT E_endOf_X, }, { refl, }, { exact consE, }, }, exact locTabEndSatThenSat (localTableau.byLocalRule lr rest) E_endOf_X satE, end -- Theorem 4, page 37 theorem completeness : ∀ X, consistent X ↔ satisfiable X := begin intro X, split, { intro X_is_consistent, let n := lengthOfSet X, apply almostCompleteness n X (by refl) X_is_consistent, }, -- use Theorem 2: exact correctness X, end lemma singletonCompleteness : ∀ φ, consistent {φ} ↔ satisfiable φ := begin intro f, have := completeness {f}, simp only [singletonSat_iff_sat] at *, tauto, end
02c3264e2a1c9a18714e8d5db3172e48cffde2d1
01ae0d022f2e2fefdaaa898938c1ac1fbce3b3ab
/categories/walking.lean
2467ecab66bafd5bd536226c3ba6ba6240af885f
[]
no_license
PatrickMassot/lean-category-theory
0f56a83464396a253c28a42dece16c93baf8ad74
ef239978e91f2e1c3b8e88b6e9c64c155dc56c99
refs/heads/master
1,629,739,187,316
1,512,422,659,000
1,512,422,659,000
113,098,786
0
0
null
1,512,424,022,000
1,512,424,022,000
null
UTF-8
Lean
false
false
3,342
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 .discrete_category import .path_category import .util.finite open categories open categories.graphs open categories.functor open categories.util.finite namespace categories.walking open Two definition WalkingPair : Graph := { Obj := Two, Hom := λ X Y, empty } -- PROJECT grah, what a pain! -- definition WalkingPair' : Category := { -- Obj := Two, -- Hom := λ X Y, match X, Y with -- | _0, _0 := unit -- | _1, _1 := unit -- | _, _ := empty -- end, -- identity := begin tidy, induction X, unfold WalkingPair'._match_1, tidy, unfold WalkingPair'._match_1, tidy end, -- compose := begin tidy, induction X, induction Y, induction Z, end, -- left_identities := sorry, -- right_identities := sorry, -- associativity := sorry, -- } @[simp] lemma Two_0_eq_1_eq_false : ¬(_0 = _1) := by contradiction @[simp] lemma Two_1_eq_0_eq_false : ¬(_1 = _0) := by contradiction @[applicable] definition decidable_true : decidable true := is_true begin trivial end @[applicable] definition decidable_false : decidable false := is_false ♯ instance Two_decidable : decidable_eq Two := ♯ -- lemma ff_eq_ff : (ff = ff) = true := by simp -- definition WalkingPair' : Category := { -- Obj := bool, -- Hom := λ X Y, if X = Y then unit else empty, -- identity := begin intros, induction X, any_goals { simp }, apply unit.star, apply unit.star end, -- tidy automates this, but I think I need a standalone example -- compose := begin intros, induction X, any_goals { induction Y }, any_goals { induction Z }, any_goals { simp at * }, any_goals { apply unit.star }, induction a_1, induction a, induction a, induction a_1, end, -- left_identity := begin intros, induction X, any_goals { induction Y }, rewrite ff_eq_ff at f, end, -- FIXME how am I meant to do this? -- right_identity := sorry, -- associativity := sorry, -- } definition WalkingParallelPair : Graph := { Obj := Two, Hom := λ X Y, match X, Y with | _0, _1 := Two | _, _ := empty end } definition Pair_homomorphism { C : Category } ( α β : C.Obj ) : GraphHomomorphism WalkingPair C.graph := { onObjects := λ X, match X with | _0 := α | _1 := β end, onMorphisms := λ X Y e, match X, Y, e with end } definition Pair_functor { C : Category } ( α β : C.Obj ) := Functor.from_GraphHomomorphism (Pair_homomorphism α β) definition ParallelPair_homomorphism { C : Category } { α β : C.Obj } ( f g : C.Hom α β ) : GraphHomomorphism WalkingParallelPair C.graph := { onObjects := λ X, match X with | _0 := α | _1 := β end, onMorphisms := λ X Y e, match X, Y, e with | _0, _1, _0 := f | _0, _1, _1 := g end } definition ParallelPair_functor { C : Category } { α β : C.Obj } ( f g : C.Hom α β ) := Functor.from_GraphHomomorphism (ParallelPair_homomorphism f g) end categories.walking
d160621ccf64a67c81824b2bf04e3039b5a2b608
54c9ed381c63410c9b6af3b0a1722c41152f037f
/Lib4/PostPort/Coe.lean
7f9aa5a71e563f4d8907ef5fb8f51c7fddb70fbc
[ "Apache-2.0" ]
permissive
dselsam/binport
0233f1aa961a77c4fc96f0dccc780d958c5efc6c
aef374df0e169e2c3f1dc911de240c076315805c
refs/heads/master
1,687,453,448,108
1,627,483,296,000
1,627,483,296,000
333,825,622
0
0
null
null
null
null
UTF-8
Lean
false
false
868
lean
import Lean3Lib.init.coe universe u v variable {α : Sort u} {β : Sort v} noncomputable instance hasCoe [inst : Mathlib.has_coe α β] : Coe α β := { coe := @Mathlib.has_coe.coe _ _ inst } -- TODO: why does mathlib declare these instances directly? -- topology/algebra/open_subgroup.lean:42:instance has_coe_set : has_coe_t (open_subgroup G) (set G) := ⟨λ U, U.1⟩ noncomputable instance hasCoeT [inst : Mathlib.has_coe_t α β] : CoeTC α β := { coe := @Mathlib.has_coe_t.coe _ _ inst } noncomputable instance hasCoeToFun [inst : Mathlib.has_coe_to_fun α] : CoeFun α (no_index (@Mathlib.has_coe_to_fun.F _ inst)) := { coe := @Mathlib.has_coe_to_fun.coe _ inst } noncomputable instance hasCoeToSort [inst : Mathlib.has_coe_to_sort α] : CoeSort α (no_index (@Mathlib.has_coe_to_sort.S _ inst)) := { coe := @Mathlib.has_coe_to_sort.coe _ inst }
a1b040a0d97ba9c9ccec008aac53fc11ea80491b
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/slow/path_groupoids.lean
f71fb7d850c61da2a869f3472b87ad7991e94fe4
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
24,012
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Jeremy Avigad -- Ported from Coq HoTT definition id {A : Type} (a : A) := a definition compose {A : Type} {B : Type} {C : Type} (g : B → C) (f : A → B) := λ x, g (f x) infixr ∘ := compose -- Path -- ---- set_option unifier.max_steps 100000 inductive path {A : Type} (a : A) : A → Type := idpath : path a a definition idpath := @path.idpath infix `≈` := path -- TODO: is this right? notation x `≈`:50 y `:>`:0 A:0 := @path A x y notation `idp`:max := idpath _ -- TODO: can we / should we use `1`? namespace path -- Concatenation and inverse -- ------------------------- definition concat {A : Type} {x y z : A} (p : x ≈ y) (q : y ≈ z) : x ≈ z := path.rec (λu, u) q p definition inverse {A : Type} {x y : A} (p : x ≈ y) : y ≈ x := path.rec (idpath x) p infixl `⬝` := concat postfix `^`:100 := inverse -- In Coq, these are not needed, because concat and inv are kept transparent definition inv_1 {A : Type} (x : A) : (idpath x)^ ≈ idpath x := idp definition concat_11 {A : Type} (x : A) : idpath x ⬝ idpath x ≈ idpath x := idp -- The 1-dimensional groupoid structure -- ------------------------------------ -- The identity path is a right unit. definition concat_p1 {A : Type} {x y : A} (p : x ≈ y) : p ⬝ idp ≈ p := rec_on p idp -- The identity path is a right unit. definition concat_1p {A : Type} {x y : A} (p : x ≈ y) : idp ⬝ p ≈ p := rec_on p idp -- Concatenation is associative. definition concat_p_pp {A : Type} {x y z t : A} (p : x ≈ y) (q : y ≈ z) (r : z ≈ t) : p ⬝ (q ⬝ r) ≈ (p ⬝ q) ⬝ r := rec_on r (rec_on q idp) definition concat_pp_p {A : Type} {x y z t : A} (p : x ≈ y) (q : y ≈ z) (r : z ≈ t) : (p ⬝ q) ⬝ r ≈ p ⬝ (q ⬝ r) := rec_on r (rec_on q idp) -- The left inverse law. definition concat_pV {A : Type} {x y : A} (p : x ≈ y) : p ⬝ p^ ≈ idp := rec_on p idp -- The right inverse law. definition concat_Vp {A : Type} {x y : A} (p : x ≈ y) : p^ ⬝ p ≈ idp := rec_on p idp -- Several auxiliary theorems about canceling inverses across associativity. These are somewhat -- redundant, following from earlier theorems. definition concat_V_pp {A : Type} {x y z : A} (p : x ≈ y) (q : y ≈ z) : p^ ⬝ (p ⬝ q) ≈ q := rec_on q (rec_on p idp) definition concat_p_Vp {A : Type} {x y z : A} (p : x ≈ y) (q : x ≈ z) : p ⬝ (p^ ⬝ q) ≈ q := rec_on q (rec_on p idp) definition concat_pp_V {A : Type} {x y z : A} (p : x ≈ y) (q : y ≈ z) : (p ⬝ q) ⬝ q^ ≈ p := rec_on q (rec_on p idp) definition concat_pV_p {A : Type} {x y z : A} (p : x ≈ z) (q : y ≈ z) : (p ⬝ q^) ⬝ q ≈ p := rec_on q (take p, rec_on p idp) p -- Inverse distributes over concatenation definition inv_pp {A : Type} {x y z : A} (p : x ≈ y) (q : y ≈ z) : (p ⬝ q)^ ≈ q^ ⬝ p^ := rec_on q (rec_on p idp) definition inv_Vp {A : Type} {x y z : A} (p : y ≈ x) (q : y ≈ z) : (p^ ⬝ q)^ ≈ q^ ⬝ p := rec_on q (rec_on p idp) -- universe metavariables definition inv_pV {A : Type} {x y z : A} (p : x ≈ y) (q : z ≈ y) : (p ⬝ q^)^ ≈ q ⬝ p^ := rec_on p (λq, rec_on q idp) q definition inv_VV {A : Type} {x y z : A} (p : y ≈ x) (q : z ≈ y) : (p^ ⬝ q^)^ ≈ q ⬝ p := rec_on p (rec_on q idp) -- Inverse is an involution. definition inv_V {A : Type} {x y : A} (p : x ≈ y) : p^^ ≈ p := rec_on p idp -- Theorems for moving things around in equations -- ---------------------------------------------- definition moveR_Mp {A : Type} {x y z : A} (p : x ≈ z) (q : y ≈ z) (r : y ≈ x) : p ≈ (r^ ⬝ q) → (r ⬝ p) ≈ q := have gen : Πp q, p ≈ (r^ ⬝ q) → (r ⬝ p) ≈ q, from rec_on r (take p q, assume h : p ≈ idp^ ⬝ q, show idp ⬝ p ≈ q, from concat_1p _ ⬝ h ⬝ concat_1p _), gen p q definition moveR_pM {A : Type} {x y z : A} (p : x ≈ z) (q : y ≈ z) (r : y ≈ x) : r ≈ q ⬝ p^ → r ⬝ p ≈ q := rec_on p (take q r h, (concat_p1 _ ⬝ h ⬝ concat_p1 _)) q r definition moveR_Vp {A : Type} {x y z : A} (p : x ≈ z) (q : y ≈ z) (r : x ≈ y) : p ≈ r ⬝ q → r^ ⬝ p ≈ q := rec_on r (take p q h, concat_1p _ ⬝ h ⬝ concat_1p _) p q definition moveR_pV {A : Type} {x y z : A} (p : z ≈ x) (q : y ≈ z) (r : y ≈ x) : r ≈ q ⬝ p → r ⬝ p^ ≈ q := rec_on p (take q r h, concat_p1 _ ⬝ h ⬝ concat_p1 _) q r definition moveL_Mp {A : Type} {x y z : A} (p : x ≈ z) (q : y ≈ z) (r : y ≈ x) : r^ ⬝ q ≈ p → q ≈ r ⬝ p := rec_on r (take p q h, (concat_1p _)^ ⬝ h ⬝ (concat_1p _)^) p q definition moveL_pM {A : Type} {x y z : A} (p : x ≈ z) (q : y ≈ z) (r : y ≈ x) : q ⬝ p^ ≈ r → q ≈ r ⬝ p := rec_on p (take q r h, (concat_p1 _)^ ⬝ h ⬝ (concat_p1 _)^) q r definition moveL_Vp {A : Type} {x y z : A} (p : x ≈ z) (q : y ≈ z) (r : x ≈ y) : r ⬝ q ≈ p → q ≈ r^ ⬝ p := rec_on r (take p q h, (concat_1p _)^ ⬝ h ⬝ (concat_1p _)^) p q definition moveL_pV {A : Type} {x y z : A} (p : z ≈ x) (q : y ≈ z) (r : y ≈ x) : q ⬝ p ≈ r → q ≈ r ⬝ p^ := rec_on p (take q r h, (concat_p1 _)^ ⬝ h ⬝ (concat_p1 _)^) q r definition moveL_1M {A : Type} {x y : A} (p q : x ≈ y) : p ⬝ q^ ≈ idp → p ≈ q := rec_on q (take p h, (concat_p1 _)^ ⬝ h) p definition moveL_M1 {A : Type} {x y : A} (p q : x ≈ y) : q^ ⬝ p ≈ idp → p ≈ q := rec_on q (take p h, (concat_1p _)^ ⬝ h) p definition moveL_1V {A : Type} {x y : A} (p : x ≈ y) (q : y ≈ x) : p ⬝ q ≈ idp → p ≈ q^ := rec_on q (take p h, (concat_p1 _)^ ⬝ h) p definition moveL_V1 {A : Type} {x y : A} (p : x ≈ y) (q : y ≈ x) : q ⬝ p ≈ idp → p ≈ q^ := rec_on q (take p h, (concat_1p _)^ ⬝ h) p definition moveR_M1 {A : Type} {x y : A} (p q : x ≈ y) : idp ≈ p^ ⬝ q → p ≈ q := rec_on p (take q h, h ⬝ (concat_1p _)) q definition moveR_1M {A : Type} {x y : A} (p q : x ≈ y) : idp ≈ q ⬝ p^ → p ≈ q := rec_on p (take q h, h ⬝ (concat_p1 _)) q definition moveR_1V {A : Type} {x y : A} (p : x ≈ y) (q : y ≈ x) : idp ≈ q ⬝ p → p^ ≈ q := rec_on p (take q h, h ⬝ (concat_p1 _)) q definition moveR_V1 {A : Type} {x y : A} (p : x ≈ y) (q : y ≈ x) : idp ≈ p ⬝ q → p^ ≈ q := rec_on p (take q h, h ⬝ (concat_1p _)) q -- Transport -- --------- -- keep transparent, so transport _ idp p is definitionally equal to p definition transport {A : Type} (P : A → Type) {x y : A} (p : x ≈ y) (u : P x) : P y := path.rec_on p u definition transport_1 {A : Type} (P : A → Type) {x : A} (u : P x) : transport _ idp u ≈ u := idp -- TODO: is the binding strength on x reasonable? (It is modeled on the notation for subst -- in the standard library.) -- This idiom makes the operation right associative. notation p `#`:65 x:64 := transport _ p x definition ap ⦃A B : Type⦄ (f : A → B) {x y:A} (p : x ≈ y) : f x ≈ f y := path.rec_on p idp -- TODO: is this better than an alias? Note use of curly brackets definition ap01 := ap definition pointwise_paths {A : Type} {P : A → Type} (f g : Πx, P x) : Type := Πx : A, f x ≈ g x infix `∼` := pointwise_paths definition apD10 {A} {B : A → Type} {f g : Πx, B x} (H : f ≈ g) : f ∼ g := λx, path.rec_on H idp definition ap10 {A B} {f g : A → B} (H : f ≈ g) : f ∼ g := apD10 H definition ap11 {A B} {f g : A → B} (H : f ≈ g) {x y : A} (p : x ≈ y) : f x ≈ g y := rec_on H (rec_on p idp) -- TODO: Note that the next line breaks the proof! -- opaque_hint (hiding rec_on) -- set_option pp.implicit true definition apD {A:Type} {B : A → Type} (f : Πa:A, B a) {x y : A} (p : x ≈ y) : p # (f x) ≈ f y := rec_on p idp -- More theorems for moving things around in equations -- --------------------------------------------------- definition moveR_transport_p {A : Type} (P : A → Type) {x y : A} (p : x ≈ y) (u : P x) (v : P y) : u ≈ p^ # v → p # u ≈ v := rec_on p (take u v, id) u v definition moveR_transport_V {A : Type} (P : A → Type) {x y : A} (p : y ≈ x) (u : P x) (v : P y) : u ≈ p # v → p^ # u ≈ v := rec_on p (take u v, id) u v definition moveL_transport_V {A : Type} (P : A → Type) {x y : A} (p : x ≈ y) (u : P x) (v : P y) : p # u ≈ v → u ≈ p^ # v := rec_on p (take u v, id) u v definition moveL_transport_p {A : Type} (P : A → Type) {x y : A} (p : y ≈ x) (u : P x) (v : P y) : p^ # u ≈ v → u ≈ p # v := rec_on p (take u v, id) u v -- Functoriality of functions -- -------------------------- -- Here we prove that functions behave like functors between groupoids, and that [ap] itself is -- functorial. -- Functions take identity paths to identity paths definition ap_1 {A B : Type} (x : A) (f : A → B) : (ap f idp) ≈ idp :> (f x ≈ f x) := idp definition apD_1 {A B} (x : A) (f : forall x : A, B x) : apD f idp ≈ idp :> (f x ≈ f x) := idp -- Functions commute with concatenation. definition ap_pp {A B : Type} (f : A → B) {x y z : A} (p : x ≈ y) (q : y ≈ z) : ap f (p ⬝ q) ≈ (ap f p) ⬝ (ap f q) := rec_on q (rec_on p idp) definition ap_p_pp {A B : Type} (f : A → B) {w x y z : A} (r : f w ≈ f x) (p : x ≈ y) (q : y ≈ z) : r ⬝ (ap f (p ⬝ q)) ≈ (r ⬝ ap f p) ⬝ (ap f q) := rec_on p (take r q, rec_on q (concat_p_pp r idp idp)) r q definition ap_pp_p {A B : Type} (f : A → B) {w x y z : A} (p : x ≈ y) (q : y ≈ z) (r : f z ≈ f w) : (ap f (p ⬝ q)) ⬝ r ≈ (ap f p) ⬝ (ap f q ⬝ r) := rec_on p (take q, rec_on q (take r, concat_pp_p _ _ _)) q r -- Functions commute with path inverses. definition inverse_ap {A B : Type} (f : A → B) {x y : A} (p : x ≈ y) : (ap f p)^ ≈ ap f (p^) := rec_on p idp definition ap_V {A B : Type} (f : A → B) {x y : A} (p : x ≈ y) : ap f (p^) ≈ (ap f p)^ := rec_on p idp -- TODO: rename id to idmap? definition ap_idmap {A : Type} {x y : A} (p : x ≈ y) : ap id p ≈ p := rec_on p idp definition ap_compose {A B C : Type} (f : A → B) (g : B → C) {x y : A} (p : x ≈ y) : ap (g ∘ f) p ≈ ap g (ap f p) := rec_on p idp -- Sometimes we don't have the actual function [compose]. definition ap_compose' {A B C : Type} (f : A → B) (g : B → C) {x y : A} (p : x ≈ y) : ap (λa, g (f a)) p ≈ ap g (ap f p) := rec_on p idp -- The action of constant maps. definition ap_const {A B : Type} {x y : A} (p : x ≈ y) (z : B) : ap (λu, z) p ≈ idp := rec_on p idp -- Naturality of [ap]. definition concat_Ap {A B : Type} {f g : A → B} (p : forall x, f x ≈ g x) {x y : A} (q : x ≈ y) : (ap f q) ⬝ (p y) ≈ (p x) ⬝ (ap g q) := rec_on q (concat_1p _ ⬝ (concat_p1 _)^) -- Naturality of [ap] at identity. definition concat_A1p {A : Type} {f : A → A} (p : forall x, f x ≈ x) {x y : A} (q : x ≈ y) : (ap f q) ⬝ (p y) ≈ (p x) ⬝ q := rec_on q (concat_1p _ ⬝ (concat_p1 _)^) definition concat_pA1 {A : Type} {f : A → A} (p : forall x, x ≈ f x) {x y : A} (q : x ≈ y) : (p x) ⬝ (ap f q) ≈ q ⬝ (p y) := rec_on q (concat_p1 _ ⬝ (concat_1p _)^) --TODO: note that the Coq proof for the preceding is -- -- match q as i in (_ ≈ y) return (p x ⬝ ap f i ≈ i ⬝ p y) with -- | idpath => concat_p1 _ ⬝ (concat_1p _)^ -- end. -- -- It is nice that we don't have to give the predicate. -- Naturality with other paths hanging around. definition concat_pA_pp {A B : Type} {f g : A → B} (p : forall x, f x ≈ g x) {x y : A} (q : x ≈ y) {w z : B} (r : w ≈ f x) (s : g y ≈ z) : (r ⬝ ap f q) ⬝ (p y ⬝ s) ≈ (r ⬝ p x) ⬝ (ap g q ⬝ s) := rec_on q (take s, rec_on s (take r, idp)) s r -- Action of [apD10] and [ap10] on paths -- ------------------------------------- -- Application of paths between functions preserves the groupoid structure definition apD10_1 {A} {B : A → Type} (f : Πx, B x) (x : A) : apD10 (idpath f) x ≈ idp := idp definition apD10_pp {A} {B : A → Type} {f f' f'' : Πx, B x} (h : f ≈ f') (h' : f' ≈ f'') (x : A) : apD10 (h ⬝ h') x ≈ apD10 h x ⬝ apD10 h' x := rec_on h (take h', rec_on h' idp) h' definition apD10_V {A : Type} {B : A → Type} {f g : Πx : A, B x} (h : f ≈ g) (x : A) : apD10 (h^) x ≈ (apD10 h x)^ := rec_on h idp definition ap10_1 {A B} {f : A → B} (x : A) : ap10 (idpath f) x ≈ idp := idp definition ap10_pp {A B} {f f' f'' : A → B} (h : f ≈ f') (h' : f' ≈ f'') (x : A) : ap10 (h ⬝ h') x ≈ ap10 h x ⬝ ap10 h' x := apD10_pp h h' x definition ap10_V {A B} {f g : A→B} (h : f ≈ g) (x:A) : ap10 (h^) x ≈ (ap10 h x)^ := apD10_V h x -- [ap10] also behaves nicely on paths produced by [ap] definition ap_ap10 {A B C} (f g : A → B) (h : B → C) (p : f ≈ g) (a : A) : ap h (ap10 p a) ≈ ap10 (ap (λ f', h ∘ f') p) a:= rec_on p idp -- Transport and the groupoid structure of paths -- --------------------------------------------- -- TODO: move from above? -- definition transport_1 {A : Type} (P : A → Type) {x : A} (u : P x) -- : idp # u ≈ u := idp definition transport_pp {A : Type} (P : A → Type) {x y z : A} (p : x ≈ y) (q : y ≈ z) (u : P x) : p ⬝ q # u ≈ q # p # u := rec_on q (rec_on p idp) definition transport_pV {A : Type} (P : A → Type) {x y : A} (p : x ≈ y) (z : P y) : p # p^ # z ≈ z := (transport_pp P (p^) p z)^ ⬝ ap (λr, transport P r z) (concat_Vp p) definition transport_Vp {A : Type} (P : A → Type) {x y : A} (p : x ≈ y) (z : P x) : p^ # p # z ≈ z := (transport_pp P p (p^) z)^ ⬝ ap (λr, transport P r z) (concat_pV p) ----------------------------------------------- -- *** Examples of difficult induction problems ----------------------------------------------- theorem double_induction {A : Type} {x y z : A} (p : x ≈ y) (q : y ≈ z) {C : Π(x y z : A), Π(p : x ≈ y), Π(q : y ≈ z), Type} (H : C x x x (idpath x) (idpath x)) : C x y z p q := rec_on p (take z q, rec_on q H) z q theorem double_induction2 {A : Type} {x y z : A} (p : x ≈ y) (q : z ≈ y) {C : Π(x y z : A), Π(p : x ≈ y), Π(q : z ≈ y), Type} (H : C z z z (idpath z) (idpath z)) : C x y z p q := rec_on p (take y q, rec_on q H) y q theorem double_induction2' {A : Type} {x y z : A} (p : x ≈ y) (q : z ≈ y) {C : Π(x y z : A), Π(p : x ≈ y), Π(q : z ≈ y), Type} (H : C z z z (idpath z) (idpath z)) : C x y z p q := rec_on p (take y q, rec_on q H) y q theorem triple_induction {A : Type} {x y z w : A} (p : x ≈ y) (q : y ≈ z) (r : z ≈ w) {C : Π(x y z w : A), Π(p : x ≈ y), Π(q : y ≈ z), Π(r: z ≈ w), Type} (H : C x x x x (idpath x) (idpath x) (idpath x)) : C x y z w p q r := rec_on p (take z q, rec_on q (take w r, rec_on r H)) z q w r -- try this again definition concat_pV_p_new {A : Type} {x y z : A} (p : x ≈ z) (q : y ≈ z) : (p ⬝ q^) ⬝ q ≈ p := double_induction2 p q idp definition transport_p_pp {A : Type} (P : A → Type) {x y z w : A} (p : x ≈ y) (q : y ≈ z) (r : z ≈ w) (u : P x) : ap (λe, e # u) (concat_p_pp p q r) ⬝ (transport_pp P (p ⬝ q) r u) ⬝ ap (transport P r) (transport_pp P p q u) ≈ (transport_pp P p (q ⬝ r) u) ⬝ (transport_pp P q r (p # u)) :> ((p ⬝ (q ⬝ r)) # u ≈ r # q # p # u) := triple_induction p q r (take u, idp) u -- Here is another coherence lemma for transport. definition transport_pVp {A} (P : A → Type) {x y : A} (p : x ≈ y) (z : P x) : transport_pV P p (transport P p z) ≈ ap (transport P p) (transport_Vp P p z) := rec_on p idp -- Dependent transport in a doubly dependent type. definition transportD {A : Type} (B : A → Type) (C : Π a : A, B a → Type) {x1 x2 : A} (p : x1 ≈ x2) (y : B x1) (z : C x1 y) : C x2 (p # y) := rec_on p z -- Transporting along higher-dimensional paths definition transport2 {A : Type} (P : A → Type) {x y : A} {p q : x ≈ y} (r : p ≈ q) (z : P x) : p # z ≈ q # z := ap (λp', p' # z) r -- An alternative definition. definition transport2_is_ap10 {A : Type} (Q : A → Type) {x y : A} {p q : x ≈ y} (r : p ≈ q) (z : Q x) : transport2 Q r z ≈ ap10 (ap (transport Q) r) z := rec_on r idp definition transport2_p2p {A : Type} (P : A → Type) {x y : A} {p1 p2 p3 : x ≈ y} (r1 : p1 ≈ p2) (r2 : p2 ≈ p3) (z : P x) : transport2 P (r1 ⬝ r2) z ≈ transport2 P r1 z ⬝ transport2 P r2 z := rec_on r1 (rec_on r2 idp) -- TODO: another interesting case definition transport2_V {A : Type} (Q : A → Type) {x y : A} {p q : x ≈ y} (r : p ≈ q) (z : Q x) : transport2 Q (r^) z ≈ ((transport2 Q r z)^) := -- rec_on r idp -- doesn't work rec_on r (idpath (inverse (transport2 Q (idpath p) z))) definition concat_AT {A : Type} (P : A → Type) {x y : A} {p q : x ≈ y} {z w : P x} (r : p ≈ q) (s : z ≈ w) : ap (transport P p) s ⬝ transport2 P r w ≈ transport2 P r z ⬝ ap (transport P q) s := rec_on r (concat_p1 _ ⬝ (concat_1p _)^) -- TODO (from Coq library): What should this be called? definition ap_transport {A} {P Q : A → Type} {x y : A} (p : x ≈ y) (f : Πx, P x → Q x) (z : P x) : f y (p # z) ≈ (p # (f x z)) := rec_on p idp -- Transporting in particular fibrations -- ------------------------------------- /- From the Coq HoTT library: One frequently needs lemmas showing that transport in a certain dependent type is equal to some more explicitly defined operation, defined according to the structure of that dependent type. For most dependent types, we prove these lemmas in the appropriate file in the types/ subdirectory. Here we consider only the most basic cases. -/ -- Transporting in a constant fibration. definition transport_const {A B : Type} {x1 x2 : A} (p : x1 ≈ x2) (y : B) : transport (λx, B) p y ≈ y := rec_on p idp definition transport2_const {A B : Type} {x1 x2 : A} {p q : x1 ≈ x2} (r : p ≈ q) (y : B) : transport_const p y ≈ transport2 (λu, B) r y ⬝ transport_const q y := rec_on r (concat_1p _)^ -- Transporting in a pulled back fibration. definition transport_compose {A B} {x y : A} (P : B → Type) (f : A → B) (p : x ≈ y) (z : P (f x)) : transport (λx, P (f x)) p z ≈ transport P (ap f p) z := rec_on p idp definition transport_precompose {A B C} (f : A → B) (g g' : B → C) (p : g ≈ g') : transport (λh : B → C, g ∘ f ≈ h ∘ f) p idp ≈ ap (λh, h ∘ f) p := rec_on p idp definition apD10_ap_precompose {A B C} (f : A → B) (g g' : B → C) (p : g ≈ g') (a : A) : apD10 (ap (λh : B → C, h ∘ f) p) a ≈ apD10 p (f a) := rec_on p idp definition apD10_ap_postcompose {A B C} (f : B → C) (g g' : A → B) (p : g ≈ g') (a : A) : apD10 (ap (λh : A → B, f ∘ h) p) a ≈ ap f (apD10 p a) := rec_on p idp -- TODO: another example where a term has to be given explicitly -- A special case of [transport_compose] which seems to come up a lot. definition transport_idmap_ap A (P : A → Type) x y (p : x ≈ y) (u : P x) : transport P p u ≈ transport (λz, z) (ap P p) u := rec_on p (idpath (transport (λ (z : Type), z) (ap P (idpath x)) u)) -- The behavior of [ap] and [apD] -- ------------------------------ -- In a constant fibration, [apD] reduces to [ap], modulo [transport_const]. definition apD_const {A B} {x y : A} (f : A → B) (p: x ≈ y) : apD f p ≈ transport_const p (f x) ⬝ ap f p := rec_on p idp -- The 2-dimensional groupoid structure -- ------------------------------------ -- Horizontal composition of 2-dimensional paths. definition concat2 {A} {x y z : A} {p p' : x ≈ y} {q q' : y ≈ z} (h : p ≈ p') (h' : q ≈ q') : p ⬝ q ≈ p' ⬝ q' := rec_on h (rec_on h' idp) infixl `⬝⬝`:75 := concat2 -- 2-dimensional path inversion definition inverse2 {A : Type} {x y : A} {p q : x ≈ y} (h : p ≈ q) : p^ ≈ q^ := rec_on h idp -- Whiskering -- ---------- definition whiskerL {A : Type} {x y z : A} (p : x ≈ y) {q r : y ≈ z} (h : q ≈ r) : p ⬝ q ≈ p ⬝ r := idp ⬝⬝ h definition whiskerR {A : Type} {x y z : A} {p q : x ≈ y} (h : p ≈ q) (r : y ≈ z) : p ⬝ r ≈ q ⬝ r := h ⬝⬝ idp -- Unwhiskering, a.k.a. cancelling -- ------------------------------- definition cancelL {A} {x y z : A} (p : x ≈ y) (q r : y ≈ z) : (p ⬝ q ≈ p ⬝ r) → (q ≈ r) := rec_on p (take r, rec_on r (take q a, (concat_1p q)^ ⬝ a)) r q definition cancelR {A} {x y z : A} (p q : x ≈ y) (r : y ≈ z) : (p ⬝ r ≈ q ⬝ r) → (p ≈ q) := rec_on r (take p, rec_on p (take q a, a ⬝ concat_p1 q)) p q -- Whiskering and identity paths. definition whiskerR_p1 {A : Type} {x y : A} {p q : x ≈ y} (h : p ≈ q) : (concat_p1 p)^ ⬝ whiskerR h idp ⬝ concat_p1 q ≈ h := rec_on h (rec_on p idp) definition whiskerR_1p {A : Type} {x y z : A} (p : x ≈ y) (q : y ≈ z) : whiskerR idp q ≈ idp :> (p ⬝ q ≈ p ⬝ q) := rec_on q idp definition whiskerL_p1 {A : Type} {x y z : A} (p : x ≈ y) (q : y ≈ z) : whiskerL p idp ≈ idp :> (p ⬝ q ≈ p ⬝ q) := rec_on q idp definition whiskerL_1p {A : Type} {x y : A} {p q : x ≈ y} (h : p ≈ q) : (concat_1p p) ^ ⬝ whiskerL idp h ⬝ concat_1p q ≈ h := rec_on h (rec_on p idp) definition concat2_p1 {A : Type} {x y : A} {p q : x ≈ y} (h : p ≈ q) : h ⬝⬝ idp ≈ whiskerR h idp :> (p ⬝ idp ≈ q ⬝ idp) := rec_on h idp definition concat2_1p {A : Type} {x y : A} {p q : x ≈ y} (h : p ≈ q) : idp ⬝⬝ h ≈ whiskerL idp h :> (idp ⬝ p ≈ idp ⬝ q) := rec_on h idp -- TODO: note, 4 inductions -- The interchange law for concatenation. definition concat_concat2 {A : Type} {x y z : A} {p p' p'' : x ≈ y} {q q' q'' : y ≈ z} (a : p ≈ p') (b : p' ≈ p'') (c : q ≈ q') (d : q' ≈ q'') : (a ⬝⬝ c) ⬝ (b ⬝⬝ d) ≈ (a ⬝ b) ⬝⬝ (c ⬝ d) := rec_on d (rec_on c (rec_on b (rec_on a idp))) definition concat_whisker {A} {x y z : A} (p p' : x ≈ y) (q q' : y ≈ z) (a : p ≈ p') (b : q ≈ q') : (whiskerR a q) ⬝ (whiskerL p' b) ≈ (whiskerL p b) ⬝ (whiskerR a q') := rec_on b (rec_on a (concat_1p _)^) -- Structure corresponding to the coherence equations of a bicategory. -- The "pentagonator": the 3-cell witnessing the associativity pentagon. definition pentagon {A : Type} {v w x y z : A} (p : v ≈ w) (q : w ≈ x) (r : x ≈ y) (s : y ≈ z) : whiskerL p (concat_p_pp q r s) ⬝ concat_p_pp p (q ⬝ r) s ⬝ whiskerR (concat_p_pp p q r) s ≈ concat_p_pp p q (r ⬝ s) ⬝ concat_p_pp (p ⬝ q) r s := rec_on p (take q, rec_on q (take r, rec_on r (take s, rec_on s idp))) q r s -- The 3-cell witnessing the left unit triangle. definition triangulator {A : Type} {x y z : A} (p : x ≈ y) (q : y ≈ z) : concat_p_pp p idp q ⬝ whiskerR (concat_p1 p) q ≈ whiskerL p (concat_1p q) := rec_on p (take q, rec_on q idp) q definition eckmann_hilton {A : Type} {x:A} (p q : idp ≈ idp :> (x ≈ x)) : p ⬝ q ≈ q ⬝ p := (whiskerR_p1 p ⬝⬝ whiskerL_1p q)^ ⬝ (concat_p1 _ ⬝⬝ concat_p1 _) ⬝ (concat_1p _ ⬝⬝ concat_1p _) ⬝ (concat_whisker _ _ _ _ p q) ⬝ (concat_1p _ ⬝⬝ concat_1p _)^ ⬝ (concat_p1 _ ⬝⬝ concat_p1 _)^ ⬝ (whiskerL_1p q ⬝⬝ whiskerR_p1 p) -- The action of functions on 2-dimensional paths definition ap02 {A B : Type} (f:A → B) {x y : A} {p q : x ≈ y} (r : p ≈ q) : ap f p ≈ ap f q := rec_on r idp definition ap02_pp {A B} (f : A → B) {x y : A} {p p' p'' : x ≈ y} (r : p ≈ p') (r' : p' ≈ p'') : ap02 f (r ⬝ r') ≈ ap02 f r ⬝ ap02 f r' := rec_on r (rec_on r' idp) definition ap02_p2p {A B} (f : A→B) {x y z : A} {p p' : x ≈ y} {q q' :y ≈ z} (r : p ≈ p') (s : q ≈ q') : ap02 f (r ⬝⬝ s) ≈ ap_pp f p q ⬝ (ap02 f r ⬝⬝ ap02 f s) ⬝ (ap_pp f p' q')^ := rec_on r (rec_on s (rec_on q (rec_on p idp))) definition apD02 {A : Type} {B : A → Type} {x y : A} {p q : x ≈ y} (f : Π x, B x) (r : p ≈ q) : apD f p ≈ transport2 B r (f x) ⬝ apD f q := rec_on r (concat_1p _)^ end path
03ab609644dfcd6a953b5dcfaf71281a772faa39
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/valid/mathd-algebra-101.lean
e4a8f31b8c9a42a2849640e093ff78e97e37222a
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
265
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import data.real.basic example (x : ℝ) (h₀ : x ^ 2 - 5 * x - 4 ≤ 10 ) : x ≥ -2 ∧ x ≤ 7 := begin sorry end
0bcd8ab2a19b7e8db8f3047108c9dc04ec7877e7
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/group_theory/perm/list.lean
9b931cda52ffdc5c581b0e16f9f96cbf8ddd0044
[ "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
17,171
lean
/- Copyright (c) 2021 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import data.list.rotate import group_theory.perm.support /-! # Permutations from a list A list `l : list α` can be interpreted as a `equiv.perm α` where each element in the list is permuted to the next one, defined as `form_perm`. When we have that `nodup l`, we prove that `equiv.perm.support (form_perm l) = l.to_finset`, and that `form_perm l` is rotationally invariant, in `form_perm_rotate`. When there are duplicate elements in `l`, how and in what arrangement with respect to the other elements they appear in the list determines the formed permutation. This is because `list.form_perm` is implemented as a product of `equiv.swap`s. That means that presence of a sublist of two adjacent duplicates like `[..., x, x, ...]` will produce the same permutation as if the adjacent duplicates were not present. The `list.form_perm` definition is meant to primarily be used with `nodup l`, so that the resulting permutation is cyclic (if `l` has at least two elements). The presence of duplicates in a particular placement can lead `list.form_perm` to produce a nontrivial permutation that is noncyclic. -/ namespace list variables {α β : Type*} section form_perm variables [decidable_eq α] (l : list α) open equiv equiv.perm /-- A list `l : list α` can be interpreted as a `equiv.perm α` where each element in the list is permuted to the next one, defined as `form_perm`. When we have that `nodup l`, we prove that `equiv.perm.support (form_perm l) = l.to_finset`, and that `form_perm l` is rotationally invariant, in `form_perm_rotate`. -/ def form_perm : equiv.perm α := (zip_with equiv.swap l l.tail).prod @[simp] lemma form_perm_nil : form_perm ([] : list α) = 1 := rfl @[simp] lemma form_perm_singleton (x : α) : form_perm [x] = 1 := rfl @[simp] lemma form_perm_cons_cons (x y : α) (l : list α) : form_perm (x :: y :: l) = swap x y * form_perm (y :: l) := prod_cons lemma form_perm_pair (x y : α) : form_perm [x, y] = swap x y := rfl variables {l} {x : α} lemma form_perm_apply_of_not_mem (x : α) (l : list α) (h : x ∉ l) : form_perm l x = x := begin cases l with y l, { simp }, induction l with z l IH generalizing x y, { simp }, { specialize IH x z (mt (mem_cons_of_mem y) h), simp only [not_or_distrib, mem_cons_iff] at h, simp [IH, swap_apply_of_ne_of_ne, h] } end lemma mem_of_form_perm_apply_ne (x : α) (l : list α) : l.form_perm x ≠ x → x ∈ l := not_imp_comm.2 $ list.form_perm_apply_of_not_mem _ _ lemma form_perm_apply_mem_of_mem (x : α) (l : list α) (h : x ∈ l) : form_perm l x ∈ l := begin cases l with y l, { simpa }, induction l with z l IH generalizing x y, { simpa using h }, { by_cases hx : x ∈ z :: l, { rw [form_perm_cons_cons, mul_apply, swap_apply_def], split_ifs; simp [IH _ _ hx] }, { replace h : x = y := or.resolve_right h hx, simp [form_perm_apply_of_not_mem _ _ hx, ←h] } } end lemma mem_of_form_perm_apply_mem (x : α) (l : list α) (h : l.form_perm x ∈ l) : x ∈ l := begin cases l with y l, { simpa }, induction l with z l IH generalizing x y, { simpa using h }, { by_cases hx : (z :: l).form_perm x ∈ z :: l, { rw [list.form_perm_cons_cons, mul_apply, swap_apply_def] at h, split_ifs at h; simp [IH _ _ hx] }, { replace hx := (function.injective.eq_iff (equiv.injective _)).mp (list.form_perm_apply_of_not_mem _ _ hx), simp only [list.form_perm_cons_cons, hx, equiv.perm.coe_mul, function.comp_app, list.mem_cons_iff, swap_apply_def, ite_eq_left_iff] at h, simp only [list.mem_cons_iff], obtain h | h | h := h; { split_ifs at h; cc }}} end lemma form_perm_mem_iff_mem : l.form_perm x ∈ l ↔ x ∈ l := ⟨l.mem_of_form_perm_apply_mem x, l.form_perm_apply_mem_of_mem x⟩ @[simp] lemma form_perm_cons_concat_apply_last (x y : α) (xs : list α) : form_perm (x :: (xs ++ [y])) y = x := begin induction xs with z xs IH generalizing x y, { simp }, { simp [IH] } end @[simp] lemma form_perm_apply_last (x : α) (xs : list α) : form_perm (x :: xs) ((x :: xs).last (cons_ne_nil x xs)) = x := begin induction xs using list.reverse_rec_on with xs y IH generalizing x; simp end @[simp] lemma form_perm_apply_nth_le_length (x : α) (xs : list α) : form_perm (x :: xs) ((x :: xs).nth_le xs.length (by simp)) = x := by rw [nth_le_cons_length, form_perm_apply_last]; refl lemma form_perm_apply_head (x y : α) (xs : list α) (h : nodup (x :: y :: xs)) : form_perm (x :: y :: xs) x = y := by simp [form_perm_apply_of_not_mem _ _ h.not_mem] lemma form_perm_apply_nth_le_zero (l : list α) (h : nodup l) (hl : 1 < l.length) : form_perm l (l.nth_le 0 (zero_lt_one.trans hl)) = l.nth_le 1 hl := begin rcases l with (_|⟨x, _|⟨y, tl⟩⟩), { simp }, { simp }, { simpa using form_perm_apply_head _ _ _ h } end variables (l) lemma form_perm_eq_head_iff_eq_last (x y : α) : form_perm (y :: l) x = y ↔ x = last (y :: l) (cons_ne_nil _ _) := iff.trans (by rw form_perm_apply_last) (form_perm (y :: l)).injective.eq_iff lemma zip_with_swap_prod_support' (l l' : list α) : {x | (zip_with swap l l').prod x ≠ x} ≤ l.to_finset ⊔ l'.to_finset := begin simp only [set.sup_eq_union, set.le_eq_subset], induction l with y l hl generalizing l', { simp }, { cases l' with z l', { simp }, { intro x, simp only [set.union_subset_iff, mem_cons_iff, zip_with_cons_cons, foldr, prod_cons, mul_apply], intro hx, by_cases h : x ∈ {x | (zip_with swap l l').prod x ≠ x}, { specialize hl l' h, refine set.mem_union.elim hl (λ hm, _) (λ hm, _); { simp only [finset.coe_insert, set.mem_insert_iff, finset.mem_coe, to_finset_cons, mem_to_finset] at hm ⊢, simp [hm] } }, { simp only [not_not, set.mem_set_of_eq] at h, simp only [h, set.mem_set_of_eq] at hx, rw swap_apply_ne_self_iff at hx, rcases hx with ⟨hyz, rfl|rfl⟩; simp } } } end lemma zip_with_swap_prod_support [fintype α] (l l' : list α) : (zip_with swap l l').prod.support ≤ l.to_finset ⊔ l'.to_finset := begin intros x hx, have hx' : x ∈ {x | (zip_with swap l l').prod x ≠ x} := by simpa using hx, simpa using zip_with_swap_prod_support' _ _ hx' end lemma support_form_perm_le' : {x | form_perm l x ≠ x} ≤ l.to_finset := begin refine (zip_with_swap_prod_support' l l.tail).trans _, simpa [finset.subset_iff] using tail_subset l end lemma support_form_perm_le [fintype α] : support (form_perm l) ≤ l.to_finset := begin intros x hx, have hx' : x ∈ {x | form_perm l x ≠ x} := by simpa using hx, simpa using support_form_perm_le' _ hx' end lemma form_perm_apply_lt (xs : list α) (h : nodup xs) (n : ℕ) (hn : n + 1 < xs.length) : form_perm xs (xs.nth_le n ((nat.lt_succ_self n).trans hn)) = xs.nth_le (n + 1) hn := begin induction n with n IH generalizing xs, { simpa using form_perm_apply_nth_le_zero _ h _ }, { rcases xs with (_|⟨x, _|⟨y, l⟩⟩), { simp }, { simp }, { specialize IH (y :: l) h.of_cons _, { simpa [nat.succ_lt_succ_iff] using hn }, simp only [swap_apply_eq_iff, coe_mul, form_perm_cons_cons, nth_le], generalize_proofs at IH, rw [IH, swap_apply_of_ne_of_ne, nth_le]; { rintro rfl, simpa [nth_le_mem _ _ _] using h } } } end lemma form_perm_apply_nth_le (xs : list α) (h : nodup xs) (n : ℕ) (hn : n < xs.length) : form_perm xs (xs.nth_le n hn) = xs.nth_le ((n + 1) % xs.length) (nat.mod_lt _ (n.zero_le.trans_lt hn)) := begin cases xs with x xs, { simp }, { have : n ≤ xs.length, { refine nat.le_of_lt_succ _, simpa using hn }, rcases this.eq_or_lt with rfl|hn', { simp }, { simp [form_perm_apply_lt, h, nat.mod_eq_of_lt, nat.succ_lt_succ hn'] } } end lemma support_form_perm_of_nodup' (l : list α) (h : nodup l) (h' : ∀ (x : α), l ≠ [x]) : {x | form_perm l x ≠ x} = l.to_finset := begin apply le_antisymm, { exact support_form_perm_le' l }, { intros x hx, simp only [finset.mem_coe, mem_to_finset] at hx, obtain ⟨n, hn, rfl⟩ := nth_le_of_mem hx, rw [set.mem_set_of_eq, form_perm_apply_nth_le _ h], intro H, rw nodup_iff_nth_le_inj at h, specialize h _ _ _ _ H, cases (nat.succ_le_of_lt hn).eq_or_lt with hn' hn', { simp only [←hn', nat.mod_self] at h, refine not_exists.mpr h' _, simpa [←h, eq_comm, length_eq_one] using hn' }, { simpa [nat.mod_eq_of_lt hn'] using h } } end lemma support_form_perm_of_nodup [fintype α] (l : list α) (h : nodup l) (h' : ∀ (x : α), l ≠ [x]) : support (form_perm l) = l.to_finset := begin rw ←finset.coe_inj, convert support_form_perm_of_nodup' _ h h', simp [set.ext_iff] end lemma form_perm_rotate_one (l : list α) (h : nodup l) : form_perm (l.rotate 1) = form_perm l := begin have h' : nodup (l.rotate 1), { simpa using h }, ext x, by_cases hx : x ∈ l.rotate 1, { obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, rw [form_perm_apply_nth_le _ h', nth_le_rotate l, nth_le_rotate l, form_perm_apply_nth_le _ h], simp }, { rw [form_perm_apply_of_not_mem _ _ hx, form_perm_apply_of_not_mem], simpa using hx } end lemma form_perm_rotate (l : list α) (h : nodup l) (n : ℕ) : form_perm (l.rotate n) = form_perm l := begin induction n with n hn, { simp }, { rw [nat.succ_eq_add_one, ←rotate_rotate, form_perm_rotate_one, hn], rwa is_rotated.nodup_iff, exact is_rotated.forall l n } end lemma form_perm_eq_of_is_rotated {l l' : list α} (hd : nodup l) (h : l ~r l') : form_perm l = form_perm l' := begin obtain ⟨n, rfl⟩ := h, exact (form_perm_rotate l hd n).symm end lemma form_perm_reverse (l : list α) (h : nodup l) : form_perm l.reverse = (form_perm l)⁻¹ := begin -- Let's show `form_perm l` is an inverse to `form_perm l.reverse`. rw [eq_comm, inv_eq_iff_mul_eq_one], ext x, -- We only have to check for `x ∈ l` that `form_perm l (form_perm l.reverse x)` rw [mul_apply, one_apply], by_cases hx : x ∈ l, swap, { rw [form_perm_apply_of_not_mem x l.reverse, form_perm_apply_of_not_mem _ _ hx], simpa using hx }, { obtain ⟨k, hk, rfl⟩ := nth_le_of_mem (mem_reverse.mpr hx), rw [form_perm_apply_nth_le l.reverse (nodup_reverse.mpr h), nth_le_reverse', form_perm_apply_nth_le _ h, nth_le_reverse'], { congr, rw [length_reverse, ←nat.succ_le_iff, nat.succ_eq_add_one] at hk, cases hk.eq_or_lt with hk' hk', { simp [←hk'] }, { rw [length_reverse, nat.mod_eq_of_lt hk', tsub_add_eq_add_tsub (nat.le_pred_of_lt hk'), nat.mod_eq_of_lt], { simp }, { rw tsub_add_cancel_of_le, refine tsub_lt_self _ (nat.zero_lt_succ _), all_goals { simpa using (nat.zero_le _).trans_lt hk' } } } }, all_goals { rw [← tsub_add_eq_tsub_tsub, ←length_reverse], refine tsub_lt_self _ (zero_lt_one.trans_le (le_add_right le_rfl)), exact k.zero_le.trans_lt hk } }, end lemma form_perm_pow_apply_nth_le (l : list α) (h : nodup l) (n k : ℕ) (hk : k < l.length) : (form_perm l ^ n) (l.nth_le k hk) = l.nth_le ((k + n) % l.length) (nat.mod_lt _ (k.zero_le.trans_lt hk)) := begin induction n with n hn, { simp [nat.mod_eq_of_lt hk] }, { simp [pow_succ, mul_apply, hn, form_perm_apply_nth_le _ h, nat.succ_eq_add_one, ←nat.add_assoc] } end lemma form_perm_pow_apply_head (x : α) (l : list α) (h : nodup (x :: l)) (n : ℕ) : (form_perm (x :: l) ^ n) x = (x :: l).nth_le (n % (x :: l).length) (nat.mod_lt _ (nat.zero_lt_succ _)) := by { convert form_perm_pow_apply_nth_le _ h n 0 _; simp } lemma form_perm_ext_iff {x y x' y' : α} {l l' : list α} (hd : nodup (x :: y :: l)) (hd' : nodup (x' :: y' :: l')) : form_perm (x :: y :: l) = form_perm (x' :: y' :: l') ↔ (x :: y :: l) ~r (x' :: y' :: l') := begin refine ⟨λ h, _, λ hr, form_perm_eq_of_is_rotated hd hr⟩, rw equiv.perm.ext_iff at h, have hx : x' ∈ (x :: y :: l), { have : x' ∈ {z | form_perm (x :: y :: l) z ≠ z}, { rw [set.mem_set_of_eq, h x', form_perm_apply_head _ _ _ hd'], simp only [mem_cons_iff, nodup_cons] at hd', push_neg at hd', exact hd'.left.left.symm }, simpa using support_form_perm_le' _ this }, obtain ⟨n, hn, hx'⟩ := nth_le_of_mem hx, have hl : (x :: y :: l).length = (x' :: y' :: l').length, { rw [←dedup_eq_self.mpr hd, ←dedup_eq_self.mpr hd', ←card_to_finset, ←card_to_finset], refine congr_arg finset.card _, rw [←finset.coe_inj, ←support_form_perm_of_nodup' _ hd (by simp), ←support_form_perm_of_nodup' _ hd' (by simp)], simp only [h] }, use n, apply list.ext_le, { rw [length_rotate, hl] }, { intros k hk hk', rw nth_le_rotate, induction k with k IH, { simp_rw [nat.zero_add, nat.mod_eq_of_lt hn], simpa }, { have : k.succ = (k + 1) % (x' :: y' :: l').length, { rw [←nat.succ_eq_add_one, nat.mod_eq_of_lt hk'] }, simp_rw this, rw [←form_perm_apply_nth_le _ hd' k (k.lt_succ_self.trans hk'), ←IH (k.lt_succ_self.trans hk), ←h, form_perm_apply_nth_le _ hd], congr' 1, have h1 : 1 = 1 % (x' :: y' :: l').length := by simp, rw [hl, nat.mod_eq_of_lt hk', h1, ←nat.add_mod, nat.succ_add] } } end lemma form_perm_apply_mem_eq_self_iff (hl : nodup l) (x : α) (hx : x ∈ l) : form_perm l x = x ↔ length l ≤ 1 := begin obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, rw [form_perm_apply_nth_le _ hl, hl.nth_le_inj_iff], cases hn : l.length, { exact absurd k.zero_le (hk.trans_le hn.le).not_le }, { rw hn at hk, cases (nat.le_of_lt_succ hk).eq_or_lt with hk' hk', { simp [←hk', nat.succ_le_succ_iff, eq_comm] }, { simpa [nat.mod_eq_of_lt (nat.succ_lt_succ hk'), nat.succ_lt_succ_iff] using k.zero_le.trans_lt hk' } } end lemma form_perm_apply_mem_ne_self_iff (hl : nodup l) (x : α) (hx : x ∈ l) : form_perm l x ≠ x ↔ 2 ≤ l.length := begin rw [ne.def, form_perm_apply_mem_eq_self_iff _ hl x hx, not_le], exact ⟨nat.succ_le_of_lt, nat.lt_of_succ_le⟩ end lemma mem_of_form_perm_ne_self (l : list α) (x : α) (h : form_perm l x ≠ x) : x ∈ l := begin suffices : x ∈ {y | form_perm l y ≠ y}, { rw ←mem_to_finset, exact support_form_perm_le' _ this }, simpa using h end lemma form_perm_eq_self_of_not_mem (l : list α) (x : α) (h : x ∉ l) : form_perm l x = x := by_contra (λ H, h $ mem_of_form_perm_ne_self _ _ H) lemma form_perm_eq_one_iff (hl : nodup l) : form_perm l = 1 ↔ l.length ≤ 1 := begin cases l with hd tl, { simp }, { rw ←form_perm_apply_mem_eq_self_iff _ hl hd (mem_cons_self _ _), split, { simp {contextual := tt} }, { intro h, simp only [(hd :: tl).form_perm_apply_mem_eq_self_iff hl hd (mem_cons_self hd tl), add_le_iff_nonpos_left, length, nonpos_iff_eq_zero, length_eq_zero] at h, simp [h] } } end lemma form_perm_eq_form_perm_iff {l l' : list α} (hl : l.nodup) (hl' : l'.nodup) : l.form_perm = l'.form_perm ↔ l ~r l' ∨ l.length ≤ 1 ∧ l'.length ≤ 1 := begin rcases l with (_ | ⟨x, _ | ⟨y, l⟩⟩), { suffices : l'.length ≤ 1 ↔ l' = nil ∨ l'.length ≤ 1, { simpa [eq_comm, form_perm_eq_one_iff, hl, hl', length_eq_zero] }, refine ⟨λ h, or.inr h, _⟩, rintro (rfl | h), { simp }, { exact h } }, { suffices : l'.length ≤ 1 ↔ [x] ~r l' ∨ l'.length ≤ 1, { simpa [eq_comm, form_perm_eq_one_iff, hl, hl', length_eq_zero, le_rfl] }, refine ⟨λ h, or.inr h, _⟩, rintro (h | h), { simp [←h.perm.length_eq] }, { exact h } }, { rcases l' with (_ | ⟨x', _ | ⟨y', l'⟩⟩), { simp [form_perm_eq_one_iff, hl, -form_perm_cons_cons] }, { suffices : ¬ (x :: y :: l) ~r [x'], { simp [form_perm_eq_one_iff, hl, -form_perm_cons_cons] }, intro h, simpa using h.perm.length_eq }, { simp [-form_perm_cons_cons, form_perm_ext_iff hl hl'] } } end lemma form_perm_zpow_apply_mem_imp_mem (l : list α) (x : α) (hx : x ∈ l) (n : ℤ) : ((form_perm l) ^ n) x ∈ l := begin by_cases h : (l.form_perm ^ n) x = x, { simpa [h] using hx }, { have : x ∈ {x | (l.form_perm ^ n) x ≠ x} := h, rw ←set_support_apply_mem at this, replace this := set_support_zpow_subset _ _ this, simpa using support_form_perm_le' _ this } end lemma form_perm_pow_length_eq_one_of_nodup (hl : nodup l) : (form_perm l) ^ (length l) = 1 := begin ext x, by_cases hx : x ∈ l, { obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, simp [form_perm_pow_apply_nth_le _ hl, nat.mod_eq_of_lt hk] }, { have : x ∉ {x | (l.form_perm ^ l.length) x ≠ x}, { intros H, refine hx _, replace H := set_support_zpow_subset l.form_perm l.length H, simpa using support_form_perm_le' _ H }, simpa } end end form_perm end list
1e920b738eb743082a2227da2985a11f0df220a1
947b78d97130d56365ae2ec264df196ce769371a
/tests/compiler/float.lean
94edec44c0d6e1a3c6bb765612b8322c12c30b7a
[ "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
967
lean
def tst1 : IO Unit := do IO.println (1 : Float); IO.println ((1 : Float) + 2); IO.println ((2 : Float) - 3); IO.println ((3 : Float) * 2); IO.println ((3 : Float) / 2); IO.println (decide ((3 : Float) < 2)); IO.println (decide ((3 : Float) < 4)); IO.println ((3 : Float) == 2); IO.println ((2 : Float) == 2); IO.println (decide ((3 : Float) ≤ 2)); IO.println (decide ((3 : Float) ≤ 3)); IO.println (decide ((3 : Float) ≤ 4)); pure () structure Foo := (x : Nat) (w : UInt64) (y : Float) (z : Float) @[noinline] def mkFoo (x : Nat) : Foo := { x := x, w := x.toUInt64, y := x.toFloat / 3, z := x.toFloat / 2 } def tst2 (x : Nat) : IO Unit := do let foo := mkFoo x; IO.println foo.y; IO.println foo.z @[noinline] def fMap (f : Float → Float) (xs : List Float) := xs.map f def tst3 (xs : List Float) (y : Float) : IO Unit := IO.println (fMap (fun x => x / y) xs) def main : IO Unit := do tst1; IO.println "-----"; tst2 7; tst3 [3, 4, 7, 8, 9, 11] 2; pure ()
c7ded692a159bce84129a9622e47593df2857d61
9dc8cecdf3c4634764a18254e94d43da07142918
/src/number_theory/function_field.lean
bccb243f2de047ede088498f79568c79a8aa0634
[ "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
10,592
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Ashvni Narayanan -/ import field_theory.ratfunc import ring_theory.algebraic import ring_theory.dedekind_domain.integral_closure import ring_theory.integrally_closed import topology.algebra.valued_field /-! # Function fields This file defines a function field and the ring of integers corresponding to it. ## Main definitions - `function_field Fq F` states that `F` is a function field over the (finite) field `Fq`, i.e. it is a finite extension of the field of rational functions in one variable over `Fq`. - `function_field.ring_of_integers` defines the ring of integers corresponding to a function field as the integral closure of `polynomial Fq` in the function field. - `function_field.infty_valuation` : The place at infinity on `Fq(t)` is the nonarchimedean valuation on `Fq(t)` with uniformizer `1/t`. - `function_field.Fqt_infty` : The completion `Fq((t⁻¹))` of `Fq(t)` with respect to the valuation at infinity. ## Implementation notes The definitions that involve a field of fractions choose a canonical field of fractions, but are independent of that choice. We also omit assumptions like `finite Fq` or `is_scalar_tower Fq[X] (fraction_ring Fq[X]) F` in definitions, adding them back in lemmas when they are needed. ## References * [D. Marcus, *Number Fields*][marcus1977number] * [J.W.S. Cassels, A. Frölich, *Algebraic Number Theory*][cassels1967algebraic] * [P. Samuel, *Algebraic Theory of Numbers*][samuel1970algebraic] ## Tags function field, ring of integers -/ noncomputable theory open_locale non_zero_divisors polynomial discrete_valuation variables (Fq F : Type) [field Fq] [field F] /-- `F` is a function field over the finite field `Fq` if it is a finite extension of the field of rational functions in one variable over `Fq`. Note that `F` can be a function field over multiple, non-isomorphic, `Fq`. -/ abbreviation function_field [algebra (ratfunc Fq) F] : Prop := finite_dimensional (ratfunc Fq) F /-- `F` is a function field over `Fq` iff it is a finite extension of `Fq(t)`. -/ protected lemma function_field_iff (Fqt : Type*) [field Fqt] [algebra Fq[X] Fqt] [is_fraction_ring Fq[X] Fqt] [algebra (ratfunc Fq) F] [algebra Fqt F] [algebra Fq[X] F] [is_scalar_tower Fq[X] Fqt F] [is_scalar_tower Fq[X] (ratfunc Fq) F] : function_field Fq F ↔ finite_dimensional Fqt F := begin let e := is_localization.alg_equiv Fq[X]⁰ (ratfunc Fq) Fqt, have : ∀ c (x : F), e c • x = c • x, { intros c x, rw [algebra.smul_def, algebra.smul_def], congr, refine congr_fun _ c, refine is_localization.ext (non_zero_divisors (Fq[X])) _ _ _ _ _ _ _; intros; simp only [alg_equiv.map_one, ring_hom.map_one, alg_equiv.map_mul, ring_hom.map_mul, alg_equiv.commutes, ← is_scalar_tower.algebra_map_apply], }, split; intro h; resetI, { let b := finite_dimensional.fin_basis (ratfunc Fq) F, exact finite_dimensional.of_fintype_basis (b.map_coeffs e this) }, { let b := finite_dimensional.fin_basis Fqt F, refine finite_dimensional.of_fintype_basis (b.map_coeffs e.symm _), intros c x, convert (this (e.symm c) x).symm, simp only [e.apply_symm_apply] }, end lemma algebra_map_injective [algebra Fq[X] F] [algebra (ratfunc Fq) F] [is_scalar_tower Fq[X] (ratfunc Fq) F] : function.injective ⇑(algebra_map Fq[X] F) := begin rw is_scalar_tower.algebra_map_eq Fq[X] (ratfunc Fq) F, exact function.injective.comp ((algebra_map (ratfunc Fq) F).injective) (is_fraction_ring.injective Fq[X] (ratfunc Fq)), end namespace function_field /-- The function field analogue of `number_field.ring_of_integers`: `function_field.ring_of_integers Fq Fqt F` is the integral closure of `Fq[t]` in `F`. We don't actually assume `F` is a function field over `Fq` in the definition, only when proving its properties. -/ def ring_of_integers [algebra Fq[X] F] := integral_closure Fq[X] F namespace ring_of_integers variables [algebra Fq[X] F] instance : is_domain (ring_of_integers Fq F) := (ring_of_integers Fq F).is_domain instance : is_integral_closure (ring_of_integers Fq F) Fq[X] F := integral_closure.is_integral_closure _ _ variables [algebra (ratfunc Fq) F] [is_scalar_tower Fq[X] (ratfunc Fq) F] lemma algebra_map_injective : function.injective ⇑(algebra_map Fq[X] (ring_of_integers Fq F)) := begin have hinj : function.injective ⇑(algebra_map Fq[X] F), { rw is_scalar_tower.algebra_map_eq Fq[X] (ratfunc Fq) F, exact function.injective.comp ((algebra_map (ratfunc Fq) F).injective) (is_fraction_ring.injective Fq[X] (ratfunc Fq)), }, rw injective_iff_map_eq_zero (algebra_map Fq[X] ↥(ring_of_integers Fq F)), intros p hp, rw [← subtype.coe_inj, subalgebra.coe_zero] at hp, rw injective_iff_map_eq_zero (algebra_map Fq[X] F) at hinj, exact hinj p hp, end lemma not_is_field : ¬ is_field (ring_of_integers Fq F) := by simpa [← ((is_integral_closure.is_integral_algebra Fq[X] F).is_field_iff_is_field (algebra_map_injective Fq F))] using (polynomial.not_is_field Fq) variables [function_field Fq F] instance : is_fraction_ring (ring_of_integers Fq F) F := integral_closure.is_fraction_ring_of_finite_extension (ratfunc Fq) F instance : is_integrally_closed (ring_of_integers Fq F) := integral_closure.is_integrally_closed_of_finite_extension (ratfunc Fq) instance [is_separable (ratfunc Fq) F] : is_noetherian Fq[X] (ring_of_integers Fq F) := is_integral_closure.is_noetherian _ (ratfunc Fq) F _ instance [is_separable (ratfunc Fq) F] : is_dedekind_domain (ring_of_integers Fq F) := is_integral_closure.is_dedekind_domain Fq[X] (ratfunc Fq) F _ end ring_of_integers /-! ### The place at infinity on Fq(t) -/ section infty_valuation variable [decidable_eq (ratfunc Fq)] /-- The valuation at infinity is the nonarchimedean valuation on `Fq(t)` with uniformizer `1/t`. Explicitly, if `f/g ∈ Fq(t)` is a nonzero quotient of polynomials, its valuation at infinity is `multiplicative.of_add(degree(f) - degree(g))`. -/ def infty_valuation_def (r : ratfunc Fq) : ℤₘ₀ := if r = 0 then 0 else (multiplicative.of_add r.int_degree) lemma infty_valuation.map_zero' : infty_valuation_def Fq 0 = 0 := if_pos rfl lemma infty_valuation.map_one' : infty_valuation_def Fq 1 = 1 := (if_neg one_ne_zero).trans $ by rw [ratfunc.int_degree_one, of_add_zero, with_zero.coe_one] lemma infty_valuation.map_mul' (x y : ratfunc Fq) : infty_valuation_def Fq (x * y) = infty_valuation_def Fq x * infty_valuation_def Fq y := begin rw [infty_valuation_def, infty_valuation_def, infty_valuation_def], by_cases hx : x = 0, { rw [hx, zero_mul, if_pos (eq.refl _), zero_mul] }, { by_cases hy : y = 0, { rw [hy, mul_zero, if_pos (eq.refl _), mul_zero] }, { rw [if_neg hx, if_neg hy, if_neg (mul_ne_zero hx hy), ← with_zero.coe_mul, with_zero.coe_inj, ← of_add_add, ratfunc.int_degree_mul hx hy], }} end lemma infty_valuation.map_add_le_max' (x y : ratfunc Fq) : infty_valuation_def Fq (x + y) ≤ max (infty_valuation_def Fq x) (infty_valuation_def Fq y) := begin by_cases hx : x = 0, { rw [hx, zero_add], conv_rhs { rw [infty_valuation_def, if_pos (eq.refl _)] }, rw max_eq_right (with_zero.zero_le (infty_valuation_def Fq y)), exact le_refl _ }, { by_cases hy : y = 0, { rw [hy, add_zero], conv_rhs { rw [max_comm, infty_valuation_def, if_pos (eq.refl _)] }, rw max_eq_right (with_zero.zero_le (infty_valuation_def Fq x)), exact le_refl _ }, { by_cases hxy : x + y = 0, { rw [infty_valuation_def, if_pos hxy], exact zero_le',}, { rw [infty_valuation_def, infty_valuation_def, infty_valuation_def, if_neg hx, if_neg hy, if_neg hxy], rw [le_max_iff, with_zero.coe_le_coe, multiplicative.of_add_le, with_zero.coe_le_coe, multiplicative.of_add_le, ← le_max_iff], exact ratfunc.int_degree_add_le hy hxy }}} end @[simp] lemma infty_valuation_of_nonzero {x : ratfunc Fq} (hx : x ≠ 0) : infty_valuation_def Fq x = (multiplicative.of_add x.int_degree) := by rw [infty_valuation_def, if_neg hx] /-- The valuation at infinity on `Fq(t)`. -/ def infty_valuation : valuation (ratfunc Fq) ℤₘ₀ := { to_fun := infty_valuation_def Fq, map_zero' := infty_valuation.map_zero' Fq, map_one' := infty_valuation.map_one' Fq, map_mul' := infty_valuation.map_mul' Fq, map_add_le_max' := infty_valuation.map_add_le_max' Fq } @[simp] lemma infty_valuation_apply {x : ratfunc Fq} : infty_valuation Fq x = infty_valuation_def Fq x := rfl @[simp] lemma infty_valuation.C {k : Fq} (hk : k ≠ 0) : infty_valuation_def Fq (ratfunc.C k) = (multiplicative.of_add (0 : ℤ)) := begin have hCk : ratfunc.C k ≠ 0 := (map_ne_zero _).mpr hk, rw [infty_valuation_def, if_neg hCk, ratfunc.int_degree_C], end @[simp] lemma infty_valuation.X : infty_valuation_def Fq (ratfunc.X) = (multiplicative.of_add (1 : ℤ)) := by rw [infty_valuation_def, if_neg ratfunc.X_ne_zero, ratfunc.int_degree_X] @[simp] lemma infty_valuation.polynomial {p : polynomial Fq} (hp : p ≠ 0) : infty_valuation_def Fq (algebra_map (polynomial Fq) (ratfunc Fq) p) = (multiplicative.of_add (p.nat_degree : ℤ)) := begin have hp' : algebra_map (polynomial Fq) (ratfunc Fq) p ≠ 0, { rw [ne.def, ratfunc.algebra_map_eq_zero_iff], exact hp }, rw [infty_valuation_def, if_neg hp', ratfunc.int_degree_polynomial] end /-- The valued field `Fq(t)` with the valuation at infinity. -/ def infty_valued_Fqt : valued (ratfunc Fq) ℤₘ₀ := valued.mk' $ infty_valuation Fq lemma infty_valued_Fqt.def {x : ratfunc Fq} : @valued.v (ratfunc Fq) _ _ _ (infty_valued_Fqt Fq) x = infty_valuation_def Fq x := rfl /-- The completion `Fq((t⁻¹))` of `Fq(t)` with respect to the valuation at infinity. -/ def Fqt_infty := @uniform_space.completion (ratfunc Fq) $ (infty_valued_Fqt Fq).to_uniform_space instance : field (Fqt_infty Fq) := by { letI := infty_valued_Fqt Fq, exact uniform_space.completion.field } instance : inhabited (Fqt_infty Fq) := ⟨(0 : Fqt_infty Fq)⟩ /-- The valuation at infinity on `k(t)` extends to a valuation on `Fqt_infty`. -/ instance valued_Fqt_infty : valued (Fqt_infty Fq) ℤₘ₀ := @valued.valued_completion _ _ _ _ (infty_valued_Fqt Fq) lemma valued_Fqt_infty.def {x : Fqt_infty Fq} : valued.v x = @valued.extension (ratfunc Fq) _ _ _ (infty_valued_Fqt Fq) x := rfl end infty_valuation end function_field
e3fefadb589b23e49c799621ca8787738683bc28
e38d5e91d30731bef617cc9b6de7f79c34cdce9a
/src/examples/adjacency.lean
029ccedfe351b5824d8ffc2aec1856794a0aa588
[ "Apache-2.0" ]
permissive
bbentzen/cubicalean
55e979c303fbf55a81ac46b1000c944b2498be7a
3b94cd2aefdfc2163c263bd3fc6f2086fef814b5
refs/heads/master
1,588,314,875,258
1,554,412,699,000
1,554,412,699,000
177,333,390
0
0
null
null
null
null
UTF-8
Lean
false
false
1,425
lean
/- Copyright (c) 2019 Bruno Bentzen. All rights reserved. Released under the Apache License 2.0 (see "License"); Author: Bruno Bentzen -/ import ..core.interval open interval variables (A : I → I → Type) (a : Π j i, A j i) -- adjacency conditions are immediate example : a i0 i0 = (λ i, a i i0) i0 := rfl example : a i1 i0 = (λ i, a i i0) i1 := rfl example : a i0 i1 = (λ i, a i i1) i0 := rfl example : a i1 i1 = (λ i, a i i1) i1 := rfl /- (λ i, a i1 i) i1 = (λ j, a j i1) i1 ======= (λ i, a i1 i) i1 = (λ j, a j i1) i1 --> i || | j | || | v || | λ j, a j i0 || a | a j i1 || | || | || v (λ i, a i1 i) i1 = (λ j, a j i1) i1 ======= (λ i, a i1 i) i1 = (λ j, a j i1) i1 -/ -- faces and degeneracies example : (λ _, a) i0 = a := rfl example : (λ _, a) i1 = a := rfl example : (λ _, a i0) i1 = a i0 := rfl
7fadb35776f97cdbb491eb65af4b05459cf9e6fd
12dabd587ce2621d9a4eff9f16e354d02e206c8e
/world10/level06.lean
2c171a231d43e74b59bd716d22ed4fe51f002342
[]
no_license
abdelq/natural-number-game
a1b5b8f1d52625a7addcefc97c966d3f06a48263
bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2
refs/heads/master
1,668,606,478,691
1,594,175,058,000
1,594,175,058,000
278,673,209
0
1
null
null
null
null
UTF-8
Lean
false
false
283
lean
theorem le_antisymm (a b : mynat) (hab : a ≤ b) (hba : b ≤ a) : a = b := begin cases hab with c hc, cases hba with d hd, rw hc at hd, rw add_assoc at hd, symmetry at hd, have h1 := eq_zero_of_add_right_eq_self hd, have h2 := add_right_eq_zero h1, rw hc, rw h2, rwa add_zero, end
e82dda9ff67c08c1457c761fe56400148be81104
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/src/Lean/Meta/Tactic/Injection.lean
5d4cf04e5a947a22883b4b408a4b79b24319c22c
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,613
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.Meta.AppBuilder import Lean.Meta.Tactic.Clear import Lean.Meta.Tactic.Assert import Lean.Meta.Tactic.Intro namespace Lean.Meta inductive InjectionResultCore where | solved | subgoal (mvarId : MVarId) (numNewEqs : Nat) def injectionCore (mvarId : MVarId) (fvarId : FVarId) : MetaM InjectionResultCore := withMVarContext mvarId do checkNotAssigned mvarId `injection let decl ← getLocalDecl fvarId let type ← whnf decl.type match type.eq? with | none => throwTacticEx `injection mvarId "equality expected" | some (α, a, b) => let a ← whnf a let b ← whnf b let target ← getMVarType mvarId let env ← getEnv match a.isConstructorApp? env, b.isConstructorApp? env with | some aCtor, some bCtor => let val ← mkNoConfusion target (mkFVar fvarId) if aCtor.name != bCtor.name then assignExprMVar mvarId val pure InjectionResultCore.solved else let valType ← inferType val let valType ← whnf valType match valType with | Expr.forallE _ newTarget _ _ => let newTarget := newTarget.headBeta let tag ← getMVarTag mvarId let newMVar ← mkFreshExprSyntheticOpaqueMVar newTarget tag assignExprMVar mvarId (mkApp val newMVar) let mvarId ← tryClear newMVar.mvarId! fvarId pure $ InjectionResultCore.subgoal mvarId aCtor.nfields | _ => throwTacticEx `injection mvarId "ill-formed noConfusion auxiliary construction" | _, _ => throwTacticEx `injection mvarId "equality of constructor applications expected" inductive InjectionResult where | solved | subgoal (mvarId : MVarId) (newEqs : Array FVarId) (remainingNames : List Name) private def heqToEq (mvarId : MVarId) (fvarId : FVarId) : MetaM (FVarId × MVarId) := withMVarContext mvarId do let decl ← getLocalDecl fvarId let type ← whnf decl.type match type.heq? with | none => pure (fvarId, mvarId) | some (α, a, β, b) => if (← isDefEq α β) then let pr ← mkEqOfHEq (mkFVar fvarId) let eq ← mkEq a b let mvarId ← assert mvarId decl.userName eq pr let mvarId ← clear mvarId fvarId let (fvarId, mvarId) ← intro1P mvarId pure (fvarId, mvarId) else pure (fvarId, mvarId) def injectionIntro : Nat → MVarId → Array FVarId → List Name → MetaM InjectionResult | 0, mvarId, fvarIds, remainingNames => pure $ InjectionResult.subgoal mvarId fvarIds remainingNames | n+1, mvarId, fvarIds, name::remainingNames => do let (fvarId, mvarId) ← intro mvarId name let (fvarId, mvarId) ← heqToEq mvarId fvarId injectionIntro n mvarId (fvarIds.push fvarId) remainingNames | n+1, mvarId, fvarIds, [] => do let (fvarId, mvarId) ← intro1 mvarId let (fvarId, mvarId) ← heqToEq mvarId fvarId injectionIntro n mvarId (fvarIds.push fvarId) [] def injection (mvarId : MVarId) (fvarId : FVarId) (newNames : List Name := []) (useUnusedNames : Bool := true) : MetaM InjectionResult := do match (← injectionCore mvarId fvarId) with | InjectionResultCore.solved => pure InjectionResult.solved | InjectionResultCore.subgoal mvarId numEqs => trace[Meta.debug]! "before injectionIntro\n{MessageData.ofGoal mvarId}" injectionIntro numEqs mvarId #[] newNames end Lean.Meta
c1f891e5b78c32cad16518b7e7e1e3f1fd438711
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/category/traversable/equiv.lean
1058c0332350e198973eb803655c6448e7c846c6
[ "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
4,789
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon Transferring `traversable` instances using isomorphisms. -/ import data.equiv.basic category.traversable.lemmas universes u namespace equiv section functor parameters {t t' : Type u → Type u} parameters (eqv : Π α, t α ≃ t' α) variables [functor t] open functor protected def map {α β : Type u} (f : α → β) (x : t' α) : t' β := eqv β $ map f ((eqv α).symm x) protected def functor : functor t' := { map := @equiv.map _ } variables [is_lawful_functor t] protected lemma id_map {α : Type u} (x : t' α) : equiv.map id x = x := by simp [equiv.map, id_map] protected lemma comp_map {α β γ : Type u} (g : α → β) (h : β → γ) (x : t' α) : equiv.map (h ∘ g) x = equiv.map h (equiv.map g x) := by simp [equiv.map]; apply comp_map protected def is_lawful_functor : @is_lawful_functor _ equiv.functor := { id_map := @equiv.id_map _ _, comp_map := @equiv.comp_map _ _ } protected def is_lawful_functor' [F : _root_.functor t'] (h₀ : ∀ {α β} (f : α → β), _root_.functor.map f = equiv.map f) (h₁ : ∀ {α β} (f : β), _root_.functor.map_const f = (equiv.map ∘ function.const α) f) : _root_.is_lawful_functor t' := begin have : F = equiv.functor, { unfreezeI, cases F, dsimp [equiv.functor], congr; ext; [rw ← h₀, rw ← h₁] }, constructor; intros; haveI F' := equiv.is_lawful_functor, { simp, intros, ext, rw [h₁], rw ← this at F', have k := @map_const_eq t' _ _ α β, rw this at ⊢ k, rw ← k, refl }, { rw [h₀], rw ← this at F', have k := id_map x, rw this at k, apply k }, { rw [h₀], rw ← this at F', have k := comp_map g h x, revert k, rw this, exact id }, end end functor section traversable parameters {t t' : Type u → Type u} parameters (eqv : Π α, t α ≃ t' α) variables [traversable t] variables {m : Type u → Type u} [applicative m] variables {α β : Type u} protected def traverse (f : α → m β) (x : t' α) : m (t' β) := eqv β <$> traverse f ((eqv α).symm x) protected def traversable : traversable t' := { to_functor := equiv.functor eqv, traverse := @equiv.traverse _ } end traversable section equiv parameters {t t' : Type u → Type u} parameters (eqv : Π α, t α ≃ t' α) variables [traversable t] [is_lawful_traversable t] variables {F G : Type u → Type u} [applicative F] [applicative G] variables [is_lawful_applicative F] [is_lawful_applicative G] variables (η : applicative_transformation F G) variables {α β γ : Type u} open is_lawful_traversable functor protected lemma id_traverse (x : t' α) : equiv.traverse eqv id.mk x = x := by simp! [equiv.traverse,id_bind,id_traverse,functor.map] with functor_norm protected lemma traverse_eq_map_id (f : α → β) (x : t' α) : equiv.traverse eqv (id.mk ∘ f) x = id.mk (equiv.map eqv f x) := by simp [equiv.traverse, traverse_eq_map_id] with functor_norm; refl protected lemma comp_traverse (f : β → F γ) (g : α → G β) (x : t' α) : equiv.traverse eqv (comp.mk ∘ functor.map f ∘ g) x = comp.mk (equiv.traverse eqv f <$> equiv.traverse eqv g x) := by simp [equiv.traverse,comp_traverse] with functor_norm; congr; ext; simp protected lemma naturality (f : α → F β) (x : t' α) : η (equiv.traverse eqv f x) = equiv.traverse eqv (@η _ ∘ f) x := by simp only [equiv.traverse] with functor_norm protected def is_lawful_traversable : @is_lawful_traversable t' (equiv.traversable eqv) := { to_is_lawful_functor := @equiv.is_lawful_functor _ _ eqv _ _, id_traverse := @equiv.id_traverse _ _, comp_traverse := @equiv.comp_traverse _ _, traverse_eq_map_id := @equiv.traverse_eq_map_id _ _, naturality := @equiv.naturality _ _ } protected def is_lawful_traversable' [_i : traversable t'] (h₀ : ∀ {α β} (f : α → β), map f = equiv.map eqv f) (h₁ : ∀ {α β} (f : β), map_const f = (equiv.map eqv ∘ function.const α) f) (h₂ : ∀ {F : Type u → Type u} [applicative F] [is_lawful_applicative F] {α β} (f : α → F β), traverse f = equiv.traverse eqv f) : _root_.is_lawful_traversable t' := begin -- we can't use the same approach as for `is_lawful_functor'` because -- h₂ needs a `is_lawful_applicative` assumption refine {to_is_lawful_functor := equiv.is_lawful_functor' eqv @h₀ @h₁, ..}; intros; resetI, { rw [h₂, equiv.id_traverse], apply_instance }, { rw [h₂, equiv.comp_traverse f g x, h₂], congr, rw [h₂], all_goals { apply_instance } }, { rw [h₂, equiv.traverse_eq_map_id, h₀]; apply_instance }, { rw [h₂, equiv.naturality, h₂]; apply_instance } end end equiv end equiv
cd7b761d01ca2d7e77fc0026288ed69da916a656
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/sum/instances_auto.lean
0c9123ae768bca34005cc179056259ccf4935996
[]
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
1,030
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 -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.mk_dec_eq_instance universes u v namespace Mathlib protected instance sum.decidable_eq {α : Type u} {β : Type v} [DecidableEq α] [DecidableEq β] : DecidableEq (α ⊕ β) := id fun (_v : α ⊕ β) => sum.cases_on _v (fun (val : α) (w : α ⊕ β) => sum.cases_on w (fun (w : α) => decidable.by_cases (fun (ᾰ : val = w) => Eq._oldrec (is_true sorry) ᾰ) fun (ᾰ : ¬val = w) => isFalse sorry) fun (w : β) => isFalse sorry) fun (val : β) (w : α ⊕ β) => sum.cases_on w (fun (w : α) => isFalse sorry) fun (w : β) => decidable.by_cases (fun (ᾰ : val = w) => Eq._oldrec (is_true sorry) ᾰ) fun (ᾰ : ¬val = w) => isFalse sorry end Mathlib
70965ebd25086049a81aaf90617de544e736d692
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/normed/group/SemiNormedGroup/completion.lean
a6d79cf48c9140ba8400c1a9a3851aa6755b612c
[ "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
4,479
lean
/- Copyright (c) 2021 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca, Johan Commelin -/ import analysis.normed.group.SemiNormedGroup import category_theory.preadditive.additive_functor import analysis.normed.group.hom_completion /-! # Completions of normed groups This file contains an API for completions of seminormed groups (basic facts about objects and morphisms). ## Main definitions - `SemiNormedGroup.Completion : SemiNormedGroup ⥤ SemiNormedGroup` : the completion of a seminormed group (defined as a functor on `SemiNormedGroup` to itself). - `SemiNormedGroup.Completion.lift (f : V ⟶ W) : (Completion.obj V ⟶ W)` : a normed group hom from `V` to complete `W` extends ("lifts") to a seminormed group hom from the completion of `V` to `W`. ## Projects 1. Construct the category of complete seminormed groups, say `CompleteSemiNormedGroup` and promote the `Completion` functor below to a functor landing in this category. 2. Prove that the functor `Completion : SemiNormedGroup ⥤ CompleteSemiNormedGroup` is left adjoint to the forgetful functor. -/ noncomputable theory universe u open uniform_space mul_opposite category_theory normed_group_hom namespace SemiNormedGroup /-- The completion of a seminormed group, as an endofunctor on `SemiNormedGroup`. -/ @[simps] def Completion : SemiNormedGroup.{u} ⥤ SemiNormedGroup.{u} := { obj := λ V, SemiNormedGroup.of (completion V), map := λ V W f, f.completion, map_id' := λ V, completion_id, map_comp' := λ U V W f g, (completion_comp f g).symm } instance Completion_complete_space {V : SemiNormedGroup} : complete_space (Completion.obj V) := completion.complete_space _ /-- The canonical morphism from a seminormed group `V` to its completion. -/ @[simps] def Completion.incl {V : SemiNormedGroup} : V ⟶ Completion.obj V := { to_fun := λ v, (v : completion V), map_add' := completion.coe_add, bound' := ⟨1, λ v, by simp⟩ } lemma Completion.norm_incl_eq {V : SemiNormedGroup} {v : V} : ∥Completion.incl v∥ = ∥v∥ := by simp lemma Completion.map_norm_noninc {V W : SemiNormedGroup} {f : V ⟶ W} (hf : f.norm_noninc) : (Completion.map f).norm_noninc := normed_group_hom.norm_noninc.norm_noninc_iff_norm_le_one.2 $ (normed_group_hom.norm_completion f).le.trans $ normed_group_hom.norm_noninc.norm_noninc_iff_norm_le_one.1 hf /-- Given a normed group hom `V ⟶ W`, this defines the associated morphism from the completion of `V` to the completion of `W`. The difference from the definition obtained from the functoriality of completion is in that the map sending a morphism `f` to the associated morphism of completions is itself additive. -/ def Completion.map_hom (V W : SemiNormedGroup.{u}) : (V ⟶ W) →+ (Completion.obj V ⟶ Completion.obj W) := add_monoid_hom.mk' (category_theory.functor.map Completion) $ λ f g, f.completion_add g @[simp] lemma Completion.map_zero (V W : SemiNormedGroup) : Completion.map (0 : V ⟶ W) = 0 := (Completion.map_hom V W).map_zero instance : preadditive SemiNormedGroup.{u} := { hom_group := λ P Q, infer_instance, add_comp' := by { intros, ext, simp only [normed_group_hom.add_apply, category_theory.comp_apply, map_add] }, comp_add' := by { intros, ext, simp only [normed_group_hom.add_apply, category_theory.comp_apply, map_add] } } instance : functor.additive Completion := { map_add' := λ X Y, (Completion.map_hom _ _).map_add } /-- Given a normed group hom `f : V → W` with `W` complete, this provides a lift of `f` to the completion of `V`. The lemmas `lift_unique` and `lift_comp_incl` provide the api for the universal property of the completion. -/ def Completion.lift {V W : SemiNormedGroup} [complete_space W] [separated_space W] (f : V ⟶ W) : Completion.obj V ⟶ W := { to_fun := f.extension, map_add' := f.extension.to_add_monoid_hom.map_add', bound' := f.extension.bound' } lemma Completion.lift_comp_incl {V W : SemiNormedGroup} [complete_space W] [separated_space W] (f : V ⟶ W) : Completion.incl ≫ (Completion.lift f) = f := by { ext, apply normed_group_hom.extension_coe } lemma Completion.lift_unique {V W : SemiNormedGroup} [complete_space W] [separated_space W] (f : V ⟶ W) (g : Completion.obj V ⟶ W) : Completion.incl ≫ g = f → g = Completion.lift f := λ h, (normed_group_hom.extension_unique _ (λ v, ((ext_iff.1 h) v).symm)).symm end SemiNormedGroup
3ed8d6ea7b8362f720f61fb2426e7da9e162e8fb
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/module/pi.lean
1eaf724c9c0eb04d26072c56ba606570699bcb77
[ "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
9,679
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot -/ import algebra.module.basic import algebra.regular.smul import algebra.ring.pi /-! # Pi instances for module and multiplicative actions This file defines instances for module, mul_action and related structures on Pi Types -/ universes u v w variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) namespace pi @[to_additive pi.has_vadd] instance has_scalar {α : Type*} [Π i, has_scalar α $ f i] : has_scalar α (Π i : I, f i) := ⟨λ s x, λ i, s • (x i)⟩ @[to_additive] lemma smul_def {α : Type*} [Π i, has_scalar α $ f i] (s : α) : s • x = λ i, s • x i := rfl @[simp, to_additive] lemma smul_apply {α : Type*} [Π i, has_scalar α $ f i] (s : α) : (s • x) i = s • x i := rfl @[to_additive pi.has_vadd'] instance has_scalar' {g : I → Type*} [Π i, has_scalar (f i) (g i)] : has_scalar (Π i, f i) (Π i : I, g i) := ⟨λ s x, λ i, (s i) • (x i)⟩ @[simp, to_additive] lemma smul_apply' {g : I → Type*} [∀ i, has_scalar (f i) (g i)] (s : Π i, f i) (x : Π i, g i) : (s • x) i = s i • x i := rfl lemma _root_.is_smul_regular.pi {α : Type*} [Π i, has_scalar α $ f i] {k : α} (hk : Π i, is_smul_regular (f i) k) : is_smul_regular (Π i, f i) k := λ _ _ h, funext $ λ i, hk i (congr_fun h i : _) instance is_scalar_tower {α β : Type*} [has_scalar α β] [Π i, has_scalar β $ f i] [Π i, has_scalar α $ f i] [Π i, is_scalar_tower α β (f i)] : is_scalar_tower α β (Π i : I, f i) := ⟨λ x y z, funext $ λ i, smul_assoc x y (z i)⟩ instance is_scalar_tower' {g : I → Type*} {α : Type*} [Π i, has_scalar α $ f i] [Π i, has_scalar (f i) (g i)] [Π i, has_scalar α $ g i] [Π i, is_scalar_tower α (f i) (g i)] : is_scalar_tower α (Π i : I, f i) (Π i : I, g i) := ⟨λ x y z, funext $ λ i, smul_assoc x (y i) (z i)⟩ instance is_scalar_tower'' {g : I → Type*} {h : I → Type*} [Π i, has_scalar (f i) (g i)] [Π i, has_scalar (g i) (h i)] [Π i, has_scalar (f i) (h i)] [Π i, is_scalar_tower (f i) (g i) (h i)] : is_scalar_tower (Π i, f i) (Π i, g i) (Π i, h i) := ⟨λ x y z, funext $ λ i, smul_assoc (x i) (y i) (z i)⟩ @[to_additive] instance smul_comm_class {α β : Type*} [Π i, has_scalar α $ f i] [Π i, has_scalar β $ f i] [∀ i, smul_comm_class α β (f i)] : smul_comm_class α β (Π i : I, f i) := ⟨λ x y z, funext $ λ i, smul_comm x y (z i)⟩ @[to_additive] instance smul_comm_class' {g : I → Type*} {α : Type*} [Π i, has_scalar α $ g i] [Π i, has_scalar (f i) (g i)] [∀ i, smul_comm_class α (f i) (g i)] : smul_comm_class α (Π i : I, f i) (Π i : I, g i) := ⟨λ x y z, funext $ λ i, smul_comm x (y i) (z i)⟩ @[to_additive] instance smul_comm_class'' {g : I → Type*} {h : I → Type*} [Π i, has_scalar (g i) (h i)] [Π i, has_scalar (f i) (h i)] [∀ i, smul_comm_class (f i) (g i) (h i)] : smul_comm_class (Π i, f i) (Π i, g i) (Π i, h i) := ⟨λ x y z, funext $ λ i, smul_comm (x i) (y i) (z i)⟩ /-- If `f i` has a faithful scalar action for a given `i`, then so does `Π i, f i`. This is not an instance as `i` cannot be inferred. -/ @[to_additive pi.has_faithful_vadd_at] lemma has_faithful_scalar_at {α : Type*} [Π i, has_scalar α $ f i] [Π i, nonempty (f i)] (i : I) [has_faithful_scalar α (f i)] : has_faithful_scalar α (Π i, f i) := ⟨λ x y h, eq_of_smul_eq_smul $ λ a : f i, begin classical, have := congr_fun (h $ function.update (λ j, classical.choice (‹Π i, nonempty (f i)› j)) i a) i, simpa using this, end⟩ @[to_additive pi.has_faithful_vadd] instance has_faithful_scalar {α : Type*} [nonempty I] [Π i, has_scalar α $ f i] [Π i, nonempty (f i)] [Π i, has_faithful_scalar α (f i)] : has_faithful_scalar α (Π i, f i) := let ⟨i⟩ := ‹nonempty I› in has_faithful_scalar_at i instance smul_with_zero (α) [has_zero α] [Π i, has_zero (f i)] [Π i, smul_with_zero α (f i)] : smul_with_zero α (Π i, f i) := { smul_zero := λ _, funext $ λ _, smul_zero' (f _) _, zero_smul := λ _, funext $ λ _, zero_smul _ _, ..pi.has_scalar } instance smul_with_zero' {g : I → Type*} [Π i, has_zero (g i)] [Π i, has_zero (f i)] [Π i, smul_with_zero (g i) (f i)] : smul_with_zero (Π i, g i) (Π i, f i) := { smul_zero := λ _, funext $ λ _, smul_zero' (f _) _, zero_smul := λ _, funext $ λ _, zero_smul _ _, ..pi.has_scalar' } @[to_additive] instance mul_action (α) {m : monoid α} [Π i, mul_action α $ f i] : @mul_action α (Π i : I, f i) m := { smul := (•), mul_smul := λ r s f, funext $ λ i, mul_smul _ _ _, one_smul := λ f, funext $ λ i, one_smul α _ } @[to_additive] instance mul_action' {g : I → Type*} {m : Π i, monoid (f i)} [Π i, mul_action (f i) (g i)] : @mul_action (Π i, f i) (Π i : I, g i) (@pi.monoid I f m) := { smul := (•), mul_smul := λ r s f, funext $ λ i, mul_smul _ _ _, one_smul := λ f, funext $ λ i, one_smul _ _ } instance mul_action_with_zero (α) [monoid_with_zero α] [Π i, has_zero (f i)] [Π i, mul_action_with_zero α (f i)] : mul_action_with_zero α (Π i, f i) := { ..pi.mul_action _, ..pi.smul_with_zero _ } instance mul_action_with_zero' {g : I → Type*} [Π i, monoid_with_zero (g i)] [Π i, has_zero (f i)] [Π i, mul_action_with_zero (g i) (f i)] : mul_action_with_zero (Π i, g i) (Π i, f i) := { ..pi.mul_action', ..pi.smul_with_zero' } instance distrib_mul_action (α) {m : monoid α} {n : ∀ i, add_monoid $ f i} [∀ i, distrib_mul_action α $ f i] : @distrib_mul_action α (Π i : I, f i) m (@pi.add_monoid I f n) := { smul_zero := λ c, funext $ λ i, smul_zero _, smul_add := λ c f g, funext $ λ i, smul_add _ _ _, ..pi.mul_action _ } instance distrib_mul_action' {g : I → Type*} {m : Π i, monoid (f i)} {n : Π i, add_monoid $ g i} [Π i, distrib_mul_action (f i) (g i)] : @distrib_mul_action (Π i, f i) (Π i : I, g i) (@pi.monoid I f m) (@pi.add_monoid I g n) := { smul_add := by { intros, ext x, apply smul_add }, smul_zero := by { intros, ext x, apply smul_zero } } lemma single_smul {α} [monoid α] [Π i, add_monoid $ f i] [Π i, distrib_mul_action α $ f i] [decidable_eq I] (i : I) (r : α) (x : f i) : single i (r • x) = r • single i x := single_op (λ i : I, ((•) r : f i → f i)) (λ j, smul_zero _) _ _ /-- A version of `pi.single_smul` for non-dependent functions. It is useful in cases Lean fails to apply `pi.single_smul`. -/ lemma single_smul' {α β} [monoid α] [add_monoid β] [distrib_mul_action α β] [decidable_eq I] (i : I) (r : α) (x : β) : single i (r • x) = r • single i x := single_smul i r x lemma single_smul₀ {g : I → Type*} [Π i, monoid_with_zero (f i)] [Π i, add_monoid (g i)] [Π i, distrib_mul_action (f i) (g i)] [decidable_eq I] (i : I) (r : f i) (x : g i) : single i (r • x) = single i r • single i x := single_op₂ (λ i : I, ((•) : f i → g i → g i)) (λ j, smul_zero _) _ _ _ instance mul_distrib_mul_action (α) {m : monoid α} {n : Π i, monoid $ f i} [Π i, mul_distrib_mul_action α $ f i] : @mul_distrib_mul_action α (Π i : I, f i) m (@pi.monoid I f n) := { smul_one := λ c, funext $ λ i, smul_one _, smul_mul := λ c f g, funext $ λ i, smul_mul' _ _ _, ..pi.mul_action _ } instance mul_distrib_mul_action' {g : I → Type*} {m : Π i, monoid (f i)} {n : Π i, monoid $ g i} [Π i, mul_distrib_mul_action (f i) (g i)] : @mul_distrib_mul_action (Π i, f i) (Π i : I, g i) (@pi.monoid I f m) (@pi.monoid I g n) := { smul_mul := by { intros, ext x, apply smul_mul' }, smul_one := by { intros, ext x, apply smul_one } } variables (I f) instance module (α) {r : semiring α} {m : ∀ i, add_comm_monoid $ f i} [∀ i, module α $ f i] : @module α (Π i : I, f i) r (@pi.add_comm_monoid I f m) := { add_smul := λ c f g, funext $ λ i, add_smul _ _ _, zero_smul := λ f, funext $ λ i, zero_smul α _, ..pi.distrib_mul_action _ } variables {I f} instance module' {g : I → Type*} {r : Π i, semiring (f i)} {m : Π i, add_comm_monoid (g i)} [Π i, module (f i) (g i)] : module (Π i, f i) (Π i, g i) := { add_smul := by { intros, ext1, apply add_smul }, zero_smul := by { intros, ext1, apply zero_smul } } instance (α) {r : semiring α} {m : Π i, add_comm_monoid $ f i} [Π i, module α $ f i] [∀ i, no_zero_smul_divisors α $ f i] : no_zero_smul_divisors α (Π i : I, f i) := ⟨λ c x h, or_iff_not_imp_left.mpr (λ hc, funext (λ i, (smul_eq_zero.mp (congr_fun h i)).resolve_left hc))⟩ end pi namespace function @[to_additive] lemma update_smul {α : Type*} [Π i, has_scalar α (f i)] [decidable_eq I] (c : α) (f₁ : Π i, f i) (i : I) (x₁ : f i) : update (c • f₁) i (c • x₁) = c • update f₁ i x₁ := funext $ λ j, (apply_update (λ i, (•) c) f₁ i x₁ j).symm end function namespace set @[to_additive] lemma piecewise_smul {α : Type*} [Π i, has_scalar α (f i)] (s : set I) [Π i, decidable (i ∈ s)] (c : α) (f₁ g₁ : Π i, f i) : s.piecewise (c • f₁) (c • g₁) = c • s.piecewise f₁ g₁ := s.piecewise_op _ _ (λ _, (•) c) end set section extend @[to_additive] lemma function.extend_smul {R α β γ : Type*} [has_scalar R γ] (r : R) (f : α → β) (g : α → γ) (e : β → γ) : function.extend f (r • g) (r • e) = r • function.extend f g e := funext $ λ _, by convert (apply_dite ((•) r) _ _ _).symm end extend
526c44c5f59d4a6855c82d04804cc8220f972291
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/tests/lean/run/633.lean
d293d660df80ecba63f2a192a64b3c8c8b4aadd3
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
1,380
lean
abbrev semantics (α:Type) := StateM (List Nat) α inductive expression : Nat → Type | const : (n : Nat) → expression n def uext {w:Nat} (x: expression w) (o:Nat) : expression w := expression.const w def eval {n : Nat} (v:expression n) : semantics (expression n) := pure (expression.const n) def set_overflow {w : Nat} (e : expression w) : semantics Unit := pure () structure instruction := (mnemonic:String) (patterns:List Nat) def definst (mnem:String) (body: expression 8 -> semantics Unit) : instruction := { mnemonic := mnem , patterns := ((body (expression.const 8)).run []).snd.reverse } def mul : instruction := do -- this is a "pure" do block (as in it is the Id monad) definst "mul" $ fun (src : expression 8) => let action : semantics Unit := do -- this is not "pure" do block let tmp <- eval $ uext src 16 set_overflow $ tmp action def mul' : instruction := do -- this is a "pure" do block (as in it is the Id monad) definst "mul" $ fun (src : expression 8) => let rec action : semantics Unit := do -- this is not "pure" do block let tmp <- eval $ uext src 16 set_overflow $ tmp action def mul'' : instruction := do -- this is a "pure" do block (as in it is the Id monad) definst "mul" $ fun (src : expression 8) => let action : semantics (expression 8) := return (<- eval $ uext src 16) pure ()
326d2d3e98f7a52b36b880e7c4c8f7ee0dba28f9
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/lean/compiler/ir/borrow.lean
1b7f2da77295388edc5d7c4a3d0fbdb6306adefd
[ "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
10,846
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.lean.compiler.exportattr import init.lean.compiler.ir.compilerm import init.lean.compiler.ir.normids namespace Lean namespace IR namespace Borrow /- We perform borrow inference in a block of mutually recursive functions. Join points are viewed as local functions, and are identified using their local id + the name of the surrounding function. We keep a mapping from function and joint points to parameters (`Array Param`). Recall that `Param` contains the field `borrow`. The type `Key` is the the key of this map. -/ inductive Key | decl (name : FunId) | jp (name : FunId) (jpid : JoinPointId) namespace Key def beq : Key → Key → Bool | (decl n₁) (decl n₂) := n₁ == n₂ | (jp n₁ id₁) (jp n₂ id₂) := n₁ == n₂ && id₁ == id₂ | _ _ := false instance : HasBeq Key := ⟨beq⟩ def getHash : Key → USize | (decl n) := hash n | (jp n id) := mixHash (hash n) (hash id) instance : Hashable Key := ⟨getHash⟩ end Key abbrev ParamMap := HashMap Key (Array Param) def ParamMap.fmt (map : ParamMap) : Format := let fmts := map.fold (fun fmt k ps => let k := match k with | Key.decl n => format n | Key.jp n id => format n ++ ":" ++ format id; fmt ++ Format.line ++ k ++ " -> " ++ formatParams ps) Format.nil; "{" ++ (Format.nest 1 fmts) ++ "}" instance : HasFormat ParamMap := ⟨ParamMap.fmt⟩ instance : HasToString ParamMap := ⟨fun m => Format.pretty (format m)⟩ namespace InitParamMap /- Mark parameters that take a reference as borrow -/ def initBorrow (ps : Array Param) : Array Param := ps.map $ fun p => { borrow := p.ty.isObj, .. p } /- We do perform borrow inference for constants marked as `export`. Reason: we current write wrappers in C++ for using exported functions. These wrappers use smart pointers such as `object_ref`. When writing a new wrapper we need to know whether an argument is a borrow inference or not. We can revise this decision when we implement code for generating the wrappers automatically. -/ def initBorrowIfNotExported (exported : Bool) (ps : Array Param) : Array Param := if exported then ps else initBorrow ps partial def visitFnBody (fnid : FunId) : FnBody → State ParamMap Unit | (FnBody.jdecl j xs v b) := do modify $ fun m => m.insert (Key.jp fnid j) (initBorrow xs); visitFnBody v; visitFnBody b | e := unless (e.isTerminal) $ do let (instr, b) := e.split; visitFnBody b def visitDecls (env : Environment) (decls : Array Decl) : State ParamMap Unit := decls.mfor $ fun decl => match decl with | Decl.fdecl f xs _ b => do let exported := isExport env f; modify $ fun m => m.insert (Key.decl f) (initBorrowIfNotExported exported xs); visitFnBody f b | _ => pure () end InitParamMap def mkInitParamMap (env : Environment) (decls : Array Decl) : ParamMap := (InitParamMap.visitDecls env decls *> get).run' {} /- Apply the inferred borrow annotations stored at `ParamMap` to a block of mutually recursive functions. -/ namespace ApplyParamMap partial def visitFnBody : FnBody → FunId → ParamMap → FnBody | (FnBody.jdecl j xs v b) fnid map := let v := visitFnBody v fnid map; let b := visitFnBody b fnid map; match map.find (Key.jp fnid j) with | some ys => FnBody.jdecl j ys v b | none => FnBody.jdecl j xs v b | e fnid map := if e.isTerminal then e else let (instr, b) := e.split; let b := visitFnBody b fnid map; instr.setBody b def visitDecls (decls : Array Decl) (map : ParamMap) : Array Decl := decls.map $ fun decl => match decl with | Decl.fdecl f xs ty b => let b := visitFnBody b f map; match map.find (Key.decl f) with | some xs => Decl.fdecl f xs ty b | none => Decl.fdecl f xs ty b | other => other end ApplyParamMap def applyParamMap (decls : Array Decl) (map : ParamMap) : Array Decl := -- dbgTrace ("applyParamMap " ++ toString map) $ fun _ => ApplyParamMap.visitDecls decls map structure BorrowInfCtx := (env : Environment) (currFn : FunId := default _) -- Function being analyzed. (paramSet : IndexSet := {}) -- Set of all function parameters in scope. This is used to implement the heuristic at `ownArgsUsingParams` structure BorrowInfState := /- `map` is a mapping storing the inferred borrow annotations for all functions (and joint points) in a mutually recursive declaration. -/ (map : ParamMap) /- Set of variables that must be `owned`. -/ (owned : IndexSet := {}) (modifiedOwned : Bool := false) (modifiedParamMap : Bool := false) abbrev M := ReaderT BorrowInfCtx (State BorrowInfState) def markModifiedParamMap : M Unit := modify $ fun s => { modifiedParamMap := true, .. s } def ownVar (x : VarId) : M Unit := -- dbgTrace ("ownVar " ++ toString x) $ fun _ => modify $ fun s => if s.owned.contains x.idx then s else { owned := s.owned.insert x.idx, modifiedOwned := true, .. s } def ownArg (x : Arg) : M Unit := match x with | (Arg.var x) => ownVar x | _ => pure () def ownArgs (xs : Array Arg) : M Unit := xs.mfor ownArg def isOwned (x : VarId) : M Bool := do s ← get; pure $ s.owned.contains x.idx /- Updates `map[k]` using the current set of `owned` variables. -/ def updateParamMap (k : Key) : M Unit := do s ← get; match s.map.find k with | some ps => do ps ← ps.mmap $ fun (p : Param) => if p.borrow && s.owned.contains p.x.idx then do markModifiedParamMap; pure { borrow := false, .. p } else pure p; modify $ fun s => { map := s.map.insert k ps, .. s } | none => pure () def getParamInfo (k : Key) : M (Array Param) := do s ← get; match s.map.find k with | some ps => pure ps | none => match k with | (Key.decl fn) => do ctx ← read; match findEnvDecl ctx.env fn with | some decl => pure decl.params | none => pure Array.empty -- unreachable if well-formed input | _ => pure Array.empty -- unreachable if well-formed input /- For each ps[i], if ps[i] is owned, then mark xs[i] as owned. -/ def ownArgsUsingParams (xs : Array Arg) (ps : Array Param) : M Unit := xs.size.mfor $ fun i => do let x := xs.get i; let p := ps.get i; unless p.borrow $ ownArg x /- For each xs[i], if xs[i] is owned, then mark ps[i] as owned. We use this action to preserve tail calls. That is, if we have a tail call `f xs`, if the i-th parameter is borrowed, but `xs[i]` is owned we would have to insert a `dec xs[i]` after `f xs` and consequently "break" the tail call. -/ def ownParamsUsingArgs (xs : Array Arg) (ps : Array Param) : M Unit := xs.size.mfor $ fun i => do let x := xs.get i; let p := ps.get i; match x with | Arg.var x => mwhen (isOwned x) $ ownVar p.x | _ => pure () /- Mark `xs[i]` as owned if it is one of the parameters `ps`. We use this action to mark function parameters that are being "packed" inside constructors. This is a heuristic, and is not related with the effectiveness of the reset/reuse optimization. It is useful for code such as ``` def f (x y : obj) := let z := ctor_1 x y; ret z ``` -/ def ownArgsIfParam (xs : Array Arg) : M Unit := do ctx ← read; xs.mfor $ fun x => match x with | Arg.var x => when (ctx.paramSet.contains x.idx) $ ownVar x | _ => pure () def collectExpr (z : VarId) : Expr → M Unit | (Expr.reset _ x) := ownVar z *> ownVar x | (Expr.reuse x _ _ ys) := ownVar z *> ownVar x *> ownArgsIfParam ys | (Expr.ctor _ xs) := ownVar z *> ownArgsIfParam xs | (Expr.proj _ x) := mwhen (isOwned z) $ ownVar x | (Expr.fap g xs) := do ps ← getParamInfo (Key.decl g); -- dbgTrace ("collectExpr: " ++ toString g ++ " " ++ toString (formatParams ps)) $ fun _ => ownVar z *> ownArgsUsingParams xs ps | (Expr.ap x ys) := ownVar z *> ownVar x *> ownArgs ys | (Expr.pap _ xs) := ownVar z *> ownArgs xs | other := pure () def preserveTailCall (x : VarId) (v : Expr) (b : FnBody) : M Unit := do ctx ← read; match v, b with | (Expr.fap g ys), (FnBody.ret (Arg.var z)) => when (ctx.currFn == g && x == z) $ do -- dbgTrace ("preserveTailCall " ++ toString b) $ fun _ => do ps ← getParamInfo (Key.decl g); ownParamsUsingArgs ys ps | _, _ => pure () def updateParamSet (ctx : BorrowInfCtx) (ps : Array Param) : BorrowInfCtx := { paramSet := ps.foldl (fun s p => s.insert p.x.idx) ctx.paramSet, .. ctx } partial def collectFnBody : FnBody → M Unit | (FnBody.jdecl j ys v b) := do adaptReader (fun ctx => updateParamSet ctx ys) (collectFnBody v); ctx ← read; updateParamMap (Key.jp ctx.currFn j); collectFnBody b | (FnBody.vdecl x _ v b) := collectFnBody b *> collectExpr x v *> preserveTailCall x v b | (FnBody.jmp j ys) := do ctx ← read; ps ← getParamInfo (Key.jp ctx.currFn j); ownArgsUsingParams ys ps; -- for making sure the join point can reuse ownParamsUsingArgs ys ps -- for making sure the tail call is preserved | (FnBody.case _ _ alts) := alts.mfor $ fun alt => collectFnBody alt.body | e := unless (e.isTerminal) $ collectFnBody e.body @[specialize] partial def whileModifingOwnedAux (x : M Unit) : Unit → M Unit | _ := do modify $ fun s => { modifiedOwned := false, .. s }; x; s ← get; if s.modifiedOwned then whileModifingOwnedAux () else pure () /- Keep executing `x` while it modifies ownedSet -/ @[inline] def whileModifingOwned (x : M Unit) : M Unit := whileModifingOwnedAux x () partial def collectDecl : Decl → M Unit | (Decl.fdecl f ys _ b) := adaptReader (fun ctx => let ctx := updateParamSet ctx ys; { currFn := f, .. ctx }) $ do modify $ fun (s : BorrowInfState) => { owned := {}, .. s }; whileModifingOwned (collectFnBody b); updateParamMap (Key.decl f) | _ := pure () @[specialize] partial def whileModifingParamMapAux (x : M Unit) : Unit → M Unit | _ := do modify $ fun s => { modifiedParamMap := false, .. s }; s ← get; -- dbgTrace (toString s.map) $ fun _ => do x; s ← get; if s.modifiedParamMap then whileModifingParamMapAux () else pure () /- Keep executing `x` while it modifies paramMap -/ @[inline] def whileModifingParamMap (x : M Unit) : M Unit := whileModifingParamMapAux x () def collectDecls (decls : Array Decl) : M ParamMap := do whileModifingParamMap (decls.mfor collectDecl); s ← get; pure s.map def infer (env : Environment) (decls : Array Decl) : ParamMap := (collectDecls decls { env := env }).run' { map := mkInitParamMap env decls } end Borrow def inferBorrow (decls : Array Decl) : CompilerM (Array Decl) := do env ← getEnv; let decls := decls.map Decl.normalizeIds; let paramMap := Borrow.infer env decls; pure (Borrow.applyParamMap decls paramMap) end IR end Lean
88afb19989f029f543e59319e90239f9d0e87e10
a5271082abc327bbe26fc4acdaa885da9582cefa
/src/exp.lean
759d7f0208a6d371ede90351a131b436e6cb2aaa
[ "Apache-2.0" ]
permissive
avigad/embed
9ee7d104609eeded173ca1e6e7a1925897b1652a
0e3612028d4039d29d06239ef03bc50576ca0f8b
refs/heads/master
1,584,548,951,613
1,527,883,346,000
1,527,883,346,000
134,967,973
0
0
null
null
null
null
UTF-8
Lean
false
false
1,052
lean
import .list .misc variable {α : Type } @[derive has_reflect] inductive exp (α : Type) : Type | bvr : nat → exp | fvr : nat → exp | cst : α → exp | lam : exp → exp | app : exp → exp → exp notation `#` n := exp.bvr _ n notation `&` n := exp.fvr _ n def seq (α : Type) : Type := (list (exp α)) × (list (exp α)) def proves (Γ Δ : list (exp α)) : seq α := ⟨Γ,Δ⟩ notation Γ `==>` Δ := proves Γ Δ def fvrs : exp α → list nat | (exp.lam e) := fvrs e | (exp.app e1 e2) := fvrs e1 ++ fvrs e2 | (exp.fvr α k) := [k] | _ := [] def fvrs_list : list (exp α) → list nat | [] := [] | (e::es) := fvrs e ++ fvrs_list es def inst_rec : nat → exp α → exp α → exp α | n t (exp.bvr α m) := if n = m then t else (exp.bvr α m) | n t (exp.lam e) := exp.lam (inst_rec (n+1) t e) | n t (exp.app e1 e2) := exp.app (inst_rec n t e1) (inst_rec n t e2) | _ _ e := e def inst (t e : exp α) : exp α := inst_rec 0 t e instance [decidable_eq α] : decidable_eq (exp α) := by tactic.mk_dec_eq_instance
1c496cc0c8f68dfc62401bd9da0a7bedcbc840d2
5a5e1bb8063d7934afac91f30aa17c715821040b
/lean3SOS/src/lib/eigenvalues.lean
d7d51c258cab15473b04e579fcbaae30c061caae
[]
no_license
ramonfmir/leanSOS
1883392d73710db5c6e291a2abd03a6c5b44a42b
14b50713dc887f6d408b7b2bce1f8af5bb619958
refs/heads/main
1,683,348,826,105
1,622,056,982,000
1,622,056,982,000
341,232,766
1
0
null
null
null
null
UTF-8
Lean
false
false
1,831
lean
import lib.psd float.basic variables {n m : nat} def delta (A : matrix (fin n) (fin n) float) (R : matrix (fin m) (fin n) float) : matrix (fin n) (fin n) float := A - (matrix.mul R.transpose R) open matrix module.End -- prove that RtR is symmetric. lemma RTR_symmetric (R : matrix (fin m) (fin n) float) : symmetric (matrix.mul R.transpose R) := begin intros i j, simp [matrix.mul, dot_product], congr, ext k, exact mul_comm _ _, end -- prove that is psd. lemma RTR_psd (R : matrix (fin m) (fin n) float) : pos_semidef (matrix.mul R.transpose R) (RTR_symmetric R) := begin intros v, rw [dot_product_transpose v _], exact dot_product_self_nonneg _, end -- copy proof of psd -> positive eigens lemma nonneg_eigenvalues_of_psd (M : matrix (fin n) (fin n) float) (h : symmetric M) (hpsd : pos_semidef M h) : nonneg_eigenvalues M h := begin rintros r hre, by_contra hc, rw [has_eigenvalue, submodule.ne_bot_iff] at hre, obtain ⟨x, hre, hxnz⟩ := hre, rw [mem_eigenspace_iff] at hre, replace hpsd := hpsd x, replace hre := congr_arg (λ y, dot_product y x) hre; simp at hre, replace hc := lt_of_not_ge hc, rw [dot_product_comm] at hpsd, suffices hsuff : r * dot_product x x < 0, { rw [←hre] at hsuff, exact ((not_le_of_lt hsuff) (ge_iff_le.1 hpsd)), }, apply mul_neg_of_neg_of_pos hc, rw [hre] at hpsd, exfalso, have hdp := dot_product_self_pos_of_nonzero x hxnz, have hc' := mul_neg_of_pos_of_neg hdp hc, rw [mul_comm] at hc', exact ((not_le_of_gt hc') hpsd), end lemma pos_eigenvalue (R : matrix (fin m) (fin n) float) (a : float) (h : has_eigenvalue (mul_vec_lin (matrix.mul R.transpose R)) a) : a ≥ 0 := (nonneg_eigenvalues_of_psd (matrix.mul R.transpose R) (RTR_symmetric R) (RTR_psd R)) a h -- argue about the difference between eigenvalues (seems hard).
c88936b06934be0fb843ed2a81c791589d78f2e3
4727251e0cd73359b15b664c3170e5d754078599
/src/group_theory/group_action/basic.lean
88f649e9ab8ff641977a7a43160eb32319ae3cba
[ "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
25,673
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.hom.group_action import group_theory.group_action.defs import group_theory.group_action.group import group_theory.quotient_group import data.setoid.basic import data.fintype.card /-! # Basic properties of group actions This file primarily concerns itself with orbits, stabilizers, and other objects defined in terms of actions. Despite this file being called `basic`, low-level helper lemmas for algebraic manipulation of `•` belong elsewhere. ## Main definitions * `mul_action.orbit` * `mul_action.fixed_points` * `mul_action.fixed_by` * `mul_action.stabilizer` -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open_locale big_operators pointwise open function namespace mul_action variables (α) [monoid α] [mul_action α β] /-- The orbit of an element under an action. -/ @[to_additive "The orbit of an element under an action."] def orbit (b : β) := set.range (λ x : α, x • b) variable {α} @[to_additive] lemma mem_orbit_iff {b₁ b₂ : β} : b₂ ∈ orbit α b₁ ↔ ∃ x : α, x • b₁ = b₂ := iff.rfl @[simp, to_additive] lemma mem_orbit (b : β) (x : α) : x • b ∈ orbit α b := ⟨x, rfl⟩ @[simp, to_additive] lemma mem_orbit_self (b : β) : b ∈ orbit α b := ⟨1, by simp [mul_action.one_smul]⟩ @[to_additive] lemma orbit_nonempty (b : β) : set.nonempty (orbit α b) := set.range_nonempty _ @[to_additive] lemma maps_to_smul_orbit (a : α) (b : β) : set.maps_to ((•) a) (orbit α b) (orbit α b) := set.range_subset_iff.2 $ λ a', ⟨a * a', mul_smul _ _ _⟩ @[to_additive] lemma smul_orbit_subset (a : α) (b : β) : a • orbit α b ⊆ orbit α b := (maps_to_smul_orbit a b).image_subset @[to_additive] lemma orbit_smul_subset (a : α) (b : β) : orbit α (a • b) ⊆ orbit α b := set.range_subset_iff.2 $ λ a', mul_smul a' a b ▸ mem_orbit _ _ @[to_additive] instance {b : β} : mul_action α (orbit α b) := { smul := λ a, (maps_to_smul_orbit a b).restrict _ _ _, one_smul := λ a, subtype.ext (one_smul α a), mul_smul := λ a a' b', subtype.ext (mul_smul a a' b') } @[simp, to_additive] lemma orbit.coe_smul {b : β} {a : α} {b' : orbit α b} : ↑(a • b') = a • (b' : β) := rfl variables (α) (β) /-- The set of elements fixed under the whole action. -/ @[to_additive "The set of elements fixed under the whole action."] def fixed_points : set β := {b : β | ∀ x : α, x • b = b} /-- `fixed_by g` is the subfield of elements fixed by `g`. -/ @[to_additive "`fixed_by g` is the subfield of elements fixed by `g`."] def fixed_by (g : α) : set β := { x | g • x = x } @[to_additive] theorem fixed_eq_Inter_fixed_by : fixed_points α β = ⋂ g : α, fixed_by α β g := set.ext $ λ x, ⟨λ hx, set.mem_Inter.2 $ λ g, hx g, λ hx g, by exact (set.mem_Inter.1 hx g : _)⟩ variables {α} (β) @[simp, to_additive] lemma mem_fixed_points {b : β} : b ∈ fixed_points α β ↔ ∀ x : α, x • b = b := iff.rfl @[simp, to_additive] lemma mem_fixed_by {g : α} {b : β} : b ∈ fixed_by α β g ↔ g • b = b := iff.rfl @[to_additive] lemma mem_fixed_points' {b : β} : b ∈ fixed_points α β ↔ (∀ b', b' ∈ orbit α b → b' = b) := ⟨λ h b h₁, let ⟨x, hx⟩ := mem_orbit_iff.1 h₁ in hx ▸ h x, λ h b, h _ (mem_orbit _ _)⟩ variables (α) {β} /-- The stabilizer of a point `b` as a submonoid of `α`. -/ @[to_additive "The stabilizer of a point `b` as an additive submonoid of `α`."] def stabilizer.submonoid (b : β) : submonoid α := { carrier := { a | a • b = b }, one_mem' := one_smul _ b, mul_mem' := λ a a' (ha : a • b = b) (hb : a' • b = b), show (a * a') • b = b, by rw [←smul_smul, hb, ha] } @[simp, to_additive] lemma mem_stabilizer_submonoid_iff {b : β} {a : α} : a ∈ stabilizer.submonoid α b ↔ a • b = b := iff.rfl @[to_additive] lemma orbit_eq_univ [is_pretransitive α β] (x : β) : orbit α x = set.univ := (surjective_smul α x).range_eq variables {α} {β} @[to_additive] lemma mem_fixed_points_iff_card_orbit_eq_one {a : β} [fintype (orbit α a)] : a ∈ fixed_points α β ↔ fintype.card (orbit α a) = 1 := begin rw [fintype.card_eq_one_iff, mem_fixed_points], split, { exact λ h, ⟨⟨a, mem_orbit_self _⟩, λ ⟨b, ⟨x, hx⟩⟩, subtype.eq $ by simp [h x, hx.symm]⟩ }, { assume h x, rcases h with ⟨⟨z, hz⟩, hz₁⟩, calc x • a = z : subtype.mk.inj (hz₁ ⟨x • a, mem_orbit _ _⟩) ... = a : (subtype.mk.inj (hz₁ ⟨a, mem_orbit_self _⟩)).symm } end end mul_action namespace mul_action variable (α) variables [group α] [mul_action α β] /-- The stabilizer of an element under an action, i.e. what sends the element to itself. A subgroup. -/ @[to_additive "The stabilizer of an element under an action, i.e. what sends the element to itself. An additive subgroup."] def stabilizer (b : β) : subgroup α := { inv_mem' := λ a (ha : a • b = b), show a⁻¹ • b = b, by rw [inv_smul_eq_iff, ha] ..stabilizer.submonoid α b } variables {α} {β} @[simp, to_additive] lemma mem_stabilizer_iff {b : β} {a : α} : a ∈ stabilizer α b ↔ a • b = b := iff.rfl @[simp, to_additive] lemma smul_orbit (a : α) (b : β) : a • orbit α b = orbit α b := (smul_orbit_subset a b).antisymm $ calc orbit α b = a • a⁻¹ • orbit α b : (smul_inv_smul _ _).symm ... ⊆ a • orbit α b : set.image_subset _ (smul_orbit_subset _ _) @[simp, to_additive] lemma orbit_smul (a : α) (b : β) : orbit α (a • b) = orbit α b := (orbit_smul_subset a b).antisymm $ calc orbit α b = orbit α (a⁻¹ • a • b) : by rw inv_smul_smul ... ⊆ orbit α (a • b) : orbit_smul_subset _ _ /-- The action of a group on an orbit is transitive. -/ @[to_additive "The action of an additive group on an orbit is transitive."] instance (x : β) : is_pretransitive α (orbit α x) := ⟨by { rintro ⟨_, a, rfl⟩ ⟨_, b, rfl⟩, use b * a⁻¹, ext1, simp [mul_smul] }⟩ @[to_additive] lemma orbit_eq_iff {a b : β} : orbit α a = orbit α b ↔ a ∈ orbit α b:= ⟨λ h, h ▸ mem_orbit_self _, λ ⟨c, hc⟩, hc ▸ orbit_smul _ _⟩ variables (α) {β} @[to_additive] lemma mem_orbit_smul (g : α) (a : β) : a ∈ orbit α (g • a) := by simp only [orbit_smul, mem_orbit_self] @[to_additive] lemma smul_mem_orbit_smul (g h : α) (a : β) : g • a ∈ orbit α (h • a) := by simp only [orbit_smul, mem_orbit] variables (α) (β) /-- The relation 'in the same orbit'. -/ @[to_additive "The relation 'in the same orbit'."] def orbit_rel : setoid β := { r := λ a b, a ∈ orbit α b, iseqv := ⟨mem_orbit_self, λ a b, by simp [orbit_eq_iff.symm, eq_comm], λ a b, by simp [orbit_eq_iff.symm, eq_comm] {contextual := tt}⟩ } local attribute [instance] orbit_rel variables {α} {β} /-- When you take a set `U` in `β`, push it down to the quotient, and pull back, you get the union of the orbit of `U` under `α`. -/ @[to_additive] lemma quotient_preimage_image_eq_union_mul (U : set β) : quotient.mk ⁻¹' (quotient.mk '' U) = ⋃ a : α, ((•) a) '' U := begin set f : β → quotient (mul_action.orbit_rel α β) := quotient.mk, ext, split, { rintros ⟨y , hy, hxy⟩, obtain ⟨a, rfl⟩ := quotient.exact hxy, rw set.mem_Union, exact ⟨a⁻¹, a • x, hy, inv_smul_smul a x⟩ }, { intros hx, rw set.mem_Union at hx, obtain ⟨a, u, hu₁, hu₂⟩ := hx, rw [set.mem_preimage, set.mem_image_iff_bex], refine ⟨a⁻¹ • x, _, by simp only [quotient.eq]; use a⁻¹⟩, rw ← hu₂, convert hu₁, simp only [inv_smul_smul], }, end @[to_additive] lemma image_inter_image_iff (U V : set β) : (quotient.mk '' U) ∩ (quotient.mk '' V) = ∅ ↔ ∀ x ∈ U, ∀ a : α, a • x ∉ V := begin set f : β → quotient (mul_action.orbit_rel α β) := quotient.mk, rw set.eq_empty_iff_forall_not_mem, split, { intros h x x_in_U a a_in_V, refine h (f (a • x)) ⟨⟨x, x_in_U, _⟩, ⟨a • x, a_in_V, rfl⟩⟩, rw quotient.eq, use a⁻¹, simp, }, { rintros h x ⟨⟨y, hy₁, hy₂⟩, ⟨z, hz₁, hz₂⟩⟩, obtain ⟨a, ha⟩ := quotient.exact (hz₂.trans hy₂.symm), apply h y hy₁ a, convert hz₁, }, end variables (α) (β) local notation `Ω` := (quotient $ orbit_rel α β) /-- Decomposition of a type `X` as a disjoint union of its orbits under a group action. This version works with any right inverse to `quotient.mk'` in order to stay computable. In most cases you'll want to use `quotient.out'`, so we provide `mul_action.self_equiv_sigma_orbits` as a special case. -/ @[to_additive "Decomposition of a type `X` as a disjoint union of its orbits under an additive group action. This version works with any right inverse to `quotient.mk'` in order to stay computable. In most cases you'll want to use `quotient.out'`, so we provide `add_action.self_equiv_sigma_orbits` as a special case."] def self_equiv_sigma_orbits' {φ : Ω → β} (hφ : right_inverse φ quotient.mk') : β ≃ Σ (ω : Ω), orbit α (φ ω) := calc β ≃ Σ (ω : Ω), {b // quotient.mk' b = ω} : (equiv.sigma_fiber_equiv quotient.mk').symm ... ≃ Σ (ω : Ω), orbit α (φ ω) : equiv.sigma_congr_right (λ ω, equiv.subtype_equiv_right $ λ x, by {rw [← hφ ω, quotient.eq', hφ ω], refl }) /-- Decomposition of a type `X` as a disjoint union of its orbits under a group action. -/ @[to_additive "Decomposition of a type `X` as a disjoint union of its orbits under an additive group action."] noncomputable def self_equiv_sigma_orbits : β ≃ Σ (ω : Ω), orbit α ω.out' := self_equiv_sigma_orbits' α β quotient.out_eq' variables {α β} /-- If the stabilizer of `x` is `S`, then the stabilizer of `g • x` is `gSg⁻¹`. -/ lemma stabilizer_smul_eq_stabilizer_map_conj (g : α) (x : β) : (stabilizer α (g • x) = (stabilizer α x).map (mul_aut.conj g).to_monoid_hom) := begin ext h, rw [mem_stabilizer_iff, ← smul_left_cancel_iff g⁻¹, smul_smul, smul_smul, smul_smul, mul_left_inv, one_smul, ← mem_stabilizer_iff, subgroup.mem_map_equiv, mul_aut.conj_symm_apply] end /-- A bijection between the stabilizers of two elements in the same orbit. -/ noncomputable def stabilizer_equiv_stabilizer_of_orbit_rel {x y : β} (h : (orbit_rel α β).rel x y) : stabilizer α x ≃* stabilizer α y := let g : α := classical.some h in have hg : g • y = x := classical.some_spec h, have this : stabilizer α x = (stabilizer α y).map (mul_aut.conj g).to_monoid_hom, by rw [← hg, stabilizer_smul_eq_stabilizer_map_conj], (mul_equiv.subgroup_congr this).trans ((mul_aut.conj g).subgroup_map $ stabilizer α y).symm end mul_action namespace add_action variables [add_group α] [add_action α β] /-- If the stabilizer of `x` is `S`, then the stabilizer of `g +ᵥ x` is `g + S + (-g)`. -/ lemma stabilizer_vadd_eq_stabilizer_map_conj (g : α) (x : β) : (stabilizer α (g +ᵥ x) = (stabilizer α x).map (add_aut.conj g).to_add_monoid_hom) := begin ext h, rw [mem_stabilizer_iff, ← vadd_left_cancel_iff (-g) , vadd_vadd, vadd_vadd, vadd_vadd, add_left_neg, zero_vadd, ← mem_stabilizer_iff, add_subgroup.mem_map_equiv, add_aut.conj_symm_apply] end /-- A bijection between the stabilizers of two elements in the same orbit. -/ noncomputable def stabilizer_equiv_stabilizer_of_orbit_rel {x y : β} (h : (orbit_rel α β).rel x y) : stabilizer α x ≃+ stabilizer α y := let g : α := classical.some h in have hg : g +ᵥ y = x := classical.some_spec h, have this : stabilizer α x = (stabilizer α y).map (add_aut.conj g).to_add_monoid_hom, by rw [← hg, stabilizer_vadd_eq_stabilizer_map_conj], (add_equiv.add_subgroup_congr this).trans ((add_aut.conj g).add_subgroup_map $ stabilizer α y).symm end add_action namespace mul_action variables [group α] section quotient_action open subgroup mul_opposite variables (β) [monoid β] [mul_action β α] (H : subgroup α) /-- A typeclass for when a `mul_action β α` descends to the quotient `α ⧸ H`. -/ class quotient_action : Prop := (inv_mul_mem : ∀ (b : β) {a a' : α}, a⁻¹ * a' ∈ H → (b • a)⁻¹ * (b • a') ∈ H) /-- A typeclass for when an `add_action β α` descends to the quotient `α ⧸ H`. -/ class _root_.add_action.quotient_action {α : Type*} (β : Type*) [add_group α] [add_monoid β] [add_action β α] (H : add_subgroup α) : Prop := (inv_mul_mem : ∀ (b : β) {a a' : α}, -a + a' ∈ H → -(b +ᵥ a) + (b +ᵥ a') ∈ H) attribute [to_additive add_action.quotient_action] mul_action.quotient_action @[to_additive] instance left_quotient_action : quotient_action α H := ⟨λ _ _ _ _, by rwa [smul_eq_mul, smul_eq_mul, mul_inv_rev, mul_assoc, inv_mul_cancel_left]⟩ @[to_additive] instance right_quotient_action : quotient_action H.normalizer.opposite H := ⟨λ b c _ _, by rwa [smul_def, smul_def, smul_eq_mul_unop, smul_eq_mul_unop, mul_inv_rev, ←mul_assoc, mem_normalizer_iff'.mp b.prop, mul_assoc, mul_inv_cancel_left]⟩ @[to_additive] instance right_quotient_action' [hH : H.normal] : quotient_action αᵐᵒᵖ H := ⟨λ _ _ _ _, by rwa [smul_eq_mul_unop, smul_eq_mul_unop, mul_inv_rev, mul_assoc, hH.mem_comm_iff, mul_assoc, mul_inv_cancel_right]⟩ @[to_additive] instance quotient [quotient_action β H] : mul_action β (α ⧸ H) := { smul := λ b, quotient.map' ((•) b) (λ a a' h, quotient_action.inv_mul_mem b h), one_smul := λ q, quotient.induction_on' q (λ a, congr_arg quotient.mk' (one_smul β a)), mul_smul := λ b b' q, quotient.induction_on' q (λ a, congr_arg quotient.mk' (mul_smul b b' a)) } variables {β} @[simp, to_additive] lemma quotient.smul_mk [quotient_action β H] (b : β) (a : α) : (b • quotient_group.mk a : α ⧸ H) = quotient_group.mk (b • a) := rfl @[simp, to_additive] lemma quotient.smul_coe [quotient_action β H] (b : β) (a : α) : (b • a : α ⧸ H) = ↑(b • a) := rfl @[simp, to_additive] lemma quotient.mk_smul_out' [quotient_action β H] (b : β) (q : α ⧸ H) : quotient_group.mk (b • q.out') = b • q := by rw [←quotient.smul_mk, quotient_group.out_eq'] @[simp, to_additive] lemma quotient.coe_smul_out' [quotient_action β H] (b : β) (q : α ⧸ H) : ↑(b • q.out') = b • q := quotient.mk_smul_out' H b q end quotient_action open quotient_group /-- The canonical map to the left cosets. -/ def _root_.mul_action_hom.to_quotient (H : subgroup α) : α →[α] α ⧸ H := ⟨coe, quotient.smul_coe H⟩ @[simp] lemma _root_.mul_action_hom.to_quotient_apply (H : subgroup α) (g : α) : mul_action_hom.to_quotient H g = g := rfl @[to_additive] instance mul_left_cosets_comp_subtype_val (H I : subgroup α) : mul_action I (α ⧸ H) := mul_action.comp_hom (α ⧸ H) (subgroup.subtype I) variables (α) {β} [mul_action α β] (x : β) /-- The canonical map from the quotient of the stabilizer to the set. -/ @[to_additive "The canonical map from the quotient of the stabilizer to the set. "] def of_quotient_stabilizer (g : α ⧸ (mul_action.stabilizer α x)) : β := quotient.lift_on' g (•x) $ λ g1 g2 H, calc g1 • x = g1 • (g1⁻¹ * g2) • x : congr_arg _ H.symm ... = g2 • x : by rw [smul_smul, mul_inv_cancel_left] @[simp, to_additive] theorem of_quotient_stabilizer_mk (g : α) : of_quotient_stabilizer α x (quotient_group.mk g) = g • x := rfl @[to_additive] theorem of_quotient_stabilizer_mem_orbit (g) : of_quotient_stabilizer α x g ∈ orbit α x := quotient.induction_on' g $ λ g, ⟨g, rfl⟩ @[to_additive] theorem of_quotient_stabilizer_smul (g : α) (g' : α ⧸ (mul_action.stabilizer α x)) : of_quotient_stabilizer α x (g • g') = g • of_quotient_stabilizer α x g' := quotient.induction_on' g' $ λ _, mul_smul _ _ _ @[to_additive] theorem injective_of_quotient_stabilizer : function.injective (of_quotient_stabilizer α x) := λ y₁ y₂, quotient.induction_on₂' y₁ y₂ $ λ g₁ g₂ (H : g₁ • x = g₂ • x), quotient.sound' $ show (g₁⁻¹ * g₂) • x = x, by rw [mul_smul, ← H, inv_smul_smul] /-- Orbit-stabilizer theorem. -/ @[to_additive "Orbit-stabilizer theorem."] noncomputable def orbit_equiv_quotient_stabilizer (b : β) : orbit α b ≃ α ⧸ (stabilizer α b) := equiv.symm $ equiv.of_bijective (λ g, ⟨of_quotient_stabilizer α b g, of_quotient_stabilizer_mem_orbit α b g⟩) ⟨λ x y hxy, injective_of_quotient_stabilizer α b (by convert congr_arg subtype.val hxy), λ ⟨b, ⟨g, hgb⟩⟩, ⟨g, subtype.eq hgb⟩⟩ /-- Orbit-stabilizer theorem. -/ @[to_additive "Orbit-stabilizer theorem."] noncomputable def orbit_prod_stabilizer_equiv_group (b : β) : orbit α b × stabilizer α b ≃ α := (equiv.prod_congr (orbit_equiv_quotient_stabilizer α _) (equiv.refl _)).trans subgroup.group_equiv_quotient_times_subgroup.symm /-- Orbit-stabilizer theorem. -/ @[to_additive "Orbit-stabilizer theorem."] lemma card_orbit_mul_card_stabilizer_eq_card_group (b : β) [fintype α] [fintype $ orbit α b] [fintype $ stabilizer α b] : fintype.card (orbit α b) * fintype.card (stabilizer α b) = fintype.card α := by rw [← fintype.card_prod, fintype.card_congr (orbit_prod_stabilizer_equiv_group α b)] @[simp, to_additive] theorem orbit_equiv_quotient_stabilizer_symm_apply (b : β) (a : α) : ((orbit_equiv_quotient_stabilizer α b).symm a : β) = a • b := rfl @[simp, to_additive] lemma stabilizer_quotient {G} [group G] (H : subgroup G) : mul_action.stabilizer G ((1 : G) : G ⧸ H) = H := by { ext, simp [quotient_group.eq] } variable (β) local notation `Ω` := (quotient $ orbit_rel α β) /-- **Class formula** : given `G` a group acting on `X` and `φ` a function mapping each orbit of `X` under this action (that is, each element of the quotient of `X` by the relation `orbit_rel G X`) to an element in this orbit, this gives a (noncomputable) bijection between `X` and the disjoint union of `G/Stab(φ(ω))` over all orbits `ω`. In most cases you'll want `φ` to be `quotient.out'`, so we provide `mul_action.self_equiv_sigma_orbits_quotient_stabilizer` as a special case. -/ @[to_additive "**Class formula** : given `G` an additive group acting on `X` and `φ` a function mapping each orbit of `X` under this action (that is, each element of the quotient of `X` by the relation `orbit_rel G X`) to an element in this orbit, this gives a (noncomputable) bijection between `X` and the disjoint union of `G/Stab(φ(ω))` over all orbits `ω`. In most cases you'll want `φ` to be `quotient.out'`, so we provide `add_action.self_equiv_sigma_orbits_quotient_stabilizer` as a special case. "] noncomputable def self_equiv_sigma_orbits_quotient_stabilizer' {φ : Ω → β} (hφ : left_inverse quotient.mk' φ) : β ≃ Σ (ω : Ω), α ⧸ (stabilizer α (φ ω)) := calc β ≃ Σ (ω : Ω), orbit α (φ ω) : self_equiv_sigma_orbits' α β hφ ... ≃ Σ (ω : Ω), α ⧸ (stabilizer α (φ ω)) : equiv.sigma_congr_right (λ ω, orbit_equiv_quotient_stabilizer α (φ ω)) /-- **Class formula** for a finite group acting on a finite type. See `mul_action.card_eq_sum_card_group_div_card_stabilizer` for a specialized version using `quotient.out'`. -/ @[to_additive "**Class formula** for a finite group acting on a finite type. See `add_action.card_eq_sum_card_add_group_div_card_stabilizer` for a specialized version using `quotient.out'`."] lemma card_eq_sum_card_group_div_card_stabilizer' [fintype α] [fintype β] [fintype Ω] [Π (b : β), fintype $ stabilizer α b] {φ : Ω → β} (hφ : left_inverse quotient.mk' φ) : fintype.card β = ∑ (ω : Ω), fintype.card α / fintype.card (stabilizer α (φ ω)) := begin classical, have : ∀ ω : Ω, fintype.card α / fintype.card ↥(stabilizer α (φ ω)) = fintype.card (α ⧸ stabilizer α (φ ω)), { intro ω, rw [fintype.card_congr (@subgroup.group_equiv_quotient_times_subgroup α _ (stabilizer α $ φ ω)), fintype.card_prod, nat.mul_div_cancel], exact fintype.card_pos_iff.mpr (by apply_instance) }, simp_rw [this, ← fintype.card_sigma, fintype.card_congr (self_equiv_sigma_orbits_quotient_stabilizer' α β hφ)], end /-- **Class formula**. This is a special case of `mul_action.self_equiv_sigma_orbits_quotient_stabilizer'` with `φ = quotient.out'`. -/ @[to_additive "**Class formula**. This is a special case of `add_action.self_equiv_sigma_orbits_quotient_stabilizer'` with `φ = quotient.out'`. "] noncomputable def self_equiv_sigma_orbits_quotient_stabilizer : β ≃ Σ (ω : Ω), α ⧸ (stabilizer α ω.out') := self_equiv_sigma_orbits_quotient_stabilizer' α β quotient.out_eq' /-- **Class formula** for a finite group acting on a finite type. -/ @[to_additive "**Class formula** for a finite group acting on a finite type."] lemma card_eq_sum_card_group_div_card_stabilizer [fintype α] [fintype β] [fintype Ω] [Π (b : β), fintype $ stabilizer α b] : fintype.card β = ∑ (ω : Ω), fintype.card α / fintype.card (stabilizer α ω.out') := card_eq_sum_card_group_div_card_stabilizer' α β quotient.out_eq' /-- **Burnside's lemma** : a (noncomputable) bijection between the disjoint union of all `{x ∈ X | g • x = x}` for `g ∈ G` and the product `G × X/G`, where `G` is a group acting on `X` and `X/G`denotes the quotient of `X` by the relation `orbit_rel G X`. -/ @[to_additive "**Burnside's lemma** : a (noncomputable) bijection between the disjoint union of all `{x ∈ X | g • x = x}` for `g ∈ G` and the product `G × X/G`, where `G` is an additive group acting on `X` and `X/G`denotes the quotient of `X` by the relation `orbit_rel G X`. "] noncomputable def sigma_fixed_by_equiv_orbits_prod_group : (Σ (a : α), (fixed_by α β a)) ≃ Ω × α := calc (Σ (a : α), fixed_by α β a) ≃ {ab : α × β // ab.1 • ab.2 = ab.2} : (equiv.subtype_prod_equiv_sigma_subtype _).symm ... ≃ {ba : β × α // ba.2 • ba.1 = ba.1} : (equiv.prod_comm α β).subtype_equiv (λ ab, iff.rfl) ... ≃ Σ (b : β), stabilizer α b : equiv.subtype_prod_equiv_sigma_subtype (λ (b : β) a, a ∈ stabilizer α b) ... ≃ Σ (ωb : (Σ (ω : Ω), orbit α ω.out')), stabilizer α (ωb.2 : β) : (self_equiv_sigma_orbits α β).sigma_congr_left' ... ≃ Σ (ω : Ω), (Σ (b : orbit α ω.out'), stabilizer α (b : β)) : equiv.sigma_assoc (λ (ω : Ω) (b : orbit α ω.out'), stabilizer α (b : β)) ... ≃ Σ (ω : Ω), (Σ (b : orbit α ω.out'), stabilizer α ω.out') : equiv.sigma_congr_right (λ ω, equiv.sigma_congr_right $ λ ⟨b, hb⟩, (stabilizer_equiv_stabilizer_of_orbit_rel hb).to_equiv) ... ≃ Σ (ω : Ω), orbit α ω.out' × stabilizer α ω.out' : equiv.sigma_congr_right (λ ω, equiv.sigma_equiv_prod _ _) ... ≃ Σ (ω : Ω), α : equiv.sigma_congr_right (λ ω, orbit_prod_stabilizer_equiv_group α ω.out') ... ≃ Ω × α : equiv.sigma_equiv_prod Ω α /-- **Burnside's lemma** : given a finite group `G` acting on a set `X`, the average number of elements fixed by each `g ∈ G` is the number of orbits. -/ @[to_additive "**Burnside's lemma** : given a finite additive group `G` acting on a set `X`, the average number of elements fixed by each `g ∈ G` is the number of orbits. "] lemma sum_card_fixed_by_eq_card_orbits_mul_card_group [fintype α] [Π a, fintype $ fixed_by α β a] [fintype Ω] : ∑ (a : α), fintype.card (fixed_by α β a) = fintype.card Ω * fintype.card α := by rw [← fintype.card_prod, ← fintype.card_sigma, fintype.card_congr (sigma_fixed_by_equiv_orbits_prod_group α β)] @[to_additive] instance is_pretransitive_quotient (G) [group G] (H : subgroup G) : is_pretransitive G (G ⧸ H) := { exists_smul_eq := begin rintros ⟨x⟩ ⟨y⟩, refine ⟨y * x⁻¹, quotient_group.eq.mpr _⟩, simp only [smul_eq_mul, H.one_mem, mul_left_inv, inv_mul_cancel_right], end } end mul_action /-- `smul` by a `k : M` over a ring is injective, if `k` is not a zero divisor. The general theory of such `k` is elaborated by `is_smul_regular`. The typeclass that restricts all terms of `M` to have this property is `no_zero_smul_divisors`. -/ lemma smul_cancel_of_non_zero_divisor {M R : Type*} [monoid M] [non_unital_non_assoc_ring R] [distrib_mul_action M R] (k : M) (h : ∀ (x : R), k • x = 0 → x = 0) {a b : R} (h' : k • a = k • b) : a = b := begin rw ←sub_eq_zero, refine h _ _, rw [smul_sub, h', sub_self] end namespace subgroup variables {G : Type*} [group G] (H : subgroup G) lemma normal_core_eq_ker : H.normal_core = (mul_action.to_perm_hom G (G ⧸ H)).ker := begin refine le_antisymm (λ g hg, equiv.perm.ext (λ q, quotient_group.induction_on q (λ g', (mul_action.quotient.smul_mk H g g').trans (quotient_group.eq.mpr _)))) (subgroup.normal_le_normal_core.mpr (λ g hg, _)), { rw [smul_eq_mul, mul_inv_rev, ←inv_inv g', inv_inv], exact H.normal_core.inv_mem hg g'⁻¹ }, { rw [←H.inv_mem_iff, ←mul_one g⁻¹, ←quotient_group.eq, ←mul_one g], exact (mul_action.quotient.smul_mk H g 1).symm.trans (equiv.perm.ext_iff.mp hg (1 : G)) }, end noncomputable instance fintype_quotient_normal_core [fintype (G ⧸ H)] : fintype (G ⧸ H.normal_core) := begin rw H.normal_core_eq_ker, classical, exact fintype.of_equiv _ (quotient_group.quotient_ker_equiv_range _).symm.to_equiv, end end subgroup
445d6e9947517d05ea8a7919d0a1e77767f21ca2
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/ring_theory/witt_vector/truncated.lean
53e63f99a751630321750f5bef081dab9cef3940
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,222
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import ring_theory.witt_vector.init_tail import tactic.equiv_rw /-! # Truncated Witt vectors The ring of truncated Witt vectors (of length `n`) is a quotient of the ring of Witt vectors. It retains the first `n` coefficients of each Witt vector. In this file, we set up the basic quotient API for this ring. The ring of Witt vectors is the projective limit of all the rings of truncated Witt vectors. ## Main declarations - `truncated_witt_vector`: the underlying type of the ring of truncated Witt vectors - `truncated_witt_vector.comm_ring`: the ring structure on truncated Witt vectors - `witt_vector.truncate`: the quotient homomorphism that truncates a Witt vector, to obtain a truncated Witt vector - `truncated_witt_vector.truncate`: the homomorphism that truncates a truncated Witt vector of length `n` to one of length `m` (for some `m ≤ n`) - `witt_vector.lift`: the unique ring homomorphism into the ring of Witt vectors that is compatible with a family of ring homomorphisms to the truncated Witt vectors: this realizes the ring of Witt vectors as projective limit of the rings of truncated Witt vectors ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ open function (injective surjective) noncomputable theory variables {p : ℕ} [hp : fact p.prime] (n : ℕ) (R : Type*) local notation `𝕎` := witt_vector p -- type as `\bbW` /-- A truncated Witt vector over `R` is a vector of elements of `R`, i.e., the first `n` coefficients of a Witt vector. We will define operations on this type that are compatible with the (untruncated) Witt vector operations. `truncated_witt_vector p n R` takes a parameter `p : ℕ` that is not used in the definition. In practice, this number `p` is assumed to be a prime number, and under this assumption we construct a ring structure on `truncated_witt_vector p n R`. (`truncated_witt_vector p₁ n R` and `truncated_witt_vector p₂ n R` are definitionally equal as types but will have different ring operations.) -/ @[nolint unused_arguments] def truncated_witt_vector (p : ℕ) (n : ℕ) (R : Type*) := fin n → R instance (p n : ℕ) (R : Type*) [inhabited R] : inhabited (truncated_witt_vector p n R) := ⟨λ _, default R⟩ variables {n R} namespace truncated_witt_vector variables (p) /-- Create a `truncated_witt_vector` from a vector `x`. -/ def mk (x : fin n → R) : truncated_witt_vector p n R := x variables {p} /-- `x.coeff i` is the `i`th entry of `x`. -/ def coeff (i : fin n) (x : truncated_witt_vector p n R) : R := x i @[ext] lemma ext {x y : truncated_witt_vector p n R} (h : ∀ i, x.coeff i = y.coeff i) : x = y := funext h lemma ext_iff {x y : truncated_witt_vector p n R} : x = y ↔ ∀ i, x.coeff i = y.coeff i := ⟨λ h i, by rw h, ext⟩ @[simp] lemma coeff_mk (x : fin n → R) (i : fin n) : (mk p x).coeff i = x i := rfl @[simp] lemma mk_coeff (x : truncated_witt_vector p n R) : mk p (λ i, x.coeff i) = x := by { ext i, rw [coeff_mk] } variable [comm_ring R] /-- We can turn a truncated Witt vector `x` into a Witt vector by setting all coefficients after `x` to be 0. -/ def out (x : truncated_witt_vector p n R) : 𝕎 R := witt_vector.mk p $ λ i, if h : i < n then x.coeff ⟨i, h⟩ else 0 @[simp] lemma coeff_out (x : truncated_witt_vector p n R) (i : fin n) : x.out.coeff i = x.coeff i := by rw [out, witt_vector.coeff_mk, dif_pos i.is_lt, fin.eta] lemma out_injective : injective (@out p n R _) := begin intros x y h, ext i, rw [witt_vector.ext_iff] at h, simpa only [coeff_out] using h ↑i end end truncated_witt_vector namespace witt_vector variables {p} (n) section local attribute [semireducible] witt_vector /-- `truncate_fun n x` uses the first `n` entries of `x` to construct a `truncated_witt_vector`, which has the same base `p` as `x`. This function is bundled into a ring homomorphism in `witt_vector.truncate` -/ def truncate_fun (x : 𝕎 R) : truncated_witt_vector p n R := truncated_witt_vector.mk p $ λ i, x.coeff i end variables {n} @[simp] lemma coeff_truncate_fun (x : 𝕎 R) (i : fin n) : (truncate_fun n x).coeff i = x.coeff i := by rw [truncate_fun, truncated_witt_vector.coeff_mk] variable [comm_ring R] @[simp] lemma out_truncate_fun (x : 𝕎 R) : (truncate_fun n x).out = init n x := begin ext i, dsimp [truncated_witt_vector.out, init, select], split_ifs with hi, swap, { refl }, rw [coeff_truncate_fun, fin.coe_mk], end end witt_vector namespace truncated_witt_vector variable [comm_ring R] @[simp] lemma truncate_fun_out (x : truncated_witt_vector p n R) : x.out.truncate_fun n = x := by simp only [witt_vector.truncate_fun, coeff_out, mk_coeff] open witt_vector variables (p n R) include hp instance : has_zero (truncated_witt_vector p n R) := ⟨truncate_fun n 0⟩ instance : has_one (truncated_witt_vector p n R) := ⟨truncate_fun n 1⟩ instance : has_add (truncated_witt_vector p n R) := ⟨λ x y, truncate_fun n (x.out + y.out)⟩ instance : has_mul (truncated_witt_vector p n R) := ⟨λ x y, truncate_fun n (x.out * y.out)⟩ instance : has_neg (truncated_witt_vector p n R) := ⟨λ x, truncate_fun n (- x.out)⟩ @[simp] lemma coeff_zero (i : fin n) : (0 : truncated_witt_vector p n R).coeff i = 0 := begin show coeff i (truncate_fun _ 0 : truncated_witt_vector p n R) = 0, rw [coeff_truncate_fun, witt_vector.zero_coeff], end end truncated_witt_vector /-- A macro tactic used to prove that `truncate_fun` respects ring operations. -/ meta def tactic.interactive.witt_truncate_fun_tac : tactic unit := `[show _ = truncate_fun n _, apply truncated_witt_vector.out_injective, iterate { rw [out_truncate_fun] }, rw init_add <|> rw init_mul <|> rw init_neg] namespace witt_vector variables (p n R) variable [comm_ring R] lemma truncate_fun_surjective : surjective (@truncate_fun p n R) := λ x, ⟨x.out, truncated_witt_vector.truncate_fun_out x⟩ include hp @[simp] lemma truncate_fun_zero : truncate_fun n (0 : 𝕎 R) = 0 := rfl @[simp] lemma truncate_fun_one : truncate_fun n (1 : 𝕎 R) = 1 := rfl variables {p R} @[simp] lemma truncate_fun_add (x y : 𝕎 R) : truncate_fun n (x + y) = truncate_fun n x + truncate_fun n y := by witt_truncate_fun_tac @[simp] lemma truncate_fun_mul (x y : 𝕎 R) : truncate_fun n (x * y) = truncate_fun n x * truncate_fun n y := by witt_truncate_fun_tac lemma truncate_fun_neg (x : 𝕎 R) : truncate_fun n (-x) = -truncate_fun n x := by witt_truncate_fun_tac end witt_vector namespace truncated_witt_vector open witt_vector variables (p n R) variable [comm_ring R] include hp instance : comm_ring (truncated_witt_vector p n R) := (truncate_fun_surjective p n R).comm_ring _ (truncate_fun_zero p n R) (truncate_fun_one p n R) (truncate_fun_add n) (truncate_fun_mul n) (truncate_fun_neg n) end truncated_witt_vector namespace witt_vector open truncated_witt_vector variables (n) variable [comm_ring R] include hp /-- `truncate n` is a ring homomorphism that truncates `x` to its first `n` entries to obtain a `truncated_witt_vector`, which has the same base `p` as `x`. -/ def truncate : 𝕎 R →+* truncated_witt_vector p n R := { to_fun := truncate_fun n, map_zero' := truncate_fun_zero p n R, map_add' := truncate_fun_add n, map_one' := truncate_fun_one p n R, map_mul' := truncate_fun_mul n } variables (p n R) lemma truncate_surjective : surjective (truncate n : 𝕎 R → truncated_witt_vector p n R) := truncate_fun_surjective p n R variables {p n R} @[simp] lemma coeff_truncate (x : 𝕎 R) (i : fin n) : (truncate n x).coeff i = x.coeff i := coeff_truncate_fun _ _ variables (n) lemma mem_ker_truncate (x : 𝕎 R) : x ∈ (@truncate p _ n R _).ker ↔ ∀ i < n, x.coeff i = 0 := begin simp only [ring_hom.mem_ker, truncate, truncate_fun, ring_hom.coe_mk, truncated_witt_vector.ext_iff, truncated_witt_vector.coeff_mk, coeff_zero], exact subtype.forall end variables (p) @[simp] lemma truncate_mk (f : ℕ → R) : truncate n (mk p f) = truncated_witt_vector.mk _ (λ k, f k) := begin ext i, rw [coeff_truncate, coeff_mk, truncated_witt_vector.coeff_mk], end end witt_vector namespace truncated_witt_vector variable [comm_ring R] include hp /-- A ring homomorphism that truncates a truncated Witt vector of length `m` to a truncated Witt vector of length `n`, for `n ≤ m`. -/ def truncate {m : ℕ} (hm : n ≤ m) : truncated_witt_vector p m R →+* truncated_witt_vector p n R := ring_hom.lift_of_surjective (witt_vector.truncate m) (witt_vector.truncate_surjective p m R) (witt_vector.truncate n) begin intro x, simp only [witt_vector.mem_ker_truncate], intros h i hi, exact h i (lt_of_lt_of_le hi hm) end @[simp] lemma truncate_comp_witt_vector_truncate {m : ℕ} (hm : n ≤ m) : (@truncate p _ n R _ m hm).comp (witt_vector.truncate m) = witt_vector.truncate n := ring_hom.lift_of_surjective_comp _ _ _ _ @[simp] lemma truncate_witt_vector_truncate {m : ℕ} (hm : n ≤ m) (x : 𝕎 R) : truncate hm (witt_vector.truncate m x) = witt_vector.truncate n x := ring_hom.lift_of_surjective_comp_apply _ _ _ _ _ @[simp] lemma truncate_truncate {n₁ n₂ n₃ : ℕ} (h1 : n₁ ≤ n₂) (h2 : n₂ ≤ n₃) (x : truncated_witt_vector p n₃ R) : (truncate h1) (truncate h2 x) = truncate (h1.trans h2) x := begin obtain ⟨x, rfl⟩ := witt_vector.truncate_surjective p n₃ R x, simp only [truncate_witt_vector_truncate], end @[simp] lemma truncate_comp {n₁ n₂ n₃ : ℕ} (h1 : n₁ ≤ n₂) (h2 : n₂ ≤ n₃) : (@truncate p _ _ R _ _ h1).comp (truncate h2) = truncate (h1.trans h2) := begin ext1 x, simp only [truncate_truncate, function.comp_app, ring_hom.coe_comp] end lemma truncate_surjective {m : ℕ} (hm : n ≤ m) : surjective (@truncate p _ _ R _ _ hm) := begin intro x, obtain ⟨x, rfl⟩ := witt_vector.truncate_surjective p _ R x, exact ⟨witt_vector.truncate _ x, truncate_witt_vector_truncate _ _⟩ end @[simp] lemma coeff_truncate {m : ℕ} (hm : n ≤ m) (i : fin n) (x : truncated_witt_vector p m R) : (truncate hm x).coeff i = x.coeff (fin.cast_le hm i) := begin obtain ⟨y, rfl⟩ := witt_vector.truncate_surjective p _ _ x, simp only [truncate_witt_vector_truncate, witt_vector.coeff_truncate, fin.coe_cast_le], end section fintype omit hp instance {R : Type*} [fintype R] : fintype (truncated_witt_vector p n R) := pi.fintype variables (p n R) lemma card {R : Type*} [fintype R] : fintype.card (truncated_witt_vector p n R) = fintype.card R ^ n := by simp only [truncated_witt_vector, fintype.card_fin, fintype.card_fun] end fintype lemma infi_ker_truncate : (⨅ i : ℕ, (@witt_vector.truncate p _ i R _).ker) = ⊥ := begin rw [submodule.eq_bot_iff], intros x hx, ext, simp only [witt_vector.mem_ker_truncate, ideal.mem_infi, witt_vector.zero_coeff] at hx ⊢, exact hx _ _ (nat.lt_succ_self _) end end truncated_witt_vector namespace witt_vector open truncated_witt_vector (hiding truncate coeff) section lift variable [comm_ring R] variables {S : Type*} [semiring S] variable (f : Π k : ℕ, S →+* truncated_witt_vector p k R) variable f_compat : ∀ (k₁ k₂ : ℕ) (hk : k₁ ≤ k₂), (truncated_witt_vector.truncate hk).comp (f k₂) = f k₁ variables {p R} variable (n) /-- Given a family `fₖ : S → truncated_witt_vector p k R` and `s : S`, we produce a Witt vector by defining the `k`th entry to be the final entry of `fₖ s`. -/ def lift_fun (s : S) : 𝕎 R := witt_vector.mk p $ λ k, truncated_witt_vector.coeff (fin.last k) (f (k+1) s) variables {f} include f_compat @[simp] lemma truncate_lift_fun (s : S) : witt_vector.truncate n (lift_fun f s) = f n s := begin ext i, simp only [lift_fun, truncated_witt_vector.coeff_mk, witt_vector.truncate_mk], rw [← f_compat (i+1) n i.is_lt, ring_hom.comp_apply, truncated_witt_vector.coeff_truncate], -- this is a bit unfortunate congr' with _, simp only [fin.coe_last, fin.coe_cast_le], end variable (f) /-- Given compatible ring homs from `S` into `truncated_witt_vector n` for each `n`, we can lift these to a ring hom `S → 𝕎 R`. `lift` defines the universal property of `𝕎 R` as the inverse limit of `truncated_witt_vector n`. -/ def lift : S →+* 𝕎 R := by refine_struct { to_fun := lift_fun f }; { intros, rw [← sub_eq_zero, ← ideal.mem_bot, ← infi_ker_truncate, ideal.mem_infi], simp [ring_hom.mem_ker, f_compat] } variable {f} @[simp] lemma truncate_lift (s : S) : witt_vector.truncate n (lift _ f_compat s) = f n s := truncate_lift_fun _ f_compat s @[simp] lemma truncate_comp_lift : (witt_vector.truncate n).comp (lift _ f_compat) = f n := by { ext1, rw [ring_hom.comp_apply, truncate_lift] } /-- The uniqueness part of the universal property of `𝕎 R`. -/ lemma lift_unique (g : S →+* 𝕎 R) (g_compat : ∀ k, (witt_vector.truncate k).comp g = f k) : lift _ f_compat = g := begin ext1 x, rw [← sub_eq_zero, ← ideal.mem_bot, ← infi_ker_truncate, ideal.mem_infi], intro i, simp only [ring_hom.mem_ker, g_compat, ←ring_hom.comp_apply, truncate_comp_lift, ring_hom.map_sub, sub_self], end omit f_compat include hp /-- The universal property of `𝕎 R` as projective limit of truncated Witt vector rings. -/ @[simps] def lift_equiv : {f : Π k, S →+* truncated_witt_vector p k R // ∀ k₁ k₂ (hk : k₁ ≤ k₂), (truncated_witt_vector.truncate hk).comp (f k₂) = f k₁} ≃ (S →+* 𝕎 R) := { to_fun := λ f, lift f.1 f.2, inv_fun := λ g, ⟨λ k, (truncate k).comp g, by { intros _ _ h, simp only [←ring_hom.comp_assoc, truncate_comp_witt_vector_truncate] }⟩, left_inv := by { rintro ⟨f, hf⟩, simp only [truncate_comp_lift] }, right_inv := λ g, lift_unique _ _ $ λ _, rfl } lemma hom_ext (g₁ g₂ : S →+* 𝕎 R) (h : ∀ k, (truncate k).comp g₁ = (truncate k).comp g₂) : g₁ = g₂ := lift_equiv.symm.injective $ subtype.ext $ funext h end lift end witt_vector
588a341595ca99667bf66afac77506f74a557924
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/list/min_max.lean
24582c1a89f5a52bb51a8e6340f85ebffd8e4839
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
11,132
lean
/- Copyright (c) 2019 Minchao Wu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Minchao Wu, Chris Hughes -/ import data.list.basic /-! # Minimum and maximum of lists ## Main definitions The main definitions are `argmax`, `argmin`, `minimum` and `maximum` for lists. `argmax f l` returns `some a`, where `a` of `l` that maximises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmax f []` = none` `minimum l` returns an `with_top α`, the smallest element of `l` for nonempty lists, and `⊤` for `[]` -/ namespace list variables {α : Type*} {β : Type*} [decidable_linear_order β] /-- Auxiliary definition to define `argmax` -/ def argmax₂ (f : α → β) (a : option α) (b : α) : option α := option.cases_on a (some b) (λ c, if f b ≤ f c then some c else some b) /-- `argmax f l` returns `some a`, where `a` of `l` that maximises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmax f []` = none` -/ def argmax (f : α → β) (l : list α) : option α := l.foldl (argmax₂ f) none /-- `argmin f l` returns `some a`, where `a` of `l` that minimises `f a`. If there are `a b` such that `f a = f b`, it returns whichever of `a` or `b` comes first in the list. `argmin f []` = none` -/ def argmin (f : α → β) (l : list α) := @argmax _ (order_dual β) _ f l @[simp] lemma argmax_two_self (f : α → β) (a : α) : argmax₂ f (some a) a = a := if_pos (le_refl _) @[simp] lemma argmax_nil (f : α → β) : argmax f [] = none := rfl @[simp] lemma argmin_nil (f : α → β) : argmin f [] = none := rfl @[simp] lemma argmax_singleton {f : α → β} {a : α} : argmax f [a] = some a := rfl @[simp] lemma argmin_singleton {f : α → β} {a : α} : argmin f [a] = a := rfl @[simp] lemma foldl_argmax₂_eq_none {f : α → β} {l : list α} {o : option α} : l.foldl (argmax₂ f) o = none ↔ l = [] ∧ o = none := list.reverse_rec_on l (by simp) $ (assume tl hd, by simp [argmax₂]; cases foldl (argmax₂ f) o tl; simp; try {split_ifs}; simp) private theorem le_of_foldl_argmax₂ {f : α → β} {l} : Π {a m : α} {o : option α}, a ∈ l → m ∈ foldl (argmax₂ f) o l → f a ≤ f m := list.reverse_rec_on l (λ _ _ _ h, absurd h $ not_mem_nil _) begin intros tl _ ih _ _ _ h ho, rw [foldl_append, foldl_cons, foldl_nil, argmax₂] at ho, cases hf : foldl (argmax₂ f) o tl, { rw [hf] at ho, rw [foldl_argmax₂_eq_none] at hf, simp [hf.1, hf.2, *] at * }, rw [hf, option.mem_def] at ho, dsimp only at ho, cases mem_append.1 h with h h, { refine le_trans (ih h hf) _, have := @le_of_lt _ _ (f val) (f m), split_ifs at ho; simp * at * }, { split_ifs at ho; simp * at * } end private theorem foldl_argmax₂_mem (f : α → β) (l) : Π (a m : α), m ∈ foldl (argmax₂ f) (some a) l → m ∈ a :: l := list.reverse_rec_on l (by simp [eq_comm]) begin assume tl hd ih a m, simp only [foldl_append, foldl_cons, foldl_nil, argmax₂], cases hf : foldl (argmax₂ f) (some a) tl, { simp {contextual := tt} }, { dsimp only, split_ifs, { finish [ih _ _ hf] }, { simp {contextual := tt} } } end theorem argmax_mem {f : α → β} : Π {l : list α} {m : α}, m ∈ argmax f l → m ∈ l | [] m := by simp | (hd::tl) m := by simpa [argmax, argmax₂] using foldl_argmax₂_mem f tl hd m theorem argmin_mem {f : α → β} : Π {l : list α} {m : α}, m ∈ argmin f l → m ∈ l := @argmax_mem _ (order_dual β) _ _ @[simp] theorem argmax_eq_none {f : α → β} {l : list α} : l.argmax f = none ↔ l = [] := by simp [argmax] @[simp] theorem argmin_eq_none {f : α → β} {l : list α} : l.argmin f = none ↔ l = [] := @argmax_eq_none _ (order_dual β) _ _ _ theorem le_argmax_of_mem {f : α → β} {a m : α} {l : list α} : a ∈ l → m ∈ argmax f l → f a ≤ f m := le_of_foldl_argmax₂ theorem argmin_le_of_mem {f : α → β} {a m : α} {l : list α} : a ∈ l → m ∈ argmin f l → f m ≤ f a:= @le_argmax_of_mem _ (order_dual β) _ _ _ _ _ theorem argmax_concat (f : α → β) (a : α) (l : list α) : argmax f (l ++ [a]) = option.cases_on (argmax f l) (some a) (λ c, if f a ≤ f c then some c else some a) := by rw [argmax, argmax]; simp [argmax₂] theorem argmin_concat (f : α → β) (a : α) (l : list α) : argmin f (l ++ [a]) = option.cases_on (argmin f l) (some a) (λ c, if f c ≤ f a then some c else some a) := @argmax_concat _ (order_dual β) _ _ _ _ theorem argmax_cons (f : α → β) (a : α) (l : list α) : argmax f (a :: l) = option.cases_on (argmax f l) (some a) (λ c, if f c ≤ f a then some a else some c) := list.reverse_rec_on l rfl $ assume hd tl ih, begin rw [← cons_append, argmax_concat, ih, argmax_concat], cases h : argmax f hd with m, { simp [h] }, { simp [h], dsimp, by_cases ham : f m ≤ f a, { rw if_pos ham, dsimp, by_cases htlm : f tl ≤ f m, { rw if_pos htlm, dsimp, rw [if_pos (le_trans htlm ham), if_pos ham] }, { rw if_neg htlm } }, { rw if_neg ham, dsimp, by_cases htlm : f tl ≤ f m, { rw if_pos htlm, dsimp, rw if_neg ham }, { rw if_neg htlm, dsimp, rw [if_neg (not_le_of_gt (lt_trans (lt_of_not_ge ham) (lt_of_not_ge htlm)))] } } } end theorem argmin_cons (f : α → β) (a : α) (l : list α) : argmin f (a :: l) = option.cases_on (argmin f l) (some a) (λ c, if f a ≤ f c then some a else some c) := @argmax_cons _ (order_dual β) _ _ _ _ theorem index_of_argmax [decidable_eq α] {f : α → β} : Π {l : list α} {m : α}, m ∈ argmax f l → ∀ {a}, a ∈ l → f m ≤ f a → l.index_of m ≤ l.index_of a | [] m _ _ _ _ := by simp | (hd::tl) m hm a ha ham := begin simp only [index_of_cons, argmax_cons, option.mem_def] at ⊢ hm, cases h : argmax f tl, { rw h at hm, simp * at * }, { rw h at hm, dsimp only at hm, cases ha with hahd hatl, { clear index_of_argmax, subst hahd, split_ifs at hm, { subst hm }, { subst hm, contradiction } }, { have := index_of_argmax h hatl, clear index_of_argmax, split_ifs at *; refl <|> exact nat.zero_le _ <|> simp [*, nat.succ_le_succ_iff, -not_le] at * } } end theorem index_of_argmin [decidable_eq α] {f : α → β} : Π {l : list α} {m : α}, m ∈ argmin f l → ∀ {a}, a ∈ l → f a ≤ f m → l.index_of m ≤ l.index_of a := @index_of_argmax _ (order_dual β) _ _ _ theorem mem_argmax_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : m ∈ argmax f l ↔ m ∈ l ∧ (∀ a ∈ l, f a ≤ f m) ∧ (∀ a ∈ l, f m ≤ f a → l.index_of m ≤ l.index_of a) := ⟨λ hm, ⟨argmax_mem hm, λ a ha, le_argmax_of_mem ha hm, λ _, index_of_argmax hm⟩, begin rintros ⟨hml, ham, hma⟩, cases harg : argmax f l with n, { simp * at * }, { have := le_antisymm (hma n (argmax_mem harg) (le_argmax_of_mem hml harg)) (index_of_argmax harg hml (ham _ (argmax_mem harg))), rw [(index_of_inj hml (argmax_mem harg)).1 this, option.mem_def] } end⟩ theorem argmax_eq_some_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : argmax f l = some m ↔ m ∈ l ∧ (∀ a ∈ l, f a ≤ f m) ∧ (∀ a ∈ l, f m ≤ f a → l.index_of m ≤ l.index_of a) := mem_argmax_iff theorem mem_argmin_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : m ∈ argmin f l ↔ m ∈ l ∧ (∀ a ∈ l, f m ≤ f a) ∧ (∀ a ∈ l, f a ≤ f m → l.index_of m ≤ l.index_of a) := @mem_argmax_iff _ (order_dual β) _ _ _ _ _ theorem argmin_eq_some_iff [decidable_eq α] {f : α → β} {m : α} {l : list α} : argmin f l = some m ↔ m ∈ l ∧ (∀ a ∈ l, f m ≤ f a) ∧ (∀ a ∈ l, f a ≤ f m → l.index_of m ≤ l.index_of a) := mem_argmin_iff variable [decidable_linear_order α] /-- `maximum l` returns an `with_bot α`, the largest element of `l` for nonempty lists, and `⊥` for `[]` -/ def maximum (l : list α) : with_bot α := argmax id l /-- `minimum l` returns an `with_top α`, the smallest element of `l` for nonempty lists, and `⊤` for `[]` -/ def minimum (l : list α) : with_top α := argmin id l @[simp] lemma maximum_nil : maximum ([] : list α) = ⊥ := rfl @[simp] lemma minimum_nil : minimum ([] : list α) = ⊤ := rfl @[simp] lemma maximum_singleton (a : α) : maximum [a] = a := rfl @[simp] lemma minimum_singleton (a : α) : minimum [a] = a := rfl theorem maximum_mem {l : list α} {m : α} : (maximum l : with_top α) = m → m ∈ l := argmax_mem theorem minimum_mem {l : list α} {m : α} : (minimum l : with_bot α) = m → m ∈ l := argmin_mem @[simp] theorem maximum_eq_none {l : list α} : l.maximum = none ↔ l = [] := argmax_eq_none @[simp] theorem minimum_eq_none {l : list α} : l.minimum = none ↔ l = [] := argmin_eq_none theorem le_maximum_of_mem {a m : α} {l : list α} : a ∈ l → (maximum l : with_bot α) = m → a ≤ m := le_argmax_of_mem theorem minimum_le_of_mem {a m : α} {l : list α} : a ∈ l → (minimum l : with_top α) = m → m ≤ a := argmin_le_of_mem theorem le_maximum_of_mem' {a : α} {l : list α} (ha : a ∈ l) : (a : with_bot α) ≤ maximum l := option.cases_on (maximum l) (λ _ h, absurd ha ((h rfl).symm ▸ not_mem_nil _)) (λ m hm _, with_bot.coe_le_coe.2 $ hm _ rfl) (λ m, @le_maximum_of_mem _ _ _ m _ ha) (@maximum_eq_none _ _ l).1 theorem le_minimum_of_mem' {a : α} {l : list α} (ha : a ∈ l) : minimum l ≤ (a : with_top α) := @le_maximum_of_mem' (order_dual α) _ _ _ ha theorem maximum_concat (a : α) (l : list α) : maximum (l ++ [a]) = max (maximum l) a := begin rw max_comm, simp only [maximum, argmax_concat, id], cases h : argmax id l, { rw [max_eq_left], refl, exact bot_le }, change (coe : α → with_bot α) with some, rw [max_comm], simp [max] end theorem minimum_concat (a : α) (l : list α) : minimum (l ++ [a]) = min (minimum l) a := @maximum_concat (order_dual α) _ _ _ theorem maximum_cons (a : α) (l : list α) : maximum (a :: l) = max a (maximum l) := list.reverse_rec_on l (by simp [@max_eq_left (with_bot α) _ _ _ bot_le]) (λ tl hd ih, by rw [← cons_append, maximum_concat, ih, maximum_concat, max_assoc]) theorem minimum_cons (a : α) (l : list α) : minimum (a :: l) = min a (minimum l) := @maximum_cons (order_dual α) _ _ _ theorem maximum_eq_coe_iff {m : α} {l : list α} : maximum l = m ↔ m ∈ l ∧ (∀ a ∈ l, a ≤ m) := begin unfold_coes, simp only [maximum, argmax_eq_some_iff, id], split, { simp only [true_and, forall_true_iff] {contextual := tt} }, { simp only [true_and, forall_true_iff] {contextual := tt}, intros h a hal hma, rw [le_antisymm hma (h.2 a hal)] } end theorem minimum_eq_coe_iff {m : α} {l : list α} : minimum l = m ↔ m ∈ l ∧ (∀ a ∈ l, m ≤ a) := @maximum_eq_coe_iff (order_dual α) _ _ _ end list
d5ce9982646a79fe1b302e3234ec8d3111d2eda6
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/measure_theory/measure_space.lean
c74ed79ee70759303dffe563496666797b042f0f
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
35,559
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 Measure spaces -- measures Measures are restricted to a measurable space (associated by the type class `measurable_space`). This allows us to prove equalities between measures by restricting to a generating set of the measurable space. On the other hand, the `μ.measure s` projection (i.e. the measure of `s` on the measure space `μ`) is the _outer_ measure generated by `μ`. This gives us a unrestricted monotonicity rule and it is somehow well-behaved on non-measurable sets. This allows us for the `lebesgue` measure space to have the `borel` measurable space, but still be a complete measure. -/ import data.set.lattice data.set.finite import topology.instances.ennreal measure_theory.outer_measure noncomputable theory open classical set lattice filter finset function open_locale classical topological_space universes u v w x namespace measure_theory section of_measurable parameters {α : Type*} [measurable_space α] parameters (m : Π (s : set α), is_measurable s → ennreal) parameters (m0 : m ∅ is_measurable.empty = 0) include m0 /-- Measure projection which is ∞ for non-measurable sets. `measure'` is mainly used to derive the outer measure, for the main `measure` projection. -/ def measure' (s : set α) : ennreal := ⨅ h : is_measurable s, m s h lemma measure'_eq {s} (h : is_measurable s) : measure' s = m s h := by simp [measure', h] lemma measure'_empty : measure' ∅ = 0 := (measure'_eq is_measurable.empty).trans m0 lemma measure'_Union_nat {f : ℕ → set α} (hm : ∀i, is_measurable (f i)) (mU : m (⋃i, f i) (is_measurable.Union hm) = (∑i, m (f i) (hm i))) : measure' (⋃i, f i) = (∑i, measure' (f i)) := (measure'_eq _).trans $ mU.trans $ by congr; funext i; rw measure'_eq /-- outer measure of a measure -/ def outer_measure' : outer_measure α := outer_measure.of_function measure' measure'_empty lemma measure'_Union_le_tsum_nat' (mU : ∀ {f : ℕ → set α} (hm : ∀i, is_measurable (f i)), m (⋃i, f i) (is_measurable.Union hm) ≤ (∑i, m (f i) (hm i))) (s : ℕ → set α) : measure' (⋃i, s i) ≤ (∑i, measure' (s i)) := begin by_cases h : ∀i, is_measurable (s i), { rw [measure'_eq _ _ (is_measurable.Union h), congr_arg tsum _], {apply mU h}, funext i, apply measure'_eq _ _ (h i) }, { cases not_forall.1 h with i hi, exact le_trans (le_infi $ λ h, hi.elim h) (ennreal.le_tsum i) } end parameter (mU : ∀ {f : ℕ → set α} (hm : ∀i, is_measurable (f i)), pairwise (disjoint on f) → m (⋃i, f i) (is_measurable.Union hm) = (∑i, m (f i) (hm i))) include mU lemma measure'_Union {β} [encodable β] {f : β → set α} (hd : pairwise (disjoint on f)) (hm : ∀i, is_measurable (f i)) : measure' (⋃i, f i) = (∑i, measure' (f i)) := begin rw [encodable.Union_decode2, outer_measure.Union_aux], { exact measure'_Union_nat _ _ (λ n, encodable.Union_decode2_cases is_measurable.empty hm) (mU _ (measurable_space.Union_decode2_disjoint_on hd)) }, { apply measure'_empty }, end lemma measure'_union {s₁ s₂ : set α} (hd : disjoint s₁ s₂) (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : measure' (s₁ ∪ s₂) = measure' s₁ + measure' s₂ := begin rw [union_eq_Union, measure'_Union _ _ @mU (pairwise_disjoint_on_bool.2 hd) (bool.forall_bool.2 ⟨h₂, h₁⟩), tsum_fintype], change _+_ = _, simp end lemma measure'_mono {s₁ s₂ : set α} (h₁ : is_measurable s₁) (hs : s₁ ⊆ s₂) : measure' s₁ ≤ measure' s₂ := le_infi $ λ h₂, begin have := measure'_union _ _ @mU disjoint_diff h₁ (h₂.diff h₁), rw union_diff_cancel hs at this, rw ← measure'_eq m m0 _, exact le_iff_exists_add.2 ⟨_, this⟩ end lemma measure'_Union_le_tsum_nat : ∀ (s : ℕ → set α), measure' (⋃i, s i) ≤ (∑i, measure' (s i)) := measure'_Union_le_tsum_nat' $ λ f h, begin simp [Union_disjointed.symm] {single_pass := tt}, rw [mU (is_measurable.disjointed h) disjoint_disjointed], refine ennreal.tsum_le_tsum (λ i, _), rw [← measure'_eq m m0, ← measure'_eq m m0], exact measure'_mono _ _ @mU (is_measurable.disjointed h _) (inter_subset_left _ _) end lemma outer_measure'_eq {s : set α} (hs : is_measurable s) : outer_measure' s = m s hs := by rw ← measure'_eq m m0 hs; exact (le_antisymm (outer_measure.of_function_le _ _ _) $ le_infi $ λ f, le_infi $ λ hf, le_trans (measure'_mono _ _ @mU hs hf) $ measure'_Union_le_tsum_nat _ _ @mU _) lemma outer_measure'_eq_measure' {s : set α} (hs : is_measurable s) : outer_measure' s = measure' s := by rw [measure'_eq m m0 hs, outer_measure'_eq m m0 @mU hs] end of_measurable namespace outer_measure variables {α : Type*} [measurable_space α] (m : outer_measure α) def trim : outer_measure α := outer_measure' (λ s _, m s) m.empty theorem trim_ge : m ≤ m.trim := λ s, le_infi $ λ f, le_infi $ λ hs, le_trans (m.mono hs) $ le_trans (m.Union_nat f) $ ennreal.tsum_le_tsum $ λ i, le_infi $ λ hf, le_refl _ theorem trim_eq {s : set α} (hs : is_measurable s) : m.trim s = m s := le_antisymm (le_trans (of_function_le _ _ _) (infi_le _ hs)) (trim_ge _ _) theorem trim_congr {m₁ m₂ : outer_measure α} (H : ∀ {s : set α}, is_measurable s → m₁ s = m₂ s) : m₁.trim = m₂.trim := by unfold trim; congr; funext s hs; exact H hs theorem trim_le_trim {m₁ m₂ : outer_measure α} (H : m₁ ≤ m₂) : m₁.trim ≤ m₂.trim := λ s, infi_le_infi $ λ f, infi_le_infi $ λ hs, ennreal.tsum_le_tsum $ λ b, infi_le_infi $ λ hf, H _ theorem le_trim_iff {m₁ m₂ : outer_measure α} : m₁ ≤ m₂.trim ↔ ∀ s, is_measurable s → m₁ s ≤ m₂ s := le_of_function.trans $ forall_congr $ λ s, le_infi_iff theorem trim_eq_infi (s : set α) : m.trim s = ⨅ t (st : s ⊆ t) (ht : is_measurable t), m t := begin refine le_antisymm (le_infi $ λ t, le_infi $ λ st, le_infi $ λ ht, _) (le_infi $ λ f, le_infi $ λ hf, _), { rw ← trim_eq m ht, exact (trim m).mono st }, { by_cases h : ∀i, is_measurable (f i), { refine infi_le_of_le _ (infi_le_of_le hf $ infi_le_of_le (is_measurable.Union h) _), rw congr_arg tsum _, {exact m.Union_nat _}, funext i, exact measure'_eq _ _ (h i) }, { cases not_forall.1 h with i hi, exact le_trans (le_infi $ λ h, hi.elim h) (ennreal.le_tsum i) } } end theorem trim_eq_infi' (s : set α) : m.trim s = ⨅ t : {t // s ⊆ t ∧ is_measurable t}, m t.1 := by simp [infi_subtype, infi_and, trim_eq_infi] theorem trim_trim (m : outer_measure α) : m.trim.trim = m.trim := le_antisymm (le_trim_iff.2 $ λ s hs, by simp [trim_eq _ hs, le_refl]) (trim_ge _) theorem trim_zero : (0 : outer_measure α).trim = 0 := ext $ λ s, le_antisymm (le_trans ((trim 0).mono (subset_univ s)) $ le_of_eq $ trim_eq _ is_measurable.univ) (zero_le _) theorem trim_add (m₁ m₂ : outer_measure α) : (m₁ + m₂).trim = m₁.trim + m₂.trim := ext $ λ s, begin simp [trim_eq_infi'], rw ennreal.infi_add_infi, rintro ⟨t₁, st₁, ht₁⟩ ⟨t₂, st₂, ht₂⟩, exact ⟨⟨_, subset_inter_iff.2 ⟨st₁, st₂⟩, ht₁.inter ht₂⟩, add_le_add' (m₁.mono' (inter_subset_left _ _)) (m₂.mono' (inter_subset_right _ _))⟩, end theorem trim_sum_ge {ι} (m : ι → outer_measure α) : sum (λ i, (m i).trim) ≤ (sum m).trim := λ s, by simp [trim_eq_infi]; exact λ t st ht, ennreal.tsum_le_tsum (λ i, infi_le_of_le t $ infi_le_of_le st $ infi_le _ ht) end outer_measure structure measure (α : Type*) [measurable_space α] extends outer_measure α := (m_Union {f : ℕ → set α} : (∀i, is_measurable (f i)) → pairwise (disjoint on f) → measure_of (⋃i, f i) = (∑i, measure_of (f i))) (trimmed : to_outer_measure.trim = to_outer_measure) /-- Measure projections for a measure space. For measurable sets this returns the measure assigned by the `measure_of` field in `measure`. But we can extend this to _all_ sets, but using the outer measure. This gives us monotonicity and subadditivity for all sets. -/ instance measure.has_coe_to_fun {α} [measurable_space α] : has_coe_to_fun (measure α) := ⟨λ _, set α → ennreal, λ m, m.to_outer_measure⟩ namespace measure def of_measurable {α} [measurable_space α] (m : Π (s : set α), is_measurable s → ennreal) (m0 : m ∅ is_measurable.empty = 0) (mU : ∀ {f : ℕ → set α} (h : ∀i, is_measurable (f i)), pairwise (disjoint on f) → m (⋃i, f i) (is_measurable.Union h) = (∑i, m (f i) (h i))) : measure α := { m_Union := λ f hf hd, show outer_measure' m m0 (Union f) = ∑ i, outer_measure' m m0 (f i), begin rw [outer_measure'_eq m m0 @mU, mU hf hd], congr, funext n, rw outer_measure'_eq m m0 @mU end, trimmed := show (outer_measure' m m0).trim = outer_measure' m m0, begin unfold outer_measure.trim, congr, funext s hs, exact outer_measure'_eq m m0 @mU hs end, ..outer_measure' m m0 } lemma of_measurable_apply {α} [measurable_space α] {m : Π (s : set α), is_measurable s → ennreal} {m0 : m ∅ is_measurable.empty = 0} {mU : ∀ {f : ℕ → set α} (h : ∀i, is_measurable (f i)), pairwise (disjoint on f) → m (⋃i, f i) (is_measurable.Union h) = (∑i, m (f i) (h i))} (s : set α) (hs : is_measurable s) : of_measurable m m0 @mU s = m s hs := outer_measure'_eq m m0 @mU hs @[ext] lemma ext {α} [measurable_space α] : ∀ {μ₁ μ₂ : measure α}, (∀s, is_measurable s → μ₁ s = μ₂ s) → μ₁ = μ₂ | ⟨m₁, u₁, h₁⟩ ⟨m₂, u₂, h₂⟩ h := by congr; rw [← h₁, ← h₂]; exact outer_measure.trim_congr h end measure section variables {α : Type*} {β : Type*} [measurable_space α] {μ μ₁ μ₂ : measure α} {s s₁ s₂ : set α} @[simp] lemma to_outer_measure_apply (s) : μ.to_outer_measure s = μ s := rfl lemma measure_eq_trim (s) : μ s = μ.to_outer_measure.trim s := by rw μ.trimmed; refl lemma measure_eq_infi (s) : μ s = ⨅ t (st : s ⊆ t) (ht : is_measurable t), μ t := by rw [measure_eq_trim, outer_measure.trim_eq_infi]; refl lemma measure_eq_outer_measure' : μ s = outer_measure' (λ s _, μ s) μ.empty s := measure_eq_trim _ lemma to_outer_measure_eq_outer_measure' : μ.to_outer_measure = outer_measure' (λ s _, μ s) μ.empty := μ.trimmed.symm lemma measure_eq_measure' (hs : is_measurable s) : μ s = measure' (λ s _, μ s) μ.empty s := by rw [measure_eq_outer_measure', outer_measure'_eq_measure' (λ s _, μ s) _ μ.m_Union hs] @[simp] lemma measure_empty : μ ∅ = 0 := μ.empty lemma measure_mono (h : s₁ ⊆ s₂) : μ s₁ ≤ μ s₂ := μ.mono h lemma measure_mono_null (h : s₁ ⊆ s₂) (h₂ : μ s₂ = 0) : μ s₁ = 0 := by rw [← le_zero_iff_eq, ← h₂]; exact measure_mono h lemma exists_is_measurable_superset_of_measure_eq_zero {s : set α} (h : μ s = 0) : ∃t, s ⊆ t ∧ is_measurable t ∧ μ t = 0 := begin rw [measure_eq_infi] at h, have h := (infi_eq_bot _).1 h, choose t ht using show ∀n:ℕ, ∃t, s ⊆ t ∧ is_measurable t ∧ μ t < n⁻¹, { assume n, have : (0 : ennreal) < n⁻¹ := (zero_lt_iff_ne_zero.2 $ ennreal.inv_ne_zero.2 $ ennreal.nat_ne_top _), rcases h _ this with ⟨t, ht⟩, use [t], simpa [(>), infi_lt_iff, -add_comm] using ht }, refine ⟨⋂n, t n, subset_Inter (λn, (ht n).1), is_measurable.Inter (λn, (ht n).2.1), _⟩, refine eq_of_le_of_forall_le_of_dense bot_le (assume r hr, _), rcases ennreal.exists_inv_nat_lt (ne_of_gt hr) with ⟨n, hn⟩, calc μ (⋂n, t n) ≤ μ (t n) : measure_mono (Inter_subset _ _) ... ≤ n⁻¹ : le_of_lt (ht n).2.2 ... ≤ r : le_of_lt hn end theorem measure_Union_le {β} [encodable β] (s : β → set α) : μ (⋃i, s i) ≤ (∑i, μ (s i)) := μ.to_outer_measure.Union _ lemma measure_Union_null {β} [encodable β] {s : β → set α} : (∀ i, μ (s i) = 0) → μ (⋃i, s i) = 0 := μ.to_outer_measure.Union_null theorem measure_union_le (s₁ s₂ : set α) : μ (s₁ ∪ s₂) ≤ μ s₁ + μ s₂ := μ.to_outer_measure.union _ _ lemma measure_union_null {s₁ s₂ : set α} : μ s₁ = 0 → μ s₂ = 0 → μ (s₁ ∪ s₂) = 0 := μ.to_outer_measure.union_null lemma measure_Union {β} [encodable β] {f : β → set α} (hn : pairwise (disjoint on f)) (h : ∀i, is_measurable (f i)) : μ (⋃i, f i) = (∑i, μ (f i)) := by rw [measure_eq_measure' (is_measurable.Union h), measure'_Union (λ s _, μ s) _ μ.m_Union hn h]; simp [measure_eq_measure', h] lemma measure_union (hd : disjoint s₁ s₂) (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ := by rw [measure_eq_measure' (h₁.union h₂), measure'_union (λ s _, μ s) _ μ.m_Union hd h₁ h₂]; simp [measure_eq_measure', h₁, h₂] lemma measure_bUnion {s : set β} {f : β → set α} (hs : countable s) (hd : pairwise_on s (disjoint on f)) (h : ∀b∈s, is_measurable (f b)) : μ (⋃b∈s, f b) = ∑p:s, μ (f p.1) := begin haveI := hs.to_encodable, rw [← measure_Union, bUnion_eq_Union], { rintro ⟨i, hi⟩ ⟨j, hj⟩ ij x ⟨h₁, h₂⟩, exact hd i hi j hj (mt subtype.eq' ij:_) ⟨h₁, h₂⟩ }, { simpa } end lemma measure_sUnion {S : set (set α)} (hs : countable S) (hd : pairwise_on S disjoint) (h : ∀s∈S, is_measurable s) : μ (⋃₀ S) = ∑s:S, μ s.1 := by rw [sUnion_eq_bUnion, measure_bUnion hs hd h] lemma measure_diff {s₁ s₂ : set α} (h : s₂ ⊆ s₁) (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) (h_fin : μ s₂ < ⊤) : μ (s₁ \ s₂) = μ s₁ - μ s₂ := begin refine (ennreal.add_sub_self' h_fin).symm.trans _, rw [← measure_union disjoint_diff h₂ (h₁.diff h₂), union_diff_cancel h] end lemma measure_Union_eq_supr_nat {s : ℕ → set α} (h : ∀i, is_measurable (s i)) (hs : monotone s) : μ (⋃i, s i) = (⨆i, μ (s i)) := begin refine le_antisymm _ (supr_le $ λ i, measure_mono $ subset_Union _ _), rw [← Union_disjointed, measure_Union disjoint_disjointed (is_measurable.disjointed h), ennreal.tsum_eq_supr_nat], refine supr_le (λ n, _), cases n, {apply zero_le _}, suffices : sum (finset.range n.succ) (λ i, μ (disjointed s i)) = μ (s n), { rw this, exact le_supr _ n }, rw [← Union_disjointed_of_mono hs, measure_Union, tsum_eq_sum], { apply sum_congr rfl, intros i hi, simp [finset.mem_range.1 hi] }, { intros i hi, simp [mt finset.mem_range.2 hi] }, { rintro i j ij x ⟨⟨_, ⟨_, rfl⟩, h₁⟩, ⟨_, ⟨_, rfl⟩, h₂⟩⟩, exact disjoint_disjointed i j ij ⟨h₁, h₂⟩ }, { intro i, by_cases h' : i < n.succ; simp [h', is_measurable.empty], apply is_measurable.disjointed h } end lemma measure_Inter_eq_infi_nat {s : ℕ → set α} (h : ∀i, is_measurable (s i)) (hs : ∀i j, i ≤ j → s j ⊆ s i) (hfin : ∃i, μ (s i) < ⊤) : μ (⋂i, s i) = (⨅i, μ (s i)) := begin rcases hfin with ⟨k, hk⟩, rw [← ennreal.sub_sub_cancel (by exact hk) (infi_le _ k), ennreal.sub_infi, ← ennreal.sub_sub_cancel (by exact hk) (measure_mono (Inter_subset _ k)), ← measure_diff (Inter_subset _ k) (h k) (is_measurable.Inter h) (lt_of_le_of_lt (measure_mono (Inter_subset _ k)) hk), diff_Inter, measure_Union_eq_supr_nat], { congr, funext i, cases le_total k i with ik ik, { exact measure_diff (hs _ _ ik) (h k) (h i) (lt_of_le_of_lt (measure_mono (hs _ _ ik)) hk) }, { rw [diff_eq_empty.2 (hs _ _ ik), measure_empty, ennreal.sub_eq_zero_of_le (measure_mono (hs _ _ ik))] } }, { exact λ i, (h k).diff (h i) }, { exact λ i j ij, diff_subset_diff_right (hs _ _ ij) } end lemma measure_eq_inter_diff {μ : measure α} {s t : set α} (hs : is_measurable s) (ht : is_measurable t) : μ s = μ (s ∩ t) + μ (s \ t) := have hd : disjoint (s ∩ t) (s \ t) := assume a ⟨⟨_, hs⟩, _, hns⟩, hns hs , by rw [← measure_union hd (hs.inter ht) (hs.diff ht), inter_union_diff s t] lemma tendsto_measure_Union {μ : measure α} {s : ℕ → set α} (hs : ∀n, is_measurable (s n)) (hm : monotone s) : tendsto (μ ∘ s) at_top (𝓝 (μ (⋃n, s n))) := begin rw measure_Union_eq_supr_nat hs hm, exact tendsto_at_top_supr_nat (μ ∘ s) (assume n m hnm, measure_mono $ hm $ hnm) end lemma tendsto_measure_Inter {μ : measure α} {s : ℕ → set α} (hs : ∀n, is_measurable (s n)) (hm : ∀n m, n ≤ m → s m ⊆ s n) (hf : ∃i, μ (s i) < ⊤) : tendsto (μ ∘ s) at_top (𝓝 (μ (⋂n, s n))) := begin rw measure_Inter_eq_infi_nat hs hm hf, exact tendsto_at_top_infi_nat (μ ∘ s) (assume n m hnm, measure_mono $ hm _ _ $ hnm), end end def outer_measure.to_measure {α} (m : outer_measure α) [ms : measurable_space α] (h : ms ≤ m.caratheodory) : measure α := measure.of_measurable (λ s _, m s) m.empty (λ f hf hd, m.Union_eq_of_caratheodory (λ i, h _ (hf i)) hd) lemma le_to_outer_measure_caratheodory {α} [ms : measurable_space α] (μ : measure α) : ms ≤ μ.to_outer_measure.caratheodory := begin assume s hs, rw to_outer_measure_eq_outer_measure', refine outer_measure.caratheodory_is_measurable (λ t, le_infi $ λ ht, _), rw [← measure_eq_measure' (ht.inter hs), ← measure_eq_measure' (ht.diff hs), ← measure_union _ (ht.inter hs) (ht.diff hs), inter_union_diff], exact le_refl _, exact λ x ⟨⟨_, h₁⟩, _, h₂⟩, h₂ h₁ end lemma to_measure_to_outer_measure {α} (m : outer_measure α) [ms : measurable_space α] (h : ms ≤ m.caratheodory) : (m.to_measure h).to_outer_measure = m.trim := rfl @[simp] lemma to_measure_apply {α} (m : outer_measure α) [ms : measurable_space α] (h : ms ≤ m.caratheodory) {s : set α} (hs : is_measurable s) : m.to_measure h s = m s := m.trim_eq hs lemma to_outer_measure_to_measure {α : Type*} [ms : measurable_space α] {μ : measure α} : μ.to_outer_measure.to_measure (le_to_outer_measure_caratheodory _) = μ := measure.ext $ λ s, μ.to_outer_measure.trim_eq namespace measure variables {α : Type*} {β : Type*} {γ : Type*} [measurable_space α] [measurable_space β] [measurable_space γ] instance : has_zero (measure α) := ⟨{ to_outer_measure := 0, m_Union := λ f hf hd, tsum_zero.symm, trimmed := outer_measure.trim_zero }⟩ @[simp] theorem zero_to_outer_measure : (0 : measure α).to_outer_measure = 0 := rfl @[simp] theorem zero_apply (s : set α) : (0 : measure α) s = 0 := rfl instance : inhabited (measure α) := ⟨0⟩ instance : has_add (measure α) := ⟨λμ₁ μ₂, { to_outer_measure := μ₁.to_outer_measure + μ₂.to_outer_measure, m_Union := λs hs hd, show μ₁ (⋃ i, s i) + μ₂ (⋃ i, s i) = ∑ i, μ₁ (s i) + μ₂ (s i), by rw [ennreal.tsum_add, measure_Union hd hs, measure_Union hd hs], trimmed := by rw [outer_measure.trim_add, μ₁.trimmed, μ₂.trimmed] }⟩ @[simp] theorem add_to_outer_measure (μ₁ μ₂ : measure α) : (μ₁ + μ₂).to_outer_measure = μ₁.to_outer_measure + μ₂.to_outer_measure := rfl @[simp] theorem add_apply (μ₁ μ₂ : measure α) (s : set α) : (μ₁ + μ₂) s = μ₁ s + μ₂ s := rfl instance : add_comm_monoid (measure α) := { zero := 0, add := (+), add_assoc := assume a b c, ext $ assume s hs, add_assoc _ _ _, add_comm := assume a b, ext $ assume s hs, add_comm _ _, zero_add := assume a, ext $ assume s hs, zero_add _, add_zero := assume a, ext $ assume s hs, add_zero _ } instance : partial_order (measure α) := { le := λm₁ m₂, ∀ s, is_measurable s → m₁ s ≤ m₂ s, le_refl := assume m s hs, le_refl _, le_trans := assume m₁ m₂ m₃ h₁ h₂ s hs, le_trans (h₁ s hs) (h₂ s hs), le_antisymm := assume m₁ m₂ h₁ h₂, ext $ assume s hs, le_antisymm (h₁ s hs) (h₂ s hs) } theorem le_iff {μ₁ μ₂ : measure α} : μ₁ ≤ μ₂ ↔ ∀ s, is_measurable s → μ₁ s ≤ μ₂ s := iff.rfl theorem to_outer_measure_le {μ₁ μ₂ : measure α} : μ₁.to_outer_measure ≤ μ₂.to_outer_measure ↔ μ₁ ≤ μ₂ := by rw [← μ₂.trimmed, outer_measure.le_trim_iff]; refl theorem le_iff' {μ₁ μ₂ : measure α} : μ₁ ≤ μ₂ ↔ ∀ s, μ₁ s ≤ μ₂ s := to_outer_measure_le.symm section variables {m : set (measure α)} {μ : measure α} lemma Inf_caratheodory (s : set α) (hs : is_measurable s) : (Inf (measure.to_outer_measure '' m)).caratheodory.is_measurable s := begin rw [outer_measure.Inf_eq_of_function_Inf_gen], refine outer_measure.caratheodory_is_measurable (assume t, _), by_cases ht : t = ∅, { simp [ht] }, simp only [outer_measure.Inf_gen_nonempty1 _ _ ht, le_infi_iff, ball_image_iff, to_outer_measure_apply, measure_eq_infi t], assume μ hμ u htu hu, have hm : ∀{s t}, s ⊆ t → outer_measure.Inf_gen (to_outer_measure '' m) s ≤ μ t, { assume s t hst, rw [outer_measure.Inf_gen_nonempty2 _ _ (mem_image_of_mem _ hμ)], refine infi_le_of_le (μ.to_outer_measure) (infi_le_of_le (mem_image_of_mem _ hμ) _), rw [to_outer_measure_apply], refine measure_mono hst }, rw [measure_eq_inter_diff hu hs], refine add_le_add' (hm $ inter_subset_inter_left _ htu) (hm $ diff_subset_diff_left htu) end instance : has_Inf (measure α) := ⟨λm, (Inf (to_outer_measure '' m)).to_measure $ Inf_caratheodory⟩ lemma Inf_apply {m : set (measure α)} {s : set α} (hs : is_measurable s) : Inf m s = Inf (to_outer_measure '' m) s := to_measure_apply _ _ hs private lemma Inf_le (h : μ ∈ m) : Inf m ≤ μ := have Inf (to_outer_measure '' m) ≤ μ.to_outer_measure := Inf_le (mem_image_of_mem _ h), assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s private lemma le_Inf (h : ∀μ' ∈ m, μ ≤ μ') : μ ≤ Inf m := have μ.to_outer_measure ≤ Inf (to_outer_measure '' m) := le_Inf $ ball_image_of_ball $ assume μ hμ, to_outer_measure_le.2 $ h _ hμ, assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s instance : has_Sup (measure α) := ⟨λs, Inf {μ' | ∀μ∈s, μ ≤ μ' }⟩ private lemma le_Sup (h : μ ∈ m) : μ ≤ Sup m := le_Inf $ assume μ' h', h' _ h private lemma Sup_le (h : ∀μ' ∈ m, μ' ≤ μ) : Sup m ≤ μ := Inf_le h instance : order_bot (measure α) := { bot := 0, bot_le := assume a s hs, bot_le, .. measure.partial_order } instance : order_top (measure α) := { top := (⊤ : outer_measure α).to_measure (by rw [outer_measure.top_caratheodory]; exact le_top), le_top := assume a s hs, by by_cases s = ∅; simp [h, to_measure_apply ⊤ _ hs, outer_measure.top_apply], .. measure.partial_order } instance : complete_lattice (measure α) := { Inf := Inf, Sup := Sup, inf := λa b, Inf {a, b}, sup := λa b, Sup {a, b}, le_Sup := assume s μ h, le_Sup h, Sup_le := assume s μ h, Sup_le h, Inf_le := assume s μ h, Inf_le h, le_Inf := assume s μ h, le_Inf h, le_sup_left := assume a b, le_Sup $ by simp, le_sup_right := assume a b, le_Sup $ by simp, sup_le := assume a b c hac hbc, Sup_le $ by simp [*, or_imp_distrib] {contextual := tt}, inf_le_left := assume a b, Inf_le $ by simp, inf_le_right := assume a b, Inf_le $ by simp, le_inf := assume a b c hac hbc, le_Inf $ by simp [*, or_imp_distrib] {contextual := tt}, .. measure.partial_order, .. measure.lattice.order_top, .. measure.lattice.order_bot } end def map (f : α → β) (μ : measure α) : measure β := if hf : measurable f then (μ.to_outer_measure.map f).to_measure $ λ s hs t, le_to_outer_measure_caratheodory μ _ (hf _ hs) (f ⁻¹' t) else 0 variables {μ ν : measure α} @[simp] theorem map_apply {f : α → β} (hf : measurable f) {s : set β} (hs : is_measurable s) : (map f μ : measure β) s = μ (f ⁻¹' s) := by rw [map, dif_pos hf, to_measure_apply _ _ hs]; refl @[simp] lemma map_id : map id μ = μ := ext $ λ s, map_apply measurable_id lemma map_map {g : β → γ} {f : α → β} (hg : measurable g) (hf : measurable f) : map g (map f μ) = map (g ∘ f) μ := ext $ λ s hs, by simp [hf, hg, hs, hg.preimage hs, hg.comp hf]; rw ← preimage_comp /-- The dirac measure. -/ def dirac (a : α) : measure α := (outer_measure.dirac a).to_measure (by simp) @[simp] lemma dirac_apply (a : α) {s : set α} (hs : is_measurable s) : (dirac a : measure α) s = ⨆ h : a ∈ s, 1 := to_measure_apply _ _ hs /-- Sum of an indexed family of measures. -/ def sum {ι : Type*} (f : ι → measure α) : measure α := (outer_measure.sum (λ i, (f i).to_outer_measure)).to_measure $ le_trans (by exact le_infi (λ i, le_to_outer_measure_caratheodory _)) (outer_measure.le_sum_caratheodory _) /-- Counting measure on any measurable space. -/ def count : measure α := sum dirac @[class] def is_complete {α} {_:measurable_space α} (μ : measure α) : Prop := ∀ s, μ s = 0 → is_measurable s /-- The "almost everywhere" filter of co-null sets. -/ def a_e (μ : measure α) : filter α := { sets := {s | μ (-s) = 0}, univ_sets := by simp [measure_empty], inter_sets := λ s t hs ht, by simp [compl_inter]; exact measure_union_null hs ht, sets_of_superset := λ s t hs hst, measure_mono_null (set.compl_subset_compl.2 hst) hs } lemma mem_a_e_iff (s : set α) : s ∈ μ.a_e.sets ↔ μ (- s) = 0 := iff.rfl end measure end measure_theory section is_complete open measure_theory variables {α : Type*} [measurable_space α] (μ : measure α) def is_null_measurable (s : set α) : Prop := ∃ t z, s = t ∪ z ∧ is_measurable t ∧ μ z = 0 theorem is_null_measurable_iff {μ : measure α} {s : set α} : is_null_measurable μ s ↔ ∃ t, t ⊆ s ∧ is_measurable t ∧ μ (s \ t) = 0 := begin split, { rintro ⟨t, z, rfl, ht, hz⟩, refine ⟨t, set.subset_union_left _ _, ht, measure_mono_null _ hz⟩, simp [union_diff_left, diff_subset] }, { rintro ⟨t, st, ht, hz⟩, exact ⟨t, _, (union_diff_cancel st).symm, ht, hz⟩ } end theorem is_null_measurable_measure_eq {μ : measure α} {s t : set α} (st : t ⊆ s) (hz : μ (s \ t) = 0) : μ s = μ t := begin refine le_antisymm _ (measure_mono st), have := measure_union_le t (s \ t), rw [union_diff_cancel st, hz] at this, simpa end theorem is_measurable.is_null_measurable {s : set α} (hs : is_measurable s) : is_null_measurable μ s := ⟨s, ∅, by simp, hs, μ.empty⟩ theorem is_null_measurable_of_complete [c : μ.is_complete] {s : set α} : is_null_measurable μ s ↔ is_measurable s := ⟨by rintro ⟨t, z, rfl, ht, hz⟩; exact is_measurable.union ht (c _ hz), λ h, h.is_null_measurable _⟩ variables {μ} theorem is_null_measurable.union_null {s z : set α} (hs : is_null_measurable μ s) (hz : μ z = 0) : is_null_measurable μ (s ∪ z) := begin rcases hs with ⟨t, z', rfl, ht, hz'⟩, exact ⟨t, z' ∪ z, set.union_assoc _ _ _, ht, le_zero_iff_eq.1 (le_trans (measure_union_le _ _) $ by simp [hz, hz'])⟩ end theorem null_is_null_measurable {z : set α} (hz : μ z = 0) : is_null_measurable μ z := by simpa using (is_measurable.empty.is_null_measurable _).union_null hz theorem is_null_measurable.Union_nat {s : ℕ → set α} (hs : ∀ i, is_null_measurable μ (s i)) : is_null_measurable μ (Union s) := begin choose t ht using assume i, is_null_measurable_iff.1 (hs i), simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩, refine is_null_measurable_iff.2 ⟨Union t, Union_subset_Union st, is_measurable.Union ht, measure_mono_null _ (measure_Union_null hz)⟩, rw [diff_subset_iff, ← Union_union_distrib], exact Union_subset_Union (λ i, by rw ← diff_subset_iff) end theorem is_measurable.diff_null {s z : set α} (hs : is_measurable s) (hz : μ z = 0) : is_null_measurable μ (s \ z) := begin rw measure_eq_infi at hz, choose f hf using show ∀ q : {q:ℚ//q>0}, ∃ t:set α, z ⊆ t ∧ is_measurable t ∧ μ t < (nnreal.of_real q.1 : ennreal), { rintro ⟨ε, ε0⟩, have : 0 < (nnreal.of_real ε : ennreal), { simpa using ε0 }, rw ← hz at this, simpa [infi_lt_iff] }, refine is_null_measurable_iff.2 ⟨s \ Inter f, diff_subset_diff_right (subset_Inter (λ i, (hf i).1)), hs.diff (is_measurable.Inter (λ i, (hf i).2.1)), measure_mono_null _ (le_zero_iff_eq.1 $ le_of_not_lt $ λ h, _)⟩, { exact Inter f }, { rw [diff_subset_iff, diff_union_self], exact subset.trans (diff_subset _ _) (subset_union_left _ _) }, rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨ε, ε0', ε0, h⟩, simp at ε0, apply not_le_of_lt (lt_trans (hf ⟨ε, ε0⟩).2.2 h), exact measure_mono (Inter_subset _ _) end theorem is_null_measurable.diff_null {s z : set α} (hs : is_null_measurable μ s) (hz : μ z = 0) : is_null_measurable μ (s \ z) := begin rcases hs with ⟨t, z', rfl, ht, hz'⟩, rw [set.union_diff_distrib], exact (ht.diff_null hz).union_null (measure_mono_null (diff_subset _ _) hz') end theorem is_null_measurable.compl {s : set α} (hs : is_null_measurable μ s) : is_null_measurable μ (-s) := begin rcases hs with ⟨t, z, rfl, ht, hz⟩, rw compl_union, exact ht.compl.diff_null hz end def null_measurable {α : Type u} [measurable_space α] (μ : measure α) : measurable_space α := { is_measurable := is_null_measurable μ, is_measurable_empty := is_measurable.empty.is_null_measurable _, is_measurable_compl := λ s hs, hs.compl, is_measurable_Union := λ f, is_null_measurable.Union_nat } def completion {α : Type u} [measurable_space α] (μ : measure α) : @measure_theory.measure α (null_measurable μ) := { to_outer_measure := μ.to_outer_measure, m_Union := λ s hs hd, show μ (Union s) = ∑ i, μ (s i), begin choose t ht using assume i, is_null_measurable_iff.1 (hs i), simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩, rw is_null_measurable_measure_eq (Union_subset_Union st), { rw measure_Union _ ht, { congr, funext i, exact (is_null_measurable_measure_eq (st i) (hz i)).symm }, { rintro i j ij x ⟨h₁, h₂⟩, exact hd i j ij ⟨st i h₁, st j h₂⟩ } }, { refine measure_mono_null _ (measure_Union_null hz), rw [diff_subset_iff, ← Union_union_distrib], exact Union_subset_Union (λ i, by rw ← diff_subset_iff) } end, trimmed := begin letI := null_measurable μ, refine le_antisymm (λ s, _) (outer_measure.trim_ge _), rw outer_measure.trim_eq_infi, dsimp, clear _inst, rw measure_eq_infi s, exact infi_le_infi (λ t, infi_le_infi $ λ st, infi_le_infi2 $ λ ht, ⟨ht.is_null_measurable _, le_refl _⟩) end } instance completion.is_complete {α : Type u} [measurable_space α] (μ : measure α) : (completion μ).is_complete := λ z hz, null_is_null_measurable hz end is_complete namespace measure_theory /-- A measure space is a measurable space equipped with a measure, referred to as `volume`. -/ class measure_space (α : Type*) extends measurable_space α := (μ {} : measure α) section measure_space variables {α : Type*} [measure_space α] {s₁ s₂ : set α} open measure_space def volume : set α → ennreal := @μ α _ @[simp] lemma volume_empty : volume (∅ : set α) = 0 := μ.empty lemma volume_mono : s₁ ⊆ s₂ → volume s₁ ≤ volume s₂ := measure_mono lemma volume_mono_null : s₁ ⊆ s₂ → volume s₂ = 0 → volume s₁ = 0 := measure_mono_null theorem volume_Union_le {β} [encodable β] : ∀ (s : β → set α), volume (⋃i, s i) ≤ (∑i, volume (s i)) := measure_Union_le lemma volume_Union_null {β} [encodable β] {s : β → set α} : (∀ i, volume (s i) = 0) → volume (⋃i, s i) = 0 := measure_Union_null theorem volume_union_le : ∀ (s₁ s₂ : set α), volume (s₁ ∪ s₂) ≤ volume s₁ + volume s₂ := measure_union_le lemma volume_union_null : volume s₁ = 0 → volume s₂ = 0 → volume (s₁ ∪ s₂) = 0 := measure_union_null lemma volume_Union {β} [encodable β] {f : β → set α} : pairwise (disjoint on f) → (∀i, is_measurable (f i)) → volume (⋃i, f i) = (∑i, volume (f i)) := measure_Union lemma volume_union : disjoint s₁ s₂ → is_measurable s₁ → is_measurable s₂ → volume (s₁ ∪ s₂) = volume s₁ + volume s₂ := measure_union lemma volume_bUnion {β} {s : set β} {f : β → set α} : countable s → pairwise_on s (disjoint on f) → (∀b∈s, is_measurable (f b)) → volume (⋃b∈s, f b) = ∑p:s, volume (f p.1) := measure_bUnion lemma volume_sUnion {S : set (set α)} : countable S → pairwise_on S disjoint → (∀s∈S, is_measurable s) → volume (⋃₀ S) = ∑s:S, volume s.1 := measure_sUnion lemma volume_bUnion_finset {β} {s : finset β} {f : β → set α} (hd : pairwise_on ↑s (disjoint on f)) (hm : ∀b∈s, is_measurable (f b)) : volume (⋃b∈s, f b) = s.sum (λp, volume (f p)) := show volume (⋃b∈(↑s : set β), f b) = s.sum (λp, volume (f p)), begin rw [volume_bUnion (countable_finite (finset.finite_to_set s)) hd hm, tsum_eq_sum], { show s.attach.sum (λb:(↑s : set β), volume (f b)) = s.sum (λb, volume (f b)), exact @finset.sum_attach _ _ s _ (λb, volume (f b)) }, simp end lemma volume_diff : s₂ ⊆ s₁ → is_measurable s₁ → is_measurable s₂ → volume s₂ < ⊤ → volume (s₁ \ s₂) = volume s₁ - volume s₂ := measure_diff /-- `∀ₘ a:α, p a` states that the property `p` is almost everywhere true in the measure space associated with `α`. This means that the measure of the complementary of `p` is `0`. In a probability measure, the measure of `p` is `1`, when `p` is measurable. -/ def all_ae (p : α → Prop) : Prop := { a | p a } ∈ (@measure_space.μ α _).a_e notation `∀ₘ` binders `, ` r:(scoped P, all_ae P) := r lemma all_ae_congr {p q : α → Prop} (h : ∀ₘ a, p a ↔ q a) : (∀ₘ a, p a) ↔ (∀ₘ a, q a) := iff.intro (assume h', by filter_upwards [h, h'] assume a hpq hp, hpq.1 hp) (assume h', by filter_upwards [h, h'] assume a hpq hq, hpq.2 hq) lemma all_ae_iff {p : α → Prop} : (∀ₘ a, p a) ↔ volume { a | ¬ p a } = 0 := iff.rfl lemma all_ae_of_all {p : α → Prop} : (∀a, p a) → ∀ₘ a, p a := assume h, by {rw all_ae_iff, convert volume_empty, simp only [h, not_true], reflexivity} lemma all_ae_all_iff {ι : Type*} [encodable ι] {p : α → ι → Prop} : (∀ₘ a, ∀i, p a i) ↔ (∀i, ∀ₘ a, p a i) := begin refine iff.intro (assume h i, _) (assume h, _), { filter_upwards [h] assume a ha, ha i }, { have h := measure_Union_null h, rw [← compl_Inter] at h, filter_upwards [h] assume a, mem_Inter.1 } end variables {β : Type*} lemma all_ae_eq_refl (f : α → β) : ∀ₘ a, f a = f a := by { filter_upwards [], assume a, apply eq.refl } lemma all_ae_eq_symm {f g : α → β} : (∀ₘ a, f a = g a) → (∀ₘ a, g a = f a) := by { assume h, filter_upwards [h], assume a, apply eq.symm } lemma all_ae_eq_trans {f g h: α → β} (h₁ : ∀ₘ a, f a = g a) (h₂ : ∀ₘ a, g a = h a) : ∀ₘ a, f a = h a := by { filter_upwards [h₁, h₂], intro a, exact eq.trans } end measure_space end measure_theory
873de540922ee4680f323cbabca113695cd70c24
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/pack_unpack3.lean
1d7e15e8d21b402655b3240efe53c965cda0991e
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
1,002
lean
inductive {u} vec (A : Type u) : nat -> Type u | vnil : vec 0 | vcons : Pi (n : nat), A -> vec n -> vec (n+1) inductive {u} tree (A : Type u) : Type u | leaf : A -> tree | node : Pi (n : nat), vec (list (list tree)) n -> tree -- set_option trace.eqn_compiler true constant {u} P {A : Type u} : tree A → Type constant {u} mk1 {A : Type u} (a : A) : P (tree.leaf a) constant {u} mk2 {A : Type u} (n : nat) (xs : vec (list (list (tree A))) n) : P (tree.node n xs) noncomputable definition {u} bla {A : Type u} : ∀ n : tree A, P n | (tree.leaf a) := mk1 a | (tree.node n xs) := mk2 n xs #check bla._main.equations._eqn_1 #check bla._main.equations._eqn_2 definition {u} foo {A : Type u} : nat → tree A → nat | 0 _ := sorry | (n+1) (tree.leaf a) := 0 | (n+1) (tree.node m xs) := foo n (tree.node m xs) #check @foo._main.equations._eqn_1 #check @foo._main.equations._eqn_2 #check @foo._main.equations._eqn_3
b0de00b0b259b4c258e0f35736c1d2b297e1bc14
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/data/set/lattice.lean
a29d0eb26cd68881c3b719f88da9e2c273ef1544
[ "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
72,994
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**. Union 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.Union_eq_sigma_of_disjoint`: Equivalence between `⋃ i, t i` and `Σ i, t i`, where `t` is an indexed family of disjoint sets. ## Naming convention In lemma names, * `⋃ i, s i` is called `Union` * `⋂ i, s i` is called `Inter` * `⋃ i j, s i j` is called `Union₂`. This is a `Union` inside a `Union`. * `⋂ i j, s i j` is called `Inter₂`. This is an `Inter` inside an `Inter`. * `⋃ i ∈ s, t i` is called `bUnion` for "bounded `Union`". This is the special case of `Union₂` where `j : i ∈ s`. * `⋂ i ∈ s, t i` is called `bInter` for "bounded `Inter`". This is the special case of `Inter₂` where `j : i ∈ s`. ## Notation * `⋃`: `set.Union` * `⋂`: `set.Inter` * `⋃₀`: `set.sUnion` * `⋂₀`: `set.sInter` -/ open function tactic set universes u variables {α β γ : Type*} {ι ι' ι₂ : Sort*} {κ κ₁ κ₂ : ι → Sort*} {κ' : ι' → Sort*} namespace set /-! ### Complete lattice and complete Boolean algebra instances -/ instance : has_Inf (set α) := ⟨λ s, {a | ∀ t ∈ s, a ∈ t}⟩ instance : has_Sup (set α) := ⟨λ s, {a | ∃ t ∈ s, a ∈ t}⟩ /-- Intersection of a set of sets. -/ def sInter (S : set (set α)) : set α := Inf S /-- Union of a set of sets. -/ def sUnion (S : set (set α)) : set α := Sup S prefix `⋂₀ `:110 := sInter prefix `⋃₀ `:110 := sUnion @[simp] theorem mem_sInter {x : α} {S : set (set α)} : x ∈ ⋂₀ S ↔ ∀ t ∈ S, x ∈ t := iff.rfl @[simp] theorem mem_sUnion {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] lemma mem_Union {x : α} {s : ι → set α} : x ∈ (⋃ i, s i) ↔ ∃ 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] lemma mem_Inter {x : α} {s : ι → set α} : x ∈ (⋂ i, s i) ↔ ∀ 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⟩ lemma mem_Union₂ {x : γ} {s : Π i, κ i → set γ} : x ∈ (⋃ i j, s i j) ↔ ∃ i j, x ∈ s i j := by simp_rw mem_Union lemma mem_Inter₂ {x : γ} {s : Π i, κ i → set γ} : x ∈ (⋂ i j, s i j) ↔ ∀ i j, x ∈ s i j := by simp_rw mem_Inter lemma mem_Union_of_mem {s : ι → set α} {a : α} (i : ι) (ha : a ∈ s i) : a ∈ ⋃ i, s i := mem_Union.2 ⟨i, ha⟩ lemma mem_Union₂_of_mem {s : Π i, κ i → set α} {a : α} {i : ι} (j : κ i) (ha : a ∈ s i j) : a ∈ ⋃ i j, s i j := mem_Union₂.2 ⟨i, j, ha⟩ lemma mem_Inter_of_mem {s : ι → set α} {a : α} (h : ∀ i, a ∈ s i) : a ∈ ⋂ i, s i := mem_Inter.2 h lemma mem_Inter₂_of_mem {s : Π i, κ i → set α} {a : α} (h : ∀ i j, a ∈ s i j) : a ∈ ⋂ i j, s i j := mem_Inter₂.2 h 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 } /-- `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 _root_.monotone.inter [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λ x, f x ∩ g x) := hf.inf hg theorem _root_.monotone_on.inter [preorder β] {f g : β → set α} {s : set β} (hf : monotone_on f s) (hg : monotone_on g s) : monotone_on (λ x, f x ∩ g x) s := hf.inf hg theorem _root_.antitone.inter [preorder β] {f g : β → set α} (hf : antitone f) (hg : antitone g) : antitone (λ x, f x ∩ g x) := hf.inf hg theorem _root_.antitone_on.inter [preorder β] {f g : β → set α} {s : set β} (hf : antitone_on f s) (hg : antitone_on g s) : antitone_on (λ x, f x ∩ g x) s := hf.inf hg theorem _root_.monotone.union [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λ x, f x ∪ g x) := hf.sup hg theorem _root_.monotone_on.union [preorder β] {f g : β → set α} {s : set β} (hf : monotone_on f s) (hg : monotone_on g s) : monotone_on (λ x, f x ∪ g x) s := hf.sup hg theorem _root_.antitone.union [preorder β] {f g : β → set α} (hf : antitone f) (hg : antitone g) : antitone (λ x, f x ∪ g x) := hf.sup hg theorem _root_.antitone_on.union [preorder β] {f g : β → set α} {s : set β} (hf : antitone_on f s) (hg : antitone_on g s) : antitone_on (λ x, f x ∪ g x) s := hf.sup hg 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 theorem antitone_set_of [preorder α] {p : α → β → Prop} (hp : ∀ b, antitone (λ a, p a b)) : antitone (λ a, {b | p a b}) := λ a a' h b, hp b h /-- Quantifying over a set is antitone in the set -/ lemma antitone_bforall {P : α → Prop} : antitone (λ s : set α, ∀ x ∈ s, P x) := λ s t hst h x hx, h x $ hst hx 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 -/ instance : order_top (set α) := { top := univ, le_top := by simp } @[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 lemma Union_subset {s : ι → set α} {t : set α} (h : ∀ i, s i ⊆ t) : (⋃ i, s i) ⊆ t := @supr_le (set α) _ _ _ _ h lemma Union₂_subset {s : Π i, κ i → set α} {t : set α} (h : ∀ i j, s i j ⊆ t) : (⋃ i j, s i j) ⊆ t := Union_subset $ λ x, Union_subset (h x) theorem subset_Inter {t : set β} {s : ι → set β} (h : ∀ i, t ⊆ s i) : t ⊆ ⋂ i, s i := @le_infi (set β) _ _ _ _ h lemma subset_Inter₂ {s : set α} {t : Π i, κ i → set α} (h : ∀ i j, s ⊆ t i j) : s ⊆ ⋂ i j, t i j := subset_Inter $ λ x, subset_Inter $ h x @[simp] lemma 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⟩ lemma Union₂_subset_iff {s : Π i, κ i → set α} {t : set α} : (⋃ i j, s i j) ⊆ t ↔ ∀ i j, s i j ⊆ t := by simp_rw Union_subset_iff @[simp] lemma subset_Inter_iff {s : set α} {t : ι → set α} : s ⊆ (⋂ i, t i) ↔ ∀ i, s ⊆ t i := @le_infi_iff (set α) _ _ _ _ @[simp] lemma subset_Inter₂_iff {s : set α} {t : Π i, κ i → set α} : s ⊆ (⋂ i j, t i j) ↔ ∀ i j, s ⊆ t i j := by simp_rw subset_Inter_iff lemma subset_Union : ∀ (s : ι → set β) (i : ι), s i ⊆ ⋃ i, s i := le_supr lemma Inter_subset : ∀ (s : ι → set β) (i : ι), (⋂ i, s i) ⊆ s i := infi_le lemma subset_Union₂ {s : Π i, κ i → set α} (i : ι) (j : κ i) : s i j ⊆ ⋃ i j, s i j := @le_supr₂ (set α) _ _ _ _ i j lemma Inter₂_subset {s : Π i, κ i → set α} (i : ι) (j : κ i) : (⋂ i j, s i j) ⊆ s i j := @infi₂_le (set α) _ _ _ _ i j /-- This rather trivial consequence of `subset_Union`is convenient with `apply`, and has `i` explicit for this purpose. -/ lemma subset_Union_of_subset {s : set α} {t : ι → set α} (i : ι) (h : s ⊆ t i) : s ⊆ ⋃ i, t i := @le_supr_of_le (set α) _ _ _ _ i h /-- This rather trivial consequence of `Inter_subset`is convenient with `apply`, and has `i` explicit for this purpose. -/ lemma Inter_subset_of_subset {s : ι → set α} {t : set α} (i : ι) (h : s i ⊆ t) : (⋂ i, s i) ⊆ t := @infi_le_of_le (set α) _ _ _ _ i h lemma Union_mono {s t : ι → set α} (h : ∀ i, s i ⊆ t i) : (⋃ i, s i) ⊆ ⋃ i, t i := @supr_mono (set α) _ _ s t h lemma Union₂_mono {s t : Π i, κ i → set α} (h : ∀ i j, s i j ⊆ t i j) : (⋃ i j, s i j) ⊆ ⋃ i j, t i j := @supr₂_mono (set α) _ _ _ s t h lemma Inter_mono {s t : ι → set α} (h : ∀ i, s i ⊆ t i) : (⋂ i, s i) ⊆ ⋂ i, t i := @infi_mono (set α) _ _ s t h lemma Inter₂_mono {s t : Π i, κ i → set α} (h : ∀ i j, s i j ⊆ t i j) : (⋂ i j, s i j) ⊆ ⋂ i j, t i j := @infi₂_mono (set α) _ _ _ s t h lemma Union_mono' {s : ι → set α} {t : ι₂ → set α} (h : ∀ i, ∃ j, s i ⊆ t j) : (⋃ i, s i) ⊆ ⋃ i, t i := @supr_mono' (set α) _ _ _ s t h lemma Union₂_mono' {s : Π i, κ i → set α} {t : Π i', κ' i' → set α} (h : ∀ i j, ∃ i' j', s i j ⊆ t i' j') : (⋃ i j, s i j) ⊆ ⋃ i' j', t i' j' := @supr₂_mono' (set α) _ _ _ _ _ s t h lemma Inter_mono' {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₂_mono' {s : Π i, κ i → set α} {t : Π i', κ' i' → set α} (h : ∀ i' j', ∃ i j, s i j ⊆ t i' j') : (⋂ i j, s i j) ⊆ ⋂ i' j', t i' j' := subset_Inter₂_iff.2 $ λ i' j', let ⟨i, j, hst⟩ := h i' j' in (Inter₂_subset _ _).trans hst lemma Union₂_subset_Union (κ : ι → Sort*) (s : ι → set α) : (⋃ i (j : κ i), s i) ⊆ ⋃ i, s i := Union_mono $ λ i, Union_subset $ λ h, subset.rfl lemma Inter_subset_Inter₂ (κ : ι → Sort*) (s : ι → set α) : (⋂ i, s i) ⊆ ⋂ i (j : κ i), s i := Inter_mono $ λ i, subset_Inter $ λ h, subset.rfl lemma Union_set_of (P : ι → α → Prop) : (⋃ i, {x : α | P i x}) = {x : α | ∃ i, P i x} := by { ext, exact mem_Union } lemma Inter_set_of (P : ι → α → Prop) : (⋂ i, {x : α | P i x}) = {x : α | ∀ i, P i x} := by { ext, exact mem_Inter } lemma Union_congr_of_surjective {f : ι → set α} {g : ι₂ → set α} (h : ι → ι₂) (h1 : surjective h) (h2 : ∀ x, g (h x) = f x) : (⋃ x, f x) = ⋃ y, g y := h1.supr_congr h h2 lemma Inter_congr_of_surjective {f : ι → set α} {g : ι₂ → set α} (h : ι → ι₂) (h1 : surjective h) (h2 : ∀ x, g (h x) = f x) : (⋂ x, f x) = ⋂ y, g y := h1.infi_congr h 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 lemma compl_Union₂ (s : Π i, κ i → set α) : (⋃ i j, s i j)ᶜ = ⋂ i j, (s i j)ᶜ := by simp_rw compl_Union @[simp] theorem compl_Inter (s : ι → set β) : (⋂ i, s i)ᶜ = (⋃ i, (s i)ᶜ) := compl_infi lemma compl_Inter₂ (s : Π i, κ i → set α) : (⋂ i j, s i j)ᶜ = ⋃ i j, (s i j)ᶜ := by simp_rw compl_Inter -- 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 Inter_union (s : ι → set β) (t : set β) : (⋂ i, s i) ∪ t = ⋂ i, s i ∪ t := infi_sup_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) := le_supr_inf_supr s t lemma Union_inter_of_monotone {ι α} [preorder ι] [is_directed ι (≤)] {s t : ι → set α} (hs : monotone s) (ht : monotone t) : (⋃ i, s i ∩ t i) = (⋃ i, s i) ∩ (⋃ i, t i) := supr_inf_of_monotone hs ht lemma Union_inter_of_antitone {ι α} [preorder ι] [is_directed ι (swap (≤))] {s t : ι → set α} (hs : antitone s) (ht : antitone t) : (⋃ i, s i ∩ t i) = (⋃ i, s i) ∩ (⋃ i, t i) := supr_inf_of_antitone hs ht lemma Inter_union_of_monotone {ι α} [preorder ι] [is_directed ι (swap (≤))] {s t : ι → set α} (hs : monotone s) (ht : monotone t) : (⋂ i, s i ∪ t i) = (⋂ i, s i) ∪ (⋂ i, t i) := infi_sup_of_monotone hs ht lemma Inter_union_of_antitone {ι α} [preorder ι] [is_directed ι (≤)] {s t : ι → set α} (hs : antitone s) (ht : antitone t) : (⋂ i, s i ∪ t i) = (⋂ i, s i) ∪ (⋂ i, t i) := infi_sup_of_antitone hs ht /-- 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 := supr_infi_le_infi_supr (flip s) 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] @[simp] lemma nonempty_bUnion {t : set α} {s : α → set β} : (⋃ i ∈ t, s i).nonempty ↔ ∃ i ∈ t, (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 lemma Union_comm (s : ι → ι' → set α) : (⋃ i i', s i i') = ⋃ i' i, s i i' := supr_comm lemma Inter_comm (s : ι → ι' → set α) : (⋂ i i', s i i') = ⋂ i' i, s i i' := infi_comm lemma Union₂_comm (s : Π i₁, κ₁ i₁ → Π i₂, κ₂ i₂ → set α) : (⋃ i₁ j₁ i₂ j₂, s i₁ j₁ i₂ j₂) = ⋃ i₂ j₂ i₁ j₁, s i₁ j₁ i₂ j₂ := supr₂_comm _ lemma Inter₂_comm (s : Π i₁, κ₁ i₁ → Π i₂, κ₂ i₂ → set α) : (⋂ i₁ j₁ i₂ j₂, s i₁ j₁ i₂ j₂) = ⋂ i₂ j₂ i₁ j₁, s i₁ j₁ i₂ j₂ := 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 -/ /-- A specialization of `mem_Union₂`. -/ theorem mem_bUnion {s : set α} {t : α → set β} {x : α} {y : β} (xs : x ∈ s) (ytx : y ∈ t x) : y ∈ ⋃ x ∈ s, t x := mem_Union₂_of_mem xs ytx /-- A specialization of `mem_Inter₂`. -/ theorem mem_bInter {s : set α} {t : α → set β} {y : β} (h : ∀ x ∈ s, y ∈ t x) : y ∈ ⋂ x ∈ s, t x := mem_Inter₂_of_mem h /-- A specialization of `subset_Union₂`. -/ theorem subset_bUnion_of_mem {s : set α} {u : α → set β} {x : α} (xs : x ∈ s) : u x ⊆ (⋃ x ∈ s, u x) := subset_Union₂ x xs /-- A specialization of `Inter₂_subset`. -/ theorem bInter_subset_of_mem {s : set α} {t : α → set β} {x : α} (xs : x ∈ s) : (⋂ x ∈ s, t x) ⊆ t x := Inter₂_subset x xs theorem bUnion_subset_bUnion_left {s s' : set α} {t : α → set β} (h : s ⊆ s') : (⋃ x ∈ s, t x) ⊆ (⋃ x ∈ s', t x) := Union₂_subset $ λ x hx, subset_bUnion_of_mem $ h hx theorem bInter_subset_bInter_left {s s' : set α} {t : α → set β} (h : s' ⊆ s) : (⋂ x ∈ s, t x) ⊆ (⋂ x ∈ s', t x) := subset_Inter₂ $ λ x hx, bInter_subset_of_mem $ h hx lemma bUnion_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 := (bUnion_subset_bUnion_left hs).trans $ Union₂_mono h lemma 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) := (bInter_subset_bInter_left hs).trans $ Inter₂_mono h lemma Union_congr {s t : ι → set α} (h : ∀ i, s i = t i) : (⋃ i, s i) = ⋃ i, t i := supr_congr h lemma Inter_congr {s t : ι → set α} (h : ∀ i, s i = t i) : (⋂ i, s i) = ⋂ i, t i := infi_congr h lemma Union₂_congr {s t : Π i, κ i → set α} (h : ∀ i j, s i j = t i j) : (⋃ i j, s i j) = ⋃ i j, t i j := Union_congr $ λ i, Union_congr $ h i lemma Inter₂_congr {s t : Π i, κ i → set α} (h : ∀ i j, s i j = t i j) : (⋂ i j, s i j) = ⋂ i j, t i j := Inter_congr $ λ i, Inter_congr $ h i 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 Union_subtype (p : α → Prop) (s : {x // p x} → set β) : (⋃ x : {x // p x}, s x) = ⋃ x (hx : p x), s ⟨x, hx⟩ := supr_subtype theorem Inter_subtype (p : α → Prop) (s : {x // p x} → set β) : (⋂ x : {x // p x}, s x) = ⋂ x (hx : p x), s ⟨x, hx⟩ := 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 (Union₂_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_coe_set {α β : Type*} (s : set α) (f : s → set β) : (⋃ i, f i) = ⋃ i ∈ s, f ⟨i, ‹i ∈ s›⟩ := Union_subtype _ _ @[simp] lemma Inter_coe_set {α β : Type*} (s : set α) (f : s → set β) : (⋂ i, f i) = ⋂ i ∈ s, f ⟨i, ‹i ∈ s›⟩ := Inter_subtype _ _ 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 lemma inter_Union₂ (s : set α) (t : Π i, κ i → set α) : s ∩ (⋃ i j, t i j) = ⋃ i j, s ∩ t i j := by simp only [inter_Union] lemma Union₂_inter (s : Π i, κ i → set α) (t : set α) : (⋃ i j, s i j) ∩ t = ⋃ i j, s i j ∩ t := by simp_rw Union_inter lemma union_Inter₂ (s : set α) (t : Π i, κ i → set α) : s ∪ (⋂ i j, t i j) = ⋂ i j, s ∪ t i j := by simp_rw union_Inter lemma Inter₂_union (s : Π i, κ i → set α) (t : set α) : (⋂ i j, s i j) ∪ t = ⋂ i j, s i j ∪ t := by simp_rw Inter_union 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 @[simp] theorem sUnion_subset_iff {s : set (set α)} {t : set α} : ⋃₀ s ⊆ t ↔ ∀ t' ∈ s, t' ⊆ t := @Sup_le_iff (set α) _ _ _ theorem subset_sInter {S : set (set α)} {t : set α} (h : ∀ t' ∈ S, t ⊆ t') : t ⊆ (⋂₀ S) := le_Inf h @[simp] theorem subset_sInter_iff {S : set (set α)} {t : set α} : t ⊆ (⋂₀ S) ↔ ∀ t' ∈ S, t ⊆ t' := @le_Inf_iff (set α) _ _ _ 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 @[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 @[simp] lemma sUnion_diff_singleton_empty (s : set (set α)) : ⋃₀ (s \ {∅}) = ⋃₀ s := Sup_diff_singleton_bot s @[simp] lemma sInter_diff_singleton_univ (s : set (set α)) : ⋂₀ (s \ {univ}) = ⋂₀ s := Inf_diff_singleton_top s 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 Union₂_eq_univ_iff {s : Π i, κ i → set α} : (⋃ i j, s i j) = univ ↔ ∀ a, ∃ i j, a ∈ s i j := 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 Inter₂_eq_empty_iff {s : Π i, κ i → set α} : (⋂ i j, s i j) = ∅ ↔ ∀ a, ∃ i j, a ∉ s i j := by simp only [eq_empty_iff_forall_not_mem, mem_Inter, not_forall] -- 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] lemma nonempty_Inter₂ {s : Π i, κ i → set α} : (⋂ i j, s i j).nonempty ↔ ∃ a, ∀ i j, a ∈ s i j := 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_eq_range_psigma (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 sigma.univ (X : α → Type*) : (set.univ : set (Σ a, X a)) = ⋃ a, range (sigma.mk a) := set.ext $ λ x, iff_of_true trivial ⟨range (sigma.mk x.1), set.mem_range_self _, x.2, sigma.eta x⟩ 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_const {s : set α} (h : ι → ι₂) : (⋃ i : ι, s) ⊆ (⋃ j : ι₂, s) := @supr_const_mono (set α) ι ι₂ _ s h @[simp] lemma Union_singleton_eq_range {α β : Type*} (f : α → β) : (⋃ (x : α), {f x}) = range f := by { ext x, simp [@eq_comm _ x] } lemma Union_of_singleton (α : Type*) : (⋃ x, {x} : set α) = univ := by simp lemma Union_of_singleton_coe (s : set α) : (⋃ (i : s), {i} : set α) = s := by simp 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 ×ˢ T, (p : (set α) × (set α)).1 ∪ p.2) := Inf_sup_Inf lemma sUnion_inter_sUnion {s t : set (set α)} : (⋃₀ s) ∩ (⋃₀ t) = (⋃ p ∈ s ×ˢ 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 _ ι] lemma bInter_Union (s : ι → set α) (t : α → set β) : (⋂ x ∈ ⋃ i, s i, t x) = ⋂ i (x ∈ s i), t x := by simp [@Inter_comm _ ι] lemma sUnion_Union (s : ι → set (set α)) : ⋃₀ (⋃ i, s i) = ⋃ i, ⋃₀ (s i) := by simp only [sUnion_eq_bUnion, bUnion_Union] theorem sInter_Union (s : ι → set (set α)) : ⋂₀ (⋃ i, s i) = ⋂ i, ⋂₀ s i := by simp only [sInter_eq_bInter, bInter_Union] 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 (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_left (s : ι → set α) (t : set α) : t ∪ (⋂ i, s i) = (⋂ i, t ∪ s i) := sup_infi_eq _ _ lemma union_distrib_Inter₂_left (s : set α) (t : Π i, κ i → set α) : s ∪ (⋂ i j, t i j) = ⋂ i j, s ∪ t i j := by simp_rw union_distrib_Inter_left lemma union_distrib_Inter_right (s : ι → set α) (t : set α) : (⋂ i, s i) ∪ t = (⋂ i, s i ∪ t) := infi_sup_eq _ _ lemma union_distrib_Inter₂_right (s : Π i, κ i → set α) (t : set α) : (⋂ i j, s i j) ∪ t = ⋂ i j, s i j ∪ t := by simp_rw 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_Union₂ {s : Π i, κ i → set α} {t : set β} {f : α → β} (H : ∀ i j, maps_to f (s i j) t) : maps_to f (⋃ i j, s i j) 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_Union₂_Union₂ {s : Π i, κ i → set α} {t : Π i, κ i → set β} {f : α → β} (H : ∀ i j, maps_to f (s i j) (t i j)) : maps_to f (⋃ i j, s i j) (⋃ i j, t i j) := 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_Inter₂ {s : set α} {t : Π i, κ i → set β} {f : α → β} (H : ∀ i j, maps_to f s (t i j)) : maps_to f s (⋂ i j, t i j) := 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_Inter₂_Inter₂ {s : Π i, κ i → set α} {t : Π i, κ i → set β} {f : α → β} (H : ∀ i j, maps_to f (s i j) (t i j)) : maps_to f (⋂ i j, s i j) (⋂ i j, t i j) := 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_Inter₂_subset (s : Π i, κ i → set α) (f : α → β) : f '' (⋂ i j, s i j) ⊆ ⋂ i j, f '' s i j := (maps_to_Inter₂_Inter₂ $ λ 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_Inter₂_subset } /-! ### `restrict_preimage` -/ section open function variables (s : set β) {f : α → β} {U : ι → set β} (hU : Union U = univ) lemma restrict_preimage_injective (hf : injective f) : injective (s.restrict_preimage f) := λ x y e, subtype.mk.inj_arrow e (λ e, subtype.coe_injective (hf e)) lemma restrict_preimage_surjective (hf : surjective f) : surjective (s.restrict_preimage f) := λ x, ⟨⟨_, (show f (hf x).some ∈ s, from (hf x).some_spec.symm ▸ x.2)⟩, subtype.ext (hf x).some_spec⟩ lemma restrict_preimage_bijective (hf : bijective f) : bijective (s.restrict_preimage f) := ⟨s.restrict_preimage_injective hf.1, s.restrict_preimage_surjective hf.2⟩ alias set.restrict_preimage_injective ← _root_.function.injective.restrict_preimage alias set.restrict_preimage_surjective ← _root_.function.surjective.restrict_preimage alias set.restrict_preimage_bijective ← _root_.function.bijective.restrict_preimage include hU lemma injective_iff_injective_of_Union_eq_univ : injective f ↔ ∀ i, injective ((U i).restrict_preimage f) := begin refine ⟨λ H i, (U i).restrict_preimage_injective H, λ H x y e, _⟩, obtain ⟨i, hi⟩ := set.mem_Union.mp (show f x ∈ set.Union U, by { rw hU, triv }), injection @H i ⟨x, hi⟩ ⟨y, show f y ∈ U i, from e ▸ hi⟩ (subtype.ext e) end lemma surjective_iff_surjective_of_Union_eq_univ : surjective f ↔ ∀ i, surjective ((U i).restrict_preimage f) := begin refine ⟨λ H i, (U i).restrict_preimage_surjective H, λ H x, _⟩, obtain ⟨i, hi⟩ := set.mem_Union.mp (show x ∈ set.Union U, by { rw hU, triv }), exact ⟨_, congr_arg subtype.val (H i ⟨x, hi⟩).some_spec⟩ end lemma bijective_iff_bijective_of_Union_eq_univ : bijective f ↔ ∀ i, bijective ((U i).restrict_preimage f) := by simp_rw [bijective, forall_and_distrib, injective_iff_injective_of_Union_eq_univ hU, surjective_iff_surjective_of_Union_eq_univ hU] end /-! ### `inj_on` -/ lemma inj_on.image_inter {f : α → β} {s t u : set α} (hf : inj_on f u) (hs : s ⊆ u) (ht : t ⊆ u) : f '' (s ∩ t) = f '' s ∩ f '' t := begin apply subset.antisymm (image_inter_subset _ _ _), rintros x ⟨⟨y, ys, hy⟩, ⟨z, zt, hz⟩⟩, have : y = z, { apply hf (hs ys) (ht zt), rwa ← hz at hy }, rw ← this at zt, exact ⟨y, ⟨ys, zt⟩, hy⟩, end 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_Union₂ {s : set α} {t : Π i, κ i → set β} {f : α → β} (H : ∀ i j, surj_on f s (t i j)) : surj_on f s (⋃ i j, t i j) := surj_on_Union $ λ i, surj_on_Union (H i) lemma surj_on_Union₂_Union₂ {s : Π i, κ i → set α} {t : Π i, κ i → set β} {f : α → β} (H : ∀ i j, surj_on f (s i j) (t i j)) : surj_on f (⋃ i j, s i j) (⋃ i j, t i j) := 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_Union₂ (f : α → β) (s : Π i, κ i → set α) : f '' (⋃ i j, s i j) = ⋃ i j, f '' s i j := by simp_rw 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] lemma preimage_Union {f : α → β} {s : ι → set β} : f ⁻¹' (⋃ i, s i) = (⋃ i, f ⁻¹' s i) := set.ext $ by simp [preimage] lemma preimage_Union₂ {f : α → β} {s : Π i, κ i → set β} : f ⁻¹' (⋃ i j, s i j) = ⋃ i j, f ⁻¹' s i j := by simp_rw preimage_Union @[simp] theorem preimage_sUnion {f : α → β} {s : set (set β)} : f ⁻¹' (⋃₀ s) = (⋃ t ∈ s, f ⁻¹' t) := by rw [sUnion_eq_bUnion, preimage_Union₂] lemma preimage_Inter {f : α → β} {s : ι → set β} : f ⁻¹' (⋂ i, s i) = (⋂ i, f ⁻¹' s i) := by ext; simp lemma preimage_Inter₂ {f : α → β} {s : Π i, κ i → set β} : f ⁻¹' (⋂ i j, s i j) = ⋂ i j, f ⁻¹' s i j := by simp_rw preimage_Inter @[simp] lemma preimage_sInter {f : α → β} {s : set (set β)} : f ⁻¹' (⋂₀ s) = ⋂ t ∈ s, f ⁻¹' t := by rw [sInter_eq_bInter, preimage_Inter₂] @[simp] lemma bUnion_preimage_singleton (f : α → β) (s : set β) : (⋃ y ∈ s, f ⁻¹' {y}) = f ⁻¹' s := by rw [← preimage_Union₂, 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 lemma prod_Union {s : set α} {t : ι → set β} : s ×ˢ (⋃ i, t i) = ⋃ i, s ×ˢ (t i) := by { ext, simp } lemma prod_Union₂ {s : set α} {t : Π i, κ i → set β} : s ×ˢ (⋃ i j, t i j) = ⋃ i j, s ×ˢ t i j := by simp_rw [prod_Union] lemma prod_sUnion {s : set α} {C : set (set β)} : s ×ˢ (⋃₀ C) = ⋃₀ ((λ t, s ×ˢ t) '' C) := by simp_rw [sUnion_eq_bUnion, bUnion_image, prod_Union₂] lemma Union_prod_const {s : ι → set α} {t : set β} : (⋃ i, s i) ×ˢ t = ⋃ i, s i ×ˢ t := by { ext, simp } lemma Union₂_prod_const {s : Π i, κ i → set α} {t : set β} : (⋃ i j, s i j) ×ˢ t = ⋃ i j, s i j ×ˢ t := by simp_rw [Union_prod_const] lemma sUnion_prod_const {C : set (set α)} {t : set β} : (⋃₀ C) ×ˢ t = ⋃₀ ((λ s : set α, s ×ˢ t) '' C) := by simp only [sUnion_eq_bUnion, Union₂_prod_const, bUnion_image] lemma Union_prod {ι ι' α β} (s : ι → set α) (t : ι' → set β) : (⋃ (x : ι × ι'), s x.1 ×ˢ t x.2) = (⋃ (i : ι), s i) ×ˢ (⋃ (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 ×ˢ t x) = (⋃ x, s x) ×ˢ (⋃ 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] lemma image2_Union₂_left (s : Π i, κ i → set α) (t : set β) : image2 f (⋃ i j, s i j) t = ⋃ i j, image2 f (s i j) t := by simp_rw image2_Union_left lemma image2_Union₂_right (s : set α) (t : Π i, κ i → set β) : image2 f s (⋃ i j, t i j) = ⋃ i j, image2 f s (t i j) := by simp_rw image2_Union_right lemma image2_Inter_subset_left (s : ι → set α) (t : set β) : image2 f (⋂ i, s i) t ⊆ ⋂ i, image2 f (s i) t := by { simp_rw [image2_subset_iff, mem_Inter], exact λ x hx y hy i, mem_image2_of_mem (hx _) hy } lemma image2_Inter_subset_right (s : set α) (t : ι → set β) : image2 f s (⋂ i, t i) ⊆ ⋂ i, image2 f s (t i) := by { simp_rw [image2_subset_iff, mem_Inter], exact λ x hx y hy i, mem_image2_of_mem hx (hy _) } lemma image2_Inter₂_subset_left (s : Π i, κ i → set α) (t : set β) : image2 f (⋂ i j, s i j) t ⊆ ⋂ i j, image2 f (s i j) t := by { simp_rw [image2_subset_iff, mem_Inter], exact λ x hx y hy i j, mem_image2_of_mem (hx _ _) hy } lemma image2_Inter₂_subset_right (s : set α) (t : Π i, κ i → set β) : image2 f s (⋂ i j, t i j) ⊆ ⋂ i j, image2 f s (t i j) := by { simp_rw [image2_subset_iff, mem_Inter], exact λ x hx y hy i j, mem_image2_of_mem hx (hy _ _) } /-- The `set.image2` version of `set.image_eq_Union` -/ lemma image2_eq_Union (s : set α) (t : set β) : image2 f s t = ⋃ (i ∈ s) (j ∈ t), {f i j} := by simp_rw [←image_eq_Union, Union_image_left] lemma prod_eq_bUnion_left : s ×ˢ t = ⋃ a ∈ s, (λ b, (a, b)) '' t := by rw [Union_image_left, image2_mk_eq_prod] lemma prod_eq_bUnion_right : s ×ˢ t = ⋃ b ∈ t, (λ a, (a, b)) '' s := by rw [Union_image_right, image2_mk_eq_prod] 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 ×ˢ 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 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 α} {f : α → β} 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 subset_left_of_subset_union (h : s ⊆ t ∪ u) (hac : disjoint s u) : s ⊆ t := hac.left_le_of_le_sup_right h lemma subset_right_of_subset_union (h : s ⊆ t ∪ u) (hab : disjoint s t) : s ⊆ u := hab.left_le_of_le_sup_left 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 : ¬disjoint s t ↔ (s ∩ t).nonempty := not_disjoint_iff alias not_disjoint_iff_nonempty_inter ↔ _ nonempty.not_disjoint lemma disjoint_or_nonempty_inter (s t : set α) : disjoint s t ∨ (s ∩ t).nonempty := (em _).imp_right not_disjoint_iff_nonempty_inter.mp lemma disjoint_iff_forall_ne : disjoint s t ↔ ∀ (x ∈ s) (y ∈ t), x ≠ y := by simp only [ne.def, disjoint_left, @imp_not_comm _ (_ = _), forall_eq'] lemma _root_.disjoint.ne_of_mem (h : disjoint s t) {x y} (hx : x ∈ s) (hy : y ∈ t) : x ≠ y := disjoint_iff_forall_ne.mp h x hx y hy 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 @[simp] lemma disjoint_Union₂_left {s : Π i, κ i → set α} {t : set α} : disjoint (⋃ i j, s i j) t ↔ ∀ i j, disjoint (s i j) t := supr₂_disjoint_iff @[simp] lemma disjoint_Union₂_right {s : set α} {t : Π i, κ i → set α} : disjoint s (⋃ i j, t i j) ↔ ∀ i j, disjoint s (t i j) := disjoint_supr₂_iff @[simp] lemma disjoint_sUnion_left {S : set (set α)} {t : set α} : disjoint (⋃₀ S) t ↔ ∀ s ∈ S, disjoint s t := Sup_disjoint_iff @[simp] lemma disjoint_sUnion_right {s : set α} {S : set (set α)} : disjoint s (⋃₀ S) ↔ ∀ t ∈ S, disjoint s t := disjoint_Sup_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 @[simp] lemma disjoint_singleton {a b : α} : disjoint ({a} : set α) {b} ↔ a ≠ b := by rw [disjoint_singleton_left, mem_singleton_iff] 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 lemma disjoint_image_of_injective {f : α → β} (hf : injective f) {s t : set α} (hd : disjoint s t) : disjoint (f '' s) (f '' t) := disjoint_image_image $ λ x hx y hy, hf.ne $ λ H, set.disjoint_iff.1 hd ⟨hx, H.symm ▸ hy⟩ lemma _root_.disjoint.of_image (h : disjoint (f '' s) (f '' t)) : disjoint s t := λ x hx, disjoint_left.1 h (mem_image_of_mem _ hx.1) (mem_image_of_mem _ hx.2) lemma disjoint_image_iff (hf : injective f) : disjoint (f '' s) (f '' t) ↔ disjoint s t := ⟨disjoint.of_image, disjoint_image_of_injective hf⟩ lemma _root_.disjoint.of_preimage (hf : surjective f) {s t : set β} (h : disjoint (f ⁻¹' s) (f ⁻¹' t)) : disjoint s t := by rw [disjoint_iff_inter_eq_empty, ←image_preimage_eq (_ ∩ _) hf, preimage_inter, h.inter_eq, image_empty] lemma disjoint_preimage_iff (hf : surjective f) {s t : set β} : disjoint (f ⁻¹' s) (f ⁻¹' t) ↔ disjoint s t := ⟨disjoint.of_preimage hf, disjoint.preimage _⟩ 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 {s : set β} : f ⁻¹' s = ∅ ↔ disjoint s (range f) := ⟨λ h, begin simp only [eq_empty_iff_forall_not_mem, disjoint_iff_inter_eq_empty, not_exists, mem_inter_eq, not_and, mem_range, mem_preimage] at h ⊢, assume y hy x hx, rw ← hx at hy, exact h x hy, end, preimage_eq_empty⟩ lemma _root_.disjoint.image {s t u : set α} {f : α → β} (h : disjoint s t) (hf : inj_on f u) (hs : s ⊆ u) (ht : t ⊆ u) : disjoint (f '' s) (f '' t) := begin rw disjoint_iff_inter_eq_empty at h ⊢, rw [← hf.image_inter hs ht, h, image_empty], end end set end disjoint /-! ### Intervals -/ namespace set variables [complete_lattice α] lemma Ici_supr (f : ι → α) : Ici (⨆ i, f i) = ⋂ i, Ici (f i) := ext $ λ _, by simp only [mem_Ici, supr_le_iff, mem_Inter] lemma Iic_infi (f : ι → α) : Iic (⨅ i, f i) = ⋂ i, Iic (f i) := ext $ λ _, by simp only [mem_Iic, le_infi_iff, mem_Inter] lemma Ici_supr₂ (f : Π i, κ i → α) : Ici (⨆ i j, f i j) = ⋂ i j, Ici (f i j) := by simp_rw Ici_supr lemma Iic_infi₂ (f : Π i, κ i → α) : Iic (⨅ i j, f i j) = ⋂ i j, Iic (f i j) := by simp_rw Iic_infi lemma Ici_Sup (s : set α) : Ici (Sup s) = ⋂ a ∈ s, Ici a := by rw [Sup_eq_supr, Ici_supr₂] lemma Iic_Inf (s : set α) : Iic (Inf s) = ⋂ a ∈ s, Iic a := by rw [Inf_eq_infi, Iic_infi₂] 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⟩⟩⟩ lemma bUnion_diff_bUnion_subset (s₁ s₂ : set α) : (⋃ x ∈ s₁, t x) \ (⋃ x ∈ s₂, t x) ⊆ (⋃ x ∈ s₁ \ s₂, t x) := begin simp only [diff_subset_iff, ← bUnion_union], apply bUnion_subset_bUnion_left, rw union_diff_self, apply subset_union_right end /-- 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 lemma Union_ge_eq_Union_nat_add (u : ℕ → set α) (n : ℕ) : (⋃ i ≥ n, u i) = ⋃ i, u (i + n) := supr_ge_eq_supr_nat_add u n lemma Inter_ge_eq_Inter_nat_add (u : ℕ → set α) (n : ℕ) : (⋂ i ≥ n, u i) = ⋂ i, u (i + n) := infi_ge_eq_infi_nat_add u n lemma _root_.monotone.Union_nat_add {f : ℕ → set α} (hf : monotone f) (k : ℕ) : (⋃ n, f (n + k)) = ⋃ n, f n := hf.supr_nat_add k lemma _root_.antitone.Inter_nat_add {f : ℕ → set α} (hf : antitone f) (k : ℕ) : (⋂ n, f (n + k)) = ⋂ n, f n := hf.infi_nat_add k @[simp] lemma Union_Inter_ge_nat_add (f : ℕ → set α) (k : ℕ) : (⋃ n, ⋂ i ≥ n, f (i + k)) = ⋃ n, ⋂ i ≥ n, f i := supr_infi_ge_nat_add f k lemma union_Union_nat_succ (u : ℕ → set α) : u 0 ∪ (⋃ i, u (i + 1)) = ⋃ i, u i := sup_supr_nat_succ u lemma inter_Inter_nat_succ (u : ℕ → set α) : u 0 ∩ (⋂ i, u (i + 1)) = ⋂ i, u i := inf_infi_nat_succ u end set section sup_closed /-- A set `s` is sup-closed if for all `x₁, x₂ ∈ s`, `x₁ ⊔ x₂ ∈ s`. -/ def sup_closed [has_sup α] (s : set α) : Prop := ∀ x1 x2, x1 ∈ s → x2 ∈ s → x1 ⊔ x2 ∈ s lemma sup_closed_singleton [semilattice_sup α] (x : α) : sup_closed ({x} : set α) := λ _ _ y1_mem y2_mem, by { rw set.mem_singleton_iff at *, rw [y1_mem, y2_mem, sup_idem], } lemma sup_closed.inter [semilattice_sup α] {s t : set α} (hs : sup_closed s) (ht : sup_closed t) : sup_closed (s ∩ t) := begin intros x y hx hy, rw set.mem_inter_iff at hx hy ⊢, exact ⟨hs x y hx.left hy.left, ht x y hx.right hy.right⟩, end lemma sup_closed_of_totally_ordered [semilattice_sup α] (s : set α) (hs : ∀ x y : α, x ∈ s → y ∈ s → y ≤ x ∨ x ≤ y) : sup_closed s := begin intros x y hxs hys, cases hs x y hxs hys, { rwa (sup_eq_left.mpr h), }, { rwa (sup_eq_right.mpr h), }, end lemma sup_closed_of_linear_order [linear_order α] (s : set α) : sup_closed s := sup_closed_of_totally_ordered s (λ x y hxs hys, le_total y x) end sup_closed open set variables [complete_lattice β] lemma supr_Union (s : ι → set α) (f : α → β) : (⨆ a ∈ (⋃ i, s i), f a) = ⨆ i (a ∈ s i), f a := by { rw supr_comm, simp_rw [mem_Union, supr_exists] } lemma infi_Union (s : ι → set α) (f : α → β) : (⨅ a ∈ (⋃ i, s i), f a) = ⨅ i (a ∈ s i), f a := @supr_Union α βᵒᵈ _ _ s f lemma Sup_sUnion (s : set (set β)) : Sup (⋃₀ s) = ⨆ t ∈ s, Sup t := by simp only [sUnion_eq_bUnion, Sup_eq_supr, supr_Union] lemma Inf_sUnion (s : set (set β)) : Inf (⋃₀ s) = ⨅ t ∈ s, Inf t := @Sup_sUnion βᵒᵈ _ _
ec16476b85f55461ce01ebd176f757c88209ff16
4727251e0cd73359b15b664c3170e5d754078599
/src/control/functor.lean
66020bb9dfa54c639920f4d0fec0ad1c1ea15a83
[ "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,658
lean
/- Copyright (c) 2017 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic.ext import tactic.lint /-! # Functors This module provides additional lemmas, definitions, and instances for `functor`s. ## Main definitions * `const α` is the functor that sends all types to `α`. * `add_const α` is `const α` but for when `α` has an additive structure. * `comp F G` for functors `F` and `G` is the functor composition of `F` and `G`. * `liftp` and `liftr` respectively lift predicates and relations on a type `α` to `F α`. Terms of `F α` are considered to, in some sense, contain values of type `α`. ## Tags functor, applicative -/ attribute [functor_norm] seq_assoc pure_seq_eq_map map_pure seq_map_assoc map_seq universes u v w section functor variables {F : Type u → Type v} variables {α β γ : Type u} variables [functor F] [is_lawful_functor F] lemma functor.map_id : (<$>) id = (id : F α → F α) := by apply funext; apply id_map lemma functor.map_comp_map (f : α → β) (g : β → γ) : ((<$>) g ∘ (<$>) f : F α → F γ) = (<$>) (g ∘ f) := by apply funext; intro; rw comp_map theorem functor.ext {F} : ∀ {F1 : functor F} {F2 : functor F} [@is_lawful_functor F F1] [@is_lawful_functor F F2] (H : ∀ α β (f : α → β) (x : F α), @functor.map _ F1 _ _ f x = @functor.map _ F2 _ _ f x), F1 = F2 | ⟨m, mc⟩ ⟨m', mc'⟩ H1 H2 H := begin cases show @m = @m', by funext α β f x; apply H, congr, funext α β, have E1 := @map_const_eq _ ⟨@m, @mc⟩ H1, have E2 := @map_const_eq _ ⟨@m, @mc'⟩ H2, exact E1.trans E2.symm end end functor /-- Introduce the `id` functor. Incidentally, this is `pure` for `id` as a `monad` and as an `applicative` functor. -/ def id.mk {α : Sort u} : α → id α := id namespace functor /-- `const α` is the constant functor, mapping every type to `α`. When `α` has a monoid structure, `const α` has an `applicative` instance. (If `α` has an additive monoid structure, see `functor.add_const`.) -/ @[nolint unused_arguments] def const (α : Type*) (β : Type*) := α /-- `const.mk` is the canonical map `α → const α β` (the identity), and it can be used as a pattern to extract this value. -/ @[pattern] def const.mk {α β} (x : α) : const α β := x /-- `const.mk'` is `const.mk` but specialized to map `α` to `const α punit`, where `punit` is the terminal object in `Type*`. -/ def const.mk' {α} (x : α) : const α punit := x /-- Extract the element of `α` from the `const` functor. -/ def const.run {α β} (x : const α β) : α := x namespace const protected lemma ext {α β} {x y : const α β} (h : x.run = y.run) : x = y := h /-- The map operation of the `const γ` functor. -/ @[nolint unused_arguments] protected def map {γ α β} (f : α → β) (x : const γ β) : const γ α := x instance {γ} : functor (const γ) := { map := @const.map γ } instance {γ} : is_lawful_functor (const γ) := by constructor; intros; refl instance {α β} [inhabited α] : inhabited (const α β) := ⟨(default : α)⟩ end const /-- `add_const α` is a synonym for constant functor `const α`, mapping every type to `α`. When `α` has a additive monoid structure, `add_const α` has an `applicative` instance. (If `α` has a multiplicative monoid structure, see `functor.const`.) -/ def add_const (α : Type*) := const α /-- `add_const.mk` is the canonical map `α → add_const α β`, which is the identity, where `add_const α β = const α β`. It can be used as a pattern to extract this value. -/ @[pattern] def add_const.mk {α β} (x : α) : add_const α β := x /-- Extract the element of `α` from the constant functor. -/ def add_const.run {α β} : add_const α β → α := id instance add_const.functor {γ} : functor (add_const γ) := @const.functor γ instance add_const.is_lawful_functor {γ} : is_lawful_functor (add_const γ) := @const.is_lawful_functor γ instance {α β} [inhabited α] : inhabited (add_const α β) := ⟨(default : α)⟩ /-- `functor.comp` is a wrapper around `function.comp` for types. It prevents Lean's type class resolution mechanism from trying a `functor (comp F id)` when `functor F` would do. -/ def comp (F : Type u → Type w) (G : Type v → Type u) (α : Type v) : Type w := F $ G α /-- Construct a term of `comp F G α` from a term of `F (G α)`, which is the same type. Can be used as a pattern to extract a term of `F (G α)`. -/ @[pattern] def comp.mk {F : Type u → Type w} {G : Type v → Type u} {α : Type v} (x : F (G α)) : comp F G α := x /-- Extract a term of `F (G α)` from a term of `comp F G α`, which is the same type. -/ def comp.run {F : Type u → Type w} {G : Type v → Type u} {α : Type v} (x : comp F G α) : F (G α) := x namespace comp variables {F : Type u → Type w} {G : Type v → Type u} protected lemma ext {α} {x y : comp F G α} : x.run = y.run → x = y := id instance {α} [inhabited (F (G α))] : inhabited (comp F G α) := ⟨(default : F (G α))⟩ variables [functor F] [functor G] /-- The map operation for the composition `comp F G` of functors `F` and `G`. -/ protected def map {α β : Type v} (h : α → β) : comp F G α → comp F G β | (comp.mk x) := comp.mk ((<$>) h <$> x) instance : functor (comp F G) := { map := @comp.map F G _ _ } @[functor_norm] lemma map_mk {α β} (h : α → β) (x : F (G α)) : h <$> comp.mk x = comp.mk ((<$>) h <$> x) := rfl @[simp] protected lemma run_map {α β} (h : α → β) (x : comp F G α) : (h <$> x).run = (<$>) h <$> x.run := rfl variables [is_lawful_functor F] [is_lawful_functor G] variables {α β γ : Type v} protected lemma id_map : ∀ (x : comp F G α), comp.map id x = x | (comp.mk x) := by simp [comp.map, functor.map_id] protected lemma comp_map (g' : α → β) (h : β → γ) : ∀ (x : comp F G α), comp.map (h ∘ g') x = comp.map h (comp.map g' x) | (comp.mk x) := by simp [comp.map, functor.map_comp_map g' h] with functor_norm instance : is_lawful_functor (comp F G) := { id_map := @comp.id_map F G _ _ _ _, comp_map := @comp.comp_map F G _ _ _ _ } theorem functor_comp_id {F} [AF : functor F] [is_lawful_functor F] : @comp.functor F id _ _ = AF := @functor.ext F _ AF (@comp.is_lawful_functor F id _ _ _ _) _ (λ α β f x, rfl) theorem functor_id_comp {F} [AF : functor F] [is_lawful_functor F] : @comp.functor id F _ _ = AF := @functor.ext F _ AF (@comp.is_lawful_functor id F _ _ _ _) _ (λ α β f x, rfl) end comp namespace comp open function (hiding comp) open functor variables {F : Type u → Type w} {G : Type v → Type u} variables [applicative F] [applicative G] /-- The `<*>` operation for the composition of applicative functors. -/ protected def seq {α β : Type v} : comp F G (α → β) → comp F G α → comp F G β | (comp.mk f) (comp.mk x) := comp.mk $ (<*>) <$> f <*> x instance : has_pure (comp F G) := ⟨λ _ x, comp.mk $ pure $ pure x⟩ instance : has_seq (comp F G) := ⟨λ _ _ f x, comp.seq f x⟩ @[simp] protected lemma run_pure {α : Type v} : ∀ x : α, (pure x : comp F G α).run = pure (pure x) | _ := rfl @[simp] protected lemma run_seq {α β : Type v} (f : comp F G (α → β)) (x : comp F G α) : (f <*> x).run = (<*>) <$> f.run <*> x.run := rfl instance : applicative (comp F G) := { map := @comp.map F G _ _, seq := @comp.seq F G _ _, ..comp.has_pure } end comp variables {F : Type u → Type u} [functor F] /-- If we consider `x : F α` to, in some sense, contain values of type `α`, predicate `liftp p x` holds iff every value contained by `x` satisfies `p`. -/ def liftp {α : Type u} (p : α → Prop) (x : F α) : Prop := ∃ u : F (subtype p), subtype.val <$> u = x /-- If we consider `x : F α` to, in some sense, contain values of type `α`, then `liftr r x y` relates `x` and `y` iff (1) `x` and `y` have the same shape and (2) we can pair values `a` from `x` and `b` from `y` so that `r a b` holds. -/ def liftr {α : Type u} (r : α → α → Prop) (x y : F α) : Prop := ∃ u : F {p : α × α // r p.fst p.snd}, (λ t : {p : α × α // r p.fst p.snd}, t.val.fst) <$> u = x ∧ (λ t : {p : α × α // r p.fst p.snd}, t.val.snd) <$> u = y /-- If we consider `x : F α` to, in some sense, contain values of type `α`, then `supp x` is the set of values of type `α` that `x` contains. -/ def supp {α : Type u} (x : F α) : set α := { y : α | ∀ ⦃p⦄, liftp p x → p y } theorem of_mem_supp {α : Type u} {x : F α} {p : α → Prop} (h : liftp p x) : ∀ y ∈ supp x, p y := λ y hy, hy h end functor
9de7841cd48fab802930558165df7085bb0781e3
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/tacUnsolvedGoalsErrors.lean
54644d28f69ce430181becbc6a1f3ff1614b3b16
[ "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
684
lean
theorem ex1 (p q r : Prop) (h1 : p ∨ q) (h2 : p → q) : q := have : q := by -- Error here skip by skip -- Error here skip theorem ex2 (p q r : Prop) (h1 : p ∨ q) (h2 : p → q) : q := have : q := by { skip } -- Error here by skip -- Error here skip theorem ex3 (p q r : Prop) (h1 : p ∨ q) (h2 : p → q) : q := by cases h1 { skip skip } -- Error here { skip skip } -- Error here theorem ex4 (p q r : Prop) (h1 : p ∨ q) (h2 : p → q) : q := by first | done | apply ex3 p q r h1 h2 theorem ex5 (p q r : Prop) (h1 : p ∨ q) (h2 : p → q) : q := by cases h1 · skip -- Error here skip · skip -- Error here skip
e8814ea48d9150dc2fc6a62cf9cfa374529ac56f
78630e908e9624a892e24ebdd21260720d29cf55
/src/logic_first_order/fol_01.lean
659dee5e6437b97b4c9f6c5197111066b9050cee
[ "CC0-1.0" ]
permissive
tomasz-lisowski/lean-logic-examples
84e612466776be0a16c23a0439ff8ef6114ddbe1
2b2ccd467b49c3989bf6c92ec0358a8d6ee68c5d
refs/heads/master
1,683,334,199,431
1,621,938,305,000
1,621,938,305,000
365,041,573
1
0
null
null
null
null
UTF-8
Lean
false
false
217
lean
namespace fol_01 variable A : Type variable R : A → A → Prop theorem fol_01 : (∀ x y, R x y) → (∀ x, R x x) := assume h1: (∀ x y, R x y), show ∀ x, R x x, from (assume hx: A, h1 hx hx) end fol_01
14ffda229211a4003b702ee255fcb69c8eafe5a1
aa2345b30d710f7e75f13157a35845ee6d48c017
/category_theory/yoneda.lean
7ae8613fdd0f8388c0653557da719c05cda47276
[ "Apache-2.0" ]
permissive
CohenCyril/mathlib
5241b20a3fd0ac0133e48e618a5fb7761ca7dcbe
a12d5a192f5923016752f638d19fc1a51610f163
refs/heads/master
1,586,031,957,957
1,541,432,824,000
1,541,432,824,000
156,246,337
0
0
Apache-2.0
1,541,434,514,000
1,541,434,513,000
null
UTF-8
Lean
false
false
5,077
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison /- The Yoneda embedding, as a functor `yoneda : C ⥤ ((Cᵒᵖ) ⥤ (Type v₁))`, along with instances that it is `full` and `faithful`. Also the Yoneda lemma, `yoneda_lemma : (yoneda_pairing C) ≅ (yoneda_evaluation C)`. -/ import category_theory.natural_transformation import category_theory.opposites import category_theory.types import category_theory.embedding import category_theory.natural_isomorphism namespace category_theory universes u₁ v₁ u₂ variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] include 𝒞 def yoneda : C ⥤ ((Cᵒᵖ) ⥤ (Type v₁)) := { obj := λ X, { obj := λ Y : C, Y ⟶ X, map' := λ Y Y' f g, f ≫ g, map_comp' := begin intros X_1 Y Z f g, ext1, dsimp at *, erw [category.assoc] end, map_id' := begin intros X_1, ext1, dsimp at *, erw [category.id_comp] end }, map' := λ X X' f, { app := λ Y g, g ≫ f } } namespace yoneda @[simp] lemma obj_obj (X Y : C) : ((yoneda C) X) Y = (Y ⟶ X) := rfl @[simp] lemma obj_map (X : C) {Y Y' : C} (f : Y ⟶ Y') : ((yoneda C) X).map f = λ g, f ≫ g := rfl @[simp] lemma map_app {X X' : C} (f : X ⟶ X') (Y : C) : ((yoneda C).map f) Y = λ g, g ≫ f := rfl lemma obj_map_id {X Y : Cᵒᵖ} (f : X ⟶ Y) : ((yoneda C) X).map f (𝟙 X) = ((yoneda C).map f) Y (𝟙 Y) := by obviously @[simp] lemma naturality {X Y : C} (α : (yoneda C) X ⟶ (yoneda C) Y) {Z Z' : C} (f : Z ⟶ Z') (h : Z' ⟶ X) : f ≫ α Z' h = α Z (f ≫ h) := begin erw [functor_to_types.naturality], refl end instance full : full (yoneda C) := { preimage := λ X Y f, (f X) (𝟙 X) }. instance faithful : faithful (yoneda C) := begin fsplit, intros X Y f g p, injection p with h, convert (congr_fun (congr_fun h X) (𝟙 X)) ; simp end /-- Extensionality via Yoneda. The typical usage would be ``` -- Goal is `X ≅ Y` apply yoneda.ext, -- Goals are now functions `(Z ⟶ X) → (Z ⟶ Y)`, `(Z ⟶ Y) → (Z ⟶ X)`, and the fact that these functions are inverses and natural in `Z`. ``` -/ def ext (X Y : C) (p : Π {Z : C}, (Z ⟶ X) → (Z ⟶ Y)) (q : Π {Z : C}, (Z ⟶ Y) → (Z ⟶ X)) (h₁ : Π {Z : C} (f : Z ⟶ X), q (p f) = f) (h₂ : Π {Z : C} (f : Z ⟶ Y), p (q f) = f) (n : Π {Z Z' : C} (f : Z' ⟶ Z) (g : Z ⟶ X), p (f ≫ g) = f ≫ p g) : X ≅ Y := @preimage_iso _ _ _ _ (yoneda C) _ _ _ _ (nat_iso.of_components (λ Z, { hom := p, inv := q, }) (by tidy)) -- We need to help typeclass inference with some awkward universe levels here. instance prod_category_instance_1 : category (((Cᵒᵖ) ⥤ Type v₁) × (Cᵒᵖ)) := category_theory.prod.{(max u₁ (v₁+1)) (max u₁ v₁) u₁ v₁} (Cᵒᵖ ⥤ Type v₁) (Cᵒᵖ) instance prod_category_instance_2 : category ((Cᵒᵖ) × ((Cᵒᵖ) ⥤ Type v₁)) := category_theory.prod.{u₁ v₁ (max u₁ (v₁+1)) (max u₁ v₁)} (Cᵒᵖ) (Cᵒᵖ ⥤ Type v₁) end yoneda open yoneda def yoneda_evaluation : (((Cᵒᵖ) ⥤ (Type v₁)) × (Cᵒᵖ)) ⥤ (Type (max u₁ v₁)) := (evaluation (Cᵒᵖ) (Type v₁)) ⋙ ulift_functor.{v₁ u₁} @[simp] lemma yoneda_evaluation_map_down (P Q : (Cᵒᵖ ⥤ Type v₁) × (Cᵒᵖ)) (α : P ⟶ Q) (x : (yoneda_evaluation C) P) : ((yoneda_evaluation C).map α x).down = (α.1) (Q.2) ((P.1).map (α.2) (x.down)) := rfl def yoneda_pairing : (((Cᵒᵖ) ⥤ (Type v₁)) × (Cᵒᵖ)) ⥤ (Type (max u₁ v₁)) := let F := (category_theory.prod.swap ((Cᵒᵖ) ⥤ (Type v₁)) (Cᵒᵖ)) in let G := (functor.prod ((yoneda C).op) (functor.id ((Cᵒᵖ) ⥤ (Type v₁)))) in let H := (functor.hom ((Cᵒᵖ) ⥤ (Type v₁))) in (F ⋙ G ⋙ H) @[simp] lemma yoneda_pairing_map (P Q : (Cᵒᵖ ⥤ Type v₁) × (Cᵒᵖ)) (α : P ⟶ Q) (β : (yoneda_pairing C) (P.1, P.2)) : (yoneda_pairing C).map α β = (yoneda C).map (α.snd) ≫ β ≫ α.fst := rfl def yoneda_lemma : (yoneda_pairing C) ≅ (yoneda_evaluation C) := { hom := { app := λ F x, ulift.up ((x.app F.2) (𝟙 F.2)), naturality' := begin intros X Y f, ext1, ext1, cases f, cases Y, cases X, dsimp at *, simp at *, erw [←functor_to_types.naturality, obj_map_id, functor_to_types.naturality, functor_to_types.map_id] end }, inv := { app := λ F x, { app := λ X a, (F.1.map a) x.down, naturality' := begin intros X Y f, ext1, cases x, cases F, dsimp at *, erw [functor_to_types.map_comp], refl end }, naturality' := begin intros X Y f, ext1, ext1, ext1, cases x, cases f, cases Y, cases X, dsimp at *, simp at *, erw [←functor_to_types.naturality, functor_to_types.map_comp] end }, hom_inv_id' := begin ext1, ext1, ext1, ext1, cases X, dsimp at *, simp at *, erw [←functor_to_types.naturality, obj_map_id, functor_to_types.naturality, functor_to_types.map_id] end, inv_hom_id' := begin ext1, ext1, ext1, cases x, cases X, dsimp at *, erw [functor_to_types.map_id] end }. end category_theory
6ccbfb32518268ed36430942328e5ac5a29ef046
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/geo/src/to_mathlib.lean
2de050d688fa34db12753389c42f222fe2e4780b
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
928
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import category_theory.epi_mono import category_theory.limits.shapes.binary_products /-! # Stuff that should be in mathlib -/ namespace category_theory universes v₁ u₁ variables {C : Type u₁} [𝒞 : category.{v₁} C] include 𝒞 lemma mono_comp_of_mono {X Y Z : C} (m : X ⟶ Y) (m' : Y ⟶ Z) (hm : mono m) (hm' : mono m') : mono (m ≫ m') := ⟨λ Z f g w, have f ≫ m = g ≫ m := (cancel_mono m').mp (by simp only [category.assoc]; exact w), (cancel_mono m).mp this⟩ lemma epi_comp_of_epi {X Y Z : C} (e : X ⟶ Y) (e' : Y ⟶ Z) (he : epi e) (he' : epi e') : epi (e ≫ e') := ⟨λ Z f g w, have e' ≫ f = e' ≫ g := (cancel_epi e).mp (by simp only [category.assoc] at w; exact w), (cancel_epi e').mp this⟩ end category_theory
ce5dbf9195f3318d8aaf7b1926edf9ef42323aca
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Meta/Tactic/Induction.lean
42a42567ba725d6ace032e1cbb689f9e3d6f002a
[ "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
11,804
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.Meta.RecursorInfo import Lean.Meta.SynthInstance import Lean.Meta.Tactic.Util import Lean.Meta.Tactic.Revert import Lean.Meta.Tactic.Intro import Lean.Meta.Tactic.Clear import Lean.Meta.Tactic.FVarSubst namespace Lean.Meta private partial def getTargetArity : Expr → Nat | Expr.mdata _ b _ => getTargetArity b | Expr.forallE _ _ b _ => getTargetArity b + 1 | e => if e.isHeadBetaTarget then getTargetArity e.headBeta else 0 private def addRecParams (mvarId : MVarId) (majorTypeArgs : Array Expr) : List (Option Nat) → Expr → MetaM Expr | [], recursor => pure recursor | some pos :: rest, recursor => if h : pos < majorTypeArgs.size then addRecParams mvarId majorTypeArgs rest (mkApp recursor (majorTypeArgs.get ⟨pos, h⟩)) else throwTacticEx `induction mvarId "ill-formed recursor" | none :: rest, recursor => do let recursorType ← inferType recursor let recursorType ← whnfForall recursorType match recursorType with | Expr.forallE _ d _ _ => do let param ← try synthInstance d catch _ => throwTacticEx `induction mvarId "failed to generate type class instance parameter" addRecParams mvarId majorTypeArgs rest (mkApp recursor param) | _ => throwTacticEx `induction mvarId "ill-formed recursor" structure InductionSubgoal where mvarId : MVarId fields : Array Expr := #[] subst : FVarSubst := {} deriving Inhabited private def getTypeBody (mvarId : MVarId) (type : Expr) (x : Expr) : MetaM Expr := do let type ← whnfForall type match type with | Expr.forallE _ _ b _ => pure $ b.instantiate1 x | _ => throwTacticEx `induction mvarId "ill-formed recursor" structure AltVarNames where explicit : Bool := false -- true if `@` modifier was used varNames : List Name := [] deriving Inhabited private partial def finalize (mvarId : MVarId) (givenNames : Array AltVarNames) (recursorInfo : RecursorInfo) (reverted : Array FVarId) (major : Expr) (indices : Array Expr) (baseSubst : FVarSubst) (recursor : Expr) : MetaM (Array InductionSubgoal) := do let target ← getMVarType mvarId let initialArity := getTargetArity target let recursorType ← inferType recursor let numMinors := recursorInfo.produceMotive.length let rec loop (pos : Nat) (minorIdx : Nat) (recursor recursorType : Expr) (consumedMajor : Bool) (subgoals : Array InductionSubgoal) := do let recursorType ← whnfForall recursorType if recursorType.isForall && pos < recursorInfo.numArgs then if pos == recursorInfo.firstIndexPos then let (recursor, recursorType) ← indices.foldlM (init := (recursor, recursorType)) fun (recursor, recursorType) index => do let recursor := mkApp recursor index let recursorType ← getTypeBody mvarId recursorType index pure (recursor, recursorType) let recursor := mkApp recursor major let recursorType ← getTypeBody mvarId recursorType major loop (pos+1+indices.size) minorIdx recursor recursorType true subgoals else -- consume motive let tag ← getMVarTag mvarId if minorIdx ≥ numMinors then throwTacticEx `induction mvarId "ill-formed recursor" match recursorType with | Expr.forallE n d b c => let d := d.headBeta -- Remark is givenNames is not empty, then user provided explicit alternatives for each minor premise if c.binderInfo.isInstImplicit && givenNames.isEmpty then match (← synthInstance? d) with | some inst => let recursor := mkApp recursor inst let recursorType ← getTypeBody mvarId recursorType inst loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor subgoals | none => do -- Add newSubgoal if type class resolution failed let mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n) let recursor := mkApp recursor mvar let recursorType ← getTypeBody mvarId recursorType mvar loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvar.mvarId! }) else let arity := getTargetArity d if arity < initialArity then throwTacticEx `induction mvarId "ill-formed recursor" let nparams := arity - initialArity -- number of fields due to minor premise let nextra := reverted.size - indices.size - 1 -- extra dependencies that have been reverted let minorGivenNames := if h : minorIdx < givenNames.size then givenNames.get ⟨minorIdx, h⟩ else {} let mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n) let recursor := mkApp recursor mvar let recursorType ← getTypeBody mvarId recursorType mvar -- Try to clear major premise from new goal let mvarId' ← tryClear mvar.mvarId! major.fvarId! let (fields, mvarId') ← introN mvarId' nparams minorGivenNames.varNames (useNamesForExplicitOnly := !minorGivenNames.explicit) let (extra, mvarId') ← introNP mvarId' nextra let subst := reverted.size.fold (init := baseSubst) fun i (subst : FVarSubst) => if i < indices.size + 1 then subst else let revertedFVarId := reverted[i] let newFVarId := extra[i - indices.size - 1] subst.insert revertedFVarId (mkFVar newFVarId) let fields := fields.map mkFVar loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvarId', fields := fields, subst := subst }) | _ => unreachable! else unless consumedMajor do throwTacticEx `induction mvarId "ill-formed recursor" assignExprMVar mvarId recursor pure subgoals loop (recursorInfo.paramsPos.length + 1) 0 recursor recursorType false #[] private def throwUnexpectedMajorType {α} (mvarId : MVarId) (majorType : Expr) : MetaM α := throwTacticEx `induction mvarId m!"unexpected major premise type{indentExpr majorType}" def induction (mvarId : MVarId) (majorFVarId : FVarId) (recursorName : Name) (givenNames : Array AltVarNames := #[]) : MetaM (Array InductionSubgoal) := withMVarContext mvarId do trace[Meta.Tactic.induction] "initial\n{MessageData.ofGoal mvarId}" checkNotAssigned mvarId `induction let majorLocalDecl ← getLocalDecl majorFVarId let recursorInfo ← mkRecursorInfo recursorName let some majorType ← whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type majorType.withApp fun _ majorTypeArgs => do recursorInfo.paramsPos.forM fun paramPos? => do match paramPos? with | none => pure () | some paramPos => if paramPos ≥ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}" let mctx ← getMCtx let indices ← recursorInfo.indicesPos.toArray.mapM fun idxPos => do if idxPos ≥ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}" let idx := majorTypeArgs.get! idxPos unless idx.isFVar do throwTacticEx `induction mvarId m!"major premise type index {idx} is not a variable{indentExpr majorType}" majorTypeArgs.size.forM fun i => do let arg := majorTypeArgs[i] if i != idxPos && arg == idx then throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs more than once{indentExpr majorType}" if i < idxPos && mctx.exprDependsOn arg idx.fvarId! then throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs in previous arguments{indentExpr majorType}" -- If arg is also and index and a variable occurring after `idx`, we need to make sure it doesn't depend on `idx`. -- Note that if `arg` is not a variable, we will fail anyway when we visit it. if i > idxPos && recursorInfo.indicesPos.contains i && arg.isFVar then let idxDecl ← getLocalDecl idx.fvarId! if mctx.localDeclDependsOn idxDecl arg.fvarId! then throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it depends on index occurring at position #{i+1}" pure idx let target ← getMVarType mvarId if !recursorInfo.depElim && mctx.exprDependsOn target majorFVarId then throwTacticEx `induction mvarId m!"recursor '{recursorName}' does not support dependent elimination, but conclusion depends on major premise" -- Revert indices and major premise preserving variable order let (reverted, mvarId) ← revert mvarId ((indices.map Expr.fvarId!).push majorFVarId) true -- Re-introduce indices and major let (indices', mvarId) ← introNP mvarId indices.size let (majorFVarId', mvarId) ← intro1P mvarId -- Create FVarSubst with indices let baseSubst := Id.run <| do let mut subst : FVarSubst := {} let mut i := 0 for index in indices do subst := subst.insert index.fvarId! (mkFVar indices'[i]) i := i + 1 pure subst trace[Meta.Tactic.induction] "after revert&intro\n{MessageData.ofGoal mvarId}" -- Update indices and major let indices := indices'.map mkFVar let majorFVarId := majorFVarId' let major := mkFVar majorFVarId withMVarContext mvarId do let target ← getMVarType mvarId let targetLevel ← getLevel target let targetLevel ← normalizeLevel targetLevel let majorLocalDecl ← getLocalDecl majorFVarId let some majorType ← whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type majorType.withApp fun majorTypeFn majorTypeArgs => do match majorTypeFn with | Expr.const majorTypeFnName majorTypeFnLevels _ => do let majorTypeFnLevels := majorTypeFnLevels.toArray let (recursorLevels, foundTargetLevel) ← recursorInfo.univLevelPos.foldlM (init := (#[], false)) fun (recursorLevels, foundTargetLevel) (univPos : RecursorUnivLevelPos) => do match univPos with | RecursorUnivLevelPos.motive => pure (recursorLevels.push targetLevel, true) | RecursorUnivLevelPos.majorType idx => if idx ≥ majorTypeFnLevels.size then throwTacticEx `induction mvarId "ill-formed recursor" pure (recursorLevels.push (majorTypeFnLevels.get! idx), foundTargetLevel) if !foundTargetLevel && !targetLevel.isZero then throwTacticEx `induction mvarId m!"recursor '{recursorName}' can only eliminate into Prop" let recursor := mkConst recursorName recursorLevels.toList let recursor ← addRecParams mvarId majorTypeArgs recursorInfo.paramsPos recursor -- Compute motive let motive := target let motive ← if recursorInfo.depElim then mkLambdaFVars #[major] motive else pure motive let motive ← mkLambdaFVars indices motive let recursor := mkApp recursor motive finalize mvarId givenNames recursorInfo reverted major indices baseSubst recursor | _ => throwTacticEx `induction mvarId "major premise is not of the form (C ...)" builtin_initialize registerTraceClass `Meta.Tactic.induction end Lean.Meta
36442d7de1acbc10aababdc910b4f17f403c3184
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/category_theory/limits/shapes/equalizers.lean
b02a851686ea5b47e9fa90a0fe843d3d30e14570
[ "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
28,315
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import category_theory.epi_mono import category_theory.limits.shapes.finite_limits /-! # Equalizers and coequalizers This file defines (co)equalizers as special cases of (co)limits. An equalizer is the categorical generalization of the subobject {a ∈ A | f(a) = g(a)} known from abelian groups or modules. It is a limit cone over the diagram formed by `f` and `g`. A coequalizer is the dual concept. ## Main definitions * `walking_parallel_pair` is the indexing category used for (co)equalizer_diagrams * `parallel_pair` is a functor from `walking_parallel_pair` to our category `C`. * a `fork` is a cone over a parallel pair. * there is really only one interesting morphism in a fork: the arrow from the vertex of the fork to the domain of f and g. It is called `fork.ι`. * an `equalizer` is now just a `limit (parallel_pair f g)` Each of these has a dual. ## Main statements * `equalizer.ι_mono` states that every equalizer map is a monomorphism * `is_limit_cone_parallel_pair_self` states that the identity on the domain of `f` is an equalizer of `f` and `f`. ## Implementation notes As with the other special shapes in the limits library, all the definitions here are given as `abbreviation`s of the general statements for limits, so all the `simp` lemmas and theorems about general limits can be used. ## References * [F. Borceux, *Handbook of Categorical Algebra 1*][borceux-vol1] -/ open category_theory namespace category_theory.limits local attribute [tidy] tactic.case_bash universes v u /-- The type of objects for the diagram indexing a (co)equalizer. -/ @[derive decidable_eq, derive inhabited] inductive walking_parallel_pair : Type v | zero | one instance fintype_walking_parallel_pair : fintype walking_parallel_pair := { elems := [walking_parallel_pair.zero, walking_parallel_pair.one].to_finset, complete := λ x, by { cases x; simp } } open walking_parallel_pair /-- The type family of morphisms for the diagram indexing a (co)equalizer. -/ @[derive decidable_eq] inductive walking_parallel_pair_hom : walking_parallel_pair → walking_parallel_pair → Type v | left : walking_parallel_pair_hom zero one | right : walking_parallel_pair_hom zero one | id : Π X : walking_parallel_pair.{v}, walking_parallel_pair_hom X X /-- Satisfying the inhabited linter -/ instance : inhabited (walking_parallel_pair_hom zero one) := { default := walking_parallel_pair_hom.left } open walking_parallel_pair_hom instance (j j' : walking_parallel_pair) : fintype (walking_parallel_pair_hom j j') := { elems := walking_parallel_pair.rec_on j (walking_parallel_pair.rec_on j' [walking_parallel_pair_hom.id zero].to_finset [left, right].to_finset) (walking_parallel_pair.rec_on j' ∅ [walking_parallel_pair_hom.id one].to_finset), complete := by tidy } /-- Composition of morphisms in the indexing diagram for (co)equalizers. -/ def walking_parallel_pair_hom.comp : Π (X Y Z : walking_parallel_pair) (f : walking_parallel_pair_hom X Y) (g : walking_parallel_pair_hom Y Z), walking_parallel_pair_hom X Z | _ _ _ (id _) h := h | _ _ _ left (id one) := left | _ _ _ right (id one) := right . instance walking_parallel_pair_hom_category : small_category walking_parallel_pair := { hom := walking_parallel_pair_hom, id := walking_parallel_pair_hom.id, comp := walking_parallel_pair_hom.comp } instance : fin_category walking_parallel_pair := { } @[simp] lemma walking_parallel_pair_hom_id (X : walking_parallel_pair) : walking_parallel_pair_hom.id X = 𝟙 X := rfl variables {C : Type u} [category.{v} C] variables {X Y : C} /-- `parallel_pair f g` is the diagram in `C` consisting of the two morphisms `f` and `g` with common domain and codomain. -/ def parallel_pair (f g : X ⟶ Y) : walking_parallel_pair ⥤ C := { obj := λ x, match x with | zero := X | one := Y end, map := λ x y h, match x, y, h with | _, _, (id _) := 𝟙 _ | _, _, left := f | _, _, right := g end, -- `tidy` can cope with this, but it's too slow: map_comp' := begin rintros (⟨⟩|⟨⟩) (⟨⟩|⟨⟩) (⟨⟩|⟨⟩) ⟨⟩⟨⟩; { unfold_aux, simp; refl }, end, }. @[simp] lemma parallel_pair_obj_zero (f g : X ⟶ Y) : (parallel_pair f g).obj zero = X := rfl @[simp] lemma parallel_pair_obj_one (f g : X ⟶ Y) : (parallel_pair f g).obj one = Y := rfl @[simp] lemma parallel_pair_map_left (f g : X ⟶ Y) : (parallel_pair f g).map left = f := rfl @[simp] lemma parallel_pair_map_right (f g : X ⟶ Y) : (parallel_pair f g).map right = g := rfl @[simp] lemma parallel_pair_functor_obj {F : walking_parallel_pair ⥤ C} (j : walking_parallel_pair) : (parallel_pair (F.map left) (F.map right)).obj j = F.obj j := begin cases j; refl end /-- Every functor indexing a (co)equalizer is naturally isomorphic (actually, equal) to a `parallel_pair` -/ def diagram_iso_parallel_pair (F : walking_parallel_pair ⥤ C) : F ≅ parallel_pair (F.map left) (F.map right) := nat_iso.of_components (λ j, eq_to_iso $ by cases j; tidy) $ by tidy /-- A fork on `f` and `g` is just a `cone (parallel_pair f g)`. -/ abbreviation fork (f g : X ⟶ Y) := cone (parallel_pair f g) /-- A cofork on `f` and `g` is just a `cocone (parallel_pair f g)`. -/ abbreviation cofork (f g : X ⟶ Y) := cocone (parallel_pair f g) variables {f g : X ⟶ Y} @[simp, reassoc] lemma fork.app_zero_left (s : fork f g) : (s.π).app zero ≫ f = (s.π).app one := by rw [←s.w left, parallel_pair_map_left] @[simp, reassoc] lemma fork.app_zero_right (s : fork f g) : (s.π).app zero ≫ g = (s.π).app one := by rw [←s.w right, parallel_pair_map_right] @[simp, reassoc] lemma cofork.left_app_one (s : cofork f g) : f ≫ (s.ι).app one = (s.ι).app zero := by rw [←s.w left, parallel_pair_map_left] @[simp, reassoc] lemma cofork.right_app_one (s : cofork f g) : g ≫ (s.ι).app one = (s.ι).app zero := by rw [←s.w right, parallel_pair_map_right] /-- A fork on `f g : X ⟶ Y` is determined by the morphism `ι : P ⟶ X` satisfying `ι ≫ f = ι ≫ g`. -/ def fork.of_ι {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : fork f g := { X := P, π := { app := λ X, begin cases X, exact ι, exact ι ≫ f, end, naturality' := λ X Y f, begin cases X; cases Y; cases f; dsimp; simp, { dsimp, simp, }, -- TODO If someone could decipher why these aren't done on the previous line, that would be great { exact w }, { dsimp, simp, }, -- TODO idem end } } /-- A cofork on `f g : X ⟶ Y` is determined by the morphism `π : Y ⟶ P` satisfying `f ≫ π = g ≫ π`. -/ def cofork.of_π {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : cofork f g := { X := P, ι := { app := λ X, begin cases X, exact f ≫ π, exact π, end, naturality' := λ X Y f, begin cases X; cases Y; cases f; dsimp; simp, { dsimp, simp, }, -- TODO idem { exact w.symm }, { dsimp, simp, }, -- TODO idem end } } @[simp] lemma fork.of_ι_app_zero {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : (fork.of_ι ι w).π.app zero = ι := rfl @[simp] lemma fork.of_ι_app_one {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : (fork.of_ι ι w).π.app one = ι ≫ f := rfl @[simp] lemma cofork.of_π_app_zero {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : (cofork.of_π π w).ι.app zero = f ≫ π := rfl @[simp] lemma cofork.of_π_app_one {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : (cofork.of_π π w).ι.app one = π := rfl /-- A fork `t` on the parallel pair `f g : X ⟶ Y` consists of two morphisms `t.π.app zero : t.X ⟶ X` and `t.π.app one : t.X ⟶ Y`. Of these, only the first one is interesting, and we give it the shorter name `fork.ι t`. -/ abbreviation fork.ι (t : fork f g) := t.π.app zero /-- A cofork `t` on the parallel_pair `f g : X ⟶ Y` consists of two morphisms `t.ι.app zero : X ⟶ t.X` and `t.ι.app one : Y ⟶ t.X`. Of these, only the second one is interesting, and we give it the shorter name `cofork.π t`. -/ abbreviation cofork.π (t : cofork f g) := t.ι.app one @[simp] lemma fork.ι_of_ι {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : fork.ι (fork.of_ι ι w) = ι := rfl @[simp] lemma cofork.π_of_π {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : cofork.π (cofork.of_π π w) = π := rfl lemma fork.ι_eq_app_zero (t : fork f g) : fork.ι t = t.π.app zero := rfl lemma cofork.π_eq_app_one (t : cofork f g) : cofork.π t = t.ι.app one := rfl lemma fork.condition (t : fork f g) : (fork.ι t) ≫ f = (fork.ι t) ≫ g := begin erw [t.w left, ← t.w right], refl end lemma cofork.condition (t : cofork f g) : f ≫ (cofork.π t) = g ≫ (cofork.π t) := begin erw [t.w left, ← t.w right], refl end /-- To check whether two maps are equalized by both maps of a fork, it suffices to check it for the first map -/ lemma fork.equalizer_ext (s : fork f g) {W : C} {k l : W ⟶ s.X} (h : k ≫ fork.ι s = l ≫ fork.ι s) : ∀ (j : walking_parallel_pair), k ≫ s.π.app j = l ≫ s.π.app j | zero := h | one := by rw [←fork.app_zero_left, reassoc_of h] /-- To check whether two maps are coequalized by both maps of a cofork, it suffices to check it for the second map -/ lemma cofork.coequalizer_ext (s : cofork f g) {W : C} {k l : s.X ⟶ W} (h : cofork.π s ≫ k = cofork.π s ≫ l) : ∀ (j : walking_parallel_pair), s.ι.app j ≫ k = s.ι.app j ≫ l | zero := by simp only [←cofork.left_app_one, category.assoc, h] | one := h lemma fork.is_limit.hom_ext {s : fork f g} (hs : is_limit s) {W : C} {k l : W ⟶ s.X} (h : k ≫ fork.ι s = l ≫ fork.ι s) : k = l := hs.hom_ext $ fork.equalizer_ext _ h lemma cofork.is_colimit.hom_ext {s : cofork f g} (hs : is_colimit s) {W : C} {k l : s.X ⟶ W} (h : cofork.π s ≫ k = cofork.π s ≫ l) : k = l := hs.hom_ext $ cofork.coequalizer_ext _ h /-- If `s` is a limit fork over `f` and `g`, then a morphism `k : W ⟶ X` satisfying `k ≫ f = k ≫ g` induces a morphism `l : W ⟶ s.X` such that `l ≫ fork.ι s = k`. -/ def fork.is_limit.lift' {s : fork f g} (hs : is_limit s) {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : {l : W ⟶ s.X // l ≫ fork.ι s = k} := ⟨hs.lift $ fork.of_ι _ h, hs.fac _ _⟩ /-- If `s` is a colimit cofork over `f` and `g`, then a morphism `k : Y ⟶ W` satisfying `f ≫ k = g ≫ k` induces a morphism `l : s.X ⟶ W` such that `cofork.π s ≫ l = k`. -/ def cofork.is_colimit.desc' {s : cofork f g} (hs : is_colimit s) {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : {l : s.X ⟶ W // cofork.π s ≫ l = k} := ⟨hs.desc $ cofork.of_π _ h, hs.fac _ _⟩ /-- This is a slightly more convenient method to verify that a fork is a limit cone. It only asks for a proof of facts that carry any mathematical content -/ def fork.is_limit.mk (t : fork f g) (lift : Π (s : fork f g), s.X ⟶ t.X) (fac : ∀ (s : fork f g), lift s ≫ fork.ι t = fork.ι s) (uniq : ∀ (s : fork f g) (m : s.X ⟶ t.X) (w : ∀ j : walking_parallel_pair, m ≫ t.π.app j = s.π.app j), m = lift s) : is_limit t := { lift := lift, fac' := λ s j, walking_parallel_pair.cases_on j (fac s) $ by erw [←s.w left, ←t.w left, ←category.assoc, fac]; refl, uniq' := uniq } /-- This is another convenient method to verify that a fork is a limit cone. It only asks for a proof of facts that carry any mathematical content, and allows access to the same `s` for all parts. -/ def fork.is_limit.mk' {X Y : C} {f g : X ⟶ Y} (t : fork f g) (create : Π (s : fork f g), {l // l ≫ t.ι = s.ι ∧ ∀ {m}, m ≫ t.ι = s.ι → m = l}) : is_limit t := fork.is_limit.mk t (λ s, (create s).1) (λ s, (create s).2.1) (λ s m w, (create s).2.2 (w zero)) /-- This is a slightly more convenient method to verify that a cofork is a colimit cocone. It only asks for a proof of facts that carry any mathematical content -/ def cofork.is_colimit.mk (t : cofork f g) (desc : Π (s : cofork f g), t.X ⟶ s.X) (fac : ∀ (s : cofork f g), cofork.π t ≫ desc s = cofork.π s) (uniq : ∀ (s : cofork f g) (m : t.X ⟶ s.X) (w : ∀ j : walking_parallel_pair, t.ι.app j ≫ m = s.ι.app j), m = desc s) : is_colimit t := { desc := desc, fac' := λ s j, walking_parallel_pair.cases_on j (by erw [←s.w left, ←t.w left, category.assoc, fac]; refl) (fac s), uniq' := uniq } /-- This is another convenient method to verify that a fork is a limit cone. It only asks for a proof of facts that carry any mathematical content, and allows access to the same `s` for all parts. -/ def cofork.is_colimit.mk' {X Y : C} {f g : X ⟶ Y} (t : cofork f g) (create : Π (s : cofork f g), {l : t.X ⟶ s.X // t.π ≫ l = s.π ∧ ∀ {m}, t.π ≫ m = s.π → m = l}) : is_colimit t := cofork.is_colimit.mk t (λ s, (create s).1) (λ s, (create s).2.1) (λ s m w, (create s).2.2 (w one)) /-- This is a helper construction that can be useful when verifying that a category has all equalizers. Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)`, and a fork on `F.map left` and `F.map right`, we get a cone on `F`. If you're thinking about using this, have a look at `has_equalizers_of_has_limit_parallel_pair`, which you may find to be an easier way of achieving your goal. -/ def cone.of_fork {F : walking_parallel_pair ⥤ C} (t : fork (F.map left) (F.map right)) : cone F := { X := t.X, π := { app := λ X, t.π.app X ≫ eq_to_hom (by tidy), naturality' := λ j j' g, by { cases j; cases j'; cases g; dsimp; simp } } } /-- This is a helper construction that can be useful when verifying that a category has all coequalizers. Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)`, and a cofork on `F.map left` and `F.map right`, we get a cocone on `F`. If you're thinking about using this, have a look at `has_coequalizers_of_has_colimit_parallel_pair`, which you may find to be an easier way of achieving your goal. -/ def cocone.of_cofork {F : walking_parallel_pair ⥤ C} (t : cofork (F.map left) (F.map right)) : cocone F := { X := t.X, ι := { app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X, naturality' := λ j j' g, by { cases j; cases j'; cases g; dsimp; simp } } } @[simp] lemma cone.of_fork_π {F : walking_parallel_pair ⥤ C} (t : fork (F.map left) (F.map right)) (j) : (cone.of_fork t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cocone.of_cofork_ι {F : walking_parallel_pair ⥤ C} (t : cofork (F.map left) (F.map right)) (j) : (cocone.of_cofork t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl /-- Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)` and a cone on `F`, we get a fork on `F.map left` and `F.map right`. -/ def fork.of_cone {F : walking_parallel_pair ⥤ C} (t : cone F) : fork (F.map left) (F.map right) := { X := t.X, π := { app := λ X, t.π.app X ≫ eq_to_hom (by tidy) } } /-- Given `F : walking_parallel_pair ⥤ C`, which is really the same as `parallel_pair (F.map left) (F.map right)` and a cocone on `F`, we get a cofork on `F.map left` and `F.map right`. -/ def cofork.of_cocone {F : walking_parallel_pair ⥤ C} (t : cocone F) : cofork (F.map left) (F.map right) := { X := t.X, ι := { app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X } } @[simp] lemma fork.of_cone_π {F : walking_parallel_pair ⥤ C} (t : cone F) (j) : (fork.of_cone t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cofork.of_cocone_ι {F : walking_parallel_pair ⥤ C} (t : cocone F) (j) : (cofork.of_cocone t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl variables (f g) section variables [has_limit (parallel_pair f g)] /-- If we have chosen an equalizer of `f` and `g`, we can access the corresponding object by saying `equalizer f g`. -/ abbreviation equalizer := limit (parallel_pair f g) /-- If we have chosen an equalizer of `f` and `g`, we can access the inclusion `equalizer f g ⟶ X` by saying `equalizer.ι f g`. -/ abbreviation equalizer.ι : equalizer f g ⟶ X := limit.π (parallel_pair f g) zero @[simp] lemma equalizer.ι.fork : fork.ι (limit.cone (parallel_pair f g)) = equalizer.ι f g := rfl @[simp] lemma equalizer.ι.eq_app_zero : (limit.cone (parallel_pair f g)).π.app zero = equalizer.ι f g := rfl @[reassoc] lemma equalizer.condition : equalizer.ι f g ≫ f = equalizer.ι f g ≫ g := fork.condition $ limit.cone $ parallel_pair f g variables {f g} /-- A morphism `k : W ⟶ X` satisfying `k ≫ f = k ≫ g` factors through the equalizer of `f` and `g` via `equalizer.lift : W ⟶ equalizer f g`. -/ abbreviation equalizer.lift {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : W ⟶ equalizer f g := limit.lift (parallel_pair f g) (fork.of_ι k h) @[simp, reassoc] lemma equalizer.lift_ι {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : equalizer.lift k h ≫ equalizer.ι f g = k := limit.lift_π _ _ /-- A morphism `k : W ⟶ X` satisfying `k ≫ f = k ≫ g` induces a morphism `l : W ⟶ equalizer f g` satisfying `l ≫ equalizer.ι f g = k`. -/ def equalizer.lift' {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : {l : W ⟶ equalizer f g // l ≫ equalizer.ι f g = k} := ⟨equalizer.lift k h, equalizer.lift_ι _ _⟩ /-- Two maps into an equalizer are equal if they are are equal when composed with the equalizer map. -/ @[ext] lemma equalizer.hom_ext {W : C} {k l : W ⟶ equalizer f g} (h : k ≫ equalizer.ι f g = l ≫ equalizer.ι f g) : k = l := fork.is_limit.hom_ext (limit.is_limit _) h /-- An equalizer morphism is a monomorphism -/ instance equalizer.ι_mono : mono (equalizer.ι f g) := { right_cancellation := λ Z h k w, equalizer.hom_ext w } end section variables {f g} /-- The equalizer morphism in any limit cone is a monomorphism. -/ lemma mono_of_is_limit_parallel_pair {c : cone (parallel_pair f g)} (i : is_limit c) : mono (fork.ι c) := { right_cancellation := λ Z h k w, fork.is_limit.hom_ext i w } end section variables {f g} /-- The identity determines a cone on the equalizer diagram of `f` and `g` if `f = g`. -/ def id_fork (h : f = g) : fork f g := fork.of_ι (𝟙 X) $ h ▸ rfl /-- The identity on `X` is an equalizer of `(f, g)`, if `f = g`. -/ def is_limit_id_fork (h : f = g) : is_limit (id_fork h) := fork.is_limit.mk _ (λ s, fork.ι s) (λ s, category.comp_id _) (λ s m h, by { convert h zero, exact (category.comp_id _).symm }) /-- Every equalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def is_iso_limit_cone_parallel_pair_of_eq (h₀ : f = g) {c : cone (parallel_pair f g)} (h : is_limit c) : is_iso (c.π.app zero) := is_iso.of_iso $ is_limit.cone_point_unique_up_to_iso h $ is_limit_id_fork h₀ /-- The equalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def equalizer.ι_of_eq [has_limit (parallel_pair f g)] (h : f = g) : is_iso (equalizer.ι f g) := is_iso_limit_cone_parallel_pair_of_eq h $ limit.is_limit _ /-- Every equalizer of `(f, f)` is an isomorphism. -/ def is_iso_limit_cone_parallel_pair_of_self {c : cone (parallel_pair f f)} (h : is_limit c) : is_iso (c.π.app zero) := is_iso_limit_cone_parallel_pair_of_eq rfl h /-- An equalizer that is an epimorphism is an isomorphism. -/ def is_iso_limit_cone_parallel_pair_of_epi {c : cone (parallel_pair f g)} (h : is_limit c) [epi (c.π.app zero)] : is_iso (c.π.app zero) := is_iso_limit_cone_parallel_pair_of_eq ((cancel_epi _).1 (fork.condition c)) h end /-- The equalizer of `(f, f)` is an isomorphism. -/ def equalizer.ι_of_self [has_limit (parallel_pair f f)] : is_iso (equalizer.ι f f) := equalizer.ι_of_eq rfl section variables [has_colimit (parallel_pair f g)] /-- If we have chosen a coequalizer of `f` and `g`, we can access the corresponding object by saying `coequalizer f g`. -/ abbreviation coequalizer := colimit (parallel_pair f g) /-- If we have chosen a coequalizer of `f` and `g`, we can access the corresponding projection by saying `coequalizer.π f g`. -/ abbreviation coequalizer.π : Y ⟶ coequalizer f g := colimit.ι (parallel_pair f g) one @[simp] lemma coequalizer.π.cofork : cofork.π (colimit.cocone (parallel_pair f g)) = coequalizer.π f g := rfl @[simp] lemma coequalizer.π.eq_app_one : (colimit.cocone (parallel_pair f g)).ι.app one = coequalizer.π f g := rfl @[reassoc] lemma coequalizer.condition : f ≫ coequalizer.π f g = g ≫ coequalizer.π f g := cofork.condition $ colimit.cocone $ parallel_pair f g variables {f g} /-- Any morphism `k : Y ⟶ W` satisfying `f ≫ k = g ≫ k` factors through the coequalizer of `f` and `g` via `coequalizer.desc : coequalizer f g ⟶ W`. -/ abbreviation coequalizer.desc {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : coequalizer f g ⟶ W := colimit.desc (parallel_pair f g) (cofork.of_π k h) @[simp, reassoc] lemma coequalizer.π_desc {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : coequalizer.π f g ≫ coequalizer.desc k h = k := colimit.ι_desc _ _ /-- Any morphism `k : Y ⟶ W` satisfying `f ≫ k = g ≫ k` induces a morphism `l : coequalizer f g ⟶ W` satisfying `coequalizer.π ≫ g = l`. -/ def coequalizer.desc' {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : {l : coequalizer f g ⟶ W // coequalizer.π f g ≫ l = k} := ⟨coequalizer.desc k h, coequalizer.π_desc _ _⟩ /-- Two maps from a coequalizer are equal if they are equal when composed with the coequalizer map -/ @[ext] lemma coequalizer.hom_ext {W : C} {k l : coequalizer f g ⟶ W} (h : coequalizer.π f g ≫ k = coequalizer.π f g ≫ l) : k = l := cofork.is_colimit.hom_ext (colimit.is_colimit _) h /-- A coequalizer morphism is an epimorphism -/ instance coequalizer.π_epi : epi (coequalizer.π f g) := { left_cancellation := λ Z h k w, coequalizer.hom_ext w } end section variables {f g} /-- The coequalizer morphism in any colimit cocone is an epimorphism. -/ lemma epi_of_is_colimit_parallel_pair {c : cocone (parallel_pair f g)} (i : is_colimit c) : epi (c.ι.app one) := { left_cancellation := λ Z h k w, cofork.is_colimit.hom_ext i w } end section variables {f g} /-- The identity determines a cocone on the coequalizer diagram of `f` and `g`, if `f = g`. -/ def id_cofork (h : f = g) : cofork f g := cofork.of_π (𝟙 Y) $ h ▸ rfl /-- The identity on `Y` is a coequalizer of `(f, g)`, where `f = g`. -/ def is_colimit_id_cofork (h : f = g) : is_colimit (id_cofork h) := cofork.is_colimit.mk _ (λ s, cofork.π s) (λ s, category.id_comp _) (λ s m h, by { convert h one, exact (category.id_comp _).symm }) /-- Every coequalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def is_iso_colimit_cocone_parallel_pair_of_eq (h₀ : f = g) {c : cocone (parallel_pair f g)} (h : is_colimit c) : is_iso (c.ι.app one) := is_iso.of_iso $ is_colimit.cocone_point_unique_up_to_iso (is_colimit_id_cofork h₀) h /-- The coequalizer of `(f, g)`, where `f = g`, is an isomorphism. -/ def coequalizer.π_of_eq [has_colimit (parallel_pair f g)] (h : f = g) : is_iso (coequalizer.π f g) := is_iso_colimit_cocone_parallel_pair_of_eq h $ colimit.is_colimit _ /-- Every coequalizer of `(f, f)` is an isomorphism. -/ def is_iso_colimit_cocone_parallel_pair_of_self {c : cocone (parallel_pair f f)} (h : is_colimit c) : is_iso (c.ι.app one) := is_iso_colimit_cocone_parallel_pair_of_eq rfl h /-- A coequalizer that is a monomorphism is an isomorphism. -/ def is_iso_limit_cocone_parallel_pair_of_epi {c : cocone (parallel_pair f g)} (h : is_colimit c) [mono (c.ι.app one)] : is_iso (c.ι.app one) := is_iso_colimit_cocone_parallel_pair_of_eq ((cancel_mono _).1 (cofork.condition c)) h end /-- The coequalizer of `(f, f)` is an isomorphism. -/ def coequalizer.π_of_self [has_colimit (parallel_pair f f)] : is_iso (coequalizer.π f f) := coequalizer.π_of_eq rfl variables (C) /-- `has_equalizers` represents a choice of equalizer for every pair of morphisms -/ class has_equalizers := (has_limits_of_shape : has_limits_of_shape walking_parallel_pair C) /-- `has_coequalizers` represents a choice of coequalizer for every pair of morphisms -/ class has_coequalizers := (has_colimits_of_shape : has_colimits_of_shape walking_parallel_pair C) attribute [instance] has_equalizers.has_limits_of_shape has_coequalizers.has_colimits_of_shape /-- Equalizers are finite limits, so if `C` has all finite limits, it also has all equalizers -/ def has_equalizers_of_has_finite_limits [has_finite_limits C] : has_equalizers C := { has_limits_of_shape := infer_instance } /-- Coequalizers are finite colimits, of if `C` has all finite colimits, it also has all coequalizers -/ def has_coequalizers_of_has_finite_colimits [has_finite_colimits C] : has_coequalizers C := { has_colimits_of_shape := infer_instance } /-- If `C` has all limits of diagrams `parallel_pair f g`, then it has all equalizers -/ def has_equalizers_of_has_limit_parallel_pair [Π {X Y : C} {f g : X ⟶ Y}, has_limit (parallel_pair f g)] : has_equalizers C := { has_limits_of_shape := { has_limit := λ F, has_limit_of_iso (diagram_iso_parallel_pair F).symm } } /-- If `C` has all colimits of diagrams `parallel_pair f g`, then it has all coequalizers -/ def has_coequalizers_of_has_colimit_parallel_pair [Π {X Y : C} {f g : X ⟶ Y}, has_colimit (parallel_pair f g)] : has_coequalizers C := { has_colimits_of_shape := { has_colimit := λ F, has_colimit_of_iso (diagram_iso_parallel_pair F) } } section -- In this section we show that a split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. variables {C} [split_mono f] /-- A split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. Here we build the cone, and show in `split_mono_equalizes` that it is a limit cone. -/ def cone_of_split_mono : cone (parallel_pair (𝟙 Y) (retraction f ≫ f)) := fork.of_ι f (by tidy) @[simp] lemma cone_of_split_mono_π_app_zero : (cone_of_split_mono f).π.app zero = f := rfl @[simp] lemma cone_of_split_mono_π_app_one : (cone_of_split_mono f).π.app one = f ≫ 𝟙 Y := rfl /-- A split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. -/ def split_mono_equalizes {X Y : C} (f : X ⟶ Y) [split_mono f] : is_limit (cone_of_split_mono f) := { lift := λ s, s.π.app zero ≫ retraction f, fac' := λ s, begin rintros (⟨⟩|⟨⟩), { rw [cone_of_split_mono_π_app_zero], erw [category.assoc, ← s.π.naturality right, s.π.naturality left, category.comp_id], }, { erw [cone_of_split_mono_π_app_one, category.comp_id, category.assoc, ← s.π.naturality right, category.id_comp], } end, uniq' := λ s m w, begin rw ←(w zero), simp, end, } end section -- In this section we show that a split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. variables {C} [split_epi f] /-- A split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. Here we build the cocone, and show in `split_epi_coequalizes` that it is a colimit cocone. -/ def cocone_of_split_epi : cocone (parallel_pair (𝟙 X) (f ≫ section_ f)) := cofork.of_π f (by tidy) @[simp] lemma cocone_of_split_epi_ι_app_one : (cocone_of_split_epi f).ι.app one = f := rfl @[simp] lemma cocone_of_split_epi_ι_app_zero : (cocone_of_split_epi f).ι.app zero = 𝟙 X ≫ f := rfl /-- A split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. -/ def split_epi_coequalizes {X Y : C} (f : X ⟶ Y) [split_epi f] : is_colimit (cocone_of_split_epi f) := { desc := λ s, section_ f ≫ s.ι.app one, fac' := λ s, begin rintros (⟨⟩|⟨⟩), { erw [cocone_of_split_epi_ι_app_zero, category.assoc, category.id_comp, ←category.assoc, s.ι.naturality right, functor.const.obj_map, category.comp_id], }, { erw [cocone_of_split_epi_ι_app_one, ←category.assoc, s.ι.naturality right, ←s.ι.naturality left, category.id_comp] } end, uniq' := λ s m w, begin rw ←(w one), simp, end, } end end category_theory.limits
ee8fbee3925df9953cfc47f4ba2695ee69001eb3
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/finset/basic.lean
9bb6c39eb6a061f508201c0134971c82def05105
[ "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
115,232
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, Jeremy Avigad, Minchao Wu, Mario Carneiro -/ import data.multiset.finset_ops import tactic.apply import tactic.nth_rewrite import tactic.monotonicity /-! # Finite sets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Terms of type `finset α` are one way of talking about finite subsets of `α` in mathlib. Below, `finset α` is defined as a structure with 2 fields: 1. `val` is a `multiset α` of elements; 2. `nodup` is a proof that `val` has no duplicates. Finsets in Lean are constructive in that they have an underlying `list` that enumerates their elements. In particular, any function that uses the data of the underlying list cannot depend on its ordering. This is handled on the `multiset` level by multiset API, so in most cases one needn't worry about it explicitly. Finsets give a basic foundation for defining finite sums and products over types: 1. `∑ i in (s : finset α), f i`; 2. `∏ i in (s : finset α), f i`. Lean refers to these operations as `big_operator`s. More information can be found in `algebra.big_operators.basic`. Finsets are directly used to define fintypes in Lean. A `fintype α` instance for a type `α` consists of a universal `finset α` containing every term of `α`, called `univ`. See `data.fintype.basic`. There is also `univ'`, the noncomputable partner to `univ`, which is defined to be `α` as a finset if `α` is finite, and the empty finset otherwise. See `data.fintype.basic`. `finset.card`, the size of a finset is defined in `data.finset.card`. This is then used to define `fintype.card`, the size of a type. ## Main declarations ### Main definitions * `finset`: Defines a type for the finite subsets of `α`. Constructing a `finset` requires two pieces of data: `val`, a `multiset α` of elements, and `nodup`, a proof that `val` has no duplicates. * `finset.has_mem`: Defines membership `a ∈ (s : finset α)`. * `finset.has_coe`: Provides a coercion `s : finset α` to `s : set α`. * `finset.has_coe_to_sort`: Coerce `s : finset α` to the type of all `x ∈ s`. * `finset.induction_on`: Induction on finsets. To prove a proposition about an arbitrary `finset α`, it suffices to prove it for the empty finset, and to show that if it holds for some `finset α`, then it holds for the finset obtained by inserting a new element. * `finset.choose`: Given a proof `h` of existence and uniqueness of a certain element satisfying a predicate, `choose s h` returns the element of `s` satisfying that predicate. ### Finset constructions * `singleton`: Denoted by `{a}`; the finset consisting of one element. * `finset.empty`: Denoted by `∅`. The finset associated to any type consisting of no elements. * `finset.range`: For any `n : ℕ`, `range n` is equal to `{0, 1, ... , n - 1} ⊆ ℕ`. This convention is consistent with other languages and normalizes `card (range n) = n`. Beware, `n` is not in `range n`. * `finset.attach`: Given `s : finset α`, `attach s` forms a finset of elements of the subtype `{a // a ∈ s}`; in other words, it attaches elements to a proof of membership in the set. ### Finsets from functions * `finset.filter`: Given a predicate `p : α → Prop`, `s.filter p` is the finset consisting of those elements in `s` satisfying the predicate `p`. ### The lattice structure on subsets of finsets There is a natural lattice structure on the subsets of a set. In Lean, we use lattice notation to talk about things involving unions and intersections. See `order.lattice`. For the lattice structure on finsets, `⊥` is called `bot` with `⊥ = ∅` and `⊤` is called `top` with `⊤ = univ`. * `finset.has_subset`: Lots of API about lattices, otherwise behaves exactly as one would expect. * `finset.has_union`: Defines `s ∪ t` (or `s ⊔ t`) as the union of `s` and `t`. See `finset.sup`/`finset.bUnion` for finite unions. * `finset.has_inter`: Defines `s ∩ t` (or `s ⊓ t`) as the intersection of `s` and `t`. See `finset.inf` for finite intersections. * `finset.disj_union`: Given a hypothesis `h` which states that finsets `s` and `t` are disjoint, `s.disj_union t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`; this does not require decidable equality on the type `α`. ### Operations on two or more finsets * `insert` and `finset.cons`: For any `a : α`, `insert s a` returns `s ∪ {a}`. `cons s a h` returns the same except that it requires a hypothesis stating that `a` is not already in `s`. This does not require decidable equality on the type `α`. * `finset.has_union`: see "The lattice structure on subsets of finsets" * `finset.has_inter`: see "The lattice structure on subsets of finsets" * `finset.erase`: For any `a : α`, `erase s a` returns `s` with the element `a` removed. * `finset.has_sdiff`: Defines the set difference `s \ t` for finsets `s` and `t`. * `finset.product`: Given finsets of `α` and `β`, defines finsets of `α × β`. For arbitrary dependent products, see `data.finset.pi`. * `finset.bUnion`: Finite unions of finsets; given an indexing function `f : α → finset β` and a `s : finset α`, `s.bUnion f` is the union of all finsets of the form `f a` for `a ∈ s`. * `finset.bInter`: TODO: Implemement finite intersections. ### Maps constructed using finsets * `finset.piecewise`: Given two functions `f`, `g`, `s.piecewise f g` is a function which is equal to `f` on `s` and `g` on the complement. ### Predicates on finsets * `disjoint`: defined via the lattice structure on finsets; two sets are disjoint if their intersection is empty. * `finset.nonempty`: A finset is nonempty if it has elements. This is equivalent to saying `s ≠ ∅`. TODO: Decide on the simp normal form. ### Equivalences between finsets * The `data.equiv` files describe a general type of equivalence, so look in there for any lemmas. There is some API for rewriting sums and products from `s` to `t` given that `s ≃ t`. TODO: examples ## Tags finite sets, finset -/ open multiset subtype nat function universes u variables {α : Type*} {β : Type*} {γ : Type*} /-- `finset α` is the type of finite sets of elements of `α`. It is implemented as a multiset (a list up to permutation) which has no duplicate elements. -/ structure finset (α : Type*) := (val : multiset α) (nodup : nodup val) instance multiset.can_lift_finset {α} : can_lift (multiset α) (finset α) finset.val multiset.nodup := ⟨λ m hm, ⟨⟨m, hm⟩, rfl⟩⟩ namespace finset theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t | ⟨s, _⟩ ⟨t, _⟩ rfl := rfl theorem val_injective : injective (val : finset α → multiset α) := λ _ _, eq_of_veq @[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t := val_injective.eq_iff @[simp] theorem dedup_eq_self [decidable_eq α] (s : finset α) : dedup s.1 = s.1 := s.2.dedup instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α) | s₁ s₂ := decidable_of_iff _ val_inj /-! ### membership -/ instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩ theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl @[simp] lemma mem_val {a : α} {s : finset α} : a ∈ s.1 ↔ a ∈ s := iff.rfl @[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) := multiset.decidable_mem _ _ /-! ### set coercion -/ /-- Convert a finset to a set in the natural way. -/ instance : has_coe_t (finset α) (set α) := ⟨λ s, {x | x ∈ s}⟩ @[simp, norm_cast] lemma mem_coe {a : α} {s : finset α} : a ∈ (s : set α) ↔ a ∈ s := iff.rfl @[simp] lemma set_of_mem {α} {s : finset α} : {a | a ∈ s} = s := rfl @[simp] lemma coe_mem {s : finset α} (x : (s : set α)) : ↑x ∈ s := x.2 @[simp] lemma mk_coe {s : finset α} (x : (s : set α)) {h} : (⟨x, h⟩ : (s : set α)) = x := subtype.coe_eta _ _ instance decidable_mem' [decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ (s : set α)) := s.decidable_mem _ /-! ### extensionality -/ theorem ext_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ := val_inj.symm.trans $ s₁.nodup.ext s₂.nodup @[ext] theorem ext {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := ext_iff.2 @[simp, norm_cast] theorem coe_inj {s₁ s₂ : finset α} : (s₁ : set α) = s₂ ↔ s₁ = s₂ := set.ext_iff.trans ext_iff.symm lemma coe_injective {α} : injective (coe : finset α → set α) := λ s t, coe_inj.1 /-! ### type coercion -/ /-- Coercion from a finset to the corresponding subtype. -/ instance {α : Type u} : has_coe_to_sort (finset α) (Type u) := ⟨λ s, {x // x ∈ s}⟩ @[simp] protected lemma forall_coe {α : Type*} (s : finset α) (p : s → Prop) : (∀ (x : s), p x) ↔ ∀ (x : α) (h : x ∈ s), p ⟨x, h⟩ := subtype.forall @[simp] protected lemma exists_coe {α : Type*} (s : finset α) (p : s → Prop) : (∃ (x : s), p x) ↔ ∃ (x : α) (h : x ∈ s), p ⟨x, h⟩ := subtype.exists instance pi_finset_coe.can_lift (ι : Type*) (α : Π i : ι, Type*) [ne : Π i, nonempty (α i)] (s : finset ι) : can_lift (Π i : s, α i) (Π i, α i) (λ f i, f i) (λ _, true) := pi_subtype.can_lift ι α (∈ s) instance pi_finset_coe.can_lift' (ι α : Type*) [ne : nonempty α] (s : finset ι) : can_lift (s → α) (ι → α) (λ f i, f i) (λ _, true) := pi_finset_coe.can_lift ι (λ _, α) s instance finset_coe.can_lift (s : finset α) : can_lift α s coe (λ a, a ∈ s) := { prf := λ a ha, ⟨⟨a, ha⟩, rfl⟩ } @[simp, norm_cast] lemma coe_sort_coe (s : finset α) : ((s : set α) : Sort*) = s := rfl /-! ### Subset and strict subset relations -/ section subset variables {s t : finset α} instance : has_subset (finset α) := ⟨λ s t, ∀ ⦃a⦄, a ∈ s → a ∈ t⟩ instance : has_ssubset (finset α) := ⟨λ s t, s ⊆ t ∧ ¬ t ⊆ s⟩ instance : partial_order (finset α) := { le := (⊆), lt := (⊂), le_refl := λ s a, id, le_trans := λ s t u hst htu a ha, htu $ hst ha, le_antisymm := λ s t hst hts, ext $ λ a, ⟨@hst _, @hts _⟩ } instance : is_refl (finset α) (⊆) := has_le.le.is_refl instance : is_trans (finset α) (⊆) := has_le.le.is_trans instance : is_antisymm (finset α) (⊆) := has_le.le.is_antisymm instance : is_irrefl (finset α) (⊂) := has_lt.lt.is_irrefl instance : is_trans (finset α) (⊂) := has_lt.lt.is_trans instance : is_asymm (finset α) (⊂) := has_lt.lt.is_asymm instance : is_nonstrict_strict_order (finset α) (⊆) (⊂) := ⟨λ _ _, iff.rfl⟩ lemma subset_def : s ⊆ t ↔ s.1 ⊆ t.1 := iff.rfl lemma ssubset_def : s ⊂ t ↔ s ⊆ t ∧ ¬ t ⊆ s := iff.rfl @[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _ protected lemma subset.rfl {s :finset α} : s ⊆ s := subset.refl _ protected theorem subset_of_eq {s t : finset α} (h : s = t) : s ⊆ t := h ▸ subset.refl _ theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans theorem superset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊇ s₂ → s₂ ⊇ s₃ → s₁ ⊇ s₃ := λ h' h, subset.trans h h' theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset lemma not_mem_mono {s t : finset α} (h : s ⊆ t) {a : α} : a ∉ t → a ∉ s := mt $ @h _ theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := ext $ λ a, ⟨@H₁ a, @H₂ a⟩ theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl @[simp, norm_cast] theorem coe_subset {s₁ s₂ : finset α} : (s₁ : set α) ⊆ s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2 theorem subset.antisymm_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ s₁ ⊆ s₂ ∧ s₂ ⊆ s₁ := le_antisymm_iff lemma not_subset : ¬ s ⊆ t ↔ ∃ x ∈ s, x ∉ t := by simp only [←coe_subset, set.not_subset, mem_coe] @[simp] theorem le_eq_subset : ((≤) : finset α → finset α → Prop) = (⊆) := rfl @[simp] theorem lt_eq_subset : ((<) : finset α → finset α → Prop) = (⊂) := rfl theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl @[simp, norm_cast] lemma coe_ssubset {s₁ s₂ : finset α} : (s₁ : set α) ⊂ s₂ ↔ s₁ ⊂ s₂ := show (s₁ : set α) ⊂ s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁, by simp only [set.ssubset_def, finset.coe_subset] @[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ := and_congr val_le_iff $ not_congr val_le_iff lemma ssubset_iff_subset_ne {s t : finset α} : s ⊂ t ↔ s ⊆ t ∧ s ≠ t := @lt_iff_le_and_ne _ _ s t theorem ssubset_iff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ⊂ s₂ ↔ ∃ x ∈ s₂, x ∉ s₁ := set.ssubset_iff_of_subset h lemma ssubset_of_ssubset_of_subset {s₁ s₂ s₃ : finset α} (hs₁s₂ : s₁ ⊂ s₂) (hs₂s₃ : s₂ ⊆ s₃) : s₁ ⊂ s₃ := set.ssubset_of_ssubset_of_subset hs₁s₂ hs₂s₃ lemma ssubset_of_subset_of_ssubset {s₁ s₂ s₃ : finset α} (hs₁s₂ : s₁ ⊆ s₂) (hs₂s₃ : s₂ ⊂ s₃) : s₁ ⊂ s₃ := set.ssubset_of_subset_of_ssubset hs₁s₂ hs₂s₃ lemma exists_of_ssubset {s₁ s₂ : finset α} (h : s₁ ⊂ s₂) : ∃ x ∈ s₂, x ∉ s₁ := set.exists_of_ssubset h instance is_well_founded_ssubset : is_well_founded (finset α) (⊂) := subrelation.is_well_founded (inv_image _ _) $ λ _ _, val_lt_iff.2 instance is_well_founded_lt : well_founded_lt (finset α) := finset.is_well_founded_ssubset end subset -- TODO: these should be global attributes, but this will require fixing other files local attribute [trans] subset.trans superset.trans /-! ### Order embedding from `finset α` to `set α` -/ /-- Coercion to `set α` as an `order_embedding`. -/ def coe_emb : finset α ↪o set α := ⟨⟨coe, coe_injective⟩, λ s t, coe_subset⟩ @[simp] lemma coe_coe_emb : ⇑(coe_emb : finset α ↪o set α) = coe := rfl /-! ### Nonempty -/ /-- The property `s.nonempty` expresses the fact that the finset `s` is not empty. It should be used in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : finset α) : Prop := ∃ x : α, x ∈ s instance decidable_nonempty {s : finset α} : decidable s.nonempty := decidable_of_iff (∃ a ∈ s, true) $ by simp_rw [exists_prop, and_true, finset.nonempty] @[simp, norm_cast] lemma coe_nonempty {s : finset α} : (s : set α).nonempty ↔ s.nonempty := iff.rfl @[simp] lemma nonempty_coe_sort {s : finset α} : nonempty ↥s ↔ s.nonempty := nonempty_subtype alias coe_nonempty ↔ _ nonempty.to_set alias nonempty_coe_sort ↔ _ nonempty.coe_sort lemma nonempty.bex {s : finset α} (h : s.nonempty) : ∃ x : α, x ∈ s := h lemma nonempty.mono {s t : finset α} (hst : s ⊆ t) (hs : s.nonempty) : t.nonempty := set.nonempty.mono hst hs lemma nonempty.forall_const {s : finset α} (h : s.nonempty) {p : Prop} : (∀ x ∈ s, p) ↔ p := let ⟨x, hx⟩ := h in ⟨λ h, h x hx, λ h x hx, h⟩ lemma nonempty.to_subtype {s : finset α} : s.nonempty → nonempty s := nonempty_coe_sort.2 lemma nonempty.to_type {s : finset α} : s.nonempty → nonempty α := λ ⟨x, hx⟩, ⟨x⟩ /-! ### empty -/ section empty variables {s : finset α} /-- The empty finset -/ protected def empty : finset α := ⟨0, nodup_zero⟩ instance : has_emptyc (finset α) := ⟨finset.empty⟩ instance inhabited_finset : inhabited (finset α) := ⟨∅⟩ @[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl @[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id @[simp] theorem not_nonempty_empty : ¬(∅ : finset α).nonempty := λ ⟨x, hx⟩, not_mem_empty x hx @[simp] theorem mk_zero : (⟨0, nodup_zero⟩ : finset α) = ∅ := rfl theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ := λ e, not_mem_empty a $ e ▸ h theorem nonempty.ne_empty {s : finset α} (h : s.nonempty) : s ≠ ∅ := exists.elim h $ λ a, ne_empty_of_mem @[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _ lemma eq_empty_of_forall_not_mem {s : finset α} (H : ∀ x, x ∉ s) : s = ∅ := eq_of_veq (eq_zero_of_forall_not_mem H) lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s := ⟨by rintro rfl x; exact id, λ h, eq_empty_of_forall_not_mem h⟩ @[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅ theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero @[simp] lemma not_ssubset_empty (s : finset α) : ¬s ⊂ ∅ := λ h, let ⟨x, he, hs⟩ := exists_of_ssubset h in he theorem nonempty_of_ne_empty {s : finset α} (h : s ≠ ∅) : s.nonempty := exists_mem_of_ne_zero (mt val_eq_zero.1 h) theorem nonempty_iff_ne_empty {s : finset α} : s.nonempty ↔ s ≠ ∅ := ⟨nonempty.ne_empty, nonempty_of_ne_empty⟩ @[simp] theorem not_nonempty_iff_eq_empty {s : finset α} : ¬s.nonempty ↔ s = ∅ := nonempty_iff_ne_empty.not.trans not_not theorem eq_empty_or_nonempty (s : finset α) : s = ∅ ∨ s.nonempty := classical.by_cases or.inl (λ h, or.inr (nonempty_of_ne_empty h)) @[simp, norm_cast] lemma coe_empty : ((∅ : finset α) : set α) = ∅ := rfl @[simp, norm_cast] lemma coe_eq_empty {s : finset α} : (s : set α) = ∅ ↔ s = ∅ := by rw [← coe_empty, coe_inj] @[simp] lemma is_empty_coe_sort {s : finset α} : is_empty ↥s ↔ s = ∅ := by simpa using @set.is_empty_coe_sort α s instance : is_empty (∅ : finset α) := is_empty_coe_sort.2 rfl /-- A `finset` for an empty type is empty. -/ lemma eq_empty_of_is_empty [is_empty α] (s : finset α) : s = ∅ := finset.eq_empty_of_forall_not_mem is_empty_elim instance : order_bot (finset α) := { bot := ∅, bot_le := empty_subset } @[simp] lemma bot_eq_empty : (⊥ : finset α) = ∅ := rfl @[simp] lemma empty_ssubset : ∅ ⊂ s ↔ s.nonempty := (@bot_lt_iff_ne_bot (finset α) _ _ _).trans nonempty_iff_ne_empty.symm alias empty_ssubset ↔ _ nonempty.empty_ssubset end empty /-! ### singleton -/ section singleton variables {s : finset α} {a b : α} /-- `{a} : finset a` is the set `{a}` containing `a` and nothing else. This differs from `insert a ∅` in that it does not require a `decidable_eq` instance for `α`. -/ instance : has_singleton α (finset α) := ⟨λ a, ⟨{a}, nodup_singleton a⟩⟩ @[simp] theorem singleton_val (a : α) : ({a} : finset α).1 = {a} := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ({a} : finset α) ↔ b = a := mem_singleton lemma eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : finset α)) : x = y := mem_singleton.1 h theorem not_mem_singleton {a b : α} : a ∉ ({b} : finset α) ↔ a ≠ b := not_congr mem_singleton theorem mem_singleton_self (a : α) : a ∈ ({a} : finset α) := or.inl rfl @[simp] lemma val_eq_singleton_iff {a : α} {s : finset α} : s.val = {a} ↔ s = {a} := by { rw ←val_inj, refl } lemma singleton_injective : injective (singleton : α → finset α) := λ a b h, mem_singleton.1 (h ▸ mem_singleton_self _) @[simp] lemma singleton_inj : ({a} : finset α) = {b} ↔ a = b := singleton_injective.eq_iff @[simp] theorem singleton_nonempty (a : α) : ({a} : finset α).nonempty := ⟨a, mem_singleton_self a⟩ @[simp] theorem singleton_ne_empty (a : α) : ({a} : finset α) ≠ ∅ := (singleton_nonempty a).ne_empty lemma empty_ssubset_singleton : (∅ : finset α) ⊂ {a} := (singleton_nonempty _).empty_ssubset @[simp, norm_cast] lemma coe_singleton (a : α) : (({a} : finset α) : set α) = {a} := by { ext, simp } @[simp, norm_cast] lemma coe_eq_singleton {s : finset α} {a : α} : (s : set α) = {a} ↔ s = {a} := by rw [←coe_singleton, coe_inj] lemma eq_singleton_iff_unique_mem {s : finset α} {a : α} : s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a := begin split; intro t, rw t, refine ⟨finset.mem_singleton_self _, λ _, finset.mem_singleton.1⟩, ext, rw finset.mem_singleton, refine ⟨t.right _, λ r, r.symm ▸ t.left⟩ end lemma eq_singleton_iff_nonempty_unique_mem {s : finset α} {a : α} : s = {a} ↔ s.nonempty ∧ ∀ x ∈ s, x = a := begin split, { rintro rfl, simp }, { rintros ⟨hne, h_uniq⟩, rw eq_singleton_iff_unique_mem, refine ⟨_, h_uniq⟩, rw ← h_uniq hne.some hne.some_spec, exact hne.some_spec } end lemma nonempty_iff_eq_singleton_default [unique α] {s : finset α} : s.nonempty ↔ s = {default} := by simp [eq_singleton_iff_nonempty_unique_mem] alias nonempty_iff_eq_singleton_default ↔ nonempty.eq_singleton_default _ lemma singleton_iff_unique_mem (s : finset α) : (∃ a, s = {a}) ↔ ∃! a, a ∈ s := by simp only [eq_singleton_iff_unique_mem, exists_unique] lemma singleton_subset_set_iff {s : set α} {a : α} : ↑({a} : finset α) ⊆ s ↔ a ∈ s := by rw [coe_singleton, set.singleton_subset_iff] @[simp] lemma singleton_subset_iff {s : finset α} {a : α} : {a} ⊆ s ↔ a ∈ s := singleton_subset_set_iff @[simp] lemma subset_singleton_iff {s : finset α} {a : α} : s ⊆ {a} ↔ s = ∅ ∨ s = {a} := by rw [←coe_subset, coe_singleton, set.subset_singleton_iff_eq, coe_eq_empty, coe_eq_singleton] lemma singleton_subset_singleton : ({a} : finset α) ⊆ {b} ↔ a = b := by simp protected lemma nonempty.subset_singleton_iff {s : finset α} {a : α} (h : s.nonempty) : s ⊆ {a} ↔ s = {a} := subset_singleton_iff.trans $ or_iff_right h.ne_empty lemma subset_singleton_iff' {s : finset α} {a : α} : s ⊆ {a} ↔ ∀ b ∈ s, b = a := forall₂_congr $ λ _ _, mem_singleton @[simp] lemma ssubset_singleton_iff {s : finset α} {a : α} : s ⊂ {a} ↔ s = ∅ := by rw [←coe_ssubset, coe_singleton, set.ssubset_singleton_iff, coe_eq_empty] lemma eq_empty_of_ssubset_singleton {s : finset α} {x : α} (hs : s ⊂ {x}) : s = ∅ := ssubset_singleton_iff.1 hs /-- A finset is nontrivial if it has at least two elements. -/ @[reducible] protected def nontrivial (s : finset α) : Prop := (s : set α).nontrivial @[simp] lemma not_nontrivial_empty : ¬ (∅ : finset α).nontrivial := by simp [finset.nontrivial] @[simp] lemma not_nontrivial_singleton : ¬ ({a} : finset α).nontrivial := by simp [finset.nontrivial] lemma nontrivial.ne_singleton (hs : s.nontrivial) : s ≠ {a} := by { rintro rfl, exact not_nontrivial_singleton hs } lemma eq_singleton_or_nontrivial (ha : a ∈ s) : s = {a} ∨ s.nontrivial := by { rw ←coe_eq_singleton, exact set.eq_singleton_or_nontrivial ha } lemma nontrivial_iff_ne_singleton (ha : a ∈ s) : s.nontrivial ↔ s ≠ {a} := ⟨nontrivial.ne_singleton, (eq_singleton_or_nontrivial ha).resolve_left⟩ lemma nonempty.exists_eq_singleton_or_nontrivial : s.nonempty → (∃ a, s = {a}) ∨ s.nontrivial := λ ⟨a, ha⟩, (eq_singleton_or_nontrivial ha).imp_left $ exists.intro a instance nontrivial' [nonempty α] : nontrivial (finset α) := ‹nonempty α›.elim $ λ a, ⟨⟨{a}, ∅, singleton_ne_empty _⟩⟩ instance [is_empty α] : unique (finset α) := { default := ∅, uniq := λ s, eq_empty_of_forall_not_mem is_empty_elim } end singleton /-! ### cons -/ section cons variables {s t : finset α} {a b : α} /-- `cons a s h` is the set `{a} ∪ s` containing `a` and the elements of `s`. It is the same as `insert a s` when it is defined, but unlike `insert a s` it does not require `decidable_eq α`, and the union is guaranteed to be disjoint. -/ def cons (a : α) (s : finset α) (h : a ∉ s) : finset α := ⟨a ::ₘ s.1, nodup_cons.2 ⟨h, s.2⟩⟩ @[simp] lemma mem_cons {h} : b ∈ s.cons a h ↔ b = a ∨ b ∈ s := mem_cons @[simp] lemma mem_cons_self (a : α) (s : finset α) {h} : a ∈ cons a s h := mem_cons_self _ _ @[simp] lemma cons_val (h : a ∉ s) : (cons a s h).1 = a ::ₘ s.1 := rfl lemma forall_mem_cons (h : a ∉ s) (p : α → Prop) : (∀ x, x ∈ cons a s h → p x) ↔ p a ∧ ∀ x, x ∈ s → p x := by simp only [mem_cons, or_imp_distrib, forall_and_distrib, forall_eq] /-- Useful in proofs by induction. -/ lemma forall_of_forall_cons {p : α → Prop} {h : a ∉ s} (H : ∀ x, x ∈ cons a s h → p x) (x) (h : x ∈ s) : p x := H _ $ mem_cons.2 $ or.inr h @[simp] lemma mk_cons {s : multiset α} (h : (a ::ₘ s).nodup) : (⟨a ::ₘ s, h⟩ : finset α) = cons a ⟨s, (nodup_cons.1 h).2⟩ (nodup_cons.1 h).1 := rfl @[simp] lemma cons_empty (a : α) : cons a ∅ (not_mem_empty _) = {a} := rfl @[simp] lemma nonempty_cons (h : a ∉ s) : (cons a s h).nonempty := ⟨a, mem_cons.2 $ or.inl rfl⟩ @[simp] lemma nonempty_mk {m : multiset α} {hm} : (⟨m, hm⟩ : finset α).nonempty ↔ m ≠ 0 := by induction m using multiset.induction_on; simp @[simp] lemma coe_cons {a s h} : (@cons α a s h : set α) = insert a s := by { ext, simp } lemma subset_cons (h : a ∉ s) : s ⊆ s.cons a h := subset_cons _ _ lemma ssubset_cons (h : a ∉ s) : s ⊂ s.cons a h := ssubset_cons h lemma cons_subset {h : a ∉ s} : s.cons a h ⊆ t ↔ a ∈ t ∧ s ⊆ t := cons_subset @[simp] lemma cons_subset_cons {hs ht} : s.cons a hs ⊆ t.cons a ht ↔ s ⊆ t := by rwa [← coe_subset, coe_cons, coe_cons, set.insert_subset_insert_iff, coe_subset] lemma ssubset_iff_exists_cons_subset : s ⊂ t ↔ ∃ a (h : a ∉ s), s.cons a h ⊆ t := begin refine ⟨λ h, _, λ ⟨a, ha, h⟩, ssubset_of_ssubset_of_subset (ssubset_cons _) h⟩, obtain ⟨a, hs, ht⟩ := not_subset.1 h.2, exact ⟨a, ht, cons_subset.2 ⟨hs, h.subset⟩⟩, end end cons /-! ### disjoint -/ section disjoint variables {f : α → β} {s t u : finset α} {a b : α} lemma disjoint_left : disjoint s t ↔ ∀ ⦃a⦄, a ∈ s → a ∉ t := ⟨λ h a hs ht, singleton_subset_iff.mp (h (singleton_subset_iff.mpr hs) (singleton_subset_iff.mpr ht)), λ h x hs ht a ha, h (hs ha) (ht ha)⟩ lemma disjoint_right : disjoint s t ↔ ∀ ⦃a⦄, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] lemma disjoint_iff_ne : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b := by simp only [disjoint_left, imp_not_comm, forall_eq'] @[simp] lemma disjoint_val : s.1.disjoint t.1 ↔ disjoint s t := disjoint_left.symm lemma _root_.disjoint.forall_ne_finset (h : disjoint s t) (ha : a ∈ s) (hb : b ∈ t) : a ≠ b := disjoint_iff_ne.1 h _ ha _ hb lemma not_disjoint_iff : ¬ disjoint s t ↔ ∃ a, a ∈ s ∧ a ∈ t := disjoint_left.not.trans $ not_forall.trans $ exists_congr $ λ _, by rw [not_imp, not_not] lemma disjoint_of_subset_left (h : s ⊆ u) (d : disjoint u t) : disjoint s t := disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁)) lemma disjoint_of_subset_right (h : t ⊆ u) (d : disjoint s u) : disjoint s t := disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁)) @[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left @[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right @[simp] lemma disjoint_singleton_left : disjoint (singleton a) s ↔ a ∉ s := by simp only [disjoint_left, mem_singleton, forall_eq] @[simp] lemma disjoint_singleton_right : disjoint s (singleton a) ↔ a ∉ s := disjoint.comm.trans disjoint_singleton_left @[simp] lemma disjoint_singleton : disjoint ({a} : finset α) {b} ↔ a ≠ b := by rw [disjoint_singleton_left, mem_singleton] lemma disjoint_self_iff_empty (s : finset α) : disjoint s s ↔ s = ∅ := disjoint_self @[simp, norm_cast] lemma disjoint_coe : disjoint (s : set α) t ↔ disjoint s t := by { rw [finset.disjoint_left, set.disjoint_left], refl } @[simp, norm_cast] lemma pairwise_disjoint_coe {ι : Type*} {s : set ι} {f : ι → finset α} : s.pairwise_disjoint (λ i, f i : ι → set α) ↔ s.pairwise_disjoint f := forall₅_congr $ λ _ _ _ _ _, disjoint_coe end disjoint /-! ### disjoint union -/ /-- `disj_union s t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`. It is the same as `s ∪ t`, but it does not require decidable equality on the type. The hypothesis ensures that the sets are disjoint. -/ def disj_union (s t : finset α) (h : disjoint s t) : finset α := ⟨s.1 + t.1, multiset.nodup_add.2 ⟨s.2, t.2, disjoint_val.2 h⟩⟩ @[simp] theorem mem_disj_union {α s t h a} : a ∈ @disj_union α s t h ↔ a ∈ s ∨ a ∈ t := by rcases s with ⟨⟨s⟩⟩; rcases t with ⟨⟨t⟩⟩; apply list.mem_append lemma disj_union_comm (s t : finset α) (h : disjoint s t) : disj_union s t h = disj_union t s h.symm := eq_of_veq $ add_comm _ _ @[simp] lemma empty_disj_union (t : finset α) (h : disjoint ∅ t := disjoint_bot_left) : disj_union ∅ t h = t := eq_of_veq $ zero_add _ @[simp] lemma disj_union_empty (s : finset α) (h : disjoint s ∅ := disjoint_bot_right) : disj_union s ∅ h = s := eq_of_veq $ add_zero _ lemma singleton_disj_union (a : α) (t : finset α) (h : disjoint {a} t) : disj_union {a} t h = cons a t (disjoint_singleton_left.mp h) := eq_of_veq $ multiset.singleton_add _ _ lemma disj_union_singleton (s : finset α) (a : α) (h : disjoint s {a}) : disj_union s {a} h = cons a s (disjoint_singleton_right.mp h) := by rw [disj_union_comm, singleton_disj_union] /-! ### insert -/ section insert variables [decidable_eq α] {s t u v : finset α} {a b : α} /-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/ instance : has_insert α (finset α) := ⟨λ a s, ⟨_, s.2.ndinsert a⟩⟩ lemma insert_def (a : α) (s : finset α) : insert a s = ⟨_, s.2.ndinsert a⟩ := rfl @[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = dedup (a ::ₘ s.1) := by rw [dedup_cons, dedup_eq_self]; refl theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a ::ₘ s.1 := by rw [insert_val, ndinsert_of_not_mem h] @[simp] lemma mem_insert : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := mem_ndinsert_self a s.1 lemma mem_insert_of_mem (h : a ∈ s) : a ∈ insert b s := mem_ndinsert_of_mem h lemma mem_of_mem_insert_of_ne (h : b ∈ insert a s) : b ≠ a → b ∈ s := (mem_insert.1 h).resolve_left lemma eq_of_not_mem_of_mem_insert (ha : b ∈ insert a s) (hb : b ∉ s) : b = a := (mem_insert.1 ha).resolve_right hb @[simp] theorem cons_eq_insert (a s h) : @cons α a s h = insert a s := ext $ λ a, by simp @[simp, norm_cast] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a s : set α) := set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff] lemma mem_insert_coe {s : finset α} {x y : α} : x ∈ insert y s ↔ x ∈ insert y (s : set α) := by simp instance : is_lawful_singleton α (finset α) := ⟨λ a, by { ext, simp }⟩ @[simp] lemma insert_eq_of_mem (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h @[simp] lemma insert_eq_self : insert a s = s ↔ a ∈ s := ⟨λ h, h ▸ mem_insert_self _ _, insert_eq_of_mem⟩ lemma insert_ne_self : insert a s ≠ s ↔ a ∉ s := insert_eq_self.not @[simp] theorem pair_eq_singleton (a : α) : ({a, a} : finset α) = {a} := insert_eq_of_mem $ mem_singleton_self _ theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) := ext $ λ x, by simp only [mem_insert, or.left_comm] @[simp, norm_cast] lemma coe_pair {a b : α} : (({a, b} : finset α) : set α) = {a, b} := by { ext, simp } @[simp, norm_cast] lemma coe_eq_pair {s : finset α} {a b : α} : (s : set α) = {a, b} ↔ s = {a, b} := by rw [←coe_pair, coe_inj] theorem pair_comm (a b : α) : ({a, b} : finset α) = {b, a} := insert.comm a b ∅ @[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s := ext $ λ x, by simp only [mem_insert, or.assoc.symm, or_self] @[simp] theorem insert_nonempty (a : α) (s : finset α) : (insert a s).nonempty := ⟨a, mem_insert_self a s⟩ @[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ := (insert_nonempty a s).ne_empty /-! The universe annotation is required for the following instance, possibly this is a bug in Lean. See leanprover.zulipchat.com/#narrow/stream/113488-general/topic/strange.20error.20(universe.20issue.3F) -/ instance {α : Type u} [decidable_eq α] (i : α) (s : finset α) : nonempty.{u + 1} ((insert i s : finset α) : set α) := (finset.coe_nonempty.mpr (s.insert_nonempty i)).to_subtype lemma ne_insert_of_not_mem (s t : finset α) {a : α} (h : a ∉ s) : s ≠ insert a t := by { contrapose! h, simp [h] } lemma insert_subset : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib] lemma subset_insert (a : α) (s : finset α) : s ⊆ insert a s := λ b, mem_insert_of_mem theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t := insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩ lemma insert_inj (ha : a ∉ s) : insert a s = insert b s ↔ a = b := ⟨λ h, eq_of_not_mem_of_mem_insert (h.subst $ mem_insert_self _ _) ha, congr_arg _⟩ lemma insert_inj_on (s : finset α) : set.inj_on (λ a, insert a s) sᶜ := λ a h b _, (insert_inj h).1 lemma ssubset_iff : s ⊂ t ↔ ∃ a ∉ s, insert a s ⊆ t := by exact_mod_cast @set.ssubset_iff_insert α s t lemma ssubset_insert (h : a ∉ s) : s ⊂ insert a s := ssubset_iff.mpr ⟨a, h, subset.rfl⟩ @[elab_as_eliminator] lemma cons_induction {α : Type*} {p : finset α → Prop} (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α} (h : a ∉ s), p s → p (cons a s h)) : ∀ s, p s | ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin cases nodup_cons.1 nd with m nd', rw [← (eq_of_veq _ : cons a (finset.mk s _) m = ⟨a ::ₘ s, nd⟩)], { exact h₂ (by exact m) (IH nd') }, { rw [cons_val] } end) nd @[elab_as_eliminator] lemma cons_induction_on {α : Type*} {p : finset α → Prop} (s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α} (h : a ∉ s), p s → p (cons a s h)) : p s := cons_induction h₁ h₂ s @[elab_as_eliminator] protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α] (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s := cons_induction h₁ $ λ a s ha, (s.cons_eq_insert a ha).symm ▸ h₂ ha /-- To prove a proposition about an arbitrary `finset α`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset α`, then it holds for the `finset` obtained by inserting a new element. -/ @[elab_as_eliminator] protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α] (s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s := finset.induction h₁ h₂ s /-- To prove a proposition about `S : finset α`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset α ⊆ S`, then it holds for the `finset` obtained by inserting a new element of `S`. -/ @[elab_as_eliminator] theorem induction_on' {α : Type*} {p : finset α → Prop} [decidable_eq α] (S : finset α) (h₁ : p ∅) (h₂ : ∀ {a s}, a ∈ S → s ⊆ S → a ∉ s → p s → p (insert a s)) : p S := @finset.induction_on α (λ T, T ⊆ S → p T) _ S (λ _, h₁) (λ a s has hqs hs, let ⟨hS, sS⟩ := finset.insert_subset.1 hs in h₂ hS sS has (hqs sS)) (finset.subset.refl S) /-- To prove a proposition about a nonempty `s : finset α`, it suffices to show it holds for all singletons and that if it holds for nonempty `t : finset α`, then it also holds for the `finset` obtained by inserting an element in `t`. -/ @[elab_as_eliminator] lemma nonempty.cons_induction {α : Type*} {p : Π s : finset α, s.nonempty → Prop} (h₀ : ∀ a, p {a} (singleton_nonempty _)) (h₁ : ∀ ⦃a⦄ s (h : a ∉ s) hs, p s hs → p (finset.cons a s h) (nonempty_cons h)) {s : finset α} (hs : s.nonempty) : p s hs := begin induction s using finset.cons_induction with a t ha h, { exact (not_nonempty_empty hs).elim }, obtain rfl | ht := t.eq_empty_or_nonempty, { exact h₀ a }, { exact h₁ t ha ht (h ht) } end /-- Inserting an element to a finite set is equivalent to the option type. -/ def subtype_insert_equiv_option {t : finset α} {x : α} (h : x ∉ t) : {i // i ∈ insert x t} ≃ option {i // i ∈ t} := begin refine { to_fun := λ y, if h : ↑y = x then none else some ⟨y, (mem_insert.mp y.2).resolve_left h⟩, inv_fun := λ y, y.elim ⟨x, mem_insert_self _ _⟩ $ λ z, ⟨z, mem_insert_of_mem z.2⟩, .. }, { intro y, by_cases h : ↑y = x, simp only [subtype.ext_iff, h, option.elim, dif_pos, subtype.coe_mk], simp only [h, option.elim, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, { rintro (_|y), simp only [option.elim, dif_pos, subtype.coe_mk], have : ↑y ≠ x, { rintro ⟨⟩, exact h y.2 }, simp only [this, option.elim, subtype.eta, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, end @[simp] lemma disjoint_insert_left : disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t := by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] @[simp] lemma disjoint_insert_right : disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t := disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm] end insert /-! ### Lattice structure -/ section lattice variables [decidable_eq α] {s s₁ s₂ t t₁ t₂ u v : finset α} {a b : α} /-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/ instance : has_union (finset α) := ⟨λ s t, ⟨_, t.2.ndunion s.1⟩⟩ /-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/ instance : has_inter (finset α) := ⟨λ s t, ⟨_, s.2.ndinter t.1⟩⟩ instance : lattice (finset α) := { sup := (∪), sup_le := λ s t u hs ht a ha, (mem_ndunion.1 ha).elim (λ h, hs h) (λ h, ht h), le_sup_left := λ s t a h, mem_ndunion.2 $ or.inl h, le_sup_right := λ s t a h, mem_ndunion.2 $ or.inr h, inf := (∩), le_inf := λ s t u ht hu a h, mem_ndinter.2 ⟨ht h, hu h⟩, inf_le_left := λ s t a h, (mem_ndinter.1 h).1, inf_le_right := λ s t a h, (mem_ndinter.1 h).2, ..finset.partial_order } @[simp] lemma sup_eq_union : ((⊔) : finset α → finset α → finset α) = (∪) := rfl @[simp] lemma inf_eq_inter : ((⊓) : finset α → finset α → finset α) = (∩) := rfl lemma disjoint_iff_inter_eq_empty : disjoint s t ↔ s ∩ t = ∅ := disjoint_iff instance decidable_disjoint (U V : finset α) : decidable (disjoint U V) := decidable_of_iff _ disjoint_left.symm /-! #### union -/ lemma union_val_nd (s t : finset α) : (s ∪ t).1 = ndunion s.1 t.1 := rfl @[simp] lemma union_val (s t : finset α) : (s ∪ t).1 = s.1 ∪ t.1 := ndunion_eq_union s.2 @[simp] lemma mem_union : a ∈ s ∪ t ↔ a ∈ s ∨ a ∈ t := mem_ndunion @[simp] lemma disj_union_eq_union (s t h) : @disj_union α s t h = s ∪ t := ext $ λ a, by simp lemma mem_union_left (t : finset α) (h : a ∈ s) : a ∈ s ∪ t := mem_union.2 $ or.inl h lemma mem_union_right (s : finset α) (h : a ∈ t) : a ∈ s ∪ t := mem_union.2 $ or.inr h lemma forall_mem_union {p : α → Prop} : (∀ a ∈ s ∪ t, p a) ↔ (∀ a ∈ s, p a) ∧ ∀ a ∈ t, p a := ⟨λ h, ⟨λ a, h a ∘ mem_union_left _, λ b, h b ∘ mem_union_right _⟩, λ h ab hab, (mem_union.mp hab).elim (h.1 _) (h.2 _)⟩ lemma not_mem_union : a ∉ s ∪ t ↔ a ∉ s ∧ a ∉ t := by rw [mem_union, not_or_distrib] @[simp, norm_cast] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (s₁ ∪ s₂ : set α) := set.ext $ λ x, mem_union lemma union_subset (hs : s ⊆ u) : t ⊆ u → s ∪ t ⊆ u := sup_le $ le_iff_subset.2 hs theorem subset_union_left (s₁ s₂ : finset α) : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _ theorem subset_union_right (s₁ s₂ : finset α) : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _ lemma union_subset_union (hsu : s ⊆ u) (htv : t ⊆ v) : s ∪ t ⊆ u ∪ v := sup_le_sup (le_iff_subset.2 hsu) htv lemma union_subset_union_left (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t := union_subset_union h subset.rfl lemma union_subset_union_right (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ := union_subset_union subset.rfl h lemma union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := sup_comm instance : is_commutative (finset α) (∪) := ⟨union_comm⟩ @[simp] lemma union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := sup_assoc instance : is_associative (finset α) (∪) := ⟨union_assoc⟩ @[simp] lemma union_idempotent (s : finset α) : s ∪ s = s := sup_idem instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩ lemma union_subset_left (h : s ∪ t ⊆ u) : s ⊆ u := (subset_union_left _ _).trans h lemma union_subset_right {s t u : finset α} (h : s ∪ t ⊆ u) : t ⊆ u := subset.trans (subset_union_right _ _) h lemma union_left_comm (s t u : finset α) : s ∪ (t ∪ u) = t ∪ (s ∪ u) := ext $ λ _, by simp only [mem_union, or.left_comm] lemma union_right_comm (s t u : finset α) : (s ∪ t) ∪ u = (s ∪ u) ∪ t := ext $ λ x, by simp only [mem_union, or_assoc, or_comm (x ∈ t)] theorem union_self (s : finset α) : s ∪ s = s := union_idempotent s @[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s := ext $ λ x, mem_union.trans $ or_false _ @[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s := ext $ λ x, mem_union.trans $ false_or _ theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := rfl @[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) := by simp only [insert_eq, union_assoc] @[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) := by simp only [insert_eq, union_left_comm] lemma insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t := by simp only [insert_union, union_insert, insert_idem] @[simp] lemma union_eq_left_iff_subset {s t : finset α} : s ∪ t = s ↔ t ⊆ s := sup_eq_left @[simp] lemma left_eq_union_iff_subset {s t : finset α} : s = s ∪ t ↔ t ⊆ s := by rw [← union_eq_left_iff_subset, eq_comm] @[simp] lemma union_eq_right_iff_subset {s t : finset α} : s ∪ t = t ↔ s ⊆ t := sup_eq_right @[simp] lemma right_eq_union_iff_subset {s t : finset α} : s = t ∪ s ↔ t ⊆ s := by rw [← union_eq_right_iff_subset, eq_comm] lemma union_congr_left (ht : t ⊆ s ∪ u) (hu : u ⊆ s ∪ t) : s ∪ t = s ⊔ u := sup_congr_left ht hu lemma union_congr_right (hs : s ⊆ t ∪ u) (ht : t ⊆ s ∪ u) : s ∪ u = t ∪ u := sup_congr_right hs ht lemma union_eq_union_iff_left : s ∪ t = s ∪ u ↔ t ⊆ s ∪ u ∧ u ⊆ s ∪ t := sup_eq_sup_iff_left lemma union_eq_union_iff_right : s ∪ u = t ∪ u ↔ s ⊆ t ∪ u ∧ t ⊆ s ∪ u := sup_eq_sup_iff_right @[simp] lemma disjoint_union_left : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib] @[simp] lemma disjoint_union_right : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib] /-- To prove a relation on pairs of `finset X`, it suffices to show that it is * symmetric, * it holds when one of the `finset`s is empty, * it holds for pairs of singletons, * if it holds for `[a, c]` and for `[b, c]`, then it holds for `[a ∪ b, c]`. -/ lemma induction_on_union (P : finset α → finset α → Prop) (symm : ∀ {a b}, P a b → P b a) (empty_right : ∀ {a}, P a ∅) (singletons : ∀ {a b}, P {a} {b}) (union_of : ∀ {a b c}, P a c → P b c → P (a ∪ b) c) : ∀ a b, P a b := begin intros a b, refine finset.induction_on b empty_right (λ x s xs hi, symm _), rw finset.insert_eq, apply union_of _ (symm hi), refine finset.induction_on a empty_right (λ a t ta hi, symm _), rw finset.insert_eq, exact union_of singletons (symm hi), end lemma _root_.directed.exists_mem_subset_of_finset_subset_bUnion {α ι : Type*} [hn : nonempty ι] {f : ι → set α} (h : directed (⊆) f) {s : finset α} (hs : (s : set α) ⊆ ⋃ i, f i) : ∃ i, (s : set α) ⊆ f i := begin classical, revert hs, apply s.induction_on, { refine λ _, ⟨hn.some, _⟩, simp only [coe_empty, set.empty_subset], }, { intros b t hbt htc hbtc, obtain ⟨i : ι , hti : (t : set α) ⊆ f i⟩ := htc (set.subset.trans (t.subset_insert b) hbtc), obtain ⟨j, hbj⟩ : ∃ j, b ∈ f j, by simpa [set.mem_Union₂] using hbtc (t.mem_insert_self b), rcases h j i with ⟨k, hk, hk'⟩, use k, rw [coe_insert, set.insert_subset], exact ⟨hk hbj, trans hti hk'⟩ } end lemma _root_.directed_on.exists_mem_subset_of_finset_subset_bUnion {α ι : Type*} {f : ι → set α} {c : set ι} (hn : c.nonempty) (hc : directed_on (λ i j, f i ⊆ f j) c) {s : finset α} (hs : (s : set α) ⊆ ⋃ i ∈ c, f i) : ∃ i ∈ c, (s : set α) ⊆ f i := begin rw set.bUnion_eq_Union at hs, haveI := hn.coe_sort, obtain ⟨⟨i, hic⟩, hi⟩ := (directed_comp.2 hc.directed_coe).exists_mem_subset_of_finset_subset_bUnion hs, exact ⟨i, hic, hi⟩ end /-! #### inter -/ theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl @[simp] lemma inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 := ndinter_eq_inter s₁.2 @[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₁ := (mem_inter.1 h).1 theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₂ := (mem_inter.1 h).2 theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ := and_imp.1 mem_inter.2 theorem inter_subset_left (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left theorem inter_subset_right (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right lemma subset_inter {s₁ s₂ u : finset α} : s₁ ⊆ s₂ → s₁ ⊆ u → s₁ ⊆ s₂ ∩ u := by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial @[simp, norm_cast] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (s₁ ∩ s₂ : set α) := set.ext $ λ _, mem_inter @[simp] theorem union_inter_cancel_left {s t : finset α} : (s ∪ t) ∩ s = s := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_left] @[simp] theorem union_inter_cancel_right {s t : finset α} : (s ∪ t) ∩ t = t := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_right] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext $ λ _, by simp only [mem_inter, and_comm] @[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext $ λ _, by simp only [mem_inter, and_assoc] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext $ λ _, by simp only [mem_inter, and.left_comm] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext $ λ _, by simp only [mem_inter, and.right_comm] @[simp] lemma inter_self (s : finset α) : s ∩ s = s := ext $ λ _, mem_inter.trans $ and_self _ @[simp] lemma inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext $ λ _, mem_inter.trans $ and_false _ @[simp] lemma empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext $ λ _, mem_inter.trans $ false_and _ @[simp] lemma inter_union_self (s t : finset α) : s ∩ (t ∪ s) = s := by rw [inter_comm, union_inter_cancel_right] @[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) : insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) := ext $ λ x, have x = a ∨ x ∈ s₂ ↔ x ∈ s₂, from or_iff_right_of_imp $ by rintro rfl; exact h, by simp only [mem_inter, mem_insert, or_and_distrib_left, this] @[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) : s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) := by rw [inter_comm, insert_inter_of_mem h, inter_comm] @[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) : insert a s₁ ∩ s₂ = s₁ ∩ s₂ := ext $ λ x, have ¬ (x = a ∧ x ∈ s₂), by rintro ⟨rfl, H⟩; exact h H, by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or] @[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) : s₁ ∩ insert a s₂ = s₁ ∩ s₂ := by rw [inter_comm, insert_inter_of_not_mem h, inter_comm] @[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} (H : a ∈ s) : {a} ∩ s = {a} := show insert a ∅ ∩ s = insert a ∅, by rw [insert_inter_of_mem H, empty_inter] @[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} (H : a ∉ s) : {a} ∩ s = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h @[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ {a} = {a} := by rw [inter_comm, singleton_inter_of_mem h] @[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ {a} = ∅ := by rw [inter_comm, singleton_inter_of_not_mem h] @[mono] lemma inter_subset_inter {x y s t : finset α} (h : x ⊆ y) (h' : s ⊆ t) : x ∩ s ⊆ y ∩ t := begin intros a a_in, rw finset.mem_inter at a_in ⊢, exact ⟨h a_in.1, h' a_in.2⟩ end lemma inter_subset_inter_left (h : t ⊆ u) : s ∩ t ⊆ s ∩ u := inter_subset_inter subset.rfl h lemma inter_subset_inter_right (h : s ⊆ t) : s ∩ u ⊆ t ∩ u := inter_subset_inter h subset.rfl lemma inter_subset_union : s ∩ t ⊆ s ∪ t := le_iff_subset.1 inf_le_sup instance : distrib_lattice (finset α) := { le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c, by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt}; simp only [true_or, imp_true_iff, true_and, or_true], ..finset.lattice } @[simp] theorem union_left_idem (s t : finset α) : s ∪ (s ∪ t) = s ∪ t := sup_left_idem @[simp] theorem union_right_idem (s t : finset α) : s ∪ t ∪ t = s ∪ t := sup_right_idem @[simp] theorem inter_left_idem (s t : finset α) : s ∩ (s ∩ t) = s ∩ t := inf_left_idem @[simp] theorem inter_right_idem (s t : finset α) : s ∩ t ∩ t = s ∩ t := inf_right_idem theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right lemma union_union_distrib_left (s t u : finset α) : s ∪ (t ∪ u) = (s ∪ t) ∪ (s ∪ u) := sup_sup_distrib_left _ _ _ lemma union_union_distrib_right (s t u : finset α) : (s ∪ t) ∪ u = (s ∪ u) ∪ (t ∪ u) := sup_sup_distrib_right _ _ _ lemma inter_inter_distrib_left (s t u : finset α) : s ∩ (t ∩ u) = (s ∩ t) ∩ (s ∩ u) := inf_inf_distrib_left _ _ _ lemma inter_inter_distrib_right (s t u : finset α) : (s ∩ t) ∩ u = (s ∩ u) ∩ (t ∩ u) := inf_inf_distrib_right _ _ _ lemma union_union_union_comm (s t u v : finset α) : (s ∪ t) ∪ (u ∪ v) = (s ∪ u) ∪ (t ∪ v) := sup_sup_sup_comm _ _ _ _ lemma inter_inter_inter_comm (s t u v : finset α) : (s ∩ t) ∩ (u ∩ v) = (s ∩ u) ∩ (t ∩ v) := inf_inf_inf_comm _ _ _ _ lemma union_eq_empty_iff (A B : finset α) : A ∪ B = ∅ ↔ A = ∅ ∧ B = ∅ := sup_eq_bot_iff lemma union_subset_iff : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := (sup_le_iff : s ⊔ t ≤ u ↔ s ≤ u ∧ t ≤ u) lemma subset_inter_iff : s ⊆ t ∩ u ↔ s ⊆ t ∧ s ⊆ u := (le_inf_iff : s ≤ t ⊓ u ↔ s ≤ t ∧ s ≤ u) @[simp] lemma inter_eq_left_iff_subset (s t : finset α) : s ∩ t = s ↔ s ⊆ t := inf_eq_left @[simp] lemma inter_eq_right_iff_subset (s t : finset α) : t ∩ s = s ↔ s ⊆ t := inf_eq_right lemma inter_congr_left (ht : s ∩ u ⊆ t) (hu : s ∩ t ⊆ u) : s ∩ t = s ∩ u := inf_congr_left ht hu lemma inter_congr_right (hs : t ∩ u ⊆ s) (ht : s ∩ u ⊆ t) : s ∩ u = t ∩ u := inf_congr_right hs ht lemma inter_eq_inter_iff_left : s ∩ t = s ∩ u ↔ s ∩ u ⊆ t ∧ s ∩ t ⊆ u := inf_eq_inf_iff_left lemma inter_eq_inter_iff_right : s ∩ u = t ∩ u ↔ t ∩ u ⊆ s ∧ s ∩ u ⊆ t := inf_eq_inf_iff_right lemma ite_subset_union (s s' : finset α) (P : Prop) [decidable P] : ite P s s' ⊆ s ∪ s' := ite_le_sup s s' P lemma inter_subset_ite (s s' : finset α) (P : Prop) [decidable P] : s ∩ s' ⊆ ite P s s' := inf_le_ite s s' P lemma not_disjoint_iff_nonempty_inter : ¬disjoint s t ↔ (s ∩ t).nonempty := not_disjoint_iff.trans $ by simp [finset.nonempty] alias not_disjoint_iff_nonempty_inter ↔ _ nonempty.not_disjoint lemma disjoint_or_nonempty_inter (s t : finset α) : disjoint s t ∨ (s ∩ t).nonempty := by { rw ←not_disjoint_iff_nonempty_inter, exact em _ } end lattice /-! ### erase -/ section erase variables [decidable_eq α] {s t u v : finset α} {a b : α} /-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are not equal to `a`. -/ def erase (s : finset α) (a : α) : finset α := ⟨_, s.2.erase a⟩ @[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl @[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s := s.2.mem_erase_iff lemma not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := s.2.not_mem_erase -- While this can be solved by `simp`, this lemma is eligible for `dsimp` @[nolint simp_nf, simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl @[simp] lemma erase_singleton (a : α) : ({a} : finset α).erase a = ∅ := begin ext x, rw [mem_erase, mem_singleton, not_and_self], refl, end lemma ne_of_mem_erase : b ∈ erase s a → b ≠ a := λ h, (mem_erase.1 h).1 lemma mem_of_mem_erase : b ∈ erase s a → b ∈ s := mem_of_mem_erase lemma mem_erase_of_ne_of_mem : a ≠ b → a ∈ s → a ∈ erase s b := by simp only [mem_erase]; exact and.intro /-- An element of `s` that is not an element of `erase s a` must be `a`. -/ lemma eq_of_mem_of_not_mem_erase (hs : b ∈ s) (hsa : b ∉ s.erase a) : b = a := begin rw [mem_erase, not_and] at hsa, exact not_imp_not.mp hsa hs end @[simp] theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s := eq_of_veq $ erase_of_not_mem h @[simp] lemma erase_eq_self : s.erase a = s ↔ a ∉ s := ⟨λ h, h ▸ not_mem_erase _ _, erase_eq_of_not_mem⟩ @[simp] lemma erase_insert_eq_erase (s : finset α) (a : α) : (insert a s).erase a = s.erase a := ext $ λ x, by simp only [mem_erase, mem_insert, and.congr_right_iff, false_or, iff_self, implies_true_iff] { contextual := tt } theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s := by rw [erase_insert_eq_erase, erase_eq_of_not_mem h] theorem erase_insert_of_ne {a b : α} {s : finset α} (h : a ≠ b) : erase (insert a s) b = insert a (erase s b) := ext $ λ x, have x ≠ b ∧ x = a ↔ x = a, from and_iff_right_of_imp (λ hx, hx.symm ▸ h), by simp only [mem_erase, mem_insert, and_or_distrib_left, this] theorem erase_cons_of_ne {a b : α} {s : finset α} (ha : a ∉ s) (hb : a ≠ b) : erase (cons a s ha) b = cons a (erase s b) (λ h, ha $ erase_subset _ _ h) := by simp only [cons_eq_insert, erase_insert_of_ne hb] theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s := ext $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and]; apply or_iff_right_of_imp; rintro rfl; exact h theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a := val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _ lemma subset_erase {a : α} {s t : finset α} : s ⊆ t.erase a ↔ s ⊆ t ∧ a ∉ s := ⟨λ h, ⟨h.trans (erase_subset _ _), λ ha, not_mem_erase _ _ (h ha)⟩, λ h b hb, mem_erase.2 ⟨ne_of_mem_of_not_mem hb h.2, h.1 hb⟩⟩ @[simp, norm_cast] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (s \ {a} : set α) := set.ext $ λ _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _ ... = _ : insert_erase h lemma ssubset_iff_exists_subset_erase {s t : finset α} : s ⊂ t ↔ ∃ a ∈ t, s ⊆ t.erase a := begin refine ⟨λ h, _, λ ⟨a, ha, h⟩, ssubset_of_subset_of_ssubset h $ erase_ssubset ha⟩, obtain ⟨a, ht, hs⟩ := not_subset.1 h.2, exact ⟨a, ht, subset_erase.2 ⟨h.1, hs⟩⟩, end lemma erase_ssubset_insert (s : finset α) (a : α) : s.erase a ⊂ insert a s := ssubset_iff_exists_subset_erase.2 ⟨a, mem_insert_self _ _, erase_subset_erase _ $ subset_insert _ _⟩ lemma erase_ne_self : s.erase a ≠ s ↔ a ∈ s := erase_eq_self.not_left lemma erase_cons {s : finset α} {a : α} (h : a ∉ s) : (s.cons a h).erase a = s := by rw [cons_eq_insert, erase_insert_eq_erase, erase_eq_of_not_mem h] lemma erase_idem {a : α} {s : finset α} : erase (erase s a) a = erase s a := by simp lemma erase_right_comm {a b : α} {s : finset α} : erase (erase s a) b = erase (erase s b) a := by { ext x, simp only [mem_erase, ←and_assoc], rw and_comm (x ≠ a) } theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp]; exact forall_congr (λ x, forall_swap) theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 $ subset.rfl theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 $ subset.rfl lemma subset_insert_iff_of_not_mem (h : a ∉ s) : s ⊆ insert a t ↔ s ⊆ t := by rw [subset_insert_iff, erase_eq_of_not_mem h] lemma erase_subset_iff_of_mem (h : a ∈ t) : s.erase a ⊆ t ↔ s ⊆ t := by rw [←subset_insert_iff, insert_eq_of_mem h] lemma erase_inj {x y : α} (s : finset α) (hx : x ∈ s) : s.erase x = s.erase y ↔ x = y := begin refine ⟨λ h, _, congr_arg _⟩, rw eq_of_mem_of_not_mem_erase hx, rw ←h, simp, end lemma erase_inj_on (s : finset α) : set.inj_on s.erase s := λ _ _ _ _, (erase_inj s ‹_›).mp lemma erase_inj_on' (a : α) : {s : finset α | a ∈ s}.inj_on (λ s, erase s a) := λ s hs t ht (h : s.erase a = _), by rw [←insert_erase hs, ←insert_erase ht, h] end erase /-! ### sdiff -/ section sdiff variables [decidable_eq α] {s t u v : finset α} {a b : α} /-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/ instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le tsub_le_self s₁.2⟩⟩ @[simp] lemma sdiff_val (s₁ s₂ : finset α) : (s₁ \ s₂).val = s₁.val - s₂.val := rfl @[simp] theorem mem_sdiff : a ∈ s \ t ↔ a ∈ s ∧ a ∉ t := mem_sub_of_nodup s.2 @[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h instance : generalized_boolean_algebra (finset α) := { sup_inf_sdiff := λ x y, by { simp only [ext_iff, mem_union, mem_sdiff, inf_eq_inter, sup_eq_union, mem_inter], tauto }, inf_inf_sdiff := λ x y, by { simp only [ext_iff, inter_sdiff_self, inter_empty, inter_assoc, false_iff, inf_eq_inter, not_mem_empty], tauto }, ..finset.has_sdiff, ..finset.distrib_lattice, ..finset.order_bot } lemma not_mem_sdiff_of_mem_right (h : a ∈ t) : a ∉ s \ t := by simp only [mem_sdiff, h, not_true, not_false_iff, and_false] lemma not_mem_sdiff_of_not_mem_left (h : a ∉ s) : a ∉ s \ t := by simpa lemma union_sdiff_of_subset (h : s ⊆ t) : s ∪ (t \ s) = t := sup_sdiff_cancel_right h theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ := (union_comm _ _).trans (union_sdiff_of_subset h) lemma inter_sdiff (s t u : finset α) : s ∩ (t \ u) = s ∩ t \ u := by { ext x, simp [and_assoc] } @[simp] lemma sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ := inf_sdiff_self_left @[simp] protected lemma sdiff_self (s₁ : finset α) : s₁ \ s₁ = ∅ := sdiff_self lemma sdiff_inter_distrib_right (s t u : finset α) : s \ (t ∩ u) = (s \ t) ∪ (s \ u) := sdiff_inf @[simp] lemma sdiff_inter_self_left (s t : finset α) : s \ (s ∩ t) = s \ t := sdiff_inf_self_left _ _ @[simp] lemma sdiff_inter_self_right (s t : finset α) : s \ (t ∩ s) = s \ t := sdiff_inf_self_right _ _ @[simp] lemma sdiff_empty : s \ ∅ = s := sdiff_bot @[mono] lemma sdiff_subset_sdiff (hst : s ⊆ t) (hvu : v ⊆ u) : s \ u ⊆ t \ v := sdiff_le_sdiff ‹s ≤ t› ‹v ≤ u› @[simp, norm_cast] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (s₁ \ s₂ : set α) := set.ext $ λ _, mem_sdiff @[simp] lemma union_sdiff_self_eq_union : s ∪ t \ s = s ∪ t := sup_sdiff_self_right _ _ @[simp] lemma sdiff_union_self_eq_union : s \ t ∪ t = s ∪ t := sup_sdiff_self_left _ _ lemma union_sdiff_left (s t : finset α) : (s ∪ t) \ s = t \ s := sup_sdiff_left_self lemma union_sdiff_right (s t : finset α) : (s ∪ t) \ t = s \ t := sup_sdiff_right_self lemma union_sdiff_cancel_left (h : disjoint s t) : (s ∪ t) \ s = t := h.sup_sdiff_cancel_left lemma union_sdiff_cancel_right (h : disjoint s t) : (s ∪ t) \ t = s := h.sup_sdiff_cancel_right lemma union_sdiff_symm : s ∪ (t \ s) = t ∪ (s \ t) := by simp [union_comm] lemma sdiff_union_inter (s t : finset α) : (s \ t) ∪ (s ∩ t) = s := sup_sdiff_inf _ _ @[simp] lemma sdiff_idem (s t : finset α) : s \ t \ t = s \ t := sdiff_idem lemma subset_sdiff : s ⊆ t \ u ↔ s ⊆ t ∧ disjoint s u := le_iff_subset.symm.trans le_sdiff @[simp] lemma sdiff_eq_empty_iff_subset : s \ t = ∅ ↔ s ⊆ t := sdiff_eq_bot_iff lemma sdiff_nonempty : (s \ t).nonempty ↔ ¬ s ⊆ t := nonempty_iff_ne_empty.trans sdiff_eq_empty_iff_subset.not @[simp] lemma empty_sdiff (s : finset α) : ∅ \ s = ∅ := bot_sdiff lemma insert_sdiff_of_not_mem (s : finset α) {t : finset α} {x : α} (h : x ∉ t) : (insert x s) \ t = insert x (s \ t) := begin rw [← coe_inj, coe_insert, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_not_mem s h end lemma insert_sdiff_of_mem (s : finset α) {x : α} (h : x ∈ t) : (insert x s) \ t = s \ t := begin rw [← coe_inj, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_mem s h end @[simp] lemma insert_sdiff_insert (s t : finset α) (x : α) : (insert x s) \ (insert x t) = s \ insert x t := insert_sdiff_of_mem _ (mem_insert_self _ _) lemma sdiff_insert_of_not_mem {x : α} (h : x ∉ s) (t : finset α) : s \ (insert x t) = s \ t := begin refine subset.antisymm (sdiff_subset_sdiff (subset.refl _) (subset_insert _ _)) (λ y hy, _), simp only [mem_sdiff, mem_insert, not_or_distrib] at hy ⊢, exact ⟨hy.1, λ hxy, h $ hxy ▸ hy.1, hy.2⟩ end @[simp] lemma sdiff_subset (s t : finset α) : s \ t ⊆ s := show s \ t ≤ s, from sdiff_le lemma sdiff_ssubset (h : t ⊆ s) (ht : t.nonempty) : s \ t ⊂ s := sdiff_lt ‹t ≤ s› ht.ne_empty lemma union_sdiff_distrib (s₁ s₂ t : finset α) : (s₁ ∪ s₂) \ t = s₁ \ t ∪ s₂ \ t := sup_sdiff lemma sdiff_union_distrib (s t₁ t₂ : finset α) : s \ (t₁ ∪ t₂) = (s \ t₁) ∩ (s \ t₂) := sdiff_sup lemma union_sdiff_self (s t : finset α) : (s ∪ t) \ t = s \ t := sup_sdiff_right_self -- TODO: Do we want to delete this lemma and `finset.disj_union_singleton`, -- or instead add `finset.union_singleton`/`finset.singleton_union`? lemma sdiff_singleton_eq_erase (a : α) (s : finset α) : s \ singleton a = erase s a := by { ext, rw [mem_erase, mem_sdiff, mem_singleton], tauto } -- This lemma matches `finset.insert_eq` in functionality. lemma erase_eq (s : finset α) (a : α) : s.erase a = s \ {a} := (sdiff_singleton_eq_erase _ _).symm lemma disjoint_erase_comm : disjoint (s.erase a) t ↔ disjoint s (t.erase a) := by simp_rw [erase_eq, disjoint_sdiff_comm] lemma disjoint_of_erase_left (ha : a ∉ t) (hst : disjoint (s.erase a) t) : disjoint s t := by { rw [←erase_insert ha, ←disjoint_erase_comm, disjoint_insert_right], exact ⟨not_mem_erase _ _, hst⟩ } lemma disjoint_of_erase_right (ha : a ∉ s) (hst : disjoint s (t.erase a)) : disjoint s t := by { rw [←erase_insert ha, disjoint_erase_comm, disjoint_insert_left], exact ⟨not_mem_erase _ _, hst⟩ } @[simp] lemma inter_erase (a : α) (s t : finset α) : s ∩ t.erase a = (s ∩ t).erase a := by simp only [erase_eq, inter_sdiff] @[simp] lemma erase_inter (a : α) (s t : finset α) : s.erase a ∩ t = (s ∩ t).erase a := by simpa only [inter_comm t] using inter_erase a t s lemma erase_sdiff_comm (s t : finset α) (a : α) : s.erase a \ t = (s \ t).erase a := by simp_rw [erase_eq, sdiff_right_comm] lemma insert_union_comm (s t : finset α) (a : α) : insert a s ∪ t = s ∪ insert a t := by rw [insert_union, union_insert] lemma erase_inter_comm (s t : finset α) (a : α) : s.erase a ∩ t = s ∩ t.erase a := by rw [erase_inter, inter_erase] lemma erase_union_distrib (s t : finset α) (a : α) : (s ∪ t).erase a = s.erase a ∪ t.erase a := by simp_rw [erase_eq, union_sdiff_distrib] lemma insert_inter_distrib (s t : finset α) (a : α) : insert a (s ∩ t) = insert a s ∩ insert a t := by simp_rw [insert_eq, union_distrib_left] lemma erase_sdiff_distrib (s t : finset α) (a : α) : (s \ t).erase a = s.erase a \ t.erase a := by simp_rw [erase_eq, sdiff_sdiff, sup_sdiff_eq_sup le_rfl, sup_comm] lemma erase_union_of_mem (ha : a ∈ t) (s : finset α) : s.erase a ∪ t = s ∪ t := by rw [←insert_erase (mem_union_right s ha), erase_union_distrib, ←union_insert, insert_erase ha] lemma union_erase_of_mem (ha : a ∈ s) (t : finset α) : s ∪ t.erase a = s ∪ t := by rw [←insert_erase (mem_union_left t ha), erase_union_distrib, ←insert_union, insert_erase ha] @[simp] lemma sdiff_singleton_eq_self (ha : a ∉ s) : s \ {a} = s := sdiff_eq_self_iff_disjoint.2 $ by simp [ha] lemma sdiff_sdiff_left' (s t u : finset α) : (s \ t) \ u = (s \ t) ∩ (s \ u) := sdiff_sdiff_left' lemma sdiff_union_sdiff_cancel (hts : t ⊆ s) (hut : u ⊆ t) : s \ t ∪ t \ u = s \ u := sdiff_sup_sdiff_cancel hts hut lemma sdiff_union_erase_cancel (hts : t ⊆ s) (ha : a ∈ t) : s \ t ∪ t.erase a = s.erase a := by simp_rw [erase_eq, sdiff_union_sdiff_cancel hts (singleton_subset_iff.2 ha)] lemma sdiff_sdiff_eq_sdiff_union (h : u ⊆ s) : s \ (t \ u) = s \ t ∪ u := sdiff_sdiff_eq_sdiff_sup h lemma sdiff_insert (s t : finset α) (x : α) : s \ insert x t = (s \ t).erase x := by simp_rw [← sdiff_singleton_eq_erase, insert_eq, sdiff_sdiff_left', sdiff_union_distrib, inter_comm] lemma sdiff_insert_insert_of_mem_of_not_mem {s t : finset α} {x : α} (hxs : x ∈ s) (hxt : x ∉ t) : insert x (s \ insert x t) = s \ t := by rw [sdiff_insert, insert_erase (mem_sdiff.mpr ⟨hxs, hxt⟩)] lemma sdiff_erase (h : a ∈ s) : s \ t.erase a = insert a (s \ t) := by rw [←sdiff_singleton_eq_erase, sdiff_sdiff_eq_sdiff_union (singleton_subset_iff.2 h), insert_eq, union_comm] lemma sdiff_erase_self (ha : a ∈ s) : s \ s.erase a = {a} := by rw [sdiff_erase ha, finset.sdiff_self, insert_emptyc_eq] lemma sdiff_sdiff_self_left (s t : finset α) : s \ (s \ t) = s ∩ t := sdiff_sdiff_right_self lemma sdiff_sdiff_eq_self (h : t ⊆ s) : s \ (s \ t) = t := sdiff_sdiff_eq_self h lemma sdiff_eq_sdiff_iff_inter_eq_inter {s t₁ t₂ : finset α} : s \ t₁ = s \ t₂ ↔ s ∩ t₁ = s ∩ t₂ := sdiff_eq_sdiff_iff_inf_eq_inf lemma union_eq_sdiff_union_sdiff_union_inter (s t : finset α) : s ∪ t = (s \ t) ∪ (t \ s) ∪ (s ∩ t) := sup_eq_sdiff_sup_sdiff_sup_inf lemma erase_eq_empty_iff (s : finset α) (a : α) : s.erase a = ∅ ↔ s = ∅ ∨ s = {a} := by rw [←sdiff_singleton_eq_erase, sdiff_eq_empty_iff_subset, subset_singleton_iff] --TODO@Yaël: Kill lemmas duplicate with `boolean_algebra` lemma sdiff_disjoint : disjoint (t \ s) s := disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2 lemma disjoint_sdiff : disjoint s (t \ s) := sdiff_disjoint.symm lemma disjoint_sdiff_inter (s t : finset α) : disjoint (s \ t) (s ∩ t) := disjoint_of_subset_right (inter_subset_right _ _) sdiff_disjoint lemma sdiff_eq_self_iff_disjoint : s \ t = s ↔ disjoint s t := sdiff_eq_self_iff_disjoint' lemma sdiff_eq_self_of_disjoint (h : disjoint s t) : s \ t = s := sdiff_eq_self_iff_disjoint.2 h end sdiff /-! ### Symmetric difference -/ section symm_diff variables [decidable_eq α] {s t : finset α} {a b : α} lemma mem_symm_diff : a ∈ s ∆ t ↔ a ∈ s ∧ a ∉ t ∨ a ∈ t ∧ a ∉ s := by simp_rw [symm_diff, sup_eq_union, mem_union, mem_sdiff] @[simp, norm_cast] lemma coe_symm_diff : (↑(s ∆ t) : set α) = s ∆ t := set.ext $ λ _, mem_symm_diff end symm_diff /-! ### attach -/ /-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype `{x // x ∈ s}`. -/ def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩ theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {s : finset α} (hx : x ∈ s) : sizeof x < sizeof s := by { cases s, dsimp [sizeof, has_sizeof.sizeof, finset.sizeof], apply lt_add_left, exact multiset.sizeof_lt_sizeof_of_mem hx } @[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl @[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _ @[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl @[simp] lemma attach_nonempty_iff {s : finset α} : s.attach.nonempty ↔ s.nonempty := by simp [finset.nonempty] @[simp] lemma attach_eq_empty_iff {s : finset α} : s.attach = ∅ ↔ s = ∅ := by simpa [eq_empty_iff_forall_not_mem] /-! ### piecewise -/ section piecewise /-- `s.piecewise f g` is the function equal to `f` on the finset `s`, and to `g` on its complement. -/ def piecewise {α : Type*} {δ : α → Sort*} (s : finset α) (f g : Π i, δ i) [Π j, decidable (j ∈ s)] : Π i, δ i := λi, if i ∈ s then f i else g i variables {δ : α → Sort*} (s : finset α) (f g : Π i, δ i) @[simp] lemma piecewise_insert_self [decidable_eq α] {j : α} [∀ i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g j = f j := by simp [piecewise] @[simp] lemma piecewise_empty [Π i : α, decidable (i ∈ (∅ : finset α))] : piecewise ∅ f g = g := by { ext i, simp [piecewise] } variable [Π j, decidable (j ∈ s)] -- TODO: fix this in norm_cast @[norm_cast move] lemma piecewise_coe [∀ j, decidable (j ∈ (s : set α))] : (s : set α).piecewise f g = s.piecewise f g := by { ext, congr } @[simp, priority 980] lemma piecewise_eq_of_mem {i : α} (hi : i ∈ s) : s.piecewise f g i = f i := by simp [piecewise, hi] @[simp, priority 980] lemma piecewise_eq_of_not_mem {i : α} (hi : i ∉ s) : s.piecewise f g i = g i := by simp [piecewise, hi] lemma piecewise_congr {f f' g g' : Π i, δ i} (hf : ∀ i ∈ s, f i = f' i) (hg : ∀ i ∉ s, g i = g' i) : s.piecewise f g = s.piecewise f' g' := funext $ λ i, if_ctx_congr iff.rfl (hf i) (hg i) @[simp, priority 990] lemma piecewise_insert_of_ne [decidable_eq α] {i j : α} [∀ i, decidable (i ∈ insert j s)] (h : i ≠ j) : (insert j s).piecewise f g i = s.piecewise f g i := by simp [piecewise, h] lemma piecewise_insert [decidable_eq α] (j : α) [∀ i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g = update (s.piecewise f g) j (f j) := by { classical, simp only [← piecewise_coe, coe_insert, ← set.piecewise_insert] } lemma piecewise_cases {i} (p : δ i → Prop) (hf : p (f i)) (hg : p (g i)) : p (s.piecewise f g i) := by by_cases hi : i ∈ s; simpa [hi] lemma piecewise_mem_set_pi {δ : α → Type*} {t : set α} {t' : Π i, set (δ i)} {f g} (hf : f ∈ set.pi t t') (hg : g ∈ set.pi t t') : s.piecewise f g ∈ set.pi t t' := by { classical, rw ← piecewise_coe, exact set.piecewise_mem_pi ↑s hf hg } lemma piecewise_singleton [decidable_eq α] (i : α) : piecewise {i} f g = update g i (f i) := by rw [← insert_emptyc_eq, piecewise_insert, piecewise_empty] lemma piecewise_piecewise_of_subset_left {s t : finset α} [Π i, decidable (i ∈ s)] [Π i, decidable (i ∈ t)] (h : s ⊆ t) (f₁ f₂ g : Π a, δ a) : s.piecewise (t.piecewise f₁ f₂) g = s.piecewise f₁ g := s.piecewise_congr (λ i hi, piecewise_eq_of_mem _ _ _ (h hi)) (λ _ _, rfl) @[simp] lemma piecewise_idem_left (f₁ f₂ g : Π a, δ a) : s.piecewise (s.piecewise f₁ f₂) g = s.piecewise f₁ g := piecewise_piecewise_of_subset_left (subset.refl _) _ _ _ lemma piecewise_piecewise_of_subset_right {s t : finset α} [Π i, decidable (i ∈ s)] [Π i, decidable (i ∈ t)] (h : t ⊆ s) (f g₁ g₂ : Π a, δ a) : s.piecewise f (t.piecewise g₁ g₂) = s.piecewise f g₂ := s.piecewise_congr (λ _ _, rfl) (λ i hi, t.piecewise_eq_of_not_mem _ _ (mt (@h _) hi)) @[simp] lemma piecewise_idem_right (f g₁ g₂ : Π a, δ a) : s.piecewise f (s.piecewise g₁ g₂) = s.piecewise f g₂ := piecewise_piecewise_of_subset_right (subset.refl _) f g₁ g₂ lemma update_eq_piecewise {β : Type*} [decidable_eq α] (f : α → β) (i : α) (v : β) : update f i v = piecewise (singleton i) (λj, v) f := (piecewise_singleton _ _ _).symm lemma update_piecewise [decidable_eq α] (i : α) (v : δ i) : update (s.piecewise f g) i v = s.piecewise (update f i v) (update g i v) := begin ext j, rcases em (j = i) with (rfl|hj); by_cases hs : j ∈ s; simp * end lemma update_piecewise_of_mem [decidable_eq α] {i : α} (hi : i ∈ s) (v : δ i) : update (s.piecewise f g) i v = s.piecewise (update f i v) g := begin rw update_piecewise, refine s.piecewise_congr (λ _ _, rfl) (λ j hj, update_noteq _ _ _), exact λ h, hj (h.symm ▸ hi) end lemma update_piecewise_of_not_mem [decidable_eq α] {i : α} (hi : i ∉ s) (v : δ i) : update (s.piecewise f g) i v = s.piecewise f (update g i v) := begin rw update_piecewise, refine s.piecewise_congr (λ j hj, update_noteq _ _ _) (λ _ _, rfl), exact λ h, hi (h ▸ hj) end lemma piecewise_le_of_le_of_le {δ : α → Type*} [Π i, preorder (δ i)] {f g h : Π i, δ i} (Hf : f ≤ h) (Hg : g ≤ h) : s.piecewise f g ≤ h := λ x, piecewise_cases s f g (≤ h x) (Hf x) (Hg x) lemma le_piecewise_of_le_of_le {δ : α → Type*} [Π i, preorder (δ i)] {f g h : Π i, δ i} (Hf : h ≤ f) (Hg : h ≤ g) : h ≤ s.piecewise f g := λ x, piecewise_cases s f g (λ y, h x ≤ y) (Hf x) (Hg x) lemma piecewise_le_piecewise' {δ : α → Type*} [Π i, preorder (δ i)] {f g f' g' : Π i, δ i} (Hf : ∀ x ∈ s, f x ≤ f' x) (Hg : ∀ x ∉ s, g x ≤ g' x) : s.piecewise f g ≤ s.piecewise f' g' := λ x, by { by_cases hx : x ∈ s; simp [hx, *] } lemma piecewise_le_piecewise {δ : α → Type*} [Π i, preorder (δ i)] {f g f' g' : Π i, δ i} (Hf : f ≤ f') (Hg : g ≤ g') : s.piecewise f g ≤ s.piecewise f' g' := s.piecewise_le_piecewise' (λ x _, Hf x) (λ x _, Hg x) lemma piecewise_mem_Icc_of_mem_of_mem {δ : α → Type*} [Π i, preorder (δ i)] {f f₁ g g₁ : Π i, δ i} (hf : f ∈ set.Icc f₁ g₁) (hg : g ∈ set.Icc f₁ g₁) : s.piecewise f g ∈ set.Icc f₁ g₁ := ⟨le_piecewise_of_le_of_le _ hf.1 hg.1, piecewise_le_of_le_of_le _ hf.2 hg.2⟩ lemma piecewise_mem_Icc {δ : α → Type*} [Π i, preorder (δ i)] {f g : Π i, δ i} (h : f ≤ g) : s.piecewise f g ∈ set.Icc f g := piecewise_mem_Icc_of_mem_of_mem _ (set.left_mem_Icc.2 h) (set.right_mem_Icc.2 h) lemma piecewise_mem_Icc' {δ : α → Type*} [Π i, preorder (δ i)] {f g : Π i, δ i} (h : g ≤ f) : s.piecewise f g ∈ set.Icc g f := piecewise_mem_Icc_of_mem_of_mem _ (set.right_mem_Icc.2 h) (set.left_mem_Icc.2 h) end piecewise section decidable_pi_exists variables {s : finset α} instance decidable_dforall_finset {p : Π a ∈ s, Prop} [hp : ∀ a (h : a ∈ s), decidable (p a h)] : decidable (∀ a (h : a ∈ s), p a h) := multiset.decidable_dforall_multiset /-- decidable equality for functions whose domain is bounded by finsets -/ instance decidable_eq_pi_finset {β : α → Type*} [h : ∀ a, decidable_eq (β a)] : decidable_eq (Π a ∈ s, β a) := multiset.decidable_eq_pi_multiset instance decidable_dexists_finset {p : Π a ∈ s, Prop} [hp : ∀ a (h : a ∈ s), decidable (p a h)] : decidable (∃ a (h : a ∈ s), p a h) := multiset.decidable_dexists_multiset end decidable_pi_exists /-! ### filter -/ section filter variables (p q : α → Prop) [decidable_pred p] [decidable_pred q] {s : finset α} /-- `filter p s` is the set of elements of `s` that satisfy `p`. -/ def filter (s : finset α) : finset α := ⟨_, s.2.filter p⟩ @[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl @[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _ _ variable {p} @[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter lemma mem_of_mem_filter {s : finset α} (x : α) (h : x ∈ s.filter p) : x ∈ s := mem_of_mem_filter h theorem filter_ssubset {s : finset α} : s.filter p ⊂ s ↔ ∃ x ∈ s, ¬ p x := ⟨λ h, let ⟨x, hs, hp⟩ := set.exists_of_ssubset h in ⟨x, hs, mt (λ hp, mem_filter.2 ⟨hs, hp⟩) hp⟩, λ ⟨x, hs, hp⟩, ⟨s.filter_subset _, λ h, hp (mem_filter.1 (h hs)).2⟩⟩ variable (p) theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) := ext $ assume a, by simp only [mem_filter, and_comm, and.left_comm] lemma filter_comm (s : finset α) : (s.filter p).filter q = (s.filter q).filter p := by simp_rw [filter_filter, and_comm] /- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/ @[simp] lemma filter_congr_decidable (s : finset α) (p : α → Prop) (h : decidable_pred p) [decidable_pred p] : @filter α p h s = s.filter p := by congr lemma filter_true {h} (s : finset α) : @filter α (λ a, true) h s = s := by ext; simp lemma filter_false {h} (s : finset α) : @filter α (λ a, false) h s = ∅ := by ext; simp variables {p q} lemma filter_eq_self : s.filter p = s ↔ ∀ ⦃x⦄, x ∈ s → p x := by simp [finset.ext_iff] lemma filter_eq_empty_iff : s.filter p = ∅ ↔ ∀ ⦃x⦄, x ∈ s → ¬ p x := by simp [finset.ext_iff] lemma filter_nonempty_iff {s : finset α} : (s.filter p).nonempty ↔ ∃ a ∈ s, p a := by simp only [nonempty_iff_ne_empty, ne.def, filter_eq_empty_iff, not_not, not_forall] /-- If all elements of a `finset` satisfy the predicate `p`, `s.filter p` is `s`. -/ @[simp] lemma filter_true_of_mem (h : ∀ x ∈ s, p x) : s.filter p = s := filter_eq_self.2 h /-- If all elements of a `finset` fail to satisfy the predicate `p`, `s.filter p` is `∅`. -/ @[simp] lemma filter_false_of_mem (h : ∀ x ∈ s, ¬ p x) : s.filter p = ∅ := filter_eq_empty_iff.2 h @[simp] lemma filter_const (p : Prop) [decidable p] (s : finset α) : s.filter (λ a, p) = if p then s else ∅ := by split_ifs; simp [*] lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s := eq_of_veq $ filter_congr H variables (p q) lemma filter_empty : filter p ∅ = ∅ := subset_empty.1 $ filter_subset _ _ lemma filter_subset_filter {s t : finset α} (h : s ⊆ t) : s.filter p ⊆ t.filter p := assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩ lemma monotone_filter_left : monotone (filter p) := λ _ _, filter_subset_filter p lemma monotone_filter_right (s : finset α) ⦃p q : α → Prop⦄ [decidable_pred p] [decidable_pred q] (h : p ≤ q) : s.filter p ≤ s.filter q := multiset.subset_of_le (multiset.monotone_filter_right s.val h) @[simp, norm_cast] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) := set.ext $ λ _, mem_filter lemma subset_coe_filter_of_subset_forall (s : finset α) {t : set α} (h₁ : t ⊆ s) (h₂ : ∀ x ∈ t, p x) : t ⊆ s.filter p := λ x hx, (s.coe_filter p).symm ▸ ⟨h₁ hx, h₂ x hx⟩ theorem filter_singleton (a : α) : filter p (singleton a) = if p a then singleton a else ∅ := by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_cons_of_pos (a : α) (s : finset α) (ha : a ∉ s) (hp : p a): filter p (cons a s ha) = cons a (filter p s) (mem_filter.not.mpr $ mt and.left ha) := eq_of_veq $ multiset.filter_cons_of_pos s.val hp theorem filter_cons_of_neg (a : α) (s : finset α) (ha : a ∉ s) (hp : ¬p a): filter p (cons a s ha) = filter p s := eq_of_veq $ multiset.filter_cons_of_neg s.val hp lemma disjoint_filter {s : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : disjoint (s.filter p) (s.filter q) ↔ ∀ x ∈ s, p x → ¬ q x := by split; simp [disjoint_left] {contextual := tt} lemma disjoint_filter_filter {s t : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : disjoint s t → disjoint (s.filter p) (t.filter q) := disjoint.mono (filter_subset _ _) (filter_subset _ _) lemma disjoint_filter_filter' (s t : finset α) {p q : α → Prop} [decidable_pred p] [decidable_pred q] (h : disjoint p q) : disjoint (s.filter p) (t.filter q) := begin simp_rw [disjoint_left, mem_filter], rintros a ⟨hs, hp⟩ ⟨ht, hq⟩, exact h.le_bot _ ⟨hp, hq⟩, end lemma disjoint_filter_filter_neg (s t : finset α) (p : α → Prop) [decidable_pred p] [decidable_pred (λ a, ¬ p a)] : disjoint (s.filter p) (t.filter $ λ a, ¬ p a) := disjoint_filter_filter' s t disjoint_compl_right theorem filter_disj_union (s : finset α) (t : finset α) (h : disjoint s t) : filter p (disj_union s t h) = (filter p s).disj_union (filter p t) (disjoint_filter_filter h) := eq_of_veq $ multiset.filter_add _ _ _ theorem filter_cons {a : α} (s : finset α) (ha : a ∉ s) : filter p (cons a s ha) = (if p a then {a} else ∅ : finset α).disj_union (filter p s) (by { split_ifs, { rw disjoint_singleton_left, exact (mem_filter.not.mpr $ mt and.left ha) }, { exact disjoint_empty_left _ } }) := begin split_ifs with h, { rw [filter_cons_of_pos _ _ _ ha h, singleton_disj_union] }, { rw [filter_cons_of_neg _ _ _ ha h, empty_disj_union] }, end variable [decidable_eq α] theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext $ λ _, by simp only [mem_filter, mem_union, or_and_distrib_right] theorem filter_union_right (s : finset α) : s.filter p ∪ s.filter q = s.filter (λx, p x ∨ q x) := ext $ λ x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm] lemma filter_mem_eq_inter {s t : finset α} [Π i, decidable (i ∈ t)] : s.filter (λ i, i ∈ t) = s ∩ t := ext $ λ i, by rw [mem_filter, mem_inter] lemma filter_inter_distrib (s t : finset α) : (s ∩ t).filter p = s.filter p ∩ t.filter p := by { ext, simp only [mem_filter, mem_inter], exact and_and_distrib_right _ _ _ } theorem filter_inter (s t : finset α) : filter p s ∩ t = filter p (s ∩ t) := by { ext, simp only [mem_inter, mem_filter, and.right_comm] } theorem inter_filter (s t : finset α) : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : α) (s : finset α) : filter p (insert a s) = if p a then insert a (filter p s) else filter p s := by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_erase (a : α) (s : finset α) : filter p (erase s a) = erase (filter p s) a := by { ext x, simp only [and_assoc, mem_filter, iff_self, mem_erase] } theorem filter_or [decidable_pred (λ a, p a ∨ q a)] (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q := ext $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left] theorem filter_and [decidable_pred (λ a, p a ∧ q a)] (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q := ext $ λ _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self] theorem filter_not [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p := ext $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using λ a, and_congr_right $ λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm theorem sdiff_eq_filter (s₁ s₂ : finset α) : s₁ \ s₂ = filter (∉ s₂) s₁ := ext $ λ _, by simp only [mem_sdiff, mem_filter] lemma sdiff_eq_self (s₁ s₂ : finset α) : s₁ \ s₂ = s₁ ↔ s₁ ∩ s₂ ⊆ ∅ := by { simp [subset.antisymm_iff], split; intro h, { transitivity' ((s₁ \ s₂) ∩ s₂), mono, simp }, { calc s₁ \ s₂ ⊇ s₁ \ (s₁ ∩ s₂) : by simp [(⊇)] ... ⊇ s₁ \ ∅ : by mono using [(⊇)] ... ⊇ s₁ : by simp [(⊇)] } } lemma subset_union_elim {s : finset α} {t₁ t₂ : set α} (h : ↑s ⊆ t₁ ∪ t₂) : ∃ s₁ s₂ : finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ := begin classical, refine ⟨s.filter (∈ t₁), s.filter (∉ t₁), _, _ , _⟩, { simp [filter_union_right, em] }, { intro x, simp }, { intro x, simp, intros hx hx₂, refine ⟨or.resolve_left (h hx) hx₂, hx₂⟩ } end section classical open_locale classical /-- The following instance allows us to write `{x ∈ s | p x}` for `finset.filter p s`. Since the former notation requires us to define this for all propositions `p`, and `finset.filter` only works for decidable propositions, the notation `{x ∈ s | p x}` is only compatible with classical logic because it uses `classical.prop_decidable`. We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp` unfolds the notation `{x ∈ s | p x}` to `finset.filter p s`. If `p` happens to be decidable, the simp-lemma `finset.filter_congr_decidable` will make sure that `finset.filter` uses the right instance for decidability. -/ noncomputable instance {α : Type*} : has_sep α (finset α) := ⟨λ p x, x.filter p⟩ @[simp] lemma sep_def {α : Type*} (s : finset α) (p : α → Prop) : {x ∈ s | p x} = s.filter p := rfl end classical /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq'` with the equality the other way. -/ -- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter(eq b)`. lemma filter_eq [decidable_eq β] (s : finset β) (b : β) : s.filter (eq b) = ite (b ∈ s) {b} ∅ := begin split_ifs, { ext, simp only [mem_filter, mem_singleton], exact ⟨λ h, h.2.symm, by { rintro ⟨h⟩, exact ⟨h, rfl⟩ }⟩ }, { ext, simp only [mem_filter, not_and, iff_false, not_mem_empty], rintro m ⟨e⟩, exact h m } end /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq` with the equality the other way. -/ lemma filter_eq' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a = b) = ite (b ∈ s) {b} ∅ := trans (filter_congr (λ _ _, ⟨eq.symm, eq.symm⟩)) (filter_eq s b) lemma filter_ne [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, b ≠ a) = s.erase b := by { ext, simp only [mem_filter, mem_erase, ne.def], tauto } lemma filter_ne' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a ≠ b) = s.erase b := trans (filter_congr (λ _ _, ⟨ne.symm, ne.symm⟩)) (filter_ne s b) theorem filter_inter_filter_neg_eq [decidable_pred (λ a, ¬ p a)] (s t : finset α) : s.filter p ∩ t.filter (λa, ¬ p a) = ∅ := (disjoint_filter_filter_neg s t p).eq_bot theorem filter_union_filter_of_codisjoint (s : finset α) (h : codisjoint p q) : s.filter p ∪ s.filter q = s := (filter_or _ _ _).symm.trans $ filter_true_of_mem $ λ x hx, h.top_le x trivial theorem filter_union_filter_neg_eq [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s := filter_union_filter_of_codisjoint _ _ _ codisjoint_hnot_right end filter /-! ### range -/ section range variables {n m l : ℕ} /-- `range n` is the set of natural numbers less than `n`. -/ def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩ @[simp] theorem range_val (n : ℕ) : (range n).1 = multiset.range n := rfl @[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range @[simp, norm_cast] lemma coe_range (n : ℕ) : (range n : set ℕ) = set.Iio n := set.ext $ λ _, mem_range @[simp] theorem range_zero : range 0 = ∅ := rfl @[simp] theorem range_one : range 1 = {0} := rfl theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm lemma range_add_one : range (n + 1) = insert n (range n) := range_succ @[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self @[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := multiset.self_mem_range_succ n @[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset theorem range_mono : monotone range := λ _ _, range_subset.2 lemma mem_range_succ_iff {a b : ℕ} : a ∈ finset.range b.succ ↔ a ≤ b := finset.mem_range.trans nat.lt_succ_iff lemma mem_range_le {n x : ℕ} (hx : x ∈ range n) : x ≤ n := (mem_range.1 hx).le lemma mem_range_sub_ne_zero {n x : ℕ} (hx : x ∈ range n) : n - x ≠ 0 := ne_of_gt $ tsub_pos_of_lt $ mem_range.1 hx @[simp] lemma nonempty_range_iff : (range n).nonempty ↔ n ≠ 0 := ⟨λ ⟨k, hk⟩, ((zero_le k).trans_lt $ mem_range.1 hk).ne', λ h, ⟨0, mem_range.2 $ pos_iff_ne_zero.2 h⟩⟩ @[simp] lemma range_eq_empty_iff : range n = ∅ ↔ n = 0 := by rw [← not_nonempty_iff_eq_empty, nonempty_range_iff, not_not] lemma nonempty_range_succ : (range $ n + 1).nonempty := nonempty_range_iff.2 n.succ_ne_zero @[simp] lemma range_filter_eq {n m : ℕ} : (range n).filter (= m) = if m < n then {m} else ∅ := begin convert filter_eq (range n) m, { ext, exact comm }, { simp } end end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false := by simp only [not_mem_empty, false_and, exists_false] lemma exists_mem_insert [decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ ∃ x, x ∈ s ∧ p x := by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true := iff_true_intro $ λ _, false.elim lemma forall_mem_insert [decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∀ x, x ∈ insert a s → p x) ↔ p a ∧ ∀ x, x ∈ s → p x := by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] /-- Useful in proofs by induction. -/ lemma forall_of_forall_insert [decidable_eq α] {p : α → Prop} {a : α} {s : finset α} (H : ∀ x, x ∈ insert a s → p x) (x) (h : x ∈ s) : p x := H _ $ mem_insert_of_mem h end finset /-- Equivalence between the set of natural numbers which are `≥ k` and `ℕ`, given by `n → n - k`. -/ def not_mem_range_equiv (k : ℕ) : {n // n ∉ range k} ≃ ℕ := { to_fun := λ i, i.1 - k, inv_fun := λ j, ⟨j + k, by simp⟩, left_inv := λ j, begin rw subtype.ext_iff_val, apply tsub_add_cancel_of_le, simpa using j.2 end, right_inv := λ j, add_tsub_cancel_right _ _ } @[simp] lemma coe_not_mem_range_equiv (k : ℕ) : (not_mem_range_equiv k : {n // n ∉ range k} → ℕ) = (λ i, i - k) := rfl @[simp] lemma coe_not_mem_range_equiv_symm (k : ℕ) : ((not_mem_range_equiv k).symm : ℕ → {n // n ∉ range k}) = λ j, ⟨j + k, by simp⟩ := rfl /-! ### dedup on list and multiset -/ namespace multiset variables [decidable_eq α] {s t : multiset α} /-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/ def to_finset (s : multiset α) : finset α := ⟨_, nodup_dedup s⟩ @[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.dedup := rfl theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset := finset.val_inj.1 n.dedup.symm lemma nodup.to_finset_inj {l l' : multiset α} (hl : nodup l) (hl' : nodup l') (h : l.to_finset = l'.to_finset) : l = l' := by simpa [←to_finset_eq hl, ←to_finset_eq hl'] using h @[simp] lemma mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s := mem_dedup @[simp] lemma to_finset_zero : to_finset (0 : multiset α) = ∅ := rfl @[simp] lemma to_finset_cons (a : α) (s : multiset α) : to_finset (a ::ₘ s) = insert a (to_finset s) := finset.eq_of_veq dedup_cons @[simp] lemma to_finset_singleton (a : α) : to_finset ({a} : multiset α) = {a} := by rw [←cons_zero, to_finset_cons, to_finset_zero, is_lawful_singleton.insert_emptyc_eq] @[simp] lemma to_finset_add (s t : multiset α) : to_finset (s + t) = to_finset s ∪ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_nsmul (s : multiset α) : ∀ (n : ℕ) (hn : n ≠ 0), (n • s).to_finset = s.to_finset | 0 h := by contradiction | (n+1) h := begin by_cases n = 0, { rw [h, zero_add, one_nsmul] }, { rw [add_nsmul, to_finset_add, one_nsmul, to_finset_nsmul n h, finset.union_idempotent] } end @[simp] lemma to_finset_inter (s t : multiset α) : to_finset (s ∩ t) = to_finset s ∩ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_union (s t : multiset α) : (s ∪ t).to_finset = s.to_finset ∪ t.to_finset := by ext; simp @[simp] theorem to_finset_eq_empty {m : multiset α} : m.to_finset = ∅ ↔ m = 0 := finset.val_inj.symm.trans multiset.dedup_eq_zero @[simp] lemma to_finset_nonempty : s.to_finset.nonempty ↔ s ≠ 0 := by simp only [to_finset_eq_empty, ne.def, finset.nonempty_iff_ne_empty] @[simp] lemma to_finset_subset : s.to_finset ⊆ t.to_finset ↔ s ⊆ t := by simp only [finset.subset_iff, multiset.subset_iff, multiset.mem_to_finset] @[simp] lemma to_finset_ssubset : s.to_finset ⊂ t.to_finset ↔ s ⊂ t := by { simp_rw [finset.ssubset_def, to_finset_subset], refl } @[simp] lemma to_finset_dedup (m : multiset α) : m.dedup.to_finset = m.to_finset := by simp_rw [to_finset, dedup_idempotent] @[simp] lemma to_finset_bind_dedup [decidable_eq β] (m : multiset α) (f : α → multiset β) : (m.dedup.bind f).to_finset = (m.bind f).to_finset := by simp_rw [to_finset, dedup_bind_dedup] instance is_well_founded_ssubset : is_well_founded (multiset β) (⊂) := subrelation.is_well_founded (inv_image _ _) $ λ _ _, by classical; exact to_finset_ssubset.2 end multiset namespace finset @[simp] lemma val_to_finset [decidable_eq α] (s : finset α) : s.val.to_finset = s := by { ext, rw [multiset.mem_to_finset, ←mem_def] } lemma val_le_iff_val_subset {a : finset α} {b : multiset α} : a.val ≤ b ↔ a.val ⊆ b := multiset.le_iff_subset a.nodup end finset namespace list variables [decidable_eq α] {l l' : list α} {a : α} /-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/ def to_finset (l : list α) : finset α := multiset.to_finset l @[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.dedup : multiset α) := rfl @[simp] theorem to_finset_coe (l : list α) : (l : multiset α).to_finset = l.to_finset := rfl lemma to_finset_eq (n : nodup l) : @finset.mk α l n = l.to_finset := multiset.to_finset_eq n @[simp] lemma mem_to_finset : a ∈ l.to_finset ↔ a ∈ l := mem_dedup @[simp, norm_cast] lemma coe_to_finset (l : list α) : (l.to_finset : set α) = {a | a ∈ l} := set.ext $ λ _, list.mem_to_finset @[simp] lemma to_finset_nil : to_finset (@nil α) = ∅ := rfl @[simp] lemma to_finset_cons : to_finset (a :: l) = insert a (to_finset l) := finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.dedup_cons, h] lemma to_finset_surj_on : set.surj_on to_finset {l : list α | l.nodup} set.univ := by { rintro ⟨⟨l⟩, hl⟩ _, exact ⟨l, hl, (to_finset_eq hl).symm⟩ } theorem to_finset_surjective : surjective (to_finset : list α → finset α) := λ s, let ⟨l, _, hls⟩ := to_finset_surj_on (set.mem_univ s) in ⟨l, hls⟩ lemma to_finset_eq_iff_perm_dedup : l.to_finset = l'.to_finset ↔ l.dedup ~ l'.dedup := by simp [finset.ext_iff, perm_ext (nodup_dedup _) (nodup_dedup _)] lemma to_finset.ext_iff {a b : list α} : a.to_finset = b.to_finset ↔ ∀ x, x ∈ a ↔ x ∈ b := by simp only [finset.ext_iff, mem_to_finset] lemma to_finset.ext : (∀ x, x ∈ l ↔ x ∈ l') → l.to_finset = l'.to_finset := to_finset.ext_iff.mpr lemma to_finset_eq_of_perm (l l' : list α) (h : l ~ l') : l.to_finset = l'.to_finset := to_finset_eq_iff_perm_dedup.mpr h.dedup lemma perm_of_nodup_nodup_to_finset_eq (hl : nodup l) (hl' : nodup l') (h : l.to_finset = l'.to_finset) : l ~ l' := by { rw ←multiset.coe_eq_coe, exact multiset.nodup.to_finset_inj hl hl' h } @[simp] lemma to_finset_append : to_finset (l ++ l') = l.to_finset ∪ l'.to_finset := begin induction l with hd tl hl, { simp }, { simp [hl] } end @[simp] lemma to_finset_reverse {l : list α} : to_finset l.reverse = l.to_finset := to_finset_eq_of_perm _ _ (reverse_perm l) lemma to_finset_replicate_of_ne_zero {n : ℕ} (hn : n ≠ 0) : (list.replicate n a).to_finset = {a} := by { ext x, simp [hn, list.mem_replicate] } @[simp] lemma to_finset_union (l l' : list α) : (l ∪ l').to_finset = l.to_finset ∪ l'.to_finset := by { ext, simp } @[simp] lemma to_finset_inter (l l' : list α) : (l ∩ l').to_finset = l.to_finset ∩ l'.to_finset := by { ext, simp } @[simp] lemma to_finset_eq_empty_iff (l : list α) : l.to_finset = ∅ ↔ l = nil := by cases l; simp @[simp] lemma to_finset_nonempty_iff (l : list α) : l.to_finset.nonempty ↔ l ≠ [] := by simp [finset.nonempty_iff_ne_empty] end list namespace finset section to_list /-- Produce a list of the elements in the finite set using choice. -/ noncomputable def to_list (s : finset α) : list α := s.1.to_list lemma nodup_to_list (s : finset α) : s.to_list.nodup := by { rw [to_list, ←multiset.coe_nodup, multiset.coe_to_list], exact s.nodup } @[simp] lemma mem_to_list {a : α} {s : finset α} : a ∈ s.to_list ↔ a ∈ s := mem_to_list @[simp] lemma to_list_eq_nil {s : finset α} : s.to_list = [] ↔ s = ∅ := to_list_eq_nil.trans val_eq_zero @[simp] lemma empty_to_list {s : finset α} : s.to_list.empty ↔ s = ∅ := list.empty_iff_eq_nil.trans to_list_eq_nil @[simp] lemma to_list_empty : (∅ : finset α).to_list = [] := to_list_eq_nil.mpr rfl lemma nonempty.to_list_ne_nil {s : finset α} (hs : s.nonempty) : s.to_list ≠ [] := mt to_list_eq_nil.mp hs.ne_empty lemma nonempty.not_empty_to_list {s : finset α} (hs : s.nonempty) : ¬s.to_list.empty := mt empty_to_list.mp hs.ne_empty @[simp, norm_cast] lemma coe_to_list (s : finset α) : (s.to_list : multiset α) = s.val := s.val.coe_to_list @[simp] lemma to_list_to_finset [decidable_eq α] (s : finset α) : s.to_list.to_finset = s := by { ext, simp } @[simp] lemma to_list_eq_singleton_iff {a : α} {s : finset α} : s.to_list = [a] ↔ s = {a} := by rw [to_list, to_list_eq_singleton_iff, val_eq_singleton_iff] @[simp] lemma to_list_singleton : ∀ a, ({a} : finset α).to_list = [a] := to_list_singleton lemma exists_list_nodup_eq [decidable_eq α] (s : finset α) : ∃ (l : list α), l.nodup ∧ l.to_finset = s := ⟨s.to_list, s.nodup_to_list, s.to_list_to_finset⟩ lemma to_list_cons {a : α} {s : finset α} (h : a ∉ s) : (cons a s h).to_list ~ a :: s.to_list := (list.perm_ext (nodup_to_list _) (by simp [h, nodup_to_list s])).2 $ λ x, by simp only [list.mem_cons_iff, finset.mem_to_list, finset.mem_cons] lemma to_list_insert [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) : (insert a s).to_list ~ a :: s.to_list := cons_eq_insert _ _ h ▸ to_list_cons _ end to_list /-! ### disj_Union This section is about the bounded union of a disjoint indexed family `t : α → finset β` of finite sets over a finite set `s : finset α`. In most cases `finset.bUnion` should be preferred. -/ section disj_Union variables {s s₁ s₂ : finset α} {t t₁ t₂ : α → finset β} /-- `disj_Union s f h` is the set such that `a ∈ disj_Union s f` iff `a ∈ f i` for some `i ∈ s`. It is the same as `s.bUnion f`, but it does not require decidable equality on the type. The hypothesis ensures that the sets are disjoint. -/ def disj_Union (s : finset α) (t : α → finset β) (hf : (s : set α).pairwise_disjoint t) : finset β := ⟨(s.val.bind (finset.val ∘ t)), multiset.nodup_bind.mpr ⟨λ a ha, (t a).nodup, s.nodup.pairwise $ λ a ha b hb hab, disjoint_val.2 $ hf ha hb hab⟩⟩ @[simp] theorem disj_Union_val (s : finset α) (t : α → finset β) (h) : (s.disj_Union t h).1 = (s.1.bind (λ a, (t a).1)) := rfl @[simp] theorem disj_Union_empty (t : α → finset β) : disj_Union ∅ t (by simp) = ∅ := rfl @[simp] lemma mem_disj_Union {b : β} {h} : b ∈ s.disj_Union t h ↔ ∃ a ∈ s, b ∈ t a := by simp only [mem_def, disj_Union_val, mem_bind, exists_prop] @[simp, norm_cast] lemma coe_disj_Union {h} : (s.disj_Union t h : set β) = ⋃ x ∈ (s : set α), t x := by simp only [set.ext_iff, mem_disj_Union, set.mem_Union, iff_self, mem_coe, implies_true_iff] @[simp] theorem disj_Union_cons (a : α) (s : finset α) (ha : a ∉ s) (f : α → finset β) (H) : disj_Union (cons a s ha) f H = (f a).disj_union (s.disj_Union f $ λ b hb c hc, H (mem_cons_of_mem hb) (mem_cons_of_mem hc)) (disjoint_left.mpr $ λ b hb h, let ⟨c, hc, h⟩ := mem_disj_Union.mp h in disjoint_left.mp (H (mem_cons_self a s) (mem_cons_of_mem hc) (ne_of_mem_of_not_mem hc ha).symm) hb h) := eq_of_veq $ multiset.cons_bind _ _ _ @[simp] lemma singleton_disj_Union (a : α) {h} : finset.disj_Union {a} t h = t a := eq_of_veq $ multiset.singleton_bind _ _ lemma disj_Union_disj_Union (s : finset α) (f : α → finset β) (g : β → finset γ) (h1 h2) : (s.disj_Union f h1).disj_Union g h2 = s.attach.disj_Union (λ a, (f a).disj_Union g $ λ b hb c hc, h2 (mem_disj_Union.mpr ⟨_, a.prop, hb⟩) (mem_disj_Union.mpr ⟨_, a.prop, hc⟩)) (λ a ha b hb hab, disjoint_left.mpr $ λ x hxa hxb, begin obtain ⟨xa, hfa, hga⟩ := mem_disj_Union.mp hxa, obtain ⟨xb, hfb, hgb⟩ := mem_disj_Union.mp hxb, refine disjoint_left.mp (h2 (mem_disj_Union.mpr ⟨_, a.prop, hfa⟩) (mem_disj_Union.mpr ⟨_, b.prop, hfb⟩) _) hga hgb, rintro rfl, exact disjoint_left.mp (h1 a.prop b.prop $ subtype.coe_injective.ne hab) hfa hfb, end) := eq_of_veq $ multiset.bind_assoc.trans (multiset.attach_bind_coe _ _).symm lemma disj_Union_filter_eq_of_maps_to [decidable_eq β] {s : finset α} {t : finset β} {f : α → β} (h : ∀ x ∈ s, f x ∈ t) : t.disj_Union (λ a, s.filter $ (λ c, f c = a)) (λ x' hx y' hy hne, disjoint_filter_filter' _ _ begin simp_rw [pi.disjoint_iff, Prop.disjoint_iff], rintros i ⟨rfl, rfl⟩, exact hne rfl, end) = s := ext $ λ b, by simpa using h b end disj_Union section bUnion /-! ### bUnion This section is about the bounded union of an indexed family `t : α → finset β` of finite sets over a finite set `s : finset α`. -/ variables [decidable_eq β] {s s₁ s₂ : finset α} {t t₁ t₂ : α → finset β} /-- `bUnion s t` is the union of `t x` over `x ∈ s`. (This was formerly `bind` due to the monad structure on types with `decidable_eq`.) -/ protected def bUnion (s : finset α) (t : α → finset β) : finset β := (s.1.bind (λ a, (t a).1)).to_finset @[simp] theorem bUnion_val (s : finset α) (t : α → finset β) : (s.bUnion t).1 = (s.1.bind (λ a, (t a).1)).dedup := rfl @[simp] theorem bUnion_empty : finset.bUnion ∅ t = ∅ := rfl @[simp] lemma mem_bUnion {b : β} : b ∈ s.bUnion t ↔ ∃ a ∈ s, b ∈ t a := by simp only [mem_def, bUnion_val, mem_dedup, mem_bind, exists_prop] @[simp, norm_cast] lemma coe_bUnion : (s.bUnion t : set β) = ⋃ x ∈ (s : set α), t x := by simp only [set.ext_iff, mem_bUnion, set.mem_Union, iff_self, mem_coe, implies_true_iff] @[simp] theorem bUnion_insert [decidable_eq α] {a : α} : (insert a s).bUnion t = t a ∪ s.bUnion t := ext $ λ x, by simp only [mem_bUnion, exists_prop, mem_union, mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] -- ext $ λ x, by simp [or_and_distrib_right, exists_or_distrib] lemma bUnion_congr (hs : s₁ = s₂) (ht : ∀ a ∈ s₁, t₁ a = t₂ a) : s₁.bUnion t₁ = s₂.bUnion t₂ := ext $ λ x, by simp [hs, ht] { contextual := tt } @[simp] lemma disj_Union_eq_bUnion (s : finset α) (f : α → finset β) (hf) : s.disj_Union f hf = s.bUnion f := begin dsimp [disj_Union, finset.bUnion, function.comp], generalize_proofs h, exact eq_of_veq h.dedup.symm, end theorem bUnion_subset {s' : finset β} : s.bUnion t ⊆ s' ↔ ∀ x ∈ s, t x ⊆ s' := by simp only [subset_iff, mem_bUnion]; exact ⟨λ H a ha b hb, H ⟨a, ha, hb⟩, λ H b ⟨a, ha, hb⟩, H a ha hb⟩ @[simp] lemma singleton_bUnion {a : α} : finset.bUnion {a} t = t a := by { classical, rw [← insert_emptyc_eq, bUnion_insert, bUnion_empty, union_empty] } theorem bUnion_inter (s : finset α) (f : α → finset β) (t : finset β) : s.bUnion f ∩ t = s.bUnion (λ x, f x ∩ t) := begin ext x, simp only [mem_bUnion, mem_inter], tauto end theorem inter_bUnion (t : finset β) (s : finset α) (f : α → finset β) : t ∩ s.bUnion f = s.bUnion (λ x, t ∩ f x) := by rw [inter_comm, bUnion_inter]; simp [inter_comm] lemma bUnion_bUnion [decidable_eq γ] (s : finset α) (f : α → finset β) (g : β → finset γ) : (s.bUnion f).bUnion g = s.bUnion (λ a, (f a).bUnion g) := begin ext, simp only [finset.mem_bUnion, exists_prop], simp_rw [←exists_and_distrib_right, ←exists_and_distrib_left, and_assoc], rw exists_comm, end theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) : (s.bind t).to_finset = s.to_finset.bUnion (λa, (t a).to_finset) := ext $ λ x, by simp only [multiset.mem_to_finset, mem_bUnion, multiset.mem_bind, exists_prop] lemma bUnion_mono (h : ∀ a ∈ s, t₁ a ⊆ t₂ a) : s.bUnion t₁ ⊆ s.bUnion t₂ := have ∀ b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a), from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩, by simpa only [subset_iff, mem_bUnion, exists_imp_distrib, and_imp, exists_prop] lemma bUnion_subset_bUnion_of_subset_left (t : α → finset β) (h : s₁ ⊆ s₂) : s₁.bUnion t ⊆ s₂.bUnion t := begin intro x, simp only [and_imp, mem_bUnion, exists_prop], exact Exists.imp (λ a ha, ⟨h ha.1, ha.2⟩) end lemma subset_bUnion_of_mem (u : α → finset β) {x : α} (xs : x ∈ s) : u x ⊆ s.bUnion u := singleton_bUnion.superset.trans $ bUnion_subset_bUnion_of_subset_left u $ singleton_subset_iff.2 xs @[simp] lemma bUnion_subset_iff_forall_subset {α β : Type*} [decidable_eq β] {s : finset α} {t : finset β} {f : α → finset β} : s.bUnion f ⊆ t ↔ ∀ x ∈ s, f x ⊆ t := ⟨λ h x hx, (subset_bUnion_of_mem f hx).trans h, λ h x hx, let ⟨a, ha₁, ha₂⟩ := mem_bUnion.mp hx in h _ ha₁ ha₂⟩ @[simp] lemma bUnion_singleton_eq_self [decidable_eq α] : s.bUnion (singleton : α → finset α) = s := ext $ λ x, by simp only [mem_bUnion, mem_singleton, exists_prop, exists_eq_right'] lemma filter_bUnion (s : finset α) (f : α → finset β) (p : β → Prop) [decidable_pred p] : (s.bUnion f).filter p = s.bUnion (λ a, (f a).filter p) := begin ext b, simp only [mem_bUnion, exists_prop, mem_filter], split, { rintro ⟨⟨a, ha, hba⟩, hb⟩, exact ⟨a, ha, hba, hb⟩ }, { rintro ⟨a, ha, hba, hb⟩, exact ⟨⟨a, ha, hba⟩, hb⟩ } end lemma bUnion_filter_eq_of_maps_to [decidable_eq α] {s : finset α} {t : finset β} {f : α → β} (h : ∀ x ∈ s, f x ∈ t) : t.bUnion (λ a, s.filter $ (λ c, f c = a)) = s := by simpa only [disj_Union_eq_bUnion] using disj_Union_filter_eq_of_maps_to h lemma erase_bUnion (f : α → finset β) (s : finset α) (b : β) : (s.bUnion f).erase b = s.bUnion (λ x, (f x).erase b) := by { ext, simp only [finset.mem_bUnion, iff_self, exists_and_distrib_left, finset.mem_erase] } @[simp] lemma bUnion_nonempty : (s.bUnion t).nonempty ↔ ∃ x ∈ s, (t x).nonempty := by simp [finset.nonempty, ← exists_and_distrib_left, @exists_swap α] lemma nonempty.bUnion (hs : s.nonempty) (ht : ∀ x ∈ s, (t x).nonempty) : (s.bUnion t).nonempty := bUnion_nonempty.2 $ hs.imp $ λ x hx, ⟨hx, ht x hx⟩ lemma disjoint_bUnion_left (s : finset α) (f : α → finset β) (t : finset β) : disjoint (s.bUnion f) t ↔ (∀ i ∈ s, disjoint (f i) t) := begin classical, refine s.induction _ _, { simp only [forall_mem_empty_iff, bUnion_empty, disjoint_empty_left] }, { assume i s his ih, simp only [disjoint_union_left, bUnion_insert, his, forall_mem_insert, ih] } end lemma disjoint_bUnion_right (s : finset β) (t : finset α) (f : α → finset β) : disjoint s (t.bUnion f) ↔ ∀ i ∈ t, disjoint s (f i) := by simpa only [disjoint.comm] using disjoint_bUnion_left t f s end bUnion /-! ### choose -/ section choose variables (p : α → Prop) [decidable_pred p] (l : finset α) /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def choose_x (hp : (∃! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } := multiset.choose_x p l.val hp /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the ambient type. -/ def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp lemma choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose section pairwise variables {s : finset α} lemma pairwise_subtype_iff_pairwise_finset' (r : β → β → Prop) (f : α → β) : pairwise (r on λ x : s, f x) ↔ (s : set α).pairwise (r on f) := pairwise_subtype_iff_pairwise_set (s : set α) (r on f) lemma pairwise_subtype_iff_pairwise_finset (r : α → α → Prop) : pairwise (r on λ x : s, x) ↔ (s : set α).pairwise r := pairwise_subtype_iff_pairwise_finset' r id lemma pairwise_cons' {a : α} (ha : a ∉ s) (r : β → β → Prop) (f : α → β) : pairwise (r on λ a : s.cons a ha, f a) ↔ pairwise (r on λ a : s, f a) ∧ ∀ b ∈ s, r (f a) (f b) ∧ r (f b) (f a) := begin simp only [pairwise_subtype_iff_pairwise_finset', finset.coe_cons, set.pairwise_insert, finset.mem_coe, and.congr_right_iff], exact λ hsr, ⟨λ h b hb, h b hb $ by { rintro rfl, contradiction }, λ h b hb _, h b hb⟩, end lemma pairwise_cons {a : α} (ha : a ∉ s) (r : α → α → Prop) : pairwise (r on λ a : s.cons a ha, a) ↔ pairwise (r on λ a : s, a) ∧ ∀ b ∈ s, r a b ∧ r b a := pairwise_cons' ha r id end pairwise end finset namespace equiv /-- Inhabited types are equivalent to `option β` for some `β` by identifying `default α` with `none`. -/ def sigma_equiv_option_of_inhabited (α : Type u) [inhabited α] [decidable_eq α] : Σ (β : Type u), α ≃ option β := ⟨{x : α // x ≠ default}, { to_fun := λ (x : α), if h : x = default then none else some ⟨x, h⟩, inv_fun := option.elim default coe, left_inv := λ x, by { dsimp only, split_ifs; simp [*] }, right_inv := begin rintro (_|⟨x,h⟩), { simp }, { dsimp only, split_ifs with hi, { simpa [h] using hi }, { simp } } end }⟩ end equiv namespace multiset variable [decidable_eq α] lemma disjoint_to_finset {m1 m2 : multiset α} : _root_.disjoint m1.to_finset m2.to_finset ↔ m1.disjoint m2 := begin rw finset.disjoint_iff_ne, refine ⟨λ h a ha1 ha2, _, _⟩, { rw ← multiset.mem_to_finset at ha1 ha2, exact h _ ha1 _ ha2 rfl }, { rintros h a ha b hb rfl, rw multiset.mem_to_finset at ha hb, exact h ha hb } end end multiset namespace list variables [decidable_eq α] {l l' : list α} lemma disjoint_to_finset_iff_disjoint : _root_.disjoint l.to_finset l'.to_finset ↔ l.disjoint l' := multiset.disjoint_to_finset end list -- Assert that we define `finset` without the material on `list.sublists`. -- Note that we cannot use `list.sublists` itself as that is defined very early. assert_not_exists list.sublists_len assert_not_exists multiset.powerset
ee6aa7645be11f5bf7d800b616b03ad875f41c50
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/library/init/meta/inductive_compiler.lean
dd305914f1eefd5d86e4f0891e1ffeec14fc7045
[ "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
4,392
lean
/- Copyright (c) 2017 Daniel Selsam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Daniel Selsam -/ prelude import init.meta.tactic init.meta.simp_tactic init.meta.rewrite_tactic init.meta.converter init.function namespace inductive_compiler namespace tactic open tactic list private meta def simp_assumption (ls : simp_lemmas) (e : expr) : tactic (expr × expr) := do (a, new_e, pf) ← ext_simplify_core () {} ls (λ u, failed) (λ a s r p e, failed) (λ a s r p e, do ⟨u, new_e, pr⟩ ← conv.apply_lemmas_core s assumption r e, return ((), new_e, pr, tt)) `iff e, return (new_e, pf) private meta def simp_at_assumption (S : simp_lemmas := simp_lemmas.mk) (h : expr) (extra_lemmas : list expr := []) (cfg : simp_config := {}) : tactic unit := do when (expr.is_local_constant h = ff) (fail "tactic fsimp_at failed, the given expression is not a hypothesis"), htype ← infer_type h, S ← S.append extra_lemmas, (new_htype, heq) ← simp_assumption S htype, assert (expr.local_pp_name h) new_htype, mk_app `iff.mp [heq, h] >>= exact, try $ clear h private meta def fsimp (extra_lemmas : list expr := []) (cfg : simp_config := {}) : tactic unit := do S ← return (simp_lemmas.mk), S ← S.append extra_lemmas, simplify_goal S cfg >> try triv >> try (reflexivity reducible) private meta def at_end (e : expr) : ℕ → tactic (list (option expr)) | 0 := fail "at_end expected arity > 0" | 1 := return [some e] | (n+1) := at_end n >>= (λ xs, return (none :: xs)) private meta def heq_to_eq_or_id (n : name) (H : expr) : tactic expr := do Ht ← infer_type H, do { (A, lhs, B, rhs) ← match_heq Ht, unify A B, heq ← mk_app `eq [lhs, rhs], pr ← mk_app `eq_of_heq [H], assertv n heq pr, clear H, get_local n } <|> return H private meta def intros_simp (inj_simps : simp_lemmas) : expr → tactic (list expr) | (expr.pi n bi b d) := do H ← intro n, -- H ← heq_to_eq_or_id n H, try $ simp_at_assumption inj_simps H, H ← get_local n, tgt ← target, rest ← intros_simp tgt, return (H :: rest) | e := do return [] private meta def prove_conjuncts_by_assumption : list expr → expr → tactic unit | (pf :: pfs) ```(and %%α %%β) := do split, exact pf, prove_conjuncts_by_assumption pfs β | [pf] _ := exact pf | _ _ := fail "expecting same number of proofs as conjuncts" private meta def intros_and_subst : expr → tactic unit | (expr.pi n bi b d) := do H ← intro n, H ← heq_to_eq_or_id n H, Ht ← infer_type H, try $ do { match_eq Ht, subst H }, target >>= intros_and_subst | e := return () private meta def tgt_to_eq : tactic unit := do tgt ← target, try (do c ← mk_const `heq_of_eq, apply c) meta def prove_nested_inj (inj_simps : simp_lemmas) (inner_ir_inj_arrow : name) : tactic unit := do xs ← intros, triv <|> solve1 (do H_orig_eq ← return (ilast xs), c ← mk_const inner_ir_inj_arrow, inner_inj ← to_expr `(%%c %%H_orig_eq), apply inner_inj, pfs ← (target >>= intros_simp inj_simps), target >>= prove_conjuncts_by_assumption pfs) meta def prove_pack_inj (unpack unpack_pack : name) : tactic unit := do target >>= intros_and_subst, split, -- prove easy direction first swap, solve1 (do H ← intro1, H ← heq_to_eq_or_id `H_rhs_eq H, fsimp [H]), -- hard direction H ← intro1, H ← heq_to_eq_or_id `H_lhs_eq H, tgt_to_eq, Ht ← infer_type H, (lhs, rhs) ← match_eq Ht, arity ← return (expr.get_app_num_args lhs), args1 ← at_end lhs arity, args2 ← at_end rhs arity, lhs' ← mk_mapp unpack args1, rhs' ← mk_mapp unpack args2, H_ty ← mk_app `eq [lhs', rhs'], assert `H_up H_ty, solve1 (fsimp [H]), H_up ← get_local `H_up, solve1 (do e_unpack_pack ← mk_const unpack_pack, rewrite_at_core semireducible tt tt occurrences.all ff e_unpack_pack H_up, H_up ← get_local `H_up, rewrite_at_core semireducible tt tt occurrences.all ff e_unpack_pack H_up, H_up ← get_local `H_up, exact H_up) end tactic end inductive_compiler
ad7e45b4f3460f64f9cb68791ab726bf88d8da49
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/control/uliftable.lean
926cb00dd25d5df1ae5961f3aae7ee62d59c231e
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
5,579
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author(s): Simon Hudon -/ import control.monad.basic import control.monad.cont import control.monad.writer import data.equiv.basic import tactic.interactive /-! # Universe lifting for type families Some functors such as `option` and `list` are universe polymorphic. Unlike type polymorphism where `option α` is a function application and reasoning and generalizations that apply to functions can be used, `option.{u}` and `option.{v}` are not one function applied to two universe names but one polymorphic definition instantiated twice. This means that whatever works on `option.{u}` is hard to transport over to `option.{v}`. `uliftable` is an attempt at improving the situation. `uliftable option.{u} option.{v}` gives us a generic and composable way to use `option.{u}` in a context that requires `option.{v}`. It is often used in tandem with `ulift` but the two are purposefully decoupled. ## Main definitions * `uliftable` class ## Tags universe polymorphism functor -/ universes u₀ u₁ v₀ v₁ v₂ w w₀ w₁ /-- Given a universe polymorphic type family `M.{u} : Type u₁ → Type u₂`, this class convert between instantiations, from `M.{u} : Type u₁ → Type u₂` to `M.{v} : Type v₁ → Type v₂` and back -/ class uliftable (f : Type u₀ → Type u₁) (g : Type v₀ → Type v₁) := (congr [] {α β} : α ≃ β → f α ≃ g β) namespace uliftable /-- The most common practical use `uliftable` (together with `up`), this function takes `x : M.{u} α` and lifts it to M.{max u v} (ulift.{v} α) -/ @[reducible] def up {f : Type u₀ → Type u₁} {g : Type (max u₀ v₀) → Type v₁} [uliftable f g] {α} : f α → g (ulift α) := (uliftable.congr f g equiv.ulift.symm).to_fun /-- The most common practical use of `uliftable` (together with `up`), this function takes `x : M.{max u v} (ulift.{v} α)` and lowers it to `M.{u} α` -/ @[reducible] def down {f : Type u₀ → Type u₁} {g : Type (max u₀ v₀) → Type v₁} [uliftable f g] {α} : g (ulift α) → f α := (uliftable.congr f g equiv.ulift.symm).inv_fun /-- convenient shortcut to avoid manipulating `ulift` -/ def adapt_up {F : Type u₀ → Type u₁} {G : Type (max u₀ v₀) → Type v₁} [uliftable F G] [monad G] {α β} (x : F α) (f : α → G β) : G β := up x >>= f ∘ ulift.down /-- convenient shortcut to avoid manipulating `ulift` -/ def adapt_down {F : Type (max u₀ v₀) → Type u₁} {G : Type v₀ → Type v₁} [L : uliftable G F] [monad F] {α β} (x : F α) (f : α → G β) : G β := @down.{v₀ v₁ (max u₀ v₀)} G F L β $ x >>= @up.{v₀ v₁ (max u₀ v₀)} G F L β ∘ f /-- map function that moves up universes -/ def up_map {F : Type u₀ → Type u₁} {G : Type.{max u₀ v₀} → Type v₁} [inst : uliftable F G] [functor G] {α β} (f : α → β) (x : F α) : G β := functor.map (f ∘ ulift.down) (up x) /-- map function that moves down universes -/ def down_map {F : Type.{max u₀ v₀} → Type u₁} {G : Type → Type v₁} [inst : uliftable G F] [functor F] {α β} (f : α → β) (x : F α) : G β := down (functor.map (ulift.up ∘ f) x : F (ulift β)) @[simp] lemma up_down {f : Type u₀ → Type u₁} {g : Type (max u₀ v₀) → Type v₁} [uliftable f g] {α} (x : g (ulift α)) : up (down x : f α) = x := (uliftable.congr f g equiv.ulift.symm).right_inv _ @[simp] lemma down_up {f : Type u₀ → Type u₁} {g : Type (max u₀ v₀) → Type v₁} [uliftable f g] {α} (x : f α) : down (up x : g _) = x := (uliftable.congr f g equiv.ulift.symm).left_inv _ end uliftable open ulift instance : uliftable id id := { congr := λ α β F, F } /-- for specific state types, this function helps to create a uliftable instance -/ def state_t.uliftable' {s : Type u₀} {s' : Type u₁} {m : Type u₀ → Type v₀} {m' : Type u₁ → Type v₁} [uliftable m m'] (F : s ≃ s') : uliftable (state_t s m) (state_t s' m') := { congr := λ α β G, state_t.equiv $ equiv.Pi_congr F $ λ _, uliftable.congr _ _ $ equiv.prod_congr G F } instance {s m m'} [uliftable m m'] : uliftable (state_t s m) (state_t (ulift s) m') := state_t.uliftable' equiv.ulift.symm /-- for specific reader monads, this function helps to create a uliftable instance -/ def reader_t.uliftable' {s s' m m'} [uliftable m m'] (F : s ≃ s') : uliftable (reader_t s m) (reader_t s' m') := { congr := λ α β G, reader_t.equiv $ equiv.Pi_congr F $ λ _, uliftable.congr _ _ G } instance {s m m'} [uliftable m m'] : uliftable (reader_t s m) (reader_t (ulift s) m') := reader_t.uliftable' equiv.ulift.symm /-- for specific continuation passing monads, this function helps to create a uliftable instance -/ def cont_t.uliftable' {r r' m m'} [uliftable m m'] (F : r ≃ r') : uliftable (cont_t r m) (cont_t r' m') := { congr := λ α β, cont_t.equiv (uliftable.congr _ _ F) } instance {s m m'} [uliftable m m'] : uliftable (cont_t s m) (cont_t (ulift s) m') := cont_t.uliftable' equiv.ulift.symm /-- for specific writer monads, this function helps to create a uliftable instance -/ def writer_t.uliftable' {w w' m m'} [uliftable m m'] (F : w ≃ w') : uliftable (writer_t w m) (writer_t w' m') := { congr := λ α β G, writer_t.equiv $ uliftable.congr _ _ $ equiv.prod_congr G F } instance {s m m'} [uliftable m m'] : uliftable (writer_t s m) (writer_t (ulift s) m') := writer_t.uliftable' equiv.ulift.symm
9b0b73696c6f68433dabd2b36b0ab3178faa1aa1
9dc8cecdf3c4634764a18254e94d43da07142918
/src/category_theory/connected_components.lean
ba3c0b93c162f27b9cca985668e0fa017ff15809
[ "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
5,839
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import data.list.chain import category_theory.punit import category_theory.is_connected import category_theory.sigma.basic import category_theory.full_subcategory /-! # Connected components of a category Defines a type `connected_components J` indexing the connected components of a category, and the full subcategories giving each connected component: `component j : Type u₁`. We show that each `component j` is in fact connected. We show every category can be expressed as a disjoint union of its connected components, in particular `decomposed J` is the category (definitionally) given by the sigma-type of the connected components of `J`, and it is shown that this is equivalent to `J`. -/ universes v₁ v₂ v₃ u₁ u₂ noncomputable theory open category_theory.category namespace category_theory attribute [instance, priority 100] is_connected.is_nonempty variables {J : Type u₁} [category.{v₁} J] variables {C : Type u₂} [category.{u₁} C] /-- This type indexes the connected components of the category `J`. -/ def connected_components (J : Type u₁) [category.{v₁} J] : Type u₁ := quotient (zigzag.setoid J) instance [inhabited J] : inhabited (connected_components J) := ⟨quotient.mk' default⟩ /-- Given an index for a connected component, produce the actual component as a full subcategory. -/ @[derive category] def component (j : connected_components J) : Type u₁ := full_subcategory (λ k, quotient.mk' k = j) /-- The inclusion functor from a connected component to the whole category. -/ @[derive [full, faithful], simps {rhs_md := semireducible}] def component.ι (j) : component j ⥤ J := full_subcategory_inclusion _ /-- Each connected component of the category is nonempty. -/ instance (j : connected_components J) : nonempty (component j) := begin apply quotient.induction_on' j, intro k, refine ⟨⟨k, rfl⟩⟩, end instance (j : connected_components J) : inhabited (component j) := classical.inhabited_of_nonempty' /-- Each connected component of the category is connected. -/ instance (j : connected_components J) : is_connected (component j) := begin -- Show it's connected by constructing a zigzag (in `component j`) between any two objects apply is_connected_of_zigzag, rintro ⟨j₁, hj₁⟩ ⟨j₂, rfl⟩, -- We know that the underlying objects j₁ j₂ have some zigzag between them in `J` have h₁₂ : zigzag j₁ j₂ := quotient.exact' hj₁, -- Get an explicit zigzag as a list rcases list.exists_chain_of_relation_refl_trans_gen h₁₂ with ⟨l, hl₁, hl₂⟩, -- Everything which has a zigzag to j₂ can be lifted to the same component as `j₂`. let f : Π x, zigzag x j₂ → component (quotient.mk' j₂) := λ x h, ⟨x, quotient.sound' h⟩, -- Everything in our chosen zigzag from `j₁` to `j₂` has a zigzag to `j₂`. have hf : ∀ (a : J), a ∈ l → zigzag a j₂, { intros i hi, apply list.chain.induction (λ t, zigzag t j₂) _ hl₁ hl₂ _ _ _ (or.inr hi), { intros j k, apply relation.refl_trans_gen.head }, { apply relation.refl_trans_gen.refl } }, -- Now lift the zigzag from `j₁` to `j₂` in `J` to the same thing in `component j`. refine ⟨l.pmap f hf, _, _⟩, { refine @@list.chain_pmap_of_chain _ _ _ f (λ x y _ _ h, _) hl₁ h₁₂ _, exact zag_of_zag_obj (component.ι _) h }, { erw list.last_pmap _ f (j₁ :: l) (by simpa [h₁₂] using hf) (list.cons_ne_nil _ _), exact full_subcategory.ext _ _ hl₂ }, end /-- The disjoint union of `J`s connected components, written explicitly as a sigma-type with the category structure. This category is equivalent to `J`. -/ abbreviation decomposed (J : Type u₁) [category.{v₁} J] := Σ (j : connected_components J), component j /-- The inclusion of each component into the decomposed category. This is just `sigma.incl` but having this abbreviation helps guide typeclass search to get the right category instance on `decomposed J`. -/ -- This name may cause clashes further down the road, and so might need to be changed. abbreviation inclusion (j : connected_components J) : component j ⥤ decomposed J := sigma.incl _ /-- The forward direction of the equivalence between the decomposed category and the original. -/ @[simps {rhs_md := semireducible}] def decomposed_to (J : Type u₁) [category.{v₁} J] : decomposed J ⥤ J := sigma.desc component.ι @[simp] lemma inclusion_comp_decomposed_to (j : connected_components J) : inclusion j ⋙ decomposed_to J = component.ι j := rfl instance : full (decomposed_to J) := { preimage := begin rintro ⟨j', X, hX⟩ ⟨k', Y, hY⟩ f, dsimp at f, have : j' = k', rw [← hX, ← hY, quotient.eq'], exact relation.refl_trans_gen.single (or.inl ⟨f⟩), subst this, refine sigma.sigma_hom.mk f, end, witness' := begin rintro ⟨j', X, hX⟩ ⟨_, Y, rfl⟩ f, have : quotient.mk' Y = j', { rw [← hX, quotient.eq'], exact relation.refl_trans_gen.single (or.inr ⟨f⟩) }, subst this, refl, end } instance : faithful (decomposed_to J) := { map_injective' := begin rintro ⟨_, j, rfl⟩ ⟨_, k, hY⟩ ⟨_, _, _, f⟩ ⟨_, _, _, g⟩ e, change f = g at e, subst e, end } instance : ess_surj (decomposed_to J) := { mem_ess_image := λ j, ⟨⟨_, j, rfl⟩, ⟨iso.refl _⟩⟩ } instance : is_equivalence (decomposed_to J) := equivalence.of_fully_faithfully_ess_surj _ /-- This gives that any category is equivalent to a disjoint union of connected categories. -/ @[simps functor {rhs_md := semireducible}] def decomposed_equiv : decomposed J ≌ J := (decomposed_to J).as_equivalence end category_theory
d077f55448c10bbb85f85cd5e37d0490e5677db8
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/set_theory/ordinal.lean
279be2b0791a7a2e1cc4f96f314501e3ba54590c
[ "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
56,627
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import set_theory.cardinal /-! # Ordinals Ordinals are defined as equivalences of well-ordered sets under order isomorphism. They are endowed with a total order, where an ordinal is smaller than another one if it embeds into it as an initial segment (or, equivalently, in any way). This total order is well founded. ## Main definitions * `initial_seg r s`: type of order embeddings of `r` into `s` for which the range is an initial segment (i.e., if `b` belongs to the range, then any `b' < b` also belongs to the range). It is denoted by `r ≼i s`. * `principal_seg r s`: Type of order embeddings of `r` into `s` for which the range is a principal segment, i.e., an interval of the form `(-∞, top)` for some element `top`. It is denoted by `r ≺i s`. * `ordinal`: the type of ordinals (in a given universe) * `type r`: given a well-founded order `r`, this is the corresponding ordinal * `typein r a`: given a well-founded order `r` on a type `α`, and `a : α`, the ordinal corresponding to all elements smaller than `a`. * `enum r o h`: given a well-order `r` on a type `α`, and an ordinal `o` strictly smaller than the ordinal corresponding to `r` (this is the assumption `h`), returns the `o`-th element of `α`. In other words, the elements of `α` can be enumerated using ordinals up to `type r`. * `card o`: the cardinality of an ordinal `o`. * `lift` lifts an ordinal in universe `u` to an ordinal in universe `max u v`. For a version registering additionally that this is an initial segment embedding, see `lift.initial_seg`. For a version regiserting that it is a principal segment embedding if `u < v`, see `lift.principal_seg`. * `omega` is the first infinite ordinal. It is the order type of `ℕ`. * `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. The main properties of addition (and the other operations on ordinals) are stated and proved in `ordinal_arithmetic.lean`. Here, we only introduce it and prove its basic properties to deduce the fact that the order on ordinals is total (and well founded). * `succ o` is the successor of the ordinal `o`. * `min`: the minimal element of a nonempty indexed family of ordinals * `omin` : the minimal element of a nonempty set of ordinals * `ord c`: when `c` is a cardinal, `ord c` is the smallest ordinal with this cardinality. It is the canonical way to represent a cardinal with an ordinal. ## Notations * `r ≼i s`: the type of initial segment embeddings of `r` into `s`. * `r ≺i s`: the type of principal segment embeddings of `r` into `s`. * `ω` is a notation for the first infinite ordinal in the locale ordinal. -/ noncomputable theory open function cardinal set equiv open_locale classical cardinal universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-! ### Initial segments Order embeddings whose range is an initial segment of `s` (i.e., if `b` belongs to the range, then any `b' < b` also belongs to the range). The type of these embeddings from `r` to `s` is called `initial_seg r s`, and denoted by `r ≼i s`. -/ /-- If `r` is a relation on `α` and `s` in a relation on `β`, then `f : r ≼i s` is an order embedding whose range is an initial segment. That is, whenever `b < f a` in `β` then `b` is in the range of `f`. -/ @[nolint has_inhabited_instance] structure initial_seg {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends r ↪r s := (init : ∀ a b, s b (to_rel_embedding a) → ∃ a', to_rel_embedding a' = b) local infix ` ≼i `:25 := initial_seg namespace initial_seg instance : has_coe (r ≼i s) (r ↪r s) := ⟨initial_seg.to_rel_embedding⟩ instance : has_coe_to_fun (r ≼i s) := ⟨λ _, α → β, λ f x, (f : r ↪r s) x⟩ @[simp] theorem coe_fn_mk (f : r ↪r s) (o) : (@initial_seg.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_rel_embedding (f : r ≼i s) : (f.to_rel_embedding : α → β) = f := rfl @[simp] theorem coe_coe_fn (f : r ≼i s) : ((f : r ↪r s) : α → β) = f := rfl theorem init' (f : r ≼i s) {a : α} {b : β} : s b (f a) → ∃ a', f a' = b := f.init _ _ theorem init_iff (f : r ≼i s) {a : α} {b : β} : s b (f a) ↔ ∃ a', f a' = b ∧ r a' a := ⟨λ h, let ⟨a', e⟩ := f.init' h in ⟨a', e, (f : r ↪r s).map_rel_iff.1 (e.symm ▸ h)⟩, λ ⟨a', e, h⟩, e ▸ (f : r ↪r s).map_rel_iff.2 h⟩ /-- An order isomorphism is an initial segment -/ def of_iso (f : r ≃r s) : r ≼i s := ⟨f, λ a b h, ⟨f.symm b, rel_iso.apply_symm_apply f _⟩⟩ /-- The identity function shows that `≼i` is reflexive -/ @[refl] protected def refl (r : α → α → Prop) : r ≼i r := ⟨rel_embedding.refl _, λ a b h, ⟨_, rfl⟩⟩ /-- Composition of functions shows that `≼i` is transitive -/ @[trans] protected def trans (f : r ≼i s) (g : s ≼i t) : r ≼i t := ⟨f.1.trans g.1, λ a c h, begin simp at h ⊢, rcases g.2 _ _ h with ⟨b, rfl⟩, have h := g.1.map_rel_iff.1 h, rcases f.2 _ _ h with ⟨a', rfl⟩, exact ⟨a', rfl⟩ end⟩ @[simp] theorem refl_apply (x : α) : initial_seg.refl r x = x := rfl @[simp] theorem trans_apply (f : r ≼i s) (g : s ≼i t) (a : α) : (f.trans g) a = g (f a) := rfl theorem unique_of_extensional [is_extensional β s] : well_founded r → subsingleton (r ≼i s) | ⟨h⟩ := ⟨λ f g, begin suffices : (f : α → β) = g, { cases f, cases g, congr, exact rel_embedding.coe_fn_inj this }, funext a, have := h a, induction this with a H IH, refine @is_extensional.ext _ s _ _ _ (λ x, ⟨λ h, _, λ h, _⟩), { rcases f.init_iff.1 h with ⟨y, rfl, h'⟩, rw IH _ h', exact (g : r ↪r s).map_rel_iff.2 h' }, { rcases g.init_iff.1 h with ⟨y, rfl, h'⟩, rw ← IH _ h', exact (f : r ↪r s).map_rel_iff.2 h' } end⟩ instance [is_well_order β s] : subsingleton (r ≼i s) := ⟨λ a, @subsingleton.elim _ (unique_of_extensional (@rel_embedding.well_founded _ _ r s a is_well_order.wf)) a⟩ protected theorem eq [is_well_order β s] (f g : r ≼i s) (a) : f a = g a := by rw subsingleton.elim f g theorem antisymm.aux [is_well_order α r] (f : r ≼i s) (g : s ≼i r) : left_inverse g f := initial_seg.eq (f.trans g) (initial_seg.refl _) /-- If we have order embeddings between `α` and `β` whose images are initial segments, and `β` is a well-order then `α` and `β` are order-isomorphic. -/ def antisymm [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : r ≃r s := by haveI := f.to_rel_embedding.is_well_order; exact ⟨⟨f, g, antisymm.aux f g, antisymm.aux g f⟩, f.map_rel_iff'⟩ @[simp] theorem antisymm_to_fun [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : (antisymm f g : α → β) = f := rfl @[simp] theorem antisymm_symm [is_well_order α r] [is_well_order β s] (f : r ≼i s) (g : s ≼i r) : (antisymm f g).symm = antisymm g f := rel_iso.injective_coe_fn rfl theorem eq_or_principal [is_well_order β s] (f : r ≼i s) : surjective f ∨ ∃ b, ∀ x, s x b ↔ ∃ y, f y = x := or_iff_not_imp_right.2 $ λ h b, acc.rec_on (is_well_order.wf.apply b : acc s b) $ λ x H IH, not_forall_not.1 $ λ hn, h ⟨x, λ y, ⟨(IH _), λ ⟨a, e⟩, by rw ← e; exact (trichotomous _ _).resolve_right (not_or (hn a) (λ hl, not_exists.2 hn (f.init' hl)))⟩⟩ /-- Restrict the codomain of an initial segment -/ def cod_restrict (p : set β) (f : r ≼i s) (H : ∀ a, f a ∈ p) : r ≼i subrel s p := ⟨rel_embedding.cod_restrict p f H, λ a ⟨b, m⟩ (h : s b (f a)), let ⟨a', e⟩ := f.init' h in ⟨a', by clear _let_match; subst e; refl⟩⟩ @[simp] theorem cod_restrict_apply (p) (f : r ≼i s) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- Initial segment embedding of an order `r` into the disjoint union of `r` and `s`. -/ def le_add (r : α → α → Prop) (s : β → β → Prop) : r ≼i sum.lex r s := ⟨⟨⟨sum.inl, λ _ _, sum.inl.inj⟩, λ a b, sum.lex_inl_inl⟩, λ a b, by cases b; [exact λ _, ⟨_, rfl⟩, exact false.elim ∘ sum.lex_inr_inl]⟩ @[simp] theorem le_add_apply (r : α → α → Prop) (s : β → β → Prop) (a) : le_add r s a = sum.inl a := rfl end initial_seg /-! ### Principal segments Order embeddings whose range is a principal segment of `s` (i.e., an interval of the form `(-∞, top)` for some element `top` of `β`). The type of these embeddings from `r` to `s` is called `principal_seg r s`, and denoted by `r ≺i s`. Principal segments are in particular initial segments. -/ /-- If `r` is a relation on `α` and `s` in a relation on `β`, then `f : r ≺i s` is an order embedding whose range is an open interval `(-∞, top)` for some element `top` of `β`. Such order embeddings are called principal segments -/ @[nolint has_inhabited_instance] structure principal_seg {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends r ↪r s := (top : β) (down : ∀ b, s b top ↔ ∃ a, to_rel_embedding a = b) local infix ` ≺i `:25 := principal_seg namespace principal_seg instance : has_coe (r ≺i s) (r ↪r s) := ⟨principal_seg.to_rel_embedding⟩ instance : has_coe_to_fun (r ≺i s) := ⟨λ _, α → β, λ f, f⟩ @[simp] theorem coe_fn_mk (f : r ↪r s) (t o) : (@principal_seg.mk _ _ r s f t o : α → β) = f := rfl @[simp] theorem coe_fn_to_rel_embedding (f : r ≺i s) : (f.to_rel_embedding : α → β) = f := rfl @[simp] theorem coe_coe_fn (f : r ≺i s) : ((f : r ↪r s) : α → β) = f := rfl theorem down' (f : r ≺i s) {b : β} : s b f.top ↔ ∃ a, f a = b := f.down _ theorem lt_top (f : r ≺i s) (a : α) : s (f a) f.top := f.down'.2 ⟨_, rfl⟩ theorem init [is_trans β s] (f : r ≺i s) {a : α} {b : β} (h : s b (f a)) : ∃ a', f a' = b := f.down'.1 $ trans h $ f.lt_top _ /-- A principal segment is in particular an initial segment. -/ instance has_coe_initial_seg [is_trans β s] : has_coe (r ≺i s) (r ≼i s) := ⟨λ f, ⟨f.to_rel_embedding, λ a b, f.init⟩⟩ theorem coe_coe_fn' [is_trans β s] (f : r ≺i s) : ((f : r ≼i s) : α → β) = f := rfl theorem init_iff [is_trans β s] (f : r ≺i s) {a : α} {b : β} : s b (f a) ↔ ∃ a', f a' = b ∧ r a' a := @initial_seg.init_iff α β r s f a b theorem irrefl (r : α → α → Prop) [is_well_order α r] (f : r ≺i r) : false := begin have := f.lt_top f.top, rw [show f f.top = f.top, from initial_seg.eq ↑f (initial_seg.refl r) f.top] at this, exact irrefl _ this end /-- Composition of a principal segment with an initial segment, as a principal segment -/ def lt_le (f : r ≺i s) (g : s ≼i t) : r ≺i t := ⟨@rel_embedding.trans _ _ _ r s t f g, g f.top, λ a, by simp only [g.init_iff, f.down', exists_and_distrib_left.symm, exists_swap, rel_embedding.trans_apply, exists_eq_right']; refl⟩ @[simp] theorem lt_le_apply (f : r ≺i s) (g : s ≼i t) (a : α) : (f.lt_le g) a = g (f a) := rel_embedding.trans_apply _ _ _ @[simp] theorem lt_le_top (f : r ≺i s) (g : s ≼i t) : (f.lt_le g).top = g f.top := rfl /-- Composition of two principal segments as a principal segment -/ @[trans] protected def trans [is_trans γ t] (f : r ≺i s) (g : s ≺i t) : r ≺i t := lt_le f g @[simp] theorem trans_apply [is_trans γ t] (f : r ≺i s) (g : s ≺i t) (a : α) : (f.trans g) a = g (f a) := lt_le_apply _ _ _ @[simp] theorem trans_top [is_trans γ t] (f : r ≺i s) (g : s ≺i t) : (f.trans g).top = g f.top := rfl /-- Composition of an order isomorphism with a principal segment, as a principal segment -/ def equiv_lt (f : r ≃r s) (g : s ≺i t) : r ≺i t := ⟨@rel_embedding.trans _ _ _ r s t f g, g.top, λ c, suffices (∃ (a : β), g a = c) ↔ ∃ (a : α), g (f a) = c, by simpa [g.down], ⟨λ ⟨b, h⟩, ⟨f.symm b, by simp only [h, rel_iso.apply_symm_apply, rel_iso.coe_coe_fn]⟩, λ ⟨a, h⟩, ⟨f a, h⟩⟩⟩ /-- Composition of a principal segment with an order isomorphism, as a principal segment -/ def lt_equiv {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} (f : principal_seg r s) (g : s ≃r t) : principal_seg r t := ⟨@rel_embedding.trans _ _ _ r s t f g, g f.top, begin intro x, rw [← g.apply_symm_apply x, g.map_rel_iff, f.down', exists_congr], intro y, exact ⟨congr_arg g, λ h, g.to_equiv.bijective.1 h⟩ end⟩ @[simp] theorem equiv_lt_apply (f : r ≃r s) (g : s ≺i t) (a : α) : (equiv_lt f g) a = g (f a) := rel_embedding.trans_apply _ _ _ @[simp] theorem equiv_lt_top (f : r ≃r s) (g : s ≺i t) : (equiv_lt f g).top = g.top := rfl /-- Given a well order `s`, there is a most one principal segment embedding of `r` into `s`. -/ instance [is_well_order β s] : subsingleton (r ≺i s) := ⟨λ f g, begin have ef : (f : α → β) = g, { show ((f : r ≼i s) : α → β) = g, rw @subsingleton.elim _ _ (f : r ≼i s) g, refl }, have et : f.top = g.top, { refine @is_extensional.ext _ s _ _ _ (λ x, _), simp only [f.down, g.down, ef, coe_fn_to_rel_embedding] }, cases f, cases g, have := rel_embedding.coe_fn_inj ef; congr' end⟩ theorem top_eq [is_well_order γ t] (e : r ≃r s) (f : r ≺i t) (g : s ≺i t) : f.top = g.top := by rw subsingleton.elim f (principal_seg.equiv_lt e g); refl lemma top_lt_top {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} [is_well_order γ t] (f : principal_seg r s) (g : principal_seg s t) (h : principal_seg r t) : t h.top g.top := by { rw [subsingleton.elim h (f.trans g)], apply principal_seg.lt_top } /-- Any element of a well order yields a principal segment -/ def of_element {α : Type*} (r : α → α → Prop) (a : α) : subrel r {b | r b a} ≺i r := ⟨subrel.rel_embedding _ _, a, λ b, ⟨λ h, ⟨⟨_, h⟩, rfl⟩, λ ⟨⟨_, h⟩, rfl⟩, h⟩⟩ @[simp] theorem of_element_apply {α : Type*} (r : α → α → Prop) (a : α) (b) : of_element r a b = b.1 := rfl @[simp] theorem of_element_top {α : Type*} (r : α → α → Prop) (a : α) : (of_element r a).top = a := rfl /-- Restrict the codomain of a principal segment -/ def cod_restrict (p : set β) (f : r ≺i s) (H : ∀ a, f a ∈ p) (H₂ : f.top ∈ p) : r ≺i subrel s p := ⟨rel_embedding.cod_restrict p f H, ⟨f.top, H₂⟩, λ ⟨b, h⟩, f.down'.trans $ exists_congr $ λ a, show (⟨f a, H a⟩ : p).1 = _ ↔ _, from ⟨subtype.eq, congr_arg _⟩⟩ @[simp] theorem cod_restrict_apply (p) (f : r ≺i s) (H H₂ a) : cod_restrict p f H H₂ a = ⟨f a, H a⟩ := rfl @[simp] theorem cod_restrict_top (p) (f : r ≺i s) (H H₂) : (cod_restrict p f H H₂).top = ⟨f.top, H₂⟩ := rfl end principal_seg /-! ### Properties of initial and principal segments -/ /-- To an initial segment taking values in a well order, one can associate either a principal segment (if the range is not everything, hence one can take as top the minimum of the complement of the range) or an order isomorphism (if the range is everything). -/ def initial_seg.lt_or_eq [is_well_order β s] (f : r ≼i s) : (r ≺i s) ⊕ (r ≃r s) := if h : surjective f then sum.inr (rel_iso.of_surjective f h) else have h' : _, from (initial_seg.eq_or_principal f).resolve_left h, sum.inl ⟨f, classical.some h', classical.some_spec h'⟩ theorem initial_seg.lt_or_eq_apply_left [is_well_order β s] (f : r ≼i s) (g : r ≺i s) (a : α) : g a = f a := @initial_seg.eq α β r s _ g f a theorem initial_seg.lt_or_eq_apply_right [is_well_order β s] (f : r ≼i s) (g : r ≃r s) (a : α) : g a = f a := initial_seg.eq (initial_seg.of_iso g) f a /-- Composition of an initial segment taking values in a well order and a principal segment. -/ def initial_seg.le_lt [is_well_order β s] [is_trans γ t] (f : r ≼i s) (g : s ≺i t) : r ≺i t := match f.lt_or_eq with | sum.inl f' := f'.trans g | sum.inr f' := principal_seg.equiv_lt f' g end @[simp] theorem initial_seg.le_lt_apply [is_well_order β s] [is_trans γ t] (f : r ≼i s) (g : s ≺i t) (a : α) : (f.le_lt g) a = g (f a) := begin delta initial_seg.le_lt, cases h : f.lt_or_eq with f' f', { simp only [principal_seg.trans_apply, f.lt_or_eq_apply_left] }, { simp only [principal_seg.equiv_lt_apply, f.lt_or_eq_apply_right] } end namespace rel_embedding /-- Given an order embedding into a well order, collapse the order embedding by filling the gaps, to obtain an initial segment. Here, we construct the collapsed order embedding pointwise, but the proof of the fact that it is an initial segment will be given in `collapse`. -/ def collapse_F [is_well_order β s] (f : r ↪r s) : Π a, {b // ¬ s (f a) b} := (rel_embedding.well_founded f $ is_well_order.wf).fix $ λ a IH, begin let S := {b | ∀ a h, s (IH a h).1 b}, have : f a ∈ S, from λ a' h, ((trichotomous _ _) .resolve_left $ λ h', (IH a' h).2 $ trans (f.map_rel_iff.2 h) h') .resolve_left $ λ h', (IH a' h).2 $ h' ▸ f.map_rel_iff.2 h, exact ⟨is_well_order.wf.min S ⟨_, this⟩, is_well_order.wf.not_lt_min _ _ this⟩ end theorem collapse_F.lt [is_well_order β s] (f : r ↪r s) {a : α} : ∀ {a'}, r a' a → s (collapse_F f a').1 (collapse_F f a).1 := show (collapse_F f a).1 ∈ {b | ∀ a' (h : r a' a), s (collapse_F f a').1 b}, begin unfold collapse_F, rw well_founded.fix_eq, apply well_founded.min_mem _ _ end theorem collapse_F.not_lt [is_well_order β s] (f : r ↪r s) (a : α) {b} (h : ∀ a' (h : r a' a), s (collapse_F f a').1 b) : ¬ s b (collapse_F f a).1 := begin unfold collapse_F, rw well_founded.fix_eq, exact well_founded.not_lt_min _ _ _ (show b ∈ {b | ∀ a' (h : r a' a), s (collapse_F f a').1 b}, from h) end /-- Construct an initial segment from an order embedding into a well order, by collapsing it to fill the gaps. -/ def collapse [is_well_order β s] (f : r ↪r s) : r ≼i s := by haveI := rel_embedding.is_well_order f; exact ⟨rel_embedding.of_monotone (λ a, (collapse_F f a).1) (λ a b, collapse_F.lt f), λ a b, acc.rec_on (is_well_order.wf.apply b : acc s b) (λ b H IH a h, begin let S := {a | ¬ s (collapse_F f a).1 b}, have : S.nonempty := ⟨_, asymm h⟩, existsi (is_well_order.wf : well_founded r).min S this, refine ((@trichotomous _ s _ _ _).resolve_left _).resolve_right _, { exact (is_well_order.wf : well_founded r).min_mem S this }, { refine collapse_F.not_lt f _ (λ a' h', _), by_contradiction hn, exact is_well_order.wf.not_lt_min S this hn h' } end) a⟩ theorem collapse_apply [is_well_order β s] (f : r ↪r s) (a) : collapse f a = (collapse_F f a).1 := rfl end rel_embedding /-! ### Well order on an arbitrary type -/ section well_ordering_thm parameter {σ : Type u} open function theorem nonempty_embedding_to_cardinal : nonempty (σ ↪ cardinal.{u}) := embedding.total.resolve_left $ λ ⟨⟨f, hf⟩⟩, let g : σ → cardinal.{u} := inv_fun f in let ⟨x, (hx : g x = 2 ^ sum g)⟩ := inv_fun_surjective hf (2 ^ sum g) in have g x ≤ sum g, from le_sum.{u u} g x, not_le_of_gt (by rw hx; exact cantor _) this /-- An embedding of any type to the set of cardinals. -/ def embedding_to_cardinal : σ ↪ cardinal.{u} := classical.choice nonempty_embedding_to_cardinal /-- Any type can be endowed with a well order, obtained by pulling back the well order over cardinals by some embedding. -/ def well_ordering_rel : σ → σ → Prop := embedding_to_cardinal ⁻¹'o (<) instance well_ordering_rel.is_well_order : is_well_order σ well_ordering_rel := (rel_embedding.preimage _ _).is_well_order end well_ordering_thm /-! ### Definition of ordinals -/ /-- Bundled structure registering a well order on a type. Ordinals will be defined as a quotient of this type. -/ structure Well_order : Type (u+1) := (α : Type u) (r : α → α → Prop) (wo : is_well_order α r) attribute [instance] Well_order.wo namespace Well_order instance : inhabited Well_order := ⟨⟨pempty, _, empty_relation.is_well_order⟩⟩ end Well_order /-- Equivalence relation on well orders on arbitrary types in universe `u`, given by order isomorphism. -/ instance ordinal.is_equivalent : setoid Well_order := { r := λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≃r s), iseqv := ⟨λ⟨α, r, _⟩, ⟨rel_iso.refl _⟩, λ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨e⟩, ⟨e.symm⟩, λ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩⟩ } /-- `ordinal.{u}` is the type of well orders in `Type u`, up to order isomorphism. -/ def ordinal : Type (u + 1) := quotient ordinal.is_equivalent namespace ordinal /-- The order type of a well order is an ordinal. -/ def type (r : α → α → Prop) [wo : is_well_order α r] : ordinal := ⟦⟨α, r, wo⟩⟧ /-- The order type of an element inside a well order. For the embedding as a principal segment, see `typein.principal_seg`. -/ def typein (r : α → α → Prop) [is_well_order α r] (a : α) : ordinal := type (subrel r {b | r b a}) theorem type_def (r : α → α → Prop) [wo : is_well_order α r] : @eq ordinal ⟦⟨α, r, wo⟩⟧ (type r) := rfl @[simp] theorem type_def' (r : α → α → Prop) [is_well_order α r] {wo} : @eq ordinal ⟦⟨α, r, wo⟩⟧ (type r) := rfl theorem type_eq {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r = type s ↔ nonempty (r ≃r s) := quotient.eq @[simp] lemma type_out (o : ordinal) : type o.out.r = o := by { refine eq.trans _ (by rw [←quotient.out_eq o]), cases quotient.out o, refl } @[elab_as_eliminator] theorem induction_on {C : ordinal → Prop} (o : ordinal) (H : ∀ α r [is_well_order α r], by exactI C (type r)) : C o := quot.induction_on o $ λ ⟨α, r, wo⟩, @H α r wo /-! ### The order on ordinals -/ /-- Ordinal less-equal is defined such that well orders `r` and `s` satisfy `type r ≤ type s` if there exists a function embedding `r` as an initial segment of `s`. -/ protected def le (a b : ordinal) : Prop := quotient.lift_on₂ a b (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≼i s)) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, propext ⟨ λ ⟨h⟩, ⟨(initial_seg.of_iso f.symm).trans $ h.trans (initial_seg.of_iso g)⟩, λ ⟨h⟩, ⟨(initial_seg.of_iso f).trans $ h.trans (initial_seg.of_iso g.symm)⟩⟩ instance : has_le ordinal := ⟨ordinal.le⟩ theorem type_le {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r ≤ type s ↔ nonempty (r ≼i s) := iff.rfl theorem type_le' {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r ≤ type s ↔ nonempty (r ↪r s) := ⟨λ ⟨f⟩, ⟨f⟩, λ ⟨f⟩, ⟨f.collapse⟩⟩ /-- Ordinal less-than is defined such that well orders `r` and `s` satisfy `type r < type s` if there exists a function embedding `r` as a principal segment of `s`. -/ def lt (a b : ordinal) : Prop := quotient.lift_on₂ a b (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≺i s)) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, by exactI propext ⟨ λ ⟨h⟩, ⟨principal_seg.equiv_lt f.symm $ h.lt_le (initial_seg.of_iso g)⟩, λ ⟨h⟩, ⟨principal_seg.equiv_lt f $ h.lt_le (initial_seg.of_iso g.symm)⟩⟩ instance : has_lt ordinal := ⟨ordinal.lt⟩ @[simp] theorem type_lt {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r < type s ↔ nonempty (r ≺i s) := iff.rfl instance : partial_order ordinal := { le := (≤), lt := (<), le_refl := quot.ind $ by exact λ ⟨α, r, wo⟩, ⟨initial_seg.refl _⟩, le_trans := λ a b c, quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩ ⟨g⟩, ⟨f.trans g⟩, lt_iff_le_not_le := λ a b, quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, by exactI ⟨λ ⟨f⟩, ⟨⟨f⟩, λ ⟨g⟩, (f.lt_le g).irrefl _⟩, λ ⟨⟨f⟩, h⟩, sum.rec_on f.lt_or_eq (λ g, ⟨g⟩) (λ g, (h ⟨initial_seg.of_iso g.symm⟩).elim)⟩, le_antisymm := λ x b, show x ≤ b → b ≤ x → x = b, from quotient.induction_on₂ x b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨h₁⟩ ⟨h₂⟩, by exactI quot.sound ⟨initial_seg.antisymm h₁ h₂⟩ } /-- Given two ordinals `α ≤ β`, then `initial_seg_out α β` is the initial segment embedding of `α` to `β`, as map from a model type for `α` to a model type for `β`. -/ def initial_seg_out {α β : ordinal} (h : α ≤ β) : initial_seg α.out.r β.out.r := begin rw [←quotient.out_eq α, ←quotient.out_eq β] at h, revert h, cases quotient.out α, cases quotient.out β, exact classical.choice end /-- Given two ordinals `α < β`, then `principal_seg_out α β` is the principal segment embedding of `α` to `β`, as map from a model type for `α` to a model type for `β`. -/ def principal_seg_out {α β : ordinal} (h : α < β) : principal_seg α.out.r β.out.r := begin rw [←quotient.out_eq α, ←quotient.out_eq β] at h, revert h, cases quotient.out α, cases quotient.out β, exact classical.choice end /-- Given two ordinals `α = β`, then `rel_iso_out α β` is the order isomorphism between two model types for `α` and `β`. -/ def rel_iso_out {α β : ordinal} (h : α = β) : α.out.r ≃r β.out.r := begin rw [←quotient.out_eq α, ←quotient.out_eq β] at h, revert h, cases quotient.out α, cases quotient.out β, exact classical.choice ∘ quotient.exact end theorem typein_lt_type (r : α → α → Prop) [is_well_order α r] (a : α) : typein r a < type r := ⟨principal_seg.of_element _ _⟩ @[simp] theorem typein_top {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≺i s) : typein s f.top = type r := eq.symm $ quot.sound ⟨rel_iso.of_surjective (rel_embedding.cod_restrict _ f f.lt_top) (λ ⟨a, h⟩, by rcases f.down'.1 h with ⟨b, rfl⟩; exact ⟨b, rfl⟩)⟩ @[simp] theorem typein_apply {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≼i s) (a : α) : ordinal.typein s (f a) = ordinal.typein r a := eq.symm $ quotient.sound ⟨rel_iso.of_surjective (rel_embedding.cod_restrict _ ((subrel.rel_embedding _ _).trans f) (λ ⟨x, h⟩, by rw [rel_embedding.trans_apply]; exact f.to_rel_embedding.map_rel_iff.2 h)) (λ ⟨y, h⟩, by rcases f.init' h with ⟨a, rfl⟩; exact ⟨⟨a, f.to_rel_embedding.map_rel_iff.1 h⟩, subtype.eq $ rel_embedding.trans_apply _ _ _⟩)⟩ @[simp] theorem typein_lt_typein (r : α → α → Prop) [is_well_order α r] {a b : α} : typein r a < typein r b ↔ r a b := ⟨λ ⟨f⟩, begin have : f.top.1 = a, { let f' := principal_seg.of_element r a, let g' := f.trans (principal_seg.of_element r b), have : g'.top = f'.top, {rw subsingleton.elim f' g'}, exact this }, rw ← this, exact f.top.2 end, λ h, ⟨principal_seg.cod_restrict _ (principal_seg.of_element r a) (λ x, @trans _ r _ _ _ _ x.2 h) h⟩⟩ theorem typein_surj (r : α → α → Prop) [is_well_order α r] {o} (h : o < type r) : ∃ a, typein r a = o := induction_on o (λ β s _ ⟨f⟩, by exactI ⟨f.top, typein_top _⟩) h lemma typein_injective (r : α → α → Prop) [is_well_order α r] : injective (typein r) := injective_of_increasing r (<) (typein r) (λ x y, (typein_lt_typein r).2) theorem typein_inj (r : α → α → Prop) [is_well_order α r] {a b} : typein r a = typein r b ↔ a = b := injective.eq_iff (typein_injective r) /-! ### Enumerating elements in a well-order with ordinals. -/ /-- `enum r o h` is the `o`-th element of `α` ordered by `r`. That is, `enum` maps an initial segment of the ordinals, those less than the order type of `r`, to the elements of `α`. -/ def enum (r : α → α → Prop) [is_well_order α r] (o) : o < type r → α := quot.rec_on o (λ ⟨β, s, _⟩ h, (classical.choice h).top) $ λ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨h⟩, begin resetI, refine funext (λ (H₂ : type t < type r), _), have H₁ : type s < type r, {rwa type_eq.2 ⟨h⟩}, have : ∀ {o e} (H : o < type r), @@eq.rec (λ (o : ordinal), o < type r → α) (λ (h : type s < type r), (classical.choice h).top) e H = (classical.choice H₁).top, {intros, subst e}, exact (this H₂).trans (principal_seg.top_eq h (classical.choice H₁) (classical.choice H₂)) end theorem enum_type {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : s ≺i r) {h : type s < type r} : enum r (type s) h = f.top := principal_seg.top_eq (rel_iso.refl _) _ _ @[simp] theorem enum_typein (r : α → α → Prop) [is_well_order α r] (a : α) {h : typein r a < type r} : enum r (typein r a) h = a := enum_type (principal_seg.of_element r a) @[simp] theorem typein_enum (r : α → α → Prop) [is_well_order α r] {o} (h : o < type r) : typein r (enum r o h) = o := let ⟨a, e⟩ := typein_surj r h in by clear _let_match; subst e; rw enum_typein /-- A well order `r` is order isomorphic to the set of ordinals strictly smaller than the ordinal version of `r`. -/ def typein_iso (r : α → α → Prop) [is_well_order α r] : r ≃r subrel (<) (< type r) := ⟨⟨λ x, ⟨typein r x, typein_lt_type r x⟩, λ x, enum r x.1 x.2, λ y, enum_typein r y, λ ⟨y, hy⟩, subtype.eq (typein_enum r hy)⟩, λ a b, (typein_lt_typein r)⟩ theorem enum_lt {r : α → α → Prop} [is_well_order α r] {o₁ o₂ : ordinal} (h₁ : o₁ < type r) (h₂ : o₂ < type r) : r (enum r o₁ h₁) (enum r o₂ h₂) ↔ o₁ < o₂ := by rw [← typein_lt_typein r, typein_enum, typein_enum] lemma rel_iso_enum' {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≃r s) (o : ordinal) : ∀(hr : o < type r) (hs : o < type s), f (enum r o hr) = enum s o hs := begin refine induction_on o _, rintros γ t wo ⟨g⟩ ⟨h⟩, resetI, rw [enum_type g, enum_type (principal_seg.lt_equiv g f)], refl end lemma rel_iso_enum {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≃r s) (o : ordinal) (hr : o < type r) : f (enum r o hr) = enum s o (by {convert hr using 1, apply quotient.sound, exact ⟨f.symm⟩ }) := rel_iso_enum' _ _ _ _ theorem wf : @well_founded ordinal (<) := ⟨λ a, induction_on a $ λ α r wo, by exactI suffices ∀ a, acc (<) (typein r a), from ⟨_, λ o h, let ⟨a, e⟩ := typein_surj r h in e ▸ this a⟩, λ a, acc.rec_on (wo.wf.apply a) $ λ x H IH, ⟨_, λ o h, begin rcases typein_surj r (lt_trans h (typein_lt_type r _)) with ⟨b, rfl⟩, exact IH _ ((typein_lt_typein r).1 h) end⟩⟩ instance : has_well_founded ordinal := ⟨(<), wf⟩ /-- Principal segment version of the `typein` function, embedding a well order into ordinals as a principal segment. -/ def typein.principal_seg {α : Type u} (r : α → α → Prop) [is_well_order α r] : @principal_seg α ordinal.{u} r (<) := ⟨rel_embedding.of_monotone (typein r) (λ a b, (typein_lt_typein r).2), type r, λ b, ⟨λ h, ⟨enum r _ h, typein_enum r h⟩, λ ⟨a, e⟩, e ▸ typein_lt_type _ _⟩⟩ @[simp] theorem typein.principal_seg_coe (r : α → α → Prop) [is_well_order α r] : (typein.principal_seg r : α → ordinal) = typein r := rfl /-! ### Cardinality of ordinals -/ /-- The cardinal of an ordinal is the cardinal of any set with that order type. -/ def card (o : ordinal) : cardinal := quot.lift_on o (λ ⟨α, r, _⟩, mk α) $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨e⟩, quotient.sound ⟨e.to_equiv⟩ @[simp] theorem card_type (r : α → α → Prop) [is_well_order α r] : card (type r) = mk α := rfl lemma card_typein {r : α → α → Prop} [wo : is_well_order α r] (x : α) : mk {y // r y x} = (typein r x).card := rfl theorem card_le_card {o₁ o₂ : ordinal} : o₁ ≤ o₂ → card o₁ ≤ card o₂ := induction_on o₁ $ λ α r _, induction_on o₂ $ λ β s _ ⟨⟨⟨f, _⟩, _⟩⟩, ⟨f⟩ instance : has_zero ordinal := ⟨⟦⟨pempty, empty_relation, by apply_instance⟩⟧⟩ instance : inhabited ordinal := ⟨0⟩ theorem zero_eq_type_empty : 0 = @type empty empty_relation _ := quotient.sound ⟨⟨empty_equiv_pempty.symm, λ _ _, iff.rfl⟩⟩ @[simp] theorem card_zero : card 0 = 0 := rfl protected theorem zero_le (o : ordinal) : 0 ≤ o := induction_on o $ λ α r _, ⟨⟨⟨embedding.of_not_nonempty $ λ ⟨a⟩, a.elim, λ a, a.elim⟩, λ a, a.elim⟩⟩ @[simp] protected theorem le_zero {o : ordinal} : o ≤ 0 ↔ o = 0 := by simp only [le_antisymm_iff, ordinal.zero_le, and_true] protected theorem pos_iff_ne_zero {o : ordinal} : 0 < o ↔ o ≠ 0 := by simp only [lt_iff_le_and_ne, ordinal.zero_le, true_and, ne.def, eq_comm] instance : has_one ordinal := ⟨⟦⟨punit, empty_relation, by apply_instance⟩⟧⟩ theorem one_eq_type_unit : 1 = @type unit empty_relation _ := quotient.sound ⟨⟨punit_equiv_punit, λ _ _, iff.rfl⟩⟩ @[simp] theorem card_one : card 1 = 1 := rfl /-! ### Lifting ordinals to a higher universe -/ /-- The universe lift operation for ordinals, which embeds `ordinal.{u}` as a proper initial segment of `ordinal.{v}` for `v > u`. For the initial segment version, see `lift.initial_seg`. -/ def lift (o : ordinal.{u}) : ordinal.{max u v} := quotient.lift_on o (λ ⟨α, r, wo⟩, @type _ _ (@rel_embedding.is_well_order _ _ (@equiv.ulift.{v} α ⁻¹'o r) r (rel_iso.preimage equiv.ulift.{v} r) wo)) $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨f⟩, quot.sound ⟨(rel_iso.preimage equiv.ulift r).trans $ f.trans (rel_iso.preimage equiv.ulift s).symm⟩ theorem lift_type {α} (r : α → α → Prop) [is_well_order α r] : ∃ wo', lift (type r) = @type _ (@equiv.ulift.{v} α ⁻¹'o r) wo' := ⟨_, rfl⟩ theorem lift_umax : lift.{u (max u v)} = lift.{u v} := funext $ λ a, induction_on a $ λ α r _, quotient.sound ⟨(rel_iso.preimage equiv.ulift r).trans (rel_iso.preimage equiv.ulift r).symm⟩ theorem lift_id' (a : ordinal) : lift a = a := induction_on a $ λ α r _, quotient.sound ⟨rel_iso.preimage equiv.ulift r⟩ @[simp] theorem lift_id : ∀ a, lift.{u u} a = a := lift_id'.{u u} @[simp] theorem lift_lift (a : ordinal) : lift.{(max u v) w} (lift.{u v} a) = lift.{u (max v w)} a := induction_on a $ λ α r _, quotient.sound ⟨(rel_iso.preimage equiv.ulift _).trans $ (rel_iso.preimage equiv.ulift _).trans (rel_iso.preimage equiv.ulift _).symm⟩ theorem lift_type_le {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{u (max v w)} (type r) ≤ lift.{v (max u w)} (type s) ↔ nonempty (r ≼i s) := ⟨λ ⟨f⟩, ⟨(initial_seg.of_iso (rel_iso.preimage equiv.ulift r).symm).trans $ f.trans (initial_seg.of_iso (rel_iso.preimage equiv.ulift s))⟩, λ ⟨f⟩, ⟨(initial_seg.of_iso (rel_iso.preimage equiv.ulift r)).trans $ f.trans (initial_seg.of_iso (rel_iso.preimage equiv.ulift s).symm)⟩⟩ theorem lift_type_eq {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{u (max v w)} (type r) = lift.{v (max u w)} (type s) ↔ nonempty (r ≃r s) := quotient.eq.trans ⟨λ ⟨f⟩, ⟨(rel_iso.preimage equiv.ulift r).symm.trans $ f.trans (rel_iso.preimage equiv.ulift s)⟩, λ ⟨f⟩, ⟨(rel_iso.preimage equiv.ulift r).trans $ f.trans (rel_iso.preimage equiv.ulift s).symm⟩⟩ theorem lift_type_lt {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{u (max v w)} (type r) < lift.{v (max u w)} (type s) ↔ nonempty (r ≺i s) := by haveI := @rel_embedding.is_well_order _ _ (@equiv.ulift.{(max v w)} α ⁻¹'o r) r (rel_iso.preimage equiv.ulift.{(max v w)} r) _; haveI := @rel_embedding.is_well_order _ _ (@equiv.ulift.{(max u w)} β ⁻¹'o s) s (rel_iso.preimage equiv.ulift.{(max u w)} s) _; exact ⟨λ ⟨f⟩, ⟨(f.equiv_lt (rel_iso.preimage equiv.ulift r).symm).lt_le (initial_seg.of_iso (rel_iso.preimage equiv.ulift s))⟩, λ ⟨f⟩, ⟨(f.equiv_lt (rel_iso.preimage equiv.ulift r)).lt_le (initial_seg.of_iso (rel_iso.preimage equiv.ulift s).symm)⟩⟩ @[simp] theorem lift_le {a b : ordinal} : lift.{u v} a ≤ lift b ↔ a ≤ b := induction_on a $ λ α r _, induction_on b $ λ β s _, by rw ← lift_umax; exactI lift_type_le @[simp] theorem lift_inj {a b : ordinal} : lift a = lift b ↔ a = b := by simp only [le_antisymm_iff, lift_le] @[simp] theorem lift_lt {a b : ordinal} : lift a < lift b ↔ a < b := by simp only [lt_iff_le_not_le, lift_le] @[simp] theorem lift_zero : lift 0 = 0 := quotient.sound ⟨(rel_iso.preimage equiv.ulift _).trans ⟨pempty_equiv_pempty, λ a b, iff.rfl⟩⟩ theorem zero_eq_lift_type_empty : 0 = lift.{0 u} (@type empty empty_relation _) := by rw [← zero_eq_type_empty, lift_zero] @[simp] theorem lift_one : lift 1 = 1 := quotient.sound ⟨(rel_iso.preimage equiv.ulift _).trans ⟨punit_equiv_punit, λ a b, iff.rfl⟩⟩ theorem one_eq_lift_type_unit : 1 = lift.{0 u} (@type unit empty_relation _) := by rw [← one_eq_type_unit, lift_one] @[simp] theorem lift_card (a) : (card a).lift = card (lift a) := induction_on a $ λ α r _, rfl theorem lift_down' {a : cardinal.{u}} {b : ordinal.{max u v}} (h : card b ≤ a.lift) : ∃ a', lift a' = b := let ⟨c, e⟩ := cardinal.lift_down h in quotient.induction_on c (λ α, induction_on b $ λ β s _ e', begin resetI, rw [mk_def, card_type, ← cardinal.lift_id'.{(max u v) u} (mk β), ← cardinal.lift_umax.{u v}, lift_mk_eq.{u (max u v) (max u v)}] at e', cases e' with f, have g := rel_iso.preimage f s, haveI := (g : ⇑f ⁻¹'o s ↪r s).is_well_order, have := lift_type_eq.{u (max u v) (max u v)}.2 ⟨g⟩, rw [lift_id, lift_umax.{u v}] at this, exact ⟨_, this⟩ end) e theorem lift_down {a : ordinal.{u}} {b : ordinal.{max u v}} (h : b ≤ lift a) : ∃ a', lift a' = b := @lift_down' (card a) _ (by rw lift_card; exact card_le_card h) theorem le_lift_iff {a : ordinal.{u}} {b : ordinal.{max u v}} : b ≤ lift a ↔ ∃ a', lift a' = b ∧ a' ≤ a := ⟨λ h, let ⟨a', e⟩ := lift_down h in ⟨a', e, lift_le.1 $ e.symm ▸ h⟩, λ ⟨a', e, h⟩, e ▸ lift_le.2 h⟩ theorem lt_lift_iff {a : ordinal.{u}} {b : ordinal.{max u v}} : b < lift a ↔ ∃ a', lift a' = b ∧ a' < a := ⟨λ h, let ⟨a', e⟩ := lift_down (le_of_lt h) in ⟨a', e, lift_lt.1 $ e.symm ▸ h⟩, λ ⟨a', e, h⟩, e ▸ lift_lt.2 h⟩ /-- Initial segment version of the lift operation on ordinals, embedding `ordinal.{u}` in `ordinal.{v}` as an initial segment when `u ≤ v`. -/ def lift.initial_seg : @initial_seg ordinal.{u} ordinal.{max u v} (<) (<) := ⟨⟨⟨lift.{u v}, λ a b, lift_inj.1⟩, λ a b, lift_lt⟩, λ a b h, lift_down (le_of_lt h)⟩ @[simp] theorem lift.initial_seg_coe : (lift.initial_seg : ordinal → ordinal) = lift := rfl /-! ### The first infinite ordinal `omega` -/ /-- `ω` is the first infinite ordinal, defined as the order type of `ℕ`. -/ def omega : ordinal.{u} := lift $ @type ℕ (<) _ localized "notation `ω` := ordinal.omega.{0}" in ordinal theorem card_omega : card omega = cardinal.omega := rfl @[simp] theorem lift_omega : lift omega = omega := lift_lift _ /-! ### Definition and first properties of addition on ordinals In this paragraph, we introduce the addition on ordinals, and prove just enough properties to deduce that the order on ordinals is total (and therefore well-founded). Further properties of the addition, together with properties of the other operations, are proved in `ordinal_arithmetic.lean`. -/ /-- `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. -/ instance : has_add ordinal.{u} := ⟨λo₁ o₂, quotient.lift_on₂ o₁ o₂ (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, ⟦⟨α ⊕ β, sum.lex r s, by exactI sum.lex.is_well_order⟩⟧ : Well_order → Well_order → ordinal) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, quot.sound ⟨rel_iso.sum_lex_congr f g⟩⟩ @[simp] theorem card_add (o₁ o₂ : ordinal) : card (o₁ + o₂) = card o₁ + card o₂ := induction_on o₁ $ λ α r _, induction_on o₂ $ λ β s _, rfl @[simp] theorem card_nat (n : ℕ) : card.{u} n = n := by induction n; [refl, simp only [card_add, card_one, nat.cast_succ, *]] @[simp] theorem type_add {α β : Type u} (r : α → α → Prop) (s : β → β → Prop) [is_well_order α r] [is_well_order β s] : type r + type s = type (sum.lex r s) := rfl /-- The ordinal successor is the smallest ordinal larger than `o`. It is defined as `o + 1`. -/ def succ (o : ordinal) : ordinal := o + 1 theorem succ_eq_add_one (o) : succ o = o + 1 := rfl instance : add_monoid ordinal.{u} := { add := (+), zero := 0, zero_add := λ o, induction_on o $ λ α r _, eq.symm $ quotient.sound ⟨⟨(pempty_sum α).symm, λ a b, sum.lex_inr_inr⟩⟩, add_zero := λ o, induction_on o $ λ α r _, eq.symm $ quotient.sound ⟨⟨(sum_pempty α).symm, λ a b, sum.lex_inl_inl⟩⟩, add_assoc := λ o₁ o₂ o₃, quotient.induction_on₃ o₁ o₂ o₃ $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩, quot.sound ⟨⟨sum_assoc _ _ _, λ a b, begin rcases a with ⟨a|a⟩|a; rcases b with ⟨b|b⟩|b; simp only [sum_assoc_apply_in1, sum_assoc_apply_in2, sum_assoc_apply_in3, sum.lex_inl_inl, sum.lex_inr_inr, sum.lex.sep, sum.lex_inr_inl] end⟩⟩ } theorem add_le_add_left {a b : ordinal} : a ≤ b → ∀ c, c + a ≤ c + b := induction_on a $ λ α₁ r₁ _, induction_on b $ λ α₂ r₂ _ ⟨⟨⟨f, fo⟩, fi⟩⟩ c, induction_on c $ λ β s _, ⟨⟨⟨(embedding.refl _).sum_map f, λ a b, match a, b with | sum.inl a, sum.inl b := sum.lex_inl_inl.trans sum.lex_inl_inl.symm | sum.inl a, sum.inr b := by apply iff_of_true; apply sum.lex.sep | sum.inr a, sum.inl b := by apply iff_of_false; exact sum.lex_inr_inl | sum.inr a, sum.inr b := sum.lex_inr_inr.trans $ fo.trans sum.lex_inr_inr.symm end⟩, λ a b H, match a, b, H with | _, sum.inl b, _ := ⟨sum.inl b, rfl⟩ | sum.inl a, sum.inr b, H := (sum.lex_inr_inl H).elim | sum.inr a, sum.inr b, H := let ⟨w, h⟩ := fi _ _ (sum.lex_inr_inr.1 H) in ⟨sum.inr w, congr_arg sum.inr h⟩ end⟩⟩ theorem le_add_right (a b : ordinal) : a ≤ a + b := by simpa only [add_zero] using add_le_add_left (ordinal.zero_le b) a theorem add_le_add_right {a b : ordinal} : a ≤ b → ∀ c, a + c ≤ b + c := induction_on a $ λ α₁ r₁ hr₁, induction_on b $ λ α₂ r₂ hr₂ ⟨⟨⟨f, fo⟩, fi⟩⟩ c, induction_on c $ λ β s hs, (@type_le' _ _ _ _ (@sum.lex.is_well_order _ _ _ _ hr₁ hs) (@sum.lex.is_well_order _ _ _ _ hr₂ hs)).2 ⟨⟨f.sum_map (embedding.refl _), λ a b, begin split; intro H, { cases a with a a; cases b with b b; cases H; constructor; [rwa ← fo, assumption] }, { cases H; constructor; [rwa fo, assumption] } end⟩⟩ theorem le_add_left (a b : ordinal) : a ≤ b + a := by simpa only [zero_add] using add_le_add_right (ordinal.zero_le b) a theorem lt_succ_self (o : ordinal.{u}) : o < succ o := induction_on o $ λ α r _, ⟨⟨⟨⟨λ x, sum.inl x, λ _ _, sum.inl.inj⟩, λ _ _, sum.lex_inl_inl⟩, sum.inr punit.star, λ b, sum.rec_on b (λ x, ⟨λ _, ⟨x, rfl⟩, λ _, sum.lex.sep _ _⟩) (λ x, sum.lex_inr_inr.trans ⟨false.elim, λ ⟨x, H⟩, sum.inl_ne_inr H⟩)⟩⟩ theorem succ_le {a b : ordinal} : succ a ≤ b ↔ a < b := ⟨lt_of_lt_of_le (lt_succ_self _), induction_on a $ λ α r hr, induction_on b $ λ β s hs ⟨⟨f, t, hf⟩⟩, begin refine ⟨⟨@rel_embedding.of_monotone (α ⊕ punit) β _ _ (@sum.lex.is_well_order _ _ _ _ hr _).1.1 (@is_asymm_of_is_trans_of_is_irrefl _ _ hs.1.2.2 hs.1.2.1) (sum.rec _ _) (λ a b, _), λ a b, _⟩⟩, { exact f }, { exact λ _, t }, { rcases a with a|_; rcases b with b|_, { simpa only [sum.lex_inl_inl] using f.map_rel_iff.2 }, { intro _, rw hf, exact ⟨_, rfl⟩ }, { exact false.elim ∘ sum.lex_inr_inl }, { exact false.elim ∘ sum.lex_inr_inr.1 } }, { rcases a with a|_, { intro h, have := @principal_seg.init _ _ _ _ hs.1.2.2 ⟨f, t, hf⟩ _ _ h, cases this with w h, exact ⟨sum.inl w, h⟩ }, { intro h, cases (hf b).1 h with w h, exact ⟨sum.inl w, h⟩ } } end⟩ theorem le_total (a b : ordinal) : a ≤ b ∨ b ≤ a := match lt_or_eq_of_le (le_add_left b a), lt_or_eq_of_le (le_add_right a b) with | or.inr h, _ := by rw h; exact or.inl (le_add_right _ _) | _, or.inr h := by rw h; exact or.inr (le_add_left _ _) | or.inl h₁, or.inl h₂ := induction_on a (λ α₁ r₁ _, induction_on b $ λ α₂ r₂ _ ⟨f⟩ ⟨g⟩, begin resetI, rw [← typein_top f, ← typein_top g, le_iff_lt_or_eq, le_iff_lt_or_eq, typein_lt_typein, typein_lt_typein], rcases trichotomous_of (sum.lex r₁ r₂) g.top f.top with h|h|h; [exact or.inl (or.inl h), {left, right, rw h}, exact or.inr (or.inl h)] end) h₁ h₂ end instance : linear_order ordinal := { le_total := le_total, decidable_le := classical.dec_rel _, ..ordinal.partial_order } instance : is_well_order ordinal (<) := ⟨wf⟩ @[simp] lemma typein_le_typein (r : α → α → Prop) [is_well_order α r] {x x' : α} : typein r x ≤ typein r x' ↔ ¬r x' x := by rw [←not_lt, typein_lt_typein] lemma enum_le_enum (r : α → α → Prop) [is_well_order α r] {o o' : ordinal} (ho : o < type r) (ho' : o' < type r) : ¬r (enum r o' ho') (enum r o ho) ↔ o ≤ o' := by rw [←@not_lt _ _ o' o, enum_lt ho'] /-- `univ.{u v}` is the order type of the ordinals of `Type u` as a member of `ordinal.{v}` (when `u < v`). It is an inaccessible cardinal. -/ def univ := lift.{(u+1) v} (@type ordinal.{u} (<) _) theorem univ_id : univ.{u (u+1)} = @type ordinal.{u} (<) _ := lift_id _ @[simp] theorem lift_univ : lift.{_ w} univ.{u v} = univ.{u (max v w)} := lift_lift _ theorem univ_umax : univ.{u (max (u+1) v)} = univ.{u v} := congr_fun lift_umax _ /-- Principal segment version of the lift operation on ordinals, embedding `ordinal.{u}` in `ordinal.{v}` as a principal segment when `u < v`. -/ def lift.principal_seg : @principal_seg ordinal.{u} ordinal.{max (u+1) v} (<) (<) := ⟨↑lift.initial_seg.{u (max (u+1) v)}, univ.{u v}, begin refine λ b, induction_on b _, introsI β s _, rw [univ, ← lift_umax], split; intro h, { rw ← lift_id (type s) at h ⊢, cases lift_type_lt.1 h with f, cases f with f a hf, existsi a, revert hf, apply induction_on a, introsI α r _ hf, refine lift_type_eq.{u (max (u+1) v) (max (u+1) v)}.2 ⟨(rel_iso.of_surjective (rel_embedding.of_monotone _ _) _).symm⟩, { exact λ b, enum r (f b) ((hf _).2 ⟨_, rfl⟩) }, { refine λ a b h, (typein_lt_typein r).1 _, rw [typein_enum, typein_enum], exact f.map_rel_iff.2 h }, { intro a', cases (hf _).1 (typein_lt_type _ a') with b e, existsi b, simp, simp [e] } }, { cases h with a e, rw [← e], apply induction_on a, introsI α r _, exact lift_type_lt.{u (u+1) (max (u+1) v)}.2 ⟨typein.principal_seg r⟩ } end⟩ @[simp] theorem lift.principal_seg_coe : (lift.principal_seg.{u v} : ordinal → ordinal) = lift.{u (max (u+1) v)} := rfl @[simp] theorem lift.principal_seg_top : lift.principal_seg.top = univ := rfl theorem lift.principal_seg_top' : lift.principal_seg.{u (u+1)}.top = @type ordinal.{u} (<) _ := by simp only [lift.principal_seg_top, univ_id] /-! ### Minimum -/ /-- The minimal element of a nonempty family of ordinals -/ def min {ι} (I : nonempty ι) (f : ι → ordinal) : ordinal := wf.min (set.range f) (let ⟨i⟩ := I in ⟨_, set.mem_range_self i⟩) theorem min_eq {ι} (I) (f : ι → ordinal) : ∃ i, min I f = f i := let ⟨i, e⟩ := wf.min_mem (set.range f) _ in ⟨i, e.symm⟩ theorem min_le {ι I} (f : ι → ordinal) (i) : min I f ≤ f i := le_of_not_gt $ wf.not_lt_min (set.range f) _ (set.mem_range_self i) theorem le_min {ι I} {f : ι → ordinal} {a} : a ≤ min I f ↔ ∀ i, a ≤ f i := ⟨λ h i, le_trans h (min_le _ _), λ h, let ⟨i, e⟩ := min_eq I f in e.symm ▸ h i⟩ /-- The minimal element of a nonempty set of ordinals -/ def omin (S : set ordinal.{u}) (H : ∃ x, x ∈ S) : ordinal.{u} := @min.{(u+2) u} S (let ⟨x, px⟩ := H in ⟨⟨x, px⟩⟩) subtype.val theorem omin_mem (S H) : omin S H ∈ S := let ⟨⟨i, h⟩, e⟩ := @min_eq S _ _ in (show omin S H = i, from e).symm ▸ h theorem le_omin {S H a} : a ≤ omin S H ↔ ∀ i ∈ S, a ≤ i := le_min.trans set_coe.forall theorem omin_le {S H i} (h : i ∈ S) : omin S H ≤ i := le_omin.1 (le_refl _) _ h @[simp] theorem lift_min {ι} (I) (f : ι → ordinal) : lift (min I f) = min I (lift ∘ f) := le_antisymm (le_min.2 $ λ a, lift_le.2 $ min_le _ a) $ let ⟨i, e⟩ := min_eq I (lift ∘ f) in by rw e; exact lift_le.2 (le_min.2 $ λ j, lift_le.1 $ by have := min_le (lift ∘ f) j; rwa e at this) end ordinal /-! ### Representing a cardinal with an ordinal -/ namespace cardinal open ordinal /-- The ordinal corresponding to a cardinal `c` is the least ordinal whose cardinal is `c`. For the order-embedding version, see `ord.order_embedding`. -/ def ord (c : cardinal) : ordinal := begin let ι := λ α, {r // is_well_order α r}, have : Π α, ι α := λ α, ⟨well_ordering_rel, by apply_instance⟩, let F := λ α, ordinal.min ⟨this _⟩ (λ i:ι α, ⟦⟨α, i.1, i.2⟩⟧), refine quot.lift_on c F _, suffices : ∀ {α β}, α ≈ β → F α ≤ F β, from λ α β h, le_antisymm (this h) (this (setoid.symm h)), intros α β h, cases h with f, refine ordinal.le_min.2 (λ i, _), haveI := @rel_embedding.is_well_order _ _ (f ⁻¹'o i.1) _ ↑(rel_iso.preimage f i.1) i.2, rw ← show type (f ⁻¹'o i.1) = ⟦⟨β, i.1, i.2⟩⟧, from quot.sound ⟨rel_iso.preimage f i.1⟩, exact ordinal.min_le (λ i:ι α, ⟦⟨α, i.1, i.2⟩⟧) ⟨_, _⟩ end @[nolint def_lemma doc_blame] -- TODO: This should be a theorem but Lean fails to synthesize the placeholder def ord_eq_min (α : Type u) : ord (mk α) = @ordinal.min _ _ (λ i:{r // is_well_order α r}, ⟦⟨α, i.1, i.2⟩⟧) := rfl theorem ord_eq (α) : ∃ (r : α → α → Prop) [wo : is_well_order α r], ord (mk α) = @type α r wo := let ⟨⟨r, wo⟩, h⟩ := @ordinal.min_eq {r // is_well_order α r} ⟨⟨well_ordering_rel, by apply_instance⟩⟩ (λ i:{r // is_well_order α r}, ⟦⟨α, i.1, i.2⟩⟧) in ⟨r, wo, h⟩ theorem ord_le_type (r : α → α → Prop) [is_well_order α r] : ord (mk α) ≤ ordinal.type r := @ordinal.min_le {r // is_well_order α r} ⟨⟨well_ordering_rel, by apply_instance⟩⟩ (λ i:{r // is_well_order α r}, ⟦⟨α, i.1, i.2⟩⟧) ⟨r, _⟩ theorem ord_le {c o} : ord c ≤ o ↔ c ≤ o.card := quotient.induction_on c $ λ α, induction_on o $ λ β s _, let ⟨r, _, e⟩ := ord_eq α in begin resetI, simp only [mk_def, card_type], split; intro h, { rw e at h, exact let ⟨f⟩ := h in ⟨f.to_embedding⟩ }, { cases h with f, have g := rel_embedding.preimage f s, haveI := rel_embedding.is_well_order g, exact le_trans (ord_le_type _) (type_le'.2 ⟨g⟩) } end theorem lt_ord {c o} : o < ord c ↔ o.card < c := by rw [← not_le, ← not_le, ord_le] @[simp] theorem card_ord (c) : (ord c).card = c := quotient.induction_on c $ λ α, let ⟨r, _, e⟩ := ord_eq α in by simp only [mk_def, e, card_type] theorem ord_card_le (o : ordinal) : o.card.ord ≤ o := ord_le.2 (le_refl _) lemma lt_ord_succ_card (o : ordinal) : o < o.card.succ.ord := by { rw [lt_ord], apply cardinal.lt_succ_self } @[simp] theorem ord_le_ord {c₁ c₂} : ord c₁ ≤ ord c₂ ↔ c₁ ≤ c₂ := by simp only [ord_le, card_ord] @[simp] theorem ord_lt_ord {c₁ c₂} : ord c₁ < ord c₂ ↔ c₁ < c₂ := by simp only [lt_ord, card_ord] @[simp] theorem ord_zero : ord 0 = 0 := le_antisymm (ord_le.2 $ zero_le _) (ordinal.zero_le _) @[simp] theorem ord_nat (n : ℕ) : ord n = n := le_antisymm (ord_le.2 $ by simp only [card_nat]) $ begin induction n with n IH, { apply ordinal.zero_le }, { exact (@ordinal.succ_le n _).2 (lt_of_le_of_lt IH $ ord_lt_ord.2 $ nat_cast_lt.2 (nat.lt_succ_self n)) } end @[simp] theorem lift_ord (c) : (ord c).lift = ord (lift c) := eq_of_forall_ge_iff $ λ o, le_iff_le_iff_lt_iff_lt.2 $ begin split; intro h, { rcases ordinal.lt_lift_iff.1 h with ⟨a, e, h⟩, rwa [← e, lt_ord, ← lift_card, lift_lt, ← lt_ord] }, { rw lt_ord at h, rcases lift_down' (le_of_lt h) with ⟨o, rfl⟩, rw [← lift_card, lift_lt] at h, rwa [ordinal.lift_lt, lt_ord] } end lemma mk_ord_out (c : cardinal) : mk c.ord.out.α = c := by rw [←card_type c.ord.out.r, type_out, card_ord] lemma card_typein_lt (r : α → α → Prop) [is_well_order α r] (x : α) (h : ord (mk α) = type r) : card (typein r x) < mk α := by { rw [←ord_lt_ord, h], refine lt_of_le_of_lt (ord_card_le _) (typein_lt_type r x) } lemma card_typein_out_lt (c : cardinal) (x : c.ord.out.α) : card (typein c.ord.out.r x) < c := by { convert card_typein_lt c.ord.out.r x _, rw [mk_ord_out], rw [type_out, mk_ord_out] } lemma ord_injective : injective ord := by { intros c c' h, rw [←card_ord c, ←card_ord c', h] } /-- The ordinal corresponding to a cardinal `c` is the least ordinal whose cardinal is `c`. This is the order-embedding version. For the regular function, see `ord`. -/ def ord.order_embedding : cardinal ↪o ordinal := rel_embedding.order_embedding_of_lt_embedding (rel_embedding.of_monotone cardinal.ord $ λ a b, cardinal.ord_lt_ord.2) @[simp] theorem ord.order_embedding_coe : (ord.order_embedding : cardinal → ordinal) = ord := rfl /-- The cardinal `univ` is the cardinality of ordinal `univ`, or equivalently the cardinal of `ordinal.{u}`, or `cardinal.{u}`, as an element of `cardinal.{v}` (when `u < v`). -/ def univ := lift.{(u+1) v} (mk ordinal) theorem univ_id : univ.{u (u+1)} = mk ordinal := lift_id _ @[simp] theorem lift_univ : lift.{_ w} univ.{u v} = univ.{u (max v w)} := lift_lift _ theorem univ_umax : univ.{u (max (u+1) v)} = univ.{u v} := congr_fun lift_umax _ theorem lift_lt_univ (c : cardinal) : lift.{u (u+1)} c < univ.{u (u+1)} := by simpa only [lift.principal_seg_coe, lift_ord, lift_succ, ord_le, succ_le] using le_of_lt (lift.principal_seg.{u (u+1)}.lt_top (succ c).ord) theorem lift_lt_univ' (c : cardinal) : lift.{u (max (u+1) v)} c < univ.{u v} := by simpa only [lift_lift, lift_univ, univ_umax] using lift_lt.{_ (max (u+1) v)}.2 (lift_lt_univ c) @[simp] theorem ord_univ : ord univ.{u v} = ordinal.univ.{u v} := le_antisymm (ord_card_le _) $ le_of_forall_lt $ λ o h, lt_ord.2 begin rcases lift.principal_seg.{u v}.down'.1 (by simpa only [lift.principal_seg_coe] using h) with ⟨o', rfl⟩, simp only [lift.principal_seg_coe], rw [← lift_card], apply lift_lt_univ' end theorem lt_univ {c} : c < univ.{u (u+1)} ↔ ∃ c', c = lift.{u (u+1)} c' := ⟨λ h, begin have := ord_lt_ord.2 h, rw ord_univ at this, cases lift.principal_seg.{u (u+1)}.down'.1 (by simpa only [lift.principal_seg_top]) with o e, have := card_ord c, rw [← e, lift.principal_seg_coe, ← lift_card] at this, exact ⟨_, this.symm⟩ end, λ ⟨c', e⟩, e.symm ▸ lift_lt_univ _⟩ theorem lt_univ' {c} : c < univ.{u v} ↔ ∃ c', c = lift.{u (max (u+1) v)} c' := ⟨λ h, let ⟨a, e, h'⟩ := lt_lift_iff.1 h in begin rw [← univ_id] at h', rcases lt_univ.{u}.1 h' with ⟨c', rfl⟩, exact ⟨c', by simp only [e.symm, lift_lift]⟩ end, λ ⟨c', e⟩, e.symm ▸ lift_lt_univ' _⟩ end cardinal namespace ordinal @[simp] theorem card_univ : card univ = cardinal.univ := rfl end ordinal
6885b37c4c90d30f2474bd5090ce16f7337ff15c
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/InvolutiveFixes.lean
64810bc17a02897ad652d209cb41d73ad74b984b
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
6,527
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section InvolutiveFixes structure InvolutiveFixes (A : Type) : Type := (one : A) (prim : (A → A)) (fixes_prim_one : (prim one) = one) open InvolutiveFixes structure Sig (AS : Type) : Type := (oneS : AS) (primS : (AS → AS)) structure Product (A : Type) : Type := (oneP : (Prod A A)) (primP : ((Prod A A) → (Prod A A))) (fixes_prim_1P : (primP oneP) = oneP) structure Hom {A1 : Type} {A2 : Type} (In1 : (InvolutiveFixes A1)) (In2 : (InvolutiveFixes A2)) : Type := (hom : (A1 → A2)) (pres_one : (hom (one In1)) = (one In2)) (pres_prim : (∀ {x1 : A1} , (hom ((prim In1) x1)) = ((prim In2) (hom x1)))) structure RelInterp {A1 : Type} {A2 : Type} (In1 : (InvolutiveFixes A1)) (In2 : (InvolutiveFixes A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_one : (interp (one In1) (one In2))) (interp_prim : (∀ {x1 : A1} {y1 : A2} , ((interp x1 y1) → (interp ((prim In1) x1) ((prim In2) y1))))) inductive InvolutiveFixesTerm : Type | oneL : InvolutiveFixesTerm | primL : (InvolutiveFixesTerm → InvolutiveFixesTerm) open InvolutiveFixesTerm inductive ClInvolutiveFixesTerm (A : Type) : Type | sing : (A → ClInvolutiveFixesTerm) | oneCl : ClInvolutiveFixesTerm | primCl : (ClInvolutiveFixesTerm → ClInvolutiveFixesTerm) open ClInvolutiveFixesTerm inductive OpInvolutiveFixesTerm (n : ℕ) : Type | v : ((fin n) → OpInvolutiveFixesTerm) | oneOL : OpInvolutiveFixesTerm | primOL : (OpInvolutiveFixesTerm → OpInvolutiveFixesTerm) open OpInvolutiveFixesTerm inductive OpInvolutiveFixesTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpInvolutiveFixesTerm2) | sing2 : (A → OpInvolutiveFixesTerm2) | oneOL2 : OpInvolutiveFixesTerm2 | primOL2 : (OpInvolutiveFixesTerm2 → OpInvolutiveFixesTerm2) open OpInvolutiveFixesTerm2 def simplifyCl {A : Type} : ((ClInvolutiveFixesTerm A) → (ClInvolutiveFixesTerm A)) | (primCl oneCl) := oneCl | oneCl := oneCl | (primCl x1) := (primCl (simplifyCl x1)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpInvolutiveFixesTerm n) → (OpInvolutiveFixesTerm n)) | (primOL oneOL) := oneOL | oneOL := oneOL | (primOL x1) := (primOL (simplifyOpB x1)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpInvolutiveFixesTerm2 n A) → (OpInvolutiveFixesTerm2 n A)) | (primOL2 oneOL2) := oneOL2 | oneOL2 := oneOL2 | (primOL2 x1) := (primOL2 (simplifyOp x1)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((InvolutiveFixes A) → (InvolutiveFixesTerm → A)) | In oneL := (one In) | In (primL x1) := ((prim In) (evalB In x1)) def evalCl {A : Type} : ((InvolutiveFixes A) → ((ClInvolutiveFixesTerm A) → A)) | In (sing x1) := x1 | In oneCl := (one In) | In (primCl x1) := ((prim In) (evalCl In x1)) def evalOpB {A : Type} {n : ℕ} : ((InvolutiveFixes A) → ((vector A n) → ((OpInvolutiveFixesTerm n) → A))) | In vars (v x1) := (nth vars x1) | In vars oneOL := (one In) | In vars (primOL x1) := ((prim In) (evalOpB In vars x1)) def evalOp {A : Type} {n : ℕ} : ((InvolutiveFixes A) → ((vector A n) → ((OpInvolutiveFixesTerm2 n A) → A))) | In vars (v2 x1) := (nth vars x1) | In vars (sing2 x1) := x1 | In vars oneOL2 := (one In) | In vars (primOL2 x1) := ((prim In) (evalOp In vars x1)) def inductionB {P : (InvolutiveFixesTerm → Type)} : ((P oneL) → ((∀ (x1 : InvolutiveFixesTerm) , ((P x1) → (P (primL x1)))) → (∀ (x : InvolutiveFixesTerm) , (P x)))) | p1l ppriml oneL := p1l | p1l ppriml (primL x1) := (ppriml _ (inductionB p1l ppriml x1)) def inductionCl {A : Type} {P : ((ClInvolutiveFixesTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((P oneCl) → ((∀ (x1 : (ClInvolutiveFixesTerm A)) , ((P x1) → (P (primCl x1)))) → (∀ (x : (ClInvolutiveFixesTerm A)) , (P x))))) | psing p1cl pprimcl (sing x1) := (psing x1) | psing p1cl pprimcl oneCl := p1cl | psing p1cl pprimcl (primCl x1) := (pprimcl _ (inductionCl psing p1cl pprimcl x1)) def inductionOpB {n : ℕ} {P : ((OpInvolutiveFixesTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((P oneOL) → ((∀ (x1 : (OpInvolutiveFixesTerm n)) , ((P x1) → (P (primOL x1)))) → (∀ (x : (OpInvolutiveFixesTerm n)) , (P x))))) | pv p1ol pprimol (v x1) := (pv x1) | pv p1ol pprimol oneOL := p1ol | pv p1ol pprimol (primOL x1) := (pprimol _ (inductionOpB pv p1ol pprimol x1)) def inductionOp {n : ℕ} {A : Type} {P : ((OpInvolutiveFixesTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((P oneOL2) → ((∀ (x1 : (OpInvolutiveFixesTerm2 n A)) , ((P x1) → (P (primOL2 x1)))) → (∀ (x : (OpInvolutiveFixesTerm2 n A)) , (P x)))))) | pv2 psing2 p1ol2 pprimol2 (v2 x1) := (pv2 x1) | pv2 psing2 p1ol2 pprimol2 (sing2 x1) := (psing2 x1) | pv2 psing2 p1ol2 pprimol2 oneOL2 := p1ol2 | pv2 psing2 p1ol2 pprimol2 (primOL2 x1) := (pprimol2 _ (inductionOp pv2 psing2 p1ol2 pprimol2 x1)) def stageB : (InvolutiveFixesTerm → (Staged InvolutiveFixesTerm)) | oneL := (Now oneL) | (primL x1) := (stage1 primL (codeLift1 primL) (stageB x1)) def stageCl {A : Type} : ((ClInvolutiveFixesTerm A) → (Staged (ClInvolutiveFixesTerm A))) | (sing x1) := (Now (sing x1)) | oneCl := (Now oneCl) | (primCl x1) := (stage1 primCl (codeLift1 primCl) (stageCl x1)) def stageOpB {n : ℕ} : ((OpInvolutiveFixesTerm n) → (Staged (OpInvolutiveFixesTerm n))) | (v x1) := (const (code (v x1))) | oneOL := (Now oneOL) | (primOL x1) := (stage1 primOL (codeLift1 primOL) (stageOpB x1)) def stageOp {n : ℕ} {A : Type} : ((OpInvolutiveFixesTerm2 n A) → (Staged (OpInvolutiveFixesTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | oneOL2 := (Now oneOL2) | (primOL2 x1) := (stage1 primOL2 (codeLift1 primOL2) (stageOp x1)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (oneT : (Repr A)) (primT : ((Repr A) → (Repr A))) end InvolutiveFixes
a33ff318746e108ef9e9aeb4c6d4b4145f4d8e8d
5ee26964f602030578ef0159d46145dd2e357ba5
/src/Huber_pair.lean
722f98fee08fd6e6b057f8ef0cef0452c2a6c372
[ "Apache-2.0" ]
permissive
fpvandoorn/lean-perfectoid-spaces
569b4006fdfe491ca8b58dd817bb56138ada761f
06cec51438b168837fc6e9268945735037fd1db6
refs/heads/master
1,590,154,571,918
1,557,685,392,000
1,557,685,392,000
186,363,547
0
0
Apache-2.0
1,557,730,933,000
1,557,730,933,000
null
UTF-8
Lean
false
false
1,348
lean
import power_bounded Huber_ring.basic data.polynomial universes u v --notation local postfix `ᵒ` : 66 := power_bounded_subring open power_bounded section integral variables {R : Type u} [comm_ring R] [decidable_eq R] instance subtype.val.is_ring_hom (S : set R) [is_subring S] : is_ring_hom (@subtype.val _ S) := by apply_instance def is_integral (S : set R) [is_subring S] (r : R) : Prop := ∃ f : polynomial ↥S, (f.monic) ∧ f.eval₂ (@subtype.val _ S) r = 0 def is_integrally_closed (S : set R) [is_subring S] : Prop := ∀ r : R, (is_integral S r) → r ∈ S end integral -- Wedhorn Def 7.14 structure is_ring_of_integral_elements {R : Type u} [Huber_ring R] [decidable_eq R] (Rplus : set R) : Prop := [is_subring : is_subring Rplus] (is_open : is_open Rplus) (is_int_closed : is_integrally_closed Rplus) (is_power_bounded : Rplus ⊆ Rᵒ) -- a Huber Ring is an f-adic ring. -- a Huber Pair is what Huber called an Affinoid Ring. structure Huber_pair := (R : Type) -- change this to (Type u) to enable universes [RHuber : Huber_ring R] [dec : decidable_eq R] (Rplus : set R) [intel : is_ring_of_integral_elements Rplus] instance : has_coe_to_sort Huber_pair := { S := Type, coe := Huber_pair.R } instance Huber_pair.Huber_ring (A : Huber_pair) : Huber_ring A := A.RHuber postfix `⁺` : 66 := λ A : Huber_pair, A.Rplus
d4c0bbe5d948ebcda46a98e913956079c95b103a
08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4
/src/Lean/Meta/AppBuilder.lean
ee59df2fcea22244314b3fe5e44a6813d0ade2a0
[ "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
23,673
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.Structure import Lean.Util.Recognizers import Lean.Meta.SynthInstance import Lean.Meta.Check import Lean.Meta.DecLevel namespace Lean.Meta /-- Return `id e` -/ def mkId (e : Expr) : MetaM Expr := do let type ← inferType e let u ← getLevel type return mkApp2 (mkConst ``id [u]) type e /-- Given `e` s.t. `inferType e` is definitionally equal to `expectedType`, return term `@id expectedType e`. -/ def mkExpectedTypeHint (e : Expr) (expectedType : Expr) : MetaM Expr := do let u ← getLevel expectedType return mkApp2 (mkConst ``id [u]) expectedType e /-- Return `a = b`. -/ def mkEq (a b : Expr) : MetaM Expr := do let aType ← inferType a let u ← getLevel aType return mkApp3 (mkConst ``Eq [u]) aType a b /-- Return `HEq a b`. -/ def mkHEq (a b : Expr) : MetaM Expr := do let aType ← inferType a let bType ← inferType b let u ← getLevel aType return mkApp4 (mkConst ``HEq [u]) aType a bType b /-- If `a` and `b` have definitionally equal types, return `Eq a b`, otherwise return `HEq a b`. -/ def mkEqHEq (a b : Expr) : MetaM Expr := do let aType ← inferType a let bType ← inferType b let u ← getLevel aType if (← isDefEq aType bType) then return mkApp3 (mkConst ``Eq [u]) aType a b else return mkApp4 (mkConst ``HEq [u]) aType a bType b /-- Return a proof of `a = a`. -/ def mkEqRefl (a : Expr) : MetaM Expr := do let aType ← inferType a let u ← getLevel aType return mkApp2 (mkConst ``Eq.refl [u]) aType a /-- Return a proof of `HEq a a`. -/ def mkHEqRefl (a : Expr) : MetaM Expr := do let aType ← inferType a let u ← getLevel aType return mkApp2 (mkConst ``HEq.refl [u]) aType a /-- Given `hp : P` and `nhp : Not P` returns an instance of type `e`. -/ def mkAbsurd (e : Expr) (hp hnp : Expr) : MetaM Expr := do let p ← inferType hp let u ← getLevel e return mkApp4 (mkConst ``absurd [u]) p e hp hnp /-- Given `h : False`, return an instance of type `e`. -/ def mkFalseElim (e : Expr) (h : Expr) : MetaM Expr := do let u ← getLevel e return mkApp2 (mkConst ``False.elim [u]) e h private def infer (h : Expr) : MetaM Expr := do let hType ← inferType h whnfD hType private def hasTypeMsg (e type : Expr) : MessageData := m!"{indentExpr e}\nhas type{indentExpr type}" private def throwAppBuilderException {α} (op : Name) (msg : MessageData) : MetaM α := throwError "AppBuilder for '{op}', {msg}" /-- Given `h : a = b`, returns a proof of `b = a`. -/ def mkEqSymm (h : Expr) : MetaM Expr := do if h.isAppOf ``Eq.refl then return h else let hType ← infer h match hType.eq? with | some (α, a, b) => let u ← getLevel α return mkApp4 (mkConst ``Eq.symm [u]) α a b h | none => throwAppBuilderException ``Eq.symm ("equality proof expected" ++ hasTypeMsg h hType) /-- Given `h₁ : a = b` and `h₂ : b = c` returns a proof of `a = c`. -/ def mkEqTrans (h₁ h₂ : Expr) : MetaM Expr := do if h₁.isAppOf ``Eq.refl then return h₂ else if h₂.isAppOf ``Eq.refl then return h₁ else let hType₁ ← infer h₁ let hType₂ ← infer h₂ match hType₁.eq?, hType₂.eq? with | some (α, a, b), some (_, _, c) => let u ← getLevel α return mkApp6 (mkConst ``Eq.trans [u]) α a b c h₁ h₂ | none, _ => throwAppBuilderException ``Eq.trans ("equality proof expected" ++ hasTypeMsg h₁ hType₁) | _, none => throwAppBuilderException ``Eq.trans ("equality proof expected" ++ hasTypeMsg h₂ hType₂) /-- Given `h : HEq a b`, returns a proof of `HEq b a`. -/ def mkHEqSymm (h : Expr) : MetaM Expr := do if h.isAppOf ``HEq.refl then return h else let hType ← infer h match hType.heq? with | some (α, a, β, b) => let u ← getLevel α return mkApp5 (mkConst ``HEq.symm [u]) α β a b h | none => throwAppBuilderException ``HEq.symm ("heterogeneous equality proof expected" ++ hasTypeMsg h hType) /-- Given `h₁ : HEq a b`, `h₂ : HEq b c`, returns a proof of `HEq a c`. -/ def mkHEqTrans (h₁ h₂ : Expr) : MetaM Expr := do if h₁.isAppOf ``HEq.refl then return h₂ else if h₂.isAppOf ``HEq.refl then return h₁ else let hType₁ ← infer h₁ let hType₂ ← infer h₂ match hType₁.heq?, hType₂.heq? with | some (α, a, β, b), some (_, _, γ, c) => let u ← getLevel α return mkApp8 (mkConst ``HEq.trans [u]) α β γ a b c h₁ h₂ | none, _ => throwAppBuilderException ``HEq.trans ("heterogeneous equality proof expected" ++ hasTypeMsg h₁ hType₁) | _, none => throwAppBuilderException ``HEq.trans ("heterogeneous equality proof expected" ++ hasTypeMsg h₂ hType₂) /-- Given `h : Eq a b`, returns a proof of `HEq a b`. -/ def mkEqOfHEq (h : Expr) : MetaM Expr := do let hType ← infer h match hType.heq? with | some (α, a, β, b) => unless (← isDefEq α β) do throwAppBuilderException ``eq_of_heq m!"heterogeneous equality types are not definitionally equal{indentExpr α}\nis not definitionally equal to{indentExpr β}" let u ← getLevel α return mkApp4 (mkConst ``eq_of_heq [u]) α a b h | _ => throwAppBuilderException ``HEq.trans m!"heterogeneous equality proof expected{indentExpr h}" /-- Given `f : α → β` and `h : a = b`, returns a proof of `f a = f b`.-/ def mkCongrArg (f h : Expr) : MetaM Expr := do if h.isAppOf ``Eq.refl then mkEqRefl (mkApp f h.appArg!) else let hType ← infer h let fType ← infer f match fType.arrow?, hType.eq? with | some (α, β), some (_, a, b) => let u ← getLevel α let v ← getLevel β return mkApp6 (mkConst ``congrArg [u, v]) α β a b f h | none, _ => throwAppBuilderException ``congrArg ("non-dependent function expected" ++ hasTypeMsg f fType) | _, none => throwAppBuilderException ``congrArg ("equality proof expected" ++ hasTypeMsg h hType) /-- Given `h : f = g` and `a : α`, returns a proof of `f a = g a`.-/ def mkCongrFun (h a : Expr) : MetaM Expr := do if h.isAppOf ``Eq.refl then mkEqRefl (mkApp h.appArg! a) else let hType ← infer h match hType.eq? with | some (ρ, f, g) => do let ρ ← whnfD ρ match ρ with | Expr.forallE n α β _ => let β' := Lean.mkLambda n BinderInfo.default α β let u ← getLevel α let v ← getLevel (mkApp β' a) return mkApp6 (mkConst ``congrFun [u, v]) α β' f g h a | _ => throwAppBuilderException ``congrFun ("equality proof between functions expected" ++ hasTypeMsg h hType) | _ => throwAppBuilderException ``congrFun ("equality proof expected" ++ hasTypeMsg h hType) /-- Given `h₁ : f = g` and `h₂ : a = b`, returns a proof of `f a = g b`. -/ def mkCongr (h₁ h₂ : Expr) : MetaM Expr := do if h₁.isAppOf ``Eq.refl then mkCongrArg h₁.appArg! h₂ else if h₂.isAppOf ``Eq.refl then mkCongrFun h₁ h₂.appArg! else let hType₁ ← infer h₁ let hType₂ ← infer h₂ match hType₁.eq?, hType₂.eq? with | some (ρ, f, g), some (α, a, b) => let ρ ← whnfD ρ match ρ.arrow? with | some (_, β) => do let u ← getLevel α let v ← getLevel β return mkApp8 (mkConst ``congr [u, v]) α β f g a b h₁ h₂ | _ => throwAppBuilderException ``congr ("non-dependent function expected" ++ hasTypeMsg h₁ hType₁) | none, _ => throwAppBuilderException ``congr ("equality proof expected" ++ hasTypeMsg h₁ hType₁) | _, none => throwAppBuilderException ``congr ("equality proof expected" ++ hasTypeMsg h₂ hType₂) private def mkAppMFinal (methodName : Name) (f : Expr) (args : Array Expr) (instMVars : Array MVarId) : MetaM Expr := do instMVars.forM fun mvarId => do let mvarDecl ← mvarId.getDecl let mvarVal ← synthInstance mvarDecl.type mvarId.assign mvarVal let result ← instantiateMVars (mkAppN f args) if (← hasAssignableMVar result) then throwAppBuilderException methodName ("result contains metavariables" ++ indentExpr result) return result private partial def mkAppMArgs (f : Expr) (fType : Expr) (xs : Array Expr) : MetaM Expr := let rec loop (type : Expr) (i : Nat) (j : Nat) (args : Array Expr) (instMVars : Array MVarId) : MetaM Expr := do if i >= xs.size then mkAppMFinal `mkAppM f args instMVars else match type with | Expr.forallE n d b bi => let d := d.instantiateRevRange j args.size args match bi with | BinderInfo.implicit => let mvar ← mkFreshExprMVar d MetavarKind.natural n loop b i j (args.push mvar) instMVars | BinderInfo.strictImplicit => let mvar ← mkFreshExprMVar d MetavarKind.natural n loop b i j (args.push mvar) instMVars | BinderInfo.instImplicit => let mvar ← mkFreshExprMVar d MetavarKind.synthetic n loop b i j (args.push mvar) (instMVars.push mvar.mvarId!) | _ => let x := xs[i]! let xType ← inferType x if (← isDefEq d xType) then loop b (i+1) j (args.push x) instMVars else throwAppTypeMismatch (mkAppN f args) x | type => let type := type.instantiateRevRange j args.size args let type ← whnfD type if type.isForall then loop type i args.size args instMVars else throwAppBuilderException `mkAppM m!"too many explicit arguments provided to{indentExpr f}\narguments{indentD xs}" loop fType 0 0 #[] #[] private def mkFun (constName : Name) : MetaM (Expr × Expr) := do let cinfo ← getConstInfo constName let us ← cinfo.levelParams.mapM fun _ => mkFreshLevelMVar let f := mkConst constName us let fType ← instantiateTypeLevelParams cinfo us return (f, fType) private def withAppBuilderTrace [ToMessageData α] [ToMessageData β] (f : α) (xs : β) (k : MetaM Expr) : MetaM Expr := let emoji | .ok .. => checkEmoji | .error .. => crossEmoji withTraceNode `Meta.appBuilder (return m!"{emoji ·} f: {f}, xs: {xs}") do try let res ← k trace[Meta.appBuilder.result] res pure res catch ex => trace[Meta.appBuilder.error] ex.toMessageData throw ex /-- Return the application `constName xs`. It tries to fill the implicit arguments before the last element in `xs`. Remark: ``mkAppM `arbitrary #[α]`` returns `@arbitrary.{u} α` without synthesizing the implicit argument occurring after `α`. Given a `x : (([Decidable p] → Bool) × Nat`, ``mkAppM `Prod.fst #[x]`` returns `@Prod.fst ([Decidable p] → Bool) Nat x` -/ def mkAppM (constName : Name) (xs : Array Expr) : MetaM Expr := do withAppBuilderTrace constName xs do withNewMCtxDepth do let (f, fType) ← mkFun constName mkAppMArgs f fType xs /-- Similar to `mkAppM`, but takes an `Expr` instead of a constant name. -/ def mkAppM' (f : Expr) (xs : Array Expr) : MetaM Expr := do let fType ← inferType f withAppBuilderTrace f xs do withNewMCtxDepth do mkAppMArgs f fType xs private partial def mkAppOptMAux (f : Expr) (xs : Array (Option Expr)) : Nat → Array Expr → Nat → Array MVarId → Expr → MetaM Expr | i, args, j, instMVars, Expr.forallE n d b bi => do let d := d.instantiateRevRange j args.size args if h : i < xs.size then match xs.get ⟨i, h⟩ with | none => match bi with | BinderInfo.instImplicit => do let mvar ← mkFreshExprMVar d MetavarKind.synthetic n mkAppOptMAux f xs (i+1) (args.push mvar) j (instMVars.push mvar.mvarId!) b | _ => do let mvar ← mkFreshExprMVar d MetavarKind.natural n mkAppOptMAux f xs (i+1) (args.push mvar) j instMVars b | some x => let xType ← inferType x if (← isDefEq d xType) then mkAppOptMAux f xs (i+1) (args.push x) j instMVars b else throwAppTypeMismatch (mkAppN f args) x else mkAppMFinal `mkAppOptM f args instMVars | i, args, j, instMVars, type => do let type := type.instantiateRevRange j args.size args let type ← whnfD type if type.isForall then mkAppOptMAux f xs i args args.size instMVars type else if i == xs.size then mkAppMFinal `mkAppOptM f args instMVars else do let xs : Array Expr := xs.foldl (fun r x? => match x? with | none => r | some x => r.push x) #[] throwAppBuilderException `mkAppOptM ("too many arguments provided to" ++ indentExpr f ++ Format.line ++ "arguments" ++ xs) /-- Similar to `mkAppM`, but it allows us to specify which arguments are provided explicitly using `Option` type. Example: Given `Pure.pure {m : Type u → Type v} [Pure m] {α : Type u} (a : α) : m α`, ``` mkAppOptM `Pure.pure #[m, none, none, a] ``` returns a `Pure.pure` application if the instance `Pure m` can be synthesized, and the universe match. Note that, ``` mkAppM `Pure.pure #[a] ``` fails because the only explicit argument `(a : α)` is not sufficient for inferring the remaining arguments, we would need the expected type. -/ def mkAppOptM (constName : Name) (xs : Array (Option Expr)) : MetaM Expr := do withAppBuilderTrace constName xs do withNewMCtxDepth do let (f, fType) ← mkFun constName mkAppOptMAux f xs 0 #[] 0 #[] fType /-- Similar to `mkAppOptM`, but takes an `Expr` instead of a constant name -/ def mkAppOptM' (f : Expr) (xs : Array (Option Expr)) : MetaM Expr := do let fType ← inferType f withAppBuilderTrace f xs do withNewMCtxDepth do mkAppOptMAux f xs 0 #[] 0 #[] fType def mkEqNDRec (motive h1 h2 : Expr) : MetaM Expr := do if h2.isAppOf ``Eq.refl then return h1 else let h2Type ← infer h2 match h2Type.eq? with | none => throwAppBuilderException ``Eq.ndrec ("equality proof expected" ++ hasTypeMsg h2 h2Type) | some (α, a, b) => let u2 ← getLevel α let motiveType ← infer motive match motiveType with | Expr.forallE _ _ (Expr.sort u1) _ => return mkAppN (mkConst ``Eq.ndrec [u1, u2]) #[α, a, motive, h1, b, h2] | _ => throwAppBuilderException ``Eq.ndrec ("invalid motive" ++ indentExpr motive) def mkEqRec (motive h1 h2 : Expr) : MetaM Expr := do if h2.isAppOf ``Eq.refl then return h1 else let h2Type ← infer h2 match h2Type.eq? with | none => throwAppBuilderException ``Eq.rec ("equality proof expected" ++ indentExpr h2) | some (α, a, b) => let u2 ← getLevel α let motiveType ← infer motive match motiveType with | Expr.forallE _ _ (Expr.forallE _ _ (Expr.sort u1) _) _ => return mkAppN (mkConst ``Eq.rec [u1, u2]) #[α, a, motive, h1, b, h2] | _ => throwAppBuilderException ``Eq.rec ("invalid motive" ++ indentExpr motive) def mkEqMP (eqProof pr : Expr) : MetaM Expr := mkAppM ``Eq.mp #[eqProof, pr] def mkEqMPR (eqProof pr : Expr) : MetaM Expr := mkAppM ``Eq.mpr #[eqProof, pr] def mkNoConfusion (target : Expr) (h : Expr) : MetaM Expr := do let type ← inferType h let type ← whnf type match type.eq? with | none => throwAppBuilderException `noConfusion ("equality expected" ++ hasTypeMsg h type) | some (α, a, b) => let α ← whnfD α matchConstInduct α.getAppFn (fun _ => throwAppBuilderException `noConfusion ("inductive type expected" ++ indentExpr α)) fun v us => do let u ← getLevel target return mkAppN (mkConst (Name.mkStr v.name "noConfusion") (u :: us)) (α.getAppArgs ++ #[target, a, b, h]) /-- Given a `monad` and `e : α`, makes `pure e`.-/ def mkPure (monad : Expr) (e : Expr) : MetaM Expr := mkAppOptM ``Pure.pure #[monad, none, none, e] /-- `mkProjection s fieldName` return an expression for accessing field `fieldName` of the structure `s`. Remark: `fieldName` may be a subfield of `s`. -/ partial def mkProjection (s : Expr) (fieldName : Name) : MetaM Expr := do let type ← inferType s let type ← whnf type match type.getAppFn with | Expr.const structName us => let env ← getEnv unless isStructure env structName do throwAppBuilderException `mkProjection ("structure expected" ++ hasTypeMsg s type) match getProjFnForField? env structName fieldName with | some projFn => let params := type.getAppArgs return mkApp (mkAppN (mkConst projFn us) params) s | none => let fields := getStructureFields env structName let r? ← fields.findSomeM? fun fieldName' => do match isSubobjectField? env structName fieldName' with | none => pure none | some _ => let parent ← mkProjection s fieldName' (do let r ← mkProjection parent fieldName; return some r) <|> pure none match r? with | some r => pure r | none => throwAppBuilderException `mkProjectionn ("invalid field name '" ++ toString fieldName ++ "' for" ++ hasTypeMsg s type) | _ => throwAppBuilderException `mkProjectionn ("structure expected" ++ hasTypeMsg s type) private def mkListLitAux (nil : Expr) (cons : Expr) : List Expr → Expr | [] => nil | x::xs => mkApp (mkApp cons x) (mkListLitAux nil cons xs) def mkListLit (type : Expr) (xs : List Expr) : MetaM Expr := do let u ← getDecLevel type let nil := mkApp (mkConst ``List.nil [u]) type match xs with | [] => return nil | _ => let cons := mkApp (mkConst ``List.cons [u]) type return mkListLitAux nil cons xs def mkArrayLit (type : Expr) (xs : List Expr) : MetaM Expr := do let u ← getDecLevel type let listLit ← mkListLit type xs return mkApp (mkApp (mkConst ``List.toArray [u]) type) listLit def mkSorry (type : Expr) (synthetic : Bool) : MetaM Expr := do let u ← getLevel type return mkApp2 (mkConst ``sorryAx [u]) type (toExpr synthetic) /-- Return `Decidable.decide p` -/ def mkDecide (p : Expr) : MetaM Expr := mkAppOptM ``Decidable.decide #[p, none] /-- Return a proof for `p : Prop` using `decide p` -/ def mkDecideProof (p : Expr) : MetaM Expr := do let decP ← mkDecide p let decEqTrue ← mkEq decP (mkConst ``Bool.true) let h ← mkEqRefl (mkConst ``Bool.true) let h ← mkExpectedTypeHint h decEqTrue mkAppM ``of_decide_eq_true #[h] /-- Return `a < b` -/ def mkLt (a b : Expr) : MetaM Expr := mkAppM ``LT.lt #[a, b] /-- Return `a <= b` -/ def mkLe (a b : Expr) : MetaM Expr := mkAppM ``LE.le #[a, b] /-- Return `Inhabited.default α` -/ def mkDefault (α : Expr) : MetaM Expr := mkAppOptM ``Inhabited.default #[α, none] /-- Return `@Classical.ofNonempty α _` -/ def mkOfNonempty (α : Expr) : MetaM Expr := do mkAppOptM ``Classical.ofNonempty #[α, none] /-- Return `sorryAx type` -/ def mkSyntheticSorry (type : Expr) : MetaM Expr := return mkApp2 (mkConst ``sorryAx [← getLevel type]) type (mkConst ``Bool.true) /-- Return `funext h` -/ def mkFunExt (h : Expr) : MetaM Expr := mkAppM ``funext #[h] /-- Return `propext h` -/ def mkPropExt (h : Expr) : MetaM Expr := mkAppM ``propext #[h] /-- Return `let_congr h₁ h₂` -/ def mkLetCongr (h₁ h₂ : Expr) : MetaM Expr := mkAppM ``let_congr #[h₁, h₂] /-- Return `let_val_congr b h` -/ def mkLetValCongr (b h : Expr) : MetaM Expr := mkAppM ``let_val_congr #[b, h] /-- Return `let_body_congr a h` -/ def mkLetBodyCongr (a h : Expr) : MetaM Expr := mkAppM ``let_body_congr #[a, h] /-- Return `of_eq_true h` -/ def mkOfEqTrue (h : Expr) : MetaM Expr := mkAppM ``of_eq_true #[h] /-- Return `eq_true h` -/ def mkEqTrue (h : Expr) : MetaM Expr := mkAppM ``eq_true #[h] /-- Return `eq_false h` `h` must have type definitionally equal to `¬ p` in the current reducibility setting. -/ def mkEqFalse (h : Expr) : MetaM Expr := mkAppM ``eq_false #[h] /-- Return `eq_false' h` `h` must have type definitionally equal to `p → False` in the current reducibility setting. -/ def mkEqFalse' (h : Expr) : MetaM Expr := mkAppM ``eq_false' #[h] def mkImpCongr (h₁ h₂ : Expr) : MetaM Expr := mkAppM ``implies_congr #[h₁, h₂] def mkImpCongrCtx (h₁ h₂ : Expr) : MetaM Expr := mkAppM ``implies_congr_ctx #[h₁, h₂] def mkImpDepCongrCtx (h₁ h₂ : Expr) : MetaM Expr := mkAppM ``implies_dep_congr_ctx #[h₁, h₂] def mkForallCongr (h : Expr) : MetaM Expr := mkAppM ``forall_congr #[h] /-- Return instance for `[Monad m]` if there is one -/ def isMonad? (m : Expr) : MetaM (Option Expr) := try let monadType ← mkAppM `Monad #[m] let result ← trySynthInstance monadType match result with | LOption.some inst => pure inst | _ => pure none catch _ => pure none /-- Return `(n : type)`, a numeric literal of type `type`. The method fails if we don't have an instance `OfNat type n` -/ def mkNumeral (type : Expr) (n : Nat) : MetaM Expr := do let u ← getDecLevel type let inst ← synthInstance (mkApp2 (mkConst ``OfNat [u]) type (mkRawNatLit n)) return mkApp3 (mkConst ``OfNat.ofNat [u]) type (mkRawNatLit n) inst /-- Return `a op b`, where `op` has name `opName` and is implemented using the typeclass `className`. This method assumes `a` and `b` have the same type, and typeclass `className` is heterogeneous. Examples of supported clases: `HAdd`, `HSub`, `HMul`. We use heterogeneous operators to ensure we have a uniform representation. -/ private def mkBinaryOp (className : Name) (opName : Name) (a b : Expr) : MetaM Expr := do let aType ← inferType a let u ← getDecLevel aType let inst ← synthInstance (mkApp3 (mkConst className [u, u, u]) aType aType aType) return mkApp6 (mkConst opName [u, u, u]) aType aType aType inst a b /-- Return `a + b` using a heterogeneous `+`. This method assumes `a` and `b` have the same type. -/ def mkAdd (a b : Expr) : MetaM Expr := mkBinaryOp ``HAdd ``HAdd.hAdd a b /-- Return `a - b` using a heterogeneous `-`. This method assumes `a` and `b` have the same type. -/ def mkSub (a b : Expr) : MetaM Expr := mkBinaryOp ``HSub ``HSub.hSub a b /-- Return `a * b` using a heterogeneous `*`. This method assumes `a` and `b` have the same type. -/ def mkMul (a b : Expr) : MetaM Expr := mkBinaryOp ``HMul ``HMul.hMul a b /-- Return `a r b`, where `r` has name `rName` and is implemented using the typeclass `className`. This method assumes `a` and `b` have the same type. Examples of supported clases: `LE` and `LT`. We use heterogeneous operators to ensure we have a uniform representation. -/ private def mkBinaryRel (className : Name) (rName : Name) (a b : Expr) : MetaM Expr := do let aType ← inferType a let u ← getDecLevel aType let inst ← synthInstance (mkApp (mkConst className [u]) aType) return mkApp4 (mkConst rName [u]) aType inst a b /-- Return `a ≤ b`. This method assumes `a` and `b` have the same type. -/ def mkLE (a b : Expr) : MetaM Expr := mkBinaryRel ``LE ``LE.le a b /-- Return `a < b`. This method assumes `a` and `b` have the same type. -/ def mkLT (a b : Expr) : MetaM Expr := mkBinaryRel ``LT ``LT.lt a b /-- Given `h : a = b`, return a proof for `a ↔ b`. -/ def mkIffOfEq (h : Expr) : MetaM Expr := do if h.isAppOfArity ``propext 3 then return h.appArg! else mkAppM ``Iff.of_eq #[h] builtin_initialize do registerTraceClass `Meta.appBuilder registerTraceClass `Meta.appBuilder.result (inherited := true) registerTraceClass `Meta.appBuilder.error (inherited := true) end Lean.Meta
9bd7d799da1ab6572d59c9a4f581df3a78ee602f
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/data/list/zip.lean
40f8860c19f43374fc0b75e4f97b7c443d1faedc
[ "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
5,465
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau -/ import data.list.basic universes u v w z variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type z} open nat namespace list /- zip & unzip -/ @[simp] theorem zip_cons_cons (a : α) (b : β) (l₁ : list α) (l₂ : list β) : zip (a :: l₁) (b :: l₂) = (a, b) :: zip l₁ l₂ := rfl @[simp] theorem zip_nil_left (l : list α) : zip ([] : list β) l = [] := rfl @[simp] theorem zip_nil_right (l : list α) : zip l ([] : list β) = [] := by cases l; refl @[simp] theorem zip_swap : ∀ (l₁ : list α) (l₂ : list β), (zip l₁ l₂).map prod.swap = zip l₂ l₁ | [] l₂ := (zip_nil_right _).symm | l₁ [] := by rw zip_nil_right; refl | (a::l₁) (b::l₂) := by simp only [zip_cons_cons, map_cons, zip_swap l₁ l₂, prod.swap_prod_mk]; split; refl @[simp] theorem length_zip : ∀ (l₁ : list α) (l₂ : list β), length (zip l₁ l₂) = min (length l₁) (length l₂) | [] l₂ := rfl | l₁ [] := by simp only [length, zip_nil_right, min_zero] | (a::l₁) (b::l₂) := by by simp only [length, zip_cons_cons, length_zip l₁ l₂, min_add_add_right] theorem zip_append : ∀ {l₁ l₂ r₁ r₂ : list α} (h : length l₁ = length l₂), zip (l₁ ++ r₁) (l₂ ++ r₂) = zip l₁ l₂ ++ zip r₁ r₂ | [] l₂ r₁ r₂ h := by simp only [eq_nil_of_length_eq_zero h.symm]; refl | l₁ [] r₁ r₂ h := by simp only [eq_nil_of_length_eq_zero h]; refl | (a::l₁) (b::l₂) r₁ r₂ h := by simp only [cons_append, zip_cons_cons, zip_append (succ.inj h)]; split; refl theorem zip_map (f : α → γ) (g : β → δ) : ∀ (l₁ : list α) (l₂ : list β), zip (l₁.map f) (l₂.map g) = (zip l₁ l₂).map (prod.map f g) | [] l₂ := rfl | l₁ [] := by simp only [map, zip_nil_right] | (a::l₁) (b::l₂) := by simp only [map, zip_cons_cons, zip_map l₁ l₂, prod.map]; split; refl theorem zip_map_left (f : α → γ) (l₁ : list α) (l₂ : list β) : zip (l₁.map f) l₂ = (zip l₁ l₂).map (prod.map f id) := by rw [← zip_map, map_id] theorem zip_map_right (f : β → γ) (l₁ : list α) (l₂ : list β) : zip l₁ (l₂.map f) = (zip l₁ l₂).map (prod.map id f) := by rw [← zip_map, map_id] theorem zip_map' (f : α → β) (g : α → γ) : ∀ (l : list α), zip (l.map f) (l.map g) = l.map (λ a, (f a, g a)) | [] := rfl | (a::l) := by simp only [map, zip_cons_cons, zip_map' l]; split; refl theorem mem_zip {a b} : ∀ {l₁ : list α} {l₂ : list β}, (a, b) ∈ zip l₁ l₂ → a ∈ l₁ ∧ b ∈ l₂ | (_::l₁) (_::l₂) (or.inl rfl) := ⟨or.inl rfl, or.inl rfl⟩ | (a'::l₁) (b'::l₂) (or.inr h) := by split; simp only [mem_cons_iff, or_true, mem_zip h] @[simp] theorem unzip_nil : unzip (@nil (α × β)) = ([], []) := rfl @[simp] theorem unzip_cons (a : α) (b : β) (l : list (α × β)) : unzip ((a, b) :: l) = (a :: (unzip l).1, b :: (unzip l).2) := by rw unzip; cases unzip l; refl theorem unzip_eq_map : ∀ (l : list (α × β)), unzip l = (l.map prod.fst, l.map prod.snd) | [] := rfl | ((a, b) :: l) := by simp only [unzip_cons, map_cons, unzip_eq_map l] theorem unzip_left (l : list (α × β)) : (unzip l).1 = l.map prod.fst := by simp only [unzip_eq_map] theorem unzip_right (l : list (α × β)) : (unzip l).2 = l.map prod.snd := by simp only [unzip_eq_map] theorem unzip_swap (l : list (α × β)) : unzip (l.map prod.swap) = (unzip l).swap := by simp only [unzip_eq_map, map_map]; split; refl theorem zip_unzip : ∀ (l : list (α × β)), zip (unzip l).1 (unzip l).2 = l | [] := rfl | ((a, b) :: l) := by simp only [unzip_cons, zip_cons_cons, zip_unzip l]; split; refl theorem unzip_zip_left : ∀ {l₁ : list α} {l₂ : list β}, length l₁ ≤ length l₂ → (unzip (zip l₁ l₂)).1 = l₁ | [] l₂ h := rfl | l₁ [] h := by rw eq_nil_of_length_eq_zero (eq_zero_of_le_zero h); refl | (a::l₁) (b::l₂) h := by simp only [zip_cons_cons, unzip_cons, unzip_zip_left (le_of_succ_le_succ h)]; split; refl theorem unzip_zip_right {l₁ : list α} {l₂ : list β} (h : length l₂ ≤ length l₁) : (unzip (zip l₁ l₂)).2 = l₂ := by rw [← zip_swap, unzip_swap]; exact unzip_zip_left h theorem unzip_zip {l₁ : list α} {l₂ : list β} (h : length l₁ = length l₂) : unzip (zip l₁ l₂) = (l₁, l₂) := by rw [← @prod.mk.eta _ _ (unzip (zip l₁ l₂)), unzip_zip_left (le_of_eq h), unzip_zip_right (ge_of_eq h)] @[simp] theorem length_revzip (l : list α) : length (revzip l) = length l := by simp only [revzip, length_zip, length_reverse, min_self] @[simp] theorem unzip_revzip (l : list α) : (revzip l).unzip = (l, l.reverse) := unzip_zip (length_reverse l).symm @[simp] theorem revzip_map_fst (l : list α) : (revzip l).map prod.fst = l := by rw [← unzip_left, unzip_revzip] @[simp] theorem revzip_map_snd (l : list α) : (revzip l).map prod.snd = l.reverse := by rw [← unzip_right, unzip_revzip] theorem reverse_revzip (l : list α) : reverse l.revzip = revzip l.reverse := by rw [← zip_unzip.{u u} (revzip l).reverse, unzip_eq_map]; simp; simp [revzip] theorem revzip_swap (l : list α) : (revzip l).map prod.swap = revzip l.reverse := by simp [revzip] end list
7fd5969c3b721875b33f9e91bcbf642f65244e50
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/13_More_Tactics.org.13.lean
a16414a7163cb7ed3b48f8525b125d8abafb6f96
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
434
lean
import standard import data.set open set function eq.ops variables {X Y Z : Type} lemma image_compose (f : Y → X) (g : X → Y) (a : set X) : (f ∘ g) '[a] = f '[g '[a]] := set.ext (take z, iff.intro (assume Hz, obtain x Hx₁ Hx₂, from Hz, by repeat (apply mem_image | assumption | reflexivity)) (assume Hz, obtain y [x Hz₁ Hz₂] Hy₂, from Hz, by repeat (apply mem_image | assumption | esimp [compose] | rewrite Hz₂)))
5b9e1395236ae86463a401b5e37f62ca7c2d789b
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/set_theory/schroeder_bernstein.lean
573b3041c96c02396be46be9dc0d7afc3c744fa9
[ "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,041
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 The Schröder-Bernstein theorem, and well ordering of cardinals. -/ import order.fixed_points data.set.lattice logic.function logic.embedding order.zorn open set classical open_locale classical universes u v namespace function namespace embedding section antisymm variables {α : Type u} {β : Type v} theorem schroeder_bernstein {f : α → β} {g : β → α} (hf : injective f) (hg : injective g) : ∃h:α→β, bijective h := let s : set α := lfp $ λs, - (g '' - (f '' s)) in have hs : s = - (g '' - (f '' s)), from lfp_eq $ assume s t h, compl_subset_compl.mpr $ image_subset _ $ compl_subset_compl.mpr $ image_subset _ h, have hns : - s = g '' - (f '' s), from compl_inj $ by simp [hs.symm], let g' := λa, @inv_fun β ⟨f a⟩ α g a in have g'g : g' ∘ g = id, from funext $ assume b, @left_inverse_inv_fun _ ⟨f (g b)⟩ _ _ hg b, have hg'ns : g' '' (-s) = - (f '' s), by rw [hns, ←image_comp, g'g, image_id], let h := λa, if a ∈ s then f a else g' a in have h '' univ = univ, from calc h '' univ = h '' s ∪ h '' (- s) : by rw [←image_union, union_compl_self] ... = f '' s ∪ g' '' (-s) : congr (congr_arg (∪) (image_congr $ by simp [h, if_pos] {contextual := tt})) (image_congr $ by simp [h, if_neg] {contextual := tt}) ... = univ : by rw [hg'ns, union_compl_self], have surjective h, from assume b, have b ∈ h '' univ, by rw [this]; trivial, let ⟨a, _, eq⟩ := this in ⟨a, eq⟩, have split : ∀x∈s, ∀y∉s, h x = h y → false, from assume x hx y hy eq, have y ∈ g '' - (f '' s), by rwa [←hns], let ⟨y', hy', eq_y'⟩ := this in have f x = y', from calc f x = g' y : by simp [h, hx, hy, if_pos, if_neg] at eq; assumption ... = (g' ∘ g) y' : by simp [(∘), eq_y'] ... = _ : by simp [g'g], have y' ∈ f '' s, from this ▸ mem_image_of_mem _ hx, hy' this, have injective h, from assume x y eq, by_cases (assume hx : x ∈ s, by_cases (assume hy : y ∈ s, by simp [h, hx, hy, if_pos, if_neg] at eq; exact hf eq) (assume hy : y ∉ s, (split x hx y hy eq).elim)) (assume hx : x ∉ s, by_cases (assume hy : y ∈ s, (split y hy x hx eq.symm).elim) (assume hy : y ∉ s, have x ∈ g '' - (f '' s), by rwa [←hns], let ⟨x', hx', eqx⟩ := this in have y ∈ g '' - (f '' s), by rwa [←hns], let ⟨y', hy', eqy⟩ := this in have g' x = g' y, by simp [h, hx, hy, if_pos, if_neg] at eq; assumption, have (g' ∘ g) x' = (g' ∘ g) y', by simp [(∘), eqx, eqy, this], have x' = y', by rwa [g'g] at this, calc x = g x' : eqx.symm ... = g y' : by rw [this] ... = y : eqy)), ⟨h, ‹injective h›, ‹surjective h›⟩ theorem antisymm : (α ↪ β) → (β ↪ α) → nonempty (α ≃ β) | ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ := let ⟨f, hf⟩ := schroeder_bernstein h₁ h₂ in ⟨equiv.of_bijective 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} theorem injective_min (I : nonempty ι) : ∃ i, nonempty (∀ j, β i ↪ β j) := let ⟨s, hs, ms⟩ := show ∃s∈sets, ∀a∈sets, s ⊆ a → a = s, from zorn.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, classical.not_forall] using h, let ⟨f, hf⟩ := 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⟩ := 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 theorem total {α : Type u} {β : Type v} : nonempty (α ↪ β) ∨ nonempty (β ↪ α) := match @injective_min 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
ff842bcc6f4b496e3a1612cedd3148498314470f
43390109ab88557e6090f3245c47479c123ee500
/src/Topology/Material/homotopy.lean
e052e7725dd1ea2041f52a10b10b64bb460e4b62
[ "Apache-2.0" ]
permissive
Ja1941/xena-UROP-2018
41f0956519f94d56b8bf6834a8d39473f4923200
b111fb87f343cf79eca3b886f99ee15c1dd9884b
refs/heads/master
1,662,355,955,139
1,590,577,325,000
1,590,577,325,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,891
lean
/- Copyright (c) 2018 Luca Gerolla. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luca Gerolla, Kevin Buzzard Definition of homotopy, properties and equivalence relation. -/ import analysis.topology.continuity import analysis.topology.topological_space import analysis.topology.infinite_sum import analysis.topology.topological_structures import analysis.topology.uniform_space import analysis.real import data.real.basic tactic.norm_num import data.set.basic import Topology.Material.pasting_lemma import Topology.Material.path import Topology.Material.real_results open set filter lattice classical namespace homotopy open path variables {α : Type*} [topological_space α ] variables {β : Type*} [topological_space β ] { x y z w : β } variables ( x0 : β ) variable s : I01 noncomputable theory local attribute [instance] classical.prop_decidable -- HOMOTOPY -- General Homotopy structure homotopy {f : α → β} {g : α → β} ( hcf : continuous f) ( hcg : continuous g) := (to_fun : I01 × α → β ) (at_zero : ( λ x, to_fun ( 0 , x) ) = f ) (at_one : ( λ x, to_fun ( 1 , x) ) = g) (cont : continuous to_fun ) structure path_homotopy ( f : path x y) ( g : path x y) := (to_fun : I01 × I01 → β ) (path_s : ∀ s : I01, is_path x y ( λ t, to_fun (s, t) ) ) (at_zero : ∀ y, to_fun (0,y) = f.to_fun y ) (at_one : ∀ y, to_fun (1,y) = g.to_fun y) (cont : continuous to_fun) -- Simp lemmas @[simp] lemma at_zero_path_hom { f : path x y } { g : path x y} (F : path_homotopy f g) (y : I01) : F.to_fun (0, y) = path.to_fun f y := F.3 y @[simp] lemma at_one_path_hom { f : path x y} { g : path x y} (F : path_homotopy f g) (y : I01): F.to_fun (1, y) = path.to_fun g y := F.4 y @[simp] lemma at_pt_zero_hom { f : path x y} { g : path x y} (F : path_homotopy f g) (s : I01) : F.to_fun (s, 0) = x := begin exact (F.2 s).1 end @[simp] lemma at_pt_one_hom { f : path x y} { g : path x y} (F : path_homotopy f g) (s : I01) : F.to_fun (s, 1) = y := begin exact (F.2 s).2.1 end variables { l k : path x y } variable F : path_homotopy l k -- Alternative path_homotopy.mk def path_homotopy.mk' { f : path x y} { g : path x y} (F : I01 × I01 → β) (start_pt : ∀ s : I01, F (s, 0) = x) (end_pt : ∀ s : I01, F (s, 1) = y) (at_zero : ∀ y, F (0,y) = f.to_fun y ) (at_one : ∀ y, F (1,y) = g.to_fun y ) (F_cont : continuous F) : path_homotopy f g := { to_fun := F, path_s := begin unfold is_path, intro s, split, exact start_pt s, split, exact end_pt s, refine continuous.comp _ F_cont, exact continuous.prod_mk continuous_const continuous_id, end, at_zero := at_zero, at_one := at_one, cont := F_cont } -- Ending points of path_homotopy are fixed (Can Remove - not Used) lemma hom_eq_of_pts { x y : β } { f g : path x y } ( F : path_homotopy f g ) : ∀ s : I01, check_pts x y ( λ t, F.to_fun (s, t)) := begin intro s, unfold check_pts, split, have h₁ : F.to_fun (s, 0) = ( λ t, F.to_fun (s, t)) 0, by simp, rw h₁ , exact (F.path_s s).left, have h₂ : F.to_fun (s, 1) = ( λ t, F.to_fun (s, t)) 1, by simp, rw h₂ , exact (F.path_s s).right.left end --- (Can Remove - not Used) lemma hom_path_is_cont { x y : β } { f g : path x y } ( F : path_homotopy f g ) : ∀ s : I01, continuous ( λ t, F.to_fun (s, t)) := begin intro s, exact (F.path_s s).right.right end def hom_to_path { f g : path x y } ( F : path_homotopy f g ) (s : I01) : path x y := to_path ( λ t, F.to_fun (s, t)) (F.path_s s) -------------------------------------------- -------------------------------------------- -- IDENTITY / INVERSE / COMPOSITION of HOMOTOPY --- Identity homotopy def path_homotopy_id { x y : β} (f : path x y) : path_homotopy f f := { to_fun := λ st , f.to_fun (prod.snd st) , path_s := begin intro s, unfold is_path, exact ⟨ f.at_zero, f.at_one, f.cont ⟩ end, at_zero := by simp , at_one := by simp , cont := begin let h := λ st, f.to_fun ( @prod.snd I01 I01 st ) , have hc : continuous h, exact continuous.comp continuous_snd f.cont, exact hc, end } --- Inverse homotopy lemma help_hom_inv : (λ (st : ↥I01 × ↥I01), F.to_fun (par_inv (st.fst), st.snd)) = ((λ (st : ↥I01 × ↥I01), F.to_fun (st.fst , st.snd)) ∘ (λ (x : I01 × I01) , (( par_inv x.1 , x.2 ) : I01 × I01))) := by trivial def path_homotopy_inverse { x y : β} {f : path x y} {g : path x y} ( F : path_homotopy f g) : path_homotopy g f := { to_fun := λ st , F.to_fun ( par_inv st.1 , st.2 ), path_s := begin intro s, unfold is_path, split, exact (F.path_s (par_inv s)).1, split, exact (F.path_s (par_inv s)).2.1, exact (F.path_s (par_inv s)).2.2 end, at_zero := begin intro t, simp [eqn_1_par_inv], end, at_one := begin intro t, simp, end, cont := begin show continuous ((λ (st : ↥I01 × ↥I01), F.to_fun (st.fst , st.snd)) ∘ (λ (x : I01 × I01) , (( par_inv x.1 , x.2 ) : I01 × I01))), have H : continuous (λ (x : I01 × I01) , (( par_inv x.1 , x.2 ) : I01 × I01)), { exact continuous.prod_mk ( continuous.comp continuous_fst continuous_par_inv) ( @continuous.comp (I01×I01) I01 I01 _ _ _ (λ x : I01×I01, x.2) _ continuous_snd continuous_id) }, simp [continuous.comp H F.cont], end } ------------------------------------------ ---- Composition of homotopy local notation `I` := @set.univ I01 -- Prove T1 × I01, T2 × I01 cover I01 × I01 lemma cover_prod_I01 : ( (set.prod T1 (@set.univ I01)) ∪ (set.prod T2 (@set.univ I01)) ) = @set.univ (I01 × I01) := begin apply set.ext, intro x, split, simp [mem_set_of_eq], intro H, simp, have H : 0 ≤ x.1.val ∧ x.1.val ≤ 1, by exact x.1.property, unfold T1 T2 T, simp [mem_set_of_eq, or_iff_not_imp_left, -one_div_eq_inv], intro nL, have H2 : (1 / 2 :ℝ )< x.1.val, by exact nL H.1, exact ⟨ le_of_lt H2, H.2 ⟩ , end -- Closedness and intersection of T1 × I01, T2 × I01 lemma prod_T1_is_closed : is_closed (set.prod T1 I) := begin simp [T1_is_closed, is_closed_prod] end lemma prod_T2_is_closed : is_closed (set.prod T2 I) := begin simp [T2_is_closed, is_closed_prod] end lemma prod_inter_T : set.inter (set.prod T1 I) (set.prod T2 I) = set.prod { x : I01 | x.val = 1/2 } I := begin unfold T1 T2 T set.inter set.prod, simp [mem_set_of_eq, -one_div_eq_inv], apply set.ext, intro x, split, {rw mem_set_of_eq , rw mem_set_of_eq, simp [-one_div_eq_inv], intros A B C D, have H : x.1.val < 1 / 2 ∨ x.1.val = 1/2, by exact lt_or_eq_of_le B, exact le_antisymm B C }, rw mem_set_of_eq , rw mem_set_of_eq, intro H, rw H, norm_num end -- Define general / T1 / T2 reparametrised homotopy and prove continuity def fgen_hom { x y : α } {r s : ℝ} {f g: path x y } (Hrs : r < s) ( F : path_homotopy f g) : (set.prod (T r s Hrs ) I) → α := λ st, F.to_fun (( par Hrs ⟨st.1.1, (mem_prod.1 st.2).1 ⟩) , st.1.2 ) theorem p_hom_cont { x y : α } {r s : ℝ} {f g : path x y } (Hrs : r < s) ( F : path_homotopy f g) : continuous (fgen_hom Hrs F) := begin unfold fgen_hom, refine continuous.comp _ F.cont , refine continuous.prod_mk _ (continuous.comp continuous_subtype_val continuous_snd), refine continuous.comp _ (continuous_par Hrs), refine continuous_subtype_mk _ _, exact continuous.comp continuous_subtype_val continuous_fst, end def fa_hom { x y : α }{f g: path x y } ( F : path_homotopy f g) : (set.prod T1 I) → α := @fgen_hom _ _ _ _ 0 (1/2 : ℝ ) _ _ zero_lt_half F lemma CA_hom { x y : α }{f g: path x y } ( F : path_homotopy f g) : continuous (fa_hom F) := p_hom_cont zero_lt_half F def fb_hom { x y : α }{f g: path x y } ( F : path_homotopy f g) : (set.prod T2 I) → α := @fgen_hom _ _ _ _ (1/2 : ℝ ) 1 _ _ half_lt_one F lemma CB_hom { x y : α }{f g: path x y } ( F : path_homotopy f g) : continuous (fb_hom F) := p_hom_cont half_lt_one F --- -- Other helpful lemmas @[simp] lemma cond_start {f : path x y} {g : path x y} {h : path x y} ( F : path_homotopy f g) ( G : path_homotopy g h) : paste cover_prod_I01 (fa_hom F) (fb_hom G) (s, 0) = x := begin unfold paste, split_ifs, unfold fa_hom fgen_hom, simp, unfold fb_hom fgen_hom, simp, end @[simp] lemma cond_end {f : path x y} {g : path x y} {h : path x y} ( F : path_homotopy f g) ( G : path_homotopy g h) : paste cover_prod_I01 (fa_hom F) (fb_hom G) (s, 1) = y := begin unfold paste, split_ifs, unfold fa_hom fgen_hom, simp, unfold fb_hom fgen_hom, simp, end -- Homotopy composition def path_homotopy_comp {f : path x y} {g : path x y} {h : path x y} ( F : path_homotopy f g) ( G : path_homotopy g h) : path_homotopy f h := { to_fun := λ st, ( @paste (I01 × I01) β (set.prod T1 I) (set.prod T2 I) cover_prod_I01 ( λ st , (fa_hom F ) st ) ) ( λ st, (fb_hom G ) st ) st , path_s := begin intro s, unfold is_path, split, simp, split, simp, simp, unfold paste, unfold fa_hom fb_hom fgen_hom, simp, by_cases H : ∀ t : I01, (s, t) ∈ set.prod T1 I, simp [H], refine (F.path_s (par zero_lt_half ⟨ s, _ ⟩ )).2.2, unfold set.prod at H, have H2 : (s, s) ∈ {p : ↥I01 × ↥I01 | p.fst ∈ T1 ∧ p.snd ∈ univ}, exact H s, simp [mem_set_of_eq] at H2, exact H2, simp at H, have H3: s ∉ T1, simp [not_forall] at H, exact H.2, simp [H3], refine (G.path_s (par half_lt_one ⟨ s, _ ⟩ )).2.2, exact T2_of_not_T1 H3, end, at_zero := begin intro y, simp, unfold paste, rw dif_pos, unfold fa_hom fgen_hom, simp , simp [mem_set_of_eq], exact help_T1, end, at_one := begin intro y, simp, unfold paste, rw dif_neg, unfold fb_hom fgen_hom, simp , simp [mem_set_of_eq], exact help_02, end, cont := begin simp, refine cont_of_paste _ _ _ (CA_hom F) (CB_hom G) , exact prod_T1_is_closed, exact prod_T2_is_closed, unfold match_of_fun, intros x B1 B2, have Int : x ∈ set.inter (set.prod T1 I) (set.prod T2 I), exact ⟨ B1 , B2 ⟩ , rwa [prod_inter_T] at Int, have V : x.1.1 = 1/2, rwa [set.prod, mem_set_of_eq] at Int, rwa [mem_set_of_eq] at Int, exact Int.1, cases x, have xeq : x_fst = ⟨ 1/2 , help_01 ⟩ , apply subtype.eq, rw V, simp [xeq, -one_div_eq_inv], show fa_hom F ⟨(⟨1 / 2, help_01⟩, x_snd), _⟩ = fb_hom G ⟨(⟨1 / 2, help_01⟩, x_snd), _⟩ , unfold fa_hom fb_hom fgen_hom, simp [eqn_1, eqn_2, -one_div_eq_inv], end } --------------------------------- ------------------------------------------------------ ---- EQUIVALENCE OF HOMOTOPY definition is_homotopic_to (f : path x y) ( g : path x y) : Prop := nonempty ( path_homotopy f g) theorem is_reflexive : @reflexive (path x y) ( is_homotopic_to ) := begin unfold reflexive, intro f, unfold is_homotopic_to, have H : path_homotopy f f, exact path_homotopy_id f , exact ⟨ H ⟩ end theorem is_symmetric : @symmetric (path x y) (is_homotopic_to) := begin unfold symmetric, intros f g H, unfold is_homotopic_to, cases H with F, exact ⟨path_homotopy_inverse F⟩, end theorem is_transitive : @transitive (path x y) (is_homotopic_to) := begin unfold transitive, intros f g h Hfg Hgh, unfold is_homotopic_to at *, cases Hfg with F, cases Hgh with G, exact ⟨ path_homotopy_comp F G⟩ , end theorem is_equivalence : @equivalence (path x y) (is_homotopic_to) := ⟨ is_reflexive, is_symmetric, is_transitive⟩ ----------------------------------------------------- end homotopy
9e89037a7162329e717676936a7b07eb66d35c8c
b00eb947a9c4141624aa8919e94ce6dcd249ed70
/stage0/src/Lean/PrettyPrinter/Delaborator/Basic.lean
fb3643675927f6b510d7627089814c692974bf6e
[ "Apache-2.0" ]
permissive
gebner/lean4-old
a4129a041af2d4d12afb3a8d4deedabde727719b
ee51cdfaf63ee313c914d83264f91f414a0e3b6e
refs/heads/master
1,683,628,606,745
1,622,651,300,000
1,622,654,405,000
142,608,821
1
0
null
null
null
null
UTF-8
Lean
false
false
17,034
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ /-! The delaborator is the first stage of the pretty printer, and the inverse of the elaborator: it turns fully elaborated `Expr` core terms back into surface-level `Syntax`, omitting some implicit information again and using higher-level syntax abstractions like notations where possible. The exact behavior can be customized using pretty printer options; activating `pp.all` should guarantee that the delaborator is injective and that re-elaborating the resulting `Syntax` round-trips. Pretty printer options can be given not only for the whole term, but also specific subterms. This is used both when automatically refining pp options until round-trip and when interactively selecting pp options for a subterm (both TBD). The association of options to subterms is done by assigning a unique, synthetic Nat position to each subterm derived from its position in the full term. This position is added to the corresponding Syntax object so that elaboration errors and interactions with the pretty printer output can be traced back to the subterm. The delaborator is extensible via the `[delab]` attribute. -/ import Lean.KeyedDeclsAttribute import Lean.ProjFns import Lean.Syntax import Lean.Meta.Match import Lean.Elab.Term namespace Lean register_builtin_option pp.all : Bool := { defValue := false group := "pp" descr := "(pretty printer) display coercions, implicit parameters, proof terms, fully qualified names, universes, " ++ "and disable beta reduction and notations during pretty printing" } register_builtin_option pp.notation : Bool := { defValue := true group := "pp" descr := "(pretty printer) disable/enable notation (infix, mixfix, postfix operators and unicode characters)" } register_builtin_option pp.coercions : Bool := { defValue := true group := "pp" descr := "(pretty printer) hide coercion applications" } register_builtin_option pp.universes : Bool := { defValue := false group := "pp" descr := "(pretty printer) display universes" } register_builtin_option pp.full_names : Bool := { defValue := false group := "pp" descr := "(pretty printer) display fully qualified names" } register_builtin_option pp.private_names : Bool := { defValue := false group := "pp" descr := "(pretty printer) display internal names assigned to private declarations" } register_builtin_option pp.binder_types : Bool := { defValue := false group := "pp" descr := "(pretty printer) display types of lambda and Pi parameters" } register_builtin_option pp.structure_instances : Bool := { defValue := true group := "pp" -- TODO: implement second part descr := "(pretty printer) display structure instances using the '{ fieldName := fieldValue, ... }' notation " ++ "or '⟨fieldValue, ... ⟩' if structure is tagged with [pp_using_anonymous_constructor] attribute" } register_builtin_option pp.structure_projections : Bool := { defValue := true group := "pp" descr := "(pretty printer) display structure projections using field notation" } register_builtin_option pp.explicit : Bool := { defValue := false group := "pp" descr := "(pretty printer) display implicit arguments" } register_builtin_option pp.structure_instance_type : Bool := { defValue := false group := "pp" descr := "(pretty printer) display type of structure instances" } register_builtin_option pp.safe_shadowing : Bool := { defValue := true group := "pp" descr := "(pretty printer) allow variable shadowing if there is no collision" } register_builtin_option pp.proofs : Bool := { defValue := false group := "pp" descr := "(pretty printer) if set to false, replace proofs appearing as an argument to a function with a placeholder" } register_builtin_option pp.proofs.withType : Bool := { defValue := true group := "pp" descr := "(pretty printer) when eliding a proof (see `pp.proofs`), show its type instead" } -- TODO: /- register_builtin_option g_pp_max_depth : Nat := { defValue := false group := "pp" descr := "(pretty printer) maximum expression depth, after that it will use ellipsis" } register_builtin_option g_pp_max_steps : Nat := { defValue := false group := "pp" descr := "(pretty printer) maximum number of visited expressions, after that it will use ellipsis" } register_builtin_option g_pp_locals_full_names : Bool := { defValue := false group := "pp" descr := "(pretty printer) show full names of locals" } register_builtin_option g_pp_beta : Bool := { defValue := false group := "pp" descr := "(pretty printer) apply beta-reduction when pretty printing" } register_builtin_option g_pp_goal_compact : Bool := { defValue := false group := "pp" descr := "(pretty printer) try to display goal in a single line when possible" } register_builtin_option g_pp_goal_max_hyps : Nat := { defValue := false group := "pp" descr := "(pretty printer) maximum number of hypotheses to be displayed" } register_builtin_option g_pp_instantiate_mvars : Bool := { defValue := false group := "pp" descr := "(pretty printer) instantiate assigned metavariables before pretty printing terms and goals" } register_builtin_option g_pp_annotations : Bool := { defValue := false group := "pp" descr := "(pretty printer) display internal annotations (for debugging purposes only)" } register_builtin_option g_pp_compact_let : Bool := { defValue := false group := "pp" descr := "(pretty printer) minimal indentation at `let`-declarations" } -/ def getPPAll (o : Options) : Bool := o.get `pp.all false def getPPBinderTypes (o : Options) : Bool := o.get `pp.binder_types true def getPPCoercions (o : Options) : Bool := o.get `pp.coercions (!getPPAll o) def getPPExplicit (o : Options) : Bool := o.get `pp.explicit (getPPAll o) def getPPNotation (o : Options) : Bool := o.get `pp.notation (!getPPAll o) def getPPStructureProjections (o : Options) : Bool := o.get `pp.structure_projections (!getPPAll o) def getPPStructureInstances (o : Options) : Bool := o.get `pp.structure_instances (!getPPAll o) def getPPStructureInstanceType (o : Options) : Bool := o.get `pp.structure_instance_type (getPPAll o) def getPPUniverses (o : Options) : Bool := o.get `pp.universes (getPPAll o) def getPPFullNames (o : Options) : Bool := o.get `pp.full_names (getPPAll o) def getPPPrivateNames (o : Options) : Bool := o.get `pp.private_names (getPPAll o) def getPPUnicode (o : Options) : Bool := o.get `pp.unicode true def getPPSafeShadowing (o : Options) : Bool := o.get `pp.safe_shadowing true def getPPProofs (o : Options) : Bool := o.get pp.proofs.name (getPPAll o) def getPPProofsWithType (o : Options) : Bool := o.get pp.proofs.withType.name true /-- Associate pretty printer options to a specific subterm using a synthetic position. -/ abbrev OptionsPerPos := Std.RBMap Nat Options compare namespace PrettyPrinter namespace Delaborator open Lean.Meta structure Context where -- In contrast to other systems like the elaborator, we do not pass the current term explicitly as a -- parameter, but store it in the monad so that we can keep it in sync with `pos`. expr : Expr pos : Nat := 1 defaultOptions : Options optionsPerPos : OptionsPerPos currNamespace : Name openDecls : List OpenDecl inPattern : Bool := false -- true whe delaborating `match` patterns -- Exceptions from delaborators are not expected. We use an internal exception to signal whether -- the delaborator was able to produce a Syntax object. builtin_initialize delabFailureId : InternalExceptionId ← registerInternalExceptionId `delabFailure abbrev DelabM := ReaderT Context MetaM abbrev Delab := DelabM Syntax instance {α} : Inhabited (DelabM α) where default := throw arbitrary @[inline] protected def orElse {α} (d₁ d₂ : DelabM α) : DelabM α := do catchInternalId delabFailureId d₁ (fun _ => d₂) protected def failure {α} : DelabM α := throw $ Exception.internal delabFailureId instance : Alternative DelabM := { orElse := Delaborator.orElse, failure := Delaborator.failure } -- HACK: necessary since it would otherwise prefer the instance from MonadExcept instance {α} : OrElse (DelabM α) := ⟨Delaborator.orElse⟩ -- Macro scopes in the delaborator output are ultimately ignored by the pretty printer, -- so give a trivial implementation. instance : MonadQuotation DelabM := { getCurrMacroScope := pure arbitrary, getMainModule := pure arbitrary, withFreshMacroScope := fun x => x } unsafe def mkDelabAttribute : IO (KeyedDeclsAttribute Delab) := KeyedDeclsAttribute.init { builtinName := `builtinDelab, name := `delab, descr := "Register a delaborator. [delab k] registers a declaration of type `Lean.PrettyPrinter.Delaborator.Delab` for the `Lean.Expr` constructor `k`. Multiple delaborators for a single constructor are tried in turn until the first success. If the term to be delaborated is an application of a constant `c`, elaborators for `app.c` are tried first; this is also done for `Expr.const`s (\"nullary applications\") to reduce special casing. If the term is an `Expr.mdata` with a single key `k`, `mdata.k` is tried first.", valueTypeName := `Lean.PrettyPrinter.Delaborator.Delab } `Lean.PrettyPrinter.Delaborator.delabAttribute @[builtinInit mkDelabAttribute] constant delabAttribute : KeyedDeclsAttribute Delab def getExpr : DelabM Expr := do let ctx ← read pure ctx.expr def getExprKind : DelabM Name := do let e ← getExpr pure $ match e with | Expr.bvar _ _ => `bvar | Expr.fvar _ _ => `fvar | Expr.mvar _ _ => `mvar | Expr.sort _ _ => `sort | Expr.const c _ _ => -- we identify constants as "nullary applications" to reduce special casing `app ++ c | Expr.app fn _ _ => match fn.getAppFn with | Expr.const c _ _ => `app ++ c | _ => `app | Expr.lam _ _ _ _ => `lam | Expr.forallE _ _ _ _ => `forallE | Expr.letE _ _ _ _ _ => `letE | Expr.lit _ _ => `lit | Expr.mdata m _ _ => match m.entries with | [(key, _)] => `mdata ++ key | _ => `mdata | Expr.proj _ _ _ _ => `proj /-- Evaluate option accessor, using subterm-specific options if set. -/ def getPPOption (opt : Options → Bool) : DelabM Bool := do let ctx ← read let mut opts := ctx.defaultOptions if let some opts' ← ctx.optionsPerPos.find? ctx.pos then for (k, v) in opts' do opts := opts.insert k v return opt opts def whenPPOption (opt : Options → Bool) (d : Delab) : Delab := do let b ← getPPOption opt if b then d else failure def whenNotPPOption (opt : Options → Bool) (d : Delab) : Delab := do let b ← getPPOption opt if b then failure else d /-- Descend into `child`, the `childIdx`-th subterm of the current term, and update position. Because `childIdx < 3` in the case of `Expr`, we can injectively map a path `childIdxs` to a natural number by computing the value of the 3-ary representation `1 :: childIdxs`, since n-ary representations without leading zeros are unique. Note that `pos` is initialized to `1` (case `childIdxs == []`). -/ def descend {α} (child : Expr) (childIdx : Nat) (d : DelabM α) : DelabM α := withReader (fun cfg => { cfg with expr := child, pos := cfg.pos * 3 + childIdx }) d def withAppFn {α} (d : DelabM α) : DelabM α := do let Expr.app fn _ _ ← getExpr | unreachable! descend fn 0 d def withAppArg {α} (d : DelabM α) : DelabM α := do let Expr.app _ arg _ ← getExpr | unreachable! descend arg 1 d partial def withAppFnArgs {α} : DelabM α → (α → DelabM α) → DelabM α | fnD, argD => do let Expr.app fn arg _ ← getExpr | fnD let a ← withAppFn (withAppFnArgs fnD argD) withAppArg (argD a) def withBindingDomain {α} (d : DelabM α) : DelabM α := do let e ← getExpr descend e.bindingDomain! 0 d def withBindingBody {α} (n : Name) (d : DelabM α) : DelabM α := do let e ← getExpr withLocalDecl n e.binderInfo e.bindingDomain! fun fvar => let b := e.bindingBody!.instantiate1 fvar descend b 1 d def withProj {α} (d : DelabM α) : DelabM α := do let Expr.proj _ _ e _ ← getExpr | unreachable! descend e 0 d def withMDataExpr {α} (d : DelabM α) : DelabM α := do let Expr.mdata _ e _ ← getExpr | unreachable! -- do not change position so that options on an mdata are automatically forwarded to the child withReader ({ · with expr := e }) d partial def annotatePos (pos : Nat) : Syntax → Syntax | stx@(Syntax.ident _ _ _ _) => stx.setInfo (SourceInfo.synthetic pos pos) -- app => annotate function | stx@(Syntax.node `Lean.Parser.Term.app args) => stx.modifyArg 0 (annotatePos pos) -- otherwise, annotate first direct child token if any | stx => match stx.getArgs.findIdx? Syntax.isAtom with | some idx => stx.modifyArg idx (Syntax.setInfo (SourceInfo.synthetic pos pos)) | none => stx def annotateCurPos (stx : Syntax) : Delab := do let ctx ← read pure $ annotatePos ctx.pos stx def getUnusedName (suggestion : Name) (body : Expr) : DelabM Name := do -- Use a nicer binder name than `[anonymous]`. We probably shouldn't do this in all LocalContext use cases, so do it here. let suggestion := if suggestion.isAnonymous then `a else suggestion; let suggestion := suggestion.eraseMacroScopes let lctx ← getLCtx if !lctx.usesUserName suggestion then return suggestion else if (← getPPOption getPPSafeShadowing) && !bodyUsesSuggestion lctx suggestion then return suggestion else return lctx.getUnusedName suggestion where bodyUsesSuggestion (lctx : LocalContext) (suggestion' : Name) : Bool := Option.isSome <| body.find? fun | Expr.fvar fvarId _ => match lctx.find? fvarId with | none => false | some decl => decl.userName == suggestion' | _ => false def withBindingBodyUnusedName {α} (d : Syntax → DelabM α) : DelabM α := do let n ← getUnusedName (← getExpr).bindingName! (← getExpr).bindingBody! let stxN ← annotateCurPos (mkIdent n) withBindingBody n $ d stxN @[inline] def liftMetaM {α} (x : MetaM α) : DelabM α := liftM x partial def delabFor : Name → Delab | Name.anonymous => failure | k => do let env ← getEnv (match (delabAttribute.ext.getState env).table.find? k with | some delabs => delabs.firstM id >>= annotateCurPos | none => failure) <|> -- have `app.Option.some` fall back to `app` etc. delabFor k.getRoot partial def delab : Delab := do unless (← getPPOption getPPProofs) do let e ← getExpr -- no need to hide atomic proofs unless e.isAtomic do try let ty ← Meta.inferType (← getExpr) if ← Meta.isProp ty then if ← getPPOption getPPProofsWithType then return ← ``((_ : $(← descend ty 0 delab))) else return ← ``(_) catch _ => pure () let k ← getExprKind delabFor k <|> (liftM $ show MetaM Syntax from throwError "don't know how to delaborate '{k}'") unsafe def mkAppUnexpanderAttribute : IO (KeyedDeclsAttribute Unexpander) := KeyedDeclsAttribute.init { name := `appUnexpander, descr := "Register an unexpander for applications of a given constant. [appUnexpander c] registers a `Lean.PrettyPrinter.Unexpander` for applications of the constant `c`. The unexpander is passed the result of pre-pretty printing the application *without* implicitly passed arguments. If `pp.explicit` is set to true or `pp.notation` is set to false, it will not be called at all.", valueTypeName := `Lean.PrettyPrinter.Unexpander } `Lean.PrettyPrinter.Delaborator.appUnexpanderAttribute @[builtinInit mkAppUnexpanderAttribute] constant appUnexpanderAttribute : KeyedDeclsAttribute Unexpander end Delaborator /-- "Delaborate" the given term into surface-level syntax using the default and given subterm-specific options. -/ def delab (currNamespace : Name) (openDecls : List OpenDecl) (e : Expr) (optionsPerPos : OptionsPerPos := {}) : MetaM Syntax := do trace[PrettyPrinter.delab.input] "{fmt e}" let mut opts ← MonadOptions.getOptions -- default `pp.proofs` to `true` if `e` is a proof if pp.proofs.get? opts == none then try let ty ← Meta.inferType e if ← Meta.isProp ty then opts := pp.proofs.set opts true catch _ => pure () catchInternalId Delaborator.delabFailureId (Delaborator.delab.run { expr := e, defaultOptions := opts, optionsPerPos := optionsPerPos, currNamespace := currNamespace, openDecls := openDecls }) (fun _ => unreachable!) builtin_initialize registerTraceClass `PrettyPrinter.delab end PrettyPrinter end Lean
6252da25d354dd8a97a25b29ba35c05f4d33d146
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/undergraduate/MAS114/Semester 1/Q05.lean
fb811f705656d3f877fef7302d095a27c357b516
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,848
lean
import data.fintype namespace MAS114 namespace exercises_1 namespace Q05 def A : finset ℕ := [0,1,2].to_finset def B : finset ℕ := [0,1,2,3].to_finset def C : finset ℤ := [-2,-1,0,1,2].to_finset def D : finset ℤ := [-3,-2,-1,0,1,2,3].to_finset lemma int.lt_succ_iff {n m : ℤ} : n < m + 1 ↔ n ≤ m := ⟨int.le_of_lt_add_one,int.lt_add_one_of_le⟩ lemma nat.square_le {n m : ℕ} : m ^ 2 ≤ n ↔ m ≤ n.sqrt := ⟨λ h0, le_of_not_gt (λ h1, not_le_of_gt ((nat.pow_two m).symm.subst (nat.sqrt_lt.mp h1)) h0), λ h0, le_of_not_gt (λ h1,not_le_of_gt (nat.sqrt_lt.mpr ((nat.pow_two m).subst h1)) h0)⟩ lemma nat.square_lt {n m : ℕ} : m ^ 2 < n.succ ↔ m ≤ n.sqrt := (@nat.lt_succ_iff (m ^ 2) n).trans (@nat.square_le n m) lemma int.abs_le {n m : ℤ} : abs m ≤ n ↔ (- n ≤ m ∧ m ≤ n) := begin by_cases hm : 0 ≤ m, { rw[abs_of_nonneg hm], exact ⟨ λ hmn, ⟨le_trans (neg_nonpos_of_nonneg (le_trans hm hmn)) hm,hmn⟩, λ hmn, hmn.right ⟩ },{ let hma := le_of_lt (lt_of_not_ge hm), let hmb := neg_nonneg_of_nonpos hma, rw[abs_of_nonpos hma], exact ⟨ λ hmn, ⟨(neg_neg m) ▸ (neg_le_neg hmn),le_trans (le_trans hma hmb) hmn⟩, λ hmn, (neg_neg n) ▸ (neg_le_neg hmn.left), ⟩ } end lemma int.abs_le' {n : ℕ} {m : ℤ} : m.nat_abs ≤ n ↔ (- (n : ℤ) ≤ m ∧ m ≤ n) := begin let h := @int.abs_le n m, rw[int.abs_eq_nat_abs,int.coe_nat_le] at h, exact h, end lemma int.abs_square (n : ℤ) : n ^ 2 = (abs n) ^ 2 := begin by_cases h0 : n ≥ 0, {rw[abs_of_nonneg h0]}, {rw[abs_of_neg (lt_of_not_ge h0),pow_two,pow_two,neg_mul_neg],} end lemma int.abs_square' (n : ℤ) : n ^ 2 = ((int.nat_abs n) ^ 2 : ℕ) := calc n ^ 2 = n * n : pow_two n ... = ↑ (n.nat_abs * n.nat_abs) : int.nat_abs_mul_self.symm ... = ↑ (n.nat_abs ^ 2 : ℕ) : by rw[(nat.pow_two n.nat_abs).symm] lemma int.square_le {n : ℕ} {m : ℤ} : m ^ 2 ≤ n ↔ - (n.sqrt : ℤ) ≤ m ∧ m ≤ n.sqrt := begin rw[int.abs_square',int.coe_nat_le,nat.square_le,int.abs_le'], end lemma int.square_lt {n : ℕ} {m : ℤ} : m ^ 2 < n.succ ↔ - (n.sqrt : ℤ) ≤ m ∧ m ≤ n.sqrt := begin rw[int.abs_square',int.coe_nat_lt,nat.square_lt,int.abs_le'], end lemma A_spec (n : ℕ) : n ∈ A ↔ n ^ 2 < 9 := begin have sqrt_8 : 2 = nat.sqrt 8 := nat.eq_sqrt.mpr ⟨dec_trivial,dec_trivial⟩, exact calc n ∈ A ↔ n ∈ finset.range 3 : by rw[(dec_trivial : A = finset.range 3)] ... ↔ n < 3 : finset.mem_range ... ↔ n ≤ 2 : by rw[nat.lt_succ_iff] ... ↔ n ≤ nat.sqrt 8 : by rw[← sqrt_8] ... ↔ n ^ 2 < 9 : by rw[nat.square_lt] end lemma B_spec (n : ℕ) : n ∈ B ↔ n ^ 2 ≤ 9 := begin have sqrt_9 : 3 = nat.sqrt 9 := nat.eq_sqrt.mpr ⟨dec_trivial,dec_trivial⟩, exact calc n ∈ B ↔ n ∈ finset.range 4 : by rw[(dec_trivial : B = finset.range 4)] ... ↔ n < 4 : finset.mem_range ... ↔ n ≤ 3 : by rw[nat.lt_succ_iff] ... ↔ n ≤ nat.sqrt 9 : by rw[← sqrt_9] ... ↔ n ^ 2 ≤ 9 : by rw[nat.square_le] end lemma C_spec (n : ℤ) : n ∈ C ↔ n ^ 2 < 9 := begin have sqrt_8 : 2 = nat.sqrt 8 := nat.eq_sqrt.mpr ⟨dec_trivial,dec_trivial⟩, have e0 : (3 : ℤ) = ((2 : ℕ) : ℤ) + (1 : ℤ) := rfl, have e1 : (-2 : ℤ) = - ((2 : ℕ) : ℤ) := rfl, have e2 : ((nat.succ 8) : ℤ) = 9 := rfl, let e3 := @int.square_lt 8 n, exact calc n ∈ C ↔ n ∈ (int.range (-2) 3).to_finset : by rw[(dec_trivial : C = (int.range (-2) 3).to_finset)] ... ↔ n ∈ int.range (-2) 3 : by rw[list.mem_to_finset] ... ↔ -2 ≤ n ∧ n < 3 : int.mem_range_iff ... ↔ - ((2 : ℕ) : ℤ) ≤ n ∧ n < (2 : ℕ) + 1 : by rw[e0,e1] ... ↔ - ((2 : ℕ) : ℤ) ≤ n ∧ n ≤ (2 : ℕ) : by rw[int.lt_succ_iff] ... ↔ - (nat.sqrt 8 : ℤ) ≤ n ∧ n ≤ nat.sqrt 8 : by rw[← sqrt_8] ... ↔ n ^ 2 < nat.succ 8 : by rw[e3] ... ↔ n ^ 2 < 9 : by rw[e2], end lemma D_spec (n : ℤ) : n ∈ D ↔ n ^ 2 ≤ 9 := begin have sqrt_9 : 3 = nat.sqrt 9 := nat.eq_sqrt.mpr ⟨dec_trivial,dec_trivial⟩, have e0 : (4 : ℤ) = ((3 : ℕ) : ℤ) + (1 : ℤ) := rfl, have e1 : (-3 : ℤ) = - ((3 : ℕ) : ℤ) := rfl, have e2 : ((9 : ℕ) : ℤ) = 9 := rfl, let e3 := @int.square_le 9 n, exact calc n ∈ D ↔ n ∈ (int.range (-3) 4).to_finset : by rw[(dec_trivial : D = (int.range (-3) 4).to_finset)] ... ↔ n ∈ int.range (-3) 4 : by rw[list.mem_to_finset] ... ↔ -3 ≤ n ∧ n < 4 : int.mem_range_iff ... ↔ - ((3 : ℕ) : ℤ) ≤ n ∧ n < (3 : ℕ) + 1 : by rw[e0,e1] ... ↔ - ((3 : ℕ) : ℤ) ≤ n ∧ n ≤ (3 : ℕ) : by rw[int.lt_succ_iff] ... ↔ - (nat.sqrt 9 : ℤ) ≤ n ∧ n ≤ nat.sqrt 9 : by rw[← sqrt_9] ... ↔ n ^ 2 ≤ (9 : ℕ) : by rw[e3] ... ↔ n ^ 2 ≤ 9 : by rw[e2] end end Q05 end exercises_1 end MAS114
01d7dd07c0fd069ed254e91daa12ed484d7742ff
491068d2ad28831e7dade8d6dff871c3e49d9431
/hott/types/W.hlean
51d1f6e67ed49f74caca493e47f9d7a3ef1dc64b
[ "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
4,680
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Theorems about W-types (well-founded trees) -/ import .sigma .pi open eq equiv is_equiv sigma sigma.ops inductive Wtype.{l k} {A : Type.{l}} (B : A → Type.{k}) : Type.{max l k} := sup : Π (a : A), (B a → Wtype.{l k} B) → Wtype.{l k} B namespace Wtype notation `W` binders `, ` r:(scoped B, Wtype B) := r universe variables u v variables {A A' : Type.{u}} {B B' : A → Type.{v}} {C : Π(a : A), B a → Type} {a a' : A} {f : B a → W a, B a} {f' : B a' → W a, B a} {w w' : W(a : A), B a} protected definition pr1 [unfold 3] (w : W(a : A), B a) : A := by cases w with a f; exact a protected definition pr2 [unfold 3] (w : W(a : A), B a) : B (Wtype.pr1 w) → W(a : A), B a := by cases w with a f; exact f namespace ops postfix `.1`:(max+1) := Wtype.pr1 postfix `.2`:(max+1) := Wtype.pr2 notation `⟨` a `, ` f `⟩`:0 := Wtype.sup a f --input ⟨ ⟩ as \< \> end ops open ops protected definition eta (w : W a, B a) : ⟨w.1 , w.2⟩ = w := by cases w; exact idp definition sup_eq_sup (p : a = a') (q : f =[p] f') : ⟨a, f⟩ = ⟨a', f'⟩ := by cases q; exact idp definition Wtype_eq (p : w.1 = w'.1) (q : w.2 =[p] w'.2) : w = w' := by cases w; cases w';exact (sup_eq_sup p q) definition Wtype_eq_pr1 (p : w = w') : w.1 = w'.1 := by cases p;exact idp definition Wtype_eq_pr2 (p : w = w') : w.2 =[Wtype_eq_pr1 p] w'.2 := by cases p;exact idpo namespace ops postfix `..1`:(max+1) := Wtype_eq_pr1 postfix `..2`:(max+1) := Wtype_eq_pr2 end ops open ops open sigma definition sup_path_W (p : w.1 = w'.1) (q : w.2 =[p] w'.2) : ⟨(Wtype_eq p q)..1, (Wtype_eq p q)..2⟩ = ⟨p, q⟩ := by cases w; cases w'; cases q; exact idp definition pr1_path_W (p : w.1 = w'.1) (q : w.2 =[p] w'.2) : (Wtype_eq p q)..1 = p := !sup_path_W..1 definition pr2_path_W (p : w.1 = w'.1) (q : w.2 =[p] w'.2) : (Wtype_eq p q)..2 =[pr1_path_W p q] q := !sup_path_W..2 definition eta_path_W (p : w = w') : Wtype_eq (p..1) (p..2) = p := by cases p; cases w; exact idp definition transport_pr1_path_W {B' : A → Type} (p : w.1 = w'.1) (q : w.2 =[p] w'.2) : transport (λx, B' x.1) (Wtype_eq p q) = transport B' p := by cases w; cases w'; cases q; exact idp definition path_W_uncurried (pq : Σ(p : w.1 = w'.1), w.2 =[p] w'.2) : w = w' := by cases pq with p q; exact (Wtype_eq p q) definition sup_path_W_uncurried (pq : Σ(p : w.1 = w'.1), w.2 =[p] w'.2) : ⟨(path_W_uncurried pq)..1, (path_W_uncurried pq)..2⟩ = pq := by cases pq with p q; exact (sup_path_W p q) definition pr1_path_W_uncurried (pq : Σ(p : w.1 = w'.1), w.2 =[p] w'.2) : (path_W_uncurried pq)..1 = pq.1 := !sup_path_W_uncurried..1 definition pr2_path_W_uncurried (pq : Σ(p : w.1 = w'.1), w.2 =[p] w'.2) : (path_W_uncurried pq)..2 =[pr1_path_W_uncurried pq] pq.2 := !sup_path_W_uncurried..2 definition eta_path_W_uncurried (p : w = w') : path_W_uncurried ⟨p..1, p..2⟩ = p := !eta_path_W definition transport_pr1_path_W_uncurried {B' : A → Type} (pq : Σ(p : w.1 = w'.1), w.2 =[p] w'.2) : transport (λx, B' x.1) (@path_W_uncurried A B w w' pq) = transport B' pq.1 := by cases pq with p q; exact (transport_pr1_path_W p q) definition isequiv_path_W /-[instance]-/ (w w' : W a, B a) : is_equiv (path_W_uncurried : (Σ(p : w.1 = w'.1), w.2 =[p] w'.2) → w = w') := adjointify path_W_uncurried (λp, ⟨p..1, p..2⟩) eta_path_W_uncurried sup_path_W_uncurried definition equiv_path_W (w w' : W a, B a) : (Σ(p : w.1 = w'.1), w.2 =[p] w'.2) ≃ (w = w') := equiv.mk path_W_uncurried !isequiv_path_W definition double_induction_on {P : (W a, B a) → (W a, B a) → Type} (w w' : W a, B a) (H : ∀ (a a' : A) (f : B a → W a, B a) (f' : B a' → W a, B a), (∀ (b : B a) (b' : B a'), P (f b) (f' b')) → P (sup a f) (sup a' f')) : P w w' := begin revert w', induction w with a f IH, intro w', cases w' with a' f', apply H, intro b b', apply IH end /- truncatedness -/ open is_trunc pi definition trunc_W [instance] (n : trunc_index) [HA : is_trunc (n.+1) A] : is_trunc (n.+1) (W a, B a) := begin fapply is_trunc_succ_intro, intro w w', eapply (double_induction_on w w'), intro a a' f f' IH, fapply is_trunc_equiv_closed, { apply equiv_path_W}, { apply is_trunc_sigma, intro p, cases p, esimp, apply is_trunc_equiv_closed_rev, apply pathover_idp} end end Wtype
30f2921efea0d13e46a3ca9ab46ac15d99538c47
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/src/Lean/PrettyPrinter/Delaborator/Basic.lean
6587c17080ffad6e26c7ab1288c3f28a1ac87b26
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,639
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ /-! The delaborator is the first stage of the pretty printer, and the inverse of the elaborator: it turns fully elaborated `Expr` core terms back into surface-level `Syntax`, omitting some implicit information again and using higher-level syntax abstractions like notations where possible. The exact behavior can be customized using pretty printer options; activating `pp.all` should guarantee that the delaborator is injective and that re-elaborating the resulting `Syntax` round-trips. Pretty printer options can be given not only for the whole term, but also specific subterms. This is used both when automatically refining pp options until round-trip and when interactively selecting pp options for a subterm (both TBD). The association of options to subterms is done by assigning a unique, synthetic Nat position to each subterm derived from its position in the full term. This position is added to the corresponding Syntax object so that elaboration errors and interactions with the pretty printer output can be traced back to the subterm. The delaborator is extensible via the `[delab]` attribute. -/ import Lean.KeyedDeclsAttribute import Lean.ProjFns import Lean.Syntax import Lean.Meta.Match import Lean.Elab.Term namespace Lean def getPPAll (o : Options) : Bool := o.get `pp.all false def getPPBinderTypes (o : Options) : Bool := o.get `pp.binder_types (!getPPAll o) def getPPCoercions (o : Options) : Bool := o.get `pp.coercions (!getPPAll o) def getPPExplicit (o : Options) : Bool := o.get `pp.explicit (getPPAll o) def getPPNotation (o : Options) : Bool := o.get `pp.notation (!getPPAll o) def getPPStructureProjections (o : Options) : Bool := o.get `pp.structure_projections (!getPPAll o) def getPPStructureInstances (o : Options) : Bool := o.get `pp.structure_instances (!getPPAll o) def getPPStructureInstanceType (o : Options) : Bool := o.get `pp.structure_instance_type (getPPAll o) def getPPUniverses (o : Options) : Bool := o.get `pp.universes (getPPAll o) def getPPFullNames (o : Options) : Bool := o.get `pp.full_names (getPPAll o) def getPPPrivateNames (o : Options) : Bool := o.get `pp.private_names (getPPAll o) def getPPUnicode (o : Options) : Bool := o.get `pp.unicode (!getPPAll o) builtin_initialize registerOption `pp.explicit { defValue := false, group := "pp", descr := "(pretty printer) display implicit arguments" } registerOption `pp.structure_instance_type { defValue := false, group := "pp", descr := "(pretty printer) display type of structure instances" } -- TODO: register other options when old pretty printer is removed --registerOption `pp.universes { defValue := false, group := "pp", descr := "(pretty printer) display universes" } /-- Associate pretty printer options to a specific subterm using a synthetic position. -/ abbrev OptionsPerPos := Std.RBMap Nat Options (fun a b => a < b) namespace PrettyPrinter namespace Delaborator open Lean.Meta structure Context where -- In contrast to other systems like the elaborator, we do not pass the current term explicitly as a -- parameter, but store it in the monad so that we can keep it in sync with `pos`. expr : Expr pos : Nat := 1 defaultOptions : Options optionsPerPos : OptionsPerPos currNamespace : Name openDecls : List OpenDecl -- Exceptions from delaborators are not expected. We use an internal exception to signal whether -- the delaborator was able to produce a Syntax object. builtin_initialize delabFailureId : InternalExceptionId ← registerInternalExceptionId `delabFailure abbrev DelabM := ReaderT Context MetaM abbrev Delab := DelabM Syntax instance {α} : Inhabited (DelabM α) where default := throw arbitrary @[inline] protected def orElse {α} (d₁ d₂ : DelabM α) : DelabM α := do catchInternalId delabFailureId d₁ (fun _ => d₂) protected def failure {α} : DelabM α := throw $ Exception.internal delabFailureId instance : Alternative DelabM := { orElse := Delaborator.orElse, failure := Delaborator.failure } -- HACK: necessary since it would otherwise prefer the instance from MonadExcept instance {α} : OrElse (DelabM α) := ⟨Delaborator.orElse⟩ -- Macro scopes in the delaborator output are ultimately ignored by the pretty printer, -- so give a trivial implementation. instance : MonadQuotation DelabM := { getCurrMacroScope := pure arbitrary, getMainModule := pure arbitrary, withFreshMacroScope := fun x => x } unsafe def mkDelabAttribute : IO (KeyedDeclsAttribute Delab) := KeyedDeclsAttribute.init { builtinName := `builtinDelab, name := `delab, descr := "Register a delaborator. [delab k] registers a declaration of type `Lean.PrettyPrinter.Delaborator.Delab` for the `Lean.Expr` constructor `k`. Multiple delaborators for a single constructor are tried in turn until the first success. If the term to be delaborated is an application of a constant `c`, elaborators for `app.c` are tried first; this is also done for `Expr.const`s (\"nullary applications\") to reduce special casing. If the term is an `Expr.mdata` with a single key `k`, `mdata.k` is tried first.", valueTypeName := `Lean.PrettyPrinter.Delaborator.Delab } `Lean.PrettyPrinter.Delaborator.delabAttribute @[builtinInit mkDelabAttribute] constant delabAttribute : KeyedDeclsAttribute Delab def getExpr : DelabM Expr := do let ctx ← read pure ctx.expr def getExprKind : DelabM Name := do let e ← getExpr pure $ match e with | Expr.bvar _ _ => `bvar | Expr.fvar _ _ => `fvar | Expr.mvar _ _ => `mvar | Expr.sort _ _ => `sort | Expr.const c _ _ => -- we identify constants as "nullary applications" to reduce special casing `app ++ c | Expr.app fn _ _ => match fn.getAppFn with | Expr.const c _ _ => `app ++ c | _ => `app | Expr.lam _ _ _ _ => `lam | Expr.forallE _ _ _ _ => `forallE | Expr.letE _ _ _ _ _ => `letE | Expr.lit _ _ => `lit | Expr.mdata m _ _ => match m.entries with | [(key, _)] => `mdata ++ key | _ => `mdata | Expr.proj _ _ _ _ => `proj /-- Evaluate option accessor, using subterm-specific options if set. Default to `true` if `pp.all` is set. -/ def getPPOption (opt : Options → Bool) : DelabM Bool := do let ctx ← read let val := opt ctx.defaultOptions match ctx.optionsPerPos.find? ctx.pos with | some opts => pure $ opt opts | none => pure val def whenPPOption (opt : Options → Bool) (d : Delab) : Delab := do let b ← getPPOption opt if b then d else failure def whenNotPPOption (opt : Options → Bool) (d : Delab) : Delab := do let b ← getPPOption opt if b then failure else d /-- Descend into `child`, the `childIdx`-th subterm of the current term, and update position. Because `childIdx < 3` in the case of `Expr`, we can injectively map a path `childIdxs` to a natural number by computing the value of the 3-ary representation `1 :: childIdxs`, since n-ary representations without leading zeros are unique. Note that `pos` is initialized to `1` (case `childIdxs == []`). -/ def descend {α} (child : Expr) (childIdx : Nat) (d : DelabM α) : DelabM α := withReader (fun cfg => { cfg with expr := child, pos := cfg.pos * 3 + childIdx }) d def withAppFn {α} (d : DelabM α) : DelabM α := do let Expr.app fn _ _ ← getExpr | unreachable! descend fn 0 d def withAppArg {α} (d : DelabM α) : DelabM α := do let Expr.app _ arg _ ← getExpr | unreachable! descend arg 1 d partial def withAppFnArgs {α} : DelabM α → (α → DelabM α) → DelabM α | fnD, argD => do let Expr.app fn arg _ ← getExpr | fnD let a ← withAppFn (withAppFnArgs fnD argD) withAppArg (argD a) def withBindingDomain {α} (d : DelabM α) : DelabM α := do let e ← getExpr descend e.bindingDomain! 0 d def withBindingBody {α} (n : Name) (d : DelabM α) : DelabM α := do let e ← getExpr withLocalDecl n e.binderInfo e.bindingDomain! fun fvar => let b := e.bindingBody!.instantiate1 fvar descend b 1 d def withProj {α} (d : DelabM α) : DelabM α := do let Expr.proj _ _ e _ ← getExpr | unreachable! descend e 0 d def withMDataExpr {α} (d : DelabM α) : DelabM α := do let Expr.mdata _ e _ ← getExpr | unreachable! -- do not change position so that options on an mdata are automatically forwarded to the child withReader ({ · with expr := e }) d partial def annotatePos (pos : Nat) : Syntax → Syntax | stx@(Syntax.ident _ _ _ _) => stx.setInfo { pos := pos } -- app => annotate function | stx@(Syntax.node `Lean.Parser.Term.app args) => stx.modifyArg 0 (annotatePos pos) -- otherwise, annotate first direct child token if any | stx => match stx.getArgs.findIdx? Syntax.isAtom with | some idx => stx.modifyArg idx (Syntax.setInfo { pos := pos }) | none => stx def annotateCurPos (stx : Syntax) : Delab := do let ctx ← read pure $ annotatePos ctx.pos stx def getUnusedName (suggestion : Name) : DelabM Name := do -- Use a nicer binder name than `[anonymous]`. We probably shouldn't do this in all LocalContext use cases, so do it here. let suggestion := if suggestion.isAnonymous then `a else suggestion; let lctx ← getLCtx pure $ lctx.getUnusedName suggestion def withBindingBodyUnusedName {α} (d : Syntax → DelabM α) : DelabM α := do let n ← getUnusedName (← getExpr).bindingName! let stxN ← annotateCurPos (mkIdent n) withBindingBody n $ d stxN @[inline] def liftMetaM {α} (x : MetaM α) : DelabM α := liftM x partial def delabFor : Name → Delab | Name.anonymous => failure | k => do let env ← getEnv (match (delabAttribute.ext.getState env).table.find? k with | some delabs => delabs.firstM id >>= annotateCurPos | none => failure) <|> -- have `app.Option.some` fall back to `app` etc. delabFor k.getRoot def delab : Delab := do let k ← getExprKind delabFor k <|> (liftM $ show MetaM Syntax from throwError! "don't know how to delaborate '{k}'") unsafe def mkAppUnexpanderAttribute : IO (KeyedDeclsAttribute Unexpander) := KeyedDeclsAttribute.init { name := `appUnexpander, descr := "Register an unexpander for applications of a given constant. [appUnexpander c] registers a `Lean.PrettyPrinter.Unexpander` for applications of the constant `c`. The unexpander is passed the result of pre-pretty printing the application *without* implicitly passed arguments. If `pp.explicit` is set to true or `pp.notation` is set to false, it will not be called at all.", valueTypeName := `Lean.PrettyPrinter.Unexpander } `Lean.PrettyPrinter.Delaborator.appUnexpanderAttribute @[builtinInit mkAppUnexpanderAttribute] constant appUnexpanderAttribute : KeyedDeclsAttribute Unexpander end Delaborator /-- "Delaborate" the given term into surface-level syntax using the default and given subterm-specific options. -/ def delab (currNamespace : Name) (openDecls : List OpenDecl) (e : Expr) (optionsPerPos : OptionsPerPos := {}) : MetaM Syntax := do trace[PrettyPrinter.delab.input]! "{fmt e}" let opts ← MonadOptions.getOptions catchInternalId Delaborator.delabFailureId (Delaborator.delab.run { expr := e, defaultOptions := opts, optionsPerPos := optionsPerPos, currNamespace := currNamespace, openDecls := openDecls }) (fun _ => unreachable!) builtin_initialize registerTraceClass `PrettyPrinter.delab end PrettyPrinter end Lean
45e9e2671e3f2ad08f4e66000f87a3a2eaea3542
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/geo/src/beck.lean
056623f32e5f783ec7cb24a06918551ed0078e79
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
6,017
lean
import data.fintype import category_theory.limits.limits import category_theory.monad.limits import category_theory.monad import category_theory.limits.shapes.equalizers import tactic import category_theory.monad.adjunction universes u v u₂ v₂ v₁ u₁ namespace category_theory open limits section reflexive_pair def reflexive_pair : Type v := limits.walking_parallel_pair.{v} open limits.walking_parallel_pair inductive reflexive_pair_hom : reflexive_pair.{v} → reflexive_pair.{v} → Type v |left : reflexive_pair_hom zero one |right : reflexive_pair_hom zero one |back : reflexive_pair_hom one zero |left_back : reflexive_pair_hom zero zero |right_back : reflexive_pair_hom zero zero |id : Π (X : reflexive_pair), reflexive_pair_hom X X open reflexive_pair_hom def reflexive_pair_hom.comp : Π (X Y Z : reflexive_pair.{v}) (f : reflexive_pair_hom.{v} X Y) (g : reflexive_pair_hom.{v} Y Z), reflexive_pair_hom.{v} X Z | _ _ _ back left := reflexive_pair_hom.id _ | _ _ _ back right := reflexive_pair_hom.id _ | _ _ _ left back := left_back | _ _ _ right back := right_back | _ _ _ back left_back := back | _ _ _ back right_back := back | _ _ _ left_back left_back := left_back | _ _ _ right_back right_back := right_back | _ _ _ left_back left := left | _ _ _ left_back right := left | _ _ _ right_back left := right | _ _ _ right_back right := right | _ _ _ left_back right_back := left_back | _ _ _ right_back left_back := right_back | _ _ _ (id _) h := h | _ _ _ back (id zero) := back | _ _ _ left_back (id zero) := left_back | _ _ _ right_back (id zero) := right_back | _ _ _ left (id one) := left | _ _ _ right (id one) := right end reflexive_pair instance walking_parallel_pair_hom_category : small_category.{v} reflexive_pair := { hom := reflexive_pair_hom, id := reflexive_pair_hom.id, comp := reflexive_pair_hom.comp, assoc' := begin intros, cases f; cases g; cases h, all_goals {refl} end, id_comp' := begin intros, cases f, all_goals {refl} end, comp_id' := begin intros, cases f, all_goals {refl} end, } variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 variables {A B : C} structure split_coequaliser (f g : A ⟶ B) := (cf : cofork f g) (t : B ⟶ A) (s : cf.X ⟶ B) (p1 : s ≫ cf.π = 𝟙 _) (p2 : t ≫ g = 𝟙 B) (p3 : t ≫ f = cf.π ≫ s) -- [todo] show it's a coequaliser open category_theory @[simp] lemma simp_parallel_zero {f g : A ⟶ B} (t : cofork f g) : t.ι.app walking_parallel_pair.zero = f ≫ t.π := begin rw ← cocone.w t walking_parallel_pair_hom.left, refl end /-- You can make a coequaliser by finding a π which uniquely factors any other cofork. -/ def is_coeq_lemma {f g : A ⟶ B} {X : C} (π : B ⟶ X) (e : f ≫ π = g ≫ π) (factor : ∀ {Y} (c : B ⟶ Y), (f ≫ c = g ≫ c) → unique {m : X ⟶ Y // c = π ≫ m}) : has_colimit (parallel_pair f g) := begin refine {cocone := cofork.of_π π e, is_colimit := _}, refine {desc := λ c : cofork f g, _, fac' := λ c : cofork f g, _, uniq' := λ c : cofork f g, _}, rcases (factor c.π c.condition) with ⟨⟨⟨k,h1⟩⟩,h2⟩, apply k, rcases (factor c.π c.condition) with ⟨⟨⟨k,h1⟩⟩,h2⟩, rintros (_|_), change (_ ≫ _) ≫ k = _, rw category.assoc, rw ← h1, rw simp_parallel_zero, change π ≫ k = c.π, dsimp, rw h1, rcases (factor c.π c.condition) with ⟨⟨⟨k,h1⟩⟩,h2⟩, intros, change m = k, have, apply h2 ⟨m,eq.symm (w walking_parallel_pair.one)⟩, apply subtype.ext.1 this, end def split_coequaliser_is_coequaliser {f g : A ⟶ B} (sc : split_coequaliser f g) : has_colimit (parallel_pair f g):= begin refine is_coeq_lemma sc.cf.π _ _, apply limits.cofork.condition, intros, refine ⟨⟨⟨sc.s ≫ c,_⟩⟩,_⟩, rw [← category.assoc, ← sc.p3, category.assoc, a, ← category.assoc, sc.p2, category.id_comp], rintros ⟨m2,p⟩, apply subtype.ext.2, change m2 = sc.s ≫ c, rw [p, ← category.assoc, sc.p1], dsimp, simp end -- [todo] sort out universe polymorphism variables {D : Type u} [𝒟 : category.{v} D] include 𝒟 /-- Take a G-split coequaliser `cf` for `f,g : A ⟶ B`, then we have a coequaliser for `f,g` and `G` of this coequaliser is still a colimit. -/ def creates_split_coequalisers (G : D ⥤ C) := Π {A B : D} (f g : A ⟶ B) (cf : split_coequaliser (G.map f) (G.map g)), Σ (hcl : has_colimit (parallel_pair f g)), is_colimit $ G.map_cocone hcl.cocone variables {J : Type v} [𝒥 : small_category J] include 𝒥 -- [todo] double check that mathlib doesn't have creates limits. def creates_limits (d : J ⥤ C) (F : C ⥤ D) := Π [fl : has_limit (d ⋙ F)], Σ (l : has_limit d), is_limit $ F.map_cone l.cone structure creates_limit (K : J ⥤ C) (F : C ⥤ D) (c : cone (K ⋙ F)) (t : is_limit c) := (upstairs : cone K) (up_hits : F.map_cone upstairs ≅ c) (any_up_is_lim : Π (up' : cone K) (iso : F.map_cone up' ≅ c), is_limit up') -- Π (c : cone (d ⋙ F)) (t : is_limit c), (Σ (t : cone d), F.map_cone t ≅ c) def creates_colimits (d : J ⥤ C) (F : C ⥤ D) := Π [fl : has_colimit (d ⋙ F)], Σ (l : has_colimit d), is_colimit $ F.map_cocone l.cocone open category_theory.monad open category_theory.monad.algebra variables {T : C ⥤ C} [monad T] omit 𝒟 -- def forget_really_creates_limits (d : J ⥤ algebra T) : @creates_limits (algebra T) _ C _ J _ d (monad.forget T : algebra T ⥤ C) := sorry -- def monadic_creates_colimits (d : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] : (preserves_colimits T) -- def precise_monadicity_1 (G : D ⥤ C) [is_right_adjoint G] : creates_split_coequalisers G → is_equivalence (monad.comparison G) := -- sorry -- def precise_monadicity_2 (G : D ⥤ C) [ra : is_right_adjoint G] : is_equivalence (monad.comparison G) → creates_split_coequalisers G:= -- begin -- let F := ra.1, -- rintros e A B f g ⟨cf, _⟩, -- refine ⟨_,_,_⟩, -- end end category_theory
45e330193cd05861d1b72e44c487d6efdd4adb24
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/topology/continuous_on.lean
217e3a6e9efa9cbdae859b5ba7b27f9cdad730bd
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
33,923
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.constructions /-! # Neighborhoods and continuity relative to a subset This file defines relative versions * `nhds_within` of `nhds` * `continuous_on` of `continuous` * `continuous_within_at` of `continuous_at` and proves their basic properties, including the relationships between these restricted notions and the corresponding notions for the subtype equipped with the subspace topology. ## Notation * `𝓝 x`: the filter of neighborhoods of a point `x`; * `𝓟 s`: the principal filter of a set `s`; * `𝓝[s] x`: the filter `nhds_within x s` of neighborhoods of a point `x` within a set `s`. -/ open set filter open_locale topological_space filter variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables [topological_space α] /-- The "neighborhood within" filter. Elements of `𝓝[s] a` are sets containing the intersection of `s` and a neighborhood of `a`. -/ def nhds_within (a : α) (s : set α) : filter α := 𝓝 a ⊓ 𝓟 s localized "notation `𝓝[` s `] ` x:100 := nhds_within x s" in topological_space @[simp] lemma nhds_bind_nhds_within {a : α} {s : set α} : (𝓝 a).bind (λ x, 𝓝[s] x) = 𝓝[s] a := bind_inf_principal.trans $ congr_arg2 _ nhds_bind_nhds rfl @[simp] lemma eventually_nhds_nhds_within {a : α} {s : set α} {p : α → Prop} : (∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝[s] y, p x) ↔ ∀ᶠ x in 𝓝[s] a, p x := filter.ext_iff.1 nhds_bind_nhds_within {x | p x} lemma eventually_nhds_within_iff {a : α} {s : set α} {p : α → Prop} : (∀ᶠ x in 𝓝[s] a, p x) ↔ ∀ᶠ x in 𝓝 a, x ∈ s → p x := eventually_inf_principal @[simp] lemma eventually_nhds_within_nhds_within {a : α} {s : set α} {p : α → Prop} : (∀ᶠ y in 𝓝[s] a, ∀ᶠ x in 𝓝[s] y, p x) ↔ ∀ᶠ x in 𝓝[s] a, p x := begin refine ⟨λ h, _, λ h, (eventually_nhds_nhds_within.2 h).filter_mono inf_le_left⟩, simp only [eventually_nhds_within_iff] at h ⊢, exact h.mono (λ x hx hxs, (hx hxs).self_of_nhds hxs) end theorem nhds_within_eq (a : α) (s : set α) : 𝓝[s] a = ⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, 𝓟 (t ∩ s) := ((nhds_basis_opens a).inf_principal s).eq_binfi theorem nhds_within_univ (a : α) : 𝓝[set.univ] a = 𝓝 a := by rw [nhds_within, principal_univ, inf_top_eq] lemma nhds_within_has_basis {p : β → Prop} {s : β → set α} {a : α} (h : (𝓝 a).has_basis p s) (t : set α) : (𝓝[t] a).has_basis p (λ i, s i ∩ t) := h.inf_principal t lemma nhds_within_basis_open (a : α) (t : set α) : (𝓝[t] a).has_basis (λ u, a ∈ u ∧ is_open u) (λ u, u ∩ t) := nhds_within_has_basis (nhds_basis_opens a) t theorem mem_nhds_within {t : set α} {a : α} {s : set α} : t ∈ 𝓝[s] a ↔ ∃ u, is_open u ∧ a ∈ u ∧ u ∩ s ⊆ t := by simpa only [exists_prop, and_assoc, and_comm] using (nhds_within_basis_open a s).mem_iff lemma mem_nhds_within_iff_exists_mem_nhds_inter {t : set α} {a : α} {s : set α} : t ∈ 𝓝[s] a ↔ ∃ u ∈ 𝓝 a, u ∩ s ⊆ t := (nhds_within_has_basis (𝓝 a).basis_sets s).mem_iff lemma diff_mem_nhds_within_compl {X : Type*} [topological_space X] {x : X} {s : set X} (hs : s ∈ 𝓝 x) (t : set X) : s \ t ∈ 𝓝[tᶜ] x := diff_mem_inf_principal_compl hs t lemma nhds_of_nhds_within_of_nhds {s t : set α} {a : α} (h1 : s ∈ 𝓝 a) (h2 : t ∈ 𝓝[s] a) : (t ∈ 𝓝 a) := begin rcases mem_nhds_within_iff_exists_mem_nhds_inter.mp h2 with ⟨_, Hw, hw⟩, exact (nhds a).sets_of_superset ((nhds a).inter_sets Hw h1) hw, end lemma mem_nhds_within_of_mem_nhds {s t : set α} {a : α} (h : s ∈ 𝓝 a) : s ∈ 𝓝[t] a := mem_inf_sets_of_left h theorem self_mem_nhds_within {a : α} {s : set α} : s ∈ 𝓝[s] a := mem_inf_sets_of_right (mem_principal_self s) theorem inter_mem_nhds_within (s : set α) {t : set α} {a : α} (h : t ∈ 𝓝 a) : s ∩ t ∈ 𝓝[s] a := inter_mem_sets (mem_inf_sets_of_right (mem_principal_self s)) (mem_inf_sets_of_left h) theorem nhds_within_mono (a : α) {s t : set α} (h : s ⊆ t) : 𝓝[s] a ≤ 𝓝[t] a := inf_le_inf_left _ (principal_mono.mpr h) lemma pure_le_nhds_within {a : α} {s : set α} (ha : a ∈ s) : pure a ≤ 𝓝[s] a := le_inf (pure_le_nhds a) (le_principal_iff.2 ha) lemma mem_of_mem_nhds_within {a : α} {s t : set α} (ha : a ∈ s) (ht : t ∈ 𝓝[s] a) : a ∈ t := pure_le_nhds_within ha ht lemma filter.eventually.self_of_nhds_within {p : α → Prop} {s : set α} {x : α} (h : ∀ᶠ y in 𝓝[s] x, p y) (hx : x ∈ s) : p x := mem_of_mem_nhds_within hx h lemma tendsto_const_nhds_within {l : filter β} {s : set α} {a : α} (ha : a ∈ s) : tendsto (λ x : β, a) l (𝓝[s] a) := tendsto_const_pure.mono_right $ pure_le_nhds_within ha theorem nhds_within_restrict'' {a : α} (s : set α) {t : set α} (h : t ∈ 𝓝[s] a) : 𝓝[s] a = 𝓝[s ∩ t] a := le_antisymm (le_inf inf_le_left (le_principal_iff.mpr (inter_mem_sets self_mem_nhds_within h))) (inf_le_inf_left _ (principal_mono.mpr (set.inter_subset_left _ _))) theorem nhds_within_restrict' {a : α} (s : set α) {t : set α} (h : t ∈ 𝓝 a) : 𝓝[s] a = 𝓝[s ∩ t] a := nhds_within_restrict'' s $ mem_inf_sets_of_left h theorem nhds_within_restrict {a : α} (s : set α) {t : set α} (h₀ : a ∈ t) (h₁ : is_open t) : 𝓝[s] a = 𝓝[s ∩ t] a := nhds_within_restrict' s (mem_nhds_sets h₁ h₀) theorem nhds_within_le_of_mem {a : α} {s t : set α} (h : s ∈ 𝓝[t] a) : 𝓝[t] a ≤ 𝓝[s] a := begin rcases mem_nhds_within.1 h with ⟨u, u_open, au, uts⟩, have : 𝓝[t] a = 𝓝[t ∩ u] a := nhds_within_restrict _ au u_open, rw [this, inter_comm], exact nhds_within_mono _ uts end theorem nhds_within_eq_nhds_within {a : α} {s t u : set α} (h₀ : a ∈ s) (h₁ : is_open s) (h₂ : t ∩ s = u ∩ s) : 𝓝[t] a = 𝓝[u] a := by rw [nhds_within_restrict t h₀ h₁, nhds_within_restrict u h₀ h₁, h₂] theorem nhds_within_eq_of_open {a : α} {s : set α} (h₀ : a ∈ s) (h₁ : is_open s) : 𝓝[s] a = 𝓝 a := inf_eq_left.2 $ le_principal_iff.2 $ mem_nhds_sets h₁ h₀ @[simp] theorem nhds_within_empty (a : α) : 𝓝[∅] a = ⊥ := by rw [nhds_within, principal_empty, inf_bot_eq] theorem nhds_within_union (a : α) (s t : set α) : 𝓝[s ∪ t] a = 𝓝[s] a ⊔ 𝓝[t] a := by { delta nhds_within, rw [←inf_sup_left, sup_principal] } theorem nhds_within_inter (a : α) (s t : set α) : 𝓝[s ∩ t] a = 𝓝[s] a ⊓ 𝓝[t] a := by { delta nhds_within, rw [inf_left_comm, inf_assoc, inf_principal, ←inf_assoc, inf_idem] } theorem nhds_within_inter' (a : α) (s t : set α) : 𝓝[s ∩ t] a = (𝓝[s] a) ⊓ 𝓟 t := by { delta nhds_within, rw [←inf_principal, inf_assoc] } lemma mem_nhds_within_insert {a : α} {s t : set α} (h : t ∈ 𝓝[s] a) : insert a t ∈ 𝓝[insert a s] a := begin rcases mem_nhds_within.1 h with ⟨o, o_open, ao, ho⟩, apply mem_nhds_within.2 ⟨o, o_open, ao, _⟩, assume y, simp only [and_imp, mem_inter_eq, mem_insert_iff], rintro yo (rfl | ys), { simp }, { simp [ho ⟨yo, ys⟩] } end lemma nhds_within_prod_eq {α : Type*} [topological_space α] {β : Type*} [topological_space β] (a : α) (b : β) (s : set α) (t : set β) : 𝓝[s.prod t] (a, b) = 𝓝[s] a ×ᶠ 𝓝[t] b := by { delta nhds_within, rw [nhds_prod_eq, ←filter.prod_inf_prod, filter.prod_principal_principal] } lemma nhds_within_prod {α : Type*} [topological_space α] {β : Type*} [topological_space β] {s u : set α} {t v : set β} {a : α} {b : β} (hu : u ∈ 𝓝[s] a) (hv : v ∈ 𝓝[t] b) : (u.prod v) ∈ 𝓝[s.prod t] (a, b) := by { rw nhds_within_prod_eq, exact prod_mem_prod hu hv, } theorem tendsto_if_nhds_within {f g : α → β} {p : α → Prop} [decidable_pred p] {a : α} {s : set α} {l : filter β} (h₀ : tendsto f (𝓝[s ∩ p] a) l) (h₁ : tendsto g (𝓝[s ∩ {x | ¬ p x}] a) l) : tendsto (λ x, if p x then f x else g x) (𝓝[s] a) l := by apply tendsto_if; rw [←nhds_within_inter']; assumption lemma map_nhds_within (f : α → β) (a : α) (s : set α) : map f (𝓝[s] a) = ⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, 𝓟 (set.image f (t ∩ s)) := ((nhds_within_basis_open a s).map f).eq_binfi theorem tendsto_nhds_within_mono_left {f : α → β} {a : α} {s t : set α} {l : filter β} (hst : s ⊆ t) (h : tendsto f (𝓝[t] a) l) : tendsto f (𝓝[s] a) l := h.mono_left $ nhds_within_mono a hst theorem tendsto_nhds_within_mono_right {f : β → α} {l : filter β} {a : α} {s t : set α} (hst : s ⊆ t) (h : tendsto f l (𝓝[s] a)) : tendsto f l (𝓝[t] a) := h.mono_right (nhds_within_mono a hst) theorem tendsto_nhds_within_of_tendsto_nhds {f : α → β} {a : α} {s : set α} {l : filter β} (h : tendsto f (𝓝 a) l) : tendsto f (𝓝[s] a) l := h.mono_left inf_le_left theorem principal_subtype {α : Type*} (s : set α) (t : set {x // x ∈ s}) : 𝓟 t = comap coe (𝓟 ((coe : s → α) '' t)) := by rw [comap_principal, set.preimage_image_eq _ subtype.coe_injective] lemma mem_closure_iff_nhds_within_ne_bot {s : set α} {x : α} : x ∈ closure s ↔ ne_bot (𝓝[s] x) := mem_closure_iff_cluster_pt lemma nhds_within_ne_bot_of_mem {s : set α} {x : α} (hx : x ∈ s) : ne_bot (𝓝[s] x) := mem_closure_iff_nhds_within_ne_bot.1 $ subset_closure hx lemma is_closed.mem_of_nhds_within_ne_bot {s : set α} (hs : is_closed s) {x : α} (hx : ne_bot $ 𝓝[s] x) : x ∈ s := by simpa only [hs.closure_eq] using mem_closure_iff_nhds_within_ne_bot.2 hx lemma eventually_eq_nhds_within_iff {f g : α → β} {s : set α} {a : α} : (f =ᶠ[𝓝[s] a] g) ↔ ∀ᶠ x in 𝓝 a, x ∈ s → f x = g x := mem_inf_principal lemma eventually_eq_nhds_within_of_eq_on {f g : α → β} {s : set α} {a : α} (h : eq_on f g s) : f =ᶠ[𝓝[s] a] g := mem_inf_sets_of_right h lemma set.eq_on.eventually_eq_nhds_within {f g : α → β} {s : set α} {a : α} (h : eq_on f g s) : f =ᶠ[𝓝[s] a] g := eventually_eq_nhds_within_of_eq_on h lemma tendsto_nhds_within_congr {f g : α → β} {s : set α} {a : α} {l : filter β} (hfg : ∀ x ∈ s, f x = g x) (hf : tendsto f (𝓝[s] a) l) : tendsto g (𝓝[s] a) l := (tendsto_congr' $ eventually_eq_nhds_within_of_eq_on hfg).1 hf lemma eventually_nhds_with_of_forall {s : set α} {a : α} {p : α → Prop} (h : ∀ x ∈ s, p x) : ∀ᶠ x in 𝓝[s] a, p x := mem_inf_sets_of_right h lemma tendsto_nhds_within_of_tendsto_nhds_of_eventually_within {β : Type*} {a : α} {l : filter β} {s : set α} (f : β → α) (h1 : tendsto f l (nhds a)) (h2 : ∀ᶠ x in l, f x ∈ s) : tendsto f l (𝓝[s] a) := tendsto_inf.2 ⟨h1, tendsto_principal.2 h2⟩ lemma filter.eventually_eq.eq_of_nhds_within {s : set α} {f g : α → β} {a : α} (h : f =ᶠ[𝓝[s] a] g) (hmem : a ∈ s) : f a = g a := h.self_of_nhds_within hmem lemma eventually_nhds_within_of_eventually_nhds {α : Type*} [topological_space α] {s : set α} {a : α} {p : α → Prop} (h : ∀ᶠ x in 𝓝 a, p x) : ∀ᶠ x in 𝓝[s] a, p x := mem_nhds_within_of_mem_nhds h /- nhds_within and subtypes -/ theorem mem_nhds_within_subtype {s : set α} {a : {x // x ∈ s}} {t u : set {x // x ∈ s}} : t ∈ 𝓝[u] a ↔ t ∈ comap (coe : s → α) (𝓝[coe '' u] a) := by rw [nhds_within, nhds_subtype, principal_subtype, ←comap_inf, ←nhds_within] theorem nhds_within_subtype (s : set α) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) : 𝓝[t] a = comap (coe : s → α) (𝓝[coe '' t] a) := filter.ext $ λ u, mem_nhds_within_subtype theorem nhds_within_eq_map_subtype_coe {s : set α} {a : α} (h : a ∈ s) : 𝓝[s] a = map (coe : s → α) (𝓝 ⟨a, h⟩) := have h₀ : s ∈ 𝓝[s] a, by { rw [mem_nhds_within], existsi set.univ, simp [set.diff_eq] }, have h₁ : ∀ y ∈ s, ∃ x : s, ↑x = y, from λ y h, ⟨⟨y, h⟩, rfl⟩, begin conv_rhs { rw [← nhds_within_univ, nhds_within_subtype, subtype.coe_image_univ] }, exact (map_comap_of_surjective' h₀ h₁).symm, end theorem tendsto_nhds_within_iff_subtype {s : set α} {a : α} (h : a ∈ s) (f : α → β) (l : filter β) : tendsto f (𝓝[s] a) l ↔ tendsto (s.restrict f) (𝓝 ⟨a, h⟩) l := by simp only [tendsto, nhds_within_eq_map_subtype_coe h, filter.map_map, restrict] variables [topological_space β] [topological_space γ] [topological_space δ] /-- A function between topological spaces is continuous at a point `x₀` within a subset `s` if `f x` tends to `f x₀` when `x` tends to `x₀` while staying within `s`. -/ def continuous_within_at (f : α → β) (s : set α) (x : α) : Prop := tendsto f (𝓝[s] x) (𝓝 (f x)) /-- If a function is continuous within `s` at `x`, then it tends to `f x` within `s` by definition. We register this fact for use with the dot notation, especially to use `tendsto.comp` as `continuous_within_at.comp` will have a different meaning. -/ lemma continuous_within_at.tendsto {f : α → β} {s : set α} {x : α} (h : continuous_within_at f s x) : tendsto f (𝓝[s] x) (𝓝 (f x)) := h /-- A function between topological spaces is continuous on a subset `s` when it's continuous at every point of `s` within `s`. -/ def continuous_on (f : α → β) (s : set α) : Prop := ∀ x ∈ s, continuous_within_at f s x lemma continuous_on.continuous_within_at {f : α → β} {s : set α} {x : α} (hf : continuous_on f s) (hx : x ∈ s) : continuous_within_at f s x := hf x hx theorem continuous_within_at_univ (f : α → β) (x : α) : continuous_within_at f set.univ x ↔ continuous_at f x := by rw [continuous_at, continuous_within_at, nhds_within_univ] theorem continuous_within_at_iff_continuous_at_restrict (f : α → β) {x : α} {s : set α} (h : x ∈ s) : continuous_within_at f s x ↔ continuous_at (s.restrict f) ⟨x, h⟩ := tendsto_nhds_within_iff_subtype h f _ theorem continuous_within_at.tendsto_nhds_within_image {f : α → β} {x : α} {s : set α} (h : continuous_within_at f s x) : tendsto f (𝓝[s] x) (𝓝[f '' s] (f x)) := tendsto_inf.2 ⟨h, tendsto_principal.2 $ mem_inf_sets_of_right $ mem_principal_sets.2 $ λ x, mem_image_of_mem _⟩ lemma continuous_within_at.prod_map {f : α → γ} {g : β → δ} {s : set α} {t : set β} {x : α} {y : β} (hf : continuous_within_at f s x) (hg : continuous_within_at g t y) : continuous_within_at (prod.map f g) (s.prod t) (x, y) := begin unfold continuous_within_at at *, rw [nhds_within_prod_eq, prod.map, nhds_prod_eq], exact hf.prod_map hg, end theorem continuous_on_iff {f : α → β} {s : set α} : continuous_on f s ↔ ∀ x ∈ s, ∀ t : set β, is_open t → f x ∈ t → ∃ u, is_open u ∧ x ∈ u ∧ u ∩ s ⊆ f ⁻¹' t := by simp only [continuous_on, continuous_within_at, tendsto_nhds, mem_nhds_within] theorem continuous_on_iff_continuous_restrict {f : α → β} {s : set α} : continuous_on f s ↔ continuous (s.restrict f) := begin rw [continuous_on, continuous_iff_continuous_at], split, { rintros h ⟨x, xs⟩, exact (continuous_within_at_iff_continuous_at_restrict f xs).mp (h x xs) }, intros h x xs, exact (continuous_within_at_iff_continuous_at_restrict f xs).mpr (h ⟨x, xs⟩) end theorem continuous_on_iff' {f : α → β} {s : set α} : continuous_on f s ↔ ∀ t : set β, is_open t → ∃ u, is_open u ∧ f ⁻¹' t ∩ s = u ∩ s := have ∀ t, is_open (s.restrict f ⁻¹' t) ↔ ∃ (u : set α), is_open u ∧ f ⁻¹' t ∩ s = u ∩ s, begin intro t, rw [is_open_induced_iff, set.restrict_eq, set.preimage_comp], simp only [subtype.preimage_coe_eq_preimage_coe_iff], split; { rintros ⟨u, ou, useq⟩, exact ⟨u, ou, useq.symm⟩ } end, by rw [continuous_on_iff_continuous_restrict, continuous]; simp only [this] theorem continuous_on_iff_is_closed {f : α → β} {s : set α} : continuous_on f s ↔ ∀ t : set β, is_closed t → ∃ u, is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s := have ∀ t, is_closed (s.restrict f ⁻¹' t) ↔ ∃ (u : set α), is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s, begin intro t, rw [is_closed_induced_iff, set.restrict_eq, set.preimage_comp], simp only [subtype.preimage_coe_eq_preimage_coe_iff] end, by rw [continuous_on_iff_continuous_restrict, continuous_iff_is_closed]; simp only [this] lemma continuous_on.prod_map {f : α → γ} {g : β → δ} {s : set α} {t : set β} (hf : continuous_on f s) (hg : continuous_on g t) : continuous_on (prod.map f g) (s.prod t) := λ ⟨x, y⟩ ⟨hx, hy⟩, continuous_within_at.prod_map (hf x hx) (hg y hy) lemma continuous_on_empty (f : α → β) : continuous_on f ∅ := λ x, false.elim theorem nhds_within_le_comap {x : α} {s : set α} {f : α → β} (ctsf : continuous_within_at f s x) : 𝓝[s] x ≤ comap f (𝓝[f '' s] (f x)) := map_le_iff_le_comap.1 ctsf.tendsto_nhds_within_image theorem continuous_within_at_iff_ptendsto_res (f : α → β) {x : α} {s : set α} : continuous_within_at f s x ↔ ptendsto (pfun.res f s) (𝓝 x) (𝓝 (f x)) := tendsto_iff_ptendsto _ _ _ _ lemma continuous_iff_continuous_on_univ {f : α → β} : continuous f ↔ continuous_on f univ := by simp [continuous_iff_continuous_at, continuous_on, continuous_at, continuous_within_at, nhds_within_univ] lemma continuous_within_at.mono {f : α → β} {s t : set α} {x : α} (h : continuous_within_at f t x) (hs : s ⊆ t) : continuous_within_at f s x := h.mono_left (nhds_within_mono x hs) lemma continuous_within_at_inter' {f : α → β} {s t : set α} {x : α} (h : t ∈ 𝓝[s] x) : continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x := by simp [continuous_within_at, nhds_within_restrict'' s h] lemma continuous_within_at_inter {f : α → β} {s t : set α} {x : α} (h : t ∈ 𝓝 x) : continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x := by simp [continuous_within_at, nhds_within_restrict' s h] lemma continuous_within_at.union {f : α → β} {s t : set α} {x : α} (hs : continuous_within_at f s x) (ht : continuous_within_at f t x) : continuous_within_at f (s ∪ t) x := by simp only [continuous_within_at, nhds_within_union, tendsto, map_sup, sup_le_iff.2 ⟨hs, ht⟩] lemma continuous_within_at.mem_closure_image {f : α → β} {s : set α} {x : α} (h : continuous_within_at f s x) (hx : x ∈ closure s) : f x ∈ closure (f '' s) := by haveI := (mem_closure_iff_nhds_within_ne_bot.1 hx); exact (mem_closure_of_tendsto h $ mem_sets_of_superset self_mem_nhds_within (subset_preimage_image f s)) lemma continuous_within_at.mem_closure {f : α → β} {s : set α} {x : α} {A : set β} (h : continuous_within_at f s x) (hx : x ∈ closure s) (hA : s ⊆ f⁻¹' A) : f x ∈ closure A := closure_mono (image_subset_iff.2 hA) (h.mem_closure_image hx) lemma continuous_within_at.image_closure {f : α → β} {s : set α} (hf : ∀ x ∈ closure s, continuous_within_at f s x) : f '' (closure s) ⊆ closure (f '' s) := begin rintros _ ⟨x, hx, rfl⟩, exact (hf x hx).mem_closure_image hx end theorem is_open_map.continuous_on_image_of_left_inv_on {f : α → β} {s : set α} (h : is_open_map (s.restrict f)) {finv : β → α} (hleft : left_inv_on finv f s) : continuous_on finv (f '' s) := begin rintros _ ⟨x, xs, rfl⟩ t ht, rw [hleft xs] at ht, replace h := h.nhds_le ⟨x, xs⟩, apply mem_nhds_within_of_mem_nhds, apply h, erw [map_compose.symm, function.comp, mem_map, ← nhds_within_eq_map_subtype_coe], apply mem_sets_of_superset (inter_mem_nhds_within _ ht), assume y hy, rw [mem_set_of_eq, mem_preimage, hleft hy.1], exact hy.2 end theorem is_open_map.continuous_on_range_of_left_inverse {f : α → β} (hf : is_open_map f) {finv : β → α} (hleft : function.left_inverse finv f) : continuous_on finv (range f) := begin rw [← image_univ], exact (hf.restrict is_open_univ).continuous_on_image_of_left_inv_on (λ x _, hleft x) end lemma continuous_on.congr_mono {f g : α → β} {s s₁ : set α} (h : continuous_on f s) (h' : eq_on g f s₁) (h₁ : s₁ ⊆ s) : continuous_on g s₁ := begin assume x hx, unfold continuous_within_at, have A := (h x (h₁ hx)).mono h₁, unfold continuous_within_at at A, rw ← h' hx at A, exact A.congr' h'.eventually_eq_nhds_within.symm end lemma continuous_on.congr {f g : α → β} {s : set α} (h : continuous_on f s) (h' : eq_on g f s) : continuous_on g s := h.congr_mono h' (subset.refl _) lemma continuous_on_congr {f g : α → β} {s : set α} (h' : eq_on g f s) : continuous_on g s ↔ continuous_on f s := ⟨λ h, continuous_on.congr h h'.symm, λ h, h.congr h'⟩ lemma continuous_at.continuous_within_at {f : α → β} {s : set α} {x : α} (h : continuous_at f x) : continuous_within_at f s x := continuous_within_at.mono ((continuous_within_at_univ f x).2 h) (subset_univ _) lemma continuous_within_at.continuous_at {f : α → β} {s : set α} {x : α} (h : continuous_within_at f s x) (hs : s ∈ 𝓝 x) : continuous_at f x := begin have : s = univ ∩ s, by rw univ_inter, rwa [this, continuous_within_at_inter hs, continuous_within_at_univ] at h end lemma continuous_on.continuous_at {f : α → β} {s : set α} {x : α} (h : continuous_on f s) (hx : s ∈ 𝓝 x) : continuous_at f x := (h x (mem_of_nhds hx)).continuous_at hx lemma continuous_within_at.comp {g : β → γ} {f : α → β} {s : set α} {t : set β} {x : α} (hg : continuous_within_at g t (f x)) (hf : continuous_within_at f s x) (h : s ⊆ f ⁻¹' t) : continuous_within_at (g ∘ f) s x := begin have : tendsto f (𝓟 s) (𝓟 t), by { rw tendsto_principal_principal, exact λx hx, h hx }, have : tendsto f (𝓝[s] x) (𝓟 t) := this.mono_left inf_le_right, have : tendsto f (𝓝[s] x) (𝓝[t] (f x)) := tendsto_inf.2 ⟨hf, this⟩, exact tendsto.comp hg this end lemma continuous_within_at.comp' {g : β → γ} {f : α → β} {s : set α} {t : set β} {x : α} (hg : continuous_within_at g t (f x)) (hf : continuous_within_at f s x) : continuous_within_at (g ∘ f) (s ∩ f⁻¹' t) x := hg.comp (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) lemma continuous_on.comp {g : β → γ} {f : α → β} {s : set α} {t : set β} (hg : continuous_on g t) (hf : continuous_on f s) (h : s ⊆ f ⁻¹' t) : continuous_on (g ∘ f) s := λx hx, continuous_within_at.comp (hg _ (h hx)) (hf x hx) h lemma continuous_on.mono {f : α → β} {s t : set α} (hf : continuous_on f s) (h : t ⊆ s) : continuous_on f t := λx hx, (hf x (h hx)).mono_left (nhds_within_mono _ h) lemma continuous_on.comp' {g : β → γ} {f : α → β} {s : set α} {t : set β} (hg : continuous_on g t) (hf : continuous_on f s) : continuous_on (g ∘ f) (s ∩ f⁻¹' t) := hg.comp (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) lemma continuous.continuous_on {f : α → β} {s : set α} (h : continuous f) : continuous_on f s := begin rw continuous_iff_continuous_on_univ at h, exact h.mono (subset_univ _) end lemma continuous.continuous_within_at {f : α → β} {s : set α} {x : α} (h : continuous f) : continuous_within_at f s x := h.continuous_at.continuous_within_at lemma continuous.comp_continuous_on {g : β → γ} {f : α → β} {s : set α} (hg : continuous g) (hf : continuous_on f s) : continuous_on (g ∘ f) s := hg.continuous_on.comp hf subset_preimage_univ lemma continuous_on.comp_continuous {g : β → γ} {f : α → β} {s : set β} (hg : continuous_on g s) (hf : continuous f) (hfg : range f ⊆ s) : continuous (g ∘ f) := begin rw continuous_iff_continuous_on_univ at *, apply hg.comp hf, rwa [← image_subset_iff, image_univ] end lemma continuous_within_at.preimage_mem_nhds_within {f : α → β} {x : α} {s : set α} {t : set β} (h : continuous_within_at f s x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝[s] x := h ht lemma continuous_within_at.preimage_mem_nhds_within' {f : α → β} {x : α} {s : set α} {t : set β} (h : continuous_within_at f s x) (ht : t ∈ 𝓝[f '' s] (f x)) : f ⁻¹' t ∈ 𝓝[s] x := begin rw mem_nhds_within at ht, rcases ht with ⟨u, u_open, fxu, hu⟩, have : f ⁻¹' u ∩ s ∈ 𝓝[s] x := filter.inter_mem_sets (h (mem_nhds_sets u_open fxu)) self_mem_nhds_within, apply mem_sets_of_superset this, calc f ⁻¹' u ∩ s ⊆ f ⁻¹' u ∩ f ⁻¹' (f '' s) : inter_subset_inter_right _ (subset_preimage_image f s) ... = f ⁻¹' (u ∩ f '' s) : rfl ... ⊆ f ⁻¹' t : preimage_mono hu end lemma continuous_within_at.congr_of_eventually_eq {f f₁ : α → β} {s : set α} {x : α} (h : continuous_within_at f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : continuous_within_at f₁ s x := by rwa [continuous_within_at, filter.tendsto, hx, filter.map_congr h₁] lemma continuous_within_at.congr {f f₁ : α → β} {s : set α} {x : α} (h : continuous_within_at f s x) (h₁ : ∀y∈s, f₁ y = f y) (hx : f₁ x = f x) : continuous_within_at f₁ s x := h.congr_of_eventually_eq (mem_sets_of_superset self_mem_nhds_within h₁) hx lemma continuous_within_at.congr_mono {f g : α → β} {s s₁ : set α} {x : α} (h : continuous_within_at f s x) (h' : eq_on g f s₁) (h₁ : s₁ ⊆ s) (hx : g x = f x): continuous_within_at g s₁ x := (h.mono h₁).congr h' hx lemma continuous_on_const {s : set α} {c : β} : continuous_on (λx, c) s := continuous_const.continuous_on lemma continuous_within_at_const {b : β} {s : set α} {x : α} : continuous_within_at (λ _:α, b) s x := continuous_const.continuous_within_at lemma continuous_on_id {s : set α} : continuous_on id s := continuous_id.continuous_on lemma continuous_within_at_id {s : set α} {x : α} : continuous_within_at id s x := continuous_id.continuous_within_at lemma continuous_on_open_iff {f : α → β} {s : set α} (hs : is_open s) : continuous_on f s ↔ (∀t, is_open t → is_open (s ∩ f⁻¹' t)) := begin rw continuous_on_iff', split, { assume h t ht, rcases h t ht with ⟨u, u_open, hu⟩, rw [inter_comm, hu], apply is_open_inter u_open hs }, { assume h t ht, refine ⟨s ∩ f ⁻¹' t, h t ht, _⟩, rw [@inter_comm _ s (f ⁻¹' t), inter_assoc, inter_self] } end lemma continuous_on.preimage_open_of_open {f : α → β} {s : set α} {t : set β} (hf : continuous_on f s) (hs : is_open s) (ht : is_open t) : is_open (s ∩ f⁻¹' t) := (continuous_on_open_iff hs).1 hf t ht lemma continuous_on.preimage_closed_of_closed {f : α → β} {s : set α} {t : set β} (hf : continuous_on f s) (hs : is_closed s) (ht : is_closed t) : is_closed (s ∩ f⁻¹' t) := begin rcases continuous_on_iff_is_closed.1 hf t ht with ⟨u, hu⟩, rw [inter_comm, hu.2], apply is_closed_inter hu.1 hs end lemma continuous_on.preimage_interior_subset_interior_preimage {f : α → β} {s : set α} {t : set β} (hf : continuous_on f s) (hs : is_open s) : s ∩ f⁻¹' (interior t) ⊆ s ∩ interior (f⁻¹' t) := calc s ∩ f ⁻¹' (interior t) ⊆ interior (s ∩ f ⁻¹' t) : interior_maximal (inter_subset_inter (subset.refl _) (preimage_mono interior_subset)) (hf.preimage_open_of_open hs is_open_interior) ... = s ∩ interior (f ⁻¹' t) : by rw [interior_inter, hs.interior_eq] lemma continuous_on_of_locally_continuous_on {f : α → β} {s : set α} (h : ∀x∈s, ∃t, is_open t ∧ x ∈ t ∧ continuous_on f (s ∩ t)) : continuous_on f s := begin assume x xs, rcases h x xs with ⟨t, open_t, xt, ct⟩, have := ct x ⟨xs, xt⟩, rwa [continuous_within_at, ← nhds_within_restrict _ xt open_t] at this end lemma continuous_on_open_of_generate_from {β : Type*} {s : set α} {T : set (set β)} {f : α → β} (hs : is_open s) (h : ∀t ∈ T, is_open (s ∩ f⁻¹' t)) : @continuous_on α β _ (topological_space.generate_from T) f s := begin rw continuous_on_open_iff, assume t ht, induction ht with u hu u v Tu Tv hu hv U hU hU', { exact h u hu }, { simp only [preimage_univ, inter_univ], exact hs }, { have : s ∩ f ⁻¹' (u ∩ v) = (s ∩ f ⁻¹' u) ∩ (s ∩ f ⁻¹' v), by { ext x, simp, split, finish, finish }, rw this, exact is_open_inter hu hv }, { rw [preimage_sUnion, inter_bUnion], exact is_open_bUnion hU' }, { exact hs } end lemma continuous_within_at.prod {f : α → β} {g : α → γ} {s : set α} {x : α} (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (λx, (f x, g x)) s x := hf.prod_mk_nhds hg lemma continuous_on.prod {f : α → β} {g : α → γ} {s : set α} (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λx, (f x, g x)) s := λx hx, continuous_within_at.prod (hf x hx) (hg x hx) lemma inducing.continuous_on_iff {f : α → β} {g : β → γ} (hg : inducing g) {s : set α} : continuous_on f s ↔ continuous_on (g ∘ f) s := begin simp only [continuous_on_iff_continuous_restrict, restrict_eq], conv_rhs { rw [function.comp.assoc, ← (inducing.continuous_iff hg)] }, end lemma embedding.continuous_on_iff {f : α → β} {g : β → γ} (hg : embedding g) {s : set α} : continuous_on f s ↔ continuous_on (g ∘ f) s := inducing.continuous_on_iff hg.1 lemma continuous_within_at_of_not_mem_closure {f : α → β} {s : set α} {x : α} : x ∉ closure s → continuous_within_at f s x := begin intros hx, rw [mem_closure_iff_nhds_within_ne_bot, ne_bot, not_not] at hx, rw [continuous_within_at, hx], exact tendsto_bot, end lemma continuous_on_if' {s : set α} {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)} (hpf : ∀ a ∈ s ∩ frontier {a | p a}, tendsto f (nhds_within a $ s ∩ {a | p a}) (𝓝 $ if p a then f a else g a)) (hpg : ∀ a ∈ s ∩ frontier {a | p a}, tendsto g (nhds_within a $ s ∩ {a | ¬p a}) (𝓝 $ if p a then f a else g a)) (hf : continuous_on f $ s ∩ {a | p a}) (hg : continuous_on g $ s ∩ {a | ¬p a}) : continuous_on (λ a, if p a then f a else g a) s := begin set A := {a | p a}, set B := {a | ¬p a}, rw [← (inter_univ s), ← union_compl_self A], intros x hx, by_cases hx' : x ∈ frontier A, { have hx'' : x ∈ s ∩ frontier A, from ⟨hx.1, hx'⟩, rw inter_union_distrib_left, apply continuous_within_at.union, { apply tendsto_nhds_within_congr, { exact λ y ⟨hys, hyA⟩, (piecewise_eq_of_mem _ _ _ hyA).symm }, { apply_assumption, exact hx'' } }, { apply tendsto_nhds_within_congr, { exact λ y ⟨hys, hyA⟩, (piecewise_eq_of_not_mem _ _ _ hyA).symm }, { apply_assumption, exact hx'' } } }, { rw inter_union_distrib_left at ⊢ hx, cases hx, { apply continuous_within_at.union, { exact (hf x hx).congr (λ y hy, piecewise_eq_of_mem _ _ _ hy.2) (piecewise_eq_of_mem _ _ _ hx.2), }, { rw ← frontier_compl at hx', have : x ∉ closure Aᶜ, from λ h, hx' ⟨h, (λ (h' : x ∈ interior Aᶜ), interior_subset h' hx.2)⟩, exact continuous_within_at_of_not_mem_closure (λ h, this (closure_inter_subset_inter_closure _ _ h).2) } }, { apply continuous_within_at.union, { have : x ∉ closure A, from (λ h, hx' ⟨h, (λ (h' : x ∈ interior A), hx.2 (interior_subset h'))⟩), exact continuous_within_at_of_not_mem_closure (λ h, this (closure_inter_subset_inter_closure _ _ h).2) }, { exact (hg x hx).congr (λ y hy, piecewise_eq_of_not_mem _ _ _ hy.2) (piecewise_eq_of_not_mem _ _ _ hx.2) } } } end lemma continuous_on_if {α β : Type*} [topological_space α] [topological_space β] {p : α → Prop} {h : ∀a, decidable (p a)} {s : set α} {f g : α → β} (hp : ∀ a ∈ s ∩ frontier {a | p a}, f a = g a) (hf : continuous_on f $ s ∩ closure {a | p a}) (hg : continuous_on g $ s ∩ closure {a | ¬ p a}) : continuous_on (λa, if p a then f a else g a) s := begin apply continuous_on_if', { rintros a ha, simp only [← hp a ha, if_t_t], apply tendsto_nhds_within_mono_left (inter_subset_inter_right s subset_closure), exact (hf a ⟨ha.1, ha.2.1⟩).tendsto }, { rintros a ha, simp only [hp a ha, if_t_t], apply tendsto_nhds_within_mono_left (inter_subset_inter_right s subset_closure), rcases ha with ⟨has, ⟨_, ha⟩⟩, rw [← mem_compl_iff, ← closure_compl] at ha, apply (hg a ⟨has, ha⟩).tendsto, }, { exact hf.mono (inter_subset_inter_right s subset_closure) }, { exact hg.mono (inter_subset_inter_right s subset_closure) } end lemma continuous_if' {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)} (hpf : ∀ a ∈ frontier {x | p x}, tendsto f (nhds_within a {x | p x}) (𝓝 $ ite (p a) (f a) (g a))) (hpg : ∀ a ∈ frontier {x | p x}, tendsto g (nhds_within a {x | ¬p x}) (𝓝 $ ite (p a) (f a) (g a))) (hf : continuous_on f {x | p x}) (hg : continuous_on g {x | ¬p x}) : continuous (λ a, ite (p a) (f a) (g a)) := begin rw continuous_iff_continuous_on_univ, apply continuous_on_if'; simp; assumption end lemma continuous_on_fst {s : set (α × β)} : continuous_on prod.fst s := continuous_fst.continuous_on lemma continuous_within_at_fst {s : set (α × β)} {p : α × β} : continuous_within_at prod.fst s p := continuous_fst.continuous_within_at lemma continuous_on_snd {s : set (α × β)} : continuous_on prod.snd s := continuous_snd.continuous_on lemma continuous_within_at_snd {s : set (α × β)} {p : α × β} : continuous_within_at prod.snd s p := continuous_snd.continuous_within_at
2b73b772553cc37cc4f1ed80f82cd3098dd311d7
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/logic/embedding.lean
2abfaf94bd77a0bdd159627c50a9b79df77717d6
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
10,248
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 data.equiv.basic import data.sigma.basic /-! # Injective functions -/ universes u v w x namespace function /-- `α ↪ β` is a bundled injective function. -/ structure embedding (α : Sort*) (β : Sort*) := (to_fun : α → β) (inj' : injective to_fun) infixr ` ↪ `:25 := embedding instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩ end function /-- Convert an `α ≃ β` to `α ↪ β`. -/ protected def equiv.to_embedding {α : Sort u} {β : Sort v} (f : α ≃ β) : α ↪ β := ⟨f, f.injective⟩ @[simp] theorem equiv.to_embedding_coe_fn {α : Sort u} {β : Sort v} (f : α ≃ β) : (f.to_embedding : α → β) = f := rfl namespace function namespace embedding @[ext] lemma ext {α β} {f g : embedding α β} (h : ∀ x, f x = g x) : f = g := by cases f; cases g; simpa using funext h lemma ext_iff {α β} {f g : embedding α β} : (∀ x, f x = g x) ↔ f = g := ⟨ext, λ h _, by rw h⟩ @[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl @[simp] theorem coe_fn_mk {α β} (f : α → β) (i) : (@mk _ _ f i : α → β) = f := rfl theorem injective {α β} (f : α ↪ β) : injective f := f.inj' @[refl] protected def refl (α : Sort*) : α ↪ α := ⟨id, injective_id⟩ @[trans] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ := ⟨g ∘ f, g.injective.comp f.injective⟩ @[simp] theorem refl_apply {α} (x : α) : embedding.refl α x = x := rfl @[simp] theorem trans_apply {α β γ} (f : α ↪ β) (g : β ↪ γ) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] lemma equiv_to_embedding_trans_symm_to_embedding {α β : Sort*} (e : α ≃ β) : function.embedding.trans (e.to_embedding) (e.symm.to_embedding) = function.embedding.refl _ := by { ext, simp, } @[simp] lemma equiv_symm_to_embedding_trans_to_embedding {α β : Sort*} (e : α ≃ β) : function.embedding.trans (e.symm.to_embedding) (e.to_embedding) = function.embedding.refl _ := by { ext, simp, } protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x} (e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) := (equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding) /-- A right inverse `surj_inv` of a surjective function as an `embedding`. -/ protected noncomputable def of_surjective {α β} (f : β → α) (hf : surjective f) : α ↪ β := ⟨surj_inv hf, injective_surj_inv _⟩ /-- Convert a surjective `embedding` to an `equiv` -/ protected noncomputable def equiv_of_surjective {α β} (f : α ↪ β) (hf : surjective f) : α ≃ β := equiv.of_bijective f ⟨f.injective, hf⟩ protected def of_not_nonempty {α β} (hα : ¬ nonempty α) : α ↪ β := ⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩ /-- Change the value of an embedding `f` at one point. If the prescribed image is already occupied by some `f a'`, then swap the values at these two points. -/ def set_value {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : α ↪ β := ⟨λ a', if a' = a then b else if f a' = b then f a else f a', begin intros x y h, dsimp at h, split_ifs at h; try { substI b }; try { simp only [f.injective.eq_iff] at * }; cc end⟩ theorem set_value_eq {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : set_value f a b a = b := by simp [set_value] /-- Embedding into `option` -/ protected def some {α} : α ↪ option α := ⟨some, option.some_injective α⟩ /-- Embedding of a `subtype`. -/ def subtype {α} (p : α → Prop) : subtype p ↪ α := ⟨coe, λ _ _, subtype.ext_val⟩ @[simp] lemma coe_subtype {α} (p : α → Prop) : ⇑(subtype p) = coe := rfl /-- Choosing an element `b : β` gives an embedding of `punit` into `β`. -/ def punit {β : Sort*} (b : β) : punit ↪ β := ⟨λ _, b, by { rintros ⟨⟩ ⟨⟩ _, refl, }⟩ /-- Fixing an element `b : β` gives an embedding `α ↪ α × β`. -/ def sectl (α : Sort*) {β : Sort*} (b : β) : α ↪ α × β := ⟨λ a, (a, b), λ a a' h, congr_arg prod.fst h⟩ /-- Fixing an element `a : α` gives an embedding `β ↪ α × β`. -/ def sectr {α : Sort*} (a : α) (β : Sort*): β ↪ α × β := ⟨λ b, (a, b), λ b b' h, congr_arg prod.snd h⟩ /-- Restrict the codomain of an embedding. -/ def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p := ⟨λ a, ⟨f a, H a⟩, λ a b h, f.injective (@congr_arg _ _ _ _ subtype.val h)⟩ @[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- If `e₁` and `e₂` are embeddings, then so is `prod.map e₁ e₂ : (a, b) ↦ (e₁ a, e₂ b)`. -/ def prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ := ⟨prod.map e₁ e₂, e₁.injective.prod_map e₂.injective⟩ @[simp] lemma coe_prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(e₁.prod_map e₂) = prod.map e₁ e₂ := rfl section sum open sum /-- If `e₁` and `e₂` are embeddings, then so is `sum.map e₁ e₂`. -/ def sum_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ := ⟨sum.map e₁ e₂, assume s₁ s₂ h, match s₁, s₂, h with | inl a₁, inl a₂, h := congr_arg inl $ e₁.injective $ inl.inj h | inr b₁, inr b₂, h := congr_arg inr $ e₂.injective $ inr.inj h end⟩ @[simp] theorem coe_sum_map {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(sum_map e₁ e₂) = sum.map e₁ e₂ := rfl /-- The embedding of `α` into the sum `α ⊕ β`. -/ def inl {α β : Type*} : α ↪ α ⊕ β := ⟨sum.inl, λ a b, sum.inl.inj⟩ /-- The embedding of `β` into the sum `α ⊕ β`. -/ def inr {α β : Type*} : β ↪ α ⊕ β := ⟨sum.inr, λ a b, sum.inr.inj⟩ end sum section sigma variables {α α' : Type*} {β : α → Type*} {β' : α' → Type*} /-- `sigma.mk` as an `function.embedding`. -/ def sigma_mk (a : α) : β a ↪ Σ x, β x := ⟨sigma.mk a, sigma_mk_injective⟩ @[simp] lemma coe_sigma_mk (a : α) : (sigma_mk a : β a → Σ x, β x) = sigma.mk a := rfl /-- If `f : α ↪ α'` is an embedding and `g : Π a, β α ↪ β' (f α)` is a family of embeddings, then `sigma.map f g` is an embedding. -/ def sigma_map (f : α ↪ α') (g : Π a, β a ↪ β' (f a)) : (Σ a, β a) ↪ Σ a', β' a' := ⟨sigma.map f (λ a, g a), f.injective.sigma_map (λ a, (g a).injective)⟩ @[simp] lemma coe_sigma_map (f : α ↪ α') (g : Π a, β a ↪ β' (f a)) : ⇑(f.sigma_map g) = sigma.map f (λ a, g a) := rfl end sigma def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) := ⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).injective (congr_fun h a)⟩ def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w} (e : α ↪ β) : (γ → α) ↪ (γ → β) := Pi_congr_right (λ _, e) noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ] (e : α ↪ β) : (α → γ) ↪ (β → γ) := by haveI := classical.prop_decidable; exact let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in ⟨f', assume f₁ f₂ h, funext $ assume c, have ∃c', e c' = e c, from ⟨c, rfl⟩, have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _, have eq_b : classical.some this = c, from e.injective $ classical.some_spec this, by simp [f', this, if_pos, eq_b] at eq'; assumption⟩ protected def subtype_map {α β} {p : α → Prop} {q : β → Prop} (f : α ↪ β) (h : ∀{{x}}, p x → q (f x)) : {x : α // p x} ↪ {y : β // q y} := ⟨subtype.map f h, subtype.map_injective h f.2⟩ open set /-- `set.image` as an embedding `set α ↪ set β`. -/ protected def image {α β} (f : α ↪ β) : set α ↪ set β := ⟨image f, f.2.image_injective⟩ @[simp] lemma coe_image {α β} (f : α ↪ β) : ⇑f.image = image f := rfl end embedding end function namespace equiv @[simp] lemma refl_to_embedding {α : Type*} : (equiv.refl α).to_embedding = function.embedding.refl α := rfl @[simp] lemma trans_to_embedding {α β γ : Type*} (e : α ≃ β) (f : β ≃ γ) : (e.trans f).to_embedding = e.to_embedding.trans f.to_embedding := rfl end equiv namespace set /-- The injection map is an embedding between subsets. -/ def embedding_of_subset {α} (s t : set α) (h : s ⊆ t) : s ↪ t := ⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by congr; injection h⟩ @[simp] lemma embedding_of_subset_apply_mk {α} {s t : set α} (h : s ⊆ t) (x : α) (hx : x ∈ s) : embedding_of_subset s t h ⟨x, hx⟩ = ⟨x, h hx⟩ := rfl @[simp] lemma coe_embedding_of_subset_apply {α} {s t : set α} (h : s ⊆ t) (x : s) : (embedding_of_subset s t h x : α) = x := rfl end set /-- The embedding of a left cancellative semigroup into itself by left multiplication by a fixed element. -/ @[to_additive "The embedding of a left cancellative additive semigroup into itself by left translation by a fixed element."] def mul_left_embedding {G : Type u} [left_cancel_semigroup G] (g : G) : G ↪ G := { to_fun := λ h, g * h, inj' := λ h h', (mul_right_inj g).mp, } @[simp] lemma mul_left_embedding_apply {G : Type u} [left_cancel_semigroup G] (g h : G) : mul_left_embedding g h = g * h := rfl /-- The embedding of a right cancellative semigroup into itself by right multiplication by a fixed element. -/ @[to_additive "The embedding of a right cancellative additive semigroup into itself by right translation by a fixed element."] def mul_right_embedding {G : Type u} [right_cancel_semigroup G] (g : G) : G ↪ G := { to_fun := λ h, h * g, inj' := λ h h', (mul_left_inj g).mp, } @[simp] lemma mul_right_embedding_apply {G : Type u} [right_cancel_semigroup G] (g h : G) : mul_right_embedding g h = h * g := rfl
ad2c8ab50b40e960b53032f8bcdb67b5451c142b
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/815b.lean
33a2ea0cfe50d81286610f3fd61a58f4b1bcda7d
[ "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
942
lean
def is_smooth {α β} (f : α → β) : Prop := sorry class IsSmooth {α β} (f : α → β) : Prop where (proof : is_smooth f) instance identity : IsSmooth fun a : α => a := sorry instance const (b : β) : IsSmooth fun a : α => b := sorry instance swap (f : α → β → γ) [∀ a, IsSmooth (f a)] : IsSmooth (λ b a => f a b) := sorry instance parm (f : α → β → γ) [IsSmooth f] (b : β) : IsSmooth (λ a => f a b) := sorry instance comp (f : β → γ) (g : α → β) [IsSmooth f] [IsSmooth g] : IsSmooth (fun a => f (g a)) := sorry instance diag (f : β → δ → γ) (g : α → β) (h : α → δ) [IsSmooth f] [∀ b, IsSmooth (f b)] [IsSmooth g] [IsSmooth h] : IsSmooth (λ a => f (g a) (h a)) := sorry set_option trace.Meta.synthInstance true set_option trace.Meta.synthInstance.unusedArgs true example (f : β → δ → γ) [IsSmooth f] (d : δ) : IsSmooth (λ (g : α → β) a => f (g a) d) := by infer_instance
3110f26c185d8eb9ba017b535a22bf1b3fa61145
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/analysis/normed_space/banach.lean
2b6429edd4b0f3f7ec0b02161f24dbfdef221ebd
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
18,382
lean
/- Copyright (c) 2019 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 topology.metric_space.baire import analysis.normed_space.operator_norm import analysis.normed_space.affine_isometry /-! # Banach open mapping theorem This file contains the Banach open mapping theorem, i.e., the fact that a bijective bounded linear map between Banach spaces has a bounded inverse. -/ open function metric set filter finset open_locale classical topological_space big_operators nnreal variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] (f : E →L[𝕜] F) include 𝕜 namespace continuous_linear_map /-- A (possibly nonlinear) right inverse to a continuous linear map, which doesn't have to be linear itself but which satisfies a bound `∥inverse x∥ ≤ C * ∥x∥`. A surjective continuous linear map doesn't always have a continuous linear right inverse, but it always has a nonlinear inverse in this sense, by Banach's open mapping theorem. -/ structure nonlinear_right_inverse := (to_fun : F → E) (nnnorm : ℝ≥0) (bound' : ∀ y, ∥to_fun y∥ ≤ nnnorm * ∥y∥) (right_inv' : ∀ y, f (to_fun y) = y) instance : has_coe_to_fun (nonlinear_right_inverse f) (λ _, F → E) := ⟨λ fsymm, fsymm.to_fun⟩ @[simp] lemma nonlinear_right_inverse.right_inv {f : E →L[𝕜] F} (fsymm : nonlinear_right_inverse f) (y : F) : f (fsymm y) = y := fsymm.right_inv' y lemma nonlinear_right_inverse.bound {f : E →L[𝕜] F} (fsymm : nonlinear_right_inverse f) (y : F) : ∥fsymm y∥ ≤ fsymm.nnnorm * ∥y∥ := fsymm.bound' y end continuous_linear_map /-- Given a continuous linear equivalence, the inverse is in particular an instance of `nonlinear_right_inverse` (which turns out to be linear). -/ noncomputable def continuous_linear_equiv.to_nonlinear_right_inverse (f : E ≃L[𝕜] F) : continuous_linear_map.nonlinear_right_inverse (f : E →L[𝕜] F) := { to_fun := f.inv_fun, nnnorm := nnnorm (f.symm : F →L[𝕜] E), bound' := λ y, continuous_linear_map.le_op_norm (f.symm : F →L[𝕜] E) _, right_inv' := f.apply_symm_apply } noncomputable instance (f : E ≃L[𝕜] F) : inhabited (continuous_linear_map.nonlinear_right_inverse (f : E →L[𝕜] F)) := ⟨f.to_nonlinear_right_inverse⟩ /-! ### Proof of the Banach open mapping theorem -/ variable [complete_space F] namespace continuous_linear_map /-- First step of the proof of the Banach open mapping theorem (using completeness of `F`): by Baire's theorem, there exists a ball in `E` whose image closure has nonempty interior. Rescaling everything, it follows that any `y ∈ F` is arbitrarily well approached by images of elements of norm at most `C * ∥y∥`. For further use, we will only need such an element whose image is within distance `∥y∥/2` of `y`, to apply an iterative process. -/ lemma exists_approx_preimage_norm_le (surj : surjective f) : ∃C ≥ 0, ∀y, ∃x, dist (f x) y ≤ 1/2 * ∥y∥ ∧ ∥x∥ ≤ C * ∥y∥ := begin have A : (⋃n:ℕ, closure (f '' (ball 0 n))) = univ, { refine subset.antisymm (subset_univ _) (λy hy, _), rcases surj y with ⟨x, hx⟩, rcases exists_nat_gt (∥x∥) with ⟨n, hn⟩, refine mem_Union.2 ⟨n, subset_closure _⟩, refine (mem_image _ _ _).2 ⟨x, ⟨_, hx⟩⟩, rwa [mem_ball, dist_eq_norm, sub_zero] }, have : ∃ (n : ℕ) x, x ∈ interior (closure (f '' (ball 0 n))) := nonempty_interior_of_Union_of_closed (λn, is_closed_closure) A, simp only [mem_interior_iff_mem_nhds, metric.mem_nhds_iff] at this, rcases this with ⟨n, a, ε, ⟨εpos, H⟩⟩, rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, refine ⟨(ε/2)⁻¹ * ∥c∥ * 2 * n, _, λy, _⟩, { refine mul_nonneg (mul_nonneg (mul_nonneg _ (norm_nonneg _)) (by norm_num)) _, exacts [inv_nonneg.2 (div_nonneg (le_of_lt εpos) (by norm_num)), n.cast_nonneg] }, { by_cases hy : y = 0, { use 0, simp [hy] }, { rcases rescale_to_shell hc (half_pos εpos) hy with ⟨d, hd, ydlt, leyd, dinv⟩, let δ := ∥d∥ * ∥y∥/4, have δpos : 0 < δ := div_pos (mul_pos (norm_pos_iff.2 hd) (norm_pos_iff.2 hy)) (by norm_num), have : a + d • y ∈ ball a ε, by simp [dist_eq_norm, lt_of_le_of_lt ydlt.le (half_lt_self εpos)], rcases metric.mem_closure_iff.1 (H this) _ δpos with ⟨z₁, z₁im, h₁⟩, rcases (mem_image _ _ _).1 z₁im with ⟨x₁, hx₁, xz₁⟩, rw ← xz₁ at h₁, rw [mem_ball, dist_eq_norm, sub_zero] at hx₁, have : a ∈ ball a ε, by { simp, exact εpos }, rcases metric.mem_closure_iff.1 (H this) _ δpos with ⟨z₂, z₂im, h₂⟩, rcases (mem_image _ _ _).1 z₂im with ⟨x₂, hx₂, xz₂⟩, rw ← xz₂ at h₂, rw [mem_ball, dist_eq_norm, sub_zero] at hx₂, let x := x₁ - x₂, have I : ∥f x - d • y∥ ≤ 2 * δ := calc ∥f x - d • y∥ = ∥f x₁ - (a + d • y) - (f x₂ - a)∥ : by { congr' 1, simp only [x, f.map_sub], abel } ... ≤ ∥f x₁ - (a + d • y)∥ + ∥f x₂ - a∥ : norm_sub_le _ _ ... ≤ δ + δ : begin apply add_le_add, { rw [← dist_eq_norm, dist_comm], exact le_of_lt h₁ }, { rw [← dist_eq_norm, dist_comm], exact le_of_lt h₂ } end ... = 2 * δ : (two_mul _).symm, have J : ∥f (d⁻¹ • x) - y∥ ≤ 1/2 * ∥y∥ := calc ∥f (d⁻¹ • x) - y∥ = ∥d⁻¹ • f x - (d⁻¹ * d) • y∥ : by rwa [f.map_smul _, inv_mul_cancel, one_smul] ... = ∥d⁻¹ • (f x - d • y)∥ : by rw [mul_smul, smul_sub] ... = ∥d∥⁻¹ * ∥f x - d • y∥ : by rw [norm_smul, normed_field.norm_inv] ... ≤ ∥d∥⁻¹ * (2 * δ) : begin apply mul_le_mul_of_nonneg_left I, rw inv_nonneg, exact norm_nonneg _ end ... = (∥d∥⁻¹ * ∥d∥) * ∥y∥ /2 : by { simp only [δ], ring } ... = ∥y∥/2 : by { rw [inv_mul_cancel, one_mul], simp [norm_eq_zero, hd] } ... = (1/2) * ∥y∥ : by ring, rw ← dist_eq_norm at J, have K : ∥d⁻¹ • x∥ ≤ (ε / 2)⁻¹ * ∥c∥ * 2 * ↑n * ∥y∥ := calc ∥d⁻¹ • x∥ = ∥d∥⁻¹ * ∥x₁ - x₂∥ : by rw [norm_smul, normed_field.norm_inv] ... ≤ ((ε / 2)⁻¹ * ∥c∥ * ∥y∥) * (n + n) : begin refine mul_le_mul dinv _ (norm_nonneg _) _, { exact le_trans (norm_sub_le _ _) (add_le_add (le_of_lt hx₁) (le_of_lt hx₂)) }, { apply mul_nonneg (mul_nonneg _ (norm_nonneg _)) (norm_nonneg _), exact inv_nonneg.2 (le_of_lt (half_pos εpos)) } end ... = (ε / 2)⁻¹ * ∥c∥ * 2 * ↑n * ∥y∥ : by ring, exact ⟨d⁻¹ • x, J, K⟩ } }, end variable [complete_space E] /-- The Banach open mapping theorem: if a bounded linear map between Banach spaces is onto, then any point has a preimage with controlled norm. -/ theorem exists_preimage_norm_le (surj : surjective f) : ∃C > 0, ∀y, ∃x, f x = y ∧ ∥x∥ ≤ C * ∥y∥ := begin obtain ⟨C, C0, hC⟩ := exists_approx_preimage_norm_le f surj, /- Second step of the proof: starting from `y`, we want an exact preimage of `y`. Let `g y` be the approximate preimage of `y` given by the first step, and `h y = y - f(g y)` the part that has no preimage yet. We will iterate this process, taking the approximate preimage of `h y`, leaving only `h^2 y` without preimage yet, and so on. Let `u n` be the approximate preimage of `h^n y`. Then `u` is a converging series, and by design the sum of the series is a preimage of `y`. This uses completeness of `E`. -/ choose g hg using hC, let h := λy, y - f (g y), have hle : ∀y, ∥h y∥ ≤ (1/2) * ∥y∥, { assume y, rw [← dist_eq_norm, dist_comm], exact (hg y).1 }, refine ⟨2 * C + 1, by linarith, λy, _⟩, have hnle : ∀n:ℕ, ∥(h^[n]) y∥ ≤ (1/2)^n * ∥y∥, { assume n, induction n with n IH, { simp only [one_div, nat.nat_zero_eq_zero, one_mul, iterate_zero_apply, pow_zero] }, { rw [iterate_succ'], apply le_trans (hle _) _, rw [pow_succ, mul_assoc], apply mul_le_mul_of_nonneg_left IH, norm_num } }, let u := λn, g((h^[n]) y), have ule : ∀n, ∥u n∥ ≤ (1/2)^n * (C * ∥y∥), { assume n, apply le_trans (hg _).2 _, calc C * ∥(h^[n]) y∥ ≤ C * ((1/2)^n * ∥y∥) : mul_le_mul_of_nonneg_left (hnle n) C0 ... = (1 / 2) ^ n * (C * ∥y∥) : by ring }, have sNu : summable (λn, ∥u n∥), { refine summable_of_nonneg_of_le (λn, norm_nonneg _) ule _, exact summable.mul_right _ (summable_geometric_of_lt_1 (by norm_num) (by norm_num)) }, have su : summable u := summable_of_summable_norm sNu, let x := tsum u, have x_ineq : ∥x∥ ≤ (2 * C + 1) * ∥y∥ := calc ∥x∥ ≤ ∑'n, ∥u n∥ : norm_tsum_le_tsum_norm sNu ... ≤ ∑'n, (1/2)^n * (C * ∥y∥) : tsum_le_tsum ule sNu (summable.mul_right _ summable_geometric_two) ... = (∑'n, (1/2)^n) * (C * ∥y∥) : tsum_mul_right ... = 2 * C * ∥y∥ : by rw [tsum_geometric_two, mul_assoc] ... ≤ 2 * C * ∥y∥ + ∥y∥ : le_add_of_nonneg_right (norm_nonneg y) ... = (2 * C + 1) * ∥y∥ : by ring, have fsumeq : ∀n:ℕ, f (∑ i in finset.range n, u i) = y - (h^[n]) y, { assume n, induction n with n IH, { simp [f.map_zero] }, { rw [sum_range_succ, f.map_add, IH, iterate_succ', sub_add] } }, have : tendsto (λn, ∑ i in finset.range n, u i) at_top (𝓝 x) := su.has_sum.tendsto_sum_nat, have L₁ : tendsto (λn, f (∑ i in finset.range n, u i)) at_top (𝓝 (f x)) := (f.continuous.tendsto _).comp this, simp only [fsumeq] at L₁, have L₂ : tendsto (λn, y - (h^[n]) y) at_top (𝓝 (y - 0)), { refine tendsto_const_nhds.sub _, rw tendsto_iff_norm_tendsto_zero, simp only [sub_zero], refine squeeze_zero (λ_, norm_nonneg _) hnle _, rw [← zero_mul ∥y∥], refine (tendsto_pow_at_top_nhds_0_of_lt_1 _ _).mul tendsto_const_nhds; norm_num }, have feq : f x = y - 0 := tendsto_nhds_unique L₁ L₂, rw sub_zero at feq, exact ⟨x, feq, x_ineq⟩ end /-- The Banach open mapping theorem: a surjective bounded linear map between Banach spaces is open. -/ protected theorem is_open_map (surj : surjective f) : is_open_map f := begin assume s hs, rcases exists_preimage_norm_le f surj with ⟨C, Cpos, hC⟩, refine is_open_iff.2 (λy yfs, _), rcases mem_image_iff_bex.1 yfs with ⟨x, xs, fxy⟩, rcases is_open_iff.1 hs x xs with ⟨ε, εpos, hε⟩, refine ⟨ε/C, div_pos εpos Cpos, λz hz, _⟩, rcases hC (z-y) with ⟨w, wim, wnorm⟩, have : f (x + w) = z, by { rw [f.map_add, wim, fxy, add_sub_cancel'_right] }, rw ← this, have : x + w ∈ ball x ε := calc dist (x+w) x = ∥w∥ : by { rw dist_eq_norm, simp } ... ≤ C * ∥z - y∥ : wnorm ... < C * (ε/C) : begin apply mul_lt_mul_of_pos_left _ Cpos, rwa [mem_ball, dist_eq_norm] at hz, end ... = ε : mul_div_cancel' _ (ne_of_gt Cpos), exact set.mem_image_of_mem _ (hε this) end protected theorem quotient_map (surj : surjective f) : quotient_map f := (f.is_open_map surj).to_quotient_map f.continuous surj lemma _root_.affine_map.is_open_map {P Q : Type*} [metric_space P] [normed_add_torsor E P] [metric_space Q] [normed_add_torsor F Q] (f : P →ᵃ[𝕜] Q) (hf : continuous f) (surj : surjective f) : is_open_map f := affine_map.is_open_map_linear_iff.mp $ continuous_linear_map.is_open_map { cont := affine_map.continuous_linear_iff.mpr hf, .. f.linear } (f.surjective_iff_linear_surjective.mpr surj) /-! ### Applications of the Banach open mapping theorem -/ lemma interior_preimage (hsurj : surjective f) (s : set F) : interior (f ⁻¹' s) = f ⁻¹' (interior s) := ((f.is_open_map hsurj).preimage_interior_eq_interior_preimage f.continuous s).symm lemma closure_preimage (hsurj : surjective f) (s : set F) : closure (f ⁻¹' s) = f ⁻¹' (closure s) := ((f.is_open_map hsurj).preimage_closure_eq_closure_preimage f.continuous s).symm lemma frontier_preimage (hsurj : surjective f) (s : set F) : frontier (f ⁻¹' s) = f ⁻¹' (frontier s) := ((f.is_open_map hsurj).preimage_frontier_eq_frontier_preimage f.continuous s).symm lemma exists_nonlinear_right_inverse_of_surjective (f : E →L[𝕜] F) (hsurj : f.range = ⊤) : ∃ (fsymm : nonlinear_right_inverse f), 0 < fsymm.nnnorm := begin choose C hC fsymm h using exists_preimage_norm_le _ (linear_map.range_eq_top.mp hsurj), use { to_fun := fsymm, nnnorm := ⟨C, hC.lt.le⟩, bound' := λ y, (h y).2, right_inv' := λ y, (h y).1 }, exact hC end /-- A surjective continuous linear map between Banach spaces admits a (possibly nonlinear) controlled right inverse. In general, it is not possible to ensure that such a right inverse is linear (take for instance the map from `E` to `E/F` where `F` is a closed subspace of `E` without a closed complement. Then it doesn't have a continuous linear right inverse.) -/ @[irreducible] noncomputable def nonlinear_right_inverse_of_surjective (f : E →L[𝕜] F) (hsurj : f.range = ⊤) : nonlinear_right_inverse f := classical.some (exists_nonlinear_right_inverse_of_surjective f hsurj) lemma nonlinear_right_inverse_of_surjective_nnnorm_pos (f : E →L[𝕜] F) (hsurj : f.range = ⊤) : 0 < (nonlinear_right_inverse_of_surjective f hsurj).nnnorm := begin rw nonlinear_right_inverse_of_surjective, exact classical.some_spec (exists_nonlinear_right_inverse_of_surjective f hsurj) end end continuous_linear_map namespace linear_equiv variables [complete_space E] /-- If a bounded linear map is a bijection, then its inverse is also a bounded linear map. -/ @[continuity] theorem continuous_symm (e : E ≃ₗ[𝕜] F) (h : continuous e) : continuous e.symm := begin rw continuous_def, intros s hs, rw [← e.image_eq_preimage], rw [← e.coe_coe] at h ⊢, exact continuous_linear_map.is_open_map ⟨↑e, h⟩ e.surjective s hs end /-- Associating to a linear equivalence between Banach spaces a continuous linear equivalence when the direct map is continuous, thanks to the Banach open mapping theorem that ensures that the inverse map is also continuous. -/ def to_continuous_linear_equiv_of_continuous (e : E ≃ₗ[𝕜] F) (h : continuous e) : E ≃L[𝕜] F := { continuous_to_fun := h, continuous_inv_fun := e.continuous_symm h, ..e } @[simp] lemma coe_fn_to_continuous_linear_equiv_of_continuous (e : E ≃ₗ[𝕜] F) (h : continuous e) : ⇑(e.to_continuous_linear_equiv_of_continuous h) = e := rfl @[simp] lemma coe_fn_to_continuous_linear_equiv_of_continuous_symm (e : E ≃ₗ[𝕜] F) (h : continuous e) : ⇑(e.to_continuous_linear_equiv_of_continuous h).symm = e.symm := rfl end linear_equiv namespace continuous_linear_equiv variables [complete_space E] /-- Convert a bijective continuous linear map `f : E →L[𝕜] F` from a Banach space to a normed space to a continuous linear equivalence. -/ noncomputable def of_bijective (f : E →L[𝕜] F) (hinj : f.ker = ⊥) (hsurj : f.range = ⊤) : E ≃L[𝕜] F := (linear_equiv.of_bijective ↑f (linear_map.ker_eq_bot.mp hinj) (linear_map.range_eq_top.mp hsurj)) .to_continuous_linear_equiv_of_continuous f.continuous @[simp] lemma coe_fn_of_bijective (f : E →L[𝕜] F) (hinj : f.ker = ⊥) (hsurj : f.range = ⊤) : ⇑(of_bijective f hinj hsurj) = f := rfl lemma coe_of_bijective (f : E →L[𝕜] F) (hinj : f.ker = ⊥) (hsurj : f.range = ⊤) : ↑(of_bijective f hinj hsurj) = f := by { ext, refl } @[simp] lemma of_bijective_symm_apply_apply (f : E →L[𝕜] F) (hinj : f.ker = ⊥) (hsurj : f.range = ⊤) (x : E) : (of_bijective f hinj hsurj).symm (f x) = x := (of_bijective f hinj hsurj).symm_apply_apply x @[simp] lemma of_bijective_apply_symm_apply (f : E →L[𝕜] F) (hinj : f.ker = ⊥) (hsurj : f.range = ⊤) (y : F) : f ((of_bijective f hinj hsurj).symm y) = y := (of_bijective f hinj hsurj).apply_symm_apply y end continuous_linear_equiv namespace continuous_linear_map variables [complete_space E] /-- Intermediate definition used to show `continuous_linear_map.closed_complemented_range_of_is_compl_of_ker_eq_bot`. This is `f.coprod G.subtypeL` as an `continuous_linear_equiv`. -/ noncomputable def coprod_subtypeL_equiv_of_is_compl (f : E →L[𝕜] F) {G : submodule 𝕜 F} (h : is_compl f.range G) [complete_space G] (hker : f.ker = ⊥) : (E × G) ≃L[𝕜] F := continuous_linear_equiv.of_bijective (f.coprod G.subtypeL) (begin rw ker_coprod_of_disjoint_range, { rw [hker, submodule.ker_subtypeL, submodule.prod_bot] }, { rw submodule.range_subtypeL, exact h.disjoint } end) (by simp only [range_coprod, h.sup_eq_top, submodule.range_subtypeL]) lemma range_eq_map_coprod_subtypeL_equiv_of_is_compl (f : E →L[𝕜] F) {G : submodule 𝕜 F} (h : is_compl f.range G) [complete_space G] (hker : f.ker = ⊥) : f.range = ((⊤ : submodule 𝕜 E).prod (⊥ : submodule 𝕜 G)).map (f.coprod_subtypeL_equiv_of_is_compl h hker : E × G →ₗ[𝕜] F) := by rw [coprod_subtypeL_equiv_of_is_compl, _root_.coe_coe, continuous_linear_equiv.coe_of_bijective, coe_coprod, linear_map.coprod_map_prod, submodule.map_bot, sup_bot_eq, submodule.map_top, range] /- TODO: remove the assumption `f.ker = ⊥` in the next lemma, by using the map induced by `f` on `E / f.ker`, once we have quotient normed spaces. -/ lemma closed_complemented_range_of_is_compl_of_ker_eq_bot (f : E →L[𝕜] F) (G : submodule 𝕜 F) (h : is_compl f.range G) (hG : is_closed (G : set F)) (hker : f.ker = ⊥) : is_closed (f.range : set F) := begin haveI : complete_space G := hG.complete_space_coe, let g := coprod_subtypeL_equiv_of_is_compl f h hker, rw congr_arg coe (range_eq_map_coprod_subtypeL_equiv_of_is_compl f h hker ), apply g.to_homeomorph.is_closed_image.2, exact is_closed_univ.prod is_closed_singleton, end end continuous_linear_map
9d455ab0ad8caed846aaabcd43cb6effbf3ce389
6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf
/src/game/world9/level2.lean
226377e58743a33971ac5e29f6e34ce13031b9b0
[ "Apache-2.0" ]
permissive
arolihas/natural_number_game
4f0c93feefec93b8824b2b96adff8b702b8b43ce
8e4f7b4b42888a3b77429f90cce16292bd288138
refs/heads/master
1,621,872,426,808
1,586,270,467,000
1,586,270,467,000
253,648,466
0
0
null
1,586,219,694,000
1,586,219,694,000
null
UTF-8
Lean
false
false
398
lean
import game.world9.level1 -- hide namespace mynat -- hide /- # Advanced Multiplication World ## Level 2: `eq_zero_or_eq_zero_of_mul_eq_zero` A variant on the previous level. -/ /- Theorem If $ab = 0$, then at least one of $a$ or $b$ is equal to zero. -/ theorem eq_zero_or_eq_zero_of_mul_eq_zero (a b : mynat) (h : a * b = 0) : a = 0 ∨ b = 0 := begin [nat_num_game] end end mynat -- hide
d5836f59633ce8fd20514887d8265b7270c96953
4727251e0cd73359b15b664c3170e5d754078599
/src/order/category/NonemptyFinLinOrd.lean
d281dff7e3125faf1571f5ecad6a6b58560b232a
[ "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
3,413
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 data.fintype.order import order.category.LinearOrder /-! # Nonempty finite linear orders This defines `NonemptyFinLinOrd`, the category of nonempty finite linear orders with monotone maps. This is the index category for simplicial objects. -/ universes u v open category_theory /-- A typeclass for nonempty finite linear orders. -/ class nonempty_fin_lin_ord (α : Type*) extends fintype α, linear_order α := (nonempty : nonempty α . tactic.apply_instance) attribute [instance] nonempty_fin_lin_ord.nonempty @[priority 100] instance nonempty_fin_lin_ord.to_bounded_order (α : Type*) [nonempty_fin_lin_ord α] : bounded_order α := fintype.to_bounded_order α instance punit.nonempty_fin_lin_ord : nonempty_fin_lin_ord punit := { .. punit.linear_ordered_cancel_add_comm_monoid, .. punit.fintype } instance fin.nonempty_fin_lin_ord (n : ℕ) : nonempty_fin_lin_ord (fin (n+1)) := { .. fin.fintype _, .. fin.linear_order } instance ulift.nonempty_fin_lin_ord (α : Type u) [nonempty_fin_lin_ord α] : nonempty_fin_lin_ord (ulift.{v} α) := { nonempty := ⟨ulift.up ⊥⟩, .. linear_order.lift equiv.ulift (equiv.injective _), .. ulift.fintype _ } instance (α : Type*) [nonempty_fin_lin_ord α] : nonempty_fin_lin_ord αᵒᵈ := { ..order_dual.fintype α } /-- The category of nonempty finite linear orders. -/ def NonemptyFinLinOrd := bundled nonempty_fin_lin_ord namespace NonemptyFinLinOrd instance : bundled_hom.parent_projection @nonempty_fin_lin_ord.to_linear_order := ⟨⟩ attribute [derive [large_category, concrete_category]] NonemptyFinLinOrd instance : has_coe_to_sort NonemptyFinLinOrd Type* := bundled.has_coe_to_sort /-- Construct a bundled `NonemptyFinLinOrd` from the underlying type and typeclass. -/ def of (α : Type*) [nonempty_fin_lin_ord α] : NonemptyFinLinOrd := bundled.of α @[simp] lemma coe_of (α : Type*) [nonempty_fin_lin_ord α] : ↥(of α) = α := rfl instance : inhabited NonemptyFinLinOrd := ⟨of punit⟩ instance (α : NonemptyFinLinOrd) : nonempty_fin_lin_ord α := α.str instance has_forget_to_LinearOrder : has_forget₂ NonemptyFinLinOrd LinearOrder := bundled_hom.forget₂ _ _ /-- Constructs an equivalence between nonempty finite linear orders from an order isomorphism between them. -/ @[simps] def iso.mk {α β : NonemptyFinLinOrd.{u}} (e : α ≃o β) : α ≅ β := { hom := e, inv := e.symm, hom_inv_id' := by { ext, exact e.symm_apply_apply x }, inv_hom_id' := by { ext, exact e.apply_symm_apply x } } /-- `order_dual` as a functor. -/ @[simps] def dual : NonemptyFinLinOrd ⥤ NonemptyFinLinOrd := { obj := λ X, of Xᵒᵈ, map := λ X Y, order_hom.dual } /-- The equivalence between `FinPartialOrder` and itself induced by `order_dual` both ways. -/ @[simps functor inverse] def dual_equiv : NonemptyFinLinOrd ≌ NonemptyFinLinOrd := equivalence.mk dual dual (nat_iso.of_components (λ X, iso.mk $ order_iso.dual_dual X) $ λ X Y f, rfl) (nat_iso.of_components (λ X, iso.mk $ order_iso.dual_dual X) $ λ X Y f, rfl) end NonemptyFinLinOrd lemma NonemptyFinLinOrd_dual_comp_forget_to_LinearOrder : NonemptyFinLinOrd.dual ⋙ forget₂ NonemptyFinLinOrd LinearOrder = forget₂ NonemptyFinLinOrd LinearOrder ⋙ LinearOrder.dual := rfl
a8149d99a3ef42c1cf41a1e2cecdbf515d9df2c9
93b17e1ec33b7fd9fb0d8f958cdc9f2214b131a2
/src/sep/option.lean
e6c9dc533c0e7f6383225b73d6c4c3ec3a70a0bd
[]
no_license
intoverflow/timesink
93f8535cd504bc128ba1b57ce1eda4efc74e5136
c25be4a2edb866ad0a9a87ee79e209afad6ab303
refs/heads/master
1,620,033,920,087
1,524,995,105,000
1,524,995,105,000
120,576,102
1
0
null
null
null
null
UTF-8
Lean
false
false
2,917
lean
import .basic namespace Sep universes ℓ namespace Option inductive join {A : Type.{ℓ}} (j : A → A → A → Prop) : option A → option A → option A → Prop | base : ∀ {x₁ x₂ x₃} (Jx : j x₁ x₂ x₃) , join (option.some x₁) (option.some x₂) (option.some x₃) | none_r : ∀ {x}, join none x x | none_l : ∀ {x}, join x none x def join.assoc {A : Type.{ℓ}} {j : A → A → A → Prop} (HJ : IsAssoc j) : IsAssoc (join j) := begin intros x₁ x₂ x₃ x₁₂ x₁₂₃ J₁ J₂ P C, cases J₁, { cases J₂, { apply HJ Jx Jx_1, intro a, refine C { x := option.some a.x, J₁ := _, J₂ := _ }, { constructor, exact a.J₁ }, { constructor, exact a.J₂ } }, { refine C { x := option.some x₂_1, J₁ := _, J₂ := _ }, { constructor }, { assumption } } }, { cases J₂, { refine C { x := option.some x₃_1, J₁ := _, J₂ := _ }, { assumption }, { constructor } }, { refine C { x := x₃, J₁ := _, J₂ := _ }, { constructor }, { constructor } }, { refine C { x := x₂, J₁ := _, J₂ := _ }, { constructor }, { constructor } } }, { refine C { x := x₃, J₁ := _, J₂ := _ }, { constructor }, { assumption } } end end Option def Alg.Opt (A : Alg.{ℓ}) : Alg.{ℓ} := { τ := option A.τ , join := Option.join A.join , comm := λ x₁ x₂ x₃ J , begin cases J, { constructor, apply A.comm, assumption }, { constructor }, { constructor } end , assoc := @Option.join.assoc _ _ A.assoc } def Alg.Opt.Ident (A : Alg.{ℓ}) : Alg.Ident A.Opt := { one := option.none , join_one_r := begin intro x, constructor end , join_one_uniq_r := begin intros x y H, cases H, repeat { trivial } end } def Alg.Opt.join_none_l {A : Alg.{ℓ}} {x₁ x₂ : option A.τ} (J : A.Opt.join none x₁ x₂) : x₁ = x₂ := begin cases J, repeat { trivial } end def Alg.Opt.join_none_r {A : Alg.{ℓ}} {x₁ x₂ : option A.τ} (J : A.Opt.join x₁ none x₂) : x₁ = x₂ := begin apply Alg.Opt.join_none_l, apply A.Opt.comm, assumption end def Alg.Opt.join_some_r {A : Alg.{ℓ}} {x₁} {x₂} {x₃ : option A.τ} (J : A.Opt.join x₁ (some x₂) x₃) : ¬ x₃ = none := begin cases J, repeat { intro F, cases F } end def Alg.Opt.join_some_l {A : Alg.{ℓ}} {x₁} {x₂} {x₃ : option A.τ} (J : A.Opt.join (some x₁) x₂ x₃) : ¬ x₃ = none := begin cases J, repeat { intro F, cases F } end end Sep