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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
d6027061e3260030eb77974a98f26a11c84372d6 | a4673261e60b025e2c8c825dfa4ab9108246c32e | /src/Lean/Compiler/IR/ExpandResetReuse.lean | 1a08a8a26a46a9dd71aee7de71edd3ff0249b397 | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,012 | 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.CompilerM
import Lean.Compiler.IR.NormIds
import Lean.Compiler.IR.FreeVars
namespace Lean.IR.ExpandResetReuse
/- Mapping from variable to projections -/
abbrev ProjMap := Std.HashMap VarId Expr
namespace CollectProjMap
abbrev Collector := ProjMap → ProjMap
@[inline] def collectVDecl (x : VarId) (v : Expr) : Collector := fun m =>
match v with
| Expr.proj .. => m.insert x v
| Expr.sproj .. => m.insert x v
| Expr.uproj .. => m.insert x v
| _ => m
partial def collectFnBody : FnBody → Collector
| FnBody.vdecl x _ v b => collectVDecl x v ∘ collectFnBody b
| FnBody.jdecl _ _ v b => collectFnBody v ∘ collectFnBody b
| FnBody.case _ _ _ alts => fun s => alts.foldl (fun s alt => collectFnBody alt.body s) s
| e => if e.isTerminal then id else collectFnBody e.body
end CollectProjMap
/- Create a mapping from variables to projections.
This function assumes variable ids have been normalized -/
def mkProjMap (d : Decl) : ProjMap :=
match d with
| Decl.fdecl _ _ _ b => CollectProjMap.collectFnBody b {}
| _ => {}
structure Context :=
(projMap : ProjMap)
/- Return true iff `x` is consumed in all branches of the current block.
Here consumption means the block contains a `dec x` or `reuse x ...`. -/
partial def consumed (x : VarId) : FnBody → Bool
| FnBody.vdecl _ _ v b =>
match v with
| Expr.reuse y _ _ _ => x == y || consumed x b
| _ => consumed x b
| FnBody.dec y _ _ _ b => x == y || consumed x b
| FnBody.case _ _ _ alts => alts.all fun alt => consumed x alt.body
| e => !e.isTerminal && consumed x e.body
abbrev Mask := Array (Option VarId)
/- Auxiliary function for eraseProjIncFor -/
partial def eraseProjIncForAux (y : VarId) (bs : Array FnBody) (mask : Mask) (keep : Array FnBody) : Array FnBody × Mask :=
let done (_ : Unit) := (bs ++ keep.reverse, mask)
let keepInstr (b : FnBody) := eraseProjIncForAux y bs.pop mask (keep.push b)
if bs.size < 2 then done ()
else
let b := bs.back
match b with
| (FnBody.vdecl _ _ (Expr.sproj _ _ _) _) => keepInstr b
| (FnBody.vdecl _ _ (Expr.uproj _ _) _) => keepInstr b
| (FnBody.inc z n c p _) =>
if n == 0 then done () else
let b' := bs[bs.size - 2]
match b' with
| (FnBody.vdecl w _ (Expr.proj i x) _) =>
if w == z && y == x then
/- Found
```
let z := proj[i] y
inc z n c
```
We keep `proj`, and `inc` when `n > 1`
-/
let bs := bs.pop.pop
let mask := mask.set! i (some z)
let keep := keep.push b'
let keep := if n == 1 then keep else keep.push (FnBody.inc z (n-1) c p FnBody.nil)
eraseProjIncForAux y bs mask keep
else done ()
| other => done ()
| other => done ()
/- Try to erase `inc` instructions on projections of `y` occurring in the tail of `bs`.
Return the updated `bs` and a bit mask specifying which `inc`s have been removed. -/
def eraseProjIncFor (n : Nat) (y : VarId) (bs : Array FnBody) : Array FnBody × Mask :=
eraseProjIncForAux y bs (mkArray n none) #[]
/- Replace `reuse x ctor ...` with `ctor ...`, and remoce `dec x` -/
partial def reuseToCtor (x : VarId) : FnBody → FnBody
| FnBody.dec y n c p b =>
if x == y then b -- n must be 1 since `x := reset ...`
else FnBody.dec y n c p (reuseToCtor x b)
| FnBody.vdecl z t v b =>
match v with
| Expr.reuse y c u xs =>
if x == y then FnBody.vdecl z t (Expr.ctor c xs) b
else FnBody.vdecl z t v (reuseToCtor x b)
| _ =>
FnBody.vdecl z t v (reuseToCtor x b)
| FnBody.case tid y yType alts =>
let alts := alts.map fun alt => alt.modifyBody (reuseToCtor x)
FnBody.case tid y yType alts
| e =>
if e.isTerminal then
e
else
let (instr, b) := e.split
let b := reuseToCtor x b
instr.setBody b
/-
replace
```
x := reset y; b
```
with
```
inc z_1; ...; inc z_i; dec y; b'
```
where `z_i`'s are the variables in `mask`,
and `b'` is `b` where we removed `dec x` and replaced `reuse x ctor_i ...` with `ctor_i ...`.
-/
def mkSlowPath (x y : VarId) (mask : Mask) (b : FnBody) : FnBody :=
let b := reuseToCtor x b
let b := FnBody.dec y 1 true false b
mask.foldl (init := b) fun b m => match m with
| some z => FnBody.inc z 1 true false b
| none => b
abbrev M := ReaderT Context (StateM Nat)
def mkFresh : M VarId :=
modifyGet $ fun n => ({ idx := n }, n + 1)
def releaseUnreadFields (y : VarId) (mask : Mask) (b : FnBody) : M FnBody :=
mask.size.foldM (init := b) fun i b =>
match mask.get! i with
| some _ => pure b -- code took ownership of this field
| none => do
let fld ← mkFresh
pure (FnBody.vdecl fld IRType.object (Expr.proj i y) (FnBody.dec fld 1 true false b))
def setFields (y : VarId) (zs : Array Arg) (b : FnBody) : FnBody :=
zs.size.fold (init := b) fun i b => FnBody.set y i (zs.get! i) b
/- Given `set x[i] := y`, return true iff `y := proj[i] x` -/
def isSelfSet (ctx : Context) (x : VarId) (i : Nat) (y : Arg) : Bool :=
match y with
| Arg.var y =>
match ctx.projMap.find? y with
| some (Expr.proj j w) => j == i && w == x
| _ => false
| _ => false
/- Given `uset x[i] := y`, return true iff `y := uproj[i] x` -/
def isSelfUSet (ctx : Context) (x : VarId) (i : Nat) (y : VarId) : Bool :=
match ctx.projMap.find? y with
| some (Expr.uproj j w) => j == i && w == x
| _ => false
/- Given `sset x[n, i] := y`, return true iff `y := sproj[n, i] x` -/
def isSelfSSet (ctx : Context) (x : VarId) (n : Nat) (i : Nat) (y : VarId) : Bool :=
match ctx.projMap.find? y with
| some (Expr.sproj m j w) => n == m && j == i && w == x
| _ => false
/- Remove unnecessary `set/uset/sset` operations -/
partial def removeSelfSet (ctx : Context) : FnBody → FnBody
| FnBody.set x i y b =>
if isSelfSet ctx x i y then removeSelfSet ctx b
else FnBody.set x i y (removeSelfSet ctx b)
| FnBody.uset x i y b =>
if isSelfUSet ctx x i y then removeSelfSet ctx b
else FnBody.uset x i y (removeSelfSet ctx b)
| FnBody.sset x n i y t b =>
if isSelfSSet ctx x n i y then removeSelfSet ctx b
else FnBody.sset x n i y t (removeSelfSet ctx b)
| FnBody.case tid y yType alts =>
let alts := alts.map fun alt => alt.modifyBody (removeSelfSet ctx)
FnBody.case tid y yType alts
| e =>
if e.isTerminal then e
else
let (instr, b) := e.split
let b := removeSelfSet ctx b
instr.setBody b
partial def reuseToSet (ctx : Context) (x y : VarId) : FnBody → FnBody
| FnBody.dec z n c p b =>
if x == z then FnBody.del y b
else FnBody.dec z n c p (reuseToSet ctx x y b)
| FnBody.vdecl z t v b =>
match v with
| Expr.reuse w c u zs =>
if x == w then
let b := setFields y zs (b.replaceVar z y)
let b := if u then FnBody.setTag y c.cidx b else b
removeSelfSet ctx b
else FnBody.vdecl z t v (reuseToSet ctx x y b)
| _ => FnBody.vdecl z t v (reuseToSet ctx x y b)
| FnBody.case tid z zType alts =>
let alts := alts.map fun alt => alt.modifyBody (reuseToSet ctx x y)
FnBody.case tid z zType alts
| e =>
if e.isTerminal then e
else
let (instr, b) := e.split
let b := reuseToSet ctx x y b
instr.setBody b
/-
replace
```
x := reset y; b
```
with
```
let f_i_1 := proj[i_1] y;
...
let f_i_k := proj[i_k] y;
b'
```
where `i_j`s are the field indexes
that the code did not touch immediately before the reset.
That is `mask[j] == none`.
`b'` is `b` where `y` `dec x` is replaced with `del y`,
and `z := reuse x ctor_i ws; F` is replaced with
`set x i ws[i]` operations, and we replace `z` with `x` in `F`
-/
def mkFastPath (x y : VarId) (mask : Mask) (b : FnBody) : M FnBody := do
let ctx ← read
let b := reuseToSet ctx x y b
releaseUnreadFields y mask b
-- Expand `bs; x := reset[n] y; b`
partial def expand (mainFn : FnBody → Array FnBody → M FnBody)
(bs : Array FnBody) (x : VarId) (n : Nat) (y : VarId) (b : FnBody) : M FnBody := do
let bOld := FnBody.vdecl x IRType.object (Expr.reset n y) b
let (bs, mask) := eraseProjIncFor n y bs
/- Remark: we may be duplicting variable/JP indices. That is, `bSlow` and `bFast` may
have duplicate indices. We run `normalizeIds` to fix the ids after we have expand them. -/
let bSlow := mkSlowPath x y mask b
let bFast ← mkFastPath x y mask b
/- We only optimize recursively the fast. -/
let bFast ← mainFn bFast #[]
let c ← mkFresh
let b := FnBody.vdecl c IRType.uint8 (Expr.isShared y) (mkIf c bSlow bFast)
pure $ reshape bs b
partial def searchAndExpand : FnBody → Array FnBody → M FnBody
| d@(FnBody.vdecl x t (Expr.reset n y) b), bs =>
if consumed x b then do
expand searchAndExpand bs x n y b
else
searchAndExpand b (push bs d)
| FnBody.jdecl j xs v b, bs => do
let v ← searchAndExpand v #[]
searchAndExpand b (push bs (FnBody.jdecl j xs v FnBody.nil))
| FnBody.case tid x xType alts, bs => do
let alts ← alts.mapM $ fun alt => alt.mmodifyBody fun b => searchAndExpand b #[]
pure $ reshape bs (FnBody.case tid x xType alts)
| b, bs =>
if b.isTerminal then pure $ reshape bs b
else searchAndExpand b.body (push bs b)
def main (d : Decl) : Decl :=
match d with
| (Decl.fdecl f xs t b) =>
let m := mkProjMap d
let nextIdx := d.maxIndex + 1
let b := (searchAndExpand b #[] { projMap := m }).run' nextIdx
Decl.fdecl f xs t b
| d => d
end ExpandResetReuse
/-- (Try to) expand `reset` and `reuse` instructions. -/
def Decl.expandResetReuse (d : Decl) : Decl :=
(ExpandResetReuse.main d).normalizeIds
end Lean.IR
|
d5a49b56f88aaf1f9243613af0c05f48e3b8c3ed | 12dabd587ce2621d9a4eff9f16e354d02e206c8e | /world08/level13.lean | f7a896979303a73c1e590c1b8e9424ab851c6101 | [] | 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 | 148 | lean | lemma ne_succ_self (n : mynat) : n ≠ succ n :=
begin
induction n with h hd,
apply zero_ne_succ,
intro hs,
apply hd,
apply succ_inj,
exact hs,
end
|
1731eda43cdc487596f6752b510eb636fc4dff8f | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/1315a.lean | 2b691bf5ffd7824008263ba61c8cbcbcb6002fb7 | [
"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 | 515 | lean | def k : ℕ := 0
def works : Π (n : ℕ) (m : ℕ), ℕ
| 0 m := 0
| (n+1) m :=
let val := m+1 in
match works n val with
| 0 := 0
| (l+1) := 0
end
def works2 : Π (n : ℕ) (m : ℕ), ℕ
| 0 m := 0
| (n+1) m :=
match k with
| 0 := 0
| (i+1) :=
match works2 n (m+1) with
| 0 := 0
| (l+1) := 0
end
end
def fails : Π (n : ℕ) (m : ℕ), ℕ
| 0 m := 0
| (n+1) m :=
match k with
| 0 := 0
| (i+1) :=
let val := m+1 in
match fails n val with
| 0 := 0
| (l+1) := 0
end
end
|
3a7f736dec5258818f31f6a4571499498cfc4933 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /11_Tactic-Style_Proofs.org.21.lean | 83a5867678725c2085617ac722ec6fbb3782e30d | [] | 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 | 275 | lean | import standard
import data.nat
open nat
variables x y z : ℕ
-- BEGIN
example : x + y + z = x + y + z :=
begin
generalize (x + y + z), -- goal is x y z : ℕ ⊢ ∀ (x : ℕ), x = x
clears x y z,
intro w, -- goal is w : ℕ ⊢ w = w
apply rfl
end
-- END
|
31d580ff0bc982e40807c4b627d350ee4f56a94c | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/eqn_tac.lean | 6b9e9903e50aba62b317835edc988f7a726e36c0 | [
"Apache-2.0"
] | permissive | soonhokong/lean | cb8aa01055ffe2af0fb99a16b4cda8463b882cd1 | 38607e3eb57f57f77c0ac114ad169e9e4262e24f | refs/heads/master | 1,611,187,284,081 | 1,450,766,737,000 | 1,476,122,547,000 | 11,513,992 | 2 | 0 | null | 1,401,763,102,000 | 1,374,182,235,000 | C++ | UTF-8 | Lean | false | false | 363 | lean | open nat
definition foo : nat → nat
| foo zero := begin exact zero end
| foo (succ a) := begin exact a end
example : foo zero = zero := rfl
example (a : nat) : foo (succ a) = a := rfl
definition bla : nat → nat
| bla zero := zero
| bla (succ a) :=
begin
apply foo,
exact a
end
example (a : nat) : foo (succ a) = bla (succ (succ a)) := rfl
|
f683a1c7084476eb71c0db5e9a56d90129f931fb | 9a0b1b3a653ea926b03d1495fef64da1d14b3174 | /tidy/backwards_reasoning.lean | fec5ddcdf975dfc519a0aa3715ced881d466fd56 | [
"Apache-2.0"
] | permissive | khoek/mathlib-tidy | 8623b27b4e04e7d598164e7eaf248610d58f768b | 866afa6ab597c47f1b72e8fe2b82b97fff5b980f | refs/heads/master | 1,585,598,975,772 | 1,538,659,544,000 | 1,538,659,544,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,125 | lean | -- Copyright (c) 2017 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Scott Morrison
import tactic.basic
import tactic.ext
import .recover
open tactic
meta def back_attribute : user_attribute := {
name := `back,
descr := "A lemma that should be applied to a goal whenever possible; use `backwards_reasoning` to automatically `apply` all lemmas tagged `[back]`."
}
run_cmd attribute.register `back_attribute
meta def apply_using_solve_by_elim (c : name) : tactic unit :=
do g::gs ← get_goals,
set_goals [g],
t ← mk_const c,
r ← apply t,
try (any_goals (terminal_goal >> solve_by_elim)),
gs' ← get_goals,
set_goals (gs' ++ gs)
/-- Try to apply one of the given lemmas; it succeeds as soon as one of them succeeds. -/
meta def any_apply : list name → tactic name
| [] := failed
| (c::cs) := (do
apply_using_solve_by_elim c,
pure c) <|> any_apply cs
meta def back'_attribute : user_attribute := {
name := `back',
descr := "A lemma that should be applied to a goal whenever possible, as long as all arguments to the lemma by be fulfilled from existing hypotheses; use `backwards_reasoning` to automatically apply all lemmas tagged `[back']`."
}
run_cmd attribute.register `back'_attribute
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)
/-- Try to apply one of the given lemmas, fulfilling all new goals using existing hypotheses. It succeeds if one of them succeeds. -/
meta def any_apply_no_new_goals : list name → tactic name
| [] := failed
| (c::cs) := (do g::gs ← get_goals,
set_goals [g],
t ← mk_const c,
r ← apply t,
all_goals solve_by_elim,
a ← r.mmap (λ p, do e ← instantiate_mvars p.2, return e.list_meta_vars.length),
guard (a.all (λ n, n = 0)),
gs' ← get_goals,
set_goals (gs' ++ gs),
pure c) <|> any_apply_no_new_goals cs
/-- Try to apply any lemma marked with the attributes `@[back]` or `@[back']`. -/
meta def backwards_reasoning : tactic string :=
do cs ← attribute.get_instances `back',
r ← try_core (any_apply_no_new_goals cs),
match r with
| (some n) := return ("apply " ++ n.to_string ++ " ; solve_by_elim")
| none := do
cs ← attribute.get_instances `back,
n ← any_apply cs <|> fail "no @[back] or @[back'] lemmas could be applied",
return ("apply " ++ n.to_string)
end
attribute [extensionality] subtype.eq
-- TODO should `apply_instance` be in tidy? If so, these shouldn't be needed.
@[back] definition decidable_true : decidable true := is_true dec_trivial
@[back] definition decidable_false : decidable false := is_false dec_trivial
attribute [back] quotient.mk quotient.sound
attribute [back] eqv_gen.rel
attribute [back'] Exists.intro
|
75bfc37865a660c37b2bc74da32743df4d5a823f | ea5678cc400c34ff95b661fa26d15024e27ea8cd | /addition.lean | d0a84a04bd9d1afd590e6d7660f9a355aa563032 | [] | no_license | ChrisHughes24/leanstuff | dca0b5349c3ed893e8792ffbd98cbcadaff20411 | 9efa85f72efaccd1d540385952a6acc18fce8687 | refs/heads/master | 1,654,883,241,759 | 1,652,873,885,000 | 1,652,873,885,000 | 134,599,537 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,848 | lean |
inductive xnat
| zero : xnat
| succ : xnat → xnat
open xnat
#print xnat.succ.inj
#print xnat.no_confusion
definition one := succ zero
definition two := succ one
definition add :xnat → xnat → xnat
| n zero := n
| n (succ p) := succ (add n p)
notation a + b := add a b
theorem one_add_one_equals_two : one + one = two :=
begin
unfold two,
unfold one,
unfold add,
end
theorem add_zerox (n:xnat): n+zero=n:=
begin
unfold add,
end
theorem zero_addx (n:xnat):zero+n=n:=
begin
induction n with k H,
unfold add,
unfold add,
rw[H],
end
theorem add_assocx (a b c:xnat):(a+b)+c=a+(b+c):=
begin
induction c with k H,
unfold add,
unfold add,
rw[H],
end
theorem zero_add_eq_add_zerox (n:xnat) : zero+n=n+zero:=
begin
rw[zero_addx,add_zerox],
end
theorem add_one_eq_succx (n:xnat) : n + one = succ n:=
begin
unfold one add,
end
theorem one_add_eq_succx (n : xnat) : one+n=succ n:=
begin
induction n with k H,
unfold one add,
unfold one add,
rw[←H],
unfold one,
end
theorem succ_addx (a b:xnat) : succ (a + b) = succ a + b:=begin
induction b with b hi,
trivial,
unfold add,rw hi,
end
theorem add_commx (a b:xnat) : a+b = b+a:=
begin
induction b with k H,
rw[zero_add_eq_add_zerox],
unfold add,
rw [H,succ_addx],
end
theorem eq_iff_succ_eq_succ (a b : xnat) : succ a = succ b ↔ a = b :=
begin
split,
exact succ.inj,
assume H : a = b,
rw [H],
end
theorem add_cancel_right (a b t : xnat) : a = b ↔ a+t = b+t :=
begin
split,
assume H,
rw[H],
induction t with k H,
rw[add_zerox,add_zerox],
assume H1,
exact H1,
unfold add,
rw[eq_iff_succ_eq_succ],
exact H,
end
definition mul:xnat→xnat→xnat
| n zero:=zero
| n (succ p):= mul n p + n
notation a * b := mul a b
theorem mul_zerox (a : xnat) : a * zero = zero :=
begin
trivial,
end
theorem zero_mulx (a : xnat) : zero * a = zero :=
begin
induction a with k H,
unfold mul,
unfold mul add,
rw[H],
end
theorem mul_onex (a : xnat) : a * one = a :=
begin
unfold one mul,
rw[zero_addx],
end
theorem one_mulx (a : xnat) : one * a = a :=
begin
induction a with k H,
unfold mul,
unfold mul,
rw[add_one_eq_succx, H],
end
theorem right_distribx (a b c : xnat) : a * (b + c) = a* b + a * c :=
begin
induction c with k H,
rw[mul_zerox,add_zerox,add_zerox],
unfold add mul,
rw[H, add_assocx],
end
theorem left_distribx (a b c : xnat) : (a + b) * c = a * c + b * c :=
begin
induction c with n Hn,
unfold mul,
refl,
rw [←add_one_eq_succx,right_distribx,Hn,right_distribx,right_distribx],
rw [mul_onex,mul_onex,mul_onex],
rw [add_assocx,←add_assocx (b*n),add_commx (b*n),←add_assocx,←add_assocx,←add_assocx],
end
theorem mul_assocx (a b c : xnat) : (a * b) * c = a * (b * c) :=
begin
induction c with k H,
rw[mul_zerox,mul_zerox,mul_zerox],
unfold mul,
rw[right_distribx,H]
end
theorem mul_commx (a b : xnat) : a * b = b * a :=
begin
induction b with k H,
rw[mul_zerox,zero_mulx],
unfold mul,
rw[H],
exact calc k * a + a = k * a + one * a: by rw[one_mulx]
...=(k + one) * a: by rw[left_distribx]
...=succ k * a: by rw[add_one_eq_succx],
end
definition lt : xnat → xnat → Prop
| zero zero := false
| (succ m) zero := false
| zero (succ p) := true
| (succ m) (succ p) := lt m p
notation b > a := lt a b
notation a < b := lt a b
theorem inequality_A1 (a b t : xnat) : a < b → a + t < b + t :=
begin
induction t with n H,
rw[add_zerox,add_zerox],
assume H1,
exact H1,
unfold add lt, exact H,
end
theorem blah: ∀a b c:xnat,a<b→b<c→a<c:=begin
assume a,
induction a with a1 Hia,
assume b c,
cases b with b1,
unfold lt,cc,
cases c with c1,
unfold lt,cc,
unfold lt,cc,
assume b c,
cases b with b1,
unfold lt,cc,
cases c with c1,
unfold lt,cc,
unfold lt,
exact Hia b1 c1,
end
theorem blah1: ∀a b c:xnat,a<b→b<c→a<c:=begin
assume a b, revert a,
induction b with b1 Hib,
assume a,
cases a with a1,
unfold lt,cc,
unfold lt,cc,
assume a c,
cases c with c1,
unfold lt,cc,
cases a with a1,
unfold lt,cc,
unfold lt,exact Hib a1 c1,
end
theorem blah2: ∀a b c:xnat,a<b→b<c→a<c:=begin
assume a b c,revert a b,
induction c with c1 Hic,
assume a b,
cases b with b1,
unfold lt,cc,
unfold lt,cc,
assume a b,
cases a with a1,
unfold lt,cc,
cases b with b1,
unfold lt,trivial,
unfold lt,exact Hic a1 b1,
end
#check list.
#print blah2
theorem subtraction :∀ a b:xnat,a<b→∃c,c+a=b:=begin
assume a,
induction a with a1 Hia,
assume b H1,
existsi b, unfold add,
assume b1,
cases b1 with b2,
unfold lt,trivial,
unfold lt,
assume H2,
apply exists.elim (Hia b2 H2),
assume c H3,
existsi c,rw ←H3,unfold add,
end
theorem a_lt_a_add_succ_b (a b:xnat):a<a+succ b:=begin
induction a with a1 Ha,
rw zero_addx,unfold lt,
unfold add lt,rwa[←add_one_eq_succx,add_assocx,one_add_eq_succx],
end
theorem blah3 (x y:xnat):zero<x→one<y→x<x*y:=begin
cases x with x1,
unfold lt,cc,
cases y with y1,
unfold one lt,cc,
cases y1 with y2,
unfold one lt,cc,
unfold one lt mul,
rw[add_commx,←one_add_eq_succx,add_assocx,one_add_eq_succx,one_add_eq_succx],
unfold lt,
have H1:x1 + (succ x1 * y2 + succ x1)=x1+succ (succ x1 * y2 + x1):=calc
x1 + (succ x1 * y2 + succ x1) = x1 + (succ x1 * y2 + (x1+one)):begin rw ←add_one_eq_succx end
...=x1 + (succ x1 * y2 + x1+one):begin rw add_assocx, end
...=x1 +succ (succ x1 * y2 + x1):begin rw add_one_eq_succx, end,
rw H1,
assume H2 H3,
exact a_lt_a_add_succ_b x1 (succ x1 * y2 + x1),
end
|
5971e7fc3bdd8a62b244746ca971225b5be7c5b6 | f08e5018e0d696ec84edb728e81a5744332d856e | /04_integers_and_rationals.lean | 6c3335ae794f04d3fa2efdd038273a4ddc4f0dca | [] | no_license | Shamrock-Frost/tao-analysis-one | 8ad24ac6f69920ed2b81d2c6646e73a2328bfbe7 | 82b3efa0f79880a1acbc09e88afc852be33d1c4f | refs/heads/master | 1,611,264,143,494 | 1,495,584,000,000 | 1,495,584,000,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,704 | lean | -- Let the integers be represented as an ordered pair of natural numbers.
def ℤ₀ := ℕ × ℕ
-- An equivalence relation is defined on the integers.
def eqv : ℤ₀ → ℤ₀ → Prop
| (a, b) (c, d) := a + d = c + b
infix ` ∽ `:50 := eqv
private theorem eqv.refl : ∀ (a : ℤ₀), a ∽ a
| (a, b) := rfl
private theorem eqv.symm : ∀ (a b : ℤ₀), a ∽ b → b ∽ a
| (a, b) (c, d) := λ h₁, eq.symm h₁
private theorem eqv.trans : ∀ (a b c : ℤ₀), a ∽ b → b ∽ c → a ∽ c
| (a, b) (c, d) (e, f) :=
λ (h₁ : a + d = c + b)
(h₂ : c + f = e + d),
show (a + f = e + b), from
have h₃ : a + d + c + f = c + b + e + d, from
calc
a + d + c + f = a + d + e + d : by simp [add_assoc, h₂^.symm]
... = c + b + e + d : by simp [add_assoc, h₁^.symm],
have h₄ : c + d + a + f = c + d + b + e, from
calc
c + d + a + f = a + d + c + f : by simp [add_comm]
... = c + b + e + d : h₃
... = c + d + b + e : by simp [add_comm],
have h₅ : a + f = b + e,
from @add_left_cancel ℕ _ (c + d) _ _ (by simp; simp at h₃; assumption),
h₅^.symm ▸ (add_comm b e ▸ rfl)
private theorem is_equivalence : equivalence eqv :=
mk_equivalence (_) (eqv.refl) (eqv.symm) (eqv.trans)
instance Z.setoid : setoid ℤ₀ := setoid.mk eqv is_equivalence
-- A characteristic property of eqv
-- Now define the integers as a quotient of the ordered pair of naturals.
def Z : Type := quotient (Z.setoid)
namespace Z
-- Definition 4.1.2.
def add_Z₀ : ℤ₀ → ℤ₀ → ℤ₀
| (a, b) (c, d) := (a + c, b + d)
-- Lemma 4.1.3 (Addition and multiplication are well-defined).
end Z
|
00c8ab4a7ed3c3b497c05457175162851e8eb9a7 | d7189ea2ef694124821b033e533f18905b5e87ef | /galois/list/rotate.lean | ebf2ef696e71b71cf083b0314a350117af42fe39 | [
"Apache-2.0"
] | permissive | digama0/lean-protocol-support | eaa7e6f8b8e0d5bbfff1f7f52bfb79a3b11b0f59 | cabfa3abedbdd6fdca6e2da6fbbf91a13ed48dda | refs/heads/master | 1,625,421,450,627 | 1,506,035,462,000 | 1,506,035,462,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,456 | lean | open nat
namespace list
variable {T : Type}
definition rol1 : list T → list T
| nil := nil
| (cons a l) := l ++ [a]
definition ror1_aux : T → list T → list T
| a nil := cons a nil
| a (cons b l) := cons b (cons a l)
definition ror1 : list T → list T
| nil := nil
| (cons a l) := ror1_aux a (ror1 l)
definition rol : list T → ℕ → list T
| l 0 := l
| l (succ n) := rol1 (rol l n)
--definition ror (l : list T) (n : ℕ) : list T := reverse (rol (reverse l) n)
definition ror : list T → ℕ → list T
| l 0 := l
| l (succ n) := ror1 (ror l n)
theorem ror1_concat (l : list T) (a : T)
: ror1 (l ++ [a]) = a::l :=
begin
induction l,
case list.nil { refl, },
case list.cons h r ind {
simp [ror1, ind, ror1_aux],
},
end
theorem ror1_rol1 (l : list T) : ror1 (rol1 l) = l :=
begin
induction l,
case list.nil { refl, },
case list.cons h r ind {
simp [rol1, ror1_concat],
}
end
theorem rol1_ror1_aux (a : T) (l : list T) : rol1 (ror1_aux a l) = cons a (rol1 l) :=
list.cases_on l rfl (λ b l', rfl)
theorem rol1_ror1 (l : list T) : rol1 (ror1 l) = l :=
list.rec_on l rfl (λ a l' H, eq.trans (rol1_ror1_aux _ _) (congr_arg (cons a) H))
theorem length_rol1 (l : list T) : length (rol1 l) = length l :=
begin
induction l,
case list.nil { refl, },
case list.cons h r ind {
simp [rol1],
}
end
theorem length_rol (l : list T) (n : ℕ) : length (rol l n) = length l :=
nat.rec_on n rfl (λ a H, eq.trans (length_rol1 _) H)
theorem length_ror1_aux (a : T) (l : list T) : length (ror1_aux a l) = succ (length l) :=
list.cases_on l rfl (λ b l', rfl)
theorem length_ror1 (l : list T) : length (ror1 l) = length l :=
list.rec_on l rfl (λ a l' H, (eq.trans (length_ror1_aux a (ror1 l')) (congr_arg succ H)))
theorem length_ror (l : list T) (n : ℕ) : length (ror l n) = length l :=
nat.rec_on n rfl (λ a H, eq.trans (length_ror1 _) H)
--eq.trans (@length_reverse _ _) (eq.trans (@length_rol _ _ _) (@length_reverse _ _))
--by rewrite [↑ror, length_reverse, length_rol, length_reverse]
theorem rol_rol (m n : ℕ) (l : list T) : rol (rol l m) n = rol l (m + n) :=
nat.rec_on n rfl (λ a H, congr_arg rol1 H)
--theorem ror_ror (m n : ℕ) (l : list T) : ror (ror l m) n = ror l (m + n) :=
--by rewrite [↑ror, reverse_reverse, rol_rol]
end list
|
5ccbd6876ec4878e7349b0ae28df89e65618067c | 92b50235facfbc08dfe7f334827d47281471333b | /hott/algebra/category/adjoint.hlean | c02c04885ae926f573fecc28508ba9e2149ef5dc | [
"Apache-2.0"
] | permissive | htzh/lean | 24f6ed7510ab637379ec31af406d12584d31792c | d70c79f4e30aafecdfc4a60b5d3512199200ab6e | refs/heads/master | 1,607,677,731,270 | 1,437,089,952,000 | 1,437,089,952,000 | 37,078,816 | 0 | 0 | null | 1,433,780,956,000 | 1,433,780,955,000 | null | UTF-8 | Lean | false | false | 5,786 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import algebra.category.constructions .constructions types.function arity
open category functor nat_trans eq is_trunc iso equiv prod trunc function
namespace category
variables {C D : Precategory} {F : C ⇒ D}
-- do we want to have a structure "is_adjoint" and define
-- structure is_left_adjoint (F : C ⇒ D) :=
-- (right_adjoint : D ⇒ C) -- G
-- (is_adjoint : adjoint F right_adjoint)
structure is_left_adjoint [class] (F : C ⇒ D) :=
(G : D ⇒ C)
(η : functor.id ⟹ G ∘f F)
(ε : F ∘f G ⟹ functor.id)
(H : Π(c : C), (ε (F c)) ∘ (F (η c)) = ID (F c))
(K : Π(d : D), (G (ε d)) ∘ (η (G d)) = ID (G d))
abbreviation right_adjoint := @is_left_adjoint.G
abbreviation unit := @is_left_adjoint.η
abbreviation counit := @is_left_adjoint.ε
-- structure is_left_adjoint [class] (F : C ⇒ D) :=
-- (right_adjoint : D ⇒ C) -- G
-- (unit : functor.id ⟹ right_adjoint ∘f F) -- η
-- (counit : F ∘f right_adjoint ⟹ functor.id) -- ε
-- (H : Π(c : C), (counit (F c)) ∘ (F (unit c)) = ID (F c))
-- (K : Π(d : D), (right_adjoint (counit d)) ∘ (unit (right_adjoint d)) = ID (right_adjoint d))
structure is_equivalence [class] (F : C ⇒ D) extends is_left_adjoint F :=
mk' ::
(is_iso_unit : is_iso η)
(is_iso_counit : is_iso ε)
structure equivalence (C D : Precategory) :=
(to_functor : C ⇒ D)
(struct : is_equivalence to_functor)
--TODO: review and change
--TODO: make some or all of these structures?
definition faithful (F : C ⇒ D) :=
Π⦃c c' : C⦄ (f f' : c ⟶ c'), F f = F f' → f = f'
definition full (F : C ⇒ D) := Π⦃c c' : C⦄ (g : F c ⟶ F c'), ∃(f : c ⟶ c'), F f = g
definition fully_faithful [reducible] (F : C ⇒ D) :=
Π⦃c c' : C⦄, is_equiv (@(to_fun_hom F) c c')
definition split_essentially_surjective (F : C ⇒ D) :=
Π⦃d : D⦄, Σ(c : C), F c ≅ d
definition essentially_surjective (F : C ⇒ D) :=
Π⦃d : D⦄, ∃(c : C), F c ≅ d
definition is_weak_equivalence (F : C ⇒ D) :=
fully_faithful F × essentially_surjective F
definition is_isomorphism (F : C ⇒ D) :=
fully_faithful F × is_equiv (to_fun_ob F)
structure isomorphism (C D : Precategory) :=
(to_functor : C ⇒ D)
(struct : is_isomorphism to_functor)
-- infix `⊣`:55 := adjoint
infix `⋍`:25 := equivalence -- \backsimeq or \equiv
infix `≌`:25 := isomorphism -- \backcong or \iso
definition is_hprop_is_left_adjoint {C : Category} {D : Precategory} (F : C ⇒ D)
: is_hprop (is_left_adjoint F) :=
begin
apply is_hprop.mk,
intro G G', cases G with G η ε H K, cases G' with G' η' ε' H' K',
fapply (apd011111 is_left_adjoint.mk),
{ fapply functor_eq,
{ intro d, apply eq_of_iso, fapply iso.MK,
{ exact (G' (ε d) ∘ η' (G d))},
{ exact (G (ε' d) ∘ η (G' d))},
{ apply sorry /-rewrite [assoc, -{((G (ε' d)) ∘ (η (G' d))) ∘ (G' (ε d))}(assoc)],-/
-- apply concat, apply (ap (λc, c ∘ η' _)), rewrite -assoc, apply idp
},
--/-rewrite [-nat_trans.assoc]-/apply sorry
---assoc (G (ε' d)) (η (G' d)) (G' (ε d))
{ apply sorry}},
{ apply sorry},
},
{ apply sorry},
{ apply sorry},
{ apply is_hprop.elim},
{ apply is_hprop.elim},
end
definition is_equivalence.mk (F : C ⇒ D) (G : D ⇒ C) (η : G ∘f F ≅ functor.id)
(ε : F ∘f G ≅ functor.id) : is_equivalence F :=
sorry
definition full_of_fully_faithful (H : fully_faithful F) : full F :=
sorry -- λc c' g, exists.intro ((@(to_fun_hom F) c c')⁻¹ g) _
definition faithful_of_fully_faithful (H : fully_faithful F) : faithful F :=
λc c' f f' p, is_injective_of_is_embedding p
definition fully_faithful_of_full_of_faithful (H : faithful F) (K : full F) : fully_faithful F :=
sorry
definition fully_faithful_equiv (F : C ⇒ D) : fully_faithful F ≃ (faithful F × full F) :=
sorry
definition is_equivalence_equiv (F : C ⇒ D)
: is_equivalence F ≃ (fully_faithful F × split_essentially_surjective F) :=
sorry
definition is_hprop_is_weak_equivalence (F : C ⇒ D) : is_hprop (is_weak_equivalence F) :=
sorry
definition is_hprop_is_equivalence {C D : Category} (F : C ⇒ D) : is_hprop (is_equivalence F) :=
sorry
definition is_equivalence_equiv_is_weak_equivalence {C D : Category} (F : C ⇒ D)
: is_equivalence F ≃ is_weak_equivalence F :=
sorry
definition is_hprop_is_isomorphism (F : C ⇒ D) : is_hprop (is_isomorphism F) :=
sorry
definition is_isomorphism_equiv1 (F : C ⇒ D) : is_equivalence F
≃ Σ(G : D ⇒ C) (η : functor.id = G ∘f F) (ε : F ∘f G = functor.id),
sorry ▸ ap (λ(H : C ⇒ C), F ∘f H) η = ap (λ(H : D ⇒ D), H ∘f F) ε⁻¹ :=
sorry
definition is_isomorphism_equiv2 (F : C ⇒ D) : is_equivalence F
≃ ∃(G : D ⇒ C), functor.id = G ∘f F × F ∘f G = functor.id :=
sorry
definition is_equivalence_of_isomorphism (H : is_isomorphism F) : is_equivalence F :=
sorry
definition is_isomorphism_of_is_equivalence {C D : Category} {F : C ⇒ D} (H : is_equivalence F)
: is_isomorphism F :=
sorry
definition isomorphism_of_eq {C D : Precategory} (p : C = D) : C ≌ D :=
sorry
definition is_equiv_isomorphism_of_eq (C D : Precategory) : is_equiv (@isomorphism_of_eq C D) :=
sorry
definition equivalence_of_eq {C D : Precategory} (p : C = D) : C ⋍ D :=
sorry
definition is_equiv_equivalence_of_eq (C D : Category) : is_equiv (@equivalence_of_eq C D) :=
sorry
end category
|
64615609f16d5c609d6576f96ec60f0f18f6d251 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/Lean3Lib/init/propext_auto.lean | 4199f1301eacbcfa264f900854b62c7f80959f8e | [] | 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,632 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.logic
universes u
namespace Mathlib
axiom propext {a : Prop} {b : Prop} : (a ↔ b) → a = b
/- Additional congruence lemmas. -/
theorem forall_congr_eq {a : Sort u} {p : a → Prop} {q : a → Prop} (h : ∀ (x : a), p x = q x) :
(∀ (x : a), p x) = ∀ (x : a), q x :=
propext (forall_congr fun (a : a) => eq.to_iff (h a))
theorem imp_congr_eq {a : Prop} {b : Prop} {c : Prop} {d : Prop} (h₁ : a = c) (h₂ : b = d) :
(a → b) = (c → d) :=
propext (imp_congr (eq.to_iff h₁) (eq.to_iff h₂))
theorem imp_congr_ctx_eq {a : Prop} {b : Prop} {c : Prop} {d : Prop} (h₁ : a = c) (h₂ : c → b = d) :
(a → b) = (c → d) :=
propext (imp_congr_ctx (eq.to_iff h₁) fun (hc : c) => eq.to_iff (h₂ hc))
theorem eq_true_intro {a : Prop} (h : a) : a = True := propext (iff_true_intro h)
theorem eq_false_intro {a : Prop} (h : ¬a) : a = False := propext (iff_false_intro h)
theorem iff.to_eq {a : Prop} {b : Prop} (h : a ↔ b) : a = b := propext h
theorem iff_eq_eq {a : Prop} {b : Prop} : (a ↔ b) = (a = b) :=
propext { mp := fun (h : a ↔ b) => iff.to_eq h, mpr := fun (h : a = b) => eq.to_iff h }
theorem eq_false {a : Prop} : a = False = (¬a) :=
(fun (this : (a ↔ False) = (¬a)) => iff_eq_eq ▸ this) (propext (iff_false a))
theorem eq_true {a : Prop} : a = True = a :=
(fun (this : (a ↔ True) = a) => iff_eq_eq ▸ this) (propext (iff_true a))
end Mathlib |
b335d288115d391732bb439d36f0205bbe245fbe | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch5/ex0401.lean | 8c37fdf5fa38b7fecdbdf972a940e2ddbf137425 | [] | no_license | Ailrun/Theorem_Proving_in_Lean | ae6a23f3c54d62d401314d6a771e8ff8b4132db2 | 2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68 | refs/heads/master | 1,609,838,270,467 | 1,586,846,743,000 | 1,586,846,743,000 | 240,967,761 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 316 | lean | example (p q r : Prop) : p ∧ (q ∨ r) → (p ∧ q) ∨ (p ∧ r) :=
begin
intro h,
exact
have hp: p, from h.left,
have hqr: q ∨ r, from h.right,
show (p ∧ q) ∨ (p ∧ r),
begin
cases hqr with hq hr,
exact or.inl ⟨hp, hq⟩,
exact or.inr ⟨hp, hr⟩
end
end
|
f6d92782fac657c261fab4d1089d9c2c666611b8 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/data/polynomial/degree/trailing_degree.lean | bef7f53b802b244eae53ce5d1e5f384ec6ae0b23 | [
"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 | 16,175 | lean | /-
Copyright (c) 2020 Damiano Testa. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Damiano Testa
-/
import data.nat.enat
import data.polynomial.degree.definitions
/-!
# Trailing degree of univariate polynomials
## Main definitions
* `trailing_degree p`: the multiplicity of `X` in the polynomial `p`
* `nat_trailing_degree`: a variant of `trailing_degree` that takes values in the natural numbers
* `trailing_coeff`: the coefficient at index `nat_trailing_degree p`
Converts most results about `degree`, `nat_degree` and `leading_coeff` to results about the bottom
end of a polynomial
-/
noncomputable theory
open function polynomial finsupp finset
open_locale big_operators classical polynomial
namespace polynomial
universes u v
variables {R : Type u} {S : Type v} {a b : R} {n m : ℕ}
section semiring
variables [semiring R] {p q r : R[X]}
/-- `trailing_degree p` is the multiplicity of `x` in the polynomial `p`, i.e. the smallest
`X`-exponent in `p`.
`trailing_degree p = some n` when `p ≠ 0` and `n` is the smallest power of `X` that appears
in `p`, otherwise
`trailing_degree 0 = ⊤`. -/
def trailing_degree (p : R[X]) : ℕ∞ := p.support.inf some
lemma trailing_degree_lt_wf : well_founded
(λp q : R[X], trailing_degree p < trailing_degree q) :=
inv_image.wf trailing_degree (with_top.well_founded_lt nat.lt_wf)
/-- `nat_trailing_degree p` forces `trailing_degree p` to `ℕ`, by defining
`nat_trailing_degree ⊤ = 0`. -/
def nat_trailing_degree (p : R[X]) : ℕ := (trailing_degree p).get_or_else 0
/-- `trailing_coeff p` gives the coefficient of the smallest power of `X` in `p`-/
def trailing_coeff (p : R[X]) : R := coeff p (nat_trailing_degree p)
/-- a polynomial is `monic_at` if its trailing coefficient is 1 -/
def trailing_monic (p : R[X]) := trailing_coeff p = (1 : R)
lemma trailing_monic.def : trailing_monic p ↔ trailing_coeff p = 1 := iff.rfl
instance trailing_monic.decidable [decidable_eq R] : decidable (trailing_monic p) :=
by unfold trailing_monic; apply_instance
@[simp] lemma trailing_monic.trailing_coeff {p : R[X]} (hp : p.trailing_monic) :
trailing_coeff p = 1 := hp
@[simp] lemma trailing_degree_zero : trailing_degree (0 : R[X]) = ⊤ := rfl
@[simp] lemma trailing_coeff_zero : trailing_coeff (0 : R[X]) = 0 := rfl
@[simp] lemma nat_trailing_degree_zero : nat_trailing_degree (0 : R[X]) = 0 := rfl
lemma trailing_degree_eq_top : trailing_degree p = ⊤ ↔ p = 0 :=
⟨λ h, support_eq_empty.1 (finset.min_eq_top.1 h),
λ h, by simp [h]⟩
lemma trailing_degree_eq_nat_trailing_degree (hp : p ≠ 0) :
trailing_degree p = (nat_trailing_degree p : ℕ∞) :=
let ⟨n, hn⟩ :=
not_forall.1 (mt option.eq_none_iff_forall_not_mem.2 (mt trailing_degree_eq_top.1 hp)) in
have hn : trailing_degree p = some n := not_not.1 hn,
by rw [nat_trailing_degree, hn]; refl
lemma trailing_degree_eq_iff_nat_trailing_degree_eq {p : R[X]} {n : ℕ} (hp : p ≠ 0) :
p.trailing_degree = n ↔ p.nat_trailing_degree = n :=
by rw [trailing_degree_eq_nat_trailing_degree hp, with_top.coe_eq_coe]
lemma trailing_degree_eq_iff_nat_trailing_degree_eq_of_pos {p : R[X]} {n : ℕ} (hn : 0 < n) :
p.trailing_degree = n ↔ p.nat_trailing_degree = n :=
begin
split,
{ intro H, rwa ← trailing_degree_eq_iff_nat_trailing_degree_eq, rintro rfl,
rw trailing_degree_zero at H, exact option.no_confusion H },
{ intro H, rwa trailing_degree_eq_iff_nat_trailing_degree_eq, rintro rfl,
rw nat_trailing_degree_zero at H, rw H at hn, exact lt_irrefl _ hn }
end
lemma nat_trailing_degree_eq_of_trailing_degree_eq_some {p : R[X]} {n : ℕ}
(h : trailing_degree p = n) : nat_trailing_degree p = n :=
have hp0 : p ≠ 0, from λ hp0, by rw hp0 at h; exact option.no_confusion h,
option.some_inj.1 $ show (nat_trailing_degree p : ℕ∞) = n,
by rwa [← trailing_degree_eq_nat_trailing_degree hp0]
@[simp] lemma nat_trailing_degree_le_trailing_degree :
↑(nat_trailing_degree p) ≤ trailing_degree p :=
begin
by_cases hp : p = 0, { rw [hp, trailing_degree_zero], exact le_top },
rw [trailing_degree_eq_nat_trailing_degree hp],
exact le_rfl
end
lemma nat_trailing_degree_eq_of_trailing_degree_eq [semiring S] {q : S[X]}
(h : trailing_degree p = trailing_degree q) :
nat_trailing_degree p = nat_trailing_degree q :=
by unfold nat_trailing_degree; rw h
lemma le_trailing_degree_of_ne_zero (h : coeff p n ≠ 0) : trailing_degree p ≤ n :=
show @has_le.le (ℕ∞) _ (p.support.inf some : ℕ∞) (some n : ℕ∞),
from finset.inf_le (mem_support_iff.2 h)
lemma nat_trailing_degree_le_of_ne_zero (h : coeff p n ≠ 0) : nat_trailing_degree p ≤ n :=
begin
rw [← with_top.coe_le_coe, ← trailing_degree_eq_nat_trailing_degree],
{ exact le_trailing_degree_of_ne_zero h, },
{ assume h, subst h, exact h rfl }
end
lemma trailing_degree_le_trailing_degree (h : coeff q (nat_trailing_degree p) ≠ 0) :
trailing_degree q ≤ trailing_degree p :=
begin
by_cases hp : p = 0,
{ rw hp, exact le_top },
{ rw trailing_degree_eq_nat_trailing_degree hp, exact le_trailing_degree_of_ne_zero h }
end
lemma trailing_degree_ne_of_nat_trailing_degree_ne {n : ℕ} :
p.nat_trailing_degree ≠ n → trailing_degree p ≠ n :=
mt $ λ h, by rw [nat_trailing_degree, h, option.get_or_else_coe]
theorem nat_trailing_degree_le_of_trailing_degree_le {n : ℕ} {hp : p ≠ 0}
(H : (n : ℕ∞) ≤ trailing_degree p) : n ≤ nat_trailing_degree p :=
begin
rw trailing_degree_eq_nat_trailing_degree hp at H,
exact with_top.coe_le_coe.mp H,
end
lemma nat_trailing_degree_le_nat_trailing_degree {hq : q ≠ 0}
(hpq : p.trailing_degree ≤ q.trailing_degree) :
p.nat_trailing_degree ≤ q.nat_trailing_degree :=
begin
by_cases hp : p = 0, { rw [hp, nat_trailing_degree_zero], exact zero_le _ },
rwa [trailing_degree_eq_nat_trailing_degree hp, trailing_degree_eq_nat_trailing_degree hq,
with_top.coe_le_coe] at hpq
end
@[simp] lemma trailing_degree_monomial (ha : a ≠ 0) : trailing_degree (monomial n a) = n :=
by rw [trailing_degree, support_monomial n ha, inf_singleton, with_top.some_eq_coe]
lemma nat_trailing_degree_monomial (ha : a ≠ 0) : nat_trailing_degree (monomial n a) = n :=
by rw [nat_trailing_degree, trailing_degree_monomial ha]; refl
lemma nat_trailing_degree_monomial_le : nat_trailing_degree (monomial n a) ≤ n :=
if ha : a = 0 then by simp [ha] else (nat_trailing_degree_monomial ha).le
lemma le_trailing_degree_monomial : ↑n ≤ trailing_degree (monomial n a) :=
if ha : a = 0 then by simp [ha] else (trailing_degree_monomial ha).ge
@[simp] lemma trailing_degree_C (ha : a ≠ 0) : trailing_degree (C a) = (0 : ℕ∞) :=
trailing_degree_monomial ha
lemma le_trailing_degree_C : (0 : ℕ∞) ≤ trailing_degree (C a) :=
le_trailing_degree_monomial
lemma trailing_degree_one_le : (0 : ℕ∞) ≤ trailing_degree (1 : R[X]) :=
by rw [← C_1]; exact le_trailing_degree_C
@[simp] lemma nat_trailing_degree_C (a : R) : nat_trailing_degree (C a) = 0 :=
nonpos_iff_eq_zero.1 nat_trailing_degree_monomial_le
@[simp] lemma nat_trailing_degree_one : nat_trailing_degree (1 : R[X]) = 0 :=
nat_trailing_degree_C 1
@[simp] lemma nat_trailing_degree_nat_cast (n : ℕ) : nat_trailing_degree (n : R[X]) = 0 :=
by simp only [←C_eq_nat_cast, nat_trailing_degree_C]
@[simp] lemma trailing_degree_C_mul_X_pow (n : ℕ) (ha : a ≠ 0) :
trailing_degree (C a * X ^ n) = n :=
by rw [C_mul_X_pow_eq_monomial, trailing_degree_monomial ha]
lemma le_trailing_degree_C_mul_X_pow (n : ℕ) (a : R) :
(n : ℕ∞) ≤ trailing_degree (C a * X ^ n) :=
by { rw C_mul_X_pow_eq_monomial, exact le_trailing_degree_monomial }
lemma coeff_eq_zero_of_trailing_degree_lt (h : (n : ℕ∞) < trailing_degree p) :
coeff p n = 0 :=
not_not.1 (mt le_trailing_degree_of_ne_zero (not_le_of_gt h))
lemma coeff_eq_zero_of_lt_nat_trailing_degree {p : R[X]} {n : ℕ}
(h : n < p.nat_trailing_degree) :
p.coeff n = 0 :=
begin
apply coeff_eq_zero_of_trailing_degree_lt,
by_cases hp : p = 0,
{ rw [hp, trailing_degree_zero], exact with_top.coe_lt_top n, },
{ rwa [trailing_degree_eq_nat_trailing_degree hp, with_top.coe_lt_coe] },
end
@[simp] lemma coeff_nat_trailing_degree_pred_eq_zero {p : R[X]}
{hp : (0 : ℕ∞) < nat_trailing_degree p} : p.coeff (p.nat_trailing_degree - 1) = 0 :=
coeff_eq_zero_of_lt_nat_trailing_degree $ nat.sub_lt
((with_top.zero_lt_coe (nat_trailing_degree p)).mp hp) nat.one_pos
theorem le_trailing_degree_X_pow (n : ℕ) :
(n : ℕ∞) ≤ trailing_degree (X^n : R[X]) :=
by simpa only [C_1, one_mul] using le_trailing_degree_C_mul_X_pow n (1:R)
theorem le_trailing_degree_X : (1 : ℕ∞) ≤ trailing_degree (X : R[X]) :=
le_trailing_degree_monomial
lemma nat_trailing_degree_X_le : (X : R[X]).nat_trailing_degree ≤ 1 :=
nat_trailing_degree_monomial_le
@[simp] lemma trailing_coeff_eq_zero : trailing_coeff p = 0 ↔ p = 0 :=
⟨λ h, by_contradiction $ λ hp, mt mem_support_iff.1
(not_not.2 h) (mem_of_min (trailing_degree_eq_nat_trailing_degree hp)),
λ h, h.symm ▸ leading_coeff_zero⟩
lemma trailing_coeff_nonzero_iff_nonzero : trailing_coeff p ≠ 0 ↔ p ≠ 0 :=
not_congr trailing_coeff_eq_zero
lemma nat_trailing_degree_mem_support_of_nonzero : p ≠ 0 → nat_trailing_degree p ∈ p.support :=
(mem_support_iff.mpr ∘ trailing_coeff_nonzero_iff_nonzero.mpr)
lemma nat_trailing_degree_le_of_mem_supp (a : ℕ) :
a ∈ p.support → nat_trailing_degree p ≤ a:=
nat_trailing_degree_le_of_ne_zero ∘ mem_support_iff.mp
lemma nat_trailing_degree_eq_support_min' (h : p ≠ 0) :
nat_trailing_degree p = p.support.min' (nonempty_support_iff.mpr h) :=
begin
apply le_antisymm,
{ apply le_min',
intros y hy,
exact nat_trailing_degree_le_of_mem_supp y hy },
{ apply finset.min'_le,
exact mem_support_iff.mpr (trailing_coeff_nonzero_iff_nonzero.mpr h), },
end
lemma le_nat_trailing_degree (hp : p ≠ 0) (hn : ∀ m < n, p.coeff m = 0) :
n ≤ p.nat_trailing_degree :=
begin
rw nat_trailing_degree_eq_support_min' hp,
exact finset.le_min' _ _ _ (λ m hm, not_lt.1 $ λ hmn, mem_support_iff.1 hm $ hn _ hmn),
end
lemma nat_trailing_degree_le_nat_degree (p : R[X]) :
p.nat_trailing_degree ≤ p.nat_degree :=
begin
by_cases hp : p = 0,
{ rw [hp, nat_degree_zero, nat_trailing_degree_zero] },
{ exact le_nat_degree_of_ne_zero (mt trailing_coeff_eq_zero.mp hp) },
end
lemma nat_trailing_degree_mul_X_pow {p : R[X]} (hp : p ≠ 0) (n : ℕ) :
(p * X ^ n).nat_trailing_degree = p.nat_trailing_degree + n :=
begin
apply le_antisymm,
{ refine nat_trailing_degree_le_of_ne_zero (λ h, mt trailing_coeff_eq_zero.mp hp _),
rwa [trailing_coeff, ←coeff_mul_X_pow] },
{ rw [nat_trailing_degree_eq_support_min' (λ h, hp (mul_X_pow_eq_zero h)), finset.le_min'_iff],
intros y hy,
have key : n ≤ y,
{ rw [mem_support_iff, coeff_mul_X_pow'] at hy,
exact by_contra (λ h, hy (if_neg h)) },
rw [mem_support_iff, coeff_mul_X_pow', if_pos key] at hy,
exact (le_tsub_iff_right key).mp (nat_trailing_degree_le_of_ne_zero hy) },
end
lemma le_trailing_degree_mul : p.trailing_degree + q.trailing_degree ≤ (p * q).trailing_degree :=
begin
refine le_inf (λ n hn, _),
rw [mem_support_iff, coeff_mul] at hn,
obtain ⟨⟨i, j⟩, hij, hpq⟩ := exists_ne_zero_of_sum_ne_zero hn,
refine (add_le_add (inf_le (mem_support_iff.mpr (left_ne_zero_of_mul hpq)))
(inf_le (mem_support_iff.mpr (right_ne_zero_of_mul hpq)))).trans (le_of_eq _),
rwa [with_top.some_eq_coe, with_top.some_eq_coe, with_top.some_eq_coe,
← with_top.coe_add, with_top.coe_eq_coe, ←nat.mem_antidiagonal],
end
lemma le_nat_trailing_degree_mul (h : p * q ≠ 0) :
p.nat_trailing_degree + q.nat_trailing_degree ≤ (p * q).nat_trailing_degree :=
begin
have hp : p ≠ 0 := λ hp, h (by rw [hp, zero_mul]),
have hq : q ≠ 0 := λ hq, h (by rw [hq, mul_zero]),
rw [←with_top.coe_le_coe, with_top.coe_add, ←trailing_degree_eq_nat_trailing_degree hp,
←trailing_degree_eq_nat_trailing_degree hq, ←trailing_degree_eq_nat_trailing_degree h],
exact le_trailing_degree_mul,
end
lemma coeff_mul_nat_trailing_degree_add_nat_trailing_degree :
(p * q).coeff (p.nat_trailing_degree + q.nat_trailing_degree) =
p.trailing_coeff * q.trailing_coeff :=
begin
rw coeff_mul,
refine finset.sum_eq_single (p.nat_trailing_degree, q.nat_trailing_degree) _
(λ h, (h (nat.mem_antidiagonal.mpr rfl)).elim),
rintro ⟨i, j⟩ h₁ h₂,
rw nat.mem_antidiagonal at h₁,
by_cases hi : i < p.nat_trailing_degree,
{ rw [coeff_eq_zero_of_lt_nat_trailing_degree hi, zero_mul] },
by_cases hj : j < q.nat_trailing_degree,
{ rw [coeff_eq_zero_of_lt_nat_trailing_degree hj, mul_zero] },
rw not_lt at hi hj,
refine (h₂ (prod.ext_iff.mpr _).symm).elim,
exact (add_eq_add_iff_eq_and_eq hi hj).mp h₁.symm,
end
lemma trailing_degree_mul' (h : p.trailing_coeff * q.trailing_coeff ≠ 0) :
(p * q).trailing_degree = p.trailing_degree + q.trailing_degree :=
begin
have hp : p ≠ 0 := λ hp, h (by rw [hp, trailing_coeff_zero, zero_mul]),
have hq : q ≠ 0 := λ hq, h (by rw [hq, trailing_coeff_zero, mul_zero]),
refine le_antisymm _ le_trailing_degree_mul,
rw [trailing_degree_eq_nat_trailing_degree hp, trailing_degree_eq_nat_trailing_degree hq,
← enat.coe_add],
apply le_trailing_degree_of_ne_zero,
rwa coeff_mul_nat_trailing_degree_add_nat_trailing_degree,
end
lemma nat_trailing_degree_mul' (h : p.trailing_coeff * q.trailing_coeff ≠ 0) :
(p * q).nat_trailing_degree = p.nat_trailing_degree + q.nat_trailing_degree :=
begin
have hp : p ≠ 0 := λ hp, h (by rw [hp, trailing_coeff_zero, zero_mul]),
have hq : q ≠ 0 := λ hq, h (by rw [hq, trailing_coeff_zero, mul_zero]),
apply nat_trailing_degree_eq_of_trailing_degree_eq_some,
rw [trailing_degree_mul' h, with_top.coe_add,
←trailing_degree_eq_nat_trailing_degree hp, ←trailing_degree_eq_nat_trailing_degree hq],
end
lemma nat_trailing_degree_mul [no_zero_divisors R] (hp : p ≠ 0) (hq : q ≠ 0) :
(p * q).nat_trailing_degree = p.nat_trailing_degree + q.nat_trailing_degree :=
nat_trailing_degree_mul' (mul_ne_zero (mt trailing_coeff_eq_zero.mp hp)
(mt trailing_coeff_eq_zero.mp hq))
end semiring
section nonzero_semiring
variables [semiring R] [nontrivial R] {p q : R[X]}
@[simp] lemma trailing_degree_one : trailing_degree (1 : R[X]) = (0 : ℕ∞) :=
trailing_degree_C one_ne_zero
@[simp] lemma trailing_degree_X : trailing_degree (X : R[X]) = 1 :=
trailing_degree_monomial one_ne_zero
@[simp] lemma nat_trailing_degree_X : (X : R[X]).nat_trailing_degree = 1 :=
nat_trailing_degree_monomial one_ne_zero
end nonzero_semiring
section ring
variables [ring R]
@[simp] lemma trailing_degree_neg (p : R[X]) : trailing_degree (-p) = trailing_degree p :=
by unfold trailing_degree; rw support_neg
@[simp] lemma nat_trailing_degree_neg (p : R[X]) :
nat_trailing_degree (-p) = nat_trailing_degree p :=
by simp [nat_trailing_degree]
@[simp] lemma nat_trailing_degree_int_cast (n : ℤ) : nat_trailing_degree (n : R[X]) = 0 :=
by simp only [←C_eq_int_cast, nat_trailing_degree_C]
end ring
section semiring
variables [semiring R]
/-- The second-lowest coefficient, or 0 for constants -/
def next_coeff_up (p : R[X]) : R :=
if p.nat_trailing_degree = 0 then 0 else p.coeff (p.nat_trailing_degree + 1)
@[simp]
lemma next_coeff_up_C_eq_zero (c : R) :
next_coeff_up (C c) = 0 := by { rw next_coeff_up, simp }
lemma next_coeff_up_of_pos_nat_trailing_degree (p : R[X]) (hp : 0 < p.nat_trailing_degree) :
next_coeff_up p = p.coeff (p.nat_trailing_degree + 1) :=
by { rw [next_coeff_up, if_neg], contrapose! hp, simpa }
end semiring
section semiring
variables [semiring R] {p q : R[X]} {ι : Type*}
lemma coeff_nat_trailing_degree_eq_zero_of_trailing_degree_lt
(h : trailing_degree p < trailing_degree q) :
coeff q (nat_trailing_degree p) = 0 :=
coeff_eq_zero_of_trailing_degree_lt $ nat_trailing_degree_le_trailing_degree.trans_lt h
lemma ne_zero_of_trailing_degree_lt {n : ℕ∞} (h : trailing_degree p < n) : p ≠ 0 :=
λ h₀, h.not_le (by simp [h₀])
end semiring
end polynomial
|
3cbbf4c61479acb1338d635b5601574d0379f03b | df561f413cfe0a88b1056655515399c546ff32a5 | /6-advanced-addition-world/l10.lean | 30e2eff0d02527b7c888f50c2ec7eef4548a6927 | [] | no_license | nicholaspun/natural-number-game-solutions | 31d5158415c6f582694680044c5c6469032c2a06 | 1e2aed86d2e76a3f4a275c6d99e795ad30cf6df0 | refs/heads/main | 1,675,123,625,012 | 1,607,633,548,000 | 1,607,633,548,000 | 318,933,860 | 3 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 167 | lean | lemma add_left_eq_zero {{a b : mynat}} (H : a + b = 0) : b = 0 :=
begin
cases b with d,
refl,
rw add_succ at H,
have f := succ_ne_zero (a + d) H,
exfalso,
exact f,
end |
da787fddc085216718f4a053f59aa5494de767e4 | 78630e908e9624a892e24ebdd21260720d29cf55 | /src/logic_first_order/fol_19.lean | 65f194bcec6e5b9a3260e27b739ba02c5c34b05c | [
"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 | 287 | lean | namespace fol_19
variable U : Type
variable f : U → U → U
variable P : U → Prop
theorem fol_19 : (∀ x, P (f x x)) → (∀ x, ∃ y, P (f x y)) :=
assume h1: ∀ x, P (f x x),
assume t: U,
have h2: P (f t t), from h1 t,
show ∃ y, P (f t y), from exists.intro t h2
end fol_19 |
71e9a6e1605b317912c48fe856326f5bb407b618 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/ring_theory/localization/module.lean | e47d7f15b914afdbcf367b1fe9d3bd3cc35f2377 | [
"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,012 | lean | /-
Copyright (c) 2022 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Junyan Xu, Anne Baanen
-/
import linear_algebra.linear_independent
import ring_theory.localization.fraction_ring
import ring_theory.localization.integer
/-!
# Modules / vector spaces over localizations / fraction fields
This file contains some results about vector spaces over the field of fractions of a ring.
## Main results
* `linear_independent.localization`: `b` is linear independent over a localization of `R`
if it is linear independent over `R` itself
* `basis.localization`: promote an `R`-basis `b` to an `Rₛ`-basis,
where `Rₛ` is a localization of `R`
* `linear_independent.iff_fraction_ring`: `b` is linear independent over `R` iff it is
linear independent over `Frac(R)`
-/
open_locale big_operators
open_locale non_zero_divisors
section localization
variables {R : Type*} (Rₛ : Type*) [comm_ring R] [comm_ring Rₛ] [algebra R Rₛ]
variables (S : submonoid R) [hT : is_localization S Rₛ]
include hT
section add_comm_monoid
variables {M : Type*} [add_comm_monoid M] [module R M] [module Rₛ M] [is_scalar_tower R Rₛ M]
lemma linear_independent.localization {ι : Type*} {b : ι → M} (hli : linear_independent R b) :
linear_independent Rₛ b :=
begin
rw linear_independent_iff' at ⊢ hli,
intros s g hg i hi,
choose a g' hg' using is_localization.exist_integer_multiples S s g,
letI := λ i, classical.prop_decidable (i ∈ s),
specialize hli s (λ i, if hi : i ∈ s then g' i hi else 0) _ i hi,
{ rw [← @smul_zero _ M _ _ _ (a : R), ← hg, finset.smul_sum],
refine finset.sum_congr rfl (λ i hi, _),
dsimp only,
rw [dif_pos hi, ← is_scalar_tower.algebra_map_smul Rₛ, hg' i hi, smul_assoc],
apply_instance },
refine ((is_localization.map_units Rₛ a).mul_right_eq_zero).mp _,
rw [← algebra.smul_def, ← map_zero (algebra_map R Rₛ), ← hli],
simp [hi, hg']
end
end add_comm_monoid
section add_comm_group
variables {M : Type*} [add_comm_group M] [module R M] [module Rₛ M] [is_scalar_tower R Rₛ M]
/-- Promote a basis for `M` over `R` to a basis for `M` over the localization `Rₛ` -/
noncomputable def basis.localization {ι : Type*} (b : basis ι R M) : basis ι Rₛ M :=
basis.mk (b.linear_independent.localization Rₛ S) $
by { rw [← @submodule.restrict_scalars_eq_top_iff Rₛ R, eq_top_iff, ← b.span_eq],
apply submodule.span_le_restrict_scalars }
end add_comm_group
end localization
section fraction_ring
variables (R K : Type*) [comm_ring R] [field K] [algebra R K] [is_fraction_ring R K]
variables {V : Type*} [add_comm_group V] [module R V] [module K V] [is_scalar_tower R K V]
lemma linear_independent.iff_fraction_ring {ι : Type*} {b : ι → V} :
linear_independent R b ↔ linear_independent K b :=
⟨linear_independent.localization K (R⁰),
linear_independent.restrict_scalars (smul_left_injective R one_ne_zero)⟩
end fraction_ring
|
c1a98914b0330245492865d18840f2feaafcde8f | 4727251e0cd73359b15b664c3170e5d754078599 | /src/algebra/category/FinVect.lean | ce329a91a8816d030774c1f4825241581619aec5 | [
"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 | 4,270 | lean | /-
Copyright (c) 2021 Jakob von Raumer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jakob von Raumer
-/
import category_theory.monoidal.rigid.basic
import linear_algebra.tensor_product_basis
import linear_algebra.coevaluation
import algebra.category.Module.monoidal
/-!
# The category of finite dimensional vector spaces
This introduces `FinVect K`, the category of finite dimensional vector spaces over a field `K`.
It is implemented as a full subcategory on a subtype of `Module K`.
We first create the instance as a category, then as a monoidal category and then as a rigid monoidal
category.
## Future work
* Show that `FinVect K` is a symmetric monoidal category.
-/
noncomputable theory
open category_theory Module.monoidal_category
open_locale classical big_operators
universes u
variables (K : Type u) [field K]
/-- Define `FinVect` as the subtype of `Module.{u} K` of finite dimensional vector spaces. -/
@[derive [large_category, λ α, has_coe_to_sort α (Sort*), concrete_category]]
def FinVect := { V : Module.{u} K // finite_dimensional K V }
namespace FinVect
instance finite_dimensional (V : FinVect K) : finite_dimensional K V := V.prop
instance : inhabited (FinVect K) := ⟨⟨Module.of K K, finite_dimensional.finite_dimensional_self K⟩⟩
instance : has_coe (FinVect.{u} K) (Module.{u} K) := { coe := λ V, V.1, }
protected lemma coe_comp {U V W : FinVect K} (f : U ⟶ V) (g : V ⟶ W) :
((f ≫ g) : U → W) = (g : V → W) ∘ (f : U → V) := rfl
/-- Lift an unbundled vector space to `FinVect K`. -/
def of (V : Type u) [add_comm_group V] [module K V] [finite_dimensional K V] : FinVect K :=
⟨Module.of K V, by { change finite_dimensional K V, apply_instance }⟩
instance : has_forget₂ (FinVect.{u} K) (Module.{u} K) :=
by { dsimp [FinVect], apply_instance, }
instance : full (forget₂ (FinVect K) (Module.{u} K)) :=
{ preimage := λ X Y f, f, }
instance monoidal_category : monoidal_category (FinVect K) :=
monoidal_category.full_monoidal_subcategory
(λ V, finite_dimensional K V)
(finite_dimensional.finite_dimensional_self K)
(λ X Y hX hY, by exactI finite_dimensional_tensor_product X Y)
variables (V : FinVect K)
/-- The dual module is the dual in the rigid monoidal category `FinVect K`. -/
def FinVect_dual : FinVect K :=
⟨Module.of K (module.dual K V), subspace.module.dual.finite_dimensional⟩
instance : has_coe_to_fun (FinVect_dual K V) (λ _, V → K) :=
{ coe := λ v, by { change V →ₗ[K] K at v, exact v, } }
open category_theory.monoidal_category
/-- The coevaluation map is defined in `linear_algebra.coevaluation`. -/
def FinVect_coevaluation : 𝟙_ (FinVect K) ⟶ V ⊗ (FinVect_dual K V) :=
by apply coevaluation K V
lemma FinVect_coevaluation_apply_one : FinVect_coevaluation K V (1 : K) =
∑ (i : basis.of_vector_space_index K V),
(basis.of_vector_space K V) i ⊗ₜ[K] (basis.of_vector_space K V).coord i :=
by apply coevaluation_apply_one K V
/-- The evaluation morphism is given by the contraction map. -/
def FinVect_evaluation : (FinVect_dual K V) ⊗ V ⟶ 𝟙_ (FinVect K) :=
by apply contract_left K V
@[simp]
lemma FinVect_evaluation_apply (f : (FinVect_dual K V)) (x : V) :
(FinVect_evaluation K V) (f ⊗ₜ x) = f x :=
by apply contract_left_apply f x
private theorem coevaluation_evaluation :
let V' : FinVect K := FinVect_dual K V in
(𝟙 V' ⊗ (FinVect_coevaluation K V)) ≫ (α_ V' V V').inv ≫ (FinVect_evaluation K V ⊗ 𝟙 V')
= (ρ_ V').hom ≫ (λ_ V').inv :=
by apply contract_left_assoc_coevaluation K V
private theorem evaluation_coevaluation :
(FinVect_coevaluation K V ⊗ 𝟙 V)
≫ (α_ V (FinVect_dual K V) V).hom ≫ (𝟙 V ⊗ FinVect_evaluation K V)
= (λ_ V).hom ≫ (ρ_ V).inv :=
by apply contract_left_assoc_coevaluation' K V
instance exact_pairing : exact_pairing V (FinVect_dual K V) :=
{ coevaluation := FinVect_coevaluation K V,
evaluation := FinVect_evaluation K V,
coevaluation_evaluation' := coevaluation_evaluation K V,
evaluation_coevaluation' := evaluation_coevaluation K V }
instance right_dual : has_right_dual V := ⟨FinVect_dual K V⟩
instance right_rigid_category : right_rigid_category (FinVect K) := { }
end FinVect
|
81b5a737e76e9fa607e61ebcbba76bfe7aca50b9 | 680b0d1592ce164979dab866b232f6fa743f2cc8 | /library/data/real/basic.lean | 22cf83401c7bd74bca9eaea219e038a97181195c | [
"Apache-2.0"
] | permissive | syohex/lean | 657428ab520f8277fc18cf04bea2ad200dbae782 | 081ad1212b686780f3ff8a6d0e5f8a1d29a7d8bc | refs/heads/master | 1,611,274,838,635 | 1,452,668,188,000 | 1,452,668,188,000 | 49,562,028 | 0 | 0 | null | 1,452,675,604,000 | 1,452,675,602,000 | null | UTF-8 | Lean | false | false | 42,386 | lean | /-
Copyright (c) 2015 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
The real numbers, constructed as equivalence classes of Cauchy sequences of rationals.
This construction follows Bishop and Bridges (1985).
The construction of the reals is arranged in four files.
- basic.lean proves properties about regular sequences of rationals in the namespace rat_seq,
defines ℝ to be the quotient type of regular sequences mod equivalence, and shows ℝ is a ring
in namespace real. No classical axioms are used.
- order.lean defines an order on regular sequences and lifts the order to ℝ. In the namespace real,
ℝ is shown to be an ordered ring. No classical axioms are used.
- division.lean defines the inverse of a regular sequence and lifts this to ℝ. If a sequence is
equivalent to the 0 sequence, its inverse is the zero sequence. In the namespace real, ℝ is shown
to be an ordered field. This construction is classical.
- complete.lean
-/
import data.nat data.rat.order data.pnat
open nat eq pnat
open - [coercion] rat
local postfix `⁻¹` := pnat.inv
-- small helper lemmas
private theorem s_mul_assoc_lemma_3 (a b n : ℕ+) (p : ℚ) :
p * ((a * n)⁻¹ + (b * n)⁻¹) = p * (a⁻¹ + b⁻¹) * n⁻¹ :=
by rewrite [rat.mul_assoc, right_distrib, *pnat.inv_mul_eq_mul_inv]
private theorem s_mul_assoc_lemma_4 {n : ℕ+} {ε q : ℚ} (Hε : ε > 0) (Hq : q > 0)
(H : n ≥ pceil (q / ε)) :
q * n⁻¹ ≤ ε :=
begin
note H2 := pceil_helper H (div_pos_of_pos_of_pos Hq Hε),
note H3 := mul_le_of_le_div (div_pos_of_pos_of_pos Hq Hε) H2,
rewrite -(one_mul ε),
apply mul_le_mul_of_mul_div_le,
repeat assumption
end
private theorem find_thirds (a b : ℚ) (H : b > 0) : ∃ n : ℕ+, a + n⁻¹ + n⁻¹ + n⁻¹ < a + b :=
let n := pceil (of_nat 4 / b) in
have of_nat 3 * n⁻¹ < b, from calc
of_nat 3 * n⁻¹ < of_nat 4 * n⁻¹
: mul_lt_mul_of_pos_right dec_trivial !pnat.inv_pos
... ≤ of_nat 4 * (b / of_nat 4)
: mul_le_mul_of_nonneg_left (!inv_pceil_div dec_trivial H) !of_nat_nonneg
... = b / of_nat 4 * of_nat 4 : mul.comm
... = b : !div_mul_cancel dec_trivial,
exists.intro n (calc
a + n⁻¹ + n⁻¹ + n⁻¹ = a + (1 + 1 + 1) * n⁻¹ : by rewrite [+right_distrib, +rat.one_mul, -+add.assoc]
... = a + of_nat 3 * n⁻¹ : {show 1+1+1=of_nat 3, from dec_trivial}
... < a + b : rat.add_lt_add_left this a)
private theorem squeeze {a b : ℚ} (H : ∀ j : ℕ+, a ≤ b + j⁻¹ + j⁻¹ + j⁻¹) : a ≤ b :=
begin
apply le_of_not_gt,
intro Hb,
cases exists_add_lt_and_pos_of_lt Hb with [c, Hc],
cases find_thirds b c (and.right Hc) with [j, Hbj],
have Ha : a > b + j⁻¹ + j⁻¹ + j⁻¹, from lt.trans Hbj (and.left Hc),
apply (not_le_of_gt Ha) !H
end
private theorem rewrite_helper (a b c d : ℚ) : a * b - c * d = a * (b - d) + (a - c) * d :=
by rewrite [mul_sub_left_distrib, mul_sub_right_distrib, add_sub, sub_add_cancel]
private theorem rewrite_helper3 (a b c d e f g: ℚ) : a * (b + c) - (d * e + f * g) =
(a * b - d * e) + (a * c - f * g) :=
by rewrite [left_distrib, add_sub_comm]
private theorem rewrite_helper4 (a b c d : ℚ) : a * b - c * d = (a * b - a * d) + (a * d - c * d) :=
by rewrite[add_sub, sub_add_cancel]
private theorem rewrite_helper5 (a b x y : ℚ) : a - b = (a - x) + (x - y) + (y - b) :=
by rewrite[*add_sub, *sub_add_cancel]
private theorem rewrite_helper7 (a b c d x : ℚ) :
a * b * c - d = (b * c) * (a - x) + (x * b * c - d) :=
begin
have ∀ (a b c : ℚ), a * b * c = b * c * a,
begin
intros a b c,
rewrite (mul.right_comm b c a),
rewrite (mul.comm b a)
end,
rewrite [mul_sub_left_distrib, add_sub],
calc
a * b * c - d = a * b * c - x * b * c + x * b * c - d : sub_add_cancel
... = b * c * a - b * c * x + x * b * c - d :
begin
rewrite [this a b c, this x b c]
end
end
private theorem ineq_helper (a b : ℚ) (k m n : ℕ+) (H : a ≤ (k * 2 * m)⁻¹ + (k * 2 * n)⁻¹)
(H2 : b ≤ (k * 2 * m)⁻¹ + (k * 2 * n)⁻¹) :
(rat_of_pnat k) * a + b * (rat_of_pnat k) ≤ m⁻¹ + n⁻¹ :=
assert H3 : (k * 2 * m)⁻¹ + (k * 2 * n)⁻¹ = (2 * k)⁻¹ * (m⁻¹ + n⁻¹),
begin
rewrite [left_distrib, *pnat.inv_mul_eq_mul_inv],
rewrite (mul.comm k⁻¹)
end,
have H' : a ≤ (2 * k)⁻¹ * (m⁻¹ + n⁻¹),
begin
rewrite H3 at H,
exact H
end,
have H2' : b ≤ (2 * k)⁻¹ * (m⁻¹ + n⁻¹),
begin
rewrite H3 at H2,
exact H2
end,
have a + b ≤ k⁻¹ * (m⁻¹ + n⁻¹), from calc
a + b ≤ (2 * k)⁻¹ * (m⁻¹ + n⁻¹) + (2 * k)⁻¹ * (m⁻¹ + n⁻¹) : add_le_add H' H2'
... = ((2 * k)⁻¹ + (2 * k)⁻¹) * (m⁻¹ + n⁻¹) : by rewrite right_distrib
... = k⁻¹ * (m⁻¹ + n⁻¹) : by rewrite (pnat.add_halves k),
calc (rat_of_pnat k) * a + b * (rat_of_pnat k)
= (rat_of_pnat k) * a + (rat_of_pnat k) * b : by rewrite (mul.comm b)
... = (rat_of_pnat k) * (a + b) : left_distrib
... ≤ (rat_of_pnat k) * (k⁻¹ * (m⁻¹ + n⁻¹)) :
iff.mp (!le_iff_mul_le_mul_left !rat_of_pnat_is_pos) this
... = m⁻¹ + n⁻¹ :
by rewrite[-mul.assoc, pnat.inv_cancel_left, one_mul]
private theorem factor_lemma (a b c d e : ℚ) : abs (a + b + c - (d + (b + e))) = abs ((a - d) + (c - e)) :=
!congr_arg (calc
a + b + c - (d + (b + e)) = a + b + c - (d + b + e) : rat.add_assoc
... = a + b - (d + b) + (c - e) : add_sub_comm
... = a + b - b - d + (c - e) : sub_add_eq_sub_sub_swap
... = a - d + (c - e) : add_sub_cancel)
private theorem factor_lemma_2 (a b c d : ℚ) : (a + b) + (c + d) = (a + c) + (d + b) :=
begin
note H := (binary.comm4 add.comm add.assoc a b c d),
rewrite [add.comm b d at H],
exact H
end
--------------------------------------
-- define cauchy sequences and equivalence. show equivalence actually is one
namespace rat_seq
notation `seq` := ℕ+ → ℚ
definition regular (s : seq) := ∀ m n : ℕ+, abs (s m - s n) ≤ m⁻¹ + n⁻¹
definition equiv (s t : seq) := ∀ n : ℕ+, abs (s n - t n) ≤ n⁻¹ + n⁻¹
infix `≡` := equiv
theorem equiv.refl (s : seq) : s ≡ s :=
begin
intros,
rewrite [sub_self, abs_zero],
apply add_invs_nonneg
end
theorem equiv.symm (s t : seq) (H : s ≡ t) : t ≡ s :=
begin
intros,
rewrite [-abs_neg, neg_sub],
exact H n
end
theorem bdd_of_eq {s t : seq} (H : s ≡ t) :
∀ j : ℕ+, ∀ n : ℕ+, n ≥ 2 * j → abs (s n - t n) ≤ j⁻¹ :=
begin
intros [j, n, Hn],
apply le.trans,
apply H,
rewrite -(pnat.add_halves j),
apply add_le_add,
apply inv_ge_of_le Hn,
apply inv_ge_of_le Hn
end
theorem eq_of_bdd {s t : seq} (Hs : regular s) (Ht : regular t)
(H : ∀ j : ℕ+, ∃ Nj : ℕ+, ∀ n : ℕ+, Nj ≤ n → abs (s n - t n) ≤ j⁻¹) : s ≡ t :=
begin
intros,
have Hj : (∀ j : ℕ+, abs (s n - t n) ≤ n⁻¹ + n⁻¹ + j⁻¹ + j⁻¹ + j⁻¹), begin
intros,
cases H j with [Nj, HNj],
rewrite [-(sub_add_cancel (s n) (s (max j Nj))), +sub_eq_add_neg,
add.assoc (s n + -s (max j Nj)), ↑regular at *],
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply rat.le_trans,
apply add_le_add,
apply Hs,
rewrite [-(sub_add_cancel (s (max j Nj)) (t (max j Nj))), add.assoc],
apply abs_add_le_abs_add_abs,
apply rat.le_trans,
apply rat.add_le_add_left,
apply add_le_add,
apply HNj (max j Nj) (pnat.max_right j Nj),
apply Ht,
have hsimp : ∀ m : ℕ+, n⁻¹ + m⁻¹ + (j⁻¹ + (m⁻¹ + n⁻¹)) = n⁻¹ + n⁻¹ + j⁻¹ + (m⁻¹ + m⁻¹),
from λm, calc
n⁻¹ + m⁻¹ + (j⁻¹ + (m⁻¹ + n⁻¹)) = n⁻¹ + (j⁻¹ + (m⁻¹ + n⁻¹)) + m⁻¹ : add.right_comm
... = n⁻¹ + (j⁻¹ + m⁻¹ + n⁻¹) + m⁻¹ : add.assoc
... = n⁻¹ + (n⁻¹ + (j⁻¹ + m⁻¹)) + m⁻¹ : add.comm
... = n⁻¹ + n⁻¹ + j⁻¹ + (m⁻¹ + m⁻¹) :
by rewrite[-*add.assoc],
rewrite hsimp,
have Hms : (max j Nj)⁻¹ + (max j Nj)⁻¹ ≤ j⁻¹ + j⁻¹, begin
apply add_le_add,
apply inv_ge_of_le (pnat.max_left j Nj),
apply inv_ge_of_le (pnat.max_left j Nj),
end,
apply (calc
n⁻¹ + n⁻¹ + j⁻¹ + ((max j Nj)⁻¹ + (max j Nj)⁻¹) ≤ n⁻¹ + n⁻¹ + j⁻¹ + (j⁻¹ + j⁻¹) :
rat.add_le_add_left Hms
... = n⁻¹ + n⁻¹ + j⁻¹ + j⁻¹ + j⁻¹ : by rewrite *rat.add_assoc)
end,
apply squeeze Hj
end
theorem eq_of_bdd_var {s t : seq} (Hs : regular s) (Ht : regular t)
(H : ∀ ε : ℚ, ε > 0 → ∃ Nj : ℕ+, ∀ n : ℕ+, Nj ≤ n → abs (s n - t n) ≤ ε) : s ≡ t :=
begin
apply eq_of_bdd,
repeat assumption,
intros,
apply H,
apply pnat.inv_pos
end
theorem bdd_of_eq_var {s t : seq} (Hs : regular s) (Ht : regular t) (Heq : s ≡ t) :
∀ ε : ℚ, ε > 0 → ∃ Nj : ℕ+, ∀ n : ℕ+, Nj ≤ n → abs (s n - t n) ≤ ε :=
begin
intro ε Hε,
cases pnat_bound Hε with [N, HN],
existsi 2 * N,
intro n Hn,
apply rat.le_trans,
apply bdd_of_eq Heq N n Hn,
exact HN -- assumption -- TODO: something funny here; what is 11.source.to_has_le_2?
end
theorem equiv.trans (s t u : seq) (Hs : regular s) (Ht : regular t) (Hu : regular u)
(H : s ≡ t) (H2 : t ≡ u) : s ≡ u :=
begin
apply eq_of_bdd Hs Hu,
intros,
existsi 2 * (2 * j),
intro n Hn,
rewrite [-sub_add_cancel (s n) (t n), *sub_eq_add_neg, add.assoc],
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
have Hst : abs (s n - t n) ≤ (2 * j)⁻¹, from bdd_of_eq H _ _ Hn,
have Htu : abs (t n - u n) ≤ (2 * j)⁻¹, from bdd_of_eq H2 _ _ Hn,
rewrite -(pnat.add_halves j),
apply add_le_add,
exact Hst, exact Htu
end
-----------------------------------
-- define operations on cauchy sequences. show operations preserve regularity
private definition K (s : seq) : ℕ+ := pnat.pos (ubound (abs (s pone)) + 1 + 1) dec_trivial
private theorem canon_bound {s : seq} (Hs : regular s) (n : ℕ+) : abs (s n) ≤ rat_of_pnat (K s) :=
calc
abs (s n) = abs (s n - s pone + s pone) : by rewrite sub_add_cancel
... ≤ abs (s n - s pone) + abs (s pone) : abs_add_le_abs_add_abs
... ≤ n⁻¹ + pone⁻¹ + abs (s pone) : add_le_add_right !Hs
... = n⁻¹ + (1 + abs (s pone)) : by rewrite [pone_inv, rat.add_assoc]
... ≤ 1 + (1 + abs (s pone)) : add_le_add_right (inv_le_one n)
... = abs (s pone) + (1 + 1) :
by rewrite [add.comm 1 (abs (s pone)), add.comm 1, rat.add_assoc]
... ≤ of_nat (ubound (abs (s pone))) + (1 + 1) : add_le_add_right (!ubound_ge)
... = of_nat (ubound (abs (s pone)) + (1 + 1)) : of_nat_add
... = of_nat (ubound (abs (s pone)) + 1 + 1) : add.assoc
... = rat_of_pnat (K s) : by esimp
theorem bdd_of_regular {s : seq} (H : regular s) : ∃ b : ℚ, ∀ n : ℕ+, s n ≤ b :=
begin
existsi rat_of_pnat (K s),
intros,
apply rat.le_trans,
apply le_abs_self,
apply canon_bound H
end
theorem bdd_of_regular_strict {s : seq} (H : regular s) : ∃ b : ℚ, ∀ n : ℕ+, s n < b :=
begin
cases bdd_of_regular H with [b, Hb],
existsi b + 1,
intro n,
apply rat.lt_of_le_of_lt,
apply Hb,
apply lt_add_of_pos_right,
apply zero_lt_one
end
definition K₂ (s t : seq) := max (K s) (K t)
private theorem K₂_symm (s t : seq) : K₂ s t = K₂ t s :=
if H : K s < K t then
(assert H1 : K₂ s t = K t, from pnat.max_eq_right H,
assert H2 : K₂ t s = K t, from pnat.max_eq_left (pnat.not_lt_of_ge (pnat.le_of_lt H)),
by rewrite [H1, -H2])
else
(assert H1 : K₂ s t = K s, from pnat.max_eq_left H,
if J : K t < K s then
(assert H2 : K₂ t s = K s, from pnat.max_eq_right J, by rewrite [H1, -H2])
else
(assert Heq : K t = K s, from
pnat.eq_of_le_of_ge (pnat.le_of_not_gt H) (pnat.le_of_not_gt J),
by rewrite [↑K₂, Heq]))
theorem canon_2_bound_left (s t : seq) (Hs : regular s) (n : ℕ+) :
abs (s n) ≤ rat_of_pnat (K₂ s t) :=
calc
abs (s n) ≤ rat_of_pnat (K s) : canon_bound Hs n
... ≤ rat_of_pnat (K₂ s t) : rat_of_pnat_le_of_pnat_le (!pnat.max_left)
theorem canon_2_bound_right (s t : seq) (Ht : regular t) (n : ℕ+) :
abs (t n) ≤ rat_of_pnat (K₂ s t) :=
calc
abs (t n) ≤ rat_of_pnat (K t) : canon_bound Ht n
... ≤ rat_of_pnat (K₂ s t) : rat_of_pnat_le_of_pnat_le (!pnat.max_right)
definition sadd (s t : seq) : seq := λ n, (s (2 * n)) + (t (2 * n))
theorem reg_add_reg {s t : seq} (Hs : regular s) (Ht : regular t) : regular (sadd s t) :=
begin
rewrite [↑regular at *, ↑sadd],
intros,
rewrite add_sub_comm,
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
rewrite add_halves_double,
apply add_le_add,
apply Hs,
apply Ht
end
definition smul (s t : seq) : seq := λ n : ℕ+, (s ((K₂ s t) * 2 * n)) * (t ((K₂ s t) * 2 * n))
theorem reg_mul_reg {s t : seq} (Hs : regular s) (Ht : regular t) : regular (smul s t) :=
begin
rewrite [↑regular at *, ↑smul],
intros,
rewrite rewrite_helper,
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply rat.le_trans,
apply add_le_add,
rewrite abs_mul,
apply mul_le_mul_of_nonneg_right,
apply canon_2_bound_left s t Hs,
apply abs_nonneg,
rewrite abs_mul,
apply mul_le_mul_of_nonneg_left,
apply canon_2_bound_right s t Ht,
apply abs_nonneg,
apply ineq_helper,
apply Ht,
apply Hs
end
definition sneg (s : seq) : seq := λ n : ℕ+, - (s n)
theorem reg_neg_reg {s : seq} (Hs : regular s) : regular (sneg s) :=
begin
rewrite [↑regular at *, ↑sneg],
intros,
rewrite [-abs_neg, neg_sub, sub_neg_eq_add, add.comm],
apply Hs
end
-----------------------------------
-- show properties of +, *, -
definition zero : seq := λ n, 0
definition one : seq := λ n, 1
theorem s_add_comm (s t : seq) : sadd s t ≡ sadd t s :=
begin
esimp [sadd],
intro n,
rewrite [sub_add_eq_sub_sub, add_sub_cancel, sub_self, abs_zero],
apply add_invs_nonneg
end
theorem s_add_assoc (s t u : seq) (Hs : regular s) (Hu : regular u) :
sadd (sadd s t) u ≡ sadd s (sadd t u) :=
begin
rewrite [↑sadd, ↑equiv, ↑regular at *],
intros,
rewrite factor_lemma,
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply rat.le_trans,
rotate 1,
apply add_le_add_right,
apply inv_two_mul_le_inv,
rewrite [-(pnat.add_halves (2 * n)), -(pnat.add_halves n), factor_lemma_2],
apply add_le_add,
apply Hs,
apply Hu
end
theorem s_mul_comm (s t : seq) : smul s t ≡ smul t s :=
begin
rewrite ↑smul,
intros n,
rewrite [*(K₂_symm s t), rat.mul_comm, sub_self, abs_zero],
apply add_invs_nonneg
end
private definition DK (s t : seq) := (K₂ s t) * 2
private theorem DK_rewrite (s t : seq) : (K₂ s t) * 2 = DK s t := rfl
private definition TK (s t u : seq) := (DK (λ (n : ℕ+), s (mul (DK s t) n) * t (mul (DK s t) n)) u)
private theorem TK_rewrite (s t u : seq) :
(DK (λ (n : ℕ+), s (mul (DK s t) n) * t (mul (DK s t) n)) u) = TK s t u := rfl
private theorem s_mul_assoc_lemma (s t u : seq) (a b c d : ℕ+) :
abs (s a * t a * u b - s c * t d * u d) ≤ abs (t a) * abs (u b) * abs (s a - s c) +
abs (s c) * abs (t a) * abs (u b - u d) + abs (s c) * abs (u d) * abs (t a - t d) :=
begin
rewrite (rewrite_helper7 _ _ _ _ (s c)),
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
rewrite rat.add_assoc,
apply add_le_add,
rewrite 2 abs_mul,
apply le.refl,
rewrite [*rat.mul_assoc, -mul_sub_left_distrib, -left_distrib, abs_mul],
apply mul_le_mul_of_nonneg_left,
rewrite rewrite_helper,
apply le.trans,
apply abs_add_le_abs_add_abs,
apply add_le_add,
rewrite abs_mul, apply rat.le_refl,
rewrite [abs_mul, rat.mul_comm], apply rat.le_refl,
apply abs_nonneg
end
private definition Kq (s : seq) := rat_of_pnat (K s) + 1
private theorem Kq_bound {s : seq} (H : regular s) : ∀ n, abs (s n) ≤ Kq s :=
begin
intros,
apply le_of_lt,
apply lt_of_le_of_lt,
apply canon_bound H,
apply lt_add_of_pos_right,
apply zero_lt_one
end
private theorem Kq_bound_nonneg {s : seq} (H : regular s) : 0 ≤ Kq s :=
le.trans !abs_nonneg (Kq_bound H 2)
private theorem Kq_bound_pos {s : seq} (H : regular s) : 0 < Kq s :=
have H1 : 0 ≤ rat_of_pnat (K s), from rat.le_trans (!abs_nonneg) (canon_bound H 2),
add_pos_of_nonneg_of_pos H1 rat.zero_lt_one
private theorem s_mul_assoc_lemma_5 {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(a b c : ℕ+) : abs (t a) * abs (u b) * abs (s a - s c) ≤ (Kq t) * (Kq u) * (a⁻¹ + c⁻¹) :=
begin
repeat apply mul_le_mul,
apply Kq_bound Ht,
apply Kq_bound Hu,
apply abs_nonneg,
apply Kq_bound_nonneg Ht,
apply Hs,
apply abs_nonneg,
apply rat.mul_nonneg,
apply Kq_bound_nonneg Ht,
apply Kq_bound_nonneg Hu,
end
private theorem s_mul_assoc_lemma_2 {s t u : seq} (Hs : regular s) (Ht : regular t)
(Hu : regular u) (a b c d : ℕ+) :
abs (t a) * abs (u b) * abs (s a - s c) + abs (s c) * abs (t a) * abs (u b - u d)
+ abs (s c) * abs (u d) * abs (t a - t d) ≤
(Kq t) * (Kq u) * (a⁻¹ + c⁻¹) + (Kq s) * (Kq t) * (b⁻¹ + d⁻¹) + (Kq s) * (Kq u) * (a⁻¹ + d⁻¹) :=
begin
apply add_le_add_three,
repeat (assumption | apply mul_le_mul | apply Kq_bound | apply Kq_bound_nonneg |
apply abs_nonneg),
apply Hs,
apply abs_nonneg,
apply rat.mul_nonneg,
repeat (assumption | apply mul_le_mul | apply Kq_bound | apply Kq_bound_nonneg |
apply abs_nonneg),
apply Hu,
apply abs_nonneg,
apply rat.mul_nonneg,
repeat (assumption | apply mul_le_mul | apply Kq_bound | apply Kq_bound_nonneg |
apply abs_nonneg),
apply Ht,
apply abs_nonneg,
apply rat.mul_nonneg,
repeat (apply Kq_bound_nonneg; assumption)
end
theorem s_mul_assoc {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) :
smul (smul s t) u ≡ smul s (smul t u) :=
begin
apply eq_of_bdd_var,
repeat apply reg_mul_reg,
apply Hs,
apply Ht,
apply Hu,
apply reg_mul_reg Hs,
apply reg_mul_reg Ht Hu,
intros,
apply exists.intro,
intros,
rewrite [↑smul, *DK_rewrite, *TK_rewrite, -*pnat.mul_assoc, -*mul.assoc],
apply rat.le_trans,
apply s_mul_assoc_lemma,
apply rat.le_trans,
apply s_mul_assoc_lemma_2,
apply Hs,
apply Ht,
apply Hu,
rewrite [*s_mul_assoc_lemma_3, -distrib_three_right],
apply s_mul_assoc_lemma_4,
apply a,
repeat apply add_pos,
repeat apply mul_pos,
apply Kq_bound_pos Ht,
apply Kq_bound_pos Hu,
apply add_pos,
repeat apply pnat.inv_pos,
repeat apply rat.mul_pos,
apply Kq_bound_pos Hs,
apply Kq_bound_pos Ht,
apply add_pos,
repeat apply pnat.inv_pos,
repeat apply rat.mul_pos,
apply Kq_bound_pos Hs,
apply Kq_bound_pos Hu,
apply add_pos,
repeat apply pnat.inv_pos,
apply a_1
end
theorem zero_is_reg : regular zero :=
begin
rewrite [↑regular, ↑zero],
intros,
rewrite [sub_zero, abs_zero],
apply add_invs_nonneg
end
theorem s_zero_add (s : seq) (H : regular s) : sadd zero s ≡ s :=
begin
rewrite [↑sadd, ↑zero, ↑equiv, ↑regular at H],
intros,
rewrite [rat.zero_add],
apply rat.le_trans,
apply H,
apply add_le_add,
apply inv_two_mul_le_inv,
apply rat.le_refl
end
theorem s_add_zero (s : seq) (H : regular s) : sadd s zero ≡ s :=
begin
rewrite [↑sadd, ↑zero, ↑equiv, ↑regular at H],
intros,
rewrite [rat.add_zero],
apply rat.le_trans,
apply H,
apply add_le_add,
apply inv_two_mul_le_inv,
apply rat.le_refl
end
theorem s_neg_cancel (s : seq) (H : regular s) : sadd (sneg s) s ≡ zero :=
begin
rewrite [↑sadd, ↑sneg, ↑regular at H, ↑zero, ↑equiv],
intros,
rewrite [neg_add_eq_sub, sub_self, sub_zero, abs_zero],
apply add_invs_nonneg
end
theorem neg_s_cancel (s : seq) (H : regular s) : sadd s (sneg s) ≡ zero :=
begin
apply equiv.trans,
rotate 3,
apply s_add_comm,
apply s_neg_cancel s H,
repeat (apply reg_add_reg | apply reg_neg_reg | assumption),
apply zero_is_reg
end
theorem add_well_defined {s t u v : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(Hv : regular v) (Esu : s ≡ u) (Etv : t ≡ v) : sadd s t ≡ sadd u v :=
begin
rewrite [↑sadd, ↑equiv at *],
intros,
rewrite [add_sub_comm, add_halves_double],
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply add_le_add,
apply Esu,
apply Etv
end
set_option tactic.goal_names false
private theorem mul_bound_helper {s t : seq} (Hs : regular s) (Ht : regular t) (a b c : ℕ+)
(j : ℕ+) :
∃ N : ℕ+, ∀ n : ℕ+, n ≥ N → abs (s (a * n) * t (b * n) - s (c * n) * t (c * n)) ≤ j⁻¹ :=
begin
existsi pceil (((rat_of_pnat (K s)) * (b⁻¹ + c⁻¹) + (a⁻¹ + c⁻¹) *
(rat_of_pnat (K t))) * (rat_of_pnat j)),
intros n Hn,
rewrite rewrite_helper4,
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply rat.le_trans,
rotate 1,
show n⁻¹ * ((rat_of_pnat (K s)) * (b⁻¹ + c⁻¹)) +
n⁻¹ * ((a⁻¹ + c⁻¹) * (rat_of_pnat (K t))) ≤ j⁻¹, begin
rewrite -left_distrib,
apply rat.le_trans,
apply mul_le_mul_of_nonneg_right,
apply pceil_helper Hn,
{ repeat (apply mul_pos | apply add_pos | apply rat_of_pnat_is_pos |
apply pnat.inv_pos) },
apply rat.le_of_lt,
apply add_pos,
apply rat.mul_pos,
apply rat_of_pnat_is_pos,
apply add_pos,
apply pnat.inv_pos,
apply pnat.inv_pos,
apply rat.mul_pos,
apply add_pos,
apply pnat.inv_pos,
apply pnat.inv_pos,
apply rat_of_pnat_is_pos,
have H : (rat_of_pnat (K s) * (b⁻¹ + c⁻¹) + (a⁻¹ + c⁻¹) * rat_of_pnat (K t)) ≠ 0, begin
apply ne_of_gt,
repeat (apply mul_pos | apply add_pos | apply rat_of_pnat_is_pos | apply pnat.inv_pos),
end,
rewrite (!div_helper H),
apply rat.le_refl
end,
apply add_le_add,
rewrite [-mul_sub_left_distrib, abs_mul],
apply rat.le_trans,
apply mul_le_mul,
apply canon_bound,
apply Hs,
apply Ht,
apply abs_nonneg,
apply rat.le_of_lt,
apply rat_of_pnat_is_pos,
rewrite [*pnat.inv_mul_eq_mul_inv, -right_distrib, -rat.mul_assoc, rat.mul_comm],
apply mul_le_mul_of_nonneg_left,
apply rat.le_refl,
apply rat.le_of_lt,
apply pnat.inv_pos,
rewrite [-mul_sub_right_distrib, abs_mul],
apply rat.le_trans,
apply mul_le_mul,
apply Hs,
apply canon_bound,
apply Ht,
apply abs_nonneg,
apply add_invs_nonneg,
rewrite [*pnat.inv_mul_eq_mul_inv, -right_distrib, mul.comm _ n⁻¹, rat.mul_assoc],
apply mul_le_mul,
repeat apply rat.le_refl,
apply rat.le_of_lt,
apply rat.mul_pos,
apply add_pos,
repeat apply pnat.inv_pos,
apply rat_of_pnat_is_pos,
apply rat.le_of_lt,
apply pnat.inv_pos
end
theorem s_distrib {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) :
smul s (sadd t u) ≡ sadd (smul s t) (smul s u) :=
begin
apply eq_of_bdd,
repeat (assumption | apply reg_add_reg | apply reg_mul_reg),
intros,
let exh1 := λ a b c, mul_bound_helper Hs Ht a b c (2 * j),
apply exists.elim,
apply exh1,
rotate 3,
intros N1 HN1,
let exh2 := λ d e f, mul_bound_helper Hs Hu d e f (2 * j),
apply exists.elim,
apply exh2,
rotate 3,
intros N2 HN2,
existsi max N1 N2,
intros n Hn,
rewrite [↑sadd at *, ↑smul, rewrite_helper3, -pnat.add_halves j, -*pnat.mul_assoc at *],
apply rat.le_trans,
apply abs_add_le_abs_add_abs,
apply add_le_add,
apply HN1,
apply pnat.le_trans,
apply pnat.max_left N1 N2,
apply Hn,
apply HN2,
apply pnat.le_trans,
apply pnat.max_right N1 N2,
apply Hn
end
theorem mul_zero_equiv_zero {s t : seq} (Hs : regular s) (Ht : regular t) (Htz : t ≡ zero) :
smul s t ≡ zero :=
begin
apply eq_of_bdd_var,
apply reg_mul_reg Hs Ht,
apply zero_is_reg,
intro ε Hε,
let Bd := bdd_of_eq_var Ht zero_is_reg Htz (ε / (Kq s))
(div_pos_of_pos_of_pos Hε (Kq_bound_pos Hs)),
cases Bd with [N, HN],
existsi N,
intro n Hn,
rewrite [↑equiv at Htz, ↑zero at *, sub_zero, ↑smul, abs_mul],
apply le.trans,
apply mul_le_mul,
apply Kq_bound Hs,
have HN' : ∀ (n : ℕ+), N ≤ n → abs (t n) ≤ ε / Kq s,
from λ n, (eq.subst (sub_zero (t n)) (HN n)),
apply HN',
apply pnat.le_trans Hn,
apply pnat.mul_le_mul_left,
apply abs_nonneg,
apply le_of_lt (Kq_bound_pos Hs),
rewrite (mul_div_cancel' (ne.symm (ne_of_lt (Kq_bound_pos Hs)))),
apply le.refl
end
private theorem neg_bound_eq_bound (s : seq) : K (sneg s) = K s :=
by rewrite [↑K, ↑sneg, abs_neg]
private theorem neg_bound2_eq_bound2 (s t : seq) : K₂ s (sneg t) = K₂ s t :=
by rewrite [↑K₂, neg_bound_eq_bound]
private theorem sneg_def (s : seq) : (λ (n : ℕ+), -(s n)) = sneg s := rfl
theorem mul_neg_equiv_neg_mul {s t : seq} : smul s (sneg t) ≡ sneg (smul s t) :=
begin
rewrite [↑equiv, ↑smul],
intros,
rewrite [↑sneg, *sub_neg_eq_add, -neg_mul_eq_mul_neg, add.comm, *sneg_def,
*neg_bound2_eq_bound2, add.right_inv, abs_zero],
apply add_invs_nonneg
end
theorem equiv_of_diff_equiv_zero {s t : seq} (Hs : regular s) (Ht : regular t)
(H : sadd s (sneg t) ≡ zero) : s ≡ t :=
begin
have hsimp : ∀ a b c d e : ℚ, a + b + c + (d + e) = b + d + a + e + c, from
λ a b c d e, calc
a + b + c + (d + e) = a + b + (d + e) + c : add.right_comm
... = a + (b + d) + e + c : by rewrite[-*add.assoc]
... = b + d + a + e + c : add.comm,
apply eq_of_bdd Hs Ht,
intros,
note He := bdd_of_eq H,
existsi 2 * (2 * (2 * j)),
intros n Hn,
rewrite (rewrite_helper5 _ _ (s (2 * n)) (t (2 * n))),
apply rat.le_trans,
apply abs_add_three,
apply rat.le_trans,
apply add_le_add_three,
apply Hs,
rewrite [↑sadd at He, ↑sneg at He, ↑zero at He],
let He' := λ a b c, eq.subst !sub_zero (He a b c),
apply (He' _ _ Hn),
apply Ht,
rewrite [hsimp, pnat.add_halves, -(pnat.add_halves j), -(pnat.add_halves (2 * j)), -*rat.add_assoc],
apply add_le_add_right,
apply add_le_add_three,
repeat (apply rat.le_trans; apply inv_ge_of_le Hn; apply inv_two_mul_le_inv)
end
theorem s_sub_cancel (s : seq) : sadd s (sneg s) ≡ zero :=
begin
rewrite [↑equiv, ↑sadd, ↑sneg, ↑zero],
intros,
rewrite [sub_zero, add.right_inv, abs_zero],
apply add_invs_nonneg
end
theorem diff_equiv_zero_of_equiv {s t : seq} (Hs : regular s) (Ht : regular t) (H : s ≡ t) :
sadd s (sneg t) ≡ zero :=
begin
apply equiv.trans,
rotate 4,
apply s_sub_cancel t,
rotate 2,
apply zero_is_reg,
apply add_well_defined,
repeat (assumption | apply reg_neg_reg),
apply equiv.refl,
repeat (assumption | apply reg_add_reg | apply reg_neg_reg)
end
private theorem mul_well_defined_half1 {s t u : seq} (Hs : regular s) (Ht : regular t)
(Hu : regular u) (Etu : t ≡ u) : smul s t ≡ smul s u :=
begin
apply equiv_of_diff_equiv_zero,
rotate 2,
apply equiv.trans,
rotate 3,
apply equiv.symm,
apply add_well_defined,
rotate 4,
apply equiv.refl,
apply mul_neg_equiv_neg_mul,
apply equiv.trans,
rotate 3,
apply equiv.symm,
apply s_distrib,
rotate 3,
apply mul_zero_equiv_zero,
rotate 2,
apply diff_equiv_zero_of_equiv,
repeat (assumption | apply reg_mul_reg | apply reg_neg_reg | apply reg_add_reg |
apply zero_is_reg)
end
private theorem mul_well_defined_half2 {s t u : seq} (Hs : regular s) (Ht : regular t)
(Hu : regular u) (Est : s ≡ t) : smul s u ≡ smul t u :=
begin
apply equiv.trans,
rotate 3,
apply s_mul_comm,
apply equiv.trans,
rotate 3,
apply mul_well_defined_half1,
rotate 2,
apply Ht,
rotate 1,
apply s_mul_comm,
repeat (assumption | apply reg_mul_reg)
end
theorem mul_well_defined {s t u v : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u)
(Hv : regular v) (Esu : s ≡ u) (Etv : t ≡ v) : smul s t ≡ smul u v :=
begin
apply equiv.trans,
exact reg_mul_reg Hs Ht,
exact reg_mul_reg Hs Hv,
exact reg_mul_reg Hu Hv,
apply mul_well_defined_half1,
repeat assumption,
apply mul_well_defined_half2,
repeat assumption
end
theorem neg_well_defined {s t : seq} (Est : s ≡ t) : sneg s ≡ sneg t :=
begin
rewrite [↑sneg, ↑equiv at *],
intros,
rewrite [-abs_neg, neg_sub, sub_neg_eq_add, add.comm],
apply Est
end
theorem one_is_reg : regular one :=
begin
rewrite [↑regular, ↑one],
intros,
rewrite [sub_self, abs_zero],
apply add_invs_nonneg
end
theorem s_one_mul {s : seq} (H : regular s) : smul one s ≡ s :=
begin
intros,
rewrite [↑smul, ↑one, rat.one_mul],
apply rat.le_trans,
apply H,
apply add_le_add_right,
apply pnat.inv_mul_le_inv
end
theorem s_mul_one {s : seq} (H : regular s) : smul s one ≡ s :=
begin
apply equiv.trans,
apply reg_mul_reg H one_is_reg,
rotate 2,
apply s_mul_comm,
apply s_one_mul H,
apply reg_mul_reg one_is_reg H,
apply H
end
theorem zero_nequiv_one : ¬ zero ≡ one :=
begin
intro Hz,
rewrite [↑equiv at Hz, ↑zero at Hz, ↑one at Hz],
note H := Hz (2 * 2),
rewrite [zero_sub at H, abs_neg at H, pnat.add_halves at H],
have H' : pone⁻¹ ≤ 2⁻¹, from calc
pone⁻¹ = 1 : by rewrite -pone_inv
... = abs 1 : abs_of_pos zero_lt_one
... ≤ 2⁻¹ : H,
let H'' := ge_of_inv_le H',
apply absurd (one_lt_two) (pnat.not_lt_of_ge H'')
end
---------------------------------------------
-- constant sequences
definition const (a : ℚ) : seq := λ n, a
theorem const_reg (a : ℚ) : regular (const a) :=
begin
intros,
rewrite [↑const, sub_self, abs_zero],
apply add_invs_nonneg
end
theorem add_consts (a b : ℚ) : sadd (const a) (const b) ≡ const (a + b) :=
by apply equiv.refl
theorem mul_consts (a b : ℚ) : smul (const a) (const b) ≡ const (a * b) :=
by apply equiv.refl
theorem neg_const (a : ℚ) : sneg (const a) ≡ const (-a) :=
by apply equiv.refl
section
open rat
lemma eq_of_const_equiv {a b : ℚ} (H : const a ≡ const b) : a = b :=
have H₁ : ∀ n : ℕ+, abs (a - b) ≤ n⁻¹ + n⁻¹, from H,
eq_of_forall_abs_sub_le
(take ε,
suppose ε > 0,
have ε / 2 > 0, begin exact div_pos_of_pos_of_pos this two_pos end,
obtain n (Hn : n⁻¹ ≤ ε / 2), from pnat_bound this,
show abs (a - b) ≤ ε, from calc
abs (a - b) ≤ n⁻¹ + n⁻¹ : H₁ n
... ≤ ε / 2 + ε / 2 : add_le_add Hn Hn
... = ε : add_halves)
end
---------------------------------------------
-- create the type of regular sequences and lift theorems
record reg_seq : Type :=
(sq : seq) (is_reg : regular sq)
definition requiv (s t : reg_seq) := (reg_seq.sq s) ≡ (reg_seq.sq t)
definition requiv.refl (s : reg_seq) : requiv s s := equiv.refl (reg_seq.sq s)
definition requiv.symm (s t : reg_seq) (H : requiv s t) : requiv t s :=
equiv.symm (reg_seq.sq s) (reg_seq.sq t) H
definition requiv.trans (s t u : reg_seq) (H : requiv s t) (H2 : requiv t u) : requiv s u :=
equiv.trans _ _ _ (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) H H2
definition radd (s t : reg_seq) : reg_seq :=
reg_seq.mk (sadd (reg_seq.sq s) (reg_seq.sq t))
(reg_add_reg (reg_seq.is_reg s) (reg_seq.is_reg t))
infix + := radd
definition rmul (s t : reg_seq) : reg_seq :=
reg_seq.mk (smul (reg_seq.sq s) (reg_seq.sq t))
(reg_mul_reg (reg_seq.is_reg s) (reg_seq.is_reg t))
infix * := rmul
definition rneg (s : reg_seq) : reg_seq :=
reg_seq.mk (sneg (reg_seq.sq s)) (reg_neg_reg (reg_seq.is_reg s))
prefix - := rneg
definition radd_well_defined {s t u v : reg_seq} (H : requiv s u) (H2 : requiv t v) :
requiv (s + t) (u + v) :=
add_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) (reg_seq.is_reg v) H H2
definition rmul_well_defined {s t u v : reg_seq} (H : requiv s u) (H2 : requiv t v) :
requiv (s * t) (u * v) :=
mul_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) (reg_seq.is_reg v) H H2
definition rneg_well_defined {s t : reg_seq} (H : requiv s t) : requiv (-s) (-t) :=
neg_well_defined H
theorem requiv_is_equiv : equivalence requiv :=
mk_equivalence requiv requiv.refl requiv.symm requiv.trans
definition reg_seq.to_setoid [instance] : setoid reg_seq :=
⦃setoid, r := requiv, iseqv := requiv_is_equiv⦄
definition r_zero : reg_seq :=
reg_seq.mk (zero) (zero_is_reg)
definition r_one : reg_seq :=
reg_seq.mk (one) (one_is_reg)
theorem r_add_comm (s t : reg_seq) : requiv (s + t) (t + s) :=
s_add_comm (reg_seq.sq s) (reg_seq.sq t)
theorem r_add_assoc (s t u : reg_seq) : requiv (s + t + u) (s + (t + u)) :=
s_add_assoc (reg_seq.sq s) (reg_seq.sq t) (reg_seq.sq u) (reg_seq.is_reg s) (reg_seq.is_reg u)
theorem r_zero_add (s : reg_seq) : requiv (r_zero + s) s :=
s_zero_add (reg_seq.sq s) (reg_seq.is_reg s)
theorem r_add_zero (s : reg_seq) : requiv (s + r_zero) s :=
s_add_zero (reg_seq.sq s) (reg_seq.is_reg s)
theorem r_neg_cancel (s : reg_seq) : requiv (-s + s) r_zero :=
s_neg_cancel (reg_seq.sq s) (reg_seq.is_reg s)
theorem r_mul_comm (s t : reg_seq) : requiv (s * t) (t * s) :=
s_mul_comm (reg_seq.sq s) (reg_seq.sq t)
theorem r_mul_assoc (s t u : reg_seq) : requiv (s * t * u) (s * (t * u)) :=
s_mul_assoc (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u)
theorem r_mul_one (s : reg_seq) : requiv (s * r_one) s :=
s_mul_one (reg_seq.is_reg s)
theorem r_one_mul (s : reg_seq) : requiv (r_one * s) s :=
s_one_mul (reg_seq.is_reg s)
theorem r_distrib (s t u : reg_seq) : requiv (s * (t + u)) (s * t + s * u) :=
s_distrib (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u)
theorem r_zero_nequiv_one : ¬ requiv r_zero r_one :=
zero_nequiv_one
definition r_const (a : ℚ) : reg_seq := reg_seq.mk (const a) (const_reg a)
theorem r_add_consts (a b : ℚ) : requiv (r_const a + r_const b) (r_const (a + b)) := add_consts a b
theorem r_mul_consts (a b : ℚ) : requiv (r_const a * r_const b) (r_const (a * b)) := mul_consts a b
theorem r_neg_const (a : ℚ) : requiv (-r_const a) (r_const (-a)) := neg_const a
end rat_seq
----------------------------------------------
-- take quotients to get ℝ and show it's a comm ring
open rat_seq
definition real := quot reg_seq.to_setoid
namespace real
notation `ℝ` := real
protected definition prio := num.pred rat.prio
protected definition add (x y : ℝ) : ℝ :=
(quot.lift_on₂ x y (λ a b, quot.mk (a + b))
(take a b c d : reg_seq, take Hab : requiv a c, take Hcd : requiv b d,
quot.sound (radd_well_defined Hab Hcd)))
--infix [priority real.prio] + := add
protected definition mul (x y : ℝ) : ℝ :=
(quot.lift_on₂ x y (λ a b, quot.mk (a * b))
(take a b c d : reg_seq, take Hab : requiv a c, take Hcd : requiv b d,
quot.sound (rmul_well_defined Hab Hcd)))
--infix [priority real.prio] * := mul
protected definition neg (x : ℝ) : ℝ :=
(quot.lift_on x (λ a, quot.mk (-a)) (take a b : reg_seq, take Hab : requiv a b,
quot.sound (rneg_well_defined Hab)))
--prefix [priority real.prio] `-` := neg
definition real_has_add [reducible] [instance] [priority real.prio] : has_add real :=
has_add.mk real.add
definition real_has_mul [reducible] [instance] [priority real.prio] : has_mul real :=
has_mul.mk real.mul
definition real_has_neg [reducible] [instance] [priority real.prio] : has_neg real :=
has_neg.mk real.neg
protected definition sub [reducible] (a b : ℝ) : real := a + (-b)
definition real_has_sub [reducible] [instance] [priority real.prio] : has_sub real :=
has_sub.mk real.sub
open rat -- no coercions before
definition of_rat [coercion] (a : ℚ) : ℝ := quot.mk (r_const a)
definition of_int [coercion] (i : ℤ) : ℝ := int.to.real i
definition of_nat [coercion] (n : ℕ) : ℝ := nat.to.real n
definition of_num [coercion] [reducible] (n : num) : ℝ := of_rat (rat.of_num n)
definition real_has_zero [reducible] [instance] [priority real.prio] : has_zero real :=
has_zero.mk (of_rat 0)
definition real_has_one [reducible] [instance] [priority real.prio] : has_one real :=
has_one.mk (of_rat 1)
theorem real_zero_eq_rat_zero : (0:real) = of_rat (0:rat) :=
rfl
theorem real_one_eq_rat_one : (1:real) = of_rat (1:rat) :=
rfl
protected theorem add_comm (x y : ℝ) : x + y = y + x :=
quot.induction_on₂ x y (λ s t, quot.sound (r_add_comm s t))
protected theorem add_assoc (x y z : ℝ) : x + y + z = x + (y + z) :=
quot.induction_on₃ x y z (λ s t u, quot.sound (r_add_assoc s t u))
protected theorem zero_add (x : ℝ) : 0 + x = x :=
quot.induction_on x (λ s, quot.sound (r_zero_add s))
protected theorem add_zero (x : ℝ) : x + 0 = x :=
quot.induction_on x (λ s, quot.sound (r_add_zero s))
protected theorem neg_cancel (x : ℝ) : -x + x = 0 :=
quot.induction_on x (λ s, quot.sound (r_neg_cancel s))
protected theorem mul_assoc (x y z : ℝ) : x * y * z = x * (y * z) :=
quot.induction_on₃ x y z (λ s t u, quot.sound (r_mul_assoc s t u))
protected theorem mul_comm (x y : ℝ) : x * y = y * x :=
quot.induction_on₂ x y (λ s t, quot.sound (r_mul_comm s t))
protected theorem one_mul (x : ℝ) : 1 * x = x :=
quot.induction_on x (λ s, quot.sound (r_one_mul s))
protected theorem mul_one (x : ℝ) : x * 1 = x :=
quot.induction_on x (λ s, quot.sound (r_mul_one s))
protected theorem left_distrib (x y z : ℝ) : x * (y + z) = x * y + x * z :=
quot.induction_on₃ x y z (λ s t u, quot.sound (r_distrib s t u))
protected theorem right_distrib (x y z : ℝ) : (x + y) * z = x * z + y * z :=
by rewrite [real.mul_comm, real.left_distrib, {x * _}real.mul_comm, {y * _}real.mul_comm]
protected theorem zero_ne_one : ¬ (0 : ℝ) = 1 :=
take H : 0 = 1,
absurd (quot.exact H) (r_zero_nequiv_one)
protected definition comm_ring [reducible] : comm_ring ℝ :=
begin
fapply comm_ring.mk,
exact add,
exact real.add_assoc,
exact of_num 0,
exact real.zero_add,
exact real.add_zero,
exact neg,
exact real.neg_cancel,
exact real.add_comm,
exact mul,
exact real.mul_assoc,
apply of_num 1,
apply real.one_mul,
apply real.mul_one,
apply real.left_distrib,
apply real.right_distrib,
apply real.mul_comm
end
theorem of_int_eq (a : ℤ) : of_int a = of_rat (rat.of_int a) := rfl
theorem of_nat_eq (a : ℕ) : of_nat a = of_rat (rat.of_nat a) := rfl
theorem of_rat.inj {x y : ℚ} (H : of_rat x = of_rat y) : x = y :=
eq_of_const_equiv (quot.exact H)
theorem eq_of_of_rat_eq_of_rat {x y : ℚ} (H : of_rat x = of_rat y) : x = y :=
of_rat.inj H
theorem of_rat_eq_of_rat_iff (x y : ℚ) : of_rat x = of_rat y ↔ x = y :=
iff.intro eq_of_of_rat_eq_of_rat !congr_arg
theorem of_int.inj {a b : ℤ} (H : of_int a = of_int b) : a = b :=
rat.of_int.inj (of_rat.inj H)
theorem eq_of_of_int_eq_of_int {a b : ℤ} (H : of_int a = of_int b) : a = b :=
of_int.inj H
theorem of_int_eq_of_int_iff (a b : ℤ) : of_int a = of_int b ↔ a = b :=
iff.intro of_int.inj !congr_arg
theorem of_nat.inj {a b : ℕ} (H : of_nat a = of_nat b) : a = b :=
int.of_nat.inj (of_int.inj H)
theorem eq_of_of_nat_eq_of_nat {a b : ℕ} (H : of_nat a = of_nat b) : a = b :=
of_nat.inj H
theorem of_nat_eq_of_nat_iff (a b : ℕ) : of_nat a = of_nat b ↔ a = b :=
iff.intro of_nat.inj !congr_arg
theorem of_rat_add (a b : ℚ) : of_rat (a + b) = of_rat a + of_rat b :=
quot.sound (r_add_consts a b)
theorem of_rat_neg (a : ℚ) : of_rat (-a) = -of_rat a :=
eq.symm (quot.sound (r_neg_const a))
theorem of_rat_mul (a b : ℚ) : of_rat (a * b) = of_rat a * of_rat b :=
quot.sound (r_mul_consts a b)
open int
theorem of_int_add (a b : ℤ) : of_int (a + b) = of_int a + of_int b :=
by rewrite [of_int_eq, rat.of_int_add, of_rat_add]
theorem of_int_neg (a : ℤ) : of_int (-a) = -of_int a :=
by rewrite [of_int_eq, rat.of_int_neg, of_rat_neg]
theorem of_int_mul (a b : ℤ) : of_int (a * b) = of_int a * of_int b :=
by rewrite [of_int_eq, rat.of_int_mul, of_rat_mul]
theorem of_nat_add (a b : ℕ) : of_nat (a + b) = of_nat a + of_nat b :=
by rewrite [of_nat_eq, rat.of_nat_add, of_rat_add]
theorem of_nat_mul (a b : ℕ) : of_nat (a * b) = of_nat a * of_nat b :=
by rewrite [of_nat_eq, rat.of_nat_mul, of_rat_mul]
theorem add_half_of_rat (n : ℕ+) : of_rat (2 * n)⁻¹ + of_rat (2 * n)⁻¹ = of_rat (n⁻¹) :=
by rewrite [-of_rat_add, pnat.add_halves]
theorem one_add_one : 1 + 1 = (2 : ℝ) := rfl
end real
|
9db8a1e3cb0e4ce8ca3afef88bfa79da9896a684 | 75c54c8946bb4203e0aaf196f918424a17b0de99 | /src/completion.lean | 6545a984e6c4860ffe3f25f3aa74925d2ec903cb | [
"Apache-2.0"
] | permissive | urkud/flypitch | 261e2a45f1038130178575406df8aea78255ba77 | 2250f5eda14b6ef9fc3e4e1f4a9ac4005634de5c | refs/heads/master | 1,653,266,469,246 | 1,577,819,679,000 | 1,577,819,679,000 | 259,862,235 | 1 | 0 | Apache-2.0 | 1,588,147,244,000 | 1,588,147,244,000 | null | UTF-8 | Lean | false | false | 16,067 | lean | /-
Copyright (c) 2019 The Flypitch Project. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jesse Han, Floris van Doorn
-/
/- Show that every theory can be extended to a complete theory -/
import .compactness order.zorn
local attribute [instance, priority 0] classical.prop_decidable
open fol set
universe variables u v
section
parameter L : Language.{u}
open classical zorn
lemma inconsis_not_of_provable {L} {T : Theory L} {f : sentence L} :
T ⊢' f → ¬ is_consistent (T ∪ {∼f}) :=
begin
intro H, suffices : (T ∪ {∼f}) ⊢' (⊥ : sentence L), by tidy,
apply snot_and_self' _, exact f, apply nonempty.intro, apply andI,
apply weakening, show set (formula L), exact T.fst, tidy, exact or.inr (by assumption),
exact classical.choice H, apply axm, tidy
end
lemma provable_of_inconsis_not {L} {T : Theory L} {f : sentence L} :
¬ is_consistent (T ∪ {∼f}) → T ⊢' f :=
begin
by_contra, simp[*,-a] at a, cases a with a1 a2, apply consis_not_of_not_provable a2,
exact classical.by_contradiction (by tidy)
end
/-- Given a theory T and a sentence ψ, either T ∪ {ψ} or T ∪ {∼ ψ} is consistent.--/
lemma can_extend {L : Language} (T : Theory L) (ψ : sentence L) (h : is_consistent T) :
is_consistent (T ∪ {ψ}) ∨ is_consistent (T ∪ {∼ ψ}) :=
begin
simp only [is_consistent, set.union_singleton], by_contra,
rw[not_or_distrib] at a, rcases a with ⟨H1, H2⟩,
suffices : T ⊢' (⊥ : sentence L), by contradiction,
exact snot_and_self'' (simpI' (classical.by_contradiction H1)) (simpI' (classical.by_contradiction H2))
end
-- simp[is_consistent], by_contra, rename a hc, rw[not_or_distrib] at hc,
-- have hc1 := classical.by_contradiction hc.1,
-- have hc2 := classical.by_contradiction hc.2,
-- have hc_uno : T ⊢' ∼ψ,
-- exact hc1.map simpI,
-- have hc_dos : T ⊢' ∼∼ψ,
-- exact hc2.map simpI,
-- exact hc_dos.map2 (impE _) hc_uno
/-
Now, we have to show that given an arbitrary chain in this poset, we can obtain an upper bound in this chain. We do this by taking the union.
-/
/- Given a set of theories and a proof that they form a chain under set-inclusion, return their union and a proof that this contains every theory in the chain
-/
lemma nonempty_of_not_empty {α : Type u} (s : set α) (h : ¬ s = ∅) : nonempty s :=
by rwa [coe_nonempty_iff_ne_empty]
/-- Theory_over T is the subtype of Theory L consisting of consistent theories T' such that T' ⊇ T--/
def Theory_over {L : Language.{u}} (T : Theory L) (hT : is_consistent T): Type u :=
{T' : Theory L // T ⊆ T' ∧ is_consistent T'}
/-- Every theory T is trivially a theory over itself --/
def over_self {L : Language} (T : Theory L) (hT : is_consistent T): Theory_over T hT:=
by {refine ⟨T, _⟩, split, trivial, assumption}
/-- Given two consistent theories T₁ and T₂ over T, say that T₁ ⊆ T₂ if T₁ ⊆ T₂--/
def Theory_over_subset {L : Language.{u}} {T : Theory L} {hT : is_consistent T} : Theory_over T hT → Theory_over T hT→ Prop
:= λ T1 T2, T1.val ⊆ T2.val
instance {T : Theory L} {hT : is_consistent T} : has_subset (Theory_over T hT) := ⟨Theory_over_subset⟩
instance {T : Theory L} {hT : is_consistent T} : nonempty (Theory_over T hT) := ⟨over_self T hT⟩
/- Given a sentence and the hypothesis that ψ is provable from a theory T, return a list of sentences from T and a proof that this list proves ψ -/
-- TODO: refactor this away, use theory_proof_compactness
noncomputable def theory_proof_compactness' {L : Language} (T : Theory L) (ψ : sentence L) (hψ : T ⊢' ψ) : Σ' Γ : list (sentence L), {ϕ : sentence L | ϕ ∈ Γ} ⊢' ψ ∧ {ϕ : sentence L | ϕ ∈ Γ} ⊆ T :=
begin
apply psigma_of_exists,
rcases theory_proof_compactness hψ with ⟨Γ, H, K⟩,
cases Γ with Γ hΓ, induction Γ, swap, refl,
exact ⟨Γ, H, K⟩
end
/- Given a chain of sets with nonempty union, conclude that the chain is nonempty-/
def nonempty_chain_of_nonempty_union {α : Type u} {A_i : set $ set α} {h_chain : chain (⊆) A_i}
(h : nonempty $ set.sUnion A_i) : nonempty A_i :=
by { unfreezeI, rcases h with ⟨a, s, hs, ha⟩, exact ⟨⟨s, hs⟩⟩ }
/- Given two elements in a chain of sets over T, their union over T is in the chain -/
lemma in_chain_of_union {α : Type u} (T : set α) (A_i : set $ set α)
(h_chain : chain set.subset A_i) (as : list A_i) (h_over_T : ∀ A ∈ A_i, T ⊆ A) (A1 A2 ∈ A_i) :
A1 ∪ A2 = A1 ∨ A1 ∪ A2 = A2 :=
begin
dedup,
unfold has_union.union set.union has_mem.mem set.mem,
unfold chain set.pairwise_on at h_chain,
by_cases A1 = A2,
simp*, finish,
have := h_chain A1 H A2 H_1 h, cases this,
{fapply or.inr, apply funext, intro x, apply propext, split,
intro h1, have : A1 x ∨ A2 x, by assumption, fapply or.elim, exact A1 x, exact A2 x, assumption,
intro hx, dedup, unfold set.subset at this, exact this hx, finish,
intro hx, apply or.inr, assumption},
{fapply or.inl, apply funext, intro x, apply propext, split,
intro hx, have : A1 x ∨ A2 x, by assumption, fapply or.elim, exact A2 x, exact A1 x, finish,
intro h2x, dedup, unfold set.subset at this, exact this h2x, finish,
intro h3x, apply or.inl, assumption}
end
/--Given a chain and two elements from this chain, return their maximum. --/
noncomputable def max_in_chain {α : Type u} {R : α → α → Prop} {Ts : set α}
{nonempty_Ts : nonempty Ts} (h_chain : chain R Ts) (S1 S2 : α) (h_S1 : S1 ∈ Ts) (h_S2 : S2 ∈ Ts) :
Σ' (S : α), (S = S1 ∧ (R S2 S1 ∨ S1 = S2)) ∨ (S = S2 ∧ (R S1 S2 ∨ S1 = S2)) :=
begin
unfold chain set.pairwise_on at h_chain,
have := h_chain S1 h_S1 S2 h_S2,
by_cases S1 = S2,
refine ⟨S1, _ ⟩, fapply or.inl, fapply and.intro, exact rfl, exact or.inr h,
have H := this h,
by_cases R S1 S2,
refine ⟨S2, _⟩, fapply or.inr, refine and.intro rfl _, exact or.inl h,
tactic.unfreeze_local_instances, dedup, simp[*, -H] at H, refine ⟨S1, _⟩, fapply or.inl,
refine and.intro rfl _, exact or.inl H
end
/--Given a nonempty chain under a transitive relation and a list of elements from this chain, return an upper bound, with the maximum of the empty list defined to be the witness to the nonempty --/
noncomputable def max_of_list_in_chain {α : Type u} {R : α → α → Prop} {trans : ∀{a b c}, R a b → R b c → R a c} {Ts : set α} {nonempty_Ts : nonempty Ts} (h_chain : chain R Ts) (Ss : list α) -- {nonempty_Ss : nonempty {S | S ∈ Ss}}
(h_fs : ∀ S ∈ Ss, S ∈ Ts) : Σ' (S : α), S ∈ Ts ∧ (∀ S' ∈ Ss, S' = S ∨ R S' S) :=
begin
induction Ss,
{tactic.unfreeze_local_instances, have := (classical.choice nonempty_Ts),
from ⟨this.1, ⟨this.2, by finish⟩⟩},
specialize Ss_ih (by simp at h_fs; from h_fs.right),
rcases Ss_ih with ⟨S,H_mem,H_s⟩,
by_cases (R S Ss_hd),
{use Ss_hd, use (by simp*), intros S' HS', cases HS',
from or.inl ‹_›, right, by_cases S' = S, rwa[h], finish},
{use S, use H_mem, intros S' HS', cases HS',
{subst HS', by_cases S' = S, from or.inl ‹_›,
unfold chain pairwise_on at h_chain,
specialize h_chain S' (by simp at h_fs; from h_fs.left) S ‹_› ‹_›, finish},
finish}
end
/-- Given a xs : list α, it is naturally a list {x ∈ α | x ∈ xs} --/
def list_is_list_of_subtype : Π(α : Type u), Π (fs : list α), Σ' xs : list ↥{f : α | f ∈ fs}, ∀ f, ∀ h : f ∈ fs, (⟨f,h⟩ : ↥{f : α | f ∈ fs}) ∈ xs :=
begin
intros α fs, induction fs with fs_hd fs_tl ih,
{ exact ⟨[], by simp⟩ },
{ let F : {f | f ∈ fs_tl} → {f | f ∈ list.cons fs_hd fs_tl},
by {intro f, refine ⟨f, _⟩, fapply or.inr, exact f.property},
refine ⟨_,_⟩,
{ refine _::_,
{ exact ⟨fs_hd, by simp⟩ },
{ exact list.map F ih.fst } },
{ intro a, classical, by_cases a = fs_hd,
{ finish },
{ tidy, right, tidy }}},
end
/-- The limit theory of a chain of consistent theories over T is consistent --/
lemma consis_limit {L : Language} {T : Theory L} {hT : is_consistent T} (Ts : set (Theory_over T hT)) (h_chain : chain Theory_over_subset Ts) : is_consistent (T ∪ set.sUnion (subtype.val '' Ts)) :=
begin
intro h_inconsis,
by_cases nonempty Ts, swap,
{ simp at h, simp[*, -h_inconsis] at h_inconsis, unfold is_consistent at hT, apply hT,
rw [←union_empty T], convert h_inconsis, symmetry, apply bUnion_empty },
have Γpair := theory_proof_compactness' (T ∪ ⋃₀(subtype.val '' Ts)) ⊥ h_inconsis,
have h_bad : ∃ T' : (Theory L), (T' ∈ (subtype.val '' Ts)) ∧ {ψ | ψ ∈ Γpair.fst} ⊆ T',
{cases Γpair with fs Hfs, rename h hTs,
have dSs : Π f ∈ fs, Σ' S_f : (Theory_over T hT), set.mem S_f Ts ∧ (set.mem (f) (S_f.val)), -- to each f in fs, associate an S_f containing f from the chain
{ intros f hf, have H := Hfs.right,
unfold set.image set.sUnion set.subset set.mem list.mem at H,
have H' := H hf, by_cases f ∈ T,
split, swap, {exact (classical.choice hTs).val},
{fapply and.intro, exact (choice hTs).property,
have H := (choice hTs).val.property.left,
exact H h},
simp[*, -H'] at H',
have witness := instantiate_existential H', simp* at witness,
split, swap, split, swap, exact witness.val, cases witness.property with case1 case2, cases case1 with case1' case1'', exact case1',
split, have witness_property := witness.property, cases witness_property with case1 case2, cases case1 with case1' case1'', exact case1'',
have witness_property := witness.property, cases witness_property with case1 case2, exact case2,},
have T_max : Σ' (T_max : Theory_over T hT), (T_max ∈ Ts) ∧ ∀ ψ ∈ fs, (ψ) ∈ T_max.val, -- get the theory and a proof that it contains all the f
{ let F : {f | f ∈ fs} → Theory_over T hT :=
begin intro f, exact (dSs f.val f.property).fst end,
let fs_list_subtype := list_is_list_of_subtype _ fs,
let T_list : list (Theory_over T hT) :=
begin fapply list.map F, exact fs_list_subtype.fst end,
have T_list_subset_Ts : (∀ (S : Theory_over T hT), S ∈ T_list → S ∈ Ts),
intro S, simp [-sigma.exists, -sigma.forall], intros x h1 h2, simp [*,-h2] at h2, rw[<-h2.right],
have := (dSs x h1).snd.left, assumption,
have max_of_list := max_of_list_in_chain h_chain T_list T_list_subset_Ts,
split, swap,
{exact max_of_list.fst},
{split, exact max_of_list.snd.left,
{intros f hf,
have almost_there : f ∈ (F ⟨f, begin simpa end⟩).val, simp*, exact (dSs f hf).snd.right,
have nearly_there : (F ⟨f, begin simpa end⟩) ⊆ max_of_list.fst,
have := max_of_list.snd.right (F ⟨f, begin simpa end⟩),
have so_close : F ⟨f, _⟩ = max_of_list.fst ∨ Theory_over_subset (F ⟨f, _⟩) (max_of_list.fst),
begin
refine this _, simp [*, -sigma.exists], fapply exists.intro, exact f, fapply exists.intro,
exact hf, fapply and.intro, unfold has_mem.mem list.mem,
{apply fs_list_subtype.snd},
{refl},
end,
cases so_close with case1 case2,
rw[case1], intros a h, exact h,
exact case2,
exact nearly_there almost_there,
},
},
{intros a b c, unfold Theory_over_subset, fapply subset.trans},
{assumption}},
fapply exists.intro, exact T_max.fst.val,
fapply and.intro, fapply set.mem_image_of_mem, exact T_max.snd.left,
have := T_max.snd.right, intros ψ hψ, exact this ψ hψ},
let T_bad := @strong_indefinite_description (Theory L) (λ S, S ∈ (subtype.val '' Ts) ∧ ({ϕ | ϕ ∈ Γpair.fst} ⊆ S)) begin apply_instance end,
have T_bad_inconsis : sprovable T_bad.val ⊥,
fapply nonempty.intro,
fapply sweakening (T_bad.property h_bad).right,
exact classical.choice Γpair.snd.left,
have T_bad_consis : is_consistent T_bad.val,
{have almost_done := (T_bad.property h_bad).left,
simp[set.image] at almost_done,
cases almost_done with H _, from H.right},
exact T_bad_consis T_bad_inconsis,
end
/-- Given a chain of consistent extensions of a theory T, return the union of those theories and a proof that this is a consistent extension of T --/
def limit_theory {L : Language} {T : Theory L} {hT : is_consistent T} (Ts : set (Theory_over T hT)) (h_chain : chain Theory_over_subset Ts) : Σ' T : Theory_over T hT, ∀ T' ∈ Ts, T' ⊆ T :=
begin
refine ⟨⟨T ∪ set.sUnion (subtype.val '' Ts), _⟩, _⟩, simp*, intro,
exact @consis_limit L T hT Ts h_chain begin simp* end,
intros T' hT' ψ hψ, right, split, swap, exact T'.val,
apply exists.intro, swap, exact hψ, simp*, exact T'.property
end
/-- Given a theory T, show that the poset of theories over T satisfies the hypotheses of Zorn's lemma --/
lemma can_use_zorn {L : Language.{u}} {T : Theory L} {hT : is_consistent T} : (∀c, @chain (Theory_over T hT) Theory_over_subset c → ∃ub, ∀a∈c, a ⊆ ub) ∧ (∀(a b c : Theory_over T hT), a ⊆ b → b ⊆ c → a ⊆ c) :=
begin
split, intro Ts, intro h_chain, let S := limit_theory Ts h_chain,
let T_infty := S.fst, let H_infty := S.snd,
refine exists.intro _ _, exact T_infty, intro T', intro H',
finish, tidy
end
/-- Given a consistent theory T, return a maximal extension of it given by Zorn's lemma, along with the proof that it is consistent and maximal --/
noncomputable def maximal_extension (L : Language.{u}) (T : Theory L) (hT : is_consistent T) :
Σ' (T_max : Theory_over T hT), ∀ T' : Theory_over T hT, T_max ⊆ T' → T' ⊆ T_max :=
begin
let X := strong_indefinite_description (λ T_max : Theory_over T hT, ∀ T' : Theory_over T hT, T_max ⊆ T' → T' ⊆ T_max ) begin apply_instance end,
have := @can_use_zorn L T, rename this h_can_use,
have := exists_maximal_of_chains_bounded h_can_use.left h_can_use.right, rename this h_zorn,
let T_max := X.val, let H := X.property,
exact ⟨T_max, H h_zorn⟩,
end
/-- The maximal extension returned by maximal_extension cannot be extended. --/
lemma cannot_extend_maximal_extension {L : Language} {T : Theory L} {hT : is_consistent T} (T_max' : Σ' (T_max : Theory_over T hT), ∀ T' : Theory_over T hT, T_max ⊆ T' → T' ⊆ T_max) (ψ : sentence L) (H : is_consistent (T_max'.fst.val ∪ {ψ}))(H1 : ψ ∉ T_max'.fst.val) : false :=
begin
let T_bad : Theory_over T hT :=
by {refine ⟨T_max'.fst.val ∪ {ψ}, ⟨_, H⟩⟩, simp[has_subset.subset], intros ψ hψT,
dedup, have extension_assumption := T_max'.fst.property.left, simp[has_insert.insert],
from or.inr (extension_assumption ‹_›)},
have h_bad := T_max'.snd T_bad,
from absurd (h_bad (by finish) (by simp[has_insert.insert])) H1
end
/-- Given a maximal consistent extension of consistent theory T, show it is complete --/
lemma complete_maximal_extension_of_consis {L : Language} {T : Theory L} {hT : is_consistent T}: @is_complete L (@maximal_extension L T hT).fst.val :=
begin
refine ⟨(@maximal_extension L T hT).fst.property.right, _⟩,
intro ψ, by_cases ψ ∈ (@maximal_extension L T hT).fst.val, exact or.inl h,
apply or.inr,
by_contra,
have can_extend := @can_extend L (@maximal_extension L T hT).fst.val ψ (@maximal_extension L T hT).fst.property.right,
have h_max := (@maximal_extension L T hT).snd,
by_cases is_consistent ((@maximal_extension L T hT).fst.val ∪ {ψ}),
{rename h h1,
from cannot_extend_maximal_extension _ _ ‹_› ‹_›},
{rename h h2,
have q_of_not_p : ∀ p q : Prop, ∀ h1 : p ∨ q, ∀ h2 : ¬ p, q := by tauto,
have h2' := q_of_not_p _ _ can_extend h2,
from cannot_extend_maximal_extension _ _ ‹_› ‹_›}
end
/-- Given a consistent theory, return a complete extension of it --/
noncomputable def completion_of_consis : Π ( T : Theory L) (h_consis : is_consistent T), Σ' T' : (Theory_over T h_consis), is_complete T'.val :=
λ T h_consis, ⟨(maximal_extension L T h_consis).fst, by apply complete_maximal_extension_of_consis⟩
end
|
21793fe674f9e91b5d11f9d756ca1c91d85f4f29 | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /tests/lean/run/vec_inv3.lean | a6e927979d0dca39a8fae6899dee7733d9393351 | [
"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 | 716 | lean | import data.nat.basic data.empty data.prod
open nat eq.ops prod
inductive vector (T : Type) : ℕ → Type :=
nil {} : vector T 0,
cons : T → ∀{n}, vector T n → vector T (succ n)
set_option pp.metavar_args true
set_option pp.implicit true
set_option pp.notation false
namespace vector
variables {A B C : Type}
variables {n m : nat}
theorem z_cases_on {C : vector A 0 → Type} (v : vector A 0) (Hnil : C nil) : C v :=
by cases v; apply Hnil
protected definition destruct (v : vector A (succ n)) {P : Π {n : nat}, vector A (succ n) → Type}
(H : Π {n : nat} (h : A) (t : vector A n), P (cons h t)) : P v :=
by cases v with (h', n', t'); apply (H h' t')
end vector
|
763fc023073ea4f923001613cc2a4a9beae4c478 | 8b9f17008684d796c8022dab552e42f0cb6fb347 | /library/data/int/basic.lean | 1e5025e5a649561359bc0a8783730f9f8664f667 | [
"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 | 35,359 | lean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: int.basic
Authors: Floris van Doorn, Jeremy Avigad
The integers, with addition, multiplication, and subtraction. The representation of the integers is
chosen to compute efficiently.
To faciliate proving things about these operations, we show that the integers are a quotient of
ℕ × ℕ with the usual equivalence relation, ≡, and functions
abstr : ℕ × ℕ → ℤ
repr : ℤ → ℕ × ℕ
satisfying:
abstr_repr (a : ℤ) : abstr (repr a) = a
repr_abstr (p : ℕ × ℕ) : repr (abstr p) ≡ p
abstr_eq (p q : ℕ × ℕ) : p ≡ q → abstr p = abstr q
For example, to "lift" statements about add to statements about padd, we need to prove the
following:
repr_add (a b : ℤ) : repr (a + b) = padd (repr a) (repr b)
padd_congr (p p' q q' : ℕ × ℕ) (H1 : p ≡ p') (H2 : q ≡ q') : padd p q ≡ p' q'
-/
import data.nat.basic data.nat.order data.nat.sub data.prod
import algebra.relation algebra.binary algebra.ordered_ring
import tools.fake_simplifier
open eq.ops
open prod relation nat
open decidable binary fake_simplifier
/- the type of integers -/
inductive int : Type :=
| of_nat : nat → int
| neg_succ_of_nat : nat → int
notation `ℤ` := int
attribute int.of_nat [coercion]
definition int.of_num [coercion] [reducible] (n : num) : ℤ := int.of_nat (nat.of_num n)
namespace int
/- definitions of basic functions -/
definition neg_of_nat (m : ℕ) : ℤ :=
nat.cases_on m 0 (take m', neg_succ_of_nat m')
definition sub_nat_nat (m n : ℕ) : ℤ :=
nat.cases_on (n - m)
(of_nat (m - n)) -- m ≥ n
(take k, neg_succ_of_nat k) -- m < n, and n - m = succ k
definition neg (a : ℤ) : ℤ :=
int.cases_on a
(take m, -- a = of_nat m
nat.cases_on m 0 (take m', neg_succ_of_nat m'))
(take m, of_nat (succ m)) -- a = neg_succ_of_nat m
definition add (a b : ℤ) : ℤ :=
int.cases_on a
(take m, -- a = of_nat m
int.cases_on b
(take n, of_nat (m + n)) -- b = of_nat n
(take n, sub_nat_nat m (succ n))) -- b = neg_succ_of_nat n
(take m, -- a = neg_succ_of_nat m
int.cases_on b
(take n, sub_nat_nat n (succ m)) -- b = of_nat n
(take n, neg_of_nat (succ m + succ n))) -- b = neg_succ_of_nat n
definition mul (a b : ℤ) : ℤ :=
int.cases_on a
(take m, -- a = of_nat m
int.cases_on b
(take n, of_nat (m * n)) -- b = of_nat n
(take n, neg_of_nat (m * succ n))) -- b = neg_succ_of_nat n
(take m, -- a = neg_succ_of_nat m
int.cases_on b
(take n, neg_of_nat (succ m * n)) -- b = of_nat n
(take n, of_nat (succ m * succ n))) -- b = neg_succ_of_nat n
/- notation -/
notation `-[` n `+1]` := int.neg_succ_of_nat n -- for pretty-printing output
prefix - := int.neg
infix + := int.add
infix * := int.mul
/- some basic functions and properties -/
theorem of_nat.inj {m n : ℕ} (H : of_nat m = of_nat n) : m = n :=
int.no_confusion H (λe, e)
theorem neg_succ_of_nat.inj {m n : ℕ} (H : neg_succ_of_nat m = neg_succ_of_nat n) : m = n :=
int.no_confusion H (λe, e)
theorem neg_succ_of_nat_eq (n : ℕ) : -[n +1] = -(n + 1) := rfl
definition has_decidable_eq [instance] : decidable_eq ℤ :=
take a b,
int.cases_on a
(take m,
int.cases_on b
(take n,
if H : m = n then inl (congr_arg of_nat H) else inr (take H1, H (of_nat.inj H1)))
(take n', inr (assume H, int.no_confusion H)))
(take m',
int.cases_on b
(take n, inr (assume H, int.no_confusion H))
(take n',
(if H : m' = n' then inl (congr_arg neg_succ_of_nat H) else
inr (take H1, H (neg_succ_of_nat.inj H1)))))
theorem of_nat_add_of_nat (n m : nat) : of_nat n + of_nat m = #nat n + m := rfl
theorem of_nat_succ (n : ℕ) : of_nat (succ n) = of_nat n + 1 := rfl
theorem of_nat_mul_of_nat (n m : ℕ) : of_nat n * of_nat m = n * m := rfl
theorem sub_nat_nat_of_ge {m n : ℕ} (H : m ≥ n) : sub_nat_nat m n = of_nat (m - n) :=
have H1 : n - m = 0, from sub_eq_zero_of_le H,
calc
sub_nat_nat m n = nat.cases_on 0 (of_nat (m - n)) _ : H1 ▸ rfl
... = of_nat (m - n) : rfl
section
local attribute sub_nat_nat [reducible]
theorem sub_nat_nat_of_lt {m n : ℕ} (H : m < n) :
sub_nat_nat m n = neg_succ_of_nat (pred (n - m)) :=
have H1 : n - m = succ (pred (n - m)), from (succ_pred_of_pos (sub_pos_of_lt H))⁻¹,
calc
sub_nat_nat m n = nat.cases_on (succ (pred (n - m))) (of_nat (m - n))
(take k, neg_succ_of_nat k) : H1 ▸ rfl
... = neg_succ_of_nat (pred (n - m)) : rfl
end
definition nat_abs (a : ℤ) : ℕ := int.cases_on a (take n, n) (take n', succ n')
theorem nat_abs_of_nat (n : ℕ) : nat_abs (of_nat n) = n := rfl
theorem nat_abs_eq_zero {a : ℤ} : nat_abs a = 0 → a = 0 :=
int.cases_on a
(take m, assume H : nat_abs (of_nat m) = 0, congr_arg of_nat H)
(take m', assume H : nat_abs (neg_succ_of_nat m') = 0, absurd H (succ_ne_zero _))
/- int is a quotient of ordered pairs of natural numbers -/
protected definition equiv (p q : ℕ × ℕ) : Prop := pr1 p + pr2 q = pr2 p + pr1 q
local notation p `≡` q := equiv p q
protected theorem equiv.refl {p : ℕ × ℕ} : p ≡ p := !add.comm
protected theorem equiv.symm {p q : ℕ × ℕ} (H : p ≡ q) : q ≡ p :=
calc
pr1 q + pr2 p = pr2 p + pr1 q : !add.comm
... = pr1 p + pr2 q : H⁻¹
... = pr2 q + pr1 p : !add.comm
protected theorem equiv.trans {p q r : ℕ × ℕ} (H1 : p ≡ q) (H2 : q ≡ r) : p ≡ r :=
have H3 : pr1 p + pr2 r + pr2 q = pr2 p + pr1 r + pr2 q, from
calc
pr1 p + pr2 r + pr2 q = pr1 p + pr2 q + pr2 r : by simp
... = pr2 p + pr1 q + pr2 r : {H1}
... = pr2 p + (pr1 q + pr2 r) : by simp
... = pr2 p + (pr2 q + pr1 r) : {H2}
... = pr2 p + pr1 r + pr2 q : by simp,
show pr1 p + pr2 r = pr2 p + pr1 r, from add.cancel_right H3
protected theorem equiv_equiv : is_equivalence equiv :=
is_equivalence.mk @equiv.refl @equiv.symm @equiv.trans
protected theorem equiv_cases {p q : ℕ × ℕ} (H : equiv p q) :
(pr1 p ≥ pr2 p ∧ pr1 q ≥ pr2 q) ∨ (pr1 p < pr2 p ∧ pr1 q < pr2 q) :=
or.elim (@le_or_gt (pr2 p) (pr1 p))
(assume H1: pr1 p ≥ pr2 p,
have H2 : pr2 p + pr1 q ≥ pr2 p + pr2 q, from H ▸ add_le_add_right H1 (pr2 q),
or.inl (and.intro H1 (le_of_add_le_add_left H2)))
(assume H1: pr1 p < pr2 p,
have H2 : pr2 p + pr1 q < pr2 p + pr2 q, from H ▸ add_lt_add_right H1 (pr2 q),
or.inr (and.intro H1 (lt_of_add_lt_add_left H2)))
protected theorem equiv_of_eq {p q : ℕ × ℕ} (H : p = q) : p ≡ q := H ▸ equiv.refl
calc_trans equiv.trans
calc_refl equiv.refl
calc_symm equiv.symm
/- the representation and abstraction functions -/
definition abstr (a : ℕ × ℕ) : ℤ := sub_nat_nat (pr1 a) (pr2 a)
theorem abstr_of_ge {p : ℕ × ℕ} (H : pr1 p ≥ pr2 p) : abstr p = of_nat (pr1 p - pr2 p) :=
sub_nat_nat_of_ge H
theorem abstr_of_lt {p : ℕ × ℕ} (H : pr1 p < pr2 p) :
abstr p = neg_succ_of_nat (pred (pr2 p - pr1 p)) :=
sub_nat_nat_of_lt H
definition repr (a : ℤ) : ℕ × ℕ := int.cases_on a (take m, (m, 0)) (take m, (0, succ m))
theorem abstr_repr (a : ℤ) : abstr (repr a) = a :=
int.cases_on a (take m, (sub_nat_nat_of_ge (zero_le m))) (take m, rfl)
theorem repr_sub_nat_nat (m n : ℕ) : repr (sub_nat_nat m n) ≡ (m, n) :=
or.elim (@le_or_gt n m)
(take H : m ≥ n,
have H1 : repr (sub_nat_nat m n) = (m - n, 0), from sub_nat_nat_of_ge H ▸ rfl,
H1⁻¹ ▸
(calc
m - n + n = m : sub_add_cancel H
... = 0 + m : zero_add))
(take H : m < n,
have H1 : repr (sub_nat_nat m n) = (0, succ (pred (n - m))), from sub_nat_nat_of_lt H ▸ rfl,
H1⁻¹ ▸
(calc
0 + n = n : zero_add
... = n - m + m : sub_add_cancel (le_of_lt H)
... = succ (pred (n - m)) + m : (succ_pred_of_pos (sub_pos_of_lt H))⁻¹))
theorem repr_abstr (p : ℕ × ℕ) : repr (abstr p) ≡ p :=
!prod.eta ▸ !repr_sub_nat_nat
theorem abstr_eq {p q : ℕ × ℕ} (Hequiv : p ≡ q) : abstr p = abstr q :=
or.elim (equiv_cases Hequiv)
(assume H2,
have H3 : pr1 p ≥ pr2 p, from and.elim_left H2,
have H4 : pr1 q ≥ pr2 q, from and.elim_right H2,
have H5 : pr1 p = pr1 q - pr2 q + pr2 p, from
calc
pr1 p = pr1 p + pr2 q - pr2 q : add_sub_cancel
... = pr2 p + pr1 q - pr2 q : Hequiv
... = pr2 p + (pr1 q - pr2 q) : add_sub_assoc H4
... = pr1 q - pr2 q + pr2 p : add.comm,
have H6 : pr1 p - pr2 p = pr1 q - pr2 q, from
calc
pr1 p - pr2 p = pr1 q - pr2 q + pr2 p - pr2 p : H5
... = pr1 q - pr2 q : add_sub_cancel,
abstr_of_ge H3 ⬝ congr_arg of_nat H6 ⬝ (abstr_of_ge H4)⁻¹)
(assume H2,
have H3 : pr1 p < pr2 p, from and.elim_left H2,
have H4 : pr1 q < pr2 q, from and.elim_right H2,
have H5 : pr2 p = pr2 q - pr1 q + pr1 p, from
calc
pr2 p = pr2 p + pr1 q - pr1 q : add_sub_cancel
... = pr1 p + pr2 q - pr1 q : Hequiv
... = pr1 p + (pr2 q - pr1 q) : add_sub_assoc (le_of_lt H4)
... = pr2 q - pr1 q + pr1 p : add.comm,
have H6 : pr2 p - pr1 p = pr2 q - pr1 q, from
calc
pr2 p - pr1 p = pr2 q - pr1 q + pr1 p - pr1 p : H5
... = pr2 q - pr1 q : add_sub_cancel,
abstr_of_lt H3 ⬝ congr_arg neg_succ_of_nat (congr_arg pred H6)⬝ (abstr_of_lt H4)⁻¹)
theorem equiv_iff (p q : ℕ × ℕ) : (p ≡ q) ↔ ((p ≡ p) ∧ (q ≡ q) ∧ (abstr p = abstr q)) :=
iff.intro
(assume H : equiv p q,
and.intro !equiv.refl (and.intro !equiv.refl (abstr_eq H)))
(assume H : equiv p p ∧ equiv q q ∧ abstr p = abstr q,
have H1 : abstr p = abstr q, from and.elim_right (and.elim_right H),
equiv.trans (H1 ▸ equiv.symm (repr_abstr p)) (repr_abstr q))
theorem eq_abstr_of_equiv_repr {a : ℤ} {p : ℕ × ℕ} (Hequiv : repr a ≡ p) : a = abstr p :=
calc
a = abstr (repr a) : abstr_repr
... = abstr p : abstr_eq Hequiv
theorem eq_of_repr_equiv_repr {a b : ℤ} (H : repr a ≡ repr b) : a = b :=
calc
a = abstr (repr a) : abstr_repr
... = abstr (repr b) : abstr_eq H
... = b : abstr_repr
section
local attribute abstr [reducible]
local attribute dist [reducible]
theorem nat_abs_abstr (p : ℕ × ℕ) : nat_abs (abstr p) = dist (pr1 p) (pr2 p) :=
let m := pr1 p, n := pr2 p in
or.elim (@le_or_gt n m)
(assume H : m ≥ n,
calc
nat_abs (abstr (m, n)) = nat_abs (of_nat (m - n)) : int.abstr_of_ge H
... = dist m n : dist_eq_sub_of_ge H)
(assume H : m < n,
calc
nat_abs (abstr (m, n)) = nat_abs (neg_succ_of_nat (pred (n - m))) : int.abstr_of_lt H
... = succ (pred (n - m)) : rfl
... = n - m : succ_pred_of_pos (sub_pos_of_lt H)
... = dist m n : dist_eq_sub_of_le (le_of_lt H))
end
theorem cases_of_nat (a : ℤ) : (∃n : ℕ, a = of_nat n) ∨ (∃n : ℕ, a = - of_nat n) :=
int.cases_on a
(take n, or.inl (exists.intro n rfl))
(take n', or.inr (exists.intro (succ n') rfl))
theorem cases_of_nat_succ (a : ℤ) : (∃n : ℕ, a = of_nat n) ∨ (∃n : ℕ, a = - (of_nat (succ n))) :=
int.cases_on a (take m, or.inl (exists.intro _ rfl)) (take m, or.inr (exists.intro _ rfl))
theorem by_cases_of_nat {P : ℤ → Prop} (a : ℤ)
(H1 : ∀n : ℕ, P (of_nat n)) (H2 : ∀n : ℕ, P (- of_nat n)) :
P a :=
or.elim (cases_of_nat a)
(assume H, obtain (n : ℕ) (H3 : a = n), from H, H3⁻¹ ▸ H1 n)
(assume H, obtain (n : ℕ) (H3 : a = -n), from H, H3⁻¹ ▸ H2 n)
theorem by_cases_of_nat_succ {P : ℤ → Prop} (a : ℤ)
(H1 : ∀n : ℕ, P (of_nat n)) (H2 : ∀n : ℕ, P (- of_nat (succ n))) :
P a :=
or.elim (cases_of_nat_succ a)
(assume H, obtain (n : ℕ) (H3 : a = n), from H, H3⁻¹ ▸ H1 n)
(assume H, obtain (n : ℕ) (H3 : a = -(succ n)), from H, H3⁻¹ ▸ H2 n)
/-
int is a ring
-/
/- addition -/
definition padd (p q : ℕ × ℕ) : ℕ × ℕ := (pr1 p + pr1 q, pr2 p + pr2 q)
theorem repr_add (a b : ℤ) : repr (add a b) ≡ padd (repr a) (repr b) :=
int.cases_on a
(take m,
int.cases_on b
(take n, !equiv.refl)
(take n',
have H1 : equiv (repr (add (of_nat m) (neg_succ_of_nat n'))) (m, succ n'),
from !repr_sub_nat_nat,
have H2 : padd (repr (of_nat m)) (repr (neg_succ_of_nat n')) = (m, 0 + succ n'),
from rfl,
(!zero_add ▸ H2)⁻¹ ▸ H1))
(take m',
int.cases_on b
(take n,
have H1 : equiv (repr (add (neg_succ_of_nat m') (of_nat n))) (n, succ m'),
from !repr_sub_nat_nat,
have H2 : padd (repr (neg_succ_of_nat m')) (repr (of_nat n)) = (0 + n, succ m'),
from rfl,
(!zero_add ▸ H2)⁻¹ ▸ H1)
(take n',!repr_sub_nat_nat))
theorem padd_congr {p p' q q' : ℕ × ℕ} (Ha : p ≡ p') (Hb : q ≡ q') : padd p q ≡ padd p' q' :=
calc
pr1 (padd p q) + pr2 (padd p' q') = pr1 p + pr2 p' + (pr1 q + pr2 q') : by simp
... = pr2 p + pr1 p' + (pr1 q + pr2 q') : {Ha}
... = pr2 p + pr1 p' + (pr2 q + pr1 q') : {Hb}
... = pr2 (padd p q) + pr1 (padd p' q') : by simp
theorem padd_comm (p q : ℕ × ℕ) : padd p q = padd q p :=
calc
padd p q = (pr1 p + pr1 q, pr2 p + pr2 q) : rfl
... = (pr1 q + pr1 p, pr2 p + pr2 q) : add.comm
... = (pr1 q + pr1 p, pr2 q + pr2 p) : add.comm
... = padd q p : rfl
theorem padd_assoc (p q r : ℕ × ℕ) : padd (padd p q) r = padd p (padd q r) :=
calc
padd (padd p q) r = (pr1 p + pr1 q + pr1 r, pr2 p + pr2 q + pr2 r) : rfl
... = (pr1 p + (pr1 q + pr1 r), pr2 p + pr2 q + pr2 r) : add.assoc
... = (pr1 p + (pr1 q + pr1 r), pr2 p + (pr2 q + pr2 r)) : add.assoc
... = padd p (padd q r) : rfl
theorem add.comm (a b : ℤ) : a + b = b + a :=
begin
apply eq_of_repr_equiv_repr,
apply equiv.trans,
apply repr_add,
apply equiv.symm,
apply (eq.subst (padd_comm (repr b) (repr a))),
apply repr_add
end
theorem add.assoc (a b c : ℤ) : a + b + c = a + (b + c) :=
assert H1 : repr (a + b + c) ≡ padd (padd (repr a) (repr b)) (repr c), from
equiv.trans (repr_add (a + b) c) (padd_congr !repr_add !equiv.refl),
assert H2 : repr (a + (b + c)) ≡ padd (repr a) (padd (repr b) (repr c)), from
equiv.trans (repr_add a (b + c)) (padd_congr !equiv.refl !repr_add),
begin
apply eq_of_repr_equiv_repr,
apply equiv.trans,
apply H1,
apply (eq.subst ((padd_assoc _ _ _)⁻¹)),
apply equiv.symm,
apply H2
end
theorem add_zero (a : ℤ) : a + 0 = a := int.cases_on a (take m, rfl) (take m', rfl)
theorem zero_add (a : ℤ) : 0 + a = a := add.comm a 0 ▸ add_zero a
/- negation -/
definition pneg (p : ℕ × ℕ) : ℕ × ℕ := (pr2 p, pr1 p)
-- note: this is =, not just ≡
theorem repr_neg (a : ℤ) : repr (- a) = pneg (repr a) :=
int.cases_on a
(take m,
nat.cases_on m rfl (take m', rfl))
(take m', rfl)
theorem pneg_congr {p p' : ℕ × ℕ} (H : p ≡ p') : pneg p ≡ pneg p' := eq.symm H
theorem pneg_pneg (p : ℕ × ℕ) : pneg (pneg p) = p := !prod.eta
theorem nat_abs_neg (a : ℤ) : nat_abs (-a) = nat_abs a :=
calc
nat_abs (-a) = nat_abs (abstr (repr (-a))) : abstr_repr
... = nat_abs (abstr (pneg (repr a))) : repr_neg
... = dist (pr1 (pneg (repr a))) (pr2 (pneg (repr a))) : nat_abs_abstr
... = dist (pr2 (pneg (repr a))) (pr1 (pneg (repr a))) : dist.comm
... = nat_abs (abstr (repr a)) : nat_abs_abstr
... = nat_abs a : abstr_repr
theorem padd_pneg (p : ℕ × ℕ) : padd p (pneg p) ≡ (0, 0) :=
show pr1 p + pr2 p + 0 = pr2 p + pr1 p + 0, from !nat.add.comm ▸ rfl
theorem padd_padd_pneg (p q : ℕ × ℕ) : padd (padd p q) (pneg q) ≡ p :=
show pr1 p + pr1 q + pr2 q + pr2 p = pr2 p + pr2 q + pr1 q + pr1 p, from by simp
theorem add.left_inv (a : ℤ) : -a + a = 0 :=
have H : repr (-a + a) ≡ repr 0, from
calc
repr (-a + a) ≡ padd (repr (neg a)) (repr a) : repr_add
... = padd (pneg (repr a)) (repr a) : repr_neg
... ≡ repr 0 : padd_pneg,
eq_of_repr_equiv_repr H
/- nat abs -/
definition pabs (p : ℕ × ℕ) : ℕ := dist (pr1 p) (pr2 p)
theorem pabs_congr {p q : ℕ × ℕ} (H : p ≡ q) : pabs p = pabs q :=
calc
pabs p = nat_abs (abstr p) : nat_abs_abstr
... = nat_abs (abstr q) : abstr_eq H
... = pabs q : nat_abs_abstr
theorem nat_abs_eq_pabs_repr (a : ℤ) : nat_abs a = pabs (repr a) :=
calc
nat_abs a = nat_abs (abstr (repr a)) : abstr_repr
... = pabs (repr a) : nat_abs_abstr
theorem nat_abs_add_le (a b : ℤ) : nat_abs (a + b) ≤ nat_abs a + nat_abs b :=
have H : nat_abs (a + b) = pabs (padd (repr a) (repr b)), from
calc
nat_abs (a + b) = pabs (repr (a + b)) : nat_abs_eq_pabs_repr
... = pabs (padd (repr a) (repr b)) : pabs_congr !repr_add,
have H1 : nat_abs a = pabs (repr a), from !nat_abs_eq_pabs_repr,
have H2 : nat_abs b = pabs (repr b), from !nat_abs_eq_pabs_repr,
have H3 : pabs (padd (repr a) (repr b)) ≤ pabs (repr a) + pabs (repr b),
from !dist_add_add_le_add_dist_dist,
H⁻¹ ▸ H1⁻¹ ▸ H2⁻¹ ▸ H3
section
local attribute nat_abs [reducible]
theorem mul_nat_abs (a b : ℤ) : nat_abs (a * b) = #nat (nat_abs a) * (nat_abs b) :=
int.cases_on a
(take m,
int.cases_on b
(take n, rfl)
(take n', !nat_abs_neg ▸ rfl))
(take m',
int.cases_on b
(take n, !nat_abs_neg ▸ rfl)
(take n', rfl))
end
/- multiplication -/
definition pmul (p q : ℕ × ℕ) : ℕ × ℕ :=
(pr1 p * pr1 q + pr2 p * pr2 q, pr1 p * pr2 q + pr2 p * pr1 q)
theorem repr_neg_of_nat (m : ℕ) : repr (neg_of_nat m) = (0, m) :=
nat.cases_on m rfl (take m', rfl)
-- note: we have =, not just ≡
theorem repr_mul (a b : ℤ) : repr (mul a b) = pmul (repr a) (repr b) :=
int.cases_on a
(take m,
int.cases_on b
(take n,
(calc
pmul (repr m) (repr n) = (m * n + 0 * 0, m * 0 + 0 * n) : rfl
... = (m * n + 0 * 0, m * 0 + 0) : zero_mul)⁻¹)
(take n',
(calc
pmul (repr m) (repr (neg_succ_of_nat n')) =
(m * 0 + 0 * succ n', m * succ n' + 0 * 0) : rfl
... = (m * 0 + 0, m * succ n' + 0 * 0) : zero_mul
... = repr (mul m (neg_succ_of_nat n')) : repr_neg_of_nat)⁻¹))
(take m',
int.cases_on b
(take n,
(calc
pmul (repr (neg_succ_of_nat m')) (repr n) =
(0 * n + succ m' * 0, 0 * 0 + succ m' * n) : rfl
... = (0 + succ m' * 0, 0 * 0 + succ m' * n) : zero_mul
... = (0 + succ m' * 0, succ m' * n) : {!nat.zero_add}
... = repr (mul (neg_succ_of_nat m') n) : repr_neg_of_nat)⁻¹)
(take n',
(calc
pmul (repr (neg_succ_of_nat m')) (repr (neg_succ_of_nat n')) =
(0 + succ m' * succ n', 0 * succ n') : rfl
... = (succ m' * succ n', 0 * succ n') : nat.zero_add
... = (succ m' * succ n', 0) : zero_mul
... = repr (mul (neg_succ_of_nat m') (neg_succ_of_nat n')) : rfl)⁻¹))
theorem equiv_mul_prep {xa ya xb yb xn yn xm ym : ℕ}
(H1 : xa + yb = ya + xb) (H2 : xn + ym = yn + xm)
: xa * xn + ya * yn + (xb * ym + yb * xm) = xa * yn + ya * xn + (xb * xm + yb * ym) :=
have H3 : xa * xn + ya * yn + (xb * ym + yb * xm) + (yb * xn + xb * yn + (xb * xn + yb * yn))
= xa * yn + ya * xn + (xb * xm + yb * ym) + (yb * xn + xb * yn + (xb * xn + yb * yn)), from
calc
xa * xn + ya * yn + (xb * ym + yb * xm) + (yb * xn + xb * yn + (xb * xn + yb * yn))
= xa * xn + yb * xn + (ya * yn + xb * yn) + (xb * xn + xb * ym + (yb * yn + yb * xm))
: by simp
... = (xa + yb) * xn + (ya + xb) * yn + (xb * (xn + ym) + yb * (yn + xm)) : by simp
... = (ya + xb) * xn + (xa + yb) * yn + (xb * (yn + xm) + yb * (xn + ym)) : by simp
... = ya * xn + xb * xn + (xa * yn + yb * yn) + (xb * yn + xb * xm + (yb*xn + yb*ym))
: by simp
... = xa * yn + ya * xn + (xb * xm + yb * ym) + (yb * xn + xb * yn + (xb * xn + yb * yn))
: by simp,
nat.add.cancel_right H3
theorem pmul_congr {p p' q q' : ℕ × ℕ} (H1 : p ≡ p') (H2 : q ≡ q') : pmul p q ≡ pmul p' q' :=
equiv_mul_prep H1 H2
theorem pmul_comm (p q : ℕ × ℕ) : pmul p q = pmul q p :=
calc
(pr1 p * pr1 q + pr2 p * pr2 q, pr1 p * pr2 q + pr2 p * pr1 q) =
(pr1 q * pr1 p + pr2 p * pr2 q, pr1 p * pr2 q + pr2 p * pr1 q) : mul.comm
... = (pr1 q * pr1 p + pr2 q * pr2 p, pr1 p * pr2 q + pr2 p * pr1 q) : mul.comm
... = (pr1 q * pr1 p + pr2 q * pr2 p, pr2 q * pr1 p + pr2 p * pr1 q) : mul.comm
... = (pr1 q * pr1 p + pr2 q * pr2 p, pr2 q * pr1 p + pr1 q * pr2 p) : mul.comm
... = (pr1 q * pr1 p + pr2 q * pr2 p, pr1 q * pr2 p + pr2 q * pr1 p) : nat.add.comm
theorem mul.comm (a b : ℤ) : a * b = b * a :=
eq_of_repr_equiv_repr
((calc
repr (a * b) = pmul (repr a) (repr b) : repr_mul
... = pmul (repr b) (repr a) : pmul_comm
... = repr (b * a) : repr_mul) ▸ !equiv.refl)
theorem pmul_assoc (p q r: ℕ × ℕ) : pmul (pmul p q) r = pmul p (pmul q r) :=
by simp
theorem mul.assoc (a b c : ℤ) : (a * b) * c = a * (b * c) :=
eq_of_repr_equiv_repr
((calc
repr (a * b * c) = pmul (repr (a * b)) (repr c) : repr_mul
... = pmul (pmul (repr a) (repr b)) (repr c) : repr_mul
... = pmul (repr a) (pmul (repr b) (repr c)) : pmul_assoc
... = pmul (repr a) (repr (b * c)) : repr_mul
... = repr (a * (b * c)) : repr_mul) ▸ !equiv.refl)
theorem mul_one (a : ℤ) : a * 1 = a :=
eq_of_repr_equiv_repr (equiv_of_eq
((calc
repr (a * 1) = pmul (repr a) (repr 1) : repr_mul
... = (pr1 (repr a), pr2 (repr a)) : by simp
... = repr a : prod.eta)))
theorem one_mul (a : ℤ) : 1 * a = a :=
mul.comm a 1 ▸ mul_one a
theorem mul.right_distrib (a b c : ℤ) : (a + b) * c = a * c + b * c :=
eq_of_repr_equiv_repr
(calc
repr ((a + b) * c) = pmul (repr (a + b)) (repr c) : repr_mul
... ≡ pmul (padd (repr a) (repr b)) (repr c) : pmul_congr !repr_add equiv.refl
... = padd (pmul (repr a) (repr c)) (pmul (repr b) (repr c)) : by simp
... = padd (repr (a * c)) (pmul (repr b) (repr c)) : {(repr_mul a c)⁻¹}
... = padd (repr (a * c)) (repr (b * c)) : repr_mul
... ≡ repr (a * c + b * c) : equiv.symm !repr_add)
theorem mul.left_distrib (a b c : ℤ) : a * (b + c) = a * b + a * c :=
calc
a * (b + c) = (b + c) * a : mul.comm a (b + c)
... = b * a + c * a : mul.right_distrib b c a
... = a * b + c * a : {mul.comm b a}
... = a * b + a * c : {mul.comm c a}
theorem zero_ne_one : (typeof 0 : int) ≠ 1 :=
assume H : 0 = 1,
show false, from succ_ne_zero 0 ((of_nat.inj H)⁻¹)
theorem eq_zero_or_eq_zero_of_mul_eq_zero {a b : ℤ} (H : a * b = 0) : a = 0 ∨ b = 0 :=
have H2 : (nat_abs a) * (nat_abs b) = nat.zero, from
calc
(nat_abs a) * (nat_abs b) = (nat_abs (a * b)) : (mul_nat_abs a b)⁻¹
... = (nat_abs 0) : {H}
... = nat.zero : nat_abs_of_nat nat.zero,
have H3 : (nat_abs a) = nat.zero ∨ (nat_abs b) = nat.zero,
from eq_zero_or_eq_zero_of_mul_eq_zero H2,
or_of_or_of_imp_of_imp H3
(assume H : (nat_abs a) = nat.zero, nat_abs_eq_zero H)
(assume H : (nat_abs b) = nat.zero, nat_abs_eq_zero H)
section
open [classes] algebra
protected definition integral_domain [instance] [reducible] : algebra.integral_domain int :=
⦃algebra.integral_domain,
add := add,
add_assoc := add.assoc,
zero := zero,
zero_add := zero_add,
add_zero := add_zero,
neg := neg,
add_left_inv := add.left_inv,
add_comm := add.comm,
mul := mul,
mul_assoc := mul.assoc,
one := (of_num 1),
one_mul := one_mul,
mul_one := mul_one,
left_distrib := mul.left_distrib,
right_distrib := mul.right_distrib,
mul_comm := mul.comm,
eq_zero_or_eq_zero_of_mul_eq_zero := @eq_zero_or_eq_zero_of_mul_eq_zero⦄
end
/- instantiate ring theorems to int -/
section port_algebra
open [classes] algebra
theorem mul.left_comm : ∀a b c : ℤ, a * (b * c) = b * (a * c) := algebra.mul.left_comm
theorem mul.right_comm : ∀a b c : ℤ, (a * b) * c = (a * c) * b := algebra.mul.right_comm
theorem add.left_comm : ∀a b c : ℤ, a + (b + c) = b + (a + c) := algebra.add.left_comm
theorem add.right_comm : ∀a b c : ℤ, (a + b) + c = (a + c) + b := algebra.add.right_comm
theorem add.left_cancel : ∀{a b c : ℤ}, a + b = a + c → b = c := @algebra.add.left_cancel _ _
theorem add.right_cancel : ∀{a b c : ℤ}, a + b = c + b → a = c := @algebra.add.right_cancel _ _
theorem neg_add_cancel_left : ∀a b : ℤ, -a + (a + b) = b := algebra.neg_add_cancel_left
theorem neg_add_cancel_right : ∀a b : ℤ, a + -b + b = a := algebra.neg_add_cancel_right
theorem neg_eq_of_add_eq_zero : ∀{a b : ℤ}, a + b = 0 → -a = b :=
@algebra.neg_eq_of_add_eq_zero _ _
theorem neg_zero : -0 = 0 := algebra.neg_zero
theorem neg_neg : ∀a : ℤ, -(-a) = a := algebra.neg_neg
theorem neg.inj : ∀{a b : ℤ}, -a = -b → a = b := @algebra.neg.inj _ _
theorem neg_eq_neg_iff_eq : ∀a b : ℤ, -a = -b ↔ a = b := algebra.neg_eq_neg_iff_eq
theorem neg_eq_zero_iff_eq_zero : ∀a : ℤ, -a = 0 ↔ a = 0 := algebra.neg_eq_zero_iff_eq_zero
theorem eq_neg_of_eq_neg : ∀{a b : ℤ}, a = -b → b = -a := @algebra.eq_neg_of_eq_neg _ _
theorem eq_neg_iff_eq_neg : ∀{a b : ℤ}, a = -b ↔ b = -a := @algebra.eq_neg_iff_eq_neg _ _
theorem add.right_inv : ∀a : ℤ, a + -a = 0 := algebra.add.right_inv
theorem add_neg_cancel_left : ∀a b : ℤ, a + (-a + b) = b := algebra.add_neg_cancel_left
theorem add_neg_cancel_right : ∀a b : ℤ, a + b + -b = a := algebra.add_neg_cancel_right
theorem neg_add_rev : ∀a b : ℤ, -(a + b) = -b + -a := algebra.neg_add_rev
theorem eq_add_neg_of_add_eq : ∀{a b c : ℤ}, a + c = b → a = b + -c :=
@algebra.eq_add_neg_of_add_eq _ _
theorem eq_neg_add_of_add_eq : ∀{a b c : ℤ}, b + a = c → a = -b + c :=
@algebra.eq_neg_add_of_add_eq _ _
theorem neg_add_eq_of_eq_add : ∀{a b c : ℤ}, b = a + c → -a + b = c :=
@algebra.neg_add_eq_of_eq_add _ _
theorem add_neg_eq_of_eq_add : ∀{a b c : ℤ}, a = c + b → a + -b = c :=
@algebra.add_neg_eq_of_eq_add _ _
theorem eq_add_of_add_neg_eq : ∀{a b c : ℤ}, a + -c = b → a = b + c :=
@algebra.eq_add_of_add_neg_eq _ _
theorem eq_add_of_neg_add_eq : ∀{a b c : ℤ}, -b + a = c → a = b + c :=
@algebra.eq_add_of_neg_add_eq _ _
theorem add_eq_of_eq_neg_add : ∀{a b c : ℤ}, b = -a + c → a + b = c :=
@algebra.add_eq_of_eq_neg_add _ _
theorem add_eq_of_eq_add_neg : ∀{a b c : ℤ}, a = c + -b → a + b = c :=
@algebra.add_eq_of_eq_add_neg _ _
theorem add_eq_iff_eq_neg_add : ∀a b c : ℤ, a + b = c ↔ b = -a + c :=
@algebra.add_eq_iff_eq_neg_add _ _
theorem add_eq_iff_eq_add_neg : ∀a b c : ℤ, a + b = c ↔ a = c + -b :=
@algebra.add_eq_iff_eq_add_neg _ _
definition sub (a b : ℤ) : ℤ := algebra.sub a b
infix - := int.sub
theorem sub_eq_add_neg : ∀a b : ℤ, a - b = a + -b := algebra.sub_eq_add_neg
theorem sub_self : ∀a : ℤ, a - a = 0 := algebra.sub_self
theorem sub_add_cancel : ∀a b : ℤ, a - b + b = a := algebra.sub_add_cancel
theorem add_sub_cancel : ∀a b : ℤ, a + b - b = a := algebra.add_sub_cancel
theorem eq_of_sub_eq_zero : ∀{a b : ℤ}, a - b = 0 → a = b := @algebra.eq_of_sub_eq_zero _ _
theorem eq_iff_sub_eq_zero : ∀a b : ℤ, a = b ↔ a - b = 0 := algebra.eq_iff_sub_eq_zero
theorem zero_sub : ∀a : ℤ, 0 - a = -a := algebra.zero_sub
theorem sub_zero : ∀a : ℤ, a - 0 = a := algebra.sub_zero
theorem sub_neg_eq_add : ∀a b : ℤ, a - (-b) = a + b := algebra.sub_neg_eq_add
theorem neg_sub : ∀a b : ℤ, -(a - b) = b - a := algebra.neg_sub
theorem add_sub : ∀a b c : ℤ, a + (b - c) = a + b - c := algebra.add_sub
theorem sub_add_eq_sub_sub_swap : ∀a b c : ℤ, a - (b + c) = a - c - b :=
algebra.sub_add_eq_sub_sub_swap
theorem sub_eq_iff_eq_add : ∀a b c : ℤ, a - b = c ↔ a = c + b := algebra.sub_eq_iff_eq_add
theorem eq_sub_iff_add_eq : ∀a b c : ℤ, a = b - c ↔ a + c = b := algebra.eq_sub_iff_add_eq
theorem eq_iff_eq_of_sub_eq_sub : ∀{a b c d : ℤ}, a - b = c - d → (a = b ↔ c = d) :=
@algebra.eq_iff_eq_of_sub_eq_sub _ _
theorem eq_sub_of_add_eq : ∀{a b c : ℤ}, a + c = b → a = b - c := @algebra.eq_sub_of_add_eq _ _
theorem sub_eq_of_eq_add : ∀{a b c : ℤ}, a = c + b → a - b = c := @algebra.sub_eq_of_eq_add _ _
theorem eq_add_of_sub_eq : ∀{a b c : ℤ}, a - c = b → a = b + c := @algebra.eq_add_of_sub_eq _ _
theorem add_eq_of_eq_sub : ∀{a b c : ℤ}, a = c - b → a + b = c := @algebra.add_eq_of_eq_sub _ _
theorem sub_add_eq_sub_sub : ∀a b c : ℤ, a - (b + c) = a - b - c := algebra.sub_add_eq_sub_sub
theorem neg_add_eq_sub : ∀a b : ℤ, -a + b = b - a := algebra.neg_add_eq_sub
theorem neg_add : ∀a b : ℤ, -(a + b) = -a + -b := algebra.neg_add
theorem sub_add_eq_add_sub : ∀a b c : ℤ, a - b + c = a + c - b := algebra.sub_add_eq_add_sub
theorem sub_sub_ : ∀a b c : ℤ, a - b - c = a - (b + c) := algebra.sub_sub
theorem add_sub_add_left_eq_sub : ∀a b c : ℤ, (c + a) - (c + b) = a - b :=
algebra.add_sub_add_left_eq_sub
theorem eq_sub_of_add_eq' : ∀{a b c : ℤ}, c + a = b → a = b - c := @algebra.eq_sub_of_add_eq' _ _
theorem sub_eq_of_eq_add' : ∀{a b c : ℤ}, a = b + c → a - b = c := @algebra.sub_eq_of_eq_add' _ _
theorem eq_add_of_sub_eq' : ∀{a b c : ℤ}, a - b = c → a = b + c := @algebra.eq_add_of_sub_eq' _ _
theorem add_eq_of_eq_sub' : ∀{a b c : ℤ}, b = c - a → a + b = c := @algebra.add_eq_of_eq_sub' _ _
theorem ne_zero_of_mul_ne_zero_right : ∀{a b : ℤ}, a * b ≠ 0 → a ≠ 0 :=
@algebra.ne_zero_of_mul_ne_zero_right _ _
theorem ne_zero_of_mul_ne_zero_left : ∀{a b : ℤ}, a * b ≠ 0 → b ≠ 0 :=
@algebra.ne_zero_of_mul_ne_zero_left _ _
definition dvd (a b : ℤ) : Prop := algebra.dvd a b
notation a ∣ b := dvd a b
theorem dvd.intro : ∀{a b c : ℤ} (H : a * c = b), a ∣ b := @algebra.dvd.intro _ _
theorem dvd.intro_left : ∀{a b c : ℤ} (H : c * a = b), a ∣ b := @algebra.dvd.intro_left _ _
theorem exists_eq_mul_right_of_dvd : ∀{a b : ℤ} (H : a ∣ b), ∃c, b = a * c :=
@algebra.exists_eq_mul_right_of_dvd _ _
theorem dvd.elim : ∀{P : Prop} {a b : ℤ} (H₁ : a ∣ b) (H₂ : ∀c, b = a * c → P), P :=
@algebra.dvd.elim _ _
theorem exists_eq_mul_left_of_dvd : ∀{a b : ℤ} (H : a ∣ b), ∃c, b = c * a :=
@algebra.exists_eq_mul_left_of_dvd _ _
theorem dvd.elim_left : ∀{P : Prop} {a b : ℤ} (H₁ : a ∣ b) (H₂ : ∀c, b = c * a → P), P :=
@algebra.dvd.elim_left _ _
theorem dvd.refl : ∀a : ℤ, (a ∣ a) := algebra.dvd.refl
theorem dvd.trans : ∀{a b c : ℤ} (H₁ : a ∣ b) (H₂ : b ∣ c), a ∣ c := @algebra.dvd.trans _ _
theorem eq_zero_of_zero_dvd : ∀{a : ℤ} (H : 0 ∣ a), a = 0 := @algebra.eq_zero_of_zero_dvd _ _
theorem dvd_zero : ∀a : ℤ, a ∣ 0 := algebra.dvd_zero
theorem one_dvd : ∀a : ℤ, 1 ∣ a := algebra.one_dvd
theorem dvd_mul_right : ∀a b : ℤ, a ∣ a * b := algebra.dvd_mul_right
theorem dvd_mul_left : ∀a b : ℤ, a ∣ b * a := algebra.dvd_mul_left
theorem dvd_mul_of_dvd_left : ∀{a b : ℤ} (H : a ∣ b) (c : ℤ), a ∣ b * c :=
@algebra.dvd_mul_of_dvd_left _ _
theorem dvd_mul_of_dvd_right : ∀{a b : ℤ} (H : a ∣ b) (c : ℤ), a ∣ c * b :=
@algebra.dvd_mul_of_dvd_right _ _
theorem mul_dvd_mul : ∀{a b c d : ℤ}, a ∣ b → c ∣ d → a * c ∣ b * d :=
@algebra.mul_dvd_mul _ _
theorem dvd_of_mul_right_dvd : ∀{a b c : ℤ}, a * b ∣ c → a ∣ c :=
@algebra.dvd_of_mul_right_dvd _ _
theorem dvd_of_mul_left_dvd : ∀{a b c : ℤ}, a * b ∣ c → b ∣ c :=
@algebra.dvd_of_mul_left_dvd _ _
theorem dvd_add : ∀{a b c : ℤ}, a ∣ b → a ∣ c → a ∣ b + c := @algebra.dvd_add _ _
theorem zero_mul : ∀a : ℤ, 0 * a = 0 := algebra.zero_mul
theorem mul_zero : ∀a : ℤ, a * 0 = 0 := algebra.mul_zero
theorem neg_mul_eq_neg_mul : ∀a b : ℤ, -(a * b) = -a * b := algebra.neg_mul_eq_neg_mul
theorem neg_mul_eq_mul_neg : ∀a b : ℤ, -(a * b) = a * -b := algebra.neg_mul_eq_mul_neg
theorem neg_mul_neg : ∀a b : ℤ, -a * -b = a * b := algebra.neg_mul_neg
theorem neg_mul_comm : ∀a b : ℤ, -a * b = a * -b := algebra.neg_mul_comm
theorem neg_eq_neg_one_mul : ∀a : ℤ, -a = -1 * a := algebra.neg_eq_neg_one_mul
theorem mul_sub_left_distrib : ∀a b c : ℤ, a * (b - c) = a * b - a * c :=
algebra.mul_sub_left_distrib
theorem mul_sub_right_distrib : ∀a b c : ℤ, (a - b) * c = a * c - b * c :=
algebra.mul_sub_right_distrib
theorem mul_add_eq_mul_add_iff_sub_mul_add_eq :
∀a b c d e : ℤ, a * e + c = b * e + d ↔ (a - b) * e + c = d :=
algebra.mul_add_eq_mul_add_iff_sub_mul_add_eq
theorem mul_self_sub_mul_self_eq : ∀a b : ℤ, a * a - b * b = (a + b) * (a - b) :=
algebra.mul_self_sub_mul_self_eq
theorem mul_self_sub_one_eq : ∀a : ℤ, a * a - 1 = (a + 1) * (a - 1) :=
algebra.mul_self_sub_one_eq
theorem dvd_neg_iff_dvd : ∀a b : ℤ, a ∣ -b ↔ a ∣ b := algebra.dvd_neg_iff_dvd
theorem neg_dvd_iff_dvd : ∀a b : ℤ, -a ∣ b ↔ a ∣ b := algebra.neg_dvd_iff_dvd
theorem dvd_sub : ∀a b c : ℤ, a ∣ b → a ∣ c → a ∣ b - c := algebra.dvd_sub
theorem mul_ne_zero : ∀{a b : ℤ}, a ≠ 0 → b ≠ 0 → a * b ≠ 0 := @algebra.mul_ne_zero _ _
theorem mul.cancel_right : ∀{a b c : ℤ}, a ≠ 0 → b * a = c * a → b = c :=
@algebra.mul.cancel_right _ _
theorem mul.cancel_left : ∀{a b c : ℤ}, a ≠ 0 → a * b = a * c → b = c :=
@algebra.mul.cancel_left _ _
theorem mul_self_eq_mul_self_iff : ∀a b : ℤ, a * a = b * b ↔ a = b ∨ a = -b :=
algebra.mul_self_eq_mul_self_iff
theorem mul_self_eq_one_iff : ∀a : ℤ, a * a = 1 ↔ a = 1 ∨ a = -1 :=
algebra.mul_self_eq_one_iff
theorem dvd_of_mul_dvd_mul_left : ∀{a b c : ℤ}, a ≠ 0 → a*b ∣ a*c → b ∣ c :=
@algebra.dvd_of_mul_dvd_mul_left _ _
theorem dvd_of_mul_dvd_mul_right : ∀{a b c : ℤ}, a ≠ 0 → b*a ∣ c*a → b ∣ c :=
@algebra.dvd_of_mul_dvd_mul_right _ _
end port_algebra
/- additional properties -/
theorem of_nat_sub_of_nat {m n : ℕ} (H : #nat m ≥ n) : of_nat m - of_nat n = of_nat (#nat m - n) :=
have H1 : m = (#nat m - n + n), from (nat.sub_add_cancel H)⁻¹,
have H2 : m = (#nat m - n) + n, from congr_arg of_nat H1,
sub_eq_of_eq_add H2
theorem neg_succ_of_nat_eq' (m : ℕ) : -[m +1] = -m - 1 :=
by rewrite [neg_succ_of_nat_eq, -of_nat_add_of_nat, neg_add]
end int
|
f461910b45d7d0cd22e86874d621ba384a102a16 | 968e2f50b755d3048175f176376eff7139e9df70 | /examples/prop_logic_lean_summary/unnamed_110.lean | 05048a67fe073e7c73143278d6e901202857b4ba | [] | no_license | gihanmarasingha/mth1001_sphinx | 190a003269ba5e54717b448302a27ca26e31d491 | 05126586cbf5786e521be1ea2ef5b4ba3c44e74a | refs/heads/master | 1,672,913,933,677 | 1,604,516,583,000 | 1,604,516,583,000 | 309,245,750 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 225 | lean | variables p q : Prop
-- BEGIN
example (h : p ∧ q) : q :=
begin
cases h with hp hq, -- Equivalent to both left and right and elimination.
exact hq, -- Closes the goal via reiteration using the proof term `hq`
end
-- END |
481964d7513a4a4509263e67cce650f5147dbc27 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/int/interval.lean | a85e73fcf9c526589fd1c3f4bd7a575b7c36616c | [
"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 | 7,023 | lean | /-
Copyright (c) 2021 Yaël Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies
-/
import data.int.basic
import algebra.char_zero
import order.locally_finite
import data.finset.locally_finite
/-!
# Finite intervals of integers
This file proves that `ℤ` is a `locally_finite_order` and calculates the cardinality of its
intervals as finsets and fintypes.
-/
open finset int
instance : locally_finite_order ℤ :=
{ finset_Icc := λ a b, (finset.range (b + 1 - a).to_nat).map $
nat.cast_embedding.trans $ add_left_embedding a,
finset_Ico := λ a b, (finset.range (b - a).to_nat).map $
nat.cast_embedding.trans $ add_left_embedding a,
finset_Ioc := λ a b, (finset.range (b - a).to_nat).map $
nat.cast_embedding.trans $ add_left_embedding (a + 1),
finset_Ioo := λ a b, (finset.range (b - a - 1).to_nat).map $
nat.cast_embedding.trans $ add_left_embedding (a + 1),
finset_mem_Icc := λ a b x, begin
simp_rw [mem_map, exists_prop, mem_range, int.lt_to_nat, function.embedding.trans_apply,
nat.cast_embedding_apply, add_left_embedding_apply, nat_cast_eq_coe_nat],
split,
{ rintro ⟨a, h, rfl⟩,
rw [lt_sub_iff_add_lt, int.lt_add_one_iff, add_comm] at h,
exact ⟨int.le.intro rfl, h⟩ },
{ rintro ⟨ha, hb⟩,
use (x - a).to_nat,
rw ←lt_add_one_iff at hb,
rw to_nat_sub_of_le ha,
exact ⟨sub_lt_sub_right hb _, add_sub_cancel'_right _ _⟩ }
end,
finset_mem_Ico := λ a b x, begin
simp_rw [mem_map, exists_prop, mem_range, int.lt_to_nat, function.embedding.trans_apply,
nat.cast_embedding_apply, add_left_embedding_apply, nat_cast_eq_coe_nat],
split,
{ rintro ⟨a, h, rfl⟩,
exact ⟨int.le.intro rfl, lt_sub_iff_add_lt'.mp h⟩ },
{ rintro ⟨ha, hb⟩,
use (x - a).to_nat,
rw to_nat_sub_of_le ha,
exact ⟨sub_lt_sub_right hb _, add_sub_cancel'_right _ _⟩ }
end,
finset_mem_Ioc := λ a b x, begin
simp_rw [mem_map, exists_prop, mem_range, int.lt_to_nat, function.embedding.trans_apply,
nat.cast_embedding_apply, add_left_embedding_apply, nat_cast_eq_coe_nat],
split,
{ rintro ⟨a, h, rfl⟩,
rw [←add_one_le_iff, le_sub_iff_add_le', add_comm _ (1 : ℤ), ←add_assoc] at h,
exact ⟨int.le.intro rfl, h⟩ },
{ rintro ⟨ha, hb⟩,
use (x - (a + 1)).to_nat,
rw [to_nat_sub_of_le ha, ←add_one_le_iff, sub_add, add_sub_cancel],
exact ⟨sub_le_sub_right hb _, add_sub_cancel'_right _ _⟩ }
end,
finset_mem_Ioo := λ a b x, begin
simp_rw [mem_map, exists_prop, mem_range, int.lt_to_nat, function.embedding.trans_apply,
nat.cast_embedding_apply, add_left_embedding_apply, nat_cast_eq_coe_nat],
split,
{ rintro ⟨a, h, rfl⟩,
rw [sub_sub, lt_sub_iff_add_lt'] at h,
exact ⟨int.le.intro rfl, h⟩ },
{ rintro ⟨ha, hb⟩,
use (x - (a + 1)).to_nat,
rw [to_nat_sub_of_le ha, sub_sub],
exact ⟨sub_lt_sub_right hb _, add_sub_cancel'_right _ _⟩ }
end }
namespace int
variables (a b : ℤ)
lemma Icc_eq_finset_map :
Icc a b = (finset.range (b + 1 - a).to_nat).map
(nat.cast_embedding.trans $ add_left_embedding a) := rfl
lemma Ico_eq_finset_map :
Ico a b = (finset.range (b - a).to_nat).map
(nat.cast_embedding.trans $ add_left_embedding a) := rfl
lemma Ioc_eq_finset_map :
Ioc a b = (finset.range (b - a).to_nat).map
(nat.cast_embedding.trans $ add_left_embedding (a + 1)) := rfl
lemma Ioo_eq_finset_map :
Ioo a b = (finset.range (b - a - 1).to_nat).map
(nat.cast_embedding.trans $ add_left_embedding (a + 1)) := rfl
@[simp] lemma card_Icc : (Icc a b).card = (b + 1 - a).to_nat :=
by { change (finset.map _ _).card = _, rw [finset.card_map, finset.card_range] }
@[simp] lemma card_Ico : (Ico a b).card = (b - a).to_nat :=
by { change (finset.map _ _).card = _, rw [finset.card_map, finset.card_range] }
@[simp] lemma card_Ioc : (Ioc a b).card = (b - a).to_nat :=
by { change (finset.map _ _).card = _, rw [finset.card_map, finset.card_range] }
@[simp] lemma card_Ioo : (Ioo a b).card = (b - a - 1).to_nat :=
by { change (finset.map _ _).card = _, rw [finset.card_map, finset.card_range] }
lemma card_Icc_of_le (h : a ≤ b + 1) : ((Icc a b).card : ℤ) = b + 1 - a :=
by rw [card_Icc, to_nat_sub_of_le h]
lemma card_Ico_of_le (h : a ≤ b) : ((Ico a b).card : ℤ) = b - a :=
by rw [card_Ico, to_nat_sub_of_le h]
lemma card_Ioc_of_le (h : a ≤ b) : ((Ioc a b).card : ℤ) = b - a :=
by rw [card_Ioc, to_nat_sub_of_le h]
lemma card_Ioo_of_lt (h : a < b) : ((Ioo a b).card : ℤ) = b - a - 1 :=
by rw [card_Ioo, sub_sub, to_nat_sub_of_le h]
@[simp] lemma card_fintype_Icc : fintype.card (set.Icc a b) = (b + 1 - a).to_nat :=
by rw [←card_Icc, fintype.card_of_finset]
@[simp] lemma card_fintype_Ico : fintype.card (set.Ico a b) = (b - a).to_nat :=
by rw [←card_Ico, fintype.card_of_finset]
@[simp] lemma card_fintype_Ioc : fintype.card (set.Ioc a b) = (b - a).to_nat :=
by rw [←card_Ioc, fintype.card_of_finset]
@[simp] lemma card_fintype_Ioo : fintype.card (set.Ioo a b) = (b - a - 1).to_nat :=
by rw [←card_Ioo, fintype.card_of_finset]
lemma card_fintype_Icc_of_le (h : a ≤ b + 1) : (fintype.card (set.Icc a b) : ℤ) = b + 1 - a :=
by rw [card_fintype_Icc, to_nat_sub_of_le h]
lemma card_fintype_Ico_of_le (h : a ≤ b) : (fintype.card (set.Ico a b) : ℤ) = b - a :=
by rw [card_fintype_Ico, to_nat_sub_of_le h]
lemma card_fintype_Ioc_of_le (h : a ≤ b) : (fintype.card (set.Ioc a b) : ℤ) = b - a :=
by rw [card_fintype_Ioc, to_nat_sub_of_le h]
lemma card_fintype_Ioo_of_lt (h : a < b) : (fintype.card (set.Ioo a b) : ℤ) = b - a - 1 :=
by rw [card_fintype_Ioo, sub_sub, to_nat_sub_of_le h]
lemma image_Ico_mod (n a : ℤ) (h : 0 ≤ a) :
(Ico n (n+a)).image (% a) = Ico 0 a :=
begin
obtain rfl | ha := eq_or_lt_of_le h,
{ simp, },
ext i,
simp only [mem_image, exists_prop, mem_range, mem_Ico],
split,
{ rintro ⟨i, h, rfl⟩, exact ⟨mod_nonneg i (ne_of_gt ha), mod_lt_of_pos i ha⟩, },
intro hia,
have hn := int.mod_add_div n a,
obtain hi | hi := lt_or_le i (n % a),
{ refine ⟨i + a * (n/a + 1), ⟨_, _⟩, _⟩,
{ rw [add_comm (n/a), mul_add, mul_one, ← add_assoc],
refine hn.symm.le.trans (add_le_add_right _ _),
simpa only [zero_add] using add_le_add (hia.left) (int.mod_lt_of_pos n ha).le, },
{ refine lt_of_lt_of_le (add_lt_add_right hi (a * (n/a + 1))) _,
rw [mul_add, mul_one, ← add_assoc, hn], },
{ rw [int.add_mul_mod_self_left, int.mod_eq_of_lt hia.left hia.right], } },
{ refine ⟨i + a * (n/a), ⟨_, _⟩, _⟩,
{ exact hn.symm.le.trans (add_le_add_right hi _), },
{ rw [add_comm n a],
refine add_lt_add_of_lt_of_le hia.right (le_trans _ hn.le),
simp only [zero_le, le_add_iff_nonneg_left],
exact int.mod_nonneg n (ne_of_gt ha), },
{ rw [int.add_mul_mod_self_left, int.mod_eq_of_lt hia.left hia.right], } },
end
end int
|
ad233ebc6aef6e432f1b67815acdc43dc1386544 | 07c6143268cfb72beccd1cc35735d424ebcb187b | /src/category_theory/limits/connected.lean | d3338756f26c83c22a474b575ea946ab1c55f676 | [
"Apache-2.0"
] | permissive | khoek/mathlib | bc49a842910af13a3c372748310e86467d1dc766 | aa55f8b50354b3e11ba64792dcb06cccb2d8ee28 | refs/heads/master | 1,588,232,063,837 | 1,587,304,803,000 | 1,587,304,803,000 | 176,688,517 | 0 | 0 | Apache-2.0 | 1,553,070,585,000 | 1,553,070,585,000 | null | UTF-8 | Lean | false | false | 3,832 | 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 category_theory.limits.shapes.pullbacks
import category_theory.limits.shapes.binary_products
import category_theory.limits.shapes.equalizers
import category_theory.limits.preserves
import category_theory.connected
/-!
# Connected limits
A connected limit is a limit whose shape is a connected category.
We give examples of connected categories, and prove that the functor given
by `(X × -)` preserves any connected limit. That is, any limit of shape `J`
where `J` is a connected category is preserved by the functor `(X × -)`.
-/
universes v₁ v₂ u₁ u₂
open category_theory category_theory.category category_theory.limits
namespace category_theory
section examples
instance cospan_inhabited : inhabited walking_cospan := ⟨walking_cospan.one⟩
instance cospan_connected : connected (walking_cospan) :=
begin
apply connected.of_induct,
introv _ t,
cases j,
{ rwa t walking_cospan.hom.inl },
{ rwa t walking_cospan.hom.inr },
{ assumption }
end
instance span_inhabited : inhabited walking_span := ⟨walking_span.zero⟩
instance span_connected : connected (walking_span) :=
begin
apply connected.of_induct,
introv _ t,
cases j,
{ assumption },
{ rwa ← t walking_span.hom.fst },
{ rwa ← t walking_span.hom.snd },
end
instance parallel_pair_inhabited : inhabited walking_parallel_pair := ⟨walking_parallel_pair.one⟩
instance parallel_pair_connected : connected (walking_parallel_pair) :=
begin
apply connected.of_induct,
introv _ t,
cases j,
{ rwa t walking_parallel_pair_hom.left },
{ assumption }
end
end examples
local attribute [tidy] tactic.case_bash
variables {C : Type u₂} [𝒞 : category.{v₂} C]
include 𝒞
variables [has_binary_products.{v₂} C]
variables {J : Type v₂} [small_category J]
namespace prod_preserves_connected_limits
/-- (Impl). The obvious natural transformation from (X × K -) to K. -/
@[simps]
def γ₂ {K : J ⥤ C} (X : C) : K ⋙ prod_functor.obj X ⟶ K :=
{ app := λ Y, limits.prod.snd }
/-- (Impl). The obvious natural transformation from (X × K -) to X -/
@[simps]
def γ₁ {K : J ⥤ C} (X : C) : K ⋙ prod_functor.obj X ⟶ (functor.const J).obj X :=
{ app := λ Y, limits.prod.fst }
/-- (Impl). Given a cone for (X × K -), produce a cone for K using the natural transformation `γ₂` -/
@[simps]
def forget_cone {X : C} {K : J ⥤ C} (s : cone (K ⋙ prod_functor.obj X)) : cone K :=
{ X := s.X,
π := s.π ≫ γ₂ X }
end prod_preserves_connected_limits
open prod_preserves_connected_limits
/--
The functor `(X × -)` preserves any connected limit.
Note that this functor does not preserve the two most obvious disconnected limits - that is,
`(X × -)` does not preserve products or terminal object, eg `(X ⨯ A) ⨯ (X ⨯ B)` is not isomorphic to
`X ⨯ (A ⨯ B)` and `X ⨯ 1` is not isomorphic to `1`.
-/
def prod_preserves_connected_limits [connected J] (X : C) :
preserves_limits_of_shape J (prod_functor.obj X) :=
{ preserves_limit := λ K,
{ preserves := λ c l,
{ lift := λ s, prod.lift (s.π.app (default _) ≫ limits.prod.fst) (l.lift (forget_cone s)),
fac' := λ s j,
begin
apply prod.hom_ext,
{ erw [assoc, limit.map_π, comp_id, limit.lift_π],
exact (nat_trans_from_connected (s.π ≫ γ₁ X) j).symm },
{ simp [← l.fac (forget_cone s) j] }
end,
uniq' := λ s m L,
begin
apply prod.hom_ext,
{ erw [limit.lift_π, ← L (default J), assoc, limit.map_π, comp_id],
refl },
{ rw limit.lift_π,
apply l.uniq (forget_cone s),
intro j,
simp [← L j] }
end } } }
end category_theory
|
f9456b5ae6f462e07507ff709798e6f91669b16d | 86f6f4f8d827a196a32bfc646234b73328aeb306 | /examples/logic/unnamed_1260.lean | b85ff4222e248e372f63738b41d5e0e354572ef2 | [] | no_license | jamescheuk91/mathematics_in_lean | 09f1f87d2b0dce53464ff0cbe592c568ff59cf5e | 4452499264e2975bca2f42565c0925506ba5dda3 | refs/heads/master | 1,679,716,410,967 | 1,613,957,947,000 | 1,613,957,947,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 241 | lean | import tactic
variables {α : Type*} (P : α → Prop)
open_locale classical
example (h : ¬ ∀ x, P x) : ∃ x, ¬ P x :=
begin
by_contradiction h',
apply h,
intro x,
show P x,
by_contradiction h'',
exact h' ⟨x, h''⟩
end |
51243a5617015f4d59403e5c25f377aee6fc192e | 9c1ad797ec8a5eddb37d34806c543602d9a6bf70 | /monoidal_categories/internal_objects/semigroup_modules.lean | b379e05c3d43ae15fcc98446c53889390070dece | [] | no_license | timjb/lean-category-theory | 816eefc3a0582c22c05f4ee1c57ed04e57c0982f | 12916cce261d08bb8740bc85e0175b75fb2a60f4 | refs/heads/master | 1,611,078,926,765 | 1,492,080,000,000 | 1,492,080,000,000 | 88,348,246 | 0 | 0 | null | 1,492,262,499,000 | 1,492,262,498,000 | null | UTF-8 | Lean | false | false | 2,301 | 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 .semigroups
open tqft.categories
open tqft.categories.monoidal_category
namespace tqft.categories.internal_objects
structure SemigroupModuleObject { C : Category } { m : MonoidalStructure C } ( A : SemigroupObject m ) :=
( module : C.Obj )
( action : C.Hom (m.tensorObjects A module) module )
( associativity : C.compose (m.tensorMorphisms A.multiplication (C.identity module)) action = C.compose (m.associator A A module) (C.compose (m.tensorMorphisms (C.identity A) action) action) )
attribute [ematch] SemigroupModuleObject.associativity
instance SemigroupModuleObject_coercion_to_module { C : Category } { m : MonoidalStructure C } ( A : SemigroupObject m ) : has_coe (SemigroupModuleObject A) (C.Obj) :=
{ coe := SemigroupModuleObject.module }
structure SemigroupModuleMorphism { C : Category } { m : MonoidalStructure C } { A : SemigroupObject m } ( X Y : SemigroupModuleObject A ) :=
( map : C.Hom X Y )
( compatibility : C.compose (m.tensorMorphisms (C.identity A) map) Y.action = C.compose X.action map )
attribute [simp,ematch] SemigroupModuleMorphism.compatibility
@[pointwise] lemma SemigroupModuleMorphism_pointwisewise_equal
{ C : Category }
{ m : MonoidalStructure C }
{ A : SemigroupObject m }
{ X Y : SemigroupModuleObject A }
( f g : SemigroupModuleMorphism X Y )
( w : f.map = g.map ) : f = g :=
begin
induction f,
induction g,
blast
end
instance SemigroupModuleMorphism_coercion_to_map { C : Category } { m : MonoidalStructure C } { A : SemigroupObject m } ( X Y : SemigroupModuleObject A ) : has_coe (SemigroupModuleMorphism X Y) (C.Hom X Y) :=
{ coe := SemigroupModuleMorphism.map }
-- set_option pp.implicit true
definition CategoryOfSemigroupModules { C : Category } { m : MonoidalStructure C } ( A : SemigroupObject m ) : Category :=
{
Obj := SemigroupModuleObject A,
Hom := λ X Y, SemigroupModuleMorphism X Y,
identity := λ X, ⟨ C.identity X, ♮ ⟩,
compose := λ X Y Z f g, ⟨ C.compose f.map g.map, ♮ ⟩,
left_identity := ♯,
right_identity := ♯,
associativity := ♮
}
end tqft.categories.internal_objects |
776550dfab318924704d6d5948dc044ef027953b | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/topology/topological_fiber_bundle.lean | 32a6b0c87c854152862279e9aee9a826b3c8517c | [
"Apache-2.0"
] | permissive | ilitzroth/mathlib | ea647e67f1fdfd19a0f7bdc5504e8acec6180011 | 5254ef14e3465f6504306132fe3ba9cec9ffff16 | refs/heads/master | 1,680,086,661,182 | 1,617,715,647,000 | 1,617,715,647,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 34,589 | 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.local_homeomorph
/-!
# Fiber bundles
A topological fiber bundle with fiber `F` over a base `B` is a space projecting on `B` for which the
fibers are all homeomorphic to `F`, such that the local situation around each point is a direct
product. We define a predicate `is_topological_fiber_bundle F p` saying that `p : Z → B` is a
topological fiber bundle with fiber `F`.
It is in general nontrivial to construct a fiber bundle. A way is to start from the knowledge of
how changes of local trivializations act on the fiber. From this, one can construct the total space
of the bundle and its topology by a suitable gluing construction. The main content of this file is
an implementation of this construction: starting from an object of type
`topological_fiber_bundle_core` registering the trivialization changes, one gets the corresponding
fiber bundle and projection.
## Main definitions
### Basic definitions
* `bundle_trivialization F p` : structure extending local homeomorphisms, defining a local
trivialization of a topological space `Z` with projection `p` and fiber `F`.
* `is_topological_fiber_bundle F p` : Prop saying that the map `p` between topological spaces is a
fiber bundle with fiber `F`.
* `is_trivial_topological_fiber_bundle F p` : Prop saying that the map `p : Z → B` between
topological spaces is a trivial topological fiber bundle, i.e., there exists a homeomorphism
`h : Z ≃ₜ B × F` such that `proj x = (h x).1`.
### Operations on bundles
We provide the following operations on `bundle_trivialization`s.
* `bundle_trivialization.comap`: given a local trivialization `e` of a fiber bundle `p : Z → B`, a
continuous map `f : B' → B` and a point `b' : B'` such that `f b' ∈ e.base_set`,
`e.comap f hf b' hb'` is a trivialization of the pullback bundle. The pullback bundle
(a.k.a., the induced bundle) has total space `{(x, y) : B' × Z | f x = p y}`, and is given by
`λ ⟨(x, y), h⟩, x`.
* `is_topological_fiber_bundle.comap`: if `p : Z → B` is a topological fiber bundle, then its
pullback along a continuous map `f : B' → B` is a topological fiber bundle as well.
* `bundle_trivialization.comp_homeomorph`: given a local trivialization `e` of a fiber bundle
`p : Z → B` and a homeomorphism `h : Z' ≃ₜ Z`, returns a local trivialization of the fiber bundle
`p ∘ h`.
* `is_topological_fiber_bundle.comp_homeomorph`: if `p : Z → B` is a topological fiber bundle
and `h : Z' ≃ₜ Z` is a homeomorphism, then `p ∘ h : Z' → B` is a topological fiber bundle with
the same fiber.
### Construction of a bundle from trivializations
* `bundle.total_space E` is a type synonym for `Σ (x : B), E x`, that we can endow with a suitable
topology.
* `topological_fiber_bundle_core ι B F` : structure registering how changes of coordinates act
on the fiber `F` above open subsets of `B`, where local trivializations are indexed by `ι`.
Let `Z : topological_fiber_bundle_core ι B F`. Then we define
* `Z.fiber x` : the fiber above `x`, homeomorphic to `F` (and defeq to `F` as a type).
* `Z.total_space` : the total space of `Z`, defined as a `Type` as `Σ (b : B), F`, but with a
twisted topology coming from the fiber bundle structure. It is (reducibly) the same as
`bundle.total_space Z.fiber`.
* `Z.proj` : projection from `Z.total_space` to `B`. It is continuous.
* `Z.local_triv i`: for `i : ι`, a local homeomorphism from `Z.total_space` to `B × F`, that
realizes a trivialization above the set `Z.base_set i`, which is an open set in `B`.
## Implementation notes
A topological fiber bundle with fiber `F` over a base `B` is a family of spaces isomorphic to `F`,
indexed by `B`, which is locally trivial in the following sense: there is a covering of `B` by open
sets such that, on each such open set `s`, the bundle is isomorphic to `s × F`.
To construct a fiber bundle formally, the main data is what happens when one changes trivializations
from `s × F` to `s' × F` on `s ∩ s'`: one should get a family of homeomorphisms of `F`, depending
continuously on the base point, satisfying basic compatibility conditions (cocycle property).
Useful classes of bundles can then be specified by requiring that these homeomorphisms of `F`
belong to some subgroup, preserving some structure (the "structure group of the bundle"): then
these structures are inherited by the fibers of the bundle.
Given such trivialization change data (encoded below in a structure called
`topological_fiber_bundle_core`), one can construct the fiber bundle. The intrinsic canonical
mathematical construction is the following.
The fiber above `x` is the disjoint union of `F` over all trivializations, modulo the gluing
identifications: one gets a fiber which is isomorphic to `F`, but non-canonically
(each choice of one of the trivializations around `x` gives such an isomorphism). Given a
trivialization over a set `s`, one gets an isomorphism between `s × F` and `proj^{-1} s`, by using
the identification corresponding to this trivialization. One chooses the topology on the bundle that
makes all of these into homeomorphisms.
For the practical implementation, it turns out to be more convenient to avoid completely the
gluing and quotienting construction above, and to declare above each `x` that the fiber is `F`,
but thinking that it corresponds to the `F` coming from the choice of one trivialization around `x`.
This has several practical advantages:
* without any work, one gets a topological space structure on the fiber. And if `F` has more
structure it is inherited for free by the fiber.
* In the case of the tangent bundle of manifolds, this implies that on vector spaces the derivative
(from `F` to `F`) and the manifold derivative (from `tangent_space I x` to `tangent_space I' (f x)`)
are equal.
A drawback is that some silly constructions will typecheck: in the case of the tangent bundle, one
can add two vectors in different tangent spaces (as they both are elements of `F` from the point of
view of Lean). To solve this, one could mark the tangent space as irreducible, but then one would
lose the identification of the tangent space to `F` with `F`. There is however a big advantage of
this situation: even if Lean can not check that two basepoints are defeq, it will accept the fact
that the tangent spaces are the same. For instance, if two maps `f` and `g` are locally inverse to
each other, one can express that the composition of their derivatives is the identity of
`tangent_space I x`. One could fear issues as this composition goes from `tangent_space I x` to
`tangent_space I (g (f x))` (which should be the same, but should not be obvious to Lean
as it does not know that `g (f x) = x`). As these types are the same to Lean (equal to `F`), there
are in fact no dependent type difficulties here!
For this construction of a fiber bundle from a `topological_fiber_bundle_core`, we should thus
choose for each `x` one specific trivialization around it. We include this choice in the definition
of the `topological_fiber_bundle_core`, as it makes some constructions more
functorial and it is a nice way to say that the trivializations cover the whole space `B`.
With this definition, the type of the fiber bundle space constructed from the core data is just
`Σ (b : B), F `, but the topology is not the product one, in general.
We also take the indexing type (indexing all the trivializations) as a parameter to the fiber bundle
core: it could always be taken as a subtype of all the maps from open subsets of `B` to continuous
maps of `F`, but in practice it will sometimes be something else. For instance, on a manifold, one
will use the set of charts as a good parameterization for the trivializations of the tangent bundle.
Or for the pullback of a `topological_fiber_bundle_core`, the indexing type will be the same as
for the initial bundle.
## Tags
Fiber bundle, topological bundle, vector bundle, local trivialization, structure group
-/
variables {ι : Type*} {B : Type*} {F : Type*}
open topological_space filter set
open_locale topological_space
/-! ### General definition of topological fiber bundles -/
section topological_fiber_bundle
variables (F) {Z : Type*} [topological_space B] [topological_space Z]
[topological_space F] {proj : Z → B}
/--
A structure extending local homeomorphisms, defining a local trivialization of a projection
`proj : Z → B` with fiber `F`, as a local homeomorphism between `Z` and `B × F` defined between two
sets of the form `proj ⁻¹' base_set` and `base_set × F`, acting trivially on the first coordinate.
-/
@[nolint has_inhabited_instance]
structure bundle_trivialization (proj : Z → B) extends local_homeomorph Z (B × F) :=
(base_set : set B)
(open_base_set : is_open base_set)
(source_eq : source = proj ⁻¹' base_set)
(target_eq : target = set.prod base_set univ)
(proj_to_fun : ∀ p ∈ source, (to_local_homeomorph p).1 = proj p)
instance : has_coe_to_fun (bundle_trivialization F proj) := ⟨_, λ e, e.to_fun⟩
variable {F}
@[simp, mfld_simps] lemma bundle_trivialization.coe_coe (e : bundle_trivialization F proj) :
⇑e.to_local_homeomorph = e := rfl
@[simp, mfld_simps] lemma bundle_trivialization.coe_mk
(e : local_homeomorph Z (B × F)) (i j k l m) (x : Z) :
(bundle_trivialization.mk e i j k l m : bundle_trivialization F proj) x = e x := rfl
variable (F)
/-- A topological fiber bundle with fiber `F` over a base `B` is a space projecting on `B`
for which the fibers are all homeomorphic to `F`, such that the local situation around each point
is a direct product. -/
def is_topological_fiber_bundle (proj : Z → B) : Prop :=
∀ x : B, ∃e : bundle_trivialization F proj, x ∈ e.base_set
/-- A trivial topological fiber bundle with fiber `F` over a base `B` is a space `Z`
projecting on `B` for which there exists a homeomorphism to `B × F` that sends `proj`
to `prod.fst`. -/
def is_trivial_topological_fiber_bundle (proj : Z → B) : Prop :=
∃ e : Z ≃ₜ (B × F), ∀ x, (e x).1 = proj x
variables {F}
lemma bundle_trivialization.mem_source (e : bundle_trivialization F proj)
{x : Z} : x ∈ e.source ↔ proj x ∈ e.base_set :=
by rw [e.source_eq, mem_preimage]
lemma bundle_trivialization.mem_target (e : bundle_trivialization F proj)
{x : B × F} : x ∈ e.target ↔ x.1 ∈ e.base_set :=
by rw [e.target_eq, prod_univ, mem_preimage]
@[simp, mfld_simps] lemma bundle_trivialization.coe_fst (e : bundle_trivialization F proj) {x : Z}
(ex : x ∈ e.source) : (e x).1 = proj x :=
e.proj_to_fun x ex
lemma bundle_trivialization.coe_fst' (e : bundle_trivialization F proj) {x : Z}
(ex : proj x ∈ e.base_set) : (e x).1 = proj x :=
e.coe_fst (e.mem_source.2 ex)
lemma bundle_trivialization.proj_symm_apply (e : bundle_trivialization F proj) {x : B × F}
(hx : x ∈ e.target) : proj (e.to_local_homeomorph.symm x) = x.1 :=
begin
have := (e.coe_fst (e.to_local_homeomorph.map_target hx)).symm,
rwa [← e.coe_coe, e.to_local_homeomorph.right_inv hx] at this
end
lemma bundle_trivialization.proj_symm_apply' (e : bundle_trivialization F proj) {b : B} {x : F}
(hx : b ∈ e.base_set) : proj (e.to_local_homeomorph.symm (b, x)) = b :=
e.proj_symm_apply (e.mem_target.2 hx)
lemma bundle_trivialization.apply_symm_apply (e : bundle_trivialization F proj)
{x : B × F} (hx : x ∈ e.target) : e (e.to_local_homeomorph.symm x) = x :=
e.to_local_homeomorph.right_inv hx
lemma bundle_trivialization.apply_symm_apply' (e : bundle_trivialization F proj)
{b : B} {x : F} (hx : b ∈ e.base_set) : e (e.to_local_homeomorph.symm (b, x)) = (b, x) :=
e.apply_symm_apply (e.mem_target.2 hx)
@[simp, mfld_simps] lemma bundle_trivialization.symm_apply_mk_proj
(e : bundle_trivialization F proj) {x : Z} (ex : x ∈ e.source) :
e.to_local_homeomorph.symm (proj x, (e x).2) = x :=
by rw [← e.coe_fst ex, prod.mk.eta, ← e.coe_coe, e.to_local_homeomorph.left_inv ex]
lemma bundle_trivialization.coe_fst_eventually_eq_proj (e : bundle_trivialization F proj)
{x : Z} (ex : x ∈ e.source) : prod.fst ∘ e =ᶠ[𝓝 x] proj :=
mem_nhds_sets_iff.2 ⟨e.source, λ y hy, e.coe_fst hy, e.open_source, ex⟩
lemma bundle_trivialization.coe_fst_eventually_eq_proj' (e : bundle_trivialization F proj)
{x : Z} (ex : proj x ∈ e.base_set) : prod.fst ∘ e =ᶠ[𝓝 x] proj :=
e.coe_fst_eventually_eq_proj (e.mem_source.2 ex)
lemma is_trivial_topological_fiber_bundle.is_topological_fiber_bundle
(h : is_trivial_topological_fiber_bundle F proj) :
is_topological_fiber_bundle F proj :=
let ⟨e, he⟩ := h in λ x,
⟨⟨e.to_local_homeomorph, univ, is_open_univ, rfl, univ_prod_univ.symm, λ x _, he x⟩, mem_univ x⟩
lemma bundle_trivialization.map_proj_nhds (e : bundle_trivialization F proj) {x : Z}
(ex : x ∈ e.source) : map proj (𝓝 x) = 𝓝 (proj x) :=
by rw [← e.coe_fst ex, ← map_congr (e.coe_fst_eventually_eq_proj ex), ← map_map, ← e.coe_coe,
e.to_local_homeomorph.map_nhds_eq ex, map_fst_nhds]
/-- In the domain of a bundle trivialization, the projection is continuous-/
lemma bundle_trivialization.continuous_at_proj (e : bundle_trivialization F proj) {x : Z}
(ex : x ∈ e.source) : continuous_at proj x :=
(e.map_proj_nhds ex).le
/-- The projection from a topological fiber bundle to its base is continuous. -/
lemma is_topological_fiber_bundle.continuous_proj (h : is_topological_fiber_bundle F proj) :
continuous proj :=
begin
rw continuous_iff_continuous_at,
assume x,
rcases h (proj x) with ⟨e, ex⟩,
apply e.continuous_at_proj,
rwa e.source_eq
end
/-- The projection from a topological fiber bundle to its base is an open map. -/
lemma is_topological_fiber_bundle.is_open_map_proj (h : is_topological_fiber_bundle F proj) :
is_open_map proj :=
begin
refine is_open_map_iff_nhds_le.2 (λ x, _),
rcases h (proj x) with ⟨e, ex⟩,
refine (e.map_proj_nhds _).ge,
rwa e.source_eq
end
/-- The first projection in a product is a trivial topological fiber bundle. -/
lemma is_trivial_topological_fiber_bundle_fst :
is_trivial_topological_fiber_bundle F (prod.fst : B × F → B) :=
⟨homeomorph.refl _, λ x, rfl⟩
/-- The first projection in a product is a topological fiber bundle. -/
lemma is_topological_fiber_bundle_fst : is_topological_fiber_bundle F (prod.fst : B × F → B) :=
is_trivial_topological_fiber_bundle_fst.is_topological_fiber_bundle
/-- The second projection in a product is a trivial topological fiber bundle. -/
lemma is_trivial_topological_fiber_bundle_snd :
is_trivial_topological_fiber_bundle F (prod.snd : F × B → B) :=
⟨homeomorph.prod_comm _ _, λ x, rfl⟩
/-- The second projection in a product is a topological fiber bundle. -/
lemma is_topological_fiber_bundle_snd : is_topological_fiber_bundle F (prod.snd : F × B → B) :=
is_trivial_topological_fiber_bundle_snd.is_topological_fiber_bundle
/-- Composition of a `bundle_trivialization` and a `homeomorph`. -/
def bundle_trivialization.comp_homeomorph {Z' : Type*} [topological_space Z']
(e : bundle_trivialization F proj) (h : Z' ≃ₜ Z) :
bundle_trivialization F (proj ∘ h) :=
{ to_local_homeomorph := h.to_local_homeomorph.trans e.to_local_homeomorph,
base_set := e.base_set,
open_base_set := e.open_base_set,
source_eq := by simp [e.source_eq, preimage_preimage],
target_eq := by simp [e.target_eq],
proj_to_fun := λ p hp,
have hp : h p ∈ e.source, by simpa using hp,
by simp [hp] }
lemma is_topological_fiber_bundle.comp_homeomorph {Z' : Type*} [topological_space Z']
(e : is_topological_fiber_bundle F proj) (h : Z' ≃ₜ Z) :
is_topological_fiber_bundle F (proj ∘ h) :=
λ x, let ⟨e, he⟩ := e x in
⟨e.comp_homeomorph h, by simpa [bundle_trivialization.comp_homeomorph] using he⟩
section induced
open_locale classical
variables {B' : Type*} [topological_space B']
/-- Given a bundle trivialization of `proj : Z → B` and a continuous map `f : B' → B`,
construct a bundle trivialization of `φ : {p : B' × Z | f p.1 = proj p.2} → B'`
given by `φ x = (x : B' × Z).1`. -/
noncomputable def bundle_trivialization.comap
(e : bundle_trivialization F proj) (f : B' → B) (hf : continuous f)
(b' : B') (hb' : f b' ∈ e.base_set) :
bundle_trivialization F (λ x : {p : B' × Z | f p.1 = proj p.2}, (x : B' × Z).1) :=
{ to_fun := λ p, ((p : B' × Z).1, (e (p : B' × Z).2).2),
inv_fun := λ p, if h : f p.1 ∈ e.base_set
then ⟨⟨p.1, e.to_local_homeomorph.symm (f p.1, p.2)⟩, by simp [e.proj_symm_apply' h]⟩
else ⟨⟨b', e.to_local_homeomorph.symm (f b', p.2)⟩, by simp [e.proj_symm_apply' hb']⟩,
source := {p | f (p : B' × Z).1 ∈ e.base_set},
target := {p | f p.1 ∈ e.base_set},
map_source' := λ p hp, hp,
map_target' := λ p (hp : f p.1 ∈ e.base_set), by simp [hp],
left_inv' :=
begin
rintro ⟨⟨b, x⟩, hbx⟩ hb,
dsimp at *,
have hx : x ∈ e.source, from e.mem_source.2 (hbx ▸ hb),
ext; simp *
end,
right_inv' := λ p (hp : f p.1 ∈ e.base_set), by simp [*, e.apply_symm_apply'],
open_source := e.open_base_set.preimage (hf.comp $ continuous_fst.comp continuous_subtype_coe),
open_target := e.open_base_set.preimage (hf.comp continuous_fst),
continuous_to_fun := ((continuous_fst.comp continuous_subtype_coe).continuous_on).prod $
continuous_snd.comp_continuous_on $ e.continuous_to_fun.comp
(continuous_snd.comp continuous_subtype_coe).continuous_on $
by { rintro ⟨⟨b, x⟩, (hbx : f b = proj x)⟩ (hb : f b ∈ e.base_set),
rw hbx at hb,
exact e.mem_source.2 hb },
continuous_inv_fun :=
begin
rw [embedding_subtype_coe.continuous_on_iff],
suffices : continuous_on (λ p : B' × F, (p.1, e.to_local_homeomorph.symm (f p.1, p.2)))
{p : B' × F | f p.1 ∈ e.base_set},
{ refine this.congr (λ p (hp : f p.1 ∈ e.base_set), _),
simp [hp] },
{ refine continuous_on_fst.prod (e.to_local_homeomorph.symm.continuous_on.comp _ _),
{ exact ((hf.comp continuous_fst).prod_mk continuous_snd).continuous_on },
{ exact λ p hp, e.mem_target.2 hp } }
end,
base_set := f ⁻¹' e.base_set,
source_eq := rfl,
target_eq := by { ext, simp },
open_base_set := e.open_base_set.preimage hf,
proj_to_fun := λ _ _, rfl }
/-- If `proj : Z → B` is a topological fiber bundle with fiber `F` and `f : B' → B` is a continuous
map, then the pullback bundle (a.k.a. induced bundle) is the topological bundle with the total space
`{(x, y) : B' × Z | f x = proj y}` given by `λ ⟨(x, y), h⟩, x`. -/
lemma is_topological_fiber_bundle.comap (h : is_topological_fiber_bundle F proj)
{f : B' → B} (hf : continuous f) :
is_topological_fiber_bundle F (λ x : {p : B' × Z | f p.1 = proj p.2}, (x : B' × Z).1) :=
λ x, let ⟨e, he⟩ := h (f x) in ⟨e.comap f hf x he, he⟩
end induced
end topological_fiber_bundle
/-! ### Constructing topological fiber bundles -/
namespace bundle
/- We provide a type synonym of `Σ x, E x` as `bundle.total_space E`, to be able to endow it with
a topology which is not the disjoint union topology. In general, the constructions of fiber bundles
we will make will be of this form. -/
variable (E : B → Type*)
/--
`total_space E` is the total space of the bundle `Σ x, E x`. This type synonym is used to avoid
conflicts with general sigma types.
-/
def total_space := Σ x, E x
instance [inhabited B] [inhabited (E (default B))] :
inhabited (total_space E) := ⟨⟨default B, default (E (default B))⟩⟩
/-- `bundle.proj E` is the canonical projection `total_space E → B` on the base space. -/
@[simp, mfld_simps] def proj : total_space E → B :=
λ (y : total_space E), y.1
instance {x : B} : has_coe_t (E x) (total_space E) := ⟨λ y, (⟨x, y⟩ : total_space E)⟩
lemma to_total_space_coe {x : B} (v : E x) : (v : total_space E) = ⟨x, v⟩ := rfl
/-- `bundle.trivial B F` is the trivial bundle over `B` of fiber `F`. -/
@[nolint unused_arguments]
def trivial (B : Type*) (F : Type*) : B → Type* := λ x, F
instance [inhabited F] {b : B} : inhabited (bundle.trivial B F b) :=
⟨(default F : F)⟩
/-- The trivial bundle, unlike other bundles, has a canonical projection on the fiber. -/
def trivial.proj_snd (B : Type*) (F : Type*) : (total_space (bundle.trivial B F)) → F := sigma.snd
instance [I : topological_space F] : ∀ x : B, topological_space (trivial B F x) := λ x, I
instance [t₁ : topological_space B] [t₂ : topological_space F] :
topological_space (total_space (trivial B F)) :=
topological_space.induced (proj (trivial B F)) t₁ ⊓
topological_space.induced (trivial.proj_snd B F) t₂
end bundle
/-- Core data defining a locally trivial topological bundle with fiber `F` over a topological
space `B`. Note that "bundle" is used in its mathematical sense. This is the (computer science)
bundled version, i.e., all the relevant data is contained in the following structure. A family of
local trivializations is indexed by a type ι, on open subsets `base_set i` for each `i : ι`.
Trivialization changes from `i` to `j` are given by continuous maps `coord_change i j` from
`base_set i ∩ base_set j` to the set of homeomorphisms of `F`, but we express them as maps
`B → F → F` and require continuity on `(base_set i ∩ base_set j) × F` to avoid the topology on the
space of continuous maps on `F`. -/
@[nolint has_inhabited_instance]
structure topological_fiber_bundle_core (ι : Type*) (B : Type*) [topological_space B]
(F : Type*) [topological_space F] :=
(base_set : ι → set B)
(is_open_base_set : ∀i, is_open (base_set i))
(index_at : B → ι)
(mem_base_set_at : ∀x, x ∈ base_set (index_at x))
(coord_change : ι → ι → B → F → F)
(coord_change_self : ∀i, ∀ x ∈ base_set i, ∀v, coord_change i i x v = v)
(coord_change_continuous : ∀i j, continuous_on (λp : B × F, coord_change i j p.1 p.2)
(set.prod ((base_set i) ∩ (base_set j)) univ))
(coord_change_comp : ∀i j k, ∀x ∈ (base_set i) ∩ (base_set j) ∩ (base_set k), ∀v,
(coord_change j k x) (coord_change i j x v) = coord_change i k x v)
attribute [simp, mfld_simps] topological_fiber_bundle_core.mem_base_set_at
namespace topological_fiber_bundle_core
variables [topological_space B] [topological_space F] (Z : topological_fiber_bundle_core ι B F)
include Z
/-- The index set of a topological fiber bundle core, as a convenience function for dot notation -/
@[nolint unused_arguments has_inhabited_instance]
def index := ι
/-- The base space of a topological fiber bundle core, as a convenience function for dot notation -/
@[nolint unused_arguments, reducible]
def base := B
/-- The fiber of a topological fiber bundle core, as a convenience function for dot notation and
typeclass inference -/
@[nolint unused_arguments has_inhabited_instance]
def fiber (x : B) := F
instance topological_space_fiber (x : B) : topological_space (Z.fiber x) :=
by { dsimp [fiber], apply_instance }
/-- The total space of the topological fiber bundle, as a convenience function for dot notation.
It is by definition equal to `bundle.total_space Z.fiber`, a.k.a. `Σ x, Z.fiber x` but with a
different name for typeclass inference. -/
@[nolint unused_arguments, reducible]
def total_space := bundle.total_space Z.fiber
/-- The projection from the total space of a topological fiber bundle core, on its base. -/
@[reducible, simp, mfld_simps] def proj : Z.total_space → B := bundle.proj Z.fiber
/-- Local homeomorphism version of the trivialization change. -/
def triv_change (i j : ι) : local_homeomorph (B × F) (B × F) :=
{ source := set.prod (Z.base_set i ∩ Z.base_set j) univ,
target := set.prod (Z.base_set i ∩ Z.base_set j) univ,
to_fun := λp, ⟨p.1, Z.coord_change i j p.1 p.2⟩,
inv_fun := λp, ⟨p.1, Z.coord_change j i p.1 p.2⟩,
map_source' := λp hp, by simpa using hp,
map_target' := λp hp, by simpa using hp,
left_inv' := begin
rintros ⟨x, v⟩ hx,
simp only [prod_mk_mem_set_prod_eq, mem_inter_eq, and_true, mem_univ] at hx,
rw [Z.coord_change_comp, Z.coord_change_self],
{ exact hx.1 },
{ simp [hx] }
end,
right_inv' := begin
rintros ⟨x, v⟩ hx,
simp only [prod_mk_mem_set_prod_eq, mem_inter_eq, and_true, mem_univ] at hx,
rw [Z.coord_change_comp, Z.coord_change_self],
{ exact hx.2 },
{ simp [hx] },
end,
open_source :=
(is_open_inter (Z.is_open_base_set i) (Z.is_open_base_set j)).prod is_open_univ,
open_target :=
(is_open_inter (Z.is_open_base_set i) (Z.is_open_base_set j)).prod is_open_univ,
continuous_to_fun :=
continuous_on.prod continuous_fst.continuous_on (Z.coord_change_continuous i j),
continuous_inv_fun := by simpa [inter_comm]
using continuous_on.prod continuous_fst.continuous_on (Z.coord_change_continuous j i) }
@[simp, mfld_simps] lemma mem_triv_change_source (i j : ι) (p : B × F) :
p ∈ (Z.triv_change i j).source ↔ p.1 ∈ Z.base_set i ∩ Z.base_set j :=
by { erw [mem_prod], simp }
/-- Associate to a trivialization index `i : ι` the corresponding trivialization, i.e., a bijection
between `proj ⁻¹ (base_set i)` and `base_set i × F`. As the fiber above `x` is `F` but read in the
chart with index `index_at x`, the trivialization in the fiber above x is by definition the
coordinate change from i to `index_at x`, so it depends on `x`.
The local trivialization will ultimately be a local homeomorphism. For now, we only introduce the
local equiv version, denoted with a prime. In further developments, avoid this auxiliary version,
and use `Z.local_triv` instead.
-/
def local_triv' (i : ι) : local_equiv Z.total_space (B × F) :=
{ source := Z.proj ⁻¹' (Z.base_set i),
target := set.prod (Z.base_set i) univ,
inv_fun := λp, ⟨p.1, Z.coord_change i (Z.index_at p.1) p.1 p.2⟩,
to_fun := λp, ⟨p.1, Z.coord_change (Z.index_at p.1) i p.1 p.2⟩,
map_source' := λp hp,
by simpa only [set.mem_preimage, and_true, set.mem_univ, set.prod_mk_mem_set_prod_eq] using hp,
map_target' := λp hp,
by simpa only [set.mem_preimage, and_true, set.mem_univ, set.mem_prod] using hp,
left_inv' := begin
rintros ⟨x, v⟩ hx,
change x ∈ Z.base_set i at hx,
dsimp,
rw [Z.coord_change_comp, Z.coord_change_self],
{ exact Z.mem_base_set_at _ },
{ simp [hx] }
end,
right_inv' := begin
rintros ⟨x, v⟩ hx,
simp only [prod_mk_mem_set_prod_eq, and_true, mem_univ] at hx,
rw [Z.coord_change_comp, Z.coord_change_self],
{ exact hx },
{ simp [hx] }
end }
@[simp, mfld_simps] lemma mem_local_triv'_source (i : ι) (p : Z.total_space) :
p ∈ (Z.local_triv' i).source ↔ p.1 ∈ Z.base_set i :=
iff.rfl
@[simp, mfld_simps] lemma mem_local_triv'_target (i : ι) (p : B × F) :
p ∈ (Z.local_triv' i).target ↔ p.1 ∈ Z.base_set i :=
by { erw [mem_prod], simp }
@[simp, mfld_simps] lemma local_triv'_apply (i : ι) (p : Z.total_space) :
(Z.local_triv' i) p = ⟨p.1, Z.coord_change (Z.index_at p.1) i p.1 p.2⟩ := rfl
@[simp, mfld_simps] lemma local_triv'_symm_apply (i : ι) (p : B × F) :
(Z.local_triv' i).symm p = ⟨p.1, Z.coord_change i (Z.index_at p.1) p.1 p.2⟩ := rfl
/-- The composition of two local trivializations is the trivialization change Z.triv_change i j. -/
lemma local_triv'_trans (i j : ι) :
(Z.local_triv' i).symm.trans (Z.local_triv' j) ≈ (Z.triv_change i j).to_local_equiv :=
begin
split,
{ ext x, erw [mem_prod], simp [local_equiv.trans_source] },
{ rintros ⟨x, v⟩ hx,
simp only [triv_change, local_triv', local_equiv.symm, true_and, prod_mk_mem_set_prod_eq,
local_equiv.trans_source, mem_inter_eq, and_true, mem_univ, prod.mk.inj_iff, mem_preimage,
proj, local_equiv.coe_mk, eq_self_iff_true, local_equiv.coe_trans, bundle.proj] at hx ⊢,
simp [Z.coord_change_comp, hx], }
end
/-- Topological structure on the total space of a topological bundle created from core, designed so
that all the local trivialization are continuous. -/
instance to_topological_space : topological_space (bundle.total_space Z.fiber) :=
topological_space.generate_from $ ⋃ (i : ι) (s : set (B × F)) (s_open : is_open s),
{(Z.local_triv' i).source ∩ (Z.local_triv' i) ⁻¹' s}
lemma open_source' (i : ι) : is_open (Z.local_triv' i).source :=
begin
apply topological_space.generate_open.basic,
simp only [exists_prop, mem_Union, mem_singleton_iff],
refine ⟨i, set.prod (Z.base_set i) univ, (Z.is_open_base_set i).prod is_open_univ, _⟩,
ext p,
simp only with mfld_simps
end
lemma open_target' (i : ι) : is_open (Z.local_triv' i).target :=
(Z.is_open_base_set i).prod is_open_univ
/-- Local trivialization of a topological bundle created from core, as a local homeomorphism. -/
def local_triv (i : ι) : local_homeomorph Z.total_space (B × F) :=
{ open_source := Z.open_source' i,
open_target := Z.open_target' i,
continuous_to_fun := begin
rw continuous_on_open_iff (Z.open_source' i),
assume s s_open,
apply topological_space.generate_open.basic,
simp only [exists_prop, mem_Union, mem_singleton_iff],
exact ⟨i, s, s_open, rfl⟩
end,
continuous_inv_fun := begin
apply continuous_on_open_of_generate_from (Z.open_target' i),
assume t ht,
simp only [exists_prop, mem_Union, mem_singleton_iff] at ht,
obtain ⟨j, s, s_open, ts⟩ : ∃ j s,
is_open s ∧ t = (local_triv' Z j).source ∩ (local_triv' Z j) ⁻¹' s := ht,
rw ts,
simp only [local_equiv.right_inv, preimage_inter, local_equiv.left_inv],
let e := Z.local_triv' i,
let e' := Z.local_triv' j,
let f := e.symm.trans e',
have : is_open (f.source ∩ f ⁻¹' s),
{ rw [(Z.local_triv'_trans i j).source_inter_preimage_eq],
exact (continuous_on_open_iff (Z.triv_change i j).open_source).1
((Z.triv_change i j).continuous_on) _ s_open },
convert this using 1,
dsimp [local_equiv.trans_source],
rw [← preimage_comp, inter_assoc]
end,
to_local_equiv := Z.local_triv' i }
/- We will now state again the basic properties of the local trivializations, but without primes,
i.e., for the local homeomorphism instead of the local equiv. -/
@[simp, mfld_simps] lemma mem_local_triv_source (i : ι) (p : Z.total_space) :
p ∈ (Z.local_triv i).source ↔ p.1 ∈ Z.base_set i :=
iff.rfl
@[simp, mfld_simps] lemma mem_local_triv_target (i : ι) (p : B × F) :
p ∈ (Z.local_triv i).target ↔ p.1 ∈ Z.base_set i :=
by { erw [mem_prod], simp }
@[simp, mfld_simps] lemma local_triv_apply (i : ι) (p : Z.total_space) :
(Z.local_triv i) p = ⟨p.1, Z.coord_change (Z.index_at p.1) i p.1 p.2⟩ := rfl
@[simp, mfld_simps] lemma local_triv_symm_fst (i : ι) (p : B × F) :
(Z.local_triv i).symm p = ⟨p.1, Z.coord_change i (Z.index_at p.1) p.1 p.2⟩ := rfl
/-- The composition of two local trivializations is the trivialization change Z.triv_change i j. -/
lemma local_triv_trans (i j : ι) :
(Z.local_triv i).symm.trans (Z.local_triv j) ≈ Z.triv_change i j :=
Z.local_triv'_trans i j
/-- Extended version of the local trivialization of a fiber bundle constructed from core,
registering additionally in its type that it is a local bundle trivialization. -/
def local_triv_ext (i : ι) : bundle_trivialization F Z.proj :=
{ base_set := Z.base_set i,
open_base_set := Z.is_open_base_set i,
source_eq := rfl,
target_eq := rfl,
proj_to_fun := λp hp, by simp,
to_local_homeomorph := Z.local_triv i }
/-- A topological fiber bundle constructed from core is indeed a topological fiber bundle. -/
protected theorem is_topological_fiber_bundle : is_topological_fiber_bundle F Z.proj :=
λx, ⟨Z.local_triv_ext (Z.index_at x), Z.mem_base_set_at x⟩
/-- The projection on the base of a topological bundle created from core is continuous -/
lemma continuous_proj : continuous Z.proj :=
Z.is_topological_fiber_bundle.continuous_proj
/-- The projection on the base of a topological bundle created from core is an open map -/
lemma is_open_map_proj : is_open_map Z.proj :=
Z.is_topological_fiber_bundle.is_open_map_proj
/-- Preferred local trivialization of a fiber bundle constructed from core, at a given point, as
a local homeomorphism -/
def local_triv_at (p : Z.total_space) :
local_homeomorph Z.total_space (B × F) :=
Z.local_triv (Z.index_at (Z.proj p))
@[simp, mfld_simps] lemma mem_local_triv_at_source (p : Z.total_space) :
p ∈ (Z.local_triv_at p).source :=
by simp [local_triv_at]
@[simp, mfld_simps] lemma local_triv_at_fst (p q : Z.total_space) :
((Z.local_triv_at p) q).1 = q.1 := rfl
@[simp, mfld_simps] lemma local_triv_at_symm_fst (p : Z.total_space) (q : B × F) :
((Z.local_triv_at p).symm q).1 = q.1 := rfl
/-- Preferred local trivialization of a fiber bundle constructed from core, at a given point, as
a bundle trivialization -/
def local_triv_at_ext (p : Z.total_space) : bundle_trivialization F Z.proj :=
Z.local_triv_ext (Z.index_at (Z.proj p))
@[simp, mfld_simps] lemma local_triv_at_ext_to_local_homeomorph (p : Z.total_space) :
(Z.local_triv_at_ext p).to_local_homeomorph = Z.local_triv_at p := rfl
/-- If an element of `F` is invariant under all coordinate changes, then one can define a
corresponding section of the fiber bundle, which is continuous. This applies in particular to the
zero section of a vector bundle. Another example (not yet defined) would be the identity
section of the endomorphism bundle of a vector bundle. -/
lemma continuous_const_section (v : F)
(h : ∀ i j, ∀ x ∈ (Z.base_set i) ∩ (Z.base_set j), Z.coord_change i j x v = v) :
continuous (show B → Z.total_space, from λ x, ⟨x, v⟩) :=
begin
apply continuous_iff_continuous_at.2 (λ x, _),
have A : Z.base_set (Z.index_at x) ∈ 𝓝 x :=
mem_nhds_sets (Z.is_open_base_set (Z.index_at x)) (Z.mem_base_set_at x),
apply ((Z.local_triv (Z.index_at x)).continuous_at_iff_continuous_at_comp_left _).2,
{ simp only [(∘)] with mfld_simps,
apply continuous_at_id.prod,
have : continuous_on (λ (y : B), v) (Z.base_set (Z.index_at x)) := continuous_on_const,
apply (this.congr _).continuous_at A,
assume y hy,
simp only [h, hy] with mfld_simps },
{ exact A }
end
end topological_fiber_bundle_core
|
7fcd5c5a7930b484dec8cf68507aa73443ee4b59 | 05b503addd423dd68145d68b8cde5cd595d74365 | /src/linear_algebra/basis.lean | df547010a238770a01a653da8f18cad76d868525 | [
"Apache-2.0"
] | permissive | aestriplex/mathlib | 77513ff2b176d74a3bec114f33b519069788811d | e2fa8b2b1b732d7c25119229e3cdfba8370cb00f | refs/heads/master | 1,621,969,960,692 | 1,586,279,279,000 | 1,586,279,279,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 53,669 | 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, Alexander Bentkamp
-/
import linear_algebra.basic linear_algebra.finsupp order.zorn
import data.fintype.card
/-!
# Linear independence and bases
This file defines linear independence and bases in a module or vector space.
It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light.
## Main definitions
All definitions are given for families of vectors, i.e. `v : ι → M` where `M` is the module or
vector space and `ι : Type*` is an arbitrary indexing type.
* `linear_independent R v` states that the elements of the family `v` are linearly independent.
* `linear_independent.repr hv x` returns the linear combination representing `x : span R (range v)`
on the linearly independent vectors `v`, given `hv : linear_independent R v`
(using classical choice). `linear_independent.repr hv` is provided as a linear map.
* `is_basis R v` states that the vector family `v` is a basis, i.e. it is linearly independent and
spans the entire space.
* `is_basis.repr hv x` is the basis version of `linear_independent.repr hv x`. It returns the
linear combination representing `x : M` on a basis `v` of `M` (using classical choice).
The argument `hv` must be a proof that `is_basis R v`. `is_basis.repr hv` is given as a linear
map as well.
* `is_basis.constr hv f` constructs a linear map `M₁ →ₗ[R] M₂` given the values `f : ι → M₂` at the
basis `v : ι → M₁`, given `hv : is_basis R v`.
## Main statements
* `is_basis.ext` states that two linear maps are equal if they coincide on a basis.
* `exists_is_basis` states that every vector space has a basis.
## Implementation notes
We use families instead of sets because it allows us to say that two identical vectors are linearly
dependent. For bases, this is useful as well because we can easily derive ordered bases by using an
ordered index type `ι`.
If you want to use sets, use the family `(λ x, x : s → M)` given a set `s : set M`. The lemmas
`linear_independent.to_subtype_range` and `linear_independent.of_subtype_range` connect those two
worlds.
## Tags
linearly dependent, linear dependence, linearly independent, linear independence, basis
-/
noncomputable theory
open function set submodule
open_locale classical
variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*}
{M : Type*} {M' : Type*} {V : Type*} {V' : Type*}
section module
variables {v : ι → M}
variables [ring R] [add_comm_group M] [add_comm_group M']
variables [module R M] [module R M']
variables {a b : R} {x y : M}
variables (R) (v)
/-- Linearly independent family of vectors -/
def linear_independent : Prop := (finsupp.total ι M R v).ker = ⊥
variables {R} {v}
theorem linear_independent_iff : linear_independent R v ↔
∀l, finsupp.total ι M R v l = 0 → l = 0 :=
by simp [linear_independent, linear_map.ker_eq_bot']
theorem linear_independent_iff' : linear_independent R v ↔
∀ s : finset ι, ∀ g : ι → R, s.sum (λ i, g i • v i) = 0 → ∀ i ∈ s, g i = 0 :=
linear_independent_iff.trans
⟨λ hf s g hg i his, have h : _ := hf (s.sum $ λ i, finsupp.single i (g i)) $
by simpa only [linear_map.map_sum, finsupp.total_single] using hg, calc
g i = (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single i (g i)) :
by rw [finsupp.lapply_apply, finsupp.single_eq_same]
... = s.sum (λ j, (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single j (g j))) :
eq.symm $ finset.sum_eq_single i
(λ j hjs hji, by rw [finsupp.lapply_apply, finsupp.single_eq_of_ne hji])
(λ hnis, hnis.elim his)
... = s.sum (λ j, finsupp.single j (g j)) i : (finsupp.lapply i : (ι →₀ R) →ₗ[R] R).map_sum.symm
... = 0 : finsupp.ext_iff.1 h i,
λ hf l hl, finsupp.ext $ λ i, classical.by_contradiction $ λ hni, hni $ hf _ _ hl _ $
finsupp.mem_support_iff.2 hni⟩
lemma linear_independent_empty_type (h : ¬ nonempty ι) : linear_independent R v :=
begin
rw [linear_independent_iff],
intros,
ext i,
exact false.elim (not_nonempty_iff_imp_false.1 h i)
end
lemma ne_zero_of_linear_independent
{i : ι} (ne : 0 ≠ (1:R)) (hv : linear_independent R v) : v i ≠ 0 :=
λ h, ne $ eq.symm begin
suffices : (finsupp.single i 1 : ι →₀ R) i = 0, {simpa},
rw linear_independent_iff.1 hv (finsupp.single i 1),
{simp},
{simp [h]}
end
lemma linear_independent.comp
(h : linear_independent R v) (f : ι' → ι) (hf : injective f) : linear_independent R (v ∘ f) :=
begin
rw [linear_independent_iff, finsupp.total_comp],
intros l hl,
have h_map_domain : ∀ x, (finsupp.map_domain f l) (f x) = 0,
by rw linear_independent_iff.1 h (finsupp.map_domain f l) hl; simp,
ext,
convert h_map_domain a,
simp only [finsupp.map_domain_apply hf],
end
lemma linear_independent_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : linear_independent R v :=
linear_independent_iff.2 (λ l hl, finsupp.eq_zero_of_zero_eq_one zero_eq_one _)
lemma linear_independent.unique (hv : linear_independent R v) {l₁ l₂ : ι →₀ R} :
finsupp.total ι M R v l₁ = finsupp.total ι M R v l₂ → l₁ = l₂ :=
by apply linear_map.ker_eq_bot.1 hv
lemma linear_independent.injective (zero_ne_one : (0 : R) ≠ 1) (hv : linear_independent R v) :
injective v :=
begin
intros i j hij,
let l : ι →₀ R := finsupp.single i (1 : R) - finsupp.single j 1,
have h_total : finsupp.total ι M R v l = 0,
{ rw finsupp.total_apply,
rw finsupp.sum_sub_index,
{ simp [finsupp.sum_single_index, hij] },
{ intros, apply sub_smul } },
have h_single_eq : finsupp.single i (1 : R) = finsupp.single j 1,
{ rw linear_independent_iff at hv,
simp [eq_add_of_sub_eq' (hv l h_total)] },
show i = j,
{ apply or.elim ((finsupp.single_eq_single_iff _ _ _ _).1 h_single_eq),
simp,
exact λ h, false.elim (zero_ne_one.symm h.1) }
end
lemma linear_independent_span (hs : linear_independent R v) :
@linear_independent ι R (span R (range v))
(λ i : ι, ⟨v i, subset_span (mem_range_self i)⟩) _ _ _ :=
begin
rw linear_independent_iff at *,
intros l hl,
apply hs l,
have := congr_arg (submodule.subtype (span R (range v))) hl,
convert this,
rw [finsupp.total_apply, finsupp.total_apply],
unfold finsupp.sum,
rw linear_map.map_sum (submodule.subtype (span R (range v))),
simp
end
section subtype
/-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/
theorem linear_independent_comp_subtype {s : set ι} :
linear_independent R (v ∘ subtype.val : s → M) ↔
∀ l ∈ (finsupp.supported R R s), (finsupp.total ι M R v) l = 0 → l = 0 :=
begin
rw [linear_independent_iff, finsupp.total_comp],
simp only [linear_map.comp_apply],
split,
{ intros h l hl₁ hl₂,
have h_bij : bij_on subtype.val (subtype.val ⁻¹' l.support.to_set : set s) l.support.to_set,
{ apply bij_on.mk,
{ unfold maps_to },
{ apply subtype.val_injective.inj_on },
intros i hi,
rw [image_preimage_eq_inter_range, subtype.range_val],
exact ⟨hi, (finsupp.mem_supported _ _).1 hl₁ hi⟩ },
show l = 0,
{ apply finsupp.eq_zero_of_comap_domain_eq_zero (subtype.val : s → ι) _ h_bij,
apply h,
convert hl₂,
rw [finsupp.lmap_domain_apply, finsupp.map_domain_comap_domain],
exact subtype.val_injective,
rw subtype.range_val,
exact (finsupp.mem_supported _ _).1 hl₁ } },
{ intros h l hl,
have hl' : finsupp.total ι M R v (finsupp.emb_domain ⟨subtype.val, subtype.val_injective⟩ l) = 0,
{ rw finsupp.emb_domain_eq_map_domain ⟨subtype.val, subtype.val_injective⟩ l,
apply hl },
apply finsupp.emb_domain_inj.1,
rw [h (finsupp.emb_domain ⟨subtype.val, subtype.val_injective⟩ l) _ hl',
finsupp.emb_domain_zero],
rw [finsupp.mem_supported, finsupp.support_emb_domain],
intros x hx,
rw [finset.mem_coe, finset.mem_map] at hx,
rcases hx with ⟨i, x', hx'⟩,
rw ←hx',
simp }
end
theorem linear_independent_subtype {s : set M} :
linear_independent R (λ x, x : s → M) ↔
∀ l ∈ (finsupp.supported R R s), (finsupp.total M M R id) l = 0 → l = 0 :=
by apply @linear_independent_comp_subtype _ _ _ id
theorem linear_independent_comp_subtype_disjoint {s : set ι} :
linear_independent R (v ∘ subtype.val : s → M) ↔
disjoint (finsupp.supported R R s) (finsupp.total ι M R v).ker :=
by rw [linear_independent_comp_subtype, linear_map.disjoint_ker]
theorem linear_independent_subtype_disjoint {s : set M} :
linear_independent R (λ x, x : s → M) ↔
disjoint (finsupp.supported R R s) (finsupp.total M M R id).ker :=
by apply @linear_independent_comp_subtype_disjoint _ _ _ id
theorem linear_independent_iff_total_on {s : set M} :
linear_independent R (λ x, x : s → M) ↔ (finsupp.total_on M M R id s).ker = ⊥ :=
by rw [finsupp.total_on, linear_map.ker, linear_map.comap_cod_restrict, map_bot, comap_bot,
linear_map.ker_comp, linear_independent_subtype_disjoint, disjoint, ← map_comap_subtype,
map_le_iff_le_comap, comap_bot, ker_subtype, le_bot_iff]
lemma linear_independent.to_subtype_range
(hv : linear_independent R v) : linear_independent R (λ x, x : range v → M) :=
begin
by_cases zero_eq_one : (0 : R) = 1,
{ apply linear_independent_of_zero_eq_one zero_eq_one },
rw linear_independent_subtype,
intros l hl₁ hl₂,
have h_bij : bij_on v (v ⁻¹' finset.to_set (l.support)) (finset.to_set (l.support)),
{ apply bij_on.mk,
{ unfold maps_to },
{ apply (linear_independent.injective zero_eq_one hv).inj_on },
intros x hx,
rcases mem_range.1 (((finsupp.mem_supported _ _).1 hl₁ : ↑(l.support) ⊆ range v) hx)
with ⟨i, hi⟩,
rw mem_image,
use i,
rw [mem_preimage, hi],
exact ⟨hx, rfl⟩ },
apply finsupp.eq_zero_of_comap_domain_eq_zero v l,
apply linear_independent_iff.1 hv,
rw [finsupp.total_comap_domain, finset.sum_preimage v l.support h_bij (λ (x : M), l x • x)],
rw [finsupp.total_apply, finsupp.sum] at hl₂,
apply hl₂
end
lemma linear_independent.of_subtype_range (hv : injective v)
(h : linear_independent R (λ x, x : range v → M)) : linear_independent R v :=
begin
rw linear_independent_iff,
intros l hl,
apply finsupp.injective_map_domain hv,
apply linear_independent_subtype.1 h (l.map_domain v),
{ rw finsupp.mem_supported,
intros x hx,
have := finset.mem_coe.2 (finsupp.map_domain_support hx),
rw finset.coe_image at this,
apply set.image_subset_range _ _ this, },
{ rwa [finsupp.total_map_domain _ _ hv, left_id] }
end
lemma linear_independent.restrict_of_comp_subtype {s : set ι}
(hs : linear_independent R (v ∘ subtype.val : s → M)) :
linear_independent R (s.restrict v) :=
begin
have h_restrict : restrict v s = v ∘ (λ x, x.val) := rfl,
rw [linear_independent_iff, h_restrict, finsupp.total_comp],
intros l hl,
have h_map_domain_subtype_eq_0 : l.map_domain subtype.val = 0,
{ rw linear_independent_comp_subtype at hs,
apply hs (finsupp.lmap_domain R R (λ x : subtype s, x.val) l) _ hl,
rw finsupp.mem_supported,
simp,
intros x hx,
have := finset.mem_coe.2 (finsupp.map_domain_support (finset.mem_coe.1 hx)),
rw finset.coe_image at this,
exact subtype.val_image_subset _ _ this },
apply @finsupp.injective_map_domain _ (subtype s) ι,
{ apply subtype.val_injective },
{ simpa },
end
lemma linear_independent_empty : linear_independent R (λ x, x : (∅ : set M) → M) :=
by simp [linear_independent_subtype_disjoint]
lemma linear_independent.mono {t s : set M} (h : t ⊆ s) :
linear_independent R (λ x, x : s → M) → linear_independent R (λ x, x : t → M) :=
begin
simp only [linear_independent_subtype_disjoint],
exact (disjoint.mono_left (finsupp.supported_mono h))
end
lemma linear_independent_union {s t : set M}
(hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M))
(hst : disjoint (span R s) (span R t)) :
linear_independent R (λ x, x : (s ∪ t) → M) :=
begin
rw [linear_independent_subtype_disjoint, disjoint_def, finsupp.supported_union],
intros l h₁ h₂, rw mem_sup at h₁,
rcases h₁ with ⟨ls, hls, lt, hlt, rfl⟩,
have h_ls_mem_t : finsupp.total M M R id ls ∈ span R t,
{ rw [← image_id t, finsupp.span_eq_map_total],
apply (add_mem_iff_left (map _ _) (mem_image_of_mem _ hlt)).1,
rw [← linear_map.map_add, linear_map.mem_ker.1 h₂],
apply zero_mem },
have h_lt_mem_s : finsupp.total M M R id lt ∈ span R s,
{ rw [← image_id s, finsupp.span_eq_map_total],
apply (add_mem_iff_left (map _ _) (mem_image_of_mem _ hls)).1,
rw [← linear_map.map_add, add_comm, linear_map.mem_ker.1 h₂],
apply zero_mem },
have h_ls_mem_s : (finsupp.total M M R id) ls ∈ span R s,
{ rw ← image_id s,
apply (finsupp.mem_span_iff_total _).2 ⟨ls, hls, rfl⟩ },
have h_lt_mem_t : (finsupp.total M M R id) lt ∈ span R t,
{ rw ← image_id t,
apply (finsupp.mem_span_iff_total _).2 ⟨lt, hlt, rfl⟩ },
have h_ls_0 : ls = 0 :=
disjoint_def.1 (linear_independent_subtype_disjoint.1 hs) _ hls
(linear_map.mem_ker.2 $ disjoint_def.1 hst (finsupp.total M M R id ls) h_ls_mem_s h_ls_mem_t),
have h_lt_0 : lt = 0 :=
disjoint_def.1 (linear_independent_subtype_disjoint.1 ht) _ hlt
(linear_map.mem_ker.2 $ disjoint_def.1 hst (finsupp.total M M R id lt) h_lt_mem_s h_lt_mem_t),
show ls + lt = 0,
by simp [h_ls_0, h_lt_0],
end
lemma linear_independent_of_finite (s : set M)
(H : ∀ t ⊆ s, finite t → linear_independent R (λ x, x : t → M)) :
linear_independent R (λ x, x : s → M) :=
linear_independent_subtype.2 $
λ l hl, linear_independent_subtype.1 (H _ hl (finset.finite_to_set _)) l (subset.refl _)
lemma linear_independent_Union_of_directed {η : Type*}
{s : η → set M} (hs : directed (⊆) s)
(h : ∀ i, linear_independent R (λ x, x : s i → M)) :
linear_independent R (λ x, x : (⋃ i, s i) → M) :=
begin
haveI := classical.dec (nonempty η),
by_cases hη : nonempty η,
{ refine linear_independent_of_finite (⋃ i, s i) (λ t ht ft, _),
rcases finite_subset_Union ft ht with ⟨I, fi, hI⟩,
rcases hs.finset_le hη fi.to_finset with ⟨i, hi⟩,
exact (h i).mono (subset.trans hI $ bUnion_subset $
λ j hj, hi j (finite.mem_to_finset.2 hj)) },
{ refine linear_independent_empty.mono _,
rintro _ ⟨_, ⟨i, _⟩, _⟩, exact hη ⟨i⟩ }
end
lemma linear_independent_sUnion_of_directed {s : set (set M)}
(hs : directed_on (⊆) s)
(h : ∀ a ∈ s, linear_independent R (λ x, x : (a : set M) → M)) :
linear_independent R (λ x, x : (⋃₀ s) → M) :=
by rw sUnion_eq_Union; exact
linear_independent_Union_of_directed
((directed_on_iff_directed _).1 hs) (by simpa using h)
lemma linear_independent_bUnion_of_directed {η} {s : set η} {t : η → set M}
(hs : directed_on (t ⁻¹'o (⊆)) s) (h : ∀a∈s, linear_independent R (λ x, x : t a → M)) :
linear_independent R (λ x, x : (⋃a∈s, t a) → M) :=
by rw bUnion_eq_Union; exact
linear_independent_Union_of_directed
((directed_comp _ _ _).2 $ (directed_on_iff_directed _).1 hs)
(by simpa using h)
lemma linear_independent_Union_finite_subtype {ι : Type*} {f : ι → set M}
(hl : ∀i, linear_independent R (λ x, x : f i → M))
(hd : ∀i, ∀t:set ι, finite t → i ∉ t → disjoint (span R (f i)) (⨆i∈t, span R (f i))) :
linear_independent R (λ x, x : (⋃i, f i) → M) :=
begin
rw [Union_eq_Union_finset f],
apply linear_independent_Union_of_directed,
apply directed_of_sup,
exact (assume t₁ t₂ ht, Union_subset_Union $ assume i, Union_subset_Union_const $ assume h, ht h),
assume t, rw [set.Union, ← finset.sup_eq_supr],
refine t.induction_on _ _,
{ rw finset.sup_empty,
apply linear_independent_empty_type (not_nonempty_iff_imp_false.2 _),
exact λ x, set.not_mem_empty x (subtype.mem x) },
{ rintros ⟨i⟩ s his ih,
rw [finset.sup_insert],
apply linear_independent_union,
{ apply hl },
{ apply ih },
rw [finset.sup_eq_supr],
refine (hd i _ _ his).mono_right _,
{ simp only [(span_Union _).symm],
refine span_mono (@supr_le_supr2 (set M) _ _ _ _ _ _),
rintros ⟨i⟩, exact ⟨i, le_refl _⟩ },
{ change finite (plift.up ⁻¹' s.to_set),
exact finite_preimage (assume i j _ _, plift.up.inj) s.finite_to_set } }
end
lemma linear_independent_Union_finite {η : Type*} {ιs : η → Type*}
{f : Π j : η, ιs j → M}
(hindep : ∀j, linear_independent R (f j))
(hd : ∀i, ∀t:set η, finite t → i ∉ t →
disjoint (span R (range (f i))) (⨆i∈t, span R (range (f i)))) :
linear_independent R (λ ji : Σ j, ιs j, f ji.1 ji.2) :=
begin
by_cases zero_eq_one : (0 : R) = 1,
{ apply linear_independent_of_zero_eq_one zero_eq_one },
apply linear_independent.of_subtype_range,
{ rintros ⟨x₁, x₂⟩ ⟨y₁, y₂⟩ hxy,
by_cases h_cases : x₁ = y₁,
subst h_cases,
{ apply sigma.eq,
rw linear_independent.injective zero_eq_one (hindep _) hxy,
refl },
{ have h0 : f x₁ x₂ = 0,
{ apply disjoint_def.1 (hd x₁ {y₁} (finite_singleton y₁)
(λ h, h_cases (eq_of_mem_singleton h))) (f x₁ x₂) (subset_span (mem_range_self _)),
rw supr_singleton,
simp only [] at hxy,
rw hxy,
exact (subset_span (mem_range_self y₂)) },
exact false.elim (ne_zero_of_linear_independent zero_eq_one (hindep x₁) h0) } },
rw range_sigma_eq_Union_range,
apply linear_independent_Union_finite_subtype (λ j, (hindep j).to_subtype_range) hd,
end
end subtype
section repr
variables (hv : linear_independent R v)
/-- Canonical isomorphism between linear combinations and the span of linearly independent vectors.
-/
def linear_independent.total_equiv (hv : linear_independent R v) :
(ι →₀ R) ≃ₗ[R] span R (range v) :=
begin
apply linear_equiv.of_bijective
(linear_map.cod_restrict (span R (range v)) (finsupp.total ι M R v) _),
{ rw linear_map.ker_cod_restrict,
apply hv },
{ rw [linear_map.range, linear_map.map_cod_restrict, ← linear_map.range_le_iff_comap,
range_subtype, map_top],
rw finsupp.range_total,
apply le_refl (span R (range v)) },
{ intro l,
rw ← finsupp.range_total,
rw linear_map.mem_range,
apply mem_range_self l }
end
/-- Linear combination representing a vector in the span of linearly independent vectors.
Given a family of linearly independent vectors, we can represent any vector in their span as
a linear combination of these vectors. These are provided by this linear map.
It is simply one direction of `linear_independent.total_equiv`. -/
def linear_independent.repr (hv : linear_independent R v) :
span R (range v) →ₗ[R] ι →₀ R := hv.total_equiv.symm
lemma linear_independent.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x :=
subtype.coe_ext.1 (linear_equiv.apply_symm_apply hv.total_equiv x)
lemma linear_independent.total_comp_repr :
(finsupp.total ι M R v).comp hv.repr = submodule.subtype _ :=
linear_map.ext $ hv.total_repr
lemma linear_independent.repr_ker : hv.repr.ker = ⊥ :=
by rw [linear_independent.repr, linear_equiv.ker]
lemma linear_independent.repr_range : hv.repr.range = ⊤ :=
by rw [linear_independent.repr, linear_equiv.range]
lemma linear_independent.repr_eq
{l : ι →₀ R} {x} (eq : finsupp.total ι M R v l = ↑x) :
hv.repr x = l :=
begin
have : ↑((linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l)
= finsupp.total ι M R v l := rfl,
have : (linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l = x,
{ rw eq at this,
exact subtype.coe_ext.2 this },
rw ←linear_equiv.symm_apply_apply hv.total_equiv l,
rw ←this,
refl,
end
lemma linear_independent.repr_eq_single (i) (x) (hx : ↑x = v i) :
hv.repr x = finsupp.single i 1 :=
begin
apply hv.repr_eq,
simp [finsupp.total_single, hx]
end
-- TODO: why is this so slow?
lemma linear_independent_iff_not_smul_mem_span :
linear_independent R v ↔ (∀ (i : ι) (a : R), a • (v i) ∈ span R (v '' (univ \ {i})) → a = 0) :=
⟨ λ hv i a ha, begin
rw [finsupp.span_eq_map_total, mem_map] at ha,
rcases ha with ⟨l, hl, e⟩,
rw sub_eq_zero.1 (linear_independent_iff.1 hv (l - finsupp.single i a) (by simp [e])) at hl,
by_contra hn,
exact (not_mem_of_mem_diff (hl $ by simp [hn])) (mem_singleton _),
end, λ H, linear_independent_iff.2 $ λ l hl, begin
ext i, simp only [finsupp.zero_apply],
by_contra hn,
refine hn (H i _ _),
refine (finsupp.mem_span_iff_total _).2 ⟨finsupp.single i (l i) - l, _, _⟩,
{ rw finsupp.mem_supported',
intros j hj,
have hij : j = i :=
classical.not_not.1
(λ hij : j ≠ i, hj ((mem_diff _).2 ⟨mem_univ _, λ h, hij (eq_of_mem_singleton h)⟩)),
simp [hij] },
{ simp [hl] }
end⟩
end repr
lemma surjective_of_linear_independent_of_span
(hv : linear_independent R v) (f : ι' ↪ ι)
(hss : range v ⊆ span R (range (v ∘ f))) (zero_ne_one : 0 ≠ (1 : R)):
surjective f :=
begin
intros i,
let repr : (span R (range (v ∘ f)) : Type*) → ι' →₀ R := (hv.comp f f.inj).repr,
let l := (repr ⟨v i, hss (mem_range_self i)⟩).map_domain f,
have h_total_l : finsupp.total ι M R v l = v i,
{ dsimp only [l],
rw finsupp.total_map_domain,
rw (hv.comp f f.inj).total_repr,
{ refl },
{ exact f.inj } },
have h_total_eq : (finsupp.total ι M R v) l = (finsupp.total ι M R v) (finsupp.single i 1),
by rw [h_total_l, finsupp.total_single, one_smul],
have l_eq : l = _ := linear_map.ker_eq_bot.1 hv h_total_eq,
dsimp only [l] at l_eq,
rw ←finsupp.emb_domain_eq_map_domain at l_eq,
rcases finsupp.single_of_emb_domain_single (repr ⟨v i, _⟩) f i (1 : R) zero_ne_one.symm l_eq
with ⟨i', hi'⟩,
use i',
exact hi'.2
end
lemma eq_of_linear_independent_of_span_subtype {s t : set M} (zero_ne_one : (0 : R) ≠ 1)
(hs : linear_independent R (λ x, x : s → M)) (h : t ⊆ s) (hst : s ⊆ span R t) : s = t :=
begin
let f : t ↪ s := ⟨λ x, ⟨x.1, h x.2⟩, λ a b hab, subtype.val_injective (subtype.mk.inj hab)⟩,
have h_surj : surjective f,
{ apply surjective_of_linear_independent_of_span hs f _ zero_ne_one,
convert hst; simp [f, comp], },
show s = t,
{ apply subset.antisymm _ h,
intros x hx,
rcases h_surj ⟨x, hx⟩ with ⟨y, hy⟩,
convert y.mem,
rw ← subtype.mk.inj hy,
refl }
end
open linear_map
lemma linear_independent.image (hv : linear_independent R v) {f : M →ₗ M'}
(hf_inj : disjoint (span R (range v)) f.ker) : linear_independent R (f ∘ v) :=
begin
rw [disjoint, ← set.image_univ, finsupp.span_eq_map_total, map_inf_eq_map_inf_comap,
map_le_iff_le_comap, comap_bot, finsupp.supported_univ, top_inf_eq] at hf_inj,
unfold linear_independent at hv,
rw hv at hf_inj,
haveI : inhabited M := ⟨0⟩,
rw [linear_independent, finsupp.total_comp],
rw [@finsupp.lmap_domain_total _ _ R _ _ _ _ _ _ _ _ _ _ f, ker_comp, eq_bot_iff],
apply hf_inj,
exact λ _, rfl,
end
lemma linear_independent.image_subtype {s : set M} {f : M →ₗ M'}
(hs : linear_independent R (λ x, x : s → M))
(hf_inj : disjoint (span R s) f.ker) : linear_independent R (λ x, x : f '' s → M') :=
begin
rw [disjoint, ← set.image_id s, finsupp.span_eq_map_total, map_inf_eq_map_inf_comap,
map_le_iff_le_comap, comap_bot] at hf_inj,
haveI : inhabited M := ⟨0⟩,
rw [linear_independent_subtype_disjoint, disjoint, ← finsupp.lmap_domain_supported _ _ f, map_inf_eq_map_inf_comap,
map_le_iff_le_comap, ← ker_comp],
rw [@finsupp.lmap_domain_total _ _ R _ _ _, ker_comp],
{ exact le_trans (le_inf inf_le_left hf_inj)
(le_trans (linear_independent_subtype_disjoint.1 hs) bot_le) },
{ simp }
end
lemma linear_independent_inl_union_inr {s : set M} {t : set M'}
(hs : linear_independent R (λ x, x : s → M))
(ht : linear_independent R (λ x, x : t → M')) :
linear_independent R (λ x, x : inl R M M' '' s ∪ inr R M M' '' t → M × M') :=
begin
apply linear_independent_union,
exact (hs.image_subtype $ by simp),
exact (ht.image_subtype $ by simp),
rw [span_image, span_image];
simp [disjoint_iff, prod_inf_prod]
end
lemma linear_independent_inl_union_inr' {v : ι → M} {v' : ι' → M'}
(hv : linear_independent R v) (hv' : linear_independent R v') :
linear_independent R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) :=
begin
by_cases zero_eq_one : (0 : R) = 1,
{ apply linear_independent_of_zero_eq_one zero_eq_one },
have inj_v : injective v := (linear_independent.injective zero_eq_one hv),
have inj_v' : injective v' := (linear_independent.injective zero_eq_one hv'),
apply linear_independent.of_subtype_range,
{ apply sum.elim_injective,
{ exact injective_comp prod.injective_inl inj_v },
{ exact injective_comp prod.injective_inr inj_v' },
{ intros, simp [ne_zero_of_linear_independent zero_eq_one hv] } },
{ rw sum.elim_range,
apply linear_independent_union,
{ apply linear_independent.to_subtype_range,
apply linear_independent.image hv,
simp [ker_inl] },
{ apply linear_independent.to_subtype_range,
apply linear_independent.image hv',
simp [ker_inr] },
{ apply disjoint_inl_inr.mono _ _,
{ rw [set.range_comp, span_image],
apply linear_map.map_le_range },
{ rw [set.range_comp, span_image],
apply linear_map.map_le_range } } }
end
/-- Dedekind's linear independence of characters -/
-- See, for example, Keith Conrad's note <https://kconrad.math.uconn.edu/blurbs/galoistheory/linearchar.pdf>
theorem linear_independent_monoid_hom (G : Type*) [monoid G] (L : Type*) [integral_domain L] :
@linear_independent _ L (G → L) (λ f, f : (G →* L) → (G → L)) _ _ _ :=
by letI := classical.dec_eq (G →* L);
letI : mul_action L L := distrib_mul_action.to_mul_action L L;
-- We prove linear independence by showing that only the trivial linear combination vanishes.
exact linear_independent_iff'.2
-- To do this, we use `finset` induction,
(λ s, finset.induction_on s (λ g hg i, false.elim) $ λ a s has ih g hg,
-- Here
-- * `a` is a new character we will insert into the `finset` of characters `s`,
-- * `ih` is the fact that only the trivial linear combination of characters in `s` is zero
-- * `hg` is the fact that `g` are the coefficients of a linear combination summing to zero
-- and it remains to prove that `g` vanishes on `insert a s`.
-- We now make the key calculation:
-- For any character `i` in the original `finset`, we have `g i • i = g i • a` as functions on the monoid `G`.
have h1 : ∀ i ∈ s, (g i • i : G → L) = g i • a, from λ i his, funext $ λ x : G,
-- We prove these expressions are equal by showing
-- the differences of their values on each monoid element `x` is zero
eq_of_sub_eq_zero $ ih (λ j, g j * j x - g j * a x)
(funext $ λ y : G, calc
-- After that, it's just a chase scene.
s.sum (λ i, ((g i * i x - g i * a x) • i : G → L)) y
= s.sum (λ i, (g i * i x - g i * a x) * i y) : pi.finset_sum_apply _ _ _
... = s.sum (λ i, g i * i x * i y - g i * a x * i y) : finset.sum_congr rfl
(λ _ _, sub_mul _ _ _)
... = s.sum (λ i, g i * i x * i y) - s.sum (λ i, g i * a x * i y) : finset.sum_sub_distrib
... = (g a * a x * a y + s.sum (λ i, g i * i x * i y))
- (g a * a x * a y + s.sum (λ i, g i * a x * i y)) : by rw add_sub_add_left_eq_sub
... = (insert a s).sum (λ i, g i * i x * i y) - (insert a s).sum (λ i, g i * a x * i y) :
by rw [finset.sum_insert has, finset.sum_insert has]
... = (insert a s).sum (λ i, g i * i (x * y)) - (insert a s).sum (λ i, a x * (g i * i y)) :
congr (congr_arg has_sub.sub (finset.sum_congr rfl $ λ i _, by rw [i.map_mul, mul_assoc]))
(finset.sum_congr rfl $ λ _ _, by rw [mul_assoc, mul_left_comm])
... = (insert a s).sum (λ i, (g i • i : G → L)) (x * y)
- a x * (insert a s).sum (λ i, (g i • i : G → L)) y :
by rw [pi.finset_sum_apply, pi.finset_sum_apply, finset.mul_sum]; refl
... = 0 - a x * 0 : by rw hg; refl
... = 0 : by rw [mul_zero, sub_zero])
i
his,
-- On the other hand, since `a` is not already in `s`, for any character `i ∈ s`
-- there is some element of the monoid on which it differs from `a`.
have h2 : ∀ i : G →* L, i ∈ s → ∃ y, i y ≠ a y, from λ i his,
classical.by_contradiction $ λ h,
have hia : i = a, from monoid_hom.ext $ λ y, classical.by_contradiction $ λ hy, h ⟨y, hy⟩,
has $ hia ▸ his,
-- From these two facts we deduce that `g` actually vanishes on `s`,
have h3 : ∀ i ∈ s, g i = 0, from λ i his, let ⟨y, hy⟩ := h2 i his in
have h : g i • i y = g i • a y, from congr_fun (h1 i his) y,
or.resolve_right (mul_eq_zero.1 $ by rw [mul_sub, sub_eq_zero]; exact h) (sub_ne_zero_of_ne hy),
-- And so, using the fact that the linear combination over `s` and over `insert a s` both vanish,
-- we deduce that `g a = 0`.
have h4 : g a = 0, from calc
g a = g a * 1 : (mul_one _).symm
... = (g a • a : G → L) 1 : by rw ← a.map_one; refl
... = (insert a s).sum (λ i, (g i • i : G → L)) 1 : begin
rw finset.sum_eq_single a,
{ intros i his hia, rw finset.mem_insert at his, rw [h3 i (his.resolve_left hia), zero_smul] },
{ intros haas, exfalso, apply haas, exact finset.mem_insert_self a s }
end
... = 0 : by rw hg; refl,
-- Now we're done; the last two facts together imply that `g` vanishes on every element of `insert a s`.
(finset.forall_mem_insert _ _ _).2 ⟨h4, h3⟩)
lemma le_of_span_le_span {s t u: set M} (zero_ne_one : (0 : R) ≠ 1)
(hl : linear_independent R (subtype.val : u → M )) (hsu : s ⊆ u) (htu : t ⊆ u)
(hst : span R s ≤ span R t) : s ⊆ t :=
begin
have := eq_of_linear_independent_of_span_subtype zero_ne_one
(hl.mono (set.union_subset hsu htu))
(set.subset_union_right _ _)
(set.union_subset (set.subset.trans subset_span hst) subset_span),
rw ← this, apply set.subset_union_left
end
lemma span_le_span_iff {s t u: set M} (zero_ne_one : (0 : R) ≠ 1)
(hl : linear_independent R (subtype.val : u → M )) (hsu : s ⊆ u) (htu : t ⊆ u) :
span R s ≤ span R t ↔ s ⊆ t :=
⟨le_of_span_le_span zero_ne_one hl hsu htu, span_mono⟩
variables (R) (v)
/-- A family of vectors is a basis if it is linearly independent and all vectors are in the span. -/
def is_basis := linear_independent R v ∧ span R (range v) = ⊤
variables {R} {v}
section is_basis
variables {s t : set M} (hv : is_basis R v)
lemma is_basis.mem_span (hv : is_basis R v) : ∀ x, x ∈ span R (range v) := eq_top_iff'.1 hv.2
lemma is_basis.comp (hv : is_basis R v) (f : ι' → ι) (hf : bijective f) :
is_basis R (v ∘ f) :=
begin
split,
{ apply hv.1.comp f hf.1 },
{ rw[set.range_comp, range_iff_surjective.2 hf.2, image_univ, hv.2] }
end
lemma is_basis.injective (hv : is_basis R v) (zero_ne_one : (0 : R) ≠ 1) : injective v :=
λ x y h, linear_independent.injective zero_ne_one hv.1 h
/-- Given a basis, any vector can be written as a linear combination of the basis vectors. They are
given by this linear map. This is one direction of `module_equiv_finsupp`. -/
def is_basis.repr : M →ₗ (ι →₀ R) :=
(hv.1.repr).comp (linear_map.id.cod_restrict _ hv.mem_span)
lemma is_basis.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x :=
hv.1.total_repr ⟨x, _⟩
lemma is_basis.total_comp_repr : (finsupp.total ι M R v).comp hv.repr = linear_map.id :=
linear_map.ext hv.total_repr
lemma is_basis.repr_ker : hv.repr.ker = ⊥ :=
linear_map.ker_eq_bot.2 $ injective_of_left_inverse hv.total_repr
lemma is_basis.repr_range : hv.repr.range = finsupp.supported R R univ :=
by rw [is_basis.repr, linear_map.range, submodule.map_comp,
linear_map.map_cod_restrict, submodule.map_id, comap_top, map_top, hv.1.repr_range,
finsupp.supported_univ]
lemma is_basis.repr_total (x : ι →₀ R) (hx : x ∈ finsupp.supported R R (univ : set ι)) :
hv.repr (finsupp.total ι M R v x) = x :=
begin
rw [← hv.repr_range, linear_map.mem_range] at hx,
cases hx with w hw,
rw [← hw, hv.total_repr],
end
lemma is_basis.repr_eq_single {i} : hv.repr (v i) = finsupp.single i 1 :=
by apply hv.1.repr_eq_single; simp
/-- Construct a linear map given the value at the basis. -/
def is_basis.constr (f : ι → M') : M →ₗ[R] M' :=
(finsupp.total M' M' R id).comp $ (finsupp.lmap_domain R R f).comp hv.repr
theorem is_basis.constr_apply (f : ι → M') (x : M) :
(hv.constr f : M → M') x = (hv.repr x).sum (λb a, a • f b) :=
by dsimp [is_basis.constr];
rw [finsupp.total_apply, finsupp.sum_map_domain_index]; simp [add_smul]
lemma is_basis.ext {f g : M →ₗ[R] M'} (hv : is_basis R v) (h : ∀i, f (v i) = g (v i)) : f = g :=
begin
apply linear_map.ext (λ x, linear_eq_on (range v) _ (hv.mem_span x)),
exact (λ y hy, exists.elim (set.mem_range.1 hy) (λ i hi, by rw ←hi; exact h i))
end
lemma constr_basis {f : ι → M'} {i : ι} (hv : is_basis R v) :
(hv.constr f : M → M') (v i) = f i :=
by simp [is_basis.constr_apply, hv.repr_eq_single, finsupp.sum_single_index]
lemma constr_eq {g : ι → M'} {f : M →ₗ[R] M'} (hv : is_basis R v)
(h : ∀i, g i = f (v i)) : hv.constr g = f :=
hv.ext $ λ i, (constr_basis hv).trans (h i)
lemma constr_self (f : M →ₗ[R] M') : hv.constr (λ i, f (v i)) = f :=
constr_eq hv $ λ x, rfl
lemma constr_zero (hv : is_basis R v) : hv.constr (λi, (0 : M')) = 0 :=
constr_eq hv $ λ x, rfl
lemma constr_add {g f : ι → M'} (hv : is_basis R v) :
hv.constr (λi, f i + g i) = hv.constr f + hv.constr g :=
constr_eq hv $ by simp [constr_basis hv] {contextual := tt}
lemma constr_neg {f : ι → M'} (hv : is_basis R v) : hv.constr (λi, - f i) = - hv.constr f :=
constr_eq hv $ by simp [constr_basis hv] {contextual := tt}
lemma constr_sub {g f : ι → M'} (hs : is_basis R v) :
hv.constr (λi, f i - g i) = hs.constr f - hs.constr g :=
by simp [sub_eq_add_neg, constr_add, constr_neg]
-- this only works on functions if `R` is a commutative ring
lemma constr_smul {ι R M} [comm_ring R] [add_comm_group M] [module R M]
{v : ι → R} {f : ι → M} {a : R} (hv : is_basis R v) :
hv.constr (λb, a • f b) = a • hv.constr f :=
constr_eq hv $ by simp [constr_basis hv] {contextual := tt}
lemma constr_range [nonempty ι] (hv : is_basis R v) {f : ι → M'} :
(hv.constr f).range = span R (range f) :=
by rw [is_basis.constr, linear_map.range_comp, linear_map.range_comp, is_basis.repr_range,
finsupp.lmap_domain_supported, ←set.image_univ, ←finsupp.span_eq_map_total, image_id]
/-- Canonical equivalence between a module and the linear combinations of basis vectors. -/
def module_equiv_finsupp (hv : is_basis R v) : M ≃ₗ[R] ι →₀ R :=
(hv.1.total_equiv.trans (linear_equiv.of_top _ hv.2)).symm
/-- Isomorphism between the two modules, given two modules `M` and `M'` with respective bases
`v` and `v'` and a bijection between the two bases. -/
def equiv_of_is_basis {v : ι → M} {v' : ι' → M'} {f : M → M'} {g : M' → M}
(hv : is_basis R v) (hv' : is_basis R v')
(hf : ∀i, f (v i) ∈ range v') (hg : ∀i, g (v' i) ∈ range v)
(hgf : ∀i, g (f (v i)) = v i) (hfg : ∀i, f (g (v' i)) = v' i) :
M ≃ₗ M' :=
{ inv_fun := hv'.constr (g ∘ v'),
left_inv :=
have (hv'.constr (g ∘ v')).comp (hv.constr (f ∘ v)) = linear_map.id,
from hv.ext $ λ i, exists.elim (hf i)
(λ i' hi', by simp [constr_basis, hi'.symm]; rw [hi', hgf]),
λ x, congr_arg (λ h:M →ₗ[R] M, h x) this,
right_inv :=
have (hv.constr (f ∘ v)).comp (hv'.constr (g ∘ v')) = linear_map.id,
from hv'.ext $ λ i', exists.elim (hg i')
(λ i hi, by simp [constr_basis, hi.symm]; rw [hi, hfg]),
λ y, congr_arg (λ h:M' →ₗ[R] M', h y) this,
..hv.constr (f ∘ v) }
lemma is_basis_inl_union_inr {v : ι → M} {v' : ι' → M'}
(hv : is_basis R v) (hv' : is_basis R v') :
is_basis R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) :=
begin
split,
apply linear_independent_inl_union_inr' hv.1 hv'.1,
rw [sum.elim_range, span_union,
set.range_comp, span_image (inl R M M'), hv.2, map_top,
set.range_comp, span_image (inr R M M'), hv'.2, map_top],
exact linear_map.sup_range_inl_inr
end
end is_basis
lemma is_basis_singleton_one (R : Type*) [unique ι] [ring R] :
is_basis R (λ (_ : ι), (1 : R)) :=
begin
split,
{ refine linear_independent_iff.2 (λ l, _),
rw [finsupp.unique_single l, finsupp.total_single, smul_eq_mul, mul_one],
intro hi,
simp [hi] },
{ refine top_unique (λ _ _, _),
simp [submodule.mem_span_singleton] }
end
protected lemma linear_equiv.is_basis (hs : is_basis R v)
(f : M ≃ₗ[R] M') : is_basis R (f ∘ v) :=
begin
split,
{ apply @linear_independent.image _ _ _ _ _ _ _ _ _ _ hs.1 (f : M →ₗ[R] M'),
simp [linear_equiv.ker f] },
{ rw set.range_comp,
have : span R ((f : M →ₗ[R] M') '' range v) = ⊤,
{ rw [span_image (f : M →ₗ[R] M'), hs.2],
simp },
exact this }
end
lemma is_basis_span (hs : linear_independent R v) :
@is_basis ι R (span R (range v)) (λ i : ι, ⟨v i, subset_span (mem_range_self _)⟩) _ _ _ :=
begin
split,
{ apply linear_independent_span hs },
{ rw eq_top_iff',
intro x,
have h₁ : subtype.val '' set.range (λ i, subtype.mk (v i) _) = range v,
by rw ←set.range_comp,
have h₂ : map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _)))
= span R (range v),
by rw [←span_image, submodule.subtype_eq_val, h₁],
have h₃ : (x : M) ∈ map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _))),
by rw h₂; apply subtype.mem x,
rcases mem_map.1 h₃ with ⟨y, hy₁, hy₂⟩,
have h_x_eq_y : x = y,
by rw [subtype.coe_ext, ← hy₂]; simp,
rw h_x_eq_y,
exact hy₁ }
end
lemma is_basis_empty (h_empty : ¬ nonempty ι) (h : ∀x:M, x = 0) : is_basis R (λ x : ι, (0 : M)) :=
⟨ linear_independent_empty_type h_empty,
eq_top_iff'.2 $ assume x, (h x).symm ▸ submodule.zero_mem _ ⟩
lemma is_basis_empty_bot (h_empty : ¬ nonempty ι) :
is_basis R (λ _ : ι, (0 : (⊥ : submodule R M))) :=
begin
apply is_basis_empty h_empty,
intro x,
apply subtype.ext.2,
exact (submodule.mem_bot R).1 (subtype.mem x),
end
open fintype
variables [fintype ι] (h : is_basis R v)
/-- A module over `R` with a finite basis is linearly equivalent to functions from its basis to `R`.
-/
def equiv_fun_basis : M ≃ₗ[R] (ι → R) :=
linear_equiv.trans (module_equiv_finsupp h)
{ to_fun := finsupp.to_fun,
add := λ x y, by ext; exact finsupp.add_apply,
smul := λ x y, by ext; exact finsupp.smul_apply,
..finsupp.equiv_fun_on_fintype }
theorem module.card_fintype [fintype R] [fintype M] :
card M = (card R) ^ (card ι) :=
calc card M = card (ι → R) : card_congr (equiv_fun_basis h).to_equiv
... = card R ^ card ι : card_fun
/-- Given a basis `v` indexed by `ι`, the canonical linear equivalence between `ι → R` and `M` maps
a function `x : ι → R` to the linear combination `∑_i x i • v i`. -/
@[simp] lemma equiv_fun_basis_symm_apply (x : ι → R) :
(equiv_fun_basis h).symm x = finset.sum finset.univ (λi, x i • v i) :=
begin
change finsupp.sum
((finsupp.equiv_fun_on_fintype.symm : (ι → R) ≃ (ι →₀ R)) x) (λ (i : ι) (a : R), a • v i)
= finset.sum finset.univ (λi, x i • v i),
dsimp [finsupp.equiv_fun_on_fintype, finsupp.sum],
rw finset.sum_filter,
refine finset.sum_congr rfl (λi hi, _),
by_cases H : x i = 0,
{ simp [H] },
{ simp [H], refl }
end
end module
section vector_space
variables
{v : ι → V}
[field K] [add_comm_group V] [add_comm_group V']
[vector_space K V] [vector_space K V']
{s t : set V} {x y z : V}
include K
open submodule
/- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class
(instead of a data containing type class) -/
section
set_option class.instance_max_depth 36
lemma mem_span_insert_exchange : x ∈ span K (insert y s) → x ∉ span K s → y ∈ span K (insert x s) :=
begin
simp [mem_span_insert],
rintro a z hz rfl h,
refine ⟨a⁻¹, -a⁻¹ • z, smul_mem _ _ hz, _⟩,
have a0 : a ≠ 0, {rintro rfl, simp * at *},
simp [a0, smul_add, smul_smul]
end
end
lemma linear_independent_iff_not_mem_span :
linear_independent K v ↔ (∀i, v i ∉ span K (v '' (univ \ {i}))) :=
begin
apply linear_independent_iff_not_smul_mem_span.trans,
split,
{ intros h i h_in_span,
apply one_ne_zero (h i 1 (by simp [h_in_span])) },
{ intros h i a ha,
by_contradiction ha',
exact false.elim (h _ ((smul_mem_iff _ ha').1 ha)) }
end
lemma linear_independent_unique [unique ι] (h : v (default ι) ≠ 0): linear_independent K v :=
begin
rw linear_independent_iff,
intros l hl,
ext i,
rw [unique.eq_default i, finsupp.zero_apply],
by_contra hc,
have := smul_smul (l (default ι))⁻¹ (l (default ι)) (v (default ι)),
rw [finsupp.unique_single l, finsupp.total_single] at hl,
rw [hl, inv_mul_cancel hc, smul_zero, one_smul] at this,
exact h this.symm
end
lemma linear_independent_singleton {x : V} (hx : x ≠ 0) :
linear_independent K (λ x, x : ({x} : set V) → V) :=
begin
apply @linear_independent_unique _ _ _ _ _ _ _ _ _,
apply set.unique_singleton,
apply hx,
end
lemma disjoint_span_singleton {p : submodule K V} {x : V} (x0 : x ≠ 0) :
disjoint p (span K {x}) ↔ x ∉ p :=
⟨λ H xp, x0 (disjoint_def.1 H _ xp (singleton_subset_iff.1 subset_span:_)),
begin
simp [disjoint_def, mem_span_singleton],
rintro xp y yp a rfl,
by_cases a0 : a = 0, {simp [a0]},
exact xp.elim ((smul_mem_iff p a0).1 yp),
end⟩
lemma linear_independent.insert (hs : linear_independent K (λ b, b : s → V)) (hx : x ∉ span K s) :
linear_independent K (λ b, b : insert x s → V) :=
begin
rw ← union_singleton,
have x0 : x ≠ 0 := mt (by rintro rfl; apply zero_mem _) hx,
apply linear_independent_union hs (linear_independent_singleton x0),
rwa [disjoint_span_singleton x0]
end
lemma exists_linear_independent (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) :
∃b⊆t, s ⊆ b ∧ t ⊆ span K b ∧ linear_independent K (λ x, x : b → V) :=
begin
rcases zorn.zorn_subset₀ {b | b ⊆ t ∧ linear_independent K (λ x, x : b → V)} _ _
⟨hst, hs⟩ with ⟨b, ⟨bt, bi⟩, sb, h⟩,
{ refine ⟨b, bt, sb, λ x xt, _, bi⟩,
haveI := classical.dec (x ∈ span K b),
by_contra hn,
apply hn,
rw ← h _ ⟨insert_subset.2 ⟨xt, bt⟩, bi.insert hn⟩ (subset_insert _ _),
exact subset_span (mem_insert _ _) },
{ refine λ c hc cc c0, ⟨⋃₀ c, ⟨_, _⟩, λ x, _⟩,
{ exact sUnion_subset (λ x xc, (hc xc).1) },
{ exact linear_independent_sUnion_of_directed cc.directed_on (λ x xc, (hc xc).2) },
{ exact subset_sUnion_of_mem } }
end
lemma exists_subset_is_basis (hs : linear_independent K (λ x, x : s → V)) :
∃b, s ⊆ b ∧ is_basis K (λ i : b, i.val) :=
let ⟨b, hb₀, hx, hb₂, hb₃⟩ := exists_linear_independent hs (@subset_univ _ _) in
⟨ b, hx,
@linear_independent.restrict_of_comp_subtype _ _ _ id _ _ _ _ hb₃,
by simp; exact eq_top_iff.2 hb₂⟩
variables (K V)
lemma exists_is_basis : ∃b : set V, is_basis K (λ i : b, i.val) :=
let ⟨b, _, hb⟩ := exists_subset_is_basis linear_independent_empty in ⟨b, hb⟩
variables {K V}
-- TODO(Mario): rewrite?
lemma exists_of_linear_independent_of_finite_span {t : finset V}
(hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ (span K ↑t : submodule K V)) :
∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = t.card :=
have ∀t, ∀(s' : finset V), ↑s' ⊆ s → s ∩ ↑t = ∅ → s ⊆ (span K ↑(s' ∪ t) : submodule K V) →
∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = (s' ∪ t).card :=
assume t, finset.induction_on t
(assume s' hs' _ hss',
have s = ↑s',
from eq_of_linear_independent_of_span_subtype (@zero_ne_one K _) hs hs' $
by simpa using hss',
⟨s', by simp [this]⟩)
(assume b₁ t hb₁t ih s' hs' hst hss',
have hb₁s : b₁ ∉ s,
from assume h,
have b₁ ∈ s ∩ ↑(insert b₁ t), from ⟨h, finset.mem_insert_self _ _⟩,
by rwa [hst] at this,
have hb₁s' : b₁ ∉ s', from assume h, hb₁s $ hs' h,
have hst : s ∩ ↑t = ∅,
from eq_empty_of_subset_empty $ subset.trans
(by simp [inter_subset_inter, subset.refl]) (le_of_eq hst),
classical.by_cases
(assume : s ⊆ (span K ↑(s' ∪ t) : submodule K V),
let ⟨u, hust, hsu, eq⟩ := ih _ hs' hst this in
have hb₁u : b₁ ∉ u, from assume h, (hust h).elim hb₁s hb₁t,
⟨insert b₁ u, by simp [insert_subset_insert hust],
subset.trans hsu (by simp), by simp [eq, hb₁t, hb₁s', hb₁u]⟩)
(assume : ¬ s ⊆ (span K ↑(s' ∪ t) : submodule K V),
let ⟨b₂, hb₂s, hb₂t⟩ := not_subset.mp this in
have hb₂t' : b₂ ∉ s' ∪ t, from assume h, hb₂t $ subset_span h,
have s ⊆ (span K ↑(insert b₂ s' ∪ t) : submodule K V), from
assume b₃ hb₃,
have ↑(s' ∪ insert b₁ t) ⊆ insert b₁ (insert b₂ ↑(s' ∪ t) : set V),
by simp [insert_eq, -singleton_union, -union_singleton, union_subset_union, subset.refl, subset_union_right],
have hb₃ : b₃ ∈ span K (insert b₁ (insert b₂ ↑(s' ∪ t) : set V)),
from span_mono this (hss' hb₃),
have s ⊆ (span K (insert b₁ ↑(s' ∪ t)) : submodule K V),
by simpa [insert_eq, -singleton_union, -union_singleton] using hss',
have hb₁ : b₁ ∈ span K (insert b₂ ↑(s' ∪ t)),
from mem_span_insert_exchange (this hb₂s) hb₂t,
by rw [span_insert_eq_span hb₁] at hb₃; simpa using hb₃,
let ⟨u, hust, hsu, eq⟩ := ih _ (by simp [insert_subset, hb₂s, hs']) hst this in
⟨u, subset.trans hust $ union_subset_union (subset.refl _) (by simp [subset_insert]),
hsu, by simp [eq, hb₂t', hb₁t, hb₁s']⟩)),
begin
letI := classical.dec_pred (λx, x ∈ s),
have eq : t.filter (λx, x ∈ s) ∪ t.filter (λx, x ∉ s) = t,
{ apply finset.ext.mpr,
intro x,
by_cases x ∈ s; simp *, finish },
apply exists.elim (this (t.filter (λx, x ∉ s)) (t.filter (λx, x ∈ s))
(by simp [set.subset_def]) (by simp [set.ext_iff] {contextual := tt}) (by rwa [eq])),
intros u h,
exact ⟨u, subset.trans h.1 (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}),
h.2.1, by simp only [h.2.2, eq]⟩
end
lemma exists_finite_card_le_of_finite_of_linear_independent_of_span
(ht : finite t) (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ span K t) :
∃h : finite s, h.to_finset.card ≤ ht.to_finset.card :=
have s ⊆ (span K ↑(ht.to_finset) : submodule K V), by simp; assumption,
let ⟨u, hust, hsu, eq⟩ := exists_of_linear_independent_of_finite_span hs this in
have finite s, from finite_subset u.finite_to_set hsu,
⟨this, by rw [←eq]; exact (finset.card_le_of_subset $ finset.coe_subset.mp $ by simp [hsu])⟩
lemma exists_left_inverse_linear_map_of_injective {f : V →ₗ[K] V'}
(hf_inj : f.ker = ⊥) : ∃g:V' →ₗ V, g.comp f = linear_map.id :=
begin
rcases exists_is_basis K V with ⟨B, hB⟩,
have hB₀ : _ := hB.1.to_subtype_range,
have : linear_independent K (λ x, x : f '' B → V'),
{ have h₁ := hB₀.image_subtype
(show disjoint (span K (range (λ i : B, i.val))) (linear_map.ker f), by simp [hf_inj]),
have h₂ : range (λ (i : B), i.val) = B := subtype.range_val B,
rwa h₂ at h₁ },
rcases exists_subset_is_basis this with ⟨C, BC, hC⟩,
haveI : inhabited V := ⟨0⟩,
use hC.constr (C.restrict (inv_fun f)),
apply @is_basis.ext _ _ _ _ _ _ _ _ _ _ _ _ hB,
intros b,
rw image_subset_iff at BC,
simp,
have := BC (subtype.mem b),
rw mem_preimage at this,
have : f (b.val) = (subtype.mk (f ↑b) (begin rw ←mem_preimage, exact BC (subtype.mem b) end) : C).val,
by simp; unfold_coes,
rw this,
rw [constr_basis hC],
exact left_inverse_inv_fun (linear_map.ker_eq_bot.1 hf_inj) _,
end
lemma exists_right_inverse_linear_map_of_surjective {f : V →ₗ[K] V'}
(hf_surj : f.range = ⊤) : ∃g:V' →ₗ V, f.comp g = linear_map.id :=
begin
rcases exists_is_basis K V' with ⟨C, hC⟩,
haveI : inhabited V := ⟨0⟩,
use hC.constr (C.restrict (inv_fun f)),
apply @is_basis.ext _ _ _ _ _ _ _ _ _ _ _ _ hC,
intros c,
simp [constr_basis hC],
exact right_inverse_inv_fun (linear_map.range_eq_top.1 hf_surj) _
end
set_option class.instance_max_depth 49
open submodule linear_map
theorem quotient_prod_linear_equiv (p : submodule K V) :
nonempty ((p.quotient × p) ≃ₗ[K] V) :=
begin
haveI := classical.dec_eq (quotient p),
rcases exists_right_inverse_linear_map_of_surjective p.range_mkq with ⟨f, hf⟩,
have mkf : ∀ x, submodule.quotient.mk (f x) = x := linear_map.ext_iff.1 hf,
have fp : ∀ x, x - f (p.mkq x) ∈ p :=
λ x, (submodule.quotient.eq p).1 (mkf (p.mkq x)).symm,
refine ⟨linear_equiv.of_linear (f.coprod p.subtype)
(p.mkq.prod (cod_restrict p (linear_map.id - f.comp p.mkq) fp))
(by ext; simp) _⟩,
ext ⟨⟨x⟩, y, hy⟩; simp,
{ apply (submodule.quotient.eq p).2,
simpa [sub_eq_add_neg, add_left_comm] using sub_mem p hy (fp x) },
{ refine subtype.coe_ext.2 _,
simp [mkf, (submodule.quotient.mk_eq_zero p).2 hy] }
end
open fintype
theorem vector_space.card_fintype [fintype K] [fintype V] :
∃ n : ℕ, card V = (card K) ^ n :=
begin
apply exists.elim (exists_is_basis K V),
intros b hb,
haveI := classical.dec_pred (λ x, x ∈ b),
use card b,
exact module.card_fintype hb,
end
end vector_space
namespace pi
open set linear_map
section module
variables {η : Type*} {ιs : η → Type*} {Ms : η → Type*}
variables [ring R] [∀i, add_comm_group (Ms i)] [∀i, module R (Ms i)]
lemma linear_independent_std_basis
(v : Πj, ιs j → (Ms j)) (hs : ∀i, linear_independent R (v i)) :
linear_independent R (λ (ji : Σ j, ιs j), std_basis R Ms ji.1 (v ji.1 ji.2)) :=
begin
have hs' : ∀j : η, linear_independent R (λ i : ιs j, std_basis R Ms j (v j i)),
{ intro j,
apply linear_independent.image (hs j),
simp [ker_std_basis] },
apply linear_independent_Union_finite hs',
{ assume j J _ hiJ,
simp [(set.Union.equations._eqn_1 _).symm, submodule.span_image, submodule.span_Union],
have h₀ : ∀ j, span R (range (λ (i : ιs j), std_basis R Ms j (v j i)))
≤ range (std_basis R Ms j),
{ intro j,
rw [span_le, linear_map.range_coe],
apply range_comp_subset_range },
have h₁ : span R (range (λ (i : ιs j), std_basis R Ms j (v j i)))
≤ ⨆ i ∈ {j}, range (std_basis R Ms i),
{ rw @supr_singleton _ _ _ (λ i, linear_map.range (std_basis R (λ (j : η), Ms j) i)),
apply h₀ },
have h₂ : (⨆ j ∈ J, span R (range (λ (i : ιs j), std_basis R Ms j (v j i)))) ≤
⨆ j ∈ J, range (std_basis R (λ (j : η), Ms j) j) :=
supr_le_supr (λ i, supr_le_supr (λ H, h₀ i)),
have h₃ : disjoint (λ (i : η), i ∈ {j}) J,
{ convert set.disjoint_singleton_left.2 hiJ,
rw ←@set_of_mem_eq _ {j},
refl },
exact (disjoint_std_basis_std_basis _ _ _ _ h₃).mono h₁ h₂ }
end
variable [fintype η]
lemma is_basis_std_basis (s : Πj, ιs j → (Ms j)) (hs : ∀j, is_basis R (s j)) :
is_basis R (λ (ji : Σ j, ιs j), std_basis R Ms ji.1 (s ji.1 ji.2)) :=
begin
split,
{ apply linear_independent_std_basis _ (assume i, (hs i).1) },
have h₁ : Union (λ j, set.range (std_basis R Ms j ∘ s j))
⊆ range (λ (ji : Σ (j : η), ιs j), (std_basis R Ms (ji.fst)) (s (ji.fst) (ji.snd))),
{ apply Union_subset, intro i,
apply range_comp_subset_range (λ x : ιs i, (⟨i, x⟩ : Σ (j : η), ιs j))
(λ (ji : Σ (j : η), ιs j), std_basis R Ms (ji.fst) (s (ji.fst) (ji.snd))) },
have h₂ : ∀ i, span R (range (std_basis R Ms i ∘ s i)) = range (std_basis R Ms i),
{ intro i,
rw [set.range_comp, submodule.span_image, (assume i, (hs i).2), submodule.map_top] },
apply eq_top_mono,
apply span_mono h₁,
rw span_Union,
simp only [h₂],
apply supr_range_std_basis
end
section
variables (R η)
lemma is_basis_fun₀ : is_basis R
(λ (ji : Σ (j : η), (λ _, unit) j),
(std_basis R (λ (i : η), R) (ji.fst)) 1) :=
begin
haveI := classical.dec_eq,
apply @is_basis_std_basis R η (λi:η, unit) (λi:η, R) _ _ _ _ (λ _ _, (1 : R))
(assume i, @is_basis_singleton_one _ _ _ _),
end
lemma is_basis_fun : is_basis R (λ i, std_basis R (λi:η, R) i 1) :=
begin
apply is_basis.comp (is_basis_fun₀ R η) (λ i, ⟨i, punit.star⟩),
apply bijective_iff_has_inverse.2,
use (λ x, x.1),
simp [function.left_inverse, function.right_inverse],
intros _ b,
rw [unique.eq_default b, unique.eq_default punit.star]
end
end
end module
end pi
|
4c2a1a463e5d2998245c76ee5e711b8f0e97fc12 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/topology/sheaves/stalks.lean | c502ec448242e5bea01f40a82f2944e8e8285b56 | [
"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 | 25,865 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import topology.category.Top.open_nhds
import topology.sheaves.presheaf
import topology.sheaves.sheaf_condition.unique_gluing
import category_theory.adjunction.evaluation
import category_theory.limits.types
import category_theory.limits.preserves.filtered
import category_theory.limits.final
import tactic.elementwise
import algebra.category.Ring.colimits
/-!
# Stalks
For a presheaf `F` on a topological space `X`, valued in some category `C`, the *stalk* of `F`
at the point `x : X` is defined as the colimit of the composition of the inclusion of categories
`(nhds x)ᵒᵖ ⥤ (opens X)ᵒᵖ` and the functor `F : (opens X)ᵒᵖ ⥤ C`.
For an open neighborhood `U` of `x`, we define the map `F.germ x : F.obj (op U) ⟶ F.stalk x` as the
canonical morphism into this colimit.
Taking stalks is functorial: For every point `x : X` we define a functor `stalk_functor C x`,
sending presheaves on `X` to objects of `C`. Furthermore, for a map `f : X ⟶ Y` between
topological spaces, we define `stalk_pushforward` as the induced map on the stalks
`(f _* ℱ).stalk (f x) ⟶ ℱ.stalk x`.
Some lemmas about stalks and germs only hold for certain classes of concrete categories. A basic
property of forgetful functors of categories of algebraic structures (like `Mon`, `CommRing`,...)
is that they preserve filtered colimits. Since stalks are filtered colimits, this ensures that
the stalks of presheaves valued in these categories behave exactly as for `Type`-valued presheaves.
For example, in `germ_exist` we prove that in such a category, every element of the stalk is the
germ of a section.
Furthermore, if we require the forgetful functor to reflect isomorphisms and preserve limits (as
is the case for most algebraic structures), we have access to the unique gluing API and can prove
further properties. Most notably, in `is_iso_iff_stalk_functor_map_iso`, we prove that in such
a category, a morphism of sheaves is an isomorphism if and only if all of its stalk maps are
isomorphisms.
See also the definition of "algebraic structures" in the stacks project:
https://stacks.math.columbia.edu/tag/007L
-/
noncomputable theory
universes v u v' u'
open category_theory
open Top
open category_theory.limits
open topological_space
open opposite
variables {C : Type u} [category.{v} C]
variables [has_colimits.{v} C]
variables {X Y Z : Top.{v}}
namespace Top.presheaf
variables (C)
/-- Stalks are functorial with respect to morphisms of presheaves over a fixed `X`. -/
def stalk_functor (x : X) : X.presheaf C ⥤ C :=
((whiskering_left _ _ C).obj (open_nhds.inclusion x).op) ⋙ colim
variables {C}
/--
The stalk of a presheaf `F` at a point `x` is calculated as the colimit of the functor
nbhds x ⥤ opens F.X ⥤ C
-/
def stalk (ℱ : X.presheaf C) (x : X) : C :=
(stalk_functor C x).obj ℱ -- -- colimit ((open_nhds.inclusion x).op ⋙ ℱ)
@[simp] lemma stalk_functor_obj (ℱ : X.presheaf C) (x : X) :
(stalk_functor C x).obj ℱ = ℱ.stalk x := rfl
/--
The germ of a section of a presheaf over an open at a point of that open.
-/
def germ (F : X.presheaf C) {U : opens X} (x : U) : F.obj (op U) ⟶ stalk F x :=
colimit.ι ((open_nhds.inclusion x.1).op ⋙ F) (op ⟨U, x.2⟩)
@[simp, elementwise]
lemma germ_res (F : X.presheaf C) {U V : opens X} (i : U ⟶ V) (x : U) :
F.map i.op ≫ germ F x = germ F (i x : V) :=
let i' : (⟨U, x.2⟩ : open_nhds x.1) ⟶ ⟨V, (i x : V).2⟩ := i in
colimit.w ((open_nhds.inclusion x.1).op ⋙ F) i'.op
/--
A morphism from the stalk of `F` at `x` to some object `Y` is completely determined by its
composition with the `germ` morphisms.
-/
lemma stalk_hom_ext (F : X.presheaf C) {x} {Y : C} {f₁ f₂ : F.stalk x ⟶ Y}
(ih : ∀ (U : opens X) (hxU : x ∈ U), F.germ ⟨x, hxU⟩ ≫ f₁ = F.germ ⟨x, hxU⟩ ≫ f₂) : f₁ = f₂ :=
colimit.hom_ext $ λ U, by { induction U using opposite.rec, cases U with U hxU, exact ih U hxU }
@[simp, reassoc, elementwise]
lemma stalk_functor_map_germ {F G : X.presheaf C} (U : opens X) (x : U)
(f : F ⟶ G) : germ F x ≫ (stalk_functor C x.1).map f = f.app (op U) ≫ germ G x :=
colimit.ι_map (whisker_left ((open_nhds.inclusion x.1).op) f) (op ⟨U, x.2⟩)
variables (C)
/--
For a presheaf `F` on a space `X`, a continuous map `f : X ⟶ Y` induces a morphisms between the
stalk of `f _ * F` at `f x` and the stalk of `F` at `x`.
-/
def stalk_pushforward (f : X ⟶ Y) (F : X.presheaf C) (x : X) : (f _* F).stalk (f x) ⟶ F.stalk x :=
begin
-- This is a hack; Lean doesn't like to elaborate the term written directly.
transitivity,
swap,
exact colimit.pre _ (open_nhds.map f x).op,
exact colim.map (whisker_right (nat_trans.op (open_nhds.inclusion_map_iso f x).inv) F),
end
@[simp, elementwise, reassoc]
lemma stalk_pushforward_germ (f : X ⟶ Y) (F : X.presheaf C) (U : opens Y)
(x : (opens.map f).obj U) :
(f _* F).germ ⟨f x, x.2⟩ ≫ F.stalk_pushforward C f x = F.germ x :=
begin
rw [stalk_pushforward, germ, colimit.ι_map_assoc, colimit.ι_pre, whisker_right_app],
erw [category_theory.functor.map_id, category.id_comp],
refl,
end
-- Here are two other potential solutions, suggested by @fpvandoorn at
-- <https://github.com/leanprover-community/mathlib/pull/1018#discussion_r283978240>
-- However, I can't get the subsequent two proofs to work with either one.
-- def stalk_pushforward (f : X ⟶ Y) (ℱ : X.presheaf C) (x : X) :
-- (f _* ℱ).stalk (f x) ⟶ ℱ.stalk x :=
-- colim.map ((functor.associator _ _ _).inv ≫
-- whisker_right (nat_trans.op (open_nhds.inclusion_map_iso f x).inv) ℱ) ≫
-- colimit.pre ((open_nhds.inclusion x).op ⋙ ℱ) (open_nhds.map f x).op
-- def stalk_pushforward (f : X ⟶ Y) (ℱ : X.presheaf C) (x : X) :
-- (f _* ℱ).stalk (f x) ⟶ ℱ.stalk x :=
-- (colim.map (whisker_right (nat_trans.op (open_nhds.inclusion_map_iso f x).inv) ℱ) :
-- colim.obj ((open_nhds.inclusion (f x) ⋙ opens.map f).op ⋙ ℱ) ⟶ _) ≫
-- colimit.pre ((open_nhds.inclusion x).op ⋙ ℱ) (open_nhds.map f x).op
namespace stalk_pushforward
local attribute [tidy] tactic.op_induction'
@[simp] lemma id (ℱ : X.presheaf C) (x : X) :
ℱ.stalk_pushforward C (𝟙 X) x = (stalk_functor C x).map ((pushforward.id ℱ).hom) :=
begin
dsimp [stalk_pushforward, stalk_functor],
ext1,
tactic.op_induction',
rcases j with ⟨⟨_, _⟩, _⟩,
rw [colimit.ι_map_assoc, colimit.ι_map, colimit.ι_pre, whisker_left_app, whisker_right_app,
pushforward.id_hom_app, eq_to_hom_map, eq_to_hom_refl],
dsimp,
-- FIXME A simp lemma which unfortunately doesn't fire:
erw [category_theory.functor.map_id],
end
-- This proof is sadly not at all robust:
-- having to use `erw` at all is a bad sign.
@[simp] lemma comp (ℱ : X.presheaf C) (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) :
ℱ.stalk_pushforward C (f ≫ g) x =
((f _* ℱ).stalk_pushforward C g (f x)) ≫ (ℱ.stalk_pushforward C f x) :=
begin
dsimp [stalk_pushforward, stalk_functor],
ext U,
induction U using opposite.rec,
rcases U with ⟨⟨_, _⟩, _⟩,
simp only [colimit.ι_map_assoc, colimit.ι_pre_assoc,
whisker_right_app, category.assoc],
dsimp,
-- FIXME: Some of these are simp lemmas, but don't fire successfully:
erw [category_theory.functor.map_id, category.id_comp, category.id_comp, category.id_comp,
colimit.ι_pre, colimit.ι_pre],
refl,
end
lemma stalk_pushforward_iso_of_open_embedding {f : X ⟶ Y} (hf : open_embedding f)
(F : X.presheaf C) (x : X) : is_iso (F.stalk_pushforward _ f x) :=
begin
haveI := functor.initial_of_adjunction (hf.is_open_map.adjunction_nhds x),
convert is_iso.of_iso ((functor.final.colimit_iso (hf.is_open_map.functor_nhds x).op
((open_nhds.inclusion (f x)).op ⋙ f _* F) : _).symm ≪≫ colim.map_iso _),
swap,
{ fapply nat_iso.of_components,
{ intro U,
refine F.map_iso (eq_to_iso _),
dsimp only [functor.op],
exact congr_arg op (subtype.eq $ set.preimage_image_eq (unop U).1.1 hf.inj) },
{ intros U V i, erw [← F.map_comp, ← F.map_comp], congr } },
{ ext U,
rw ← iso.comp_inv_eq,
erw colimit.ι_map_assoc,
rw [colimit.ι_pre, category.assoc],
erw [colimit.ι_map_assoc, colimit.ι_pre, ← F.map_comp_assoc],
apply colimit.w ((open_nhds.inclusion (f x)).op ⋙ f _* F) _,
dsimp only [functor.op],
refine ((hom_of_le _).op : op (unop U) ⟶ _),
exact set.image_preimage_subset _ _ },
end
end stalk_pushforward
section stalk_pullback
/-- The morphism `ℱ_{f x} ⟶ (f⁻¹ℱ)ₓ` that factors through `(f_*f⁻¹ℱ)_{f x}`. -/
def stalk_pullback_hom (f : X ⟶ Y) (F : Y.presheaf C) (x : X) :
F.stalk (f x) ⟶ (pullback_obj f F).stalk x :=
(stalk_functor _ (f x)).map ((pushforward_pullback_adjunction C f).unit.app F) ≫
stalk_pushforward _ _ _ x
/-- The morphism `(f⁻¹ℱ)(U) ⟶ ℱ_{f(x)}` for some `U ∋ x`. -/
def germ_to_pullback_stalk (f : X ⟶ Y) (F : Y.presheaf C) (U : opens X) (x : U) :
(pullback_obj f F).obj (op U) ⟶ F.stalk (f x) :=
colimit.desc (Lan.diagram (opens.map f).op F (op U))
{ X := F.stalk (f x),
ι := { app := λ V, F.germ ⟨f x, V.hom.unop.le x.2⟩,
naturality' := λ _ _ i, by { erw category.comp_id, exact F.germ_res i.left.unop _ } } }
/-- The morphism `(f⁻¹ℱ)ₓ ⟶ ℱ_{f(x)}`. -/
def stalk_pullback_inv (f : X ⟶ Y) (F : Y.presheaf C) (x : X) :
(pullback_obj f F).stalk x ⟶ F.stalk (f x) :=
colimit.desc ((open_nhds.inclusion x).op ⋙ presheaf.pullback_obj f F)
{ X := F.stalk (f x),
ι := { app := λ U, F.germ_to_pullback_stalk _ f (unop U).1 ⟨x, (unop U).2⟩,
naturality' := λ _ _ _, by { erw [colimit.pre_desc, category.comp_id], congr } } }
/-- The isomorphism `ℱ_{f(x)} ≅ (f⁻¹ℱ)ₓ`. -/
def stalk_pullback_iso (f : X ⟶ Y) (F : Y.presheaf C) (x : X) :
F.stalk (f x) ≅ (pullback_obj f F).stalk x :=
{ hom := stalk_pullback_hom _ _ _ _,
inv := stalk_pullback_inv _ _ _ _,
hom_inv_id' :=
begin
delta stalk_pullback_hom stalk_pullback_inv stalk_functor presheaf.pullback stalk_pushforward
germ_to_pullback_stalk germ,
ext j,
induction j using opposite.rec,
cases j,
simp only [topological_space.open_nhds.inclusion_map_iso_inv, whisker_right_app,
whisker_left_app, whiskering_left_obj_map, functor.comp_map, colimit.ι_map_assoc,
nat_trans.op_id, Lan_obj_map, pushforward_pullback_adjunction_unit_app_app, category.assoc,
colimit.ι_pre_assoc],
erw [colimit.ι_desc, colimit.pre_desc, colimit.ι_desc, category.comp_id],
simpa
end,
inv_hom_id' :=
begin
delta stalk_pullback_hom stalk_pullback_inv stalk_functor presheaf.pullback stalk_pushforward,
ext U j,
induction U using opposite.rec,
cases U, cases j, rcases j_right with ⟨⟨⟩⟩,
erw [colimit.map_desc, colimit.map_desc, colimit.ι_desc_assoc,
colimit.ι_desc_assoc, colimit.ι_desc, category.comp_id],
simp only [cocone.whisker_ι, colimit.cocone_ι, open_nhds.inclusion_map_iso_inv,
cocones.precompose_obj_ι, whisker_right_app, whisker_left_app, nat_trans.comp_app,
whiskering_left_obj_map, nat_trans.op_id, Lan_obj_map,
pushforward_pullback_adjunction_unit_app_app],
erw ←colimit.w _
(@hom_of_le (open_nhds x) _
⟨_, U_property⟩ ⟨(opens.map f).obj (unop j_left), j_hom.unop.le U_property⟩
j_hom.unop.le).op,
erw colimit.ι_pre_assoc (Lan.diagram _ F _) (costructured_arrow.map _),
erw colimit.ι_pre_assoc (Lan.diagram _ F _) (costructured_arrow.map _),
congr,
simp only [category.assoc, costructured_arrow.map_mk],
delta costructured_arrow.mk,
congr,
end }
end stalk_pullback
section stalk_specializes
variables {C}
/-- If `x` specializes to `y`, then there is a natural map `F.stalk y ⟶ F.stalk x`. -/
noncomputable
def stalk_specializes (F : X.presheaf C) {x y : X} (h : x ⤳ y) : F.stalk y ⟶ F.stalk x :=
begin
refine colimit.desc _ ⟨_,λ U, _,_⟩,
{ exact colimit.ι ((open_nhds.inclusion x).op ⋙ F)
(op ⟨(unop U).1, (specializes_iff_forall_open.mp h _ (unop U).1.2 (unop U).2 : _)⟩) },
{ intros U V i,
dsimp,
rw category.comp_id,
let U' : open_nhds x := ⟨_, (specializes_iff_forall_open.mp h _ (unop U).1.2 (unop U).2 : _)⟩,
let V' : open_nhds x := ⟨_, (specializes_iff_forall_open.mp h _ (unop V).1.2 (unop V).2 : _)⟩,
exact colimit.w ((open_nhds.inclusion x).op ⋙ F) (show V' ⟶ U', from i.unop).op }
end
@[simp, reassoc, elementwise]
lemma germ_stalk_specializes (F : X.presheaf C) {U : opens X} {y : U} {x : X} (h : x ⤳ y) :
F.germ y ≫ F.stalk_specializes h =
F.germ ⟨x, specializes_iff_forall_open.mp h _ U.2 y.prop⟩ := colimit.ι_desc _ _
@[simp, reassoc, elementwise]
lemma germ_stalk_specializes' (F : X.presheaf C) {U : opens X} {x y : X} (h : x ⤳ y) (hy : y ∈ U) :
F.germ ⟨y, hy⟩ ≫ F.stalk_specializes h =
F.germ ⟨x, specializes_iff_forall_open.mp h _ U.2 hy⟩ := colimit.ι_desc _ _
@[simp, reassoc, elementwise]
lemma stalk_specializes_stalk_functor_map {F G : X.presheaf C} (f : F ⟶ G) {x y : X} (h : x ⤳ y) :
F.stalk_specializes h ≫ (stalk_functor C x).map f =
(stalk_functor C y).map f ≫ G.stalk_specializes h :=
by { ext, delta stalk_functor, simpa [stalk_specializes] }
@[simp, reassoc, elementwise]
lemma stalk_specializes_stalk_pushforward (f : X ⟶ Y) (F : X.presheaf C) {x y : X} (h : x ⤳ y) :
(f _* F).stalk_specializes (f.map_specializes h) ≫ F.stalk_pushforward _ f x =
F.stalk_pushforward _ f y ≫ F.stalk_specializes h :=
by { ext, delta stalk_pushforward, simpa [stalk_specializes] }
end stalk_specializes
section concrete
variables {C}
variables [concrete_category.{v} C]
local attribute [instance] concrete_category.has_coe_to_sort concrete_category.has_coe_to_fun
@[ext]
lemma germ_ext (F : X.presheaf C) {U V : opens X} {x : X} {hxU : x ∈ U} {hxV : x ∈ V}
(W : opens X) (hxW : x ∈ W) (iWU : W ⟶ U) (iWV : W ⟶ V) {sU : F.obj (op U)} {sV : F.obj (op V)}
(ih : F.map iWU.op sU = F.map iWV.op sV) :
F.germ ⟨x, hxU⟩ sU = F.germ ⟨x, hxV⟩ sV :=
by erw [← F.germ_res iWU ⟨x, hxW⟩,
← F.germ_res iWV ⟨x, hxW⟩, comp_apply, comp_apply, ih]
variables [preserves_filtered_colimits (forget C)]
/--
For presheaves valued in a concrete category whose forgetful functor preserves filtered colimits,
every element of the stalk is the germ of a section.
-/
lemma germ_exist (F : X.presheaf C) (x : X) (t : stalk F x) :
∃ (U : opens X) (m : x ∈ U) (s : F.obj (op U)), F.germ ⟨x, m⟩ s = t :=
begin
obtain ⟨U, s, e⟩ := types.jointly_surjective.{v v} _
(is_colimit_of_preserves (forget C) (colimit.is_colimit _)) t,
revert s e,
rw [(show U = op (unop U), from rfl)],
generalize : unop U = V, clear U,
cases V with V m,
intros s e,
exact ⟨V, m, s, e⟩,
end
lemma germ_eq (F : X.presheaf C) {U V : opens X} (x : X) (mU : x ∈ U) (mV : x ∈ V)
(s : F.obj (op U)) (t : F.obj (op V))
(h : germ F ⟨x, mU⟩ s = germ F ⟨x, mV⟩ t) :
∃ (W : opens X) (m : x ∈ W) (iU : W ⟶ U) (iV : W ⟶ V), F.map iU.op s = F.map iV.op t :=
begin
obtain ⟨W, iU, iV, e⟩ := (types.filtered_colimit.is_colimit_eq_iff.{v v} _
(is_colimit_of_preserves _ (colimit.is_colimit ((open_nhds.inclusion x).op ⋙ F)))).mp h,
exact ⟨(unop W).1, (unop W).2, iU.unop, iV.unop, e⟩,
end
lemma stalk_functor_map_injective_of_app_injective {F G : presheaf C X} (f : F ⟶ G)
(h : ∀ U : opens X, function.injective (f.app (op U))) (x : X) :
function.injective ((stalk_functor C x).map f) := λ s t hst,
begin
rcases germ_exist F x s with ⟨U₁, hxU₁, s, rfl⟩,
rcases germ_exist F x t with ⟨U₂, hxU₂, t, rfl⟩,
simp only [stalk_functor_map_germ_apply _ ⟨x,_⟩] at hst,
obtain ⟨W, hxW, iWU₁, iWU₂, heq⟩ := G.germ_eq x hxU₁ hxU₂ _ _ hst,
rw [← comp_apply, ← comp_apply, ← f.naturality, ← f.naturality, comp_apply, comp_apply] at heq,
replace heq := h W heq,
convert congr_arg (F.germ ⟨x,hxW⟩) heq,
exacts [(F.germ_res_apply iWU₁ ⟨x,hxW⟩ s).symm,
(F.germ_res_apply iWU₂ ⟨x,hxW⟩ t).symm],
end
variables [has_limits C] [preserves_limits (forget C)] [reflects_isomorphisms (forget C)]
/--
Let `F` be a sheaf valued in a concrete category, whose forgetful functor reflects isomorphisms,
preserves limits and filtered colimits. Then two sections who agree on every stalk must be equal.
-/
lemma section_ext (F : sheaf C X) (U : opens X) (s t : F.1.obj (op U))
(h : ∀ x : U, F.presheaf.germ x s = F.presheaf.germ x t) :
s = t :=
begin
-- We use `germ_eq` and the axiom of choice, to pick for every point `x` a neighbourhood
-- `V x`, such that the restrictions of `s` and `t` to `V x` coincide.
choose V m i₁ i₂ heq using λ x : U, F.presheaf.germ_eq x.1 x.2 x.2 s t (h x),
-- Since `F` is a sheaf, we can prove the equality locally, if we can show that these
-- neighborhoods form a cover of `U`.
apply F.eq_of_locally_eq' V U i₁,
{ intros x hxU,
rw [opens.mem_coe, opens.mem_supr],
exact ⟨⟨x, hxU⟩, m ⟨x, hxU⟩⟩ },
{ intro x,
rw [heq, subsingleton.elim (i₁ x) (i₂ x)] }
end
/-
Note that the analogous statement for surjectivity is false: Surjectivity on stalks does not
imply surjectivity of the components of a sheaf morphism. However it does imply that the morphism
is an epi, but this fact is not yet formalized.
-/
lemma app_injective_of_stalk_functor_map_injective {F : sheaf C X} {G : presheaf C X}
(f : F.1 ⟶ G) (U : opens X) (h : ∀ x : U, function.injective ((stalk_functor C x.val).map f)) :
function.injective (f.app (op U)) :=
λ s t hst, section_ext F _ _ _ $ λ x, h x $ by
rw [stalk_functor_map_germ_apply, stalk_functor_map_germ_apply, hst]
lemma app_injective_iff_stalk_functor_map_injective {F : sheaf C X}
{G : presheaf C X} (f : F.1 ⟶ G) :
(∀ x : X, function.injective ((stalk_functor C x).map f)) ↔
(∀ U : opens X, function.injective (f.app (op U))) :=
⟨λ h U, app_injective_of_stalk_functor_map_injective f U (λ x, h x.1),
stalk_functor_map_injective_of_app_injective f⟩
instance stalk_functor_preserves_mono (x : X) :
functor.preserves_monomorphisms (sheaf.forget C X ⋙ stalk_functor C x) :=
⟨λ 𝓐 𝓑 f m, concrete_category.mono_of_injective _ $
(app_injective_iff_stalk_functor_map_injective f.1).mpr
(λ c, (@@concrete_category.mono_iff_injective_of_preserves_pullback _ _ (f.1.app (op c)) _).mp
((nat_trans.mono_iff_mono_app _ f.1).mp
(@@category_theory.presheaf_mono_of_mono _ _ _ _ _ _ _ _ _ _ _ m) $ op c)) x⟩
lemma stalk_mono_of_mono {F G : sheaf C X} (f : F ⟶ G) [mono f] :
Π x, mono $ (stalk_functor C x).map f.1 :=
λ x, by convert functor.map_mono (sheaf.forget.{v} C X ⋙ stalk_functor C x) f
lemma mono_of_stalk_mono {F G : sheaf C X} (f : F ⟶ G)
[Π x, mono $ (stalk_functor C x).map f.1] : mono f :=
(Sheaf.hom.mono_iff_presheaf_mono _ _ _).mpr $ (nat_trans.mono_iff_mono_app _ _).mpr $ λ U,
(concrete_category.mono_iff_injective_of_preserves_pullback _).mpr $
app_injective_of_stalk_functor_map_injective f.1 U.unop $ λ ⟨x, hx⟩,
(concrete_category.mono_iff_injective_of_preserves_pullback _).mp $ infer_instance
lemma mono_iff_stalk_mono {F G : sheaf C X} (f : F ⟶ G) :
mono f ↔ ∀ x, mono ((stalk_functor C x).map f.1) :=
⟨by { introI m, exact stalk_mono_of_mono _ }, by { introI m, exact mono_of_stalk_mono _ }⟩
/-- For surjectivity, we are given an arbitrary section `t` and need to find a preimage for it.
We claim that it suffices to find preimages *locally*. That is, for each `x : U` we construct
a neighborhood `V ≤ U` and a section `s : F.obj (op V))` such that `f.app (op V) s` and `t`
agree on `V`. -/
lemma app_surjective_of_injective_of_locally_surjective {F G : sheaf C X} (f : F ⟶ G)
(U : opens X) (hinj : ∀ x : U, function.injective ((stalk_functor C x.1).map f.1))
(hsurj : ∀ (t) (x : U), ∃ (V : opens X) (m : x.1 ∈ V) (iVU : V ⟶ U) (s : F.1.obj (op V)),
f.1.app (op V) s = G.1.map iVU.op t) :
function.surjective (f.1.app (op U)) :=
begin
intro t,
-- We use the axiom of choice to pick around each point `x` an open neighborhood `V` and a
-- preimage under `f` on `V`.
choose V mV iVU sf heq using hsurj t,
-- These neighborhoods clearly cover all of `U`.
have V_cover : U ≤ supr V,
{ intros x hxU,
rw [opens.mem_coe, opens.mem_supr],
exact ⟨⟨x, hxU⟩, mV ⟨x, hxU⟩⟩ },
-- Since `F` is a sheaf, we can glue all the local preimages together to get a global preimage.
obtain ⟨s, s_spec, -⟩ := F.exists_unique_gluing' V U iVU V_cover sf _,
{ use s,
apply G.eq_of_locally_eq' V U iVU V_cover,
intro x,
rw [← comp_apply, ← f.1.naturality, comp_apply, s_spec, heq] },
{ intros x y,
-- What's left to show here is that the secions `sf` are compatible, i.e. they agree on
-- the intersections `V x ⊓ V y`. We prove this by showing that all germs are equal.
apply section_ext,
intro z,
-- Here, we need to use injectivity of the stalk maps.
apply (hinj ⟨z, (iVU x).le ((inf_le_left : V x ⊓ V y ≤ V x) z.2)⟩),
dsimp only,
erw [stalk_functor_map_germ_apply, stalk_functor_map_germ_apply],
simp_rw [← comp_apply, f.1.naturality, comp_apply, heq, ← comp_apply, ← G.1.map_comp],
refl }
end
lemma app_surjective_of_stalk_functor_map_bijective {F G : sheaf C X} (f : F ⟶ G)
(U : opens X) (h : ∀ x : U, function.bijective ((stalk_functor C x.val).map f.1)) :
function.surjective (f.1.app (op U)) :=
begin
refine app_surjective_of_injective_of_locally_surjective f U (λ x, (h x).1) (λ t x, _),
-- Now we need to prove our initial claim: That we can find preimages of `t` locally.
-- Since `f` is surjective on stalks, we can find a preimage `s₀` of the germ of `t` at `x`
obtain ⟨s₀,hs₀⟩ := (h x).2 (G.presheaf.germ x t),
-- ... and this preimage must come from some section `s₁` defined on some open neighborhood `V₁`
obtain ⟨V₁,hxV₁,s₁,hs₁⟩ := F.presheaf.germ_exist x.1 s₀,
subst hs₁, rename hs₀ hs₁,
erw stalk_functor_map_germ_apply V₁ ⟨x.1,hxV₁⟩ f.1 s₁ at hs₁,
-- Now, the germ of `f.app (op V₁) s₁` equals the germ of `t`, hence they must coincide on
-- some open neighborhood `V₂`.
obtain ⟨V₂, hxV₂, iV₂V₁, iV₂U, heq⟩ := G.presheaf.germ_eq x.1 hxV₁ x.2 _ _ hs₁,
-- The restriction of `s₁` to that neighborhood is our desired local preimage.
use [V₂, hxV₂, iV₂U, F.1.map iV₂V₁.op s₁],
rw [← comp_apply, f.1.naturality, comp_apply, heq],
end
lemma app_bijective_of_stalk_functor_map_bijective {F G : sheaf C X} (f : F ⟶ G)
(U : opens X) (h : ∀ x : U, function.bijective ((stalk_functor C x.val).map f.1)) :
function.bijective (f.1.app (op U)) :=
⟨app_injective_of_stalk_functor_map_injective f.1 U (λ x, (h x).1),
app_surjective_of_stalk_functor_map_bijective f U h⟩
lemma app_is_iso_of_stalk_functor_map_iso {F G : sheaf C X} (f : F ⟶ G) (U : opens X)
[∀ x : U, is_iso ((stalk_functor C x.val).map f.1)] : is_iso (f.1.app (op U)) :=
begin
-- Since the forgetful functor of `C` reflects isomorphisms, it suffices to see that the
-- underlying map between types is an isomorphism, i.e. bijective.
suffices : is_iso ((forget C).map (f.1.app (op U))),
{ exactI is_iso_of_reflects_iso (f.1.app (op U)) (forget C) },
rw is_iso_iff_bijective,
apply app_bijective_of_stalk_functor_map_bijective,
intro x,
apply (is_iso_iff_bijective _).mp,
exact functor.map_is_iso (forget C) ((stalk_functor C x.1).map f.1)
end
/--
Let `F` and `G` be sheaves valued in a concrete category, whose forgetful functor reflects
isomorphisms, preserves limits and filtered colimits. Then if the stalk maps of a morphism
`f : F ⟶ G` are all isomorphisms, `f` must be an isomorphism.
-/
-- Making this an instance would cause a loop in typeclass resolution with `functor.map_is_iso`
lemma is_iso_of_stalk_functor_map_iso {F G : sheaf C X} (f : F ⟶ G)
[∀ x : X, is_iso ((stalk_functor C x).map f.1)] : is_iso f :=
begin
-- Since the inclusion functor from sheaves to presheaves is fully faithful, it suffices to
-- show that `f`, as a morphism between _presheaves_, is an isomorphism.
suffices : is_iso ((sheaf.forget C X).map f),
{ exactI is_iso_of_fully_faithful (sheaf.forget C X) f },
-- We show that all components of `f` are isomorphisms.
suffices : ∀ U : (opens X)ᵒᵖ, is_iso (f.1.app U),
{ exact @nat_iso.is_iso_of_is_iso_app _ _ _ _ F.1 G.1 f.1 this, },
intro U, induction U using opposite.rec,
apply app_is_iso_of_stalk_functor_map_iso
end
/--
Let `F` and `G` be sheaves valued in a concrete category, whose forgetful functor reflects
isomorphisms, preserves limits and filtered colimits. Then a morphism `f : F ⟶ G` is an
isomorphism if and only if all of its stalk maps are isomorphisms.
-/
lemma is_iso_iff_stalk_functor_map_iso {F G : sheaf C X} (f : F ⟶ G) :
is_iso f ↔ ∀ x : X, is_iso ((stalk_functor C x).map f.1) :=
begin
split,
{ intros h x, resetI,
exact @functor.map_is_iso _ _ _ _ _ _ (stalk_functor C x) f.1
((sheaf.forget C X).map_is_iso f) },
{ intro h,
exactI is_iso_of_stalk_functor_map_iso f }
end
end concrete
instance (F : X.presheaf CommRing) {U : opens X} (x : U) :
algebra (F.obj $ op U) (F.stalk x) :=
(F.germ x).to_algebra
@[simp]
lemma stalk_open_algebra_map {X : Top} (F : X.presheaf CommRing) {U : opens X} (x : U) :
algebra_map (F.obj $ op U) (F.stalk x) = F.germ x := rfl
end Top.presheaf
|
cb112593f005dd40ac1592dd58992f1d3da8c40f | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /test/derive_fintype.lean | 72b43c01f4afa77c2bed94b0d86dfd956442acb7 | [
"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 | 929 | lean | /-
Copyright (c) 2020 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import tactic.derive_fintype
@[derive fintype]
inductive alphabet
| a | b | c | d | e | f | g | h | i | j | k | l | m
| n | o | p | q | r | s | t | u | v | w | x | y | z
| A | B | C | D | E | F | G | H | I | J | K | L | M
| N | O | P | Q | R | S | T | U | V | W | X | Y | Z
@[derive fintype]
inductive foo
| A (x : bool)
| B (y : unit)
| C (z : fin 37)
@[derive fintype]
inductive foo2 (α : Type)
| A : α → foo2
| B : α → α → foo2
| C : α × α → foo2
| D : foo2
-- @[derive fintype] -- won't work because missing decidable instance
inductive foo3 (α β : Type) (n : ℕ)
| A : (α → β) → foo3
| B : fin n → foo3
instance (α β : Type) [decidable_eq α] [fintype α] [fintype β] (n : ℕ) : fintype (foo3 α β n) :=
by tactic.mk_fintype_instance
|
3b61ff4e23925b4e0dac91ecfea3fba89ee3bcab | 947b78d97130d56365ae2ec264df196ce769371a | /src/Lean/Data/Json/Basic.lean | 7a304cfdfed3e21e83796ace6b7339706712dab8 | [
"Apache-2.0"
] | permissive | shyamalschandra/lean4 | 27044812be8698f0c79147615b1d5090b9f4b037 | 6e7a883b21eaf62831e8111b251dc9b18f40e604 | refs/heads/master | 1,671,417,126,371 | 1,601,859,995,000 | 1,601,860,020,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,815 | lean | /-
Copyright (c) 2019 Gabriel Ebner. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gabriel Ebner, Marc Huisinga
-/
import Std.Data.RBTree
namespace Lean
-- mantissa * 10^-exponent
structure JsonNumber :=
(mantissa : Int) (exponent : Nat)
protected def JsonNumber.decEq : ∀ (a b : JsonNumber), Decidable (a = b)
| ⟨m1, e1⟩, ⟨m2, e2⟩ =>
match decEq m1 m2 with
| isTrue hm =>
match decEq e1 e2 with
| isTrue he => isTrue (hm ▸ he ▸ rfl)
| isFalse he => isFalse (fun h => JsonNumber.noConfusion h (fun hm he2 => he he2))
| isFalse hm => isFalse (fun h => JsonNumber.noConfusion h (fun hm2 he => hm hm2))
instance JsonNumber.decidableEq : DecidableEq JsonNumber :=
JsonNumber.decEq
namespace JsonNumber
protected def fromNat (n : Nat) : JsonNumber := ⟨n, 0⟩
protected def fromInt (n : Int) : JsonNumber := ⟨n, 0⟩
instance natToJsonNumber : HasCoe Nat JsonNumber := ⟨JsonNumber.fromNat⟩
instance intToJsonNumber : HasCoe Int JsonNumber := ⟨JsonNumber.fromInt⟩
private partial def countDigitsAux : Nat → Nat → Nat | n, digits =>
if n ≤ 9 then
digits
else
countDigitsAux (n/10) (digits+1)
private def countDigits (n : Nat) : Nat :=
countDigitsAux n 1
protected def toString : JsonNumber → String
| ⟨m, 0⟩ => m.repr
| ⟨m, e⟩ =>
let s : Bool := m ≥ 0;
let m := m.natAbs;
-- if there are too many zeroes after the decimal, we
-- use exponents to compress the representation.
-- this is mostly done for memory usage reasons:
-- the size of the representation would otherwise
-- grow exponentially in the value of exponent.
let exp := 9 + (countDigits m : Int) - (e : Int);
let exp := if exp < 0 then exp else 0;
let f := 10 ^ (e - exp.natAbs);
let left := m / f;
let right := (f : Int) + m % f;
let rightUntrimmed := right.repr.mkIterator.next.remainingToString;
(if s then "" else "-") ++
left.repr ++ "." ++
(rightUntrimmed.toSubstring.dropRightWhile (fun c => c = '0')).toString ++
(if exp = 0 then "" else "e" ++ exp.repr)
-- shift a JsonNumber by a specified amount of places to the left
protected def shiftl : JsonNumber → Nat → JsonNumber
-- if s ≤ e, then 10 ^ (s - e) = 1, and hence the mantissa remains unchanged.
-- otherwise, the expression pads the mantissa with zeroes
-- to accomodate for the remaining places to shift.
| ⟨m, e⟩, s => ⟨m * (10 ^ (s - e) : Nat), e - s⟩
-- shift a JsonNumber by a specified amount of places to the right
protected def shiftr : JsonNumber → Nat → JsonNumber
| ⟨m, e⟩, s => ⟨m, e + s⟩
instance jsonNumberToString : HasToString JsonNumber :=
⟨JsonNumber.toString⟩
instance jsonNumberHasRepr : HasRepr JsonNumber :=
⟨fun ⟨m, e⟩ => "⟨" ++ m.repr ++ "," ++ e.repr ++ "⟩"⟩
end JsonNumber
def strLt (a b : String) := Decidable.decide (a < b)
open Std (RBNode RBNode.leaf)
inductive Json
| null
| bool (b : Bool)
| num (n : JsonNumber)
| str (s : String)
| arr (elems : Array Json)
-- uses RBNode instead of RBMap because RBMap is a def
-- and thus currently cannot be used to define a type that
-- is recursive in one of its parameters
| obj (kvPairs : RBNode String (fun _ => Json))
namespace Json
-- HACK(Marc): temporary ugliness until we can use RBMap for JSON objects
def mkObj (o : List (String × Json)) : Json :=
obj (o.foldr (fun ⟨k, v⟩ acc => acc.insert strLt k v) RBNode.leaf)
instance natToJson : HasCoe Nat Json := ⟨fun n => Json.num n⟩
instance intToJson : HasCoe Int Json := ⟨fun n => Json.num n⟩
instance stringToJson : HasCoe String Json := ⟨Json.str⟩
instance boolToJson : HasCoe Bool Json := ⟨Json.bool⟩
def getObj? : Json → Option (RBNode String (fun _ => Json))
| obj kvs => kvs
| _ => none
def getArr? : Json → Option (Array Json)
| arr a => a
| _ => none
def getStr? : Json → Option String
| str s => some s
| _ => none
def getNat? : Json → Option Nat
| (n : Nat) => some n
| _ => none
def getInt? : Json → Option Int
| (i : Int) => some i
| _ => none
def getBool? : Json → Option Bool
| (b : Bool) => some b
| _ => none
def getNum? : Json → Option JsonNumber
| num n => n
| _ => none
def getObjVal? : Json → String → Option Json
| obj kvs, k => kvs.find strLt k
| _ , _ => none
def getArrVal? : Json → Nat → Option Json
| arr a, i => a.get? i
| _ , _ => none
def getObjValD (j : Json) (k : String) : Json :=
(j.getObjVal? k).getD null
inductive Structured
| arr (elems : Array Json)
| obj (kvPairs : RBNode String (fun _ => Json))
instance arrayToStructured : HasCoe (Array Json) Structured :=
⟨Structured.arr⟩
instance kvPairsToStructured : HasCoe (RBNode String (fun _ => Json)) Structured :=
⟨Structured.obj⟩
end Json
end Lean
|
4b0a0992bfceef5bda23470103a2fde18ad3c050 | ec5a7ae10c533e1b1f4b0bc7713e91ecf829a3eb | /ijcar16/examples/cc13.lean | ef65f4d32f0bf0797e5fbcfbd794fbb803e51f44 | [
"MIT"
] | permissive | leanprover/leanprover.github.io | cf248934af7c7e9aeff17cf8df3c12c5e7e73f1a | 071a20d2e059a2c3733e004c681d3949cac3c07a | refs/heads/master | 1,692,621,047,417 | 1,691,396,994,000 | 1,691,396,994,000 | 19,366,263 | 18 | 27 | MIT | 1,693,989,071,000 | 1,399,006,345,000 | Lean | UTF-8 | Lean | false | false | 1,090 | lean | /-
Example/test file for the congruence closure procedure described in the paper:
"Congruence Closure for Intensional Type Theory"
Daniel Selsam and Leonardo de Moura
The tactic `by blast` has been configured in this file to use just
the congruence closure procedure using the command
set_option blast.strategy "cc"
-/
set_option blast.strategy "cc"
example (a b c : Prop) : a = b → b = c → (a ↔ c) :=
by blast
example (a b c : Prop) : a = b → b == c → (a ↔ c) :=
by blast
example (a b c : nat) : a == b → b = c → a == c :=
by blast
example (a b c : nat) : a == b → b = c → a = c :=
by blast
example (a b c d : nat) : a == b → b == c → c == d → a = d :=
by blast
example (a b c d : nat) : a == b → b = c → c == d → a = d :=
by blast
example (a b c : Prop) : a = b → b = c → (a ↔ c) :=
by blast
example (a b c : Prop) : a == b → b = c → (a ↔ c) :=
by blast
example (a b c d : Prop) : a == b → b == c → c == d → (a ↔ d) :=
by blast
definition foo (a b c d : Prop) : a == b → b = c → c == d → (a ↔ d) :=
by blast
|
086de0a6feb6c9f23c4e30b38c11da6212a97159 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/differential_object.lean | 3ea1d59ac7a49ac7076f6c473f4aba134aa4b98f | [
"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,670 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import data.int.basic
import category_theory.shift
import category_theory.concrete_category.basic
/-!
# Differential objects in a category.
A differential object in a category with zero morphisms and a shift is
an object `X` equipped with
a morphism `d : X ⟶ X⟦1⟧`, such that `d^2 = 0`.
We build the category of differential objects, and some basic constructions
such as the forgetful functor, zero morphisms and zero objects, and the shift functor
on differential objects.
-/
open category_theory.limits
universes v u
namespace category_theory
variables (C : Type u) [category.{v} C]
-- TODO: generaize to `has_shift C A` for an arbitrary `[add_monoid A]` `[has_one A]`.
variables [has_zero_morphisms C] [has_shift C ℤ]
/--
A differential object in a category with zero morphisms and a shift is
an object `X` equipped with
a morphism `d : X ⟶ X⟦1⟧`, such that `d^2 = 0`.
-/
@[nolint has_inhabited_instance]
structure differential_object :=
(X : C)
(d : X ⟶ X⟦1⟧)
(d_squared' : d ≫ d⟦(1:ℤ)⟧' = 0 . obviously)
restate_axiom differential_object.d_squared'
attribute [simp] differential_object.d_squared
variables {C}
namespace differential_object
/--
A morphism of differential objects is a morphism commuting with the differentials.
-/
@[ext, nolint has_inhabited_instance]
structure hom (X Y : differential_object C) :=
(f : X.X ⟶ Y.X)
(comm' : X.d ≫ f⟦1⟧' = f ≫ Y.d . obviously)
restate_axiom hom.comm'
attribute [simp, reassoc] hom.comm
namespace hom
/-- The identity morphism of a differential object. -/
@[simps]
def id (X : differential_object C) : hom X X :=
{ f := 𝟙 X.X }
/-- The composition of morphisms of differential objects. -/
@[simps]
def comp {X Y Z : differential_object C} (f : hom X Y) (g : hom Y Z) : hom X Z :=
{ f := f.f ≫ g.f, }
end hom
instance category_of_differential_objects : category (differential_object C) :=
{ hom := hom,
id := hom.id,
comp := λ X Y Z f g, hom.comp f g, }
@[simp]
lemma id_f (X : differential_object C) : ((𝟙 X) : X ⟶ X).f = 𝟙 (X.X) := rfl
@[simp]
lemma comp_f {X Y Z : differential_object C} (f : X ⟶ Y) (g : Y ⟶ Z) :
(f ≫ g).f = f.f ≫ g.f :=
rfl
@[simp]
lemma eq_to_hom_f {X Y : differential_object C} (h : X = Y) :
hom.f (eq_to_hom h) = eq_to_hom (congr_arg _ h) :=
by { subst h, rw [eq_to_hom_refl, eq_to_hom_refl], refl }
variables (C)
/-- The forgetful functor taking a differential object to its underlying object. -/
def forget : (differential_object C) ⥤ C :=
{ obj := λ X, X.X,
map := λ X Y f, f.f, }
instance forget_faithful : faithful (forget C) :=
{ }
instance has_zero_morphisms : has_zero_morphisms (differential_object C) :=
{ has_zero := λ X Y,
⟨{ f := 0 }⟩}
variables {C}
@[simp]
lemma zero_f (P Q : differential_object C) : (0 : P ⟶ Q).f = 0 := rfl
/--
An isomorphism of differential objects gives an isomorphism of the underlying objects.
-/
@[simps] def iso_app {X Y : differential_object C} (f : X ≅ Y) : X.X ≅ Y.X :=
⟨f.hom.f, f.inv.f, by { dsimp, rw [← comp_f, iso.hom_inv_id, id_f] },
by { dsimp, rw [← comp_f, iso.inv_hom_id, id_f] }⟩
@[simp] lemma iso_app_refl (X : differential_object C) : iso_app (iso.refl X) = iso.refl X.X := rfl
@[simp] lemma iso_app_symm {X Y : differential_object C} (f : X ≅ Y) :
iso_app f.symm = (iso_app f).symm := rfl
@[simp] lemma iso_app_trans {X Y Z : differential_object C} (f : X ≅ Y) (g : Y ≅ Z) :
iso_app (f ≪≫ g) = iso_app f ≪≫ iso_app g := rfl
/-- An isomorphism of differential objects can be constructed
from an isomorphism of the underlying objects that commutes with the differentials. -/
@[simps] def mk_iso {X Y : differential_object C}
(f : X.X ≅ Y.X) (hf : X.d ≫ f.hom⟦1⟧' = f.hom ≫ Y.d) : X ≅ Y :=
{ hom := ⟨f.hom, hf⟩,
inv := ⟨f.inv, by { dsimp, rw [← functor.map_iso_inv, iso.comp_inv_eq, category.assoc,
iso.eq_inv_comp, functor.map_iso_hom, hf] }⟩,
hom_inv_id' := by { ext1, dsimp, exact f.hom_inv_id },
inv_hom_id' := by { ext1, dsimp, exact f.inv_hom_id } }
end differential_object
namespace functor
universes v' u'
variables (D : Type u') [category.{v'} D]
variables [has_zero_morphisms D] [has_shift D ℤ]
/--
A functor `F : C ⥤ D` which commutes with shift functors on `C` and `D` and preserves zero morphisms
can be lifted to a functor `differential_object C ⥤ differential_object D`.
-/
@[simps]
def map_differential_object (F : C ⥤ D)
(η : (shift_functor C (1:ℤ)).comp F ⟶ F.comp (shift_functor D (1:ℤ)))
(hF : ∀ c c', F.map (0 : c ⟶ c') = 0) :
differential_object C ⥤ differential_object D :=
{ obj := λ X, { X := F.obj X.X,
d := F.map X.d ≫ η.app X.X,
d_squared' := begin
rw [functor.map_comp, ← functor.comp_map F (shift_functor D (1:ℤ))],
slice_lhs 2 3 { rw [← η.naturality X.d] },
rw [functor.comp_map],
slice_lhs 1 2 { rw [← F.map_comp, X.d_squared, hF] },
rw [zero_comp, zero_comp],
end },
map := λ X Y f, { f := F.map f.f,
comm' := begin
dsimp,
slice_lhs 2 3 { rw [← functor.comp_map F (shift_functor D (1:ℤ)), ← η.naturality f.f] },
slice_lhs 1 2 { rw [functor.comp_map, ← F.map_comp, f.comm, F.map_comp] },
rw [category.assoc]
end },
map_id' := by { intros, ext, simp },
map_comp' := by { intros, ext, simp }, }
end functor
end category_theory
namespace category_theory
namespace differential_object
variables (C : Type u) [category.{v} C]
variables [has_zero_object C] [has_zero_morphisms C] [has_shift C ℤ]
open_locale zero_object
instance has_zero_object : has_zero_object (differential_object C) :=
by { refine ⟨⟨⟨0, 0⟩, λ X, ⟨⟨⟨⟨0⟩⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨⟨0⟩⟩, λ f, _⟩⟩⟩⟩; ext, }
end differential_object
namespace differential_object
variables (C : Type (u+1)) [large_category C] [concrete_category C]
[has_zero_morphisms C] [has_shift C ℤ]
instance concrete_category_of_differential_objects :
concrete_category (differential_object C) :=
{ forget := forget C ⋙ category_theory.forget C }
instance : has_forget₂ (differential_object C) C :=
{ forget₂ := forget C }
end differential_object
/-! The category of differential objects itself has a shift functor. -/
namespace differential_object
variables (C : Type u) [category.{v} C]
variables [has_zero_morphisms C] [has_shift C ℤ]
noncomputable theory
/-- The shift functor on `differential_object C`. -/
@[simps]
def shift_functor (n : ℤ) : differential_object C ⥤ differential_object C :=
{ obj := λ X,
{ X := X.X⟦n⟧,
d := X.d⟦n⟧' ≫ (shift_comm _ _ _).hom,
d_squared' := by rw [functor.map_comp, category.assoc, shift_comm_hom_comp_assoc,
←functor.map_comp_assoc, X.d_squared, functor.map_zero, zero_comp] },
map := λ X Y f,
{ f := f.f⟦n⟧',
comm' := by { dsimp, rw [category.assoc, shift_comm_hom_comp, ← functor.map_comp_assoc,
f.comm, functor.map_comp_assoc], }, },
map_id' := by { intros X, ext1, dsimp, rw functor.map_id },
map_comp' := by { intros X Y Z f g, ext1, dsimp, rw functor.map_comp } }
local attribute [reducible] discrete.add_monoidal shift_comm
/-- The shift functor on `differential_object C` is additive. -/
@[simps] def shift_functor_add (m n : ℤ) :
shift_functor C (m + n) ≅ shift_functor C m ⋙ shift_functor C n :=
begin
refine nat_iso.of_components (λ X, mk_iso (shift_add X.X _ _) _) _,
{ dsimp,
simp_rw [category.assoc, obj_μ_inv_app, μ_inv_hom_app_assoc, functor.map_comp, obj_μ_app,
category.assoc, μ_naturality_assoc, μ_inv_hom_app_assoc, obj_μ_inv_app, category.assoc,
μ_naturalityₗ_assoc, μ_inv_hom_app_assoc, μ_inv_naturalityᵣ_assoc],
simp [opaque_eq_to_iso] },
{ intros X Y f, ext, dsimp, exact nat_trans.naturality _ _ }
end
local attribute [reducible] endofunctor_monoidal_category
section
local attribute [instance] endofunctor_monoidal_category
/-- The shift by zero is naturally isomorphic to the identity. -/
@[simps]
def shift_ε : 𝟭 (differential_object C) ≅ shift_functor C 0 :=
begin
refine nat_iso.of_components (λ X, mk_iso ((shift_monoidal_functor C ℤ).ε_iso.app X.X) _) _,
{ dsimp, simp, dsimp, simp },
{ introv, ext, dsimp, simp }
end
end
instance : has_shift (differential_object C) ℤ :=
has_shift_mk _ _
{ F := shift_functor C,
ε := shift_ε C,
μ := λ m n, (shift_functor_add C m n).symm }
end differential_object
end category_theory
|
7be22dc4bb02a4ae9dd7aae6e952ed6ce017371c | 6b2a480f27775cba4f3ae191b1c1387a29de586e | /group_rep1/hello.lean | 6fe382626294f23ffcbaae3873f27c422bc5afd2 | [] | no_license | Or7ando/group_representation | a681de2e19d1930a1e1be573d6735a2f0b8356cb | 9b576984f17764ebf26c8caa2a542d248f1b50d2 | refs/heads/master | 1,662,413,107,324 | 1,590,302,389,000 | 1,590,302,389,000 | 258,130,829 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 19 | lean | Hello c'st un test
|
cee42dcc08d43ec1269504a244c5b8d544700a41 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/field_theory/polynomial_galois_group.lean | 4c73c3d5bf348f3662600ed2400af274f1afd049 | [
"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 | 21,214 | lean | /-
Copyright (c) 2020 Thomas Browning, Patrick Lutz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Thomas Browning, Patrick Lutz
-/
import analysis.complex.polynomial
import field_theory.galois
import group_theory.perm.cycle.type
/-!
# Galois Groups of Polynomials
In this file, we introduce the Galois group of a polynomial `p` over a field `F`,
defined as the automorphism group of its splitting field. We also provide
some results about some extension `E` above `p.splitting_field`, and some specific
results about the Galois groups of ℚ-polynomials with specific numbers of non-real roots.
## Main definitions
- `polynomial.gal p`: the Galois group of a polynomial p.
- `polynomial.gal.restrict p E`: the restriction homomorphism `(E ≃ₐ[F] E) → gal p`.
- `polynomial.gal.gal_action p E`: the action of `gal p` on the roots of `p` in `E`.
## Main results
- `polynomial.gal.restrict_smul`: `restrict p E` is compatible with `gal_action p E`.
- `polynomial.gal.gal_action_hom_injective`: `gal p` acting on the roots of `p` in `E` is faithful.
- `polynomial.gal.restrict_prod_injective`: `gal (p * q)` embeds as a subgroup of `gal p × gal q`.
- `polynomial.gal.card_of_separable`: For a separable polynomial, its Galois group has cardinality
equal to the dimension of its splitting field over `F`.
- `polynomial.gal.gal_action_hom_bijective_of_prime_degree`:
An irreducible polynomial of prime degree with two non-real roots has full Galois group.
## Other results
- `polynomial.gal.card_complex_roots_eq_card_real_add_card_not_gal_inv`: The number of complex roots
equals the number of real roots plus the number of roots not fixed by complex conjugation
(i.e. with some imaginary component).
-/
noncomputable theory
open_locale classical polynomial
open finite_dimensional
namespace polynomial
variables {F : Type*} [field F] (p q : F[X]) (E : Type*) [field E] [algebra F E]
/-- The Galois group of a polynomial. -/
@[derive [group, fintype]]
def gal := p.splitting_field ≃ₐ[F] p.splitting_field
namespace gal
instance : has_coe_to_fun p.gal (λ _, p.splitting_field → p.splitting_field) :=
alg_equiv.has_coe_to_fun
instance apply_mul_semiring_action : mul_semiring_action p.gal p.splitting_field :=
alg_equiv.apply_mul_semiring_action
@[ext] lemma ext {σ τ : p.gal} (h : ∀ x ∈ p.root_set p.splitting_field, σ x = τ x) : σ = τ :=
begin
refine alg_equiv.ext (λ x, (alg_hom.mem_equalizer σ.to_alg_hom τ.to_alg_hom x).mp
((set_like.ext_iff.mp _ x).mpr algebra.mem_top)),
rwa [eq_top_iff, ←splitting_field.adjoin_roots, algebra.adjoin_le_iff],
end
/-- If `p` splits in `F` then the `p.gal` is trivial. -/
def unique_gal_of_splits (h : p.splits (ring_hom.id F)) : unique p.gal :=
{ default := 1,
uniq := λ f, alg_equiv.ext (λ x, by { obtain ⟨y, rfl⟩ := algebra.mem_bot.mp
((set_like.ext_iff.mp ((is_splitting_field.splits_iff _ p).mp h) x).mp algebra.mem_top),
rw [alg_equiv.commutes, alg_equiv.commutes] }) }
instance [h : fact (p.splits (ring_hom.id F))] : unique p.gal :=
unique_gal_of_splits _ (h.1)
instance unique_gal_zero : unique (0 : F[X]).gal :=
unique_gal_of_splits _ (splits_zero _)
instance unique_gal_one : unique (1 : F[X]).gal :=
unique_gal_of_splits _ (splits_one _)
instance unique_gal_C (x : F) : unique (C x).gal :=
unique_gal_of_splits _ (splits_C _ _)
instance unique_gal_X : unique (X : F[X]).gal :=
unique_gal_of_splits _ (splits_X _)
instance unique_gal_X_sub_C (x : F) : unique (X - C x).gal :=
unique_gal_of_splits _ (splits_X_sub_C _)
instance unique_gal_X_pow (n : ℕ) : unique (X ^ n : F[X]).gal :=
unique_gal_of_splits _ (splits_X_pow _ _)
instance [h : fact (p.splits (algebra_map F E))] : algebra p.splitting_field E :=
(is_splitting_field.lift p.splitting_field p h.1).to_ring_hom.to_algebra
instance [h : fact (p.splits (algebra_map F E))] : is_scalar_tower F p.splitting_field E :=
is_scalar_tower.of_algebra_map_eq
(λ x, ((is_splitting_field.lift p.splitting_field p h.1).commutes x).symm)
-- The `algebra p.splitting_field E` instance above behaves badly when
-- `E := p.splitting_field`, since it may result in a unification problem
-- `is_splitting_field.lift.to_ring_hom.to_algebra =?= algebra.id`,
-- which takes an extremely long time to resolve, causing timeouts.
-- Since we don't really care about this definition, marking it as irreducible
-- causes that unification to error out early.
attribute [irreducible] gal.algebra
/-- Restrict from a superfield automorphism into a member of `gal p`. -/
def restrict [fact (p.splits (algebra_map F E))] : (E ≃ₐ[F] E) →* p.gal :=
alg_equiv.restrict_normal_hom p.splitting_field
lemma restrict_surjective [fact (p.splits (algebra_map F E))] [normal F E] :
function.surjective (restrict p E) :=
alg_equiv.restrict_normal_hom_surjective E
section roots_action
/-- The function taking `roots p p.splitting_field` to `roots p E`. This is actually a bijection,
see `polynomial.gal.map_roots_bijective`. -/
def map_roots [fact (p.splits (algebra_map F E))] :
root_set p p.splitting_field → root_set p E :=
set.maps_to.restrict (is_scalar_tower.to_alg_hom F p.splitting_field E) _ _ $ root_set_maps_to _
lemma map_roots_bijective [h : fact (p.splits (algebra_map F E))] :
function.bijective (map_roots p E) :=
begin
split,
{ exact λ _ _ h, subtype.ext (ring_hom.injective _ (subtype.ext_iff.mp h)) },
{ intro y,
-- this is just an equality of two different ways to write the roots of `p` as an `E`-polynomial
have key := roots_map
(is_scalar_tower.to_alg_hom F p.splitting_field E : p.splitting_field →+* E)
((splits_id_iff_splits _).mpr (is_splitting_field.splits p.splitting_field p)),
rw [map_map, alg_hom.comp_algebra_map] at key,
have hy := subtype.mem y,
simp only [root_set, finset.mem_coe, multiset.mem_to_finset, key, multiset.mem_map] at hy,
rcases hy with ⟨x, hx1, hx2⟩,
exact ⟨⟨x, multiset.mem_to_finset.mpr hx1⟩, subtype.ext hx2⟩ }
end
/-- The bijection between `root_set p p.splitting_field` and `root_set p E`. -/
def roots_equiv_roots [fact (p.splits (algebra_map F E))] :
(root_set p p.splitting_field) ≃ (root_set p E) :=
equiv.of_bijective (map_roots p E) (map_roots_bijective p E)
instance gal_action_aux : mul_action p.gal (root_set p p.splitting_field) :=
{ smul := λ ϕ, set.maps_to.restrict ϕ _ _ $ root_set_maps_to ϕ.to_alg_hom,
one_smul := λ _, by { ext, refl },
mul_smul := λ _ _ _, by { ext, refl } }
/-- The action of `gal p` on the roots of `p` in `E`. -/
instance gal_action [fact (p.splits (algebra_map F E))] : mul_action p.gal (root_set p E) :=
{ smul := λ ϕ x, roots_equiv_roots p E (ϕ • ((roots_equiv_roots p E).symm x)),
one_smul := λ _, by simp only [equiv.apply_symm_apply, one_smul],
mul_smul := λ _ _ _, by simp only [equiv.apply_symm_apply, equiv.symm_apply_apply, mul_smul] }
variables {p E}
/-- `polynomial.gal.restrict p E` is compatible with `polynomial.gal.gal_action p E`. -/
@[simp] lemma restrict_smul [fact (p.splits (algebra_map F E))]
(ϕ : E ≃ₐ[F] E) (x : root_set p E) : ↑((restrict p E ϕ) • x) = ϕ x :=
begin
let ψ := alg_equiv.of_injective_field (is_scalar_tower.to_alg_hom F p.splitting_field E),
change ↑(ψ (ψ.symm _)) = ϕ x,
rw alg_equiv.apply_symm_apply ψ,
change ϕ (roots_equiv_roots p E ((roots_equiv_roots p E).symm x)) = ϕ x,
rw equiv.apply_symm_apply (roots_equiv_roots p E),
end
variables (p E)
/-- `polynomial.gal.gal_action` as a permutation representation -/
def gal_action_hom [fact (p.splits (algebra_map F E))] : p.gal →* equiv.perm (root_set p E) :=
mul_action.to_perm_hom _ _
lemma gal_action_hom_restrict [fact (p.splits (algebra_map F E))]
(ϕ : E ≃ₐ[F] E) (x : root_set p E) : ↑(gal_action_hom p E (restrict p E ϕ) x) = ϕ x :=
restrict_smul ϕ x
/-- `gal p` embeds as a subgroup of permutations of the roots of `p` in `E`. -/
lemma gal_action_hom_injective [fact (p.splits (algebra_map F E))] :
function.injective (gal_action_hom p E) :=
begin
rw injective_iff_map_eq_one,
intros ϕ hϕ,
ext x hx,
have key := equiv.perm.ext_iff.mp hϕ (roots_equiv_roots p E ⟨x, hx⟩),
change roots_equiv_roots p E (ϕ • (roots_equiv_roots p E).symm
(roots_equiv_roots p E ⟨x, hx⟩)) = roots_equiv_roots p E ⟨x, hx⟩ at key,
rw equiv.symm_apply_apply at key,
exact subtype.ext_iff.mp (equiv.injective (roots_equiv_roots p E) key),
end
end roots_action
variables {p q}
/-- `polynomial.gal.restrict`, when both fields are splitting fields of polynomials. -/
def restrict_dvd (hpq : p ∣ q) : q.gal →* p.gal :=
if hq : q = 0 then 1 else @restrict F _ p _ _ _
⟨splits_of_splits_of_dvd (algebra_map F q.splitting_field) hq (splitting_field.splits q) hpq⟩
lemma restrict_dvd_surjective (hpq : p ∣ q) (hq : q ≠ 0) :
function.surjective (restrict_dvd hpq) :=
by simp only [restrict_dvd, dif_neg hq, restrict_surjective]
variables (p q)
/-- The Galois group of a product maps into the product of the Galois groups. -/
def restrict_prod : (p * q).gal →* p.gal × q.gal :=
monoid_hom.prod (restrict_dvd (dvd_mul_right p q)) (restrict_dvd (dvd_mul_left q p))
/-- `polynomial.gal.restrict_prod` is actually a subgroup embedding. -/
lemma restrict_prod_injective : function.injective (restrict_prod p q) :=
begin
by_cases hpq : (p * q) = 0,
{ haveI : unique (p * q).gal, { rw hpq, apply_instance },
exact λ f g h, eq.trans (unique.eq_default f) (unique.eq_default g).symm },
intros f g hfg,
dsimp only [restrict_prod, restrict_dvd] at hfg,
simp only [dif_neg hpq, monoid_hom.prod_apply, prod.mk.inj_iff] at hfg,
ext x hx,
rw [root_set, polynomial.map_mul, polynomial.roots_mul] at hx,
cases multiset.mem_add.mp (multiset.mem_to_finset.mp hx) with h h,
{ haveI : fact (p.splits (algebra_map F (p * q).splitting_field)) :=
⟨splits_of_splits_of_dvd _ hpq (splitting_field.splits (p * q)) (dvd_mul_right p q)⟩,
have key : x = algebra_map (p.splitting_field) (p * q).splitting_field
((roots_equiv_roots p _).inv_fun ⟨x, multiset.mem_to_finset.mpr h⟩) :=
subtype.ext_iff.mp (equiv.apply_symm_apply (roots_equiv_roots p _) ⟨x, _⟩).symm,
rw [key, ←alg_equiv.restrict_normal_commutes, ←alg_equiv.restrict_normal_commutes],
exact congr_arg _ (alg_equiv.ext_iff.mp hfg.1 _) },
{ haveI : fact (q.splits (algebra_map F (p * q).splitting_field)) :=
⟨splits_of_splits_of_dvd _ hpq (splitting_field.splits (p * q)) (dvd_mul_left q p)⟩,
have key : x = algebra_map (q.splitting_field) (p * q).splitting_field
((roots_equiv_roots q _).inv_fun ⟨x, multiset.mem_to_finset.mpr h⟩) :=
subtype.ext_iff.mp (equiv.apply_symm_apply (roots_equiv_roots q _) ⟨x, _⟩).symm,
rw [key, ←alg_equiv.restrict_normal_commutes, ←alg_equiv.restrict_normal_commutes],
exact congr_arg _ (alg_equiv.ext_iff.mp hfg.2 _) },
{ rwa [ne.def, mul_eq_zero, map_eq_zero, map_eq_zero, ←mul_eq_zero] }
end
lemma mul_splits_in_splitting_field_of_mul {p₁ q₁ p₂ q₂ : F[X]}
(hq₁ : q₁ ≠ 0) (hq₂ : q₂ ≠ 0) (h₁ : p₁.splits (algebra_map F q₁.splitting_field))
(h₂ : p₂.splits (algebra_map F q₂.splitting_field)) :
(p₁ * p₂).splits (algebra_map F (q₁ * q₂).splitting_field) :=
begin
apply splits_mul,
{ rw ← (splitting_field.lift q₁ (splits_of_splits_of_dvd _
(mul_ne_zero hq₁ hq₂) (splitting_field.splits _) (dvd_mul_right q₁ q₂))).comp_algebra_map,
exact splits_comp_of_splits _ _ h₁, },
{ rw ← (splitting_field.lift q₂ (splits_of_splits_of_dvd _
(mul_ne_zero hq₁ hq₂) (splitting_field.splits _) (dvd_mul_left q₂ q₁))).comp_algebra_map,
exact splits_comp_of_splits _ _ h₂, },
end
/-- `p` splits in the splitting field of `p ∘ q`, for `q` non-constant. -/
lemma splits_in_splitting_field_of_comp (hq : q.nat_degree ≠ 0) :
p.splits (algebra_map F (p.comp q).splitting_field) :=
begin
let P : F[X] → Prop := λ r, r.splits (algebra_map F (r.comp q).splitting_field),
have key1 : ∀ {r : F[X]}, irreducible r → P r,
{ intros r hr,
by_cases hr' : nat_degree r = 0,
{ exact splits_of_nat_degree_le_one _ (le_trans (le_of_eq hr') zero_le_one) },
obtain ⟨x, hx⟩ := exists_root_of_splits _ (splitting_field.splits (r.comp q))
(λ h, hr' ((mul_eq_zero.mp (nat_degree_comp.symm.trans
(nat_degree_eq_of_degree_eq_some h))).resolve_right hq)),
rw [←aeval_def, aeval_comp] at hx,
have h_normal : normal F (r.comp q).splitting_field := splitting_field.normal (r.comp q),
have qx_int := normal.is_integral h_normal (aeval x q),
exact splits_of_splits_of_dvd _
(minpoly.ne_zero qx_int)
(normal.splits h_normal _)
((minpoly.irreducible qx_int).dvd_symm hr (minpoly.dvd F _ hx)) },
have key2 : ∀ {p₁ p₂ : F[X]}, P p₁ → P p₂ → P (p₁ * p₂),
{ intros p₁ p₂ hp₁ hp₂,
by_cases h₁ : p₁.comp q = 0,
{ cases comp_eq_zero_iff.mp h₁ with h h,
{ rw [h, zero_mul],
exact splits_zero _ },
{ exact false.rec _ (hq (by rw [h.2, nat_degree_C])) } },
by_cases h₂ : p₂.comp q = 0,
{ cases comp_eq_zero_iff.mp h₂ with h h,
{ rw [h, mul_zero],
exact splits_zero _ },
{ exact false.rec _ (hq (by rw [h.2, nat_degree_C])) } },
have key := mul_splits_in_splitting_field_of_mul h₁ h₂ hp₁ hp₂,
rwa ← mul_comp at key },
exact wf_dvd_monoid.induction_on_irreducible p (splits_zero _)
(λ _, splits_of_is_unit _) (λ _ _ _ h, key2 (key1 h)),
end
/-- `polynomial.gal.restrict` for the composition of polynomials. -/
def restrict_comp (hq : q.nat_degree ≠ 0) : (p.comp q).gal →* p.gal :=
@restrict F _ p _ _ _ ⟨splits_in_splitting_field_of_comp p q hq⟩
lemma restrict_comp_surjective (hq : q.nat_degree ≠ 0) :
function.surjective (restrict_comp p q hq) :=
by simp only [restrict_comp, restrict_surjective]
variables {p q}
/-- For a separable polynomial, its Galois group has cardinality
equal to the dimension of its splitting field over `F`. -/
lemma card_of_separable (hp : p.separable) :
fintype.card p.gal = finrank F p.splitting_field :=
begin
haveI : is_galois F p.splitting_field := is_galois.of_separable_splitting_field hp,
exact is_galois.card_aut_eq_finrank F p.splitting_field,
end
lemma prime_degree_dvd_card [char_zero F] (p_irr : irreducible p) (p_deg : p.nat_degree.prime) :
p.nat_degree ∣ fintype.card p.gal :=
begin
rw gal.card_of_separable p_irr.separable,
have hp : p.degree ≠ 0 :=
λ h, nat.prime.ne_zero p_deg (nat_degree_eq_zero_iff_degree_le_zero.mpr (le_of_eq h)),
let α : p.splitting_field := root_of_splits (algebra_map F p.splitting_field)
(splitting_field.splits p) hp,
have hα : is_integral F α := algebra.is_integral_of_finite _ _ α,
use finite_dimensional.finrank F⟮α⟯ p.splitting_field,
suffices : (minpoly F α).nat_degree = p.nat_degree,
{ rw [←finite_dimensional.finrank_mul_finrank F F⟮α⟯ p.splitting_field,
intermediate_field.adjoin.finrank hα, this] },
suffices : minpoly F α ∣ p,
{ have key := (minpoly.irreducible hα).dvd_symm p_irr this,
apply le_antisymm,
{ exact nat_degree_le_of_dvd this p_irr.ne_zero },
{ exact nat_degree_le_of_dvd key (minpoly.ne_zero hα) } },
apply minpoly.dvd F α,
rw [aeval_def, map_root_of_splits _ (splitting_field.splits p) hp],
end
section rationals
lemma splits_ℚ_ℂ {p : ℚ[X]} : fact (p.splits (algebra_map ℚ ℂ)) :=
⟨is_alg_closed.splits_codomain p⟩
local attribute [instance] splits_ℚ_ℂ
/-- The number of complex roots equals the number of real roots plus
the number of roots not fixed by complex conjugation (i.e. with some imaginary component). -/
lemma card_complex_roots_eq_card_real_add_card_not_gal_inv (p : ℚ[X]) :
(p.root_set ℂ).to_finset.card = (p.root_set ℝ).to_finset.card +
(gal_action_hom p ℂ (restrict p ℂ (complex.conj_ae.restrict_scalars ℚ))).support.card :=
begin
by_cases hp : p = 0,
{ haveI : is_empty (p.root_set ℂ) := by { rw [hp, root_set_zero], apply_instance },
simp_rw [(gal_action_hom p ℂ _).support.eq_empty_of_is_empty, hp, root_set_zero,
set.to_finset_empty, finset.card_empty] },
have inj : function.injective (is_scalar_tower.to_alg_hom ℚ ℝ ℂ) := (algebra_map ℝ ℂ).injective,
rw [←finset.card_image_of_injective _ subtype.coe_injective,
←finset.card_image_of_injective _ inj],
let a : finset ℂ := _,
let b : finset ℂ := _,
let c : finset ℂ := _,
change a.card = b.card + c.card,
have ha : ∀ z : ℂ, z ∈ a ↔ aeval z p = 0,
{ intro z, rw [set.mem_to_finset, mem_root_set_of_ne hp], apply_instance },
have hb : ∀ z : ℂ, z ∈ b ↔ aeval z p = 0 ∧ z.im = 0,
{ intro z,
simp_rw [finset.mem_image, exists_prop, set.mem_to_finset, mem_root_set_of_ne hp],
split,
{ rintros ⟨w, hw, rfl⟩,
exact ⟨by rw [aeval_alg_hom_apply, hw, alg_hom.map_zero], rfl⟩ },
{ rintros ⟨hz1, hz2⟩,
have key : is_scalar_tower.to_alg_hom ℚ ℝ ℂ z.re = z := by { ext, refl, rw hz2, refl },
exact ⟨z.re, inj (by rwa [←aeval_alg_hom_apply, key, alg_hom.map_zero]), key⟩ } },
have hc0 : ∀ w : p.root_set ℂ, gal_action_hom p ℂ
(restrict p ℂ (complex.conj_ae.restrict_scalars ℚ)) w = w ↔ w.val.im = 0,
{ intro w,
rw [subtype.ext_iff, gal_action_hom_restrict],
exact complex.conj_eq_iff_im },
have hc : ∀ z : ℂ, z ∈ c ↔ aeval z p = 0 ∧ z.im ≠ 0,
{ intro z,
simp_rw [finset.mem_image, exists_prop],
split,
{ rintros ⟨w, hw, rfl⟩,
exact ⟨(mem_root_set.mp w.2).2, mt (hc0 w).mpr (equiv.perm.mem_support.mp hw)⟩ },
{ rintros ⟨hz1, hz2⟩,
exact ⟨⟨z, mem_root_set.mpr ⟨hp, hz1⟩⟩,
equiv.perm.mem_support.mpr (mt (hc0 _).mp hz2), rfl⟩ } },
rw ← finset.card_disjoint_union,
{ apply congr_arg finset.card,
simp_rw [finset.ext_iff, finset.mem_union, ha, hb, hc],
tauto },
{ rw finset.disjoint_left,
intros z,
rw [hb, hc],
tauto },
{ apply_instance },
end
/-- An irreducible polynomial of prime degree with two non-real roots has full Galois group. -/
lemma gal_action_hom_bijective_of_prime_degree
{p : ℚ[X]} (p_irr : irreducible p) (p_deg : p.nat_degree.prime)
(p_roots : fintype.card (p.root_set ℂ) = fintype.card (p.root_set ℝ) + 2) :
function.bijective (gal_action_hom p ℂ) :=
begin
have h1 : fintype.card (p.root_set ℂ) = p.nat_degree,
{ simp_rw [root_set_def, finset.coe_sort_coe, fintype.card_coe],
rw [multiset.to_finset_card_of_nodup, ←nat_degree_eq_card_roots],
{ exact is_alg_closed.splits_codomain p },
{ exact nodup_roots ((separable_map (algebra_map ℚ ℂ)).mpr p_irr.separable) } },
have h2 : fintype.card p.gal = fintype.card (gal_action_hom p ℂ).range :=
fintype.card_congr (monoid_hom.of_injective (gal_action_hom_injective p ℂ)).to_equiv,
let conj := restrict p ℂ (complex.conj_ae.restrict_scalars ℚ),
refine ⟨gal_action_hom_injective p ℂ, λ x, (congr_arg (has_mem.mem x)
(show (gal_action_hom p ℂ).range = ⊤, from _)).mpr (subgroup.mem_top x)⟩,
apply equiv.perm.subgroup_eq_top_of_swap_mem,
{ rwa h1 },
{ rw h1,
convert prime_degree_dvd_card p_irr p_deg using 1,
convert h2.symm },
{ exact ⟨conj, rfl⟩ },
{ rw ← equiv.perm.card_support_eq_two,
apply nat.add_left_cancel,
rw [←p_roots, ←set.to_finset_card (root_set p ℝ), ←set.to_finset_card (root_set p ℂ)],
exact (card_complex_roots_eq_card_real_add_card_not_gal_inv p).symm },
end
/-- An irreducible polynomial of prime degree with 1-3 non-real roots has full Galois group. -/
lemma gal_action_hom_bijective_of_prime_degree'
{p : ℚ[X]} (p_irr : irreducible p) (p_deg : p.nat_degree.prime)
(p_roots1 : fintype.card (p.root_set ℝ) + 1 ≤ fintype.card (p.root_set ℂ))
(p_roots2 : fintype.card (p.root_set ℂ) ≤ fintype.card (p.root_set ℝ) + 3) :
function.bijective (gal_action_hom p ℂ) :=
begin
apply gal_action_hom_bijective_of_prime_degree p_irr p_deg,
let n := (gal_action_hom p ℂ (restrict p ℂ
(complex.conj_ae.restrict_scalars ℚ))).support.card,
have hn : 2 ∣ n :=
equiv.perm.two_dvd_card_support (by rw [←monoid_hom.map_pow, ←monoid_hom.map_pow,
show alg_equiv.restrict_scalars ℚ complex.conj_ae ^ 2 = 1,
from alg_equiv.ext complex.conj_conj, monoid_hom.map_one, monoid_hom.map_one]),
have key := card_complex_roots_eq_card_real_add_card_not_gal_inv p,
simp_rw [set.to_finset_card] at key,
rw [key, add_le_add_iff_left] at p_roots1 p_roots2,
rw [key, add_right_inj],
suffices : ∀ m : ℕ, 2 ∣ m → 1 ≤ m → m ≤ 3 → m = 2,
{ exact this n hn p_roots1 p_roots2 },
rintros m ⟨k, rfl⟩ h2 h3,
exact le_antisymm (nat.lt_succ_iff.mp (lt_of_le_of_ne h3 (show 2 * k ≠ 2 * 1 + 1,
from nat.two_mul_ne_two_mul_add_one))) (nat.succ_le_iff.mpr (lt_of_le_of_ne h2
(show 2 * 0 + 1 ≠ 2 * k, from nat.two_mul_ne_two_mul_add_one.symm))),
end
end rationals
end gal
end polynomial
|
f7b425cb828c3056e314670d97dd872110a710ab | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/tropical/basic.lean | 28206249952f0c5689dcdb9cb0be8a4da360ac7e | [
"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 | 16,316 | 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 algebra.group_power.order
import algebra.order.monoid.with_top
import algebra.smul_with_zero
import algebra.order.monoid.min_max
/-!
# Tropical algebraic structures
This file defines algebraic structures of the (min-)tropical numbers, up to the tropical semiring.
Some basic lemmas about conversion from the base type `R` to `tropical R` are provided, as
well as the expected implementations of tropical addition and tropical multiplication.
## Main declarations
* `tropical R`: The type synonym of the tropical interpretation of `R`.
If `[linear_order R]`, then addition on `R` is via `min`.
* `semiring (tropical R)`: A `linear_ordered_add_comm_monoid_with_top R`
induces a `semiring (tropical R)`. If one solely has `[linear_ordered_add_comm_monoid R]`,
then the "tropicalization of `R`" would be `tropical (with_top R)`.
## Implementation notes
The tropical structure relies on `has_top` and `min`. For the max-tropical numbers, use
`order_dual R`.
Inspiration was drawn from the implementation of `additive`/`multiplicative`/`opposite`,
where a type synonym is created with some barebones API, and quickly made irreducible.
Algebraic structures are provided with as few typeclass assumptions as possible, even though
most references rely on `semiring (tropical R)` for building up the whole theory.
## References followed
* https://arxiv.org/pdf/math/0408099.pdf
* https://www.mathenjeans.fr/sites/default/files/sujets/tropical_geometry_-_casagrande.pdf
-/
universes u v
variables (R : Type u)
/-- The tropicalization of a type `R`. -/
def tropical : Type u := R
variables {R}
namespace tropical
/-- Reinterpret `x : R` as an element of `tropical R`.
See `tropical.trop_equiv` for the equivalence.
-/
@[pp_nodot]
def trop : R → tropical R := id
/-- Reinterpret `x : tropical R` as an element of `R`.
See `tropical.trop_equiv` for the equivalence. -/
@[pp_nodot]
def untrop : tropical R → R := id
lemma trop_injective : function.injective (trop : R → tropical R) := λ _ _, id
lemma untrop_injective : function.injective (untrop : tropical R → R) := λ _ _, id
@[simp] lemma trop_inj_iff (x y : R) : trop x = trop y ↔ x = y := iff.rfl
@[simp] lemma untrop_inj_iff (x y : tropical R) : untrop x = untrop y ↔ x = y := iff.rfl
@[simp] lemma trop_untrop (x : tropical R) : trop (untrop x) = x := rfl
@[simp] lemma untrop_trop (x : R) : untrop (trop x) = x := rfl
lemma left_inverse_trop : function.left_inverse (trop : R → tropical R) untrop := trop_untrop
lemma right_inverse_trop : function.right_inverse (trop : R → tropical R) untrop := trop_untrop
attribute [irreducible] tropical
/-- Reinterpret `x : R` as an element of `tropical R`.
See `tropical.trop_order_iso` for the order-preserving equivalence. -/
def trop_equiv : R ≃ tropical R :=
{ to_fun := trop,
inv_fun := untrop,
left_inv := untrop_trop,
right_inv := trop_untrop }
@[simp]
lemma trop_equiv_coe_fn : (trop_equiv : R → tropical R) = trop := rfl
@[simp]
lemma trop_equiv_symm_coe_fn : (trop_equiv.symm : tropical R → R) = untrop := rfl
lemma trop_eq_iff_eq_untrop {x : R} {y} : trop x = y ↔ x = untrop y :=
trop_equiv.apply_eq_iff_eq_symm_apply
lemma untrop_eq_iff_eq_trop {x} {y : R} : untrop x = y ↔ x = trop y :=
trop_equiv.symm.apply_eq_iff_eq_symm_apply
lemma injective_trop : function.injective (trop : R → tropical R) := trop_equiv.injective
lemma injective_untrop : function.injective (untrop : tropical R → R) := trop_equiv.symm.injective
lemma surjective_trop : function.surjective (trop : R → tropical R) := trop_equiv.surjective
lemma surjective_untrop : function.surjective (untrop : tropical R → R) :=
trop_equiv.symm.surjective
instance [inhabited R] : inhabited (tropical R) := ⟨trop default⟩
/-- Recursing on a `x' : tropical R` is the same as recursing on an `x : R` reinterpreted
as a term of `tropical R` via `trop x`. -/
@[simp]
def trop_rec {F : Π (X : tropical R), Sort v} (h : Π X, F (trop X)) : Π X, F X :=
λ X, h (untrop X)
instance [decidable_eq R] : decidable_eq (tropical R) :=
λ x y, decidable_of_iff _ injective_untrop.eq_iff
section order
instance [has_le R] : has_le (tropical R) :=
{ le := λ x y, untrop x ≤ untrop y }
@[simp] lemma untrop_le_iff [has_le R] {x y : tropical R} :
untrop x ≤ untrop y ↔ x ≤ y := iff.rfl
instance decidable_le [has_le R] [decidable_rel ((≤) : R → R → Prop)] :
decidable_rel ((≤) : tropical R → tropical R → Prop) :=
λ x y, ‹decidable_rel (≤)› (untrop x) (untrop y)
instance [has_lt R] : has_lt (tropical R) :=
{ lt := λ x y, untrop x < untrop y }
@[simp] lemma untrop_lt_iff [has_lt R] {x y : tropical R} :
untrop x < untrop y ↔ x < y := iff.rfl
instance decidable_lt [has_lt R] [decidable_rel ((<) : R → R → Prop)] :
decidable_rel ((<) : tropical R → tropical R → Prop) :=
λ x y, ‹decidable_rel (<)› (untrop x) (untrop y)
instance [preorder R] : preorder (tropical R) :=
{ le_refl := λ _, le_rfl,
le_trans := λ _ _ _ h h', le_trans h h',
lt_iff_le_not_le := λ _ _, lt_iff_le_not_le,
..tropical.has_le,
..tropical.has_lt }
/-- Reinterpret `x : R` as an element of `tropical R`, preserving the order. -/
def trop_order_iso [preorder R] : R ≃o tropical R :=
{ map_rel_iff' := λ _ _, untrop_le_iff,
..trop_equiv }
@[simp]
lemma trop_order_iso_coe_fn [preorder R] : (trop_order_iso : R → tropical R) = trop := rfl
@[simp]
lemma trop_order_iso_symm_coe_fn [preorder R] : (trop_order_iso.symm : tropical R → R) = untrop :=
rfl
lemma trop_monotone [preorder R] : monotone (trop : R → tropical R) := λ _ _, id
lemma untrop_monotone [preorder R] : monotone (untrop : tropical R → R) := λ _ _, id
instance [partial_order R] : partial_order (tropical R) :=
{ le_antisymm := λ _ _ h h', untrop_injective (le_antisymm h h'),
..tropical.preorder }
instance [has_top R] : has_zero (tropical R) := ⟨trop ⊤⟩
instance [has_top R] : has_top (tropical R) := ⟨0⟩
@[simp] lemma untrop_zero [has_top R] : untrop (0 : tropical R) = ⊤ := rfl
@[simp] lemma trop_top [has_top R] : trop (⊤ : R) = 0 := rfl
@[simp] lemma trop_coe_ne_zero (x : R) : trop (x : with_top R) ≠ 0 .
@[simp] lemma zero_ne_trop_coe (x : R) : (0 : tropical (with_top R)) ≠ trop x .
@[simp] lemma le_zero [has_le R] [order_top R] (x : tropical R) : x ≤ 0 := le_top
instance [has_le R] [order_top R] : order_top (tropical R) :=
{ le_top := λ _, le_top,
..tropical.has_top }
variable [linear_order R]
/-- Tropical addition is the minimum of two underlying elements of `R`. -/
instance : has_add (tropical R) :=
⟨λ x y, trop (min (untrop x) (untrop y))⟩
instance : add_comm_semigroup (tropical R) :=
{ add := (+),
add_assoc := λ _ _ _, untrop_injective (min_assoc _ _ _),
add_comm := λ _ _, untrop_injective (min_comm _ _) }
@[simp] lemma untrop_add (x y : tropical R) : untrop (x + y) = min (untrop x) (untrop y) := rfl
@[simp] lemma trop_min (x y : R) : trop (min x y) = trop x + trop y := rfl
@[simp] lemma trop_inf (x y : R) : trop (x ⊓ y) = trop x + trop y := rfl
lemma trop_add_def (x y : tropical R) : x + y = trop (min (untrop x) (untrop y)) := rfl
instance : linear_order (tropical R) :=
{ le_total := λ a b, le_total (untrop a) (untrop b),
decidable_le := tropical.decidable_le,
decidable_lt := tropical.decidable_lt,
decidable_eq := tropical.decidable_eq,
max := λ a b, trop (max (untrop a) (untrop b)),
max_def := begin
ext x y,
rw [max_default, max_def, apply_ite trop, trop_untrop, trop_untrop,
if_congr untrop_le_iff rfl rfl],
end,
min := (+),
min_def := begin
ext x y,
rw [trop_add_def, min_default, min_def, apply_ite trop, trop_untrop, trop_untrop,
if_congr untrop_le_iff rfl rfl],
end,
..tropical.partial_order }
@[simp] lemma untrop_sup (x y : tropical R) : untrop (x ⊔ y) = untrop x ⊔ untrop y := rfl
@[simp] lemma untrop_max (x y : tropical R) : untrop (max x y) = max (untrop x) (untrop y) := rfl
@[simp] lemma min_eq_add : (min : tropical R → tropical R → tropical R) = (+) := rfl
@[simp] lemma inf_eq_add : ((⊓) : tropical R → tropical R → tropical R) = (+) := rfl
lemma trop_max_def (x y : tropical R) : max x y = trop (max (untrop x) (untrop y)) := rfl
lemma trop_sup_def (x y : tropical R) : x ⊔ y = trop (untrop x ⊔ untrop y) := rfl
@[simp] lemma add_eq_left ⦃x y : tropical R⦄ (h : x ≤ y) :
x + y = x := untrop_injective (by simpa using h)
@[simp] lemma add_eq_right ⦃x y : tropical R⦄ (h : y ≤ x) :
x + y = y := untrop_injective (by simpa using h)
lemma add_eq_left_iff {x y : tropical R} : x + y = x ↔ x ≤ y :=
by rw [trop_add_def, trop_eq_iff_eq_untrop, ←untrop_le_iff, min_eq_left_iff]
lemma add_eq_right_iff {x y : tropical R} : x + y = y ↔ y ≤ x :=
by rw [trop_add_def, trop_eq_iff_eq_untrop, ←untrop_le_iff, min_eq_right_iff]
@[simp] lemma add_self (x : tropical R) : x + x = x := untrop_injective (min_eq_right le_rfl)
@[simp] lemma bit0 (x : tropical R) : bit0 x = x := add_self x
lemma add_eq_iff {x y z : tropical R} :
x + y = z ↔ x = z ∧ x ≤ y ∨ y = z ∧ y ≤ x :=
by { rw [trop_add_def, trop_eq_iff_eq_untrop], simp [min_eq_iff] }
@[simp] lemma add_eq_zero_iff {a b : tropical (with_top R)} :
a + b = 0 ↔ a = 0 ∧ b = 0 :=
begin
rw add_eq_iff,
split,
{ rintro (⟨rfl, h⟩|⟨rfl, h⟩),
{ exact ⟨rfl, le_antisymm (le_zero _) h⟩ },
{ exact ⟨le_antisymm (le_zero _) h, rfl⟩ } },
{ rintro ⟨rfl, rfl⟩,
simp }
end
instance [order_top R] : add_comm_monoid (tropical R) :=
{ zero_add := λ _, untrop_injective (min_top_left _),
add_zero := λ _, untrop_injective (min_top_right _),
..tropical.has_zero,
..tropical.add_comm_semigroup }
end order
section monoid
/-- Tropical multiplication is the addition in the underlying `R`. -/
instance [has_add R] : has_mul (tropical R) :=
⟨λ x y, trop (untrop x + untrop y)⟩
@[simp] lemma trop_add [has_add R] (x y : R) :
trop (x + y) = trop x * trop y := rfl
@[simp] lemma untrop_mul [has_add R] (x y : tropical R) :
untrop (x * y) = untrop x + untrop y := rfl
lemma trop_mul_def [has_add R] (x y : tropical R) :
x * y = trop (untrop x + untrop y) := rfl
instance [has_zero R] : has_one (tropical R) := ⟨trop 0⟩
@[simp] lemma trop_zero [has_zero R] : trop (0 : R) = 1 := rfl
@[simp] lemma untrop_one [has_zero R] : untrop (1 : tropical R) = 0 := rfl
instance [linear_order R] [order_top R] [has_zero R] : add_monoid_with_one (tropical R) :=
{ nat_cast := λ n, if n = 0 then 0 else 1,
nat_cast_zero := rfl,
nat_cast_succ := λ n, (untrop_inj_iff _ _).1 (by cases n; simp [nat.cast]),
.. tropical.has_one, .. tropical.add_comm_monoid }
instance [has_zero R] : nontrivial (tropical (with_top R)) :=
⟨⟨0, 1, trop_injective.ne with_top.top_ne_coe⟩⟩
instance [has_neg R] : has_inv (tropical R) := ⟨λ x, trop (- untrop x)⟩
@[simp] lemma untrop_inv [has_neg R] (x : tropical R) : untrop x⁻¹ = - untrop x := rfl
instance [has_sub R] : has_div (tropical R) := ⟨λ x y, trop (untrop x - untrop y)⟩
@[simp] lemma untrop_div [has_sub R] (x y : tropical R) :
untrop (x / y) = untrop x - untrop y := rfl
instance [add_semigroup R] : semigroup (tropical R) :=
{ mul := (*),
mul_assoc := λ _ _ _, untrop_injective (add_assoc _ _ _) }
instance [add_comm_semigroup R] : comm_semigroup (tropical R) :=
{ mul_comm := λ _ _, untrop_injective (add_comm _ _),
..tropical.semigroup }
instance {α : Type*} [has_smul α R] : has_pow (tropical R) α :=
{ pow := λ x n, trop $ n • untrop x }
@[simp] lemma untrop_pow {α : Type*} [has_smul α R] (x : tropical R) (n : α) :
untrop (x ^ n) = n • untrop x := rfl
@[simp] lemma trop_smul {α : Type*} [has_smul α R] (x : R) (n : α) :
trop (n • x) = trop x ^ n := rfl
instance [add_zero_class R] : mul_one_class (tropical R) :=
{ one := 1,
mul := (*),
one_mul := λ _, untrop_injective $ zero_add _,
mul_one := λ _, untrop_injective $ add_zero _ }
instance [add_monoid R] : monoid (tropical R) :=
{ npow := λ n x, x ^ n,
npow_zero' := λ _, untrop_injective $ zero_smul _ _,
npow_succ' := λ _ _, untrop_injective $ succ_nsmul _ _,
..tropical.mul_one_class,
..tropical.semigroup }
@[simp] lemma trop_nsmul [add_monoid R] (x : R) (n : ℕ) :
trop (n • x) = trop x ^ n := rfl
instance [add_comm_monoid R] : comm_monoid (tropical R) :=
{ ..tropical.monoid, ..tropical.comm_semigroup }
instance [add_group R] : group (tropical R) :=
{ inv := has_inv.inv,
mul_left_inv := λ _, untrop_injective $ add_left_neg _,
zpow := λ n x, trop $ n • untrop x,
zpow_zero' := λ _, untrop_injective $ zero_zsmul _,
zpow_succ' := λ _ _, untrop_injective $ add_group.zsmul_succ' _ _,
zpow_neg' := λ _ _, untrop_injective $ add_group.zsmul_neg' _ _,
..tropical.monoid }
instance [add_comm_group R] : comm_group (tropical R) :=
{ mul_comm := λ _ _, untrop_injective (add_comm _ _),
..tropical.group }
@[simp] lemma untrop_zpow [add_group R] (x : tropical R) (n : ℤ) :
untrop (x ^ n) = n • untrop x := rfl
@[simp] lemma trop_zsmul [add_group R] (x : R) (n : ℤ) :
trop (n • x) = trop x ^ n := rfl
end monoid
section distrib
instance covariant_mul [has_le R] [has_add R] [covariant_class R R (+) (≤)] :
covariant_class (tropical R) (tropical R) (*) (≤) :=
⟨λ x y z h, add_le_add_left h _⟩
instance covariant_swap_mul [has_le R] [has_add R] [covariant_class R R (function.swap (+)) (≤)] :
covariant_class (tropical R) (tropical R) (function.swap (*)) (≤) :=
⟨λ x y z h, add_le_add_right h _⟩
instance covariant_add [linear_order R] : covariant_class (tropical R) (tropical R) (+) (≤) :=
⟨λ x y z h, begin
cases le_total x y with hx hy,
{ rw [add_eq_left hx, add_eq_left (hx.trans h)] },
{ rw [add_eq_right hy],
cases le_total x z with hx hx,
{ rwa [add_eq_left hx] },
{ rwa [add_eq_right hx] } }
end⟩
instance covariant_mul_lt [has_lt R] [has_add R] [covariant_class R R (+) (<)] :
covariant_class (tropical R) (tropical R) (*) (<) :=
⟨λ x y z h, add_lt_add_left h _⟩
instance covariant_swap_mul_lt [preorder R] [has_add R]
[covariant_class R R (function.swap (+)) (<)] :
covariant_class (tropical R) (tropical R) (function.swap (*)) (<) :=
⟨λ x y z h, add_lt_add_right h _⟩
instance [linear_order R] [has_add R]
[covariant_class R R (+) (≤)] [covariant_class R R (function.swap (+)) (≤)] :
distrib (tropical R) :=
{ mul := (*),
add := (+),
left_distrib := λ _ _ _, untrop_injective (min_add_add_left _ _ _).symm,
right_distrib := λ _ _ _, untrop_injective (min_add_add_right _ _ _).symm }
@[simp] lemma add_pow [linear_order R] [add_monoid R]
[covariant_class R R (+) (≤)] [covariant_class R R (function.swap (+)) (≤)]
(x y : tropical R) (n : ℕ) :
(x + y) ^ n = x ^ n + y ^ n :=
begin
cases le_total x y with h h,
{ rw [add_eq_left h, add_eq_left (pow_le_pow_of_le_left' h _)] },
{ rw [add_eq_right h, add_eq_right (pow_le_pow_of_le_left' h _)] }
end
end distrib
section semiring
variable [linear_ordered_add_comm_monoid_with_top R]
instance : comm_semiring (tropical R) :=
{ zero_mul := λ _, untrop_injective (top_add _),
mul_zero := λ _, untrop_injective (add_top _),
..tropical.add_monoid_with_one,
..tropical.distrib,
..tropical.add_comm_monoid,
..tropical.comm_monoid }
@[simp] lemma succ_nsmul {R} [linear_order R] [order_top R] (x : tropical R) (n : ℕ) :
(n + 1) • x = x :=
begin
induction n with n IH,
{ simp },
{ rw [add_nsmul, IH, one_nsmul, add_self] }
end
-- TODO: find/create the right classes to make this hold (for enat, ennreal, etc)
-- Requires `zero_eq_bot` to be true
-- lemma add_eq_zero_iff {a b : tropical R} :
-- a + b = 1 ↔ a = 1 ∨ b = 1 := sorry
@[simp] lemma mul_eq_zero_iff {R : Type*} [linear_ordered_add_comm_monoid R]
{a b : tropical (with_top R)} :
a * b = 0 ↔ a = 0 ∨ b = 0 :=
by simp [←untrop_inj_iff, with_top.add_eq_top]
instance {R : Type*} [linear_ordered_add_comm_monoid R] :
no_zero_divisors (tropical (with_top R)) :=
⟨λ _ _, mul_eq_zero_iff.mp⟩
end semiring
end tropical
|
a10b502af762054ea54166a3a0ba7154c7cf8349 | 26ac254ecb57ffcb886ff709cf018390161a9225 | /src/algebra/category/CommRing/colimits.lean | 5ecf9fe26b6138f99251f3b30e7c1803bbd30369 | [
"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 | 12,690 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import algebra.category.CommRing.basic
/-!
# The category of commutative rings has all colimits.
This file uses a "pre-automated" approach, just as for `Mon/colimits.lean`.
It is a very uniform approach, that conceivably could be synthesised directly
by a tactic that analyses the shape of `comm_ring` and `ring_hom`.
-/
universes u v
open category_theory
open category_theory.limits
-- [ROBOT VOICE]:
-- You should pretend for now that this file was automatically generated.
-- It follows the same template as colimits in Mon.
/-
`#print comm_ring` says:
structure comm_ring : Type u → Type u
fields:
comm_ring.zero : Π (α : Type u) [c : comm_ring α], α
comm_ring.one : Π (α : Type u) [c : comm_ring α], α
comm_ring.neg : Π {α : Type u} [c : comm_ring α], α → α
comm_ring.add : Π {α : Type u} [c : comm_ring α], α → α → α
comm_ring.mul : Π {α : Type u} [c : comm_ring α], α → α → α
comm_ring.zero_add : ∀ {α : Type u} [c : comm_ring α] (a : α), 0 + a = a
comm_ring.add_zero : ∀ {α : Type u} [c : comm_ring α] (a : α), a + 0 = a
comm_ring.one_mul : ∀ {α : Type u} [c : comm_ring α] (a : α), 1 * a = a
comm_ring.mul_one : ∀ {α : Type u} [c : comm_ring α] (a : α), a * 1 = a
comm_ring.add_left_neg : ∀ {α : Type u} [c : comm_ring α] (a : α), -a + a = 0
comm_ring.add_comm : ∀ {α : Type u} [c : comm_ring α] (a b : α), a + b = b + a
comm_ring.mul_comm : ∀ {α : Type u} [c : comm_ring α] (a b : α), a * b = b * a
comm_ring.add_assoc : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a + b + c_1 = a + (b + c_1)
comm_ring.mul_assoc : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a * b * c_1 = a * (b * c_1)
comm_ring.left_distrib : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a * (b + c_1) = a * b + a * c_1
comm_ring.right_distrib : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), (a + b) * c_1 = a * c_1 + b * c_1
-/
namespace CommRing.colimits
/-!
We build the colimit of a diagram in `CommRing` by constructing the
free commutative ring on the disjoint union of all the commutative rings in the diagram,
then taking the quotient by the commutative ring laws within each commutative ring,
and the identifications given by the morphisms in the diagram.
-/
variables {J : Type v} [small_category J] (F : J ⥤ CommRing.{v})
/--
An inductive type representing all commutative ring expressions (without relations)
on a collection of types indexed by the objects of `J`.
-/
inductive prequotient
-- There's always `of`
| of : Π (j : J) (x : F.obj j), prequotient
-- Then one generator for each operation
| zero : prequotient
| one : prequotient
| neg : prequotient → prequotient
| add : prequotient → prequotient → prequotient
| mul : prequotient → prequotient → prequotient
instance : inhabited (prequotient F) := ⟨prequotient.zero⟩
open prequotient
/--
The relation on `prequotient` saying when two expressions are equal
because of the commutative ring laws, or
because one element is mapped to another by a morphism in the diagram.
-/
inductive relation : prequotient F → prequotient F → Prop
-- Make it an equivalence relation:
| refl : Π (x), relation x x
| symm : Π (x y) (h : relation x y), relation y x
| trans : Π (x y z) (h : relation x y) (k : relation y z), relation x z
-- There's always a `map` relation
| map : Π (j j' : J) (f : j ⟶ j') (x : F.obj j), relation (of j' (F.map f x)) (of j x)
-- Then one relation per operation, describing the interaction with `of`
| zero : Π (j), relation (of j 0) zero
| one : Π (j), relation (of j 1) one
| neg : Π (j) (x : F.obj j), relation (of j (-x)) (neg (of j x))
| add : Π (j) (x y : F.obj j), relation (of j (x + y)) (add (of j x) (of j y))
| mul : Π (j) (x y : F.obj j), relation (of j (x * y)) (mul (of j x) (of j y))
-- Then one relation per argument of each operation
| neg_1 : Π (x x') (r : relation x x'), relation (neg x) (neg x')
| add_1 : Π (x x' y) (r : relation x x'), relation (add x y) (add x' y)
| add_2 : Π (x y y') (r : relation y y'), relation (add x y) (add x y')
| mul_1 : Π (x x' y) (r : relation x x'), relation (mul x y) (mul x' y)
| mul_2 : Π (x y y') (r : relation y y'), relation (mul x y) (mul x y')
-- And one relation per axiom
| zero_add : Π (x), relation (add zero x) x
| add_zero : Π (x), relation (add x zero) x
| one_mul : Π (x), relation (mul one x) x
| mul_one : Π (x), relation (mul x one) x
| add_left_neg : Π (x), relation (add (neg x) x) zero
| add_comm : Π (x y), relation (add x y) (add y x)
| mul_comm : Π (x y), relation (mul x y) (mul y x)
| add_assoc : Π (x y z), relation (add (add x y) z) (add x (add y z))
| mul_assoc : Π (x y z), relation (mul (mul x y) z) (mul x (mul y z))
| left_distrib : Π (x y z), relation (mul x (add y z)) (add (mul x y) (mul x z))
| right_distrib : Π (x y z), relation (mul (add x y) z) (add (mul x z) (mul y z))
/--
The setoid corresponding to commutative expressions modulo monoid relations and identifications.
-/
def colimit_setoid : setoid (prequotient F) :=
{ r := relation F, iseqv := ⟨relation.refl, relation.symm, relation.trans⟩ }
attribute [instance] colimit_setoid
/--
The underlying type of the colimit of a diagram in `CommRing`.
-/
@[derive inhabited]
def colimit_type : Type v := quotient (colimit_setoid F)
instance : comm_ring (colimit_type F) :=
{ zero :=
begin
exact quot.mk _ zero
end,
one :=
begin
exact quot.mk _ one
end,
neg :=
begin
fapply @quot.lift,
{ intro x,
exact quot.mk _ (neg x) },
{ intros x x' r,
apply quot.sound,
exact relation.neg_1 _ _ r },
end,
add :=
begin
fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)),
{ intro x,
fapply @quot.lift,
{ intro y,
exact quot.mk _ (add x y) },
{ intros y y' r,
apply quot.sound,
exact relation.add_2 _ _ _ r } },
{ intros x x' r,
funext y,
induction y,
dsimp,
apply quot.sound,
{ exact relation.add_1 _ _ _ r },
{ refl } },
end,
mul :=
begin
fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)),
{ intro x,
fapply @quot.lift,
{ intro y,
exact quot.mk _ (mul x y) },
{ intros y y' r,
apply quot.sound,
exact relation.mul_2 _ _ _ r } },
{ intros x x' r,
funext y,
induction y,
dsimp,
apply quot.sound,
{ exact relation.mul_1 _ _ _ r },
{ refl } },
end,
zero_add := λ x,
begin
induction x,
dsimp,
apply quot.sound,
apply relation.zero_add,
refl,
end,
add_zero := λ x,
begin
induction x,
dsimp,
apply quot.sound,
apply relation.add_zero,
refl,
end,
one_mul := λ x,
begin
induction x,
dsimp,
apply quot.sound,
apply relation.one_mul,
refl,
end,
mul_one := λ x,
begin
induction x,
dsimp,
apply quot.sound,
apply relation.mul_one,
refl,
end,
add_left_neg := λ x,
begin
induction x,
dsimp,
apply quot.sound,
apply relation.add_left_neg,
refl,
end,
add_comm := λ x y,
begin
induction x,
induction y,
dsimp,
apply quot.sound,
apply relation.add_comm,
refl,
refl,
end,
mul_comm := λ x y,
begin
induction x,
induction y,
dsimp,
apply quot.sound,
apply relation.mul_comm,
refl,
refl,
end,
add_assoc := λ x y z,
begin
induction x,
induction y,
induction z,
dsimp,
apply quot.sound,
apply relation.add_assoc,
refl,
refl,
refl,
end,
mul_assoc := λ x y z,
begin
induction x,
induction y,
induction z,
dsimp,
apply quot.sound,
apply relation.mul_assoc,
refl,
refl,
refl,
end,
left_distrib := λ x y z,
begin
induction x,
induction y,
induction z,
dsimp,
apply quot.sound,
apply relation.left_distrib,
refl,
refl,
refl,
end,
right_distrib := λ x y z,
begin
induction x,
induction y,
induction z,
dsimp,
apply quot.sound,
apply relation.right_distrib,
refl,
refl,
refl,
end, }
@[simp] lemma quot_zero : quot.mk setoid.r zero = (0 : colimit_type F) := rfl
@[simp] lemma quot_one : quot.mk setoid.r one = (1 : colimit_type F) := rfl
@[simp] lemma quot_neg (x) : quot.mk setoid.r (neg x) = (-(quot.mk setoid.r x) : colimit_type F) := rfl
@[simp] lemma quot_add (x y) : quot.mk setoid.r (add x y) = ((quot.mk setoid.r x) + (quot.mk setoid.r y) : colimit_type F) := rfl
@[simp] lemma quot_mul (x y) : quot.mk setoid.r (mul x y) = ((quot.mk setoid.r x) * (quot.mk setoid.r y) : colimit_type F) := rfl
/-- The bundled commutative ring giving the colimit of a diagram. -/
def colimit : CommRing := CommRing.of (colimit_type F)
/-- The function from a given commutative ring in the diagram to the colimit commutative ring. -/
def cocone_fun (j : J) (x : F.obj j) : colimit_type F :=
quot.mk _ (of j x)
/-- The ring homomorphism from a given commutative ring in the diagram to the colimit commutative ring. -/
def cocone_morphism (j : J) : F.obj j ⟶ colimit F :=
{ to_fun := cocone_fun F j,
map_one' := by apply quot.sound; apply relation.one,
map_mul' := by intros; apply quot.sound; apply relation.mul,
map_zero' := by apply quot.sound; apply relation.zero,
map_add' := by intros; apply quot.sound; apply relation.add }
@[simp] lemma cocone_naturality {j j' : J} (f : j ⟶ j') :
F.map f ≫ (cocone_morphism F j') = cocone_morphism F j :=
begin
ext,
apply quot.sound,
apply relation.map,
end
@[simp] lemma cocone_naturality_components (j j' : J) (f : j ⟶ j') (x : F.obj j):
(cocone_morphism F j') (F.map f x) = (cocone_morphism F j) x :=
by { rw ←cocone_naturality F f, refl }
/-- The cocone over the proposed colimit commutative ring. -/
def colimit_cocone : cocone F :=
{ X := colimit F,
ι :=
{ app := cocone_morphism F } }.
/-- The function from the free commutative ring on the diagram to the cone point of any other cocone. -/
@[simp] def desc_fun_lift (s : cocone F) : prequotient F → s.X
| (of j x) := (s.ι.app j) x
| zero := 0
| one := 1
| (neg x) := -(desc_fun_lift x)
| (add x y) := desc_fun_lift x + desc_fun_lift y
| (mul x y) := desc_fun_lift x * desc_fun_lift y
/-- The function from the colimit commutative ring to the cone point of any other cocone. -/
def desc_fun (s : cocone F) : colimit_type F → s.X :=
begin
fapply quot.lift,
{ exact desc_fun_lift F s },
{ intros x y r,
induction r; try { dsimp },
-- refl
{ refl },
-- symm
{ exact r_ih.symm },
-- trans
{ exact eq.trans r_ih_h r_ih_k },
-- map
{ simp, },
-- zero
{ simp, },
-- one
{ simp, },
-- neg
{ simp, },
-- add
{ simp, },
-- mul
{ simp, },
-- neg_1
{ rw r_ih, },
-- add_1
{ rw r_ih, },
-- add_2
{ rw r_ih, },
-- mul_1
{ rw r_ih, },
-- mul_2
{ rw r_ih, },
-- zero_add
{ rw zero_add, },
-- add_zero
{ rw add_zero, },
-- one_mul
{ rw one_mul, },
-- mul_one
{ rw mul_one, },
-- add_left_neg
{ rw add_left_neg, },
-- add_comm
{ rw add_comm, },
-- mul_comm
{ rw mul_comm, },
-- add_assoc
{ rw add_assoc, },
-- mul_assoc
{ rw mul_assoc, },
-- left_distrib
{ rw left_distrib, },
-- right_distrib
{ rw right_distrib, },
}
end
/-- The ring homomorphism from the colimit commutative ring to the cone point of any other cocone. -/
@[simps]
def desc_morphism (s : cocone F) : colimit F ⟶ s.X :=
{ to_fun := desc_fun F s,
map_one' := rfl,
map_zero' := rfl,
map_add' := λ x y, by { induction x; induction y; refl },
map_mul' := λ x y, by { induction x; induction y; refl }, }
/-- Evidence that the proposed colimit is the colimit. -/
def colimit_is_colimit : is_colimit (colimit_cocone F) :=
{ desc := λ s, desc_morphism F s,
uniq' := λ s m w,
begin
ext,
induction x,
induction x,
{ have w' := congr_fun (congr_arg (λ f : F.obj x_j ⟶ s.X, (f : F.obj x_j → s.X)) (w x_j)) x_x,
erw w',
refl, },
{ simp, },
{ simp, },
{ simp *, },
{ simp *, },
{ simp *, },
refl
end }.
instance has_colimits_CommRing : has_colimits CommRing :=
{ has_colimits_of_shape := λ J 𝒥,
{ has_colimit := λ F, by exactI
{ cocone := colimit_cocone F,
is_colimit := colimit_is_colimit F } } }
end CommRing.colimits
|
92754527972113339495e9887757922a91a5ff1d | ebbdcbd7ddc89a9ef7c3b397b301d5f5272a918f | /qp/p1_categories/c8_fibered_cats/s1_fibration.lean | 609584c095dc801cb8446ab048756928a900a980 | [] | no_license | intoverflow/qvr | 34b9ef23604738381ca20b7d622fd0399d88f2dd | 0cfcd33fe4bf8d93851a00cec5bfd21e77105d74 | refs/heads/master | 1,616,591,570,371 | 1,492,575,772,000 | 1,492,575,772,000 | 80,061,627 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,823 | lean | /- -----------------------------------------------------------------------
Fibrations.
----------------------------------------------------------------------- -/
import ..c1_basic
import ..c2_limits
import ..c3_wtypes
import ..c4_topoi
import ..c7_cat_of_cats
namespace qp
open stdaux
universe variables ℓobjb ℓhomb ℓobj ℓhom
/- -----------------------------------------------------------------------
Cartesian homs.
----------------------------------------------------------------------- -/
/-! #brief A cartesian hom.
-/
structure CartesianHom {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}}
(P : Fun E B)
{e₁ e₂ : E^.obj} (φ : E^.hom e₁ e₂)
:= (char : ∀ {e₀ : E^.obj} (ψ : E^.hom e₀ e₂)
(g : B^.hom (P^.obj e₀) (P^.obj e₁))
(ω : P^.hom φ ∘∘ g = P^.hom ψ)
, E^.hom e₀ e₁)
(comm : ∀ {e₀ : E^.obj} (ψ : E^.hom e₀ e₂)
(g : B^.hom (P^.obj e₀) (P^.obj e₁))
(ω : P^.hom φ ∘∘ g = P^.hom ψ)
, ψ = φ ∘∘ char ψ g ω)
(char_im : ∀ {e₀ : E^.obj} (ψ : E^.hom e₀ e₂)
(g : B^.hom (P^.obj e₀) (P^.obj e₁))
(ω : P^.hom φ ∘∘ g = P^.hom ψ)
, P^.hom (char ψ g ω) = g)
(uniq : ∀ {e₀ : E^.obj} (ψ : E^.hom e₀ e₂)
(g : B^.hom (P^.obj e₀) (P^.obj e₁))
(ω : P^.hom φ ∘∘ g = P^.hom ψ)
(char' : E^.hom e₀ e₁)
(ωcomm : ψ = φ ∘∘ char')
(ωim : P^.hom char' = g)
, char' = char ψ g ω)
/-! #brief A cartesian lift.
-/
structure CartesianLift {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}}
(P : Fun E B)
{b : B^.obj} {e : E^.obj} (f : B^.hom b (P^.obj e))
: Type (max ℓobj ℓhom ℓobjb ℓhomb)
:= (obj : E^.obj)
(obj_im : P^.obj obj = b)
(hom : E^.hom obj e)
(hom_im : P^.hom hom = f ∘∘ cast_hom obj_im)
(hom_cart : CartesianHom P hom)
/- -----------------------------------------------------------------------
Fibrations and cloven functors.
----------------------------------------------------------------------- -/
/-! #brief A fibration.
-/
class Fibration {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}}
(P : Fun E B)
: Type (max ℓobj ℓhom ℓobjb ℓhomb)
:= (lift : ∀ {b : B^.obj} {e : E^.obj} (f : B^.hom b (P^.obj e))
, MerelyExists (CartesianLift P f))
/-! #brief A cloven functor.
-/
structure Cloven {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}}
(P : Fun E B)
: Type (max ℓobj ℓhom ℓobjb ℓhomb)
:= (lift : ∀ {b : B^.obj} {e : E^.obj} (f : B^.hom b (P^.obj e))
, CartesianLift P f)
/-! #brief Every cloven functor is a fibration.
-/
definition Cloven.Fibration {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}}
(P : Fun E B)
(P_Cloven : Cloven P)
: Fibration P
:= { lift := λ b e f, MerelyExists.intro (P_Cloven^.lift f)}
/-! #brief With the axiom of choice, every fibration is cloven.
-/
noncomputable definition Fibration.Cloven {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}}
(P : Fun E B)
[P_Fibration : Fibration P]
: Cloven P
:= { lift := λ b e f, MerelyExists.choice (Fibration.lift f)}
/- -----------------------------------------------------------------------
The codomain fibration.
----------------------------------------------------------------------- -/
/-! #brief The codomain fibration.
-/
definition CodomFib (C : Cat.{ℓobj ℓhom})
: Fun (ArrCat C) C
:= { obj := λ arr, arr^.codom
, hom := λ arr₁ arr₂ f, f^.hom_codom
, hom_id := λ arr₁, rfl
, hom_circ := λ arr₁ arr₂ arr₃ g f, rfl
}
/-! #brief If a category has pullbacks, CodomFib has Cartesian lifts.
-/
definition CodomFib.CartesianLift (C : Cat.{ℓobj ℓhom})
[C_HasAllPullbacks : HasAllPullbacks C]
{b : C^.obj} {e : (ArrCat C)^.obj} (f : C^.hom b ((CodomFib C)^.obj e))
: CartesianLift (CodomFib C) f
:= { obj := { dom := pullback C (f ↗→ e^.hom ↗→↗)
, codom := b
, hom := pullback.π C (f ↗→ e^.hom ↗→↗) (@fin_of 1 0)
}
, obj_im := rfl
, hom := { hom_dom := pullback.π C (f ↗→ e^.hom ↗→↗) (@fin_of 0 1)
, hom_codom := f
, comm := by apply pullback.π_comm C (f ↗→ e^.hom ↗→↗) (@fin_of 1 0) (@fin_of 0 1)
}
, hom_im := eq.symm C^.circ_id_right
, hom_cart
:= let pcone : ∀ (e₀ : Arr C)
(ψ : ArrHom C e₀ e)
(g : C^.hom e₀^.codom b)
(ω : f ∘∘ g = ψ^.hom_codom)
, PullbackCone C (f↗→(e^.hom)↗→↗)
:= λ e₀ ψ g ω
, PullbackCone.mk (f↗→(e^.hom)↗→↗) e₀^.dom
(ψ^.hom_codom ∘∘ e₀^.hom)
((g ∘∘ e₀^.hom) ↗← ψ^.hom_dom ↗←↗)
begin
apply HomsList.eq, { rw [C^.circ_assoc, ω] },
apply HomsList.eq, { exact ψ^.comm },
trivial
end
in { char := λ e₀ ψ (g : C^.hom e₀^.codom b) (ω : f ∘∘ g = ψ^.hom_codom)
, { hom_dom := pullback.univ C (f↗→(e^.hom)↗→↗) (pcone e₀ ψ g ω)
, hom_codom := g
, comm := pullback.univ.mediates C (f↗→(e^.hom)↗→↗) (pcone e₀ ψ g ω) (@fin_of 1 0)
}
, comm := λ e₀ ψ (g : C^.hom e₀^.codom b) (ω : f ∘∘ g = ψ^.hom_codom)
, ArrHom.eq
sorry
(eq.trans (eq.symm ω) rfl)
, char_im := λ e₀ ψ (g : C^.hom e₀^.codom b) (ω : f ∘∘ g = ψ^.hom_codom)
, rfl
, uniq := λ e₀ ψ (g : C^.hom e₀^.codom b) (ω : f ∘∘ g = ψ^.hom_codom)
char' ωcomm ωim
, ArrHom.eq
(pullback.univ.uniq C (f↗→(e^.hom)↗→↗) (pcone e₀ ψ g ω)
char'^.hom_dom
(λ n, begin
cases n with n ωn,
cases n with n,
{ exact sorry },
cases n with n,
{ exact sorry },
cases ωn with _ ωn,
cases ωn with _ ωn,
cases ωn
end)
begin
exact sorry
end)
ωim
}
}
/-! #brief If a category has pullbacks, CodomFib is cloven.
-/
definition CodomFib.Cloven (C : Cat.{ℓobj ℓhom})
[C_HasAllPullbacks : HasAllPullbacks C]
: Cloven (CodomFib C)
:= { lift := λ b e f, CodomFib.CartesianLift C f
}
/-! #brief If a category has pullbacks. CodomFib is a fibration.
-/
instance CodomFib.Fibration (C : Cat.{ℓobj ℓhom})
[C_HasAllPullbacks : HasAllPullbacks C]
: Fibration (CodomFib C)
:= Cloven.Fibration (CodomFib C) (CodomFib.Cloven C)
/- -----------------------------------------------------------------------
Fibrations are Conduché.
----------------------------------------------------------------------- -/
-- /-! #brief Fibrations can factorize.
-- -/
-- definition Fibration.factorize {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}}
-- (P : Fun E B)
-- (P_Fibration : Fibration P)
-- {a c : ⟦E⟧} (f : ⟦E : a →→ c⟧) {b : ⟦B⟧} (h : ⟦B : b →→ P^.obj c⟧)
-- (g : ⟦B : P^.obj a →→ b⟧) (ω : P^.hom f = h ∘∘ g)
-- : ConducheFact P f h g ω
-- := let cart := (P_Fibration^.lift h)^.hom_cart
-- in let g' := cast_hom (eq.symm (P_Fibration^.lift h)^.obj_im) ∘∘ g
-- in let ωchar : P^.hom ((P_Fibration^.lift h)^.hom) ∘∘ g' = P^.hom f
-- := begin
-- rw (P_Fibration^.lift h)^.hom_im,
-- refine eq.trans _ (eq.symm ω),
-- rw B^.circ_assoc,
-- apply Cat.circ.congr_left,
-- rw -B^.circ_assoc,
-- rw cast_hom.circ,
-- rw cast_hom.simp,
-- exact B^.circ_id_right
-- end
-- in ConducheFact.mk ω
-- (P_Fibration^.lift h)^.obj
-- (P_Fibration^.lift h)^.obj_im
-- (P_Fibration^.lift h)^.hom
-- (P_Fibration^.lift h)^.hom_im
-- (cart^.char f g' ωchar)
-- (cart^.char_im _ _ _)
-- (cart^.comm _ _ _)
-- /-! #brief Fibrations can factorize.
-- -/
-- definition Fibration.zigzag_in {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}}
-- (P : Fun E B)
-- (P_Fibration : Fibration P)
-- {a c : ⟦E⟧} {f : ⟦E : a →→ c⟧} {b : ⟦B⟧} {h : ⟦B : b →→ P^.obj c⟧}
-- {g : ⟦B : P^.obj a →→ b⟧} {ω : P^.hom f = h ∘∘ g}
-- (fac : ConducheFact P f h g ω)
-- : E^.hom fac^.obj (Fibration.factorize P P_Fibration f h g ω)^.obj
-- := let cart := (P_Fibration^.lift h)^.hom_cart
-- in cart^.char fac^.left (cast_hom (eq.trans fac^.obj_im (eq.symm (P_Fibration^.lift h)^.obj_im)))
-- begin
-- rw (P_Fibration^.lift h)^.hom_im,
-- refine eq.trans _ (eq.symm fac^.left_im),
-- rw -B^.circ_assoc,
-- apply Cat.circ.congr_right,
-- rw cast_hom.circ
-- end
-- /-! #brief Fibrations can factorize.
-- -/
-- definition Fibration.zigzag_out {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}}
-- (P : Fun E B)
-- (P_Fibration : Fibration P)
-- {a c : ⟦E⟧} {f : ⟦E : a →→ c⟧} {b : ⟦B⟧} {h : ⟦B : b →→ P^.obj c⟧}
-- {g : ⟦B : P^.obj a →→ b⟧} {ω : P^.hom f = h ∘∘ g}
-- (fac : ConducheFact P f h g ω)
-- : E^.hom (Fibration.factorize P P_Fibration f h g ω)^.obj fac^.obj
-- := let f₂ : ⟦E : (P_Fibration^.lift (cast_hom (eq.symm (fac^.obj_im)) ∘∘ g))^.obj →→ fac^.obj⟧
-- := (P_Fibration^.lift (P^.hom fac^.right))^.hom ∘∘ cast_hom begin rw fac^.right_im end
-- in let baz := (P_Fibration^.lift (cast_hom (eq.symm (fac^.obj_im)) ∘∘ g))^.hom_cart
-- in let bar := @CartesianHom.char _ _ _ _ _ _ baz
-- in let moo := (P_Fibration^.lift (cast_hom (eq.symm (fac^.obj_im)) ∘∘ g))^.obj_im
-- --in let moo := @CartesianHom.char _ _ _ _ _ _ (P_Fibration^.lift h)^.hom_cart
-- in f₂ ∘∘
-- begin
-- dsimp [Fibration.factorize],
-- exact sorry
-- end
-- /-! #brief Fibrations can factorize.
-- -/
-- definition Fibration.zigzag {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}}
-- (P : Fun E B)
-- (P_Fibration : Fibration P)
-- {a c : ⟦E⟧} {f : ⟦E : a →→ c⟧} {b : ⟦B⟧} {h : ⟦B : b →→ P^.obj c⟧}
-- {g : ⟦B : P^.obj a →→ b⟧} {ω : P^.hom f = h ∘∘ g}
-- (fac₁ fac₂ : ConducheFact P f h g ω)
-- : E^.hom fac₁^.obj fac₂^.obj
-- := E^.circ (Fibration.zigzag_out P P_Fibration fac₂)
-- (Fibration.zigzag_in P P_Fibration fac₁)
-- /-! #brief Fibrations are Conduché.
-- -/
-- definition Fibration.Conduche {E : Cat.{ℓobj ℓhom}} {B : Cat.{ℓobjb ℓhomb}}
-- (P : Fun E B)
-- (P_Fibration : Fibration P)
-- : Conduche P
-- := { factorize := @Fibration.factorize E B P P_Fibration
-- , zigzag := @Fibration.zigzag E B P P_Fibration
-- , zigzag_im := begin end
-- , zigzag_left := begin end
-- , zigzag_right := begin end
-- }
end qp
|
c58cb395f49bfd8b9fdb8a5932eea5fc4d849905 | 32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7 | /tests/lean/run/inductive1.lean | 7cdf2252a143a83858e5b4f89f50320349298448 | [
"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 | 1,161 | lean | new_frontend
inductive L1.{u} (α : Type u)
| nil
| cons : α → L1 α → L1 α
#check L1
#check @L1.cons
inductive L2.{u} (α : Type u)
| nil
| cons (head : α) (tail : L2 α)
#check @L2.cons
universes u v
variable (α : Type u)
inductive A (β : Type v)
| nil {}
| protected cons : α → β → A β → A β
#check @A.cons
#check A.nil Nat Bool
mutual
inductive isEven : Nat → Prop
| z : isEven 0
| s (n : Nat) : isOdd n → isEven (n+1)
inductive isOdd : Nat → Prop
| s (n : Nat) : isEven n → isOdd (n+1)
end
#check isEven
#check isOdd.s
#check @isEven.rec
inductive V (α : Type _) : Nat → Type _
| nil : V α 0
| cons {n : Nat} : α → V α n → V α (n+1)
#check @V.nil
#check @V.cons
#check @V.rec
#check @V.noConfusion
#check @V.brecOn
#check @V.binductionOn
#check @V.casesOn
#check @V.recOn
#check @V.below
class inductive Dec (p : Prop) : Type
| isTrue (h : p)
| isFalse (h : Not p)
instance tst : Dec True :=
Dec.isTrue True.intro
#check tst
variable (β : Type _)
inductive T1
| mk : β → β → T1
#check @T1.mk
inductive MyEq {α : Type} (a : α) : α → Prop
| refl : MyEq a a
#check @MyEq.refl
|
06db665083a43ce3da07b9ee48ec1828bb9643b0 | 22e97a5d648fc451e25a06c668dc03ac7ed7bc25 | /test/norm_cast.lean | d6f510149f2c1a2c792581a5243a515c5455593f | [
"Apache-2.0"
] | permissive | keeferrowan/mathlib | f2818da875dbc7780830d09bd4c526b0764a4e50 | aad2dfc40e8e6a7e258287a7c1580318e865817e | refs/heads/master | 1,661,736,426,952 | 1,590,438,032,000 | 1,590,438,032,000 | 266,892,663 | 0 | 0 | Apache-2.0 | 1,590,445,835,000 | 1,590,445,835,000 | null | UTF-8 | Lean | false | false | 4,015 | lean | /-
Tests for norm_cast
-/
import tactic.norm_cast
import data.complex.basic -- ℕ, ℤ, ℚ, ℝ, ℂ
import data.real.ennreal
constants (an bn cn dn : ℕ) (az bz cz dz : ℤ) (aq bq cq dq : ℚ)
constants (ar br cr dr : ℝ) (ac bc cc dc : ℂ)
example : (an : ℤ) = bn → an = bn := by {intro h, exact_mod_cast h}
example : an = bn → (an : ℤ) = bn := by {intro h, exact_mod_cast h}
example : az = bz ↔ (az : ℚ) = bz := by norm_cast
example : (aq : ℝ) = br ↔ (aq : ℂ) = br := by norm_cast
example : (an : ℚ) = bz ↔ (an : ℂ) = bz := by norm_cast
example : (((an : ℤ) : ℚ) : ℝ) = bq ↔ ((an : ℚ) : ℂ) = (bq : ℝ) :=
by norm_cast
example : (an : ℤ) < bn ↔ an < bn := by norm_cast
example : (an : ℚ) < bz ↔ (an : ℝ) < bz := by norm_cast
example : ((an : ℤ) : ℝ) < bq ↔ (an : ℚ) < bq := by norm_cast
example : (an : ℤ) ≠ (bn : ℤ) ↔ an ≠ bn := by norm_cast
-- zero and one cause special problems
example : 0 < (bq : ℝ) ↔ 0 < bq := by norm_cast
example : az > (1 : ℕ) ↔ az > 1 := by norm_cast
example : az > (0 : ℕ) ↔ az > 0 := by norm_cast
example : (an : ℤ) ≠ 0 ↔ an ≠ 0 := by norm_cast
example : aq < (1 : ℕ) ↔ (aq : ℝ) < (1 : ℤ) := by norm_cast
example : (an : ℤ) + bn = (an + bn : ℕ) := by norm_cast
example : (an : ℂ) + bq = ((an + bq) : ℚ) := by norm_cast
example : (((an : ℤ) : ℚ) : ℝ) + bn = (an + (bn : ℤ)) := by norm_cast
example : (((((an : ℚ) : ℝ) * bq) + (cq : ℝ) ^ dn) : ℂ) = (an : ℂ) * (bq : ℝ) + cq ^ dn :=
by norm_cast
example : ((an : ℤ) : ℝ) < bq ∧ (cr : ℂ) ^ 2 = dz ↔ (an : ℚ) < bq ∧ ((cr ^ 2) : ℂ) = dz :=
by norm_cast
--testing numerals
example : ((42 : ℕ) : ℤ) = 42 := by norm_cast
example : ((42 : ℕ) : ℂ) = 42 := by norm_cast
example : ((42 : ℤ) : ℚ) = 42 := by norm_cast
example : ((42 : ℚ) : ℝ) = 42 := by norm_cast
example (h : (an : ℝ) = 0) : an = 0 := by exact_mod_cast h
example (h : (an : ℝ) = 42) : an = 42 := by exact_mod_cast h
example (h : (an + 42) ≠ 42) : (an : ℝ) + 42 ≠ 42 := by exact_mod_cast h
-- testing the heuristic
example (h : bn ≤ an) : an - bn = 1 ↔ (an - bn : ℤ) = 1 :=
by norm_cast
example (h : (cz : ℚ) = az / bz) : (cz : ℝ) = az / bz :=
by assumption_mod_cast
namespace hidden
def with_zero (α) := option α
variables {α : Type*}
instance : has_coe_t α (with_zero α) := ⟨some⟩
instance : has_zero (with_zero α) := ⟨none⟩
instance [has_one α]: has_one (with_zero α) := ⟨some 1⟩
instance [has_mul α] : mul_zero_class (with_zero α) :=
{ mul := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a * b)),
zero_mul := λ a, rfl,
mul_zero := λ a, by cases a; refl,
..with_zero.has_zero }
@[norm_cast] lemma coe_one [has_one α] : ((1 : α) : with_zero α) = 1 := rfl
@[norm_cast] lemma coe_inj {a b : α} : (a : with_zero α) = b ↔ a = b :=
option.some_inj
@[norm_cast] lemma mul_coe {α : Type*} [has_mul α] (a b : α) :
((a * b : α) : with_zero α) = (a : with_zero α) * b := rfl
example [has_mul α] [has_one α] (x y : α) (h : (x : with_zero α) * y = 1) : x*y = 1 :=
by exact_mod_cast h
end hidden
example (k : ℕ) {x y : ℕ} :
(x * x + y * y : ℤ) - ↑((x * y + 1) * k) = ↑y * ↑y - ↑k * ↑x * ↑y + (↑x * ↑x - ↑k) :=
begin
push_cast,
ring
end
example (k : ℕ) {x y : ℕ} (h : ((x + y + k : ℕ) : ℤ) = 0) : x + y + k = 0 :=
begin
push_cast at h,
guard_hyp h := (x : ℤ) + y + k = 0,
assumption_mod_cast
end
example {x : ℚ} : ((x + 42 : ℚ) : ℝ) = x + 42 := by push_cast
namespace ennreal
--TODO: debug
lemma half_lt_self_bis {a : ennreal} (hz : a ≠ 0) (ht : a ≠ ⊤) : a / 2 < a :=
begin
lift a to nnreal using ht,
have h : (2 : ennreal) = ((2 : nnreal) : ennreal), from rfl,
have h' : (2 : nnreal) ≠ 0, from _root_.two_ne_zero',
rw [h, ← coe_div h', coe_lt_coe], -- `norm_cast` fails to apply `coe_div`
norm_cast at hz,
exact nnreal.half_lt_self hz
end
end ennreal
|
c56519bfaefbded60f366e7671e474bfca136da0 | b2e508d02500f1512e1618150413e6be69d9db10 | /src/category_theory/isomorphism.lean | 61ebb2b3bf091451f2cd8de112baeaaa1f998ab7 | [
"Apache-2.0"
] | permissive | callum-sutton/mathlib | c3788f90216e9cd43eeffcb9f8c9f959b3b01771 | afd623825a3ac6bfbcc675a9b023edad3f069e89 | refs/heads/master | 1,591,371,888,053 | 1,560,990,690,000 | 1,560,990,690,000 | 192,476,045 | 0 | 0 | Apache-2.0 | 1,568,941,843,000 | 1,560,837,965,000 | Lean | UTF-8 | Lean | false | false | 9,414 | 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, Floris van Doorn
import category_theory.functor
universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation
namespace category_theory
open category
structure iso {C : Type u} [category.{v} C] (X Y : C) :=
(hom : X ⟶ Y)
(inv : Y ⟶ X)
(hom_inv_id' : hom ≫ inv = 𝟙 X . obviously)
(inv_hom_id' : inv ≫ hom = 𝟙 Y . obviously)
restate_axiom iso.hom_inv_id'
restate_axiom iso.inv_hom_id'
attribute [simp] iso.hom_inv_id iso.inv_hom_id
infixr ` ≅ `:10 := iso -- type as \cong or \iso
variables {C : Type u} [𝒞 : category.{v} C]
include 𝒞
variables {X Y Z : C}
namespace iso
@[simp] lemma hom_inv_id_assoc (α : X ≅ Y) (f : X ⟶ Z) : α.hom ≫ α.inv ≫ f = f :=
by rw [←category.assoc, α.hom_inv_id, category.id_comp]
@[simp] lemma inv_hom_id_assoc (α : X ≅ Y) (f : Y ⟶ Z) : α.inv ≫ α.hom ≫ f = f :=
by rw [←category.assoc, α.inv_hom_id, category.id_comp]
@[extensionality] lemma ext (α β : X ≅ Y) (w : α.hom = β.hom) : α = β :=
suffices α.inv = β.inv, by cases α; cases β; cc,
calc α.inv
= α.inv ≫ (β.hom ≫ β.inv) : by rw [iso.hom_inv_id, category.comp_id]
... = (α.inv ≫ α.hom) ≫ β.inv : by rw [category.assoc, ←w]
... = β.inv : by rw [iso.inv_hom_id, category.id_comp]
@[symm] def symm (I : X ≅ Y) : Y ≅ X :=
{ hom := I.inv,
inv := I.hom,
hom_inv_id' := I.inv_hom_id',
inv_hom_id' := I.hom_inv_id' }
@[simp] lemma symm_hom (α : X ≅ Y) : α.symm.hom = α.inv := rfl
@[simp] lemma symm_inv (α : X ≅ Y) : α.symm.inv = α.hom := rfl
@[simp] lemma symm_mk {X Y : C} (hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id) (inv_hom_id) :
iso.symm {hom := hom, inv := inv, hom_inv_id' := hom_inv_id, inv_hom_id' := inv_hom_id} =
{hom := inv, inv := hom, hom_inv_id' := inv_hom_id, inv_hom_id' := hom_inv_id} := rfl
@[refl] def refl (X : C) : X ≅ X :=
{ hom := 𝟙 X,
inv := 𝟙 X }
@[simp] lemma refl_hom (X : C) : (iso.refl X).hom = 𝟙 X := rfl
@[simp] lemma refl_inv (X : C) : (iso.refl X).inv = 𝟙 X := rfl
@[trans] def trans (α : X ≅ Y) (β : Y ≅ Z) : X ≅ Z :=
{ hom := α.hom ≫ β.hom,
inv := β.inv ≫ α.inv }
infixr ` ≪≫ `:80 := iso.trans -- type as `\ll \gg`.
@[simp] lemma trans_hom (α : X ≅ Y) (β : Y ≅ Z) : (α ≪≫ β).hom = α.hom ≫ β.hom := rfl
@[simp] lemma trans_inv (α : X ≅ Y) (β : Y ≅ Z) : (α ≪≫ β).inv = β.inv ≫ α.inv := rfl
@[simp] lemma trans_mk {X Y Z : C}
(hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id) (inv_hom_id)
(hom' : Y ⟶ Z) (inv' : Z ⟶ Y) (hom_inv_id') (inv_hom_id') (hom_inv_id'') (inv_hom_id'') :
iso.trans
{hom := hom, inv := inv, hom_inv_id' := hom_inv_id, inv_hom_id' := inv_hom_id}
{hom := hom', inv := inv', hom_inv_id' := hom_inv_id', inv_hom_id' := inv_hom_id'} =
{hom := hom ≫ hom', inv := inv' ≫ inv, hom_inv_id' := hom_inv_id'', inv_hom_id' := inv_hom_id''} :=
rfl
@[simp] lemma refl_symm (X : C) : (iso.refl X).hom = 𝟙 X := rfl
@[simp] lemma trans_symm (α : X ≅ Y) (β : Y ≅ Z) : (α ≪≫ β).inv = β.inv ≫ α.inv := rfl
lemma inv_comp_eq (α : X ≅ Y) {f : X ⟶ Z} {g : Y ⟶ Z} : α.inv ≫ f = g ↔ f = α.hom ≫ g :=
⟨λ H, by simp [H.symm], λ H, by simp [H]⟩
lemma eq_inv_comp (α : X ≅ Y) {f : X ⟶ Z} {g : Y ⟶ Z} : g = α.inv ≫ f ↔ α.hom ≫ g = f :=
(inv_comp_eq α.symm).symm
lemma comp_inv_eq (α : X ≅ Y) {f : Z ⟶ Y} {g : Z ⟶ X} : f ≫ α.inv = g ↔ f = g ≫ α.hom :=
⟨λ H, by simp [H.symm], λ H, by simp [H]⟩
lemma eq_comp_inv (α : X ≅ Y) {f : Z ⟶ Y} {g : Z ⟶ X} : g = f ≫ α.inv ↔ g ≫ α.hom = f :=
(comp_inv_eq α.symm).symm
lemma inv_eq_inv (f g : X ≅ Y) : f.inv = g.inv ↔ f.hom = g.hom :=
have ∀{X Y : C} (f g : X ≅ Y), f.hom = g.hom → f.inv = g.inv, from λ X Y f g h, by rw [ext _ _ h],
⟨this f.symm g.symm, this f g⟩
lemma hom_comp_eq_id (α : X ≅ Y) {f : Y ⟶ X} : α.hom ≫ f = 𝟙 X ↔ f = α.inv :=
by rw [←eq_inv_comp, comp_id]
lemma comp_hom_eq_id (α : X ≅ Y) {f : Y ⟶ X} : f ≫ α.hom = 𝟙 Y ↔ f = α.inv :=
by rw [←eq_comp_inv, id_comp]
lemma hom_eq_inv (α : X ≅ Y) (β : Y ≅ X) : α.hom = β.inv ↔ β.hom = α.inv :=
by { erw [inv_eq_inv α.symm β, eq_comm], refl }
end iso
/-- `is_iso` typeclass expressing that a morphism is invertible.
This contains the data of the inverse, but is a subsingleton type. -/
class is_iso (f : X ⟶ Y) :=
(inv : Y ⟶ X)
(hom_inv_id' : f ≫ inv = 𝟙 X . obviously)
(inv_hom_id' : inv ≫ f = 𝟙 Y . obviously)
def inv (f : X ⟶ Y) [is_iso f] := is_iso.inv f
namespace is_iso
@[simp] lemma hom_inv_id (f : X ⟶ Y) [is_iso f] : f ≫ category_theory.inv f = 𝟙 X :=
is_iso.hom_inv_id' f
@[simp] lemma inv_hom_id (f : X ⟶ Y) [is_iso f] : category_theory.inv f ≫ f = 𝟙 Y :=
is_iso.inv_hom_id' f
@[simp] lemma hom_inv_id_assoc {Z} (f : X ⟶ Y) [is_iso f] (g : X ⟶ Z) : f ≫ category_theory.inv f ≫ g = g :=
by rw [←category.assoc, hom_inv_id, category.id_comp]
@[simp] lemma inv_hom_id_assoc {Z} (f : X ⟶ Y) [is_iso f] (g : Y ⟶ Z) : category_theory.inv f ≫ f ≫ g = g :=
by rw [←category.assoc, inv_hom_id, category.id_comp]
instance (X : C) : is_iso (𝟙 X) :=
{ inv := 𝟙 X }
instance of_iso (f : X ≅ Y) : is_iso f.hom :=
{ inv := f.inv }
instance of_iso_inverse (f : X ≅ Y) : is_iso f.inv :=
{ inv := f.hom }
variables {f g : X ⟶ Y} {h : Y ⟶ Z}
instance inv_is_iso [is_iso f] : is_iso (category_theory.inv f) :=
{ inv := f,
hom_inv_id' := inv_hom_id f,
inv_hom_id' := hom_inv_id f }
instance comp_is_iso [is_iso f] [is_iso h] : is_iso (f ≫ h) :=
{ inv := category_theory.inv h ≫ category_theory.inv f,
hom_inv_id' := begin erw [category.assoc, hom_inv_id_assoc], exact hom_inv_id f, end,
inv_hom_id' := begin erw [category.assoc, inv_hom_id_assoc], exact inv_hom_id h, end }
@[simp] lemma inv_id : category_theory.inv (𝟙 X) = 𝟙 X := rfl
@[simp] lemma inv_comp [is_iso f] [is_iso h] :
category_theory.inv (f ≫ h) = category_theory.inv h ≫ category_theory.inv f := rfl
@[simp] lemma is_iso.inv_inv [is_iso f] : category_theory.inv (category_theory.inv f) = f := rfl
@[simp] lemma iso.inv_inv (f : X ≅ Y) :
category_theory.inv (f.inv) = f.hom := rfl
@[simp] lemma iso.inv_hom (f : X ≅ Y) :
category_theory.inv (f.hom) = f.inv := rfl
instance epi_of_iso (f : X ⟶ Y) [is_iso f] : epi f :=
{ left_cancellation := λ Z g h w,
-- This is an interesting test case for better rewrite automation.
by rw [←category.id_comp C g, ←category.id_comp C h, ←is_iso.inv_hom_id f, category.assoc, w, category.assoc] }
instance mono_of_iso (f : X ⟶ Y) [is_iso f] : mono f :=
{ right_cancellation := λ Z g h w,
by rw [←category.comp_id C g, ←category.comp_id C h, ←is_iso.hom_inv_id f, ←category.assoc, w, ←category.assoc] }
end is_iso
open is_iso
lemma eq_of_inv_eq_inv {f g : X ⟶ Y} [is_iso f] [is_iso g] (p : inv f = inv g) : f = g :=
begin
apply (cancel_epi (inv f)).1,
erw [inv_hom_id, p, inv_hom_id],
end
def as_iso (f : X ⟶ Y) [is_iso f] : X ≅ Y :=
{ hom := f, inv := inv f }
@[simp] lemma as_iso_hom (f : X ⟶ Y) [is_iso f] : (as_iso f).hom = f := rfl
@[simp] lemma as_iso_inv (f : X ⟶ Y) [is_iso f] : (as_iso f).inv = inv f := rfl
instance (f : X ⟶ Y) : subsingleton (is_iso f) :=
⟨λ a b,
suffices a.inv = b.inv, by cases a; cases b; congr; exact this,
show (@as_iso C _ _ _ f a).inv = (@as_iso C _ _ _ f b).inv,
by congr' 1; ext; refl⟩
lemma is_iso.inv_eq_inv {f g : X ⟶ Y} [is_iso f] [is_iso g] : inv f = inv g ↔ f = g :=
iso.inv_eq_inv (as_iso f) (as_iso g)
instance is_iso_comp (f : X ⟶ Y) (g : Y ⟶ Z) [is_iso f] [is_iso g] : is_iso (f ≫ g) :=
{ inv := inv g ≫ inv f }
instance is_iso_id : is_iso (𝟙 X) := { inv := 𝟙 X }
namespace functor
universes u₁ v₁ u₂ v₂
variables {D : Type u₂}
variables [𝒟 : category.{v₂} D]
include 𝒟
def map_iso (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : F.obj X ≅ F.obj Y :=
{ hom := F.map i.hom,
inv := F.map i.inv,
hom_inv_id' := by rw [←map_comp, iso.hom_inv_id, ←map_id],
inv_hom_id' := by rw [←map_comp, iso.inv_hom_id, ←map_id] }
@[simp] lemma map_iso_hom (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : (F.map_iso i).hom = F.map i.hom := rfl
@[simp] lemma map_iso_inv (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : (F.map_iso i).inv = F.map i.inv := rfl
instance (F : C ⥤ D) (f : X ⟶ Y) [is_iso f] : is_iso (F.map f) :=
{ ..(F.map_iso (as_iso f)) }
@[simp] lemma map_hom_inv (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] :
F.map f ≫ F.map (inv f) = 𝟙 (F.obj X) :=
by rw [←map_comp, is_iso.hom_inv_id, map_id]
@[simp] lemma map_inv_hom (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] :
F.map (inv f) ≫ F.map f = 𝟙 (F.obj Y) :=
by rw [←map_comp, is_iso.inv_hom_id, map_id]
end functor
end category_theory
namespace category_theory
variables {C : Type u} [𝒞 : category.{v+1} C]
include 𝒞
def Aut (X : C) := X ≅ X
attribute [extensionality Aut] iso.ext
instance {X : C} : group (Aut X) :=
by refine { one := iso.refl X,
inv := iso.symm,
mul := iso.trans, .. } ; obviously
end category_theory
|
463eb14b574df767168a224fe727eb4277d25074 | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/data/list/zip.lean | 588f39c5f1b83dbb40a73722c0de9754d301a848 | [
"Apache-2.0"
] | permissive | dexmagic/mathlib | ff48eefc56e2412429b31d4fddd41a976eb287ce | 7a5d15a955a92a90e1d398b2281916b9c41270b2 | refs/heads/master | 1,693,481,322,046 | 1,633,360,193,000 | 1,633,360,193,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 14,756 | 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
/-!
# zip & unzip
This file provides results about `list.zip_with`, `list.zip` and `list.unzip` (definitions are in
core Lean).
`zip_with f l₁ l₂` applies `f : α → β → γ` pointwise to a list `l₁ : list α` and `l₂ : list β`. It
applies, until one of the lists is exhausted. For example,
`zip_with f [0, 1, 2] [6.28, 31] = [f 0 6.28, f 1 31]`.
`zip` is `zip_with` applied to `prod.mk`. For example,
`zip [a₁, a₂] [b₁, b₂, b₃] = [(a₁, b₁), (a₂, b₂)]`.
`unzip` undoes `zip`. For example, `unzip [(a₁, b₁), (a₂, b₂)] = ([a₁, a₂], [b₁, b₂])`.
-/
universe u
open nat
namespace list
variables {α : Type u} {β γ δ : Type*}
@[simp] theorem zip_with_cons_cons (f : α → β → γ) (a : α) (b : β) (l₁ : list α) (l₂ : list β) :
zip_with f (a :: l₁) (b :: l₂) = f a b :: zip_with f l₁ l₂ := rfl
@[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_with_nil_left (f : α → β → γ) (l) : zip_with f [] l = [] := rfl
@[simp] theorem zip_with_nil_right (f : α → β → γ) (l) : zip_with f l [] = [] :=
by cases l; refl
@[simp] lemma zip_with_eq_nil_iff {f : α → β → γ} {l l'} :
zip_with f l l' = [] ↔ l = [] ∨ l' = [] :=
by { cases l; cases l'; simp }
@[simp] theorem zip_nil_left (l : list α) : zip ([] : list β) l = [] := rfl
@[simp] theorem zip_nil_right (l : list α) : zip l ([] : list β) = [] :=
zip_with_nil_right _ l
@[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_with (f : α → β → γ) : ∀ (l₁ : list α) (l₂ : list β),
length (zip_with f l₁ l₂) = min (length l₁) (length l₂)
| [] l₂ := rfl
| l₁ [] := by simp only [length, nat.min_zero, zip_with_nil_right]
| (a::l₁) (b::l₂) := by by simp [length, zip_cons_cons, length_zip_with l₁ l₂, min_add_add_right]
@[simp] theorem length_zip : ∀ (l₁ : list α) (l₂ : list β),
length (zip l₁ l₂) = min (length l₁) (length l₂) :=
length_zip_with _
lemma lt_length_left_of_zip_with {f : α → β → γ} {i : ℕ} {l : list α} {l' : list β}
(h : i < (zip_with f l l').length) :
i < l.length :=
by { rw [length_zip_with, lt_min_iff] at h, exact h.left }
lemma lt_length_right_of_zip_with {f : α → β → γ} {i : ℕ} {l : list α} {l' : list β}
(h : i < (zip_with f l l').length) :
i < l'.length :=
by { rw [length_zip_with, lt_min_iff] at h, exact h.right }
lemma lt_length_left_of_zip {i : ℕ} {l : list α} {l' : list β} (h : i < (zip l l').length) :
i < l.length :=
lt_length_left_of_zip_with h
lemma lt_length_right_of_zip {i : ℕ} {l : list α} {l' : list β} (h : i < (zip l l').length) :
i < l'.length :=
lt_length_right_of_zip_with h
theorem zip_append : ∀ {l₁ r₁ : list α} {l₂ r₂ : list β} (h : length l₁ = length l₂),
zip (l₁ ++ r₁) (l₂ ++ r₂) = zip l₁ l₂ ++ zip r₁ r₂
| [] r₁ l₂ 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₁) r₁ (b::l₂) 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]
@[simp] lemma zip_with_map {μ}
(f : γ → δ → μ) (g : α → γ) (h : β → δ) (as : list α) (bs : list β) :
zip_with f (as.map g) (bs.map h) =
zip_with (λ a b, f (g a) (h b)) as bs :=
begin
induction as generalizing bs,
{ simp },
{ cases bs; simp * }
end
lemma zip_with_map_left
(f : α → β → γ) (g : δ → α) (l : list δ) (l' : list β) :
zip_with f (l.map g) l' = zip_with (f ∘ g) l l' :=
by { convert (zip_with_map f g id l l'), exact eq.symm (list.map_id _) }
lemma zip_with_map_right
(f : α → β → γ) (l : list α) (g : δ → β) (l' : list δ) :
zip_with f l (l'.map g) = zip_with (λ x, f x ∘ g) l l' :=
by { convert (list.zip_with_map f id g l l'), exact eq.symm (list.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
lemma map_zip_with {δ : Type*} (f : α → β) (g : γ → δ → α) (l : list γ) (l' : list δ) :
map f (zip_with g l l') = zip_with (λ x y, f (g x y)) l l' :=
begin
induction l with hd tl hl generalizing l',
{ simp },
{ cases l',
{ simp },
{ simp [hl] } }
end
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]
theorem map_fst_zip : ∀ (l₁ : list α) (l₂ : list β),
l₁.length ≤ l₂.length →
map prod.fst (zip l₁ l₂) = l₁
| [] bs _ := rfl
| (a :: as) (b :: bs) h := by { simp at h, simp! * }
| (a :: as) [] h := by { simp at h, contradiction }
theorem map_snd_zip : ∀ (l₁ : list α) (l₂ : list β),
l₂.length ≤ l₁.length →
map prod.snd (zip l₁ l₂) = l₂
| _ [] _ := by { rw zip_nil_right, refl }
| [] (b :: bs) h := by { simp at h, contradiction }
| (a :: as) (b :: bs) h := by { simp at h, simp! * }
@[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 (nat.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)]
lemma zip_of_prod {l : list α} {l' : list β} {lp : list (α × β)}
(hl : lp.map prod.fst = l) (hr : lp.map prod.snd = l') :
lp = l.zip l' :=
by rw [←hl, ←hr, ←zip_unzip lp, ←unzip_left, ←unzip_right, zip_unzip, zip_unzip]
lemma map_prod_left_eq_zip {l : list α} (f : α → β) : l.map (λ x, (x, f x)) = l.zip (l.map f) :=
by { rw ←zip_map', congr, exact map_id _ }
lemma map_prod_right_eq_zip {l : list α} (f : α → β) : l.map (λ x, (f x, x)) = (l.map f).zip l :=
by { rw ←zip_map', congr, exact map_id _ }
lemma zip_with_comm (f : α → α → β) (comm : ∀ (x y : α), f x y = f y x)
(l l' : list α) :
zip_with f l l' = zip_with f l' l :=
begin
induction l with hd tl hl generalizing l',
{ simp },
{ cases l',
{ simp },
{ simp [comm, hl] } }
end
instance (f : α → α → β) [is_symm_op α β f] : is_symm_op (list α) (list β) (zip_with f) :=
⟨zip_with_comm f is_symm_op.symm_op⟩
@[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]
lemma nth_zip_with (f : α → β → γ) (l₁ : list α) (l₂ : list β) (i : ℕ) :
(zip_with f l₁ l₂).nth i = ((l₁.nth i).map f).bind (λ g, (l₂.nth i).map g) :=
begin
induction l₁ generalizing l₂ i,
{ simp [zip_with, (<*>)] },
{ cases l₂; simp only [zip_with, has_seq.seq, functor.map, nth, option.map_none'],
{ cases ((l₁_hd :: l₁_tl).nth i); refl },
{ cases i; simp only [option.map_some', nth, option.some_bind', *] } }
end
lemma nth_zip_with_eq_some {α β γ} (f : α → β → γ) (l₁ : list α) (l₂ : list β) (z : γ) (i : ℕ) :
(zip_with f l₁ l₂).nth i = some z ↔ ∃ x y, l₁.nth i = some x ∧ l₂.nth i = some y ∧ f x y = z :=
begin
induction l₁ generalizing l₂ i,
{ simp [zip_with] },
{ cases l₂; simp only [zip_with, nth, exists_false, and_false, false_and],
cases i; simp *, },
end
lemma nth_zip_eq_some (l₁ : list α) (l₂ : list β) (z : α × β) (i : ℕ) :
(zip l₁ l₂).nth i = some z ↔ l₁.nth i = some z.1 ∧ l₂.nth i = some z.2 :=
begin
cases z,
rw [zip, nth_zip_with_eq_some], split,
{ rintro ⟨x, y, h₀, h₁, h₂⟩, cc },
{ rintro ⟨h₀, h₁⟩, exact ⟨_,_,h₀,h₁,rfl⟩ }
end
@[simp] lemma nth_le_zip_with {f : α → β → γ} {l : list α} {l' : list β} {i : ℕ}
{h : i < (zip_with f l l').length} :
(zip_with f l l').nth_le i h =
f (l.nth_le i (lt_length_left_of_zip_with h)) (l'.nth_le i (lt_length_right_of_zip_with h)) :=
begin
rw [←option.some_inj, ←nth_le_nth, nth_zip_with_eq_some],
refine ⟨l.nth_le i (lt_length_left_of_zip_with h), l'.nth_le i (lt_length_right_of_zip_with h),
nth_le_nth _, _⟩,
simp only [←nth_le_nth, eq_self_iff_true, and_self]
end
@[simp] lemma nth_le_zip {l : list α} {l' : list β} {i : ℕ} {h : i < (zip l l').length} :
(zip l l').nth_le i h =
(l.nth_le i (lt_length_left_of_zip h), l'.nth_le i (lt_length_right_of_zip h)) :=
nth_le_zip_with
lemma mem_zip_inits_tails {l : list α} {init tail : list α} :
(init, tail) ∈ zip l.inits l.tails ↔ init ++ tail = l :=
begin
induction l generalizing init tail;
simp_rw [tails, inits, zip_cons_cons],
{ simp },
{ split; rw [mem_cons_iff, zip_map_left, mem_map, prod.exists],
{ rintros (⟨rfl, rfl⟩ | ⟨_, _, h, rfl, rfl⟩),
{ simp },
{ simp [l_ih.mp h], }, },
{ cases init,
{ simp },
{ intro h,
right,
use [init_tl, tail],
simp * at *, }, }, },
end
lemma map_uncurry_zip_eq_zip_with
(f : α → β → γ) (l : list α) (l' : list β) :
map (function.uncurry f) (l.zip l') = zip_with f l l' :=
begin
induction l with hd tl hl generalizing l',
{ simp },
{ cases l' with hd' tl',
{ simp },
{ simp [hl] } }
end
@[simp] lemma sum_zip_with_distrib_left {γ : Type*} [semiring γ]
(f : α → β → γ) (n : γ) (l : list α) (l' : list β) :
(l.zip_with (λ x y, n * f x y) l').sum = n * (l.zip_with f l').sum :=
begin
induction l with hd tl hl generalizing f n l',
{ simp },
{ cases l' with hd' tl',
{ simp, },
{ simp [hl, mul_add] } }
end
section distrib
/-! ### Operations that can be applied before or after a `zip_with` -/
variables (f : α → β → γ) (l : list α) (l' : list β) (n : ℕ)
lemma zip_with_distrib_take :
(zip_with f l l').take n = zip_with f (l.take n) (l'.take n) :=
begin
induction l with hd tl hl generalizing l' n,
{ simp },
{ cases l',
{ simp },
{ cases n,
{ simp },
{ simp [hl] } } }
end
lemma zip_with_distrib_drop :
(zip_with f l l').drop n = zip_with f (l.drop n) (l'.drop n) :=
begin
induction l with hd tl hl generalizing l' n,
{ simp },
{ cases l',
{ simp },
{ cases n,
{ simp },
{ simp [hl] } } }
end
lemma zip_with_distrib_tail :
(zip_with f l l').tail = zip_with f l.tail l'.tail :=
by simp_rw [←drop_one, zip_with_distrib_drop]
lemma zip_with_append (f : α → β → γ) (l la : list α) (l' lb : list β) (h : l.length = l'.length) :
zip_with f (l ++ la) (l' ++ lb) = zip_with f l l' ++ zip_with f la lb :=
begin
induction l with hd tl hl generalizing l',
{ have : l' = [] := eq_nil_of_length_eq_zero (by simpa using h.symm),
simp [this], },
{ cases l',
{ simpa using h },
{ simp only [add_left_inj, length] at h,
simp [hl _ h] } }
end
lemma zip_with_distrib_reverse (h : l.length = l'.length) :
(zip_with f l l').reverse = zip_with f l.reverse l'.reverse :=
begin
induction l with hd tl hl generalizing l',
{ simp },
{ cases l' with hd' tl',
{ simp },
{ simp only [add_left_inj, length] at h,
have : tl.reverse.length = tl'.reverse.length := by simp [h],
simp [hl _ h, zip_with_append _ _ _ _ _ this] } }
end
end distrib
end list
|
6587438f4bbae2ed04b7c8f50cd6d42ecfc99345 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/data/semiquot.lean | 11530ce7862b3d8bf2d461c8cc9027d9195e0775 | [
"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 | 7,470 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
A data type for semiquotients, which are classically equivalent to
nonempty sets, but are useful for programming; the idea is that
a semiquotient set `S` represents some (particular but unknown)
element of `S`. This can be used to model nondeterministic functions,
which return something in a range of values (represented by the
predicate `S`) but are not completely determined.
-/
import data.set.lattice data.quot
/-- A member of `semiquot α` is classically a nonempty `set α`,
and in the VM is represented by an element of `α`; the relation
between these is that the VM element is required to be a member
of the set `s`. The specific element of `s` that the VM computes
is hidden by a quotient construction, allowing for the representation
of nondeterministic functions. -/
structure {u} semiquot (α : Type*) := mk' ::
(s : set α)
(val : trunc ↥s)
namespace semiquot
variables {α : Type*} {β : Type*}
instance : has_mem α (semiquot α) := ⟨λ a q, a ∈ q.s⟩
/-- Construct a `semiquot α` from `h : a ∈ s` where `s : set α`. -/
def mk {a : α} {s : set α} (h : a ∈ s) : semiquot α :=
⟨s, trunc.mk ⟨a, h⟩⟩
theorem ext_s {q₁ q₂ : semiquot α} : q₁ = q₂ ↔ q₁.s = q₂.s :=
⟨congr_arg _,
λ h, by cases q₁; cases q₂; congr; exact h⟩
theorem ext {q₁ q₂ : semiquot α} : q₁ = q₂ ↔ ∀ a, a ∈ q₁ ↔ a ∈ q₂ :=
ext_s.trans (set.ext_iff _ _)
theorem exists_mem (q : semiquot α) : ∃ a, a ∈ q :=
let ⟨⟨a, h⟩, h₂⟩ := q.2.exists_rep in ⟨a, h⟩
theorem eq_mk_of_mem {q : semiquot α} {a : α} (h : a ∈ q) :
q = @mk _ a q.1 h := ext_s.2 rfl
theorem nonempty (q : semiquot α) : q.s.nonempty := q.exists_mem
/-- `pure a` is `a` reinterpreted as an unspecified element of `{a}`. -/
protected def pure (a : α) : semiquot α := mk (set.mem_singleton a)
@[simp] theorem mem_pure' {a b : α} : a ∈ semiquot.pure b ↔ a = b :=
set.mem_singleton_iff
/-- Replace `s` in a `semiquot` with a superset. -/
def blur' (q : semiquot α) {s : set α} (h : q.s ⊆ s) : semiquot α :=
⟨s, trunc.lift (λ a : q.s, trunc.mk ⟨a.1, h a.2⟩)
(λ _ _, trunc.eq _ _) q.2⟩
/-- Replace `s` in a `q : semiquot α` with a union `s ∪ q.s` -/
def blur (s : set α) (q : semiquot α) : semiquot α :=
blur' q (set.subset_union_right s q.s)
theorem blur_eq_blur' (q : semiquot α) (s : set α) (h : q.s ⊆ s) :
blur s q = blur' q h :=
by unfold blur; congr; exact set.union_eq_self_of_subset_right h
@[simp] theorem mem_blur' (q : semiquot α) {s : set α} (h : q.s ⊆ s)
{a : α} : a ∈ blur' q h ↔ a ∈ s := iff.rfl
/-- Convert a `trunc α` to a `semiquot α`. -/
def of_trunc (q : trunc α) : semiquot α :=
⟨set.univ, q.map (λ a, ⟨a, trivial⟩)⟩
/-- Convert a `semiquot α` to a `trunc α`. -/
def to_trunc (q : semiquot α) : trunc α :=
q.2.map subtype.val
/-- If `f` is a constant on `q.s`, then `q.lift_on f` is the value of `f`
at any point of `q`. -/
def lift_on (q : semiquot α) (f : α → β) (h : ∀ a b ∈ q, f a = f b) : β :=
trunc.lift_on q.2 (λ x, f x.1) (λ x y, h _ _ x.2 y.2)
theorem lift_on_of_mem (q : semiquot α)
(f : α → β) (h : ∀ a b ∈ q, f a = f b)
(a : α) (aq : a ∈ q) : lift_on q f h = f a :=
by revert h; rw eq_mk_of_mem aq; intro; refl
def map (f : α → β) (q : semiquot α) : semiquot β :=
⟨f '' q.1, q.2.map (λ x, ⟨f x.1, set.mem_image_of_mem _ x.2⟩)⟩
@[simp] theorem mem_map (f : α → β) (q : semiquot α) (b : β) :
b ∈ map f q ↔ ∃ a, a ∈ q ∧ f a = b := set.mem_image _ _ _
def bind (q : semiquot α) (f : α → semiquot β) : semiquot β :=
⟨⋃ a ∈ q.1, (f a).1,
q.2.bind (λ a, (f a.1).2.map (λ b, ⟨b.1, set.mem_bUnion a.2 b.2⟩))⟩
@[simp] theorem mem_bind (q : semiquot α) (f : α → semiquot β) (b : β) :
b ∈ bind q f ↔ ∃ a ∈ q, b ∈ f a := set.mem_bUnion_iff
instance : monad semiquot :=
{ pure := @semiquot.pure,
map := @semiquot.map,
bind := @semiquot.bind }
@[simp] theorem mem_pure {a b : α} : a ∈ (pure b : semiquot α) ↔ a = b :=
set.mem_singleton_iff
theorem mem_pure_self (a : α) : a ∈ (pure a : semiquot α) :=
set.mem_singleton a
@[simp] theorem pure_inj {a b : α} : (pure a : semiquot α) = pure b ↔ a = b :=
ext_s.trans set.singleton_eq_singleton_iff
instance : is_lawful_monad semiquot :=
{ pure_bind := λ α β x f, ext.2 $ by simp,
bind_assoc := λ α β γ s f g, ext.2 $ by simp; exact
λ c, ⟨λ ⟨b, ⟨a, as, bf⟩, cg⟩, ⟨a, as, b, bf, cg⟩,
λ ⟨a, as, b, bf, cg⟩, ⟨b, ⟨a, as, bf⟩, cg⟩⟩,
id_map := λ α q, ext.2 $ by simp,
bind_pure_comp_eq_map := λ α β f s, ext.2 $ by simp [eq_comm] }
instance : has_le (semiquot α) := ⟨λ s t, s.s ⊆ t.s⟩
instance : partial_order (semiquot α) :=
{ le := λ s t, ∀ ⦃x⦄, x ∈ s → x ∈ t,
le_refl := λ s, set.subset.refl _,
le_trans := λ s t u, set.subset.trans,
le_antisymm := λ s t h₁ h₂, ext_s.2 (set.subset.antisymm h₁ h₂) }
instance : semilattice_sup (semiquot α) :=
{ sup := λ s, blur s.s,
le_sup_left := λ s t, set.subset_union_left _ _,
le_sup_right := λ s t, set.subset_union_right _ _,
sup_le := λ s t u, set.union_subset,
..semiquot.partial_order }
@[simp] theorem pure_le {a : α} {s : semiquot α} : pure a ≤ s ↔ a ∈ s :=
set.singleton_subset_iff
def is_pure (q : semiquot α) := ∀ a b ∈ q, a = b
def get (q : semiquot α) (h : q.is_pure) : α := lift_on q id h
theorem get_mem {q : semiquot α} (p) : get q p ∈ q :=
let ⟨a, h⟩ := exists_mem q in
by unfold get; rw lift_on_of_mem q _ _ a h; exact h
theorem eq_pure {q : semiquot α} (p) : q = pure (get q p) :=
ext.2 $ λ a, by simp; exact
⟨λ h, p _ _ h (get_mem _), λ e, e.symm ▸ get_mem _⟩
@[simp] theorem pure_is_pure (a : α) : is_pure (pure a)
| b c ab ac := by { simp at ab ac, cc }
theorem is_pure_iff {s : semiquot α} : is_pure s ↔ ∃ a, s = pure a :=
⟨λ h, ⟨_, eq_pure h⟩, λ ⟨a, e⟩, e.symm ▸ pure_is_pure _⟩
theorem is_pure.mono {s t : semiquot α}
(st : s ≤ t) (h : is_pure t) : is_pure s
| a b as bs := h _ _ (st as) (st bs)
theorem is_pure.min {s t : semiquot α} (h : is_pure t) : s ≤ t ↔ s = t :=
⟨λ st, le_antisymm st $ by rw [eq_pure h, eq_pure (h.mono st)]; simp;
exact h _ _ (get_mem _) (st $ get_mem _),
le_of_eq⟩
theorem is_pure_of_subsingleton [subsingleton α] (q : semiquot α) : is_pure q
| a b aq bq := subsingleton.elim _ _
/-- `univ : semiquot α` represents an unspecified element of `univ : set α`. -/
def univ [inhabited α] : semiquot α :=
mk $ set.mem_univ (default _)
instance [inhabited α] : inhabited (semiquot α) := ⟨univ⟩
@[simp] theorem mem_univ [inhabited α] : ∀ a, a ∈ @univ α _ :=
@set.mem_univ α
@[congr] theorem univ_unique (I J : inhabited α) : @univ _ I = @univ _ J :=
ext.2 $ by simp
@[simp] theorem is_pure_univ [inhabited α] : @is_pure α univ ↔ subsingleton α :=
⟨λ h, ⟨λ a b, h a b trivial trivial⟩, λ ⟨h⟩ a b _ _, h a b⟩
instance [inhabited α] : order_top (semiquot α) :=
{ top := univ,
le_top := λ s, set.subset_univ _,
..semiquot.partial_order }
instance [inhabited α] : semilattice_sup_top (semiquot α) :=
{ ..semiquot.order_top,
..semiquot.semilattice_sup }
end semiquot
|
11139b6fe793f91bc931111660e02737373daf29 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Init/Data/Format/Macro.lean | b69fed4d6483732da8a06d1e4ac4f8432a5e7be6 | [
"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 | 411 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
prelude
import Init.Data.Format.Basic
import Init.Data.ToString.Macro
namespace Std
syntax:max "f!" interpolatedStr(term) : term
macro_rules
| `(f! $interpStr) => do interpStr.expandInterpolatedStr (← `(Format)) (← `(Std.format))
end Std
|
28939c5532aa34998933c970ea17853256bd5fbf | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/data/complex/exponential.lean | 8cb0914700bae40f3d4a4de0d0f2f87ac22cbb0f | [
"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 | 47,823 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Abhimanyu Pallavi Sudhir
-/
import algebra.archimedean algebra.geom_sum
import data.nat.choose data.complex.basic
import tactic.linarith
local notation `abs'` := _root_.abs
open is_absolute_value
open_locale classical
section
open real is_absolute_value finset
lemma forall_ge_le_of_forall_le_succ {α : Type*} [preorder α] (f : ℕ → α) {m : ℕ}
(h : ∀ n ≥ m, f n.succ ≤ f n) : ∀ {l}, ∀ k ≥ m, k ≤ l → f l ≤ f k :=
begin
assume l k hkm hkl,
generalize hp : l - k = p,
have : l = k + p := add_comm p k ▸ (nat.sub_eq_iff_eq_add hkl).1 hp,
subst this,
clear hkl hp,
induction p with p ih,
{ simp },
{ exact le_trans (h _ (le_trans hkm (nat.le_add_right _ _))) ih }
end
variables {α : Type*} {β : Type*} [ring β]
[discrete_linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv]
lemma is_cau_of_decreasing_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a)
(hnm : ∀ n ≥ m, f n.succ ≤ f n) : is_cau_seq abs f :=
λ ε ε0,
let ⟨k, hk⟩ := archimedean.arch a ε0 in
have h : ∃ l, ∀ n ≥ m, a - add_monoid.smul l ε < f n :=
⟨k + k + 1, λ n hnm, lt_of_lt_of_le
(show a - add_monoid.smul (k + (k + 1)) ε < -abs (f n),
from lt_neg.1 $ lt_of_le_of_lt (ham n hnm) (begin
rw [neg_sub, lt_sub_iff_add_lt, add_monoid.add_smul],
exact add_lt_add_of_le_of_lt hk (lt_of_le_of_lt hk
(lt_add_of_pos_left _ ε0)),
end))
(neg_le.2 $ (abs_neg (f n)) ▸ le_abs_self _)⟩,
let l := nat.find h in
have hl : ∀ (n : ℕ), n ≥ m → f n > a - add_monoid.smul l ε := nat.find_spec h,
have hl0 : l ≠ 0 := λ hl0, not_lt_of_ge (ham m (le_refl _))
(lt_of_lt_of_le (by have := hl m (le_refl m); simpa [hl0] using this) (le_abs_self (f m))),
begin
cases classical.not_forall.1
(nat.find_min h (nat.pred_lt hl0)) with i hi,
rw [not_imp, not_lt] at hi,
existsi i,
assume j hj,
have hfij : f j ≤ f i := forall_ge_le_of_forall_le_succ f hnm _ hi.1 hj,
rw [abs_of_nonpos (sub_nonpos.2 hfij), neg_sub, sub_lt_iff_lt_add'],
exact calc f i ≤ a - add_monoid.smul (nat.pred l) ε : hi.2
... = a - add_monoid.smul l ε + ε :
by conv {to_rhs, rw [← nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hl0), succ_smul',
sub_add, add_sub_cancel] }
... < f j + ε : add_lt_add_right (hl j (le_trans hi.1 hj)) _
end
lemma is_cau_of_mono_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a)
(hnm : ∀ n ≥ m, f n ≤ f n.succ) : is_cau_seq abs f :=
begin
refine @eq.rec_on (ℕ → α) _ (is_cau_seq abs) _ _
(-⟨_, @is_cau_of_decreasing_bounded _ _ _ (λ n, -f n) a m (by simpa) (by simpa)⟩ : cau_seq α abs).2,
ext,
exact neg_neg _
end
lemma is_cau_series_of_abv_le_cau {f : ℕ → β} {g : ℕ → α} (n : ℕ) : (∀ m, n ≤ m → abv (f m) ≤ g m) →
is_cau_seq abs (λ n, (range n).sum g) → is_cau_seq abv (λ n, (range n).sum f) :=
begin
assume hm hg ε ε0,
cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi,
existsi max n i,
assume j ji,
have hi₁ := hi j (le_trans (le_max_right n i) ji),
have hi₂ := hi (max n i) (le_max_right n i),
have sub_le := abs_sub_le ((range j).sum g) ((range i).sum g) ((range (max n i)).sum g),
have := add_lt_add hi₁ hi₂,
rw [abs_sub ((range (max n i)).sum g), add_halves ε] at this,
refine lt_of_le_of_lt (le_trans (le_trans _ (le_abs_self _)) sub_le) this,
generalize hk : j - max n i = k,
clear this hi₂ hi₁ hi ε0 ε hg sub_le,
rw nat.sub_eq_iff_eq_add ji at hk,
rw hk,
clear hk ji j,
induction k with k' hi,
{ simp [abv_zero abv] },
{ dsimp at *,
simp only [nat.succ_add, sum_range_succ, sub_eq_add_neg, add_assoc],
refine le_trans (abv_add _ _ _) _,
exact add_le_add (hm _ (le_add_of_nonneg_of_le (nat.zero_le _) (le_max_left _ _))) hi },
end
lemma is_cau_series_of_abv_cau {f : ℕ → β} : is_cau_seq abs (λ m, (range m).sum (λ n, abv (f n))) →
is_cau_seq abv (λ m, (range m).sum f) :=
is_cau_series_of_abv_le_cau 0 (λ n h, le_refl _)
lemma is_cau_geo_series {β : Type*} [field β] {abv : β → α} [is_absolute_value abv]
(x : β) (hx1 : abv x < 1) : is_cau_seq abv (λ n, (range n).sum (λ m, x ^ m)) :=
have hx1' : abv x ≠ 1 := λ h, by simpa [h, lt_irrefl] using hx1,
is_cau_series_of_abv_cau
begin
simp only [abv_pow abv] {eta := ff},
have : (λ (m : ℕ), (range m).sum (λ n, (abv x) ^ n)) =
λ m, geom_series (abv x) m := rfl,
simp only [this, geom_sum hx1'] {eta := ff},
conv in (_ / _) { rw [← neg_div_neg_eq, neg_sub, neg_sub] },
refine @is_cau_of_mono_bounded _ _ _ _ ((1 : α) / (1 - abv x)) 0 _ _,
{ assume n hn,
rw abs_of_nonneg,
refine div_le_div_of_le_of_pos (sub_le_self _ (abv_pow abv x n ▸ abv_nonneg _ _))
(sub_pos.2 hx1),
refine div_nonneg (sub_nonneg.2 _) (sub_pos.2 hx1),
clear hn,
induction n with n ih,
{ simp },
{ rw [_root_.pow_succ, ← one_mul (1 : α)],
refine mul_le_mul (le_of_lt hx1) ih (abv_pow abv x n ▸ abv_nonneg _ _) (by norm_num) } },
{ assume n hn,
refine div_le_div_of_le_of_pos (sub_le_sub_left _ _) (sub_pos.2 hx1),
rw [← one_mul (_ ^ n), _root_.pow_succ],
exact mul_le_mul_of_nonneg_right (le_of_lt hx1) (pow_nonneg (abv_nonneg _ _) _) }
end
lemma is_cau_geo_series_const (a : α) {x : α} (hx1 : abs x < 1) : is_cau_seq abs (λ m, (range m).sum (λ n, a * x ^ n)) :=
have is_cau_seq abs (λ m, a * (range m).sum (λ n, x ^ n)) := (cau_seq.const abs a * ⟨_, is_cau_geo_series x hx1⟩).2,
by simpa only [mul_sum]
lemma series_ratio_test {f : ℕ → β} (n : ℕ) (r : α)
(hr0 : 0 ≤ r) (hr1 : r < 1) (h : ∀ m, n ≤ m → abv (f m.succ) ≤ r * abv (f m)) :
is_cau_seq abv (λ m, (range m).sum f) :=
have har1 : abs r < 1, by rwa abs_of_nonneg hr0,
begin
refine is_cau_series_of_abv_le_cau n.succ _ (is_cau_geo_series_const (abv (f n.succ) * r⁻¹ ^ n.succ) har1),
assume m hmn,
cases classical.em (r = 0) with r_zero r_ne_zero,
{ have m_pos := lt_of_lt_of_le (nat.succ_pos n) hmn,
have := h m.pred (nat.le_of_succ_le_succ (by rwa [nat.succ_pred_eq_of_pos m_pos])),
simpa [r_zero, nat.succ_pred_eq_of_pos m_pos, pow_succ] },
generalize hk : m - n.succ = k,
have r_pos : 0 < r := lt_of_le_of_ne hr0 (ne.symm r_ne_zero),
replace hk : m = k + n.succ := (nat.sub_eq_iff_eq_add hmn).1 hk,
induction k with k ih generalizing m n,
{ rw [hk, zero_add, mul_right_comm, inv_pow' _ _, ← div_eq_mul_inv, mul_div_cancel],
exact (ne_of_lt (pow_pos r_pos _)).symm },
{ have kn : k + n.succ ≥ n.succ, by rw ← zero_add n.succ; exact add_le_add (zero_le _) (by simp),
rw [hk, nat.succ_add, pow_succ' r, ← mul_assoc],
exact le_trans (by rw mul_comm; exact h _ (nat.le_of_succ_le kn))
(mul_le_mul_of_nonneg_right (ih (k + n.succ) n h kn rfl) hr0) }
end
lemma sum_range_diag_flip {α : Type*} [add_comm_monoid α] (n : ℕ) (f : ℕ → ℕ → α) :
(range n).sum (λ m, (range (m + 1)).sum (λ k, f k (m - k))) =
(range n).sum (λ m, (range (n - m)).sum (f m)) :=
have h₁ : ((range n).sigma (range ∘ nat.succ)).sum
(λ (a : Σ m, ℕ), f (a.2) (a.1 - a.2)) =
(range n).sum (λ m, (range (m + 1)).sum
(λ k, f k (m - k))) := sum_sigma,
have h₂ : ((range n).sigma (λ m, range (n - m))).sum (λ a : Σ (m : ℕ), ℕ, f (a.1) (a.2)) =
(range n).sum (λ m, (range (n - m)).sum (f m)) := sum_sigma,
h₁ ▸ h₂ ▸ sum_bij
(λ a _, ⟨a.2, a.1 - a.2⟩)
(λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1,
have h₂ : a.2 < nat.succ a.1 := mem_range.1 (mem_sigma.1 ha).2,
mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁),
mem_range.2 ((nat.sub_lt_sub_right_iff (nat.le_of_lt_succ h₂)).2 h₁)⟩)
(λ _ _, rfl)
(λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h,
have ha : a₁ < n ∧ a₂ ≤ a₁ :=
⟨mem_range.1 (mem_sigma.1 ha).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩,
have hb : b₁ < n ∧ b₂ ≤ b₁ :=
⟨mem_range.1 (mem_sigma.1 hb).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩,
have h : a₂ = b₂ ∧ _ := sigma.mk.inj h,
have h' : a₁ = b₁ - b₂ + a₂ := (nat.sub_eq_iff_eq_add ha.2).1 (eq_of_heq h.2),
sigma.mk.inj_iff.2
⟨nat.sub_add_cancel hb.2 ▸ h'.symm ▸ h.1 ▸ rfl,
(heq_of_eq h.1)⟩)
(λ ⟨a₁, a₂⟩ ha,
have ha : a₁ < n ∧ a₂ < n - a₁ :=
⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩,
⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 (nat.lt_sub_right_iff_add_lt.1 ha.2),
mem_range.2 (nat.lt_succ_of_le (nat.le_add_left _ _))⟩,
sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (nat.add_sub_cancel _ _).symm⟩⟩⟩)
lemma abv_sum_le_sum_abv {γ : Type*} (f : γ → β) (s : finset γ) :
abv (s.sum f) ≤ s.sum (abv ∘ f) :=
by haveI := classical.dec_eq γ; exact
finset.induction_on s (by simp [abv_zero abv])
(λ a s has ih, by rw [sum_insert has, sum_insert has];
exact le_trans (abv_add abv _ _) (add_le_add_left ih _))
lemma sum_range_sub_sum_range {α : Type*} [add_comm_group α] {f : ℕ → α}
{n m : ℕ} (hnm : n ≤ m) : (range m).sum f - (range n).sum f =
((range m).filter (λ k, n ≤ k)).sum f :=
begin
rw [← sum_sdiff (@filter_subset _ (λ k, n ≤ k) _ (range m)),
sub_eq_iff_eq_add, ← eq_sub_iff_add_eq, add_sub_cancel'],
refine finset.sum_congr
(finset.ext.2 $ λ a, ⟨λ h, by simp at *; finish,
λ h, have ham : a < m := lt_of_lt_of_le (mem_range.1 h) hnm,
by simp * at *⟩)
(λ _ _, rfl),
end
lemma cauchy_product {a b : ℕ → β}
(ha : is_cau_seq abs (λ m, (range m).sum (λ n, abv (a n))))
(hb : is_cau_seq abv (λ m, (range m).sum b)) (ε : α) (ε0 : 0 < ε) :
∃ i : ℕ, ∀ j ≥ i, abv ((range j).sum a * (range j).sum b -
(range j).sum (λ n, (range (n + 1)).sum (λ m, a m * b (n - m)))) < ε :=
let ⟨Q, hQ⟩ := cau_seq.bounded ⟨_, hb⟩ in
let ⟨P, hP⟩ := cau_seq.bounded ⟨_, ha⟩ in
have hP0 : 0 < P, from lt_of_le_of_lt (abs_nonneg _) (hP 0),
have hPε0 : 0 < ε / (2 * P),
from div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) hP0),
let ⟨N, hN⟩ := cau_seq.cauchy₂ ⟨_, hb⟩ hPε0 in
have hQε0 : 0 < ε / (4 * Q),
from div_pos ε0 (mul_pos (show (0 : α) < 4, by norm_num) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))),
let ⟨M, hM⟩ := cau_seq.cauchy₂ ⟨_, ha⟩ hQε0 in
⟨2 * (max N M + 1), λ K hK,
have h₁ : (range K).sum (λ m, (range (m + 1)).sum (λ k, a k * b (m - k))) =
(range K).sum (λ m, (range (K - m)).sum (λ n, a m * b n)),
by simpa using sum_range_diag_flip K (λ m n, a m * b n),
have h₂ : (λ i, (range (K - i)).sum (λ k, a i * b k)) = (λ i, a i * (range (K - i)).sum b),
by simp [finset.mul_sum],
have h₃ : (range K).sum (λ i, a i * (range (K - i)).sum b) =
(range K).sum (λ i, a i * ((range (K - i)).sum b - (range K).sum b))
+ (range K).sum (λ i, a i * (range K).sum b),
by rw ← sum_add_distrib; simp [(mul_add _ _ _).symm],
have two_mul_two : (4 : α) = 2 * 2, by norm_num,
have hQ0 : Q ≠ 0, from λ h, by simpa [h, lt_irrefl] using hQε0,
have h2Q0 : 2 * Q ≠ 0, from mul_ne_zero two_ne_zero hQ0,
have hε : ε / (2 * P) * P + ε / (4 * Q) * (2 * Q) = ε,
by rw [← div_div_eq_div_mul, div_mul_cancel _ (ne.symm (ne_of_lt hP0)),
two_mul_two, mul_assoc, ← div_div_eq_div_mul, div_mul_cancel _ h2Q0, add_halves],
have hNMK : max N M + 1 < K,
from lt_of_lt_of_le (by rw two_mul; exact lt_add_of_pos_left _ (nat.succ_pos _)) hK,
have hKN : N < K,
from calc N ≤ max N M : le_max_left _ _
... < max N M + 1 : nat.lt_succ_self _
... < K : hNMK,
have hsumlesum : (range (max N M + 1)).sum (λ i, abv (a i) *
abv ((range (K - i)).sum b - (range K).sum b)) ≤ (range (max N M + 1)).sum
(λ i, abv (a i) * (ε / (2 * P))),
from sum_le_sum (λ m hmJ, mul_le_mul_of_nonneg_left
(le_of_lt (hN (K - m) K
(nat.le_sub_left_of_add_le (le_trans
(by rw two_mul; exact add_le_add (le_of_lt (mem_range.1 hmJ))
(le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))) hK))
(le_of_lt hKN))) (abv_nonneg abv _)),
have hsumltP : (range (max N M + 1)).sum (λ n, abv (a n)) < P :=
calc (range (max N M + 1)).sum (λ n, abv (a n))
= abs ((range (max N M + 1)).sum (λ n, abv (a n))) :
eq.symm (abs_of_nonneg (sum_nonneg (λ x h, abv_nonneg abv (a x))))
... < P : hP (max N M + 1),
begin
rw [h₁, h₂, h₃, sum_mul, ← sub_sub, sub_right_comm, sub_self, zero_sub, abv_neg abv],
refine lt_of_le_of_lt (abv_sum_le_sum_abv _ _) _,
suffices : (range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) +
((range K).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) -(range (max N M + 1)).sum
(λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b))) < ε / (2 * P) * P + ε / (4 * Q) * (2 * Q),
{ rw hε at this, simpa [abv_mul abv] },
refine add_lt_add (lt_of_le_of_lt hsumlesum
(by rw [← sum_mul, mul_comm]; exact (mul_lt_mul_left hPε0).mpr hsumltP)) _,
rw sum_range_sub_sum_range (le_of_lt hNMK),
exact calc ((range K).filter (λ k, max N M + 1 ≤ k)).sum
(λ i, abv (a i) * abv ((range (K - i)).sum b - (range K).sum b))
≤ ((range K).filter (λ k, max N M + 1 ≤ k)).sum (λ i, abv (a i) * (2 * Q)) :
sum_le_sum (λ n hn, begin
refine mul_le_mul_of_nonneg_left _ (abv_nonneg _ _),
rw sub_eq_add_neg,
refine le_trans (abv_add _ _ _) _,
rw [two_mul, abv_neg abv],
exact add_le_add (le_of_lt (hQ _)) (le_of_lt (hQ _)),
end)
... < ε / (4 * Q) * (2 * Q) :
by rw [← sum_mul, ← sum_range_sub_sum_range (le_of_lt hNMK)];
refine (mul_lt_mul_right $ by rw two_mul;
exact add_pos (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))
(lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).2
(lt_of_le_of_lt (le_abs_self _)
(hM _ _ (le_trans (nat.le_succ_of_le (le_max_right _ _)) (le_of_lt hNMK))
(nat.le_succ_of_le (le_max_right _ _))))
end⟩
end
open finset
open cau_seq
namespace complex
lemma is_cau_abs_exp (z : ℂ) : is_cau_seq _root_.abs
(λ n, (range n).sum (λ m, abs (z ^ m / nat.fact m))) :=
let ⟨n, hn⟩ := exists_nat_gt (abs z) in
have hn0 : (0 : ℝ) < n, from lt_of_le_of_lt (abs_nonneg _) hn,
series_ratio_test n (complex.abs z / n) (div_nonneg_of_nonneg_of_pos (complex.abs_nonneg _) hn0)
(by rwa [div_lt_iff hn0, one_mul])
(λ m hm,
by rw [abs_abs, abs_abs, nat.fact_succ, pow_succ,
mul_comm m.succ, nat.cast_mul, ← div_div_eq_div_mul, mul_div_assoc,
mul_div_right_comm, abs_mul, abs_div, abs_cast_nat];
exact mul_le_mul_of_nonneg_right
(div_le_div_of_le_left (abs_nonneg _) hn0
(nat.cast_le.2 (le_trans hm (nat.le_succ _)))) (abs_nonneg _))
noncomputable theory
lemma is_cau_exp (z : ℂ) : is_cau_seq abs (λ n, (range n).sum (λ m, z ^ m / nat.fact m)) :=
is_cau_series_of_abv_cau (is_cau_abs_exp z)
def exp' (z : ℂ) : cau_seq ℂ complex.abs := ⟨λ n, (range n).sum (λ m, z ^ m / nat.fact m), is_cau_exp z⟩
def exp (z : ℂ) : ℂ := lim (exp' z)
def sin (z : ℂ) : ℂ := ((exp (-z * I) - exp (z * I)) * I) / 2
def cos (z : ℂ) : ℂ := (exp (z * I) + exp (-z * I)) / 2
def tan (z : ℂ) : ℂ := sin z / cos z
def sinh (z : ℂ) : ℂ := (exp z - exp (-z)) / 2
def cosh (z : ℂ) : ℂ := (exp z + exp (-z)) / 2
def tanh (z : ℂ) : ℂ := sinh z / cosh z
end complex
namespace real
open complex
def exp (x : ℝ) : ℝ := (exp x).re
def sin (x : ℝ) : ℝ := (sin x).re
def cos (x : ℝ) : ℝ := (cos x).re
def tan (x : ℝ) : ℝ := (tan x).re
def sinh (x : ℝ) : ℝ := (sinh x).re
def cosh (x : ℝ) : ℝ := (cosh x).re
def tanh (x : ℝ) : ℝ := (tanh x).re
end real
namespace complex
variables (x y : ℂ)
@[simp] lemma exp_zero : exp 0 = 1 :=
lim_eq_of_equiv_const $
λ ε ε0, ⟨1, λ j hj, begin
convert ε0,
cases j,
{ exact absurd hj (not_le_of_gt zero_lt_one) },
{ dsimp [exp'],
induction j with j ih,
{ dsimp [exp']; simp },
{ rw ← ih dec_trivial,
simp only [sum_range_succ, pow_succ],
simp } }
end⟩
lemma exp_add : exp (x + y) = exp x * exp y :=
show lim (⟨_, is_cau_exp (x + y)⟩ : cau_seq ℂ abs) =
lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp x⟩)
* lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp y⟩),
from
have hj : ∀ j : ℕ, (range j).sum
(λ m, (x + y) ^ m / m.fact) = (range j).sum
(λ i, (range (i + 1)).sum (λ k, x ^ k / k.fact *
(y ^ (i - k) / (i - k).fact))),
from assume j,
finset.sum_congr rfl (λ m hm, begin
rw [add_pow, div_eq_mul_inv, sum_mul],
refine finset.sum_congr rfl (λ i hi, _),
have h₁ : (nat.choose m i : ℂ) ≠ 0 := nat.cast_ne_zero.2
(nat.pos_iff_ne_zero.1 (nat.choose_pos (nat.le_of_lt_succ (mem_range.1 hi)))),
have h₂ := nat.choose_mul_fact_mul_fact (nat.le_of_lt_succ $ finset.mem_range.1 hi),
rw [← h₂, nat.cast_mul, nat.cast_mul, mul_inv', mul_inv'],
simp only [mul_left_comm (nat.choose m i : ℂ), mul_assoc, mul_left_comm (nat.choose m i : ℂ)⁻¹,
mul_comm (nat.choose m i : ℂ)],
rw inv_mul_cancel h₁,
simp [div_eq_mul_inv, mul_comm, mul_assoc, mul_left_comm]
end),
by rw lim_mul_lim;
exact eq.symm (lim_eq_lim_of_equiv (by dsimp; simp only [hj];
exact cauchy_product (is_cau_abs_exp x) (is_cau_exp y)))
attribute [irreducible] complex.exp
lemma exp_list_sum (l : list ℂ) : exp l.sum = (l.map exp).prod :=
@monoid_hom.map_list_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ l
lemma exp_multiset_sum (s : multiset ℂ) : exp s.sum = (s.map exp).prod :=
@monoid_hom.map_multiset_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ s
lemma exp_sum {α : Type*} (s : finset α) (f : α → ℂ) : exp (s.sum f) = s.prod (exp ∘ f) :=
@monoid_hom.map_prod α (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ f s
lemma exp_nat_mul (x : ℂ) : ∀ n : ℕ, exp(n*x) = (exp x)^n
| 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero]
| (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul]
lemma exp_ne_zero : exp x ≠ 0 :=
λ h, @zero_ne_one ℂ _ $
by rw [← exp_zero, ← add_neg_self x, exp_add, h]; simp
lemma exp_neg : exp (-x) = (exp x)⁻¹ :=
by rw [← domain.mul_left_inj (exp_ne_zero x), ← exp_add];
simp [mul_inv_cancel (exp_ne_zero x)]
lemma exp_sub : exp (x - y) = exp x / exp y :=
by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv]
@[simp] lemma exp_conj : exp (conj x) = conj (exp x) :=
begin
dsimp [exp],
rw [← lim_conj],
refine congr_arg lim (cau_seq.ext (λ _, _)),
dsimp [exp', function.comp, cau_seq_conj],
rw ← sum_hom _ conj,
refine sum_congr rfl (λ n hn, _),
rw [conj_div, conj_pow, ← of_real_nat_cast, conj_of_real]
end
@[simp] lemma of_real_exp_of_real_re (x : ℝ) : ((exp x).re : ℂ) = exp x :=
eq_conj_iff_re.1 $ by rw [← exp_conj, conj_of_real]
@[simp] lemma of_real_exp (x : ℝ) : (real.exp x : ℂ) = exp x :=
of_real_exp_of_real_re _
@[simp] lemma exp_of_real_im (x : ℝ) : (exp x).im = 0 :=
by rw [← of_real_exp_of_real_re, of_real_im]
lemma exp_of_real_re (x : ℝ) : (exp x).re = real.exp x := rfl
lemma two_sinh : 2 * sinh x = exp x - exp (-x) :=
mul_div_cancel' _ two_ne_zero'
lemma two_cosh : 2 * cosh x = exp x + exp (-x) :=
mul_div_cancel' _ two_ne_zero'
@[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh]
@[simp] lemma sinh_neg : sinh (-x) = -sinh x :=
by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul]
private lemma sinh_add_aux {a b c d : ℂ} :
(a - b) * (c + d) + (a + b) * (c - d) = 2 * (a * c - b * d) := by ring
lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y :=
begin
rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), two_sinh,
exp_add, neg_add, exp_add, eq_comm,
mul_add, ← mul_assoc, two_sinh, mul_left_comm, two_sinh,
← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_add,
mul_left_comm, two_cosh, ← mul_assoc, two_cosh],
exact sinh_add_aux
end
@[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh]
@[simp] lemma cosh_neg : cosh (-x) = cosh x :=
by simp [add_comm, cosh, exp_neg]
private lemma cosh_add_aux {a b c d : ℂ} :
(a + b) * (c + d) + (a - b) * (c - d) = 2 * (a * c + b * d) := by ring
lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y :=
begin
rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), two_cosh,
exp_add, neg_add, exp_add, eq_comm,
mul_add, ← mul_assoc, two_cosh, ← mul_assoc, two_sinh,
← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_add,
mul_left_comm, two_cosh, mul_left_comm, two_sinh],
exact cosh_add_aux
end
lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y :=
by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg]
lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y :=
by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg]
lemma sinh_conj : sinh (conj x) = conj (sinh x) :=
by rw [sinh, ← conj_neg, exp_conj, exp_conj, ← conj_sub, sinh, conj_div, conj_two]
@[simp] lemma of_real_sinh_of_real_re (x : ℝ) : ((sinh x).re : ℂ) = sinh x :=
eq_conj_iff_re.1 $ by rw [← sinh_conj, conj_of_real]
@[simp] lemma of_real_sinh (x : ℝ) : (real.sinh x : ℂ) = sinh x :=
of_real_sinh_of_real_re _
@[simp] lemma sinh_of_real_im (x : ℝ) : (sinh x).im = 0 :=
by rw [← of_real_sinh_of_real_re, of_real_im]
lemma sinh_of_real_re (x : ℝ) : (sinh x).re = real.sinh x := rfl
lemma cosh_conj : cosh (conj x) = conj (cosh x) :=
by rw [cosh, ← conj_neg, exp_conj, exp_conj, ← conj_add, cosh, conj_div, conj_two]
@[simp] lemma of_real_cosh_of_real_re (x : ℝ) : ((cosh x).re : ℂ) = cosh x :=
eq_conj_iff_re.1 $ by rw [← cosh_conj, conj_of_real]
@[simp] lemma of_real_cosh (x : ℝ) : (real.cosh x : ℂ) = cosh x :=
of_real_cosh_of_real_re _
@[simp] lemma cosh_of_real_im (x : ℝ) : (cosh x).im = 0 :=
by rw [← of_real_cosh_of_real_re, of_real_im]
lemma cosh_of_real_re (x : ℝ) : (cosh x).re = real.cosh x := rfl
lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := rfl
@[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh]
@[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div]
lemma tanh_conj : tanh (conj x) = conj (tanh x) :=
by rw [tanh, sinh_conj, cosh_conj, ← conj_div, tanh]
@[simp] lemma of_real_tanh_of_real_re (x : ℝ) : ((tanh x).re : ℂ) = tanh x :=
eq_conj_iff_re.1 $ by rw [← tanh_conj, conj_of_real]
@[simp] lemma of_real_tanh (x : ℝ) : (real.tanh x : ℂ) = tanh x :=
of_real_tanh_of_real_re _
@[simp] lemma tanh_of_real_im (x : ℝ) : (tanh x).im = 0 :=
by rw [← of_real_tanh_of_real_re, of_real_im]
lemma tanh_of_real_re (x : ℝ) : (tanh x).re = real.tanh x := rfl
lemma cosh_add_sinh : cosh x + sinh x = exp x :=
by rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_add,
two_cosh, two_sinh, add_add_sub_cancel, two_mul]
lemma sinh_add_cosh : sinh x + cosh x = exp x :=
by rw [add_comm, cosh_add_sinh]
lemma cosh_sub_sinh : cosh x - sinh x = exp (-x) :=
by rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_sub,
two_cosh, two_sinh, add_sub_sub_cancel, two_mul]
lemma cosh_sq_sub_sinh_sq : cosh x ^ 2 - sinh x ^ 2 = 1 :=
by rw [sq_sub_sq, cosh_add_sinh, cosh_sub_sinh, ← exp_add, add_neg_self, exp_zero]
@[simp] lemma sin_zero : sin 0 = 0 := by simp [sin]
@[simp] lemma sin_neg : sin (-x) = -sin x :=
by simp [sin, sub_eq_add_neg, exp_neg, (neg_div _ _).symm, add_mul]
lemma two_sin : 2 * sin x = (exp (-x * I) - exp (x * I)) * I :=
mul_div_cancel' _ two_ne_zero'
lemma two_cos : 2 * cos x = exp (x * I) + exp (-x * I) :=
mul_div_cancel' _ two_ne_zero'
lemma sinh_mul_I : sinh (x * I) = sin x * I :=
by rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), two_sinh,
← mul_assoc, two_sin, mul_assoc, I_mul_I, mul_neg_one,
neg_sub, neg_mul_eq_neg_mul]
lemma cosh_mul_I : cosh (x * I) = cos x :=
by rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), two_cosh,
two_cos, neg_mul_eq_neg_mul]
lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y :=
by rw [← domain.mul_right_inj I_ne_zero, ← sinh_mul_I,
add_mul, add_mul, mul_right_comm, ← sinh_mul_I,
mul_assoc, ← sinh_mul_I, ← cosh_mul_I, ← cosh_mul_I, sinh_add]
@[simp] lemma cos_zero : cos 0 = 1 := by simp [cos]
@[simp] lemma cos_neg : cos (-x) = cos x :=
by simp [cos, sub_eq_add_neg, exp_neg, add_comm]
private lemma cos_add_aux {a b c d : ℂ} :
(a + b) * (c + d) - (b - a) * (d - c) * (-1) =
2 * (a * c + b * d) := by ring
lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y :=
by rw [← cosh_mul_I, add_mul, cosh_add, cosh_mul_I, cosh_mul_I,
sinh_mul_I, sinh_mul_I, mul_mul_mul_comm, I_mul_I,
mul_neg_one, sub_eq_add_neg]
lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y :=
by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg]
lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y :=
by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg]
lemma sin_conj : sin (conj x) = conj (sin x) :=
by rw [← domain.mul_right_inj I_ne_zero, ← sinh_mul_I,
← conj_neg_I, ← conj_mul, ← conj_mul, sinh_conj,
mul_neg_eq_neg_mul_symm, sinh_neg, sinh_mul_I, mul_neg_eq_neg_mul_symm]
@[simp] lemma of_real_sin_of_real_re (x : ℝ) : ((sin x).re : ℂ) = sin x :=
eq_conj_iff_re.1 $ by rw [← sin_conj, conj_of_real]
@[simp] lemma of_real_sin (x : ℝ) : (real.sin x : ℂ) = sin x :=
of_real_sin_of_real_re _
@[simp] lemma sin_of_real_im (x : ℝ) : (sin x).im = 0 :=
by rw [← of_real_sin_of_real_re, of_real_im]
lemma sin_of_real_re (x : ℝ) : (sin x).re = real.sin x := rfl
lemma cos_conj : cos (conj x) = conj (cos x) :=
by rw [← cosh_mul_I, ← conj_neg_I, ← conj_mul, ← cosh_mul_I,
cosh_conj, mul_neg_eq_neg_mul_symm, cosh_neg]
@[simp] lemma of_real_cos_of_real_re (x : ℝ) : ((cos x).re : ℂ) = cos x :=
eq_conj_iff_re.1 $ by rw [← cos_conj, conj_of_real]
@[simp] lemma of_real_cos (x : ℝ) : (real.cos x : ℂ) = cos x :=
of_real_cos_of_real_re _
@[simp] lemma cos_of_real_im (x : ℝ) : (cos x).im = 0 :=
by rw [← of_real_cos_of_real_re, of_real_im]
lemma cos_of_real_re (x : ℝ) : (cos x).re = real.cos x := rfl
@[simp] lemma tan_zero : tan 0 = 0 := by simp [tan]
lemma tan_eq_sin_div_cos : tan x = sin x / cos x := rfl
@[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div]
lemma tan_conj : tan (conj x) = conj (tan x) :=
by rw [tan, sin_conj, cos_conj, ← conj_div, tan]
@[simp] lemma of_real_tan_of_real_re (x : ℝ) : ((tan x).re : ℂ) = tan x :=
eq_conj_iff_re.1 $ by rw [← tan_conj, conj_of_real]
@[simp] lemma of_real_tan (x : ℝ) : (real.tan x : ℂ) = tan x :=
of_real_tan_of_real_re _
@[simp] lemma tan_of_real_im (x : ℝ) : (tan x).im = 0 :=
by rw [← of_real_tan_of_real_re, of_real_im]
lemma tan_of_real_re (x : ℝ) : (tan x).re = real.tan x := rfl
lemma cos_add_sin_I : cos x + sin x * I = exp (x * I) :=
by rw [← cosh_add_sinh, sinh_mul_I, cosh_mul_I]
lemma cos_sub_sin_I : cos x - sin x * I = exp (-x * I) :=
by rw [← neg_mul_eq_neg_mul, ← cosh_sub_sinh, sinh_mul_I, cosh_mul_I]
lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 :=
eq.trans
(by rw [cosh_mul_I, sinh_mul_I, mul_pow, I_sq, mul_neg_one, sub_neg_eq_add, add_comm])
(cosh_sq_sub_sinh_sq (x * I))
lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 :=
by rw [two_mul, cos_add, ← pow_two, ← pow_two]
lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 :=
by rw [cos_two_mul', eq_sub_iff_add_eq.2 (sin_sq_add_cos_sq x),
← sub_add, sub_add_eq_add_sub, two_mul]
lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x :=
by rw [two_mul, sin_add, two_mul, add_mul, mul_comm]
lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 :=
by simp [cos_two_mul, div_add_div_same, mul_div_cancel_left, two_ne_zero', -one_div_eq_inv]
lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 :=
by { rw [←sin_sq_add_cos_sq x], simp }
lemma exp_mul_I : exp (x * I) = cos x + sin x * I :=
(cos_add_sin_I _).symm
lemma exp_add_mul_I : exp (x + y * I) = exp x * (cos y + sin y * I) :=
by rw [exp_add, exp_mul_I]
lemma exp_eq_exp_re_mul_sin_add_cos : exp x = exp x.re * (cos x.im + sin x.im * I) :=
by rw [← exp_add_mul_I, re_add_im]
theorem cos_add_sin_mul_I_pow (n : ℕ) (z : ℂ) : (cos z + sin z * I) ^ n = cos (↑n * z) + sin (↑n * z) * I :=
begin
rw [← exp_mul_I, ← exp_mul_I],
induction n with n ih,
{ rw [pow_zero, nat.cast_zero, zero_mul, zero_mul, exp_zero] },
{ rw [pow_succ', ih, nat.cast_succ, add_mul, add_mul, one_mul, exp_add] }
end
end complex
namespace real
open complex
variables (x y : ℝ)
@[simp] lemma exp_zero : exp 0 = 1 :=
by simp [real.exp]
lemma exp_add : exp (x + y) = exp x * exp y :=
by simp [exp_add, exp]
lemma exp_list_sum (l : list ℝ) : exp l.sum = (l.map exp).prod :=
@monoid_hom.map_list_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ l
lemma exp_multiset_sum (s : multiset ℝ) : exp s.sum = (s.map exp).prod :=
@monoid_hom.map_multiset_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ s
lemma exp_sum {α : Type*} (s : finset α) (f : α → ℝ) : exp (s.sum f) = s.prod (exp ∘ f) :=
@monoid_hom.map_prod α (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ f s
lemma exp_nat_mul (x : ℝ) : ∀ n : ℕ, exp(n*x) = (exp x)^n
| 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero]
| (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul]
lemma exp_ne_zero : exp x ≠ 0 :=
λ h, exp_ne_zero x $ by rw [exp, ← of_real_inj] at h; simp * at *
lemma exp_neg : exp (-x) = (exp x)⁻¹ :=
by rw [← of_real_inj, exp, of_real_exp_of_real_re, of_real_neg, exp_neg,
of_real_inv, of_real_exp]
lemma exp_sub : exp (x - y) = exp x / exp y :=
by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv]
@[simp] lemma sin_zero : sin 0 = 0 := by simp [sin]
@[simp] lemma sin_neg : sin (-x) = -sin x :=
by simp [sin, exp_neg, (neg_div _ _).symm, add_mul]
lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y :=
by rw [← of_real_inj]; simp [sin, sin_add]
@[simp] lemma cos_zero : cos 0 = 1 := by simp [cos]
@[simp] lemma cos_neg : cos (-x) = cos x :=
by simp [cos, exp_neg]
lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y :=
by rw ← of_real_inj; simp [cos, cos_add]
lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y :=
by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg]
lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y :=
by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg]
lemma tan_eq_sin_div_cos : tan x = sin x / cos x :=
if h : complex.cos x = 0 then by simp [sin, cos, tan, *, complex.tan, div_eq_mul_inv] at *
else
by rw [sin, cos, tan, complex.tan, ← of_real_inj, div_eq_mul_inv, mul_re];
simp [norm_sq, (div_div_eq_div_mul _ _ _).symm, div_self h]; refl
@[simp] lemma tan_zero : tan 0 = 0 := by simp [tan]
@[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div]
lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 :=
of_real_inj.1 $ by simpa using sin_sq_add_cos_sq x
lemma sin_sq_le_one : sin x ^ 2 ≤ 1 :=
by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_right' (pow_two_nonneg _)
lemma cos_sq_le_one : cos x ^ 2 ≤ 1 :=
by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_left' (pow_two_nonneg _)
lemma abs_sin_le_one : abs' (sin x) ≤ 1 :=
(mul_self_le_mul_self_iff (_root_.abs_nonneg (sin x)) (by exact zero_le_one)).2 $
by rw [← _root_.abs_mul, abs_mul_self, mul_one, ← pow_two];
apply sin_sq_le_one
lemma abs_cos_le_one : abs' (cos x) ≤ 1 :=
(mul_self_le_mul_self_iff (_root_.abs_nonneg (cos x)) (by exact zero_le_one)).2 $
by rw [← _root_.abs_mul, abs_mul_self, mul_one, ← pow_two];
apply cos_sq_le_one
lemma sin_le_one : sin x ≤ 1 :=
(abs_le.1 (abs_sin_le_one _)).2
lemma cos_le_one : cos x ≤ 1 :=
(abs_le.1 (abs_cos_le_one _)).2
lemma neg_one_le_sin : -1 ≤ sin x :=
(abs_le.1 (abs_sin_le_one _)).1
lemma neg_one_le_cos : -1 ≤ cos x :=
(abs_le.1 (abs_cos_le_one _)).1
lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 :=
by rw ← of_real_inj; simp [cos_two_mul]
lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x :=
by rw ← of_real_inj; simp [sin_two_mul]
lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 :=
of_real_inj.1 $ by simpa using cos_square x
lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 :=
eq_sub_iff_add_eq.2 $ sin_sq_add_cos_sq _
@[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh]
@[simp] lemma sinh_neg : sinh (-x) = -sinh x :=
by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul]
lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y :=
by rw ← of_real_inj; simp [sinh_add]
@[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh]
@[simp] lemma cosh_neg : cosh (-x) = cosh x :=
by simp [cosh, exp_neg]
lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y :=
by rw ← of_real_inj; simp [cosh, cosh_add]
lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y :=
by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg]
lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y :=
by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg]
lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x :=
of_real_inj.1 $ by simp [tanh_eq_sinh_div_cosh]
@[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh]
@[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div]
open is_absolute_value
/- TODO make this private and prove ∀ x -/
lemma add_one_le_exp_of_nonneg {x : ℝ} (hx : 0 ≤ x) : x + 1 ≤ exp x :=
calc x + 1 ≤ lim (⟨(λ n : ℕ, ((exp' x) n).re), is_cau_seq_re (exp' x)⟩ : cau_seq ℝ abs') :
le_lim (cau_seq.le_of_exists ⟨2,
λ j hj, show x + (1 : ℝ) ≤ ((range j).sum (λ m, (x ^ m / m.fact : ℂ))).re,
from have h₁ : (((λ m : ℕ, (x ^ m / m.fact : ℂ)) ∘ nat.succ) 0).re = x, by simp,
have h₂ : ((x : ℂ) ^ 0 / nat.fact 0).re = 1, by simp,
begin
rw [← nat.sub_add_cancel hj, sum_range_succ', sum_range_succ',
add_re, add_re, h₁, h₂, add_assoc,
← @sum_hom _ _ _ _ _ _ _ complex.re
(is_add_group_hom.to_is_add_monoid_hom _)],
refine le_add_of_nonneg_of_le (sum_nonneg (λ m hm, _)) (le_refl _), dsimp [-nat.fact_succ],
rw [← of_real_pow, ← of_real_nat_cast, ← of_real_div, of_real_re],
exact div_nonneg (pow_nonneg hx _) (nat.cast_pos.2 (nat.fact_pos _)),
end⟩)
... = exp x : by rw [exp, complex.exp, ← cau_seq_re, lim_re]
lemma one_le_exp {x : ℝ} (hx : 0 ≤ x) : 1 ≤ exp x :=
by linarith [add_one_le_exp_of_nonneg hx]
lemma exp_pos (x : ℝ) : 0 < exp x :=
(le_total 0 x).elim (lt_of_lt_of_le zero_lt_one ∘ one_le_exp)
(λ h, by rw [← neg_neg x, real.exp_neg];
exact inv_pos.2 (lt_of_lt_of_le zero_lt_one (one_le_exp (neg_nonneg.2 h))))
@[simp] lemma abs_exp (x : ℝ) : abs' (exp x) = exp x :=
abs_of_pos (exp_pos _)
lemma exp_strict_mono : strict_mono exp :=
λ x y h, by rw [← sub_add_cancel y x, real.exp_add];
exact (lt_mul_iff_one_lt_left (exp_pos _)).2
(lt_of_lt_of_le (by linarith) (add_one_le_exp_of_nonneg (by linarith)))
lemma exp_lt_exp {x y : ℝ} : exp x < exp y ↔ x < y := exp_strict_mono.lt_iff_lt
lemma exp_le_exp {x y : ℝ} : exp x ≤ exp y ↔ x ≤ y := exp_strict_mono.le_iff_le
lemma exp_injective : function.injective exp := exp_strict_mono.injective
@[simp] lemma exp_eq_one_iff : exp x = 1 ↔ x = 0 :=
by rw [← exp_zero, exp_injective.eq_iff]
lemma one_lt_exp_iff {x : ℝ} : 1 < exp x ↔ 0 < x :=
by rw [← exp_zero, exp_lt_exp]
lemma exp_lt_one_iff {x : ℝ} : exp x < 1 ↔ x < 0 :=
by rw [← exp_zero, exp_lt_exp]
end real
namespace complex
lemma sum_div_fact_le {α : Type*} [discrete_linear_ordered_field α] (n j : ℕ) (hn : 0 < n) :
(filter (λ k, n ≤ k) (range j)).sum (λ m : ℕ, (1 / m.fact : α)) ≤ n.succ * (n.fact * n)⁻¹ :=
calc (filter (λ k, n ≤ k) (range j)).sum (λ m : ℕ, (1 / m.fact : α))
= (range (j - n)).sum (λ m, 1 / (m + n).fact) :
sum_bij (λ m _, m - n)
(λ m hm, mem_range.2 $ (nat.sub_lt_sub_right_iff (by simp at hm; tauto)).2
(by simp at hm; tauto))
(λ m hm, by rw nat.sub_add_cancel; simp at *; tauto)
(λ a₁ a₂ ha₁ ha₂ h,
by rwa [nat.sub_eq_iff_eq_add, ← nat.sub_add_comm, eq_comm, nat.sub_eq_iff_eq_add, add_right_inj, eq_comm] at h;
simp at *; tauto)
(λ b hb, ⟨b + n, mem_filter.2 ⟨mem_range.2 $ nat.add_lt_of_lt_sub_right (mem_range.1 hb), nat.le_add_left _ _⟩,
by rw nat.add_sub_cancel⟩)
... ≤ (range (j - n)).sum (λ m, (nat.fact n * n.succ ^ m)⁻¹) :
begin
refine sum_le_sum (assume m n, _),
rw [one_div_eq_inv, inv_le_inv],
{ rw [← nat.cast_pow, ← nat.cast_mul, nat.cast_le, add_comm],
exact nat.fact_mul_pow_le_fact },
{ exact nat.cast_pos.2 (nat.fact_pos _) },
{ exact mul_pos (nat.cast_pos.2 (nat.fact_pos _)) (pow_pos (nat.cast_pos.2 (nat.succ_pos _)) _) },
end
... = (nat.fact n)⁻¹ * (range (j - n)).sum (λ m, n.succ⁻¹ ^ m) :
by simp [mul_inv', mul_sum.symm, sum_mul.symm, -nat.fact_succ, mul_comm, inv_pow']
... = (n.succ - n.succ * n.succ⁻¹ ^ (j - n)) / (n.fact * n) :
have h₁ : (n.succ : α) ≠ 1, from @nat.cast_one α _ _ ▸ mt nat.cast_inj.1
(mt nat.succ_inj (nat.pos_iff_ne_zero.1 hn)),
have h₂ : (n.succ : α) ≠ 0, from nat.cast_ne_zero.2 (nat.succ_ne_zero _),
have h₃ : (n.fact * n : α) ≠ 0, from mul_ne_zero (nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 (nat.fact_pos _)))
(nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 hn)),
have h₄ : (n.succ - 1 : α) = n, by simp,
by rw [← geom_series_def, geom_sum_inv h₁ h₂, eq_div_iff_mul_eq _ _ h₃, mul_comm _ (n.fact * n : α),
← mul_assoc (n.fact⁻¹ : α), ← mul_inv', h₄, ← mul_assoc (n.fact * n : α),
mul_comm (n : α) n.fact, mul_inv_cancel h₃];
simp [mul_add, add_mul, mul_assoc, mul_comm]
... ≤ n.succ / (n.fact * n) :
begin
refine (div_le_div_right (mul_pos _ _)).2 _,
exact nat.cast_pos.2 (nat.fact_pos _),
exact nat.cast_pos.2 hn,
exact sub_le_self _ (mul_nonneg (nat.cast_nonneg _) (pow_nonneg (inv_nonneg.2 (nat.cast_nonneg _)) _))
end
lemma exp_bound {x : ℂ} (hx : abs x ≤ 1) {n : ℕ} (hn : 0 < n) :
abs (exp x - (range n).sum (λ m, x ^ m / m.fact)) ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹) :=
begin
rw [← lim_const ((range n).sum _), exp, sub_eq_add_neg, ← lim_neg, lim_add, ← lim_abs],
refine lim_le (cau_seq.le_of_exists ⟨n, λ j hj, _⟩),
show abs ((range j).sum (λ m, x ^ m / m.fact) - (range n).sum (λ m, x ^ m / m.fact))
≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹),
rw sum_range_sub_sum_range hj,
exact calc abs (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (x ^ m / m.fact : ℂ)))
= abs (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (x ^ n * (x ^ (m - n) / m.fact) : ℂ))) :
congr_arg abs (sum_congr rfl (λ m hm, by rw [← mul_div_assoc, ← pow_add, nat.add_sub_cancel']; simp at hm; tauto))
... ≤ (filter (λ k, n ≤ k) (range j)).sum (λ m, abs (x ^ n * (_ / m.fact))) : abv_sum_le_sum_abv _ _
... ≤ (filter (λ k, n ≤ k) (range j)).sum (λ m, abs x ^ n * (1 / m.fact)) :
begin
refine sum_le_sum (λ m hm, _),
rw [abs_mul, abv_pow abs, abs_div, abs_cast_nat],
refine mul_le_mul_of_nonneg_left ((div_le_div_right _).2 _) _,
exact nat.cast_pos.2 (nat.fact_pos _),
rw abv_pow abs,
exact (pow_le_one _ (abs_nonneg _) hx),
exact pow_nonneg (abs_nonneg _) _
end
... = abs x ^ n * (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (1 / m.fact : ℝ))) :
by simp [abs_mul, abv_pow abs, abs_div, mul_sum.symm]
... ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹) :
mul_le_mul_of_nonneg_left (sum_div_fact_le _ _ hn) (pow_nonneg (abs_nonneg _) _)
end
lemma abs_exp_sub_one_le {x : ℂ} (hx : abs x ≤ 1) :
abs (exp x - 1) ≤ 2 * abs x :=
calc abs (exp x - 1) = abs (exp x - (range 1).sum (λ m, x ^ m / m.fact)) :
by simp [sum_range_succ]
... ≤ abs x ^ 1 * ((nat.succ 1) * (nat.fact 1 * (1 : ℕ))⁻¹) :
exp_bound hx dec_trivial
... = 2 * abs x : by simp [two_mul, mul_two, mul_add, mul_comm]
lemma abs_exp_sub_one_sub_id_le {x : ℂ} (hx : abs x ≤ 1) :
abs (exp x - 1 - x) ≤ (abs x)^2 :=
calc abs (exp x - 1 - x) = abs (exp x - (range 2).sum (λ m, x ^ m / m.fact)) :
by simp [sub_eq_add_neg, sum_range_succ]
... ≤ (abs x)^2 * (nat.succ 2 * (nat.fact 2 * (2 : ℕ))⁻¹) :
exp_bound hx dec_trivial
... ≤ (abs x)^2 * 1 :
mul_le_mul_of_nonneg_left (by norm_num) (pow_two_nonneg (abs x))
... = (abs x)^2 :
by rw [mul_one]
end complex
namespace real
open complex finset
lemma cos_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (cos x - (1 - x ^ 2 / 2)) ≤ abs' x ^ 4 * (5 / 96) :=
calc abs' (cos x - (1 - x ^ 2 / 2)) = abs (complex.cos x - (1 - x ^ 2 / 2)) :
by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv]
... = abs ((complex.exp (x * I) + complex.exp (-x * I) - (2 - x ^ 2)) / 2) :
by simp [complex.cos, sub_div, add_div, neg_div, div_self (@two_ne_zero' ℂ _ _ _)]
... = abs (((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) +
((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)))) / 2) :
congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin
simp only [sum_range_succ],
simp [pow_succ],
apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring
end)
... ≤ abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) / 2) +
abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) / 2) :
by rw add_div; exact abs_add _ _
... = (abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) / 2 +
abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact))) / 2) :
by simp [complex.abs_div]
... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2 +
(complex.abs (-x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2) :
add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial))
((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial))
... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc]
lemma sin_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (sin x - (x - x ^ 3 / 6)) ≤ abs' x ^ 4 * (5 / 96) :=
calc abs' (sin x - (x - x ^ 3 / 6)) = abs (complex.sin x - (x - x ^ 3 / 6)) :
by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv]
... = abs (((complex.exp (-x * I) - complex.exp (x * I)) * I - (2 * x - x ^ 3 / 3)) / 2) :
by simp [complex.sin, sub_div, add_div, neg_div, mul_div_cancel_left _ (@two_ne_zero' ℂ _ _ _),
div_div_eq_div_mul, show (3 : ℂ) * 2 = 6, by norm_num]
... = abs ((((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) -
(complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) * I) / 2) :
congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin
simp only [sum_range_succ],
simp [pow_succ],
apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring
end)
... ≤ abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) * I / 2) +
abs (-((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) * I) / 2) :
by rw [sub_mul, sub_eq_add_neg, add_div]; exact abs_add _ _
... = (abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) / 2 +
abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact))) / 2) :
by simp [add_comm, complex.abs_div, complex.abs_mul]
... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2 +
(complex.abs (-x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2) :
add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial))
((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial))
... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc]
lemma cos_pos_of_le_one {x : ℝ} (hx : abs' x ≤ 1) : 0 < cos x :=
calc 0 < (1 - x ^ 2 / 2) - abs' x ^ 4 * (5 / 96) :
sub_pos.2 $ lt_sub_iff_add_lt.2
(calc abs' x ^ 4 * (5 / 96) + x ^ 2 / 2
≤ 1 * (5 / 96) + 1 / 2 :
add_le_add
(mul_le_mul_of_nonneg_right (pow_le_one _ (abs_nonneg _) hx) (by norm_num))
((div_le_div_right (by norm_num)).2 (by rw [pow_two, ← abs_mul_self, _root_.abs_mul];
exact mul_le_one hx (abs_nonneg _) hx))
... < 1 : by norm_num)
... ≤ cos x : sub_le.1 (abs_sub_le_iff.1 (cos_bound hx)).2
lemma sin_pos_of_pos_of_le_one {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 1) : 0 < sin x :=
calc 0 < x - x ^ 3 / 6 - abs' x ^ 4 * (5 / 96) :
sub_pos.2 $ lt_sub_iff_add_lt.2
(calc abs' x ^ 4 * (5 / 96) + x ^ 3 / 6
≤ x * (5 / 96) + x / 6 :
add_le_add
(mul_le_mul_of_nonneg_right
(calc abs' x ^ 4 ≤ abs' x ^ 1 : pow_le_pow_of_le_one (abs_nonneg _)
(by rwa _root_.abs_of_nonneg (le_of_lt hx0))
dec_trivial
... = x : by simp [_root_.abs_of_nonneg (le_of_lt (hx0))]) (by norm_num))
((div_le_div_right (by norm_num)).2
(calc x ^ 3 ≤ x ^ 1 : pow_le_pow_of_le_one (le_of_lt hx0) hx dec_trivial
... = x : pow_one _))
... < x : by linarith)
... ≤ sin x : sub_le.1 (abs_sub_le_iff.1 (sin_bound
(by rwa [_root_.abs_of_nonneg (le_of_lt hx0)]))).2
lemma sin_pos_of_pos_of_le_two {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 2) : 0 < sin x :=
have x / 2 ≤ 1, from div_le_of_le_mul (by norm_num) (by simpa),
calc 0 < 2 * sin (x / 2) * cos (x / 2) :
mul_pos (mul_pos (by norm_num) (sin_pos_of_pos_of_le_one (half_pos hx0) this))
(cos_pos_of_le_one (by rwa [_root_.abs_of_nonneg (le_of_lt (half_pos hx0))]))
... = sin x : by rw [← sin_two_mul, two_mul, add_halves]
lemma cos_one_le : cos 1 ≤ 2 / 3 :=
calc cos 1 ≤ abs' (1 : ℝ) ^ 4 * (5 / 96) + (1 - 1 ^ 2 / 2) :
sub_le_iff_le_add.1 (abs_sub_le_iff.1 (cos_bound (by simp))).1
... ≤ 2 / 3 : by norm_num
lemma cos_one_pos : 0 < cos 1 := cos_pos_of_le_one (by simp)
lemma cos_two_neg : cos 2 < 0 :=
calc cos 2 = cos (2 * 1) : congr_arg cos (mul_one _).symm
... = _ : real.cos_two_mul 1
... ≤ 2 * (2 / 3) ^ 2 - 1 :
sub_le_sub_right (mul_le_mul_of_nonneg_left
(by rw [pow_two, pow_two]; exact
mul_self_le_mul_self (le_of_lt cos_one_pos)
cos_one_le)
(by norm_num)) _
... < 0 : by norm_num
end real
namespace complex
lemma abs_cos_add_sin_mul_I (x : ℝ) : abs (cos x + sin x * I) = 1 :=
have _ := real.sin_sq_add_cos_sq x,
by simp [add_comm, abs, norm_sq, pow_two, *, sin_of_real_re, cos_of_real_re, mul_re] at *
lemma abs_exp_eq_iff_re_eq {x y : ℂ} : abs (exp x) = abs (exp y) ↔ x.re = y.re :=
by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y,
abs_mul, abs_mul, abs_cos_add_sin_mul_I, abs_cos_add_sin_mul_I,
← of_real_exp, ← of_real_exp, abs_of_nonneg (le_of_lt (real.exp_pos _)),
abs_of_nonneg (le_of_lt (real.exp_pos _)), mul_one, mul_one];
exact ⟨λ h, real.exp_injective h, congr_arg _⟩
@[simp] lemma abs_exp_of_real (x : ℝ) : abs (exp x) = real.exp x :=
by rw [← of_real_exp]; exact abs_of_nonneg (le_of_lt (real.exp_pos _))
end complex
|
b6dd16421c99abafa04291652097f9b4b541b2a0 | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/topology/algebra/valuation.lean | 0235dab2cadfc0cdcf5428684e31bec3ed60a440 | [
"Apache-2.0"
] | permissive | jumpy4/mathlib | d3829e75173012833e9f15ac16e481e17596de0f | af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13 | refs/heads/master | 1,693,508,842,818 | 1,636,203,271,000 | 1,636,203,271,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,114 | lean | /-
Copyright (c) 2021 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot
-/
import topology.algebra.nonarchimedean.bases
import topology.algebra.uniform_filter_basis
import ring_theory.valuation.basic
/-!
# The topology on a valued ring
In this file, we define the non archimedean topology induced by a valuation on a ring.
The main definition is a `valued` type class which equips a ring with a valuation taking
values in a group with zero (living in the same universe). Other instances are then deduced from
this.
-/
open_locale classical topological_space
open set valuation
noncomputable theory
universe u
/-- A valued ring is a ring that comes equipped with a distinguished valuation.-/
class valued (R : Type u) [ring R] :=
(Γ₀ : Type u)
[grp : linear_ordered_comm_group_with_zero Γ₀]
(v : valuation R Γ₀)
attribute [instance] valued.grp
namespace valued
variables {R : Type*} [ring R] [valued R]
/-- The basis of open subgroups for the topology on a valued ring.-/
lemma subgroups_basis : ring_subgroups_basis (λ γ : units (Γ₀ R), valued.v.lt_add_subgroup γ) :=
{ inter := begin
rintros γ₀ γ₁,
use min γ₀ γ₁,
simp [valuation.lt_add_subgroup] ; tauto
end,
mul := begin
rintros γ,
cases exists_square_le γ with γ₀ h,
use γ₀,
rintro - ⟨r, s, r_in, s_in, rfl⟩,
calc v (r*s) = v r * v s : valuation.map_mul _ _ _
... < γ₀*γ₀ : mul_lt_mul₀ r_in s_in
... ≤ γ : by exact_mod_cast h
end,
left_mul := begin
rintros x γ,
rcases group_with_zero.eq_zero_or_unit (v x) with Hx | ⟨γx, Hx⟩,
{ use 1,
rintros y (y_in : v y < 1),
change v (x * y) < _,
rw [valuation.map_mul, Hx, zero_mul],
exact units.zero_lt γ },
{ simp only [image_subset_iff, set_of_subset_set_of, preimage_set_of_eq, valuation.map_mul],
use γx⁻¹*γ,
rintros y (vy_lt : v y < ↑(γx⁻¹ * γ)),
change v (x * y) < γ,
rw [valuation.map_mul, Hx, mul_comm],
rw [units.coe_mul, mul_comm] at vy_lt,
simpa using mul_inv_lt_of_lt_mul₀ vy_lt }
end,
right_mul := begin
rintros x γ,
rcases group_with_zero.eq_zero_or_unit (v x) with Hx | ⟨γx, Hx⟩,
{ use 1,
rintros y (y_in : v y < 1),
change v (y * x) < _,
rw [valuation.map_mul, Hx, mul_zero],
exact units.zero_lt γ },
{ use γx⁻¹*γ,
rintros y (vy_lt : v y < ↑(γx⁻¹ * γ)),
change v (y * x) < γ,
rw [valuation.map_mul, Hx],
rw [units.coe_mul, mul_comm] at vy_lt,
simpa using mul_inv_lt_of_lt_mul₀ vy_lt }
end }
@[priority 100]
instance : topological_space R := subgroups_basis.topology
lemma mem_nhds {s : set R} {x : R} :
(s ∈ 𝓝 x) ↔ ∃ γ : units (valued.Γ₀ R), {y | v (y - x) < γ } ⊆ s :=
by simpa [(subgroups_basis.has_basis_nhds x).mem_iff]
lemma mem_nhds_zero {s : set R} :
(s ∈ 𝓝 (0 : R)) ↔ ∃ γ : units (Γ₀ R), {x | v x < (γ : Γ₀ R) } ⊆ s :=
by simp [valued.mem_nhds, sub_zero]
lemma loc_const {x : R} (h : v x ≠ 0) : {y : R | v y = v x} ∈ 𝓝 x :=
begin
rw valued.mem_nhds,
rcases units.exists_iff_ne_zero.mpr h with ⟨γ, hx⟩,
use γ,
rw hx,
intros y y_in,
exact valuation.map_eq_of_sub_lt _ y_in
end
/-- The uniform structure on a valued ring.-/
@[priority 100]
instance uniform_space : uniform_space R := topological_add_group.to_uniform_space R
/-- A valued ring is a uniform additive group.-/
@[priority 100]
instance uniform_add_group : uniform_add_group R := topological_add_group_is_uniform
lemma cauchy_iff {F : filter R} :
cauchy F ↔ F.ne_bot ∧ ∀ γ : units (Γ₀ R), ∃ M ∈ F, ∀ x y, x ∈ M → y ∈ M → v (y - x) < γ :=
begin
rw add_group_filter_basis.cauchy_iff,
apply and_congr iff.rfl,
simp_rw subgroups_basis.mem_add_group_filter_basis_iff,
split,
{ intros h γ,
exact h _ (subgroups_basis.mem_add_group_filter_basis _) },
{ rintros h - ⟨γ, rfl⟩,
exact h γ }
end
end valued
|
2f8912a8eedd2e366d4864cedf58683ad6fbc41c | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/measure_theory/lattice.lean | 17a658e8f46418d529ab4f2c26110ccedc174499 | [
"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 | 7,048 | lean | /-
Copyright (c) 2021 Rémy Degenne. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Rémy Degenne
-/
import measure_theory.measure.measure_space
/-!
# Typeclasses for measurability of lattice operations
In this file we define classes `has_measurable_sup` and `has_measurable_inf` and prove dot-style
lemmas (`measurable.sup`, `ae_measurable.sup` etc). For binary operations we define two typeclasses:
- `has_measurable_sup` says that both left and right sup are measurable;
- `has_measurable_sup₂` says that `λ p : α × α, p.1 ⊔ p.2` is measurable,
and similarly for other binary operations. The reason for introducing these classes is that in case
of topological space `α` equipped with the Borel `σ`-algebra, instances for `has_measurable_sup₂`
etc require `α` to have a second countable topology.
For instances relating, e.g., `has_continuous_sup` to `has_measurable_sup` see file
`measure_theory.borel_space`.
## Tags
measurable function, lattice operation
-/
open measure_theory
/-- We say that a type `has_measurable_sup` if `((⊔) c)` and `(⊔ c)` are measurable functions.
For a typeclass assuming measurability of `uncurry (⊔)` see `has_measurable_sup₂`. -/
class has_measurable_sup (M : Type*) [measurable_space M] [has_sup M] : Prop :=
(measurable_const_sup : ∀ c : M, measurable ((⊔) c))
(measurable_sup_const : ∀ c : M, measurable (⊔ c))
/-- We say that a type `has_measurable_sup₂` if `uncurry (⊔)` is a measurable functions.
For a typeclass assuming measurability of `((⊔) c)` and `(⊔ c)` see `has_measurable_sup`. -/
class has_measurable_sup₂ (M : Type*) [measurable_space M] [has_sup M] : Prop :=
(measurable_sup : measurable (λ p : M × M, p.1 ⊔ p.2))
export has_measurable_sup₂ (measurable_sup)
has_measurable_sup (measurable_const_sup measurable_sup_const)
/-- We say that a type `has_measurable_inf` if `((⊓) c)` and `(⊓ c)` are measurable functions.
For a typeclass assuming measurability of `uncurry (⊓)` see `has_measurable_inf₂`. -/
class has_measurable_inf (M : Type*) [measurable_space M] [has_inf M] : Prop :=
(measurable_const_inf : ∀ c : M, measurable ((⊓) c))
(measurable_inf_const : ∀ c : M, measurable (⊓ c))
/-- We say that a type `has_measurable_inf₂` if `uncurry (⊔)` is a measurable functions.
For a typeclass assuming measurability of `((⊔) c)` and `(⊔ c)` see `has_measurable_inf`. -/
class has_measurable_inf₂ (M : Type*) [measurable_space M] [has_inf M] : Prop :=
(measurable_inf : measurable (λ p : M × M, p.1 ⊓ p.2))
export has_measurable_inf₂ (measurable_inf)
has_measurable_inf (measurable_const_inf measurable_inf_const)
variables {M : Type*} [measurable_space M]
section order_dual
@[priority 100]
instance order_dual.has_measurable_sup [has_inf M] [has_measurable_inf M] :
has_measurable_sup (order_dual M) :=
⟨@measurable_const_inf M _ _ _, @measurable_inf_const M _ _ _⟩
@[priority 100]
instance order_dual.has_measurable_inf [has_sup M] [has_measurable_sup M] :
has_measurable_inf (order_dual M) :=
⟨@measurable_const_sup M _ _ _, @measurable_sup_const M _ _ _⟩
@[priority 100]
instance order_dual.has_measurable_sup₂ [has_inf M] [has_measurable_inf₂ M] :
has_measurable_sup₂ (order_dual M) :=
⟨@measurable_inf M _ _ _⟩
@[priority 100]
instance order_dual.has_measurable_inf₂ [has_sup M] [has_measurable_sup₂ M] :
has_measurable_inf₂ (order_dual M) :=
⟨@measurable_sup M _ _ _⟩
end order_dual
variables {α : Type*} {m : measurable_space α} {μ : measure α} {f g : α → M}
include m
section sup
variables [has_sup M]
section measurable_sup
variables [has_measurable_sup M]
@[measurability]
lemma measurable.const_sup (hf : measurable f) (c : M) : measurable (λ x, c ⊔ f x) :=
(measurable_const_sup c).comp hf
@[measurability]
lemma ae_measurable.const_sup (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, c ⊔ f x) μ :=
(has_measurable_sup.measurable_const_sup c).comp_ae_measurable hf
@[measurability]
lemma measurable.sup_const (hf : measurable f) (c : M) : measurable (λ x, f x ⊔ c) :=
(measurable_sup_const c).comp hf
@[measurability]
lemma ae_measurable.sup_const (hf : ae_measurable f μ) (c : M) :
ae_measurable (λ x, f x ⊔ c) μ :=
(measurable_sup_const c).comp_ae_measurable hf
end measurable_sup
section measurable_sup₂
variables [has_measurable_sup₂ M]
@[measurability]
lemma measurable.sup' (hf : measurable f) (hg : measurable g) : measurable (f ⊔ g) :=
measurable_sup.comp (hf.prod_mk hg)
@[measurability]
lemma measurable.sup (hf : measurable f) (hg : measurable g) : measurable (λ a, f a ⊔ g a) :=
measurable_sup.comp (hf.prod_mk hg)
@[measurability]
lemma ae_measurable.sup' (hf : ae_measurable f μ) (hg : ae_measurable g μ) :
ae_measurable (f ⊔ g) μ :=
measurable_sup.comp_ae_measurable (hf.prod_mk hg)
@[measurability]
lemma ae_measurable.sup (hf : ae_measurable f μ) (hg : ae_measurable g μ) :
ae_measurable (λ a, f a ⊔ g a) μ :=
measurable_sup.comp_ae_measurable (hf.prod_mk hg)
omit m
@[priority 100]
instance has_measurable_sup₂.to_has_measurable_sup : has_measurable_sup M :=
⟨λ c, measurable_const.sup measurable_id, λ c, measurable_id.sup measurable_const⟩
include m
end measurable_sup₂
end sup
section inf
variables [has_inf M]
section measurable_inf
variables [has_measurable_inf M]
@[measurability]
lemma measurable.const_inf (hf : measurable f) (c : M) :
measurable (λ x, c ⊓ f x) :=
(measurable_const_inf c).comp hf
@[measurability]
lemma ae_measurable.const_inf (hf : ae_measurable f μ) (c : M) :
ae_measurable (λ x, c ⊓ f x) μ :=
(has_measurable_inf.measurable_const_inf c).comp_ae_measurable hf
@[measurability]
lemma measurable.inf_const (hf : measurable f) (c : M) :
measurable (λ x, f x ⊓ c) :=
(measurable_inf_const c).comp hf
@[measurability]
lemma ae_measurable.inf_const (hf : ae_measurable f μ) (c : M) :
ae_measurable (λ x, f x ⊓ c) μ :=
(measurable_inf_const c).comp_ae_measurable hf
end measurable_inf
section measurable_inf₂
variables [has_measurable_inf₂ M]
@[measurability]
lemma measurable.inf' (hf : measurable f) (hg : measurable g) : measurable (f ⊓ g) :=
measurable_inf.comp (hf.prod_mk hg)
@[measurability]
lemma measurable.inf (hf : measurable f) (hg : measurable g) : measurable (λ a, f a ⊓ g a) :=
measurable_inf.comp (hf.prod_mk hg)
@[measurability]
lemma ae_measurable.inf' (hf : ae_measurable f μ) (hg : ae_measurable g μ) :
ae_measurable (f ⊓ g) μ :=
measurable_inf.comp_ae_measurable (hf.prod_mk hg)
@[measurability]
lemma ae_measurable.inf (hf : ae_measurable f μ) (hg : ae_measurable g μ) :
ae_measurable (λ a, f a ⊓ g a) μ :=
measurable_inf.comp_ae_measurable (hf.prod_mk hg)
omit m
@[priority 100]
instance has_measurable_inf₂.to_has_measurable_inf : has_measurable_inf M :=
⟨λ c, measurable_const.inf measurable_id, λ c, measurable_id.inf measurable_const⟩
include m
end measurable_inf₂
end inf
|
9120f69abfd66b8d785b35d94022347db7e54fee | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/algebraic_topology/alternating_face_map_complex.lean | c9c34188e4bd778ba03f903874157101e6782b79 | [
"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 | 7,977 | lean | /-
Copyright (c) 2021 Joël Riou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joël Riou, Adam Topaz, Johan Commelin
-/
import algebra.homology.homological_complex
import algebraic_topology.simplicial_object
import algebraic_topology.Moore_complex
import category_theory.abelian.basic
import algebra.big_operators.basic
import tactic.ring_exp
import data.fintype.card
/-!
# The alternating face map complex of a simplicial object in a preadditive category
We construct the alternating face map complex, as a
functor `alternating_face_map_complex : simplicial_object C ⥤ chain_complex C ℕ`
for any preadditive category `C`. For any simplicial object `X` in `C`,
this is the homological complex `... → X_2 → X_1 → X_0`
where the differentials are alternating sums of faces.
We also construct the natural transformation
`inclusion_of_Moore_complex : normalized_Moore_complex A ⟶ alternating_face_map_complex A`
when `A` is an abelian category.
## References
* https://stacks.math.columbia.edu/tag/0194
* https://ncatlab.org/nlab/show/Moore+complex
-/
open category_theory category_theory.limits category_theory.subobject
open category_theory.preadditive
open opposite
open_locale big_operators
open_locale simplicial
noncomputable theory
namespace algebraic_topology
namespace alternating_face_map_complex
/-!
## Construction of the alternating face map complex
-/
variables {C : Type*} [category C] [preadditive C]
variables (X : simplicial_object C)
variables (Y : simplicial_object C)
/-- The differential on the alternating face map complex is the alternate
sum of the face maps -/
@[simp]
def obj_d (n : ℕ) : X _[n+1] ⟶ X _[n] :=
∑ (i : fin (n+2)), (-1 : ℤ)^(i : ℕ) • X.δ i
/--
## The chain complex relation `d ≫ d`
-/
lemma d_squared (n : ℕ) : obj_d X (n+1) ≫ obj_d X n = 0 :=
begin
/- we start by expanding d ≫ d as a double sum -/
dsimp,
rw comp_sum,
let d_l := λ (j : fin (n+3)), (-1 : ℤ)^(j : ℕ) • X.δ j,
let d_r := λ (i : fin (n+2)), (-1 : ℤ)^(i : ℕ) • X.δ i,
rw [show (λ i , (∑ j : fin (n+3), d_l j) ≫ d_r i) =
(λ i, ∑ j : fin (n+3), (d_l j ≫ d_r i)), by { ext i, rw sum_comp, }],
rw ← finset.sum_product',
/- then, we decompose the index set P into a subet S and its complement Sᶜ -/
let P := fin (n+2) × fin (n+3),
let S := finset.univ.filter (λ (ij : P), (ij.2 : ℕ) ≤ (ij.1 : ℕ)),
let term := λ (ij : P), d_l ij.2 ≫ d_r ij.1,
erw [show ∑ (ij : P), term ij =
(∑ ij in S, term ij) + (∑ ij in Sᶜ, term ij), by rw finset.sum_add_sum_compl],
rw [← eq_neg_iff_add_eq_zero, ← finset.sum_neg_distrib],
/- we are reduced to showing that two sums are equal, and this is obtained
by constructing a bijection φ : S -> Sᶜ, which maps (i,j) to (j,i+1),
and by comparing the terms -/
let φ : Π (ij : P), ij ∈ S → P := λ ij hij,
(fin.cast_lt ij.2
(lt_of_le_of_lt (finset.mem_filter.mp hij).right (fin.is_lt ij.1)), ij.1.succ),
apply finset.sum_bij φ,
{ -- φ(S) is contained in Sᶜ
intros ij hij,
simp only [finset.mem_univ, finset.compl_filter, finset.mem_filter, true_and,
fin.coe_succ, fin.coe_cast_lt] at hij ⊢,
linarith, },
{ /- identification of corresponding terms in both sums -/
rintro ⟨i, j⟩ hij,
simp only [term, d_l, d_r, φ, comp_zsmul, zsmul_comp, ← neg_smul, ← mul_smul,
pow_add, neg_mul_eq_neg_mul_symm, mul_one, fin.coe_cast_lt,
fin.coe_succ, pow_one, mul_neg_eq_neg_mul_symm, neg_neg],
let jj : fin (n+2) := (φ (i,j) hij).1,
have ineq : jj ≤ i, { rw ← fin.coe_fin_le, simpa using hij, },
rw [category_theory.simplicial_object.δ_comp_δ X ineq, fin.cast_succ_cast_lt, mul_comm] },
{ -- φ : S → Sᶜ is injective
rintro ⟨i, j⟩ ⟨i', j'⟩ hij hij' h,
rw [prod.mk.inj_iff],
refine ⟨by simpa using congr_arg prod.snd h, _⟩,
have h1 := congr_arg fin.cast_succ (congr_arg prod.fst h),
simpa [fin.cast_succ_cast_lt] using h1 },
{ -- φ : S → Sᶜ is surjective
rintro ⟨i', j'⟩ hij',
simp only [true_and, finset.mem_univ, finset.compl_filter, not_le,
finset.mem_filter] at hij',
refine ⟨(j'.pred _, fin.cast_succ i'), _, _⟩,
{ intro H,
simpa only [H, nat.not_lt_zero, fin.coe_zero] using hij' },
{ simpa only [true_and, finset.mem_univ, fin.coe_cast_succ, fin.coe_pred,
finset.mem_filter] using nat.le_pred_of_lt hij', },
{ simp only [prod.mk.inj_iff, fin.succ_pred, fin.cast_lt_cast_succ],
split; refl }, },
end
/-!
## Construction of the alternating face map complex functor
-/
/-- The alternating face map complex, on objects -/
def obj : chain_complex C ℕ := chain_complex.of (λ n, X _[n]) (obj_d X) (d_squared X)
variables {X} {Y}
/-- The alternating face map complex, on morphisms -/
@[simp]
def map (f : X ⟶ Y) : obj X ⟶ obj Y :=
chain_complex.of_hom _ _ _ _ _ _
(λ n, f.app (op [n]))
(λ n,
begin
dsimp,
rw [comp_sum, sum_comp],
apply finset.sum_congr rfl (λ x h, _),
rw [comp_zsmul, zsmul_comp],
apply congr_arg,
erw f.naturality,
refl,
end)
end alternating_face_map_complex
variables (C : Type*) [category C] [preadditive C]
/-- The alternating face map complex, as a functor -/
@[simps]
def alternating_face_map_complex : simplicial_object C ⥤ chain_complex C ℕ :=
{ obj := alternating_face_map_complex.obj,
map := λ X Y f, alternating_face_map_complex.map f }
/-!
## Construction of the natural inclusion of the normalized Moore complex
-/
variables {A : Type*} [category A] [abelian A]
/-- The inclusion map of the Moore complex in the alternating face map complex -/
def inclusion_of_Moore_complex_map (X : simplicial_object A) :
(normalized_Moore_complex A).obj X ⟶ (alternating_face_map_complex A).obj X :=
chain_complex.of_hom _ _ _ _ _ _
(λ n, (normalized_Moore_complex.obj_X X n).arrow)
(λ n,
begin
/- we have to show the compatibility of the differentials on the alternating
face map complex with those defined on the normalized Moore complex:
we first get rid of the terms of the alternating sum that are obviously
zero on the normalized_Moore_complex -/
simp only [alternating_face_map_complex.obj_d],
rw comp_sum,
let t := λ (j : fin (n+2)), (normalized_Moore_complex.obj_X X (n+1)).arrow ≫
((-1 : ℤ)^(j : ℕ) • X.δ j),
have def_t : (∀ j : fin (n+2), t j = (normalized_Moore_complex.obj_X X (n+1)).arrow ≫
((-1 : ℤ)^(j : ℕ) • X.δ j)) := by { intro j, refl, },
rw [fin.sum_univ_succ t],
have null : ∀ j : fin (n+1), t j.succ = 0,
{ intro j,
rw [def_t, comp_zsmul, ← zsmul_zero ((-1 : ℤ)^(j.succ : ℕ))],
apply congr_arg,
rw normalized_Moore_complex.obj_X,
rw ← factor_thru_arrow _ _
(finset_inf_arrow_factors finset.univ _ j (by simp only [finset.mem_univ])),
slice_lhs 2 3 { erw kernel_subobject_arrow_comp (X.δ j.succ), },
simp only [comp_zero], },
rw [fintype.sum_eq_zero _ null],
simp only [add_zero],
/- finally, we study the remaining term which is induced by X.δ 0 -/
let eq := def_t 0,
rw [show (-1 : ℤ)^((0 : fin (n+2)) : ℕ) = 1, by ring] at eq,
rw one_smul at eq,
rw eq,
cases n; dsimp; simp,
end)
@[simp]
lemma inclusion_of_Moore_complex_map_f (X : simplicial_object A) (n : ℕ) :
(inclusion_of_Moore_complex_map X).f n = (normalized_Moore_complex.obj_X X n).arrow :=
chain_complex.of_hom_f _ _ _ _ _ _ _ _ n
variables (A)
/-- The inclusion map of the Moore complex in the alternating face map complex,
as a natural transformation -/
@[simps]
def inclusion_of_Moore_complex :
(normalized_Moore_complex A) ⟶ (alternating_face_map_complex A) :=
{ app := inclusion_of_Moore_complex_map, }
end algebraic_topology
|
8334050ff5ce89b861baf77bb1c4fe5103300268 | 9d2e3d5a2e2342a283affd97eead310c3b528a24 | /src/exercises_sources/thursday/morning/groups_rings_fields.lean | fba926c1fbb39635fa653558d04276febb9cda0c | [] | permissive | Vtec234/lftcm2020 | ad2610ab614beefe44acc5622bb4a7fff9a5ea46 | bbbd4c8162f8c2ef602300ab8fdeca231886375d | refs/heads/master | 1,668,808,098,623 | 1,594,989,081,000 | 1,594,990,079,000 | 280,423,039 | 0 | 0 | MIT | 1,594,990,209,000 | 1,594,990,209,000 | null | UTF-8 | Lean | false | false | 9,270 | lean | import linear_algebra.finite_dimensional
import ring_theory.algebraic
import data.zmod.basic
import data.real.basic
import tactic
/-!
```
____
/ ___|_ __ ___ _ _ _ __ ___
| | _| '__/ _ \| | | | '_ \/ __|
| |_| | | | (_) | |_| | |_) \__ \_
\____|_| \___/ \__,_| .__/|___( )
|_| |/
_ __ (_) _ __ __ _ ___
| '__| | | | '_ \ / _` | / __|
| | | | | | | | | (_| | \__ \ _
|_| |_| |_| |_| \__, | |___/ ( )
|___/ |/
_ __ _ _ _
__ _ _ __ __| | / _| (_) ___ | | __| | ___
/ _` | | '_ \ / _` | | |_ | | / _ \ | | / _` | / __|
| (_| | | | | | | (_| | | _| | | | __/ | | | (_| | \__ \
\__,_| |_| |_| \__,_| |_| |_| \___| |_| \__,_| |___/
```
-/
/-! ## Reminder on updating the exercises
These instructions are now available at:
https://leanprover-community.github.io/lftcm2020/exercises.html
To get a new copy of the exercises,
run the following commands in your terminal:
```
leanproject get lftcm2020
cp -r lftcm2020/src/exercises_sources/ lftcm2020/src/my_exercises
code lftcm2020
```
To update your exercise files, run the following commands:
```
cd /path/to/lftcm2020
git pull
leanproject get-mathlib-cache
```
Don’t forget to copy the updated files to `src/my_exercises`.
-/
/-! ## What do we have?
Too much to cover in detail in 10 minutes.
Take a look at the “General algebra” section on
https://leanprover-community.github.io/mathlib-overview.html
All the basic concepts are there:
`group`, `ring`, `field`, `module`, etc...
Also versions that are compatible with an ordering, like `ordered_ring`
And versions that express compatibility with a topology: `topological_group`
Finally constructions, like `polynomial R`, or `mv_polynomial σ R`,
or `monoid_algebra K G`, or `ℤ_[p]`, or `zmod n`, or `localization R S`.
-/
/-! ## Morphisms
We are in the middle of a transition to “bundled” morphisms.
(Why? Long story... but they work better with `simp`)
* `X → Y` -- ordinary function
* `X →+ Y` -- function respects `0` and `+`
* `X →* Y` -- function respects `1` and `*`
* `X →+* Y` -- function respects `0`, `1`, `+`, `*` (surprise!)
-/
section
variables {R S : Type*} [ring R] [ring S]
-- We used to write
example (f : R → S) [is_ring_hom f] : true := trivial
-- But now we write
example (f : R →+* S) : true := trivial
/-
This heavily relies on the “coercion to function”
that we have seen a couple of times this week.
-/
end
/-! ## Where are these things in the library?
`algebra/` for basic definitions and properties; “algebraic hierarchy”
`group_theory/` ⎫
`ring_theory/` ⎬ “advanced” and “specialised” material
`field_theory/` ⎭
`data/` definitions and examples
To give an idea:
* `algebra/ordered_ring.lean`
* `ring_theory/noetherian.lean`
* `field_theory/chevalley_warning.lean`
* `data/nat/*.lean`, `data/real/*.lean`, `data/padics/*.lean`
-/
/-! ## How to find things (search tools)
* `library_search` -- it often helps to carve out
the exact lemma statement that you are looking for
* online documentation: https://leanprover-community.github.io/mathlib_docs/
new search bar under construction
* Old-skool: `grep`
* Search in VS Code:
- `Ctrl - Shift - F`
-- don't forget to change settings, to search everywhere
-- click the three dots (`…`) below the search bar
-- disable the blue cogwheel
- `Ctrl - P` -- search for filenames
- `Ctrl - P`, `#` -- search for lemmas and definitions
-/
/-! ## How to find things (autocomplete)
Mathlib follows pretty strict naming conventions:
```
/-- The binomial theorem-/
theorem add_pow [comm_semiring α] (x y : α) (n : ℕ) :
(x + y) ^ n = ∑ m in range (n + 1), x ^ m * y ^ (n - m) * choose n m :=
(commute.all x y).add_pow n
```
After a while, you get the hang of this,
and you can start guessing names.
-/
open_locale big_operators -- nice notation ∑, ∏
open finset -- `finset.range n` is the finite set `{0,1,..., n-1}`
-- Demonstrate autocompletion
example (f : ℕ → ℝ) (n : ℕ) :
57 + ∑ i in range (n+1), f i = 57 + f n + ∑ i in range n, f i :=
begin
sorry
end
/-! ## How to find things (jump to definition)
Another good strategy for finding useful results about `X`,
is to “jump to the definition” and scroll through the next 3 screens of lemmas.
If you are looking for a basic fact about `X`, you will usually find it there.
-/
-- demonstrate “jump to definition”
#check polynomial.coeff
/-! ## Exercise 1
We will warm up with a well-known result:
“Subgroups of abelian groups are normal.”
Hints for proving this result:
* Notice that `normal` is a structure,
which you can see by going to the definition.
The `constructor` tactic will help you to get started.
-/
namespace add_subgroup
variables {A : Type*} [add_comm_group A]
lemma normal_of_add_comm_group (H : add_subgroup A) : normal H :=
begin
sorry
end
end add_subgroup
/-! ## Exercise 2
The following exercise will show the classical fact:
“Finite field extensions are algebraic.”
Hints for proving this result:
* Look up the definition of `finite_dimensional`.
* Search the library for useful lemmas about `is_algebraic` and `is_integral`.
-/
namespace algebra
variables {K L : Type*} [field K] [field L] [algebra K L] [finite_dimensional K L]
lemma is_algebraic_of_finite_dimensional : is_algebraic K L :=
begin
sorry
end
end algebra
/-! ## Exercise 3
In this exercise we will define the Frobenius morphism.
-/
section
variables (p : ℕ) [fact p.prime]
variables (K : Type*) [field K] [char_p K p]
/-! ### Subchallenge -/
lemma add_pow_char' (x y : K) : (x + y) ^ p = x ^ p + y ^ p :=
begin
-- Hint: `add_pow_char` already exists.
-- You can use it if you don't want to spend time on this.
/- Hints if you do want to attempt this:
* `finset.sum_range_succ`
* `finset.sum_eq_single`
* `nat.prime.ne_zero`
* `char_p.cast_eq_zero_iff`
* `nat.prime.dvd_choose_self`
-/
sorry
end
def frobenius_hom : K →+* K :=
{ to_fun := λ x, x^p,
map_zero' :=
begin
-- Hint: `zero_pow`, search for lemmas near `nat.prime`
sorry
end,
map_one' :=
begin
sorry
end,
map_mul' :=
begin
sorry
end,
map_add' :=
begin
-- Hint: `add_pow_char` -- can you prove that one yourself?
sorry
end }
end
/-! ## Exercise 4 [challenging]
The next exercise asks to show that a monic polynomial `f ∈ ℤ[X]` is irreducible
if it is irreducible modulo a prime `p`.
This fact is also not in mathlib.
Hint: prove the helper lemma that is stated first.
Follow-up question:
Can you generalise `irreducible_of_irreducible_mod_prime`?
-/
namespace polynomial
variables {R S : Type*} [semiring R] [integral_domain S] (φ : R →+* S)
/-
Useful library lemmas (in no particular order):
- `degree_eq_zero_of_is_unit`
- `eq_C_of_degree_eq_zero`
- `is_unit.map'`
- `leading_coeff_C`
- `degree_map_eq_of_leading_coeff_ne_zero`
- `is_unit.map'`
- `is_unit.ne_zero`
-/
lemma is_unit_of_is_unit_leading_coeff_of_is_unit_map
(f : polynomial R) (hf : is_unit (leading_coeff f)) (H : is_unit (map φ f)) :
is_unit f :=
begin
sorry
end
/-
Useful library lemmas (in no particular order):
- `is_unit.map'`
- `is_unit_of_mul_is_unit_left` (also `_right`)
- `leading_coeff_mul`
- `is_unit_of_is_unit_leading_coeff_of_is_unit_map` (the helper lemma we just proved)
- `is_unit_one`
-/
lemma irreducible_of_irreducible_mod_prime (f : polynomial ℤ) (p : ℕ) [fact p.prime]
(h_mon : monic f) (h_irr : irreducible (map (int.cast_ring_hom (zmod p)) f)) :
irreducible f :=
begin
sorry
end
end polynomial
-- SCROLL DOWN FOR THE BONUS EXERCISE
section
/-! ## Bonus exercise (wicked hard) -/
noncomputable theory -- because `polynomial` is noncomputable (implementation detail)
open polynomial -- we want to write `X`, instead of `polynomial.X`
/-
First we make some definitions
Scroll to the end for the actual exercise
-/
def partial_ramanujan_tau_polynomial (n : ℕ) : polynomial ℤ :=
X * ∏ k in finset.Ico 1 n, (1 - X^k)^24
def ramanujan_tau (n : ℕ) : ℤ :=
coeff (partial_ramanujan_tau_polynomial n) n
-- Some nice suggestive notation
prefix `τ`:300 := ramanujan_tau
/-
Some lemmas to warm up
Hint: unfold definitions, `simp`
-/
example : τ 0 = 0 :=
begin
sorry
end
example : τ 1 = 1 :=
begin
sorry
end
-- This one is nontrivial
-- Use `have : subresult,` or state helper lemmas and prove them first!
example : τ 2 = -24 :=
begin
-- Really, we ought to have a tactic that makes this easy
delta ramanujan_tau partial_ramanujan_tau_polynomial,
rw [mul_comm, coeff_mul_X],
suffices : ((1 - X) ^ 24 : polynomial ℤ).coeff 1 = -(24 : ℕ), by simpa,
generalize : (24 : ℕ) = n,
sorry
end
/-
The actual exercise. Good luck (-;
-/
theorem deligne (p : ℕ) (hp : p.prime) : (abs (τ p) : ℝ) ≤ 2 * p^(11/2) :=
begin
sorry
end
end
|
010a4c7e6a06931f720f62c7e7f36cadf59aab9f | 05b503addd423dd68145d68b8cde5cd595d74365 | /src/category_theory/concrete_category/bundled.lean | e521f49ac658eb7a3051b3ee2dea7777fc0cb603 | [
"Apache-2.0"
] | permissive | aestriplex/mathlib | 77513ff2b176d74a3bec114f33b519069788811d | e2fa8b2b1b732d7c25119229e3cdfba8370cb00f | refs/heads/master | 1,621,969,960,692 | 1,586,279,279,000 | 1,586,279,279,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,357 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Johannes Hölzl, Reid Barton, Sean Leather
Bundled types.
-/
import tactic.doc_commands
/-!
`bundled c` provides a uniform structure for bundling a type equipped with a type class.
We provide `category` instances for these in `unbundled_hom.lean` (for categories with unbundled
homs, e.g. topological spaces) and in `bundled_hom.lean` (for categories with bundled homs, e.g.
monoids).
-/
universes u v
namespace category_theory
variables {c d : Type u → Type v} {α : Type u}
/-- `bundled` is a type bundled with a type class instance for that type. Only
the type class is exposed as a parameter. -/
structure bundled (c : Type u → Type v) : Type (max (u+1) v) :=
(α : Type u)
(str : c α . tactic.apply_instance)
namespace bundled
/-- A generic function for lifting a type equipped with an instance to a bundled object. -/
-- Usually explicit instances will provide their own version of this, e.g. `Mon.of` and `Top.of`.
def of {c : Type u → Type v} (α : Type u) [str : c α] : bundled c := ⟨α, str⟩
/--
In order for simp lemmas for bundled morphisms to apply correctly,
it seems to be necessary for all the `has_coe_to_sort` instances for bundled categories
to be marked `[reducible]`.
Examples verifying correct behaviour are also marked with this
note [reducible has_coe_to_sort instances for bundled categories].
-/
library_note "reducible has_coe_to_sort instances for bundled categories"
/--
has_coe_to_sort instances for bundled categories must be reducible,
see note [reducible has_coe_to_sort instances for bundled categories].
-/
@[reducible]
instance : has_coe_to_sort (bundled c) :=
{ S := Type u, coe := bundled.α }
/-
`bundled.map` is reducible so that, if we define a category
def Ring : Type (u+1) := induced_category SemiRing (bundled.map @ring.to_semiring)
instance search is able to "see" that a morphism R ⟶ S in Ring is really
a (semi)ring homomorphism from R.α to S.α, and not merely from
`(bundled.map @ring.to_semiring R).α` to `(bundled.map @ring.to_semiring S).α`.
-/
/-- Map over the bundled structure -/
@[reducible] def map (f : Π {α}, c α → d α) (b : bundled c) : bundled d :=
⟨b.α, f b.str⟩
end bundled
end category_theory
|
9591ac4310734c78b6cecd8e9018998cb463ab78 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/topology/category/Top/opens.lean | dbee66e67e784d84f371f2ee6eae044c5b1ae6f9 | [
"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 | 7,867 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import topology.category.Top.basic
import category_theory.eq_to_hom
/-!
# The category of open sets in a topological space.
We define `to_Top : opens X ⥤ Top` and
`map (f : X ⟶ Y) : opens Y ⥤ opens X`, given by taking preimages of open sets.
Unfortunately `opens` isn't (usefully) a functor `Top ⥤ Cat`.
(One can in fact define such a functor,
but using it results in unresolvable `eq.rec` terms in goals.)
Really it's a 2-functor from (spaces, continuous functions, equalities)
to (categories, functors, natural isomorphisms).
We don't attempt to set up the full theory here, but do provide the natural isomorphisms
`map_id : map (𝟙 X) ≅ 𝟭 (opens X)` and
`map_comp : map (f ≫ g) ≅ map g ⋙ map f`.
Beyond that, there's a collection of simp lemmas for working with these constructions.
-/
open category_theory
open topological_space
open opposite
universe u
namespace topological_space.opens
variables {X Y Z : Top.{u}}
/-!
Since `opens X` has a partial order, it automatically receives a `category` instance.
Unfortunately, because we do not allow morphisms in `Prop`,
the morphisms `U ⟶ V` are not just proofs `U ≤ V`, but rather
`ulift (plift (U ≤ V))`.
-/
instance opens_hom_has_coe_to_fun {U V : opens X} : has_coe_to_fun (U ⟶ V) :=
{ F := λ f, U → V,
coe := λ f x, ⟨x, f.le x.2⟩ }
/-!
We now construct as morphisms various inclusions of open sets.
-/
-- This is tedious, but necessary because we decided not to allow Prop as morphisms in a category...
/--
The inclusion `U ⊓ V ⟶ U` as a morphism in the category of open sets.
-/
def inf_le_left (U V : opens X) : U ⊓ V ⟶ U := inf_le_left.hom
/--
The inclusion `U ⊓ V ⟶ V` as a morphism in the category of open sets.
-/
def inf_le_right (U V : opens X) : U ⊓ V ⟶ V := inf_le_right.hom
/--
The inclusion `U i ⟶ supr U` as a morphism in the category of open sets.
-/
def le_supr {ι : Type*} (U : ι → opens X) (i : ι) : U i ⟶ supr U := (le_supr U i).hom
/--
The inclusion `⊥ ⟶ U` as a morphism in the category of open sets.
-/
def bot_le (U : opens X) : ⊥ ⟶ U := bot_le.hom
/--
The inclusion `U ⟶ ⊤` as a morphism in the category of open sets.
-/
def le_top (U : opens X) : U ⟶ ⊤ := le_top.hom
-- We do not mark this as a simp lemma because it breaks open `x`.
-- Nevertheless, it is useful in `sheaf_of_functions`.
lemma inf_le_left_apply (U V : opens X) (x) :
(inf_le_left U V) x = ⟨x.1, (@_root_.inf_le_left _ _ U V : _ ≤ _) x.2⟩ :=
rfl
@[simp]
lemma inf_le_left_apply_mk (U V : opens X) (x) (m) :
(inf_le_left U V) ⟨x, m⟩ = ⟨x, (@_root_.inf_le_left _ _ U V : _ ≤ _) m⟩ :=
rfl
@[simp]
lemma le_supr_apply_mk {ι : Type*} (U : ι → opens X) (i : ι) (x) (m) :
(le_supr U i) ⟨x, m⟩ = ⟨x, (_root_.le_supr U i : _) m⟩ :=
rfl
/--
The functor from open sets in `X` to `Top`,
realising each open set as a topological space itself.
-/
def to_Top (X : Top.{u}) : opens X ⥤ Top :=
{ obj := λ U, ⟨U.val, infer_instance⟩,
map := λ U V i, ⟨λ x, ⟨x.1, i.le x.2⟩,
(embedding.continuous_iff embedding_subtype_coe).2 continuous_induced_dom⟩ }
@[simp]
lemma to_Top_map (X : Top.{u}) {U V : opens X} {f : U ⟶ V} {x} {h} :
((to_Top X).map f) ⟨x, h⟩ = ⟨x, f.le h⟩ :=
rfl
/--
The inclusion map from an open subset to the whole space, as a morphism in `Top`.
-/
@[simps]
def inclusion {X : Top.{u}} (U : opens X) : (to_Top X).obj U ⟶ X :=
{ to_fun := _,
continuous_to_fun := continuous_subtype_coe }
lemma open_embedding {X : Top.{u}} (U : opens X) : open_embedding (inclusion U) :=
is_open.open_embedding_subtype_coe U.2
/-- `opens.map f` gives the functor from open sets in Y to open set in X,
given by taking preimages under f. -/
def map (f : X ⟶ Y) : opens Y ⥤ opens X :=
{ obj := λ U, ⟨ f ⁻¹' U.val, U.property.preimage f.continuous ⟩,
map := λ U V i, ⟨ ⟨ λ a b, i.le b ⟩ ⟩ }.
@[simp] lemma map_obj (f : X ⟶ Y) (U) (p) :
(map f).obj ⟨U, p⟩ = ⟨f ⁻¹' U, p.preimage f.continuous⟩ := rfl
@[simp] lemma map_id_obj (U : opens X) : (map (𝟙 X)).obj U = U :=
by { ext, refl } -- not quite `rfl`, since we don't have eta for records
@[simp] lemma map_id_obj' (U) (p) : (map (𝟙 X)).obj ⟨U, p⟩ = ⟨U, p⟩ :=
rfl
@[simp] lemma map_id_obj_unop (U : (opens X)ᵒᵖ) : (map (𝟙 X)).obj (unop U) = unop U :=
by simp
@[simp] lemma op_map_id_obj (U : (opens X)ᵒᵖ) : (map (𝟙 X)).op.obj U = U :=
by simp
/--
The inclusion `U ⟶ (map f).obj ⊤` as a morphism in the category of open sets.
-/
def le_map_top (f : X ⟶ Y) (U : opens X) : U ⟶ (map f).obj ⊤ :=
hom_of_le $ λ _ _, trivial
@[simp] lemma map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) :
(map (f ≫ g)).obj U = (map f).obj ((map g).obj U) :=
by { ext, refl } -- not quite `rfl`, since we don't have eta for records
@[simp] lemma map_comp_obj' (f : X ⟶ Y) (g : Y ⟶ Z) (U) (p) :
(map (f ≫ g)).obj ⟨U, p⟩ = (map f).obj ((map g).obj ⟨U, p⟩) :=
rfl
@[simp] lemma map_comp_map (f : X ⟶ Y) (g : Y ⟶ Z) {U V} (i : U ⟶ V) :
(map (f ≫ g)).map i = (map f).map ((map g).map i) :=
rfl
@[simp] lemma map_comp_obj_unop (f : X ⟶ Y) (g : Y ⟶ Z) (U) :
(map (f ≫ g)).obj (unop U) = (map f).obj ((map g).obj (unop U)) :=
map_comp_obj f g (unop U)
@[simp] lemma op_map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) :
(map (f ≫ g)).op.obj U = (map f).op.obj ((map g).op.obj U) :=
by simp
section
variable (X)
/--
The functor `opens X ⥤ opens X` given by taking preimages under the identity function
is naturally isomorphic to the identity functor.
-/
@[simps]
def map_id : map (𝟙 X) ≅ 𝟭 (opens X) :=
{ hom := { app := λ U, eq_to_hom (map_id_obj U) },
inv := { app := λ U, eq_to_hom (map_id_obj U).symm } }
end
/--
The natural isomorphism between taking preimages under `f ≫ g`, and the composite
of taking preimages under `g`, then preimages under `f`.
-/
@[simps]
def map_comp (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map g ⋙ map f :=
{ hom := { app := λ U, eq_to_hom (map_comp_obj f g U) },
inv := { app := λ U, eq_to_hom (map_comp_obj f g U).symm } }
/--
If two continuous maps `f g : X ⟶ Y` are equal,
then the functors `opens Y ⥤ opens X` they induce are isomorphic.
-/
-- We could make `f g` implicit here, but it's nice to be able to see when
-- they are the identity (often!)
def map_iso (f g : X ⟶ Y) (h : f = g) : map f ≅ map g :=
nat_iso.of_components (λ U, eq_to_iso (congr_fun (congr_arg functor.obj (congr_arg map h)) U) )
(by obviously)
@[simp] lemma map_iso_refl (f : X ⟶ Y) (h) : map_iso f f h = iso.refl (map _) := rfl
@[simp] lemma map_iso_hom_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) :
(map_iso f g h).hom.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h)) U) :=
rfl
@[simp] lemma map_iso_inv_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) :
(map_iso f g h).inv.app U =
eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h.symm)) U) :=
rfl
end topological_space.opens
/--
An open map `f : X ⟶ Y` induces a functor `opens X ⥤ opens Y`.
-/
@[simps]
def is_open_map.functor {X Y : Top} {f : X ⟶ Y} (hf : is_open_map f) :
opens X ⥤ opens Y :=
{ obj := λ U, ⟨f '' U, hf U U.2⟩,
map := λ U V h, ⟨⟨set.image_subset _ h.down.down⟩⟩ }
/--
An open map `f : X ⟶ Y` induces an adjunction between `opens X` and `opens Y`.
-/
def is_open_map.adjunction {X Y : Top} {f : X ⟶ Y} (hf : is_open_map f) :
adjunction hf.functor (topological_space.opens.map f) :=
adjunction.mk_of_unit_counit
{ unit := { app := λ U, hom_of_le $ λ x hxU, ⟨x, hxU, rfl⟩ },
counit := { app := λ V, hom_of_le $ λ y ⟨x, hfxV, hxy⟩, hxy ▸ hfxV } }
|
88b8dae06af8a0b354254d92c08a45dbf3a0fabe | 36c7a18fd72e5b57229bd8ba36493daf536a19ce | /tests/lean/run/blast3.lean | 190015b5e9684b5012d2211922f3cde50dc768fa | [
"Apache-2.0"
] | permissive | YHVHvx/lean | 732bf0fb7a298cd7fe0f15d82f8e248c11db49e9 | 038369533e0136dd395dc252084d3c1853accbf2 | refs/heads/master | 1,610,701,080,210 | 1,449,128,595,000 | 1,449,128,595,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 844 | lean | set_option blast.init_depth 10
set_option blast.cc false
example (a b c : Prop) : b → c → b ∧ c :=
by blast
example (a b c : Prop) : b → c → c ∧ b :=
by blast
example (a b : Prop) : a → a ∨ b :=
by blast
example (a b : Prop) : b → a ∨ b :=
by blast
example (a b : Prop) : b → a ∨ a ∨ b :=
by blast
example (a b c : Prop) : b → c → a ∨ a ∨ (b ∧ c) :=
by blast
example (p q : nat → Prop) (a b : nat) : p a → q b → ∃ x, p x :=
by blast
example {A : Type} (p q : A → Prop) (a b : A) : q a → p b → ∃ x, p x :=
by blast
lemma foo₁ {A : Type} (p q : A → Prop) (a b : A) : q a → p b → ∃ x, (p x ∧ x = b) ∨ q x :=
by blast
lemma foo₂ {A : Type} (p q : A → Prop) (a b : A) : p b → ∃ x, q x ∨ (p x ∧ x = b) :=
by blast
reveal foo₁ foo₂
print foo₁
print foo₂
|
906d3618283ac2066c5337f41baae77cf51586f5 | d436468d80b739ba7e06843c4d0d2070e43448e5 | /src/data/nat/enat.lean | a2e516f098a2b2c1e64f0ca453769d899a7b13cf | [
"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 | 11,728 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
Natural numbers with infinity, represented as roption ℕ.
-/
import data.pfun algebra.ordered_group
import tactic.norm_cast tactic.norm_num
open roption lattice
def enat : Type := roption ℕ
namespace enat
instance : has_zero enat := ⟨some 0⟩
instance : has_one enat := ⟨some 1⟩
instance : has_add enat := ⟨λ x y, ⟨x.dom ∧ y.dom, λ h, get x h.1 + get y h.2⟩⟩
instance : has_coe ℕ enat := ⟨some⟩
instance (n : ℕ) : decidable (n : enat).dom := is_true trivial
@[simp] lemma coe_inj {x y : ℕ} : (x : enat) = y ↔ x = y := roption.some_inj
instance : add_comm_monoid enat :=
{ add := (+),
zero := (0),
add_comm := λ x y, roption.ext' and.comm (λ _ _, add_comm _ _),
zero_add := λ x, roption.ext' (true_and _) (λ _ _, zero_add _),
add_zero := λ x, roption.ext' (and_true _) (λ _ _, add_zero _),
add_assoc := λ x y z, roption.ext' and.assoc (λ _ _, add_assoc _ _ _) }
instance : has_le enat := ⟨λ x y, ∃ h : y.dom → x.dom, ∀ hy : y.dom, x.get (h hy) ≤ y.get hy⟩
instance : has_top enat := ⟨none⟩
instance : has_bot enat := ⟨0⟩
instance : has_sup enat := ⟨λ x y, ⟨x.dom ∧ y.dom, λ h, x.get h.1 ⊔ y.get h.2⟩⟩
@[elab_as_eliminator] protected lemma cases_on {P : enat → Prop} : ∀ a : enat,
P ⊤ → (∀ n : ℕ, P n) → P a :=
roption.induction_on
@[simp] lemma top_add (x : enat) : ⊤ + x = ⊤ :=
roption.ext' (false_and _) (λ h, h.left.elim)
@[simp] lemma add_top (x : enat) : x + ⊤ = ⊤ :=
by rw [add_comm, top_add]
@[simp, squash_cast] lemma coe_zero : ((0 : ℕ) : enat) = 0 := rfl
@[simp, squash_cast] lemma coe_one : ((1 : ℕ) : enat) = 1 := rfl
@[simp, move_cast] lemma coe_add (x y : ℕ) : ((x + y : ℕ) : enat) = x + y :=
roption.ext' (and_true _).symm (λ _ _, rfl)
@[simp] lemma coe_add_get {x : ℕ} {y : enat} (h : ((x : enat) + y).dom) :
get ((x : enat) + y) h = x + get y h.2 := rfl
@[simp] lemma get_add {x y : enat} (h : (x + y).dom) :
get (x + y) h = x.get h.1 + y.get h.2 := rfl
@[simp, squash_cast] lemma coe_get {x : enat} (h : x.dom) : (x.get h : enat) = x :=
roption.ext' (iff_of_true trivial h) (λ _ _, rfl)
@[simp] lemma get_zero (h : (0 : enat).dom) : (0 : enat).get h = 0 := rfl
@[simp] lemma get_one (h : (1 : enat).dom) : (1 : enat).get h = 1 := rfl
lemma dom_of_le_some {x : enat} {y : ℕ} : x ≤ y → x.dom :=
λ ⟨h, _⟩, h trivial
instance : partial_order enat :=
{ le := (≤),
le_refl := λ x, ⟨id, λ _, le_refl _⟩,
le_trans := λ x y z ⟨hxy₁, hxy₂⟩ ⟨hyz₁, hyz₂⟩,
⟨hxy₁ ∘ hyz₁, λ _, le_trans (hxy₂ _) (hyz₂ _)⟩,
le_antisymm := λ x y ⟨hxy₁, hxy₂⟩ ⟨hyx₁, hyx₂⟩, roption.ext' ⟨hyx₁, hxy₁⟩
(λ _ _, le_antisymm (hxy₂ _) (hyx₂ _)) }
@[simp, elim_cast] lemma coe_le_coe {x y : ℕ} : (x : enat) ≤ y ↔ x ≤ y :=
⟨λ ⟨_, h⟩, h trivial, λ h, ⟨λ _, trivial, λ _, h⟩⟩
@[simp, elim_cast] lemma coe_lt_coe {x y : ℕ} : (x : enat) < y ↔ x < y :=
by rw [lt_iff_le_not_le, lt_iff_le_not_le, coe_le_coe, coe_le_coe]
lemma get_le_get {x y : enat} {hx : x.dom} {hy : y.dom} :
x.get hx ≤ y.get hy ↔ x ≤ y :=
by conv { to_lhs, rw [← coe_le_coe, coe_get, coe_get]}
instance semilattice_sup_bot : semilattice_sup_bot enat :=
{ sup := (⊔),
bot := (⊥),
bot_le := λ _, ⟨λ _, trivial, λ _, nat.zero_le _⟩,
le_sup_left := λ _ _, ⟨and.left, λ _, le_sup_left⟩,
le_sup_right := λ _ _, ⟨and.right, λ _, le_sup_right⟩,
sup_le := λ x y z ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩, ⟨λ hz, ⟨hx₁ hz, hy₁ hz⟩,
λ _, sup_le (hx₂ _) (hy₂ _)⟩,
..enat.partial_order }
instance order_top : order_top enat :=
{ top := (⊤),
le_top := λ x, ⟨λ h, false.elim h, λ hy, false.elim hy⟩,
..enat.semilattice_sup_bot }
lemma top_eq_none : (⊤ : enat) = none := rfl
lemma coe_lt_top (x : ℕ) : (x : enat) < ⊤ :=
lt_of_le_of_ne le_top (λ h, absurd (congr_arg dom h) true_ne_false)
@[simp] lemma coe_ne_top (x : ℕ) : (x : enat) ≠ ⊤ := ne_of_lt (coe_lt_top x)
lemma ne_top_iff {x : enat} : x ≠ ⊤ ↔ ∃(n : ℕ), x = n := roption.ne_none_iff
lemma ne_top_of_lt {x y : enat} (h : x < y) : x ≠ ⊤ :=
ne_of_lt $ lt_of_lt_of_le h lattice.le_top
lemma pos_iff_one_le {x : enat} : 0 < x ↔ 1 ≤ x :=
enat.cases_on x ⟨λ _, le_top, λ _, coe_lt_top _⟩
(λ n, ⟨λ h, enat.coe_le_coe.2 (enat.coe_lt_coe.1 h),
λ h, enat.coe_lt_coe.2 (enat.coe_le_coe.1 h)⟩)
noncomputable instance : decidable_linear_order enat :=
{ le_total := λ x y, enat.cases_on x
(or.inr le_top) (enat.cases_on y (λ _, or.inl le_top)
(λ x y, (le_total x y).elim (or.inr ∘ coe_le_coe.2)
(or.inl ∘ coe_le_coe.2))),
decidable_le := classical.dec_rel _,
..enat.partial_order }
noncomputable instance : bounded_lattice enat :=
{ inf := min,
inf_le_left := min_le_left,
inf_le_right := min_le_right,
le_inf := λ _ _ _, le_min,
..enat.order_top,
..enat.semilattice_sup_bot }
lemma sup_eq_max {a b : enat} : a ⊔ b = max a b :=
le_antisymm (sup_le (le_max_left _ _) (le_max_right _ _))
(max_le le_sup_left le_sup_right)
lemma inf_eq_min {a b : enat} : a ⊓ b = min a b := rfl
instance : ordered_comm_monoid enat :=
{ add_le_add_left := λ a b ⟨h₁, h₂⟩ c,
enat.cases_on c (by simp)
(λ c, ⟨λ h, and.intro trivial (h₁ h.2),
λ _, add_le_add_left (h₂ _) c⟩),
lt_of_add_lt_add_left := λ a b c, enat.cases_on a
(λ h, by simpa [lt_irrefl] using h)
(λ a, enat.cases_on b
(λ h, absurd h (not_lt_of_ge (by rw add_top; exact le_top)))
(λ b, enat.cases_on c
(λ _, coe_lt_top _)
(λ c h, coe_lt_coe.2 (by rw [← coe_add, ← coe_add, coe_lt_coe] at h;
exact lt_of_add_lt_add_left h)))),
..enat.decidable_linear_order,
..enat.add_comm_monoid }
instance : canonically_ordered_monoid enat :=
{ le_iff_exists_add := λ a b, enat.cases_on b
(iff_of_true le_top ⟨⊤, (add_top _).symm⟩)
(λ b, enat.cases_on a
(iff_of_false (not_le_of_gt (coe_lt_top _))
(not_exists.2 (λ x, ne_of_lt (by rw [top_add]; exact coe_lt_top _))))
(λ a, ⟨λ h, ⟨(b - a : ℕ),
by rw [← coe_add, coe_inj, add_comm, nat.sub_add_cancel (coe_le_coe.1 h)]⟩,
(λ ⟨c, hc⟩, enat.cases_on c
(λ hc, hc.symm ▸ show (a : enat) ≤ a + ⊤, by rw [add_top]; exact le_top)
(λ c (hc : (b : enat) = a + c),
coe_le_coe.2 (by rw [← coe_add, coe_inj] at hc;
rw hc; exact nat.le_add_right _ _)) hc)⟩)),
..enat.semilattice_sup_bot,
..enat.ordered_comm_monoid }
protected lemma add_lt_add_right {x y z : enat} (h : x < y) (hz : z ≠ ⊤) : x + z < y + z :=
begin
rcases ne_top_iff.mp (ne_top_of_lt h) with ⟨m, rfl⟩,
rcases ne_top_iff.mp hz with ⟨k, rfl⟩,
induction y using enat.cases_on with n,
{ rw [top_add], apply_mod_cast coe_lt_top },
norm_cast at h, apply_mod_cast add_lt_add_right h
end
protected lemma add_lt_add_iff_right {x y z : enat} (hz : z ≠ ⊤) : x + z < y + z ↔ x < y :=
⟨lt_of_add_lt_add_right', λ h, enat.add_lt_add_right h hz⟩
protected lemma add_lt_add_iff_left {x y z : enat} (hz : z ≠ ⊤) : z + x < z + y ↔ x < y :=
by simpa using enat.add_lt_add_iff_right hz
protected lemma lt_add_iff_pos_right {x y : enat} (hx : x ≠ ⊤) : x < x + y ↔ 0 < y :=
by { conv_rhs { rw [← enat.add_lt_add_iff_left hx] }, rw [add_zero] }
lemma lt_add_one {x : enat} (hx : x ≠ ⊤) : x < x + 1 :=
by { rw [enat.lt_add_iff_pos_right hx], norm_cast, norm_num }
lemma le_of_lt_add_one {x y : enat} (h : x < y + 1) : x ≤ y :=
begin
induction y using enat.cases_on with n, apply lattice.le_top,
rcases ne_top_iff.mp (ne_top_of_lt h) with ⟨m, rfl⟩,
apply_mod_cast nat.le_of_lt_succ, apply_mod_cast h
end
lemma add_one_le_of_lt {x y : enat} (h : x < y) : x + 1 ≤ y :=
begin
induction y using enat.cases_on with n, apply lattice.le_top,
rcases ne_top_iff.mp (ne_top_of_lt h) with ⟨m, rfl⟩,
apply_mod_cast nat.succ_le_of_lt, apply_mod_cast h
end
lemma add_one_le_iff_lt {x y : enat} (hx : x ≠ ⊤) : x + 1 ≤ y ↔ x < y :=
begin
split, swap, exact add_one_le_of_lt,
intro h, rcases ne_top_iff.mp hx with ⟨m, rfl⟩,
induction y using enat.cases_on with n, apply coe_lt_top,
apply_mod_cast nat.lt_of_succ_le, apply_mod_cast h
end
lemma lt_add_one_iff_lt {x y : enat} (hx : x ≠ ⊤) : x < y + 1 ↔ x ≤ y :=
begin
split, exact le_of_lt_add_one,
intro h, rcases ne_top_iff.mp hx with ⟨m, rfl⟩,
induction y using enat.cases_on with n, { rw [top_add], apply coe_lt_top },
apply_mod_cast nat.lt_succ_of_le, apply_mod_cast h
end
section with_top
/-- Computably converts an `enat` to a `with_top ℕ`. -/
def to_with_top (x : enat) [decidable x.dom]: with_top ℕ := x.to_option
lemma to_with_top_top : to_with_top ⊤ = ⊤ := rfl
@[simp] lemma to_with_top_top' {h : decidable (⊤ : enat).dom} : to_with_top ⊤ = ⊤ :=
by convert to_with_top_top
lemma to_with_top_zero : to_with_top 0 = 0 := rfl
@[simp] lemma to_with_top_zero' {h : decidable (0 : enat).dom}: to_with_top 0 = 0 :=
by convert to_with_top_zero
lemma to_with_top_coe (n : ℕ) : to_with_top n = n := rfl
@[simp] lemma to_with_top_coe' (n : ℕ) {h : decidable (n : enat).dom} : to_with_top (n : enat) = n :=
by convert to_with_top_coe n
@[simp] lemma to_with_top_le {x y : enat} : Π [decidable x.dom]
[decidable y.dom], by exactI to_with_top x ≤ to_with_top y ↔ x ≤ y :=
enat.cases_on y (by simp) (enat.cases_on x (by simp) (by intros; simp))
@[simp] lemma to_with_top_lt {x y : enat} [decidable x.dom] [decidable y.dom] :
to_with_top x < to_with_top y ↔ x < y :=
by simp only [lt_iff_le_not_le, to_with_top_le]
end with_top
section with_top_equiv
open_locale classical
/-- Order isomorphism between `enat` and `with_top ℕ`. -/
noncomputable def with_top_equiv : enat ≃ with_top ℕ :=
{ to_fun := λ x, to_with_top x,
inv_fun := λ x, match x with (some n) := coe n | none := ⊤ end,
left_inv := λ x, by apply enat.cases_on x; intros; simp; refl,
right_inv := λ x, by cases x; simp [with_top_equiv._match_1]; refl }
@[simp] lemma with_top_equiv_top : with_top_equiv ⊤ = ⊤ :=
to_with_top_top'
@[simp] lemma with_top_equiv_coe (n : nat) : with_top_equiv n = n :=
to_with_top_coe' _
@[simp] lemma with_top_equiv_zero : with_top_equiv 0 = 0 :=
with_top_equiv_coe _
@[simp] lemma with_top_equiv_le {x y : enat} : with_top_equiv x ≤ with_top_equiv y ↔ x ≤ y :=
to_with_top_le
@[simp] lemma with_top_equiv_lt {x y : enat} : with_top_equiv x < with_top_equiv y ↔ x < y :=
to_with_top_lt
@[simp] lemma with_top_equiv_symm_top : with_top_equiv.symm ⊤ = ⊤ :=
rfl
@[simp] lemma with_top_equiv_symm_coe (n : nat) : with_top_equiv.symm n = n :=
rfl
@[simp] lemma with_top_equiv_symm_zero : with_top_equiv.symm 0 = 0 :=
rfl
@[simp] lemma with_top_equiv_symm_le {x y : with_top ℕ} :
with_top_equiv.symm x ≤ with_top_equiv.symm y ↔ x ≤ y :=
by rw ← with_top_equiv_le; simp
@[simp] lemma with_top_equiv_symm_lt {x y : with_top ℕ} :
with_top_equiv.symm x < with_top_equiv.symm y ↔ x < y :=
by rw ← with_top_equiv_lt; simp
end with_top_equiv
lemma lt_wf : well_founded ((<) : enat → enat → Prop) :=
show well_founded (λ a b : enat, a < b),
by haveI := classical.dec; simp only [to_with_top_lt.symm] {eta := ff};
exact inv_image.wf _ (with_top.well_founded_lt nat.lt_wf)
instance : has_well_founded enat := ⟨(<), lt_wf⟩
end enat
|
9a28dbc19d94e24e79270bb72aa713e2ee1a4482 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/algebra/category/Module/simple.lean | 7ca97e2ef7a9f97f51ac3d621cfb47f2541aca3c | [
"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 | 1,152 | lean | /-
Copyright (c) 2022 Pierre-Alexandre Bazin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Pierre-Alexandre Bazin, Scott Morrison
-/
import category_theory.simple
import algebra.category.Module.abelian
import algebra.category.Module.subobject
import ring_theory.simple_module
/-!
# Simple objects in the category of `R`-modules
We prove simple modules are exactly simple objects in the category of `R`-modules.
-/
variables {R M : Type*} [ring R] [add_comm_group M] [module R M]
open category_theory Module
lemma simple_iff_is_simple_module : simple (of R M) ↔ is_simple_module R M :=
(simple_iff_subobject_is_simple_order _).trans (subobject_Module (of R M)).is_simple_order_iff
/-- A simple module is a simple object in the category of modules. -/
instance simple_of_is_simple_module [is_simple_module R M] : simple (of R M) :=
simple_iff_is_simple_module.mpr ‹_›
/-- A simple object in the category of modules is a simple module. -/
instance is_simple_module_of_simple (M : Module R) [simple M] : is_simple_module R M :=
simple_iff_is_simple_module.mp (simple.of_iso (of_self_iso M))
|
3ac4a3636701a53d9b2cd4eac23ff6215b13dbfb | 46125763b4dbf50619e8846a1371029346f4c3db | /src/group_theory/submonoid.lean | c4563cd9c714d5ff876fa890abbdcac08b9706dc | [
"Apache-2.0"
] | permissive | thjread/mathlib | a9d97612cedc2c3101060737233df15abcdb9eb1 | 7cffe2520a5518bba19227a107078d83fa725ddc | refs/heads/master | 1,615,637,696,376 | 1,583,953,063,000 | 1,583,953,063,000 | 246,680,271 | 0 | 0 | Apache-2.0 | 1,583,960,875,000 | 1,583,960,875,000 | null | UTF-8 | Lean | false | false | 43,040 | 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, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston
-/
import algebra.big_operators
import data.finset
import data.equiv.algebra
/-!
# Submonoids
This file defines multiplicative and additive submonoids, first in an unbundled form (deprecated)
and then in a bundled form.
We prove submonoids of a monoid form a complete lattice, and results about images and preimages of
submonoids under monoid homomorphisms. For the unbundled submonoids, these theorems use unbundled
monoid homomorphisms (also deprecated), and the bundled versions use bundled monoid homomorphisms.
There are also theorems about the submonoids generated by an element or a subset of a monoid,
defined both inductively and as the infimum of the set of submonoids containing a given
element/subset.
## Implementation notes
Unbundled submonoids will slowly be removed from mathlib.
(Bundled) submonoid inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as
membership of a submonoid's underlying set.
## Tags
submonoid, submonoids, is_submonoid
-/
variables {M : Type*} [monoid M] {s : set M}
variables {A : Type*} [add_monoid A] {t : set A}
/-- `s` is an additive submonoid: a set containing 0 and closed under addition. -/
class is_add_submonoid (s : set A) : Prop :=
(zero_mem : (0:A) ∈ s)
(add_mem {a b} : a ∈ s → b ∈ s → a + b ∈ s)
/-- `s` is a submonoid: a set containing 1 and closed under multiplication. -/
@[to_additive is_add_submonoid]
class is_submonoid (s : set M) : Prop :=
(one_mem : (1:M) ∈ s)
(mul_mem {a b} : a ∈ s → b ∈ s → a * b ∈ s)
instance additive.is_add_submonoid
(s : set M) : ∀ [is_submonoid s], @is_add_submonoid (additive M) _ s
| ⟨h₁, h₂⟩ := ⟨h₁, @h₂⟩
theorem additive.is_add_submonoid_iff
{s : set M} : @is_add_submonoid (additive M) _ s ↔ is_submonoid s :=
⟨λ ⟨h₁, h₂⟩, ⟨h₁, @h₂⟩, λ h, by resetI; apply_instance⟩
instance multiplicative.is_submonoid
(s : set A) : ∀ [is_add_submonoid s], @is_submonoid (multiplicative A) _ s
| ⟨h₁, h₂⟩ := ⟨h₁, @h₂⟩
theorem multiplicative.is_submonoid_iff
{s : set A} : @is_submonoid (multiplicative A) _ s ↔ is_add_submonoid s :=
⟨λ ⟨h₁, h₂⟩, ⟨h₁, @h₂⟩, λ h, by resetI; apply_instance⟩
/-- The intersection of two submonoids of a monoid `M` is a submonoid of `M`. -/
@[to_additive "The intersection of two `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of M."]
instance is_submonoid.inter (s₁ s₂ : set M) [is_submonoid s₁] [is_submonoid s₂] :
is_submonoid (s₁ ∩ s₂) :=
{ one_mem := ⟨is_submonoid.one_mem _, is_submonoid.one_mem _⟩,
mul_mem := λ x y hx hy,
⟨is_submonoid.mul_mem hx.1 hy.1, is_submonoid.mul_mem hx.2 hy.2⟩ }
/-- The intersection of an indexed set of submonoids of a monoid `M` is a submonoid of `M`. -/
@[to_additive "The intersection of an indexed set of `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of `M`."]
instance is_submonoid.Inter {ι : Sort*} (s : ι → set M) [h : ∀ y : ι, is_submonoid (s y)] :
is_submonoid (set.Inter s) :=
{ one_mem := set.mem_Inter.2 $ λ y, is_submonoid.one_mem (s y),
mul_mem := λ x₁ x₂ h₁ h₂, set.mem_Inter.2 $
λ y, is_submonoid.mul_mem (set.mem_Inter.1 h₁ y) (set.mem_Inter.1 h₂ y) }
/-- The union of an indexed, directed, nonempty set of submonoids of a monoid `M` is a submonoid
of `M`. -/
@[to_additive is_add_submonoid_Union_of_directed "The union of an indexed, directed, nonempty set of `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of `M`. "]
lemma is_submonoid_Union_of_directed {ι : Type*} [hι : nonempty ι]
(s : ι → set M) [∀ i, is_submonoid (s i)]
(directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) :
is_submonoid (⋃i, s i) :=
{ one_mem := let ⟨i⟩ := hι in set.mem_Union.2 ⟨i, is_submonoid.one_mem _⟩,
mul_mem := λ a b ha hb,
let ⟨i, hi⟩ := set.mem_Union.1 ha in
let ⟨j, hj⟩ := set.mem_Union.1 hb in
let ⟨k, hk⟩ := directed i j in
set.mem_Union.2 ⟨k, is_submonoid.mul_mem (hk.1 hi) (hk.2 hj)⟩ }
section powers
/-- The set of natural number powers `1, x, x², ...` of an element `x` of a monoid. -/
def powers (x : M) : set M := {y | ∃ n:ℕ, x^n = y}
/-- The set of natural number multiples `0, x, 2x, ...` of an element `x` of an `add_monoid`. -/
def multiples (x : A) : set A := {y | ∃ n:ℕ, add_monoid.smul n x = y}
attribute [to_additive multiples] powers
/-- 1 is in the set of natural number powers of an element of a monoid. -/
lemma powers.one_mem {x : M} : (1 : M) ∈ powers x := ⟨0, pow_zero _⟩
/-- 0 is in the set of natural number multiples of an element of an `add_monoid`. -/
lemma multiples.zero_mem {x : A} : (0 : A) ∈ multiples x := ⟨0, add_monoid.zero_smul _⟩
attribute [to_additive] powers.one_mem
/-- An element of a monoid is in the set of that element's natural number powers. -/
lemma powers.self_mem {x : M} : x ∈ powers x := ⟨1, pow_one _⟩
/-- An element of an `add_monoid` is in the set of that element's natural number multiples. -/
lemma multiples.self_mem {x : A} : x ∈ multiples x := ⟨1, add_monoid.one_smul _⟩
attribute [to_additive] powers.self_mem
/-- The set of natural number powers of an element of a monoid is closed under multiplication. -/
lemma powers.mul_mem {x y z : M} : (y ∈ powers x) → (z ∈ powers x) → (y * z ∈ powers x) :=
λ ⟨n₁, h₁⟩ ⟨n₂, h₂⟩, ⟨n₁ + n₂, by simp only [pow_add, *]⟩
/-- The set of natural number multiples of an element of an `add_monoid` is closed under
addition. -/
lemma multiples.add_mem {x y z : A} :
(y ∈ multiples x) → (z ∈ multiples x) → (y + z ∈ multiples x) :=
@powers.mul_mem (multiplicative A) _ _ _ _
attribute [to_additive] powers.mul_mem
/-- The set of natural number powers of an element of a monoid `M` is a submonoid of `M`. -/
@[to_additive is_add_submonoid "The set of natural number multiples of an element of an `add_monoid` `M` is an `add_submonoid` of `M`."]
instance powers.is_submonoid (x : M) : is_submonoid (powers x) :=
{ one_mem := powers.one_mem,
mul_mem := λ y z, powers.mul_mem }
/-- A monoid is a submonoid of itself. -/
@[to_additive is_add_submonoid "An `add_monoid` is an `add_submonoid` of itself."]
instance univ.is_submonoid : is_submonoid (@set.univ M) := by split; simp
/-- The preimage of a submonoid under a monoid hom is a submonoid of the domain. -/
@[to_additive is_add_submonoid "The preimage of an `add_submonoid` under an `add_monoid` hom is an `add_submonoid` of the domain."]
instance preimage.is_submonoid {N : Type*} [monoid N] (f : M → N) [is_monoid_hom f]
(s : set N) [is_submonoid s] : is_submonoid (f ⁻¹' s) :=
{ one_mem := show f 1 ∈ s, by rw is_monoid_hom.map_one f; exact is_submonoid.one_mem s,
mul_mem := λ a b (ha : f a ∈ s) (hb : f b ∈ s),
show f (a * b) ∈ s, by rw is_monoid_hom.map_mul f; exact is_submonoid.mul_mem ha hb }
/-- The image of a submonoid under a monoid hom is a submonoid of the codomain. -/
@[instance, to_additive is_add_submonoid "The image of an `add_submonoid` under an `add_monoid` hom is an `add_submonoid` of the codomain."]
lemma image.is_submonoid {γ : Type*} [monoid γ] (f : M → γ) [is_monoid_hom f]
(s : set M) [is_submonoid s] : is_submonoid (f '' s) :=
{ one_mem := ⟨1, is_submonoid.one_mem s, is_monoid_hom.map_one f⟩,
mul_mem := λ a b ⟨x, hx⟩ ⟨y, hy⟩, ⟨x * y, is_submonoid.mul_mem hx.1 hy.1,
by rw [is_monoid_hom.map_mul f, hx.2, hy.2]⟩ }
/-- The image of a monoid hom is a submonoid of the codomain. -/
@[to_additive is_add_submonoid "The image of an `add_monoid` hom is an `add_submonoid` of the codomain."]
instance range.is_submonoid {γ : Type*} [monoid γ] (f : M → γ) [is_monoid_hom f] :
is_submonoid (set.range f) :=
by rw ← set.image_univ; apply_instance
/-- Submonoids are closed under natural powers. -/
lemma is_submonoid.pow_mem {a : M} [is_submonoid s] (h : a ∈ s) : ∀ {n : ℕ}, a ^ n ∈ s
| 0 := is_submonoid.one_mem s
| (n + 1) := is_submonoid.mul_mem h is_submonoid.pow_mem
/-- An `add_submonoid` is closed under multiplication by naturals. -/
lemma is_add_submonoid.smul_mem {a : A} [is_add_submonoid t] :
∀ (h : a ∈ t) {n : ℕ}, add_monoid.smul n a ∈ t :=
@is_submonoid.pow_mem (multiplicative A) _ _ _ _
attribute [to_additive smul_mem] is_submonoid.pow_mem
/-- The set of natural number powers of an element of a submonoid is a subset of the submonoid. -/
lemma is_submonoid.power_subset {a : M} [is_submonoid s] (h : a ∈ s) : powers a ⊆ s :=
assume x ⟨n, hx⟩, hx ▸ is_submonoid.pow_mem h
/-- The set of natural number multiples of an element of an `add_submonoid` is a subset of the
`add_submonoid`. -/
lemma is_add_submonoid.multiple_subset {a : A} [is_add_submonoid t] :
a ∈ t → multiples a ⊆ t :=
@is_submonoid.power_subset (multiplicative A) _ _ _ _
attribute [to_additive multiple_subset] is_submonoid.power_subset
end powers
namespace is_submonoid
/-- The product of a list of elements of a submonoid is an element of the submonoid. -/
@[to_additive "The sum of a list of elements of an `add_submonoid` is an element of the `add_submonoid`."]
lemma list_prod_mem [is_submonoid s] : ∀{l : list M}, (∀x∈l, x ∈ s) → l.prod ∈ s
| [] h := one_mem s
| (a::l) h :=
suffices a * l.prod ∈ s, by simpa,
have a ∈ s ∧ (∀x∈l, x ∈ s), by simpa using h,
is_submonoid.mul_mem this.1 (list_prod_mem this.2)
/-- The product of a multiset of elements of a submonoid of a `comm_monoid` is an element of the submonoid. -/
@[to_additive "The sum of a multiset of elements of an `add_submonoid` of an `add_comm_monoid` is an element of the `add_submonoid`. "]
lemma multiset_prod_mem {M} [comm_monoid M] (s : set M) [is_submonoid s] (m : multiset M) :
(∀a∈m, a ∈ s) → m.prod ∈ s :=
begin
refine quotient.induction_on m (assume l hl, _),
rw [multiset.quot_mk_to_coe, multiset.coe_prod],
exact list_prod_mem hl
end
/-- The product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is an element of the submonoid. -/
@[to_additive "The sum of elements of an `add_submonoid` of an `add_comm_monoid` indexed by a `finset` is an element of the `add_submonoid`."]
lemma finset_prod_mem {M A} [comm_monoid M] (s : set M) [is_submonoid s] (f : A → M) :
∀(t : finset A), (∀b∈t, f b ∈ s) → t.prod f ∈ s
| ⟨m, hm⟩ hs :=
begin
refine multiset_prod_mem s _ _,
simp,
rintros a b hb rfl,
exact hs _ hb
end
end is_submonoid
-- TODO: modify `subtype_instance` to produce this definition, then use it here
-- and for `subtype.group`
/-- Submonoids are themselves monoids. -/
@[to_additive add_monoid "An `add_submonoid` is itself an `add_monoid`."]
instance subtype.monoid {s : set M} [is_submonoid s] : monoid s :=
{ one := ⟨1, is_submonoid.one_mem s⟩,
mul := λ x y, ⟨x * y, is_submonoid.mul_mem x.2 y.2⟩,
mul_one := λ x, subtype.eq $ mul_one x.1,
one_mul := λ x, subtype.eq $ one_mul x.1,
mul_assoc := λ x y z, subtype.eq $ mul_assoc x.1 y.1 z.1 }
/-- Submonoids of commutative monoids are themselves commutative monoids. -/
@[to_additive add_comm_monoid "An `add_submonoid` of a commutative `add_monoid` is itself a commutative `add_monoid`. "]
instance subtype.comm_monoid {M} [comm_monoid M] {s : set M} [is_submonoid s] : comm_monoid s :=
{ mul_comm := λ x y, subtype.eq $ mul_comm x.1 y.1,
.. subtype.monoid }
/-- Submonoids inherit the 1 of the monoid. -/
@[simp, to_additive "An `add_submonoid` inherits the 0 of the `add_monoid`. "]
lemma is_submonoid.coe_one [is_submonoid s] : ((1 : s) : M) = 1 := rfl
/-- Submonoids inherit the multiplication of the monoid. -/
@[simp, to_additive "An `add_submonoid` inherits the addition of the `add_monoid`. "]
lemma is_submonoid.coe_mul [is_submonoid s] (a b : s) : ((a * b : s) : M) = a * b := rfl
/-- Submonoids inherit the exponentiation by naturals of the monoid. -/
@[simp] lemma is_submonoid.coe_pow [is_submonoid s] (a : s) (n : ℕ) :
((a ^ n : s) : M) = a ^ n :=
by induction n; simp [*, pow_succ]
/-- An `add_submonoid` inherits the multiplication by naturals of the `add_monoid`. -/
@[simp] lemma is_add_submonoid.smul_coe {A : Type*} [add_monoid A] {s : set A}
[is_add_submonoid s] (a : s) (n : ℕ) : ((add_monoid.smul n a : s) : A) = add_monoid.smul n a :=
by {induction n, refl, simp [*, succ_smul]}
attribute [to_additive smul_coe] is_submonoid.coe_pow
/-- The natural injection from a submonoid into the monoid is a monoid hom. -/
@[to_additive is_add_monoid_hom "The natural injection from an `add_submonoid` into the `add_monoid` is an `add_monoid` hom. "]
instance subtype_val.is_monoid_hom [is_submonoid s] : is_monoid_hom (subtype.val : s → M) :=
{ map_one := rfl, map_mul := λ _ _, rfl }
/-- The natural injection from a submonoid into the monoid is a monoid hom. -/
@[to_additive is_add_monoid_hom "The natural injection from an `add_submonoid` into the `add_monoid` is an `add_monoid` hom. "]
instance coe.is_monoid_hom [is_submonoid s] : is_monoid_hom (coe : s → M) :=
subtype_val.is_monoid_hom
/-- Given a monoid hom `f : γ → M` whose image is contained in a submonoid `s`, the induced map
from `γ` to `s` is a monoid hom. -/
@[to_additive is_add_monoid_hom "Given an `add_monoid` hom `f : γ → M` whose image is contained in an `add_submonoid` s, the induced map from `γ` to `s` is an `add_monoid` hom."]
instance subtype_mk.is_monoid_hom {γ : Type*} [monoid γ] [is_submonoid s] (f : γ → M)
[is_monoid_hom f] (h : ∀ x, f x ∈ s) : is_monoid_hom (λ x, (⟨f x, h x⟩ : s)) :=
{ map_one := subtype.eq (is_monoid_hom.map_one f),
map_mul := λ x y, subtype.eq (is_monoid_hom.map_mul f x y) }
/-- Given two submonoids `s` and `t` such that `s ⊆ t`, the natural injection from `s` into `t` is
a monoid hom. -/
@[to_additive is_add_monoid_hom "Given two `add_submonoid`s `s` and `t` such that `s ⊆ t`, the natural injection from `s` into `t` is an `add_monoid` hom."]
instance set_inclusion.is_monoid_hom (t : set M) [is_submonoid s] [is_submonoid t] (h : s ⊆ t) :
is_monoid_hom (set.inclusion h) :=
subtype_mk.is_monoid_hom _ _
namespace add_monoid
/-- The inductively defined membership predicate for the submonoid generated by a subset of a
monoid. -/
inductive in_closure (s : set A) : A → Prop
| basic {a : A} : a ∈ s → in_closure a
| zero : in_closure 0
| add {a b : A} : in_closure a → in_closure b → in_closure (a + b)
end add_monoid
namespace monoid
/-- The inductively defined membership predicate for the `add_submonoid` generated by a subset of an
add_monoid. -/
inductive in_closure (s : set M) : M → Prop
| basic {a : M} : a ∈ s → in_closure a
| one : in_closure 1
| mul {a b : M} : in_closure a → in_closure b → in_closure (a * b)
attribute [to_additive] monoid.in_closure
attribute [to_additive] monoid.in_closure.one
attribute [to_additive] monoid.in_closure.mul
/-- The inductively defined submonoid generated by a subset of a monoid. -/
@[to_additive "The inductively defined `add_submonoid` genrated by a subset of an `add_monoid`."]
def closure (s : set M) : set M := {a | in_closure s a }
@[to_additive is_add_submonoid]
instance closure.is_submonoid (s : set M) : is_submonoid (closure s) :=
{ one_mem := in_closure.one s, mul_mem := assume a b, in_closure.mul }
/-- A subset of a monoid is contained in the submonoid it generates. -/
@[to_additive "A subset of an `add_monoid` is contained in the `add_submonoid` it generates."]
theorem subset_closure {s : set M} : s ⊆ closure s :=
assume a, in_closure.basic
/-- The submonoid generated by a set is contained in any submonoid that contains the set. -/
@[to_additive "The `add_submonoid` generated by a set is contained in any `add_submonoid` that contains the set."]
theorem closure_subset {s t : set M} [is_submonoid t] (h : s ⊆ t) : closure s ⊆ t :=
assume a ha, by induction ha; simp [h _, *, is_submonoid.one_mem, is_submonoid.mul_mem]
/-- Given subsets `t` and `s` of a monoid `M`, if `s ⊆ t`, the submonoid of `M` generated by `s` is
contained in the submonoid generated by `t`. -/
@[to_additive "Given subsets `t` and `s` of an `add_monoid M`, if `s ⊆ t`, the `add_submonoid` of `M` generated by `s` is contained in the `add_submonoid` generated by `t`."]
theorem closure_mono {s t : set M} (h : s ⊆ t) : closure s ⊆ closure t :=
closure_subset $ set.subset.trans h subset_closure
/-- The submonoid generated by an element of a monoid equals the set of natural number powers of
the element. -/
@[to_additive "The `add_submonoid` generated by an element of an `add_monoid` equals the set of natural number multiples of the element."]
theorem closure_singleton {x : M} : closure ({x} : set M) = powers x :=
set.eq_of_subset_of_subset (closure_subset $ set.singleton_subset_iff.2 $ powers.self_mem) $
is_submonoid.power_subset $ set.singleton_subset_iff.1 $ subset_closure
/-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated
by the image of the set under the monoid hom. -/
@[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals the `add_submonoid` generated by the image of the set under the `add_monoid` hom."]
lemma image_closure {A : Type*} [monoid A] (f : M → A) [is_monoid_hom f] (s : set M) :
f '' closure s = closure (f '' s) :=
le_antisymm
begin
rintros _ ⟨x, hx, rfl⟩,
apply in_closure.rec_on hx; intros,
{ solve_by_elim [subset_closure, set.mem_image_of_mem] },
{ rw [is_monoid_hom.map_one f], apply is_submonoid.one_mem },
{ rw [is_monoid_hom.map_mul f], solve_by_elim [is_submonoid.mul_mem] }
end
(closure_subset $ set.image_subset _ subset_closure)
/-- Given an element `a` of the submonoid of a monoid `M` generated by a set `s`, there exists a list of
elements of `s` whose product is `a`. -/
@[to_additive "Given an element `a` of the `add_submonoid` of an `add_monoid M` generated by a set `s`, there exists a list of elements of `s` whose sum is `a`."]
theorem exists_list_of_mem_closure {s : set M} {a : M} (h : a ∈ closure s) :
(∃l:list M, (∀x∈l, x ∈ s) ∧ l.prod = a) :=
begin
induction h,
case in_closure.basic : a ha { existsi ([a]), simp [ha] },
case in_closure.one { existsi ([]), simp },
case in_closure.mul : a b _ _ ha hb {
rcases ha with ⟨la, ha, eqa⟩,
rcases hb with ⟨lb, hb, eqb⟩,
existsi (la ++ lb),
simp [eqa.symm, eqb.symm, or_imp_distrib],
exact assume a, ⟨ha a, hb a⟩
}
end
/-- Given sets `s, t` of a commutative monoid `M`, `x ∈ M` is in the submonoid of `M` generated by
`s ∪ t` iff there exists an element of the submonoid generated by `s` and an element of the
submonoid generated by `t` whose product is `x`. -/
@[to_additive "Given sets `s, t` of a commutative `add_monoid M`, `x ∈ M` is in the `add_submonoid` of `M` generated by `s ∪ t` iff there exists an element of the `add_submonoid` generated by `s` and an element of the `add_submonoid` generated by `t` whose sum is `x`."]
theorem mem_closure_union_iff {M : Type*} [comm_monoid M] {s t : set M} {x : M} :
x ∈ closure (s ∪ t) ↔ ∃ y ∈ closure s, ∃ z ∈ closure t, y * z = x :=
⟨λ hx, let ⟨L, HL1, HL2⟩ := exists_list_of_mem_closure hx in HL2 ▸
list.rec_on L (λ _, ⟨1, is_submonoid.one_mem _, 1, is_submonoid.one_mem _, mul_one _⟩)
(λ hd tl ih HL1, let ⟨y, hy, z, hz, hyzx⟩ := ih (list.forall_mem_of_forall_mem_cons HL1) in
or.cases_on (HL1 hd $ list.mem_cons_self _ _)
(λ hs, ⟨hd * y, is_submonoid.mul_mem (subset_closure hs) hy, z, hz, by rw [mul_assoc, list.prod_cons, ← hyzx]; refl⟩)
(λ ht, ⟨y, hy, z * hd, is_submonoid.mul_mem hz (subset_closure ht), by rw [← mul_assoc, list.prod_cons, ← hyzx, mul_comm hd]; refl⟩)) HL1,
λ ⟨y, hy, z, hz, hyzx⟩, hyzx ▸ is_submonoid.mul_mem (closure_mono (set.subset_union_left _ _) hy)
(closure_mono (set.subset_union_right _ _) hz)⟩
end monoid
-- Bundled submonoids and `add_submonoid`s
/-- A submonoid of a monoid `M` is a subset containing 1 and closed under multiplication. -/
structure submonoid (M : Type*) [monoid M] :=
(carrier : set M)
(one_mem' : (1 : M) ∈ carrier)
(mul_mem' {a b} : a ∈ carrier → b ∈ carrier → a * b ∈ carrier)
/-- An additive submonoid of an additive monoid `M` is a subset containing 0 and
closed under addition. -/
structure add_submonoid (M : Type*) [add_monoid M] :=
(carrier : set M)
(zero_mem' : (0 : M) ∈ carrier)
(add_mem' {a b} : a ∈ carrier → b ∈ carrier → a + b ∈ carrier)
attribute [to_additive add_submonoid] submonoid
/-- Map from submonoids of monoid `M` to `add_submonoid`s of `additive M`. -/
def submonoid.to_add_submonoid {M : Type*} [monoid M] (S : submonoid M) :
add_submonoid (additive M) :=
{ carrier := S.carrier,
zero_mem' := S.one_mem',
add_mem' := S.mul_mem' }
/-- Map from `add_submonoid`s of `additive M` to submonoids of `M`. -/
def submonoid.of_add_submonoid {M : Type*} [monoid M] (S : add_submonoid (additive M)) :
submonoid M :=
{ carrier := S.carrier,
one_mem' := S.zero_mem',
mul_mem' := S.add_mem' }
/-- Map from `add_submonoid`s of `add_monoid M` to submonoids of `multiplicative M`. -/
def add_submonoid.to_submonoid {M : Type*} [add_monoid M] (S : add_submonoid M) :
submonoid (multiplicative M) :=
{ carrier := S.carrier,
one_mem' := S.zero_mem',
mul_mem' := S.add_mem' }
/-- Map from submonoids of `multiplicative M` to `add_submonoid`s of `add_monoid M`. -/
def add_submonoid.of_submonoid {M : Type*} [add_monoid M] (S : submonoid (multiplicative M)) :
add_submonoid M :=
{ carrier := S.carrier,
zero_mem' := S.one_mem',
add_mem' := S.mul_mem' }
/-- Submonoids of monoid `M` are isomorphic to additive submonoids of `additive M`. -/
def submonoid.add_submonoid_equiv (M : Type*) [monoid M] :
submonoid M ≃ add_submonoid (additive M) :=
{ to_fun := submonoid.to_add_submonoid,
inv_fun := submonoid.of_add_submonoid,
left_inv := λ x, by cases x; refl,
right_inv := λ x, by cases x; refl }
namespace submonoid
variables (S : submonoid M)
@[to_additive]
instance : has_coe (submonoid M) (set M) := ⟨submonoid.carrier⟩
@[to_additive]
instance : has_mem M (submonoid M) := ⟨λ m S, m ∈ S.carrier⟩
@[to_additive]
instance : has_le (submonoid M) := ⟨λ S T, S.carrier ⊆ T.carrier⟩
@[simp, to_additive]
lemma mem_coe {m : M} : m ∈ (S : set M) ↔ m ∈ S := iff.rfl
/-- Two submonoids are equal if the underlying subsets are equal. -/
@[to_additive "Two `add_submonoid`s are equal if the underlying subsets are equal."]
theorem ext' {S T : submonoid M} (h : (S : set M) = T) : S = T :=
by cases S; cases T; congr'
/-- Two submonoids are equal if and only if the underlying subsets are equal. -/
@[to_additive "Two `add_submonoid`s are equal if and only if the underlying subsets are equal."]
protected theorem ext'_iff {S T : submonoid M} : (S : set M) = T ↔ S = T :=
⟨ext', λ h, h ▸ rfl⟩
/-- Two submonoids are equal if they have the same elements. -/
@[ext, to_additive "Two `add_submonoid`s are equal if they have the same elements."]
theorem ext {S T : submonoid M}
(h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h
attribute [ext] add_submonoid.ext
/-- A submonoid contains the monoid's 1. -/
@[to_additive "An `add_submonoid` contains the monoid's 0."]
theorem one_mem : (1 : M) ∈ S := S.one_mem'
/-- A submonoid is closed under multiplication. -/
@[to_additive "An `add_submonoid` is closed under addition."]
theorem mul_mem {x y : M} : x ∈ S → y ∈ S → x * y ∈ S := submonoid.mul_mem' S
/-- A finite product of elements of a submonoid of a commutative monoid is in the submonoid. -/
@[to_additive "A finite sum of elements of an `add_submonoid` of an `add_comm_monoid` is in the `add_submonoid`."]
lemma prod_mem {M : Type*} [comm_monoid M] (S : submonoid M)
{ι : Type*} [decidable_eq ι] {t : finset ι} {f : ι → M} :
(∀c ∈ t, f c ∈ S) → t.prod f ∈ S :=
finset.induction_on t (by simp [S.one_mem]) (by simp [S.mul_mem] {contextual := tt})
/-- A directed union of submonoids is a submonoid. -/
@[to_additive "A directed union of `add_submonoid`s is an `add_submonoid`."]
def Union_of_directed {ι : Type*} [hι : nonempty ι]
(s : ι → submonoid M)
(directed : ∀ i j, ∃ k, s i ≤ s k ∧ s j ≤ s k) :
submonoid M :=
{ carrier := (⋃i, s i),
one_mem' := let ⟨i⟩ := hι in set.mem_Union.2 ⟨i, submonoid.one_mem _⟩,
mul_mem' := λ a b ha hb,
let ⟨i, hi⟩ := set.mem_Union.1 ha in
let ⟨j, hj⟩ := set.mem_Union.1 hb in
let ⟨k, hk⟩ := directed i j in
set.mem_Union.2 ⟨k, (s k).mul_mem (hk.1 hi) (hk.2 hj)⟩ }
/-- A submonoid of a monoid inherits a multiplication. -/
@[to_additive "An `add_submonoid` of an `add_monoid` inherits an addition."]
instance has_mul : has_mul S := ⟨λ a b, ⟨a.1 * b.1, S.mul_mem a.2 b.2⟩⟩
/-- A submonoid of a monoid inherits a 1. -/
@[to_additive "An `add_submonoid` of an `add_monoid` inherits a zero."]
instance has_one : has_one S := ⟨⟨_, S.one_mem⟩⟩
@[simp, to_additive] lemma coe_mul (x y : S) : (↑(x * y) : M) = ↑x * ↑y := rfl
@[simp, to_additive] lemma coe_one : ((1 : S) : M) = 1 := rfl
/-- A submonoid of a monoid inherits a monoid structure. -/
@[to_additive to_add_monoid "An `add_submonoid` of an `add_monoid` inherits an `add_monoid` structure."]
instance to_monoid {M : Type*} [monoid M] {S : submonoid M} : monoid S :=
by refine { mul := (*), one := 1, ..}; by simp [mul_assoc]
/-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/
@[to_additive to_add_comm_monoid "An `add_submonoid` of an `add_comm_monoid` is an `add_comm_monoid`."]
instance to_comm_monoid {M} [comm_monoid M] (S : submonoid M) : comm_monoid S :=
{ mul_comm := λ _ _, subtype.ext.2 $ mul_comm _ _, ..submonoid.to_monoid}
/-- The natural monoid hom from a submonoid of monoid `M` to `M`. -/
@[to_additive "The natural monoid hom from an `add_submonoid` of `add_monoid` `M` to `M`."]
def subtype : S →* M :=
{ to_fun := coe,
map_one' := rfl,
map_mul' := λ _ _, rfl }
@[simp, to_additive] theorem subtype_apply (x : S) : S.subtype x = x := rfl
@[to_additive] lemma subtype_eq_val : (S.subtype : S → M) = subtype.val := rfl
/-- The powers `1, x, x², ...` of an element `x` of a monoid `M` are a submonoid. -/
def powers (x : M) : submonoid M :=
{ carrier := {y | ∃ n:ℕ, x^n = y},
one_mem' := ⟨0, pow_zero x⟩,
mul_mem' := by rintros x₁ x₂ ⟨n₁, rfl⟩ ⟨n₂, rfl⟩; exact ⟨n₁ + n₂, pow_add _ _ _ ⟩ }
/-- An element `x` of a monoid is in the submonoid generated by `x`. -/
lemma powers.self_mem {x : M} : x ∈ powers x := ⟨1, pow_one _⟩
/-- If `a` is in a submonoid, so are all its natural number powers. -/
lemma pow_mem {a : M} (h : a ∈ S) : ∀ {n : ℕ}, a ^ n ∈ S
| 0 := S.one_mem
| (n + 1) := S.mul_mem h pow_mem
lemma powers_subset {a : M} (h : a ∈ S) : powers a ≤ S :=
assume x ⟨n, hx⟩, hx ▸ S.pow_mem h
@[simp] lemma coe_pow (a : S) (n : ℕ) : ((a ^ n : S) : M) = a ^ n :=
by induction n; simp [*, pow_succ]
end submonoid
namespace add_submonoid
variables (S : add_submonoid A)
/-- The multiples `0, x, 2x, ...` of an element `x` of an `add_monoid M` are an `add_submonoid`. -/
def multiples (x : A) : add_submonoid A :=
{ carrier := {y | ∃ n:ℕ, add_monoid.smul n x = y},
zero_mem' := ⟨0, add_monoid.zero_smul x⟩,
add_mem' := by rintros x₁ x₂ ⟨n₁, rfl⟩ ⟨n₂, rfl⟩; exact ⟨n₁ + n₂, add_monoid.add_smul _ _ _ ⟩ }
/-- An element `x` of an `add_monoid` is in the `add_submonoid` generated by `x`. -/
lemma multiples.self_mem {x : A} : x ∈ multiples x := ⟨1, add_monoid.one_smul x⟩
lemma smul_mem {a : A} (h : a ∈ S) {n : ℕ} : add_monoid.smul n a ∈ S :=
submonoid.pow_mem (add_submonoid.to_submonoid S) h
lemma multiples_subset {a : A} (h : a ∈ S) : multiples a ≤ S :=
submonoid.powers_subset (add_submonoid.to_submonoid S) h
@[simp] lemma coe_smul (a : S) (n : ℕ) : ((add_monoid.smul n a : S) : A) = add_monoid.smul n a :=
submonoid.coe_pow (add_submonoid.to_submonoid S) a n
end add_submonoid
namespace submonoid
variables (S : submonoid M)
/-- The submonoid `M` of the monoid `M`. -/
@[to_additive "The `add_submonoid M` of the `add_monoid M`."]
def univ : submonoid M :=
{ carrier := set.univ,
one_mem' := set.mem_univ 1,
mul_mem' := λ _ _ _ _, set.mem_univ _ }
/-- The trivial submonoid `{1}` of an monoid `M`. -/
@[to_additive "The trivial `add_submonoid` `{0}` of an `add_monoid` `M`."]
def bot : submonoid M :=
{ carrier := {1},
one_mem' := set.mem_singleton 1,
mul_mem' := λ a b ha hb, by simp * at *}
/-- Submonoids of a monoid are partially ordered (by inclusion). -/
@[to_additive "The `add_submonoid`s of an `add_monoid` are partially ordered (by inclusion)."]
instance : partial_order (submonoid M) :=
partial_order.lift (coe : submonoid M → set M) (λ a b, ext') (by apply_instance)
@[to_additive]
lemma le_def (p p' : submonoid M) : p ≤ p' ↔ ∀ x ∈ p, x ∈ p' := iff.rfl
open lattice
@[to_additive]
instance : has_bot (submonoid M) := ⟨submonoid.bot⟩
@[to_additive]
instance : inhabited (submonoid M) := ⟨⊥⟩
@[simp, to_additive] lemma mem_bot {x : M} : x ∈ (⊥ : submonoid M) ↔ x = 1 := set.mem_singleton_iff
@[to_additive]
instance : order_bot (submonoid M) :=
{ bot := ⊥,
bot_le := λ P x hx, by simp * at *; exact P.one_mem,
..submonoid.partial_order
}
@[to_additive]
instance : has_top (submonoid M) := ⟨univ⟩
@[simp, to_additive] lemma mem_top (x : M) : x ∈ (⊤ : submonoid M) := set.mem_univ x
@[to_additive]
instance : order_top (submonoid M) :=
{ top := ⊤,
le_top := λ p x _, mem_top x,
..submonoid.partial_order}
/-- The inf of two submonoids is their intersection. -/
@[to_additive "The inf of two `add_submonoid`s is their intersection."]
def inf (S₁ S₂ : submonoid M) :
submonoid M :=
{ carrier := S₁ ∩ S₂,
one_mem' := ⟨S₁.one_mem, S₂.one_mem⟩,
mul_mem' := λ _ _ ⟨hx, hx'⟩ ⟨hy, hy'⟩,
⟨S₁.mul_mem hx hy, S₂.mul_mem hx' hy'⟩ }
@[to_additive]
instance : has_inf (submonoid M) := ⟨inf⟩
@[to_additive]
lemma mem_inf {p p' : submonoid M} {x : M} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' :=
⟨λ h, ⟨h.1, h.2⟩, λ h, (p ⊓ p').mem_coe.2 ⟨h.1, h.2⟩⟩
@[to_additive]
instance : has_Inf (submonoid M) :=
⟨λ s, {
carrier := ⋂ t ∈ s, ↑t,
one_mem' := set.mem_bInter $ λ i h, i.one_mem,
mul_mem' := λ x y hx hy, set.mem_bInter $ λ i h,
i.mul_mem (by apply set.mem_bInter_iff.1 hx i h) (by apply set.mem_bInter_iff.1 hy i h) }⟩
@[to_additive]
lemma Inf_le' {S : set (submonoid M)} {p} : p ∈ S → Inf S ≤ p :=
set.bInter_subset_of_mem
@[to_additive]
lemma le_Inf' {S : set (submonoid M)} {p} : (∀p' ∈ S, p ≤ p') → p ≤ Inf S :=
set.subset_bInter
@[to_additive]
lemma mem_Inf {S : set (submonoid M)} {x : M} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff
/-- Submonoids of a monoid form a lattice. -/
@[to_additive "The `add_submonoid`s of an `add_monoid` form a lattice."]
instance lattice.lattice : lattice (submonoid M) :=
{ sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x},
le_sup_left := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, ha,
le_sup_right := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, hb,
sup_le := λ a b c h₁ h₂, Inf_le' ⟨h₁, h₂⟩,
inf := (⊓),
le_inf := λ a b c ha hb, set.subset_inter ha hb,
inf_le_left := λ a b, set.inter_subset_left _ _,
inf_le_right := λ a b, set.inter_subset_right _ _, ..submonoid.partial_order}
/-- Submonoids of a monoid form a complete lattice. -/
@[to_additive "The `add_submonoid`s of an `add_monoid` form a complete lattice."]
instance : complete_lattice (submonoid M) :=
{ Sup := λ tt, Inf {t | ∀t'∈tt, t' ≤ t},
le_Sup := λ s p hs, le_Inf' $ λ p' hp', hp' _ hs,
Sup_le := λ s p hs, Inf_le' hs,
Inf := Inf,
le_Inf := λ s a, le_Inf',
Inf_le := λ s a, Inf_le',
..submonoid.lattice.order_top,
..submonoid.lattice.order_bot,
..submonoid.lattice.lattice}
/-- Submonoids of a monoid form an `add_comm_monoid`. -/
@[to_additive "The `add_submonoid`s of an `add_monoid` form an `add_comm_monoid`."]
instance complete_lattice.add_comm_monoid :
add_comm_monoid (submonoid M) :=
{ add := (⊔),
add_assoc := λ _ _ _, sup_assoc,
zero := ⊥,
zero_add := λ _, bot_sup_eq,
add_zero := λ _, sup_bot_eq,
add_comm := λ _ _, sup_comm }
end submonoid
namespace monoid_hom
variables (S : submonoid M)
open submonoid
/-- The preimage of a submonoid along a monoid homomorphism is a submonoid. -/
@[to_additive "The preimage of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."]
def comap {N : Type*} [monoid N] (f : M →* N)
(S : submonoid N) : submonoid M :=
{ carrier := (f ⁻¹' S),
one_mem' := show f 1 ∈ S, by rw f.map_one; exact S.one_mem,
mul_mem' := λ a b ha hb,
show f (a * b) ∈ S, by rw f.map_mul; exact S.mul_mem ha hb }
/-- The image of a submonoid along a monoid homomorphism is a submonoid. -/
@[to_additive "The image of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."]
def map {N : Type*} [monoid N] (f : M →* N) (S : submonoid M) : submonoid N :=
{ carrier := (f '' S),
one_mem' := ⟨1, S.one_mem, f.map_one⟩,
mul_mem' := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, exact ⟨x * y, S.mul_mem hx hy,
by rw f.map_mul; refl⟩ end }
/-- The range of a monoid homomorphism is a submonoid. -/
@[to_additive "The range of an `add_monoid_hom` is an `add_submonoid`."]
def range {N : Type*} [monoid N] (f : M →* N) :
submonoid N := map f univ
end monoid_hom
namespace submonoid
variables (S : submonoid M)
/-- Product of a list of elements in a submonoid is in the submonoid. -/
@[to_additive "Sum of a list of elements in an `add_submonoid` is in the `add_submonoid`."]
lemma list_prod_mem : ∀ {l : list M}, (∀x∈l, x ∈ S) → l.prod ∈ S
| [] h := S.one_mem
| (a::l) h :=
suffices a * l.prod ∈ S, by simpa,
have a ∈ S ∧ (∀ x ∈ l, x ∈ S), by simpa using h,
S.mul_mem this.1 (list_prod_mem this.2)
/-- Product of a multiset of elements in a submonoid of a `comm_monoid` is in the submonoid. -/
@[to_additive "Sum of a multiset of elements in an `add_submonoid` of an `add_comm_monoid` is in the `add_submonoid`."]
lemma multiset_prod_mem {M} [comm_monoid M] (S : submonoid M) (m : multiset M) :
(∀a ∈ m, a ∈ S) → m.prod ∈ S :=
begin
refine quotient.induction_on m (assume l hl, _),
rw [multiset.quot_mk_to_coe, multiset.coe_prod],
exact S.list_prod_mem hl
end
/-- Product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is in the
submonoid. -/
@[to_additive "Sum of elements in an `add_submonoid` of an `add_comm_monoid` indexed by a `finset` is in the `add_submonoid`."]
lemma finset_prod_mem {M ι} [comm_monoid M] (S : submonoid M) (f : ι → M) :
∀(t : finset ι), (∀b∈t, f b ∈ S) → t.prod f ∈ S
| ⟨m, hm⟩ hs :=
begin
refine S.multiset_prod_mem _ _,
suffices : ∀ (a : M) (x : ι), x ∈ m → f x = a → a ∈ S,
simpa using this,
rintros a b hb rfl,
exact hs _ hb
end
end submonoid
namespace monoid_hom
variables (S : submonoid M)
/-- Restriction of a monoid hom to a submonoid of the domain. -/
@[to_additive "Restriction of an add_monoid hom to an `add_submonoid` of the domain."]
def restrict {N : Type*} [monoid N] (f : M →* N) : S →* N :=
⟨λ s, f s, f.map_one, λ x y, f.map_mul x y⟩
/-- Restriction of a monoid hom to a submonoid of the codomain. -/
@[to_additive "Restriction of an `add_monoid` hom to an `add_submonoid` of the codomain."]
def subtype_mk {N : Type*} [monoid N] (f : N →* M) (h : ∀ x, f x ∈ S) : N →* S :=
{ to_fun := λ n, ⟨f n, h n⟩,
map_one' := subtype.eq f.map_one,
map_mul' := λ x y, subtype.eq (f.map_mul x y) }
/-- Restriction of a monoid hom to its range. -/
@[to_additive "Restriction of an `add_monoid` hom to its range."]
def range_mk {N} [monoid N] (f : M →* N) : M →* f.range :=
subtype_mk f.range f $ λ x, ⟨x, submonoid.mem_top x, rfl⟩
/-- The range of a surjective monoid hom is the whole of the codomain. -/
@[to_additive "The range of a surjective `add_monoid` hom is the whole of the codomain."]
lemma range_top_of_surjective {N} [monoid N] (f : M →* N) (hf : function.surjective f) :
f.range = (⊤ : submonoid N) :=
submonoid.ext'_iff.1 $ (set.ext_iff _ _).2 $ λ x,
⟨λ h, submonoid.mem_top x, λ h, exists.elim (hf x) $ λ w hw, ⟨w, submonoid.mem_top w, hw⟩⟩
/-- The monoid hom associated to an inclusion of submonoids. -/
@[to_additive "The `add_monoid` hom associated to an inclusion of submonoids."]
def set_inclusion (T : submonoid M) (h : S ≤ T) : S →* T :=
subtype_mk _ S.subtype (λ x, h x.2)
end monoid_hom
namespace monoid
variables (S : submonoid M)
open submonoid
/-- The inductively defined submonoid generated by a set. -/
@[to_additive "The inductively defined `add_submonoid` generated by a set. "]
def closure' (s : set M) : submonoid M :=
{ carrier := in_closure s,
one_mem' := in_closure.one s,
mul_mem' := λ _ _, in_closure.mul}
/-- The submonoid generated by a set contains the set. -/
@[to_additive "The `add_submonoid` generated by a set contains the set."]
theorem le_closure' {s : set M} : s ≤ closure' s :=
λ a, in_closure.basic
/-- The submonoid generated by a set is contained in any submonoid that contains the set. -/
@[to_additive "The `add_submonoid` generated by a set is contained in any `add_submonoid` that contains the set."]
theorem closure'_le {s : set M} {T : submonoid M} (h : s ≤ T) : closure' s ≤ T :=
λ a ha, begin induction ha with _ hb _ _ _ _ ha hb,
{exact h hb },
{exact T.one_mem },
{exact T.mul_mem ha hb }
end
/-- Given subsets `t` and `s` of a monoid `M`, if `s ⊆ t`, the submonoid of `M` generated by `s` is
contained in the submonoid generated by `t`. -/
@[to_additive "Given subsets `t` and `s` of an `add_monoid` `M`, if `s ⊆ t`, the `add_submonoid` of `M` generated by `s` is contained in the `add_submonoid` generated by `t`."]
theorem closure'_mono {s t : set M} (h : s ≤ t) : closure' s ≤ closure' t :=
closure'_le $ set.subset.trans h le_closure'
/-- The submonoid generated by an element of a monoid equals the set of natural number powers
of the element. -/
theorem closure'_singleton {x : M} : closure' ({x} : set M) = powers x :=
ext' $ set.eq_of_subset_of_subset (closure'_le $ set.singleton_subset_iff.2 powers.self_mem) $
submonoid.powers_subset _ $ in_closure.basic $ set.mem_singleton x
/-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated
by the image of the set. -/
@[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals the `add_submonoid` generated by the image of the set."]
lemma image_closure' {N : Type*} [monoid N] (f : M →* N) (s : set M) :
f.map (closure' s) = closure' (f '' s) :=
le_antisymm
begin
rintros _ ⟨x, hx, rfl⟩,
apply in_closure.rec_on hx; intros,
{ solve_by_elim [le_closure', set.mem_image_of_mem] },
{ rw f.map_one, apply submonoid.one_mem },
{ rw f.map_mul, solve_by_elim [submonoid.mul_mem] }
end
(closure'_le $ set.image_subset _ le_closure')
/-- Given an element `a` of the submonoid of a monoid `M` generated by a set `s`, there exists a list of
elements of `s` whose product is `a`. -/
@[to_additive "Given an element `a` of the `add_submonoid` of an `add_monoid` `M` generated by a set `s`, there exists a list of elements of `s` whose sum is `a`."]
theorem exists_list_of_mem_closure' {s : set M} {a : M} (h : a ∈ closure' s) :
(∃l:list M, (∀x∈l, x ∈ s) ∧ l.prod = a) :=
begin
induction h,
case in_closure.basic : a ha { existsi ([a]), simp [ha] },
case in_closure.one { existsi ([]), simp },
case in_closure.mul : a b _ _ ha hb {
rcases ha with ⟨la, ha, eqa⟩,
rcases hb with ⟨lb, hb, eqb⟩,
existsi (la ++ lb),
simp [eqa.symm, eqb.symm, or_imp_distrib],
exact assume a, ⟨ha a, hb a⟩
}
end
/-- Given sets `s, t` of a commutative monoid `M`, `x ∈ M` is in the submonoid of `M` generated by `s ∪ t`
iff there exists an element of the submonoid generated by `s` and an element of the submonoid
generated by `t` whose product is `x`. -/
@[to_additive "Given sets `s, t` of a commutative `add_monoid` `M`, `x ∈ M` is in the `add_submonoid` of `M` generated by `s ∪ t` iff there exists an element of the `add_submonoid` generated by `s` and an element of the `add_submonoid` generated by `t` whose sum is `x`."]
theorem mem_closure'_union_iff {M : Type*} [comm_monoid M] {s t : set M} {x : M} :
x ∈ closure' (s ∪ t) ↔ ∃ y ∈ closure' s, ∃ z ∈ closure' t, y * z = x :=
⟨λ hx, let ⟨L, HL1, HL2⟩ := exists_list_of_mem_closure' hx in HL2 ▸
list.rec_on L (λ _, ⟨1, submonoid.one_mem _, 1, submonoid.one_mem _, mul_one _⟩)
(λ hd tl ih HL1, let ⟨y, hy, z, hz, hyzx⟩ := ih (list.forall_mem_of_forall_mem_cons HL1) in
or.cases_on (HL1 hd $ list.mem_cons_self _ _)
(λ hs, ⟨hd * y, submonoid.mul_mem _ (le_closure' hs) hy, z, hz,
by rw [mul_assoc, list.prod_cons, ← hyzx]; refl⟩)
(λ ht, ⟨y, hy, z * hd, submonoid.mul_mem _ hz (le_closure' ht),
by rw [← mul_assoc, list.prod_cons, ← hyzx, mul_comm hd]; refl⟩)) HL1,
λ ⟨y, hy, z, hz, hyzx⟩, hyzx ▸ submonoid.mul_mem _
((closure_mono (set.subset_union_left s t)) hy)
((closure_mono (set.subset_union_right s t)) hz)⟩
end monoid
namespace add_monoid
open add_submonoid
/-- The `add_submonoid` generated by an element of an `add_monoid` equals the set of natural number
multiples of the element. -/
theorem closure'_singleton {x : A} : closure' ({x} : set A) = multiples x :=
ext' $ set.eq_of_subset_of_subset (closure'_le $ set.singleton_subset_iff.2 multiples.self_mem) $
multiples_subset _ $ in_closure.basic $ set.mem_singleton x
end add_monoid
namespace mul_equiv
variables {S T : submonoid M}
/-- Makes the identity isomorphism from a proof two submonoids of a multiplicative
monoid are equal. -/
@[to_additive add_submonoid_congr "Makes the identity additive isomorphism from a proof two submonoids of an additive monoid are equal."]
def submonoid_congr (h : S = T) : S ≃* T :=
{ map_mul' := λ _ _, rfl, ..equiv.set_congr $ submonoid.ext'_iff.2 h }
end mul_equiv
|
71021d444663a13a47a9c328d826b4d2e66d82f6 | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/linear_algebra/alternating.lean | 92a64ea1031c7f2c98a30c97f125e89c3d635a72 | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 29,908 | lean | /-
Copyright (c) 2020 Zhangir Azerbayev. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser, Zhangir Azerbayev
-/
import linear_algebra.multilinear
import linear_algebra.linear_independent
import group_theory.perm.sign
import group_theory.perm.subgroup
import data.equiv.fin
import linear_algebra.tensor_product
import group_theory.quotient_group
/-!
# Alternating Maps
We construct the bundled function `alternating_map`, which extends `multilinear_map` with all the
arguments of the same type.
## Main definitions
* `alternating_map R M N ι` is the space of `R`-linear alternating maps from `ι → M` to `N`.
* `f.map_eq_zero_of_eq` expresses that `f` is zero when two inputs are equal.
* `f.map_swap` expresses that `f` is negated when two inputs are swapped.
* `f.map_perm` expresses how `f` varies by a sign change under a permutation of its inputs.
* An `add_comm_monoid`, `add_comm_group`, and `module` structure over `alternating_map`s that
matches the definitions over `multilinear_map`s.
* `multilinear_map.alternatization`, which makes an alternating map out of a non-alternating one.
* `alternating_map.dom_coprod`, which behaves as a product between two alternating maps.
## Implementation notes
`alternating_map` is defined in terms of `map_eq_zero_of_eq`, as this is easier to work with than
using `map_swap` as a definition, and does not require `has_neg N`.
`alternating_map`s are provided with a coercion to `multilinear_map`, along with a set of
`norm_cast` lemmas that act on the algebraic structure:
* `alternating_map.coe_add`
* `alternating_map.coe_zero`
* `alternating_map.coe_sub`
* `alternating_map.coe_neg`
* `alternating_map.coe_smul`
-/
-- semiring / add_comm_monoid
variables {R : Type*} [semiring R]
variables {M : Type*} [add_comm_monoid M] [module R M]
variables {N : Type*} [add_comm_monoid N] [module R N]
-- semiring / add_comm_group
variables {M' : Type*} [add_comm_group M'] [module R M']
variables {N' : Type*} [add_comm_group N'] [module R N']
variables {ι : Type*} [decidable_eq ι]
set_option old_structure_cmd true
section
variables (R M N ι)
/--
An alternating map is a multilinear map that vanishes when two of its arguments are equal.
-/
structure alternating_map extends multilinear_map R (λ i : ι, M) N :=
(map_eq_zero_of_eq' : ∀ (v : ι → M) (i j : ι) (h : v i = v j) (hij : i ≠ j), to_fun v = 0)
end
/-- The multilinear map associated to an alternating map -/
add_decl_doc alternating_map.to_multilinear_map
namespace alternating_map
variables (f f' : alternating_map R M N ι)
variables (g g₂ : alternating_map R M N' ι)
variables (g' : alternating_map R M' N' ι)
variables (v : ι → M) (v' : ι → M')
open function
/-! Basic coercion simp lemmas, largely copied from `ring_hom` and `multilinear_map` -/
section coercions
instance : has_coe_to_fun (alternating_map R M N ι) := ⟨_, λ x, x.to_fun⟩
initialize_simps_projections alternating_map (to_fun → apply)
@[simp] lemma to_fun_eq_coe : f.to_fun = f := rfl
@[simp] lemma coe_mk (f : (ι → M) → N) (h₁ h₂ h₃) : ⇑(⟨f, h₁, h₂, h₃⟩ :
alternating_map R M N ι) = f := rfl
theorem congr_fun {f g : alternating_map R M N ι} (h : f = g) (x : ι → M) : f x = g x :=
congr_arg (λ h : alternating_map R M N ι, h x) h
theorem congr_arg (f : alternating_map R M N ι) {x y : ι → M} (h : x = y) : f x = f y :=
congr_arg (λ x : ι → M, f x) h
theorem coe_inj ⦃f g : alternating_map R M N ι⦄ (h : ⇑f = g) : f = g :=
by { cases f, cases g, cases h, refl }
@[ext] theorem ext {f f' : alternating_map R M N ι} (H : ∀ x, f x = f' x) : f = f' :=
coe_inj (funext H)
theorem ext_iff {f g : alternating_map R M N ι} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, ext h⟩
instance : has_coe (alternating_map R M N ι) (multilinear_map R (λ i : ι, M) N) :=
⟨λ x, x.to_multilinear_map⟩
@[simp, norm_cast] lemma coe_multilinear_map : ⇑(f : multilinear_map R (λ i : ι, M) N) = f := rfl
lemma coe_multilinear_map_injective :
function.injective (coe : alternating_map R M N ι → multilinear_map R (λ i : ι, M) N) :=
λ x y h, ext $ multilinear_map.congr_fun h
@[simp] lemma to_multilinear_map_eq_coe : f.to_multilinear_map = f := rfl
@[simp] lemma coe_multilinear_map_mk (f : (ι → M) → N) (h₁ h₂ h₃) :
((⟨f, h₁, h₂, h₃⟩ : alternating_map R M N ι) : multilinear_map R (λ i : ι, M) N) = ⟨f, h₁, h₂⟩ :=
rfl
end coercions
/-!
### Simp-normal forms of the structure fields
These are expressed in terms of `⇑f` instead of `f.to_fun`.
-/
@[simp] lemma map_add (i : ι) (x y : M) :
f (update v i (x + y)) = f (update v i x) + f (update v i y) :=
f.to_multilinear_map.map_add' v i x y
@[simp] lemma map_sub (i : ι) (x y : M') :
g' (update v' i (x - y)) = g' (update v' i x) - g' (update v' i y) :=
g'.to_multilinear_map.map_sub v' i x y
@[simp] lemma map_neg (i : ι) (x : M') :
g' (update v' i (-x)) = -g' (update v' i x) :=
g'.to_multilinear_map.map_neg v' i x
@[simp] lemma map_smul (i : ι) (r : R) (x : M) :
f (update v i (r • x)) = r • f (update v i x) :=
f.to_multilinear_map.map_smul' v i r x
@[simp] lemma map_eq_zero_of_eq (v : ι → M) {i j : ι} (h : v i = v j) (hij : i ≠ j) :
f v = 0 :=
f.map_eq_zero_of_eq' v i j h hij
lemma map_coord_zero {m : ι → M} (i : ι) (h : m i = 0) : f m = 0 :=
f.to_multilinear_map.map_coord_zero i h
@[simp] lemma map_update_zero (m : ι → M) (i : ι) : f (update m i 0) = 0 :=
f.to_multilinear_map.map_update_zero m i
@[simp] lemma map_zero [nonempty ι] : f 0 = 0 :=
f.to_multilinear_map.map_zero
/-!
### Algebraic structure inherited from `multilinear_map`
`alternating_map` carries the same `add_comm_monoid`, `add_comm_group`, and `module` structure
as `multilinear_map`
-/
instance : has_add (alternating_map R M N ι) :=
⟨λ a b,
{ map_eq_zero_of_eq' :=
λ v i j h hij, by simp [a.map_eq_zero_of_eq v h hij, b.map_eq_zero_of_eq v h hij],
..(a + b : multilinear_map R (λ i : ι, M) N)}⟩
@[simp] lemma add_apply : (f + f') v = f v + f' v := rfl
@[norm_cast] lemma coe_add : (↑(f + f') : multilinear_map R (λ i : ι, M) N) = f + f' := rfl
instance : has_zero (alternating_map R M N ι) :=
⟨{map_eq_zero_of_eq' := λ v i j h hij, by simp,
..(0 : multilinear_map R (λ i : ι, M) N)}⟩
@[simp] lemma zero_apply : (0 : alternating_map R M N ι) v = 0 := rfl
@[norm_cast] lemma coe_zero :
((0 : alternating_map R M N ι) : multilinear_map R (λ i : ι, M) N) = 0 := rfl
instance : inhabited (alternating_map R M N ι) := ⟨0⟩
instance : add_comm_monoid (alternating_map R M N ι) :=
{ zero := 0,
add := (+),
zero_add := by intros; ext; simp [add_comm, add_left_comm],
add_zero := by intros; ext; simp [add_comm, add_left_comm],
add_comm := by intros; ext; simp [add_comm, add_left_comm],
add_assoc := by intros; ext; simp [add_comm, add_left_comm],
nsmul := λ n f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij],
.. ((n • f : multilinear_map R (λ i : ι, M) N)) },
nsmul_zero' := by { intros, ext, simp [add_smul], },
nsmul_succ' := by { intros, ext, simp [add_smul, nat.succ_eq_one_add], } }
instance : has_neg (alternating_map R M N' ι) :=
⟨λ f,
{ map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij],
..(-(f : multilinear_map R (λ i : ι, M) N')) }⟩
@[simp] lemma neg_apply (m : ι → M) : (-g) m = -(g m) := rfl
@[norm_cast] lemma coe_neg :
((-g : alternating_map R M N' ι) : multilinear_map R (λ i : ι, M) N') = -g := rfl
instance : has_sub (alternating_map R M N' ι) :=
⟨λ f g,
{ map_eq_zero_of_eq' :=
λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij, g.map_eq_zero_of_eq v h hij],
..(f - g : multilinear_map R (λ i : ι, M) N') }⟩
@[simp] lemma sub_apply (m : ι → M) : (g - g₂) m = g m - g₂ m := rfl
@[norm_cast] lemma coe_sub : (↑(g - g₂) : multilinear_map R (λ i : ι, M) N') = g - g₂ := rfl
instance : add_comm_group (alternating_map R M N' ι) :=
by refine
{ zero := 0,
add := (+),
neg := has_neg.neg,
sub := has_sub.sub,
sub_eq_add_neg := _,
nsmul := λ n f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij],
.. ((n • f : multilinear_map R (λ i : ι, M) N')) },
gsmul := λ n f, { map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij],
.. ((n • f : multilinear_map R (λ i : ι, M) N')) },
gsmul_zero' := _,
gsmul_succ' := _,
gsmul_neg' := _,
.. alternating_map.add_comm_monoid, .. };
intros; ext;
simp [add_comm, add_left_comm, sub_eq_add_neg, add_smul, nat.succ_eq_add_one, gsmul_coe_nat]
section distrib_mul_action
variables {S : Type*} [monoid S] [distrib_mul_action S N] [smul_comm_class R S N]
instance : has_scalar S (alternating_map R M N ι) :=
⟨λ c f,
{ map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij],
..((c • f : multilinear_map R (λ i : ι, M) N)) }⟩
@[simp] lemma smul_apply (c : S) (m : ι → M) :
(c • f) m = c • f m := rfl
@[norm_cast] lemma coe_smul (c : S):
((c • f : alternating_map R M N ι) : multilinear_map R (λ i : ι, M) N) = c • f := rfl
instance : distrib_mul_action S (alternating_map R M N ι) :=
{ one_smul := λ f, ext $ λ x, one_smul _ _,
mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul _ _ _,
smul_zero := λ r, ext $ λ x, smul_zero _,
smul_add := λ r f₁ f₂, ext $ λ x, smul_add _ _ _ }
end distrib_mul_action
section module
variables {S : Type*} [semiring S] [module S N] [smul_comm_class R S N]
/-- The space of multilinear maps over an algebra over `R` is a module over `R`, for the pointwise
addition and scalar multiplication. -/
instance : module S (alternating_map R M N ι) :=
{ add_smul := λ r₁ r₂ f, ext $ λ x, add_smul _ _ _,
zero_smul := λ f, ext $ λ x, zero_smul _ _ }
end module
end alternating_map
/-!
### Composition with linear maps
-/
namespace linear_map
variables {N₂ : Type*} [add_comm_monoid N₂] [module R N₂]
/-- Composing a alternating map with a linear map gives again a alternating map. -/
def comp_alternating_map (g : N →ₗ[R] N₂) : alternating_map R M N ι →+ alternating_map R M N₂ ι :=
{ to_fun := λ f,
{ map_eq_zero_of_eq' := λ v i j h hij, by simp [f.map_eq_zero_of_eq v h hij],
..(g.comp_multilinear_map (f : multilinear_map R (λ _ : ι, M) N)) },
map_zero' := by { ext, simp },
map_add' := λ a b, by { ext, simp } }
@[simp] lemma coe_comp_alternating_map (g : N →ₗ[R] N₂) (f : alternating_map R M N ι) :
⇑(g.comp_alternating_map f) = g ∘ f := rfl
lemma comp_alternating_map_apply (g : N →ₗ[R] N₂) (f : alternating_map R M N ι) (m : ι → M) :
g.comp_alternating_map f m = g (f m) := rfl
end linear_map
namespace alternating_map
variables (f f' : alternating_map R M N ι)
variables (g g₂ : alternating_map R M N' ι)
variables (g' : alternating_map R M' N' ι)
variables (v : ι → M) (v' : ι → M')
open function
/-!
### Other lemmas from `multilinear_map`
-/
section
open_locale big_operators
lemma map_update_sum {α : Type*} (t : finset α) (i : ι) (g : α → M) (m : ι → M):
f (update m i (∑ a in t, g a)) = ∑ a in t, f (update m i (g a)) :=
f.to_multilinear_map.map_update_sum t i g m
end
/-!
### Theorems specific to alternating maps
Various properties of reordered and repeated inputs which follow from
`alternating_map.map_eq_zero_of_eq`.
-/
lemma map_update_self {i j : ι} (hij : i ≠ j) :
f (function.update v i (v j)) = 0 :=
f.map_eq_zero_of_eq _ (by rw [function.update_same, function.update_noteq hij.symm]) hij
lemma map_update_update {i j : ι} (hij : i ≠ j) (m : M) :
f (function.update (function.update v i m) j m) = 0 :=
f.map_eq_zero_of_eq _
(by rw [function.update_same, function.update_noteq hij, function.update_same]) hij
lemma map_swap_add {i j : ι} (hij : i ≠ j) :
f (v ∘ equiv.swap i j) + f v = 0 :=
begin
rw equiv.comp_swap_eq_update,
convert f.map_update_update v hij (v i + v j),
simp [f.map_update_self _ hij,
f.map_update_self _ hij.symm,
function.update_comm hij (v i + v j) (v _) v,
function.update_comm hij.symm (v i) (v i) v],
end
lemma map_add_swap {i j : ι} (hij : i ≠ j) :
f v + f (v ∘ equiv.swap i j) = 0 :=
by { rw add_comm, exact f.map_swap_add v hij }
lemma map_swap {i j : ι} (hij : i ≠ j) :
g (v ∘ equiv.swap i j) = - g v :=
eq_neg_of_add_eq_zero (g.map_swap_add v hij)
lemma map_perm [fintype ι] (v : ι → M) (σ : equiv.perm ι) :
g (v ∘ σ) = σ.sign • g v :=
begin
apply equiv.perm.swap_induction_on' σ,
{ simp },
{ intros s x y hxy hI,
simpa [g.map_swap (v ∘ s) hxy, equiv.perm.sign_swap hxy] using hI, }
end
lemma map_congr_perm [fintype ι] (σ : equiv.perm ι) :
g v = σ.sign • g (v ∘ σ) :=
by { rw [g.map_perm, smul_smul], simp }
lemma coe_dom_dom_congr [fintype ι] (σ : equiv.perm ι) :
(g : multilinear_map R (λ _ : ι, M) N').dom_dom_congr σ
= σ.sign • (g : multilinear_map R (λ _ : ι, M) N') :=
multilinear_map.ext $ λ v, g.map_perm v σ
/-- If the arguments are linearly dependent then the result is `0`. -/
lemma map_linear_dependent
{K : Type*} [ring K]
{M : Type*} [add_comm_group M] [module K M]
{N : Type*} [add_comm_group N] [module K N] [no_zero_smul_divisors K N]
(f : alternating_map K M N ι) (v : ι → M)
(h : ¬linear_independent K v) :
f v = 0 :=
begin
obtain ⟨s, g, h, i, hi, hz⟩ := linear_dependent_iff.mp h,
suffices : f (update v i (g i • v i)) = 0,
{ rw [f.map_smul, function.update_eq_self, smul_eq_zero] at this,
exact or.resolve_left this hz, },
conv at h in (g _ • v _) { rw ←if_t_t (i = x) (g _ • v _), },
rw [finset.sum_ite, finset.filter_eq, finset.filter_ne, if_pos hi, finset.sum_singleton,
add_eq_zero_iff_eq_neg] at h,
rw [h, f.map_neg, f.map_update_sum, neg_eq_zero, finset.sum_eq_zero],
intros j hj,
obtain ⟨hij, _⟩ := finset.mem_erase.mp hj,
rw [f.map_smul, f.map_update_self _ hij.symm, smul_zero],
end
end alternating_map
open_locale big_operators
namespace multilinear_map
open equiv
variables [fintype ι]
private lemma alternization_map_eq_zero_of_eq_aux
(m : multilinear_map R (λ i : ι, M) N')
(v : ι → M) (i j : ι) (i_ne_j : i ≠ j) (hv : v i = v j) :
(∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ) v = 0 :=
begin
rw sum_apply,
exact finset.sum_involution
(λ σ _, swap i j * σ)
(λ σ _, by simp [perm.sign_swap i_ne_j, apply_swap_eq_self hv])
(λ σ _ _, (not_congr swap_mul_eq_iff).mpr i_ne_j)
(λ σ _, finset.mem_univ _)
(λ σ _, swap_mul_involutive i j σ)
end
/-- Produce an `alternating_map` out of a `multilinear_map`, by summing over all argument
permutations. -/
def alternatization : multilinear_map R (λ i : ι, M) N' →+ alternating_map R M N' ι :=
{ to_fun := λ m,
{ to_fun := ⇑(∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ),
map_eq_zero_of_eq' := λ v i j hvij hij, alternization_map_eq_zero_of_eq_aux m v i j hij hvij,
.. (∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ)},
map_add' := λ a b, begin
ext,
simp only [
finset.sum_add_distrib, smul_add, add_apply, dom_dom_congr_apply, alternating_map.add_apply,
alternating_map.coe_mk, smul_apply, sum_apply],
end,
map_zero' := begin
ext,
simp only [
finset.sum_const_zero, smul_zero, zero_apply, dom_dom_congr_apply, alternating_map.zero_apply,
alternating_map.coe_mk, smul_apply, sum_apply],
end }
lemma alternatization_def (m : multilinear_map R (λ i : ι, M) N') :
⇑(alternatization m) = (∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ : _) :=
rfl
lemma alternatization_coe (m : multilinear_map R (λ i : ι, M) N') :
↑m.alternatization = (∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ : _) :=
coe_inj rfl
lemma alternatization_apply (m : multilinear_map R (λ i : ι, M) N') (v : ι → M) :
alternatization m v = ∑ (σ : perm ι), σ.sign • m.dom_dom_congr σ v :=
by simp only [alternatization_def, smul_apply, sum_apply]
end multilinear_map
namespace alternating_map
/-- Alternatizing a multilinear map that is already alternating results in a scale factor of `n!`,
where `n` is the number of inputs. -/
lemma coe_alternatization [fintype ι] (a : alternating_map R M N' ι) :
(↑a : multilinear_map R (λ ι, M) N').alternatization = nat.factorial (fintype.card ι) • a :=
begin
apply alternating_map.coe_inj,
simp_rw [multilinear_map.alternatization_def, coe_dom_dom_congr, smul_smul,
int.units_mul_self, one_smul, finset.sum_const, finset.card_univ, fintype.card_perm,
←coe_multilinear_map, coe_smul],
end
end alternating_map
namespace linear_map
variables {N'₂ : Type*} [add_comm_group N'₂] [module R N'₂] [fintype ι]
/-- Composition with a linear map before and after alternatization are equivalent. -/
lemma comp_multilinear_map_alternatization (g : N' →ₗ[R] N'₂)
(f : multilinear_map R (λ _ : ι, M) N') :
(g.comp_multilinear_map f).alternatization = g.comp_alternating_map (f.alternatization) :=
by { ext, simp [multilinear_map.alternatization_def] }
end linear_map
section coprod
open_locale big_operators
open_locale tensor_product
variables {ιa ιb : Type*} [decidable_eq ιa] [decidable_eq ιb] [fintype ιa] [fintype ιb]
variables
{R' : Type*} {Mᵢ N₁ N₂ : Type*}
[comm_semiring R']
[add_comm_group N₁] [module R' N₁]
[add_comm_group N₂] [module R' N₂]
[add_comm_monoid Mᵢ] [module R' Mᵢ]
namespace equiv.perm
/-- Elements which are considered equivalent if they differ only by swaps within α or β -/
abbreviation mod_sum_congr (α β : Type*) :=
quotient_group.quotient (equiv.perm.sum_congr_hom α β).range
lemma mod_sum_congr.swap_smul_involutive {α β : Type*} [decidable_eq (α ⊕ β)] (i j : α ⊕ β) :
function.involutive (has_scalar.smul (equiv.swap i j) : mod_sum_congr α β → mod_sum_congr α β) :=
λ σ, begin
apply σ.induction_on' (λ σ, _),
exact _root_.congr_arg quotient.mk' (equiv.swap_mul_involutive i j σ)
end
end equiv.perm
namespace alternating_map
open equiv
/-- summand used in `alternating_map.dom_coprod` -/
def dom_coprod.summand
(a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb)
(σ : perm.mod_sum_congr ιa ιb) :
multilinear_map R' (λ _ : ιa ⊕ ιb, Mᵢ) (N₁ ⊗[R'] N₂) :=
quotient.lift_on' σ
(λ σ,
σ.sign •
(multilinear_map.dom_coprod ↑a ↑b : multilinear_map R' (λ _, Mᵢ) (N₁ ⊗ N₂)).dom_dom_congr σ)
(λ σ₁ σ₂ ⟨⟨sl, sr⟩, h⟩, begin
ext v,
simp only [multilinear_map.dom_dom_congr_apply, multilinear_map.dom_coprod_apply,
coe_multilinear_map, multilinear_map.smul_apply],
replace h := inv_mul_eq_iff_eq_mul.mp h.symm,
have : (σ₁ * perm.sum_congr_hom _ _ (sl, sr)).sign = σ₁.sign * (sl.sign * sr.sign) :=
by simp,
rw [h, this, mul_smul, mul_smul, smul_left_cancel_iff,
←tensor_product.tmul_smul, tensor_product.smul_tmul'],
simp only [sum.map_inr, perm.sum_congr_hom_apply, perm.sum_congr_apply, sum.map_inl,
function.comp_app, perm.coe_mul],
rw [←a.map_congr_perm (λ i, v (σ₁ _)), ←b.map_congr_perm (λ i, v (σ₁ _))],
end)
lemma dom_coprod.summand_mk'
(a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb)
(σ : equiv.perm (ιa ⊕ ιb)) :
dom_coprod.summand a b (quotient.mk' σ) = σ.sign •
(multilinear_map.dom_coprod ↑a ↑b : multilinear_map R' (λ _, Mᵢ) (N₁ ⊗ N₂)).dom_dom_congr σ :=
rfl
/-- Swapping elements in `σ` with equal values in `v` results in an addition that cancels -/
lemma dom_coprod.summand_add_swap_smul_eq_zero
(a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb)
(σ : perm.mod_sum_congr ιa ιb)
{v : ιa ⊕ ιb → Mᵢ} {i j : ιa ⊕ ιb} (hv : v i = v j) (hij : i ≠ j) :
dom_coprod.summand a b σ v + dom_coprod.summand a b (swap i j • σ) v = 0 :=
begin
apply σ.induction_on' (λ σ, _),
dsimp only [quotient.lift_on'_mk', quotient.map'_mk', mul_action.quotient.smul_mk,
dom_coprod.summand],
rw [perm.sign_mul, perm.sign_swap hij],
simp only [one_mul, units.neg_mul, function.comp_app, units.neg_smul, perm.coe_mul,
units.coe_neg, multilinear_map.smul_apply, multilinear_map.neg_apply,
multilinear_map.dom_dom_congr_apply, multilinear_map.dom_coprod_apply],
convert add_right_neg _;
{ ext k, rw equiv.apply_swap_eq_self hv },
end
/-- Swapping elements in `σ` with equal values in `v` result in zero if the swap has no effect
on the quotient. -/
lemma dom_coprod.summand_eq_zero_of_smul_invariant
(a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb)
(σ : perm.mod_sum_congr ιa ιb)
{v : ιa ⊕ ιb → Mᵢ} {i j : ιa ⊕ ιb} (hv : v i = v j) (hij : i ≠ j) :
swap i j • σ = σ → dom_coprod.summand a b σ v = 0 :=
begin
apply σ.induction_on' (λ σ, _),
dsimp only [quotient.lift_on'_mk', quotient.map'_mk', multilinear_map.smul_apply,
multilinear_map.dom_dom_congr_apply, multilinear_map.dom_coprod_apply, dom_coprod.summand],
intro hσ,
with_cases {
cases hi : σ⁻¹ i;
cases hj : σ⁻¹ j;
rw perm.inv_eq_iff_eq at hi hj;
substs hi hj, },
case [sum.inl sum.inr : i' j', sum.inr sum.inl : i' j'] {
-- the term pairs with and cancels another term
all_goals { obtain ⟨⟨sl, sr⟩, hσ⟩ := quotient.exact' hσ, },
work_on_goal 0 { replace hσ := equiv.congr_fun hσ (sum.inl i'), },
work_on_goal 1 { replace hσ := equiv.congr_fun hσ (sum.inr i'), },
all_goals {
rw [←equiv.mul_swap_eq_swap_mul, mul_inv_rev, equiv.swap_inv, inv_mul_cancel_right] at hσ,
simpa using hσ, }, },
case [sum.inr sum.inr : i' j', sum.inl sum.inl : i' j'] {
-- the term does not pair but is zero
all_goals { convert smul_zero _, },
work_on_goal 0 { convert tensor_product.tmul_zero _ _, },
work_on_goal 1 { convert tensor_product.zero_tmul _ _, },
all_goals { exact alternating_map.map_eq_zero_of_eq _ _ hv (λ hij', hij (hij' ▸ rfl)), } },
end
/-- Like `multilinear_map.dom_coprod`, but ensures the result is also alternating.
Note that this is usually defined (for instance, as used in Proposition 22.24 in [Gallier2011Notes])
over integer indices `ιa = fin n` and `ιb = fin m`, as
$$
(f \wedge g)(u_1, \ldots, u_{m+n}) =
\sum_{\operatorname{shuffle}(m, n)} \operatorname{sign}(\sigma)
f(u_{\sigma(1)}, \ldots, u_{\sigma(m)}) g(u_{\sigma(m+1)}, \ldots, u_{\sigma(m+n)}),
$$
where $\operatorname{shuffle}(m, n)$ consists of all permutations of $[1, m+n]$ such that
$\sigma(1) < \cdots < \sigma(m)$ and $\sigma(m+1) < \cdots < \sigma(m+n)$.
Here, we generalize this by replacing:
* the product in the sum with a tensor product
* the filtering of $[1, m+n]$ to shuffles with an isomorphic quotient
* the additions in the subscripts of $\sigma$ with an index of type `sum`
The specialized version can be obtained by combining this definition with `fin_sum_fin_equiv` and
`algebra.lmul'`.
-/
@[simps]
def dom_coprod
(a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) :
alternating_map R' Mᵢ (N₁ ⊗[R'] N₂) (ιa ⊕ ιb) :=
{ to_fun := λ v, ⇑(∑ σ : perm.mod_sum_congr ιa ιb, dom_coprod.summand a b σ) v,
map_eq_zero_of_eq' := λ v i j hv hij, begin
dsimp only,
rw multilinear_map.sum_apply,
exact finset.sum_involution
(λ σ _, equiv.swap i j • σ)
(λ σ _, dom_coprod.summand_add_swap_smul_eq_zero a b σ hv hij)
(λ σ _, mt $ dom_coprod.summand_eq_zero_of_smul_invariant a b σ hv hij)
(λ σ _, finset.mem_univ _)
(λ σ _, equiv.perm.mod_sum_congr.swap_smul_involutive i j σ),
end,
..(∑ σ : perm.mod_sum_congr ιa ιb, dom_coprod.summand a b σ) }
lemma dom_coprod_coe (a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) :
(↑(a.dom_coprod b) : multilinear_map R' (λ _, Mᵢ) _) =
∑ σ : perm.mod_sum_congr ιa ιb, dom_coprod.summand a b σ :=
multilinear_map.ext $ λ _, rfl
/-- A more bundled version of `alternating_map.dom_coprod` that maps
`((ι₁ → N) → N₁) ⊗ ((ι₂ → N) → N₂)` to `(ι₁ ⊕ ι₂ → N) → N₁ ⊗ N₂`. -/
def dom_coprod' :
(alternating_map R' Mᵢ N₁ ιa ⊗[R'] alternating_map R' Mᵢ N₂ ιb) →ₗ[R']
alternating_map R' Mᵢ (N₁ ⊗[R'] N₂) (ιa ⊕ ιb) :=
tensor_product.lift $ by
refine linear_map.mk₂ R' (dom_coprod)
(λ m₁ m₂ n, _)
(λ c m n, _)
(λ m n₁ n₂, _)
(λ c m n, _);
{ ext,
simp only [dom_coprod_apply, add_apply, smul_apply, ←finset.sum_add_distrib,
finset.smul_sum, multilinear_map.sum_apply, dom_coprod.summand],
congr,
ext σ,
apply σ.induction_on' (λ σ, _),
simp only [quotient.lift_on'_mk', coe_add, coe_smul, multilinear_map.smul_apply,
←multilinear_map.dom_coprod'_apply],
simp only [tensor_product.add_tmul, ←tensor_product.smul_tmul',
tensor_product.tmul_add, tensor_product.tmul_smul, linear_map.map_add, linear_map.map_smul],
rw ←smul_add <|> rw smul_comm,
congr }
@[simp]
lemma dom_coprod'_apply
(a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) :
dom_coprod' (a ⊗ₜ[R'] b) = dom_coprod a b :=
by simp only [dom_coprod', tensor_product.lift.tmul, linear_map.mk₂_apply]
end alternating_map
open equiv
/-- A helper lemma for `multilinear_map.dom_coprod_alternization`. -/
lemma multilinear_map.dom_coprod_alternization_coe
(a : multilinear_map R' (λ _ : ιa, Mᵢ) N₁) (b : multilinear_map R' (λ _ : ιb, Mᵢ) N₂) :
multilinear_map.dom_coprod ↑a.alternatization ↑b.alternatization =
∑ (σa : perm ιa) (σb : perm ιb), σa.sign • σb.sign •
multilinear_map.dom_coprod (a.dom_dom_congr σa) (b.dom_dom_congr σb) :=
begin
simp_rw [←multilinear_map.dom_coprod'_apply, multilinear_map.alternatization_coe],
simp_rw [tensor_product.sum_tmul, tensor_product.tmul_sum, linear_map.map_sum,
←tensor_product.smul_tmul', tensor_product.tmul_smul, linear_map.map_smul_of_tower],
end
open alternating_map
/-- Computing the `multilinear_map.alternatization` of the `multilinear_map.dom_coprod` is the same
as computing the `alternating_map.dom_coprod` of the `multilinear_map.alternatization`s.
-/
lemma multilinear_map.dom_coprod_alternization
(a : multilinear_map R' (λ _ : ιa, Mᵢ) N₁) (b : multilinear_map R' (λ _ : ιb, Mᵢ) N₂) :
(multilinear_map.dom_coprod a b).alternatization =
a.alternatization.dom_coprod b.alternatization :=
begin
apply coe_multilinear_map_injective,
rw [dom_coprod_coe, multilinear_map.alternatization_coe,
finset.sum_partition (quotient_group.left_rel (perm.sum_congr_hom ιa ιb).range)],
congr' 1,
ext1 σ,
apply σ.induction_on' (λ σ, _),
-- unfold the quotient mess left by `finset.sum_partition`
conv in (_ = quotient.mk' _) {
change quotient.mk' _ = quotient.mk' _,
rw quotient.eq',
rw [quotient_group.left_rel],
dsimp only [setoid.r] },
-- eliminate a multiplication
have : @finset.univ (perm (ιa ⊕ ιb)) _ = finset.univ.image ((*) σ) :=
(finset.eq_univ_iff_forall.mpr $ λ a, let ⟨a', ha'⟩ := mul_left_surjective σ a in
finset.mem_image.mpr ⟨a', finset.mem_univ _, ha'⟩).symm,
rw [this, finset.image_filter],
simp only [function.comp, mul_inv_rev, inv_mul_cancel_right, subgroup.inv_mem_iff],
simp only [monoid_hom.mem_range], -- needs to be separate from the above `simp only`
rw [finset.filter_congr_decidable,
finset.univ_filter_exists (perm.sum_congr_hom ιa ιb),
finset.sum_image (λ x _ y _ (h : _ = _), mul_right_injective _ h),
finset.sum_image (λ x _ y _ (h : _ = _), perm.sum_congr_hom_injective h)],
dsimp only,
-- now we're ready to clean up the RHS, pulling out the summation
rw [dom_coprod.summand_mk', multilinear_map.dom_coprod_alternization_coe,
←finset.sum_product', finset.univ_product_univ,
←multilinear_map.dom_dom_congr_equiv_apply, add_equiv.map_sum, finset.smul_sum],
congr' 1,
ext1 ⟨al, ar⟩,
dsimp only,
-- pull out the pair of smuls on the RHS, by rewriting to `_ →ₗ[ℤ] _` and back
rw [←add_equiv.coe_to_add_monoid_hom, ←add_monoid_hom.coe_to_int_linear_map,
linear_map.map_smul_of_tower,
linear_map.map_smul_of_tower,
add_monoid_hom.coe_to_int_linear_map, add_equiv.coe_to_add_monoid_hom,
multilinear_map.dom_dom_congr_equiv_apply],
-- pick up the pieces
rw [multilinear_map.dom_dom_congr_mul, perm.sign_mul,
perm.sum_congr_hom_apply, multilinear_map.dom_coprod_dom_dom_congr_sum_congr,
perm.sign_sum_congr, mul_smul, mul_smul],
end
/-- Taking the `multilinear_map.alternatization` of the `multilinear_map.dom_coprod` of two
`alternating_map`s gives a scaled version of the `alternating_map.coprod` of those maps.
-/
lemma multilinear_map.dom_coprod_alternization_eq
(a : alternating_map R' Mᵢ N₁ ιa) (b : alternating_map R' Mᵢ N₂ ιb) :
(multilinear_map.dom_coprod a b : multilinear_map R' (λ _ : ιa ⊕ ιb, Mᵢ) (N₁ ⊗ N₂))
.alternatization =
((fintype.card ιa).factorial * (fintype.card ιb).factorial) • a.dom_coprod b :=
begin
rw [multilinear_map.dom_coprod_alternization, coe_alternatization, coe_alternatization, mul_smul,
←dom_coprod'_apply, ←dom_coprod'_apply, ←tensor_product.smul_tmul', tensor_product.tmul_smul,
linear_map.map_smul_of_tower dom_coprod', linear_map.map_smul_of_tower dom_coprod'],
-- typeclass resolution is a little confused here
apply_instance, apply_instance,
end
end coprod
|
b0a219397331a948982c4f167f250a6224b5d748 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/measure_theory/lebesgue_measure.lean | 525613fa56f82db586322c5d18d1dfda9dc6bf88 | [
"Apache-2.0"
] | permissive | Lix0120/mathlib | 0020745240315ed0e517cbf32e738d8f9811dd80 | e14c37827456fc6707f31b4d1d16f1f3a3205e91 | refs/heads/master | 1,673,102,855,024 | 1,604,151,044,000 | 1,604,151,044,000 | 308,930,245 | 0 | 0 | Apache-2.0 | 1,604,164,710,000 | 1,604,163,547,000 | null | UTF-8 | Lean | false | false | 14,060 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import measure_theory.measure_space
import measure_theory.borel_space
/-!
# Lebesgue measure on the real line
-/
noncomputable theory
open classical set filter
open ennreal (of_real)
open_locale big_operators
namespace measure_theory
/-- Length of an interval. This is the largest monotonic function which correctly
measures all intervals. -/
def lebesgue_length (s : set ℝ) : ennreal := ⨅a b (h : s ⊆ Ico a b), of_real (b - a)
@[simp] lemma lebesgue_length_empty : lebesgue_length ∅ = 0 :=
le_zero_iff_eq.1 $ infi_le_of_le 0 $ infi_le_of_le 0 $ by simp
@[simp] lemma lebesgue_length_Ico (a b : ℝ) :
lebesgue_length (Ico a b) = of_real (b - a) :=
begin
refine le_antisymm (infi_le_of_le a $ binfi_le b (subset.refl _))
(le_infi $ λ a', le_infi $ λ b', le_infi $ λ h, ennreal.coe_le_coe.2 _),
cases le_or_lt b a with ab ab,
{ rw nnreal.of_real_of_nonpos (sub_nonpos.2 ab), apply zero_le },
cases (Ico_subset_Ico_iff ab).1 h with h₁ h₂,
exact nnreal.of_real_le_of_real (sub_le_sub h₂ h₁)
end
lemma lebesgue_length_mono {s₁ s₂ : set ℝ} (h : s₁ ⊆ s₂) :
lebesgue_length s₁ ≤ lebesgue_length s₂ :=
infi_le_infi $ λ a, infi_le_infi $ λ b, infi_le_infi2 $ λ h', ⟨subset.trans h h', le_refl _⟩
lemma lebesgue_length_eq_infi_Ioo (s) :
lebesgue_length s = ⨅a b (h : s ⊆ Ioo a b), of_real (b - a) :=
begin
refine le_antisymm
(infi_le_infi $ λ a, infi_le_infi $ λ b, infi_le_infi2 $ λ h,
⟨subset.trans h Ioo_subset_Ico_self, le_refl _⟩) _,
refine le_infi (λ a, le_infi $ λ b, le_infi $ λ h, _),
refine ennreal.le_of_forall_epsilon_le (λ ε ε0 _, _),
refine infi_le_of_le (a - ε) (infi_le_of_le b $ infi_le_of_le
(subset.trans h $ Ico_subset_Ioo_left $ (sub_lt_self_iff _).2 ε0) _),
rw ← sub_add,
refine le_trans ennreal.of_real_add_le (add_le_add_left _ _),
simp only [ennreal.of_real_coe_nnreal, le_refl]
end
@[simp] lemma lebesgue_length_Ioo (a b : ℝ) :
lebesgue_length (Ioo a b) = of_real (b - a) :=
begin
rw ← lebesgue_length_Ico,
refine le_antisymm (lebesgue_length_mono Ioo_subset_Ico_self) _,
rw lebesgue_length_eq_infi_Ioo (Ioo a b),
refine (le_infi $ λ a', le_infi $ λ b', le_infi $ λ h, _),
cases le_or_lt b a with ab ab, {simp [ab]},
cases (Ioo_subset_Ioo_iff ab).1 h with h₁ h₂,
rw [lebesgue_length_Ico],
exact ennreal.of_real_le_of_real (sub_le_sub h₂ h₁)
end
lemma lebesgue_length_eq_infi_Icc (s) :
lebesgue_length s = ⨅a b (h : s ⊆ Icc a b), of_real (b - a) :=
begin
refine le_antisymm _
(infi_le_infi $ λ a, infi_le_infi $ λ b, infi_le_infi2 $ λ h,
⟨subset.trans h Ico_subset_Icc_self, le_refl _⟩),
refine le_infi (λ a, le_infi $ λ b, le_infi $ λ h, _),
refine ennreal.le_of_forall_epsilon_le (λ ε ε0 _, _),
refine infi_le_of_le a (infi_le_of_le (b + ε) $ infi_le_of_le
(subset.trans h $ Icc_subset_Ico_right $ (lt_add_iff_pos_right _).2 ε0) _),
rw [← sub_add_eq_add_sub],
refine le_trans ennreal.of_real_add_le (add_le_add_left _ _),
simp only [ennreal.of_real_coe_nnreal, le_refl]
end
@[simp] lemma lebesgue_length_Icc (a b : ℝ) :
lebesgue_length (Icc a b) = of_real (b - a) :=
begin
rw ← lebesgue_length_Ico,
refine le_antisymm _ (lebesgue_length_mono Ico_subset_Icc_self),
rw lebesgue_length_eq_infi_Icc (Icc a b),
exact infi_le_of_le a (infi_le_of_le b $ infi_le_of_le (by refl) (by simp [le_refl]))
end
/-- The Lebesgue outer measure, as an outer measure of ℝ. -/
def lebesgue_outer : outer_measure ℝ :=
outer_measure.of_function lebesgue_length lebesgue_length_empty
lemma lebesgue_outer_le_length (s : set ℝ) : lebesgue_outer s ≤ lebesgue_length s :=
outer_measure.of_function_le _
lemma lebesgue_length_subadditive {a b : ℝ} {c d : ℕ → ℝ}
(ss : Icc a b ⊆ ⋃i, Ioo (c i) (d i)) :
(of_real (b - a) : ennreal) ≤ ∑' i, of_real (d i - c i) :=
begin
suffices : ∀ (s:finset ℕ) b
(cv : Icc a b ⊆ ⋃ i ∈ (↑s:set ℕ), Ioo (c i) (d i)),
(of_real (b - a) : ennreal) ≤ ∑ i in s, of_real (d i - c i),
{ rcases compact_Icc.elim_finite_subcover_image (λ (i : ℕ) (_ : i ∈ univ),
@is_open_Ioo _ _ _ _ (c i) (d i)) (by simpa using ss) with ⟨s, su, hf, hs⟩,
have e : (⋃ i ∈ (↑hf.to_finset:set ℕ),
Ioo (c i) (d i)) = (⋃ i ∈ s, Ioo (c i) (d i)), {simp [set.ext_iff]},
rw ennreal.tsum_eq_supr_sum,
refine le_trans _ (le_supr _ hf.to_finset),
exact this hf.to_finset _ (by simpa [e]) },
clear ss b,
refine λ s, finset.strong_induction_on s (λ s IH b cv, _),
cases le_total b a with ab ab,
{ rw ennreal.of_real_eq_zero.2 (sub_nonpos.2 ab), exact zero_le _ },
have := cv ⟨ab, le_refl _⟩, simp at this,
rcases this with ⟨i, is, cb, bd⟩,
rw [← finset.insert_erase is] at cv ⊢,
rw [finset.coe_insert, bUnion_insert] at cv,
rw [finset.sum_insert (finset.not_mem_erase _ _)],
refine le_trans _ (add_le_add_left (IH _ (finset.erase_ssubset is) (c i) _) _),
{ refine le_trans (ennreal.of_real_le_of_real _) ennreal.of_real_add_le,
rw sub_add_sub_cancel,
exact sub_le_sub_right (le_of_lt bd) _ },
{ rintro x ⟨h₁, h₂⟩,
refine (cv ⟨h₁, le_trans h₂ (le_of_lt cb)⟩).resolve_left
(mt and.left (not_lt_of_le h₂)) }
end
@[simp] lemma lebesgue_outer_Icc (a b : ℝ) :
lebesgue_outer (Icc a b) = of_real (b - a) :=
begin
refine le_antisymm (by rw ← lebesgue_length_Icc; apply lebesgue_outer_le_length)
(le_binfi $ λ f hf, ennreal.le_of_forall_epsilon_le $ λ ε ε0 h, _),
rcases ennreal.exists_pos_sum_of_encodable
(ennreal.zero_lt_coe_iff.2 ε0) ℕ with ⟨ε', ε'0, hε⟩,
refine le_trans _ (add_le_add_left (le_of_lt hε) _),
rw ← ennreal.tsum_add,
choose g hg using show
∀ i, ∃ p:ℝ×ℝ, f i ⊆ Ioo p.1 p.2 ∧ (of_real (p.2 - p.1) : ennreal) <
lebesgue_length (f i) + ε' i,
{ intro i,
have := (ennreal.lt_add_right (lt_of_le_of_lt (ennreal.le_tsum i) h)
(ennreal.zero_lt_coe_iff.2 (ε'0 i))),
conv at this {to_lhs, rw lebesgue_length_eq_infi_Ioo},
simpa [infi_lt_iff] },
refine le_trans _ (ennreal.tsum_le_tsum $ λ i, le_of_lt (hg i).2),
exact lebesgue_length_subadditive (subset.trans hf $
Union_subset_Union $ λ i, (hg i).1)
end
@[simp] lemma lebesgue_outer_singleton (a : ℝ) : lebesgue_outer {a} = 0 :=
by simpa using lebesgue_outer_Icc a a
@[simp] lemma lebesgue_outer_Ico (a b : ℝ) :
lebesgue_outer (Ico a b) = of_real (b - a) :=
by rw [← Icc_diff_right, lebesgue_outer.diff_null _ (lebesgue_outer_singleton _),
lebesgue_outer_Icc]
@[simp] lemma lebesgue_outer_Ioo (a b : ℝ) :
lebesgue_outer (Ioo a b) = of_real (b - a) :=
by rw [← Ico_diff_left, lebesgue_outer.diff_null _ (lebesgue_outer_singleton _), lebesgue_outer_Ico]
@[simp] lemma lebesgue_outer_Ioc (a b : ℝ) :
lebesgue_outer (Ioc a b) = of_real (b - a) :=
by rw [← Icc_diff_left, lebesgue_outer.diff_null _ (lebesgue_outer_singleton _), lebesgue_outer_Icc]
lemma is_lebesgue_measurable_Iio {c : ℝ} :
lebesgue_outer.caratheodory.is_measurable' (Iio c) :=
outer_measure.of_function_caratheodory $ λ t,
le_infi $ λ a, le_infi $ λ b, le_infi $ λ h, begin
refine le_trans (add_le_add
(lebesgue_length_mono $ inter_subset_inter_left _ h)
(lebesgue_length_mono $ diff_subset_diff_left h)) _,
cases le_total a c with hac hca; cases le_total b c with hbc hcb;
simp [*, -sub_eq_add_neg, sub_add_sub_cancel', le_refl],
{ simp [*, ← ennreal.of_real_add, -sub_eq_add_neg, sub_add_sub_cancel', le_refl] },
{ simp only [ennreal.of_real_eq_zero.2 (sub_nonpos.2 (le_trans hbc hca)), zero_add, le_refl] }
end
theorem lebesgue_outer_trim : lebesgue_outer.trim = lebesgue_outer :=
begin
refine le_antisymm (λ s, _) (outer_measure.le_trim _),
rw outer_measure.trim_eq_infi,
refine le_infi (λ f, le_infi $ λ hf,
ennreal.le_of_forall_epsilon_le $ λ ε ε0 h, _),
rcases ennreal.exists_pos_sum_of_encodable
(ennreal.zero_lt_coe_iff.2 ε0) ℕ with ⟨ε', ε'0, hε⟩,
refine le_trans _ (add_le_add_left (le_of_lt hε) _),
rw ← ennreal.tsum_add,
choose g hg using show
∀ i, ∃ s, f i ⊆ s ∧ is_measurable s ∧ lebesgue_outer s ≤ lebesgue_length (f i) + of_real (ε' i),
{ intro i,
have := (ennreal.lt_add_right (lt_of_le_of_lt (ennreal.le_tsum i) h)
(ennreal.zero_lt_coe_iff.2 (ε'0 i))),
conv at this {to_lhs, rw lebesgue_length},
simp only [infi_lt_iff] at this,
rcases this with ⟨a, b, h₁, h₂⟩,
rw ← lebesgue_outer_Ico at h₂,
exact ⟨_, h₁, is_measurable_Ico, le_of_lt $ by simpa using h₂⟩ },
simp at hg,
apply infi_le_of_le (Union g) _,
apply infi_le_of_le (subset.trans hf $ Union_subset_Union (λ i, (hg i).1)) _,
apply infi_le_of_le (is_measurable.Union (λ i, (hg i).2.1)) _,
exact le_trans (lebesgue_outer.Union _) (ennreal.tsum_le_tsum $ λ i, (hg i).2.2)
end
lemma borel_le_lebesgue_measurable : borel ℝ ≤ lebesgue_outer.caratheodory :=
begin
rw real.borel_eq_generate_from_Iio_rat,
refine measurable_space.generate_from_le _,
simp [is_lebesgue_measurable_Iio] { contextual := tt }
end
/-- Lebesgue measure on the Borel sets
The outer Lebesgue measure is the completion of this measure. (TODO: proof this)
-/
instance real.measure_space : measure_space ℝ :=
⟨{to_outer_measure := lebesgue_outer,
m_Union := λ f hf, lebesgue_outer.Union_eq_of_caratheodory $
λ i, borel_le_lebesgue_measurable _ (hf i),
trimmed := lebesgue_outer_trim }⟩
@[simp] theorem lebesgue_to_outer_measure :
(volume : measure ℝ).to_outer_measure = lebesgue_outer := rfl
end measure_theory
open measure_theory
namespace real
open_locale topological_space
theorem volume_val (s) : volume s = lebesgue_outer s := rfl
instance has_no_atoms_volume : has_no_atoms (volume : measure ℝ) :=
⟨lebesgue_outer_singleton⟩
@[simp] lemma volume_Ico {a b : ℝ} : volume (Ico a b) = of_real (b - a) := lebesgue_outer_Ico a b
@[simp] lemma volume_Icc {a b : ℝ} : volume (Icc a b) = of_real (b - a) := lebesgue_outer_Icc a b
@[simp] lemma volume_Ioo {a b : ℝ} : volume (Ioo a b) = of_real (b - a) := lebesgue_outer_Ioo a b
@[simp] lemma volume_Ioc {a b : ℝ} : volume (Ioc a b) = of_real (b - a) := lebesgue_outer_Ioc a b
@[simp] lemma volume_singleton {a : ℝ} : volume ({a} : set ℝ) = 0 := lebesgue_outer_singleton a
@[simp] lemma volume_interval {a b : ℝ} : volume (interval a b) = of_real (abs (b - a)) :=
by rw [interval, volume_Icc, max_sub_min_eq_abs]
instance locally_finite_volume : locally_finite_measure (volume : measure ℝ) :=
⟨λ x, ⟨Ioo (x - 1) (x + 1),
mem_nhds_sets is_open_Ioo ⟨sub_lt_self _ zero_lt_one, lt_add_of_pos_right _ zero_lt_one⟩,
by simp only [real.volume_Ioo, ennreal.of_real_lt_top]⟩⟩
lemma map_volume_add_left (a : ℝ) : measure.map ((+) a) volume = volume :=
eq.symm $ real.measure_ext_Ioo_rat $ λ p q,
by simp [measure.map_apply (measurable_add_left a) is_measurable_Ioo, sub_sub_sub_cancel_right]
lemma map_volume_add_right (a : ℝ) : measure.map (+ a) volume = volume :=
by simpa only [add_comm] using real.map_volume_add_left a
lemma smul_map_volume_mul_left {a : ℝ} (h : a ≠ 0) :
ennreal.of_real (abs a) • measure.map ((*) a) volume = volume :=
begin
refine (real.measure_ext_Ioo_rat $ λ p q, _).symm,
cases lt_or_gt_of_ne h with h h,
{ simp only [real.volume_Ioo, measure.smul_apply, ← ennreal.of_real_mul (le_of_lt $ neg_pos.2 h),
measure.map_apply (measurable_mul_left a) is_measurable_Ioo, neg_sub_neg,
← neg_mul_eq_neg_mul, preimage_const_mul_Ioo_of_neg _ _ h, abs_of_neg h, mul_sub,
mul_div_cancel' _ (ne_of_lt h)] },
{ simp only [real.volume_Ioo, measure.smul_apply, ← ennreal.of_real_mul (le_of_lt h),
measure.map_apply (measurable_mul_left a) is_measurable_Ioo, preimage_const_mul_Ioo _ _ h,
abs_of_pos h, mul_sub, mul_div_cancel' _ (ne_of_gt h)] }
end
lemma map_volume_mul_left {a : ℝ} (h : a ≠ 0) :
measure.map ((*) a) volume = ennreal.of_real (abs a⁻¹) • volume :=
by conv_rhs { rw [← real.smul_map_volume_mul_left h, smul_smul,
← ennreal.of_real_mul (abs_nonneg _), ← abs_mul, inv_mul_cancel h, abs_one, ennreal.of_real_one,
one_smul] }
lemma smul_map_volume_mul_right {a : ℝ} (h : a ≠ 0) :
ennreal.of_real (abs a) • measure.map (* a) volume = volume :=
by simpa only [mul_comm] using real.smul_map_volume_mul_left h
lemma map_volume_mul_right {a : ℝ} (h : a ≠ 0) :
measure.map (* a) volume = ennreal.of_real (abs a⁻¹) • volume :=
by simpa only [mul_comm] using real.map_volume_mul_left h
@[simp] lemma map_volume_neg : measure.map has_neg.neg (volume : measure ℝ) = volume :=
eq.symm $ real.measure_ext_Ioo_rat $ λ p q,
by simp [measure.map_apply measurable_neg is_measurable_Ioo]
end real
open_locale topological_space
lemma filter.eventually.volume_pos_of_nhds_real {p : ℝ → Prop} {a : ℝ} (h : ∀ᶠ x in 𝓝 a, p x) :
(0 : ennreal) < volume {x | p x} :=
begin
rcases h.exists_Ioo_subset with ⟨l, u, hx, hs⟩,
refine lt_of_lt_of_le _ (measure_mono hs),
simpa [-mem_Ioo] using hx.1.trans hx.2
end
/-
section vitali
def vitali_aux_h (x : ℝ) (h : x ∈ Icc (0:ℝ) 1) :
∃ y ∈ Icc (0:ℝ) 1, ∃ q:ℚ, ↑q = x - y :=
⟨x, h, 0, by simp⟩
def vitali_aux (x : ℝ) (h : x ∈ Icc (0:ℝ) 1) : ℝ :=
classical.some (vitali_aux_h x h)
theorem vitali_aux_mem (x : ℝ) (h : x ∈ Icc (0:ℝ) 1) : vitali_aux x h ∈ Icc (0:ℝ) 1 :=
Exists.fst (classical.some_spec (vitali_aux_h x h):_)
theorem vitali_aux_rel (x : ℝ) (h : x ∈ Icc (0:ℝ) 1) :
∃ q:ℚ, ↑q = x - vitali_aux x h :=
Exists.snd (classical.some_spec (vitali_aux_h x h):_)
def vitali : set ℝ := {x | ∃ h, x = vitali_aux x h}
theorem vitali_nonmeasurable : ¬ is_null_measurable measure_space.μ vitali :=
sorry
end vitali
-/
|
21d81c399bc2f077cdb5619281fd9670cfd7a553 | f1b175e38ffc5cc1c7c5551a72d0dbaf70786f83 | /data/quot.lean | 0d9ad13770ab47aedda2176346364dc590b69482 | [
"Apache-2.0"
] | permissive | mjendrusch/mathlib | df3ae884dd5ce38c7edf452bcbfd3baf4e3a6214 | 5c209edb7eb616a26f64efe3500f2b1ba95b8d55 | refs/heads/master | 1,585,663,284,800 | 1,539,062,055,000 | 1,539,062,055,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,213 | 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
Quotients -- extends the core library
-/
variables {α : Sort*} {β : Sort*}
namespace quot
variables {ra : α → α → Prop} {rb : β → β → Prop} {φ : quot ra → quot rb → Sort*}
local notation `⟦`:max a `⟧` := quot.mk _ a
protected def hrec_on₂ (qa : quot ra) (qb : quot rb) (f : ∀ a b, φ ⟦a⟧ ⟦b⟧)
(ca : ∀ {b a₁ a₂}, ra a₁ a₂ → f a₁ b == f a₂ b)
(cb : ∀ {a b₁ b₂}, rb b₁ b₂ → f a b₁ == f a b₂) : φ qa qb :=
quot.hrec_on qa (λ a, quot.hrec_on qb (f a) (λ b₁ b₂ pb, cb pb)) $ λ a₁ a₂ pa,
quot.induction_on qb $ λ b,
calc @quot.hrec_on _ _ (φ _) ⟦b⟧ (f a₁) (@cb _)
== f a₁ b : by simp
... == f a₂ b : ca pa
... == @quot.hrec_on _ _ (φ _) ⟦b⟧ (f a₂) (@cb _) : by simp
end quot
namespace quotient
variables [sa : setoid α] [sb : setoid β]
variables {φ : quotient sa → quotient sb → Sort*}
protected def hrec_on₂ (qa : quotient sa) (qb : quotient sb) (f : ∀ a b, φ ⟦a⟧ ⟦b⟧)
(c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ == f a₂ b₂) : φ qa qb :=
quot.hrec_on₂ qa qb f
(λ _ _ _ p, c _ _ _ _ p (setoid.refl _))
(λ _ _ _ p, c _ _ _ _ (setoid.refl _) p)
end quotient
@[simp] theorem quotient.eq [r : setoid α] {x y : α} : ⟦x⟧ = ⟦y⟧ ↔ x ≈ y :=
⟨quotient.exact, quotient.sound⟩
theorem forall_quotient_iff {α : Type*} [r : setoid α] {p : quotient r → Prop} :
(∀a:quotient r, p a) ↔ (∀a:α, p ⟦a⟧) :=
⟨assume h x, h _, assume h a, a.induction_on h⟩
@[simp] lemma quotient.lift_beta [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b) (x : α):
quotient.lift f h (quotient.mk x) = f x := rfl
@[simp] lemma quotient.lift_on_beta [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b) (x : α):
quotient.lift_on (quotient.mk x) f h = f x := rfl
/-- Choose an element of the equivalence class using the axiom of choice.
Sound but noncomputable. -/
noncomputable def quot.out {r : α → α → Prop} (q : quot r) : α :=
classical.some (quot.exists_rep q)
/-- Unwrap the VM representation of a quotient to obtain an element of the equivalence class.
Computable but unsound. -/
meta def quot.unquot {r : α → α → Prop} : quot r → α := unchecked_cast
@[simp] theorem quot.out_eq {r : α → α → Prop} (q : quot r) : quot.mk r q.out = q :=
classical.some_spec (quot.exists_rep q)
/-- Choose an element of the equivalence class using the axiom of choice.
Sound but noncomputable. -/
noncomputable def quotient.out [s : setoid α] : quotient s → α := quot.out
@[simp] theorem quotient.out_eq [s : setoid α] (q : quotient s) : ⟦q.out⟧ = q := q.out_eq
theorem quotient.mk_out [s : setoid α] (a : α) : ⟦a⟧.out ≈ a :=
quotient.exact (quotient.out_eq _)
instance pi_setoid {ι : Sort*} {α : ι → Sort*} [∀ i, setoid (α i)] : setoid (Π i, α i) :=
{ r := λ a b, ∀ i, a i ≈ b i,
iseqv := ⟨
λ a i, setoid.refl _,
λ a b h i, setoid.symm (h _),
λ a b c h₁ h₂ i, setoid.trans (h₁ _) (h₂ _)⟩ }
noncomputable def quotient.choice {ι : Type*} {α : ι → Type*} [S : ∀ i, setoid (α i)]
(f : ∀ i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) :=
⟦λ i, (f i).out⟧
theorem quotient.choice_eq {ι : Type*} {α : ι → Type*} [∀ i, setoid (α i)]
(f : ∀ i, α i) : quotient.choice (λ i, ⟦f i⟧) = ⟦f⟧ :=
quotient.sound $ λ i, quotient.mk_out _
/-- `trunc α` is the quotient of `α` by the always-true relation. This
is related to the propositional truncation in HoTT, and is similar
in effect to `nonempty α`, but unlike `nonempty α`, `trunc α` is data,
so the VM representation is the same as `α`, and so this can be used to
maintain computability. -/
def {u} trunc (α : Sort u) : Sort u := @quot α (λ _ _, true)
theorem true_equivalence : @equivalence α (λ _ _, true) :=
⟨λ _, trivial, λ _ _ _, trivial, λ _ _ _ _ _, trivial⟩
namespace trunc
/-- Constructor for `trunc α` -/
def mk (a : α) : trunc α := quot.mk _ a
/-- Any constant function lifts to a function out of the truncation -/
def lift (f : α → β) (c : ∀ a b : α, f a = f b) : trunc α → β :=
quot.lift f (λ a b _, c a b)
theorem ind {β : trunc α → Prop} : (∀ a : α, β (mk a)) → ∀ q : trunc α, β q := quot.ind
protected theorem lift_beta (f : α → β) (c) (a : α) : lift f c (mk a) = f a := rfl
@[reducible, elab_as_eliminator]
protected def lift_on (q : trunc α) (f : α → β)
(c : ∀ a b : α, f a = f b) : β := lift f c q
@[elab_as_eliminator]
protected theorem induction_on {β : trunc α → Prop} (q : trunc α)
(h : ∀ a, β (mk a)) : β q := ind h q
theorem exists_rep (q : trunc α) : ∃ a : α, mk a = q := quot.exists_rep q
attribute [elab_as_eliminator]
protected theorem induction_on₂
{C : trunc α → trunc β → Prop} (q₁ : trunc α) (q₂ : trunc β) (h : ∀ a b, C (mk a) (mk b)) : C q₁ q₂ :=
trunc.induction_on q₁ $ λ a₁, trunc.induction_on q₂ (h a₁)
protected theorem eq (a b : trunc α) : a = b :=
trunc.induction_on₂ a b (λ x y, quot.sound trivial)
instance : subsingleton (trunc α) := ⟨trunc.eq⟩
def bind (q : trunc α) (f : α → trunc β) : trunc β :=
trunc.lift_on q f (λ a b, trunc.eq _ _)
def map (f : α → β) (q : trunc α) : trunc β := bind q (trunc.mk ∘ f)
instance : monad trunc :=
{ pure := @trunc.mk,
bind := @trunc.bind }
instance : is_lawful_monad trunc :=
{ id_map := λ α q, trunc.eq _ _,
pure_bind := λ α β q f, rfl,
bind_assoc := λ α β γ x f g, trunc.eq _ _ }
variable {C : trunc α → Sort*}
@[reducible, elab_as_eliminator]
protected def rec
(f : Π a, C (mk a)) (h : ∀ (a b : α), (eq.rec (f a) (trunc.eq (mk a) (mk b)) : C (mk b)) = f b)
(q : trunc α) : C q :=
quot.rec f (λ a b _, h a b) q
@[reducible, elab_as_eliminator]
protected def rec_on (q : trunc α) (f : Π a, C (mk a))
(h : ∀ (a b : α), (eq.rec (f a) (trunc.eq (mk a) (mk b)) : C (mk b)) = f b) : C q :=
trunc.rec f h q
@[reducible, elab_as_eliminator]
protected def rec_on_subsingleton
[∀ a, subsingleton (C (mk a))] (q : trunc α) (f : Π a, C (mk a)) : C q :=
trunc.rec f (λ a b, subsingleton.elim _ (f b)) q
/-- Noncomputably extract a representative of `trunc α` (using the axiom of choice). -/
noncomputable def out : trunc α → α := quot.out
@[simp] theorem out_eq (q : trunc α) : mk q.out = q := trunc.eq _ _
end trunc
theorem nonempty_of_trunc (q : trunc α) : nonempty α :=
let ⟨a, _⟩ := q.exists_rep in ⟨a⟩
namespace quotient
variables {γ : Sort*} {φ : Sort*}
{s₁ : setoid α} {s₂ : setoid β} {s₃ : setoid γ}
/- Versions of quotient definitions and lemmas ending in `'` use unification instead
of typeclass inference for inferring the `setoid` argument. This is useful when there are
several different quotient relations on a type, for example quotient groups, rings and modules -/
protected def mk' (a : α) : quotient s₁ := quot.mk s₁.1 a
@[elab_as_eliminator, reducible]
protected def lift_on' (q : quotient s₁) (f : α → φ)
(h : ∀ a b, @setoid.r α s₁ a b → f a = f b) : φ := quotient.lift_on q f h
@[elab_as_eliminator, reducible]
protected def lift_on₂' (q₁ : quotient s₁) (q₂ : quotient s₂) (f : α → β → γ)
(h : ∀ a₁ a₂ b₁ b₂, @setoid.r α s₁ a₁ b₁ → @setoid.r β s₂ a₂ b₂ → f a₁ a₂ = f b₁ b₂) : γ :=
quotient.lift_on₂ q₁ q₂ f h
@[elab_as_eliminator]
protected lemma induction_on' {p : quotient s₁ → Prop} (q : quotient s₁)
(h : ∀ a, p (quotient.mk' a)) : p q := quotient.induction_on q h
@[elab_as_eliminator]
protected lemma induction_on₂' {p : quotient s₁ → quotient s₂ → Prop} (q₁ : quotient s₁)
(q₂ : quotient s₂) (h : ∀ a₁ a₂, p (quotient.mk' a₁) (quotient.mk' a₂)) : p q₁ q₂ :=
quotient.induction_on₂ q₁ q₂ h
@[elab_as_eliminator]
protected lemma induction_on₃' {p : quotient s₁ → quotient s₂ → quotient s₃ → Prop}
(q₁ : quotient s₁) (q₂ : quotient s₂) (q₃ : quotient s₃)
(h : ∀ a₁ a₂ a₃, p (quotient.mk' a₁) (quotient.mk' a₂) (quotient.mk' a₃)) : p q₁ q₂ q₃ :=
quotient.induction_on₃ q₁ q₂ q₃ h
lemma exact' {a b : α} :
(quotient.mk' a : quotient s₁) = quotient.mk' b → @setoid.r _ s₁ a b :=
quotient.exact
lemma sound' {a b : α} : @setoid.r _ s₁ a b → @quotient.mk' α s₁ a = quotient.mk' b :=
quotient.sound
@[simp] protected lemma eq' {a b : α} : @quotient.mk' α s₁ a = quotient.mk' b ↔ @setoid.r _ s₁ a b :=
quotient.eq
noncomputable def out' (a : quotient s₁) : α := quotient.out a
@[simp] theorem out_eq' (q : quotient s₁) : quotient.mk' q.out' = q := q.out_eq
theorem mk_out' (a : α) : @setoid.r α s₁ (quotient.mk' a : quotient s₁).out' a :=
quotient.exact (quotient.out_eq _)
end quotient |
bb6526b285d2adee145e7d32a916470524fc2b5e | a4673261e60b025e2c8c825dfa4ab9108246c32e | /stage0/src/Lean/Elab/DeclModifiers.lean | 20daea9cdcd6e6630aa766c79f77958dbf888c1e | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,599 | 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.Modifiers
import Lean.Elab.Attributes
import Lean.Elab.Exception
import Lean.Elab.DeclUtil
namespace Lean.Elab
def checkNotAlreadyDeclared {m} [Monad m] [MonadEnv m] [MonadExceptOf Exception m] [MonadRef m] [AddErrorMessageContext m] (declName : Name) : m Unit := do
let env ← getEnv
if env.contains declName then
match privateToUserName? declName with
| none => throwError! "'{declName}' has already been declared"
| some declName => throwError! "private declaration '{declName}' has already been declared"
if env.contains (mkPrivateName env declName) then
throwError! "a private declaration '{declName}' has already been declared"
match privateToUserName? declName with
| none => pure ()
| some declName =>
if env.contains declName then
throwError! "a non-private declaration '{declName}' has already been declared"
inductive Visibility :=
| regular | «protected» | «private»
instance : ToString Visibility := ⟨fun
| Visibility.regular => "regular"
| Visibility.«private» => "private"
| Visibility.«protected» => "protected"⟩
structure Modifiers :=
(docString : Option String := none)
(visibility : Visibility := Visibility.regular)
(isNoncomputable : Bool := false)
(isPartial : Bool := false)
(isUnsafe : Bool := false)
(attrs : Array Attribute := #[])
def Modifiers.isPrivate : Modifiers → Bool
| { visibility := Visibility.private, .. } => true
| _ => false
def Modifiers.isProtected : Modifiers → Bool
| { visibility := Visibility.protected, .. } => true
| _ => false
def Modifiers.addAttribute (modifiers : Modifiers) (attr : Attribute) : Modifiers :=
{ modifiers with attrs := modifiers.attrs.push attr }
instance : ToFormat Modifiers := ⟨fun m =>
let components : List Format :=
(match m.docString with
| some str => ["/--" ++ str ++ "-/"]
| none => [])
++ (match m.visibility with
| Visibility.regular => []
| Visibility.protected => ["protected"]
| Visibility.private => ["private"])
++ (if m.isNoncomputable then ["noncomputable"] else [])
++ (if m.isPartial then ["partial"] else [])
++ (if m.isUnsafe then ["unsafe"] else [])
++ m.attrs.toList.map (fun attr => fmt attr)
Format.bracket "{" (Format.joinSep components ("," ++ Format.line)) "}"⟩
instance : ToString Modifiers := ⟨toString ∘ format⟩
section Methods
variables {m : Type → Type} [Monad m] [MonadEnv m] [MonadExceptOf Exception m] [MonadRef m] [AddErrorMessageContext m]
def elabModifiers (stx : Syntax) : m Modifiers := do
let docCommentStx := stx[0]
let attrsStx := stx[1]
let visibilityStx := stx[2]
let noncompStx := stx[3]
let unsafeStx := stx[4]
let partialStx := stx[5]
let docString ← match docCommentStx.getOptional? with
| none => pure none
| some s => match s[1] with
| Syntax.atom _ val => pure (some (val.extract 0 (val.bsize - 2)))
| _ => throwErrorAt! s "unexpected doc string {s[1]}"
let visibility ← match visibilityStx.getOptional? with
| none => pure Visibility.regular
| some v =>
let kind := v.getKind
if kind == `Lean.Parser.Command.private then pure Visibility.private
else if kind == `Lean.Parser.Command.protected then pure Visibility.protected
else throwErrorAt v "unexpected visibility modifier"
let attrs ← match attrsStx.getOptional? with
| none => pure #[]
| some attrs => elabDeclAttrs attrs
pure {
docString := docString,
visibility := visibility,
isPartial := !partialStx.isNone,
isUnsafe := !unsafeStx.isNone,
isNoncomputable := !noncompStx.isNone,
attrs := attrs
}
def applyVisibility (visibility : Visibility) (declName : Name) : m Name := do
match visibility with
| Visibility.private =>
let env ← getEnv
let declName := mkPrivateName env declName
checkNotAlreadyDeclared declName
pure declName
| Visibility.protected =>
checkNotAlreadyDeclared declName
let env ← getEnv
let env := addProtected env declName
setEnv env
pure declName
| _ =>
checkNotAlreadyDeclared declName
pure declName
def mkDeclName (currNamespace : Name) (modifiers : Modifiers) (shortName : Name) : m (Name × Name) := do
let name := (extractMacroScopes shortName).name
unless name.isAtomic || isFreshInstanceName name do
throwError! "atomic identifier expected '{shortName}'"
let declName := currNamespace ++ shortName
let declName ← applyVisibility modifiers.visibility declName
match modifiers.visibility with
| Visibility.protected =>
match currNamespace with
| Name.str _ s _ => pure (declName, Name.mkSimple s ++ shortName)
| _ => throwError "protected declarations must be in a namespace"
| _ => pure (declName, shortName)
/-
`declId` is of the form
```
parser! ident >> optional (".{" >> sepBy1 ident ", " >> "}")
```
but we also accept a single identifier to users to make macro writing more convenient .
-/
def expandDeclIdCore (declId : Syntax) : Name × Syntax :=
if declId.isIdent then
(declId.getId, mkNullNode)
else
let id := declId[0].getId
let optUnivDeclStx := declId[1]
(id, optUnivDeclStx)
structure ExpandDeclIdResult :=
(shortName : Name)
(declName : Name)
(levelNames : List Name)
def expandDeclId (currNamespace : Name) (currLevelNames : List Name) (declId : Syntax) (modifiers : Modifiers) : m ExpandDeclIdResult := do
-- ident >> optional (".{" >> sepBy1 ident ", " >> "}")
let (shortName, optUnivDeclStx) := expandDeclIdCore declId
let levelNames ←
if optUnivDeclStx.isNone then
pure currLevelNames
else
let extraLevels := optUnivDeclStx[1].getArgs.getEvenElems
extraLevels.foldlM
(fun levelNames idStx =>
let id := idStx.getId
if levelNames.elem id then
withRef idStx $ throwAlreadyDeclaredUniverseLevel id
else
pure (id :: levelNames))
currLevelNames
let (declName, shortName) ← withRef declId $ mkDeclName currNamespace modifiers shortName
pure { shortName := shortName, declName := declName, levelNames := levelNames }
end Methods
end Lean.Elab
|
0b3824f8e4232f6e2b5d01d831b29261022ade48 | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/tactic/omega/eq_elim.lean | f25ca36c1f6b103e85621cdcfc807924174cf233 | [
"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 | 14,548 | lean | /-
Copyright (c) 2019 Seul Baek. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Seul Baek
Correctness lemmas for equality elimination.
See 5.5 of http://www.decision-procedures.org/ for details.
-/
import tactic.omega.clause
open list.func
namespace omega
def symdiv (i j : int) : int :=
if (2 * (i % j)) < j
then i / j
else (i / j) + 1
def symmod (i j : int) : int :=
if (2 * (i % j)) < j
then i % j
else (i % j) - j
lemma symmod_add_one_self {i : int} :
0 < i → symmod i (i+1) = -1 :=
begin
intro h1,
unfold symmod,
rw [int.mod_eq_of_lt (le_of_lt h1) (lt_add_one _), if_neg],
simp only [add_comm, add_neg_cancel_left,
neg_add_rev, sub_eq_add_neg],
have h2 : 2 * i = (1 + 1) * i := rfl,
simpa only [h2, add_mul, one_mul,
add_lt_add_iff_left, not_lt] using h1
end
lemma mul_symdiv_eq {i j : int} :
j * (symdiv i j) = i - (symmod i j) :=
begin
unfold symdiv, unfold symmod,
by_cases h1 : (2 * (i % j)) < j,
{ repeat {rw if_pos h1},
rw [int.mod_def, sub_sub_cancel] },
{ repeat {rw if_neg h1},
rw [int.mod_def, sub_sub, sub_sub_cancel,
mul_add, mul_one] }
end
lemma symmod_eq {i j : int} :
symmod i j = i - j * (symdiv i j) :=
by rw [mul_symdiv_eq, sub_sub_cancel]
/- (sgm v b as n) is the new value assigned to the nth variable
after a single step of equality elimination using valuation v,
term ⟨b, as⟩, and variable index n. If v satisfies the initial
constraint set, then (v ⟨n ↦ sgm v b as n⟩) satisfies the new
constraint set after equality elimination. -/
def sgm (v : nat → int) (b : int) (as : list int) (n : nat) :=
let a_n : int := get n as in
let m : int := a_n + 1 in
((symmod b m) + (coeffs.val v (as.map (λ x, symmod x m)))) / m
local notation as ` {` m ` ↦ ` a `}` := set a as m
def rhs : nat → int → list int → term
| n b as :=
let m := get n as + 1 in
⟨(symmod b m), (as.map (λ x, symmod x m)) {n ↦ -m}⟩
lemma rhs_correct_aux {v : nat → int} {m : int} {as : list int} :
∀ {k}, ∃ d, (m * d +
coeffs.val_between v (as.map (λ (x : ℤ), symmod x m)) 0 k =
coeffs.val_between v as 0 k)
| 0 :=
begin
existsi (0 : int),
simp only [add_zero, mul_zero, coeffs.val_between]
end
| (k+1) :=
begin
simp only [zero_add, coeffs.val_between, list.map],
cases @rhs_correct_aux k with d h1, rw ← h1,
by_cases hk : k < as.length,
{ rw [get_map hk, symmod_eq, sub_mul],
existsi (d + (symdiv (get k as) m * v k)),
ring },
{ rw not_lt at hk,
repeat {rw get_eq_default_of_le},
existsi d,
rw add_assoc,
exact hk,
simp only [hk, list.length_map] }
end
local notation v ` ⟨` m ` ↦ ` a `⟩` := update m a v
lemma rhs_correct {v : nat → int}
{b : int} {as : list int} (n : nat) :
0 < get n as →
0 = term.val v (b,as) →
v n = term.val (v ⟨n ↦ sgm v b as n⟩) (rhs n b as) :=
begin
intros h0 h1,
let a_n := get n as,
let m := a_n + 1,
have h3 : m ≠ 0,
{ apply ne_of_gt, apply lt_trans h0, simp [a_n, m] },
have h2 : m * (sgm v b as n) = (symmod b m) +
coeffs.val v (as.map (λ x, symmod x m)),
{ simp only [sgm, mul_comm m],
rw [int.div_mul_cancel],
have h4 : ∃ c, m * c + (symmod b (get n as + 1) +
coeffs.val v (as.map (λ (x : ℤ), symmod x m))) = term.val v (b,as),
{ have h5: ∃ d, m * d +
(coeffs.val v (as.map (λ x, symmod x m))) = coeffs.val v as,
{ simp only [coeffs.val, list.length_map], apply rhs_correct_aux },
cases h5 with d h5, rw symmod_eq,
existsi (symdiv b m + d),
unfold term.val, rw ← h5,
simp only [term.val, mul_add, add_mul, m, a_n],
ring },
cases h4 with c h4,
rw [dvd_add_iff_right (dvd_mul_right m c), h4, ← h1],
apply dvd_zero },
apply calc v n
= -(m * sgm v b as n) + (symmod b m) +
(coeffs.val_except n v (as.map (λ x, symmod x m))) :
begin
rw [h2, ← coeffs.val_except_add_eq n],
have hn : n < as.length,
{ by_contra hc, rw not_lt at hc,
rw (get_eq_default_of_le n hc) at h0,
cases h0 },
rw get_map hn,
simp only [a_n, m],
rw [add_comm, symmod_add_one_self h0],
ring
end
... = term.val (v⟨n↦sgm v b as n⟩) (rhs n b as) :
begin
unfold rhs, unfold term.val,
rw [← coeffs.val_except_add_eq n, get_set, update_eq],
have h2 : ∀ a b c : int, a + b + c = b + (c + a) := by {intros, ring},
rw (h2 (- _)),
apply fun_mono_2 rfl,
apply fun_mono_2,
{ rw coeffs.val_except_update_set },
{ simp only [m, a_n], ring }
end
end
def sym_sym (m b : int) : int :=
symdiv b m + symmod b m
def coeffs_reduce : nat → int → list int → term
| n b as :=
let a := get n as in
let m := a + 1 in
(sym_sym m b, (as.map (sym_sym m)) {n ↦ -a})
lemma coeffs_reduce_correct
{v : nat → int} {b : int} {as : list int} {n : nat} :
0 < get n as →
0 = term.val v (b,as) →
0 = term.val (v ⟨n ↦ sgm v b as n⟩) (coeffs_reduce n b as) :=
begin
intros h1 h2,
let a_n := get n as,
let m := a_n + 1,
have h3 : m ≠ 0,
{ apply ne_of_gt,
apply lt_trans h1,
simp only [m, lt_add_iff_pos_right] },
have h4 : 0 = (term.val (v⟨n↦sgm v b as n⟩) (coeffs_reduce n b as)) * m :=
calc 0
= term.val v (b,as) : h2
... = b + coeffs.val_except n v as
+ a_n * ((rhs n b as).val (v⟨n ↦ sgm v b as n⟩)) :
begin
unfold term.val,
rw [← coeffs.val_except_add_eq n, rhs_correct n h1 h2],
simp only [a_n, add_assoc],
end
... = -(m * a_n * sgm v b as n) + (b + a_n * (symmod b m)) +
(coeffs.val_except n v as +
a_n * coeffs.val_except n v (as.map (λ x, symmod x m))) :
begin
simp only [term.val, rhs, mul_add, m, a_n,
add_assoc, add_left_inj, add_comm, add_left_comm],
rw [← coeffs.val_except_add_eq n,
get_set, update_eq, mul_add],
apply fun_mono_2,
{ rw coeffs.val_except_eq_val_except
update_eq_of_ne (get_set_eq_of_ne _) },
simp only [m], ring,
end
... = -(m * a_n * sgm v b as n) + (b + a_n * (symmod b m))
+ coeffs.val_except n v (as.map (λ a_i, a_i + a_n * (symmod a_i m))) :
begin
apply fun_mono_2 rfl,
simp only [coeffs.val_except, mul_add],
repeat {rw ← coeffs.val_between_map_mul},
have h4 : ∀ {a b c d : int},
a + b + (c + d) = (a + c) + (b + d),
{ intros, ring }, rw h4,
have h5 : add as (list.map (has_mul.mul a_n)
(list.map (λ (x : ℤ), symmod x (get n as + 1)) as)) =
list.map (λ (a_i : ℤ), a_i + a_n * symmod a_i m) as,
{ rw [list.map_map, ←map_add_map],
apply fun_mono_2,
{ have h5 : (λ x : int, x) = id,
{ rw function.funext_iff, intro x, refl },
rw [h5, list.map_id] },
{ apply fun_mono_2 _ rfl,
rw function.funext_iff, intro x,
simp only [m] } },
simp only [list.length_map],
repeat { rw [← coeffs.val_between_add, h5] },
end
... = -(m * a_n * sgm v b as n) + (m * sym_sym m b)
+ coeffs.val_except n v (as.map (λ a_i, m * sym_sym m a_i)) :
begin
repeat {rw add_assoc}, apply fun_mono_2, refl,
rw ← add_assoc,
have h4 : ∀ (x : ℤ), x + a_n * symmod x m = m * sym_sym m x,
{ intro x, have h5 : a_n = m - 1,
{ simp only [m],
rw add_sub_cancel },
rw [h5, sub_mul, one_mul, add_sub,
add_comm, add_sub_assoc, ← mul_symdiv_eq],
simp only [sym_sym, mul_add, add_comm] },
apply fun_mono_2 (h4 _),
apply coeffs.val_except_eq_val_except; intros x h5, refl,
apply congr_arg,
apply fun_mono_2 _ rfl,
rw function.funext_iff,
apply h4
end
... = (-(a_n * sgm v b as n) + (sym_sym m b)
+ coeffs.val_except n v (as.map (sym_sym m))) * m :
begin
simp only [add_mul _ _ m],
apply fun_mono_2, ring,
simp only [coeffs.val_except, add_mul _ _ m],
apply fun_mono_2,
{ rw [mul_comm _ m, ← coeffs.val_between_map_mul, list.map_map] },
simp only [list.length_map, mul_comm _ m],
rw [← coeffs.val_between_map_mul, list.map_map]
end
... = (sym_sym m b + (coeffs.val_except n v (as.map (sym_sym m)) +
(-a_n * sgm v b as n))) * m : by ring
... = (term.val (v ⟨n ↦ sgm v b as n⟩) (coeffs_reduce n b as)) * m :
begin
simp only [coeffs_reduce, term.val, m, a_n],
rw [← coeffs.val_except_add_eq n,
coeffs.val_except_update_set, get_set, update_eq]
end,
rw [← int.mul_div_cancel (term.val _ _) h3, ← h4, int.zero_div]
end
-- Requires : t1.coeffs[m] = 1
def cancel (m : nat) (t1 t2 : term) : term :=
term.add (t1.mul (-(get m (t2.snd)))) t2
def subst (n : nat) (t1 t2 : term) : term :=
term.add (t1.mul (get n t2.snd)) (t2.fst, t2.snd {n ↦ 0})
lemma subst_correct {v : nat → int} {b : int}
{as : list int} {t : term} {n : nat} :
0 < get n as → 0 = term.val v (b,as) →
term.val v t = term.val (v ⟨n ↦ sgm v b as n⟩) (subst n (rhs n b as) t) :=
begin
intros h1 h2,
simp only [subst, term.val, term.val_add, term.val_mul],
rw ← rhs_correct _ h1 h2,
cases t with b' as',
simp only [term.val],
have h3 : coeffs.val (v ⟨n ↦ sgm v b as n⟩) (as' {n ↦ 0}) =
coeffs.val_except n v as',
{ rw [← coeffs.val_except_add_eq n, get_set,
zero_mul, add_zero, coeffs.val_except_update_set] },
rw [h3, ← coeffs.val_except_add_eq n], ring
end
/- The type of equality elimination rules. -/
@[derive has_reflect]
inductive ee : Type
| drop : ee
| nondiv : int → ee
| factor : int → ee
| neg : ee
| reduce : nat → ee
| cancel : nat → ee
namespace ee
def repr : ee → string
| drop := "↓"
| (nondiv i) := i.repr ++ "∤"
| (factor i) := "/" ++ i.repr
| neg := "-"
| (reduce n) := "≻" ++ n.repr
| (cancel n) := "+" ++ n.repr
instance has_repr : has_repr ee := ⟨repr⟩
meta instance has_to_format : has_to_format ee := ⟨λ x, x.repr⟩
end ee
def eq_elim : list ee → clause → clause
| [] ([], les) := ([],les)
| [] ((_::_), les) := ([],[])
| (_::_) ([], les) := ([],[])
| (ee.drop::es) ((eq::eqs), les) := eq_elim es (eqs, les)
| (ee.neg::es) ((eq::eqs), les) := eq_elim es ((eq.neg::eqs), les)
| (ee.nondiv i::es) ((b,as)::eqs, les) :=
if ¬(i ∣ b) ∧ (∀ x ∈ as, i ∣ x)
then ([],[⟨-1,[]⟩])
else ([],[])
| (ee.factor i::es) ((b,as)::eqs, les) :=
if (i ∣ b) ∧ (∀ x ∈ as, i ∣ x)
then eq_elim es ((term.div i (b,as)::eqs), les)
else ([],[])
| (ee.reduce n::es) ((b,as)::eqs, les) :=
if 0 < get n as
then let eq' := coeffs_reduce n b as in
let r := rhs n b as in
let eqs' := eqs.map (subst n r) in
let les' := les.map (subst n r) in
eq_elim es ((eq'::eqs'), les')
else ([],[])
| (ee.cancel m::es) ((eq::eqs), les) :=
eq_elim es ((eqs.map (cancel m eq)), (les.map (cancel m eq)))
open tactic
lemma sat_empty : clause.sat ([],[]) :=
⟨λ _,0, ⟨dec_trivial, dec_trivial⟩⟩
lemma sat_eq_elim :
∀ {es : list ee} {c : clause}, c.sat → (eq_elim es c).sat
| [] ([], les) h := h
| (e::_) ([], les) h :=
by {cases e; simp only [eq_elim]; apply sat_empty}
| [] ((_::_), les) h := sat_empty
| (ee.drop::es) ((eq::eqs), les) h1 :=
begin
apply (@sat_eq_elim es _ _),
rcases h1 with ⟨v,h1,h2⟩,
refine ⟨v, list.forall_mem_of_forall_mem_cons h1, h2⟩
end
| (ee.neg::es) ((eq::eqs), les) h1 :=
begin
simp only [eq_elim], apply sat_eq_elim,
cases h1 with v h1,
existsi v,
cases h1 with hl hr,
apply and.intro _ hr,
rw list.forall_mem_cons at *,
apply and.intro _ hl.right,
rw term.val_neg,
rw ← hl.left,
refl
end
| (ee.nondiv i::es) ((b,as)::eqs, les) h1 :=
begin
unfold eq_elim,
by_cases h2 : (¬i ∣ b ∧ ∀ (x : ℤ), x ∈ as → i ∣ x),
{ exfalso, cases h1 with v h1,
have h3 : 0 = b + coeffs.val v as := h1.left _ (or.inl rfl),
have h4 : i ∣ coeffs.val v as := coeffs.dvd_val h2.right,
have h5 : i ∣ b + coeffs.val v as := by { rw ← h3, apply dvd_zero },
rw ← dvd_add_iff_left h4 at h5, apply h2.left h5 },
rw if_neg h2, apply sat_empty
end
| (ee.factor i::es) ((b,as)::eqs, les) h1 :=
begin
simp only [eq_elim],
by_cases h2 : (i ∣ b) ∧ (∀ x ∈ as, i ∣ x),
{ rw if_pos h2, apply sat_eq_elim, cases h1 with v h1,
existsi v, cases h1 with h3 h4, apply and.intro _ h4,
rw list.forall_mem_cons at *, cases h3 with h5 h6,
apply and.intro _ h6,
rw [term.val_div h2.left h2.right, ← h5, int.zero_div] },
{ rw if_neg h2, apply sat_empty }
end
| (ee.reduce n::es) ((b,as)::eqs, les) h1 :=
begin
simp only [eq_elim],
by_cases h2 : 0 < get n as,
tactic.rotate 1,
{ rw if_neg h2, apply sat_empty },
rw if_pos h2,
apply sat_eq_elim,
cases h1 with v h1,
existsi v ⟨n ↦ sgm v b as n⟩,
cases h1 with h1 h3,
rw list.forall_mem_cons at h1,
cases h1 with h4 h5,
constructor,
{ rw list.forall_mem_cons,
constructor,
{ apply coeffs_reduce_correct h2 h4 },
{ intros x h6, rw list.mem_map at h6,
cases h6 with t h6, cases h6 with h6 h7,
rw [← h7, ← subst_correct h2 h4], apply h5 _ h6 } },
{ intros x h6, rw list.mem_map at h6,
cases h6 with t h6, cases h6 with h6 h7,
rw [← h7, ← subst_correct h2 h4], apply h3 _ h6 }
end
| (ee.cancel m::es) ((eq::eqs), les) h1 :=
begin
unfold eq_elim,
apply sat_eq_elim,
cases h1 with v h1,
existsi v,
cases h1 with h1 h2,
rw list.forall_mem_cons at h1, cases h1 with h1 h3,
constructor; intros t h4; rw list.mem_map at h4;
rcases h4 with ⟨s,h4,h5⟩; rw ← h5;
simp only [term.val_add, term.val_mul, cancel];
rw [← h1, mul_zero, zero_add],
{ apply h3 _ h4 },
{ apply h2 _ h4 }
end
lemma unsat_of_unsat_eq_elim (ee : list ee) (c : clause) :
(eq_elim ee c).unsat → c.unsat :=
by {intros h1 h2, apply h1, apply sat_eq_elim h2}
end omega
|
d191ff2d651dc27c1fe0731d2eeb9a4e6d6a4b61 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/model_theory/semantics.lean | cab8779e403c5b8103b8ca8859392c3af9edcac3 | [
"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 | 35,126 | lean | /-
Copyright (c) 2021 Aaron Anderson, Jesse Michael Han, Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson, Jesse Michael Han, Floris van Doorn
-/
import data.finset.basic
import model_theory.syntax
/-!
# Basics on First-Order Semantics
This file defines the interpretations of first-order terms, formulas, sentences, and theories
in a style inspired by the [Flypitch project](https://flypitch.github.io/).
## Main Definitions
* `first_order.language.term.realize` is defined so that `t.realize v` is the term `t` evaluated at
variables `v`.
* `first_order.language.bounded_formula.realize` is defined so that `φ.realize v xs` is the bounded
formula `φ` evaluated at tuples of variables `v` and `xs`.
* `first_order.language.formula.realize` is defined so that `φ.realize v` is the formula `φ`
evaluated at variables `v`.
* `first_order.language.sentence.realize` is defined so that `φ.realize M` is the sentence `φ`
evaluated in the structure `M`. Also denoted `M ⊨ φ`.
* `first_order.language.Theory.model` is defined so that `T.model M` is true if and only if every
sentence of `T` is realized in `M`. Also denoted `T ⊨ φ`.
## Main Results
* `first_order.language.bounded_formula.realize_to_prenex` shows that the prenex normal form of a
formula has the same realization as the original formula.
* Several results in this file show that syntactic constructions such as `relabel`, `cast_le`,
`lift_at`, `subst`, and the actions of language maps commute with realization of terms, formulas,
sentences, and theories.
## Implementation Notes
* Formulas use a modified version of de Bruijn variables. Specifically, a `L.bounded_formula α n`
is a formula with some variables indexed by a type `α`, which cannot be quantified over, and some
indexed by `fin n`, which can. For any `φ : L.bounded_formula α (n + 1)`, we define the formula
`∀' φ : L.bounded_formula α n` by universally quantifying over the variable indexed by
`n : fin (n + 1)`.
## References
For the Flypitch project:
- [J. Han, F. van Doorn, *A formal proof of the independence of the continuum hypothesis*]
[flypitch_cpp]
- [J. Han, F. van Doorn, *A formalization of forcing and the unprovability of
the continuum hypothesis*][flypitch_itp]
-/
universes u v w u' v'
namespace first_order
namespace language
variables {L : language.{u v}} {L' : language}
variables {M : Type w} {N P : Type*} [L.Structure M] [L.Structure N] [L.Structure P]
variables {α : Type u'} {β : Type v'}
open_locale first_order cardinal
open Structure cardinal fin
namespace term
/-- A term `t` with variables indexed by `α` can be evaluated by giving a value to each variable. -/
@[simp] def realize (v : α → M) :
∀ (t : L.term α), M
| (var k) := v k
| (func f ts) := fun_map f (λ i, (ts i).realize)
@[simp] lemma realize_relabel {t : L.term α} {g : α → β} {v : β → M} :
(t.relabel g).realize v = t.realize (v ∘ g) :=
begin
induction t with _ n f ts ih,
{ refl, },
{ simp [ih] }
end
@[simp] lemma realize_lift_at {n n' m : ℕ} {t : L.term (α ⊕ fin n)}
{v : α ⊕ fin (n + n') → M} :
(t.lift_at n' m).realize v = t.realize (v ∘
(sum.map id (λ i, if ↑i < m then fin.cast_add n' i else fin.add_nat n' i))) :=
realize_relabel
@[simp] lemma realize_constants {c : L.constants} {v : α → M} :
c.term.realize v = c :=
fun_map_eq_coe_constants
@[simp] lemma realize_functions_apply₁ {f : L.functions 1} {t : L.term α} {v : α → M} :
(f.apply₁ t).realize v = fun_map f ![t.realize v] :=
begin
rw [functions.apply₁, term.realize],
refine congr rfl (funext (λ i, _)),
simp only [matrix.cons_val_fin_one],
end
@[simp] lemma realize_functions_apply₂ {f : L.functions 2} {t₁ t₂ : L.term α} {v : α → M} :
(f.apply₂ t₁ t₂).realize v = fun_map f ![t₁.realize v, t₂.realize v] :=
begin
rw [functions.apply₂, term.realize],
refine congr rfl (funext (fin.cases _ _)),
{ simp only [matrix.cons_val_zero], },
{ simp only [matrix.cons_val_succ, matrix.cons_val_fin_one, forall_const] }
end
lemma realize_con {A : set M} {a : A} {v : α → M} :
(L.con a).term.realize v = a := rfl
@[simp] lemma realize_subst {t : L.term α} {tf : α → L.term β} {v : β → M} :
(t.subst tf).realize v = t.realize (λ a, (tf a).realize v) :=
begin
induction t with _ _ _ _ ih,
{ refl },
{ simp [ih] }
end
@[simp] lemma realize_restrict_var [decidable_eq α] {t : L.term α} {s : set α}
(h : ↑t.var_finset ⊆ s) {v : α → M} :
(t.restrict_var (set.inclusion h)).realize (v ∘ coe) = t.realize v :=
begin
induction t with _ _ _ _ ih,
{ refl },
{ simp_rw [var_finset, finset.coe_bUnion, set.Union_subset_iff] at h,
exact congr rfl (funext (λ i, ih i (h i (finset.mem_univ i)))) },
end
@[simp] lemma realize_restrict_var_left [decidable_eq α] {γ : Type*}
{t : L.term (α ⊕ γ)} {s : set α}
(h : ↑t.var_finset_left ⊆ s) {v : α → M} {xs : γ → M} :
(t.restrict_var_left (set.inclusion h)).realize (sum.elim (v ∘ coe) xs) =
t.realize (sum.elim v xs) :=
begin
induction t with a _ _ _ ih,
{ cases a;
refl },
{ simp_rw [var_finset_left, finset.coe_bUnion, set.Union_subset_iff] at h,
exact congr rfl (funext (λ i, ih i (h i (finset.mem_univ i)))) },
end
end term
namespace Lhom
@[simp] lemma realize_on_term [L'.Structure M] (φ : L →ᴸ L') [φ.is_expansion_on M]
(t : L.term α) (v : α → M) :
(φ.on_term t).realize v = t.realize v :=
begin
induction t with _ n f ts ih,
{ refl },
{ simp only [term.realize, Lhom.on_term, Lhom.is_expansion_on.map_on_function, ih] }
end
end Lhom
@[simp] lemma hom.realize_term (g : M →[L] N) {t : L.term α} {v : α → M} :
t.realize (g ∘ v) = g (t.realize v) :=
begin
induction t,
{ refl },
{ rw [term.realize, term.realize, g.map_fun],
refine congr rfl _,
ext x,
simp [t_ih x], },
end
@[simp] lemma embedding.realize_term {v : α → M}
(t : L.term α) (g : M ↪[L] N) :
t.realize (g ∘ v) = g (t.realize v) :=
g.to_hom.realize_term
@[simp] lemma equiv.realize_term {v : α → M}
(t : L.term α) (g : M ≃[L] N) :
t.realize (g ∘ v) = g (t.realize v) :=
g.to_hom.realize_term
variables {L} {α} {n : ℕ}
namespace bounded_formula
open term
/-- A bounded formula can be evaluated as true or false by giving values to each free variable. -/
def realize :
∀ {l} (f : L.bounded_formula α l) (v : α → M) (xs : fin l → M), Prop
| _ falsum v xs := false
| _ (bounded_formula.equal t₁ t₂) v xs := t₁.realize (sum.elim v xs) = t₂.realize (sum.elim v xs)
| _ (bounded_formula.rel R ts) v xs := rel_map R (λ i, (ts i).realize (sum.elim v xs))
| _ (bounded_formula.imp f₁ f₂) v xs := realize f₁ v xs → realize f₂ v xs
| _ (bounded_formula.all f) v xs := ∀(x : M), realize f v (snoc xs x)
variables {l : ℕ} {φ ψ : L.bounded_formula α l} {θ : L.bounded_formula α l.succ}
variables {v : α → M} {xs : fin l → M}
@[simp] lemma realize_bot :
(⊥ : L.bounded_formula α l).realize v xs ↔ false :=
iff.rfl
@[simp] lemma realize_not :
φ.not.realize v xs ↔ ¬ φ.realize v xs :=
iff.rfl
@[simp] lemma realize_bd_equal (t₁ t₂ : L.term (α ⊕ fin l)) :
(t₁.bd_equal t₂).realize v xs ↔
(t₁.realize (sum.elim v xs) = t₂.realize (sum.elim v xs)) :=
iff.rfl
@[simp] lemma realize_top :
(⊤ : L.bounded_formula α l).realize v xs ↔ true :=
by simp [has_top.top]
@[simp] lemma realize_inf : (φ ⊓ ψ).realize v xs ↔ (φ.realize v xs ∧ ψ.realize v xs) :=
by simp [has_inf.inf, realize]
@[simp] lemma realize_foldr_inf (l : list (L.bounded_formula α n))
(v : α → M) (xs : fin n → M) :
(l.foldr (⊓) ⊤).realize v xs ↔ ∀ φ ∈ l, bounded_formula.realize φ v xs :=
begin
induction l with φ l ih,
{ simp },
{ simp [ih] }
end
@[simp] lemma realize_imp : (φ.imp ψ).realize v xs ↔ (φ.realize v xs → ψ.realize v xs) :=
by simp only [realize]
@[simp] lemma realize_rel {k : ℕ} {R : L.relations k} {ts : fin k → L.term _} :
(R.bounded_formula ts).realize v xs ↔ rel_map R (λ i, (ts i).realize (sum.elim v xs)) :=
iff.rfl
@[simp] lemma realize_rel₁ {R : L.relations 1} {t : L.term _} :
(R.bounded_formula₁ t).realize v xs ↔ rel_map R ![t.realize (sum.elim v xs)] :=
begin
rw [relations.bounded_formula₁, realize_rel, iff_eq_eq],
refine congr rfl (funext (λ _, _)),
simp only [matrix.cons_val_fin_one],
end
@[simp] lemma realize_rel₂ {R : L.relations 2} {t₁ t₂ : L.term _} :
(R.bounded_formula₂ t₁ t₂).realize v xs ↔
rel_map R ![t₁.realize (sum.elim v xs), t₂.realize (sum.elim v xs)] :=
begin
rw [relations.bounded_formula₂, realize_rel, iff_eq_eq],
refine congr rfl (funext (fin.cases _ _)),
{ simp only [matrix.cons_val_zero]},
{ simp only [matrix.cons_val_succ, matrix.cons_val_fin_one, forall_const] }
end
@[simp] lemma realize_sup : (φ ⊔ ψ).realize v xs ↔ (φ.realize v xs ∨ ψ.realize v xs) :=
begin
simp only [realize, has_sup.sup, realize_not, eq_iff_iff],
tauto,
end
@[simp] lemma realize_foldr_sup (l : list (L.bounded_formula α n))
(v : α → M) (xs : fin n → M) :
(l.foldr (⊔) ⊥).realize v xs ↔ ∃ φ ∈ l, bounded_formula.realize φ v xs :=
begin
induction l with φ l ih,
{ simp },
{ simp_rw [list.foldr_cons, realize_sup, ih, exists_prop, list.mem_cons_iff,
or_and_distrib_right, exists_or_distrib, exists_eq_left] }
end
@[simp] lemma realize_all : (all θ).realize v xs ↔ ∀ (a : M), (θ.realize v (fin.snoc xs a)) :=
iff.rfl
@[simp] lemma realize_ex : θ.ex.realize v xs ↔ ∃ (a : M), (θ.realize v (fin.snoc xs a)) :=
begin
rw [bounded_formula.ex, realize_not, realize_all, not_forall],
simp_rw [realize_not, not_not],
end
@[simp] lemma realize_iff : (φ.iff ψ).realize v xs ↔ (φ.realize v xs ↔ ψ.realize v xs) :=
by simp only [bounded_formula.iff, realize_inf, realize_imp, and_imp, ← iff_def]
lemma realize_cast_le_of_eq {m n : ℕ} (h : m = n) {h' : m ≤ n} {φ : L.bounded_formula α m}
{v : α → M} {xs : fin n → M} :
(φ.cast_le h').realize v xs ↔ φ.realize v (xs ∘ fin.cast h) :=
begin
subst h,
simp only [cast_le_rfl, cast_refl, order_iso.coe_refl, function.comp.right_id],
end
lemma realize_map_term_rel_id [L'.Structure M]
{ft : ∀ n, L.term (α ⊕ fin n) → L'.term (β ⊕ fin n)}
{fr : ∀ n, L.relations n → L'.relations n}
{n} {φ : L.bounded_formula α n} {v : α → M} {v' : β → M} {xs : fin n → M}
(h1 : ∀ n (t : L.term (α ⊕ fin n)) (xs : fin n → M),
(ft n t).realize (sum.elim v' xs) = t.realize (sum.elim v xs))
(h2 : ∀ n (R : L.relations n) (x : fin n → M), rel_map (fr n R) x = rel_map R x) :
(φ.map_term_rel ft fr (λ _, id)).realize v' xs ↔ φ.realize v xs :=
begin
induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih,
{ refl },
{ simp [map_term_rel, realize, h1] },
{ simp [map_term_rel, realize, h1, h2] },
{ simp [map_term_rel, realize, ih1, ih2], },
{ simp only [map_term_rel, realize, ih, id.def] },
end
lemma realize_map_term_rel_add_cast_le [L'.Structure M]
{k : ℕ}
{ft : ∀ n, L.term (α ⊕ fin n) → L'.term (β ⊕ fin (k + n))}
{fr : ∀ n, L.relations n → L'.relations n}
{n} {φ : L.bounded_formula α n} (v : ∀ {n}, (fin (k + n) → M) → α → M) {v' : β → M}
(xs : fin (k + n) → M)
(h1 : ∀ n (t : L.term (α ⊕ fin n)) (xs' : fin (k + n) → M),
(ft n t).realize (sum.elim v' xs') =
t.realize (sum.elim (v xs') (xs' ∘ fin.nat_add _)))
(h2 : ∀ n (R : L.relations n) (x : fin n → M), rel_map (fr n R) x = rel_map R x)
(hv : ∀ n (xs : fin (k + n) → M) (x : M), @v (n+1) (snoc xs x : fin _ → M) = v xs):
(φ.map_term_rel ft fr (λ n, cast_le (add_assoc _ _ _).symm.le)).realize v' xs ↔
φ.realize (v xs) (xs ∘ fin.nat_add _) :=
begin
induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih,
{ refl },
{ simp [map_term_rel, realize, h1] },
{ simp [map_term_rel, realize, h1, h2] },
{ simp [map_term_rel, realize, ih1, ih2], },
{ simp [map_term_rel, realize, ih, hv] },
end
lemma realize_relabel {m n : ℕ}
{φ : L.bounded_formula α n} {g : α → β ⊕ fin m} {v : β → M} {xs : fin (m + n) → M} :
(φ.relabel g).realize v xs ↔
φ.realize (sum.elim v (xs ∘ fin.cast_add n) ∘ g) (xs ∘ fin.nat_add m) :=
by rw [relabel, realize_map_term_rel_add_cast_le]; intros; simp
lemma realize_lift_at {n n' m : ℕ} {φ : L.bounded_formula α n}
{v : α → M} {xs : fin (n + n') → M} (hmn : m + n' ≤ n + 1) :
(φ.lift_at n' m).realize v xs ↔ φ.realize v (xs ∘
(λ i, if ↑i < m then fin.cast_add n' i else fin.add_nat n' i)) :=
begin
rw lift_at,
induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 k _ ih3,
{ simp [realize, map_term_rel], },
{ simp [realize, map_term_rel, realize_rel, realize_lift_at, sum.elim_comp_map], },
{ simp [realize, map_term_rel, realize_rel, realize_lift_at, sum.elim_comp_map], },
{ simp only [map_term_rel, realize, ih1 hmn, ih2 hmn] },
{ have h : k + 1 + n' = k + n'+ 1,
{ rw [add_assoc, add_comm 1 n', ← add_assoc], },
simp only [map_term_rel, realize, realize_cast_le_of_eq h, ih3 (hmn.trans k.succ.le_succ)],
refine forall_congr (λ x, iff_eq_eq.mpr (congr rfl (funext (fin.last_cases _ (λ i, _))))),
{ simp only [function.comp_app, coe_last, snoc_last],
by_cases (k < m),
{ rw if_pos h,
refine (congr rfl (ext _)).trans (snoc_last _ _),
simp only [coe_cast, coe_cast_add, coe_last, self_eq_add_right],
refine le_antisymm (le_of_add_le_add_left ((hmn.trans (nat.succ_le_of_lt h)).trans _))
n'.zero_le,
rw add_zero },
{ rw if_neg h,
refine (congr rfl (ext _)).trans (snoc_last _ _),
simp } },
{ simp only [function.comp_app, fin.snoc_cast_succ],
refine (congr rfl (ext _)).trans (snoc_cast_succ _ _ _),
simp only [cast_refl, coe_cast_succ, order_iso.coe_refl, id.def],
split_ifs;
simp } }
end
lemma realize_lift_at_one {n m : ℕ} {φ : L.bounded_formula α n}
{v : α → M} {xs : fin (n + 1) → M} (hmn : m ≤ n) :
(φ.lift_at 1 m).realize v xs ↔ φ.realize v (xs ∘
(λ i, if ↑i < m then cast_succ i else i.succ)) :=
by simp_rw [realize_lift_at (add_le_add_right hmn 1), cast_succ, add_nat_one]
@[simp] lemma realize_lift_at_one_self {n : ℕ} {φ : L.bounded_formula α n}
{v : α → M} {xs : fin (n + 1) → M} :
(φ.lift_at 1 n).realize v xs ↔ φ.realize v (xs ∘ cast_succ) :=
begin
rw [realize_lift_at_one (refl n), iff_eq_eq],
refine congr rfl (congr rfl (funext (λ i, _))),
rw [if_pos i.is_lt],
end
lemma realize_subst {φ : L.bounded_formula α n} {tf : α → L.term β} {v : β → M} {xs : fin n → M} :
(φ.subst tf).realize v xs ↔ φ.realize (λ a, (tf a).realize v) xs :=
realize_map_term_rel_id (λ n t x, begin
rw term.realize_subst,
rcongr a,
{ cases a,
{ simp only [sum.elim_inl, term.realize_relabel, sum.elim_comp_inl] },
{ refl } }
end) (by simp)
@[simp] lemma realize_restrict_free_var [decidable_eq α] {n : ℕ} {φ : L.bounded_formula α n}
{s : set α} (h : ↑φ.free_var_finset ⊆ s) {v : α → M} {xs : fin n → M} :
(φ.restrict_free_var (set.inclusion h)).realize (v ∘ coe) xs ↔
φ.realize v xs :=
begin
induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3,
{ refl },
{ simp [restrict_free_var, realize] },
{ simp [restrict_free_var, realize] },
{ simp [restrict_free_var, realize, ih1, ih2] },
{ simp [restrict_free_var, realize, ih3] },
end
variables [nonempty M]
lemma realize_all_lift_at_one_self {n : ℕ} {φ : L.bounded_formula α n}
{v : α → M} {xs : fin n → M} :
(φ.lift_at 1 n).all.realize v xs ↔ φ.realize v xs :=
begin
inhabit M,
simp only [realize_all, realize_lift_at_one_self],
refine ⟨λ h, _, λ h a, _⟩,
{ refine (congr rfl (funext (λ i, _))).mp (h default),
simp, },
{ refine (congr rfl (funext (λ i, _))).mp h,
simp }
end
lemma realize_to_prenex_imp_right {φ ψ : L.bounded_formula α n}
(hφ : is_qf φ) (hψ : is_prenex ψ) {v : α → M} {xs : fin n → M} :
(φ.to_prenex_imp_right ψ).realize v xs ↔ (φ.imp ψ).realize v xs :=
begin
revert φ,
induction hψ with _ _ hψ _ _ hψ ih _ _ hψ ih; intros φ hφ,
{ rw hψ.to_prenex_imp_right },
{ refine trans (forall_congr (λ _, ih hφ.lift_at)) _,
simp only [realize_imp, realize_lift_at_one_self, snoc_comp_cast_succ, realize_all],
exact ⟨λ h1 a h2, h1 h2 a, λ h1 h2 a, h1 a h2⟩, },
{ rw [to_prenex_imp_right, realize_ex],
refine trans (exists_congr (λ _, ih hφ.lift_at)) _,
simp only [realize_imp, realize_lift_at_one_self, snoc_comp_cast_succ, realize_ex],
refine ⟨_, λ h', _⟩,
{ rintro ⟨a, ha⟩ h,
exact ⟨a, ha h⟩ },
{ by_cases φ.realize v xs,
{ obtain ⟨a, ha⟩ := h' h,
exact ⟨a, λ _, ha⟩ },
{ inhabit M,
exact ⟨default, λ h'', (h h'').elim⟩ } } }
end
lemma realize_to_prenex_imp {φ ψ : L.bounded_formula α n}
(hφ : is_prenex φ) (hψ : is_prenex ψ) {v : α → M} {xs : fin n → M} :
(φ.to_prenex_imp ψ).realize v xs ↔ (φ.imp ψ).realize v xs :=
begin
revert ψ,
induction hφ with _ _ hφ _ _ hφ ih _ _ hφ ih; intros ψ hψ,
{ rw [hφ.to_prenex_imp],
exact realize_to_prenex_imp_right hφ hψ, },
{ rw [to_prenex_imp, realize_ex],
refine trans (exists_congr (λ _, ih hψ.lift_at)) _,
simp only [realize_imp, realize_lift_at_one_self, snoc_comp_cast_succ, realize_all],
refine ⟨_, λ h', _⟩,
{ rintro ⟨a, ha⟩ h,
exact ha (h a) },
{ by_cases ψ.realize v xs,
{ inhabit M,
exact ⟨default, λ h'', h⟩ },
{ obtain ⟨a, ha⟩ := not_forall.1 (h ∘ h'),
exact ⟨a, λ h, (ha h).elim⟩ } } },
{ refine trans (forall_congr (λ _, ih hψ.lift_at)) _,
simp, },
end
@[simp] lemma realize_to_prenex (φ : L.bounded_formula α n) {v : α → M} :
∀ {xs : fin n → M}, φ.to_prenex.realize v xs ↔ φ.realize v xs :=
begin
refine bounded_formula.rec_on φ
(λ _ _, iff.rfl)
(λ _ _ _ _, iff.rfl)
(λ _ _ _ _ _, iff.rfl)
(λ _ f1 f2 h1 h2 _, _)
(λ _ f h xs, _),
{ rw [to_prenex, realize_to_prenex_imp f1.to_prenex_is_prenex f2.to_prenex_is_prenex,
realize_imp, realize_imp, h1, h2],
apply_instance },
{ rw [realize_all, to_prenex, realize_all],
exact forall_congr (λ a, h) },
end
end bounded_formula
attribute [protected] bounded_formula.falsum bounded_formula.equal bounded_formula.rel
attribute [protected] bounded_formula.imp bounded_formula.all
namespace Lhom
open bounded_formula
@[simp] lemma realize_on_bounded_formula [L'.Structure M] (φ : L →ᴸ L') [φ.is_expansion_on M]
{n : ℕ} (ψ : L.bounded_formula α n) {v : α → M} {xs : fin n → M} :
(φ.on_bounded_formula ψ).realize v xs ↔ ψ.realize v xs :=
begin
induction ψ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3,
{ refl },
{ simp only [on_bounded_formula, realize_bd_equal, realize_on_term],
refl, },
{ simp only [on_bounded_formula, realize_rel, realize_on_term, is_expansion_on.map_on_relation],
refl, },
{ simp only [on_bounded_formula, ih1, ih2, realize_imp], },
{ simp only [on_bounded_formula, ih3, realize_all], },
end
end Lhom
attribute [protected] bounded_formula.falsum bounded_formula.equal bounded_formula.rel
attribute [protected] bounded_formula.imp bounded_formula.all
namespace formula
/-- A formula can be evaluated as true or false by giving values to each free variable. -/
def realize (φ : L.formula α) (v : α → M) : Prop :=
φ.realize v default
variables {M} {φ ψ : L.formula α} {v : α → M}
@[simp] lemma realize_not :
(φ.not).realize v ↔ ¬ φ.realize v :=
iff.rfl
@[simp] lemma realize_bot :
(⊥ : L.formula α).realize v ↔ false :=
iff.rfl
@[simp] lemma realize_top :
(⊤ : L.formula α).realize v ↔ true :=
bounded_formula.realize_top
@[simp] lemma realize_inf : (φ ⊓ ψ).realize v ↔ (φ.realize v ∧ ψ.realize v) :=
bounded_formula.realize_inf
@[simp] lemma realize_imp : (φ.imp ψ).realize v ↔ (φ.realize v → ψ.realize v) :=
bounded_formula.realize_imp
@[simp] lemma realize_rel {k : ℕ} {R : L.relations k} {ts : fin k → L.term α} :
(R.formula ts).realize v ↔ rel_map R (λ i, (ts i).realize v) :=
bounded_formula.realize_rel.trans (by simp)
@[simp] lemma realize_rel₁ {R : L.relations 1} {t : L.term _} :
(R.formula₁ t).realize v ↔ rel_map R ![t.realize v] :=
begin
rw [relations.formula₁, realize_rel, iff_eq_eq],
refine congr rfl (funext (λ _, _)),
simp only [matrix.cons_val_fin_one],
end
@[simp] lemma realize_rel₂ {R : L.relations 2} {t₁ t₂ : L.term _} :
(R.formula₂ t₁ t₂).realize v ↔
rel_map R ![t₁.realize v, t₂.realize v] :=
begin
rw [relations.formula₂, realize_rel, iff_eq_eq],
refine congr rfl (funext (fin.cases _ _)),
{ simp only [matrix.cons_val_zero]},
{ simp only [matrix.cons_val_succ, matrix.cons_val_fin_one, forall_const] }
end
@[simp] lemma realize_sup : (φ ⊔ ψ).realize v ↔ (φ.realize v ∨ ψ.realize v) :=
bounded_formula.realize_sup
@[simp] lemma realize_iff : (φ.iff ψ).realize v ↔ (φ.realize v ↔ ψ.realize v) :=
bounded_formula.realize_iff
@[simp] lemma realize_relabel {φ : L.formula α} {g : α → β} {v : β → M} :
(φ.relabel g).realize v ↔ φ.realize (v ∘ g) :=
begin
rw [realize, realize, relabel, bounded_formula.realize_relabel,
iff_eq_eq, fin.cast_add_zero],
exact congr rfl (funext fin_zero_elim),
end
lemma realize_relabel_sum_inr (φ : L.formula (fin n)) {v : empty → M} {x : fin n → M} :
(bounded_formula.relabel sum.inr φ).realize v x ↔ φ.realize x :=
by rw [bounded_formula.realize_relabel, formula.realize, sum.elim_comp_inr, fin.cast_add_zero,
cast_refl, order_iso.coe_refl, function.comp.right_id,
subsingleton.elim (x ∘ (nat_add n : fin 0 → fin n)) default]
@[simp]
lemma realize_equal {t₁ t₂ : L.term α} {x : α → M} :
(t₁.equal t₂).realize x ↔ t₁.realize x = t₂.realize x :=
by simp [term.equal, realize]
@[simp]
lemma realize_graph {f : L.functions n} {x : fin n → M} {y : M} :
(formula.graph f).realize (fin.cons y x : _ → M) ↔ fun_map f x = y :=
begin
simp only [formula.graph, term.realize, realize_equal, fin.cons_zero, fin.cons_succ],
rw eq_comm,
end
end formula
@[simp] lemma Lhom.realize_on_formula [L'.Structure M] (φ : L →ᴸ L') [φ.is_expansion_on M]
(ψ : L.formula α) {v : α → M} :
(φ.on_formula ψ).realize v ↔ ψ.realize v :=
φ.realize_on_bounded_formula ψ
@[simp] lemma Lhom.set_of_realize_on_formula [L'.Structure M] (φ : L →ᴸ L') [φ.is_expansion_on M]
(ψ : L.formula α) :
(set_of (φ.on_formula ψ).realize : set (α → M)) = set_of ψ.realize :=
by { ext, simp }
variable (M)
/-- A sentence can be evaluated as true or false in a structure. -/
def sentence.realize (φ : L.sentence) : Prop :=
φ.realize (default : _ → M)
infix ` ⊨ `:51 := sentence.realize -- input using \|= or \vDash, but not using \models
@[simp] lemma sentence.realize_not {φ : L.sentence} :
M ⊨ φ.not ↔ ¬ M ⊨ φ :=
iff.rfl
@[simp] lemma Lhom.realize_on_sentence [L'.Structure M] (φ : L →ᴸ L') [φ.is_expansion_on M]
(ψ : L.sentence) :
M ⊨ φ.on_sentence ψ ↔ M ⊨ ψ :=
φ.realize_on_formula ψ
variables (L)
/-- The complete theory of a structure `M` is the set of all sentences `M` satisfies. -/
def complete_theory : L.Theory := { φ | M ⊨ φ }
variable (N)
/-- Two structures are elementarily equivalent when they satisfy the same sentences. -/
def elementarily_equivalent : Prop := L.complete_theory M = L.complete_theory N
localized "notation A ` ≅[`:25 L `] ` B:50 := first_order.language.elementarily_equivalent L A B"
in first_order
variables {L} {M} {N}
@[simp] lemma mem_complete_theory {φ : sentence L} : φ ∈ L.complete_theory M ↔ M ⊨ φ := iff.rfl
lemma elementarily_equivalent_iff : M ≅[L] N ↔ ∀ φ : L.sentence, M ⊨ φ ↔ N ⊨ φ :=
by simp only [elementarily_equivalent, set.ext_iff, complete_theory, set.mem_set_of_eq]
variables (M)
/-- A model of a theory is a structure in which every sentence is realized as true. -/
class Theory.model (T : L.Theory) : Prop :=
(realize_of_mem : ∀ φ ∈ T, M ⊨ φ)
infix ` ⊨ `:51 := Theory.model -- input using \|= or \vDash, but not using \models
variables {M} (T : L.Theory)
@[simp] lemma Theory.model_iff : M ⊨ T ↔ ∀ φ ∈ T, M ⊨ φ := ⟨λ h, h.realize_of_mem, λ h, ⟨h⟩⟩
lemma Theory.realize_sentence_of_mem [M ⊨ T] {φ : L.sentence} (h : φ ∈ T) :
M ⊨ φ :=
Theory.model.realize_of_mem φ h
@[simp] lemma Lhom.on_Theory_model [L'.Structure M] (φ : L →ᴸ L') [φ.is_expansion_on M]
(T : L.Theory) :
M ⊨ φ.on_Theory T ↔ M ⊨ T :=
by simp [Theory.model_iff, Lhom.on_Theory]
variables {M} {T}
instance model_empty : M ⊨ (∅ : L.Theory) := ⟨λ φ hφ, (set.not_mem_empty φ hφ).elim⟩
namespace Theory
lemma model.mono {T' : L.Theory} (h : M ⊨ T') (hs : T ⊆ T') :
M ⊨ T :=
⟨λ φ hφ, T'.realize_sentence_of_mem (hs hφ)⟩
lemma model.union {T' : L.Theory} (h : M ⊨ T) (h' : M ⊨ T') :
M ⊨ T ∪ T' :=
begin
simp only [model_iff, set.mem_union_eq] at *,
exact λ φ hφ, hφ.elim (h _) (h' _),
end
@[simp] lemma model_union_iff {T' : L.Theory} :
M ⊨ T ∪ T' ↔ M ⊨ T ∧ M ⊨ T' :=
⟨λ h, ⟨h.mono (T.subset_union_left T'), h.mono (T.subset_union_right T')⟩, λ h, h.1.union h.2⟩
lemma model_singleton_iff {φ : L.sentence} :
M ⊨ ({φ} : L.Theory) ↔ M ⊨ φ :=
by simp
theorem model_iff_subset_complete_theory :
M ⊨ T ↔ T ⊆ L.complete_theory M :=
T.model_iff
end Theory
instance model_complete_theory : M ⊨ L.complete_theory M :=
Theory.model_iff_subset_complete_theory.2 (subset_refl _)
variables (M N)
theorem realize_iff_of_model_complete_theory [N ⊨ L.complete_theory M] (φ : L.sentence) :
N ⊨ φ ↔ M ⊨ φ :=
begin
refine ⟨λ h, _, (L.complete_theory M).realize_sentence_of_mem⟩,
contrapose! h,
rw [← sentence.realize_not] at *,
exact (L.complete_theory M).realize_sentence_of_mem (mem_complete_theory.2 h)
end
variables {M N}
namespace bounded_formula
@[simp] lemma realize_alls {φ : L.bounded_formula α n} {v : α → M} :
φ.alls.realize v ↔
∀ (xs : fin n → M), (φ.realize v xs) :=
begin
induction n with n ih,
{ exact unique.forall_iff.symm },
{ simp only [alls, ih, realize],
exact ⟨λ h xs, (fin.snoc_init_self xs) ▸ h _ _, λ h xs x, h (fin.snoc xs x)⟩ }
end
@[simp] lemma realize_exs {φ : L.bounded_formula α n} {v : α → M} :
φ.exs.realize v ↔ ∃ (xs : fin n → M), (φ.realize v xs) :=
begin
induction n with n ih,
{ exact unique.exists_iff.symm },
{ simp only [bounded_formula.exs, ih, realize_ex],
split,
{ rintros ⟨xs, x, h⟩,
exact ⟨_, h⟩ },
{ rintros ⟨xs, h⟩,
rw ← fin.snoc_init_self xs at h,
exact ⟨_, _, h⟩ } }
end
@[simp] lemma realize_to_formula (φ : L.bounded_formula α n) (v : α ⊕ fin n → M) :
φ.to_formula.realize v ↔ φ.realize (v ∘ sum.inl) (v ∘ sum.inr) :=
begin
induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3 a8 a9 a0,
{ refl },
{ simp [bounded_formula.realize] },
{ simp [bounded_formula.realize] },
{ rw [to_formula, formula.realize, realize_imp, ← formula.realize, ih1, ← formula.realize, ih2,
realize_imp], },
{ rw [to_formula, formula.realize, realize_all, realize_all],
refine forall_congr (λ a, _),
have h := ih3 (sum.elim (v ∘ sum.inl) (snoc (v ∘ sum.inr) a)),
simp only [sum.elim_comp_inl, sum.elim_comp_inr] at h,
rw [← h, realize_relabel, formula.realize],
rcongr,
{ cases x,
{ simp },
{ refine fin.last_cases _ (λ i, _) x,
{ rw [sum.elim_inr, snoc_last, function.comp_app, sum.elim_inr, function.comp_app,
fin_sum_fin_equiv_symm_last, sum.map_inr, sum.elim_inr, function.comp_app],
exact (congr rfl (subsingleton.elim _ _)).trans (snoc_last _ _) },
{ simp only [cast_succ, function.comp_app, sum.elim_inr,
fin_sum_fin_equiv_symm_apply_cast_add, sum.map_inl, sum.elim_inl],
rw [← cast_succ, snoc_cast_succ] } } },
{ exact subsingleton.elim _ _ } }
end
end bounded_formula
namespace equiv
@[simp] lemma realize_bounded_formula (g : M ≃[L] N) (φ : L.bounded_formula α n)
{v : α → M} {xs : fin n → M} :
φ.realize (g ∘ v) (g ∘ xs) ↔ φ.realize v xs :=
begin
induction φ with _ _ _ _ _ _ _ _ _ _ _ ih1 ih2 _ _ ih3,
{ refl },
{ simp only [bounded_formula.realize, ← sum.comp_elim, equiv.realize_term, g.injective.eq_iff] },
{ simp only [bounded_formula.realize, ← sum.comp_elim, equiv.realize_term, g.map_rel], },
{ rw [bounded_formula.realize, ih1, ih2, bounded_formula.realize] },
{ rw [bounded_formula.realize, bounded_formula.realize],
split,
{ intros h a,
have h' := h (g a),
rw [← fin.comp_snoc, ih3] at h',
exact h' },
{ intros h a,
have h' := h (g.symm a),
rw [← ih3, fin.comp_snoc, g.apply_symm_apply] at h',
exact h' }}
end
@[simp] lemma realize_formula (g : M ≃[L] N) (φ : L.formula α) {v : α → M} :
φ.realize (g ∘ v) ↔ φ.realize v :=
by rw [formula.realize, formula.realize, ← g.realize_bounded_formula φ,
iff_eq_eq, unique.eq_default (g ∘ default)]
lemma realize_sentence (g : M ≃[L] N) (φ : L.sentence) :
M ⊨ φ ↔ N ⊨ φ :=
by rw [sentence.realize, sentence.realize, ← g.realize_formula, unique.eq_default (g ∘ default)]
lemma Theory_model (g : M ≃[L] N) [M ⊨ T] : N ⊨ T :=
⟨λ φ hφ, (g.realize_sentence φ).1 (Theory.realize_sentence_of_mem T hφ)⟩
lemma elementarily_equivalent (g : M ≃[L] N) : M ≅[L] N :=
elementarily_equivalent_iff.2 g.realize_sentence
end equiv
namespace relations
open bounded_formula
variable {r : L.relations 2}
@[simp]
lemma realize_reflexive :
M ⊨ r.reflexive ↔ reflexive (λ (x y : M), rel_map r ![x,y]) :=
forall_congr (λ _, realize_rel₂)
@[simp]
lemma realize_irreflexive :
M ⊨ r.irreflexive ↔ irreflexive (λ (x y : M), rel_map r ![x,y]) :=
forall_congr (λ _, not_congr realize_rel₂)
@[simp]
lemma realize_symmetric :
M ⊨ r.symmetric ↔ symmetric (λ (x y : M), rel_map r ![x,y]) :=
forall_congr (λ _, forall_congr (λ _, imp_congr realize_rel₂ realize_rel₂))
@[simp]
lemma realize_antisymmetric :
M ⊨ r.antisymmetric ↔ anti_symmetric (λ (x y : M), rel_map r ![x,y]) :=
forall_congr (λ _, forall_congr (λ _, imp_congr realize_rel₂ (imp_congr realize_rel₂ iff.rfl)))
@[simp]
lemma realize_transitive :
M ⊨ r.transitive ↔ transitive (λ (x y : M), rel_map r ![x,y]) :=
forall_congr (λ _, forall_congr (λ _, forall_congr
(λ _, imp_congr realize_rel₂ (imp_congr realize_rel₂ realize_rel₂))))
@[simp]
lemma realize_total :
M ⊨ r.total ↔ total (λ (x y : M), rel_map r ![x,y]) :=
forall_congr (λ _, forall_congr (λ _, realize_sup.trans (or_congr realize_rel₂ realize_rel₂)))
end relations
section cardinality
variable (L)
@[simp] lemma sentence.realize_card_ge (n) : M ⊨ (sentence.card_ge L n) ↔ ↑n ≤ (# M) :=
begin
rw [← lift_mk_fin, ← lift_le, lift_lift, lift_mk_le, sentence.card_ge, sentence.realize,
bounded_formula.realize_exs],
simp_rw [bounded_formula.realize_foldr_inf],
simp only [function.comp_app, list.mem_map, prod.exists, ne.def, list.mem_product,
list.mem_fin_range, forall_exists_index, and_imp, list.mem_filter, true_and],
refine ⟨_, λ xs, ⟨xs.some, _⟩⟩,
{ rintro ⟨xs, h⟩,
refine ⟨⟨xs, λ i j ij, _⟩⟩,
contrapose! ij,
have hij := h _ i j ij rfl,
simp only [bounded_formula.realize_not, term.realize, bounded_formula.realize_bd_equal,
sum.elim_inr] at hij,
exact hij },
{ rintro _ i j ij rfl,
simp [ij] }
end
@[simp] lemma model_infinite_theory_iff : M ⊨ L.infinite_theory ↔ infinite M :=
by simp [infinite_theory, infinite_iff, aleph_0_le]
instance model_infinite_theory [h : infinite M] :
M ⊨ L.infinite_theory :=
L.model_infinite_theory_iff.2 h
@[simp] lemma model_nonempty_theory_iff :
M ⊨ L.nonempty_theory ↔ nonempty M :=
by simp only [nonempty_theory, Theory.model_iff, set.mem_singleton_iff, forall_eq,
sentence.realize_card_ge, nat.cast_one, one_le_iff_ne_zero, mk_ne_zero_iff]
instance model_nonempty [h : nonempty M] :
M ⊨ L.nonempty_theory :=
L.model_nonempty_theory_iff.2 h
lemma model_distinct_constants_theory {M : Type w} [L[[α]].Structure M] (s : set α) :
M ⊨ L.distinct_constants_theory s ↔ set.inj_on (λ (i : α), (L.con i : M)) s :=
begin
simp only [distinct_constants_theory, Theory.model_iff, set.mem_image,
set.mem_inter_eq, set.mem_prod, set.mem_compl_eq, prod.exists, forall_exists_index, and_imp],
refine ⟨λ h a as b bs ab, _, _⟩,
{ contrapose! ab,
have h' := h _ a b as bs ab rfl,
simp only [sentence.realize, formula.realize_not, formula.realize_equal,
term.realize_constants] at h',
exact h', },
{ rintros h φ a b as bs ab rfl,
simp only [sentence.realize, formula.realize_not, formula.realize_equal,
term.realize_constants],
exact λ contra, ab (h as bs contra) }
end
lemma card_le_of_model_distinct_constants_theory (s : set α) (M : Type w) [L[[α]].Structure M]
[h : M ⊨ L.distinct_constants_theory s] :
cardinal.lift.{w} (# s) ≤ cardinal.lift.{u'} (# M) :=
lift_mk_le'.2 ⟨⟨_, set.inj_on_iff_injective.1 ((L.model_distinct_constants_theory s).1 h)⟩⟩
end cardinality
namespace elementarily_equivalent
@[symm] lemma symm (h : M ≅[L] N) : N ≅[L] M := h.symm
@[trans] lemma trans (MN : M ≅[L] N) (NP : N ≅[L] P) : M ≅[L] P := MN.trans NP
lemma complete_theory_eq (h : M ≅[L] N) : L.complete_theory M = L.complete_theory N := h
lemma realize_sentence (h : M ≅[L] N) (φ : L.sentence) : M ⊨ φ ↔ N ⊨ φ :=
(elementarily_equivalent_iff.1 h) φ
lemma Theory_model_iff (h : M ≅[L] N) : M ⊨ T ↔ N ⊨ T :=
by rw [Theory.model_iff_subset_complete_theory, Theory.model_iff_subset_complete_theory,
h.complete_theory_eq]
lemma Theory_model [MT : M ⊨ T] (h : M ≅[L] N) : N ⊨ T :=
h.Theory_model_iff.1 MT
lemma nonempty_iff (h : M ≅[L] N) : nonempty M ↔ nonempty N :=
(model_nonempty_theory_iff L).symm.trans (h.Theory_model_iff.trans (model_nonempty_theory_iff L))
lemma nonempty [Mn : nonempty M] (h : M ≅[L] N) : nonempty N := h.nonempty_iff.1 Mn
lemma infinite_iff (h : M ≅[L] N) : infinite M ↔ infinite N :=
(model_infinite_theory_iff L).symm.trans (h.Theory_model_iff.trans (model_infinite_theory_iff L))
lemma infinite [Mi : infinite M] (h : M ≅[L] N) : infinite N := h.infinite_iff.1 Mi
end elementarily_equivalent
end language
end first_order
|
79845d26cf92a629ee328482a08aeeaea99ff0d6 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /04_Quantifiers_and_Equality.org.32.lean | 0022978c93a2441aa45f348c092b3334ce14de45 | [] | 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 | 399 | lean | /- page 60 -/
import standard
import data.nat
open nat
variable f : ℕ → ℕ
premise H : ∀ x : ℕ, f x ≤ f (x + 1)
-- BEGIN
example : f 0 ≥ f 1 → f 1 ≥ f 2 → f 0 = f 2 :=
assume `f 0 ≥ f 1`,
assume `f 1 ≥ f 2`,
have f 0 ≥ f 2, from le.trans `f 2 ≤ f 1` `f 1 ≤ f 0`,
have f 0 ≤ f 2, from le.trans (H 0) (H 1),
show f 0 = f 2, from le.antisymm this `f 0 ≥ f 2`
-- END
|
0a5680804ae3cad15de4bf3379f6479bae0a749f | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/data/equiv/encodable.lean | d0579500e5a37ff3e411e455031309217a9d29b4 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 10,981 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura, Mario Carneiro
Type class for encodable Types.
Note that every encodable Type is countable.
-/
import data.equiv.nat
open option list nat function
/-- An encodable type is a "constructively countable" type. This is where
we have an explicit injection `encode : α → nat` and a partial inverse
`decode : nat → option α`. This makes the range of `encode` decidable,
although it is not decidable if `α` is finite or not. -/
class encodable (α : Type*) :=
(encode : α → nat) (decode : nat → option α) (encodek : ∀ a, decode (encode a) = some a)
namespace encodable
variables {α : Type*} {β : Type*}
universe u
open encodable
theorem encode_injective [encodable α] : function.injective (@encode α _)
| x y e := option.some.inj $ by rw [← encodek, e, encodek]
/- This is not set as an instance because this is usually not the best way
to infer decidability. -/
def decidable_eq_of_encodable (α) [encodable α] : decidable_eq α
| a b := decidable_of_iff _ encode_injective.eq_iff
def of_left_injection [encodable α]
(f : β → α) (finv : α → option β) (linv : ∀ b, finv (f b) = some b) : encodable β :=
⟨λ b, encode (f b),
λ n, (decode α n).bind finv,
λ b, by simp [encodable.encodek, linv]⟩
def of_left_inverse [encodable α]
(f : β → α) (finv : α → β) (linv : ∀ b, finv (f b) = b) : encodable β :=
of_left_injection f (some ∘ finv) (λ b, congr_arg some (linv b))
def of_equiv (α) [encodable α] (e : β ≃ α) : encodable β :=
of_left_inverse e e.symm e.left_inv
@[simp] theorem encode_of_equiv {α β} [encodable α] (e : β ≃ α) (b : β) :
@encode _ (of_equiv _ e) b = encode (e b) := rfl
@[simp] theorem decode_of_equiv {α β} [encodable α] (e : β ≃ α) (n : ℕ) :
@decode _ (of_equiv _ e) n = (decode α n).map e.symm := rfl
instance nat : encodable nat :=
⟨id, some, λ a, rfl⟩
@[simp] theorem encode_nat (n : ℕ) : encode n = n := rfl
@[simp] theorem decode_nat (n : ℕ) : decode ℕ n = some n := rfl
instance empty : encodable empty :=
⟨λ a, a.rec _, λ n, none, λ a, a.rec _⟩
instance unit : encodable punit :=
⟨λ_, zero, λn, nat.cases_on n (some punit.star) (λ _, none), λ⟨⟩, by simp⟩
@[simp] theorem encode_star : encode punit.star = 0 := rfl
@[simp] theorem decode_unit_zero : decode punit 0 = some punit.star := rfl
@[simp] theorem decode_unit_succ (n) : decode punit (succ n) = none := rfl
instance option {α : Type*} [h : encodable α] : encodable (option α) :=
⟨λ o, option.cases_on o nat.zero (λ a, succ (encode a)),
λ n, nat.cases_on n (some none) (λ m, (decode α m).map some),
λ o, by cases o; dsimp; simp [encodek, nat.succ_ne_zero]⟩
@[simp] theorem encode_none [encodable α] : encode (@none α) = 0 := rfl
@[simp] theorem encode_some [encodable α] (a : α) :
encode (some a) = succ (encode a) := rfl
@[simp] theorem decode_option_zero [encodable α] : decode (option α) 0 = some none := rfl
@[simp] theorem decode_option_succ [encodable α] (n) :
decode (option α) (succ n) = (decode α n).map some := rfl
def decode2 (α) [encodable α] (n : ℕ) : option α :=
(decode α n).bind (option.guard (λ a, encode a = n))
theorem mem_decode2' [encodable α] {n : ℕ} {a : α} :
a ∈ decode2 α n ↔ a ∈ decode α n ∧ encode a = n :=
by simp [decode2]; exact
⟨λ ⟨_, h₁, rfl, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨_, h₁, rfl, h₂⟩⟩
theorem mem_decode2 [encodable α] {n : ℕ} {a : α} :
a ∈ decode2 α n ↔ encode a = n :=
mem_decode2'.trans (and_iff_right_of_imp $ λ e, e ▸ encodek _)
theorem decode2_is_partial_inv [encodable α] : is_partial_inv encode (decode2 α) :=
λ a n, mem_decode2
theorem decode2_inj [encodable α] {n : ℕ} {a₁ a₂ : α}
(h₁ : a₁ ∈ decode2 α n) (h₂ : a₂ ∈ decode2 α n) : a₁ = a₂ :=
encode_injective $ (mem_decode2.1 h₁).trans (mem_decode2.1 h₂).symm
theorem encodek2 [encodable α] (a : α) : decode2 α (encode a) = some a :=
mem_decode2.2 rfl
def decidable_range_encode (α : Type*) [encodable α] : decidable_pred (set.range (@encode α _)) :=
λ x, decidable_of_iff (option.is_some (decode2 α x))
⟨λ h, ⟨option.get h, by rw [← decode2_is_partial_inv (option.get h), option.some_get]⟩,
λ ⟨n, hn⟩, by rw [← hn, encodek2]; exact rfl⟩
def equiv_range_encode (α : Type*) [encodable α] : α ≃ set.range (@encode α _) :=
{ to_fun := λ a : α, ⟨encode a, set.mem_range_self _⟩,
inv_fun := λ n, option.get (show is_some (decode2 α n.1),
by cases n.2 with x hx; rw [← hx, encodek2]; exact rfl),
left_inv := λ a, by dsimp;
rw [← option.some_inj, option.some_get, encodek2],
right_inv := λ ⟨n, x, hx⟩, begin
apply subtype.eq,
dsimp,
conv {to_rhs, rw ← hx},
rw [encode_injective.eq_iff, ← option.some_inj, option.some_get, ← hx, encodek2],
end }
section sum
variables [encodable α] [encodable β]
def encode_sum : α ⊕ β → nat
| (sum.inl a) := bit0 $ encode a
| (sum.inr b) := bit1 $ encode b
def decode_sum (n : nat) : option (α ⊕ β) :=
match bodd_div2 n with
| (ff, m) := (decode α m).map sum.inl
| (tt, m) := (decode β m).map sum.inr
end
instance sum : encodable (α ⊕ β) :=
⟨encode_sum, decode_sum, λ s,
by cases s; simp [encode_sum, decode_sum, encodek]; refl⟩
@[simp] theorem encode_inl (a : α) :
@encode (α ⊕ β) _ (sum.inl a) = bit0 (encode a) := rfl
@[simp] theorem encode_inr (b : β) :
@encode (α ⊕ β) _ (sum.inr b) = bit1 (encode b) := rfl
@[simp] theorem decode_sum_val (n : ℕ) :
decode (α ⊕ β) n = decode_sum n := rfl
end sum
instance bool : encodable bool :=
of_equiv (unit ⊕ unit) equiv.bool_equiv_punit_sum_punit
@[simp] theorem encode_tt : encode tt = 1 := rfl
@[simp] theorem encode_ff : encode ff = 0 := rfl
@[simp] theorem decode_zero : decode bool 0 = some ff := rfl
@[simp] theorem decode_one : decode bool 1 = some tt := rfl
theorem decode_ge_two (n) (h : 2 ≤ n) : decode bool n = none :=
begin
suffices : decode_sum n = none,
{ change (decode_sum n).map _ = none, rw this, refl },
have : 1 ≤ div2 n,
{ rw [div2_val, nat.le_div_iff_mul_le],
exacts [h, dec_trivial] },
cases exists_eq_succ_of_ne_zero (ne_of_gt this) with m e,
simp [decode_sum]; cases bodd n; simp [decode_sum]; rw e; refl
end
section sigma
variables {γ : α → Type*} [encodable α] [∀ a, encodable (γ a)]
def encode_sigma : sigma γ → ℕ
| ⟨a, b⟩ := mkpair (encode a) (encode b)
def decode_sigma (n : ℕ) : option (sigma γ) :=
let (n₁, n₂) := unpair n in
(decode α n₁).bind $ λ a, (decode (γ a) n₂).map $ sigma.mk a
instance sigma : encodable (sigma γ) :=
⟨encode_sigma, decode_sigma, λ ⟨a, b⟩,
by simp [encode_sigma, decode_sigma, unpair_mkpair, encodek]⟩
@[simp] theorem decode_sigma_val (n : ℕ) : decode (sigma γ) n =
(decode α n.unpair.1).bind (λ a, (decode (γ a) n.unpair.2).map $ sigma.mk a) :=
show decode_sigma._match_1 _ = _, by cases n.unpair; refl
@[simp] theorem encode_sigma_val (a b) : @encode (sigma γ) _ ⟨a, b⟩ =
mkpair (encode a) (encode b) := rfl
end sigma
section prod
variables [encodable α] [encodable β]
instance prod : encodable (α × β) :=
of_equiv _ (equiv.sigma_equiv_prod α β).symm
@[simp] theorem decode_prod_val (n : ℕ) : decode (α × β) n =
(decode α n.unpair.1).bind (λ a, (decode β n.unpair.2).map $ prod.mk a) :=
show (decode (sigma (λ _, β)) n).map (equiv.sigma_equiv_prod α β) = _,
by simp; cases decode α n.unpair.1; simp;
cases decode β n.unpair.2; refl
@[simp] theorem encode_prod_val (a b) : @encode (α × β) _ (a, b) =
mkpair (encode a) (encode b) := rfl
end prod
section subtype
open subtype decidable
variable {P : α → Prop}
variable [encA : encodable α]
variable [decP : decidable_pred P]
include encA
def encode_subtype : {a : α // P a} → nat
| ⟨v, h⟩ := encode v
include decP
def decode_subtype (v : nat) : option {a : α // P a} :=
(decode α v).bind $ λ a,
if h : P a then some ⟨a, h⟩ else none
instance subtype : encodable {a : α // P a} :=
⟨encode_subtype, decode_subtype,
λ ⟨v, h⟩, by simp [encode_subtype, decode_subtype, encodek, h]⟩
end subtype
instance fin (n) : encodable (fin n) :=
of_equiv _ (equiv.fin_equiv_subtype _)
instance int : encodable ℤ :=
of_equiv _ equiv.int_equiv_nat
instance ulift [encodable α] : encodable (ulift α) :=
of_equiv _ equiv.ulift
instance plift [encodable α] : encodable (plift α) :=
of_equiv _ equiv.plift
noncomputable def of_inj [encodable β] (f : α → β) (hf : injective f) : encodable α :=
of_left_injection f (partial_inv f) (λ x, (partial_inv_of_injective hf _ _).2 rfl)
end encodable
/-
Choice function for encodable types and decidable predicates.
We provide the following API
choose {α : Type*} {p : α → Prop} [c : encodable α] [d : decidable_pred p] : (∃ x, p x) → α :=
choose_spec {α : Type*} {p : α → Prop} [c : encodable α] [d : decidable_pred p] (ex : ∃ x, p x) : p (choose ex) :=
-/
namespace encodable
section find_a
variables {α : Type*} (p : α → Prop) [encodable α] [decidable_pred p]
private def good : option α → Prop
| (some a) := p a
| none := false
private def decidable_good : decidable_pred (good p)
| n := by cases n; unfold good; apply_instance
local attribute [instance] decidable_good
open encodable
variable {p}
def choose_x (h : ∃ x, p x) : {a:α // p a} :=
have ∃ n, good p (decode α n), from
let ⟨w, pw⟩ := h in ⟨encode w, by simp [good, encodek, pw]⟩,
match _, nat.find_spec this : ∀ o, good p o → {a // p a} with
| some a, h := ⟨a, h⟩
end
def choose (h : ∃ x, p x) : α := (choose_x h).1
lemma choose_spec (h : ∃ x, p x) : p (choose h) := (choose_x h).2
end find_a
theorem axiom_of_choice {α : Type*} {β : α → Type*} {R : Π x, β x → Prop}
[Π a, encodable (β a)] [∀ x y, decidable (R x y)]
(H : ∀x, ∃y, R x y) : ∃f:Πa, β a, ∀x, R x (f x) :=
⟨λ x, choose (H x), λ x, choose_spec (H x)⟩
theorem skolem {α : Type*} {β : α → Type*} {P : Π x, β x → Prop}
[c : Π a, encodable (β a)] [d : ∀ x y, decidable (P x y)] :
(∀x, ∃y, P x y) ↔ ∃f : Π a, β a, (∀x, P x (f x)) :=
⟨axiom_of_choice, λ ⟨f, H⟩ x, ⟨_, H x⟩⟩
end encodable
namespace quot
open encodable
variables {α : Type*} {s : setoid α} [@decidable_rel α (≈)] [encodable α]
-- Choose equivalence class representative
def rep (q : quotient s) : α :=
choose (exists_rep q)
theorem rep_spec (q : quotient s) : ⟦rep q⟧ = q :=
choose_spec (exists_rep q)
def encodable_quotient : encodable (quotient s) :=
⟨λ q, encode (rep q),
λ n, quotient.mk <$> decode α n,
by rintros ⟨l⟩; rw encodek; exact congr_arg some (rep_spec _)⟩
end quot
|
5d5da057f2fb36f12021ccc3bc9d8d9bf8e3d1da | bf35a3ed54de6fced25e870a19cf82da937bdc9e | /src/line_echo.lean | bc482f71245558a9298bbfb1797420020f0d5811 | [] | no_license | khoek/klean-demo | cd01e703e1333fd6095ea5349986a614b53383f5 | d572f3ee90589854beb66cb7499a99722c454689 | refs/heads/master | 1,585,083,090,000 | 1,533,310,995,000 | 1,533,310,995,000 | 143,000,189 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,645 | lean | import system.io
import .lib
open io
def read_sock_chunk (sock : socket) : io string := do
c ← read_sock sock 1,
return c
meta def process_command (cmd : string) : io unit := do
-- FIXME how do you concatenate strings!?! :O
io.print "printing:",
io.print_ln cmd
-- The option ℕ is where the first nl character is.
-- Our job is to call process_command if we find a nl and then advance the buffer,
-- else just keep the buff how it is. We keep looking for newlines until there aren't
-- any more.
meta def parse_chunk_internal : string → option ℕ → io string
| buff none := pure buff
| buff (some n) := do
(cmd, c, rest) ← pure (string_cut buff n),
process_command cmd,
-- FIXME how do you forward declare?
let nl_pos := find_char '\n' rest in do
parse_chunk_internal rest nl_pos
meta def parse_chunk (buff : string) : io string :=
let nl_pos := find_char '\n' buff in
parse_chunk_internal buff nl_pos
-- HEY Scott: does lean implement tail call elimination? If it doesn't that means
-- its impossible to implement a request-handling loop which can run forever
-- (eventually the stack will overflow).
meta def main_loop_internal (sock : socket) : string → io unit :=
-- FIXME why does this lambda have to be in here!?!
λ buff, do
newchunk ← read_sock_chunk sock,
new_buff ← parse_chunk (string.append buff newchunk),
main_loop_internal new_buff
meta def run_main_loop (sock : socket) : io unit :=
main_loop_internal sock ""
meta def main : io unit := do
sock ← io.mk_socket_handle "/home/khoek/lsocket1",
run_main_loop sock
|
79b8b81825c96b9b2b1e98faa2836058577cd6cf | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/computability/tm_to_partrec_auto.lean | a1ed89a837fdd4852d2b55cdae996ba663968d00 | [] | 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 | 38,885 | lean | /-
Copyright (c) 2020 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.computability.halting
import Mathlib.computability.turing_machine
import Mathlib.data.num.lemmas
import Mathlib.PostPort
universes l u_1
namespace Mathlib
/-!
# Modelling partial recursive functions using Turing machines
This file defines a simplified basis for partial recursive functions, and a `turing.TM2` model
Turing machine for evaluating these functions. This amounts to a constructive proof that every
`partrec` function can be evaluated by a Turing machine.
## Main definitions
* `to_partrec.code`: a simplified basis for partial recursive functions, valued in
`list ℕ →. list ℕ`.
* `to_partrec.code.eval`: semantics for a `to_partrec.code` program
* `partrec_to_TM2.tr`: A TM2 turing machine which can evaluate `code` programs
-/
namespace turing
/-!
## A simplified basis for partrec
This section constructs the type `code`, which is a data type of programs with `list ℕ` input and
output, with enough expressivity to write any partial recursive function. The primitives are:
* `zero'` appends a `0` to the input. That is, `zero' v = 0 :: v`.
* `succ` returns the successor of the head of the input, defaulting to zero if there is no head:
* `succ [] = [1]`
* `succ (n :: v) = [n + 1]`
* `tail` returns the tail of the input
* `tail [] = []`
* `tail (n :: v) = v`
* `cons f fs` calls `f` and `fs` on the input and conses the results:
* `cons f fs v = (f v).head :: fs v`
* `comp f g` calls `f` on the output of `g`:
* `comp f g v = f (g v)`
* `case f g` cases on the head of the input, calling `f` or `g` depending on whether it is zero or
a successor (similar to `nat.cases_on`).
* `case f g [] = f []`
* `case f g (0 :: v) = f v`
* `case f g (n+1 :: v) = g (n :: v)`
* `fix f` calls `f` repeatedly, using the head of the result of `f` to decide whether to call `f`
again or finish:
* `fix f v = []` if `f v = []`
* `fix f v = w` if `f v = 0 :: w`
* `fix f v = fix f w` if `f v = n+1 :: w` (the exact value of `n` is discarded)
This basis is convenient because it is closer to the Turing machine model - the key operations are
splitting and merging of lists of unknown length, while the messy `n`-ary composition operation
from the traditional basis for partial recursive functions is absent - but it retains a
compositional semantics. The first step in transitioning to Turing machines is to make a sequential
evaluator for this basis, which we take up in the next section.
-/
namespace to_partrec
/-- The type of codes for primitive recursive functions. Unlike `nat.partrec.code`, this uses a set
of operations on `list ℕ`. See `code.eval` for a description of the behavior of the primitives. -/
inductive code where
| zero' : code
| succ : code
| tail : code
| cons : code → code → code
| comp : code → code → code
| case : code → code → code
| fix : code → code
/-- The semantics of the `code` primitives, as partial functions `list ℕ →. list ℕ`. By convention
we functions that return a single result return a singleton `[n]`, or in some cases `n :: v` where
`v` will be ignored by a subsequent function.
* `zero'` appends a `0` to the input. That is, `zero' v = 0 :: v`.
* `succ` returns the successor of the head of the input, defaulting to zero if there is no head:
* `succ [] = [1]`
* `succ (n :: v) = [n + 1]`
* `tail` returns the tail of the input
* `tail [] = []`
* `tail (n :: v) = v`
* `cons f fs` calls `f` and `fs` on the input and conses the results:
* `cons f fs v = (f v).head :: fs v`
* `comp f g` calls `f` on the output of `g`:
* `comp f g v = f (g v)`
* `case f g` cases on the head of the input, calling `f` or `g` depending on whether it is zero or
a successor (similar to `nat.cases_on`).
* `case f g [] = f []`
* `case f g (0 :: v) = f v`
* `case f g (n+1 :: v) = g (n :: v)`
* `fix f` calls `f` repeatedly, using the head of the result of `f` to decide whether to call `f`
again or finish:
* `fix f v = []` if `f v = []`
* `fix f v = w` if `f v = 0 :: w`
* `fix f v = fix f w` if `f v = n+1 :: w` (the exact value of `n` is discarded)
-/
@[simp] def code.eval : code → List ℕ →. List ℕ := sorry
namespace code
/-- `nil` is the constant nil function: `nil v = []`. -/
def nil : code := comp tail succ
@[simp] theorem nil_eval (v : List ℕ) : eval nil v = pure [] := sorry
/-- `id` is the identity function: `id v = v`. -/
def id : code := comp tail zero'
@[simp] theorem id_eval (v : List ℕ) : eval id v = pure v := sorry
/-- `head` gets the head of the input list: `head [] = [0]`, `head (n :: v) = [n]`. -/
def head : code := cons id nil
@[simp] theorem head_eval (v : List ℕ) : eval head v = pure [list.head v] := sorry
/-- `zero` is the constant zero function: `zero v = [0]`. -/
def zero : code := cons zero' nil
@[simp] theorem zero_eval (v : List ℕ) : eval zero v = pure [0] := sorry
/-- `pred` returns the predecessor of the head of the input:
`pred [] = [0]`, `pred (0 :: v) = [0]`, `pred (n+1 :: v) = [n]`. -/
def pred : code := case zero head
@[simp] theorem pred_eval (v : List ℕ) : eval pred v = pure [Nat.pred (list.head v)] := sorry
/-- `rfind f` performs the function of the `rfind` primitive of partial recursive functions.
`rfind f v` returns the smallest `n` such that `(f (n :: v)).head = 0`.
It is implemented as:
rfind f v = pred (fix (λ (n::v), f (n::v) :: n+1 :: v) (0 :: v))
The idea is that the initial state is `0 :: v`, and the `fix` keeps `n :: v` as its internal state;
it calls `f (n :: v)` as the exit test and `n+1 :: v` as the next state. At the end we get
`n+1 :: v` where `n` is the desired output, and `pred (n+1 :: v) = [n]` returns the result.
-/
def rfind (f : code) : code := comp pred (comp (fix (cons f (cons succ tail))) zero')
/-- `prec f g` implements the `prec` (primitive recursion) operation of partial recursive
functions. `prec f g` evaluates as:
* `prec f g [] = [f []]`
* `prec f g (0 :: v) = [f v]`
* `prec f g (n+1 :: v) = [g (n :: prec f g (n :: v) :: v)]`
It is implemented as:
G (a :: b :: IH :: v) = (b :: a+1 :: b-1 :: g (a :: IH :: v) :: v)
F (0 :: f_v :: v) = (f_v :: v)
F (n+1 :: f_v :: v) = (fix G (0 :: n :: f_v :: v)).tail.tail
prec f g (a :: v) = [(F (a :: f v :: v)).head]
Because `fix` always evaluates its body at least once, we must special case the `0` case to avoid
calling `g` more times than necessary (which could be bad if `g` diverges). If the input is
`0 :: v`, then `F (0 :: f v :: v) = (f v :: v)` so we return `[f v]`. If the input is `n+1 :: v`,
we evaluate the function from the bottom up, with initial state `0 :: n :: f v :: v`. The first
number counts up, providing arguments for the applications to `g`, while the second number counts
down, providing the exit condition (this is the initial `b` in the return value of `G`, which is
stripped by `fix`). After the `fix` is complete, the final state is `n :: 0 :: res :: v` where
`res` is the desired result, and the rest reduces this to `[res]`. -/
def prec (f : code) (g : code) : code :=
let G : code :=
cons tail
(cons succ
(cons (comp pred tail)
(cons (comp g (cons id (comp tail tail))) (comp tail (comp tail tail)))));
let F : code := case id (comp (comp (comp tail tail) (fix G)) zero');
cons (comp F (cons head (cons (comp f tail) tail))) nil
theorem exists_code.comp {m : ℕ} {n : ℕ} {f : vector ℕ n →. ℕ} {g : fin n → vector ℕ m →. ℕ}
(hf : ∃ (c : code), ∀ (v : vector ℕ n), eval c (subtype.val v) = pure <$> f v)
(hg :
∀ (i : fin n), ∃ (c : code), ∀ (v : vector ℕ m), eval c (subtype.val v) = pure <$> g i v) :
∃ (c : code),
∀ (v : vector ℕ m),
eval c (subtype.val v) = pure <$> ((vector.m_of_fn fun (i : fin n) => g i v) >>= f) :=
sorry
theorem exists_code {n : ℕ} {f : vector ℕ n →. ℕ} (hf : nat.partrec' f) :
∃ (c : code), ∀ (v : vector ℕ n), eval c (subtype.val v) = pure <$> f v :=
sorry
end code
/-!
## From compositional semantics to sequential semantics
Our initial sequential model is designed to be as similar as possible to the compositional
semantics in terms of its primitives, but it is a sequential semantics, meaning that rather than
defining an `eval c : list ℕ →. list ℕ` function for each program, defined by recursion on
programs, we have a type `cfg` with a step function `step : cfg → option cfg` that provides a
deterministic evaluation order. In order to do this, we introduce the notion of a *continuation*,
which can be viewed as a `code` with a hole in it where evaluation is currently taking place.
Continuations can be assigned a `list ℕ →. list ℕ` semantics as well, with the interpretation
being that given a `list ℕ` result returned from the code in the hole, the remainder of the
program will evaluate to a `list ℕ` final value.
The continuations are:
* `halt`: the empty continuation: the hole is the whole program, whatever is returned is the
final result. In our notation this is just `_`.
* `cons₁ fs v k`: evaluating the first part of a `cons`, that is `k (_ :: fs v)`, where `k` is the
outer continuation.
* `cons₂ ns k`: evaluating the second part of a `cons`: `k (ns.head :: _)`. (Technically we don't
need to hold on to all of `ns` here since we are already committed to taking the head, but this
is more regular.)
* `comp f k`: evaluating the first part of a composition: `k (f _)`.
* `fix f k`: waiting for the result of `f` in a `fix f` expression:
`k (if _.head = 0 then _.tail else fix f (_.tail))`
The type `cfg` of evaluation states is:
* `ret k v`: we have received a result, and are now evaluating the continuation `k` with result
`v`; that is, `k v` where `k` is ready to evaluate.
* `halt v`: we are done and the result is `v`.
The main theorem of this section is that for each code `c`, the state `step_normal c halt v` steps
to `v'` in finitely many steps if and only if `code.eval c v = some v'`.
-/
/-- The type of continuations, built up during evaluation of a `code` expression. -/
inductive cont where
| halt : cont
| cons₁ : code → List ℕ → cont → cont
| cons₂ : List ℕ → cont → cont
| comp : code → cont → cont
| fix : code → cont → cont
/-- The semantics of a continuation. -/
def cont.eval : cont → List ℕ →. List ℕ := sorry
/-- The semantics of a continuation. -/
inductive cfg where
| halt : List ℕ → cfg
| ret : cont → List ℕ → cfg
/-- Evaluating `c : code` in a continuation `k : cont` and input `v : list ℕ`. This goes by
recursion on `c`, building an augmented continuation and a value to pass to it.
* `zero' v = 0 :: v` evaluates immediately, so we return it to the parent continuation
* `succ v = [v.head.succ]` evaluates immediately, so we return it to the parent continuation
* `tail v = v.tail` evaluates immediately, so we return it to the parent continuation
* `cons f fs v = (f v).head :: fs v` requires two sub-evaluations, so we evaluate
`f v` in the continuation `k (_.head :: fs v)` (called `cont.cons₁ fs v k`)
* `comp f g v = f (g v)` requires two sub-evaluations, so we evaluate
`g v` in the continuation `k (f _)` (called `cont.comp f k`)
* `case f g v = v.head.cases_on (f v.tail) (λ n, g (n :: v.tail))` has the information needed to
evaluate the case statement, so we do that and transition to either `f v` or `g (n :: v.tail)`.
* `fix f v = let v' := f v in if v'.head = 0 then k v'.tail else fix f v'.tail`
needs to first evaluate `f v`, so we do that and leave the rest for the continuation (called
`cont.fix f k`)
-/
def step_normal : code → cont → List ℕ → cfg := sorry
/-- Evaluating a continuation `k : cont` on input `v : list ℕ`. This is the second part of
evaluation, when we receive results from continuations built by `step_normal`.
* `cont.halt v = v`, so we are done and transition to the `cfg.halt v` state
* `cont.cons₁ fs as k v = k (v.head :: fs as)`, so we evaluate `fs as` now with the continuation
`k (v.head :: _)` (called `cons₂ v k`).
* `cont.cons₂ ns k v = k (ns.head :: v)`, where we now have everything we need to evaluate
`ns.head :: v`, so we return it to `k`.
* `cont.comp f k v = k (f v)`, so we call `f v` with `k` as the continuation.
* `cont.fix f k v = k (if v.head = 0 then k v.tail else fix f v.tail)`, where `v` is a value,
so we evaluate the if statement and either call `k` with `v.tail`, or call `fix f v` with `k` as
the continuation (which immediately calls `f` with `cont.fix f k` as the continuation).
-/
def step_ret : cont → List ℕ → cfg := sorry
/-- If we are not done (in `cfg.halt` state), then we must be still stuck on a continuation, so
this main loop calls `step_ret` with the new continuation. The overall `step` function transitions
from one `cfg` to another, only halting at the `cfg.halt` state. -/
def step : cfg → Option cfg := sorry
/-- In order to extract a compositional semantics from the sequential execution behavior of
configurations, we observe that continuations have a monoid structure, with `cont.halt` as the unit
and `cont.then` as the multiplication. `cont.then k₁ k₂` runs `k₁` until it halts, and then takes
the result of `k₁` and passes it to `k₂`.
We will not prove it is associative (although it is), but we are instead interested in the
associativity law `k₂ (eval c k₁) = eval c (k₁.then k₂)`. This holds at both the sequential and
compositional levels, and allows us to express running a machine without the ambient continuation
and relate it to the original machine's evaluation steps. In the literature this is usually
where one uses Turing machines embedded inside other Turing machines, but this approach allows us
to avoid changing the ambient type `cfg` in the middle of the recursion.
-/
def cont.then : cont → cont → cont := sorry
theorem cont.then_eval {k : cont} {k' : cont} {v : List ℕ} :
cont.eval (cont.then k k') v = cont.eval k v >>= cont.eval k' :=
sorry
/-- The `then k` function is a "configuration homomorphism". Its operation on states is to append
`k` to the continuation of a `cfg.ret` state, and to run `k` on `v` if we are in the `cfg.halt v`
state. -/
def cfg.then : cfg → cont → cfg := sorry
/-- The `step_normal` function respects the `then k'` homomorphism. Note that this is an exact
equality, not a simulation; the original and embedded machines move in lock-step until the
embedded machine reaches the halt state. -/
theorem step_normal_then (c : code) (k : cont) (k' : cont) (v : List ℕ) :
step_normal c (cont.then k k') v = cfg.then (step_normal c k v) k' :=
sorry
/-- The `step_ret` function respects the `then k'` homomorphism. Note that this is an exact
equality, not a simulation; the original and embedded machines move in lock-step until the
embedded machine reaches the halt state. -/
theorem step_ret_then {k : cont} {k' : cont} {v : List ℕ} :
step_ret (cont.then k k') v = cfg.then (step_ret k v) k' :=
sorry
/-- This is a temporary definition, because we will prove in `code_is_ok` that it always holds.
It asserts that `c` is semantically correct; that is, for any `k` and `v`,
`eval (step_normal c k v) = eval (cfg.ret k (code.eval c v))`, as an equality of partial values
(so one diverges iff the other does).
In particular, we can let `k = cont.halt`, and then this asserts that `step_normal c cont.halt v`
evaluates to `cfg.halt (code.eval c v)`. -/
def code.ok (c : code) :=
∀ (k : cont) (v : List ℕ),
eval step (step_normal c k v) =
do
let v ← code.eval c v
eval step (cfg.ret k v)
theorem code.ok.zero {c : code} (h : code.ok c) {v : List ℕ} :
eval step (step_normal c cont.halt v) = cfg.halt <$> code.eval c v :=
sorry
theorem step_normal.is_ret (c : code) (k : cont) (v : List ℕ) :
∃ (k' : cont), ∃ (v' : List ℕ), step_normal c k v = cfg.ret k' v' :=
sorry
theorem cont_eval_fix {f : code} {k : cont} {v : List ℕ} (fok : code.ok f) :
eval step (step_normal f (cont.fix f k) v) =
do
let v ← code.eval (code.fix f) v
eval step (cfg.ret k v) :=
sorry
theorem code_is_ok (c : code) : code.ok c := sorry
theorem step_normal_eval (c : code) (v : List ℕ) :
eval step (step_normal c cont.halt v) = cfg.halt <$> code.eval c v :=
code.ok.zero (code_is_ok c)
theorem step_ret_eval {k : cont} {v : List ℕ} :
eval step (step_ret k v) = cfg.halt <$> cont.eval k v :=
sorry
end to_partrec
/-!
## Simulating sequentialized partial recursive functions in TM2
At this point we have a sequential model of partial recursive functions: the `cfg` type and
`step : cfg → option cfg` function from the previous section. The key feature of this model is that
it does a finite amount of computation (in fact, an amount which is statically bounded by the size
of the program) between each step, and no individual step can diverge (unlike the compositional
semantics, where every sub-part of the computation is potentially divergent). So we can utilize the
same techniques as in the other TM simulations in `computability.turing_machine` to prove that
each step corresponds to a finite number of steps in a lower level model. (We don't prove it here,
but in anticipation of the complexity class P, the simulation is actually polynomial-time as well.)
The target model is `turing.TM2`, which has a fixed finite set of stacks, a bit of local storage,
with programs selected from a potentially infinite (but finitely accessible) set of program
positions, or labels `Λ`, each of which executes a finite sequence of basic stack commands.
For this program we will need four stacks, each on an alphabet `Γ'` like so:
inductive Γ' | Cons | cons | bit0 | bit1
We represent a number as a bit sequence, lists of numbers by putting `cons` after each element, and
lists of lists of natural numbers by putting `Cons` after each list. For example:
0 ~> []
1 ~> [bit1]
6 ~> [bit0, bit1, bit1]
[1, 2] ~> [bit1, cons, bit0, bit1, cons]
[[], [1, 2]] ~> [Cons, bit1, cons, bit0, bit1, cons, Cons]
The four stacks are `main`, `rev`, `aux`, `stack`. In normal mode, `main` contains the input to the
current program (a `list ℕ`) and `stack` contains data (a `list (list ℕ)`) associated to the
current continuation, and in `ret` mode `main` contains the value that is being passed to the
continuation and `stack` contains the data for the continuation. The `rev` and `aux` stacks are
usually empty; `rev` is used to store reversed data when e.g. moving a value from one stack to
another, while `aux` is used as a temporary for a `main`/`stack` swap that happens during `cons₁`
evaluation.
The only local store we need is `option Γ'`, which stores the result of the last pop
operation. (Most of our working data are natural numbers, which are too large to fit in the local
store.)
The continuations from the previous section are data-carrying, containing all the values that have
been computed and are awaiting other arguments. In order to have only a finite number of
continuations appear in the program so that they can be used in machine states, we separate the
data part (anything with type `list ℕ`) from the `cont` type, producing a `cont'` type that lacks
this information. The data is kept on the `stack` stack.
Because we want to have subroutines for e.g. moving an entire stack to another place, we use an
infinite inductive type `Λ'` so that we can execute a program and then return to do something else
without having to define too many different kinds of intermediate states. (We must nevertheless
prove that only finitely many labels are accessible.) The labels are:
* `move p k₁ k₂ q`: move elements from stack `k₁` to `k₂` while `p` holds of the value being moved.
The last element, that fails `p`, is placed in neither stack but left in the local store.
At the end of the operation, `k₂` will have the elements of `k₁` in reverse order. Then do `q`.
* `clear p k q`: delete elements from stack `k` until `p` is true. Like `move`, the last element is
left in the local storage. Then do `q`.
* `copy q`: Move all elements from `rev` to both `main` and `stack` (in reverse order),
then do `q`. That is, it takes `(a, b, c, d)` to `(b.reverse ++ a, [], c, b.reverse ++ d)`.
* `push k f q`: push `f s`, where `s` is the local store, to stack `k`, then do `q`. This is a
duplicate of the `push` instruction that is part of the TM2 model, but by having a subroutine
just for this purpose we can build up programs to execute inside a `goto` statement, where we
have the flexibility to be general recursive.
* `read (f : option Γ' → Λ')`: go to state `f s` where `s` is the local store. Again this is only
here for convenience.
* `succ q`: perform a successor operation. Assuming `[n]` is encoded on `main` before,
`[n+1]` will be on main after. This implements successor for binary natural numbers.
* `pred q₁ q₂`: perform a predecessor operation or `case` statement. If `[]` is encoded on
`main` before, then we transition to `q₁` with `[]` on main; if `(0 :: v)` is on `main` before
then `v` will be on `main` after and we transition to `q₁`; and if `(n+1 :: v)` is on `main`
before then `n :: v` will be on `main` after and we transition to `q₂`.
* `ret k`: call continuation `k`. Each continuation has its own interpretation of the data in
`stack` and sets up the data for the next continuation.
* `ret (cons₁ fs k)`: `v :: k_data` on `stack` and `ns` on `main`, and the next step expects
`v` on `main` and `ns :: k_data` on `stack`. So we have to do a little dance here with six
reverse-moves using the `aux` stack to perform a three-point swap, each of which involves two
reversals.
* `ret (cons₂ k)`: `ns :: k_data` is on `stack` and `v` is on `main`, and we have to put
`ns.head :: v` on `main` and `k_data` on `stack`. This is done using the `head` subroutine.
* `ret (fix f k)`: This stores no data, so we just check if `main` starts with `0` and
if so, remove it and call `k`, otherwise `clear` the first value and call `f`.
* `ret halt`: the stack is empty, and `main` has the output. Do nothing and halt.
In addition to these basic states, we define some additional subroutines that are used in the
above:
* `push'`, `peek'`, `pop'` are special versions of the builtins that use the local store to supply
inputs and outputs.
* `unrev`: special case `move ff rev main` to move everything from `rev` back to `main`. Used as a
cleanup operation in several functions.
* `move_excl p k₁ k₂ q`: same as `move` but pushes the last value read back onto the source stack.
* `move₂ p k₁ k₂ q`: double `move`, so that the result comes out in the right order at the target
stack. Implemented as `move_excl p k rev; move ff rev k₂`. Assumes that neither `k₁` nor `k₂` is
`rev` and `rev` is initially empty.
* `head k q`: get the first natural number from stack `k` and reverse-move it to `rev`, then clear
the rest of the list at `k` and then `unrev` to reverse-move the head value to `main`. This is
used with `k = main` to implement regular `head`, i.e. if `v` is on `main` before then `[v.head]`
will be on `main` after; and also with `k = stack` for the `cons` operation, which has `v` on
`main` and `ns :: k_data` on `stack`, and results in `k_data` on `stack` and `ns.head :: v` on
`main`.
* `tr_normal` is the main entry point, defining states that perform a given `code` computation.
It mostly just dispatches to functions written above.
The main theorem of this section is `tr_eval`, which asserts that for each that for each code `c`,
the state `init c v` steps to `halt v'` in finitely many steps if and only if
`code.eval c v = some v'`.
-/
namespace partrec_to_TM2
/-- The alphabet for the stacks in the program. `bit0` and `bit1` are used to represent `ℕ` values
as lists of binary digits, `cons` is used to separate `list ℕ` values, and `Cons` is used to
separate `list (list ℕ)` values. See the section documentation. -/
inductive Γ' where
| Cons : Γ'
| cons : Γ'
| bit0 : Γ'
| bit1 : Γ'
/-- The four stacks used by the program. `main` is used to store the input value in `tr_normal`
mode and the output value in `Λ'.ret` mode, while `stack` is used to keep all the data for the
continuations. `rev` is used to store reversed lists when transferring values between stacks, and
`aux` is only used once in `cons₁`. See the section documentation. -/
inductive K' where
| main : K'
| rev : K'
| aux : K'
| stack : K'
/-- Continuations as in `to_partrec.cont` but with the data removed. This is done because we want
the set of all continuations in the program to be finite (so that it can ultimately be encoded into
the finite state machine of a Turing machine), but a continuation can handle a potentially infinite
number of data values during execution. -/
inductive cont' where
| halt : cont'
| cons₁ : to_partrec.code → cont' → cont'
| cons₂ : cont' → cont'
| comp : to_partrec.code → cont' → cont'
| fix : to_partrec.code → cont' → cont'
/-- The set of program positions. We make extensive use of inductive types here to let us describe
"subroutines"; for example `clear p k q` is a program that clears stack `k`, then does `q` where
`q` is another label. In order to prevent this from resulting in an infinite number of distinct
accessible states, we are careful to be non-recursive (although loops are okay). See the section
documentation for a description of all the programs. -/
inductive Λ' where
| move : (Γ' → Bool) → K' → K' → Λ' → Λ'
| clear : (Γ' → Bool) → K' → Λ' → Λ'
| copy : Λ' → Λ'
| push : K' → (Option Γ' → Option Γ') → Λ' → Λ'
| read : (Option Γ' → Λ') → Λ'
| succ : Λ' → Λ'
| pred : Λ' → Λ' → Λ'
| ret : cont' → Λ'
protected instance Λ'.inhabited : Inhabited Λ' := { default := Λ'.ret cont'.halt }
/-- The type of TM2 statements used by this machine. -/
/-- The type of TM2 configurations used by this machine. -/
def stmt' := TM2.stmt (fun (_x : K') => Γ') Λ' (Option Γ')
def cfg' := TM2.cfg (fun (_x : K') => Γ') Λ' (Option Γ')
/-- A predicate that detects the end of a natural number, either `Γ'.cons` or `Γ'.Cons` (or
implicitly the end of the list), for use in predicate-taking functions like `move` and `clear`. -/
def nat_end : Γ' → Bool := sorry
/-- Pop a value from the stack and place the result in local store. -/
/-- Peek a value from the stack and place the result in local store. -/
@[simp] def pop' (k : K') : stmt' → stmt' := TM2.stmt.pop k fun (x v : Option Γ') => v
/-- Push the value in the local store to the given stack. -/
@[simp] def peek' (k : K') : stmt' → stmt' := TM2.stmt.peek k fun (x v : Option Γ') => v
@[simp] def push' (k : K') : stmt' → stmt' := TM2.stmt.push k fun (x : Option Γ') => option.iget x
/-- Move everything from the `rev` stack to the `main` stack (reversed). -/
def unrev (q : Λ') : Λ' := Λ'.move (fun (_x : Γ') => false) K'.rev K'.main
/-- Move elements from `k₁` to `k₂` while `p` holds, with the last element being left on `k₁`. -/
def move_excl (p : Γ' → Bool) (k₁ : K') (k₂ : K') (q : Λ') : Λ' := Λ'.move p k₁ k₂ (Λ'.push k₁ id q)
/-- Move elements from `k₁` to `k₂` without reversion, by performing a double move via the `rev`
stack. -/
def move₂ (p : Γ' → Bool) (k₁ : K') (k₂ : K') (q : Λ') : Λ' :=
move_excl p k₁ K'.rev (Λ'.move (fun (_x : Γ') => false) K'.rev k₂ q)
/-- Assuming `tr_list v` is on the front of stack `k`, remove it, and push `v.head` onto `main`.
See the section documentation. -/
def head (k : K') (q : Λ') : Λ' :=
Λ'.move nat_end k K'.rev
(Λ'.push K'.rev (fun (_x : Option Γ') => some Γ'.cons)
(Λ'.read
fun (s : Option Γ') =>
ite (s = some Γ'.Cons) id (Λ'.clear (fun (x : Γ') => to_bool (x = Γ'.Cons)) k) (unrev q)))
/-- The program that evaluates code `c` with continuation `k`. This expects an initial state where
`tr_list v` is on `main`, `tr_cont_stack k` is on `stack`, and `aux` and `rev` are empty.
See the section documentation for details. -/
@[simp] def tr_normal : to_partrec.code → cont' → Λ' := sorry
/-- The main program. See the section documentation for details. -/
@[simp] def tr : Λ' → stmt' := sorry
/-- Translating a `cont` continuation to a `cont'` continuation simply entails dropping all the
data. This data is instead encoded in `tr_cont_stack` in the configuration. -/
def tr_cont : to_partrec.cont → cont' := sorry
/-- We use `pos_num` to define the translation of binary natural numbers. A natural number is
represented as a little-endian list of `bit0` and `bit1` elements:
1 = [bit1]
2 = [bit0, bit1]
3 = [bit1, bit1]
4 = [bit0, bit0, bit1]
In particular, this representation guarantees no trailing `bit0`'s at the end of the list. -/
def tr_pos_num : pos_num → List Γ' := sorry
/-- We use `num` to define the translation of binary natural numbers. Positive numbers are
translated using `tr_pos_num`, and `tr_num 0 = []`. So there are never any trailing `bit0`'s in
a translated `num`.
0 = []
1 = [bit1]
2 = [bit0, bit1]
3 = [bit1, bit1]
4 = [bit0, bit0, bit1]
-/
def tr_num : num → List Γ' := sorry
/-- Because we use binary encoding, we define `tr_nat` in terms of `tr_num`, using `num`, which are
binary natural numbers. (We could also use `nat.binary_rec_on`, but `num` and `pos_num` make for
easy inductions.) -/
def tr_nat (n : ℕ) : List Γ' := tr_num ↑n
@[simp] theorem tr_nat_zero : tr_nat 0 = [] := rfl
/-- Lists are translated with a `cons` after each encoded number.
For example:
[] = []
[0] = [cons]
[1] = [bit1, cons]
[6, 0] = [bit0, bit1, bit1, cons, cons]
-/
@[simp] def tr_list : List ℕ → List Γ' := sorry
/-- Lists of lists are translated with a `Cons` after each encoded list.
For example:
[] = []
[[]] = [Cons]
[[], []] = [Cons, Cons]
[[0]] = [cons, Cons]
[[1, 2], [0]] = [bit1, cons, bit0, bit1, cons, Cons, cons, Cons]
-/
@[simp] def tr_llist : List (List ℕ) → List Γ' := sorry
/-- The data part of a continuation is a list of lists, which is encoded on the `stack` stack
using `tr_llist`. -/
@[simp] def cont_stack : to_partrec.cont → List (List ℕ) := sorry
/-- The data part of a continuation is a list of lists, which is encoded on the `stack` stack
using `tr_llist`. -/
def tr_cont_stack (k : to_partrec.cont) : List Γ' := tr_llist (cont_stack k)
/-- This is the nondependent eliminator for `K'`, but we use it specifically here in order to
represent the stack data as four lists rather than as a function `K' → list Γ'`, because this makes
rewrites easier. The theorems `K'.elim_update_main` et. al. show how such a function is updated
after an `update` to one of the components. -/
@[simp] def K'.elim (a : List Γ') (b : List Γ') (c : List Γ') (d : List Γ') : K' → List Γ' := sorry
@[simp] theorem K'.elim_update_main {a : List Γ'} {b : List Γ'} {c : List Γ'} {d : List Γ'}
{a' : List Γ'} : function.update (K'.elim a b c d) K'.main a' = K'.elim a' b c d :=
sorry
@[simp] theorem K'.elim_update_rev {a : List Γ'} {b : List Γ'} {c : List Γ'} {d : List Γ'}
{b' : List Γ'} : function.update (K'.elim a b c d) K'.rev b' = K'.elim a b' c d :=
sorry
@[simp] theorem K'.elim_update_aux {a : List Γ'} {b : List Γ'} {c : List Γ'} {d : List Γ'}
{c' : List Γ'} : function.update (K'.elim a b c d) K'.aux c' = K'.elim a b c' d :=
sorry
@[simp] theorem K'.elim_update_stack {a : List Γ'} {b : List Γ'} {c : List Γ'} {d : List Γ'}
{d' : List Γ'} : function.update (K'.elim a b c d) K'.stack d' = K'.elim a b c d' :=
sorry
/-- The halting state corresponding to a `list ℕ` output value. -/
def halt (v : List ℕ) : cfg' := TM2.cfg.mk none none (K'.elim (tr_list v) [] [] [])
/-- The `cfg` states map to `cfg'` states almost one to one, except that in normal operation the
local store contains an arbitrary garbage value. To make the final theorem cleaner we explicitly
clear it in the halt state so that there is exactly one configuration corresponding to output `v`.
-/
def tr_cfg : to_partrec.cfg → cfg' → Prop := sorry
/-- This could be a general list definition, but it is also somewhat specialized to this
application. `split_at_pred p L` will search `L` for the first element satisfying `p`.
If it is found, say `L = l₁ ++ a :: l₂` where `a` satisfies `p` but `l₁` does not, then it returns
`(l₁, some a, l₂)`. Otherwise, if there is no such element, it returns `(L, none, [])`. -/
def split_at_pred {α : Type u_1} (p : α → Bool) : List α → List α × Option α × List α := sorry
theorem split_at_pred_eq {α : Type u_1} (p : α → Bool) (L : List α) (l₁ : List α) (o : Option α)
(l₂ : List α) :
(∀ (x : α), x ∈ l₁ → p x = false) →
(option.elim o (L = l₁ ∧ l₂ = []) fun (a : α) => p a = tt ∧ L = l₁ ++ a :: l₂) →
split_at_pred p L = (l₁, o, l₂) :=
sorry
theorem split_at_pred_ff {α : Type u_1} (L : List α) :
split_at_pred (fun (_x : α) => false) L = (L, none, []) :=
split_at_pred_eq (fun (_x : α) => false) L L none [] (fun (_x : α) (_x : _x ∈ L) => rfl)
{ left := rfl, right := rfl }
theorem move_ok {p : Γ' → Bool} {k₁ : K'} {k₂ : K'} {q : Λ'} {s : Option Γ'} {L₁ : List Γ'}
{o : Option Γ'} {L₂ : List Γ'} {S : K' → List Γ'} (h₁ : k₁ ≠ k₂)
(e : split_at_pred p (S k₁) = (L₁, o, L₂)) :
reaches₁ (TM2.step tr) (TM2.cfg.mk (some (Λ'.move p k₁ k₂ q)) s S)
(TM2.cfg.mk (some q) o
(function.update (function.update S k₁ L₂) k₂ (list.reverse_core L₁ (S k₂)))) :=
sorry
theorem unrev_ok {q : Λ'} {s : Option Γ'} {S : K' → List Γ'} :
reaches₁ (TM2.step tr) (TM2.cfg.mk (some (unrev q)) s S)
(TM2.cfg.mk (some q) none
(function.update (function.update S K'.rev []) K'.main
(list.reverse_core (S K'.rev) (S K'.main)))) :=
move_ok (of_as_true trivial) (split_at_pred_ff (S K'.rev))
theorem move₂_ok {p : Γ' → Bool} {k₁ : K'} {k₂ : K'} {q : Λ'} {s : Option Γ'} {L₁ : List Γ'}
{o : Option Γ'} {L₂ : List Γ'} {S : K' → List Γ'} (h₁ : k₁ ≠ K'.rev ∧ k₂ ≠ K'.rev ∧ k₁ ≠ k₂)
(h₂ : S K'.rev = []) (e : split_at_pred p (S k₁) = (L₁, o, L₂)) :
reaches₁ (TM2.step tr) (TM2.cfg.mk (some (move₂ p k₁ k₂ q)) s S)
(TM2.cfg.mk (some q) none
(function.update (function.update S k₁ (option.elim o id List.cons L₂)) k₂
(L₁ ++ S k₂))) :=
sorry
theorem clear_ok {p : Γ' → Bool} {k : K'} {q : Λ'} {s : Option Γ'} {L₁ : List Γ'} {o : Option Γ'}
{L₂ : List Γ'} {S : K' → List Γ'} (e : split_at_pred p (S k) = (L₁, o, L₂)) :
reaches₁ (TM2.step tr) (TM2.cfg.mk (some (Λ'.clear p k q)) s S)
(TM2.cfg.mk (some q) o (function.update S k L₂)) :=
sorry
theorem copy_ok (q : Λ') (s : Option Γ') (a : List Γ') (b : List Γ') (c : List Γ') (d : List Γ') :
reaches₁ (TM2.step tr) (TM2.cfg.mk (some (Λ'.copy q)) s (K'.elim a b c d))
(TM2.cfg.mk (some q) none (K'.elim (list.reverse_core b a) [] c (list.reverse_core b d))) :=
sorry
theorem tr_pos_num_nat_end (n : pos_num) (x : Γ') (H : x ∈ tr_pos_num n) : nat_end x = false :=
sorry
theorem tr_num_nat_end (n : num) (x : Γ') (H : x ∈ tr_num n) : nat_end x = false := sorry
theorem tr_nat_nat_end (n : ℕ) (x : Γ') (H : x ∈ tr_nat n) : nat_end x = false := tr_num_nat_end ↑n
theorem tr_list_ne_Cons (l : List ℕ) (x : Γ') (H : x ∈ tr_list l) : x ≠ Γ'.Cons := sorry
theorem head_main_ok {q : Λ'} {s : Option Γ'} {L : List ℕ} {c : List Γ'} {d : List Γ'} :
reaches₁ (TM2.step tr) (TM2.cfg.mk (some (head K'.main q)) s (K'.elim (tr_list L) [] c d))
(TM2.cfg.mk (some q) none (K'.elim (tr_list [list.head L]) [] c d)) :=
sorry
theorem head_stack_ok {q : Λ'} {s : Option Γ'} {L₁ : List ℕ} {L₂ : List ℕ} {L₃ : List Γ'} :
reaches₁ (TM2.step tr)
(TM2.cfg.mk (some (head K'.stack q)) s
(K'.elim (tr_list L₁) [] [] (tr_list L₂ ++ Γ'.Cons :: L₃)))
(TM2.cfg.mk (some q) none (K'.elim (tr_list (list.head L₂ :: L₁)) [] [] L₃)) :=
sorry
theorem succ_ok {q : Λ'} {s : Option Γ'} {n : ℕ} {c : List Γ'} {d : List Γ'} :
reaches₁ (TM2.step tr) (TM2.cfg.mk (some (Λ'.succ q)) s (K'.elim (tr_list [n]) [] c d))
(TM2.cfg.mk (some q) none (K'.elim (tr_list [Nat.succ n]) [] c d)) :=
sorry
theorem pred_ok (q₁ : Λ') (q₂ : Λ') (s : Option Γ') (v : List ℕ) (c : List Γ') (d : List Γ') :
∃ (s' : Option Γ'),
reaches₁ (TM2.step tr) (TM2.cfg.mk (some (Λ'.pred q₁ q₂)) s (K'.elim (tr_list v) [] c d))
(nat.elim (TM2.cfg.mk (some q₁) s' (K'.elim (tr_list (list.tail v)) [] c d))
(fun (n : ℕ) (_x : TM2.cfg (fun (_x : K') => Γ') Λ' (Option Γ')) =>
TM2.cfg.mk (some q₂) s' (K'.elim (tr_list (n :: list.tail v)) [] c d))
(list.head v)) :=
sorry
theorem tr_normal_respects (c : to_partrec.code) (k : to_partrec.cont) (v : List ℕ)
(s : Option Γ') :
∃ (b₂ : cfg'),
tr_cfg (to_partrec.step_normal c k v) b₂ ∧
reaches₁ (TM2.step tr)
(TM2.cfg.mk (some (tr_normal c (tr_cont k))) s
(K'.elim (tr_list v) [] [] (tr_cont_stack k)))
b₂ :=
sorry
theorem tr_ret_respects (k : to_partrec.cont) (v : List ℕ) (s : Option Γ') :
∃ (b₂ : cfg'),
tr_cfg (to_partrec.step_ret k v) b₂ ∧
reaches₁ (TM2.step tr)
(TM2.cfg.mk (some (Λ'.ret (tr_cont k))) s (K'.elim (tr_list v) [] [] (tr_cont_stack k)))
b₂ :=
sorry
theorem tr_respects : respects to_partrec.step (TM2.step tr) tr_cfg := sorry
/-- The initial state, evaluating function `c` on input `v`. -/
def init (c : to_partrec.code) (v : List ℕ) : cfg' :=
TM2.cfg.mk (some (tr_normal c cont'.halt)) none (K'.elim (tr_list v) [] [] [])
theorem tr_init (c : to_partrec.code) (v : List ℕ) :
∃ (b : cfg'),
tr_cfg (to_partrec.step_normal c to_partrec.cont.halt v) b ∧
reaches₁ (TM2.step tr) (init c v) b :=
tr_normal_respects c to_partrec.cont.halt v none
theorem tr_eval (c : to_partrec.code) (v : List ℕ) :
eval (TM2.step tr) (init c v) = halt <$> to_partrec.code.eval c v :=
sorry
end Mathlib |
916487a9d8889b7c955dde1dd4a7393136af30b8 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/elabissues/typeclass_nested_validate.lean | 3005ce077aa1d842822e19ec9ee44bcc2e726851 | [
"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 | 2,947 | lean | /-
This example demonstrates a case where Lean4's tabled typeclass resolution may loop.
It also suggests a workaround, new instance binder semantics, new syntax support, and a new instance validation rule.
-/
#exit
class Field (K : Type) := (u : Unit)
class VectorSpace (K : Type) [Field K] (E : Type) := (u : Unit)
instance VectorSpaceSelf (K : Type) [Field K] : VectorSpace K K := {u:=()}
class CompleteSpace (α : Type) := (u : Unit)
def AlgebraicClosure (K : Type) [Field K] : Type := K
/-
Note that this instance is not a problem when `K` is known,
because it will only ever "unpack" AlgebraicClosures, not create new ones
However, if `K` is ever not known, it will create them ad infinitum!
-/
instance AlgebraicClosure.Field (K : Type) [Field K] : Field (AlgebraicClosure K) := {u:=()}
/-
Here is the "bad" instance one may be tempted to write:
<<
instance bad (K E : Type) [Field K] [VectorSpace K E] [CompleteSpace K] : CompleteSpace E := {u := ()}
>>
It is bad because typeclass resolution will try to find `Field ?K` before it knows what `?K` is,
which in conjunction with the instance `AlgebraicClosure.Field`, will cause resolution to diverge.
Here is the workaround, which is very ugly:
<<
instance veryUgly (K E : Type) {fK : Field K} [@VectorSpace K fK E] [CompleteSpace K] : CompleteSpace E := {u := ()}
>>
Here, `Field ?K` is not solved by typeclass resolution, and instead `VectorSpace ?K ?fK E` will be solved first instead.
With the Lean3 instance semantics, one could make this less ugly by writing
-/
instance ugly (K E : Type) {_ : Field K} [VectorSpace K E] [CompleteSpace K] : CompleteSpace E := {u := ()}
/-
This would work because the `Field K` instance would still be considered for typeclass resolution even though it was not in a `[]` binder.
However, the original plan for Lean4 was that one would only consider `[]` binders for typeclass resolution.
We suggest that we revert to the Lean3 protocol instead, and consider any local variable with class type as a candidate instance.
Finally, this instance could be made reasonable by allowing `{}` binders without names:
<<
instance reasonable (K E : Type) {Field K} [VectorSpace K E] [CompleteSpace K] : CompleteSpace E := {u := ()} -- should work in Lean4
>>
-/
axiom K : Type
instance K_Field : Field K := {u:=()}
#synth CompleteSpace K -- should fail quickly (and in particular, not run forever)
/-
The bad instance above could trigger a validation warning:
<<
instance bad (K E : Type) [Field K] [VectorSpace K E] [CompleteSpace K] : CompleteSpace E := {u := ()}
>>
-- Warning: argument #3 is a class that occurs in downstream arguments and not the return type.
-- You may want to replace [Field K] with {Field K} so typeclass resolution infers this instance after solving downstream instances.
-/
/-
The syntax `[Field K]` is ambiguous. It can be interpreted also as {Field K : _}. Note that we use this version all the time (e.g., {α β}).
-/
|
8f4d5e40ca607f956d46286ea99b2adb526ae8e7 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/measure_theory/integral/divergence_theorem.lean | 8219c2b6bc01b75cb81f17865b5e6e8a89738977 | [
"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 | 30,215 | 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.divergence_theorem
import analysis.box_integral.integrability
import measure_theory.integral.interval_integral
/-!
# Divergence theorem for Bochner integral
In this file we prove the Divergence theorem for Bochner integral on a box in
`ℝⁿ⁺¹ = fin (n + 1) → ℝ`. More precisely, we prove the following theorem.
Let `E` be a complete normed space. If `f : ℝⁿ⁺¹ → Eⁿ⁺¹` is
continuous on a rectangular box `[a, b] : set ℝⁿ⁺¹`, `a ≤ b`, differentiable on its interior with
derivative `f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹`, and the divergence `λ x, ∑ i, f' x eᵢ i` is integrable on
`[a, b]`, where `eᵢ = pi.single i 1` is the `i`-th basis vector, then its integral is equal to the
sum of integrals of `f` over the faces of `[a, b]`, taken with appropriate signs. Moreover, the same
is true if the function is not differentiable at countably many points of the interior of `[a, b]`.
Once we prove the general theorem, we deduce corollaries for functions `ℝ → E` and pairs of
functions `(ℝ × ℝ) → E`.
## Notations
We use the following local notation to make the statement more readable. Note that the documentation
website shows the actual terms, not those abbreviated using local notations.
* `ℝⁿ`, `ℝⁿ⁺¹`, `Eⁿ⁺¹`: `fin n → ℝ`, `fin (n + 1) → ℝ`, `fin (n + 1) → E`;
* `face i`: the `i`-th face of the box `[a, b]` as a closed segment in `ℝⁿ`, namely `[a ∘
fin.succ_above i, b ∘ fin.succ_above i]`;
* `e i` : `i`-th basis vector `pi.single i 1`;
* `front_face i`, `back_face i`: embeddings `ℝⁿ → ℝⁿ⁺¹` corresponding to the front face
`{x | x i = b i}` and back face `{x | x i = a i}` of the box `[a, b]`, respectively.
They are given by `fin.insert_nth i (b i)` and `fin.insert_nth i (a i)`.
## TODO
* Add a version that assumes existence and integrability of partial derivatives.
## Tags
divergence theorem, Bochner integral
-/
open set finset topological_space function box_integral measure_theory filter
open_locale big_operators classical topological_space interval
universes u
namespace measure_theory
variables {E : Type u} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E]
section
variables {n : ℕ}
local notation `ℝⁿ` := fin n → ℝ
local notation `ℝⁿ⁺¹` := fin (n + 1) → ℝ
local notation `Eⁿ⁺¹` := fin (n + 1) → E
local notation `e ` i := pi.single i 1
section
/-!
### Divergence theorem for functions on `ℝⁿ⁺¹ = fin (n + 1) → ℝ`.
In this section we use the divergence theorem for a Henstock-Kurzweil-like integral
`box_integral.has_integral_GP_divergence_of_forall_has_deriv_within_at` to prove the divergence
theorem for Bochner integral. The divergence theorem for Bochner integral
`measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable` assumes that the function
itself is continuous on a closed box, differentiable at all but countably many points of its
interior, and the divergence is integrable on the box.
This statement differs from `box_integral.has_integral_GP_divergence_of_forall_has_deriv_within_at`
in several aspects.
* We use Bochner integral instead of a Henstock-Kurzweil integral. This modification is done in
`measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable_aux₁`. As a side effect
of this change, we need to assume that the divergence is integrable.
* We don't assume differentiability on the boundary of the box. This modification is done in
`measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable_aux₂`. To prove it, we
choose an increasing sequence of smaller boxes that cover the interior of the original box, then
apply the previous lemma to these smaller boxes and take the limit of both sides of the equation.
* We assume `a ≤ b` instead of `∀ i, a i < b i`. This is the last step of the proof, and it is done
in the main theorem `measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable`.
-/
/-- An auxiliary lemma for
`measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable`. This is exactly
`box_integral.has_integral_GP_divergence_of_forall_has_deriv_within_at` reformulated for the
Bochner integral. -/
lemma integral_divergence_of_has_fderiv_within_at_off_countable_aux₁ (I : box (fin (n + 1)))
(f : ℝⁿ⁺¹ → Eⁿ⁺¹) (f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹) (s : set ℝⁿ⁺¹) (hs : s.countable)
(Hc : continuous_on f I.Icc) (Hd : ∀ x ∈ I.Icc \ s, has_fderiv_within_at f (f' x) I.Icc x)
(Hi : integrable_on (λ x, ∑ i, f' x (e i) i) I.Icc) :
∫ x in I.Icc, ∑ i, f' x (e i) i =
∑ i : fin (n + 1),
((∫ x in (I.face i).Icc, f (i.insert_nth (I.upper i) x) i) -
∫ x in (I.face i).Icc, f (i.insert_nth (I.lower i) x) i) :=
begin
simp only [← set_integral_congr_set_ae (box.coe_ae_eq_Icc _)],
have A := ((Hi.mono_set box.coe_subset_Icc).has_box_integral ⊥ rfl),
have B := has_integral_GP_divergence_of_forall_has_deriv_within_at I f f' (s ∩ I.Icc)
(hs.mono (inter_subset_left _ _)) (λ x hx, Hc _ hx.2)
(λ x hx, Hd _ ⟨hx.1, λ h, hx.2 ⟨h, hx.1⟩⟩),
rw continuous_on_pi at Hc,
refine (A.unique B).trans (sum_congr rfl $ λ i hi, _),
refine congr_arg2 has_sub.sub _ _,
{ have := box.continuous_on_face_Icc (Hc i) (set.right_mem_Icc.2 (I.lower_le_upper i)),
have := (this.integrable_on_compact (box.is_compact_Icc _)).mono_set box.coe_subset_Icc,
exact (this.has_box_integral ⊥ rfl).integral_eq, apply_instance },
{ have := box.continuous_on_face_Icc (Hc i) (set.left_mem_Icc.2 (I.lower_le_upper i)),
have := (this.integrable_on_compact (box.is_compact_Icc _)).mono_set box.coe_subset_Icc,
exact (this.has_box_integral ⊥ rfl).integral_eq, apply_instance }
end
/-- An auxiliary lemma for
`measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable`. Compared to the previous
lemma, here we drop the assumption of differentiability on the boundary of the box. -/
lemma integral_divergence_of_has_fderiv_within_at_off_countable_aux₂ (I : box (fin (n + 1)))
(f : ℝⁿ⁺¹ → Eⁿ⁺¹) (f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹) (s : set ℝⁿ⁺¹) (hs : s.countable)
(Hc : continuous_on f I.Icc) (Hd : ∀ x ∈ I.Ioo \ s, has_fderiv_at f (f' x) x)
(Hi : integrable_on (λ x, ∑ i, f' x (e i) i) I.Icc) :
∫ x in I.Icc, ∑ i, f' x (e i) i =
∑ i : fin (n + 1),
((∫ x in (I.face i).Icc, f (i.insert_nth (I.upper i) x) i) -
∫ x in (I.face i).Icc, f (i.insert_nth (I.lower i) x) i) :=
begin
/- Choose a monotone sequence `J k` of subboxes that cover the interior of `I` and prove that
these boxes satisfy the assumptions of the previous lemma. -/
rcases I.exists_seq_mono_tendsto with ⟨J, hJ_sub, hJl, hJu⟩,
have hJ_sub' : ∀ k, (J k).Icc ⊆ I.Icc, from λ k, (hJ_sub k).trans I.Ioo_subset_Icc,
have hJ_le : ∀ k, J k ≤ I, from λ k, box.le_iff_Icc.2 (hJ_sub' k),
have HcJ : ∀ k, continuous_on f (J k).Icc, from λ k, Hc.mono (hJ_sub' k),
have HdJ : ∀ k (x ∈ (J k).Icc \ s), has_fderiv_within_at f (f' x) (J k).Icc x,
from λ k x hx, (Hd x ⟨hJ_sub k hx.1, hx.2⟩).has_fderiv_within_at,
have HiJ : ∀ k, integrable_on (λ x, ∑ i, f' x (e i) i) (J k).Icc,
from λ k, Hi.mono_set (hJ_sub' k),
-- Apply the previous lemma to `J k`.
have HJ_eq := λ k, integral_divergence_of_has_fderiv_within_at_off_countable_aux₁ (J k) f f' s hs
(HcJ k) (HdJ k) (HiJ k),
/- Note that the LHS of `HJ_eq k` tends to the LHS of the goal as `k → ∞`. -/
have hI_tendsto : tendsto (λ k, ∫ x in (J k).Icc, ∑ i, f' x (e i) i) at_top
(𝓝 (∫ x in I.Icc, ∑ i, f' x (e i) i)),
{ simp only [integrable_on, ← measure.restrict_congr_set (box.Ioo_ae_eq_Icc _)] at Hi ⊢,
rw ← box.Union_Ioo_of_tendsto J.monotone hJl hJu at Hi ⊢,
exact tendsto_set_integral_of_monotone (λ k, (J k).measurable_set_Ioo)
(box.Ioo.comp J).monotone Hi },
/- Thus it suffices to prove the same about the RHS. -/
refine tendsto_nhds_unique_of_eventually_eq hI_tendsto _ (eventually_of_forall HJ_eq),
clear hI_tendsto,
rw tendsto_pi_nhds at hJl hJu,
/- We'll need to prove a similar statement about the integrals over the front sides and the
integrals over the back sides. In order to avoid repeating ourselves, we formulate a lemma. -/
suffices : ∀ (i : fin (n + 1)) (c : ℕ → ℝ) d,
(∀ k, c k ∈ Icc (I.lower i) (I.upper i)) → tendsto c at_top (𝓝 d) →
tendsto (λ k, ∫ x in ((J k).face i).Icc, f (i.insert_nth (c k) x) i) at_top
(𝓝 $ ∫ x in (I.face i).Icc, f (i.insert_nth d x) i),
{ rw box.Icc_eq_pi at hJ_sub',
refine tendsto_finset_sum _ (λ i hi, (this _ _ _ _ (hJu _)).sub (this _ _ _ _ (hJl _))),
exacts [λ k, hJ_sub' k (J k).upper_mem_Icc _ trivial,
λ k, hJ_sub' k (J k).lower_mem_Icc _ trivial] },
intros i c d hc hcd,
/- First we prove that the integrals of the restriction of `f` to `{x | x i = d}` over increasing
boxes `((J k).face i).Icc` tend to the desired limit. The proof mostly repeats the one above. -/
have hd : d ∈ Icc (I.lower i) (I.upper i),
from is_closed_Icc.mem_of_tendsto hcd (eventually_of_forall hc),
have Hic : ∀ k, integrable_on (λ x, f (i.insert_nth (c k) x) i) (I.face i).Icc,
from λ k, (box.continuous_on_face_Icc ((continuous_apply i).comp_continuous_on Hc)
(hc k)).integrable_on_Icc,
have Hid : integrable_on (λ x, f (i.insert_nth d x) i) (I.face i).Icc,
from (box.continuous_on_face_Icc ((continuous_apply i).comp_continuous_on Hc)
hd).integrable_on_Icc,
have H : tendsto (λ k, ∫ x in ((J k).face i).Icc, f (i.insert_nth d x) i) at_top
(𝓝 $ ∫ x in (I.face i).Icc, f (i.insert_nth d x) i),
{ have hIoo : (⋃ k, ((J k).face i).Ioo) = (I.face i).Ioo,
from box.Union_Ioo_of_tendsto ((box.monotone_face i).comp J.monotone)
(tendsto_pi_nhds.2 (λ _, hJl _)) (tendsto_pi_nhds.2 (λ _, hJu _)),
simp only [integrable_on, ← measure.restrict_congr_set (box.Ioo_ae_eq_Icc _), ← hIoo] at Hid ⊢,
exact tendsto_set_integral_of_monotone (λ k, ((J k).face i).measurable_set_Ioo)
(box.Ioo.monotone.comp ((box.monotone_face i).comp J.monotone)) Hid },
/- Thus it suffices to show that the distance between the integrals of the restrictions of `f` to
`{x | x i = c k}` and `{x | x i = d}` over `((J k).face i).Icc` tends to zero as `k → ∞`. Choose
`ε > 0`. -/
refine H.congr_dist (metric.nhds_basis_closed_ball.tendsto_right_iff.2 (λ ε εpos, _)),
have hvol_pos : ∀ J : box (fin n), 0 < ∏ j, (J.upper j - J.lower j),
from λ J, (prod_pos $ λ j hj, sub_pos.2 $ J.lower_lt_upper _),
/- Choose `δ > 0` such that for any `x y ∈ I.Icc` at distance at most `δ`, the distance between
`f x` and `f y` is at most `ε / volume (I.face i).Icc`, then the distance between the integrals
is at most `(ε / volume (I.face i).Icc) * volume ((J k).face i).Icc ≤ ε`. -/
rcases metric.uniform_continuous_on_iff_le.1
(I.is_compact_Icc.uniform_continuous_on_of_continuous Hc)
(ε / ∏ j, ((I.face i).upper j - (I.face i).lower j)) (div_pos εpos (hvol_pos (I.face i)))
with ⟨δ, δpos, hδ⟩,
refine (hcd.eventually (metric.ball_mem_nhds _ δpos)).mono (λ k hk, _),
have Hsub : ((J k).face i).Icc ⊆ (I.face i).Icc,
from box.le_iff_Icc.1 (box.face_mono (hJ_le _) i),
rw [mem_closed_ball_zero_iff, real.norm_eq_abs, abs_of_nonneg dist_nonneg,
dist_eq_norm, ← integral_sub (Hid.mono_set Hsub) ((Hic _).mono_set Hsub)],
calc ‖(∫ x in ((J k).face i).Icc, f (i.insert_nth d x) i - f (i.insert_nth (c k) x) i)‖
≤ (ε / ∏ j, ((I.face i).upper j - (I.face i).lower j)) * (volume ((J k).face i).Icc).to_real :
begin
refine norm_set_integral_le_of_norm_le_const' (((J k).face i).measure_Icc_lt_top _)
((J k).face i).measurable_set_Icc (λ x hx, _),
rw ← dist_eq_norm,
calc dist (f (i.insert_nth d x) i) (f (i.insert_nth (c k) x) i)
≤ dist (f (i.insert_nth d x)) (f (i.insert_nth (c k) x)) :
dist_le_pi_dist (f (i.insert_nth d x)) (f (i.insert_nth (c k) x)) i
... ≤ (ε / ∏ j, ((I.face i).upper j - (I.face i).lower j)) :
hδ _ (I.maps_to_insert_nth_face_Icc hd $ Hsub hx)
_ (I.maps_to_insert_nth_face_Icc (hc _) $ Hsub hx) _,
rw [fin.dist_insert_nth_insert_nth, dist_self, dist_comm],
exact max_le hk.le δpos.lt.le
end
... ≤ ε :
begin
rw [box.Icc_def, real.volume_Icc_pi_to_real ((J k).face i).lower_le_upper,
← le_div_iff (hvol_pos _)],
refine div_le_div_of_le_left εpos.le (hvol_pos _) (prod_le_prod (λ j hj, _) (λ j hj, _)),
exacts [sub_nonneg.2 (box.lower_le_upper _ _),
sub_le_sub ((hJ_sub' _ (J _).upper_mem_Icc).2 _) ((hJ_sub' _ (J _).lower_mem_Icc).1 _)]
end
end
variables (a b : ℝⁿ⁺¹)
local notation `face ` i := set.Icc (a ∘ fin.succ_above i) (b ∘ fin.succ_above i)
local notation `front_face ` i:2000 := fin.insert_nth i (b i)
local notation `back_face ` i:2000 := fin.insert_nth i (a i)
/-- **Divergence theorem** for Bochner integral. If `f : ℝⁿ⁺¹ → Eⁿ⁺¹` is continuous on a rectangular
box `[a, b] : set ℝⁿ⁺¹`, `a ≤ b`, is differentiable on its interior with derivative
`f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹` and the divergence `λ x, ∑ i, f' x eᵢ i` is integrable on `[a, b]`,
where `eᵢ = pi.single i 1` is the `i`-th basis vector, then its integral is equal to the sum of
integrals of `f` over the faces of `[a, b]`, taken with appropriat signs.
Moreover, the same is true if the function is not differentiable at countably many
points of the interior of `[a, b]`.
We represent both faces `x i = a i` and `x i = b i` as the box
`face i = [a ∘ fin.succ_above i, b ∘ fin.succ_above i]` in `ℝⁿ`, where
`fin.succ_above : fin n ↪o fin (n + 1)` is the order embedding with range `{i}ᶜ`. The restrictions
of `f : ℝⁿ⁺¹ → Eⁿ⁺¹` to these faces are given by `f ∘ back_face i` and `f ∘ front_face i`, where
`back_face i = fin.insert_nth i (a i)` and `front_face i = fin.insert_nth i (b i)` are embeddings
`ℝⁿ → ℝⁿ⁺¹` that take `y : ℝⁿ` and insert `a i` (resp., `b i`) as `i`-th coordinate. -/
lemma integral_divergence_of_has_fderiv_within_at_off_countable (hle : a ≤ b) (f : ℝⁿ⁺¹ → Eⁿ⁺¹)
(f' : ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] Eⁿ⁺¹) (s : set ℝⁿ⁺¹) (hs : s.countable) (Hc : continuous_on f (Icc a b))
(Hd : ∀ x ∈ set.pi univ (λ i, Ioo (a i) (b i)) \ s, has_fderiv_at f (f' x) x)
(Hi : integrable_on (λ x, ∑ i, f' x (e i) i) (Icc a b)) :
∫ x in Icc a b, ∑ i, f' x (e i) i =
∑ i : fin (n + 1),
((∫ x in face i, f (front_face i x) i) - ∫ x in face i, f (back_face i x) i) :=
begin
rcases em (∃ i, a i = b i) with ⟨i, hi⟩|hne,
{ /- First we sort out the trivial case `∃ i, a i = b i`. -/
simp only [volume_pi, ← set_integral_congr_set_ae measure.univ_pi_Ioc_ae_eq_Icc],
have hi' : Ioc (a i) (b i) = ∅ := Ioc_eq_empty hi.not_lt,
have : pi set.univ (λ j, Ioc (a j) (b j)) = ∅, from univ_pi_eq_empty hi',
rw [this, integral_empty, sum_eq_zero],
rintro j -,
rcases eq_or_ne i j with rfl|hne,
{ simp [hi] },
{ rcases fin.exists_succ_above_eq hne with ⟨i, rfl⟩,
have : pi set.univ (λ k : fin n, Ioc (a $ j.succ_above k) (b $ j.succ_above k)) = ∅,
from univ_pi_eq_empty hi',
rw [this, integral_empty, integral_empty, sub_self] } },
{ /- In the non-trivial case `∀ i, a i < b i`, we apply a lemma we proved above. -/
have hlt : ∀ i, a i < b i, from λ i, (hle i).lt_of_ne (λ hi, hne ⟨i, hi⟩),
convert integral_divergence_of_has_fderiv_within_at_off_countable_aux₂ ⟨a, b, hlt⟩
f f' s hs Hc Hd Hi }
end
/-- **Divergence theorem** for a family of functions `f : fin (n + 1) → ℝⁿ⁺¹ → E`. See also
`measure_theory.integral_divergence_of_has_fderiv_within_at_off_countable'` for a version formulated
in terms of a vector-valued function `f : ℝⁿ⁺¹ → Eⁿ⁺¹`. -/
lemma integral_divergence_of_has_fderiv_within_at_off_countable' (hle : a ≤ b)
(f : fin (n + 1) → ℝⁿ⁺¹ → E) (f' : fin (n + 1) → ℝⁿ⁺¹ → ℝⁿ⁺¹ →L[ℝ] E)
(s : set ℝⁿ⁺¹) (hs : s.countable) (Hc : ∀ i, continuous_on (f i) (Icc a b))
(Hd : ∀ (x ∈ pi set.univ (λ i, Ioo (a i) (b i)) \ s) i, has_fderiv_at (f i) (f' i x) x)
(Hi : integrable_on (λ x, ∑ i, f' i x (e i)) (Icc a b)) :
∫ x in Icc a b, ∑ i, f' i x (e i) =
∑ i : fin (n + 1),
((∫ x in face i, f i (front_face i x)) - ∫ x in face i, f i (back_face i x)) :=
integral_divergence_of_has_fderiv_within_at_off_countable a b hle (λ x i, f i x)
(λ x, continuous_linear_map.pi (λ i, f' i x)) s hs
(continuous_on_pi.2 Hc) (λ x hx, has_fderiv_at_pi.2 (Hd x hx)) Hi
end
/-- An auxiliary lemma that is used to specialize the general divergence theorem to spaces that do
not have the form `fin n → ℝ`. -/
lemma integral_divergence_of_has_fderiv_within_at_off_countable_of_equiv
{F : Type*} [normed_add_comm_group F] [normed_space ℝ F] [partial_order F] [measure_space F]
[borel_space F] (eL : F ≃L[ℝ] ℝⁿ⁺¹) (he_ord : ∀ x y, eL x ≤ eL y ↔ x ≤ y)
(he_vol : measure_preserving eL volume volume) (f : fin (n + 1) → F → E)
(f' : fin (n + 1) → F → F →L[ℝ] E) (s : set F) (hs : s.countable)
(a b : F) (hle : a ≤ b) (Hc : ∀ i, continuous_on (f i) (Icc a b))
(Hd : ∀ (x ∈ interior (Icc a b) \ s) i, has_fderiv_at (f i) (f' i x) x)
(DF : F → E) (hDF : ∀ x, DF x = ∑ i, f' i x (eL.symm $ e i)) (Hi : integrable_on DF (Icc a b)) :
∫ x in Icc a b, DF x =
∑ i : fin (n + 1), ((∫ x in Icc (eL a ∘ i.succ_above) (eL b ∘ i.succ_above),
f i (eL.symm $ i.insert_nth (eL b i) x)) -
(∫ x in Icc (eL a ∘ i.succ_above) (eL b ∘ i.succ_above),
f i (eL.symm $ i.insert_nth (eL a i) x))) :=
have he_emb : measurable_embedding eL := eL.to_homeomorph.to_measurable_equiv.measurable_embedding,
have hIcc : eL ⁻¹' (Icc (eL a) (eL b)) = Icc a b,
by { ext1 x, simp only [set.mem_preimage, set.mem_Icc, he_ord] },
have hIcc' : Icc (eL a) (eL b) = eL.symm ⁻¹' (Icc a b),
by rw [← hIcc, eL.symm_preimage_preimage],
calc ∫ x in Icc a b, DF x = ∫ x in Icc a b, ∑ i, f' i x (eL.symm $ e i) : by simp only [hDF]
... = ∫ x in Icc (eL a) (eL b), ∑ i, f' i (eL.symm x) (eL.symm $ e i) :
begin
rw [← he_vol.set_integral_preimage_emb he_emb],
simp only [hIcc, eL.symm_apply_apply]
end
... = ∑ i : fin (n + 1), ((∫ x in Icc (eL a ∘ i.succ_above) (eL b ∘ i.succ_above),
f i (eL.symm $ i.insert_nth (eL b i) x)) -
(∫ x in Icc (eL a ∘ i.succ_above) (eL b ∘ i.succ_above),
f i (eL.symm $ i.insert_nth (eL a i) x))) :
begin
convert integral_divergence_of_has_fderiv_within_at_off_countable' (eL a) (eL b)
((he_ord _ _).2 hle) (λ i x, f i (eL.symm x))
(λ i x, f' i (eL.symm x) ∘L (eL.symm : ℝⁿ⁺¹ →L[ℝ] F))
(eL.symm ⁻¹' s) (hs.preimage eL.symm.injective) _ _ _,
{ exact λ i, (Hc i).comp eL.symm.continuous_on hIcc'.subset },
{ refine λ x hx i, (Hd (eL.symm x) ⟨_, hx.2⟩ i).comp x eL.symm.has_fderiv_at,
rw ← hIcc,
refine preimage_interior_subset_interior_preimage eL.continuous _,
simpa only [set.mem_preimage, eL.apply_symm_apply, ← pi_univ_Icc, interior_pi_set finite_univ,
interior_Icc] using hx.1 },
{ rw [← he_vol.integrable_on_comp_preimage he_emb, hIcc],
simp [← hDF, (∘), Hi] }
end
end
open_locale interval
open continuous_linear_map (smul_right)
local notation `ℝ¹` := fin 1 → ℝ
local notation `ℝ²` := fin 2 → ℝ
local notation `E¹` := fin 1 → E
local notation `E²` := fin 2 → E
/-- **Fundamental theorem of calculus, part 2**. This version assumes that `f` is continuous on the
interval and is differentiable off a countable set `s`.
See also
* `interval_integral.integral_eq_sub_of_has_deriv_right_of_le` for a version that only assumes right
differentiability of `f`;
* `measure_theory.integral_eq_of_has_deriv_within_at_off_countable` for a version that works both
for `a ≤ b` and `b ≤ a` at the expense of using unordered intervals instead of `set.Icc`. -/
theorem integral_eq_of_has_deriv_within_at_off_countable_of_le (f f' : ℝ → E)
{a b : ℝ} (hle : a ≤ b) {s : set ℝ} (hs : s.countable)
(Hc : continuous_on f (Icc a b)) (Hd : ∀ x ∈ Ioo a b \ s, has_deriv_at f (f' x) x)
(Hi : interval_integrable f' volume a b) :
∫ x in a..b, f' x = f b - f a :=
begin
set e : ℝ ≃L[ℝ] ℝ¹ := (continuous_linear_equiv.fun_unique (fin 1) ℝ ℝ).symm,
have e_symm : ∀ x, e.symm x = x 0 := λ x, rfl,
set F' : ℝ → ℝ →L[ℝ] E := λ x, smul_right (1 : ℝ →L[ℝ] ℝ) (f' x),
have hF' : ∀ x y, F' x y = y • f' x := λ x y, rfl,
calc ∫ x in a..b, f' x = ∫ x in Icc a b, f' x :
by simp only [interval_integral.integral_of_le hle, set_integral_congr_set_ae Ioc_ae_eq_Icc]
... = ∑ i : fin 1, ((∫ x in Icc (e a ∘ i.succ_above) (e b ∘ i.succ_above),
f (e.symm $ i.insert_nth (e b i) x)) -
(∫ x in Icc (e a ∘ i.succ_above) (e b ∘ i.succ_above),
f (e.symm $ i.insert_nth (e a i) x))) :
begin
simp only [← interior_Icc] at Hd,
refine integral_divergence_of_has_fderiv_within_at_off_countable_of_equiv e _ _
(λ _, f) (λ _, F') s hs a b hle (λ i, Hc) (λ x hx i, Hd x hx) _ _ _,
{ exact λ x y, (order_iso.fun_unique (fin 1) ℝ).symm.le_iff_le },
{ exact (volume_preserving_fun_unique (fin 1) ℝ).symm _ },
{ intro x, rw [fin.sum_univ_one, hF', e_symm, pi.single_eq_same, one_smul] },
{ rw [interval_integrable_iff_integrable_Ioc_of_le hle] at Hi,
exact Hi.congr_set_ae Ioc_ae_eq_Icc.symm }
end
... = f b - f a :
begin
simp only [fin.sum_univ_one, e_symm],
have : ∀ (c : ℝ), const (fin 0) c = is_empty_elim := λ c, subsingleton.elim _ _,
simp [this, volume_pi, measure.pi_of_empty (λ _ : fin 0, volume)]
end
end
/-- **Fundamental theorem of calculus, part 2**. This version assumes that `f` is continuous on the
interval and is differentiable off a countable set `s`.
See also `measure_theory.interval_integral.integral_eq_sub_of_has_deriv_right` for a version that
only assumes right differentiability of `f`.
-/
theorem integral_eq_of_has_deriv_within_at_off_countable (f f' : ℝ → E) {a b : ℝ} {s : set ℝ}
(hs : s.countable) (Hc : continuous_on f [a, b])
(Hd : ∀ x ∈ Ioo (min a b) (max a b) \ s, has_deriv_at f (f' x) x)
(Hi : interval_integrable f' volume a b) :
∫ x in a..b, f' x = f b - f a :=
begin
cases le_total a b with hab hab,
{ simp only [interval_of_le hab, min_eq_left hab, max_eq_right hab] at *,
exact integral_eq_of_has_deriv_within_at_off_countable_of_le f f' hab hs Hc Hd Hi },
{ simp only [interval_of_ge hab, min_eq_right hab, max_eq_left hab] at *,
rw [interval_integral.integral_symm, neg_eq_iff_neg_eq, neg_sub, eq_comm],
exact integral_eq_of_has_deriv_within_at_off_countable_of_le f f' hab hs Hc Hd Hi.symm }
end
/-- **Divergence theorem** for functions on the plane along rectangles. It is formulated in terms of
two functions `f g : ℝ × ℝ → E` and an integral over `Icc a b = [a.1, b.1] × [a.2, b.2]`, where
`a b : ℝ × ℝ`, `a ≤ b`. When thinking of `f` and `g` as the two coordinates of a single function
`F : ℝ × ℝ → E × E` and when `E = ℝ`, this is the usual statement that the integral of the
divergence of `F` inside the rectangle equals the integral of the normal derivative of `F` along the
boundary.
See also `measure_theory.integral2_divergence_prod_of_has_fderiv_within_at_off_countable` for a
version that does not assume `a ≤ b` and uses iterated interval integral instead of the integral
over `Icc a b`. -/
lemma integral_divergence_prod_Icc_of_has_fderiv_within_at_off_countable_of_le (f g : ℝ × ℝ → E)
(f' g' : ℝ × ℝ → ℝ × ℝ →L[ℝ] E) (a b : ℝ × ℝ) (hle : a ≤ b) (s : set (ℝ × ℝ)) (hs : s.countable)
(Hcf : continuous_on f (Icc a b)) (Hcg : continuous_on g (Icc a b))
(Hdf : ∀ x ∈ Ioo a.1 b.1 ×ˢ Ioo a.2 b.2 \ s, has_fderiv_at f (f' x) x)
(Hdg : ∀ x ∈ Ioo a.1 b.1 ×ˢ Ioo a.2 b.2 \ s, has_fderiv_at g (g' x) x)
(Hi : integrable_on (λ x, f' x (1, 0) + g' x (0, 1)) (Icc a b)) :
∫ x in Icc a b, f' x (1, 0) + g' x (0, 1) =
(∫ x in a.1..b.1, g (x, b.2)) - (∫ x in a.1..b.1, g (x, a.2)) +
(∫ y in a.2..b.2, f (b.1, y)) - ∫ y in a.2..b.2, f (a.1, y) :=
let e : (ℝ × ℝ) ≃L[ℝ] ℝ² := (continuous_linear_equiv.fin_two_arrow ℝ ℝ).symm in
calc ∫ x in Icc a b, f' x (1, 0) + g' x (0, 1)
= ∑ i : fin 2, ((∫ x in Icc (e a ∘ i.succ_above) (e b ∘ i.succ_above),
![f, g] i (e.symm $ i.insert_nth (e b i) x)) -
(∫ x in Icc (e a ∘ i.succ_above) (e b ∘ i.succ_above),
![f, g] i (e.symm $ i.insert_nth (e a i) x))) :
begin
refine integral_divergence_of_has_fderiv_within_at_off_countable_of_equiv e _ _
![f, g] ![f', g'] s hs a b hle _ (λ x hx, _) _ _ Hi,
{ exact λ x y, (order_iso.fin_two_arrow_iso ℝ).symm.le_iff_le },
{ exact (volume_preserving_fin_two_arrow ℝ).symm _ },
{ exact fin.forall_fin_two.2 ⟨Hcf, Hcg⟩ },
{ rw [Icc_prod_eq, interior_prod_eq, interior_Icc, interior_Icc] at hx,
exact fin.forall_fin_two.2 ⟨Hdf x hx, Hdg x hx⟩ },
{ intro x, rw fin.sum_univ_two, simp }
end
... = (∫ y in Icc a.2 b.2, f (b.1, y)) - (∫ y in Icc a.2 b.2, f (a.1, y)) +
((∫ x in Icc a.1 b.1, g (x, b.2)) - ∫ x in Icc a.1 b.1, g (x, a.2)) :
begin
have : ∀ (a b : ℝ¹) (f : ℝ¹ → E), ∫ x in Icc a b, f x = ∫ x in Icc (a 0) (b 0), f (λ _, x),
{ intros a b f,
convert (((volume_preserving_fun_unique (fin 1) ℝ).symm _).set_integral_preimage_emb
(measurable_equiv.measurable_embedding _) _ _).symm,
exact ((order_iso.fun_unique (fin 1) ℝ).symm.preimage_Icc a b).symm },
simp only [fin.sum_univ_two, this],
refl
end
... = (∫ x in a.1..b.1, g (x, b.2)) - (∫ x in a.1..b.1, g (x, a.2)) +
(∫ y in a.2..b.2, f (b.1, y)) - ∫ y in a.2..b.2, f (a.1, y) :
begin
simp only [interval_integral.integral_of_le hle.1, interval_integral.integral_of_le hle.2,
set_integral_congr_set_ae Ioc_ae_eq_Icc],
abel
end
/-- **Divergence theorem** for functions on the plane. It is formulated in terms of two functions
`f g : ℝ × ℝ → E` and iterated integral `∫ x in a₁..b₁, ∫ y in a₂..b₂, _`, where
`a₁ a₂ b₁ b₂ : ℝ`. When thinking of `f` and `g` as the two coordinates of a single function
`F : ℝ × ℝ → E × E` and when `E = ℝ`, this is the usual statement that the integral of the
divergence of `F` inside the rectangle with vertices `(aᵢ, bⱼ)`, `i, j =1,2`, equals the integral of
the normal derivative of `F` along the boundary.
See also `measure_theory.integral_divergence_prod_Icc_of_has_fderiv_within_at_off_countable_of_le`
for a version that uses an integral over `Icc a b`, where `a b : ℝ × ℝ`, `a ≤ b`. -/
lemma integral2_divergence_prod_of_has_fderiv_within_at_off_countable (f g : ℝ × ℝ → E)
(f' g' : ℝ × ℝ → ℝ × ℝ →L[ℝ] E) (a₁ a₂ b₁ b₂ : ℝ) (s : set (ℝ × ℝ)) (hs : s.countable)
(Hcf : continuous_on f ([a₁, b₁] ×ˢ [a₂, b₂])) (Hcg : continuous_on g ([a₁, b₁] ×ˢ [a₂, b₂]))
(Hdf : ∀ x ∈ Ioo (min a₁ b₁) (max a₁ b₁) ×ˢ Ioo (min a₂ b₂) (max a₂ b₂) \ s,
has_fderiv_at f (f' x) x)
(Hdg : ∀ x ∈ Ioo (min a₁ b₁) (max a₁ b₁) ×ˢ Ioo (min a₂ b₂) (max a₂ b₂) \ s,
has_fderiv_at g (g' x) x)
(Hi : integrable_on (λ x, f' x (1, 0) + g' x (0, 1)) ([a₁, b₁] ×ˢ [a₂, b₂])) :
∫ x in a₁..b₁, ∫ y in a₂..b₂, f' (x, y) (1, 0) + g' (x, y) (0, 1) =
(∫ x in a₁..b₁, g (x, b₂)) - (∫ x in a₁..b₁, g (x, a₂)) +
(∫ y in a₂..b₂, f (b₁, y)) - ∫ y in a₂..b₂, f (a₁, y) :=
begin
wlog h₁ : a₁ ≤ b₁ := le_total a₁ b₁ using [a₁ b₁, b₁ a₁] tactic.skip,
wlog h₂ : a₂ ≤ b₂ := le_total a₂ b₂ using [a₂ b₂, b₂ a₂] tactic.skip,
{ simp only [interval_of_le h₁, interval_of_le h₂, min_eq_left, max_eq_right, h₁, h₂]
at Hcf Hcg Hdf Hdg Hi,
calc ∫ x in a₁..b₁, ∫ y in a₂..b₂, f' (x, y) (1, 0) + g' (x, y) (0, 1)
= ∫ x in Icc a₁ b₁, ∫ y in Icc a₂ b₂, f' (x, y) (1, 0) + g' (x, y) (0, 1) :
by simp only [interval_integral.integral_of_le, h₁, h₂,
set_integral_congr_set_ae Ioc_ae_eq_Icc]
... = ∫ x in Icc a₁ b₁ ×ˢ Icc a₂ b₂, f' x (1, 0) + g' x (0, 1) :
(set_integral_prod _ Hi).symm
... = (∫ x in a₁..b₁, g (x, b₂)) - (∫ x in a₁..b₁, g (x, a₂)) +
(∫ y in a₂..b₂, f (b₁, y)) - ∫ y in a₂..b₂, f (a₁, y) :
begin
rw Icc_prod_Icc at *,
apply integral_divergence_prod_Icc_of_has_fderiv_within_at_off_countable_of_le f g f' g'
(a₁, a₂) (b₁, b₂) ⟨h₁, h₂⟩ s; assumption
end },
{ rw [interval_swap b₂ a₂, min_comm b₂ a₂, max_comm b₂ a₂] at this,
intros Hcf Hcg Hdf Hdg Hi,
simp only [interval_integral.integral_symm b₂ a₂, interval_integral.integral_neg],
refine (congr_arg has_neg.neg (this Hcf Hcg Hdf Hdg Hi)).trans _, abel },
{ rw [interval_swap b₁ a₁, min_comm b₁ a₁, max_comm b₁ a₁] at this,
intros Hcf Hcg Hdf Hdg Hi,
simp only [interval_integral.integral_symm b₁ a₁],
refine (congr_arg has_neg.neg (this Hcf Hcg Hdf Hdg Hi)).trans _, abel }
end
end measure_theory
|
e09879004e6bf2a88dce321885d7200b4e263739 | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /algebra/ring.lean | 5399e76356e3f4f55ea8c028d1ff334290e01303 | [
"Apache-2.0"
] | permissive | sebzim4500/mathlib | e0b5a63b1655f910dee30badf09bd7e191d3cf30 | 6997cafbd3a7325af5cb318561768c316ceb7757 | refs/heads/master | 1,585,549,958,618 | 1,538,221,723,000 | 1,538,221,723,000 | 150,869,076 | 0 | 0 | Apache-2.0 | 1,538,229,323,000 | 1,538,229,323,000 | null | UTF-8 | Lean | false | false | 9,925 | 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, Floris van Doorn
-/
import algebra.group data.set.basic
universes u v
variable {α : Type u}
section
variable [semiring α]
theorem mul_two (n : α) : n * 2 = n + n :=
(left_distrib n 1 1).trans (by simp)
theorem bit0_eq_two_mul (n : α) : bit0 n = 2 * n :=
(two_mul _).symm
variable (α)
lemma zero_ne_one_or_forall_eq_0 : (0 : α) ≠ 1 ∨ (∀a:α, a = 0) :=
by haveI := classical.dec;
refine not_or_of_imp (λ h a, _); simpa using congr_arg ((*) a) h.symm
variable {α}
end
namespace units
variables [ring α] {a b : α}
instance : has_neg (units α) := ⟨λu, ⟨-↑u, -↑u⁻¹, by simp, by simp⟩ ⟩
@[simp] protected theorem coe_neg (u : units α) : (↑-u : α) = -u := rfl
@[simp] protected theorem neg_inv (u : units α) : (-u)⁻¹ = -u⁻¹ := rfl
@[simp] protected theorem neg_neg (u : units α) : - -u = u :=
units.ext $ neg_neg _
@[simp] protected theorem neg_mul (u₁ u₂ : units α) : -u₁ * u₂ = -(u₁ * u₂) :=
units.ext $ neg_mul_eq_neg_mul_symm _ _
@[simp] protected theorem mul_neg (u₁ u₂ : units α) : u₁ * -u₂ = -(u₁ * u₂) :=
units.ext $ (neg_mul_eq_mul_neg _ _).symm
@[simp] protected theorem neg_mul_neg (u₁ u₂ : units α) : -u₁ * -u₂ = u₁ * u₂ := by simp
protected theorem neg_eq_neg_one_mul (u : units α) : -u = -1 * u := by simp
end units
instance [semiring α] : semiring (with_zero α) :=
{ left_distrib := λ a b c, begin
cases a with a, {refl},
cases b with b; cases c with c; try {refl},
exact congr_arg some (left_distrib _ _ _)
end,
right_distrib := λ a b c, begin
cases c with c,
{ change (a + b) * 0 = a * 0 + b * 0, simp },
cases a with a; cases b with b; try {refl},
exact congr_arg some (right_distrib _ _ _)
end,
..with_zero.add_comm_monoid,
..with_zero.mul_zero_class,
..with_zero.monoid }
attribute [refl] dvd_refl
attribute [trans] dvd.trans
class is_semiring_hom {α : Type u} {β : Type v} [semiring α] [semiring β] (f : α → β) : Prop :=
(map_zero : f 0 = 0)
(map_one : f 1 = 1)
(map_add : ∀ {x y}, f (x + y) = f x + f y)
(map_mul : ∀ {x y}, f (x * y) = f x * f y)
namespace is_semiring_hom
variables {β : Type v} [semiring α] [semiring β]
variables (f : α → β) [is_semiring_hom f] {x y : α}
instance id : is_semiring_hom (@id α) := by refine {..}; intros; refl
instance comp {γ} [semiring γ] (g : β → γ) [is_semiring_hom g] :
is_semiring_hom (g ∘ f) :=
{ map_zero := by simp [map_zero f]; exact map_zero g,
map_one := by simp [map_one f]; exact map_one g,
map_add := λ x y, by simp [map_add f]; rw map_add g; refl,
map_mul := λ x y, by simp [map_mul f]; rw map_mul g; refl }
instance : is_add_monoid_hom f :=
{ ..‹is_semiring_hom f› }
instance : is_monoid_hom f :=
{ ..‹is_semiring_hom f› }
end is_semiring_hom
@[simp] lemma zero_dvd_iff_eq_zero [comm_semiring α] (a : α) : 0 ∣ a ↔ a = 0 :=
iff.intro
eq_zero_of_zero_dvd
(assume ha, ha ▸ dvd_refl a)
section
variables [ring α] (a b c d e : α)
lemma mul_neg_one (a : α) : a * -1 = -a := by simp
lemma neg_one_mul (a : α) : -1 * a = -a := by simp
theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d :=
calc
a * e + c = b * e + d ↔ a * e + c = d + b * e : by simp
... ↔ a * e + c - b * e = d : iff.intro (λ h, begin simp [h] end) (λ h,
begin simp [h.symm] end)
... ↔ (a - b) * e + c = d : begin simp [@sub_eq_add_neg α, @right_distrib α] end
theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d :=
assume h,
calc
(a - b) * e + c = (a * e + c) - b * e : begin simp [@sub_eq_add_neg α, @right_distrib α] end
... = d : begin rw h, simp [@add_sub_cancel α] end
theorem ne_zero_and_ne_zero_of_mul_ne_zero {a b : α} (h : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 :=
begin
split,
{ intro ha, apply h, simp [ha] },
{ intro hb, apply h, simp [hb] }
end
end
@[simp] lemma zero_dvd_iff [comm_semiring α] {a : α} : 0 ∣ a ↔ a = 0 :=
⟨eq_zero_of_zero_dvd, λ h, by rw h⟩
section comm_ring
variable [comm_ring α]
@[simp] lemma dvd_neg (a b : α) : (a ∣ -b) ↔ (a ∣ b) :=
⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩
@[simp] lemma neg_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) :=
⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩
end comm_ring
class is_ring_hom {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) : Prop :=
(map_one : f 1 = 1)
(map_mul : ∀ {x y}, f (x * y) = f x * f y)
(map_add : ∀ {x y}, f (x + y) = f x + f y)
namespace is_ring_hom
variables {β : Type v} [ring α] [ring β]
def of_semiring (f : α → β) [H : is_semiring_hom f] : is_ring_hom f := {..H}
variables (f : α → β) [is_ring_hom f] {x y : α}
lemma map_zero : f 0 = 0 :=
calc f 0 = f (0 + 0) - f 0 : by rw [map_add f]; simp
... = 0 : by simp
lemma map_neg : f (-x) = -f x :=
calc f (-x) = f (-x + x) - f x : by rw [map_add f]; simp
... = -f x : by simp [map_zero f]
lemma map_sub : f (x - y) = f x - f y :=
by simp [map_add f, map_neg f]
instance id : is_ring_hom (@id α) := by refine {..}; intros; refl
instance comp {γ} [ring γ] (g : β → γ) [is_ring_hom g] :
is_ring_hom (g ∘ f) :=
{ map_add := λ x y, by simp [map_add f]; rw map_add g; refl,
map_mul := λ x y, by simp [map_mul f]; rw map_mul g; refl,
map_one := by simp [map_one f]; exact map_one g }
instance : is_semiring_hom f :=
{ map_zero := map_zero f, ..‹is_ring_hom f› }
instance : is_add_group_hom f :=
⟨λ _ _, map_add f⟩
end is_ring_hom
set_option old_structure_cmd true
class nonzero_comm_ring (α : Type*) extends zero_ne_one_class α, comm_ring α
instance integral_domain.to_nonzero_comm_ring (α : Type*) [id : integral_domain α] :
nonzero_comm_ring α :=
{ ..id }
/-- A domain is a ring with no zero divisors, i.e. satisfying
the condition `a * b = 0 ↔ a = 0 ∨ b = 0`. Alternatively, a domain
is an integral domain without assuming commutativity of multiplication. -/
class domain (α : Type u) extends ring α, no_zero_divisors α, zero_ne_one_class α
section domain
variable [domain α]
@[simp] theorem mul_eq_zero {a b : α} : a * b = 0 ↔ a = 0 ∨ b = 0 :=
⟨eq_zero_or_eq_zero_of_mul_eq_zero, λo,
or.elim o (λh, by rw h; apply zero_mul) (λh, by rw h; apply mul_zero)⟩
@[simp] theorem zero_eq_mul {a b : α} : 0 = a * b ↔ a = 0 ∨ b = 0 :=
by rw [eq_comm, mul_eq_zero]
theorem mul_ne_zero' {a b : α} (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 :=
λ h, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero h) h₁ h₂
theorem domain.mul_right_inj {a b c : α} (ha : a ≠ 0) : b * a = c * a ↔ b = c :=
by rw [← sub_eq_zero, ← mul_sub_right_distrib, mul_eq_zero];
simp [ha]; exact sub_eq_zero
theorem domain.mul_left_inj {a b c : α} (ha : a ≠ 0) : a * b = a * c ↔ b = c :=
by rw [← sub_eq_zero, ← mul_sub_left_distrib, mul_eq_zero];
simp [ha]; exact sub_eq_zero
theorem eq_zero_of_mul_eq_self_right' {a b : α} (h₁ : b ≠ 1) (h₂ : a * b = a) : a = 0 :=
by apply (mul_eq_zero.1 _).resolve_right (sub_ne_zero.2 h₁);
rw [mul_sub_left_distrib, mul_one, sub_eq_zero, h₂]
theorem eq_zero_of_mul_eq_self_left' {a b : α} (h₁ : b ≠ 1) (h₂ : b * a = a) : a = 0 :=
by apply (mul_eq_zero.1 _).resolve_left (sub_ne_zero.2 h₁);
rw [mul_sub_right_distrib, one_mul, sub_eq_zero, h₂]
theorem mul_ne_zero_comm' {a b : α} (h : a * b ≠ 0) : b * a ≠ 0 :=
mul_ne_zero' (ne_zero_of_mul_ne_zero_left h) (ne_zero_of_mul_ne_zero_right h)
end domain
/- integral domains -/
section
variables [s : integral_domain α] (a b c d e : α)
include s
instance integral_domain.to_domain : domain α := {..s}
theorem eq_of_mul_eq_mul_right_of_ne_zero {a b c : α} (ha : a ≠ 0) (h : b * a = c * a) : b = c :=
have b * a - c * a = 0, by simp [h],
have (b - c) * a = 0, by rw [mul_sub_right_distrib, this],
have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right ha,
eq_of_sub_eq_zero this
theorem eq_of_mul_eq_mul_left_of_ne_zero {a b c : α} (ha : a ≠ 0) (h : a * b = a * c) : b = c :=
have a * b - a * c = 0, by simp [h],
have a * (b - c) = 0, by rw [mul_sub_left_distrib, this],
have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_left ha,
eq_of_sub_eq_zero this
theorem mul_dvd_mul_iff_left {a b c : α} (ha : a ≠ 0) : a * b ∣ a * c ↔ b ∣ c :=
exists_congr $ λ d, by rw [mul_assoc, domain.mul_left_inj ha]
theorem mul_dvd_mul_iff_right {a b c : α} (hc : c ≠ 0) : a * c ∣ b * c ↔ a ∣ b :=
exists_congr $ λ d, by rw [mul_right_comm, domain.mul_right_inj hc]
end
/- units in various rings -/
namespace units
section comm_semiring
variables [comm_semiring α] (a b : α) (u : units α)
@[simp] lemma coe_dvd : ↑u ∣ a := ⟨↑u⁻¹ * a, by simp⟩
@[simp] lemma dvd_coe_mul : a ∣ b * u ↔ a ∣ b :=
iff.intro
(assume ⟨c, eq⟩, ⟨c * ↑u⁻¹, by rw [← mul_assoc, ← eq, units.mul_inv_cancel_right]⟩)
(assume ⟨c, eq⟩, eq.symm ▸ dvd_mul_of_dvd_left (dvd_mul_right _ _) _)
@[simp] lemma dvd_coe : a ∣ ↑u ↔ a ∣ 1 :=
suffices a ∣ 1 * ↑u ↔ a ∣ 1, by simpa,
dvd_coe_mul _ _ _
@[simp] lemma coe_mul_dvd : a * u ∣ b ↔ a ∣ b :=
iff.intro
(assume ⟨c, eq⟩, ⟨c * ↑u, eq.symm ▸ by ac_refl⟩)
(assume h, suffices a * ↑u ∣ b * 1, by simpa, mul_dvd_mul h (coe_dvd _ _))
end comm_semiring
section domain
variables [domain α]
@[simp] theorem ne_zero : ∀(u : units α), (↑u : α) ≠ 0
| ⟨u, v, (huv : 0 * v = 1), hvu⟩ rfl := by simpa using huv
end domain
end units
|
25bc9e97342f1c96d43a44abb141bdd4c89a1d8c | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebraic_topology/dold_kan/normalized.lean | d01fa53ec4cd0cdc9214143ba69e0e9b8ca036cb | [
"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 | 6,510 | lean | /-
Copyright (c) 2022 Joël Riou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joël Riou
-/
import algebraic_topology.dold_kan.functor_n
/-!
# Comparison with the normalized Moore complex functor
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
TODO (@joelriou) continue adding the various files referenced below
In this file, we show that when the category `A` is abelian,
there is an isomorphism `N₁_iso_normalized_Moore_complex_comp_to_karoubi` between
the functor `N₁ : simplicial_object A ⥤ karoubi (chain_complex A ℕ)`
defined in `functor_n.lean` and the composition of
`normalized_Moore_complex A` with the inclusion
`chain_complex A ℕ ⥤ karoubi (chain_complex A ℕ)`.
This isomorphism shall be used in `equivalence.lean` in order to obtain
the Dold-Kan equivalence
`category_theory.abelian.dold_kan.equivalence : simplicial_object A ≌ chain_complex A ℕ`
with a functor (definitionally) equal to `normalized_Moore_complex A`.
(See `equivalence.lean` for the general strategy of proof of the Dold-Kan equivalence.)
-/
open category_theory category_theory.category category_theory.limits
category_theory.subobject category_theory.idempotents
open_locale dold_kan
noncomputable theory
namespace algebraic_topology
namespace dold_kan
universe v
variables {A : Type*} [category A] [abelian A] {X : simplicial_object A}
lemma higher_faces_vanish.inclusion_of_Moore_complex_map (n : ℕ) :
higher_faces_vanish (n+1) ((inclusion_of_Moore_complex_map X).f (n+1)) := λ j hj,
begin
dsimp [inclusion_of_Moore_complex_map],
rw [← factor_thru_arrow _ _ (finset_inf_arrow_factors finset.univ
_ j (by simp only [finset.mem_univ])), assoc, kernel_subobject_arrow_comp, comp_zero],
end
lemma factors_normalized_Moore_complex_P_infty (n : ℕ) :
subobject.factors (normalized_Moore_complex.obj_X X n) (P_infty.f n) :=
begin
cases n,
{ apply top_factors, },
{ rw [P_infty_f, normalized_Moore_complex.obj_X, finset_inf_factors],
intros i hi,
apply kernel_subobject_factors,
exact (higher_faces_vanish.of_P (n+1) n) i (le_add_self), }
end
/-- P_infty factors through the normalized Moore complex -/
@[simps]
def P_infty_to_normalized_Moore_complex (X : simplicial_object A) : K[X] ⟶ N[X] :=
chain_complex.of_hom _ _ _ _ _ _
(λ n, factor_thru _ _ (factors_normalized_Moore_complex_P_infty n))
(λ n, begin
rw [← cancel_mono (normalized_Moore_complex.obj_X X n).arrow, assoc, assoc,
factor_thru_arrow, ← inclusion_of_Moore_complex_map_f,
← normalized_Moore_complex_obj_d, ← (inclusion_of_Moore_complex_map X).comm' (n+1) n rfl,
inclusion_of_Moore_complex_map_f, factor_thru_arrow_assoc,
← alternating_face_map_complex_obj_d],
exact P_infty.comm' (n+1) n rfl,
end)
@[simp, reassoc]
lemma P_infty_to_normalized_Moore_complex_comp_inclusion_of_Moore_complex_map
(X : simplicial_object A) :
P_infty_to_normalized_Moore_complex X ≫ inclusion_of_Moore_complex_map X = P_infty := by tidy
@[simp, reassoc]
lemma P_infty_to_normalized_Moore_complex_naturality {X Y : simplicial_object A} (f : X ⟶ Y) :
alternating_face_map_complex.map f ≫ P_infty_to_normalized_Moore_complex Y =
P_infty_to_normalized_Moore_complex X ≫ normalized_Moore_complex.map f := by tidy
@[simp, reassoc]
lemma P_infty_comp_P_infty_to_normalized_Moore_complex (X : simplicial_object A) :
P_infty ≫ P_infty_to_normalized_Moore_complex X = P_infty_to_normalized_Moore_complex X :=
by tidy
@[simp, reassoc]
lemma inclusion_of_Moore_complex_map_comp_P_infty (X : simplicial_object A) :
inclusion_of_Moore_complex_map X ≫ P_infty = inclusion_of_Moore_complex_map X :=
begin
ext n,
cases n,
{ dsimp, simp only [comp_id], },
{ exact (higher_faces_vanish.inclusion_of_Moore_complex_map n).comp_P_eq_self, },
end
instance : mono (inclusion_of_Moore_complex_map X) :=
⟨λ Y f₁ f₂ hf, by { ext n, exact homological_complex.congr_hom hf n, }⟩
/-- `inclusion_of_Moore_complex_map X` is a split mono. -/
def split_mono_inclusion_of_Moore_complex_map (X : simplicial_object A) :
split_mono (inclusion_of_Moore_complex_map X) :=
{ retraction := P_infty_to_normalized_Moore_complex X,
id' := by simp only [← cancel_mono (inclusion_of_Moore_complex_map X), assoc, id_comp,
P_infty_to_normalized_Moore_complex_comp_inclusion_of_Moore_complex_map,
inclusion_of_Moore_complex_map_comp_P_infty], }
variable (A)
/-- When the category `A` is abelian,
the functor `N₁ : simplicial_object A ⥤ karoubi (chain_complex A ℕ)` defined
using `P_infty` identifies to the composition of the normalized Moore complex functor
and the inclusion in the Karoubi envelope. -/
def N₁_iso_normalized_Moore_complex_comp_to_karoubi :
N₁ ≅ (normalized_Moore_complex A ⋙ to_karoubi _) :=
{ hom :=
{ app := λ X,
{ f := P_infty_to_normalized_Moore_complex X,
comm := by erw [comp_id, P_infty_comp_P_infty_to_normalized_Moore_complex] },
naturality' := λ X Y f, by simp only [functor.comp_map, normalized_Moore_complex_map,
P_infty_to_normalized_Moore_complex_naturality, karoubi.hom_ext, karoubi.comp_f, N₁_map_f,
P_infty_comp_P_infty_to_normalized_Moore_complex_assoc, to_karoubi_map_f, assoc] },
inv :=
{ app := λ X,
{ f := inclusion_of_Moore_complex_map X,
comm := by erw [inclusion_of_Moore_complex_map_comp_P_infty, id_comp] },
naturality' := λ X Y f, by { ext, simp only [functor.comp_map, normalized_Moore_complex_map,
karoubi.comp_f, to_karoubi_map_f, homological_complex.comp_f, normalized_Moore_complex.map_f,
inclusion_of_Moore_complex_map_f, factor_thru_arrow, N₁_map_f,
inclusion_of_Moore_complex_map_comp_P_infty_assoc, alternating_face_map_complex.map_f] } },
hom_inv_id' := begin
ext X : 3,
simp only [P_infty_to_normalized_Moore_complex_comp_inclusion_of_Moore_complex_map,
nat_trans.comp_app, karoubi.comp_f, N₁_obj_p, nat_trans.id_app, karoubi.id_eq],
end,
inv_hom_id' := begin
ext X : 3,
simp only [← cancel_mono (inclusion_of_Moore_complex_map X),
nat_trans.comp_app, karoubi.comp_f, assoc, nat_trans.id_app, karoubi.id_eq,
P_infty_to_normalized_Moore_complex_comp_inclusion_of_Moore_complex_map,
inclusion_of_Moore_complex_map_comp_P_infty],
dsimp only [functor.comp_obj, to_karoubi],
rw id_comp,
end }
end dold_kan
end algebraic_topology
|
4d944ce4465134417401494d4486bd794ae8b944 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/ring_theory/localization/at_prime.lean | fc7325ff90e02e2f985cf0be6f5b8f1ed0bc05e0 | [
"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 | 9,918 | 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, Johan Commelin, Amelia Livingston, Anne Baanen
-/
import ring_theory.ideal.local_ring
import ring_theory.localization.ideal
/-!
# Localizations of commutative rings at the complement of a prime ideal
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
## Main definitions
* `is_localization.at_prime (I : ideal R) [is_prime I] (S : Type*)` expresses that `S` is a
localization at (the complement of) a prime ideal `I`, as an abbreviation of
`is_localization I.prime_compl S`
## Main results
* `is_localization.at_prime.local_ring`: a theorem (not an instance) stating a localization at the
complement of a prime ideal is a local ring
## Implementation notes
See `src/ring_theory/localization/basic.lean` for a design overview.
## Tags
localization, ring localization, commutative ring localization, characteristic predicate,
commutative ring, field of fractions
-/
variables {R : Type*} [comm_semiring R] (M : submonoid R) (S : Type*) [comm_semiring S]
variables [algebra R S] {P : Type*} [comm_semiring P]
section at_prime
variables (I : ideal R) [hp : I.is_prime]
include hp
namespace ideal
/-- The complement of a prime ideal `I ⊆ R` is a submonoid of `R`. -/
def prime_compl :
submonoid R :=
{ carrier := (Iᶜ : set R),
one_mem' := by convert I.ne_top_iff_one.1 hp.1; refl,
mul_mem' := λ x y hnx hny hxy, or.cases_on (hp.mem_or_mem hxy) hnx hny }
lemma prime_compl_le_non_zero_divisors [no_zero_divisors R] : I.prime_compl ≤ non_zero_divisors R :=
le_non_zero_divisors_of_no_zero_divisors $ not_not_intro I.zero_mem
end ideal
variables (S)
/-- Given a prime ideal `P`, the typeclass `is_localization.at_prime S P` states that `S` is
isomorphic to the localization of `R` at the complement of `P`. -/
protected abbreviation is_localization.at_prime := is_localization I.prime_compl S
/-- Given a prime ideal `P`, `localization.at_prime S P` is a localization of
`R` at the complement of `P`, as a quotient type. -/
protected abbreviation localization.at_prime := localization I.prime_compl
namespace is_localization
lemma at_prime.nontrivial [is_localization.at_prime S I] : nontrivial S :=
nontrivial_of_ne (0 : S) 1 $ λ hze,
begin
rw [←(algebra_map R S).map_one, ←(algebra_map R S).map_zero] at hze,
obtain ⟨t, ht⟩ := (eq_iff_exists I.prime_compl S).1 hze,
have htz : (t : R) = 0, by simpa using ht.symm,
exact t.2 (htz.symm ▸ I.zero_mem : ↑t ∈ I)
end
local attribute [instance] at_prime.nontrivial
theorem at_prime.local_ring [is_localization.at_prime S I] : local_ring S :=
local_ring.of_nonunits_add
begin
intros x y hx hy hu,
cases is_unit_iff_exists_inv.1 hu with z hxyz,
have : ∀ {r : R} {s : I.prime_compl}, mk' S r s ∈ nonunits S → r ∈ I, from
λ (r : R) (s : I.prime_compl), not_imp_comm.1
(λ nr, is_unit_iff_exists_inv.2 ⟨mk' S ↑s (⟨r, nr⟩ : I.prime_compl),
mk'_mul_mk'_eq_one' _ _ nr⟩),
rcases mk'_surjective I.prime_compl x with ⟨rx, sx, hrx⟩,
rcases mk'_surjective I.prime_compl y with ⟨ry, sy, hry⟩,
rcases mk'_surjective I.prime_compl z with ⟨rz, sz, hrz⟩,
rw [←hrx, ←hry, ←hrz, ←mk'_add, ←mk'_mul,
←mk'_self S I.prime_compl.one_mem] at hxyz,
rw ←hrx at hx, rw ←hry at hy,
obtain ⟨t, ht⟩ := is_localization.eq.1 hxyz,
simp only [mul_one, one_mul, submonoid.coe_mul, subtype.coe_mk] at ht,
suffices : ↑t * (↑sx * ↑sy * ↑sz) ∈ I, from
not_or (mt hp.mem_or_mem $ not_or sx.2 sy.2) sz.2
(hp.mem_or_mem $ (hp.mem_or_mem this).resolve_left t.2),
rw [←ht],
exact I.mul_mem_left _ (I.mul_mem_right _ (I.add_mem (I.mul_mem_right _ $ this hx)
(I.mul_mem_right _ $ this hy))),
end
end is_localization
namespace localization
/-- The localization of `R` at the complement of a prime ideal is a local ring. -/
instance at_prime.local_ring : local_ring (localization I.prime_compl) :=
is_localization.at_prime.local_ring (localization I.prime_compl) I
end localization
end at_prime
namespace is_localization
variables {A : Type*} [comm_ring A] [is_domain A]
/--
The localization of an integral domain at the complement of a prime ideal is an integral domain.
-/
instance is_domain_of_local_at_prime {P : ideal A} (hp : P.is_prime) :
is_domain (localization.at_prime P) :=
is_domain_localization P.prime_compl_le_non_zero_divisors
namespace at_prime
variables (I : ideal R) [hI : I.is_prime] [is_localization.at_prime S I]
include hI
lemma is_unit_to_map_iff (x : R) :
is_unit ((algebra_map R S) x) ↔ x ∈ I.prime_compl :=
⟨λ h hx, (is_prime_of_is_prime_disjoint I.prime_compl S I hI disjoint_compl_left).ne_top $
(ideal.map (algebra_map R S) I).eq_top_of_is_unit_mem (ideal.mem_map_of_mem _ hx) h,
λ h, map_units S ⟨x, h⟩⟩
-- Can't use typeclasses to infer the `local_ring` instance, so use an `opt_param` instead
-- (since `local_ring` is a `Prop`, there should be no unification issues.)
lemma to_map_mem_maximal_iff (x : R) (h : _root_.local_ring S := local_ring S I) :
algebra_map R S x ∈ local_ring.maximal_ideal S ↔ x ∈ I :=
not_iff_not.mp $ by
simpa only [local_ring.mem_maximal_ideal, mem_nonunits_iff, not_not]
using is_unit_to_map_iff S I x
lemma comap_maximal_ideal (h : _root_.local_ring S := local_ring S I) :
(local_ring.maximal_ideal S).comap (algebra_map R S) = I :=
ideal.ext $ λ x, by simpa only [ideal.mem_comap] using to_map_mem_maximal_iff _ I x
lemma is_unit_mk'_iff (x : R) (y : I.prime_compl) :
is_unit (mk' S x y) ↔ x ∈ I.prime_compl :=
⟨λ h hx, mk'_mem_iff.mpr ((to_map_mem_maximal_iff S I x).mpr hx) h,
λ h, is_unit_iff_exists_inv.mpr ⟨mk' S ↑y ⟨x, h⟩, mk'_mul_mk'_eq_one ⟨x, h⟩ y⟩⟩
lemma mk'_mem_maximal_iff (x : R) (y : I.prime_compl) (h : _root_.local_ring S := local_ring S I) :
mk' S x y ∈ local_ring.maximal_ideal S ↔ x ∈ I :=
not_iff_not.mp $ by
simpa only [local_ring.mem_maximal_ideal, mem_nonunits_iff, not_not]
using is_unit_mk'_iff S I x y
end at_prime
end is_localization
namespace localization
open is_localization
local attribute [instance] classical.prop_decidable
variables (I : ideal R) [hI : I.is_prime]
include hI
variables {I}
/-- The unique maximal ideal of the localization at `I.prime_compl` lies over the ideal `I`. -/
lemma at_prime.comap_maximal_ideal :
ideal.comap (algebra_map R (localization.at_prime I))
(local_ring.maximal_ideal (localization I.prime_compl)) = I :=
at_prime.comap_maximal_ideal _ _
/-- The image of `I` in the localization at `I.prime_compl` is a maximal ideal, and in particular
it is the unique maximal ideal given by the local ring structure `at_prime.local_ring` -/
lemma at_prime.map_eq_maximal_ideal :
ideal.map (algebra_map R (localization.at_prime I)) I =
(local_ring.maximal_ideal (localization I.prime_compl)) :=
begin
convert congr_arg (ideal.map _) at_prime.comap_maximal_ideal.symm,
rw map_comap I.prime_compl
end
lemma le_comap_prime_compl_iff {J : ideal P} [hJ : J.is_prime] {f : R →+* P} :
I.prime_compl ≤ J.prime_compl.comap f ↔ J.comap f ≤ I :=
⟨λ h x hx, by { contrapose! hx, exact h hx },
λ h x hx hfxJ, hx (h hfxJ)⟩
variables (I)
/--
For a ring hom `f : R →+* S` and a prime ideal `J` in `S`, the induced ring hom from the
localization of `R` at `J.comap f` to the localization of `S` at `J`.
To make this definition more flexible, we allow any ideal `I` of `R` as input, together with a proof
that `I = J.comap f`. This can be useful when `I` is not definitionally equal to `J.comap f`.
-/
noncomputable def local_ring_hom (J : ideal P) [hJ : J.is_prime] (f : R →+* P)
(hIJ : I = J.comap f) :
localization.at_prime I →+* localization.at_prime J :=
is_localization.map (localization.at_prime J) f (le_comap_prime_compl_iff.mpr (ge_of_eq hIJ))
lemma local_ring_hom_to_map (J : ideal P) [hJ : J.is_prime] (f : R →+* P)
(hIJ : I = J.comap f) (x : R) :
local_ring_hom I J f hIJ (algebra_map _ _ x) = algebra_map _ _ (f x) :=
map_eq _ _
lemma local_ring_hom_mk' (J : ideal P) [hJ : J.is_prime] (f : R →+* P)
(hIJ : I = J.comap f) (x : R) (y : I.prime_compl) :
local_ring_hom I J f hIJ (is_localization.mk' _ x y) =
is_localization.mk' (localization.at_prime J) (f x)
(⟨f y, le_comap_prime_compl_iff.mpr (ge_of_eq hIJ) y.2⟩ : J.prime_compl) :=
map_mk' _ _ _
instance is_local_ring_hom_local_ring_hom (J : ideal P) [hJ : J.is_prime] (f : R →+* P)
(hIJ : I = J.comap f) :
is_local_ring_hom (local_ring_hom I J f hIJ) :=
is_local_ring_hom.mk $ λ x hx,
begin
rcases is_localization.mk'_surjective I.prime_compl x with ⟨r, s, rfl⟩,
rw local_ring_hom_mk' at hx,
rw at_prime.is_unit_mk'_iff at hx ⊢,
exact λ hr, hx ((set_like.ext_iff.mp hIJ r).mp hr),
end
lemma local_ring_hom_unique (J : ideal P) [hJ : J.is_prime] (f : R →+* P)
(hIJ : I = J.comap f) {j : localization.at_prime I →+* localization.at_prime J}
(hj : ∀ x : R, j (algebra_map _ _ x) = algebra_map _ _ (f x)) :
local_ring_hom I J f hIJ = j :=
map_unique _ _ hj
@[simp] lemma local_ring_hom_id :
local_ring_hom I I (ring_hom.id R) (ideal.comap_id I).symm = ring_hom.id _ :=
local_ring_hom_unique _ _ _ _ (λ x, rfl)
@[simp] lemma local_ring_hom_comp {S : Type*} [comm_semiring S]
(J : ideal S) [hJ : J.is_prime] (K : ideal P) [hK : K.is_prime]
(f : R →+* S) (hIJ : I = J.comap f) (g : S →+* P) (hJK : J = K.comap g) :
local_ring_hom I K (g.comp f) (by rw [hIJ, hJK, ideal.comap_comap f g]) =
(local_ring_hom J K g hJK).comp (local_ring_hom I J f hIJ) :=
local_ring_hom_unique _ _ _ _
(λ r, by simp only [function.comp_app, ring_hom.coe_comp, local_ring_hom_to_map])
end localization
|
480feaae37210c1508b425c370e011d6b2397acf | 54d7e71c3616d331b2ec3845d31deb08f3ff1dea | /tests/lean/run/listex.lean | c1d84de8c649f891f29cde9cc2fcc284f139001c | [
"Apache-2.0"
] | permissive | pachugupta/lean | 6f3305c4292288311cc4ab4550060b17d49ffb1d | 0d02136a09ac4cf27b5c88361750e38e1f485a1a | refs/heads/master | 1,611,110,653,606 | 1,493,130,117,000 | 1,493,167,649,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,557 | lean | universe variable u
constant in_tail {α : Type u} {a b : α} {l : list α} : a ∈ l → a ∈ b::l
constant in_head {α : Type u} {a : α} {l : list α} : a ∈ a::l
constant in_left {α : Type u} {a : α} {l : list α} (r : list α) : a ∈ l → a ∈ l ++ r
constant in_right {α : Type u} {a : α} (l : list α) {r : list α} : a ∈ r → a ∈ l ++ r
open expr tactic
meta def search_mem_list : expr → expr → tactic expr
| a e :=
(do m ← mk_app `mem [a, e], find_assumption m)
<|>
(do [_, _, l, r] ← match_app_of e `append | failed, h ← search_mem_list a l, mk_app `in_left [l, r, h])
<|>
(do [_, _, l, r] ← match_app_of e `append | failed, h ← search_mem_list a r, mk_app `in_right [l, r, h])
<|>
(do [_, b, t] ← match_app_of e `list.cons | failed, is_def_eq a b, mk_app `in_head [b, t])
<|>
(do [_, b, t] ← match_app_of e `list.cons | failed, h ← search_mem_list a t, mk_app `in_tail [a, b, t, h])
meta def mk_mem_list : tactic unit :=
do t ← target,
[_, _, _, a, e] ← match_app_of t `mem | failed,
search_mem_list a e >>= exact
example (a b c : nat) : a ∈ [b, c] ++ [b, a, b] :=
by mk_mem_list
example (a b c : nat) : a ∈ [b, c] ++ [b, a+0, b] :=
by mk_mem_list
example (a b c : nat) : a ∈ [b, c] ++ [b, c, c] ++ [b, a+0, b] :=
by mk_mem_list
example (a b c : nat) (l : list nat) : a ∈ l → a ∈ [b, c] ++ b::l :=
by tactic.intros >> mk_mem_list
example (a b c : nat) (l : list nat) : a ∈ l → a ∈ b::b::c::l ++ [c, c, b] :=
by tactic.intros >> mk_mem_list
|
2f99a01572f9db1d00a990ac1179d358b3b292d8 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/ring_theory/witt_vector/defs.lean | 70d0f1395cf528ee56c239beacc045553117a429 | [
"Apache-2.0"
] | permissive | Lix0120/mathlib | 0020745240315ed0e517cbf32e738d8f9811dd80 | e14c37827456fc6707f31b4d1d16f1f3a3205e91 | refs/heads/master | 1,673,102,855,024 | 1,604,151,044,000 | 1,604,151,044,000 | 308,930,245 | 0 | 0 | Apache-2.0 | 1,604,164,710,000 | 1,604,163,547,000 | null | UTF-8 | Lean | false | false | 11,155 | 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.structure_polynomial
/-!
# Witt vectors
In this file we define the type of `p`-typical Witt vectors and ring operations on it.
The ring axioms are verified in `ring_theory/witt_vector/basic.lean`.
For a fixed commutative ring `R` and prime `p`,
a Witt vector `x : 𝕎 R` is an infinite sequence `ℕ → R` of elements of `R`.
However, the ring operations `+` and `*` are not defined in the obvious component-wise way.
Instead, these operations are defined via certain polynomials
using the machinery in `structure_polynomial.lean`.
The `n`th value of the sum of two Witt vectors can depend on the `0`-th through `n`th values
of the summands. This effectively simulates a “carrying” operation.
## Main definitions
* `witt_vector p R`: the type of `p`-typical Witt vectors with coefficients in `R`.
* `witt_vector.coeff x n`: projects the `n`th value of the Witt vector `x`.
## Notation
We use notation `𝕎 R`, entered `\bbW`, for the Witt vectors over `R`.
-/
noncomputable theory
/-- `witt_vector p R` is the ring of `p`-typical Witt vectors over the commutative ring `R`,
where `p` is a prime number.
If `p` is invertible in `R`, this ring is isomorphic to `ℕ → R` (the product of `ℕ` copies of `R`).
If `R` is a ring of characteristic `p`, then `witt_vector p R` is a ring of characteristic `0`.
The canonical example is `witt_vector p (zmod p)`,
which is isomorphic to the `p`-adic integers `ℤ_[p]`. -/
@[nolint unused_arguments]
def witt_vector (p : ℕ) (R : Type*) := ℕ → R
variables {p : ℕ}
/- We cannot make this `localized` notation, because the `p` on the RHS doesn't occur on the left
Hiding the `p` in the notation is very convenient, so we opt for repeating the `local notation`
in other files that use Witt vectors. -/
local notation `𝕎` := witt_vector p -- type as `\bbW`
namespace witt_vector
variables (p) {R : Type*}
/-- Construct a Witt vector `mk p x : 𝕎 R` from a sequence `x` of elements of `R`. -/
def mk (x : ℕ → R) : witt_vector p R := x
instance [inhabited R] : inhabited (𝕎 R) := ⟨mk p $ λ _, default R⟩
/--
`x.coeff n` is the `n`th coefficient of the Witt vector `x`.
This concept does not have a standard name in the literature.
-/
def coeff (x : 𝕎 R) (n : ℕ) : R := x n
@[ext] lemma ext {x y : 𝕎 R} (h : ∀ n, x.coeff n = y.coeff n) : x = y :=
funext $ λ n, h n
lemma ext_iff {x y : 𝕎 R} : x = y ↔ ∀ n, x.coeff n = y.coeff n :=
⟨λ h n, by rw h, ext⟩
@[simp] lemma coeff_mk (x : ℕ → R) :
(mk p x).coeff = x := rfl
/- These instances are not needed for the rest of the development,
but it is interesting to establish early on that `witt_vector p` is a lawful functor. -/
instance : functor (witt_vector p) :=
{ map := λ α β f v, f ∘ v,
map_const := λ α β a v, λ _, a }
instance : is_lawful_functor (witt_vector p) :=
{ map_const_eq := λ α β, rfl,
id_map := λ α v, rfl,
comp_map := λ α β γ f g v, rfl }
variables (p) [hp : fact p.prime] [comm_ring R]
include hp
open mv_polynomial
section ring_operations
/-- The polynomials used for defining the element `0` of the ring of Witt vectors. -/
def witt_zero : ℕ → mv_polynomial (fin 0 × ℕ) ℤ :=
witt_structure_int p 0
/-- The polynomials used for defining the element `1` of the ring of Witt vectors. -/
def witt_one : ℕ → mv_polynomial (fin 0 × ℕ) ℤ :=
witt_structure_int p 1
/-- The polynomials used for defining the addition of the ring of Witt vectors. -/
def witt_add : ℕ → mv_polynomial (fin 2 × ℕ) ℤ :=
witt_structure_int p (X 0 + X 1)
/-- The polynomials used for describing the subtraction of the ring of Witt vectors.
Note that `a - b` is defined as `a + -b`.
See `witt_vector.sub_coeff` for a proof that subtraction is precisely
given by these polynomials `witt_vector.witt_sub` -/
def witt_sub : ℕ → mv_polynomial (fin 2 × ℕ) ℤ :=
witt_structure_int p (X 0 - X 1)
/-- The polynomials used for defining the multiplication of the ring of Witt vectors. -/
def witt_mul : ℕ → mv_polynomial (fin 2 × ℕ) ℤ :=
witt_structure_int p (X 0 * X 1)
/-- The polynomials used for defining the negation of the ring of Witt vectors. -/
def witt_neg : ℕ → mv_polynomial (fin 1 × ℕ) ℤ :=
witt_structure_int p (-X 0)
variable {p}
omit hp
/-- An auxiliary definition used in `witt_vector.eval`.
Evaluates a polynomial whose variables come from the disjoint union of `k` copies of `ℕ`,
with a curried evaluation `x`.
This can be defined more generally but we use only a specific instance here. -/
def peval {k : ℕ} (φ : mv_polynomial (fin k × ℕ) ℤ) (x : fin k → ℕ → R) : R :=
aeval (function.uncurry x) φ
/--
Let `φ` be a family of polynomials, indexed by natural numbers, whose variables come from the
disjoint union of `k` copies of `ℕ`, and let `xᵢ` be a Witt vector for `0 ≤ i < k`.
`eval φ x` evaluates `φ` mapping the variable `X_(i, n)` to the `n`th coefficient of `xᵢ`.
Instantiating `φ` with certain polynomials defined in `structure_polynomial.lean` establishes the
ring operations on `𝕎 R`. For example, `witt_vector.witt_add` is such a `φ` with `k = 2`;
evaluating this at `(x₀, x₁)` gives us the sum of two Witt vectors `x₀ + x₁`.
-/
def eval {k : ℕ} (φ : ℕ → mv_polynomial (fin k × ℕ) ℤ) (x : fin k → 𝕎 R) : 𝕎 R :=
mk p $ λ n, peval (φ n) $ λ i, (x i).coeff
variables (R) [fact p.prime]
instance : has_zero (𝕎 R) :=
⟨eval (witt_zero p) ![]⟩
instance : has_one (𝕎 R) :=
⟨eval (witt_one p) ![]⟩
instance : has_add (𝕎 R) :=
⟨λ x y, eval (witt_add p) ![x, y]⟩
instance : has_mul (𝕎 R) :=
⟨λ x y, eval (witt_mul p) ![x, y]⟩
instance : has_neg (𝕎 R) :=
⟨λ x, eval (witt_neg p) ![x]⟩
end ring_operations
section witt_structure_simplifications
@[simp] lemma witt_zero_eq_zero (n : ℕ) : witt_zero p n = 0 :=
begin
apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective,
simp only [witt_zero, witt_structure_rat, bind₁, aeval_zero',
constant_coeff_X_in_terms_of_W, ring_hom.map_zero,
alg_hom.map_zero, map_witt_structure_int],
end
@[simp] lemma witt_one_zero_eq_one : witt_one p 0 = 1 :=
begin
apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective,
simp only [witt_one, witt_structure_rat, X_in_terms_of_W_zero, alg_hom.map_one,
ring_hom.map_one, bind₁_X_right, map_witt_structure_int]
end
@[simp] lemma witt_one_pos_eq_zero (n : ℕ) (hn : 0 < n) : witt_one p n = 0 :=
begin
apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective,
simp only [witt_one, witt_structure_rat, ring_hom.map_zero, alg_hom.map_one,
ring_hom.map_one, map_witt_structure_int],
revert hn, apply nat.strong_induction_on n, clear n,
intros n IH hn,
rw X_in_terms_of_W_eq,
simp only [alg_hom.map_mul, alg_hom.map_sub, alg_hom.map_sum, alg_hom.map_pow,
bind₁_X_right, bind₁_C_right],
rw [sub_mul, one_mul],
rw [finset.sum_eq_single 0],
{ simp only [inv_of_eq_inv, one_mul, inv_pow', nat.sub_zero, ring_hom.map_one, pow_zero],
simp only [one_pow, one_mul, X_in_terms_of_W_zero, sub_self, bind₁_X_right] },
{ intros i hin hi0,
rw [finset.mem_range] at hin,
rw [IH _ hin (nat.pos_of_ne_zero hi0), zero_pow (pow_pos hp.pos _), mul_zero], },
{ rw finset.mem_range, intro, contradiction }
end
@[simp] lemma witt_add_zero : witt_add p 0 = X (0,0) + X (1,0) :=
begin
apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective,
simp only [witt_add, witt_structure_rat, alg_hom.map_add, ring_hom.map_add,
rename_X, X_in_terms_of_W_zero, map_X,
witt_polynomial_zero, bind₁_X_right, map_witt_structure_int],
end
@[simp] lemma witt_sub_zero : witt_sub p 0 = X (0,0) - X (1,0) :=
begin
apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective,
simp only [witt_sub, witt_structure_rat, alg_hom.map_sub, ring_hom.map_sub,
rename_X, X_in_terms_of_W_zero, map_X,
witt_polynomial_zero, bind₁_X_right, map_witt_structure_int],
end
@[simp] lemma witt_mul_zero : witt_mul p 0 = X (0,0) * X (1,0) :=
begin
apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective,
simp only [witt_mul, witt_structure_rat, rename_X, X_in_terms_of_W_zero, map_X,
witt_polynomial_zero, ring_hom.map_mul,
bind₁_X_right, alg_hom.map_mul, map_witt_structure_int]
end
@[simp] lemma witt_neg_zero : witt_neg p 0 = - X (0,0) :=
begin
apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective,
simp only [witt_neg, witt_structure_rat, rename_X, X_in_terms_of_W_zero, map_X,
witt_polynomial_zero, ring_hom.map_neg,
alg_hom.map_neg, bind₁_X_right, map_witt_structure_int]
end
@[simp] lemma constant_coeff_witt_add (n : ℕ) :
constant_coeff (witt_add p n) = 0 :=
begin
apply constant_coeff_witt_structure_int p _ _ n,
simp only [add_zero, ring_hom.map_add, constant_coeff_X],
end
@[simp] lemma constant_coeff_witt_sub (n : ℕ) :
constant_coeff (witt_sub p n) = 0 :=
begin
apply constant_coeff_witt_structure_int p _ _ n,
simp only [sub_zero, ring_hom.map_sub, constant_coeff_X],
end
@[simp] lemma constant_coeff_witt_mul (n : ℕ) :
constant_coeff (witt_mul p n) = 0 :=
begin
apply constant_coeff_witt_structure_int p _ _ n,
simp only [mul_zero, ring_hom.map_mul, constant_coeff_X],
end
@[simp] lemma constant_coeff_witt_neg (n : ℕ) :
constant_coeff (witt_neg p n) = 0 :=
begin
apply constant_coeff_witt_structure_int p _ _ n,
simp only [neg_zero, ring_hom.map_neg, constant_coeff_X],
end
end witt_structure_simplifications
section coeff
variables (p R)
@[simp] lemma zero_coeff (n : ℕ) : (0 : 𝕎 R).coeff n = 0 :=
show (aeval _ (witt_zero p n) : R) = 0,
by simp only [witt_zero_eq_zero, alg_hom.map_zero]
@[simp] lemma one_coeff_zero : (1 : 𝕎 R).coeff 0 = 1 :=
show (aeval _ (witt_one p 0) : R) = 1,
by simp only [witt_one_zero_eq_one, alg_hom.map_one]
@[simp] lemma one_coeff_eq_of_pos (n : ℕ) (hn : 0 < n) : coeff (1 : 𝕎 R) n = 0 :=
show (aeval _ (witt_one p n) : R) = 0,
by simp only [hn, witt_one_pos_eq_zero, alg_hom.map_zero]
variables {p R}
lemma add_coeff (x y : 𝕎 R) (n : ℕ) :
(x + y).coeff n = peval (witt_add p n) ![x.coeff, y.coeff] := rfl
lemma mul_coeff (x y : 𝕎 R) (n : ℕ) :
(x * y).coeff n = peval (witt_mul p n) ![x.coeff, y.coeff] := rfl
lemma neg_coeff (x : 𝕎 R) (n : ℕ) :
(-x).coeff n = peval (witt_neg p n) ![x.coeff] := rfl
end coeff
lemma witt_add_vars (n : ℕ) :
(witt_add p n).vars ⊆ finset.univ.product (finset.range (n + 1)) :=
witt_structure_int_vars _ _ _
lemma witt_mul_vars (n : ℕ) :
(witt_mul p n).vars ⊆ finset.univ.product (finset.range (n + 1)) :=
witt_structure_int_vars _ _ _
lemma witt_neg_vars (n : ℕ) :
(witt_neg p n).vars ⊆ finset.univ.product (finset.range (n + 1)) :=
witt_structure_int_vars _ _ _
end witt_vector
attribute [irreducible] witt_vector
|
f5112aea5bcd49bacd82f6b5210cd55376ba19b2 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/algebra/ring/prod.lean | 100b618e55be561c3f837cd21ef6dc89b851b687 | [
"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 | 5,406 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Chris Hughes, Mario Carneiro, Yury Kudryashov
-/
import algebra.group.prod
import algebra.ring.basic
import data.equiv.ring
/-!
# Semiring, ring etc structures on `R × S`
In this file we define two-binop (`semiring`, `ring` etc) structures on `R × S`. We also prove
trivial `simp` lemmas, and define the following operations on `ring_hom`s:
* `fst R S : R × S →+* R`, `snd R S : R × S →+* R`: projections `prod.fst` and `prod.snd`
as `ring_hom`s;
* `f.prod g : `R →+* S × T`: sends `x` to `(f x, g x)`;
* `f.prod_map g : `R × S → R' × S'`: `prod.map f g` as a `ring_hom`,
sends `(x, y)` to `(f x, g y)`.
-/
variables {R : Type*} {R' : Type*} {S : Type*} {S' : Type*} {T : Type*} {T' : Type*}
namespace prod
/-- Product of two distributive types is distributive. -/
instance [distrib R] [distrib S] : distrib (R × S) :=
{ left_distrib := λ a b c, mk.inj_iff.mpr ⟨left_distrib _ _ _, left_distrib _ _ _⟩,
right_distrib := λ a b c, mk.inj_iff.mpr ⟨right_distrib _ _ _, right_distrib _ _ _⟩,
.. prod.has_add, .. prod.has_mul }
/-- Product of two `non_unital_non_assoc_semiring`s is a `non_unital_non_assoc_semiring`. -/
instance [non_unital_non_assoc_semiring R] [non_unital_non_assoc_semiring S] :
non_unital_non_assoc_semiring (R × S) :=
{ .. prod.add_comm_monoid, .. prod.mul_zero_class, .. prod.distrib }
/-- Product of two `non_unital_semiring`s is a `non_unital_semiring`. -/
instance [non_unital_semiring R] [non_unital_semiring S] :
non_unital_semiring (R × S) :=
{ .. prod.non_unital_non_assoc_semiring, .. prod.semigroup }
/-- Product of two `non_assoc_semiring`s is a `non_assoc_semiring`. -/
instance [non_assoc_semiring R] [non_assoc_semiring S] :
non_assoc_semiring (R × S) :=
{ .. prod.non_unital_non_assoc_semiring, .. prod.mul_one_class }
/-- Product of two semirings is a semiring. -/
instance [semiring R] [semiring S] : semiring (R × S) :=
{ .. prod.add_comm_monoid, .. prod.monoid_with_zero, .. prod.distrib }
/-- Product of two commutative semirings is a commutative semiring. -/
instance [comm_semiring R] [comm_semiring S] : comm_semiring (R × S) :=
{ .. prod.semiring, .. prod.comm_monoid }
/-- Product of two rings is a ring. -/
instance [ring R] [ring S] : ring (R × S) :=
{ .. prod.add_comm_group, .. prod.semiring }
/-- Product of two commutative rings is a commutative ring. -/
instance [comm_ring R] [comm_ring S] : comm_ring (R × S) :=
{ .. prod.ring, .. prod.comm_monoid }
end prod
namespace ring_hom
variables (R S) [non_assoc_semiring R] [non_assoc_semiring S]
/-- Given semirings `R`, `S`, the natural projection homomorphism from `R × S` to `R`.-/
def fst : R × S →+* R := { to_fun := prod.fst, .. monoid_hom.fst R S, .. add_monoid_hom.fst R S }
/-- Given semirings `R`, `S`, the natural projection homomorphism from `R × S` to `S`.-/
def snd : R × S →+* S := { to_fun := prod.snd, .. monoid_hom.snd R S, .. add_monoid_hom.snd R S }
variables {R S}
@[simp] lemma coe_fst : ⇑(fst R S) = prod.fst := rfl
@[simp] lemma coe_snd : ⇑(snd R S) = prod.snd := rfl
section prod
variables [non_assoc_semiring T] (f : R →+* S) (g : R →+* T)
/-- Combine two ring homomorphisms `f : R →+* S`, `g : R →+* T` into `f.prod g : R →+* S × T`
given by `(f.prod g) x = (f x, g x)` -/
protected def prod (f : R →+* S) (g : R →+* T) : R →+* S × T :=
{ to_fun := λ x, (f x, g x),
.. monoid_hom.prod (f : R →* S) (g : R →* T), .. add_monoid_hom.prod (f : R →+ S) (g : R →+ T) }
@[simp] lemma prod_apply (x) : f.prod g x = (f x, g x) := rfl
@[simp] lemma fst_comp_prod : (fst S T).comp (f.prod g) = f :=
ext $ λ x, rfl
@[simp] lemma snd_comp_prod : (snd S T).comp (f.prod g) = g :=
ext $ λ x, rfl
lemma prod_unique (f : R →+* S × T) :
((fst S T).comp f).prod ((snd S T).comp f) = f :=
ext $ λ x, by simp only [prod_apply, coe_fst, coe_snd, comp_apply, prod.mk.eta]
end prod
section prod_map
variables [non_assoc_semiring R'] [non_assoc_semiring S'] [non_assoc_semiring T]
variables (f : R →+* R') (g : S →+* S')
/-- `prod.map` as a `ring_hom`. -/
def prod_map : R × S →* R' × S' := (f.comp (fst R S)).prod (g.comp (snd R S))
lemma prod_map_def : prod_map f g = (f.comp (fst R S)).prod (g.comp (snd R S)) := rfl
@[simp]
lemma coe_prod_map : ⇑(prod_map f g) = prod.map f g := rfl
lemma prod_comp_prod_map (f : T →* R) (g : T →* S) (f' : R →* R') (g' : S →* S') :
(f'.prod_map g').comp (f.prod g) = (f'.comp f).prod (g'.comp g) :=
rfl
end prod_map
end ring_hom
namespace ring_equiv
variables {R S} [non_assoc_semiring R] [non_assoc_semiring S]
/-- Swapping components as an equivalence of (semi)rings. -/
def prod_comm : R × S ≃+* S × R :=
{ ..add_equiv.prod_comm, ..mul_equiv.prod_comm }
@[simp] lemma coe_prod_comm : ⇑(prod_comm : R × S ≃+* S × R) = prod.swap := rfl
@[simp] lemma coe_prod_comm_symm : ⇑((prod_comm : R × S ≃+* S × R).symm) = prod.swap := rfl
@[simp] lemma fst_comp_coe_prod_comm :
(ring_hom.fst S R).comp ↑(prod_comm : R × S ≃+* S × R) = ring_hom.snd R S :=
ring_hom.ext $ λ _, rfl
@[simp] lemma snd_comp_coe_prod_comm :
(ring_hom.snd S R).comp ↑(prod_comm : R × S ≃+* S × R) = ring_hom.fst R S :=
ring_hom.ext $ λ _, rfl
end ring_equiv
|
6d3716c4cf1f6aecbea791ac5c2541554b4ce21a | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/ring_theory/valuation/valuation_subring.lean | 156e1ae708a0e2ad2c85424f85c53239047c4819 | [
"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 | 30,186 | lean | /-
Copyright (c) 2022 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz, Junyan Xu, Jack McKoen
-/
import ring_theory.valuation.valuation_ring
import ring_theory.localization.as_subring
import ring_theory.subring.pointwise
import algebraic_geometry.prime_spectrum.basic
/-!
# Valuation subrings of a field
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
## Projects
The order structure on `valuation_subring K`.
-/
open_locale classical
noncomputable theory
variables (K : Type*) [field K]
/-- A valuation subring of a field `K` is a subring `A` such that for every `x : K`,
either `x ∈ A` or `x⁻¹ ∈ A`. -/
structure valuation_subring extends subring K :=
(mem_or_inv_mem' : ∀ x : K, x ∈ carrier ∨ x⁻¹ ∈ carrier)
namespace valuation_subring
variables {K} (A : valuation_subring K)
instance : set_like (valuation_subring K) K :=
{ coe := λ A, A.to_subring,
coe_injective' := by { rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ _, congr' } }
@[simp] lemma mem_carrier (x : K) : x ∈ A.carrier ↔ x ∈ A := iff.refl _
@[simp] lemma mem_to_subring (x : K) : x ∈ A.to_subring ↔ x ∈ A := iff.refl _
@[ext] lemma ext (A B : valuation_subring K)
(h : ∀ x, x ∈ A ↔ x ∈ B) : A = B := set_like.ext h
lemma zero_mem : (0 : K) ∈ A := A.to_subring.zero_mem
lemma one_mem : (1 : K) ∈ A := A.to_subring.one_mem
lemma add_mem (x y : K) : x ∈ A → y ∈ A → x + y ∈ A := A.to_subring.add_mem
lemma mul_mem (x y : K) : x ∈ A → y ∈ A → x * y ∈ A := A.to_subring.mul_mem
lemma neg_mem (x : K) : x ∈ A → (-x) ∈ A := A.to_subring.neg_mem
lemma mem_or_inv_mem (x : K) : x ∈ A ∨ x⁻¹ ∈ A := A.mem_or_inv_mem' _
instance : subring_class (valuation_subring K) K :=
{ zero_mem := zero_mem,
add_mem := add_mem,
one_mem := one_mem,
mul_mem := mul_mem,
neg_mem := neg_mem }
lemma to_subring_injective : function.injective (to_subring : valuation_subring K → subring K) :=
λ x y h, by { cases x, cases y, congr' }
instance : comm_ring A := show comm_ring A.to_subring, by apply_instance
instance : is_domain A := show is_domain A.to_subring, by apply_instance
instance : has_top (valuation_subring K) := has_top.mk $
{ mem_or_inv_mem' := λ x, or.inl trivial,
..(⊤ : subring K) }
lemma mem_top (x : K) : x ∈ (⊤ : valuation_subring K) := trivial
lemma le_top : A ≤ ⊤ := λ a ha, mem_top _
instance : order_top (valuation_subring K) :=
{ top := ⊤,
le_top := le_top }
instance : inhabited (valuation_subring K) := ⟨⊤⟩
instance : valuation_ring A :=
{ cond := λ a b,
begin
by_cases (b : K) = 0, { use 0, left, ext, simp [h] },
by_cases (a : K) = 0, { use 0, right, ext, simp [h] },
cases A.mem_or_inv_mem (a/b) with hh hh,
{ use ⟨a/b, hh⟩, right, ext, field_simp, ring },
{ rw (show (a/b : K)⁻¹ = b/a, by field_simp) at hh,
use ⟨b/a, hh⟩, left, ext, field_simp, ring },
end }
instance : algebra A K :=
show algebra A.to_subring K, by apply_instance
@[simp]
lemma algebra_map_apply (a : A) : algebra_map A K a = a := rfl
instance : is_fraction_ring A K :=
{ map_units := λ ⟨y, hy⟩,
(units.mk0 (y : K) (λ c, non_zero_divisors.ne_zero hy $ subtype.ext c)).is_unit,
surj := λ z, begin
by_cases z = 0, { use (0, 1), simp [h] },
cases A.mem_or_inv_mem z with hh hh,
{ use (⟨z, hh⟩, 1), simp },
{ refine ⟨⟨1, ⟨⟨_, hh⟩, _⟩⟩, mul_inv_cancel h⟩,
exact mem_non_zero_divisors_iff_ne_zero.2 (λ c, h (inv_eq_zero.mp (congr_arg coe c))) },
end,
eq_iff_exists := λ a b, ⟨ λ h, ⟨1, by { ext, simpa using h }⟩, λ ⟨c, h⟩,
congr_arg coe ((mul_eq_mul_left_iff.1 h).resolve_right (non_zero_divisors.ne_zero c.2)) ⟩ }
/-- The value group of the valuation associated to `A`. Note: it is actually a group with zero. -/
@[derive linear_ordered_comm_group_with_zero]
def value_group := valuation_ring.value_group A K
/-- Any valuation subring of `K` induces a natural valuation on `K`. -/
def valuation : valuation K A.value_group := valuation_ring.valuation A K
instance inhabited_value_group : inhabited A.value_group := ⟨A.valuation 0⟩
lemma valuation_le_one (a : A) : A.valuation a ≤ 1 :=
(valuation_ring.mem_integer_iff A K _).2 ⟨a, rfl⟩
lemma mem_of_valuation_le_one (x : K) (h : A.valuation x ≤ 1) : x ∈ A :=
let ⟨a, ha⟩ := (valuation_ring.mem_integer_iff A K x).1 h in ha ▸ a.2
lemma valuation_le_one_iff (x : K) : A.valuation x ≤ 1 ↔ x ∈ A :=
⟨mem_of_valuation_le_one _ _, λ ha, A.valuation_le_one ⟨x, ha⟩⟩
lemma valuation_eq_iff (x y : K) : A.valuation x = A.valuation y ↔
∃ a : Aˣ, (a : K) * y = x := quotient.eq'
lemma valuation_le_iff (x y : K) : A.valuation x ≤ A.valuation y ↔
∃ a : A, (a : K) * y = x := iff.rfl
lemma valuation_surjective : function.surjective A.valuation := surjective_quot_mk _
lemma valuation_unit (a : Aˣ) : A.valuation a = 1 :=
by { rw [← A.valuation.map_one, valuation_eq_iff], use a, simp }
lemma valuation_eq_one_iff (a : A) : is_unit a ↔ A.valuation a = 1 :=
⟨ λ h, A.valuation_unit h.unit,
λ h, begin
have ha : (a : K) ≠ 0,
{ intro c, rw [c, A.valuation.map_zero] at h, exact zero_ne_one h },
have ha' : (a : K)⁻¹ ∈ A,
{ rw [← valuation_le_one_iff, map_inv₀, h, inv_one] },
apply is_unit_of_mul_eq_one a ⟨a⁻¹, ha'⟩, ext, field_simp,
end ⟩
lemma valuation_lt_one_or_eq_one (a : A) : A.valuation a < 1 ∨ A.valuation a = 1 :=
lt_or_eq_of_le (A.valuation_le_one a)
lemma valuation_lt_one_iff (a : A) : a ∈ local_ring.maximal_ideal A ↔ A.valuation a < 1 :=
begin
rw local_ring.mem_maximal_ideal,
dsimp [nonunits], rw valuation_eq_one_iff,
exact (A.valuation_le_one a).lt_iff_ne.symm,
end
/-- A subring `R` of `K` such that for all `x : K` either `x ∈ R` or `x⁻¹ ∈ R` is
a valuation subring of `K`. -/
def of_subring (R : subring K) (hR : ∀ x : K, x ∈ R ∨ x⁻¹ ∈ R) : valuation_subring K :=
{ mem_or_inv_mem' := hR, ..R }
@[simp]
lemma mem_of_subring (R : subring K) (hR : ∀ x : K, x ∈ R ∨ x⁻¹ ∈ R) (x : K) :
x ∈ of_subring R hR ↔ x ∈ R := iff.refl _
/-- An overring of a valuation ring is a valuation ring. -/
def of_le (R : valuation_subring K) (S : subring K) (h : R.to_subring ≤ S) :
valuation_subring K :=
{ mem_or_inv_mem' := λ x, (R.mem_or_inv_mem x).imp (@h x) (@h _), ..S}
section order
instance : semilattice_sup (valuation_subring K) :=
{ sup := λ R S, of_le R (R.to_subring ⊔ S.to_subring) $ le_sup_left,
le_sup_left := λ R S x hx, (le_sup_left : R.to_subring ≤ R.to_subring ⊔ S.to_subring) hx,
le_sup_right := λ R S x hx, (le_sup_right : S.to_subring ≤ R.to_subring ⊔ S.to_subring) hx,
sup_le := λ R S T hR hT x hx, (sup_le hR hT : R.to_subring ⊔ S.to_subring ≤ T.to_subring) hx,
..(infer_instance : partial_order (valuation_subring K)) }
/-- The ring homomorphism induced by the partial order. -/
def inclusion (R S : valuation_subring K) (h : R ≤ S) : R →+* S :=
subring.inclusion h
/-- The canonical ring homomorphism from a valuation ring to its field of fractions. -/
def subtype (R : valuation_subring K) : R →+* K :=
subring.subtype R.to_subring
/-- The canonical map on value groups induced by a coarsening of valuation rings. -/
def map_of_le (R S : valuation_subring K) (h : R ≤ S) :
R.value_group →*₀ S.value_group :=
{ to_fun := quotient.map' id $ λ x y ⟨u, hu⟩, ⟨units.map (R.inclusion S h).to_monoid_hom u, hu⟩,
map_zero' := rfl,
map_one' := rfl,
map_mul' := by { rintro ⟨⟩ ⟨⟩, refl } }
@[mono]
lemma monotone_map_of_le (R S : valuation_subring K) (h : R ≤ S) :
monotone (R.map_of_le S h) :=
by { rintros ⟨⟩ ⟨⟩ ⟨a, ha⟩, exact ⟨R.inclusion S h a, ha⟩ }
@[simp]
lemma map_of_le_comp_valuation (R S : valuation_subring K) (h : R ≤ S) :
R.map_of_le S h ∘ R.valuation = S.valuation := by { ext, refl }
@[simp]
lemma map_of_le_valuation_apply (R S : valuation_subring K) (h : R ≤ S) (x : K) :
R.map_of_le S h (R.valuation x) = S.valuation x := rfl
/-- The ideal corresponding to a coarsening of a valuation ring. -/
def ideal_of_le (R S : valuation_subring K) (h : R ≤ S) : ideal R :=
(local_ring.maximal_ideal S).comap (R.inclusion S h)
instance prime_ideal_of_le (R S : valuation_subring K) (h : R ≤ S) :
(ideal_of_le R S h).is_prime := (local_ring.maximal_ideal S).comap_is_prime _
/-- The coarsening of a valuation ring associated to a prime ideal. -/
def of_prime (A : valuation_subring K) (P : ideal A) [P.is_prime] :
valuation_subring K :=
of_le A (localization.subalgebra.of_field K _ P.prime_compl_le_non_zero_divisors).to_subring $
λ a ha, subalgebra.algebra_map_mem _ (⟨a, ha⟩ : A)
instance of_prime_algebra (A : valuation_subring K) (P : ideal A) [P.is_prime] :
algebra A (A.of_prime P) := subalgebra.algebra _
instance of_prime_scalar_tower (A : valuation_subring K) (P : ideal A) [P.is_prime] :
is_scalar_tower A (A.of_prime P) K := is_scalar_tower.subalgebra' A K K _
instance of_prime_localization (A : valuation_subring K) (P : ideal A) [P.is_prime] :
is_localization.at_prime (A.of_prime P) P :=
by apply localization.subalgebra.is_localization_of_field K P.prime_compl
P.prime_compl_le_non_zero_divisors
lemma le_of_prime (A : valuation_subring K) (P : ideal A) [P.is_prime] :
A ≤ of_prime A P :=
λ a ha, subalgebra.algebra_map_mem _ (⟨a, ha⟩ : A)
lemma of_prime_valuation_eq_one_iff_mem_prime_compl
(A : valuation_subring K)
(P : ideal A) [P.is_prime] (x : A) :
(of_prime A P).valuation x = 1 ↔ x ∈ P.prime_compl :=
begin
rw [← is_localization.at_prime.is_unit_to_map_iff (A.of_prime P) P x, valuation_eq_one_iff], refl,
end
@[simp]
lemma ideal_of_le_of_prime (A : valuation_subring K) (P : ideal A) [P.is_prime] :
ideal_of_le A (of_prime A P) (le_of_prime A P) = P :=
by { ext, apply is_localization.at_prime.to_map_mem_maximal_iff }
@[simp]
lemma of_prime_ideal_of_le (R S : valuation_subring K) (h : R ≤ S) :
of_prime R (ideal_of_le R S h) = S :=
begin
ext x, split,
{ rintro ⟨a, r, hr, rfl⟩, apply mul_mem, { exact h a.2 },
{ rw [← valuation_le_one_iff, map_inv₀, ← inv_one, inv_le_inv₀],
{ exact not_lt.1 ((not_iff_not.2 $ valuation_lt_one_iff S _).1 hr) },
{ intro hh, erw [valuation.zero_iff, subring.coe_eq_zero_iff] at hh,
apply hr, rw hh, apply ideal.zero_mem (R.ideal_of_le S h) },
{ exact one_ne_zero } } },
{ intro hx, by_cases hr : x ∈ R, { exact R.le_of_prime _ hr },
have : x ≠ 0 := λ h, hr (by { rw h, exact R.zero_mem }),
replace hr := (R.mem_or_inv_mem x).resolve_left hr,
{ use [1, x⁻¹, hr], split,
{ change (⟨x⁻¹, h hr⟩ : S) ∉ nonunits S,
erw [mem_nonunits_iff, not_not],
apply is_unit_of_mul_eq_one _ (⟨x, hx⟩ : S),
ext, field_simp },
{ field_simp } } },
end
lemma of_prime_le_of_le (P Q : ideal A) [P.is_prime] [Q.is_prime]
(h : P ≤ Q) : of_prime A Q ≤ of_prime A P :=
λ x ⟨a, s, hs, he⟩, ⟨a, s, λ c, hs (h c), he⟩
lemma ideal_of_le_le_of_le (R S : valuation_subring K)
(hR : A ≤ R) (hS : A ≤ S) (h : R ≤ S) :
ideal_of_le A S hS ≤ ideal_of_le A R hR :=
λ x hx, (valuation_lt_one_iff R _).2 begin
by_contra c, push_neg at c, replace c := monotone_map_of_le R S h c,
rw [(map_of_le _ _ _).map_one, map_of_le_valuation_apply] at c,
apply not_le_of_lt ((valuation_lt_one_iff S _).1 hx) c,
end
/-- The equivalence between coarsenings of a valuation ring and its prime ideals.-/
@[simps]
def prime_spectrum_equiv :
prime_spectrum A ≃ { S | A ≤ S } :=
{ to_fun := λ P, ⟨of_prime A P.as_ideal, le_of_prime _ _⟩,
inv_fun := λ S, ⟨ideal_of_le _ S S.2, infer_instance⟩,
left_inv := λ P, by { ext1, simp },
right_inv := λ S, by { ext1, simp } }
/-- An ordered variant of `prime_spectrum_equiv`. -/
@[simps]
def prime_spectrum_order_equiv : (prime_spectrum A)ᵒᵈ ≃o {S | A ≤ S} :=
{ map_rel_iff' := λ P Q,
⟨ λ h, begin
have := ideal_of_le_le_of_le A _ _ _ _ h,
iterate 2 { erw ideal_of_le_of_prime at this },
exact this,
end,
λ h, by { apply of_prime_le_of_le, exact h } ⟩,
..(prime_spectrum_equiv A) }
instance linear_order_overring : linear_order { S | A ≤ S } :=
{ le_total :=
let i : is_total (prime_spectrum A) (≤) := ⟨λ ⟨x, _⟩ ⟨y, _⟩, has_le.le.is_total.total x y⟩ in
by exactI (prime_spectrum_order_equiv A).symm.to_rel_embedding.is_total.total,
decidable_le := infer_instance,
..(infer_instance : partial_order _) }
end order
end valuation_subring
namespace valuation
variables {K} {Γ Γ₁ Γ₂ : Type*}
[linear_ordered_comm_group_with_zero Γ]
[linear_ordered_comm_group_with_zero Γ₁]
[linear_ordered_comm_group_with_zero Γ₂]
(v : valuation K Γ)
(v₁ : valuation K Γ₁)
(v₂ : valuation K Γ₂)
/-- The valuation subring associated to a valuation. -/
def valuation_subring : valuation_subring K :=
{ mem_or_inv_mem' := begin
intros x,
cases le_or_lt (v x) 1,
{ left, exact h },
{ right, change v x⁻¹ ≤ 1,
rw [map_inv₀ v, ← inv_one, inv_le_inv₀],
{ exact le_of_lt h },
{ intro c, simpa [c] using h },
{ exact one_ne_zero } }
end,
.. v.integer }
@[simp]
lemma mem_valuation_subring_iff (x : K) : x ∈ v.valuation_subring ↔ v x ≤ 1 := iff.refl _
lemma is_equiv_iff_valuation_subring : v₁.is_equiv v₂ ↔
v₁.valuation_subring = v₂.valuation_subring :=
begin
split,
{ intros h, ext x, specialize h x 1, simpa using h },
{ intros h, apply is_equiv_of_val_le_one,
intros x,
have : x ∈ v₁.valuation_subring ↔ x ∈ v₂.valuation_subring, by rw h,
simpa using this }
end
lemma is_equiv_valuation_valuation_subring :
v.is_equiv v.valuation_subring.valuation :=
begin
rw [is_equiv_iff_val_le_one],
intro x,
rw valuation_subring.valuation_le_one_iff,
refl,
end
end valuation
namespace valuation_subring
variables {K} (A : valuation_subring K)
@[simp]
lemma valuation_subring_valuation : A.valuation.valuation_subring = A :=
by { ext, rw ← A.valuation_le_one_iff, refl }
section unit_group
/-- The unit group of a valuation subring, as a subgroup of `Kˣ`. -/
def unit_group : subgroup Kˣ :=
(A.valuation.to_monoid_with_zero_hom.to_monoid_hom.comp (units.coe_hom K)).ker
@[simp] lemma mem_unit_group_iff (x : Kˣ) : x ∈ A.unit_group ↔ A.valuation x = 1 := iff.rfl
/-- For a valuation subring `A`, `A.unit_group` agrees with the units of `A`. -/
def unit_group_mul_equiv : A.unit_group ≃* Aˣ :=
{ to_fun := λ x,
{ val := ⟨x, mem_of_valuation_le_one A _ x.prop.le⟩,
inv := ⟨↑(x⁻¹), mem_of_valuation_le_one _ _ (x⁻¹).prop.le⟩,
val_inv := subtype.ext (units.mul_inv x),
inv_val := subtype.ext (units.inv_mul x) },
inv_fun := λ x, ⟨units.map A.subtype.to_monoid_hom x, A.valuation_unit x⟩,
left_inv := λ a, by { ext, refl },
right_inv := λ a, by { ext, refl },
map_mul' := λ a b, by { ext, refl } }
@[simp]
lemma coe_unit_group_mul_equiv_apply (a : A.unit_group) :
(A.unit_group_mul_equiv a : K) = a := rfl
@[simp]
lemma coe_unit_group_mul_equiv_symm_apply (a : Aˣ) :
(A.unit_group_mul_equiv.symm a : K) = a := rfl
lemma unit_group_le_unit_group {A B : valuation_subring K} :
A.unit_group ≤ B.unit_group ↔ A ≤ B :=
begin
split,
{ intros h x hx,
rw [← A.valuation_le_one_iff x, le_iff_lt_or_eq] at hx,
by_cases h_1 : x = 0, { simp only [h_1, zero_mem] },
by_cases h_2 : 1 + x = 0,
{ simp only [← add_eq_zero_iff_neg_eq.1 h_2, neg_mem _ _ (one_mem _)] },
cases hx,
{ have := h (show (units.mk0 _ h_2) ∈ A.unit_group, from A.valuation.map_one_add_of_lt hx),
simpa using B.add_mem _ _
(show 1 + x ∈ B, from set_like.coe_mem ((B.unit_group_mul_equiv ⟨_, this⟩) : B))
(B.neg_mem _ B.one_mem) },
{ have := h (show (units.mk0 x h_1) ∈ A.unit_group, from hx),
refine set_like.coe_mem ((B.unit_group_mul_equiv ⟨_, this⟩) : B) } },
{ rintros h x (hx : A.valuation x = 1),
apply_fun A.map_of_le B h at hx,
simpa using hx }
end
lemma unit_group_injective : function.injective (unit_group : valuation_subring K → subgroup _) :=
λ A B h, by { simpa only [le_antisymm_iff, unit_group_le_unit_group] using h}
lemma eq_iff_unit_group {A B : valuation_subring K} :
A = B ↔ A.unit_group = B.unit_group :=
unit_group_injective.eq_iff.symm
/-- The map on valuation subrings to their unit groups is an order embedding. -/
def unit_group_order_embedding : valuation_subring K ↪o subgroup Kˣ :=
{ to_fun := λ A, A.unit_group,
inj' := unit_group_injective,
map_rel_iff' := λ A B, unit_group_le_unit_group }
lemma unit_group_strict_mono : strict_mono (unit_group : valuation_subring K → subgroup _) :=
unit_group_order_embedding.strict_mono
end unit_group
section nonunits
/-- The nonunits of a valuation subring of `K`, as a subsemigroup of `K`-/
def nonunits : subsemigroup K :=
{ carrier := { x | A.valuation x < 1 },
mul_mem' := λ a b ha hb, (mul_lt_mul₀ ha hb).trans_eq $ mul_one _ }
lemma mem_nonunits_iff {x : K} : x ∈ A.nonunits ↔ A.valuation x < 1 := iff.rfl
lemma nonunits_le_nonunits {A B : valuation_subring K} :
B.nonunits ≤ A.nonunits ↔ A ≤ B :=
begin
split,
{ intros h x hx,
by_cases h_1 : x = 0, { simp only [h_1, zero_mem] },
rw [← valuation_le_one_iff, ← not_lt, valuation.one_lt_val_iff _ h_1] at hx ⊢,
by_contra h_2, from hx (h h_2) },
{ intros h x hx,
by_contra h_1, from not_lt.2 (monotone_map_of_le _ _ h (not_lt.1 h_1)) hx }
end
lemma nonunits_injective :
function.injective (nonunits : valuation_subring K → subsemigroup _) :=
λ A B h, by { simpa only [le_antisymm_iff, nonunits_le_nonunits] using h.symm }
lemma nonunits_inj {A B : valuation_subring K} : A.nonunits = B.nonunits ↔ A = B :=
nonunits_injective.eq_iff
/-- The map on valuation subrings to their nonunits is a dual order embedding. -/
def nonunits_order_embedding :
valuation_subring K ↪o (subsemigroup K)ᵒᵈ :=
{ to_fun := λ A, A.nonunits,
inj' := nonunits_injective,
map_rel_iff' := λ A B, nonunits_le_nonunits }
variables {A}
/-- The elements of `A.nonunits` are those of the maximal ideal of `A` after coercion to `K`.
See also `mem_nonunits_iff_exists_mem_maximal_ideal`, which gets rid of the coercion to `K`,
at the expense of a more complicated right hand side.
-/
theorem coe_mem_nonunits_iff {a : A} : (a : K) ∈ A.nonunits ↔ a ∈ local_ring.maximal_ideal A :=
(valuation_lt_one_iff _ _).symm
lemma nonunits_le : A.nonunits ≤ A.to_subring.to_submonoid.to_subsemigroup :=
λ a ha, (A.valuation_le_one_iff _).mp (A.mem_nonunits_iff.mp ha).le
lemma nonunits_subset : (A.nonunits : set K) ⊆ A := nonunits_le
/-- The elements of `A.nonunits` are those of the maximal ideal of `A`.
See also `coe_mem_nonunits_iff`, which has a simpler right hand side but requires the element
to be in `A` already.
-/
theorem mem_nonunits_iff_exists_mem_maximal_ideal {a : K} :
a ∈ A.nonunits ↔ ∃ ha, (⟨a, ha⟩ : A) ∈ local_ring.maximal_ideal A :=
⟨λ h, ⟨nonunits_subset h, coe_mem_nonunits_iff.mp h⟩,
λ ⟨ha, h⟩, coe_mem_nonunits_iff.mpr h⟩
/-- `A.nonunits` agrees with the maximal ideal of `A`, after taking its image in `K`. -/
theorem image_maximal_ideal : (coe : A → K) '' local_ring.maximal_ideal A = A.nonunits :=
begin
ext a,
simp only [set.mem_image, set_like.mem_coe, mem_nonunits_iff_exists_mem_maximal_ideal],
erw subtype.exists,
simp_rw [subtype.coe_mk, exists_and_distrib_right, exists_eq_right],
end
end nonunits
section principal_unit_group
/-- The principal unit group of a valuation subring, as a subgroup of `Kˣ`. -/
def principal_unit_group : subgroup Kˣ :=
{ carrier := { x | A.valuation (x - 1) < 1 },
mul_mem' := begin
intros a b ha hb,
refine lt_of_le_of_lt _ (max_lt hb ha),
rw [← one_mul (A.valuation (b - 1)), ← A.valuation.map_one_add_of_lt ha, add_sub_cancel'_right,
← valuation.map_mul, mul_sub_one, ← sub_add_sub_cancel],
exact A.valuation.map_add _ _,
end,
one_mem' := by simp,
inv_mem' := begin
dsimp,
intros a ha,
conv {to_lhs, rw [← mul_one (A.valuation _), ← A.valuation.map_one_add_of_lt ha]},
rwa [add_sub_cancel'_right, ← valuation.map_mul, sub_mul, units.inv_mul, ← neg_sub, one_mul,
valuation.map_neg],
end }
lemma principal_units_le_units : A.principal_unit_group ≤ A.unit_group :=
λ a h, by simpa only [add_sub_cancel'_right] using A.valuation.map_one_add_of_lt h
lemma mem_principal_unit_group_iff (x : Kˣ) :
x ∈ A.principal_unit_group ↔ A.valuation ((x : K) - 1) < 1 := iff.rfl
lemma principal_unit_group_le_principal_unit_group {A B : valuation_subring K} :
B.principal_unit_group ≤ A.principal_unit_group ↔ A ≤ B :=
begin
split,
{ intros h x hx,
by_cases h_1 : x = 0, { simp only [h_1, zero_mem] },
by_cases h_2 : x⁻¹ + 1 = 0,
{ rw [add_eq_zero_iff_eq_neg, inv_eq_iff_eq_inv, inv_neg, inv_one] at h_2,
simpa only [h_2] using B.neg_mem _ B.one_mem },
{ rw [← valuation_le_one_iff, ← not_lt, valuation.one_lt_val_iff _ h_1, ← add_sub_cancel x⁻¹,
← units.coe_mk0 h_2, ← mem_principal_unit_group_iff] at hx ⊢,
simpa only [hx] using @h (units.mk0 (x⁻¹ + 1) h_2) } },
{ intros h x hx,
by_contra h_1, from not_lt.2 (monotone_map_of_le _ _ h (not_lt.1 h_1)) hx }
end
lemma principal_unit_group_injective :
function.injective (principal_unit_group : valuation_subring K → subgroup _) :=
λ A B h, by { simpa [le_antisymm_iff, principal_unit_group_le_principal_unit_group] using h.symm }
lemma eq_iff_principal_unit_group {A B : valuation_subring K} :
A = B ↔ A.principal_unit_group = B.principal_unit_group :=
principal_unit_group_injective.eq_iff.symm
/-- The map on valuation subrings to their principal unit groups is an order embedding. -/
def principal_unit_group_order_embedding :
valuation_subring K ↪o (subgroup Kˣ)ᵒᵈ :=
{ to_fun := λ A, A.principal_unit_group,
inj' := principal_unit_group_injective,
map_rel_iff' := λ A B, principal_unit_group_le_principal_unit_group }
lemma coe_mem_principal_unit_group_iff {x : A.unit_group} :
(x : Kˣ) ∈ A.principal_unit_group ↔
A.unit_group_mul_equiv x ∈ (units.map (local_ring.residue A).to_monoid_hom).ker :=
begin
rw [monoid_hom.mem_ker, units.ext_iff],
let π := ideal.quotient.mk (local_ring.maximal_ideal A), convert_to _ ↔ π _ = 1,
rw [← π.map_one, ← sub_eq_zero, ← π.map_sub, ideal.quotient.eq_zero_iff_mem,
valuation_lt_one_iff],
simpa,
end
/-- The principal unit group agrees with the kernel of the canonical map from
the units of `A` to the units of the residue field of `A`. -/
def principal_unit_group_equiv :
A.principal_unit_group ≃* (units.map (local_ring.residue A).to_monoid_hom).ker :=
{ to_fun := λ x, ⟨A.unit_group_mul_equiv ⟨_, A.principal_units_le_units x.2⟩,
A.coe_mem_principal_unit_group_iff.1 x.2⟩,
inv_fun := λ x, ⟨A.unit_group_mul_equiv.symm x,
by { rw A.coe_mem_principal_unit_group_iff, simpa using set_like.coe_mem x }⟩,
left_inv := λ x, by simp,
right_inv := λ x, by simp,
map_mul' := λ x y, by refl, }
@[simp]
lemma principal_unit_group_equiv_apply (a : A.principal_unit_group) :
(principal_unit_group_equiv A a : K) = a := rfl
@[simp]
lemma principal_unit_group_symm_apply
(a : (units.map (local_ring.residue A).to_monoid_hom).ker) :
(A.principal_unit_group_equiv.symm a : K) = a := rfl
/-- The canonical map from the unit group of `A` to the units of the residue field of `A`. -/
def unit_group_to_residue_field_units :
A.unit_group →* (local_ring.residue_field A)ˣ :=
monoid_hom.comp (units.map $ (ideal.quotient.mk _).to_monoid_hom)
A.unit_group_mul_equiv.to_monoid_hom
@[simp]
lemma coe_unit_group_to_residue_field_units_apply (x : A.unit_group) :
(A.unit_group_to_residue_field_units x : (local_ring.residue_field A) ) =
(ideal.quotient.mk _ (A.unit_group_mul_equiv x : A)) := rfl
lemma ker_unit_group_to_residue_field_units :
A.unit_group_to_residue_field_units.ker = A.principal_unit_group.comap A.unit_group.subtype :=
by { ext, simpa only [subgroup.mem_comap, subgroup.coe_subtype, coe_mem_principal_unit_group_iff] }
lemma surjective_unit_group_to_residue_field_units :
function.surjective A.unit_group_to_residue_field_units :=
(local_ring.surjective_units_map_of_local_ring_hom _
ideal.quotient.mk_surjective local_ring.is_local_ring_hom_residue).comp (mul_equiv.surjective _)
/-- The quotient of the unit group of `A` by the principal unit group of `A` agrees with
the units of the residue field of `A`. -/
def units_mod_principal_units_equiv_residue_field_units :
(A.unit_group ⧸ (A.principal_unit_group.comap A.unit_group.subtype)) ≃*
(local_ring.residue_field A)ˣ :=
(quotient_group.quotient_mul_equiv_of_eq A.ker_unit_group_to_residue_field_units.symm).trans
(quotient_group.quotient_ker_equiv_of_surjective _ A.surjective_unit_group_to_residue_field_units)
@[simp]
lemma units_mod_principal_units_equiv_residue_field_units_comp_quotient_group_mk :
A.units_mod_principal_units_equiv_residue_field_units.to_monoid_hom.comp
(quotient_group.mk' _) = A.unit_group_to_residue_field_units := rfl
@[simp]
lemma units_mod_principal_units_equiv_residue_field_units_comp_quotient_group_mk_apply
(x : A.unit_group) :
A.units_mod_principal_units_equiv_residue_field_units.to_monoid_hom
(quotient_group.mk x) = A.unit_group_to_residue_field_units x := rfl
end principal_unit_group
/-! ### Pointwise actions
This transfers the action from `subring.pointwise_mul_action`, noting that it only applies when
the action is by a group. Notably this provides an instances when `G` is `K ≃+* K`.
These instances are in the `pointwise` locale.
The lemmas in this section are copied from `ring_theory/subring/pointwise.lean`; try to keep these
in sync.
-/
section pointwise_actions
open_locale pointwise
variables {G : Type*} [group G] [mul_semiring_action G K]
/-- The action on a valuation subring corresponding to applying the action to every element.
This is available as an instance in the `pointwise` locale. -/
def pointwise_has_smul : has_smul G (valuation_subring K) :=
{ smul := λ g S,
-- TODO: if we add `valuation_subring.map` at a later date, we should use it here
{ mem_or_inv_mem' := λ x, (mem_or_inv_mem S (g⁻¹ • x)).imp
(subring.mem_pointwise_smul_iff_inv_smul_mem.mpr)
(λ h, subring.mem_pointwise_smul_iff_inv_smul_mem.mpr $ by rwa smul_inv''),
.. g • S.to_subring } }
localized "attribute [instance] valuation_subring.pointwise_has_smul" in pointwise
open_locale pointwise
@[simp] lemma coe_pointwise_smul (g : G) (S : valuation_subring K) : ↑(g • S) = g • (S : set K) :=
rfl
@[simp] lemma pointwise_smul_to_subring (g : G) (S : valuation_subring K) :
(g • S).to_subring = g • S.to_subring := rfl
/-- The action on a valuation subring corresponding to applying the action to every element.
This is available as an instance in the `pointwise` locale.
This is a stronger version of `valuation_subring.pointwise_has_smul`. -/
def pointwise_mul_action : mul_action G (valuation_subring K) :=
to_subring_injective.mul_action to_subring pointwise_smul_to_subring
localized "attribute [instance] valuation_subring.pointwise_mul_action" in pointwise
open_locale pointwise
lemma smul_mem_pointwise_smul (g : G) (x : K) (S : valuation_subring K) : x ∈ S → g • x ∈ g • S :=
(set.smul_mem_smul_set : _ → _ ∈ g • (S : set K))
lemma mem_smul_pointwise_iff_exists (g : G) (x : K) (S : valuation_subring K) :
x ∈ g • S ↔ ∃ (s : K), s ∈ S ∧ g • s = x :=
(set.mem_smul_set : x ∈ g • (S : set K) ↔ _)
instance pointwise_central_scalar [mul_semiring_action Gᵐᵒᵖ K] [is_central_scalar G K] :
is_central_scalar G (valuation_subring K) :=
⟨λ g S, to_subring_injective $ by exact op_smul_eq_smul g S.to_subring⟩
@[simp] lemma smul_mem_pointwise_smul_iff {g : G} {S : valuation_subring K} {x : K} :
g • x ∈ g • S ↔ x ∈ S :=
set.smul_mem_smul_set_iff
lemma mem_pointwise_smul_iff_inv_smul_mem {g : G} {S : valuation_subring K} {x : K} :
x ∈ g • S ↔ g⁻¹ • x ∈ S :=
set.mem_smul_set_iff_inv_smul_mem
lemma mem_inv_pointwise_smul_iff {g : G} {S : valuation_subring K} {x : K} :
x ∈ g⁻¹ • S ↔ g • x ∈ S :=
set.mem_inv_smul_set_iff
@[simp] lemma pointwise_smul_le_pointwise_smul_iff {g : G} {S T : valuation_subring K} :
g • S ≤ g • T ↔ S ≤ T :=
set.set_smul_subset_set_smul_iff
lemma pointwise_smul_subset_iff {g : G} {S T : valuation_subring K} : g • S ≤ T ↔ S ≤ g⁻¹ • T :=
set.set_smul_subset_iff
lemma subset_pointwise_smul_iff {g : G} {S T : valuation_subring K} : S ≤ g • T ↔ g⁻¹ • S ≤ T :=
set.subset_set_smul_iff
end pointwise_actions
section
variables {L J: Type*} [field L] [field J]
/-- The pullback of a valuation subring `A` along a ring homomorphism `K →+* L`. -/
def comap (A : valuation_subring L) (f : K →+* L) :
valuation_subring K :=
{ mem_or_inv_mem' := λ k, by simp [valuation_subring.mem_or_inv_mem],
..(A.to_subring.comap f) }
@[simp]
lemma coe_comap (A : valuation_subring L) (f : K →+* L) : (A.comap f : set K) = f ⁻¹' A := rfl
@[simp]
lemma mem_comap {A : valuation_subring L} {f : K →+* L} {x : K} : x ∈ A.comap f ↔ f x ∈ A := iff.rfl
lemma comap_comap (A : valuation_subring J) (g : L →+* J) (f : K →+* L) :
(A.comap g).comap f = A.comap (g.comp f) :=
rfl
end
end valuation_subring
namespace valuation
variables {Γ : Type*} [linear_ordered_comm_group_with_zero Γ] (v : valuation K Γ) (x : Kˣ)
@[simp] lemma mem_unit_group_iff : x ∈ v.valuation_subring.unit_group ↔ v x = 1 :=
(valuation.is_equiv_iff_val_eq_one _ _).mp (valuation.is_equiv_valuation_valuation_subring _).symm
end valuation
|
0f36779f2ca384f26072b07cf44b5fb87ba4f9cc | 05f637fa14ac28031cb1ea92086a0f4eb23ff2b1 | /tests/lean/simp22.lean | 2a491e40d44670f476f879427ddd1e80d9c3b9e2 | [
"Apache-2.0"
] | permissive | codyroux/lean0.1 | 1ce92751d664aacff0529e139083304a7bbc8a71 | 0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef | refs/heads/master | 1,610,830,535,062 | 1,402,150,480,000 | 1,402,150,480,000 | 19,588,851 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,603 | lean | variable vec : Nat → Type
variable concat {n m : Nat} (v : vec n) (w : vec m) : vec (n + m)
infixl 65 ; : concat
axiom concat_assoc {n1 n2 n3 : Nat} (v1 : vec n1) (v2 : vec n2) (v3 : vec n3) :
(v1 ; v2) ; v3 = cast (to_heq (congr2 vec (symm (Nat::add_assoc n1 n2 n3))))
(v1 ; (v2 ; v3))
variable empty : vec 0
axiom concat_empty {n : Nat} (v : vec n) :
v ; empty = cast (to_heq (congr2 vec (symm (Nat::add_zeror n))))
v
rewrite_set simple
-- The simplification rules used for Nat and Vectors should "mirror" each other.
-- Concatenation is not commutative. So, by adding Nat::add_comm to the
-- rewrite set, we prevent the simplifier from reducing the following example
add_rewrite concat_assoc concat_empty Nat::add_assoc Nat::add_zeror Nat::add_comm : simple
universe M >= 1
definition TypeM := (Type M)
variable n : Nat
variable v : vec n
variable w : vec n
variable f {A : TypeM} : A → A
(*
local opts = options({"simplifier", "heq"}, true)
local t = parse_lean([[ f ((v ; w) ; empty ; (v ; empty)) ]])
print(t)
print("===>")
local t2, pr = simplify(t, "simple", opts)
print(t2)
print(pr)
get_environment():type_check(pr)
*)
-- Now, if we disable Nat::add_comm, the simplifier works
disable_rewrite Nat::add_comm : simple
print "After disabling Nat::add_comm"
(*
local opts = options({"simplifier", "heq"}, true)
local t = parse_lean([[ f ((v ; w) ; empty ; (v ; empty)) ]])
print(t)
print("===>")
local t2, pr = simplify(t, "simple", opts)
print(t2)
print(pr)
get_environment():type_check(pr)
*)
|
53e8fcd1701a0e1a5cf10cda894d3cf0c66b156e | bb31430994044506fa42fd667e2d556327e18dfe | /src/measure_theory/measure/haar_of_inner.lean | b10f73db4734b211c77548c12aa7a9e131e1e066 | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 2,922 | lean | /-
Copyright (c) 2022 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.inner_product_space.orientation
import measure_theory.measure.haar_lebesgue
/-!
# Volume forms and measures on inner product spaces
A volume form induces a Lebesgue measure on general finite-dimensional real vector spaces. In this
file, we discuss the specific situation of inner product spaces, where an orientation gives
rise to a canonical volume form. We show that the measure coming from this volume form gives
measure `1` to the parallelepiped spanned by any orthonormal basis, and that it coincides with
the canonical `volume` from the `measure_space` instance.
-/
open finite_dimensional measure_theory measure_theory.measure set
variables {ι F : Type*} [fintype ι] [inner_product_space ℝ F] [finite_dimensional ℝ F]
[measurable_space F] [borel_space F]
section
variables {m n : ℕ} [_i : fact (finrank ℝ F = n)]
include _i
/-- The volume form coming from an orientation in an inner product space gives measure `1` to the
parallelepiped associated to any orthonormal basis. This is a rephrasing of
`abs_volume_form_apply_of_orthonormal` in terms of measures. -/
lemma orientation.measure_orthonormal_basis
(o : orientation ℝ F (fin n)) (b : orthonormal_basis ι ℝ F) :
o.volume_form.measure (parallelepiped b) = 1 :=
begin
have e : ι ≃ fin n,
{ refine fintype.equiv_fin_of_card_eq _,
rw [← _i.out, finrank_eq_card_basis b.to_basis] },
have A : ⇑b = (b.reindex e) ∘ e,
{ ext x,
simp only [orthonormal_basis.coe_reindex, function.comp_app, equiv.symm_apply_apply] },
rw [A, parallelepiped_comp_equiv, alternating_map.measure_parallelepiped,
o.abs_volume_form_apply_of_orthonormal, ennreal.of_real_one],
end
/-- In an oriented inner product space, the measure coming from the canonical volume form
associated to an orientation coincides with the volume. -/
lemma orientation.measure_eq_volume (o : orientation ℝ F (fin n)) :
o.volume_form.measure = volume :=
begin
have A : o.volume_form.measure ((std_orthonormal_basis ℝ F).to_basis.parallelepiped) = 1,
from orientation.measure_orthonormal_basis o (std_orthonormal_basis ℝ F),
rw [add_haar_measure_unique o.volume_form.measure
((std_orthonormal_basis ℝ F).to_basis.parallelepiped), A, one_smul],
simp only [volume, basis.add_haar],
end
end
/-- The volume measure in a finite-dimensional inner product space gives measure `1` to the
parallelepiped spanned by any orthonormal basis. -/
lemma orthonormal_basis.volume_parallelepiped (b : orthonormal_basis ι ℝ F) :
volume (parallelepiped b) = 1 :=
begin
haveI : fact (finrank ℝ F = finrank ℝ F) := ⟨rfl⟩,
let o := (std_orthonormal_basis ℝ F).to_basis.orientation,
rw ← o.measure_eq_volume,
exact o.measure_orthonormal_basis b,
end
|
3c30d5fa39b17aa70ed191a7a27602ddc7903f85 | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/number_theory/divisors.lean | 25c84f5b86bf9cfd9f8c656e3e0769b49b00c709 | [
"Apache-2.0"
] | permissive | mcncm/mathlib | 8d25099344d9d2bee62822cb9ed43aa3e09fa05e | fde3d78cadeec5ef827b16ae55664ef115e66f57 | refs/heads/master | 1,672,743,316,277 | 1,602,618,514,000 | 1,602,618,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,094 | lean | /-
Copyright (c) 2020 Aaron Anderson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson
-/
import algebra.big_operators.basic
import tactic
/-!
# Divisor finsets
This file defines sets of divisors of a natural number. This is particularly useful as background
for defining Dirichlet convolution.
## Main Definitions
Let `n : ℕ`. All of the following definitions are in the `nat` namespace:
* `divisors n` is the `finset` of natural numbers that divide `n`.
* `proper_divisors n` is the `finset` of natural numbers that divide `n`, other than `n`.
* `divisors_antidiagonal n` is the `finset` of pairs `(x,y)` such that `x * y = n`.
* `perfect n` is true when `n` is positive and the sum of `proper_divisors n` is `n`.
## Implementation details
* `divisors 0`, `proper_divisors 0`, and `divisors_antidiagonal 0` are defined to be `∅`.
## Tags
divisors, perfect numbers
-/
open_locale classical
open_locale big_operators
namespace nat
variable (n : ℕ)
/-- `divisors n` is the `finset` of divisors of `n`. As a special case, `divisors 0 = ∅`. -/
def divisors : finset ℕ := finset.filter (λ x : ℕ, x ∣ n) (finset.Ico 1 (n + 1))
/-- `proper_divisors n` is the `finset` of divisors of `n`, other than `n`.
As a special case, `proper_divisors 0 = ∅`. -/
def proper_divisors : finset ℕ := finset.filter (λ x : ℕ, x ∣ n) (finset.Ico 1 n)
/-- `divisors_antidiagonal n` is the `finset` of pairs `(x,y)` such that `x * y = n`.
As a special case, `divisors_antidiagonal 0 = ∅`. -/
def divisors_antidiagonal : finset (ℕ × ℕ) :=
((finset.Ico 1 (n + 1)).product (finset.Ico 1 (n + 1))).filter (λ x, x.fst * x.snd = n)
variable {n}
lemma proper_divisors.not_self_mem : ¬ n ∈ proper_divisors n :=
begin
rw proper_divisors,
simp,
end
@[simp]
lemma mem_proper_divisors {m : ℕ} : n ∈ proper_divisors m ↔ n ∣ m ∧ n < m :=
begin
rw [proper_divisors, finset.mem_filter, finset.Ico.mem, and_comm],
apply and_congr_right,
rw and_iff_right_iff_imp,
intros hdvd hlt,
apply nat.pos_of_ne_zero _,
rintro rfl,
rw zero_dvd_iff.1 hdvd at hlt,
apply lt_irrefl 0 hlt,
end
lemma divisors_eq_proper_divisors_insert_self_of_pos (h : 0 < n):
divisors n = has_insert.insert n (proper_divisors n) :=
by rw [divisors, proper_divisors, finset.Ico.succ_top h, finset.filter_insert, if_pos (dvd_refl n)]
@[simp]
lemma mem_divisors {m : ℕ} :
n ∈ divisors m ↔ (n ∣ m ∧ m ≠ 0) :=
begin
cases m,
{ simp [divisors] },
simp only [divisors, finset.Ico.mem, ne.def, finset.mem_filter, succ_ne_zero, and_true,
and_iff_right_iff_imp, not_false_iff],
intro hdvd,
split,
{ apply nat.pos_of_ne_zero,
rintro rfl,
apply nat.succ_ne_zero,
rwa zero_dvd_iff at hdvd },
{ rw nat.lt_succ_iff,
apply nat.le_of_dvd (nat.succ_pos m) hdvd }
end
lemma dvd_of_mem_divisors {m : ℕ} (h : n ∈ divisors m) : n ∣ m :=
begin
cases m,
{ apply dvd_zero },
{ simp [mem_divisors.1 h], }
end
@[simp]
lemma mem_divisors_antidiagonal {x : ℕ × ℕ} :
x ∈ divisors_antidiagonal n ↔ x.fst * x.snd = n ∧ n ≠ 0 :=
begin
simp only [divisors_antidiagonal, finset.Ico.mem, ne.def, finset.mem_filter, finset.mem_product],
rw and_comm,
apply and_congr_right,
rintro rfl,
split; intro h,
{ contrapose! h, simp [h], },
{ rw [nat.lt_add_one_iff, nat.lt_add_one_iff],
rw [mul_eq_zero, decidable.not_or_iff_and_not] at h,
simp only [succ_le_of_lt (nat.pos_of_ne_zero h.1), succ_le_of_lt (nat.pos_of_ne_zero h.2),
true_and],
exact ⟨le_mul_of_pos_right (nat.pos_of_ne_zero h.2),
le_mul_of_pos_left (nat.pos_of_ne_zero h.1)⟩ }
end
variable {n}
lemma divisor_le {m : ℕ}:
n ∈ divisors m → n ≤ m :=
begin
cases m,
{ simp },
simp only [mem_divisors, m.succ_ne_zero, and_true, ne.def, not_false_iff],
exact nat.le_of_dvd (nat.succ_pos m),
end
variable (n)
@[simp]
lemma divisors_zero : divisors 0 = ∅ := by { ext, simp }
@[simp]
lemma proper_divisors_zero : proper_divisors 0 = ∅ := by { ext, simp }
@[simp]
lemma divisors_antidiagonal_zero : divisors_antidiagonal 0 = ∅ := by { ext, simp }
@[simp]
lemma divisors_antidiagonal_one : divisors_antidiagonal 1 = {(1,1)} :=
by { ext, simp [nat.mul_eq_one_iff, prod.ext_iff], }
lemma swap_mem_divisors_antidiagonal {n : ℕ} {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) :
x.swap ∈ divisors_antidiagonal n :=
begin
rw [mem_divisors_antidiagonal, mul_comm] at h,
simp [h.1, h.2],
end
lemma fst_mem_divisors_of_mem_antidiagonal {n : ℕ} {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) :
x.fst ∈ divisors n :=
begin
rw mem_divisors_antidiagonal at h,
simp [dvd.intro _ h.1, h.2],
end
lemma snd_mem_divisors_of_mem_antidiagonal {n : ℕ} {x : ℕ × ℕ} (h : x ∈ divisors_antidiagonal n) :
x.snd ∈ divisors n :=
begin
rw mem_divisors_antidiagonal at h,
simp [dvd.intro_left _ h.1, h.2],
end
@[simp]
lemma map_swap_divisors_antidiagonal {n : ℕ} :
(divisors_antidiagonal n).map ⟨prod.swap, prod.swap_right_inverse.injective⟩
= divisors_antidiagonal n :=
begin
ext,
simp only [exists_prop, mem_divisors_antidiagonal, finset.mem_map, function.embedding.coe_fn_mk,
ne.def, prod.swap_prod_mk, prod.exists],
split,
{ rintros ⟨x, y, ⟨⟨rfl, h⟩, rfl⟩⟩,
simp [mul_comm, h], },
{ rintros ⟨rfl, h⟩,
use [a.snd, a.fst],
rw mul_comm,
simp [h] }
end
lemma sum_divisors_eq_sum_proper_divisors_add_self :
∑ i in divisors n, i = ∑ i in proper_divisors n, i + n :=
begin
cases n,
{ simp },
{ rw [divisors_eq_proper_divisors_insert_self_of_pos (nat.succ_pos _),
finset.sum_insert (proper_divisors.not_self_mem), add_comm] }
end
/-- `n : ℕ` is perfect if and only the sum of the proper divisors of `n` is `n` and `n`
is positive. -/
def perfect (n : ℕ) : Prop := (∑ i in proper_divisors n, i = n) ∧ 0 < n
theorem perfect_iff_sum_proper_divisors {n : ℕ} (h : 0 < n) :
perfect n ↔ ∑ i in proper_divisors n, i = n := and_iff_left h
theorem perfect_iff_sum_divisors_eq_two_mul {n : ℕ} (h : 0 < n) :
perfect n ↔ ∑ i in divisors n, i = 2 * n :=
begin
rw [perfect_iff_sum_proper_divisors h, sum_divisors_eq_sum_proper_divisors_add_self, two_mul],
split; intro h,
{ rw h },
{ apply add_right_cancel h }
end
lemma mem_divisors_prime_pow {p : ℕ} (pp : p.prime) (k : ℕ) {x : ℕ} :
x ∈ divisors (p ^ k) ↔ ∃ (j : ℕ) (H : j ≤ k), x = p ^ j :=
by rw [mem_divisors, nat.dvd_prime_pow pp, and_iff_left (ne_of_gt (pow_pos pp.pos k))]
lemma divisors_prime {p : ℕ} (pp : p.prime) :
divisors p = {1, p} :=
begin
ext,
simp only [pp.ne_zero, and_true, ne.def, not_false_iff, finset.mem_insert,
finset.mem_singleton, mem_divisors],
refine ⟨pp.2 a, λ h, _⟩,
rcases h; subst h,
apply one_dvd,
end
lemma divisors_prime_pow {p : ℕ} (pp : p.prime) (k : ℕ) :
divisors (p ^ k) = (finset.range (k + 1)).map ⟨pow p, pow_right_injective pp.two_le⟩ :=
by { ext, simp [mem_divisors_prime_pow, pp, nat.lt_succ_iff, @eq_comm _ a] }
open finset
@[simp]
lemma sum_divisors_prime {α : Type*} [add_comm_monoid α] {p : ℕ} {f : ℕ → α} (h : p.prime) :
∑ x in p.divisors, f x = f p + f 1 :=
begin
simp only [h, divisors_prime],
rw [sum_insert, sum_singleton, add_comm],
rw mem_singleton,
apply h.ne_one.symm,
end
@[simp]
lemma prod_divisors_prime {α : Type*} [comm_monoid α] {p : ℕ} {f : ℕ → α} (h : p.prime) :
∏ x in p.divisors, f x = f p * f 1 :=
@sum_divisors_prime (additive α) _ _ _ h
@[simp]
lemma sum_divisors_prime_pow {α : Type*} [add_comm_monoid α] {k p : ℕ} {f : ℕ → α} (h : p.prime) :
∑ x in (p ^ k).divisors, f x = ∑ x in range (k + 1), f (p ^ x) :=
by simp [h, divisors_prime_pow]
@[simp]
lemma prod_divisors_prime_pow {α : Type*} [comm_monoid α] {k p : ℕ} {f : ℕ → α} (h : p.prime) :
∏ x in (p ^ k).divisors, f x = ∏ x in range (k + 1), f (p ^ x) :=
@sum_divisors_prime_pow (additive α) _ _ _ _ h
end nat
|
c3aee05bd6ebccfe88de129aad3ec7b92fada8f2 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /scripts/mk_nolint.lean | f553e02e9b4e19907615aa6b5feaf6914059c537 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 1,360 | lean | /-
Copyright (c) 2019 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
-/
import tactic.lint system.io data.list.sort -- these are required
import all -- then import everything, to parse the library for failing linters
/-!
# mk_nolint
Defines a function that writes a file containing the names of all declarations
that fail the linting tests in `active_linters`.
This is mainly used in the Travis check for mathlib.
It assumes that files generated by `mk_all.sh` are present.
Usage: `lean --run mk_nolint.lean` writes a file `nolints.txt` in the current directory.
-/
open io io.fs
/-- Defines the list of linters that will be considered. -/
meta def active_linters :=
[`linter.unused_arguments, `linter.dup_namespace, `linter.doc_blame,
`linter.illegal_constants, `linter.def_lemma, `linter.instance_priority]
/-- Runs when called with `lean --run` -/
meta def main : io unit :=
do (ns, _) ← run_tactic $ lint_mathlib tt tt active_linters tt,
handle ← mk_file_handle "nolints.txt" mode.write,
put_str_ln handle "import .all",
put_str_ln handle "run_cmd tactic.skip",
put_str_ln handle "apply_nolint",
(ns.to_list.merge_sort (λ a b, name.lex_cmp a b = ordering.lt)).mmap $
λ n, put_str_ln handle (to_string n) >> return n,
close handle
|
a65b2719a1d90e3bed151c68fd92cedd11bf2c9c | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/order/filter/modeq.lean | 015caefbdf3fe1db99050199ab9b1cc8d9defb1c | [
"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 | 1,209 | 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 data.nat.parity
import order.filter.at_top_bot
/-!
# Numbers are frequently modeq to fixed numbers
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we prove that `m ≡ d [MOD n]` frequently as `m → ∞`.
-/
open filter
namespace nat
/-- Infinitely many natural numbers are equal to `d` mod `n`. -/
lemma frequently_modeq {n : ℕ} (h : n ≠ 0) (d : ℕ) : ∃ᶠ m in at_top, m ≡ d [MOD n] :=
((tendsto_add_at_top_nat d).comp (tendsto_id.nsmul_at_top h.bot_lt)).frequently $
frequently_of_forall $ λ m, by { simp [nat.modeq_iff_dvd, ← sub_sub] }
lemma frequently_mod_eq {d n : ℕ} (h : d < n) : ∃ᶠ m in at_top, m % n = d :=
by simpa only [nat.modeq, mod_eq_of_lt h] using frequently_modeq h.ne_bot d
lemma frequently_even : ∃ᶠ m : ℕ in at_top, even m :=
by simpa only [even_iff] using frequently_mod_eq zero_lt_two
lemma frequently_odd : ∃ᶠ m : ℕ in at_top, odd m :=
by simpa only [odd_iff] using frequently_mod_eq one_lt_two
end nat
|
1e7c56dd64d538eeff212b11d872d9040c223ec6 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/limits/over.lean | ca2272a529949ca98119ea4f0cd179d7f7e487f5 | [
"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 | 5,639 | lean | /-
Copyright (c) 2018 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Reid Barton, Bhavik Mehta
-/
import category_theory.over
import category_theory.adjunction.opposites
import category_theory.limits.preserves.basic
import category_theory.limits.shapes.pullbacks
import category_theory.limits.creates
import category_theory.limits.comma
/-!
# Limits and colimits in the over and under categories
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
Show that the forgetful functor `forget X : over X ⥤ C` creates colimits, and hence `over X` has
any colimits that `C` has (as well as the dual that `forget X : under X ⟶ C` creates limits).
Note that the folder `category_theory.limits.shapes.constructions.over` further shows that
`forget X : over X ⥤ C` creates connected limits (so `over X` has connected limits), and that
`over X` has `J`-indexed products if `C` has `J`-indexed wide pullbacks.
TODO: If `C` has binary products, then `forget X : over X ⥤ C` has a right adjoint.
-/
noncomputable theory
universes v u -- morphism levels before object levels. See note [category_theory universes].
open category_theory category_theory.limits
variables {J : Type v} [small_category J]
variables {C : Type u} [category.{v} C]
variable {X : C}
namespace category_theory.over
instance has_colimit_of_has_colimit_comp_forget
(F : J ⥤ over X) [i : has_colimit (F ⋙ forget X)] : has_colimit F :=
@@costructured_arrow.has_colimit _ _ _ _ i _
instance [has_colimits_of_shape J C] : has_colimits_of_shape J (over X) := {}
instance [has_colimits C] : has_colimits (over X) := ⟨infer_instance⟩
instance creates_colimits : creates_colimits (forget X) := costructured_arrow.creates_colimits
-- We can automatically infer that the forgetful functor preserves and reflects colimits.
example [has_colimits C] : preserves_colimits (forget X) := infer_instance
example : reflects_colimits (forget X) := infer_instance
lemma epi_left_of_epi [has_pushouts C] {f g : over X} (h : f ⟶ g) [epi h] : epi h.left :=
costructured_arrow.epi_left_of_epi _
lemma epi_iff_epi_left [has_pushouts C] {f g : over X} (h : f ⟶ g) : epi h ↔ epi h.left :=
costructured_arrow.epi_iff_epi_left _
section
variables [has_pullbacks C]
open tactic
/-- When `C` has pullbacks, a morphism `f : X ⟶ Y` induces a functor `over Y ⥤ over X`,
by pulling back a morphism along `f`. -/
@[simps]
def pullback {X Y : C} (f : X ⟶ Y) : over Y ⥤ over X :=
{ obj := λ g, over.mk (pullback.snd : pullback g.hom f ⟶ X),
map := λ g h k,
over.hom_mk
(pullback.lift (pullback.fst ≫ k.left) pullback.snd (by simp [pullback.condition]))
(by tidy) }
/-- `over.map f` is left adjoint to `over.pullback f`. -/
def map_pullback_adj {A B : C} (f : A ⟶ B) :
over.map f ⊣ pullback f :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ g h,
{ to_fun := λ X, over.hom_mk (pullback.lift X.left g.hom (over.w X)) (pullback.lift_snd _ _ _),
inv_fun := λ Y,
begin
refine over.hom_mk _ _,
refine Y.left ≫ pullback.fst,
dsimp,
rw [← over.w Y, category.assoc, pullback.condition, category.assoc], refl,
end,
left_inv := λ X, by { ext, dsimp, simp, },
right_inv := λ Y, begin
ext, dsimp,
simp only [pullback.lift_fst],
dsimp,
rw [pullback.lift_snd, ← over.w Y],
refl,
end } }
/-- pullback (𝟙 A) : over A ⥤ over A is the identity functor. -/
def pullback_id {A : C} : pullback (𝟙 A) ≅ 𝟭 _ :=
adjunction.right_adjoint_uniq
(map_pullback_adj _)
(adjunction.id.of_nat_iso_left over.map_id.symm)
/-- pullback commutes with composition (up to natural isomorphism). -/
def pullback_comp {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) :
pullback (f ≫ g) ≅ pullback g ⋙ pullback f :=
adjunction.right_adjoint_uniq
(map_pullback_adj _)
(((map_pullback_adj _).comp (map_pullback_adj _)).of_nat_iso_left
(over.map_comp _ _).symm)
instance pullback_is_right_adjoint {A B : C} (f : A ⟶ B) :
is_right_adjoint (pullback f) :=
⟨_, map_pullback_adj f⟩
end
end category_theory.over
namespace category_theory.under
instance has_limit_of_has_limit_comp_forget
(F : J ⥤ under X) [i : has_limit (F ⋙ forget X)] : has_limit F :=
@@structured_arrow.has_limit _ _ _ _ i _
instance [has_limits_of_shape J C] : has_limits_of_shape J (under X) := {}
instance [has_limits C] : has_limits (under X) := ⟨infer_instance⟩
lemma mono_right_of_mono [has_pullbacks C] {f g : under X} (h : f ⟶ g) [mono h] : mono h.right :=
structured_arrow.mono_right_of_mono _
lemma mono_iff_mono_right [has_pullbacks C] {f g : under X} (h : f ⟶ g) : mono h ↔ mono h.right :=
structured_arrow.mono_iff_mono_right _
instance creates_limits : creates_limits (forget X) := structured_arrow.creates_limits
-- We can automatically infer that the forgetful functor preserves and reflects limits.
example [has_limits C] : preserves_limits (forget X) := infer_instance
example : reflects_limits (forget X) := infer_instance
section
variables [has_pushouts C]
/-- When `C` has pushouts, a morphism `f : X ⟶ Y` induces a functor `under X ⥤ under Y`,
by pushing a morphism forward along `f`. -/
@[simps]
def pushout {X Y : C} (f : X ⟶ Y) : under X ⥤ under Y :=
{ obj := λ g, under.mk (pushout.inr : Y ⟶ pushout g.hom f),
map := λ g h k,
under.hom_mk
(pushout.desc (k.right ≫ pushout.inl) pushout.inr (by { simp [←pushout.condition], }))
(by tidy) }
end
end category_theory.under
|
ff49c98a2b4e9136e4989371aae891dd3405e9bf | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/algebra/associated.lean | 66a912526745d075200abb6e25053db93c7af66e | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 27,211 | 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, Jens Wagemaker
Associated and irreducible elements.
-/
import algebra.group data.multiset
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
open lattice
/-- is unit -/
def is_unit [monoid α] (a : α) : Prop := ∃u:units α, a = u
@[simp] lemma is_unit_unit [monoid α] (u : units α) : is_unit (u : α) := ⟨u, rfl⟩
theorem is_unit.mk0 [division_ring α] (x : α) (hx : x ≠ 0) : is_unit x := is_unit_unit (units.mk0 x hx)
lemma is_unit.map [monoid α] [monoid β] (f : α →* β) {x : α} (h : is_unit x) : is_unit (f x) :=
by rcases h with ⟨y, rfl⟩; exact is_unit_unit (units.map f y)
lemma is_unit.map' [monoid α] [monoid β] (f : α → β) {x : α} (h : is_unit x) [is_monoid_hom f] :
is_unit (f x) :=
h.map (monoid_hom.of f)
@[simp] theorem is_unit_zero_iff [semiring α] : is_unit (0 : α) ↔ (0:α) = 1 :=
⟨λ ⟨⟨_, a, (a0 : 0 * a = 1), _⟩, rfl⟩, by rwa zero_mul at a0,
λ h, begin
haveI := subsingleton_of_zero_eq_one _ h,
refine ⟨⟨0, 0, _, _⟩, rfl⟩; apply subsingleton.elim
end⟩
@[simp] theorem not_is_unit_zero [nonzero_comm_ring α] : ¬ is_unit (0 : α) :=
mt is_unit_zero_iff.1 zero_ne_one
@[simp] theorem is_unit_one [monoid α] : is_unit (1:α) := ⟨1, rfl⟩
theorem is_unit_of_mul_one [comm_monoid α] (a b : α) (h : a * b = 1) : is_unit a :=
⟨units.mk_of_mul_eq_one a b h, rfl⟩
theorem is_unit_iff_exists_inv [comm_monoid α] {a : α} : is_unit a ↔ ∃ b, a * b = 1 :=
⟨by rintro ⟨⟨a, b, hab, _⟩, rfl⟩; exact ⟨b, hab⟩,
λ ⟨b, hab⟩, is_unit_of_mul_one _ b hab⟩
theorem is_unit_iff_exists_inv' [comm_monoid α] {a : α} : is_unit a ↔ ∃ b, b * a = 1 :=
by simp [is_unit_iff_exists_inv, mul_comm]
lemma is_unit_pow [monoid α] {a : α} (n : ℕ) : is_unit a → is_unit (a ^ n) :=
λ ⟨u, hu⟩, ⟨u ^ n, by simp *⟩
@[simp] theorem units.is_unit_mul_units [monoid α] (a : α) (u : units α) :
is_unit (a * u) ↔ is_unit a :=
iff.intro
(assume ⟨v, hv⟩,
have is_unit (a * ↑u * ↑u⁻¹), by existsi v * u⁻¹; rw [hv, units.coe_mul],
by rwa [mul_assoc, units.mul_inv, mul_one] at this)
(assume ⟨v, hv⟩, hv.symm ▸ ⟨v * u, (units.coe_mul v u).symm⟩)
theorem is_unit_of_mul_is_unit_left {α} [comm_monoid α] {x y : α}
(hu : is_unit (x * y)) : is_unit x :=
let ⟨z, hz⟩ := is_unit_iff_exists_inv.1 hu in
is_unit_iff_exists_inv.2 ⟨y * z, by rwa ← mul_assoc⟩
theorem is_unit_of_mul_is_unit_right {α} [comm_monoid α] {x y : α}
(hu : is_unit (x * y)) : is_unit y :=
@is_unit_of_mul_is_unit_left _ _ y x $ by rwa mul_comm
theorem is_unit_iff_dvd_one [comm_semiring α] {x : α} : is_unit x ↔ x ∣ 1 :=
⟨by rintro ⟨u, rfl⟩; exact ⟨_, u.mul_inv.symm⟩,
λ ⟨y, h⟩, ⟨⟨x, y, h.symm, by rw [h, mul_comm]⟩, rfl⟩⟩
theorem is_unit_iff_forall_dvd [comm_semiring α] {x : α} :
is_unit x ↔ ∀ y, x ∣ y :=
is_unit_iff_dvd_one.trans ⟨λ h y, dvd.trans h (one_dvd _), λ h, h _⟩
theorem mul_dvd_of_is_unit_left [comm_semiring α] {x y z : α} (h : is_unit x) : x * y ∣ z ↔ y ∣ z :=
⟨dvd_trans (dvd_mul_left _ _),
dvd_trans $ by simpa using mul_dvd_mul_right (is_unit_iff_dvd_one.1 h) y⟩
theorem mul_dvd_of_is_unit_right [comm_semiring α] {x y z : α} (h : is_unit y) : x * y ∣ z ↔ x ∣ z :=
by rw [mul_comm, mul_dvd_of_is_unit_left h]
@[simp] lemma unit_mul_dvd_iff [comm_semiring α] {a b : α} {u : units α} : (u : α) * a ∣ b ↔ a ∣ b :=
mul_dvd_of_is_unit_left (is_unit_unit _)
@[simp] lemma mul_unit_dvd_iff [comm_semiring α] {a b : α} {u : units α} : a * u ∣ b ↔ a ∣ b :=
mul_dvd_of_is_unit_right (is_unit_unit _)
theorem is_unit_of_dvd_unit {α} [comm_semiring α] {x y : α}
(xy : x ∣ y) (hu : is_unit y) : is_unit x :=
is_unit_iff_dvd_one.2 $ dvd_trans xy $ is_unit_iff_dvd_one.1 hu
@[simp] theorem is_unit_nat {n : ℕ} : is_unit n ↔ n = 1 :=
iff.intro
(assume ⟨u, hu⟩, match n, u, hu, nat.units_eq_one u with _, _, rfl, rfl := rfl end)
(assume h, h.symm ▸ ⟨1, rfl⟩)
theorem is_unit_int {n : ℤ} : is_unit n ↔ n.nat_abs = 1 :=
⟨λ ⟨u, hu⟩, (int.units_eq_one_or u).elim (by simp *) (by simp *),
λ h, is_unit_iff_dvd_one.2 ⟨n, by rw [← int.nat_abs_mul_self, h]; refl⟩⟩
lemma is_unit_of_dvd_one [comm_semiring α] : ∀a ∣ 1, is_unit (a:α)
| a ⟨b, eq⟩ := ⟨units.mk_of_mul_eq_one a b eq.symm, rfl⟩
lemma dvd_and_not_dvd_iff [integral_domain α] {x y : α} :
x ∣ y ∧ ¬y ∣ x ↔ x ≠ 0 ∧ ∃ d : α, ¬ is_unit d ∧ y = x * d :=
⟨λ ⟨⟨d, hd⟩, hyx⟩, ⟨λ hx0, by simpa [hx0] using hyx, ⟨d,
mt is_unit_iff_dvd_one.1 (λ ⟨e, he⟩, hyx ⟨e, by rw [hd, mul_assoc, ← he, mul_one]⟩), hd⟩⟩,
λ ⟨hx0, d, hdu, hdx⟩, ⟨⟨d, hdx⟩, λ ⟨e, he⟩, hdu (is_unit_of_dvd_one _
⟨e, (domain.mul_left_inj hx0).1 $ by conv {to_lhs, rw [he, hdx]};simp [mul_assoc]⟩)⟩⟩
lemma pow_dvd_pow_iff [integral_domain α] {x : α} {n m : ℕ} (h0 : x ≠ 0) (h1 : ¬ is_unit x) :
x ^ n ∣ x ^ m ↔ n ≤ m :=
begin
split,
{ intro h, rw [← not_lt], intro hmn, apply h1,
have : x * x ^ m ∣ 1 * x ^ m,
{ rw [← pow_succ, one_mul], exact dvd_trans (pow_dvd_pow _ (nat.succ_le_of_lt hmn)) h },
rwa [mul_dvd_mul_iff_right, ← is_unit_iff_dvd_one] at this, apply pow_ne_zero m h0 },
{ apply pow_dvd_pow }
end
/-- prime element of a semiring -/
def prime [comm_semiring α] (p : α) : Prop :=
p ≠ 0 ∧ ¬ is_unit p ∧ (∀a b, p ∣ a * b → p ∣ a ∨ p ∣ b)
namespace prime
lemma ne_zero [comm_semiring α] {p : α} (hp : prime p) : p ≠ 0 :=
hp.1
lemma not_unit [comm_semiring α] {p : α} (hp : prime p) : ¬ is_unit p :=
hp.2.1
lemma div_or_div [comm_semiring α] {p : α} (hp : prime p) {a b : α} (h : p ∣ a * b) :
p ∣ a ∨ p ∣ b :=
hp.2.2 a b h
end prime
@[simp] lemma not_prime_zero [comm_semiring α] : ¬ prime (0 : α) :=
λ h, h.ne_zero rfl
@[simp] lemma not_prime_one [comm_semiring α] : ¬ prime (1 : α) :=
λ h, h.not_unit is_unit_one
lemma exists_mem_multiset_dvd_of_prime [comm_semiring α] {s : multiset α} {p : α} (hp : prime p) :
p ∣ s.prod → ∃a∈s, p ∣ a :=
multiset.induction_on s (assume h, (hp.not_unit $ is_unit_of_dvd_one _ h).elim) $
assume a s ih h,
have p ∣ a * s.prod, by simpa using h,
match hp.div_or_div this with
| or.inl h := ⟨a, multiset.mem_cons_self a s, h⟩
| or.inr h := let ⟨a, has, h⟩ := ih h in ⟨a, multiset.mem_cons_of_mem has, h⟩
end
/-- `irreducible p` states that `p` is non-unit and only factors into units.
We explicitly avoid stating that `p` is non-zero, this would require a semiring. Assuming only a
monoid allows us to reuse irreducible for associated elements.
-/
@[class] def irreducible [monoid α] (p : α) : Prop :=
¬ is_unit p ∧ ∀a b, p = a * b → is_unit a ∨ is_unit b
namespace irreducible
lemma not_unit [monoid α] {p : α} (hp : irreducible p) : ¬ is_unit p :=
hp.1
lemma is_unit_or_is_unit [monoid α] {p : α} (hp : irreducible p) {a b : α} (h : p = a * b) :
is_unit a ∨ is_unit b :=
hp.2 a b h
end irreducible
@[simp] theorem not_irreducible_one [monoid α] : ¬ irreducible (1 : α) :=
by simp [irreducible]
@[simp] theorem not_irreducible_zero [semiring α] : ¬ irreducible (0 : α)
| ⟨hn0, h⟩ := have is_unit (0:α) ∨ is_unit (0:α), from h 0 0 ((mul_zero 0).symm),
this.elim hn0 hn0
theorem irreducible.ne_zero [semiring α] : ∀ {p:α}, irreducible p → p ≠ 0
| _ hp rfl := not_irreducible_zero hp
theorem of_irreducible_mul {α} [monoid α] {x y : α} :
irreducible (x * y) → is_unit x ∨ is_unit y
| ⟨_, h⟩ := h _ _ rfl
theorem irreducible_or_factor {α} [monoid α] (x : α) (h : ¬ is_unit x) :
irreducible x ∨ ∃ a b, ¬ is_unit a ∧ ¬ is_unit b ∧ a * b = x :=
begin
haveI := classical.dec,
refine or_iff_not_imp_right.2 (λ H, _),
simp [h, irreducible] at H ⊢,
refine λ a b h, classical.by_contradiction $ λ o, _,
simp [not_or_distrib] at o,
exact H _ o.1 _ o.2 h.symm
end
lemma irreducible_of_prime [integral_domain α] {p : α} (hp : prime p) : irreducible p :=
⟨hp.not_unit, λ a b hab,
(show a * b ∣ a ∨ a * b ∣ b, from hab ▸ hp.div_or_div (hab ▸ (dvd_refl _))).elim
(λ ⟨x, hx⟩, or.inr (is_unit_iff_dvd_one.2
⟨x, (domain.mul_left_inj (show a ≠ 0, from λ h, by simp [*, prime] at *)).1
$ by conv {to_lhs, rw hx}; simp [mul_comm, mul_assoc, mul_left_comm]⟩))
(λ ⟨x, hx⟩, or.inl (is_unit_iff_dvd_one.2
⟨x, (domain.mul_left_inj (show b ≠ 0, from λ h, by simp [*, prime] at *)).1
$ by conv {to_lhs, rw hx}; simp [mul_comm, mul_assoc, mul_left_comm]⟩))⟩
lemma succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul [integral_domain α] {p : α} (hp : prime p) {a b : α}
{k l : ℕ} : p ^ k ∣ a → p ^ l ∣ b → p ^ ((k + l) + 1) ∣ a * b →
p ^ (k + 1) ∣ a ∨ p ^ (l + 1) ∣ b :=
λ ⟨x, hx⟩ ⟨y, hy⟩ ⟨z, hz⟩,
have h : p ^ (k + l) * (x * y) = p ^ (k + l) * (p * z),
by simpa [mul_comm, _root_.pow_add, hx, hy, mul_assoc, mul_left_comm] using hz,
have hp0: p ^ (k + l) ≠ 0, from pow_ne_zero _ hp.ne_zero,
have hpd : p ∣ x * y, from ⟨z, by rwa [domain.mul_left_inj hp0] at h⟩,
(hp.div_or_div hpd).elim
(λ ⟨d, hd⟩, or.inl ⟨d, by simp [*, _root_.pow_succ, mul_comm, mul_left_comm, mul_assoc]⟩)
(λ ⟨d, hd⟩, or.inr ⟨d, by simp [*, _root_.pow_succ, mul_comm, mul_left_comm, mul_assoc]⟩)
def associated [monoid α] (x y : α) : Prop := ∃u:units α, x * u = y
local infix ` ~ᵤ ` : 50 := associated
namespace associated
@[refl] protected theorem refl [monoid α] (x : α) : x ~ᵤ x := ⟨1, by simp⟩
@[symm] protected theorem symm [monoid α] : ∀{x y : α}, x ~ᵤ y → y ~ᵤ x
| x _ ⟨u, rfl⟩ := ⟨u⁻¹, by rw [mul_assoc, units.mul_inv, mul_one]⟩
@[trans] protected theorem trans [monoid α] : ∀{x y z : α}, x ~ᵤ y → y ~ᵤ z → x ~ᵤ z
| x _ _ ⟨u, rfl⟩ ⟨v, rfl⟩ := ⟨u * v, by rw [units.coe_mul, mul_assoc]⟩
protected def setoid (α : Type*) [monoid α] : setoid α :=
{ r := associated, iseqv := ⟨associated.refl, λa b, associated.symm, λa b c, associated.trans⟩ }
end associated
local attribute [instance] associated.setoid
theorem unit_associated_one [monoid α] {u : units α} : (u : α) ~ᵤ 1 := ⟨u⁻¹, units.mul_inv u⟩
theorem associated_one_iff_is_unit [monoid α] {a : α} : (a : α) ~ᵤ 1 ↔ is_unit a :=
iff.intro
(assume h, let ⟨c, h⟩ := h.symm in h ▸ ⟨c, one_mul _⟩)
(assume ⟨c, h⟩, associated.symm ⟨c, by simp [h]⟩)
theorem associated_zero_iff_eq_zero [comm_semiring α] (a : α) : a ~ᵤ 0 ↔ a = 0 :=
iff.intro
(assume h, let ⟨u, h⟩ := h.symm in by simpa using h.symm)
(assume h, h ▸ associated.refl a)
theorem associated_one_of_mul_eq_one [comm_monoid α] {a : α} (b : α) (hab : a * b = 1) : a ~ᵤ 1 :=
show (units.mk_of_mul_eq_one a b hab : α) ~ᵤ 1, from unit_associated_one
theorem associated_one_of_associated_mul_one [comm_monoid α] {a b : α} :
a * b ~ᵤ 1 → a ~ᵤ 1
| ⟨u, h⟩ := associated_one_of_mul_eq_one (b * u) $ by simpa [mul_assoc] using h
lemma associated_mul_mul [comm_monoid α] {a₁ a₂ b₁ b₂ : α} :
a₁ ~ᵤ b₁ → a₂ ~ᵤ b₂ → (a₁ * a₂) ~ᵤ (b₁ * b₂)
| ⟨c₁, h₁⟩ ⟨c₂, h₂⟩ := ⟨c₁ * c₂, by simp [h₁.symm, h₂.symm, mul_assoc, mul_comm, mul_left_comm]⟩
theorem associated_of_dvd_dvd [integral_domain α] {a b : α} (hab : a ∣ b) (hba : b ∣ a) : a ~ᵤ b :=
begin
haveI := classical.dec_eq α,
rcases hab with ⟨c, rfl⟩,
rcases hba with ⟨d, a_eq⟩,
by_cases ha0 : a = 0,
{ simp [*] at * },
have : a * 1 = a * (c * d),
{ simpa [mul_assoc] using a_eq },
have : 1 = (c * d), from eq_of_mul_eq_mul_left ha0 this,
exact ⟨units.mk_of_mul_eq_one c d (this.symm), by rw [units.mk_of_mul_eq_one, units.val_coe]⟩
end
lemma exists_associated_mem_of_dvd_prod [integral_domain α] {p : α}
(hp : prime p) {s : multiset α} : (∀ r ∈ s, prime r) → p ∣ s.prod → ∃ q ∈ s, p ~ᵤ q :=
multiset.induction_on s (by simp [mt is_unit_iff_dvd_one.2 hp.not_unit])
(λ a s ih hs hps, begin
rw [multiset.prod_cons] at hps,
cases hp.div_or_div hps with h h,
{ use [a, by simp],
cases h with u hu,
cases ((irreducible_of_prime (hs a (multiset.mem_cons.2
(or.inl rfl)))).2 p u hu).resolve_left hp.not_unit with v hv,
exact ⟨v, by simp [hu, hv]⟩ },
{ rcases ih (λ r hr, hs _ (multiset.mem_cons.2 (or.inr hr))) h with ⟨q, hq₁, hq₂⟩,
exact ⟨q, multiset.mem_cons.2 (or.inr hq₁), hq₂⟩ }
end)
lemma dvd_iff_dvd_of_rel_left [comm_semiring α] {a b c : α} (h : a ~ᵤ b) : a ∣ c ↔ b ∣ c :=
let ⟨u, hu⟩ := h in hu ▸ mul_unit_dvd_iff.symm
@[simp] lemma dvd_mul_unit_iff [comm_semiring α] {a b : α} {u : units α} : a ∣ b * u ↔ a ∣ b :=
⟨λ ⟨d, hd⟩, ⟨d * (u⁻¹ : units α), by simp [(mul_assoc _ _ _).symm, hd.symm]⟩,
λ h, dvd.trans h (by simp)⟩
lemma dvd_iff_dvd_of_rel_right [comm_semiring α] {a b c : α} (h : b ~ᵤ c) : a ∣ b ↔ a ∣ c :=
let ⟨u, hu⟩ := h in hu ▸ dvd_mul_unit_iff.symm
lemma eq_zero_iff_of_associated [comm_semiring α] {a b : α} (h : a ~ᵤ b) : a = 0 ↔ b = 0 :=
⟨λ ha, let ⟨u, hu⟩ := h in by simp [hu.symm, ha],
λ hb, let ⟨u, hu⟩ := h.symm in by simp [hu.symm, hb]⟩
lemma ne_zero_iff_of_associated [comm_semiring α] {a b : α} (h : a ~ᵤ b) : a ≠ 0 ↔ b ≠ 0 :=
by haveI := classical.dec; exact not_iff_not.2 (eq_zero_iff_of_associated h)
lemma prime_of_associated [comm_semiring α] {p q : α} (h : p ~ᵤ q) (hp : prime p) : prime q :=
⟨(ne_zero_iff_of_associated h).1 hp.ne_zero,
let ⟨u, hu⟩ := h in
⟨λ ⟨v, hv⟩, hp.not_unit ⟨v * u⁻¹, by simp [hv.symm, hu.symm]⟩,
hu ▸ by { simp [mul_unit_dvd_iff], intros a b, exact hp.div_or_div }⟩⟩
lemma prime_iff_of_associated [comm_semiring α] {p q : α}
(h : p ~ᵤ q) : prime p ↔ prime q :=
⟨prime_of_associated h, prime_of_associated h.symm⟩
lemma is_unit_iff_of_associated [monoid α] {a b : α} (h : a ~ᵤ b) : is_unit a ↔ is_unit b :=
⟨let ⟨u, hu⟩ := h in λ ⟨v, hv⟩, ⟨v * u, by simp [hv, hu.symm]⟩,
let ⟨u, hu⟩ := h.symm in λ ⟨v, hv⟩, ⟨v * u, by simp [hv, hu.symm]⟩⟩
lemma irreducible_of_associated [comm_semiring α] {p q : α} (h : p ~ᵤ q)
(hp : irreducible p) : irreducible q :=
⟨mt (is_unit_iff_of_associated h).2 hp.1,
let ⟨u, hu⟩ := h in λ a b hab,
have hpab : p = a * (b * (u⁻¹ : units α)),
from calc p = (p * u) * (u ⁻¹ : units α) : by simp
... = _ : by rw hu; simp [hab, mul_assoc],
(hp.2 _ _ hpab).elim or.inl (λ ⟨v, hv⟩, or.inr ⟨v * u, by simp [hv.symm]⟩)⟩
lemma irreducible_iff_of_associated [comm_semiring α] {p q : α} (h : p ~ᵤ q) :
irreducible p ↔ irreducible q :=
⟨irreducible_of_associated h, irreducible_of_associated h.symm⟩
lemma associated_mul_left_cancel [integral_domain α] {a b c d : α}
(h : a * b ~ᵤ c * d) (h₁ : a ~ᵤ c) (ha : a ≠ 0) : b ~ᵤ d :=
let ⟨u, hu⟩ := h in let ⟨v, hv⟩ := associated.symm h₁ in
⟨u * (v : units α), (domain.mul_left_inj ha).1
begin
rw [← hv, mul_assoc c (v : α) d, mul_left_comm c, ← hu],
simp [hv.symm, mul_assoc, mul_comm, mul_left_comm]
end⟩
lemma associated_mul_right_cancel [integral_domain α] {a b c d : α} :
a * b ~ᵤ c * d → b ~ᵤ d → b ≠ 0 → a ~ᵤ c :=
by rw [mul_comm a, mul_comm c]; exact associated_mul_left_cancel
def associates (α : Type*) [monoid α] : Type* :=
quotient (associated.setoid α)
namespace associates
open associated
protected def mk {α : Type*} [monoid α] (a : α) : associates α :=
⟦ a ⟧
theorem mk_eq_mk_iff_associated [monoid α] {a b : α} :
associates.mk a = associates.mk b ↔ a ~ᵤ b :=
iff.intro quotient.exact quot.sound
theorem quotient_mk_eq_mk [monoid α] (a : α) : ⟦ a ⟧ = associates.mk a := rfl
theorem quot_mk_eq_mk [monoid α] (a : α) : quot.mk setoid.r a = associates.mk a := rfl
theorem forall_associated [monoid α] {p : associates α → Prop} :
(∀a, p a) ↔ (∀a, p (associates.mk a)) :=
iff.intro
(assume h a, h _)
(assume h a, quotient.induction_on a h)
instance [monoid α] : has_one (associates α) := ⟨⟦ 1 ⟧⟩
theorem one_eq_mk_one [monoid α] : (1 : associates α) = associates.mk 1 := rfl
instance [monoid α] : has_bot (associates α) := ⟨1⟩
section comm_monoid
variable [comm_monoid α]
instance : has_mul (associates α) :=
⟨λa' b', quotient.lift_on₂ a' b' (λa b, ⟦ a * b ⟧) $
assume a₁ a₂ b₁ b₂ ⟨c₁, h₁⟩ ⟨c₂, h₂⟩,
quotient.sound $ ⟨c₁ * c₂, by simp [h₁.symm, h₂.symm, mul_assoc, mul_comm, mul_left_comm]⟩⟩
theorem mk_mul_mk {x y : α} : associates.mk x * associates.mk y = associates.mk (x * y) :=
rfl
instance : comm_monoid (associates α) :=
{ one := 1,
mul := (*),
mul_one := assume a', quotient.induction_on a' $
assume a, show ⟦a * 1⟧ = ⟦ a ⟧, by simp,
one_mul := assume a', quotient.induction_on a' $
assume a, show ⟦1 * a⟧ = ⟦ a ⟧, by simp,
mul_assoc := assume a' b' c', quotient.induction_on₃ a' b' c' $
assume a b c, show ⟦a * b * c⟧ = ⟦a * (b * c)⟧, by rw [mul_assoc],
mul_comm := assume a' b', quotient.induction_on₂ a' b' $
assume a b, show ⟦a * b⟧ = ⟦b * a⟧, by rw [mul_comm] }
instance : preorder (associates α) :=
{ le := λa b, ∃c, a * c = b,
le_refl := assume a, ⟨1, by simp⟩,
le_trans := assume a b c ⟨f₁, h₁⟩ ⟨f₂, h₂⟩, ⟨f₁ * f₂, h₂ ▸ h₁ ▸ (mul_assoc _ _ _).symm⟩}
instance : has_dvd (associates α) := ⟨(≤)⟩
@[simp] lemma mk_one : associates.mk (1 : α) = 1 := rfl
lemma mk_pow (a : α) (n : ℕ) : associates.mk (a ^ n) = (associates.mk a) ^ n :=
by induction n; simp [*, pow_succ, associates.mk_mul_mk.symm]
lemma dvd_eq_le : ((∣) : associates α → associates α → Prop) = (≤) := rfl
theorem prod_mk {p : multiset α} : (p.map associates.mk).prod = associates.mk p.prod :=
multiset.induction_on p (by simp; refl) $ assume a s ih, by simp [ih]; refl
theorem rel_associated_iff_map_eq_map {p q : multiset α} :
multiset.rel associated p q ↔ p.map associates.mk = q.map associates.mk :=
by rw [← multiset.rel_eq];
simp [multiset.rel_map_left, multiset.rel_map_right, mk_eq_mk_iff_associated]
theorem mul_eq_one_iff {x y : associates α} : x * y = 1 ↔ (x = 1 ∧ y = 1) :=
iff.intro
(quotient.induction_on₂ x y $ assume a b h,
have a * b ~ᵤ 1, from quotient.exact h,
⟨quotient.sound $ associated_one_of_associated_mul_one this,
quotient.sound $ associated_one_of_associated_mul_one $ by rwa [mul_comm] at this⟩)
(by simp {contextual := tt})
theorem prod_eq_one_iff {p : multiset (associates α)} :
p.prod = 1 ↔ (∀a ∈ p, (a:associates α) = 1) :=
multiset.induction_on p
(by simp)
(by simp [mul_eq_one_iff, or_imp_distrib, forall_and_distrib] {contextual := tt})
theorem coe_unit_eq_one : ∀u:units (associates α), (u : associates α) = 1
| ⟨u, v, huv, hvu⟩ := by rw [mul_eq_one_iff] at huv; exact huv.1
theorem is_unit_iff_eq_one (a : associates α) : is_unit a ↔ a = 1 :=
iff.intro
(assume ⟨u, h⟩, h.symm ▸ coe_unit_eq_one _)
(assume h, h.symm ▸ is_unit_one)
theorem is_unit_mk {a : α} : is_unit (associates.mk a) ↔ is_unit a :=
calc is_unit (associates.mk a) ↔ a ~ᵤ 1 :
by rw [is_unit_iff_eq_one, one_eq_mk_one, mk_eq_mk_iff_associated]
... ↔ is_unit a : associated_one_iff_is_unit
section order
theorem mul_mono {a b c d : associates α} (h₁ : a ≤ b) (h₂ : c ≤ d) :
a * c ≤ b * d :=
let ⟨x, hx⟩ := h₁, ⟨y, hy⟩ := h₂ in
⟨x * y, by simp [hx.symm, hy.symm, mul_comm, mul_assoc, mul_left_comm]⟩
theorem one_le {a : associates α} : 1 ≤ a :=
⟨a, one_mul a⟩
theorem prod_le_prod {p q : multiset (associates α)} (h : p ≤ q) : p.prod ≤ q.prod :=
begin
haveI := classical.dec_eq (associates α),
haveI := classical.dec_eq α,
suffices : p.prod ≤ (p + (q - p)).prod, { rwa [multiset.add_sub_of_le h] at this },
suffices : p.prod * 1 ≤ p.prod * (q - p).prod, { simpa },
exact mul_mono (le_refl p.prod) one_le
end
theorem le_mul_right {a b : associates α} : a ≤ a * b := ⟨b, rfl⟩
theorem le_mul_left {a b : associates α} : a ≤ b * a :=
by rw [mul_comm]; exact le_mul_right
end order
end comm_monoid
instance [has_zero α] [monoid α] : has_zero (associates α) := ⟨⟦ 0 ⟧⟩
instance [has_zero α] [monoid α] : has_top (associates α) := ⟨0⟩
section comm_semiring
variables [comm_semiring α]
@[simp] theorem mk_zero_eq (a : α) : associates.mk a = 0 ↔ a = 0 :=
⟨assume h, (associated_zero_iff_eq_zero a).1 $ quotient.exact h, assume h, h.symm ▸ rfl⟩
@[simp] theorem mul_zero : ∀(a : associates α), a * 0 = 0 :=
by rintros ⟨a⟩; show associates.mk (a * 0) = associates.mk 0; rw [mul_zero]
@[simp] protected theorem zero_mul : ∀(a : associates α), 0 * a = 0 :=
by rintros ⟨a⟩; show associates.mk (0 * a) = associates.mk 0; rw [zero_mul]
theorem mk_eq_zero_iff_eq_zero {a : α} : associates.mk a = 0 ↔ a = 0 :=
calc associates.mk a = 0 ↔ (a ~ᵤ 0) : mk_eq_mk_iff_associated
... ↔ a = 0 : associated_zero_iff_eq_zero a
theorem dvd_of_mk_le_mk {a b : α} : associates.mk a ≤ associates.mk b → a ∣ b
| ⟨c', hc'⟩ := (quotient.induction_on c' $ assume c hc,
let ⟨d, hd⟩ := (quotient.exact hc).symm in
⟨(↑d⁻¹) * c,
calc b = (a * c) * ↑d⁻¹ : by rw [← hd, mul_assoc, units.mul_inv, mul_one]
... = a * (↑d⁻¹ * c) : by ac_refl⟩) hc'
theorem mk_le_mk_of_dvd {a b : α} : a ∣ b → associates.mk a ≤ associates.mk b :=
assume ⟨c, hc⟩, ⟨associates.mk c, by simp [hc]; refl⟩
theorem mk_le_mk_iff_dvd_iff {a b : α} : associates.mk a ≤ associates.mk b ↔ a ∣ b :=
iff.intro dvd_of_mk_le_mk mk_le_mk_of_dvd
def prime (p : associates α) : Prop := p ≠ 0 ∧ p ≠ 1 ∧ (∀a b, p ≤ a * b → p ≤ a ∨ p ≤ b)
lemma prime.ne_zero {p : associates α} (hp : prime p) : p ≠ 0 :=
hp.1
lemma prime.ne_one {p : associates α} (hp : prime p) : p ≠ 1 :=
hp.2.1
lemma prime.le_or_le {p : associates α} (hp : prime p) {a b : associates α} (h : p ≤ a * b) :
p ≤ a ∨ p ≤ b :=
hp.2.2 a b h
lemma exists_mem_multiset_le_of_prime {s : multiset (associates α)} {p : associates α}
(hp : prime p) :
p ≤ s.prod → ∃a∈s, p ≤ a :=
multiset.induction_on s (assume ⟨d, eq⟩, (hp.ne_one (mul_eq_one_iff.1 eq).1).elim) $
assume a s ih h,
have p ≤ a * s.prod, by simpa using h,
match hp.le_or_le this with
| or.inl h := ⟨a, multiset.mem_cons_self a s, h⟩
| or.inr h := let ⟨a, has, h⟩ := ih h in ⟨a, multiset.mem_cons_of_mem has, h⟩
end
lemma prime_mk (p : α) : prime (associates.mk p) ↔ _root_.prime p :=
begin
rw [associates.prime, _root_.prime, forall_associated],
transitivity,
{ apply and_congr, refl,
apply and_congr, refl,
apply forall_congr, assume a,
exact forall_associated },
apply and_congr,
{ rw [(≠), mk_zero_eq] },
apply and_congr,
{ rw [(≠), ← is_unit_iff_eq_one, is_unit_mk], },
apply forall_congr, assume a,
apply forall_congr, assume b,
rw [mk_mul_mk, mk_le_mk_iff_dvd_iff, mk_le_mk_iff_dvd_iff, mk_le_mk_iff_dvd_iff]
end
end comm_semiring
section integral_domain
variable [integral_domain α]
instance : partial_order (associates α) :=
{ le_antisymm := assume a' b',
quotient.induction_on₂ a' b' $ assume a b ⟨f₁', h₁⟩ ⟨f₂', h₂⟩,
(quotient.induction_on₂ f₁' f₂' $ assume f₁ f₂ h₁ h₂,
let ⟨c₁, h₁⟩ := quotient.exact h₁, ⟨c₂, h₂⟩ := quotient.exact h₂ in
quotient.sound $ associated_of_dvd_dvd
(h₁ ▸ dvd_mul_of_dvd_left (dvd_mul_right _ _) _)
(h₂ ▸ dvd_mul_of_dvd_left (dvd_mul_right _ _) _)) h₁ h₂
.. associates.preorder }
instance : lattice.order_bot (associates α) :=
{ bot := 1,
bot_le := assume a, one_le,
.. associates.partial_order }
instance : lattice.order_top (associates α) :=
{ top := 0,
le_top := assume a, ⟨0, mul_zero a⟩,
.. associates.partial_order }
theorem zero_ne_one : (0 : associates α) ≠ 1 :=
assume h,
have (0 : α) ~ᵤ 1, from quotient.exact h,
have (0 : α) = 1, from ((associated_zero_iff_eq_zero 1).1 this.symm).symm,
zero_ne_one this
theorem mul_eq_zero_iff {x y : associates α} : x * y = 0 ↔ x = 0 ∨ y = 0 :=
iff.intro
(quotient.induction_on₂ x y $ assume a b h,
have a * b = 0, from (associated_zero_iff_eq_zero _).1 (quotient.exact h),
have a = 0 ∨ b = 0, from mul_eq_zero_iff_eq_zero_or_eq_zero.1 this,
this.imp (assume h, h.symm ▸ rfl) (assume h, h.symm ▸ rfl))
(by simp [or_imp_distrib] {contextual := tt})
theorem prod_eq_zero_iff {s : multiset (associates α)} :
s.prod = 0 ↔ (0 : associates α) ∈ s :=
multiset.induction_on s (by simp; exact zero_ne_one.symm) $
assume a s, by simp [mul_eq_zero_iff, @eq_comm _ 0 a] {contextual := tt}
theorem irreducible_mk_iff (a : α) : irreducible (associates.mk a) ↔ irreducible a :=
begin
simp [irreducible, is_unit_mk],
apply and_congr iff.rfl,
split,
{ assume h x y eq,
have : is_unit (associates.mk x) ∨ is_unit (associates.mk y),
from h _ _ (by rw [eq]; refl),
simpa [is_unit_mk] },
{ refine assume h x y, quotient.induction_on₂ x y (assume x y eq, _),
rcases quotient.exact eq.symm with ⟨u, eq⟩,
have : a = x * (y * u), by rwa [mul_assoc, eq_comm] at eq,
show is_unit (associates.mk x) ∨ is_unit (associates.mk y),
simpa [is_unit_mk] using h _ _ this }
end
lemma eq_of_mul_eq_mul_left :
∀(a b c : associates α), a ≠ 0 → a * b = a * c → b = c :=
begin
rintros ⟨a⟩ ⟨b⟩ ⟨c⟩ ha h,
rcases quotient.exact' h with ⟨u, hu⟩,
have hu : a * (b * ↑u) = a * c, { rwa [← mul_assoc] },
exact quotient.sound' ⟨u, eq_of_mul_eq_mul_left (mt (mk_zero_eq a).2 ha) hu⟩
end
lemma le_of_mul_le_mul_left (a b c : associates α) (ha : a ≠ 0) :
a * b ≤ a * c → b ≤ c
| ⟨d, hd⟩ := ⟨d, eq_of_mul_eq_mul_left a _ _ ha $ by rwa ← mul_assoc⟩
lemma one_or_eq_of_le_of_prime :
∀(p m : associates α), prime p → m ≤ p → (m = 1 ∨ m = p)
| _ m ⟨hp0, hp1, h⟩ ⟨d, rfl⟩ :=
match h m d (le_refl _) with
| or.inl h := classical.by_cases (assume : m = 0, by simp [this]) $
assume : m ≠ 0,
have m * d ≤ m * 1, by simpa using h,
have d ≤ 1, from associates.le_of_mul_le_mul_left m d 1 ‹m ≠ 0› this,
have d = 1, from lattice.bot_unique this,
by simp [this]
| or.inr h := classical.by_cases (assume : d = 0, by simp [this] at hp0; contradiction) $
assume : d ≠ 0,
have d * m ≤ d * 1, by simpa [mul_comm] using h,
or.inl $ lattice.bot_unique $ associates.le_of_mul_le_mul_left d m 1 ‹d ≠ 0› this
end
end integral_domain
end associates
|
f2f5760981028de6d8895618e26400607558f3a4 | 0c1546a496eccfb56620165cad015f88d56190c5 | /library/init/meta/simp_tactic.lean | 85932737a04391964dd5f3521d417347ce400dc8 | [
"Apache-2.0"
] | permissive | Solertis/lean | 491e0939957486f664498fbfb02546e042699958 | 84188c5aa1673fdf37a082b2de8562dddf53df3f | refs/heads/master | 1,610,174,257,606 | 1,486,263,620,000 | 1,486,263,620,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 14,850 | 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.tactic init.meta.attribute init.meta.constructor_tactic
import init.meta.relation_tactics init.meta.occurrences
open tactic
meta constant simp_lemmas : Type
meta constant simp_lemmas.mk : simp_lemmas
meta constant simp_lemmas.join : simp_lemmas → simp_lemmas → simp_lemmas
meta constant simp_lemmas.erase : simp_lemmas → list name → simp_lemmas
meta constant simp_lemmas.mk_default_core : transparency → tactic simp_lemmas
meta constant simp_lemmas.add_core : transparency → simp_lemmas → expr → tactic simp_lemmas
meta constant simp_lemmas.add_simp_core : transparency → simp_lemmas → name → tactic simp_lemmas
meta constant simp_lemmas.add_congr_core : transparency → simp_lemmas → name → tactic simp_lemmas
meta def simp_lemmas.mk_default : tactic simp_lemmas :=
simp_lemmas.mk_default_core reducible
meta def simp_lemmas.add : simp_lemmas → expr → tactic simp_lemmas :=
simp_lemmas.add_core reducible
meta def simp_lemmas.add_simp : simp_lemmas → name → tactic simp_lemmas :=
simp_lemmas.add_simp_core reducible
meta def simp_lemmas.add_congr : simp_lemmas → name → tactic simp_lemmas :=
simp_lemmas.add_congr_core reducible
meta def simp_lemmas.append : simp_lemmas → list expr → tactic simp_lemmas
| sls [] := return sls
| sls (l::ls) := do
new_sls ← simp_lemmas.add sls l,
simp_lemmas.append new_sls ls
/- (simp_lemmas.rewrite_core m s prove R e) apply a simplification lemma from 's'
- 'prove' is used to discharge proof obligations.
- 'R' is the equivalence relation being used (e.g., 'eq', 'iff')
- 'e' is the expression to be "simplified"
Result (new_e, pr) is the new expression 'new_e' and a proof (pr : e R new_e) -/
meta constant simp_lemmas.rewrite_core : transparency → simp_lemmas → tactic unit → name → expr → tactic (expr × expr)
meta def simp_lemmas.rewrite : simp_lemmas → tactic unit → name → expr → tactic (expr × expr) :=
simp_lemmas.rewrite_core reducible
/- (simp_lemmas.drewrite s e) tries to rewrite 'e' using only refl lemmas in 's' -/
meta constant simp_lemmas.drewrite_core : transparency → simp_lemmas → expr → tactic expr
meta def simp_lemmas.drewrite : simp_lemmas → expr → tactic expr :=
simp_lemmas.drewrite_core reducible
/- (Definitional) Simplify the given expression using *only* reflexivity equality lemmas from the given set of lemmas.
The resulting expression is definitionally equal to the input. -/
meta constant simp_lemmas.dsimplify_core (max_steps : nat) (visit_instances : bool) : simp_lemmas → expr → tactic expr
meta constant is_valid_simp_lemma_cnst : transparency → name → tactic bool
meta constant is_valid_simp_lemma : transparency → expr → tactic bool
def default_max_steps := 10000000
meta def simp_lemmas.dsimplify : simp_lemmas → expr → tactic expr :=
simp_lemmas.dsimplify_core default_max_steps ff
meta constant simp_lemmas.pp : simp_lemmas → tactic format
namespace tactic
/- (get_eqn_lemmas_for deps d) returns the automatically generated equational lemmas for definition d.
If deps is tt, then lemmas for automatically generated auxiliary declarations used to define d are also included. -/
meta constant get_eqn_lemmas_for : bool → name → tactic (list name)
meta constant dsimplify_core
/- The user state type. -/
{α : Type}
/- Initial user data -/
(a : α)
(max_steps : nat)
/- If visit_instances = ff, then instance implicit arguments are not visited, but
tactic will canonize them. -/
(visit_instances : bool)
/- (pre a e) is invoked before visiting the children of subterm 'e',
if it succeeds the result (new_a, new_e, flag) where
- 'new_a' is the new value for the user data
- 'new_e' is a new expression that must be definitionally equal to 'e',
- 'flag' if tt 'new_e' children should be visited, and 'post' invoked. -/
(pre : α → expr → tactic (α × expr × bool))
/- (post a e) is invoked after visiting the children of subterm 'e',
The output is similar to (pre a e), but the 'flag' indicates whether
the new expression should be revisited or not. -/
(post : α → expr → tactic (α × expr × bool))
: expr → tactic (α × expr)
meta def dsimplify
(pre : expr → tactic (expr × bool))
(post : expr → tactic (expr × bool))
: expr → tactic expr :=
λ e, do (a, new_e) ← dsimplify_core () default_max_steps ff
(λ u e, do r ← pre e, return (u, r))
(λ u e, do r ← post e, return (u, r)) e,
return new_e
meta constant dunfold_expr_core : transparency → expr → tactic expr
meta def dunfold_expr : expr → tactic expr :=
dunfold_expr_core reducible
meta constant unfold_projection_core : transparency → expr → tactic expr
meta def unfold_projection : expr → tactic expr :=
unfold_projection_core reducible
meta def dunfold_occs_core (m : transparency) (max_steps : nat) (occs : occurrences) (cs : list name) (e : expr) : tactic expr :=
let unfold (c : nat) (e : expr) : tactic (nat × expr × bool) := do
guard (cs^.any e^.is_app_of),
new_e ← dunfold_expr_core m e,
if occs^.contains c
then return (c+1, new_e, tt)
else return (c+1, e, tt)
in do (c, new_e) ← dsimplify_core 1 max_steps tt unfold (λ c e, failed) e,
return new_e
meta def dunfold_core (m : transparency) (max_steps : nat) (cs : list name) (e : expr) : tactic expr :=
let unfold (u : unit) (e : expr) : tactic (unit × expr × bool) := do
guard (cs^.any e^.is_app_of),
new_e ← dunfold_expr_core m e,
return (u, new_e, tt)
in do (c, new_e) ← dsimplify_core () max_steps tt (λ c e, failed) unfold e,
return new_e
meta def dunfold : list name → tactic unit :=
λ cs, target >>= dunfold_core reducible default_max_steps cs >>= change
meta def dunfold_occs_of (occs : list nat) (c : name) : tactic unit :=
target >>= dunfold_occs_core reducible default_max_steps (occurrences.pos occs) [c] >>= change
meta def dunfold_core_at (occs : occurrences) (cs : list name) (h : expr) : tactic unit :=
do num_reverted ← revert h,
(expr.pi n bi d b : expr) ← target | failed,
new_d : expr ← dunfold_occs_core reducible default_max_steps occs cs d,
change $ expr.pi n bi new_d b,
intron num_reverted
meta def dunfold_at (cs : list name) (h : expr) : tactic unit :=
do num_reverted ← revert h,
(expr.pi n bi d b : expr) ← target | failed,
new_d : expr ← dunfold_core reducible default_max_steps cs d,
change $ expr.pi n bi new_d b,
intron num_reverted
structure delta_config :=
(max_steps := default_max_steps)
(visit_instances := tt)
private meta def is_delta_target (e : expr) (cs : list name) : bool :=
cs^.any (λ c,
if e^.is_app_of c then tt /- Exact match -/
else let f := e^.get_app_fn in
/- f is an auxiliary constant generated when compiling c -/
f^.is_constant && f^.const_name^.is_internal && to_bool (f^.const_name^.get_prefix = c))
/- Delta reduce the given constant names -/
meta def delta_core (cfg : delta_config) (cs : list name) (e : expr) : tactic expr :=
let unfold (u : unit) (e : expr) : tactic (unit × expr × bool) := do
guard (is_delta_target e cs),
(expr.const f_name f_lvls) ← return $ e^.get_app_fn | failed,
env ← get_env,
decl ← returnex $ env^.get f_name,
new_f ← returnopt $ decl^.instantiate_value_univ_params f_lvls,
new_e ← beta (expr.mk_app new_f e^.get_app_args),
return (u, new_e, tt)
in do (c, new_e) ← dsimplify_core () cfg^.max_steps cfg^.visit_instances (λ c e, failed) unfold e,
return new_e
meta def delta (cs : list name) : tactic unit :=
target >>= delta_core {} cs >>= change
meta def delta_at (cs : list name) (h : expr) : tactic unit :=
do num_reverted ← revert h,
(expr.pi n bi d b : expr) ← target | failed,
new_d : expr ← delta_core {} cs d,
change $ expr.pi n bi new_d b,
intron num_reverted
structure simplify_config :=
(max_steps : nat := default_max_steps)
(contextual : bool := ff)
(lift_eq : bool := tt)
(canonize_instances : bool := tt)
(canonize_proofs : bool := ff)
(use_axioms : bool := tt)
meta constant simplify_core
(c : simplify_config)
(s : simp_lemmas)
(r : name) :
expr → tactic (expr × expr)
meta constant ext_simplify_core
/- The user state type. -/
{α : Type}
/- Initial user data -/
(a : α)
(c : simplify_config)
/- Congruence and simplification lemmas.
Remark: the simplification lemmas at not applied automatically like in the simplify_core tactic.
the caller must use them at pre/post. -/
(s : simp_lemmas)
/- Tactic for dischaging hypothesis in conditional rewriting rules.
The argument 'α' is the current user state. -/
(prove : α → tactic α)
/- (pre a r s p e) is invoked before visiting the children of subterm 'e',
'r' is the simplification relation being used, 's' is the updated set of lemmas if 'contextual' is tt,
'p' is the "parent" expression (if there is one).
if it succeeds the result is (new_a, new_e, new_pr, flag) where
- 'new_a' is the new value for the user data
- 'new_e' is a new expression s.t. 'e r new_e'
- 'new_pr' is a proof for 'e r new_e', If it is none, the proof is assumed to be by reflexivity
- 'flag' if tt 'new_e' children should be visited, and 'post' invoked. -/
(pre : α → simp_lemmas → name → option expr → expr → tactic (α × expr × option expr × bool))
/- (post a r s p e) is invoked after visiting the children of subterm 'e',
The output is similar to (pre a r s p e), but the 'flag' indicates whether
the new expression should be revisited or not. -/
(post : α → simp_lemmas → name → option expr → expr → tactic (α × expr × option expr × bool))
/- simplification relation -/
(r : name) :
expr → tactic (α × expr × expr)
meta def simplify (cfg : simplify_config) (S : simp_lemmas) (e : expr) : tactic (expr × expr) :=
do e_type ← infer_type e >>= whnf,
simplify_core cfg S `eq e
meta def simplify_goal_core (cfg : simplify_config) (S : simp_lemmas) : tactic unit :=
do (new_target, heq) ← target >>= simplify cfg S,
assert `htarget new_target, swap,
ht ← get_local `htarget,
mk_eq_mpr heq ht >>= exact
meta def simplify_goal (S : simp_lemmas) : tactic unit :=
simplify_goal_core {} S
meta def simp : tactic unit :=
do S ← simp_lemmas.mk_default,
simplify_goal S >> try triv >> try (reflexivity_core reducible)
meta def simp_using (hs : list expr) : tactic unit :=
do S ← simp_lemmas.mk_default,
S ← S^.append hs,
simplify_goal S >> try triv
meta def ctx_simp : tactic unit :=
do S ← simp_lemmas.mk_default,
simplify_goal_core {contextual := tt} S >> try triv >> try (reflexivity_core reducible)
meta def dsimp_core (s : simp_lemmas) : tactic unit :=
target >>= s^.dsimplify >>= change
meta def dsimp : tactic unit :=
simp_lemmas.mk_default >>= dsimp_core
meta def dsimp_at_core (s : simp_lemmas) (h : expr) : tactic unit :=
do num_reverted : ℕ ← revert h,
(expr.pi n bi d b : expr) ← target | failed,
h_simp ← s^.dsimplify d,
change $ expr.pi n bi h_simp b,
intron num_reverted
meta def dsimp_at (h : expr) : tactic unit :=
do s ← simp_lemmas.mk_default, dsimp_at_core s h
private meta def is_equation : expr → bool
| (expr.pi n bi d b) := is_equation b
| e := match (expr.is_eq e) with (some a) := tt | none := ff end
private meta def collect_simps : list expr → tactic (list expr)
| [] := return []
| (h :: hs) := do
result ← collect_simps hs,
htype ← infer_type h >>= whnf,
if is_equation htype
then return (h :: result)
else do
pr ← is_prop htype,
return $ if pr then (h :: result) else result
meta def collect_ctx_simps : tactic (list expr) :=
local_context >>= collect_simps
/- Simplify target using all hypotheses in the local context. -/
meta def simp_using_hs : tactic unit :=
collect_ctx_simps >>= simp_using
meta def simp_core_at (extra_lemmas : list expr) (h : expr) : tactic unit :=
do when (expr.is_local_constant h = ff) (fail "tactic simp_at failed, the given expression is not a hypothesis"),
htype ← infer_type h,
S ← simp_lemmas.mk_default,
S ← S^.append extra_lemmas,
(new_htype, heq) ← simplify {} S htype,
assert (expr.local_pp_name h) new_htype,
mk_eq_mp heq h >>= exact,
try $ clear h
meta def simp_at : expr → tactic unit :=
simp_core_at []
meta def simp_at_using (hs : list expr) : expr → tactic unit :=
simp_core_at hs
meta def simp_at_using_hs (h : expr) : tactic unit :=
do hs ← collect_ctx_simps,
simp_core_at (list.filter (ne h) hs) h
meta def mk_eq_simp_ext (simp_ext : expr → tactic (expr × expr)) : tactic unit :=
do (lhs, rhs) ← target >>= match_eq,
(new_rhs, heq) ← simp_ext lhs,
unify rhs new_rhs,
exact heq
/- Simp attribute support -/
meta def to_simp_lemmas : simp_lemmas → list name → tactic simp_lemmas
| S [] := return S
| S (n::ns) := do S' ← S^.add_simp n, to_simp_lemmas S' ns
meta def mk_simp_attr (attr_name : name) : command :=
do t ← to_expr `(caching_user_attribute simp_lemmas),
a ← attr_name^.to_expr,
v ← to_expr `({ name := %%a,
descr := "simplifier attribute",
mk_cache := λ ns, do {tactic.to_simp_lemmas simp_lemmas.mk ns},
dependencies := [`reducibility] } : caching_user_attribute simp_lemmas),
add_decl (declaration.defn attr_name [] t v reducibility_hints.abbrev ff),
attribute.register attr_name
meta def get_user_simp_lemmas (attr_name : name) : tactic simp_lemmas :=
if attr_name = `default then simp_lemmas.mk_default
else do
cnst ← return (expr.const attr_name []),
attr ← eval_expr (caching_user_attribute simp_lemmas) cnst,
caching_user_attribute.get_cache attr
meta def join_user_simp_lemmas_core : simp_lemmas → list name → tactic simp_lemmas
| S [] := return S
| S (attr_name::R) := do S' ← get_user_simp_lemmas attr_name, join_user_simp_lemmas_core (S^.join S') R
meta def join_user_simp_lemmas : list name → tactic simp_lemmas
| [] := simp_lemmas.mk_default
| attr_names := join_user_simp_lemmas_core simp_lemmas.mk attr_names
/- Normalize numerical expression, returns a pair (n, pr) where n is the resultant numeral,
and pr is a proof that the input argument is equal to n. -/
meta constant norm_num : expr → tactic (expr × expr)
end tactic
export tactic (mk_simp_attr)
|
67dae9d20bd31bf6a7e808a90a8d963b984ee77e | 8cb37a089cdb4af3af9d8bf1002b417e407a8e9e | /library/data/rbtree/default.lean | 3e4107221da6759b491a19fb928070b029c2cbeb | [
"Apache-2.0"
] | permissive | kbuzzard/lean | ae3c3db4bb462d750dbf7419b28bafb3ec983ef7 | ed1788fd674bb8991acffc8fca585ec746711928 | refs/heads/master | 1,620,983,366,617 | 1,618,937,600,000 | 1,618,937,600,000 | 359,886,396 | 1 | 0 | Apache-2.0 | 1,618,936,987,000 | 1,618,936,987,000 | null | UTF-8 | Lean | false | false | 188 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import data.rbtree.main
|
3ed539295d1389e628643b723acfe0445b07b210 | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/linear_algebra/multilinear.lean | 0e4272e0c33c846e351f21e26eee5a9711503a4c | [
"Apache-2.0"
] | permissive | ilitzroth/mathlib | ea647e67f1fdfd19a0f7bdc5504e8acec6180011 | 5254ef14e3465f6504306132fe3ba9cec9ffff16 | refs/heads/master | 1,680,086,661,182 | 1,617,715,647,000 | 1,617,715,647,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 53,709 | 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 linear_algebra.basic
import algebra.algebra.basic
import data.fintype.sort
/-!
# Multilinear maps
We define multilinear maps as maps from `Π(i : ι), M₁ i` to `M₂` which are linear in each
coordinate. Here, `M₁ i` and `M₂` are modules over a ring `R`, and `ι` is an arbitrary type
(although some statements will require it to be a fintype). This space, denoted by
`multilinear_map R M₁ M₂`, inherits a module structure by pointwise addition and multiplication.
## Main definitions
* `multilinear_map R M₁ M₂` is the space of multilinear maps from `Π(i : ι), M₁ i` to `M₂`.
* `f.map_smul` is the multiplicativity of the multilinear map `f` along each coordinate.
* `f.map_add` is the additivity of the multilinear map `f` along each coordinate.
* `f.map_smul_univ` expresses the multiplicativity of `f` over all coordinates at the same time,
writing `f (λi, c i • m i)` as `(∏ i, c i) • f m`.
* `f.map_add_univ` expresses the additivity of `f` over all coordinates at the same time, writing
`f (m + m')` as the sum over all subsets `s` of `ι` of `f (s.piecewise m m')`.
* `f.map_sum` expresses `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` as the sum of
`f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all possible functions.
We also register isomorphisms corresponding to currying or uncurrying variables, transforming a
multilinear function `f` on `n+1` variables into a linear function taking values in multilinear
functions in `n` variables, and into a multilinear function in `n` variables taking values in linear
functions. These operations are called `f.curry_left` and `f.curry_right` respectively
(with inverses `f.uncurry_left` and `f.uncurry_right`). These operations induce linear equivalences
between spaces of multilinear functions in `n+1` variables and spaces of linear functions into
multilinear functions in `n` variables (resp. multilinear functions in `n` variables taking values
in linear functions), called respectively `multilinear_curry_left_equiv` and
`multilinear_curry_right_equiv`.
## Implementation notes
Expressing that a map is linear along the `i`-th coordinate when all other coordinates are fixed
can be done in two (equivalent) different ways:
* fixing a vector `m : Π(j : ι - i), M₁ j.val`, and then choosing separately the `i`-th coordinate
* fixing a vector `m : Πj, M₁ j`, and then modifying its `i`-th coordinate
The second way is more artificial as the value of `m` at `i` is not relevant, but it has the
advantage of avoiding subtype inclusion issues. This is the definition we use, based on
`function.update` that allows to change the value of `m` at `i`.
-/
open function fin set
open_locale big_operators
universes u v v' v₁ v₂ v₃ w u'
variables {R : Type u} {ι : Type u'} {n : ℕ}
{M : fin n.succ → Type v} {M₁ : ι → Type v₁} {M₂ : Type v₂} {M₃ : Type v₃} {M' : Type v'}
[decidable_eq ι]
/-- Multilinear maps over the ring `R`, from `Πi, M₁ i` to `M₂` where `M₁ i` and `M₂` are modules
over `R`. -/
structure multilinear_map (R : Type u) {ι : Type u'} (M₁ : ι → Type v) (M₂ : Type w)
[decidable_eq ι] [semiring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M₂]
[∀i, semimodule R (M₁ i)] [semimodule R M₂] :=
(to_fun : (Πi, M₁ i) → M₂)
(map_add' : ∀(m : Πi, M₁ i) (i : ι) (x y : M₁ i),
to_fun (update m i (x + y)) = to_fun (update m i x) + to_fun (update m i y))
(map_smul' : ∀(m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i),
to_fun (update m i (c • x)) = c • to_fun (update m i x))
namespace multilinear_map
section semiring
variables [semiring R]
[∀i, add_comm_monoid (M i)] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M₂] [add_comm_monoid M₃]
[add_comm_monoid M']
[∀i, semimodule R (M i)] [∀i, semimodule R (M₁ i)] [semimodule R M₂] [semimodule R M₃]
[semimodule R M']
(f f' : multilinear_map R M₁ M₂)
instance : has_coe_to_fun (multilinear_map R M₁ M₂) := ⟨_, to_fun⟩
initialize_simps_projections multilinear_map (to_fun → apply)
@[simp] lemma to_fun_eq_coe : f.to_fun = f := rfl
@[simp] lemma coe_mk (f : (Π i, M₁ i) → M₂) (h₁ h₂ ) :
⇑(⟨f, h₁, h₂⟩ : multilinear_map R M₁ M₂) = f := rfl
theorem congr_fun {f g : multilinear_map R M₁ M₂} (h : f = g) (x : Π i, M₁ i) : f x = g x :=
congr_arg (λ h : multilinear_map R M₁ M₂, h x) h
theorem congr_arg (f : multilinear_map R M₁ M₂) {x y : Π i, M₁ i} (h : x = y) : f x = f y :=
congr_arg (λ x : Π i, M₁ i, f x) h
theorem coe_inj ⦃f g : multilinear_map R M₁ M₂⦄ (h : ⇑f = g) : f = g :=
by cases f; cases g; cases h; refl
@[ext] theorem ext {f f' : multilinear_map R M₁ M₂} (H : ∀ x, f x = f' x) : f = f' :=
coe_inj (funext H)
theorem ext_iff {f g : multilinear_map R M₁ M₂} : f = g ↔ ∀ x, f x = g x :=
⟨λ h x, h ▸ rfl, λ h, ext h⟩
@[simp] lemma map_add (m : Πi, M₁ i) (i : ι) (x y : M₁ i) :
f (update m i (x + y)) = f (update m i x) + f (update m i y) :=
f.map_add' m i x y
@[simp] lemma map_smul (m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i) :
f (update m i (c • x)) = c • f (update m i x) :=
f.map_smul' m i c x
lemma map_coord_zero {m : Πi, M₁ i} (i : ι) (h : m i = 0) : f m = 0 :=
begin
have : (0 : R) • (0 : M₁ i) = 0, by simp,
rw [← update_eq_self i m, h, ← this, f.map_smul, zero_smul]
end
@[simp] lemma map_update_zero (m : Πi, M₁ i) (i : ι) : f (update m i 0) = 0 :=
f.map_coord_zero i (update_same i 0 m)
@[simp] lemma map_zero [nonempty ι] : f 0 = 0 :=
begin
obtain ⟨i, _⟩ : ∃i:ι, i ∈ set.univ := set.exists_mem_of_nonempty ι,
exact map_coord_zero f i rfl
end
instance : has_add (multilinear_map R M₁ M₂) :=
⟨λf f', ⟨λx, f x + f' x, λm i x y, by simp [add_left_comm, add_assoc],
λm i c x, by simp [smul_add]⟩⟩
@[simp] lemma add_apply (m : Πi, M₁ i) : (f + f') m = f m + f' m := rfl
instance : has_zero (multilinear_map R M₁ M₂) :=
⟨⟨λ _, 0, λm i x y, by simp, λm i c x, by simp⟩⟩
instance : inhabited (multilinear_map R M₁ M₂) := ⟨0⟩
@[simp] lemma zero_apply (m : Πi, M₁ i) : (0 : multilinear_map R M₁ M₂) m = 0 := rfl
instance : add_comm_monoid (multilinear_map R M₁ M₂) :=
by refine {zero := 0, add := (+), ..};
intros; ext; simp [add_comm, add_left_comm]
@[simp] lemma sum_apply {α : Type*} (f : α → multilinear_map R M₁ M₂)
(m : Πi, M₁ i) : ∀ {s : finset α}, (∑ a in s, f a) m = ∑ a in s, f a m :=
begin
classical,
apply finset.induction,
{ rw finset.sum_empty, simp },
{ assume a s has H, rw finset.sum_insert has, simp [H, has] }
end
/-- If `f` is a multilinear map, then `f.to_linear_map m i` is the linear map obtained by fixing all
coordinates but `i` equal to those of `m`, and varying the `i`-th coordinate. -/
def to_linear_map (m : Πi, M₁ i) (i : ι) : M₁ i →ₗ[R] M₂ :=
{ to_fun := λx, f (update m i x),
map_add' := λx y, by simp,
map_smul' := λc x, by simp }
/-- The cartesian product of two multilinear maps, as a multilinear map. -/
def prod (f : multilinear_map R M₁ M₂) (g : multilinear_map R M₁ M₃) :
multilinear_map R M₁ (M₂ × M₃) :=
{ to_fun := λ m, (f m, g m),
map_add' := λ m i x y, by simp,
map_smul' := λ m i c x, by simp }
/-- Combine a family of multilinear maps with the same domain and codomains `M' i` into a
multilinear map taking values in the space of functions `Π i, M' i`. -/
@[simps] def pi {ι' : Type*} {M' : ι' → Type*} [Π i, add_comm_monoid (M' i)]
[Π i, semimodule R (M' i)] (f : Π i, multilinear_map R M₁ (M' i)) :
multilinear_map R M₁ (Π i, M' i) :=
{ to_fun := λ m i, f i m,
map_add' := λ m i x y, funext $ λ j, (f j).map_add _ _ _ _,
map_smul' := λ m i c x, funext $ λ j, (f j).map_smul _ _ _ _ }
/-- Given a multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset `s` of `k`
of these variables, one gets a new multilinear map on `fin k` by varying these variables, and fixing
the other ones equal to a given value `z`. It is denoted by `f.restr s hk z`, where `hk` is a
proof that the cardinality of `s` is `k`. The implicit identification between `fin k` and `s` that
we use is the canonical (increasing) bijection. -/
def restr {k n : ℕ} (f : multilinear_map R (λ i : fin n, M') M₂) (s : finset (fin n))
(hk : s.card = k) (z : M') :
multilinear_map R (λ i : fin k, M') M₂ :=
{ to_fun := λ v, f (λ j, if h : j ∈ s then v ((s.order_iso_of_fin hk).symm ⟨j, h⟩) else z),
map_add' := λ v i x y,
by { erw [dite_comp_equiv_update, dite_comp_equiv_update, dite_comp_equiv_update], simp },
map_smul' := λ v i c x, by { erw [dite_comp_equiv_update, dite_comp_equiv_update], simp } }
variable {R}
/-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build
an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the additivity of a
multilinear map along the first variable. -/
lemma cons_add (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (x y : M 0) :
f (cons (x+y) m) = f (cons x m) + f (cons y m) :=
by rw [← update_cons_zero x m (x+y), f.map_add, update_cons_zero, update_cons_zero]
/-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build
an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity
of a multilinear map along the first variable. -/
lemma cons_smul (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (c : R) (x : M 0) :
f (cons (c • x) m) = c • f (cons x m) :=
by rw [← update_cons_zero x m (c • x), f.map_smul, update_cons_zero]
/-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build
an element of `Π(i : fin (n+1)), M i` using `snoc`, one can express directly the additivity of a
multilinear map along the first variable. -/
lemma snoc_add (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (x y : M (last n)) :
f (snoc m (x+y)) = f (snoc m x) + f (snoc m y) :=
by rw [← update_snoc_last x m (x+y), f.map_add, update_snoc_last, update_snoc_last]
/-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build
an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity
of a multilinear map along the first variable. -/
lemma snoc_smul (f : multilinear_map R M M₂)
(m : Π(i : fin n), M i.cast_succ) (c : R) (x : M (last n)) :
f (snoc m (c • x)) = c • f (snoc m x) :=
by rw [← update_snoc_last x m (c • x), f.map_smul, update_snoc_last]
section
variables {M₁' : ι → Type*} [Π i, add_comm_monoid (M₁' i)] [Π i, semimodule R (M₁' i)]
/-- If `g` is a multilinear map and `f` is a collection of linear maps,
then `g (f₁ m₁, ..., fₙ mₙ)` is again a multilinear map, that we call
`g.comp_linear_map f`. -/
def comp_linear_map (g : multilinear_map R M₁' M₂) (f : Π i, M₁ i →ₗ[R] M₁' i) :
multilinear_map R M₁ M₂ :=
{ to_fun := λ m, g $ λ i, f i (m i),
map_add' := λ m i x y,
have ∀ j z, f j (update m i z j) = update (λ k, f k (m k)) i (f i z) j :=
λ j z, function.apply_update (λ k, f k) _ _ _ _,
by simp [this],
map_smul' := λ m i c x,
have ∀ j z, f j (update m i z j) = update (λ k, f k (m k)) i (f i z) j :=
λ j z, function.apply_update (λ k, f k) _ _ _ _,
by simp [this] }
@[simp] lemma comp_linear_map_apply (g : multilinear_map R M₁' M₂) (f : Π i, M₁ i →ₗ[R] M₁' i)
(m : Π i, M₁ i) :
g.comp_linear_map f m = g (λ i, f i (m i)) :=
rfl
end
/-- If one adds to a vector `m'` another vector `m`, but only for coordinates in a finset `t`, then
the image under a multilinear map `f` is the sum of `f (s.piecewise m m')` along all subsets `s` of
`t`. This is mainly an auxiliary statement to prove the result when `t = univ`, given in
`map_add_univ`, although it can be useful in its own right as it does not require the index set `ι`
to be finite.-/
lemma map_piecewise_add (m m' : Πi, M₁ i) (t : finset ι) :
f (t.piecewise (m + m') m') = ∑ s in t.powerset, f (s.piecewise m m') :=
begin
revert m',
refine finset.induction_on t (by simp) _,
assume i t hit Hrec m',
have A : (insert i t).piecewise (m + m') m' = update (t.piecewise (m + m') m') i (m i + m' i) :=
t.piecewise_insert _ _ _,
have B : update (t.piecewise (m + m') m') i (m' i) = t.piecewise (m + m') m',
{ ext j,
by_cases h : j = i,
{ rw h, simp [hit] },
{ simp [h] } },
let m'' := update m' i (m i),
have C : update (t.piecewise (m + m') m') i (m i) = t.piecewise (m + m'') m'',
{ ext j,
by_cases h : j = i,
{ rw h, simp [m'', hit] },
{ by_cases h' : j ∈ t; simp [h, hit, m'', h'] } },
rw [A, f.map_add, B, C, finset.sum_powerset_insert hit, Hrec, Hrec, add_comm],
congr' 1,
apply finset.sum_congr rfl (λs hs, _),
have : (insert i s).piecewise m m' = s.piecewise m m'',
{ ext j,
by_cases h : j = i,
{ rw h, simp [m'', finset.not_mem_of_mem_powerset_of_not_mem hs hit] },
{ by_cases h' : j ∈ s; simp [h, m'', h'] } },
rw this
end
/-- Additivity of a multilinear map along all coordinates at the same time,
writing `f (m + m')` as the sum of `f (s.piecewise m m')` over all sets `s`. -/
lemma map_add_univ [fintype ι] (m m' : Πi, M₁ i) :
f (m + m') = ∑ s : finset ι, f (s.piecewise m m') :=
by simpa using f.map_piecewise_add m m' finset.univ
section apply_sum
variables {α : ι → Type*} (g : Π i, α i → M₁ i) (A : Π i, finset (α i))
open_locale classical
open fintype finset
/-- If `f` is multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of
`f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ...,
`r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each
coordinate. Here, we give an auxiliary statement tailored for an inductive proof. Use instead
`map_sum_finset`. -/
lemma map_sum_finset_aux [fintype ι] {n : ℕ} (h : ∑ i, (A i).card = n) :
f (λ i, ∑ j in A i, g i j) = ∑ r in pi_finset A, f (λ i, g i (r i)) :=
begin
induction n using nat.strong_induction_on with n IH generalizing A,
-- If one of the sets is empty, then all the sums are zero
by_cases Ai_empty : ∃ i, A i = ∅,
{ rcases Ai_empty with ⟨i, hi⟩,
have : ∑ j in A i, g i j = 0, by convert sum_empty,
rw f.map_coord_zero i this,
have : pi_finset A = ∅,
{ apply finset.eq_empty_of_forall_not_mem (λ r hr, _),
have : r i ∈ A i := mem_pi_finset.mp hr i,
rwa hi at this },
convert sum_empty.symm },
push_neg at Ai_empty,
-- Otherwise, if all sets are at most singletons, then they are exactly singletons and the result
-- is again straightforward
by_cases Ai_singleton : ∀ i, (A i).card ≤ 1,
{ have Ai_card : ∀ i, (A i).card = 1,
{ assume i,
have pos : finset.card (A i) ≠ 0, by simp [finset.card_eq_zero, Ai_empty i],
have : finset.card (A i) ≤ 1 := Ai_singleton i,
exact le_antisymm this (nat.succ_le_of_lt (_root_.pos_iff_ne_zero.mpr pos)) },
have : ∀ (r : Π i, α i), r ∈ pi_finset A → f (λ i, g i (r i)) = f (λ i, ∑ j in A i, g i j),
{ assume r hr,
unfold_coes,
congr' with i,
have : ∀ j ∈ A i, g i j = g i (r i),
{ assume j hj,
congr,
apply finset.card_le_one_iff.1 (Ai_singleton i) hj,
exact mem_pi_finset.mp hr i },
simp only [finset.sum_congr rfl this, finset.mem_univ, finset.sum_const, Ai_card i,
one_nsmul] },
simp only [sum_congr rfl this, Ai_card, card_pi_finset, prod_const_one, one_nsmul,
sum_const] },
-- Remains the interesting case where one of the `A i`, say `A i₀`, has cardinality at least 2.
-- We will split into two parts `B i₀` and `C i₀` of smaller cardinality, let `B i = C i = A i`
-- for `i ≠ i₀`, apply the inductive assumption to `B` and `C`, and add up the corresponding
-- parts to get the sum for `A`.
push_neg at Ai_singleton,
obtain ⟨i₀, hi₀⟩ : ∃ i, 1 < (A i).card := Ai_singleton,
obtain ⟨j₁, j₂, hj₁, hj₂, j₁_ne_j₂⟩ : ∃ j₁ j₂, (j₁ ∈ A i₀) ∧ (j₂ ∈ A i₀) ∧ j₁ ≠ j₂ :=
finset.one_lt_card_iff.1 hi₀,
let B := function.update A i₀ (A i₀ \ {j₂}),
let C := function.update A i₀ {j₂},
have B_subset_A : ∀ i, B i ⊆ A i,
{ assume i,
by_cases hi : i = i₀,
{ rw hi, simp only [B, sdiff_subset, update_same]},
{ simp only [hi, B, update_noteq, ne.def, not_false_iff, finset.subset.refl] } },
have C_subset_A : ∀ i, C i ⊆ A i,
{ assume i,
by_cases hi : i = i₀,
{ rw hi, simp only [C, hj₂, finset.singleton_subset_iff, update_same] },
{ simp only [hi, C, update_noteq, ne.def, not_false_iff, finset.subset.refl] } },
-- split the sum at `i₀` as the sum over `B i₀` plus the sum over `C i₀`, to use additivity.
have A_eq_BC : (λ i, ∑ j in A i, g i j) =
function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in B i₀, g i₀ j + ∑ j in C i₀, g i₀ j),
{ ext i,
by_cases hi : i = i₀,
{ rw [hi],
simp only [function.update_same],
have : A i₀ = B i₀ ∪ C i₀,
{ simp only [B, C, function.update_same, finset.sdiff_union_self_eq_union],
symmetry,
simp only [hj₂, finset.singleton_subset_iff, union_eq_left_iff_subset] },
rw this,
apply finset.sum_union,
apply finset.disjoint_right.2 (λ j hj, _),
have : j = j₂, by { dsimp [C] at hj, simpa using hj },
rw this,
dsimp [B],
simp only [mem_sdiff, eq_self_iff_true, not_true, not_false_iff, finset.mem_singleton,
update_same, and_false] },
{ simp [hi] } },
have Beq : function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in B i₀, g i₀ j) =
(λ i, ∑ j in B i, g i j),
{ ext i,
by_cases hi : i = i₀,
{ rw hi, simp only [update_same] },
{ simp only [hi, B, update_noteq, ne.def, not_false_iff] } },
have Ceq : function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in C i₀, g i₀ j) =
(λ i, ∑ j in C i, g i j),
{ ext i,
by_cases hi : i = i₀,
{ rw hi, simp only [update_same] },
{ simp only [hi, C, update_noteq, ne.def, not_false_iff] } },
-- Express the inductive assumption for `B`
have Brec : f (λ i, ∑ j in B i, g i j) = ∑ r in pi_finset B, f (λ i, g i (r i)),
{ have : ∑ i, finset.card (B i) < ∑ i, finset.card (A i),
{ refine finset.sum_lt_sum (λ i hi, finset.card_le_of_subset (B_subset_A i))
⟨i₀, finset.mem_univ _, _⟩,
have : {j₂} ⊆ A i₀, by simp [hj₂],
simp only [B, finset.card_sdiff this, function.update_same, finset.card_singleton],
exact nat.pred_lt (ne_of_gt (lt_trans nat.zero_lt_one hi₀)) },
rw h at this,
exact IH _ this B rfl },
-- Express the inductive assumption for `C`
have Crec : f (λ i, ∑ j in C i, g i j) = ∑ r in pi_finset C, f (λ i, g i (r i)),
{ have : ∑ i, finset.card (C i) < ∑ i, finset.card (A i) :=
finset.sum_lt_sum (λ i hi, finset.card_le_of_subset (C_subset_A i))
⟨i₀, finset.mem_univ _, by simp [C, hi₀]⟩,
rw h at this,
exact IH _ this C rfl },
have D : disjoint (pi_finset B) (pi_finset C),
{ have : disjoint (B i₀) (C i₀), by simp [B, C],
exact pi_finset_disjoint_of_disjoint B C this },
have pi_BC : pi_finset A = pi_finset B ∪ pi_finset C,
{ apply finset.subset.antisymm,
{ assume r hr,
by_cases hri₀ : r i₀ = j₂,
{ apply finset.mem_union_right,
apply mem_pi_finset.2 (λ i, _),
by_cases hi : i = i₀,
{ have : r i₀ ∈ C i₀, by simp [C, hri₀],
convert this },
{ simp [C, hi, mem_pi_finset.1 hr i] } },
{ apply finset.mem_union_left,
apply mem_pi_finset.2 (λ i, _),
by_cases hi : i = i₀,
{ have : r i₀ ∈ B i₀,
by simp [B, hri₀, mem_pi_finset.1 hr i₀],
convert this },
{ simp [B, hi, mem_pi_finset.1 hr i] } } },
{ exact finset.union_subset (pi_finset_subset _ _ (λ i, B_subset_A i))
(pi_finset_subset _ _ (λ i, C_subset_A i)) } },
rw A_eq_BC,
simp only [multilinear_map.map_add, Beq, Ceq, Brec, Crec, pi_BC],
rw ← finset.sum_union D,
end
/-- If `f` is multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of
`f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ...,
`r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each
coordinate. -/
lemma map_sum_finset [fintype ι] :
f (λ i, ∑ j in A i, g i j) = ∑ r in pi_finset A, f (λ i, g i (r i)) :=
f.map_sum_finset_aux _ _ rfl
/-- If `f` is multilinear, then `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` is the sum of
`f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions `r`. This follows from
multilinearity by expanding successively with respect to each coordinate. -/
lemma map_sum [fintype ι] [∀ i, fintype (α i)] :
f (λ i, ∑ j, g i j) = ∑ r : Π i, α i, f (λ i, g i (r i)) :=
f.map_sum_finset g (λ i, finset.univ)
lemma map_update_sum {α : Type*} (t : finset α) (i : ι) (g : α → M₁ i) (m : Π i, M₁ i):
f (update m i (∑ a in t, g a)) = ∑ a in t, f (update m i (g a)) :=
begin
induction t using finset.induction with a t has ih h,
{ simp },
{ simp [finset.sum_insert has, ih] }
end
end apply_sum
section restrict_scalar
variables (R) {A : Type*} [semiring A] [has_scalar R A] [Π (i : ι), semimodule A (M₁ i)]
[semimodule A M₂] [∀ i, is_scalar_tower R A (M₁ i)] [is_scalar_tower R A M₂]
/-- Reinterpret an `A`-multilinear map as an `R`-multilinear map, if `A` is an algebra over `R`
and their actions on all involved semimodules agree with the action of `R` on `A`. -/
def restrict_scalars (f : multilinear_map A M₁ M₂) : multilinear_map R M₁ M₂ :=
{ to_fun := f,
map_add' := f.map_add,
map_smul' := λ m i, (f.to_linear_map m i).map_smul_of_tower }
@[simp] lemma coe_restrict_scalars (f : multilinear_map A M₁ M₂) :
⇑(f.restrict_scalars R) = f := rfl
end restrict_scalar
section
variables {ι₁ ι₂ ι₃ : Type*} [decidable_eq ι₁] [decidable_eq ι₂] [decidable_eq ι₃]
/-- Transfer the arguments to a map along an equivalence between argument indices.
The naming is derived from `finsupp.dom_congr`, noting that here the permutation applies to the
domain of the domain. -/
@[simps apply]
def dom_dom_congr (σ : ι₁ ≃ ι₂) (m : multilinear_map R (λ i : ι₁, M₂) M₃) :
multilinear_map R (λ i : ι₂, M₂) M₃ :=
{ to_fun := λ v, m (λ i, v (σ i)),
map_add' := λ v i a b, by { simp_rw function.update_apply_equiv_apply v, rw m.map_add, },
map_smul' := λ v i a b, by { simp_rw function.update_apply_equiv_apply v, rw m.map_smul, }, }
lemma dom_dom_congr_trans (σ₁ : ι₁ ≃ ι₂) (σ₂ : ι₂ ≃ ι₃) (m : multilinear_map R (λ i : ι₁, M₂) M₃) :
m.dom_dom_congr (σ₁.trans σ₂) = (m.dom_dom_congr σ₁).dom_dom_congr σ₂ := rfl
lemma dom_dom_congr_mul (σ₁ : equiv.perm ι₁) (σ₂ : equiv.perm ι₁)
(m : multilinear_map R (λ i : ι₁, M₂) M₃) :
m.dom_dom_congr (σ₂ * σ₁) = (m.dom_dom_congr σ₁).dom_dom_congr σ₂ := rfl
/-- `multilinear_map.dom_dom_congr` as an equivalence.
This is declared separately because it does not work with dot notation. -/
@[simps apply symm_apply]
def dom_dom_congr_equiv (σ : ι₁ ≃ ι₂) :
multilinear_map R (λ i : ι₁, M₂) M₃ ≃+ multilinear_map R (λ i : ι₂, M₂) M₃ :=
{ to_fun := dom_dom_congr σ,
inv_fun := dom_dom_congr σ.symm,
left_inv := λ m, by {ext, simp},
right_inv := λ m, by {ext, simp},
map_add' := λ a b, by {ext, simp} }
end
end semiring
end multilinear_map
namespace linear_map
variables [semiring R]
[Πi, add_comm_monoid (M₁ i)] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M']
[∀i, semimodule R (M₁ i)] [semimodule R M₂] [semimodule R M₃] [semimodule R M']
/-- Composing a multilinear map with a linear map gives again a multilinear map. -/
def comp_multilinear_map (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂) :
multilinear_map R M₁ M₃ :=
{ to_fun := g ∘ f,
map_add' := λ m i x y, by simp,
map_smul' := λ m i c x, by simp }
@[simp] lemma coe_comp_multilinear_map (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂) :
⇑(g.comp_multilinear_map f) = g ∘ f := rfl
lemma comp_multilinear_map_apply (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂) (m : Π i, M₁ i) :
g.comp_multilinear_map f m = g (f m) := rfl
variables {ι₁ ι₂ : Type*} [decidable_eq ι₁] [decidable_eq ι₂]
@[simp] lemma comp_multilinear_map_dom_dom_congr (σ : ι₁ ≃ ι₂) (g : M₂ →ₗ[R] M₃)
(f : multilinear_map R (λ i : ι₁, M') M₂) :
(g.comp_multilinear_map f).dom_dom_congr σ = g.comp_multilinear_map (f.dom_dom_congr σ) :=
by { ext, simp }
end linear_map
namespace multilinear_map
section comm_semiring
variables [comm_semiring R] [∀i, add_comm_monoid (M₁ i)] [∀i, add_comm_monoid (M i)]
[add_comm_monoid M₂] [∀i, semimodule R (M i)] [∀i, semimodule R (M₁ i)] [semimodule R M₂]
(f f' : multilinear_map R M₁ M₂)
/-- If one multiplies by `c i` the coordinates in a finset `s`, then the image under a multilinear
map is multiplied by `∏ i in s, c i`. This is mainly an auxiliary statement to prove the result when
`s = univ`, given in `map_smul_univ`, although it can be useful in its own right as it does not
require the index set `ι` to be finite. -/
lemma map_piecewise_smul (c : ι → R) (m : Πi, M₁ i) (s : finset ι) :
f (s.piecewise (λi, c i • m i) m) = (∏ i in s, c i) • f m :=
begin
refine s.induction_on (by simp) _,
assume j s j_not_mem_s Hrec,
have A : function.update (s.piecewise (λi, c i • m i) m) j (m j) =
s.piecewise (λi, c i • m i) m,
{ ext i,
by_cases h : i = j,
{ rw h, simp [j_not_mem_s] },
{ simp [h] } },
rw [s.piecewise_insert, f.map_smul, A, Hrec],
simp [j_not_mem_s, mul_smul]
end
/-- Multiplicativity of a multilinear map along all coordinates at the same time,
writing `f (λi, c i • m i)` as `(∏ i, c i) • f m`. -/
lemma map_smul_univ [fintype ι] (c : ι → R) (m : Πi, M₁ i) :
f (λi, c i • m i) = (∏ i, c i) • f m :=
by simpa using map_piecewise_smul f c m finset.univ
section distrib_mul_action
variables {R' A : Type*} [monoid R'] [semiring A]
[Π i, semimodule A (M₁ i)] [distrib_mul_action R' M₂] [semimodule A M₂] [smul_comm_class A R' M₂]
instance : has_scalar R' (multilinear_map A M₁ M₂) := ⟨λ c f,
⟨λ m, c • f m, λm i x y, by simp [smul_add], λl i x d, by simp [←smul_comm x c] ⟩⟩
@[simp] lemma smul_apply (f : multilinear_map A M₁ M₂) (c : R') (m : Πi, M₁ i) :
(c • f) m = c • f m := rfl
instance : distrib_mul_action R' (multilinear_map A M₁ M₂) :=
{ one_smul := λ f, ext $ λ x, one_smul _ _,
mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul _ _ _,
smul_zero := λ r, ext $ λ x, smul_zero _,
smul_add := λ r f₁ f₂, ext $ λ x, smul_add _ _ _ }
end distrib_mul_action
section semimodule
variables {R' A : Type*} [semiring R'] [semiring A]
[Π i, semimodule A (M₁ i)] [semimodule A M₂]
[add_comm_monoid M₃] [semimodule R' M₃] [semimodule A M₃] [smul_comm_class A R' M₃]
/-- The space of multilinear maps over an algebra over `R` is a module over `R`, for the pointwise
addition and scalar multiplication. -/
instance [semimodule R' M₂] [smul_comm_class A R' M₂] : semimodule R' (multilinear_map A M₁ M₂) :=
{ add_smul := λ r₁ r₂ f, ext $ λ x, add_smul _ _ _,
zero_smul := λ f, ext $ λ x, zero_smul _ _ }
variables (M₂ M₃ R' A)
/-- `multilinear_map.dom_dom_congr` as a `linear_equiv`. -/
@[simps apply symm_apply]
def dom_dom_congr_linear_equiv {ι₁ ι₂} [decidable_eq ι₁] [decidable_eq ι₂] (σ : ι₁ ≃ ι₂) :
multilinear_map A (λ i : ι₁, M₂) M₃ ≃ₗ[R'] multilinear_map A (λ i : ι₂, M₂) M₃ :=
{ map_smul' := λ c f, by { ext, simp },
.. (dom_dom_congr_equiv σ : multilinear_map A (λ i : ι₁, M₂) M₃ ≃+
multilinear_map A (λ i : ι₂, M₂) M₃) }
end semimodule
section dom_coprod
open_locale tensor_product
variables {ι₁ ι₂ ι₃ ι₄ : Type*}
variables [decidable_eq ι₁] [decidable_eq ι₂][decidable_eq ι₃] [decidable_eq ι₄]
variables {N₁ : Type*} [add_comm_monoid N₁] [semimodule R N₁]
variables {N₂ : Type*} [add_comm_monoid N₂] [semimodule R N₂]
variables {N : Type*} [add_comm_monoid N] [semimodule R N]
/-- Given two multilinear maps `(ι₁ → N) → N₁` and `(ι₂ → N) → N₂`, this produces the map
`(ι₁ ⊕ ι₂ → N) → N₁ ⊗ N₂` by taking the coproduct of the domain and the tensor product
of the codomain.
This can be thought of as combining `equiv.sum_arrow_equiv_prod_arrow.symm` with
`tensor_product.map`, noting that the two operations can't be separated as the intermediate result
is not a `multilinear_map`.
While this can be generalized to work for dependent `Π i : ι₁, N'₁ i` instead of `ι₁ → N`, doing so
introduces `sum.elim N'₁ N'₂` types in the result which are difficult to work with and not defeq
to the simple case defined here. See [this zulip thread](
https://leanprover.zulipchat.com/#narrow/stream/217875-Is-there.20code.20for.20X.3F/topic/Instances.20on.20.60sum.2Eelim.20A.20B.20i.60/near/218484619).
-/
@[simps apply]
def dom_coprod
(a : multilinear_map R (λ _ : ι₁, N) N₁) (b : multilinear_map R (λ _ : ι₂, N) N₂) :
multilinear_map R (λ _ : ι₁ ⊕ ι₂, N) (N₁ ⊗[R] N₂) :=
{ to_fun := λ v, a (λ i, v (sum.inl i)) ⊗ₜ b (λ i, v (sum.inr i)),
map_add' := λ v i p q, by cases i; simp [tensor_product.add_tmul, tensor_product.tmul_add],
map_smul' := λ v i c p, by cases i; simp [tensor_product.smul_tmul', tensor_product.tmul_smul] }
/-- A more bundled version of `multilinear_map.dom_coprod` that maps
`((ι₁ → N) → N₁) ⊗ ((ι₂ → N) → N₂)` to `(ι₁ ⊕ ι₂ → N) → N₁ ⊗ N₂`. -/
def dom_coprod' :
multilinear_map R (λ _ : ι₁, N) N₁ ⊗[R] multilinear_map R (λ _ : ι₂, N) N₂ →ₗ[R]
multilinear_map R (λ _ : ι₁ ⊕ ι₂, N) (N₁ ⊗[R] N₂) :=
tensor_product.lift $ linear_map.mk₂ R (dom_coprod)
(λ m₁ m₂ n, by { ext, simp only [dom_coprod_apply, tensor_product.add_tmul, add_apply] })
(λ c m n, by { ext, simp only [dom_coprod_apply, tensor_product.smul_tmul', smul_apply] })
(λ m n₁ n₂, by { ext, simp only [dom_coprod_apply, tensor_product.tmul_add, add_apply] })
(λ c m n, by { ext, simp only [dom_coprod_apply, tensor_product.tmul_smul, smul_apply] })
@[simp]
lemma dom_coprod'_apply
(a : multilinear_map R (λ _ : ι₁, N) N₁) (b : multilinear_map R (λ _ : ι₂, N) N₂) :
dom_coprod' (a ⊗ₜ[R] b) = dom_coprod a b := rfl
/-- When passed an `equiv.sum_congr`, `multilinear_map.dom_dom_congr` distributes over
`multilinear_map.dom_coprod`. -/
lemma dom_coprod_dom_dom_congr_sum_congr
(a : multilinear_map R (λ _ : ι₁, N) N₁) (b : multilinear_map R (λ _ : ι₂, N) N₂)
(σa : ι₁ ≃ ι₃) (σb : ι₂ ≃ ι₄) :
(a.dom_coprod b).dom_dom_congr (σa.sum_congr σb) =
(a.dom_dom_congr σa).dom_coprod (b.dom_dom_congr σb) := rfl
end dom_coprod
section
variables (R ι) (A : Type*) [comm_semiring A] [algebra R A] [fintype ι]
/-- Given an `R`-algebra `A`, `mk_pi_algebra` is the multilinear map on `A^ι` associating
to `m` the product of all the `m i`.
See also `multilinear_map.mk_pi_algebra_fin` for a version that works with a non-commutative
algebra `A` but requires `ι = fin n`. -/
protected def mk_pi_algebra : multilinear_map R (λ i : ι, A) A :=
{ to_fun := λ m, ∏ i, m i,
map_add' := λ m i x y, by simp [finset.prod_update_of_mem, add_mul],
map_smul' := λ m i c x, by simp [finset.prod_update_of_mem] }
variables {R A ι}
@[simp] lemma mk_pi_algebra_apply (m : ι → A) :
multilinear_map.mk_pi_algebra R ι A m = ∏ i, m i :=
rfl
end
section
variables (R n) (A : Type*) [semiring A] [algebra R A]
/-- Given an `R`-algebra `A`, `mk_pi_algebra_fin` is the multilinear map on `A^n` associating
to `m` the product of all the `m i`.
See also `multilinear_map.mk_pi_algebra` for a version that assumes `[comm_semiring A]` but works
for `A^ι` with any finite type `ι`. -/
protected def mk_pi_algebra_fin : multilinear_map R (λ i : fin n, A) A :=
{ to_fun := λ m, (list.of_fn m).prod,
map_add' :=
begin
intros m i x y,
have : (list.fin_range n).index_of i < n,
by simpa using list.index_of_lt_length.2 (list.mem_fin_range i),
simp [list.of_fn_eq_map, (list.nodup_fin_range n).map_update, list.prod_update_nth, add_mul,
this, mul_add, add_mul]
end,
map_smul' :=
begin
intros m i c x,
have : (list.fin_range n).index_of i < n,
by simpa using list.index_of_lt_length.2 (list.mem_fin_range i),
simp [list.of_fn_eq_map, (list.nodup_fin_range n).map_update, list.prod_update_nth, this]
end }
variables {R A n}
@[simp] lemma mk_pi_algebra_fin_apply (m : fin n → A) :
multilinear_map.mk_pi_algebra_fin R n A m = (list.of_fn m).prod :=
rfl
lemma mk_pi_algebra_fin_apply_const (a : A) :
multilinear_map.mk_pi_algebra_fin R n A (λ _, a) = a ^ n :=
by simp
end
/-- Given an `R`-multilinear map `f` taking values in `R`, `f.smul_right z` is the map
sending `m` to `f m • z`. -/
def smul_right (f : multilinear_map R M₁ R) (z : M₂) : multilinear_map R M₁ M₂ :=
(linear_map.smul_right linear_map.id z).comp_multilinear_map f
@[simp] lemma smul_right_apply (f : multilinear_map R M₁ R) (z : M₂) (m : Π i, M₁ i) :
f.smul_right z m = f m • z :=
rfl
variables (R ι)
/-- The canonical multilinear map on `R^ι` when `ι` is finite, associating to `m` the product of
all the `m i` (multiplied by a fixed reference element `z` in the target module). See also
`mk_pi_algebra` for a more general version. -/
protected def mk_pi_ring [fintype ι] (z : M₂) : multilinear_map R (λ(i : ι), R) M₂ :=
(multilinear_map.mk_pi_algebra R ι R).smul_right z
variables {R ι}
@[simp] lemma mk_pi_ring_apply [fintype ι] (z : M₂) (m : ι → R) :
(multilinear_map.mk_pi_ring R ι z : (ι → R) → M₂) m = (∏ i, m i) • z := rfl
lemma mk_pi_ring_apply_one_eq_self [fintype ι] (f : multilinear_map R (λ(i : ι), R) M₂) :
multilinear_map.mk_pi_ring R ι (f (λi, 1)) = f :=
begin
ext m,
have : m = (λi, m i • 1), by { ext j, simp },
conv_rhs { rw [this, f.map_smul_univ] },
refl
end
end comm_semiring
section range_add_comm_group
variables [semiring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_group M₂]
[∀i, semimodule R (M₁ i)] [semimodule R M₂]
(f g : multilinear_map R M₁ M₂)
instance : has_neg (multilinear_map R M₁ M₂) :=
⟨λ f, ⟨λ m, - f m, λm i x y, by simp [add_comm], λm i c x, by simp⟩⟩
@[simp] lemma neg_apply (m : Πi, M₁ i) : (-f) m = - (f m) := rfl
instance : has_sub (multilinear_map R M₁ M₂) :=
⟨λ f g,
⟨λ m, f m - g m,
λ m i x y, by { simp only [map_add, sub_eq_add_neg, neg_add], cc },
λ m i c x, by { simp only [map_smul, smul_sub] }⟩⟩
@[simp] lemma sub_apply (m : Πi, M₁ i) : (f - g) m = f m - g m := rfl
instance : add_comm_group (multilinear_map R M₁ M₂) :=
by refine { zero := 0, add := (+), neg := has_neg.neg,
sub := has_sub.sub, sub_eq_add_neg := _, .. };
intros; ext; simp [add_comm, add_left_comm, sub_eq_add_neg]
end range_add_comm_group
section add_comm_group
variables [semiring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂]
[∀i, semimodule R (M₁ i)] [semimodule R M₂]
(f : multilinear_map R M₁ M₂)
@[simp] lemma map_neg (m : Πi, M₁ i) (i : ι) (x : M₁ i) :
f (update m i (-x)) = -f (update m i x) :=
eq_neg_of_add_eq_zero $ by rw [←map_add, add_left_neg, f.map_coord_zero i (update_same i 0 m)]
@[simp] lemma map_sub (m : Πi, M₁ i) (i : ι) (x y : M₁ i) :
f (update m i (x - y)) = f (update m i x) - f (update m i y) :=
by rw [sub_eq_add_neg, sub_eq_add_neg, map_add, map_neg]
end add_comm_group
section comm_semiring
variables [comm_semiring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂]
[∀i, semimodule R (M₁ i)] [semimodule R M₂]
/-- When `ι` is finite, multilinear maps on `R^ι` with values in `M₂` are in bijection with `M₂`,
as such a multilinear map is completely determined by its value on the constant vector made of ones.
We register this bijection as a linear equivalence in `multilinear_map.pi_ring_equiv`. -/
protected def pi_ring_equiv [fintype ι] : M₂ ≃ₗ[R] (multilinear_map R (λ(i : ι), R) M₂) :=
{ to_fun := λ z, multilinear_map.mk_pi_ring R ι z,
inv_fun := λ f, f (λi, 1),
map_add' := λ z z', by { ext m, simp [smul_add] },
map_smul' := λ c z, by { ext m, simp [smul_smul, mul_comm] },
left_inv := λ z, by simp,
right_inv := λ f, f.mk_pi_ring_apply_one_eq_self }
end comm_semiring
end multilinear_map
section currying
/-!
### Currying
We associate to a multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two
curried functions, named `f.curry_left` (which is a linear map on `E 0` taking values
in multilinear maps in `n` variables) and `f.curry_right` (wich is a multilinear map in `n`
variables taking values in linear maps on `E 0`). In both constructions, the variable that is
singled out is `0`, to take advantage of the operations `cons` and `tail` on `fin n`.
The inverse operations are called `uncurry_left` and `uncurry_right`.
We also register linear equiv versions of these correspondences, in
`multilinear_curry_left_equiv` and `multilinear_curry_right_equiv`.
-/
open multilinear_map
variables {R M M₂}
[comm_semiring R] [∀i, add_comm_monoid (M i)] [add_comm_monoid M'] [add_comm_monoid M₂]
[∀i, semimodule R (M i)] [semimodule R M'] [semimodule R M₂]
/-! #### Left currying -/
/-- Given a linear map `f` from `M 0` to multilinear maps on `n` variables,
construct the corresponding multilinear map on `n+1` variables obtained by concatenating
the variables, given by `m ↦ f (m 0) (tail m)`-/
def linear_map.uncurry_left
(f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) :
multilinear_map R M M₂ :=
{ to_fun := λm, f (m 0) (tail m),
map_add' := λm i x y, begin
by_cases h : i = 0,
{ subst i,
rw [update_same, update_same, update_same, f.map_add, add_apply,
tail_update_zero, tail_update_zero, tail_update_zero] },
{ rw [update_noteq (ne.symm h), update_noteq (ne.symm h), update_noteq (ne.symm h)],
revert x y,
rw ← succ_pred i h,
assume x y,
rw [tail_update_succ, map_add, tail_update_succ, tail_update_succ] }
end,
map_smul' := λm i c x, begin
by_cases h : i = 0,
{ subst i,
rw [update_same, update_same, tail_update_zero, tail_update_zero,
← smul_apply, f.map_smul] },
{ rw [update_noteq (ne.symm h), update_noteq (ne.symm h)],
revert x,
rw ← succ_pred i h,
assume x,
rw [tail_update_succ, tail_update_succ, map_smul] }
end }
@[simp] lemma linear_map.uncurry_left_apply
(f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) (m : Πi, M i) :
f.uncurry_left m = f (m 0) (tail m) := rfl
/-- Given a multilinear map `f` in `n+1` variables, split the first variable to obtain
a linear map into multilinear maps in `n` variables, given by `x ↦ (m ↦ f (cons x m))`. -/
def multilinear_map.curry_left
(f : multilinear_map R M M₂) :
M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂) :=
{ to_fun := λx,
{ to_fun := λm, f (cons x m),
map_add' := λm i y y', by simp,
map_smul' := λm i y c, by simp },
map_add' := λx y, by { ext m, exact cons_add f m x y },
map_smul' := λc x, by { ext m, exact cons_smul f m c x } }
@[simp] lemma multilinear_map.curry_left_apply
(f : multilinear_map R M M₂) (x : M 0) (m : Π(i : fin n), M i.succ) :
f.curry_left x m = f (cons x m) := rfl
@[simp] lemma linear_map.curry_uncurry_left
(f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) :
f.uncurry_left.curry_left = f :=
begin
ext m x,
simp only [tail_cons, linear_map.uncurry_left_apply, multilinear_map.curry_left_apply],
rw cons_zero
end
@[simp] lemma multilinear_map.uncurry_curry_left
(f : multilinear_map R M M₂) :
f.curry_left.uncurry_left = f :=
by { ext m, simp, }
variables (R M M₂)
/-- The space of multilinear maps on `Π(i : fin (n+1)), M i` is canonically isomorphic to
the space of linear maps from `M 0` to the space of multilinear maps on
`Π(i : fin n), M i.succ `, by separating the first variable. We register this isomorphism as a
linear isomorphism in `multilinear_curry_left_equiv R M M₂`.
The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these
unless you need the full framework of linear equivs. -/
def multilinear_curry_left_equiv :
(M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) ≃ₗ[R] (multilinear_map R M M₂) :=
{ to_fun := linear_map.uncurry_left,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, refl },
inv_fun := multilinear_map.curry_left,
left_inv := linear_map.curry_uncurry_left,
right_inv := multilinear_map.uncurry_curry_left }
variables {R M M₂}
/-! #### Right currying -/
/-- Given a multilinear map `f` in `n` variables to the space of linear maps from `M (last n)` to
`M₂`, construct the corresponding multilinear map on `n+1` variables obtained by concatenating
the variables, given by `m ↦ f (init m) (m (last n))`-/
def multilinear_map.uncurry_right
(f : (multilinear_map R (λ(i : fin n), M i.cast_succ) (M (last n) →ₗ[R] M₂))) :
multilinear_map R M M₂ :=
{ to_fun := λm, f (init m) (m (last n)),
map_add' := λm i x y, begin
by_cases h : i.val < n,
{ have : last n ≠ i := ne.symm (ne_of_lt h),
rw [update_noteq this, update_noteq this, update_noteq this],
revert x y,
rw [(cast_succ_cast_lt i h).symm],
assume x y,
rw [init_update_cast_succ, map_add, init_update_cast_succ, init_update_cast_succ,
linear_map.add_apply] },
{ revert x y,
rw eq_last_of_not_lt h,
assume x y,
rw [init_update_last, init_update_last, init_update_last,
update_same, update_same, update_same, linear_map.map_add] }
end,
map_smul' := λm i c x, begin
by_cases h : i.val < n,
{ have : last n ≠ i := ne.symm (ne_of_lt h),
rw [update_noteq this, update_noteq this],
revert x,
rw [(cast_succ_cast_lt i h).symm],
assume x,
rw [init_update_cast_succ, init_update_cast_succ, map_smul, linear_map.smul_apply] },
{ revert x,
rw eq_last_of_not_lt h,
assume x,
rw [update_same, update_same, init_update_last, init_update_last,
linear_map.map_smul] }
end }
@[simp] lemma multilinear_map.uncurry_right_apply
(f : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))) (m : Πi, M i) :
f.uncurry_right m = f (init m) (m (last n)) := rfl
/-- Given a multilinear map `f` in `n+1` variables, split the last variable to obtain
a multilinear map in `n` variables taking values in linear maps from `M (last n)` to `M₂`, given by
`m ↦ (x ↦ f (snoc m x))`. -/
def multilinear_map.curry_right (f : multilinear_map R M M₂) :
multilinear_map R (λ(i : fin n), M (fin.cast_succ i)) ((M (last n)) →ₗ[R] M₂) :=
{ to_fun := λm,
{ to_fun := λx, f (snoc m x),
map_add' := λx y, by rw f.snoc_add,
map_smul' := λc x, by rw f.snoc_smul },
map_add' := λm i x y, begin
ext z,
change f (snoc (update m i (x + y)) z)
= f (snoc (update m i x) z) + f (snoc (update m i y) z),
rw [snoc_update, snoc_update, snoc_update, f.map_add]
end,
map_smul' := λm i c x, begin
ext z,
change f (snoc (update m i (c • x)) z) = c • f (snoc (update m i x) z),
rw [snoc_update, snoc_update, f.map_smul]
end }
@[simp] lemma multilinear_map.curry_right_apply
(f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (x : M (last n)) :
f.curry_right m x = f (snoc m x) := rfl
@[simp] lemma multilinear_map.curry_uncurry_right
(f : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))) :
f.uncurry_right.curry_right = f :=
begin
ext m x,
simp only [snoc_last, multilinear_map.curry_right_apply, multilinear_map.uncurry_right_apply],
rw init_snoc
end
@[simp] lemma multilinear_map.uncurry_curry_right
(f : multilinear_map R M M₂) : f.curry_right.uncurry_right = f :=
by { ext m, simp }
variables (R M M₂)
/-- The space of multilinear maps on `Π(i : fin (n+1)), M i` is canonically isomorphic to
the space of linear maps from the space of multilinear maps on `Π(i : fin n), M i.cast_succ` to the
space of linear maps on `M (last n)`, by separating the last variable. We register this isomorphism
as a linear isomorphism in `multilinear_curry_right_equiv R M M₂`.
The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these
unless you need the full framework of linear equivs. -/
def multilinear_curry_right_equiv :
(multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))
≃ₗ[R] (multilinear_map R M M₂) :=
{ to_fun := multilinear_map.uncurry_right,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, rw [smul_apply], refl },
inv_fun := multilinear_map.curry_right,
left_inv := multilinear_map.curry_uncurry_right,
right_inv := multilinear_map.uncurry_curry_right }
namespace multilinear_map
variables {ι' : Type*} [decidable_eq ι'] [decidable_eq (ι ⊕ ι')] {R M₂}
/-- A multilinear map on `Π i : ι ⊕ ι', M'` defines a multilinear map on `Π i : ι, M'`
taking values in the space of multilinear maps on `Π i : ι', M'`. -/
def curry_sum (f : multilinear_map R (λ x : ι ⊕ ι', M') M₂) :
multilinear_map R (λ x : ι, M') (multilinear_map R (λ x : ι', M') M₂) :=
{ to_fun := λ u,
{ to_fun := λ v, f (sum.elim u v),
map_add' := λ v i x y, by simp only [← sum.update_elim_inr, f.map_add],
map_smul' := λ v i c x, by simp only [← sum.update_elim_inr, f.map_smul] },
map_add' := λ u i x y, ext $ λ v,
by simp only [multilinear_map.coe_mk, add_apply, ← sum.update_elim_inl, f.map_add],
map_smul' := λ u i c x, ext $ λ v,
by simp only [multilinear_map.coe_mk, smul_apply, ← sum.update_elim_inl, f.map_smul] }
@[simp] lemma curry_sum_apply (f : multilinear_map R (λ x : ι ⊕ ι', M') M₂)
(u : ι → M') (v : ι' → M') :
f.curry_sum u v = f (sum.elim u v) :=
rfl
/-- A multilinear map on `Π i : ι, M'` taking values in the space of multilinear maps
on `Π i : ι', M'` defines a multilinear map on `Π i : ι ⊕ ι', M'`. -/
def uncurry_sum (f : multilinear_map R (λ x : ι, M') (multilinear_map R (λ x : ι', M') M₂)) :
multilinear_map R (λ x : ι ⊕ ι', M') M₂ :=
{ to_fun := λ u, f (u ∘ sum.inl) (u ∘ sum.inr),
map_add' := λ u i x y, by cases i;
simp only [map_add, add_apply, sum.update_inl_comp_inl, sum.update_inl_comp_inr,
sum.update_inr_comp_inl, sum.update_inr_comp_inr],
map_smul' := λ u i c x, by cases i;
simp only [map_smul, smul_apply, sum.update_inl_comp_inl, sum.update_inl_comp_inr,
sum.update_inr_comp_inl, sum.update_inr_comp_inr] }
@[simp] lemma uncurry_sum_aux_apply
(f : multilinear_map R (λ x : ι, M') (multilinear_map R (λ x : ι', M') M₂)) (u : ι ⊕ ι' → M') :
f.uncurry_sum u = f (u ∘ sum.inl) (u ∘ sum.inr) :=
rfl
variables (ι ι' R M₂ M')
/-- Linear equivalence between the space of multilinear maps on `Π i : ι ⊕ ι', M'` and the space
of multilinear maps on `Π i : ι, M'` taking values in the space of multilinear maps
on `Π i : ι', M'`. -/
def curry_sum_equiv : multilinear_map R (λ x : ι ⊕ ι', M') M₂ ≃ₗ[R]
multilinear_map R (λ x : ι, M') (multilinear_map R (λ x : ι', M') M₂) :=
{ to_fun := curry_sum,
inv_fun := uncurry_sum,
left_inv := λ f, ext $ λ u, by simp,
right_inv := λ f, by { ext, simp },
map_add' := λ f g, by { ext, refl },
map_smul' := λ c f, by { ext, refl } }
variables {ι ι' R M₂ M'}
@[simp] lemma coe_curry_sum_equiv : ⇑(curry_sum_equiv R ι M₂ M' ι') = curry_sum := rfl
@[simp] lemma coe_curr_sum_equiv_symm : ⇑(curry_sum_equiv R ι M₂ M' ι').symm = uncurry_sum := rfl
variables (R M₂ M')
/-- If `s : finset (fin n)` is a finite set of cardinality `k` and its complement has cardinality
`l`, then the space of multilinear maps on `λ i : fin n, M'` is isomorphic to the space of
multilinear maps on `λ i : fin k, M'` taking values in the space of multilinear maps
on `λ i : fin l, M'`. -/
def curry_fin_finset {k l n : ℕ} {s : finset (fin n)} [decidable_pred (s : set (fin n))]
(hk : s.card = k) (hl : sᶜ.card = l) :
multilinear_map R (λ x : fin n, M') M₂ ≃ₗ[R]
multilinear_map R (λ x : fin k, M') (multilinear_map R (λ x : fin l, M') M₂) :=
(dom_dom_congr_linear_equiv M' M₂ R R (fin_sum_equiv_of_finset hk hl).symm).trans
(curry_sum_equiv R (fin k) M₂ M' (fin l))
variables {R M₂ M'}
@[simp]
lemma curry_fin_finset_apply {k l n : ℕ} {s : finset (fin n)} [decidable_pred (s : set (fin n))]
(hk : s.card = k) (hl : sᶜ.card = l) (f : multilinear_map R (λ x : fin n, M') M₂)
(mk : fin k → M') (ml : fin l → M') :
curry_fin_finset R M₂ M' hk hl f mk ml =
f (λ i, sum.elim mk ml ((fin_sum_equiv_of_finset hk hl).symm i)) :=
rfl
@[simp] lemma curry_fin_finset_symm_apply {k l n : ℕ} {s : finset (fin n)}
[decidable_pred (s : set (fin n))] (hk : s.card = k) (hl : sᶜ.card = l)
(f : multilinear_map R (λ x : fin k, M') (multilinear_map R (λ x : fin l, M') M₂))
(m : fin n → M') :
(curry_fin_finset R M₂ M' hk hl).symm f m =
f (λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inl i))
(λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inr i)) :=
rfl
@[simp] lemma curry_fin_finset_symm_apply_piecewise_const {k l n : ℕ} {s : finset (fin n)}
[decidable_pred (s : set (fin n))] (hk : s.card = k) (hl : sᶜ.card = l)
(f : multilinear_map R (λ x : fin k, M') (multilinear_map R (λ x : fin l, M') M₂)) (x y : M') :
(curry_fin_finset R M₂ M' hk hl).symm f (s.piecewise (λ _, x) (λ _, y)) = f (λ _, x) (λ _, y) :=
begin
rw curry_fin_finset_symm_apply, congr,
{ ext i, rw [fin_sum_equiv_of_finset_inl, finset.piecewise_eq_of_mem],
apply finset.order_emb_of_fin_mem },
{ ext i, rw [fin_sum_equiv_of_finset_inr, finset.piecewise_eq_of_not_mem],
exact finset.mem_compl.1 (finset.order_emb_of_fin_mem _ _ _) }
end
@[simp] lemma curry_fin_finset_symm_apply_const {k l n : ℕ} {s : finset (fin n)}
[decidable_pred (s : set (fin n))] (hk : s.card = k) (hl : sᶜ.card = l)
(f : multilinear_map R (λ x : fin k, M') (multilinear_map R (λ x : fin l, M') M₂)) (x : M') :
(curry_fin_finset R M₂ M' hk hl).symm f (λ _, x) = f (λ _, x) (λ _, x) :=
rfl
@[simp] lemma curry_fin_finset_apply_const {k l n : ℕ} {s : finset (fin n)}
[decidable_pred (s : set (fin n))]
(hk : s.card = k) (hl : sᶜ.card = l) (f : multilinear_map R (λ x : fin n, M') M₂) (x y : M') :
curry_fin_finset R M₂ M' hk hl f (λ _, x) (λ _, y) = f (s.piecewise (λ _, x) (λ _, y)) :=
begin
refine (curry_fin_finset_symm_apply_piecewise_const hk hl _ _ _).symm.trans _, -- `rw` fails
rw linear_equiv.symm_apply_apply
end
end multilinear_map
end currying
section submodule
variables {R M M₂}
[ring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M'] [add_comm_monoid M₂]
[∀i, semimodule R (M₁ i)] [semimodule R M'] [semimodule R M₂]
namespace multilinear_map
/-- The pushforward of an indexed collection of submodule `p i ⊆ M₁ i` by `f : M₁ → M₂`.
Note that this is not a submodule - it is not closed under addition. -/
def map [nonempty ι] (f : multilinear_map R M₁ M₂) (p : Π i, submodule R (M₁ i)) :
sub_mul_action R M₂ :=
{ carrier := f '' { v | ∀ i, v i ∈ p i},
smul_mem' := λ c _ ⟨x, hx, hf⟩, let ⟨i⟩ := ‹nonempty ι› in by {
refine ⟨update x i (c • x i), λ j, if hij : j = i then _ else _, hf ▸ _⟩,
{ rw [hij, update_same], exact (p i).smul_mem _ (hx i) },
{ rw [update_noteq hij], exact hx j },
{ rw [f.map_smul, update_eq_self] } } }
/-- The map is always nonempty. This lemma is needed to apply `sub_mul_action.zero_mem`. -/
lemma map_nonempty [nonempty ι] (f : multilinear_map R M₁ M₂) (p : Π i, submodule R (M₁ i)) :
(map f p : set M₂).nonempty :=
⟨f 0, 0, λ i, (p i).zero_mem, rfl⟩
/-- The range of a multilinear map, closed under scalar multiplication. -/
def range [nonempty ι] (f : multilinear_map R M₁ M₂) : sub_mul_action R M₂ :=
f.map (λ i, ⊤)
end multilinear_map
end submodule
|
bdaa43f863f7572dee3d77009f3e19bc72801ae8 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/data/sym/basic.lean | 0532c8e600ddf86978dad950282ecd8e3d0eaddc | [
"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 | 11,549 | lean | /-
Copyright (c) 2020 Kyle Miller All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kyle Miller
-/
import data.multiset.basic
import data.vector.basic
import data.setoid.basic
import tactic.apply_fun
/-!
# Symmetric powers
This file defines symmetric powers of a type. The nth symmetric power
consists of homogeneous n-tuples modulo permutations by the symmetric
group.
The special case of 2-tuples is called the symmetric square, which is
addressed in more detail in `data.sym.sym2`.
TODO: This was created as supporting material for `sym2`; it
needs a fleshed-out interface.
## Tags
symmetric powers
-/
open function
/--
The nth symmetric power is n-tuples up to permutation. We define it
as a subtype of `multiset` since these are well developed in the
library. We also give a definition `sym.sym'` in terms of vectors, and we
show these are equivalent in `sym.sym_equiv_sym'`.
-/
def sym (α : Type*) (n : ℕ) := {s : multiset α // s.card = n}
instance sym.has_coe (α : Type*) (n : ℕ) : has_coe (sym α n) (multiset α) := coe_subtype
/--
This is the `list.perm` setoid lifted to `vector`.
See note [reducible non-instances].
-/
@[reducible]
def vector.perm.is_setoid (α : Type*) (n : ℕ) : setoid (vector α n) :=
(list.is_setoid α).comap subtype.val
local attribute [instance] vector.perm.is_setoid
namespace sym
variables {α β : Type*} {n : ℕ} {s : sym α n} {a b : α}
lemma coe_injective : injective (coe : sym α n → multiset α) := subtype.coe_injective
@[simp, norm_cast] lemma coe_inj {s₁ s₂ : sym α n} : (s₁ : multiset α) = s₂ ↔ s₁ = s₂ :=
coe_injective.eq_iff
/--
Construct an element of the `n`th symmetric power from a multiset of cardinality `n`.
-/
@[simps, pattern]
abbreviation mk (m : multiset α) (h : m.card = n) : sym α n := ⟨m, h⟩
/--
The unique element in `sym α 0`.
-/
@[pattern] def nil : sym α 0 := ⟨0, multiset.card_zero⟩
/--
Inserts an element into the term of `sym α n`, increasing the length by one.
-/
@[pattern] def cons (a : α) (s : sym α n) : sym α n.succ :=
⟨a ::ₘ s.1, by rw [multiset.card_cons, s.2]⟩
infixr ` ::ₛ `:67 := cons
@[simp]
lemma cons_inj_right (a : α) (s s' : sym α n) : a ::ₛ s = a ::ₛ s' ↔ s = s' :=
subtype.ext_iff.trans $ (multiset.cons_inj_right _).trans subtype.ext_iff.symm
@[simp]
lemma cons_inj_left (a a' : α) (s : sym α n) : a ::ₛ s = a' ::ₛ s ↔ a = a' :=
subtype.ext_iff.trans $ multiset.cons_inj_left _
lemma cons_swap (a b : α) (s : sym α n) : a ::ₛ b ::ₛ s = b ::ₛ a ::ₛ s :=
subtype.ext $ multiset.cons_swap a b s.1
lemma coe_cons (s : sym α n) (a : α) : (a ::ₛ s : multiset α) = a ::ₘ s := rfl
/--
This is the quotient map that takes a list of n elements as an n-tuple and produces an nth
symmetric power.
-/
instance : has_lift (vector α n) (sym α n) :=
{ lift := λ x, ⟨↑x.val, (multiset.coe_card _).trans x.2⟩ }
@[simp] lemma of_vector_nil : ↑(vector.nil : vector α 0) = (sym.nil : sym α 0) := rfl
@[simp] lemma of_vector_cons (a : α) (v : vector α n) :
↑(vector.cons a v) = a ::ₛ (↑v : sym α n) := by { cases v, refl }
/--
`α ∈ s` means that `a` appears as one of the factors in `s`.
-/
instance : has_mem α (sym α n) := ⟨λ a s, a ∈ s.1⟩
instance decidable_mem [decidable_eq α] (a : α) (s : sym α n) : decidable (a ∈ s) :=
s.1.decidable_mem _
@[simp]
lemma mem_mk (a : α) (s : multiset α) (h : s.card = n) : a ∈ mk s h ↔ a ∈ s := iff.rfl
@[simp] lemma mem_cons {a b : α} {s : sym α n} : a ∈ b ::ₛ s ↔ a = b ∨ a ∈ s :=
multiset.mem_cons
lemma mem_cons_of_mem {a b : α} {s : sym α n} (h : a ∈ s) : a ∈ b ::ₛ s :=
multiset.mem_cons_of_mem h
@[simp] lemma mem_cons_self (a : α) (s : sym α n) : a ∈ a ::ₛ s :=
multiset.mem_cons_self a s.1
lemma cons_of_coe_eq (a : α) (v : vector α n) : a ::ₛ (↑v : sym α n) = ↑(a ::ᵥ v) :=
subtype.ext $ by { cases v, refl }
lemma sound {a b : vector α n} (h : a.val ~ b.val) : (↑a : sym α n) = ↑b :=
subtype.ext $ quotient.sound h
/-- `erase s a h` is the sym that subtracts 1 from the
multiplicity of `a` if a is present in the sym. -/
def erase [decidable_eq α] (s : sym α (n + 1)) (a : α) (h : a ∈ s) : sym α n :=
⟨s.val.erase a, (multiset.card_erase_of_mem h).trans $ s.property.symm ▸ n.pred_succ⟩
@[simp] lemma erase_mk [decidable_eq α] (m : multiset α) (hc : m.card = n + 1) (a : α) (h : a ∈ m) :
(mk m hc).erase a h = mk (m.erase a) (by { rw [multiset.card_erase_of_mem h, hc], refl }) := rfl
@[simp] lemma coe_erase [decidable_eq α] {s : sym α n.succ} {a : α} (h : a ∈ s) :
(s.erase a h : multiset α) = multiset.erase s a := rfl
@[simp] lemma cons_erase [decidable_eq α] {s : sym α n.succ} {a : α} (h : a ∈ s) :
a ::ₛ s.erase a h = s :=
coe_injective $ multiset.cons_erase h
@[simp] lemma erase_cons_head [decidable_eq α] (s : sym α n) (a : α)
(h : a ∈ a ::ₛ s := mem_cons_self a s) : (a ::ₛ s).erase a h = s :=
coe_injective $ multiset.erase_cons_head a s.1
/--
Another definition of the nth symmetric power, using vectors modulo permutations. (See `sym`.)
-/
def sym' (α : Type*) (n : ℕ) := quotient (vector.perm.is_setoid α n)
/--
This is `cons` but for the alternative `sym'` definition.
-/
def cons' {α : Type*} {n : ℕ} : α → sym' α n → sym' α (nat.succ n) :=
λ a, quotient.map (vector.cons a) (λ ⟨l₁, h₁⟩ ⟨l₂, h₂⟩ h, list.perm.cons _ h)
notation a :: b := cons' a b
/--
Multisets of cardinality n are equivalent to length-n vectors up to permutations.
-/
def sym_equiv_sym' {α : Type*} {n : ℕ} : sym α n ≃ sym' α n :=
equiv.subtype_quotient_equiv_quotient_subtype _ _ (λ _, by refl) (λ _ _, by refl)
lemma cons_equiv_eq_equiv_cons (α : Type*) (n : ℕ) (a : α) (s : sym α n) :
a :: sym_equiv_sym' s = sym_equiv_sym' (a ::ₛ s) :=
by { rcases s with ⟨⟨l⟩, _⟩, refl, }
instance : has_zero (sym α 0) := ⟨⟨0, rfl⟩⟩
instance : has_emptyc (sym α 0) := ⟨0⟩
lemma eq_nil_of_card_zero (s : sym α 0) : s = nil :=
subtype.ext $ multiset.card_eq_zero.1 s.2
instance unique_zero : unique (sym α 0) :=
⟨⟨nil⟩, eq_nil_of_card_zero⟩
/-- `repeat a n` is the sym containing only `a` with multiplicity `n`. -/
def repeat (a : α) (n : ℕ) : sym α n := ⟨multiset.repeat a n, multiset.card_repeat _ _⟩
lemma repeat_succ {a : α} {n : ℕ} : repeat a n.succ = a ::ₛ repeat a n := rfl
lemma coe_repeat : (repeat a n : multiset α) = multiset.repeat a n := rfl
@[simp] lemma mem_repeat : b ∈ repeat a n ↔ n ≠ 0 ∧ b = a := multiset.mem_repeat
lemma eq_repeat_iff : s = repeat a n ↔ ∀ b ∈ s, b = a :=
begin
rw [subtype.ext_iff, coe_repeat],
convert multiset.eq_repeat',
exact s.2.symm,
end
lemma exists_mem (s : sym α n.succ) : ∃ a, a ∈ s :=
multiset.card_pos_iff_exists_mem.1 $ s.2.symm ▸ n.succ_pos
lemma exists_eq_cons_of_succ (s : sym α n.succ) : ∃ (a : α) (s' : sym α n), s = a ::ₛ s' :=
begin
obtain ⟨a, ha⟩ := exists_mem s,
classical,
exact ⟨a, s.erase a ha, (cons_erase ha).symm⟩,
end
lemma eq_repeat {a : α} {n : ℕ} {s : sym α n} : s = repeat a n ↔ ∀ b ∈ s, b = a :=
subtype.ext_iff.trans $ multiset.eq_repeat.trans $ and_iff_right s.prop
lemma eq_repeat_of_subsingleton [subsingleton α] (a : α) {n : ℕ} (s : sym α n) : s = repeat a n :=
eq_repeat.2 $ λ b hb, subsingleton.elim _ _
instance [subsingleton α] (n : ℕ) : subsingleton (sym α n) :=
⟨begin
cases n,
{ simp, },
{ intros s s',
obtain ⟨b, -⟩ := exists_mem s,
rw [eq_repeat_of_subsingleton b s', eq_repeat_of_subsingleton b s], },
end⟩
instance inhabited_sym [inhabited α] (n : ℕ) : inhabited (sym α n) :=
⟨repeat default n⟩
instance inhabited_sym' [inhabited α] (n : ℕ) : inhabited (sym' α n) :=
⟨quotient.mk' (vector.repeat default n)⟩
instance (n : ℕ) [is_empty α] : is_empty (sym α n.succ) :=
⟨λ s, by { obtain ⟨a, -⟩ := exists_mem s, exact is_empty_elim a }⟩
instance (n : ℕ) [unique α] : unique (sym α n) := unique.mk' _
lemma repeat_left_inj {a b : α} {n : ℕ} (h : n ≠ 0) : repeat a n = repeat b n ↔ a = b :=
subtype.ext_iff.trans (multiset.repeat_left_inj h)
lemma repeat_left_injective {n : ℕ} (h : n ≠ 0) : function.injective (λ x : α, repeat x n) :=
λ a b, (repeat_left_inj h).1
instance (n : ℕ) [nontrivial α] : nontrivial (sym α (n + 1)) :=
(repeat_left_injective n.succ_ne_zero).nontrivial
/-- A function `α → β` induces a function `sym α n → sym β n` by applying it to every element of
the underlying `n`-tuple. -/
def map {n : ℕ} (f : α → β) (x : sym α n) : sym β n :=
⟨x.val.map f, by simpa [multiset.card_map] using x.property⟩
@[simp] lemma mem_map {n : ℕ} {f : α → β} {b : β} {l : sym α n} :
b ∈ sym.map f l ↔ ∃ a, a ∈ l ∧ f a = b := multiset.mem_map
/-- Note: `sym.map_id` is not simp-normal, as simp ends up unfolding `id` with `sym.map_congr` -/
@[simp] lemma map_id' {α : Type*} {n : ℕ} (s : sym α n) : sym.map (λ (x : α), x) s = s :=
by simp [sym.map]
lemma map_id {α : Type*} {n : ℕ} (s : sym α n) : sym.map id s = s :=
by simp [sym.map]
@[simp] lemma map_map {α β γ : Type*} {n : ℕ} (g : β → γ) (f : α → β) (s : sym α n) :
sym.map g (sym.map f s) = sym.map (g ∘ f) s :=
by simp [sym.map]
@[simp] lemma map_zero (f : α → β) :
sym.map f (0 : sym α 0) = (0 : sym β 0) := rfl
@[simp] lemma map_cons {n : ℕ} (f : α → β) (a : α) (s : sym α n) :
(a ::ₛ s).map f = (f a) ::ₛ s.map f :=
by simp [map, cons]
@[congr] lemma map_congr {f g : α → β} {s : sym α n} (h : ∀ x ∈ s, f x = g x) :
map f s = map g s := subtype.ext $ multiset.map_congr rfl h
@[simp] lemma map_mk {f : α → β} {m : multiset α} {hc : m.card = n} :
map f (mk m hc) = mk (m.map f) (by simp [hc]) := rfl
@[simp] lemma coe_map (s : sym α n) (f : α → β) : ↑(s.map f) = multiset.map f s := rfl
lemma map_injective {f : α → β} (hf : injective f) (n : ℕ) :
injective (map f : sym α n → sym β n) :=
λ s t h, coe_injective $ multiset.map_injective hf $ coe_inj.2 h
/-- Mapping an equivalence `α ≃ β` using `sym.map` gives an equivalence between `sym α n` and
`sym β n`. -/
@[simps]
def equiv_congr (e : α ≃ β) : sym α n ≃ sym β n :=
{ to_fun := map e,
inv_fun := map e.symm,
left_inv := λ x, by rw [map_map, equiv.symm_comp_self, map_id],
right_inv := λ x, by rw [map_map, equiv.self_comp_symm, map_id] }
/-- "Attach" a proof that `a ∈ s` to each element `a` in `s` to produce
an element of the symmetric power on `{x // x ∈ s}`. -/
def attach (s : sym α n) : sym {x // x ∈ s} n := ⟨s.val.attach, by rw [multiset.card_attach, s.2]⟩
@[simp] lemma attach_mk {m : multiset α} {hc : m.card = n} :
attach (mk m hc) = mk m.attach (multiset.card_attach.trans hc) := rfl
@[simp] lemma coe_attach (s : sym α n) : (s.attach : multiset {a // a ∈ s}) = multiset.attach s :=
rfl
lemma attach_map_coe (s : sym α n) : s.attach.map coe = s :=
coe_injective $ multiset.attach_map_val _
@[simp] lemma mem_attach (s : sym α n) (x : {x // x ∈ s}) : x ∈ s.attach :=
multiset.mem_attach _ _
@[simp] lemma attach_nil : (nil : sym α 0).attach = nil := rfl
@[simp] lemma attach_cons (x : α) (s : sym α n) :
(cons x s).attach = cons ⟨x, mem_cons_self _ _⟩ (s.attach.map (λ x, ⟨x, mem_cons_of_mem x.prop⟩))
:=
coe_injective $ multiset.attach_cons _ _
end sym
|
ec8df747d86d4ba59bf5e7bf99d6b827afad7f0a | 3dc4623269159d02a444fe898d33e8c7e7e9461b | /.github/workflows/geo/D.lean | f01bf794ea8c46abd639a106dd333abbb692cbe5 | [] | 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 | 1,461 | lean | import .global
import .ideals
universe u
local notation `Ring` := CommRing.{u}
local notation `Set` := Type u
open CommRing
namespace D
variables (R :Ring)
variables (S : set R)
def D_obj (A : Ring) := {ζ : R ⟶ A | (1 : A) ∈ ideal.map (ζ) (ideal.span S)}
@[ext]lemma ext (A : Ring)(ζ1 : D_obj R S A)(ζ2 : D_obj R S A) : ζ1.val =ζ2.val → ζ1 = ζ2 :=
begin
intro h,
cases ζ1,
cases ζ2,
congr ; try { assumption },
end
def D_map (A B : Ring) (ψ : A ⟶ B) : D_obj R S A → D_obj R S B := λ ζ,
begin
exact {val := ζ.val ≫ ψ , property := begin
unfold D_obj,
have t : ideal.map (ζ.val ≫ ψ) (ideal.span S) = ideal.map ψ (ideal.map (ζ.val) (ideal.span S)),
rw ideal.ideal_comp,
have T2 : ψ (1 : A) ∈ ideal.map ψ (ideal.map (ζ.val) (ideal.span S)),
exact ideal.mem_map_of_mem ζ.property ,
rw ← t at T2,
erw ψ.map_one at T2,
use T2,
end }
end
lemma D_map_comp (A B : Ring) (ψ : A ⟶ B) (ζ : D_obj R S A) : (D_map R S A B ψ ζ).val = ζ.val ≫ ψ := rfl
lemma D_map_one (A : Ring) (ζ : D_obj R S A) : D_map R S A A (𝟙 A) ζ = ζ := begin
ext,
rw D_map,
exact rfl,
end
def D (S : set R) : Ring ⥤ Set :=
{
obj := λ A, D_obj R S A,
map := D_map R S,
map_id' := λ A, begin
funext, rw D_map_one, exact rfl,
end
}
end D |
f0354ab459d15b05c0dd42d651b100b6ab1dcf2b | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /counterexamples/pseudoelement.lean | a05f885f332ae895bb3bcf6b2cafea7f4efb1179 | [
"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 | 5,475 | lean | /-
Copyright (c) 2022 Riccardo Brasca. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Riccardo Brasca
-/
import category_theory.abelian.pseudoelements
import algebra.category.Module.biproducts
/-!
# Pseudoelements and pullbacks
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
Borceux claims in Proposition 1.9.5 that the pseudoelement constructed in
`category_theory.abelian.pseudoelement.pseudo_pullback` is unique. We show here that this claim is
false. This means in particular that we cannot have an extensionality principle for pullbacks in
terms of pseudoelements.
## Implementation details
The construction, suggested in https://mathoverflow.net/a/419951/7845, is the following.
We work in the category of `Module ℤ` and we consider the special case of `ℚ ⊞ ℚ` (that is the
pullback over the terminal object). We consider the pseudoelements associated to `x : ℚ ⟶ ℚ ⊞ ℚ`
given by `t ↦ (t, 2 * t)` and `y : ℚ ⟶ ℚ ⊞ ℚ` given by `t ↦ (t, t)`.
## Main results
* `category_theory.abelian.pseudoelement.exist_ne_and_fst_eq_fst_and_snd_eq_snd` : there are two
pseudoelements `x y : ℚ ⊞ ℚ` such that `x ≠ y`, `biprod.fst x = biprod.fst y` and
`biprod.snd x = biprod.snd y`.
## References
* [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2]
-/
open category_theory.abelian category_theory category_theory.limits Module linear_map
namespace counterexample
noncomputable theory
open category_theory.abelian.pseudoelement
/-- `x` is given by `t ↦ (t, 2 * t)`. -/
def x : over ((of ℤ ℚ) ⊞ (of ℤ ℚ)) :=
over.mk (biprod.lift (of_hom id) (of_hom (2 * id)))
/-- `y` is given by `t ↦ (t, t)`. -/
def y : over ((of ℤ ℚ) ⊞ (of ℤ ℚ)) :=
over.mk (biprod.lift (of_hom id) (of_hom id))
/-- `biprod.fst ≫ x` is pseudoequal to `biprod.fst y`. -/
lemma fst_x_pseudo_eq_fst_y : pseudo_equal _ (app biprod.fst x) (app biprod.fst y) :=
begin
refine ⟨of ℤ ℚ, (of_hom id), (of_hom id),
category_struct.id.epi (of ℤ ℚ), _, _⟩,
{ exact (Module.epi_iff_surjective _).2 (λ a, ⟨(a : ℚ), by simp⟩) },
{ dsimp [x, y],
simp }
end
/-- `biprod.snd ≫ x` is pseudoequal to `biprod.snd y`. -/
lemma snd_x_pseudo_eq_snd_y : pseudo_equal _
(app biprod.snd x) (app biprod.snd y) :=
begin
refine ⟨of ℤ ℚ, (of_hom id), 2 • (of_hom id),
category_struct.id.epi (of ℤ ℚ), _, _⟩,
{ refine (Module.epi_iff_surjective _).2 (λ a, ⟨(a/2 : ℚ), _⟩),
simp only [two_smul, add_apply, of_hom_apply, id_coe, id.def],
exact add_halves' (show ℚ, from a) },
{ dsimp [x, y],
exact concrete_category.hom_ext _ _ (λ a, by simpa) }
end
/-- `x` is not pseudoequal to `y`. -/
lemma x_not_pseudo_eq : ¬(pseudo_equal _ x y) :=
begin
intro h,
replace h := Module.eq_range_of_pseudoequal h,
dsimp [x, y] at h,
let φ := (biprod.lift (of_hom (id : ℚ →ₗ[ℤ] ℚ)) (of_hom (2 * id))),
have mem_range := mem_range_self φ (1 : ℚ),
rw h at mem_range,
obtain ⟨a, ha⟩ := mem_range,
rw [← Module.id_apply (φ (1 : ℚ)), ← biprod.total, ← linear_map.comp_apply, ← comp_def,
preadditive.comp_add] at ha,
let π₁ := (biprod.fst : (of ℤ ℚ) ⊞ (of ℤ ℚ) ⟶ _),
have ha₁ := congr_arg π₁ ha,
simp only [← linear_map.comp_apply, ← comp_def] at ha₁,
simp only [biprod.lift_fst, of_hom_apply, id_coe, id.def, preadditive.add_comp, category.assoc,
biprod.inl_fst, category.comp_id, biprod.inr_fst, limits.comp_zero, add_zero] at ha₁,
let π₂ := (biprod.snd : (of ℤ ℚ) ⊞ (of ℤ ℚ) ⟶ _),
have ha₂ := congr_arg π₂ ha,
simp only [← linear_map.comp_apply, ← comp_def] at ha₂,
have : (2 : ℚ →ₗ[ℤ] ℚ) 1 = 1 + 1 := rfl,
simp only [ha₁, this, biprod.lift_snd, of_hom_apply, id_coe, id.def, preadditive.add_comp,
category.assoc, biprod.inl_snd, limits.comp_zero, biprod.inr_snd, category.comp_id, zero_add,
mul_apply, self_eq_add_left] at ha₂,
exact one_ne_zero' ℚ ha₂,
end
local attribute [instance] pseudoelement.setoid
open_locale pseudoelement
/-- `biprod.fst ⟦x⟧ = biprod.fst ⟦y⟧`. -/
lemma fst_mk_x_eq_fst_mk_y : (biprod.fst : (of ℤ ℚ) ⊞ (of ℤ ℚ) ⟶ _) ⟦x⟧ =
(biprod.fst : (of ℤ ℚ) ⊞ (of ℤ ℚ) ⟶ _) ⟦y⟧ :=
by simpa only [abelian.pseudoelement.pseudo_apply_mk, quotient.eq] using fst_x_pseudo_eq_fst_y
/-- `biprod.snd ⟦x⟧ = biprod.snd ⟦y⟧`. -/
lemma snd_mk_x_eq_snd_mk_y : (biprod.snd : (of ℤ ℚ) ⊞ (of ℤ ℚ) ⟶ _) ⟦x⟧ =
(biprod.snd : (of ℤ ℚ) ⊞ (of ℤ ℚ) ⟶ _) ⟦y⟧ :=
by simpa only [abelian.pseudoelement.pseudo_apply_mk, quotient.eq] using snd_x_pseudo_eq_snd_y
/-- `⟦x⟧ ≠ ⟦y⟧`. -/
lemma mk_x_ne_mk_y : ⟦x⟧ ≠ ⟦y⟧ :=
λ h, x_not_pseudo_eq $ quotient.eq.1 h
/-- There are two pseudoelements `x y : ℚ ⊞ ℚ` such that `x ≠ y`, `biprod.fst x = biprod.fst y` and
`biprod.snd x = biprod.snd y`. -/
lemma exist_ne_and_fst_eq_fst_and_snd_eq_snd : ∃ x y : (of ℤ ℚ) ⊞ (of ℤ ℚ),
x ≠ y ∧
(biprod.fst : (of ℤ ℚ) ⊞ (of ℤ ℚ) ⟶ _) x = (biprod.fst : (of ℤ ℚ) ⊞ (of ℤ ℚ) ⟶ _) y ∧
(biprod.snd : (of ℤ ℚ) ⊞ (of ℤ ℚ) ⟶ _) x = (biprod.snd : (of ℤ ℚ) ⊞ (of ℤ ℚ) ⟶ _) y:=
⟨⟦x⟧, ⟦y⟧, mk_x_ne_mk_y, fst_mk_x_eq_fst_mk_y, snd_mk_x_eq_snd_mk_y⟩
end counterexample
|
b250d4c79763504915b46e501327cc0e13c115e0 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /12_Axioms.org.28.lean | 4c72ce601f56a610f7d2ced2da3d72ff33306e09 | [] | 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 | 418 | lean | import standard
open classical decidable inhabited nonempty
namespace hide
-- BEGIN
noncomputable definition decidable_inhabited [instance] (a : Prop) : inhabited (decidable a) :=
inhabited_of_nonempty
(or.elim (em a)
(assume Ha, nonempty.intro (inl Ha))
(assume Hna, nonempty.intro (inr Hna)))
noncomputable definition prop_decidable [instance] (a : Prop) : decidable a :=
arbitrary (decidable a)
-- END
end hide
|
7a50cd42567e61a30a81206163881b5e555f48fb | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /library/hott/types/sigma.lean | 42bd425c87887e0f92d57dbd365d0861716d509e | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 14,502 | lean | /-
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
Ported from Coq HoTT
Theorems about sigma-types (dependent sums)
-/
import ..trunc .prod
open path sigma sigma.ops Equiv IsEquiv
namespace sigma
-- remove the ₁'s (globally)
variables {A A' : Type} {B : A → Type} {B' : A' → Type} {C : Πa, B a → Type}
{D : Πa b, C a b → Type}
{a a' a'' : A} {b b₁ b₂ : B a} {b' : B a'} {b'' : B a''} {u v w : Σa, B a}
definition eta_sigma (u : Σa, B a) : ⟨u.1 , u.2⟩ ≈ u :=
destruct u (λu1 u2, idp)
definition eta2_sigma (u : Σa b, C a b) : ⟨u.1, u.2.1, u.2.2⟩ ≈ u :=
destruct u (λu1 u2, destruct u2 (λu21 u22, idp))
definition eta3_sigma (u : Σa b c, D a b c) : ⟨u.1, u.2.1, u.2.2.1, u.2.2.2⟩ ≈ u :=
destruct u (λu1 u2, destruct u2 (λu21 u22, destruct u22 (λu221 u222, idp)))
definition path_sigma_dpair (p : a ≈ a') (q : p ▹ b ≈ b') : dpair a b ≈ dpair a' b' :=
path.rec_on p (λb b' q, path.rec_on q idp) b b' q
/- In Coq they often have to give u and v explicitly -/
definition path_sigma (p : u.1 ≈ v.1) (q : p ▹ u.2 ≈ v.2) : u ≈ v :=
destruct u
(λu1 u2, destruct v (λ v1 v2, path_sigma_dpair))
p q
/- Projections of paths from a total space -/
definition pr1_path (p : u ≈ v) : u.1 ≈ v.1 :=
ap dpr1 p
postfix `..1`:10000 := pr1_path
definition pr2_path (p : u ≈ v) : p..1 ▹ u.2 ≈ v.2 :=
path.rec_on p idp
--Coq uses the following proof, which only computes if u,v are dpairs AND p is idp
--(transport_compose B dpr1 p u.2)⁻¹ ⬝ apD dpr2 p
postfix `..2`:10000 := pr2_path
definition dpair_path_sigma (p : u.1 ≈ v.1) (q : p ▹ u.2 ≈ v.2)
: dpair (path_sigma p q)..1 (path_sigma p q)..2 ≈ ⟨p, q⟩ :=
begin
generalize q, generalize p,
apply (destruct u), intros (u1, u2),
apply (destruct v), intros (v1, v2, p), generalize v2,
apply (path.rec_on p), intros (v2, q),
apply (path.rec_on q), apply idp
end
definition pr1_path_sigma (p : u.1 ≈ v.1) (q : p ▹ u.2 ≈ v.2) : (path_sigma p q)..1 ≈ p :=
(!dpair_path_sigma)..1
definition pr2_path_sigma (p : u.1 ≈ v.1) (q : p ▹ u.2 ≈ v.2)
: pr1_path_sigma p q ▹ (path_sigma p q)..2 ≈ q :=
(!dpair_path_sigma)..2
definition eta_path_sigma (p : u ≈ v) : path_sigma (p..1) (p..2) ≈ p :=
begin
apply (path.rec_on p),
apply (destruct u), intros (u1, u2),
apply idp
end
definition transport_pr1_path_sigma {B' : A → Type} (p : u.1 ≈ v.1) (q : p ▹ u.2 ≈ v.2)
: transport (λx, B' x.1) (path_sigma p q) ≈ transport B' p :=
begin
generalize q, generalize p,
apply (destruct u), intros (u1, u2),
apply (destruct v), intros (v1, v2, p), generalize v2,
apply (path.rec_on p), intros (v2, q),
apply (path.rec_on q), apply idp
end
/- the uncurried version of path_sigma. We will prove that this is an equivalence -/
definition path_sigma_uncurried (pq : Σ(p : dpr1 u ≈ dpr1 v), p ▹ (dpr2 u) ≈ dpr2 v) : u ≈ v :=
destruct pq path_sigma
definition dpair_path_sigma_uncurried (pq : Σ(p : u.1 ≈ v.1), p ▹ u.2 ≈ v.2)
: dpair (path_sigma_uncurried pq)..1 (path_sigma_uncurried pq)..2 ≈ pq :=
destruct pq dpair_path_sigma
definition pr1_path_sigma_uncurried (pq : Σ(p : u.1 ≈ v.1), p ▹ u.2 ≈ v.2)
: (path_sigma_uncurried pq)..1 ≈ pq.1 :=
(!dpair_path_sigma_uncurried)..1
definition pr2_path_sigma_uncurried (pq : Σ(p : u.1 ≈ v.1), p ▹ u.2 ≈ v.2)
: (pr1_path_sigma_uncurried pq) ▹ (path_sigma_uncurried pq)..2 ≈ pq.2 :=
(!dpair_path_sigma_uncurried)..2
definition eta_path_sigma_uncurried (p : u ≈ v) : path_sigma_uncurried (dpair p..1 p..2) ≈ p :=
!eta_path_sigma
definition transport_pr1_path_sigma_uncurried {B' : A → Type} (pq : Σ(p : u.1 ≈ v.1), p ▹ u.2 ≈ v.2)
: transport (λx, B' x.1) (@path_sigma_uncurried A B u v pq) ≈ transport B' pq.1 :=
destruct pq transport_pr1_path_sigma
definition isequiv_path_sigma /-[instance]-/ (u v : Σa, B a)
: IsEquiv (@path_sigma_uncurried A B u v) :=
adjointify path_sigma_uncurried
(λp, ⟨p..1, p..2⟩)
eta_path_sigma_uncurried
dpair_path_sigma_uncurried
definition equiv_path_sigma (u v : Σa, B a) : (Σ(p : u.1 ≈ v.1), p ▹ u.2 ≈ v.2) ≃ (u ≈ v) :=
Equiv.mk path_sigma_uncurried !isequiv_path_sigma
definition path_sigma_dpair_pp_pp (p1 : a ≈ a' ) (q1 : p1 ▹ b ≈ b' )
(p2 : a' ≈ a'') (q2 : p2 ▹ b' ≈ b'') :
path_sigma_dpair (p1 ⬝ p2) (transport_pp B p1 p2 b ⬝ ap (transport B p2) q1 ⬝ q2)
≈ path_sigma_dpair p1 q1 ⬝ path_sigma_dpair p2 q2 :=
begin
generalize q2, generalize q1, generalize b'', generalize p2, generalize b',
apply (path.rec_on p1), intros (b', p2),
apply (path.rec_on p2), intros (b'', q1),
apply (path.rec_on q1), intro q2,
apply (path.rec_on q2), apply idp
end
definition path_sigma_pp_pp (p1 : u.1 ≈ v.1) (q1 : p1 ▹ u.2 ≈ v.2)
(p2 : v.1 ≈ w.1) (q2 : p2 ▹ v.2 ≈ w.2) :
path_sigma (p1 ⬝ p2) (transport_pp B p1 p2 u.2 ⬝ ap (transport B p2) q1 ⬝ q2)
≈ path_sigma p1 q1 ⬝ path_sigma p2 q2 :=
begin
generalize q2, generalize p2, generalize q1, generalize p1,
apply (destruct u), intros (u1, u2),
apply (destruct v), intros (v1, v2),
apply (destruct w), intros,
apply path_sigma_dpair_pp_pp
end
definition path_sigma_dpair_p1_1p (p : a ≈ a') (q : p ▹ b ≈ b') :
path_sigma_dpair p q ≈ path_sigma_dpair p idp ⬝ path_sigma_dpair idp q :=
begin
generalize q, generalize b',
apply (path.rec_on p), intros (b', q),
apply (path.rec_on q), apply idp
end
/- pr1_path commutes with the groupoid structure. -/
definition pr1_path_1 (u : Σa, B a) : (idpath u)..1 ≈ idpath (u.1) := idp
definition pr1_path_pp (p : u ≈ v) (q : v ≈ w) : (p ⬝ q) ..1 ≈ (p..1) ⬝ (q..1) := !ap_pp
definition pr1_path_V (p : u ≈ v) : p⁻¹ ..1 ≈ (p..1)⁻¹ := !ap_V
/- Applying dpair to one argument is the same as path_sigma_dpair with reflexivity in the first place. -/
definition ap_dpair (q : b₁ ≈ b₂) : ap (dpair a) q ≈ path_sigma_dpair idp q :=
path.rec_on q idp
/- Dependent transport is the same as transport along a path_sigma. -/
definition transportD_is_transport (p : a ≈ a') (c : C a b) :
p ▹D c ≈ transport (λu, C (u.1) (u.2)) (path_sigma_dpair p idp) c :=
path.rec_on p idp
definition path_path_sigma_path_sigma {p1 q1 : a ≈ a'} {p2 : p1 ▹ b ≈ b'} {q2 : q1 ▹ b ≈ b'}
(r : p1 ≈ q1) (s : r ▹ p2 ≈ q2) : path_sigma p1 p2 ≈ path_sigma q1 q2 :=
path.rec_on r
proof (λq2 s, path.rec_on s idp) qed
q2
s
-- begin
-- generalize s, generalize q2,
-- apply (path.rec_on r), intros (q2, s),
-- apply (path.rec_on s), apply idp
-- end
/- A path between paths in a total space is commonly shown component wise. -/
definition path_path_sigma {p q : u ≈ v} (r : p..1 ≈ q..1) (s : r ▹ p..2 ≈ q..2) : p ≈ q :=
begin
generalize s, generalize r, generalize q,
apply (path.rec_on p),
apply (destruct u), intros (u1, u2, q, r, s),
apply concat, rotate 1,
apply eta_path_sigma,
apply (path_path_sigma_path_sigma r s)
end
/- In Coq they often have to give u and v explicitly when using the following definition -/
definition path_path_sigma_uncurried {p q : u ≈ v}
(rs : Σ(r : p..1 ≈ q..1), transport (λx, transport B x u.2 ≈ v.2) r p..2 ≈ q..2) : p ≈ q :=
destruct rs path_path_sigma
/- Transport -/
/- The concrete description of transport in sigmas (and also pis) is rather trickier than in the other types. In particular, these cannot be described just in terms of transport in simpler types; they require also the dependent transport [transportD].
In particular, this indicates why `transport` alone cannot be fully defined by induction on the structure of types, although Id-elim/transportD can be (cf. Observational Type Theory). A more thorough set of lemmas, along the lines of the present ones but dealing with Id-elim rather than just transport, might be nice to have eventually? -/
definition transport_sigma (p : a ≈ a') (bc : Σ(b : B a), C a b) : p ▹ bc ≈ ⟨p ▹ bc.1, p ▹D bc.2⟩
:=
begin
apply (path.rec_on p),
apply (destruct bc), intros (b, c),
apply idp
end
/- The special case when the second variable doesn't depend on the first is simpler. -/
definition transport_sigma' {B : Type} {C : A → B → Type} (p : a ≈ a') (bc : Σ(b : B), C a b)
: p ▹ bc ≈ ⟨bc.1, p ▹ bc.2⟩ :=
begin
apply (path.rec_on p),
apply (destruct bc), intros (b, c),
apply idp
end
/- Or if the second variable contains a first component that doesn't depend on the first. Need to think about the naming of these. -/
definition transport_sigma_' {C : A → Type} {D : Π a:A, B a → C a → Type} (p : a ≈ a')
(bcd : Σ(b : B a) (c : C a), D a b c) : p ▹ bcd ≈ ⟨p ▹ bcd.1, p ▹ bcd.2.1, p ▹D2 bcd.2.2⟩ :=
begin
generalize bcd,
apply (path.rec_on p), intro bcd,
apply (destruct bcd), intros (b, cd),
apply (destruct cd), intros (c, d),
apply idp
end
/- Functorial action -/
variables (f : A → A') (g : Πa, B a → B' (f a))
definition functor_sigma (u : Σa, B a) : Σa', B' a' :=
⟨f u.1, g u.1 u.2⟩
/- Equivalences -/
--remove explicit arguments of IsEquiv
definition isequiv_functor_sigma [H1 : IsEquiv f] [H2 : Π a, @IsEquiv (B a) (B' (f a)) (g a)]
: IsEquiv (functor_sigma f g) :=
adjointify (functor_sigma f g)
(functor_sigma (f⁻¹) (λ(x : A') (y : B' x), ((g (f⁻¹ x))⁻¹ ((retr f x)⁻¹ ▹ y))))
sorry
sorry
definition equiv_functor_sigma [H1 : IsEquiv f] [H2 : Π a, IsEquiv (g a)] : (Σa, B a) ≃ (Σa', B' a') :=
Equiv.mk (functor_sigma f g) !isequiv_functor_sigma
context --remove
irreducible inv function.compose --remove
definition equiv_functor_sigma' (Hf : A ≃ A') (Hg : Π a, B a ≃ B' (equiv_fun Hf a)) :
(Σa, B a) ≃ (Σa', B' a') :=
equiv_functor_sigma (equiv_fun Hf) (λ a, equiv_fun (Hg a))
end --remove
/- definition 3.11.9(i): Summing up a contractible family of types does nothing. -/
open truncation
definition isequiv_pr1_contr [instance] (B : A → Type) [H : Π a, is_contr (B a)]
: IsEquiv (@dpr1 A B) :=
adjointify dpr1
(λa, ⟨a, !center⟩)
(λa, idp)
(λu, path_sigma idp !contr)
definition equiv_sigma_contr [H : Π a, is_contr (B a)] : (Σa, B a) ≃ A :=
Equiv.mk dpr1 _
/- definition 3.11.9(ii): Dually, summing up over a contractible type does nothing. -/
definition equiv_contr_sigma (B : A → Type) [H : is_contr A] : (Σa, B a) ≃ B (center A) :=
Equiv.mk _ (adjointify
(λu, contr u.1⁻¹ ▹ u.2)
(λb, ⟨!center, b⟩)
(λb, ap (λx, x ▹ b) !path2_contr)
(λu, path_sigma !contr !transport_pV))
/- Associativity -/
--this proof is harder here than in Coq because we don't have eta definitionally for sigma
definition equiv_sigma_assoc (C : (Σa, B a) → Type) : (Σa b, C ⟨a,b⟩) ≃ (Σu, C u) :=
-- begin
-- apply Equiv.mk,
-- apply (adjointify (λav, ⟨⟨av.1, av.2.1⟩, av.2.2⟩)
-- (λuc, ⟨uc.1.1, uc.1.2, !eta_sigma⁻¹ ▹ uc.2⟩)),
-- intro uc, apply (destruct uc), intro u,
-- apply (destruct u), intros (a, b, c),
-- apply idp,
-- intro av, apply (destruct av), intros (a, v),
-- apply (destruct v), intros (b, c),
-- apply idp,
-- end
Equiv.mk _ (adjointify
(λav, ⟨⟨av.1, av.2.1⟩, av.2.2⟩)
(λuc, ⟨uc.1.1, uc.1.2, !eta_sigma⁻¹ ▹ uc.2⟩)
proof (λuc, destruct uc (λu, destruct u (λa b c, idp))) qed
proof (λav, destruct av (λa v, destruct v (λb c, idp))) qed)
open prod
definition equiv_sigma_prod (C : (A × A') → Type) : (Σa a', C (a,a')) ≃ (Σu, C u) :=
Equiv.mk _ (adjointify
(λav, ⟨(av.1, av.2.1), av.2.2⟩)
(λuc, ⟨pr₁ (uc.1), pr₂ (uc.1), !eta_prod⁻¹ ▹ uc.2⟩)
proof (λuc, destruct uc (λu, prod.destruct u (λa b c, idp))) qed
proof (λav, destruct av (λa v, destruct v (λb c, idp))) qed)
/- Symmetry -/
-- if this breaks, replace "Equiv.id" by "proof Equiv.id qed"
definition equiv_sigma_symm_prod (C : A × A' → Type) : (Σa a', C (a, a')) ≃ (Σa' a, C (a, a')) :=
calc
(Σa a', C (a, a')) ≃ Σu, C u : equiv_sigma_prod
... ≃ Σv, C (flip v) : equiv_functor_sigma' !equiv_prod_symm
(λu, prod.destruct u (λa a', Equiv.id))
... ≃ (Σa' a, C (a, a')) : equiv_sigma_prod
definition equiv_sigma_symm (C : A → A' → Type) : (Σa a', C a a') ≃ (Σa' a, C a a') :=
sigma.equiv_sigma_symm_prod (λu, C (pr1 u) (pr2 u))
definition equiv_sigma0_prod (A B : Type) : (Σ(a : A), B) ≃ A × B :=
Equiv.mk _ (adjointify
(λs, (s.1, s.2))
(λp, ⟨pr₁ p, pr₂ p⟩)
proof (λp, prod.destruct p (λa b, idp)) qed
proof (λs, destruct s (λa b, idp)) qed)
definition equiv_sigma_symm0 (A B : Type) : (Σ(a : A), B) ≃ Σ(b : B), A :=
calc
(Σ(a : A), B) ≃ A × B : equiv_sigma0_prod
... ≃ B × A : equiv_prod_symm
... ≃ Σ(b : B), A : equiv_sigma0_prod
/- truncatedness -/
definition sigma_trunc (n : trunc_index) [HA : is_trunc n A] [HB : Πa, is_trunc n (B a)]
: is_trunc n (Σa, B a) :=
begin
generalize HB, generalize HA, generalize B, generalize A,
apply (truncation.trunc_index.rec_on n),
intros (A, B, HA, HB),
apply trunc_equiv',
apply Equiv.inv_closed,
apply equiv_contr_sigma, apply HA,
apply HB,
intros (n, IH, A, B, HA, HB),
apply is_trunc_succ, intros (u, v),
apply trunc_equiv',
apply equiv_path_sigma,
apply IH,
apply succ_is_trunc,
intro aa, apply (succ_is_trunc (aa ▹ u.2) (v.2)),
end
end sigma
|
a9076bf1c74a3ff2f9e8c338a9581221523e7dcd | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/linear_algebra/matrix/fpow.lean | 5ecce7134a84986e8db89b8595c13513e253ec94 | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,326 | 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 linear_algebra.matrix.nonsingular_inverse
/-!
# Integer powers of square matrices
In this file, we define integer power of matrices, relying on
the nonsingular inverse definition for negative powers.
## Implementation details
The main definition is a direct recursive call on the integer inductive type,
as provided by the `div_inv_monoid.gpow` default implementation.
The lemma names are taken from `algebra.group_with_zero.power`.
## Tags
matrix inverse, matrix powers
-/
open_locale matrix
namespace matrix
variables {n' : Type*} [decidable_eq n'] [fintype n'] {R : Type*} [comm_ring R]
local notation `M` := matrix n' n' R
noncomputable instance : div_inv_monoid M :=
{ ..(show monoid M, by apply_instance),
..(show has_inv M, by apply_instance) }
section nat_pow
@[simp] theorem inv_pow' (A : M) (n : ℕ) : (A⁻¹) ^ n = (A ^ n)⁻¹ :=
begin
induction n with n ih,
{ simp },
{ rw [pow_succ A, mul_eq_mul, mul_inv_rev, ← ih, ← mul_eq_mul, ← pow_succ'] }
end
theorem pow_sub' (A : M) {m n : ℕ} (ha : is_unit A.det) (h : n ≤ m) :
A ^ (m - n) = A ^ m ⬝ (A ^ n)⁻¹ :=
begin
rw [←tsub_add_cancel_of_le h, pow_add, mul_eq_mul, matrix.mul_assoc, mul_nonsing_inv,
tsub_add_cancel_of_le h, matrix.mul_one],
simpa using ha.pow n
end
theorem pow_inv_comm' (A : M) (m n : ℕ) : (A⁻¹) ^ m ⬝ A ^ n = A ^ n ⬝ (A⁻¹) ^ m :=
begin
induction n with n IH generalizing m,
{ simp },
cases m,
{ simp },
rcases nonsing_inv_cancel_or_zero A with ⟨h, h'⟩ | h,
{ calc A⁻¹ ^ (m + 1) ⬝ A ^ (n + 1)
= A⁻¹ ^ m ⬝ (A⁻¹ ⬝ A) ⬝ A ^ n :
by simp only [pow_succ' A⁻¹, pow_succ A, mul_eq_mul, matrix.mul_assoc]
... = A ^ n ⬝ A⁻¹ ^ m :
by simp only [h, matrix.mul_one, matrix.one_mul, IH m]
... = A ^ n ⬝ (A ⬝ A⁻¹) ⬝ A⁻¹ ^ m :
by simp only [h', matrix.mul_one, matrix.one_mul]
... = A ^ (n + 1) ⬝ A⁻¹ ^ (m + 1) :
by simp only [pow_succ' A, pow_succ A⁻¹, mul_eq_mul, matrix.mul_assoc] },
{ simp [h] }
end
end nat_pow
section int_pow
open int
@[simp] theorem one_fpow : ∀ (n : ℤ), (1 : M) ^ n = 1
| (n : ℕ) := by rw [gpow_coe_nat, one_pow]
| -[1+ n] := by rw [gpow_neg_succ_of_nat, one_pow, inv_one]
lemma zero_fpow : ∀ z : ℤ, z ≠ 0 → (0 : M) ^ z = 0
| (n : ℕ) h := by { rw [gpow_coe_nat, zero_pow], refine lt_of_le_of_ne n.zero_le (ne.symm _),
simpa using h }
| -[1+n] h := by simp [zero_pow n.zero_lt_succ]
lemma zero_fpow_eq (n : ℤ) : (0 : M) ^ n = if n = 0 then 1 else 0 :=
begin
split_ifs with h,
{ rw [h, gpow_zero] },
{ rw [zero_fpow _ h] }
end
theorem inv_fpow (A : M) : ∀n:ℤ, A⁻¹ ^ n = (A ^ n)⁻¹
| (n : ℕ) := by rw [gpow_coe_nat, gpow_coe_nat, inv_pow']
| -[1+ n] := by rw [gpow_neg_succ_of_nat, gpow_neg_succ_of_nat, inv_pow']
@[simp] lemma fpow_neg_one (A : M) : A ^ (-1 : ℤ) = A⁻¹ :=
begin
convert div_inv_monoid.gpow_neg' 0 A,
simp only [gpow_one, int.coe_nat_zero, int.coe_nat_succ, gpow_eq_pow, zero_add]
end
theorem fpow_coe_nat (A : M) (n : ℕ) : A ^ (n : ℤ) = (A ^ n) :=
gpow_coe_nat _ _
@[simp] theorem fpow_neg_coe_nat (A : M) (n : ℕ) : A ^ (-n : ℤ) = (A ^ n)⁻¹ :=
begin
cases n,
{ simp },
{ exact div_inv_monoid.gpow_neg' _ _ }
end
lemma _root_.is_unit.det_fpow {A : M} (h : is_unit A.det) (n : ℤ) : is_unit (A ^ n).det :=
begin
cases n,
{ simpa using h.pow n },
{ simpa using h.pow n.succ }
end
lemma is_unit_det_fpow_iff {A : M} {z : ℤ} :
is_unit (A ^ z).det ↔ is_unit A.det ∨ z = 0 :=
begin
induction z using int.induction_on with z IH z IH,
{ simp },
{ rw [←int.coe_nat_succ, fpow_coe_nat, det_pow, is_unit_pos_pow_iff (z.zero_lt_succ),
←int.coe_nat_zero, int.coe_nat_eq_coe_nat_iff],
simp },
{ rw [←neg_add', ←int.coe_nat_succ, fpow_neg_coe_nat, is_unit_nonsing_inv_det_iff,
det_pow, is_unit_pos_pow_iff (z.zero_lt_succ), neg_eq_zero, ←int.coe_nat_zero,
int.coe_nat_eq_coe_nat_iff],
simp }
end
theorem fpow_neg {A : M} (h : is_unit A.det) : ∀ (n : ℤ), A ^ -n = (A ^ n)⁻¹
| (n : ℕ) := fpow_neg_coe_nat _ _
| -[1+ n] := by { rw [gpow_neg_succ_of_nat, neg_neg_of_nat_succ, of_nat_eq_coe, fpow_coe_nat,
nonsing_inv_nonsing_inv],
rw det_pow,
exact h.pow _ }
lemma inv_fpow' {A : M} (h : is_unit A.det) (n : ℤ) :
(A ⁻¹) ^ n = A ^ (-n) :=
by rw [fpow_neg h, inv_fpow]
lemma fpow_add_one {A : M} (h : is_unit A.det) : ∀ n : ℤ, A ^ (n + 1) = A ^ n * A
| (n : ℕ) := by simp [← int.coe_nat_succ, pow_succ']
| -((n : ℕ) + 1) :=
calc A ^ (-(n + 1) + 1 : ℤ)
= (A ^ n)⁻¹ : by rw [neg_add, neg_add_cancel_right, fpow_neg h, fpow_coe_nat]
... = (A ⬝ A ^ n)⁻¹ ⬝ A : by rw [mul_inv_rev, matrix.mul_assoc, nonsing_inv_mul _ h, matrix.mul_one]
... = A ^ -(n + 1 : ℤ) * A :
by rw [fpow_neg h, ← int.coe_nat_succ, fpow_coe_nat, pow_succ, mul_eq_mul, mul_eq_mul]
lemma fpow_sub_one {A : M} (h : is_unit A.det) (n : ℤ) : A ^ (n - 1) = A ^ n * A⁻¹ :=
calc A ^ (n - 1) = A ^ (n - 1) * A * A⁻¹ : by rw [mul_assoc, mul_eq_mul A, mul_nonsing_inv _ h,
mul_one]
... = A^n * A⁻¹ : by rw [← fpow_add_one h, sub_add_cancel]
lemma fpow_add {A : M} (ha : is_unit A.det) (m n : ℤ) : A ^ (m + n) = A ^ m * A ^ n :=
begin
induction n using int.induction_on with n ihn n ihn,
case hz : { simp },
{ simp only [← add_assoc, fpow_add_one ha, ihn, mul_assoc] },
{ rw [fpow_sub_one ha, ← mul_assoc, ← ihn, ← fpow_sub_one ha, add_sub_assoc] }
end
lemma fpow_add_of_nonpos {A : M} {m n : ℤ} (hm : m ≤ 0) (hn : n ≤ 0) :
A ^ (m + n) = A ^ m * A ^ n :=
begin
rcases nonsing_inv_cancel_or_zero A with ⟨h, h'⟩ | h,
{ exact fpow_add (is_unit_det_of_left_inverse h) m n },
{ obtain ⟨k, rfl⟩ := exists_eq_neg_of_nat hm,
obtain ⟨l, rfl⟩ := exists_eq_neg_of_nat hn,
simp_rw [←neg_add, ←int.coe_nat_add, fpow_neg_coe_nat, ←inv_pow', h, pow_add] }
end
lemma fpow_add_of_nonneg {A : M} {m n : ℤ} (hm : 0 ≤ m) (hn : 0 ≤ n) :
A ^ (m + n) = A ^ m * A ^ n :=
begin
rcases nonsing_inv_cancel_or_zero A with ⟨h, h'⟩ | h,
{ exact fpow_add (is_unit_det_of_left_inverse h) m n },
{ obtain ⟨k, rfl⟩ := eq_coe_of_zero_le hm,
obtain ⟨l, rfl⟩ := eq_coe_of_zero_le hn,
rw [←int.coe_nat_add, fpow_coe_nat, fpow_coe_nat, fpow_coe_nat, pow_add] }
end
theorem fpow_one_add {A : M} (h : is_unit A.det) (i : ℤ) : A ^ (1 + i) = A * A ^ i :=
by rw [fpow_add h, gpow_one]
theorem semiconj_by.fpow_right {A X Y : M} (hx : is_unit X.det) (hy : is_unit Y.det)
(h : semiconj_by A X Y) :
∀ m : ℤ, semiconj_by A (X^m) (Y^m)
| (n : ℕ) := by simp [h.pow_right n]
| -[1+n] := begin
by_cases ha : A = 0,
{ simp only [ha, semiconj_by.zero_left] },
have hx' : is_unit (X ^ n.succ).det,
{ rw det_pow,
exact hx.pow n.succ },
have hy' : is_unit (Y ^ n.succ).det,
{ rw det_pow,
exact hy.pow n.succ },
rw [gpow_neg_succ_of_nat, gpow_neg_succ_of_nat, nonsing_inv_apply _ hx', nonsing_inv_apply _ hy',
semiconj_by],
refine (is_regular_of_is_left_regular_det hy'.is_regular.left).left _,
rw [←mul_assoc, ←(h.pow_right n.succ).eq, mul_assoc, mul_eq_mul (X ^ _), mul_smul, mul_adjugate,
mul_eq_mul, mul_eq_mul, mul_eq_mul, ←matrix.mul_assoc, mul_smul (Y ^ _) (↑(hy'.unit)⁻¹ : R),
mul_adjugate, smul_smul, smul_smul, hx'.coe_inv_mul,
hy'.coe_inv_mul, one_smul, matrix.mul_one, matrix.one_mul],
end
theorem commute.fpow_right {A B : M} (h : commute A B) (m : ℤ) : commute A (B^m) :=
begin
rcases nonsing_inv_cancel_or_zero B with ⟨hB, hB'⟩ | hB,
{ refine semiconj_by.fpow_right _ _ h _;
exact is_unit_det_of_left_inverse hB },
{ cases m,
{ simpa using h.pow_right _ },
{ simp [←inv_pow', hB] } }
end
theorem commute.fpow_left {A B : M} (h : commute A B) (m : ℤ) : commute (A^m) B :=
(commute.fpow_right h.symm m).symm
theorem commute.fpow_fpow {A B : M} (h : commute A B) (m n : ℤ) : commute (A^m) (B^n) :=
commute.fpow_right (commute.fpow_left h _) _
theorem commute.fpow_self (A : M) (n : ℤ) : commute (A^n) A :=
commute.fpow_left (commute.refl A) _
theorem commute.self_fpow (A : M) (n : ℤ) : commute A (A^n) :=
commute.fpow_right (commute.refl A) _
theorem commute.fpow_fpow_self (A : M) (m n : ℤ) : commute (A^m) (A^n) :=
commute.fpow_fpow (commute.refl A) _ _
theorem fpow_bit0 (A : M) (n : ℤ) : A ^ bit0 n = A ^ n * A ^ n :=
begin
cases le_total 0 n with nonneg nonpos,
{ exact fpow_add_of_nonneg nonneg nonneg },
{ exact fpow_add_of_nonpos nonpos nonpos }
end
lemma fpow_add_one_of_ne_neg_one {A : M} : ∀ (n : ℤ), n ≠ -1 → A ^ (n + 1) = A ^ n * A
| (n : ℕ) _ := by simp [← int.coe_nat_succ, pow_succ']
| (-1) h := absurd rfl h
| (-((n : ℕ) + 2)) _ := begin
rcases nonsing_inv_cancel_or_zero A with ⟨h, h'⟩ | h,
{ apply fpow_add_one (is_unit_det_of_left_inverse h) },
{ show A ^ (-((n + 1 : ℕ) : ℤ)) = A ^ -((n + 2 : ℕ) : ℤ) * A,
simp_rw [fpow_neg_coe_nat, ←inv_pow', h, zero_pow nat.succ_pos', zero_mul] }
end
theorem fpow_bit1 (A : M) (n : ℤ) : A ^ bit1 n = A ^ n * A ^ n * A :=
begin
rw [bit1, fpow_add_one_of_ne_neg_one, fpow_bit0],
intro h,
simpa using congr_arg bodd h
end
theorem fpow_mul (A : M) (h : is_unit A.det) : ∀ m n : ℤ, A ^ (m * n) = (A ^ m) ^ n
| (m : ℕ) (n : ℕ) := by rw [gpow_coe_nat, gpow_coe_nat, ← pow_mul, ← gpow_coe_nat, int.coe_nat_mul]
| (m : ℕ) -[1+ n] := by rw [gpow_coe_nat, gpow_neg_succ_of_nat, ← pow_mul, coe_nat_mul_neg_succ,
←int.coe_nat_mul, fpow_neg_coe_nat]
| -[1+ m] (n : ℕ) := by rw [gpow_coe_nat, gpow_neg_succ_of_nat, ← inv_pow', ← pow_mul,
neg_succ_mul_coe_nat, ←int.coe_nat_mul, fpow_neg_coe_nat, inv_pow']
| -[1+ m] -[1+ n] := by { rw [gpow_neg_succ_of_nat, gpow_neg_succ_of_nat, neg_succ_mul_neg_succ,
←int.coe_nat_mul, fpow_coe_nat, inv_pow', ←pow_mul, nonsing_inv_nonsing_inv],
rw det_pow,
exact h.pow _ }
theorem fpow_mul' (A : M) (h : is_unit A.det) (m n : ℤ) : A ^ (m * n) = (A ^ n) ^ m :=
by rw [mul_comm, fpow_mul _ h]
@[simp, norm_cast] lemma units.coe_inv'' (u : units M) :
((u⁻¹ : units M) : M) = u⁻¹ :=
begin
refine (inv_eq_left_inv _).symm,
rw [←mul_eq_mul, ←units.coe_mul, inv_mul_self, units.coe_one]
end
@[simp, norm_cast] lemma units.coe_fpow (u : units M) :
∀ (n : ℤ), ((u ^ n : units M) : M) = u ^ n
| (n : ℕ) := by rw [gpow_coe_nat, gpow_coe_nat, units.coe_pow]
| -[1+k] := by rw [gpow_neg_succ_of_nat, gpow_neg_succ_of_nat, ←inv_pow, u⁻¹.coe_pow, ←inv_pow',
units.coe_inv'']
lemma fpow_ne_zero_of_is_unit_det [nonempty n'] [nontrivial R] {A : M}
(ha : is_unit A.det) (z : ℤ) : A ^ z ≠ 0 :=
begin
have := ha.det_fpow z,
contrapose! this,
rw [this, det_zero ‹_›],
exact not_is_unit_zero
end
lemma fpow_sub {A : M} (ha : is_unit A.det) (z1 z2 : ℤ) : A ^ (z1 - z2) = A ^ z1 / A ^ z2 :=
by rw [sub_eq_add_neg, fpow_add ha, fpow_neg ha, div_eq_mul_inv]
lemma commute.mul_fpow {A B : M} (h : commute A B) :
∀ (i : ℤ), (A * B) ^ i = (A ^ i) * (B ^ i)
| (n : ℕ) := by simp [h.mul_pow n, -mul_eq_mul]
| -[1+n] := by rw [gpow_neg_succ_of_nat, gpow_neg_succ_of_nat, gpow_neg_succ_of_nat,
mul_eq_mul (_⁻¹), ←mul_inv_rev, ←mul_eq_mul, h.mul_pow n.succ,
(h.pow_pow _ _).eq]
theorem fpow_bit0' (A : M) (n : ℤ) : A ^ bit0 n = (A * A) ^ n :=
(fpow_bit0 A n).trans (commute.mul_fpow (commute.refl A) n).symm
theorem fpow_bit1' (A : M) (n : ℤ) : A ^ bit1 n = (A * A) ^ n * A :=
by rw [fpow_bit1, commute.mul_fpow (commute.refl A)]
theorem fpow_neg_mul_fpow_self (n : ℤ) {A : M} (h : is_unit A.det) :
A ^ (-n) * A ^ n = 1 :=
by rw [fpow_neg h, mul_eq_mul, nonsing_inv_mul _ (h.det_fpow _)]
theorem one_div_pow {A : M} (n : ℕ) :
(1 / A) ^ n = 1 / A ^ n :=
by simp only [one_div, inv_pow']
theorem one_div_fpow {A : M} (n : ℤ) :
(1 / A) ^ n = 1 / A ^ n :=
by simp only [one_div, inv_fpow]
end int_pow
end matrix
|
904426ba3f63cb959668cc4478c018dcb5da5c56 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /08_Building_Theories_and_Proofs.org.35.lean | 8ca5788a6feb05cbaf23b9f319622fc64ca148ff | [] | 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 | 76 | lean | import standard
import data.nat
open nat (renaming add -> plus)
check plus
|
771a03bb8eac08ac4414c6828a51b2c706d6b0c7 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/data/nat/fib.lean | 8421207f132060dbc270c58717bf6a557691ffe9 | [
"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 | 5,740 | lean | /-
Copyright (c) 2019 Kevin Kappelmann. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Kappelmann
-/
import data.stream.basic
import data.nat.gcd
import tactic.ring
/-!
# The Fibonacci Sequence
## Summary
Definition of the Fibonacci sequence `F₀ = 0, F₁ = 1, Fₙ₊₂ = Fₙ + Fₙ₊₁`.
## Main Definitions
- `fib` returns the stream of Fibonacci numbers.
## Main Statements
- `fib_succ_succ` : shows that `fib` indeed satisfies the Fibonacci recurrence `Fₙ₊₂ = Fₙ + Fₙ₊₁.`.
- `fib_gcd` : `fib n` is a strong divisibility sequence.
## Implementation Notes
For efficiency purposes, the sequence is defined using `stream.iterate`.
## Tags
fib, fibonacci
-/
namespace nat
/-- Auxiliary function used in the definition of `fib_aux_stream`. -/
private def fib_aux_step : (ℕ × ℕ) → (ℕ × ℕ) := λ p, ⟨p.snd, p.fst + p.snd⟩
/-- Auxiliary stream creating Fibonacci pairs `⟨Fₙ, Fₙ₊₁⟩`. -/
private def fib_aux_stream : stream (ℕ × ℕ) := stream.iterate fib_aux_step ⟨0, 1⟩
/--
Implementation of the fibonacci sequence satisfying
`fib 0 = 0, fib 1 = 1, fib (n + 2) = fib n + fib (n + 1)`.
*Note:* We use a stream iterator for better performance when compared to the naive recursive
implementation.
-/
@[pp_nodot]
def fib (n : ℕ) : ℕ := (fib_aux_stream n).fst
@[simp] lemma fib_zero : fib 0 = 0 := rfl
@[simp] lemma fib_one : fib 1 = 1 := rfl
@[simp] lemma fib_two : fib 2 = 1 := rfl
private lemma fib_aux_stream_succ {n : ℕ} :
fib_aux_stream (n + 1) = fib_aux_step (fib_aux_stream n) :=
begin
change (stream.nth (n + 1) $ stream.iterate fib_aux_step ⟨0, 1⟩) =
fib_aux_step (stream.nth n $ stream.iterate fib_aux_step ⟨0, 1⟩),
rw [stream.nth_succ_iterate, stream.map_iterate, stream.nth_map]
end
/-- Shows that `fib` indeed satisfies the Fibonacci recurrence `Fₙ₊₂ = Fₙ + Fₙ₊₁.` -/
lemma fib_succ_succ {n : ℕ} : fib (n + 2) = fib n + fib (n + 1) :=
by simp only [fib, fib_aux_stream_succ, fib_aux_step]
lemma fib_pos {n : ℕ} (n_pos : 0 < n) : 0 < fib n :=
begin
induction n with n IH,
case nat.zero { norm_num at n_pos },
case nat.succ
{ cases n,
case nat.zero { simp [fib_succ_succ, zero_lt_one] },
case nat.succ
{ have : 0 ≤ fib n, by simp,
exact (lt_add_of_nonneg_of_lt this $ IH n.succ_pos) }}
end
lemma fib_le_fib_succ {n : ℕ} : fib n ≤ fib (n + 1) := by { cases n; simp [fib_succ_succ] }
@[mono] lemma fib_mono : monotone fib :=
monotone_of_monotone_nat $ λ _, fib_le_fib_succ
/-- `fib (n + 2)` is strictly monotone. -/
lemma fib_add_two_strict_mono : strict_mono (λ n, fib (n + 2)) :=
strict_mono.nat $ λ n, lt_add_of_pos_left _ $ fib_pos succ_pos'
lemma le_fib_self {n : ℕ} (five_le_n : 5 ≤ n) : n ≤ fib n :=
begin
induction five_le_n with n five_le_n IH,
{ have : 5 = fib 5, by refl, -- 5 ≤ fib 5
exact le_of_eq this },
{ -- n + 1 ≤ fib (n + 1) for 5 ≤ n
cases n with n', -- rewrite n = succ n' to use fib.succ_succ
{ have : 5 = 0, from nat.le_zero_iff.elim_left five_le_n, contradiction },
rw fib_succ_succ,
suffices : 1 + (n' + 1) ≤ fib n' + fib (n' + 1), by rwa [nat.succ_eq_add_one, add_comm],
have : n' ≠ 0, by { intro h, have : 5 ≤ 1, by rwa h at five_le_n, norm_num at this },
have : 1 ≤ fib n', from nat.succ_le_of_lt (fib_pos $ pos_iff_ne_zero.mpr this),
mono }
end
/-- Subsequent Fibonacci numbers are coprime,
see https://proofwiki.org/wiki/Consecutive_Fibonacci_Numbers_are_Coprime -/
lemma fib_coprime_fib_succ (n : ℕ) : nat.coprime (fib n) (fib (n + 1)) :=
begin
unfold coprime,
induction n with n ih,
{ simp },
{ convert ih using 1,
rw [fib_succ_succ, succ_eq_add_one, gcd_rec, add_mod_right, gcd_comm (fib n),
gcd_rec (fib (n + 1))], }
end
/-- See https://proofwiki.org/wiki/Fibonacci_Number_in_terms_of_Smaller_Fibonacci_Numbers -/
lemma fib_add (m n : ℕ) :
fib m * fib n + fib (m + 1) * fib (n + 1) = fib (m + n + 1) :=
begin
induction n with n ih generalizing m,
{ simp },
{ intros,
specialize ih (m + 1),
rw [add_assoc m 1 n, add_comm 1 n] at ih,
simp only [fib_succ_succ, ← ih],
ring, }
end
lemma gcd_fib_add_self (m n : ℕ) : gcd (fib m) (fib (n + m)) = gcd (fib m) (fib n) :=
begin
cases eq_zero_or_pos n,
{ rw h, simp },
replace h := nat.succ_pred_eq_of_pos h, rw [← h, succ_eq_add_one],
calc gcd (fib m) (fib (n.pred + 1 + m))
= gcd (fib m) (fib (n.pred) * (fib m) + fib (n.pred + 1) * fib (m + 1)) :
by { rw fib_add n.pred _, ring_nf }
... = gcd (fib m) (fib (n.pred + 1) * fib (m + 1)) :
by rw [add_comm, gcd_add_mul_self (fib m) _ (fib (n.pred))]
... = gcd (fib m) (fib (n.pred + 1)) :
coprime.gcd_mul_right_cancel_right
(fib (n.pred + 1)) (coprime.symm (fib_coprime_fib_succ m))
end
lemma gcd_fib_add_mul_self (m n : ℕ) : ∀ k, gcd (fib m) (fib (n + k * m)) = gcd (fib m) (fib n)
| 0 := by simp
| (k+1) := by rw [← gcd_fib_add_mul_self k, add_mul, ← add_assoc, one_mul, gcd_fib_add_self _ _]
/-- `fib n` is a strong divisibility sequence,
see https://proofwiki.org/wiki/GCD_of_Fibonacci_Numbers -/
lemma fib_gcd (m n : ℕ) : fib (gcd m n) = gcd (fib m) (fib n) :=
begin
wlog h : m ≤ n using [n m, m n],
exact le_total m n,
{ apply gcd.induction m n,
{ simp },
intros m n mpos h,
rw ← gcd_rec m n at h,
conv_rhs { rw ← mod_add_div' n m },
rwa [gcd_fib_add_mul_self m (n % m) (n / m), gcd_comm (fib m) _] },
rwa [gcd_comm, gcd_comm (fib m)]
end
lemma fib_dvd (m n : ℕ) (h : m ∣ n) : fib m ∣ fib n :=
by rwa [gcd_eq_left_iff_dvd, ← fib_gcd, gcd_eq_left_iff_dvd.mp]
end nat
|
f926c2e28707493c336b715ef55e0d7f23d5fda9 | 2cf781335f4a6706b7452ab07ce323201e2e101f | /lean/deps/galois_stdlib/src/galois/data/array.lean | 3e94dfb21795bc295c6cfddf415785d435c76f5b | [
"Apache-2.0"
] | permissive | simonjwinwood/reopt-vcg | 697cdd5e68366b5aa3298845eebc34fc97ccfbe2 | 6aca24e759bff4f2230bb58270bac6746c13665e | refs/heads/master | 1,586,353,878,347 | 1,549,667,148,000 | 1,549,667,148,000 | 159,409,828 | 0 | 0 | null | 1,543,358,444,000 | 1,543,358,444,000 | null | UTF-8 | Lean | false | false | 1,457 | lean | -- Additional definitions for arrays
import data.array.lemmas
import .array.lex_order
import ..logic
namespace array
/- Simplification rule for reading from an array constructed by push_back with a less-than test. -/
theorem read_push_back_lt_iff {α} {n:ℕ} (a : array n α) (x:α) (i : fin (n+1))
: read (push_back a x) i =
if lt:i.val < n then
read a ⟨i.val,lt⟩
else
x :=
begin
cases i with i i_lt_plus_1,
have i_le := nat.le_of_succ_le_succ i_lt_plus_1,
dsimp [push_back, read, d_array.read],
cases (decide (i = n)),
case decidable.is_true : is_eq {
simp [is_eq],
},
case decidable.is_false : is_neq {
have i_lt := lt_of_le_of_ne i_le is_neq,
simp [is_neq, i_lt],
},
end
/-- Lemma needed for stating read_slice below -/
theorem read_slice.len (s:ℕ) {e n:ℕ} (e_le_n : e ≤ n) (i:fin (e - s))
: s + i.val < n :=
calc s + i.val = i.val + s : add_comm _ _
... < e : nat.add_lt_of_lt_sub_right i.is_lt
... ≤ n : e_le_n
/- Simplify read (slice ...) -/
theorem read_slice {n:ℕ} {α} (a:array n α) (s e :ℕ) (s_le_e : s ≤ e) (e_le_n : e ≤ n) (i:fin (e - s))
: read (slice a s e s_le_e e_le_n) i = read a ⟨s + i.val, read_slice.len s e_le_n i⟩ :=
begin
cases a with f,
cases i,
simp [read, d_array.read, slice],
end
/-- Empty buffer converted to array is same as array.nil -/
@[simp]
theorem to_list_nil {α} : to_list (@nil α) = [] := eq.refl _
end array
|
65b0c82a78616bd20283b7a1508358d3c8153f1f | 947b78d97130d56365ae2ec264df196ce769371a | /src/Lean/Util/PPGoal.lean | 7c2e43591f8d1141bc6018807ad436488f7ec3cc | [
"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 | 3,091 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
import Lean.Util.PPExt
namespace Lean
def ppAuxDeclsDefault := false
@[init] def ppAuxDeclsOption : IO Unit :=
registerOption `pp.auxDecls { defValue := ppAuxDeclsDefault, group := "pp", descr := "display auxiliary declarations used to compile recursive functions" }
def getAuxDeclsOption (o : Options) : Bool:= o.get `pp.auxDecls ppAuxDeclsDefault
def ppGoal (ppCtx : PPContext) (mvarId : MVarId) : IO Format :=
let env := ppCtx.env;
let mctx := ppCtx.mctx;
let opts := ppCtx.opts;
match mctx.findDecl? mvarId with
| none => pure "unknown goal"
| some mvarDecl => do
let indent := 2; -- Use option
let ppAuxDecls := getAuxDeclsOption opts;
let lctx := mvarDecl.lctx;
let lctx := lctx.sanitizeNames.run' { options := opts };
let ppCtx := { ppCtx with lctx := lctx };
let pp (e : Expr) : IO Format := ppExpr ppCtx e;
let instMVars (e : Expr) : Expr := (mctx.instantiateMVars e).1;
let addLine (fmt : Format) : Format := if fmt.isNil then fmt else fmt ++ Format.line;
let pushPending (ids : List Name) (type? : Option Expr) (fmt : Format) : IO Format :=
if ids.isEmpty then
pure fmt
else
let fmt := addLine fmt;
match ids, type? with
| [], _ => pure fmt
| _, none => pure fmt
| _, some type => do {
typeFmt ← pp type;
pure $ fmt ++ (Format.joinSep ids.reverse " " ++ " :" ++ Format.nest indent (Format.line ++ typeFmt)).group
};
(varNames, type?, fmt) ← lctx.foldlM
(fun (acc : List Name × Option Expr × Format) (localDecl : LocalDecl) =>
if !ppAuxDecls && localDecl.isAuxDecl then pure acc else
let (varNames, prevType?, fmt) := acc;
match localDecl with
| LocalDecl.cdecl _ _ varName type _ =>
let varName := varName.simpMacroScopes;
let type := instMVars type;
if prevType? == none || prevType? == some type then
pure (varName :: varNames, some type, fmt)
else do
fmt ← pushPending varNames prevType? fmt;
pure ([varName], some type, fmt)
| LocalDecl.ldecl _ _ varName type val _ => do
let varName := varName.simpMacroScopes;
fmt ← pushPending varNames prevType? fmt;
let fmt := addLine fmt;
let type := instMVars type;
let val := instMVars val;
typeFmt ← pp type;
valFmt ← pp val;
let fmt := fmt ++ (format varName ++ " : " ++ typeFmt ++ " :=" ++ Format.nest indent (Format.line ++ valFmt)).group;
pure ([], none, fmt))
([], none, Format.nil);
fmt ← pushPending varNames type? fmt;
let fmt := addLine fmt;
typeFmt ← pp mvarDecl.type;
let fmt := fmt ++ "⊢" ++ " " ++ Format.nest indent typeFmt;
match mvarDecl.userName with
| Name.anonymous => pure fmt
| name => pure $ "case " ++ format name.eraseMacroScopes ++ Format.line ++ fmt
end Lean
|
6480058151b9bae09265d699bcc03d0f6aea56e5 | 00d2363f9655e2a7618f6b94dda7e2c4e5cf8d19 | /lean_modifications/tactic_interactive_modifications.lean | b98c63395a8d355b04f2fb8dd04c639234c76c5d | [
"Apache-2.0"
] | permissive | devjuice1/lean_proof_recording | 927e276e2ab8fb1288f51d9146dcfbf0d6444a87 | bf7c527315deccd35363fa7ca89d97d7b9cb6ac1 | refs/heads/master | 1,692,914,925,585 | 1,633,018,872,000 | 1,633,018,872,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,180 | lean | /- This is a staging area for code which will be inserted
into a Lean file.
The code to be inserted is between the line comments
`PR BEGIN MODIFICATION` and `PR END MODIFICATION`
It will be inserted by `insert_proof_recording_code.py`.
Insert info:
- file: `_target/deps/lean/library/init/meta/interactive.lean`
- location: after the itactic definition
Most of this code is carefully written, but
any code labeled "BEGIN/END CUSTOMIZABLE CODE"
encourages customization to change what
is being recorded
-/
prelude
import init.meta.tactic init.meta.type_context init.meta.rewrite_tactic init.meta.simp_tactic
import init.meta.smt.congruence_closure init.control.combinators
import init.meta.interactive_base init.meta.derive init.meta.match_tactic
import init.meta.congr_tactic init.meta.case_tag
import .interactive_base_modifications
namespace tactic
namespace interactive
--PR BEGIN MODIFICATION
@[reducible] meta def pr.recorded_itactic /-(tactic_name: string) (arg_num : nat)-/ : lean.parser (tactic unit) :=
lean.parser.val $ interactive.pr.record /-tactic_name arg_num "itactic"-/ lean.parser.itactic_reflected
--PR END MODIFICATION
end interactive
end tactic |
337ed900222871ac82add7b13ccc8ae354d7956b | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/testing/slim_check/sampleable.lean | 3da36e6b25d90caf2f1f175631ab40078cb07091 | [
"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 | 31,350 | lean | /-
Copyright (c) 2020 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
-/
import data.lazy_list.basic
import data.tree
import data.pnat.basic
import control.bifunctor
import control.ulift
import testing.slim_check.gen
import tactic.linarith
/-!
# `sampleable` Class
This class permits the creation samples of a given type
controlling the size of those values using the `gen` monad`. It also
helps minimize examples by creating smaller versions of given values.
When testing a proposition like `∀ n : ℕ, prime n → n ≤ 100`,
`slim_check` requires that `ℕ` have an instance of `sampleable` and for
`prime n` to be decidable. `slim_check` will then use the instance of
`sampleable` to generate small examples of ℕ and progressively increase
in size. For each example `n`, `prime n` is tested. If it is false,
the example will be rejected (not a test success nor a failure) and
`slim_check` will move on to other examples. If `prime n` is true, `n
≤ 100` will be tested. If it is false, `n` is a counter-example of `∀
n : ℕ, prime n → n ≤ 100` and the test fails. If `n ≤ 100` is true,
the test passes and `slim_check` moves on to trying more examples.
This is a port of the Haskell QuickCheck library.
## Main definitions
* `sampleable` class
* `sampleable_functor` and `sampleable_bifunctor` class
* `sampleable_ext` class
### `sampleable`
`sampleable α` provides ways of creating examples of type `α`,
and given such an example `x : α`, gives us a way to shrink it
and find simpler examples.
### `sampleable_ext`
`sampleable_ext` generalizes the behavior of `sampleable`
and makes it possible to express instances for types that
do not lend themselves to introspection, such as `ℕ → ℕ`.
If we test a quantification over functions the
counter-examples cannot be shrunken or printed meaningfully.
For that purpose, `sampleable_ext` provides a proxy representation
`proxy_repr` that can be printed and shrunken as well
as interpreted (using `interp`) as an object of the right type.
### `sampleable_functor` and `sampleable_bifunctor`
`sampleable_functor F` and `sampleable_bifunctor F` makes it possible
to create samples of and shrink `F α` given a sampling function and a
shrinking function for arbitrary `α`.
This allows us to separate the logic for generating the shape of a
collection from the logic for generating its contents. Specifically,
the contents could be generated using either `sampleable` or
`sampleable_ext` instance and the `sampleable_(bi)functor` does not
need to use that information
## Shrinking
Shrinking happens when `slim_check` find a counter-example to a
property. It is likely that the example will be more complicated than
necessary so `slim_check` proceeds to shrink it as much as
possible. Although equally valid, a smaller counter-example is easier
for a user to understand and use.
The `sampleable` class, beside having the `sample` function, has a
`shrink` function so that we can use specialized knowledge while
shrinking a value. It is not responsible for the whole shrinking process
however. It only has to take one step in the shrinking process.
`slim_check` will repeatedly call `shrink` until no more steps can
be taken. Because `shrink` guarantees that the size of the candidates
it produces is strictly smaller than the argument, we know that
`slim_check` is guaranteed to terminate.
## Tags
random testing
## References
* https://hackage.haskell.org/package/QuickCheck
-/
universes u v w
namespace slim_check
variables (α : Type u)
local infix ` ≺ `:50 := has_well_founded.r
/-- `sizeof_lt x y` compares the sizes of `x` and `y`. -/
def sizeof_lt {α} [has_sizeof α] (x y : α) := sizeof x < sizeof y
/-- `shrink_fn α` is the type of functions that shrink an
argument of type `α` -/
@[reducible]
def shrink_fn (α : Type*) [has_sizeof α] := Π x : α, lazy_list { y : α // sizeof_lt y x }
/-- `sampleable α` provides ways of creating examples of type `α`,
and given such an example `x : α`, gives us a way to shrink it
and find simpler examples. -/
class sampleable :=
[wf : has_sizeof α]
(sample [] : gen α)
(shrink : Π x : α, lazy_list { y : α // @sizeof _ wf y < @sizeof _ wf x } := λ _, lazy_list.nil)
attribute [instance, priority 100] has_well_founded_of_has_sizeof default_has_sizeof
attribute [instance, priority 200] sampleable.wf
/-- `sampleable_functor F` makes it possible to create samples of and
shrink `F α` given a sampling function and a shrinking function for
arbitrary `α` -/
class sampleable_functor (F : Type u → Type v) [functor F] :=
[wf : Π α [has_sizeof α], has_sizeof (F α)]
(sample [] : ∀ {α}, gen α → gen (F α))
(shrink : ∀ α [has_sizeof α], shrink_fn α → shrink_fn (F α))
(p_repr : ∀ α, has_repr α → has_repr (F α))
/-- `sampleable_bifunctor F` makes it possible to create samples of
and shrink `F α β` given a sampling function and a shrinking function
for arbitrary `α` and `β` -/
class sampleable_bifunctor (F : Type u → Type v → Type w) [bifunctor F] :=
[wf : Π α β [has_sizeof α] [has_sizeof β], has_sizeof (F α β)]
(sample [] : ∀ {α β}, gen α → gen β → gen (F α β))
(shrink : ∀ α β [has_sizeof α] [has_sizeof β], shrink_fn α → shrink_fn β → shrink_fn (F α β))
(p_repr : ∀ α β, has_repr α → has_repr β → has_repr (F α β))
export sampleable (sample shrink)
/-- This function helps infer the proxy representation and
interpretation in `sampleable_ext` instances. -/
meta def sampleable.mk_trivial_interp : tactic unit :=
tactic.refine ``(id)
/-- `sampleable_ext` generalizes the behavior of `sampleable`
and makes it possible to express instances for types that
do not lend themselves to introspection, such as `ℕ → ℕ`.
If we test a quantification over functions the
counter-examples cannot be shrunken or printed meaningfully.
For that purpose, `sampleable_ext` provides a proxy representation
`proxy_repr` that can be printed and shrunken as well
as interpreted (using `interp`) as an object of the right type. -/
class sampleable_ext (α : Sort u) :=
(proxy_repr : Type v)
[wf : has_sizeof proxy_repr]
(interp [] : proxy_repr → α . sampleable.mk_trivial_interp)
[p_repr : has_repr proxy_repr]
(sample [] : gen proxy_repr)
(shrink : shrink_fn proxy_repr)
attribute [instance, priority 100] sampleable_ext.p_repr sampleable_ext.wf
open nat lazy_list
section prio
open sampleable_ext
set_option default_priority 50
instance sampleable_ext.of_sampleable {α} [sampleable α] [has_repr α] : sampleable_ext α :=
{ proxy_repr := α,
sample := sampleable.sample α,
shrink := shrink }
instance sampleable.functor {α} {F} [functor F] [sampleable_functor F] [sampleable α] :
sampleable (F α) :=
{ wf := _,
sample := sampleable_functor.sample F (sampleable.sample α),
shrink := sampleable_functor.shrink α sampleable.shrink }
instance sampleable.bifunctor {α β} {F} [bifunctor F] [sampleable_bifunctor F] [sampleable α]
[sampleable β] : sampleable (F α β) :=
{ wf := _,
sample := sampleable_bifunctor.sample F (sampleable.sample α) (sampleable.sample β),
shrink := sampleable_bifunctor.shrink α β sampleable.shrink sampleable.shrink }
set_option default_priority 100
instance sampleable_ext.functor {α} {F} [functor F] [sampleable_functor F] [sampleable_ext α] :
sampleable_ext (F α) :=
{ wf := _,
proxy_repr := F (proxy_repr α),
interp := functor.map (interp _),
sample := sampleable_functor.sample F (sampleable_ext.sample α),
shrink := sampleable_functor.shrink _ sampleable_ext.shrink,
p_repr := sampleable_functor.p_repr _ sampleable_ext.p_repr }
instance sampleable_ext.bifunctor {α β} {F} [bifunctor F] [sampleable_bifunctor F]
[sampleable_ext α] [sampleable_ext β] : sampleable_ext (F α β) :=
{ wf := _,
proxy_repr := F (proxy_repr α) (proxy_repr β),
interp := bifunctor.bimap (interp _) (interp _),
sample := sampleable_bifunctor.sample F (sampleable_ext.sample α) (sampleable_ext.sample β),
shrink := sampleable_bifunctor.shrink _ _ sampleable_ext.shrink sampleable_ext.shrink,
p_repr := sampleable_bifunctor.p_repr _ _ sampleable_ext.p_repr sampleable_ext.p_repr }
end prio
/-- `nat.shrink' k n` creates a list of smaller natural numbers by
successively dividing `n` by 2 and subtracting the difference from
`k`. For example, `nat.shrink 100 = [50, 75, 88, 94, 97, 99]`. -/
def nat.shrink' (k : ℕ) : Π n : ℕ, n ≤ k →
list { m : ℕ // has_well_founded.r m k } → list { m : ℕ // has_well_founded.r m k }
| n hn ls :=
if h : n ≤ 1
then ls.reverse
else
have h₂ : 0 < n, by linarith,
have 1 * n / 2 < n,
from nat.div_lt_of_lt_mul (nat.mul_lt_mul_of_pos_right (by norm_num) h₂),
have n / 2 < n, by simpa,
let m := n / 2 in
have h₀ : m ≤ k, from le_trans (le_of_lt this) hn,
have h₃ : 0 < m,
by simp only [m, lt_iff_add_one_le, zero_add]; rw [nat.le_div_iff_mul_le]; linarith,
have h₁ : k - m < k,
from nat.sub_lt (lt_of_lt_of_le h₂ hn) h₃,
nat.shrink' m h₀ (⟨k - m, h₁⟩ :: ls)
/-- `nat.shrink n` creates a list of smaller natural numbers by
successively dividing by 2 and subtracting the difference from
`n`. For example, `nat.shrink 100 = [50, 75, 88, 94, 97, 99]`. -/
def nat.shrink (n : ℕ) : list { m : ℕ // has_well_founded.r m n } :=
if h : n > 0 then
have ∀ k, 1 < k → n / k < n, from
λ k hk,
nat.div_lt_of_lt_mul
(suffices 1 * n < k * n, by simpa,
nat.mul_lt_mul_of_pos_right hk h),
⟨n/11, this _ (by norm_num)⟩ :: ⟨n/3, this _ (by norm_num)⟩ :: nat.shrink' n n le_rfl []
else
[]
open gen
/--
Transport a `sampleable` instance from a type `α` to a type `β` using
functions between the two, going in both directions.
Function `g` is used to define the well-founded order that
`shrink` is expected to follow.
-/
def sampleable.lift (α : Type u) {β : Type u} [sampleable α] (f : α → β) (g : β → α)
(h : ∀ (a : α), sizeof (g (f a)) ≤ sizeof a) : sampleable β :=
{ wf := ⟨ sizeof ∘ g ⟩,
sample := f <$> sample α,
shrink := λ x,
have ∀ a, sizeof a < sizeof (g x) → sizeof (g (f a)) < sizeof (g x),
by introv h'; solve_by_elim [lt_of_le_of_lt],
subtype.map f this <$> shrink (g x) }
instance nat.sampleable : sampleable ℕ :=
{ sample := sized $ λ sz, freq [(1, coe <$> choose_any (fin $ succ (sz^3))),
(3, coe <$> choose_any (fin $ succ sz))] dec_trivial,
shrink := λ x, lazy_list.of_list $ nat.shrink x }
/-- `iterate_shrink p x` takes a decidable predicate `p` and a
value `x` of some sampleable type and recursively shrinks `x`.
It first calls `shrink x` to get a list of candidate sample,
finds the first that satisfies `p` and recursively tries
to shrink that one. -/
def iterate_shrink {α} [has_to_string α] [sampleable α]
(p : α → Prop) [decidable_pred p] :
α → option α :=
well_founded.fix has_well_founded.wf $ λ x f_rec,
do trace sformat!"{x} : {(shrink x).to_list}" $ pure (),
y ← (shrink x).find (λ a, p a),
f_rec y y.property <|> some y.val .
instance fin.sampleable {n : ℕ} [ne_zero n] : sampleable (fin n) :=
sampleable.lift ℕ fin.of_nat' fin.val $
λ i, (mod_le _ _ : i % n ≤ i)
@[priority 100]
instance fin.sampleable' {n} : sampleable (fin (succ n)) :=
sampleable.lift ℕ fin.of_nat fin.val $
λ i, (mod_le _ _ : i % succ n ≤ i)
instance pnat.sampleable : sampleable ℕ+ :=
sampleable.lift ℕ nat.succ_pnat pnat.nat_pred $ λ a,
by unfold_wf; simp only [pnat.nat_pred, succ_pnat, pnat.mk_coe, tsub_zero, succ_sub_succ_eq_sub]
/-- Redefine `sizeof` for `int` to make it easier to use with `nat` -/
def int.has_sizeof : has_sizeof ℤ := ⟨ int.nat_abs ⟩
local attribute [instance, priority 2000] int.has_sizeof
instance int.sampleable : sampleable ℤ :=
{ wf := _,
sample := sized $ λ sz,
freq [(1, subtype.val <$> choose (-(sz^3 + 1) : ℤ) (sz^3 + 1) (neg_le_self dec_trivial)),
(3, subtype.val <$> choose (-(sz + 1)) (sz + 1) (neg_le_self dec_trivial))]
dec_trivial,
shrink :=
λ x, lazy_list.of_list $ (nat.shrink $ int.nat_abs x).bind $
λ ⟨y,h⟩, [⟨y, h⟩, ⟨-y, by dsimp [sizeof,has_sizeof.sizeof]; rw int.nat_abs_neg; exact h ⟩] }
instance bool.sampleable : sampleable bool :=
{ wf := ⟨ λ b, if b then 1 else 0 ⟩,
sample := do { x ← choose_any bool,
return x },
shrink := λ b, if h : b then lazy_list.singleton ⟨ff, by cases h; unfold_wf⟩
else lazy_list.nil }
/--
Provided two shrinking functions `prod.shrink` shrinks a pair `(x, y)` by
first shrinking `x` and pairing the results with `y` and then shrinking
`y` and pairing the results with `x`.
All pairs either contain `x` untouched or `y` untouched. We rely on
shrinking being repeated for `x` to get maximally shrunken and then
for `y` to get shrunken too.
-/
def prod.shrink {α β} [has_sizeof α] [has_sizeof β]
(shr_a : shrink_fn α) (shr_b : shrink_fn β) : shrink_fn (α × β)
| ⟨x₀,x₁⟩ :=
let xs₀ : lazy_list { y : α × β // sizeof_lt y (x₀,x₁) } :=
(shr_a x₀).map $ subtype.map (λ a, (a, x₁))
(λ x h, by dsimp [sizeof_lt]; unfold_wf; apply h),
xs₁ : lazy_list { y : α × β // sizeof_lt y (x₀,x₁) } :=
(shr_b x₁).map $ subtype.map (λ a, (x₀, a))
(λ x h, by dsimp [sizeof_lt]; unfold_wf; apply h) in
xs₀.append xs₁
instance prod.sampleable : sampleable_bifunctor.{u v} prod :=
{ wf := _,
sample := λ α β sama samb, do
{ ⟨x⟩ ← (uliftable.up $ sama : gen (ulift.{max u v} α)),
⟨y⟩ ← (uliftable.up $ samb : gen (ulift.{max u v} β)),
pure (x,y) },
shrink := @prod.shrink,
p_repr := @prod.has_repr }
instance sigma.sampleable {α β} [sampleable α] [sampleable β] : sampleable (Σ _ : α, β) :=
sampleable.lift (α × β) (λ ⟨x,y⟩, ⟨x,y⟩) (λ ⟨x,y⟩, ⟨x,y⟩) $ λ ⟨x,y⟩, le_rfl
/-- shrinking function for sum types -/
def sum.shrink {α β} [has_sizeof α] [has_sizeof β] (shrink_α : shrink_fn α)
(shrink_β : shrink_fn β) : shrink_fn (α ⊕ β)
| (sum.inr x) := (shrink_β x).map $ subtype.map sum.inr $ λ a,
by dsimp [sizeof_lt]; unfold_wf; solve_by_elim
| (sum.inl x) := (shrink_α x).map $ subtype.map sum.inl $ λ a,
by dsimp [sizeof_lt]; unfold_wf; solve_by_elim
instance sum.sampleable : sampleable_bifunctor.{u v} sum :=
{ wf := _,
sample := λ (α : Type u) (β : Type v) sam_α sam_β,
(@uliftable.up_map gen.{u} gen.{max u v} _ _ _ _ (@sum.inl α β) sam_α <|>
@uliftable.up_map gen.{v} gen.{max v u} _ _ _ _ (@sum.inr α β) sam_β),
shrink := λ α β Iα Iβ shr_α shr_β, @sum.shrink _ _ Iα Iβ shr_α shr_β,
p_repr := @sum.has_repr }
instance rat.sampleable : sampleable ℚ :=
sampleable.lift (ℤ × ℕ+) (λ x, prod.cases_on x rat.mk_pnat) (λ r, (r.num, ⟨r.denom, r.pos⟩)) $
begin
intro i,
rcases i with ⟨x,⟨y,hy⟩⟩; unfold_wf;
dsimp [rat.mk_pnat],
mono*,
{ rw [← int.coe_nat_le, ← int.abs_eq_nat_abs, ← int.abs_eq_nat_abs],
apply int.abs_div_le_abs },
{ change _ - 1 ≤ y-1,
apply tsub_le_tsub_right,
apply nat.div_le_of_le_mul,
suffices : 1 * y ≤ x.nat_abs.gcd y * y, { simpa },
apply nat.mul_le_mul_right,
apply gcd_pos_of_pos_right _ hy }
end
/-- `sampleable_char` can be specialized into customized `sampleable char` instances.
The resulting instance has `1 / length` chances of making an unrestricted choice of characters
and it otherwise chooses a character from `characters` with uniform probabilities. -/
def sampleable_char (length : nat) (characters : string) : sampleable char :=
{ sample := do { x ← choose_nat 0 length dec_trivial,
if x.val = 0 then do
n ← sample ℕ,
pure $ char.of_nat n
else do
i ← choose_nat 0 (characters.length - 1) dec_trivial,
pure (characters.mk_iterator.nextn i).curr },
shrink := λ _, lazy_list.nil }
instance char.sampleable : sampleable char :=
sampleable_char 3 " 0123abcABC:,;`\\/"
variables {α}
section list_shrink
variables [has_sizeof α] (shr : Π x : α, lazy_list { y : α // sizeof_lt y x })
lemma list.sizeof_drop_lt_sizeof_of_lt_length {xs : list α} {k}
(hk : 0 < k) (hk' : k < xs.length) :
sizeof (list.drop k xs) < sizeof xs :=
begin
induction xs with x xs generalizing k,
{ cases hk' },
cases k,
{ cases hk },
have : sizeof xs < sizeof (x :: xs),
{ unfold_wf },
cases k,
{ simp only [this, list.drop] },
{ simp only [list.drop],
transitivity,
{ solve_by_elim [xs_ih, lt_of_succ_lt_succ hk', zero_lt_succ] },
{ assumption } }
end
lemma list.sizeof_cons_lt_right (a b : α) {xs : list α} (h : sizeof a < sizeof b) :
sizeof (a :: xs) < sizeof (b :: xs) :=
by unfold_wf; assumption
lemma list.sizeof_cons_lt_left (x : α) {xs xs' : list α} (h : sizeof xs < sizeof xs') :
sizeof (x :: xs) < sizeof (x :: xs') :=
by unfold_wf; assumption
lemma list.sizeof_append_lt_left {xs ys ys' : list α} (h : sizeof ys < sizeof ys') :
sizeof (xs ++ ys) < sizeof (xs ++ ys') :=
begin
induction xs,
{ apply h },
{ unfold_wf,
simp only [list.sizeof, add_lt_add_iff_left],
exact xs_ih }
end
lemma list.one_le_sizeof (xs : list α) : 1 ≤ sizeof xs :=
by cases xs; unfold_wf; linarith
/--
`list.shrink_removes` shrinks a list by removing chunks of size `k` in
the middle of the list.
-/
def list.shrink_removes (k : ℕ) (hk : 0 < k) : Π (xs : list α) n,
n = xs.length → lazy_list { ys : list α // sizeof_lt ys xs }
| xs n hn :=
if hkn : k > n then lazy_list.nil
else
if hkn' : k = n then
have 1 < xs.sizeof,
by { subst_vars, cases xs, { contradiction },
unfold_wf, apply lt_of_lt_of_le,
show 1 < 1 + has_sizeof.sizeof xs_hd + 1, { linarith },
{ mono, apply list.one_le_sizeof, } },
lazy_list.singleton ⟨[], this ⟩
else
have h₂ : k < xs.length, from hn ▸ lt_of_le_of_ne (le_of_not_gt hkn) hkn',
match list.split_at k xs, rfl : Π ys, ys = list.split_at k xs → _ with
| ⟨xs₁,xs₂⟩, h :=
have h₄ : xs₁ = xs.take k,
by simp only [list.split_at_eq_take_drop, prod.mk.inj_iff] at h; tauto,
have h₃ : xs₂ = xs.drop k,
by simp only [list.split_at_eq_take_drop, prod.mk.inj_iff] at h; tauto,
have sizeof xs₂ < sizeof xs,
by rw h₃; solve_by_elim [list.sizeof_drop_lt_sizeof_of_lt_length],
have h₁ : n - k = xs₂.length,
by simp only [h₃, ←hn, list.length_drop],
have h₅ : ∀ (a : list α), sizeof_lt a xs₂ → sizeof_lt (xs₁ ++ a) xs,
by intros a h; rw [← list.take_append_drop k xs, ← h₃, ← h₄];
solve_by_elim [list.sizeof_append_lt_left],
lazy_list.cons ⟨xs₂, this⟩ $ subtype.map ((++) xs₁) h₅ <$> list.shrink_removes xs₂ (n - k) h₁
end
/--
`list.shrink_one xs` shrinks list `xs` by shrinking only one item in
the list.
-/
def list.shrink_one : shrink_fn (list α)
| [] := lazy_list.nil
| (x :: xs) :=
lazy_list.append
(subtype.map (λ x', x' :: xs) (λ a, list.sizeof_cons_lt_right _ _) <$> shr x)
(subtype.map ((::) x) (λ _, list.sizeof_cons_lt_left _) <$> list.shrink_one xs)
/-- `list.shrink_with shrink_f xs` shrinks `xs` by first
considering `xs` with chunks removed in the middle (starting with
chunks of size `xs.length` and halving down to `1`) and then
shrinks only one element of the list.
This strategy is taken directly from Haskell's QuickCheck -/
def list.shrink_with (xs : list α) :
lazy_list { ys : list α // sizeof_lt ys xs } :=
let n := xs.length in
lazy_list.append
((lazy_list.cons n $ (shrink n).reverse.map subtype.val).bind (λ k,
if hk : 0 < k
then list.shrink_removes k hk xs n rfl
else lazy_list.nil ))
(list.shrink_one shr _)
end list_shrink
instance list.sampleable : sampleable_functor list.{u} :=
{ wf := _,
sample := λ α sam_α, list_of sam_α,
shrink := λ α Iα shr_α, @list.shrink_with _ Iα shr_α,
p_repr := @list.has_repr }
instance Prop.sampleable_ext : sampleable_ext Prop :=
{ proxy_repr := bool,
interp := coe,
sample := choose_any bool,
shrink := λ _, lazy_list.nil }
/-- `no_shrink` is a type annotation to signal that
a certain type is not to be shrunk. It can be useful in
combination with other types: e.g. `xs : list (no_shrink ℤ)`
will result in the list being cut down but individual
integers being kept as is. -/
def no_shrink (α : Type*) := α
instance no_shrink.inhabited {α} [inhabited α] : inhabited (no_shrink α) :=
⟨ (default : α) ⟩
/-- Introduction of the `no_shrink` type. -/
def no_shrink.mk {α} (x : α) : no_shrink α := x
/-- Selector of the `no_shrink` type. -/
def no_shrink.get {α} (x : no_shrink α) : α := x
instance no_shrink.sampleable {α} [sampleable α] : sampleable (no_shrink α) :=
{ sample := no_shrink.mk <$> sample α }
instance string.sampleable : sampleable string :=
{ sample := do { x ← list_of (sample char), pure x.as_string },
.. sampleable.lift (list char) list.as_string string.to_list $ λ _, le_rfl }
/-- implementation of `sampleable (tree α)` -/
def tree.sample (sample : gen α) : ℕ → gen (tree α) | n :=
if h : n > 0
then have n / 2 < n, from div_lt_self h (by norm_num),
tree.node <$> sample <*> tree.sample (n / 2) <*> tree.sample (n / 2)
else pure tree.nil
/-- `rec_shrink x f_rec` takes the recursive call `f_rec` introduced
by `well_founded.fix` and turns it into a shrinking function whose
result is adequate to use in a recursive call. -/
def rec_shrink {α : Type*} [has_sizeof α] (t : α)
(sh : Π x : α, sizeof_lt x t → lazy_list { y : α // sizeof_lt y x }) :
shrink_fn { t' : α // sizeof_lt t' t }
| ⟨t',ht'⟩ := (λ t'' : { y : α // sizeof_lt y t' },
⟨⟨t''.val, lt_trans t''.property ht'⟩, t''.property⟩ ) <$> sh t' ht'
lemma tree.one_le_sizeof {α} [has_sizeof α] (t : tree α) : 1 ≤ sizeof t :=
by cases t; unfold_wf; linarith
instance : functor tree :=
{ map := @tree.map }
/--
Recursion principle for shrinking tree-like structures.
-/
def rec_shrink_with [has_sizeof α]
(shrink_a : Π x : α, shrink_fn { y : α // sizeof_lt y x } →
list (lazy_list { y : α // sizeof_lt y x })) :
shrink_fn α :=
well_founded.fix (sizeof_measure_wf _) $ λ t f_rec,
lazy_list.join
(lazy_list.of_list $
shrink_a t $ λ ⟨t', h⟩, rec_shrink _ f_rec _)
lemma rec_shrink_with_eq [has_sizeof α]
(shrink_a : Π x : α, shrink_fn { y : α // sizeof_lt y x } →
list (lazy_list { y : α // sizeof_lt y x }))
(x : α) :
rec_shrink_with shrink_a x =
lazy_list.join
(lazy_list.of_list $ shrink_a x $ λ t', rec_shrink _ (λ x h', rec_shrink_with shrink_a x) _) :=
begin
conv_lhs { rw [rec_shrink_with, well_founded.fix_eq], },
congr, ext ⟨y, h⟩, refl
end
/-- `tree.shrink_with shrink_f t` shrinks `xs` by using the empty tree,
each subtrees, and by shrinking the subtree to recombine them.
This strategy is taken directly from Haskell's QuickCheck -/
def tree.shrink_with [has_sizeof α] (shrink_a : shrink_fn α) : shrink_fn (tree α) :=
rec_shrink_with $ λ t,
match t with
| tree.nil := λ f_rec, []
| (tree.node x t₀ t₁) :=
λ f_rec,
have h₂ : sizeof_lt tree.nil (tree.node x t₀ t₁),
by clear _match; have := tree.one_le_sizeof t₀;
dsimp [sizeof_lt, sizeof, has_sizeof.sizeof] at *;
unfold_wf; linarith,
have h₀ : sizeof_lt t₀ (tree.node x t₀ t₁),
by dsimp [sizeof_lt]; unfold_wf; linarith,
have h₁ : sizeof_lt t₁ (tree.node x t₀ t₁),
by dsimp [sizeof_lt]; unfold_wf; linarith,
[lazy_list.of_list [⟨tree.nil, h₂⟩, ⟨t₀, h₀⟩, ⟨t₁, h₁⟩],
(prod.shrink shrink_a (prod.shrink f_rec f_rec) (x, ⟨t₀, h₀⟩, ⟨t₁, h₁⟩)).map
$ λ ⟨⟨y,⟨t'₀, _⟩,⟨t'₁, _⟩⟩,hy⟩, ⟨tree.node y t'₀ t'₁,
by revert hy; dsimp [sizeof_lt]; unfold_wf; intro; linarith⟩]
end
instance sampleable_tree : sampleable_functor tree :=
{ wf := _,
sample := λ α sam_α, sized $ tree.sample sam_α,
shrink := λ α Iα shr_α, @tree.shrink_with _ Iα shr_α,
p_repr := @tree.has_repr }
/-- Type tag that signals to `slim_check` to use small values for a given type. -/
def small (α : Type*) := α
/-- Add the `small` type tag -/
def small.mk {α} (x : α) : small α := x
/-- Type tag that signals to `slim_check` to use large values for a given type. -/
def large (α : Type*) := α
/-- Add the `large` type tag -/
def large.mk {α} (x : α) : large α := x
instance small.functor : functor small := id.monad.to_functor
instance large.functor : functor large := id.monad.to_functor
instance small.inhabited [inhabited α] : inhabited (small α) := ⟨ (default : α) ⟩
instance large.inhabited [inhabited α] : inhabited (large α) := ⟨ (default : α) ⟩
instance small.sampleable_functor : sampleable_functor small :=
{ wf := _,
sample := λ α samp, gen.resize (λ n, n / 5 + 5) samp,
shrink := λ α _, id,
p_repr := λ α, id }
instance large.sampleable_functor : sampleable_functor large :=
{ wf := _,
sample := λ α samp, gen.resize (λ n, n * 5) samp,
shrink := λ α _, id,
p_repr := λ α, id }
instance ulift.sampleable_functor : sampleable_functor ulift.{u v} :=
{ wf := λ α h, ⟨ λ ⟨x⟩, @sizeof α h x ⟩,
sample := λ α samp, uliftable.up_map ulift.up $ samp,
shrink := λ α _ shr ⟨x⟩, (shr x).map (subtype.map ulift.up (λ a h, h)),
p_repr := λ α h, ⟨ @repr α h ∘ ulift.down ⟩ }
/-!
## Subtype instances
The following instances are meant to improve the testing of properties of the form
`∀ i j, i ≤ j, ...`
The naive way to test them is to choose two numbers `i` and `j` and check that
the proper ordering is satisfied. Instead, the following instances make it
so that `j` will be chosen with considerations to the required ordering
constraints. The benefit is that we will not have to discard any choice
of `j`.
-/
/-! ### Subtypes of `ℕ` -/
instance nat_le.sampleable {y} : slim_check.sampleable { x : ℕ // x ≤ y } :=
{ sample :=
do { ⟨x,h⟩ ← slim_check.gen.choose_nat 0 y dec_trivial,
pure ⟨x, h.2⟩},
shrink := λ ⟨x, h⟩, (λ a : subtype _, subtype.rec_on a $
λ x' h', ⟨⟨x', le_trans (le_of_lt h') h⟩, h'⟩) <$> shrink x }
instance nat_ge.sampleable {x} : slim_check.sampleable { y : ℕ // x ≤ y } :=
{ sample :=
do { (y : ℕ) ← slim_check.sampleable.sample ℕ,
pure ⟨x+y, by norm_num⟩ },
shrink := λ ⟨y, h⟩, (λ a : { y' // sizeof y' < sizeof (y - x) },
subtype.rec_on a $ λ δ h', ⟨⟨x + δ, nat.le_add_right _ _⟩, lt_tsub_iff_left.mp h'⟩) <$>
shrink (y - x) }
/- there is no `nat_lt.sampleable` instance because if `y = 0`, there is no valid choice
to satisfy `x < y` -/
instance nat_gt.sampleable {x} : slim_check.sampleable { y : ℕ // x < y } :=
{ sample :=
do { (y : ℕ) ← slim_check.sampleable.sample ℕ,
pure ⟨x+y+1, by linarith⟩ },
shrink := λ x, shrink _ }
/-! ### Subtypes of any `linear_ordered_add_comm_group` -/
instance le.sampleable {y : α} [sampleable α] [linear_ordered_add_comm_group α] :
slim_check.sampleable { x : α // x ≤ y } :=
{ sample :=
do { x ← sample α,
pure ⟨y - |x|, sub_le_self _ (abs_nonneg _) ⟩ },
shrink := λ _, lazy_list.nil }
instance ge.sampleable {x : α} [sampleable α] [linear_ordered_add_comm_group α] :
slim_check.sampleable { y : α // x ≤ y } :=
{ sample :=
do { y ← sample α,
pure ⟨x + |y|, by norm_num [abs_nonneg]⟩ },
shrink := λ _, lazy_list.nil }
/-!
### Subtypes of `ℤ`
Specializations of `le.sampleable` and `ge.sampleable` for `ℤ` to help instance search.
-/
instance int_le.sampleable {y : ℤ} : slim_check.sampleable { x : ℤ // x ≤ y } :=
sampleable.lift ℕ (λ n, ⟨y - n, int.sub_left_le_of_le_add $ by simp⟩) (λ ⟨i, h⟩, (y - i).nat_abs)
(λ n, by unfold_wf; simp [int_le.sampleable._match_1]; ring)
instance int_ge.sampleable {x : ℤ} : slim_check.sampleable { y : ℤ // x ≤ y } :=
sampleable.lift ℕ (λ n, ⟨x + n, by simp⟩) (λ ⟨i, h⟩, (i - x).nat_abs)
(λ n, by unfold_wf; simp [int_ge.sampleable._match_1]; ring)
instance int_lt.sampleable {y} : slim_check.sampleable { x : ℤ // x < y } :=
sampleable.lift ℕ (λ n, ⟨y - (n+1), int.sub_left_lt_of_lt_add $
by linarith [int.coe_nat_nonneg n]⟩)
(λ ⟨i, h⟩, (y - i - 1).nat_abs)
(λ n, by unfold_wf; simp [int_lt.sampleable._match_1]; ring)
instance int_gt.sampleable {x} : slim_check.sampleable { y : ℤ // x < y } :=
sampleable.lift ℕ (λ n, ⟨x + (n+1), by linarith⟩) (λ ⟨i, h⟩, (i - x - 1).nat_abs)
(λ n, by unfold_wf; simp [int_gt.sampleable._match_1]; ring)
/-! ### Subtypes of any `list` -/
instance perm.slim_check {xs : list α} : slim_check.sampleable { ys : list α // list.perm xs ys } :=
{ sample := permutation_of xs,
shrink := λ _, lazy_list.nil }
instance perm'.slim_check {xs : list α} :
slim_check.sampleable { ys : list α // list.perm ys xs } :=
{ sample := subtype.map id (@list.perm.symm α _) <$> permutation_of xs,
shrink := λ _, lazy_list.nil }
setup_tactic_parser
open tactic
/--
Print (at most) 10 samples of a given type to stdout for debugging.
-/
def print_samples {t : Type u} [has_repr t] (g : gen t) : io unit := do
xs ← io.run_rand $ uliftable.down $
do { xs ← (list.range 10).mmap $ g.run ∘ ulift.up,
pure ⟨xs.map repr⟩ },
xs.mmap' io.put_str_ln
/-- Create a `gen α` expression from the argument of `#sample` -/
meta def mk_generator (e : expr) : tactic (expr × expr) := do
t ← infer_type e,
match t with
| `(gen %%t) := do
repr_inst ← mk_app ``has_repr [t] >>= mk_instance,
pure (repr_inst, e)
| _ := do
samp_inst ← to_expr ``(sampleable_ext %%e) >>= mk_instance,
repr_inst ← mk_mapp ``sampleable_ext.p_repr [e, samp_inst],
gen ← mk_mapp ``sampleable_ext.sample [none, samp_inst],
pure (repr_inst, gen)
end
/--
`#sample my_type`, where `my_type` has an instance of `sampleable`, prints ten random
values of type `my_type` of using an increasing size parameter.
```lean
#sample nat
-- prints
-- 0
-- 0
-- 2
-- 24
-- 64
-- 76
-- 5
-- 132
-- 8
-- 449
-- or some other sequence of numbers
#sample list int
-- prints
-- []
-- [1, 1]
-- [-7, 9, -6]
-- [36]
-- [-500, 105, 260]
-- [-290]
-- [17, 156]
-- [-2364, -7599, 661, -2411, -3576, 5517, -3823, -968]
-- [-643]
-- [11892, 16329, -15095, -15461]
-- or whatever
```
-/
@[user_command]
meta def sample_cmd (_ : parse $ tk "#sample") : lean.parser unit :=
do e ← texpr,
of_tactic $ do
e ← i_to_expr e,
(repr_inst, gen) ← mk_generator e,
print_samples ← mk_mapp ``print_samples [none, repr_inst, gen],
sample ← eval_expr (io unit) print_samples,
unsafe_run_io sample
end slim_check
|
b2119c0238b7f9c48693a0e66e2f01b255977bac | d1a52c3f208fa42c41df8278c3d280f075eb020c | /src/Lean/Class.lean | d86036190902c38edc1a284d59c5c3d6aa7910f1 | [
"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 | 4,410 | 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.Attributes
namespace Lean
structure ClassEntry where
name : Name
hasOutParam : Bool
namespace ClassEntry
def lt (a b : ClassEntry) : Bool :=
Name.quickLt a.name b.name
end ClassEntry
structure ClassState where
hasOutParam : SMap Name Bool := SMap.empty
deriving Inhabited
namespace ClassState
def addEntry (s : ClassState) (entry : ClassEntry) : ClassState :=
{ s with hasOutParam := s.hasOutParam.insert entry.name entry.hasOutParam }
def switch (s : ClassState) : ClassState :=
{ s with hasOutParam := s.hasOutParam.switch }
end ClassState
/- TODO: add support for scoped instances -/
builtin_initialize classExtension : SimplePersistentEnvExtension ClassEntry ClassState ←
registerSimplePersistentEnvExtension {
name := `classExt
addEntryFn := ClassState.addEntry
addImportedFn := fun es => (mkStateFromImportedEntries ClassState.addEntry {} es).switch
}
@[export lean_is_class]
def isClass (env : Environment) (n : Name) : Bool :=
(classExtension.getState env).hasOutParam.contains n
@[export lean_has_out_params]
def hasOutParams (env : Environment) (n : Name) : Bool :=
match (classExtension.getState env).hasOutParam.find? n with
| some b => b
| none => false
@[export lean_is_out_param]
def isOutParam (e : Expr) : Bool :=
e.isAppOfArity `outParam 1
/--
Auxiliary function for checking whether a class has `outParam`, and
whether they are being correctly used.
A regular (i.e., non `outParam`) must not depend on an `outParam`.
Reason for this restriction:
When performing type class resolution, we replace arguments that
are `outParam`s with fresh metavariables. If regular parameters could
depend on `outParam`s, then we would also have to replace them with
fresh metavariables. Otherwise, the resulting expression could be type
incorrect. This transformation would be counterintuitive to users since
we would implicitly treat these regular parameters as `outParam`s.
-/
private partial def checkOutParam : Nat → Array FVarId → Expr → Except String Bool
| i, outParams, Expr.forallE _ d b _ =>
if isOutParam d then
let fvarId := { name := Name.mkNum `_fvar outParams.size }
let outParams := outParams.push fvarId
let fvar := mkFVar fvarId
let b := b.instantiate1 fvar
checkOutParam (i+1) outParams b
else if d.hasAnyFVar fun fvarId => outParams.contains fvarId then
Except.error s!"invalid class, parameter #{i} depends on `outParam`, but it is not an `outParam`"
else
checkOutParam (i+1) outParams b
| i, outParams, e => pure (outParams.size > 0)
def addClass (env : Environment) (clsName : Name) : Except String Environment :=
if isClass env clsName then
Except.error s!"class has already been declared '{clsName}'"
else match env.find? clsName with
| none => Except.error ("unknown declaration '" ++ toString clsName ++ "'")
| some decl@(ConstantInfo.inductInfo _) => do
let b ← checkOutParam 1 #[] decl.type
Except.ok (classExtension.addEntry env { name := clsName, hasOutParam := b })
| some _ => Except.error ("invalid 'class', declaration '" ++ toString clsName ++ "' must be inductive datatype or structure")
private def consumeNLambdas : Nat → Expr → Option Expr
| 0, e => some e
| i+1, Expr.lam _ _ b _ => consumeNLambdas i b
| _, _ => none
partial def getClassName (env : Environment) : Expr → Option Name
| Expr.forallE _ _ b _ => getClassName env b
| e => OptionM.run do
let Expr.const c _ _ ← pure e.getAppFn | none
let info ← env.find? c
match info.value? with
| some val => do
let body ← consumeNLambdas e.getAppNumArgs val
getClassName env body
| none =>
if isClass env c then some c
else none
builtin_initialize
registerBuiltinAttribute {
name := `class,
descr := "type class",
add := fun decl stx kind => do
let env ← getEnv
Attribute.Builtin.ensureNoArgs stx
unless kind == AttributeKind.global do throwError "invalid attribute 'class', must be global"
let env ← ofExcept (addClass env decl)
setEnv env
}
end Lean
|
e976b4d19a29099a68fee49929bd522489f9ba61 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/number_theory/zsqrtd/to_real.lean | 8277868f99867f07f6744eed456b3c59668dcd0a | [
"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 | 966 | lean | /-
Copyright (c) 2021 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import data.real.sqrt
import number_theory.zsqrtd.basic
/-!
# Image of `zsqrtd` in `ℝ`
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines `zsqrtd.to_real` and related lemmas.
It is in a separate file to avoid pulling in all of `data.real` into `data.zsqrtd`.
-/
namespace zsqrtd
/-- The image of `zsqrtd` in `ℝ`, using `real.sqrt` which takes the positive root of `d`.
If the negative root is desired, use `to_real h a.conj`. -/
@[simps]
noncomputable def to_real {d : ℤ} (h : 0 ≤ d) : ℤ√d →+* ℝ :=
lift ⟨real.sqrt d, real.mul_self_sqrt (int.cast_nonneg.mpr h)⟩
lemma to_real_injective {d : ℤ} (h0d : 0 ≤ d) (hd : ∀ n : ℤ, d ≠ n*n) :
function.injective (to_real h0d) :=
lift_injective _ hd
end zsqrtd
|
189de9b86615509121bcbd8ff712733ca5c86edf | a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7 | /src/category_theory/connected.lean | eb45adee448d3349f5d326e502706c73c86a43ec | [
"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 | 9,208 | 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 category_theory.const
import category_theory.discrete_category
import category_theory.eq_to_hom
import category_theory.punit
/-!
# Connected category
Define a connected category as a _nonempty_ category for which every functor
to a discrete category is isomorphic to the constant functor.
NB. Some authors include the empty category as connected, we do not.
We instead are interested in categories with exactly one 'connected
component'.
We give some equivalent definitions:
- A nonempty category for which every functor to a discrete category is
constant on objects.
See `any_functor_const_on_obj` and `connected.of_any_functor_const_on_obj`.
- A nonempty category for which every function `F` for which the presence of a
morphism `f : j₁ ⟶ j₂` implies `F j₁ = F j₂` must be constant everywhere.
See `constant_of_preserves_morphisms` and `connected.of_constant_of_preserves_morphisms`.
- A nonempty category for which any subset of its elements containing the
default and closed under morphisms is everything.
See `induct_on_objects` and `connected.of_induct`.
- A nonempty category for which every object is related under the reflexive
transitive closure of the relation "there is a morphism in some direction
from `j₁` to `j₂`".
See `connected_zigzag` and `zigzag_connected`.
- A nonempty category for which for any two objects there is a sequence of
morphisms (some reversed) from one to the other.
See `exists_zigzag'` and `connected_of_zigzag`.
We also prove the result that the functor given by `(X × -)` preserves any
connected limit. That is, any limit of shape `J` where `J` is a connected
category is preserved by the functor `(X × -)`.
-/
universes v₁ v₂ u₁ u₂
open category_theory.category
namespace category_theory
section connected
-- See note [default priority]
set_option default_priority 100
/--
We define a connected category as a _nonempty_ category for which every
functor to a discrete category is constant.
NB. Some authors include the empty category as connected, we do not.
We instead are interested in categories with exactly one 'connected
component'.
This allows us to show that the functor X ⨯ - preserves connected limits.
-/
class connected (J : Type v₂) [category.{v₁} J] extends inhabited J :=
(iso_constant : Π {α : Type v₂} (F : J ⥤ discrete α), F ≅ (functor.const J).obj (F.obj default))
end connected
variables {J : Type v₂} [category.{v₁} J]
/--
If J is connected, any functor to a discrete category is constant on objects.
The converse is given in `connected.of_any_functor_const_on_obj`.
-/
lemma any_functor_const_on_obj [connected J] {α : Type v₂} (F : J ⥤ discrete α) (j : J) :
F.obj j = F.obj (default J) :=
((connected.iso_constant F).hom.app j).down.1
/--
If any functor to a discrete category is constant on objects, J is connected.
The converse of `any_functor_const_on_obj`.
-/
def connected.of_any_functor_const_on_obj [inhabited J]
(h : ∀ {α : Type v₂} (F : J ⥤ discrete α), ∀ (j : J), F.obj j = F.obj (default J)) :
connected J :=
{ iso_constant := λ α F, nat_iso.of_components (λ B, eq_to_iso (h F B)) (λ _ _ _, subsingleton.elim _ _) }
/--
If `J` is connected, then given any function `F` such that the presence of a
morphism `j₁ ⟶ j₂` implies `F j₁ = F j₂`, we have that `F` is constant.
This can be thought of as a local-to-global property.
The converse is shown in `connected.of_constant_of_preserves_morphisms`
-/
lemma constant_of_preserves_morphisms [connected J] {α : Type v₂} (F : J → α)
(h : ∀ (j₁ j₂ : J) (f : j₁ ⟶ j₂), F j₁ = F j₂) (j : J) :
F j = F (default J) :=
any_functor_const_on_obj { obj := F, map := λ _ _ f, eq_to_hom (h _ _ f) } j
/--
`J` is connected if: given any function `F : J → α` which is constant for any
`j₁, j₂` for which there is a morphism `j₁ ⟶ j₂`, then `F` is constant.
This can be thought of as a local-to-global property.
The converse of `constant_of_preserves_morphisms`.
-/
def connected.of_constant_of_preserves_morphisms [inhabited J]
(h : ∀ {α : Type v₂} (F : J → α), (∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), F j₁ = F j₂) → (∀ j : J, F j = F (default J))) :
connected J :=
connected.of_any_functor_const_on_obj (λ _ F, h F.obj (λ _ _ f, (F.map f).down.1))
/--
An inductive-like property for the objects of a connected category.
If `default J` is in the set `p`, and `p` is closed under morphisms of `J`,
then `p` contains all of `J`.
The converse is given in `connected.of_induct`.
-/
lemma induct_on_objects [connected J] (p : set J) (h0 : default J ∈ p)
(h1 : ∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), j₁ ∈ p ↔ j₂ ∈ p) (j : J) :
j ∈ p :=
begin
injection (constant_of_preserves_morphisms (λ k, ulift.up (k ∈ p)) (λ j₁ j₂ f, _) j) with i,
rwa i,
dsimp,
exact congr_arg ulift.up (propext (h1 f)),
end
/--
If any maximal connected component of J containing the default is all of J, then J is connected.
The converse of `induct_on_objects`.
-/
def connected.of_induct [inhabited J]
(h : ∀ (p : set J), default J ∈ p → (∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), j₁ ∈ p ↔ j₂ ∈ p) → ∀ (j : J), j ∈ p) :
connected J :=
connected.of_constant_of_preserves_morphisms (λ α F a, h {j | F j = F (default J)} rfl (λ _ _ f, by simp [a f] ))
/-- j₁ and j₂ are related by `zag` if there is a morphism between them. -/
@[reducible]
def zag (j₁ j₂ : J) : Prop := nonempty (j₁ ⟶ j₂) ∨ nonempty (j₂ ⟶ j₁)
/--
`j₁` and `j₂` are related by `zigzag` if there is a chain of
morphisms from `j₁` to `j₂`, with backward morphisms allowed.
-/
@[reducible]
def zigzag : J → J → Prop := relation.refl_trans_gen zag
/-- Any equivalence relation containing (⟶) holds for all pairs of a connected category. -/
lemma equiv_relation [connected J] (r : J → J → Prop) (hr : _root_.equivalence r)
(h : ∀ {j₁ j₂ : J} (f : j₁ ⟶ j₂), r j₁ j₂) :
∀ (j₁ j₂ : J), r j₁ j₂ :=
begin
have z: ∀ (j : J), r (default J) j :=
induct_on_objects (λ k, r (default J) k)
(hr.1 (default J)) (λ _ _ f, ⟨λ t, hr.2.2 t (h f), λ t, hr.2.2 t (hr.2.1 (h f))⟩),
intros, apply hr.2.2 (hr.2.1 (z _)) (z _)
end
/-- In a connected category, any two objects are related by `zigzag`. -/
lemma connected_zigzag [connected J] (j₁ j₂ : J) : zigzag j₁ j₂ :=
equiv_relation _
(mk_equivalence _
relation.reflexive_refl_trans_gen
(relation.refl_trans_gen.symmetric (λ _ _ _, by rwa [zag, or_comm]))
relation.transitive_refl_trans_gen)
(λ _ _ f, relation.refl_trans_gen.single (or.inl (nonempty.intro f))) _ _
/--
If any two objects in an inhabited category are related by `zigzag`, the category is connected.
-/
def zigzag_connected [inhabited J] (h : ∀ (j₁ j₂ : J), zigzag j₁ j₂) : connected J :=
begin
apply connected.of_induct,
intros,
have: ∀ (j₁ j₂ : J), zigzag j₁ j₂ → (j₁ ∈ p ↔ j₂ ∈ p),
{ introv k,
induction k,
{ refl },
{ rw k_ih,
rcases k_a_1 with ⟨⟨_⟩⟩ | ⟨⟨_⟩⟩,
apply a_1 k_a_1,
apply (a_1 k_a_1).symm } },
rwa this j (default J) (h _ _)
end
lemma exists_zigzag' [connected J] (j₁ j₂ : J) :
∃ l, list.chain zag j₁ l ∧ list.last (j₁ :: l) (list.cons_ne_nil _ _) = j₂ :=
list.exists_chain_of_relation_refl_trans_gen (connected_zigzag _ _)
/--
If any two objects in an inhabited category are linked by a sequence of (potentially reversed)
morphisms, then J is connected.
The converse of `exists_zigzag'`.
-/
def connected_of_zigzag [inhabited J]
(h : ∀ (j₁ j₂ : J), ∃ l, list.chain zag j₁ l ∧ list.last (j₁ :: l) (list.cons_ne_nil _ _) = j₂) :
connected J :=
begin
apply connected.of_induct,
intros p d k j,
obtain ⟨l, zags, lst⟩ := h j (default J),
apply list.chain.induction p l zags lst _ d,
rintros _ _ (⟨⟨_⟩⟩ | ⟨⟨_⟩⟩),
{ exact (k a).2 },
{ exact (k a).1 }
end
/-- If `discrete α` is connected, then `α` is (type-)equivalent to `punit`. -/
def discrete_connected_equiv_punit {α : Type*} [connected (discrete α)] : α ≃ punit :=
discrete.equiv_of_equivalence
{ functor := functor.star _,
inverse := discrete.functor (λ _, default _),
unit_iso := by apply connected.iso_constant,
counit_iso := functor.punit_ext _ _ }
variables {C : Type u₂} [category.{v₂} C]
/--
For objects `X Y : C`, any natural transformation `α : const X ⟶ const Y` from a connected
category must be constant.
This is the key property of connected categories which we use to establish properties about limits.
-/
lemma nat_trans_from_connected [conn : connected J] {X Y : C}
(α : (functor.const J).obj X ⟶ (functor.const J).obj Y) :
∀ (j : J), α.app j = (α.app (default J) : X ⟶ Y) :=
@constant_of_preserves_morphisms _ _ _
(X ⟶ Y)
(λ j, α.app j)
(λ _ _ f, (by { have := α.naturality f, erw [id_comp, comp_id] at this, exact this.symm }))
end category_theory
|
499b22a5eed42f96d15d282eff3a1a66efacd948 | 618003631150032a5676f229d13a079ac875ff77 | /src/category_theory/limits/shapes/finite_limits.lean | 8b6e133eda2242c3f79108ee2804f46217d288ba | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 1,916 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.limits.shapes.products
universes v u
open category_theory
namespace category_theory.limits
/-- A category with a `fintype` of objects, and a `fintype` for each morphism space. -/
class fin_category (J : Type v) [small_category J] :=
(decidable_eq_obj : decidable_eq J . tactic.apply_instance)
(fintype_obj : fintype J . tactic.apply_instance)
(decidable_eq_hom : Π (j j' : J), decidable_eq (j ⟶ j') . tactic.apply_instance)
(fintype_hom : Π (j j' : J), fintype (j ⟶ j') . tactic.apply_instance)
attribute [instance] fin_category.decidable_eq_obj fin_category.fintype_obj
fin_category.decidable_eq_hom fin_category.fintype_hom
-- We need a `decidable_eq` instance here to construct `fintype` on the morphism spaces.
instance fin_category_discrete_of_decidable_fintype (J : Type v) [fintype J] [decidable_eq J] :
fin_category (discrete J) :=
{ }
variables (C : Type u) [category.{v} C]
class has_finite_limits :=
(has_limits_of_shape : Π (J : Type v) [small_category J] [fin_category J], has_limits_of_shape.{v} J C)
class has_finite_colimits :=
(has_colimits_of_shape : Π (J : Type v) [small_category J] [fin_category J], has_colimits_of_shape.{v} J C)
attribute [instance, priority 100] -- see Note [lower instance priority]
has_finite_limits.has_limits_of_shape
has_finite_colimits.has_colimits_of_shape
@[priority 100] -- see Note [lower instance priority]
instance [has_limits.{v} C] : has_finite_limits.{v} C :=
{ has_limits_of_shape := λ J _ _, by { resetI, apply_instance } }
@[priority 100] -- see Note [lower instance priority]
instance [has_colimits.{v} C] : has_finite_colimits.{v} C :=
{ has_colimits_of_shape := λ J _ _, by { resetI, apply_instance } }
end category_theory.limits
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.