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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
508bc8d3554af5403db20f7c9f0b494d1ec07dfa | a4673261e60b025e2c8c825dfa4ab9108246c32e | /tests/lean/run/incmd.lean | 7333fcbfd6e3f6402175c98ebe8839e268f183c6 | [
"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 | 223 | lean |
variable {α : Type} in
def f : α → α :=
fun x => x
#check @f
variables {α : Type} {β : Type} in
variable (h : α → α) in
set_option syntaxMaxDepth 1000 in
def g : α → β → α :=
fun x _ => h x
#check @g
|
3fc3d395939695301a4b60221f8a7a6c856eb557 | 958488bc7f3c2044206e0358e56d7690b6ae696c | /lean/nat.lean | ef2b259f316ab3df2a3e3a1e0fc2a6b3e456f5b1 | [] | no_license | possientis/Prog | a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4 | d4b3debc37610a88e0dac3ac5914903604fd1d1f | refs/heads/master | 1,692,263,717,723 | 1,691,757,179,000 | 1,691,757,179,000 | 40,361,602 | 3 | 0 | null | 1,679,896,438,000 | 1,438,953,859,000 | Coq | UTF-8 | Lean | false | false | 1,939 | lean | namespace hidden1
inductive nat : Type
| zero : nat
| succ : nat → nat
open nat
--#check @nat.succ
--#check @nat.rec
--#check @nat.rec_on
def add2 (m n : nat) : nat :=
nat.rec_on n m (λ n add_m_n, nat.succ add_m_n)
end hidden1
open nat
lemma L1 : ∀ (n : ℕ), 0 + n = n :=
assume n,
nat.rec_on n
(show 0 + 0 = 0, from rfl)
(assume m (IH : 0 + m = m),
show 0 + succ m = succ m, from
calc
0 + succ m = succ (0 + m) : rfl
... = succ m: by rw IH)
--#check L1
--#check add_succ
lemma L2 : ∀ (n : ℕ), 0 + n = n :=
assume n, nat.rec_on n rfl (λ (n:ℕ) (p:0 + n = n), by rw [add_succ, p])
--#check L2
lemma L3 : ∀ (n : ℕ), 0 + n = n :=
assume n, nat.rec_on n rfl (λ n IH, by simp only [add_succ, IH])
--#check L3
lemma L4 : ∀ (m n p : ℕ), (m + n) + p = m + (n + p) :=
assume m n p, nat.rec_on p
(by simp)
(assume p IH, show (m + n) + succ p = m + (n + succ p), from
calc
(m + n) + succ p = succ ((m + n) + p) : by refl
... = succ (m + (n + p)) : by rw [IH]
... = m + succ (n + p) : by refl
... = m + (n + succ p) : by refl)
--#check L4
lemma L5 : ∀ (m n : ℕ), succ m + n = succ (m + n) :=
assume m n, nat.rec_on n
(by refl)
(assume n IH, show succ m + succ n = succ (m + succ n), from
calc
succ m + succ n = succ (succ m + n) : by refl
... = succ (succ (m + n)) : by rw IH
... = succ (m + succ n) : by refl)
--#check L5
lemma L6 : ∀ (m n : ℕ), m + n = n + m :=
assume m n, nat.rec_on n
(begin symmetry, apply L1 end)
(assume n IH,
show m + succ n = succ n + m, from
calc
m + succ n = succ (m + n) : by refl
... = succ (n + m) : by rw IH
... = succ n + m : symm (L5 n m))
--#check L6
|
b0cd6bef82ada50e2790934b5b47b909a36bbf77 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/topology/uniform_space/basic.lean | 733ea8c3e668ab413a09cc9b7267b997f9ba173d | [
"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 | 68,359 | 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, Patrick Massot
-/
import order.filter.lift
import topology.separation
/-!
# Uniform spaces
Uniform spaces are a generalization of metric spaces and topological groups. Many concepts directly
generalize to uniform spaces, e.g.
* uniform continuity (in this file)
* completeness (in `cauchy.lean`)
* extension of uniform continuous functions to complete spaces (in `uniform_embedding.lean`)
* totally bounded sets (in `cauchy.lean`)
* totally bounded complete sets are compact (in `cauchy.lean`)
A uniform structure on a type `X` is a filter `𝓤 X` on `X × X` satisfying some conditions
which makes it reasonable to say that `∀ᶠ (p : X × X) in 𝓤 X, ...` means
"for all p.1 and p.2 in X close enough, ...". Elements of this filter are called entourages
of `X`. The two main examples are:
* If `X` is a metric space, `V ∈ 𝓤 X ↔ ∃ ε > 0, { p | dist p.1 p.2 < ε } ⊆ V`
* If `G` is an additive topological group, `V ∈ 𝓤 G ↔ ∃ U ∈ 𝓝 (0 : G), {p | p.2 - p.1 ∈ U} ⊆ V`
Those examples are generalizations in two different directions of the elementary example where
`X = ℝ` and `V ∈ 𝓤 ℝ ↔ ∃ ε > 0, { p | |p.2 - p.1| < ε } ⊆ V` which features both the topological
group structure on `ℝ` and its metric space structure.
Each uniform structure on `X` induces a topology on `X` characterized by
> `nhds_eq_comap_uniformity : ∀ {x : X}, 𝓝 x = comap (prod.mk x) (𝓤 X)`
where `prod.mk x : X → X × X := (λ y, (x, y))` is the partial evaluation of the product
constructor.
The dictionary with metric spaces includes:
* an upper bound for `dist x y` translates into `(x, y) ∈ V` for some `V ∈ 𝓤 X`
* a ball `ball x r` roughly corresponds to `uniform_space.ball x V := {y | (x, y) ∈ V}`
for some `V ∈ 𝓤 X`, but the later is more general (it includes in
particular both open and closed balls for suitable `V`).
In particular we have:
`is_open_iff_ball_subset {s : set X} : is_open s ↔ ∀ x ∈ s, ∃ V ∈ 𝓤 X, ball x V ⊆ s`
The triangle inequality is abstracted to a statement involving the composition of relations in `X`.
First note that the triangle inequality in a metric space is equivalent to
`∀ (x y z : X) (r r' : ℝ), dist x y ≤ r → dist y z ≤ r' → dist x z ≤ r + r'`.
Then, for any `V` and `W` with type `set (X × X)`, the composition `V ○ W : set (X × X)` is
defined as `{ p : X × X | ∃ z, (p.1, z) ∈ V ∧ (z, p.2) ∈ W }`.
In the metric space case, if `V = { p | dist p.1 p.2 ≤ r }` and `W = { p | dist p.1 p.2 ≤ r' }`
then the triangle inequality, as reformulated above, says `V ○ W` is contained in
`{p | dist p.1 p.2 ≤ r + r'}` which is the entourage associated to the radius `r + r'`.
In general we have `mem_ball_comp (h : y ∈ ball x V) (h' : z ∈ ball y W) : z ∈ ball x (V ○ W)`.
Note that this discussion does not depend on any axiom imposed on the uniformity filter,
it is simply captured by the definition of composition.
The uniform space axioms ask the filter `𝓤 X` to satisfy the following:
* every `V ∈ 𝓤 X` contains the diagonal `id_rel = { p | p.1 = p.2 }`. This abstracts the fact
that `dist x x ≤ r` for every non-negative radius `r` in the metric space case and also that
`x - x` belongs to every neighborhood of zero in the topological group case.
* `V ∈ 𝓤 X → prod.swap '' V ∈ 𝓤 X`. This is tightly related the fact that `dist x y = dist y x`
in a metric space, and to continuity of negation in the topological group case.
* `∀ V ∈ 𝓤 X, ∃ W ∈ 𝓤 X, W ○ W ⊆ V`. In the metric space case, it corresponds
to cutting the radius of a ball in half and applying the triangle inequality.
In the topological group case, it comes from continuity of addition at `(0, 0)`.
These three axioms are stated more abstractly in the definition below, in terms of
operations on filters, without directly manipulating entourages.
## Main definitions
* `uniform_space X` is a uniform space structure on a type `X`
* `uniform_continuous f` is a predicate saying a function `f : α → β` between uniform spaces
is uniformly continuous : `∀ r ∈ 𝓤 β, ∀ᶠ (x : α × α) in 𝓤 α, (f x.1, f x.2) ∈ r`
In this file we also define a complete lattice structure on the type `uniform_space X`
of uniform structures on `X`, as well as the pullback (`uniform_space.comap`) of uniform structures
coming from the pullback of filters.
Like distance functions, uniform structures cannot be pushed forward in general.
## Notations
Localized in `uniformity`, we have the notation `𝓤 X` for the uniformity on a uniform space `X`,
and `○` for composition of relations, seen as terms with type `set (X × X)`.
## Implementation notes
There is already a theory of relations in `data/rel.lean` where the main definition is
`def rel (α β : Type*) := α → β → Prop`.
The relations used in the current file involve only one type, but this is not the reason why
we don't reuse `data/rel.lean`. We use `set (α × α)`
instead of `rel α α` because we really need sets to use the filter library, and elements
of filters on `α × α` have type `set (α × α)`.
The structure `uniform_space X` bundles a uniform structure on `X`, a topology on `X` and
an assumption saying those are compatible. This may not seem mathematically reasonable at first,
but is in fact an instance of the forgetful inheritance pattern. See Note [forgetful inheritance]
below.
## References
The formalization uses the books:
* [N. Bourbaki, *General Topology*][bourbaki1966]
* [I. M. James, *Topologies and Uniformities*][james1999]
But it makes a more systematic use of the filter library.
-/
open set filter classical
open_locale classical topological_space filter
set_option eqn_compiler.zeta true
universes u
/-!
### Relations, seen as `set (α × α)`
-/
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Sort*}
/-- The identity relation, or the graph of the identity function -/
def id_rel {α : Type*} := {p : α × α | p.1 = p.2}
@[simp] theorem mem_id_rel {a b : α} : (a, b) ∈ @id_rel α ↔ a = b := iff.rfl
@[simp] theorem id_rel_subset {s : set (α × α)} : id_rel ⊆ s ↔ ∀ a, (a, a) ∈ s :=
by simp [subset_def]; exact forall_congr (λ a, by simp)
/-- The composition of relations -/
def comp_rel {α : Type u} (r₁ r₂ : set (α×α)) := {p : α × α | ∃z:α, (p.1, z) ∈ r₁ ∧ (z, p.2) ∈ r₂}
localized "infix ` ○ `:55 := comp_rel" in uniformity
@[simp] theorem mem_comp_rel {r₁ r₂ : set (α×α)}
{x y : α} : (x, y) ∈ r₁ ○ r₂ ↔ ∃ z, (x, z) ∈ r₁ ∧ (z, y) ∈ r₂ := iff.rfl
@[simp] theorem swap_id_rel : prod.swap '' id_rel = @id_rel α :=
set.ext $ assume ⟨a, b⟩, by simp [image_swap_eq_preimage_swap]; exact eq_comm
theorem monotone_comp_rel [preorder β] {f g : β → set (α×α)}
(hf : monotone f) (hg : monotone g) : monotone (λx, (f x) ○ (g x)) :=
assume a b h p ⟨z, h₁, h₂⟩, ⟨z, hf h h₁, hg h h₂⟩
@[mono]
lemma comp_rel_mono {f g h k: set (α×α)} (h₁ : f ⊆ h) (h₂ : g ⊆ k) : f ○ g ⊆ h ○ k :=
λ ⟨x, y⟩ ⟨z, h, h'⟩, ⟨z, h₁ h, h₂ h'⟩
lemma prod_mk_mem_comp_rel {a b c : α} {s t : set (α×α)} (h₁ : (a, c) ∈ s) (h₂ : (c, b) ∈ t) :
(a, b) ∈ s ○ t :=
⟨c, h₁, h₂⟩
@[simp] lemma id_comp_rel {r : set (α×α)} : id_rel ○ r = r :=
set.ext $ assume ⟨a, b⟩, by simp
lemma comp_rel_assoc {r s t : set (α×α)} :
(r ○ s) ○ t = r ○ (s ○ t) :=
by ext p; cases p; simp only [mem_comp_rel]; tauto
lemma subset_comp_self {α : Type*} {s : set (α × α)} (h : id_rel ⊆ s) : s ⊆ s ○ s :=
λ ⟨x, y⟩ xy_in, ⟨x, h (by rw mem_id_rel), xy_in⟩
/-- The relation is invariant under swapping factors. -/
def symmetric_rel (V : set (α × α)) : Prop := prod.swap ⁻¹' V = V
/-- The maximal symmetric relation contained in a given relation. -/
def symmetrize_rel (V : set (α × α)) : set (α × α) := V ∩ prod.swap ⁻¹' V
lemma symmetric_symmetrize_rel (V : set (α × α)) : symmetric_rel (symmetrize_rel V) :=
by simp [symmetric_rel, symmetrize_rel, preimage_inter, inter_comm, ← preimage_comp]
lemma symmetrize_rel_subset_self (V : set (α × α)) : symmetrize_rel V ⊆ V :=
sep_subset _ _
@[mono]
lemma symmetrize_mono {V W: set (α × α)} (h : V ⊆ W) : symmetrize_rel V ⊆ symmetrize_rel W :=
inter_subset_inter h $ preimage_mono h
lemma symmetric_rel_inter {U V : set (α × α)} (hU : symmetric_rel U) (hV : symmetric_rel V) :
symmetric_rel (U ∩ V) :=
begin
unfold symmetric_rel at *,
rw [preimage_inter, hU, hV],
end
/-- This core description of a uniform space is outside of the type class hierarchy. It is useful
for constructions of uniform spaces, when the topology is derived from the uniform space. -/
structure uniform_space.core (α : Type u) :=
(uniformity : filter (α × α))
(refl : 𝓟 id_rel ≤ uniformity)
(symm : tendsto prod.swap uniformity uniformity)
(comp : uniformity.lift' (λs, s ○ s) ≤ uniformity)
/-- An alternative constructor for `uniform_space.core`. This version unfolds various
`filter`-related definitions. -/
def uniform_space.core.mk' {α : Type u} (U : filter (α × α))
(refl : ∀ (r ∈ U) x, (x, x) ∈ r)
(symm : ∀ r ∈ U, prod.swap ⁻¹' r ∈ U)
(comp : ∀ r ∈ U, ∃ t ∈ U, t ○ t ⊆ r) : uniform_space.core α :=
⟨U, λ r ru, id_rel_subset.2 (refl _ ru), symm,
begin
intros r ru,
rw [mem_lift'_sets],
exact comp _ ru,
apply monotone_comp_rel; exact monotone_id,
end⟩
/-- A uniform space generates a topological space -/
def uniform_space.core.to_topological_space {α : Type u} (u : uniform_space.core α) :
topological_space α :=
{ is_open := λs, ∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ u.uniformity,
is_open_univ := by simp; intro; exact univ_mem_sets,
is_open_inter :=
assume s t hs ht x ⟨xs, xt⟩, by filter_upwards [hs x xs, ht x xt]; simp {contextual := tt},
is_open_sUnion :=
assume s hs x ⟨t, ts, xt⟩, by filter_upwards [hs t ts x xt] assume p ph h, ⟨t, ts, ph h⟩ }
lemma uniform_space.core_eq :
∀{u₁ u₂ : uniform_space.core α}, u₁.uniformity = u₂.uniformity → u₁ = u₂
| ⟨u₁, _, _, _⟩ ⟨u₂, _, _, _⟩ h := by { congr, exact h }
-- the topological structure is embedded in the uniform structure
-- to avoid instance diamond issues. See Note [forgetful inheritance].
/-- A uniform space is a generalization of the "uniform" topological aspects of a
metric space. It consists of a filter on `α × α` called the "uniformity", which
satisfies properties analogous to the reflexivity, symmetry, and triangle properties
of a metric.
A metric space has a natural uniformity, and a uniform space has a natural topology.
A topological group also has a natural uniformity, even when it is not metrizable. -/
class uniform_space (α : Type u) extends topological_space α, uniform_space.core α :=
(is_open_uniformity : ∀s, is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ uniformity))
/-- Alternative constructor for `uniform_space α` when a topology is already given. -/
@[pattern] def uniform_space.mk' {α} (t : topological_space α)
(c : uniform_space.core α)
(is_open_uniformity : ∀s:set α, t.is_open s ↔
(∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ c.uniformity)) :
uniform_space α := ⟨c, is_open_uniformity⟩
/-- Construct a `uniform_space` from a `uniform_space.core`. -/
def uniform_space.of_core {α : Type u} (u : uniform_space.core α) : uniform_space α :=
{ to_core := u,
to_topological_space := u.to_topological_space,
is_open_uniformity := assume a, iff.rfl }
/-- Construct a `uniform_space` from a `u : uniform_space.core` and a `topological_space` structure
that is equal to `u.to_topological_space`. -/
def uniform_space.of_core_eq {α : Type u} (u : uniform_space.core α) (t : topological_space α)
(h : t = u.to_topological_space) : uniform_space α :=
{ to_core := u,
to_topological_space := t,
is_open_uniformity := assume a, h.symm ▸ iff.rfl }
lemma uniform_space.to_core_to_topological_space (u : uniform_space α) :
u.to_core.to_topological_space = u.to_topological_space :=
topological_space_eq $ funext $ assume s,
by rw [uniform_space.core.to_topological_space, uniform_space.is_open_uniformity]
@[ext]
lemma uniform_space_eq : ∀{u₁ u₂ : uniform_space α}, u₁.uniformity = u₂.uniformity → u₁ = u₂
| (uniform_space.mk' t₁ u₁ o₁) (uniform_space.mk' t₂ u₂ o₂) h :=
have u₁ = u₂, from uniform_space.core_eq h,
have t₁ = t₂, from topological_space_eq $ funext $ assume s, by rw [o₁, o₂]; simp [this],
by simp [*]
lemma uniform_space.of_core_eq_to_core
(u : uniform_space α) (t : topological_space α) (h : t = u.to_core.to_topological_space) :
uniform_space.of_core_eq u.to_core t h = u :=
uniform_space_eq rfl
section uniform_space
variables [uniform_space α]
/-- The uniformity is a filter on α × α (inferred from an ambient uniform space
structure on α). -/
def uniformity (α : Type u) [uniform_space α] : filter (α × α) :=
(@uniform_space.to_core α _).uniformity
localized "notation `𝓤` := uniformity" in uniformity
lemma is_open_uniformity {s : set α} :
is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ 𝓤 α) :=
uniform_space.is_open_uniformity s
lemma refl_le_uniformity : 𝓟 id_rel ≤ 𝓤 α :=
(@uniform_space.to_core α _).refl
lemma refl_mem_uniformity {x : α} {s : set (α × α)} (h : s ∈ 𝓤 α) :
(x, x) ∈ s :=
refl_le_uniformity h rfl
lemma symm_le_uniformity : map (@prod.swap α α) (𝓤 _) ≤ (𝓤 _) :=
(@uniform_space.to_core α _).symm
lemma comp_le_uniformity : (𝓤 α).lift' (λs:set (α×α), s ○ s) ≤ 𝓤 α :=
(@uniform_space.to_core α _).comp
lemma tendsto_swap_uniformity : tendsto (@prod.swap α α) (𝓤 α) (𝓤 α) :=
symm_le_uniformity
lemma comp_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) :
∃ t ∈ 𝓤 α, t ○ t ⊆ s :=
have s ∈ (𝓤 α).lift' (λt:set (α×α), t ○ t),
from comp_le_uniformity hs,
(mem_lift'_sets $ monotone_comp_rel monotone_id monotone_id).mp this
/-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is transitive. -/
lemma filter.tendsto.uniformity_trans {l : filter β} {f₁ f₂ f₃ : β → α}
(h₁₂ : tendsto (λ x, (f₁ x, f₂ x)) l (𝓤 α)) (h₂₃ : tendsto (λ x, (f₂ x, f₃ x)) l (𝓤 α)) :
tendsto (λ x, (f₁ x, f₃ x)) l (𝓤 α) :=
begin
refine le_trans (le_lift' $ λ s hs, mem_map.2 _) comp_le_uniformity,
filter_upwards [h₁₂ hs, h₂₃ hs],
exact λ x hx₁₂ hx₂₃, ⟨_, hx₁₂, hx₂₃⟩
end
/-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is symmetric -/
lemma filter.tendsto.uniformity_symm {l : filter β} {f : β → α × α}
(h : tendsto f l (𝓤 α)) :
tendsto (λ x, ((f x).2, (f x).1)) l (𝓤 α) :=
tendsto_swap_uniformity.comp h
/-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is reflexive. -/
lemma tendsto_diag_uniformity (f : β → α) (l : filter β) :
tendsto (λ x, (f x, f x)) l (𝓤 α) :=
assume s hs, mem_map.2 $ univ_mem_sets' $ λ x, refl_mem_uniformity hs
lemma tendsto_const_uniformity {a : α} {f : filter β} : tendsto (λ _, (a, a)) f (𝓤 α) :=
tendsto_diag_uniformity (λ _, a) f
lemma symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) :
∃ t ∈ 𝓤 α, (∀a b, (a, b) ∈ t → (b, a) ∈ t) ∧ t ⊆ s :=
have preimage prod.swap s ∈ 𝓤 α, from symm_le_uniformity hs,
⟨s ∩ preimage prod.swap s, inter_mem_sets hs this, λ a b ⟨h₁, h₂⟩, ⟨h₂, h₁⟩, inter_subset_left _ _⟩
lemma comp_symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) :
∃ t ∈ 𝓤 α, (∀{a b}, (a, b) ∈ t → (b, a) ∈ t) ∧ t ○ t ⊆ s :=
let ⟨t, ht₁, ht₂⟩ := comp_mem_uniformity_sets hs in
let ⟨t', ht', ht'₁, ht'₂⟩ := symm_of_uniformity ht₁ in
⟨t', ht', ht'₁, subset.trans (monotone_comp_rel monotone_id monotone_id ht'₂) ht₂⟩
lemma uniformity_le_symm : 𝓤 α ≤ (@prod.swap α α) <$> 𝓤 α :=
by rw [map_swap_eq_comap_swap];
from map_le_iff_le_comap.1 tendsto_swap_uniformity
lemma uniformity_eq_symm : 𝓤 α = (@prod.swap α α) <$> 𝓤 α :=
le_antisymm uniformity_le_symm symm_le_uniformity
lemma symmetrize_mem_uniformity {V : set (α × α)} (h : V ∈ 𝓤 α) : symmetrize_rel V ∈ 𝓤 α :=
begin
apply (𝓤 α).inter_sets h,
rw [← image_swap_eq_preimage_swap, uniformity_eq_symm],
exact image_mem_map h,
end
theorem uniformity_lift_le_swap {g : set (α×α) → filter β} {f : filter β} (hg : monotone g)
(h : (𝓤 α).lift (λs, g (preimage prod.swap s)) ≤ f) : (𝓤 α).lift g ≤ f :=
calc (𝓤 α).lift g ≤ (filter.map (@prod.swap α α) $ 𝓤 α).lift g :
lift_mono uniformity_le_symm (le_refl _)
... ≤ _ :
by rw [map_lift_eq2 hg, image_swap_eq_preimage_swap]; exact h
lemma uniformity_lift_le_comp {f : set (α×α) → filter β} (h : monotone f) :
(𝓤 α).lift (λs, f (s ○ s)) ≤ (𝓤 α).lift f :=
calc (𝓤 α).lift (λs, f (s ○ s)) =
((𝓤 α).lift' (λs:set (α×α), s ○ s)).lift f :
begin
rw [lift_lift'_assoc],
exact monotone_comp_rel monotone_id monotone_id,
exact h
end
... ≤ (𝓤 α).lift f : lift_mono comp_le_uniformity (le_refl _)
lemma comp_le_uniformity3 :
(𝓤 α).lift' (λs:set (α×α), s ○ (s ○ s)) ≤ (𝓤 α) :=
calc (𝓤 α).lift' (λd, d ○ (d ○ d)) =
(𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), s ○ (t ○ t))) :
begin
rw [lift_lift'_same_eq_lift'],
exact (assume x, monotone_comp_rel monotone_const $ monotone_comp_rel monotone_id monotone_id),
exact (assume x, monotone_comp_rel monotone_id monotone_const),
end
... ≤ (𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), s ○ t)) :
lift_mono' $ assume s hs, @uniformity_lift_le_comp α _ _ (𝓟 ∘ (○) s) $
monotone_principal.comp (monotone_comp_rel monotone_const monotone_id)
... = (𝓤 α).lift' (λs:set(α×α), s ○ s) :
lift_lift'_same_eq_lift'
(assume s, monotone_comp_rel monotone_const monotone_id)
(assume s, monotone_comp_rel monotone_id monotone_const)
... ≤ (𝓤 α) : comp_le_uniformity
lemma comp_symm_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) :
∃ t ∈ 𝓤 α, symmetric_rel t ∧ t ○ t ⊆ s :=
begin
obtain ⟨w, w_in, w_sub⟩ : ∃ w ∈ 𝓤 α, w ○ w ⊆ s := comp_mem_uniformity_sets hs,
use [symmetrize_rel w, symmetrize_mem_uniformity w_in, symmetric_symmetrize_rel w],
have : symmetrize_rel w ⊆ w := symmetrize_rel_subset_self w,
calc symmetrize_rel w ○ symmetrize_rel w ⊆ w ○ w : by mono
... ⊆ s : w_sub,
end
lemma subset_comp_self_of_mem_uniformity {s : set (α × α)} (h : s ∈ 𝓤 α) : s ⊆ s ○ s :=
subset_comp_self (refl_le_uniformity h)
lemma comp_comp_symm_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) :
∃ t ∈ 𝓤 α, symmetric_rel t ∧ t ○ t ○ t ⊆ s :=
begin
rcases comp_symm_mem_uniformity_sets hs with ⟨w, w_in, w_symm, w_sub⟩,
rcases comp_symm_mem_uniformity_sets w_in with ⟨t, t_in, t_symm, t_sub⟩,
use [t, t_in, t_symm],
have : t ⊆ t ○ t := subset_comp_self_of_mem_uniformity t_in,
calc
t ○ t ○ t ⊆ w ○ t : by mono
... ⊆ w ○ (t ○ t) : by mono
... ⊆ w ○ w : by mono
... ⊆ s : w_sub,
end
/-!
### Balls in uniform spaces
-/
/-- The ball around `(x : β)` with respect to `(V : set (β × β))`. Intended to be
used for `V ∈ 𝓤 β`, but this is not needed for the definition. Recovers the
notions of metric space ball when `V = {p | dist p.1 p.2 < r }`. -/
def uniform_space.ball (x : β) (V : set (β × β)) : set β := (prod.mk x) ⁻¹' V
open uniform_space (ball)
lemma uniform_space.mem_ball_self (x : α) {V : set (α × α)} (hV : V ∈ 𝓤 α) :
x ∈ ball x V :=
refl_mem_uniformity hV
/-- The triangle inequality for `uniform_space.ball` -/
lemma mem_ball_comp {V W : set (β × β)} {x y z} (h : y ∈ ball x V) (h' : z ∈ ball y W) :
z ∈ ball x (V ○ W) :=
prod_mk_mem_comp_rel h h'
lemma ball_subset_of_comp_subset {V W : set (β × β)} {x y} (h : x ∈ ball y W) (h' : W ○ W ⊆ V) :
ball x W ⊆ ball y V :=
λ z z_in, h' (mem_ball_comp h z_in)
lemma ball_mono {V W : set (β × β)} (h : V ⊆ W) (x : β) : ball x V ⊆ ball x W :=
by tauto
lemma ball_inter_left (x : β) (V W : set (β × β)) : ball x (V ∩ W) ⊆ ball x V :=
ball_mono (inter_subset_left V W) x
lemma ball_inter_right (x : β) (V W : set (β × β)) : ball x (V ∩ W) ⊆ ball x W :=
ball_mono (inter_subset_right V W) x
lemma mem_ball_symmetry {V : set (β × β)} (hV : symmetric_rel V) {x y} :
x ∈ ball y V ↔ y ∈ ball x V :=
show (x, y) ∈ prod.swap ⁻¹' V ↔ (x, y) ∈ V, by { unfold symmetric_rel at hV, rw hV }
lemma ball_eq_of_symmetry {V : set (β × β)} (hV : symmetric_rel V) {x} :
ball x V = {y | (y, x) ∈ V} :=
by { ext y, rw mem_ball_symmetry hV, exact iff.rfl }
lemma mem_comp_of_mem_ball {V W : set (β × β)} {x y z : β} (hV : symmetric_rel V)
(hx : x ∈ ball z V) (hy : y ∈ ball z W) : (x, y) ∈ V ○ W :=
begin
rw mem_ball_symmetry hV at hx,
exact ⟨z, hx, hy⟩
end
lemma uniform_space.is_open_ball (x : α) {V : set (α × α)} (hV : is_open V) :
is_open (ball x V) :=
hV.preimage $ continuous_const.prod_mk continuous_id
lemma mem_comp_comp {V W M : set (β × β)} (hW' : symmetric_rel W) {p : β × β} :
p ∈ V ○ M ○ W ↔ ((ball p.1 V).prod (ball p.2 W) ∩ M).nonempty :=
begin
cases p with x y,
split,
{ rintros ⟨z, ⟨w, hpw, hwz⟩, hzy⟩,
exact ⟨(w, z), ⟨hpw, by rwa mem_ball_symmetry hW'⟩, hwz⟩, },
{ rintro ⟨⟨w, z⟩, ⟨w_in, z_in⟩, hwz⟩,
rwa mem_ball_symmetry hW' at z_in,
use [z, w] ; tauto },
end
/-!
### Neighborhoods in uniform spaces
-/
lemma mem_nhds_uniformity_iff_right {x : α} {s : set α} :
s ∈ 𝓝 x ↔ {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α :=
⟨ begin
simp only [mem_nhds_iff, is_open_uniformity, and_imp, exists_imp_distrib],
exact assume t ts ht xt, by filter_upwards [ht x xt] assume ⟨x', y⟩ h eq, ts $ h eq
end,
assume hs,
mem_nhds_iff.mpr ⟨{x | {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α},
assume x' hx', refl_mem_uniformity hx' rfl,
is_open_uniformity.mpr $ assume x' hx',
let ⟨t, ht, tr⟩ := comp_mem_uniformity_sets hx' in
by filter_upwards [ht] assume ⟨a, b⟩ hp' (hax' : a = x'),
by filter_upwards [ht] assume ⟨a, b'⟩ hp'' (hab : a = b),
have hp : (x', b) ∈ t, from hax' ▸ hp',
have (b, b') ∈ t, from hab ▸ hp'',
have (x', b') ∈ t ○ t, from ⟨b, hp, this⟩,
show b' ∈ s,
from tr this rfl,
hs⟩⟩
lemma mem_nhds_uniformity_iff_left {x : α} {s : set α} :
s ∈ 𝓝 x ↔ {p : α × α | p.2 = x → p.1 ∈ s} ∈ 𝓤 α :=
by { rw [uniformity_eq_symm, mem_nhds_uniformity_iff_right], refl }
lemma nhds_eq_comap_uniformity_aux {α : Type u} {x : α} {s : set α} {F : filter (α × α)} :
{p : α × α | p.fst = x → p.snd ∈ s} ∈ F ↔ s ∈ comap (prod.mk x) F :=
by rw mem_comap_sets ; from iff.intro
(assume hs, ⟨_, hs, assume x hx, hx rfl⟩)
(assume ⟨t, h, ht⟩, F.sets_of_superset h $
assume ⟨p₁, p₂⟩ hp (h : p₁ = x), ht $ by simp [h.symm, hp])
lemma nhds_eq_comap_uniformity {x : α} : 𝓝 x = (𝓤 α).comap (prod.mk x) :=
by { ext s, rw [mem_nhds_uniformity_iff_right], exact nhds_eq_comap_uniformity_aux }
lemma is_open_iff_ball_subset {s : set α} : is_open s ↔ ∀ x ∈ s, ∃ V ∈ 𝓤 α, ball x V ⊆ s :=
begin
simp_rw [is_open_iff_mem_nhds, nhds_eq_comap_uniformity],
exact iff.rfl,
end
lemma nhds_basis_uniformity' {p : β → Prop} {s : β → set (α × α)} (h : (𝓤 α).has_basis p s)
{x : α} :
(𝓝 x).has_basis p (λ i, ball x (s i)) :=
by { rw [nhds_eq_comap_uniformity], exact h.comap (prod.mk x) }
lemma nhds_basis_uniformity {p : β → Prop} {s : β → set (α × α)} (h : (𝓤 α).has_basis p s) {x : α} :
(𝓝 x).has_basis p (λ i, {y | (y, x) ∈ s i}) :=
begin
replace h := h.comap prod.swap,
rw [← map_swap_eq_comap_swap, ← uniformity_eq_symm] at h,
exact nhds_basis_uniformity' h
end
lemma uniform_space.mem_nhds_iff {x : α} {s : set α} : s ∈ 𝓝 x ↔ ∃ V ∈ 𝓤 α, ball x V ⊆ s :=
begin
rw [nhds_eq_comap_uniformity, mem_comap_sets],
exact iff.rfl,
end
lemma uniform_space.ball_mem_nhds (x : α) ⦃V : set (α × α)⦄ (V_in : V ∈ 𝓤 α) : ball x V ∈ 𝓝 x :=
begin
rw uniform_space.mem_nhds_iff,
exact ⟨V, V_in, subset.refl _⟩
end
lemma uniform_space.mem_nhds_iff_symm {x : α} {s : set α} :
s ∈ 𝓝 x ↔ ∃ V ∈ 𝓤 α, symmetric_rel V ∧ ball x V ⊆ s :=
begin
rw uniform_space.mem_nhds_iff,
split,
{ rintros ⟨V, V_in, V_sub⟩,
use [symmetrize_rel V, symmetrize_mem_uniformity V_in, symmetric_symmetrize_rel V],
exact subset.trans (ball_mono (symmetrize_rel_subset_self V) x) V_sub },
{ rintros ⟨V, V_in, V_symm, V_sub⟩,
exact ⟨V, V_in, V_sub⟩ }
end
lemma uniform_space.has_basis_nhds (x : α) :
has_basis (𝓝 x) (λ s : set (α × α), s ∈ 𝓤 α ∧ symmetric_rel s) (λ s, ball x s) :=
⟨λ t, by simp [uniform_space.mem_nhds_iff_symm, and_assoc]⟩
open uniform_space
lemma uniform_space.mem_closure_iff_symm_ball {s : set α} {x} :
x ∈ closure s ↔ ∀ {V}, V ∈ 𝓤 α → symmetric_rel V → (s ∩ ball x V).nonempty :=
begin
simp [mem_closure_iff_nhds_basis (has_basis_nhds x)],
tauto,
end
lemma uniform_space.mem_closure_iff_ball {s : set α} {x} :
x ∈ closure s ↔ ∀ {V}, V ∈ 𝓤 α → (ball x V ∩ s).nonempty :=
begin
simp_rw [mem_closure_iff_nhds, uniform_space.mem_nhds_iff],
split,
{ intros h V V_in,
exact h (ball x V) ⟨V, V_in, subset.refl _⟩ },
{ rintros h t ⟨V, V_in, Vt⟩,
exact nonempty.mono (inter_subset_inter_left s Vt) (h V_in) },
end
lemma uniform_space.has_basis_nhds_prod (x y : α) :
has_basis (𝓝 (x, y)) (λ s, s ∈ 𝓤 α ∧ symmetric_rel s) $ λ s, (ball x s).prod (ball y s) :=
begin
rw nhds_prod_eq,
apply (has_basis_nhds x).prod' (has_basis_nhds y),
rintro U V ⟨U_in, U_symm⟩ ⟨V_in, V_symm⟩,
exact ⟨U ∩ V, ⟨(𝓤 α).inter_sets U_in V_in, symmetric_rel_inter U_symm V_symm⟩,
ball_inter_left x U V, ball_inter_right y U V⟩,
end
lemma nhds_eq_uniformity {x : α} : 𝓝 x = (𝓤 α).lift' (ball x) :=
(nhds_basis_uniformity' (𝓤 α).basis_sets).eq_binfi
lemma mem_nhds_left (x : α) {s : set (α×α)} (h : s ∈ 𝓤 α) :
{y : α | (x, y) ∈ s} ∈ 𝓝 x :=
ball_mem_nhds x h
lemma mem_nhds_right (y : α) {s : set (α×α)} (h : s ∈ 𝓤 α) :
{x : α | (x, y) ∈ s} ∈ 𝓝 y :=
mem_nhds_left _ (symm_le_uniformity h)
lemma tendsto_right_nhds_uniformity {a : α} : tendsto (λa', (a', a)) (𝓝 a) (𝓤 α) :=
assume s, mem_nhds_right a
lemma tendsto_left_nhds_uniformity {a : α} : tendsto (λa', (a, a')) (𝓝 a) (𝓤 α) :=
assume s, mem_nhds_left a
lemma lift_nhds_left {x : α} {g : set α → filter β} (hg : monotone g) :
(𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) :=
eq.trans
begin
rw [nhds_eq_uniformity],
exact (filter.lift_assoc $ monotone_principal.comp $ monotone_preimage.comp monotone_preimage )
end
(congr_arg _ $ funext $ assume s, filter.lift_principal hg)
lemma lift_nhds_right {x : α} {g : set α → filter β} (hg : monotone g) :
(𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (y, x) ∈ s}) :=
calc (𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : lift_nhds_left hg
... = ((@prod.swap α α) <$> (𝓤 α)).lift (λs:set (α×α), g {y | (x, y) ∈ s}) :
by rw [←uniformity_eq_symm]
... = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ image prod.swap s}) :
map_lift_eq2 $ hg.comp monotone_preimage
... = _ : by simp [image_swap_eq_preimage_swap]
lemma nhds_nhds_eq_uniformity_uniformity_prod {a b : α} :
𝓝 a ×ᶠ 𝓝 b =
(𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set (α×α),
set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ t})) :=
begin
rw [prod_def],
show (𝓝 a).lift (λs:set α, (𝓝 b).lift (λt:set α, 𝓟 (set.prod s t))) = _,
rw [lift_nhds_right],
apply congr_arg, funext s,
rw [lift_nhds_left],
refl,
exact monotone_principal.comp (monotone_prod monotone_const monotone_id),
exact (monotone_lift' monotone_const $ monotone_lam $
assume x, monotone_prod monotone_id monotone_const)
end
lemma nhds_eq_uniformity_prod {a b : α} :
𝓝 (a, b) =
(𝓤 α).lift' (λs:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ s}) :=
begin
rw [nhds_prod_eq, nhds_nhds_eq_uniformity_uniformity_prod, lift_lift'_same_eq_lift'],
{ intro s, exact monotone_prod monotone_const monotone_preimage },
{ intro t, exact monotone_prod monotone_preimage monotone_const }
end
lemma nhdset_of_mem_uniformity {d : set (α×α)} (s : set (α×α)) (hd : d ∈ 𝓤 α) :
∃(t : set (α×α)), is_open t ∧ s ⊆ t ∧ t ⊆ {p | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} :=
let cl_d := {p:α×α | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} in
have ∀p ∈ s, ∃t ⊆ cl_d, is_open t ∧ p ∈ t, from
assume ⟨x, y⟩ hp, _root_.mem_nhds_iff.mp $
show cl_d ∈ 𝓝 (x, y),
begin
rw [nhds_eq_uniformity_prod, mem_lift'_sets],
exact ⟨d, hd, assume ⟨a, b⟩ ⟨ha, hb⟩, ⟨x, y, ha, hp, hb⟩⟩,
exact monotone_prod monotone_preimage monotone_preimage
end,
have ∃t:(Π(p:α×α) (h:p ∈ s), set (α×α)),
∀p, ∀h:p ∈ s, t p h ⊆ cl_d ∧ is_open (t p h) ∧ p ∈ t p h,
by simp [classical.skolem] at this; simp; assumption,
match this with
| ⟨t, ht⟩ :=
⟨(⋃ p:α×α, ⋃ h : p ∈ s, t p h : set (α×α)),
is_open_Union $ assume (p:α×α), is_open_Union $ assume hp, (ht p hp).right.left,
assume ⟨a, b⟩ hp, begin simp; exact ⟨a, b, hp, (ht (a,b) hp).right.right⟩ end,
Union_subset $ assume p, Union_subset $ assume hp, (ht p hp).left⟩
end
/-- Entourages are neighborhoods of the diagonal. -/
lemma nhds_le_uniformity (x : α) : 𝓝 (x, x) ≤ 𝓤 α :=
begin
intros V V_in,
rcases comp_symm_mem_uniformity_sets V_in with ⟨w, w_in, w_symm, w_sub⟩,
have : (ball x w).prod (ball x w) ∈ 𝓝 (x, x),
{ rw nhds_prod_eq,
exact prod_mem_prod (ball_mem_nhds x w_in) (ball_mem_nhds x w_in) },
apply mem_sets_of_superset this,
rintros ⟨u, v⟩ ⟨u_in, v_in⟩,
exact w_sub (mem_comp_of_mem_ball w_symm u_in v_in)
end
/-- Entourages are neighborhoods of the diagonal. -/
lemma supr_nhds_le_uniformity : (⨆ x : α, 𝓝 (x, x)) ≤ 𝓤 α :=
supr_le nhds_le_uniformity
/-!
### Closure and interior in uniform spaces
-/
lemma closure_eq_uniformity (s : set $ α × α) :
closure s = ⋂ V ∈ {V | V ∈ 𝓤 α ∧ symmetric_rel V}, V ○ s ○ V :=
begin
ext ⟨x, y⟩,
simp_rw [mem_closure_iff_nhds_basis (uniform_space.has_basis_nhds_prod x y),
mem_Inter, mem_set_of_eq],
apply forall_congr,
intro V,
apply forall_congr,
rintros ⟨V_in, V_symm⟩,
simp_rw [mem_comp_comp V_symm, inter_comm, exists_prop],
exact iff.rfl,
end
lemma uniformity_has_basis_closed : has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α ∧ is_closed V) id :=
begin
refine filter.has_basis_self.2 (λ t h, _),
rcases comp_comp_symm_mem_uniformity_sets h with ⟨w, w_in, w_symm, r⟩,
refine ⟨closure w, mem_sets_of_superset w_in subset_closure, is_closed_closure, _⟩,
refine subset.trans _ r,
rw closure_eq_uniformity,
apply Inter_subset_of_subset,
apply Inter_subset,
exact ⟨w_in, w_symm⟩
end
/-- Closed entourages form a basis of the uniformity filter. -/
lemma uniformity_has_basis_closure : has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α) closure :=
⟨begin
intro t,
rw uniformity_has_basis_closed.mem_iff,
split,
{ rintros ⟨r, ⟨r_in, r_closed⟩, r_sub⟩,
use [r, r_in],
convert r_sub,
rw r_closed.closure_eq,
refl },
{ rintros ⟨r, r_in, r_sub⟩,
exact ⟨closure r, ⟨mem_sets_of_superset r_in subset_closure, is_closed_closure⟩, r_sub⟩ }
end⟩
lemma closure_eq_inter_uniformity {t : set (α×α)} :
closure t = (⋂ d ∈ 𝓤 α, d ○ (t ○ d)) :=
set.ext $ assume ⟨a, b⟩,
calc (a, b) ∈ closure t ↔ (𝓝 (a, b) ⊓ 𝓟 t ≠ ⊥) : mem_closure_iff_nhds_ne_bot
... ↔ (((@prod.swap α α) <$> 𝓤 α).lift'
(λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ 𝓟 t ≠ ⊥) :
by rw [←uniformity_eq_symm, nhds_eq_uniformity_prod]
... ↔ ((map (@prod.swap α α) (𝓤 α)).lift'
(λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ 𝓟 t ≠ ⊥) :
by refl
... ↔ ((𝓤 α).lift'
(λ (s : set (α × α)), set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s}) ⊓ 𝓟 t ≠ ⊥) :
begin
rw [map_lift'_eq2],
simp [image_swap_eq_preimage_swap, function.comp],
exact monotone_prod monotone_preimage monotone_preimage
end
... ↔ (∀s ∈ 𝓤 α, (set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s} ∩ t).nonempty) :
begin
rw [lift'_inf_principal_eq, ← ne_bot_iff, lift'_ne_bot_iff],
exact monotone_inter (monotone_prod monotone_preimage monotone_preimage) monotone_const
end
... ↔ (∀ s ∈ 𝓤 α, (a, b) ∈ s ○ (t ○ s)) :
forall_congr $ assume s, forall_congr $ assume hs,
⟨assume ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩, ⟨x, hx, y, hxyt, hy⟩,
assume ⟨x, hx, y, hxyt, hy⟩, ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩⟩
... ↔ _ : by simp
lemma uniformity_eq_uniformity_closure : 𝓤 α = (𝓤 α).lift' closure :=
le_antisymm
(le_infi $ assume s, le_infi $ assume hs, by simp; filter_upwards [hs] subset_closure)
(calc (𝓤 α).lift' closure ≤ (𝓤 α).lift' (λd, d ○ (d ○ d)) :
lift'_mono' (by intros s hs; rw [closure_eq_inter_uniformity]; exact bInter_subset_of_mem hs)
... ≤ (𝓤 α) : comp_le_uniformity3)
lemma uniformity_eq_uniformity_interior : 𝓤 α = (𝓤 α).lift' interior :=
le_antisymm
(le_infi $ assume d, le_infi $ assume hd,
let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $
monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp
(comp_le_uniformity3 hd) in
let ⟨t, ht, hst, ht_comp⟩ := nhdset_of_mem_uniformity s hs in
have s ⊆ interior d, from
calc s ⊆ t : hst
... ⊆ interior d : (subset_interior_iff_subset_of_open ht).mpr $
λ x (hx : x ∈ t), let ⟨x, y, h₁, h₂, h₃⟩ := ht_comp hx in hs_comp ⟨x, h₁, y, h₂, h₃⟩,
have interior d ∈ 𝓤 α, by filter_upwards [hs] this,
by simp [this])
(assume s hs, ((𝓤 α).lift' interior).sets_of_superset (mem_lift' hs) interior_subset)
lemma interior_mem_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) :
interior s ∈ 𝓤 α :=
by rw [uniformity_eq_uniformity_interior]; exact mem_lift' hs
lemma mem_uniformity_is_closed {s : set (α×α)} (h : s ∈ 𝓤 α) :
∃t ∈ 𝓤 α, is_closed t ∧ t ⊆ s :=
let ⟨t, ⟨ht_mem, htc⟩, hts⟩ := uniformity_has_basis_closed.mem_iff.1 h in
⟨t, ht_mem, htc, hts⟩
/-- The uniform neighborhoods of all points of a dense set cover the whole space. -/
lemma dense.bUnion_uniformity_ball {s : set α} {U : set (α × α)} (hs : dense s) (hU : U ∈ 𝓤 α) :
(⋃ x ∈ s, ball x U) = univ :=
begin
refine bUnion_eq_univ_iff.2 (λ y, _),
rcases hs.inter_nhds_nonempty (mem_nhds_right y hU) with ⟨x, hxs, hxy : (x, y) ∈ U⟩,
exact ⟨x, hxs, hxy⟩
end
/-!
### Uniformity bases
-/
/-- Open elements of `𝓤 α` form a basis of `𝓤 α`. -/
lemma uniformity_has_basis_open : has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α ∧ is_open V) id :=
has_basis_self.2 $ λ s hs,
⟨interior s, interior_mem_uniformity hs, is_open_interior, interior_subset⟩
lemma filter.has_basis.mem_uniformity_iff {p : β → Prop} {s : β → set (α×α)}
(h : (𝓤 α).has_basis p s) {t : set (α × α)} :
t ∈ 𝓤 α ↔ ∃ i (hi : p i), ∀ a b, (a, b) ∈ s i → (a, b) ∈ t :=
h.mem_iff.trans $ by simp only [prod.forall, subset_def]
/-- Symmetric entourages form a basis of `𝓤 α` -/
lemma uniform_space.has_basis_symmetric :
(𝓤 α).has_basis (λ s : set (α × α), s ∈ 𝓤 α ∧ symmetric_rel s) id :=
has_basis_self.2 $ λ t t_in, ⟨symmetrize_rel t, symmetrize_mem_uniformity t_in,
symmetric_symmetrize_rel t, symmetrize_rel_subset_self t⟩
/-- Open elements `s : set (α × α)` of `𝓤 α` such that `(x, y) ∈ s ↔ (y, x) ∈ s` form a basis
of `𝓤 α`. -/
lemma uniformity_has_basis_open_symmetric :
has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α ∧ is_open V ∧ symmetric_rel V) id :=
begin
simp only [← and_assoc],
refine uniformity_has_basis_open.restrict (λ s hs, ⟨symmetrize_rel s, _⟩),
exact ⟨⟨symmetrize_mem_uniformity hs.1, is_open.inter hs.2 (hs.2.preimage continuous_swap)⟩,
symmetric_symmetrize_rel s, symmetrize_rel_subset_self s⟩
end
lemma uniform_space.has_seq_basis (h : is_countably_generated $ 𝓤 α) :
∃ V : ℕ → set (α × α), has_antimono_basis (𝓤 α) (λ _, true) V ∧ ∀ n, symmetric_rel (V n) :=
let ⟨U, hsym, hbasis⟩ := h.exists_antimono_subbasis uniform_space.has_basis_symmetric
in ⟨U, hbasis, λ n, (hsym n).2⟩
/-! ### Uniform continuity -/
/-- A function `f : α → β` is *uniformly continuous* if `(f x, f y)` tends to the diagonal
as `(x, y)` tends to the diagonal. In other words, if `x` is sufficiently close to `y`, then
`f x` is close to `f y` no matter where `x` and `y` are located in `α`. -/
def uniform_continuous [uniform_space β] (f : α → β) :=
tendsto (λx:α×α, (f x.1, f x.2)) (𝓤 α) (𝓤 β)
/-- A function `f : α → β` is *uniformly continuous* on `s : set α` if `(f x, f y)` tends to
the diagonal as `(x, y)` tends to the diagonal while remaining in `s.prod s`.
In other words, if `x` is sufficiently close to `y`, then `f x` is close to
`f y` no matter where `x` and `y` are located in `s`.-/
def uniform_continuous_on [uniform_space β] (f : α → β) (s : set α) : Prop :=
tendsto (λ x : α × α, (f x.1, f x.2)) (𝓤 α ⊓ principal (s.prod s)) (𝓤 β)
theorem uniform_continuous_def [uniform_space β] {f : α → β} :
uniform_continuous f ↔ ∀ r ∈ 𝓤 β, { x : α × α | (f x.1, f x.2) ∈ r} ∈ 𝓤 α :=
iff.rfl
theorem uniform_continuous_iff_eventually [uniform_space β] {f : α → β} :
uniform_continuous f ↔ ∀ r ∈ 𝓤 β, ∀ᶠ (x : α × α) in 𝓤 α, (f x.1, f x.2) ∈ r :=
iff.rfl
lemma uniform_continuous_of_const [uniform_space β] {c : α → β} (h : ∀a b, c a = c b) :
uniform_continuous c :=
have (λ (x : α × α), (c (x.fst), c (x.snd))) ⁻¹' id_rel = univ, from
eq_univ_iff_forall.2 $ assume ⟨a, b⟩, h a b,
le_trans (map_le_iff_le_comap.2 $ by simp [comap_principal, this, univ_mem_sets]) refl_le_uniformity
lemma uniform_continuous_id : uniform_continuous (@id α) :=
by simp [uniform_continuous]; exact tendsto_id
lemma uniform_continuous_const [uniform_space β] {b : β} : uniform_continuous (λa:α, b) :=
uniform_continuous_of_const $ λ _ _, rfl
lemma uniform_continuous.comp [uniform_space β] [uniform_space γ] {g : β → γ} {f : α → β}
(hg : uniform_continuous g) (hf : uniform_continuous f) : uniform_continuous (g ∘ f) :=
hg.comp hf
lemma filter.has_basis.uniform_continuous_iff [uniform_space β] {p : γ → Prop} {s : γ → set (α×α)}
(ha : (𝓤 α).has_basis p s) {q : δ → Prop} {t : δ → set (β×β)} (hb : (𝓤 β).has_basis q t)
{f : α → β} :
uniform_continuous f ↔ ∀ i (hi : q i), ∃ j (hj : p j), ∀ x y, (x, y) ∈ s j → (f x, f y) ∈ t i :=
(ha.tendsto_iff hb).trans $ by simp only [prod.forall]
lemma filter.has_basis.uniform_continuous_on_iff [uniform_space β] {p : γ → Prop}
{s : γ → set (α×α)} (ha : (𝓤 α).has_basis p s) {q : δ → Prop} {t : δ → set (β×β)}
(hb : (𝓤 β).has_basis q t) {f : α → β} {S : set α} :
uniform_continuous_on f S ↔
∀ i (hi : q i), ∃ j (hj : p j), ∀ x y ∈ S, (x, y) ∈ s j → (f x, f y) ∈ t i :=
((ha.inf_principal (S.prod S)).tendsto_iff hb).trans $ by finish [prod.forall]
end uniform_space
open_locale uniformity
section constructions
instance : partial_order (uniform_space α) :=
{ le := λt s, t.uniformity ≤ s.uniformity,
le_antisymm := assume t s h₁ h₂, uniform_space_eq $ le_antisymm h₁ h₂,
le_refl := assume t, le_refl _,
le_trans := assume a b c h₁ h₂, le_trans h₁ h₂ }
instance : has_Inf (uniform_space α) :=
⟨assume s, uniform_space.of_core {
uniformity := (⨅u∈s, @uniformity α u),
refl := le_infi $ assume u, le_infi $ assume hu, u.refl,
symm := le_infi $ assume u, le_infi $ assume hu,
le_trans (map_mono $ infi_le_of_le _ $ infi_le _ hu) u.symm,
comp := le_infi $ assume u, le_infi $ assume hu,
le_trans (lift'_mono (infi_le_of_le _ $ infi_le _ hu) $ le_refl _) u.comp }⟩
private lemma Inf_le {tt : set (uniform_space α)} {t : uniform_space α} (h : t ∈ tt) :
Inf tt ≤ t :=
show (⨅u∈tt, @uniformity α u) ≤ t.uniformity,
from infi_le_of_le t $ infi_le _ h
private lemma le_Inf {tt : set (uniform_space α)} {t : uniform_space α} (h : ∀t'∈tt, t ≤ t') :
t ≤ Inf tt :=
show t.uniformity ≤ (⨅u∈tt, @uniformity α u),
from le_infi $ assume t', le_infi $ assume ht', h t' ht'
instance : has_top (uniform_space α) :=
⟨uniform_space.of_core { uniformity := ⊤, refl := le_top, symm := le_top, comp := le_top }⟩
instance : has_bot (uniform_space α) :=
⟨{ to_topological_space := ⊥,
uniformity := 𝓟 id_rel,
refl := le_refl _,
symm := by simp [tendsto]; apply subset.refl,
comp :=
begin
rw [lift'_principal], {simp},
exact monotone_comp_rel monotone_id monotone_id
end,
is_open_uniformity :=
assume s, by simp [is_open_fold, subset_def, id_rel] {contextual := tt } } ⟩
instance : complete_lattice (uniform_space α) :=
{ sup := λa b, Inf {x | a ≤ x ∧ b ≤ x},
le_sup_left := λ a b, le_Inf (λ _ ⟨h, _⟩, h),
le_sup_right := λ a b, le_Inf (λ _ ⟨_, h⟩, h),
sup_le := λ a b c h₁ h₂, Inf_le ⟨h₁, h₂⟩,
inf := λ a b, Inf {a, b},
le_inf := λ a b c h₁ h₂, le_Inf (λ u h,
by { cases h, exact h.symm ▸ h₁, exact (mem_singleton_iff.1 h).symm ▸ h₂ }),
inf_le_left := λ a b, Inf_le (by simp),
inf_le_right := λ a b, Inf_le (by simp),
top := ⊤,
le_top := λ a, show a.uniformity ≤ ⊤, from le_top,
bot := ⊥,
bot_le := λ u, u.refl,
Sup := λ tt, Inf {t | ∀ t' ∈ tt, t' ≤ t},
le_Sup := λ s u h, le_Inf (λ u' h', h' u h),
Sup_le := λ s u h, Inf_le h,
Inf := Inf,
le_Inf := λ s a hs, le_Inf hs,
Inf_le := λ s a ha, Inf_le ha,
..uniform_space.partial_order }
lemma infi_uniformity {ι : Sort*} {u : ι → uniform_space α} :
(infi u).uniformity = (⨅i, (u i).uniformity) :=
show (⨅a (h : ∃i:ι, u i = a), a.uniformity) = _, from
le_antisymm
(le_infi $ assume i, infi_le_of_le (u i) $ infi_le _ ⟨i, rfl⟩)
(le_infi $ assume a, le_infi $ assume ⟨i, (ha : u i = a)⟩, ha ▸ infi_le _ _)
lemma inf_uniformity {u v : uniform_space α} :
(u ⊓ v).uniformity = u.uniformity ⊓ v.uniformity :=
have (u ⊓ v) = (⨅i (h : i = u ∨ i = v), i), by simp [infi_or, infi_inf_eq],
calc (u ⊓ v).uniformity = ((⨅i (h : i = u ∨ i = v), i) : uniform_space α).uniformity : by rw [this]
... = _ : by simp [infi_uniformity, infi_or, infi_inf_eq]
instance inhabited_uniform_space : inhabited (uniform_space α) := ⟨⊥⟩
instance inhabited_uniform_space_core : inhabited (uniform_space.core α) :=
⟨@uniform_space.to_core _ (default _)⟩
/-- Given `f : α → β` and a uniformity `u` on `β`, the inverse image of `u` under `f`
is the inverse image in the filter sense of the induced function `α × α → β × β`. -/
def uniform_space.comap (f : α → β) (u : uniform_space β) : uniform_space α :=
{ uniformity := u.uniformity.comap (λp:α×α, (f p.1, f p.2)),
to_topological_space := u.to_topological_space.induced f,
refl := le_trans (by simp; exact assume ⟨a, b⟩ (h : a = b), h ▸ rfl) (comap_mono u.refl),
symm := by simp [tendsto_comap_iff, prod.swap, (∘)];
exact tendsto_swap_uniformity.comp tendsto_comap,
comp := le_trans
begin
rw [comap_lift'_eq, comap_lift'_eq2],
exact (lift'_mono' $ assume s hs ⟨a₁, a₂⟩ ⟨x, h₁, h₂⟩, ⟨f x, h₁, h₂⟩),
repeat { exact monotone_comp_rel monotone_id monotone_id }
end
(comap_mono u.comp),
is_open_uniformity := λ s, begin
change (@is_open α (u.to_topological_space.induced f) s ↔ _),
simp [is_open_iff_nhds, nhds_induced, mem_nhds_uniformity_iff_right, filter.comap, and_comm],
refine ball_congr (λ x hx, ⟨_, _⟩),
{ rintro ⟨t, hts, ht⟩, refine ⟨_, ht, _⟩,
rintro ⟨x₁, x₂⟩ h rfl, exact hts (h rfl) },
{ rintro ⟨t, ht, hts⟩,
exact ⟨{y | (f x, y) ∈ t}, λ y hy, @hts (x, y) hy rfl,
mem_nhds_uniformity_iff_right.1 $ mem_nhds_left _ ht⟩ }
end }
lemma uniformity_comap [uniform_space α] [uniform_space β] {f : α → β}
(h : ‹uniform_space α› = uniform_space.comap f ‹uniform_space β›) :
𝓤 α = comap (prod.map f f) (𝓤 β) :=
by { rw h, refl }
lemma uniform_space_comap_id {α : Type*} : uniform_space.comap (id : α → α) = id :=
by ext u ; dsimp [uniform_space.comap] ; rw [prod.id_prod, filter.comap_id]
lemma uniform_space.comap_comap {α β γ} [uγ : uniform_space γ] {f : α → β} {g : β → γ} :
uniform_space.comap (g ∘ f) uγ = uniform_space.comap f (uniform_space.comap g uγ) :=
by ext ; dsimp [uniform_space.comap] ; rw filter.comap_comap
lemma uniform_continuous_iff {α β} [uα : uniform_space α] [uβ : uniform_space β] {f : α → β} :
uniform_continuous f ↔ uα ≤ uβ.comap f :=
filter.map_le_iff_le_comap
lemma uniform_continuous_comap {f : α → β} [u : uniform_space β] :
@uniform_continuous α β (uniform_space.comap f u) u f :=
tendsto_comap
theorem to_topological_space_comap {f : α → β} {u : uniform_space β} :
@uniform_space.to_topological_space _ (uniform_space.comap f u) =
topological_space.induced f (@uniform_space.to_topological_space β u) := rfl
lemma uniform_continuous_comap' {f : γ → β} {g : α → γ} [v : uniform_space β] [u : uniform_space α]
(h : uniform_continuous (f ∘ g)) : @uniform_continuous α γ u (uniform_space.comap f v) g :=
tendsto_comap_iff.2 h
lemma to_nhds_mono {u₁ u₂ : uniform_space α} (h : u₁ ≤ u₂) (a : α) :
@nhds _ (@uniform_space.to_topological_space _ u₁) a ≤
@nhds _ (@uniform_space.to_topological_space _ u₂) a :=
by rw [@nhds_eq_uniformity α u₁ a, @nhds_eq_uniformity α u₂ a]; exact (lift'_mono h le_rfl)
lemma to_topological_space_mono {u₁ u₂ : uniform_space α} (h : u₁ ≤ u₂) :
@uniform_space.to_topological_space _ u₁ ≤ @uniform_space.to_topological_space _ u₂ :=
le_of_nhds_le_nhds $ to_nhds_mono h
lemma uniform_continuous.continuous [uniform_space α] [uniform_space β] {f : α → β}
(hf : uniform_continuous f) : continuous f :=
continuous_iff_le_induced.mpr $ to_topological_space_mono $ uniform_continuous_iff.1 hf
lemma to_topological_space_bot : @uniform_space.to_topological_space α ⊥ = ⊥ := rfl
lemma to_topological_space_top : @uniform_space.to_topological_space α ⊤ = ⊤ :=
top_unique $ assume s hs, s.eq_empty_or_nonempty.elim
(assume : s = ∅, this.symm ▸ @is_open_empty _ ⊤)
(assume ⟨x, hx⟩,
have s = univ, from top_unique $ assume y hy, hs x hx (x, y) rfl,
this.symm ▸ @is_open_univ _ ⊤)
lemma to_topological_space_infi {ι : Sort*} {u : ι → uniform_space α} :
(infi u).to_topological_space = ⨅i, (u i).to_topological_space :=
begin
by_cases h : nonempty ι,
{ resetI,
refine (eq_of_nhds_eq_nhds $ assume a, _),
rw [nhds_infi, nhds_eq_uniformity],
change (infi u).uniformity.lift' (preimage $ prod.mk a) = _,
rw [infi_uniformity, lift'_infi],
{ simp only [nhds_eq_uniformity], refl },
{ exact assume a b, rfl } },
{ rw [infi_of_empty h, infi_of_empty h, to_topological_space_top] }
end
lemma to_topological_space_Inf {s : set (uniform_space α)} :
(Inf s).to_topological_space = (⨅i∈s, @uniform_space.to_topological_space α i) :=
begin
rw [Inf_eq_infi],
simp only [← to_topological_space_infi],
end
lemma to_topological_space_inf {u v : uniform_space α} :
(u ⊓ v).to_topological_space = u.to_topological_space ⊓ v.to_topological_space :=
by rw [to_topological_space_Inf, infi_pair]
instance : uniform_space empty := ⊥
instance : uniform_space punit := ⊥
instance : uniform_space bool := ⊥
instance : uniform_space ℕ := ⊥
instance : uniform_space ℤ := ⊥
instance {p : α → Prop} [t : uniform_space α] : uniform_space (subtype p) :=
uniform_space.comap subtype.val t
lemma uniformity_subtype {p : α → Prop} [t : uniform_space α] :
𝓤 (subtype p) = comap (λq:subtype p × subtype p, (q.1.1, q.2.1)) (𝓤 α) :=
rfl
lemma uniform_continuous_subtype_val {p : α → Prop} [uniform_space α] :
uniform_continuous (subtype.val : {a : α // p a} → α) :=
uniform_continuous_comap
lemma uniform_continuous_subtype_mk {p : α → Prop} [uniform_space α] [uniform_space β]
{f : β → α} (hf : uniform_continuous f) (h : ∀x, p (f x)) :
uniform_continuous (λx, ⟨f x, h x⟩ : β → subtype p) :=
uniform_continuous_comap' hf
lemma uniform_continuous_on_iff_restrict [uniform_space α] [uniform_space β] {f : α → β}
{s : set α} :
uniform_continuous_on f s ↔ uniform_continuous (s.restrict f) :=
begin
unfold uniform_continuous_on set.restrict uniform_continuous tendsto,
rw [show (λ x : s × s, (f x.1, f x.2)) = prod.map f f ∘ coe, by ext x; cases x; refl,
uniformity_comap rfl,
show prod.map subtype.val subtype.val = (coe : s × s → α × α), by ext x; cases x; refl],
conv in (map _ (comap _ _)) { rw ← filter.map_map },
rw subtype_coe_map_comap_prod, refl,
end
lemma tendsto_of_uniform_continuous_subtype
[uniform_space α] [uniform_space β] {f : α → β} {s : set α} {a : α}
(hf : uniform_continuous (λx:s, f x.val)) (ha : s ∈ 𝓝 a) :
tendsto f (𝓝 a) (𝓝 (f a)) :=
by rw [(@map_nhds_subtype_coe_eq α _ s a (mem_of_mem_nhds ha) ha).symm]; exact
tendsto_map' (continuous_iff_continuous_at.mp hf.continuous _)
lemma uniform_continuous_on.continuous_on [uniform_space α] [uniform_space β] {f : α → β}
{s : set α} (h : uniform_continuous_on f s) : continuous_on f s :=
begin
rw uniform_continuous_on_iff_restrict at h,
rw continuous_on_iff_continuous_restrict,
exact h.continuous
end
section prod
/- a similar product space is possible on the function space (uniformity of pointwise convergence),
but we want to have the uniformity of uniform convergence on function spaces -/
instance [u₁ : uniform_space α] [u₂ : uniform_space β] : uniform_space (α × β) :=
uniform_space.of_core_eq
(u₁.comap prod.fst ⊓ u₂.comap prod.snd).to_core
prod.topological_space
(calc prod.topological_space = (u₁.comap prod.fst ⊓ u₂.comap prod.snd).to_topological_space :
by rw [to_topological_space_inf, to_topological_space_comap, to_topological_space_comap]; refl
... = _ : by rw [uniform_space.to_core_to_topological_space])
theorem uniformity_prod [uniform_space α] [uniform_space β] : 𝓤 (α × β) =
(𝓤 α).comap (λp:(α × β) × α × β, (p.1.1, p.2.1)) ⊓
(𝓤 β).comap (λp:(α × β) × α × β, (p.1.2, p.2.2)) :=
inf_uniformity
lemma uniformity_prod_eq_prod [uniform_space α] [uniform_space β] :
𝓤 (α×β) =
map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) (𝓤 α ×ᶠ 𝓤 β) :=
have map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) =
comap (λp:(α×β)×(α×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))),
from funext $ assume f, map_eq_comap_of_inverse
(funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl) (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl),
by rw [this, uniformity_prod, filter.prod, comap_inf, comap_comap, comap_comap]
lemma mem_map_sets_iff' {α : Type*} {β : Type*} {f : filter α} {m : α → β} {t : set β} :
t ∈ (map m f).sets ↔ (∃s∈f, m '' s ⊆ t) :=
mem_map_sets_iff
lemma mem_uniformity_of_uniform_continuous_invariant [uniform_space α] {s:set (α×α)} {f : α → α → α}
(hf : uniform_continuous (λp:α×α, f p.1 p.2)) (hs : s ∈ 𝓤 α) :
∃u∈𝓤 α, ∀a b c, (a, b) ∈ u → (f a c, f b c) ∈ s :=
begin
rw [uniform_continuous, uniformity_prod_eq_prod, tendsto_map'_iff, (∘)] at hf,
rcases mem_map_sets_iff'.1 (hf hs) with ⟨t, ht, hts⟩, clear hf,
rcases mem_prod_iff.1 ht with ⟨u, hu, v, hv, huvt⟩, clear ht,
refine ⟨u, hu, assume a b c hab, hts $ (mem_image _ _ _).2 ⟨⟨⟨a, b⟩, ⟨c, c⟩⟩, huvt ⟨_, _⟩, _⟩⟩,
exact hab,
exact refl_mem_uniformity hv,
refl
end
lemma mem_uniform_prod [t₁ : uniform_space α] [t₂ : uniform_space β] {a : set (α × α)}
{b : set (β × β)} (ha : a ∈ 𝓤 α) (hb : b ∈ 𝓤 β) :
{p:(α×β)×(α×β) | (p.1.1, p.2.1) ∈ a ∧ (p.1.2, p.2.2) ∈ b } ∈ (@uniformity (α × β) _) :=
by rw [uniformity_prod]; exact inter_mem_inf_sets (preimage_mem_comap ha) (preimage_mem_comap hb)
lemma tendsto_prod_uniformity_fst [uniform_space α] [uniform_space β] :
tendsto (λp:(α×β)×(α×β), (p.1.1, p.2.1)) (𝓤 (α × β)) (𝓤 α) :=
le_trans (map_mono (@inf_le_left (uniform_space (α×β)) _ _ _)) map_comap_le
lemma tendsto_prod_uniformity_snd [uniform_space α] [uniform_space β] :
tendsto (λp:(α×β)×(α×β), (p.1.2, p.2.2)) (𝓤 (α × β)) (𝓤 β) :=
le_trans (map_mono (@inf_le_right (uniform_space (α×β)) _ _ _)) map_comap_le
lemma uniform_continuous_fst [uniform_space α] [uniform_space β] :
uniform_continuous (λp:α×β, p.1) :=
tendsto_prod_uniformity_fst
lemma uniform_continuous_snd [uniform_space α] [uniform_space β] :
uniform_continuous (λp:α×β, p.2) :=
tendsto_prod_uniformity_snd
variables [uniform_space α] [uniform_space β] [uniform_space γ]
lemma uniform_continuous.prod_mk
{f₁ : α → β} {f₂ : α → γ} (h₁ : uniform_continuous f₁) (h₂ : uniform_continuous f₂) :
uniform_continuous (λa, (f₁ a, f₂ a)) :=
by rw [uniform_continuous, uniformity_prod]; exact
tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩
lemma uniform_continuous.prod_mk_left {f : α × β → γ} (h : uniform_continuous f) (b) :
uniform_continuous (λ a, f (a,b)) :=
h.comp (uniform_continuous_id.prod_mk uniform_continuous_const)
lemma uniform_continuous.prod_mk_right {f : α × β → γ} (h : uniform_continuous f) (a) :
uniform_continuous (λ b, f (a,b)) :=
h.comp (uniform_continuous_const.prod_mk uniform_continuous_id)
lemma uniform_continuous.prod_map [uniform_space δ] {f : α → γ} {g : β → δ}
(hf : uniform_continuous f) (hg : uniform_continuous g) :
uniform_continuous (prod.map f g) :=
(hf.comp uniform_continuous_fst).prod_mk (hg.comp uniform_continuous_snd)
lemma to_topological_space_prod {α} {β} [u : uniform_space α] [v : uniform_space β] :
@uniform_space.to_topological_space (α × β) prod.uniform_space =
@prod.topological_space α β u.to_topological_space v.to_topological_space := rfl
end prod
section
open uniform_space function
variables {δ' : Type*} [uniform_space α] [uniform_space β] [uniform_space γ] [uniform_space δ]
[uniform_space δ']
local notation f `∘₂` g := function.bicompr f g
/-- Uniform continuity for functions of two variables. -/
def uniform_continuous₂ (f : α → β → γ) := uniform_continuous (uncurry f)
lemma uniform_continuous₂_def (f : α → β → γ) :
uniform_continuous₂ f ↔ uniform_continuous (uncurry f) := iff.rfl
lemma uniform_continuous₂.uniform_continuous {f : α → β → γ} (h : uniform_continuous₂ f) :
uniform_continuous (uncurry f) := h
lemma uniform_continuous₂_curry (f : α × β → γ) :
uniform_continuous₂ (function.curry f) ↔ uniform_continuous f :=
by rw [uniform_continuous₂, uncurry_curry]
lemma uniform_continuous₂.comp {f : α → β → γ} {g : γ → δ}
(hg : uniform_continuous g) (hf : uniform_continuous₂ f) :
uniform_continuous₂ (g ∘₂ f) :=
hg.comp hf
lemma uniform_continuous₂.bicompl {f : α → β → γ} {ga : δ → α} {gb : δ' → β}
(hf : uniform_continuous₂ f) (hga : uniform_continuous ga) (hgb : uniform_continuous gb) :
uniform_continuous₂ (bicompl f ga gb) :=
hf.uniform_continuous.comp (hga.prod_map hgb)
end
lemma to_topological_space_subtype [u : uniform_space α] {p : α → Prop} :
@uniform_space.to_topological_space (subtype p) subtype.uniform_space =
@subtype.topological_space α p u.to_topological_space := rfl
section sum
variables [uniform_space α] [uniform_space β]
open sum
/-- Uniformity on a disjoint union. Entourages of the diagonal in the union are obtained
by taking independently an entourage of the diagonal in the first part, and an entourage of
the diagonal in the second part. -/
def uniform_space.core.sum : uniform_space.core (α ⊕ β) :=
uniform_space.core.mk'
(map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔ map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β))
(λ r ⟨H₁, H₂⟩ x, by cases x; [apply refl_mem_uniformity H₁, apply refl_mem_uniformity H₂])
(λ r ⟨H₁, H₂⟩, ⟨symm_le_uniformity H₁, symm_le_uniformity H₂⟩)
(λ r ⟨Hrα, Hrβ⟩, begin
rcases comp_mem_uniformity_sets Hrα with ⟨tα, htα, Htα⟩,
rcases comp_mem_uniformity_sets Hrβ with ⟨tβ, htβ, Htβ⟩,
refine ⟨_,
⟨mem_map_sets_iff.2 ⟨tα, htα, subset_union_left _ _⟩,
mem_map_sets_iff.2 ⟨tβ, htβ, subset_union_right _ _⟩⟩, _⟩,
rintros ⟨_, _⟩ ⟨z, ⟨⟨a, b⟩, hab, ⟨⟩⟩ | ⟨⟨a, b⟩, hab, ⟨⟩⟩,
⟨⟨_, c⟩, hbc, ⟨⟩⟩ | ⟨⟨_, c⟩, hbc, ⟨⟩⟩⟩,
{ have A : (a, c) ∈ tα ○ tα := ⟨b, hab, hbc⟩,
exact Htα A },
{ have A : (a, c) ∈ tβ ○ tβ := ⟨b, hab, hbc⟩,
exact Htβ A }
end)
/-- The union of an entourage of the diagonal in each set of a disjoint union is again an entourage
of the diagonal. -/
lemma union_mem_uniformity_sum
{a : set (α × α)} (ha : a ∈ 𝓤 α) {b : set (β × β)} (hb : b ∈ 𝓤 β) :
((λ p : (α × α), (inl p.1, inl p.2)) '' a ∪ (λ p : (β × β), (inr p.1, inr p.2)) '' b) ∈
(@uniform_space.core.sum α β _ _).uniformity :=
⟨mem_map_sets_iff.2 ⟨_, ha, subset_union_left _ _⟩,
mem_map_sets_iff.2 ⟨_, hb, subset_union_right _ _⟩⟩
/- To prove that the topology defined by the uniform structure on the disjoint union coincides with
the disjoint union topology, we need two lemmas saying that open sets can be characterized by
the uniform structure -/
lemma uniformity_sum_of_open_aux {s : set (α ⊕ β)} (hs : is_open s) {x : α ⊕ β} (xs : x ∈ s) :
{ p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈ (@uniform_space.core.sum α β _ _).uniformity :=
begin
cases x,
{ refine mem_sets_of_superset
(union_mem_uniformity_sum (mem_nhds_uniformity_iff_right.1 (is_open.mem_nhds hs.1 xs))
univ_mem_sets)
(union_subset _ _);
rintro _ ⟨⟨_, b⟩, h, ⟨⟩⟩ ⟨⟩,
exact h rfl },
{ refine mem_sets_of_superset
(union_mem_uniformity_sum univ_mem_sets (mem_nhds_uniformity_iff_right.1
(is_open.mem_nhds hs.2 xs)))
(union_subset _ _);
rintro _ ⟨⟨a, _⟩, h, ⟨⟩⟩ ⟨⟩,
exact h rfl },
end
lemma open_of_uniformity_sum_aux {s : set (α ⊕ β)}
(hs : ∀x ∈ s, { p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈
(@uniform_space.core.sum α β _ _).uniformity) :
is_open s :=
begin
split,
{ refine (@is_open_iff_mem_nhds α _ _).2 (λ a ha, mem_nhds_uniformity_iff_right.2 _),
rcases mem_map_sets_iff.1 (hs _ ha).1 with ⟨t, ht, st⟩,
refine mem_sets_of_superset ht _,
rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl },
{ refine (@is_open_iff_mem_nhds β _ _).2 (λ b hb, mem_nhds_uniformity_iff_right.2 _),
rcases mem_map_sets_iff.1 (hs _ hb).2 with ⟨t, ht, st⟩,
refine mem_sets_of_superset ht _,
rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl }
end
/- We can now define the uniform structure on the disjoint union -/
instance sum.uniform_space : uniform_space (α ⊕ β) :=
{ to_core := uniform_space.core.sum,
is_open_uniformity := λ s, ⟨uniformity_sum_of_open_aux, open_of_uniformity_sum_aux⟩ }
lemma sum.uniformity : 𝓤 (α ⊕ β) =
map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔
map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β) := rfl
end sum
end constructions
-- For a version of the Lebesgue number lemma assuming only a sequentially compact space,
-- see topology/sequences.lean
/-- Let `c : ι → set α` be an open cover of a compact set `s`. Then there exists an entourage
`n` such that for each `x ∈ s` its `n`-neighborhood is contained in some `c i`. -/
lemma lebesgue_number_lemma {α : Type u} [uniform_space α] {s : set α} {ι} {c : ι → set α}
(hs : is_compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) :
∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ i, {y | (x, y) ∈ n} ⊆ c i :=
begin
let u := λ n, {x | ∃ i (m ∈ 𝓤 α), {y | (x, y) ∈ m ○ n} ⊆ c i},
have hu₁ : ∀ n ∈ 𝓤 α, is_open (u n),
{ refine λ n hn, is_open_uniformity.2 _,
rintro x ⟨i, m, hm, h⟩,
rcases comp_mem_uniformity_sets hm with ⟨m', hm', mm'⟩,
apply (𝓤 α).sets_of_superset hm',
rintros ⟨x, y⟩ hp rfl,
refine ⟨i, m', hm', λ z hz, h (monotone_comp_rel monotone_id monotone_const mm' _)⟩,
dsimp at hz ⊢, rw comp_rel_assoc,
exact ⟨y, hp, hz⟩ },
have hu₂ : s ⊆ ⋃ n ∈ 𝓤 α, u n,
{ intros x hx,
rcases mem_Union.1 (hc₂ hx) with ⟨i, h⟩,
rcases comp_mem_uniformity_sets (is_open_uniformity.1 (hc₁ i) x h) with ⟨m', hm', mm'⟩,
exact mem_bUnion hm' ⟨i, _, hm', λ y hy, mm' hy rfl⟩ },
rcases hs.elim_finite_subcover_image hu₁ hu₂ with ⟨b, bu, b_fin, b_cover⟩,
refine ⟨_, (bInter_mem_sets b_fin).2 bu, λ x hx, _⟩,
rcases mem_bUnion_iff.1 (b_cover hx) with ⟨n, bn, i, m, hm, h⟩,
refine ⟨i, λ y hy, h _⟩,
exact prod_mk_mem_comp_rel (refl_mem_uniformity hm) (bInter_subset_of_mem bn hy)
end
/-- Let `c : set (set α)` be an open cover of a compact set `s`. Then there exists an entourage
`n` such that for each `x ∈ s` its `n`-neighborhood is contained in some `t ∈ c`. -/
lemma lebesgue_number_lemma_sUnion {α : Type u} [uniform_space α] {s : set α} {c : set (set α)}
(hs : is_compact s) (hc₁ : ∀ t ∈ c, is_open t) (hc₂ : s ⊆ ⋃₀ c) :
∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ t ∈ c, ∀ y, (x, y) ∈ n → y ∈ t :=
by rw sUnion_eq_Union at hc₂;
simpa using lebesgue_number_lemma hs (by simpa) hc₂
/-!
### Expressing continuity properties in uniform spaces
We reformulate the various continuity properties of functions taking values in a uniform space
in terms of the uniformity in the target. Since the same lemmas (essentially with the same names)
also exist for metric spaces and emetric spaces (reformulating things in terms of the distance or
the edistance in the target), we put them in a namespace `uniform` here.
In the metric and emetric space setting, there are also similar lemmas where one assumes that
both the source and the target are metric spaces, reformulating things in terms of the distance
on both sides. These lemmas are generally written without primes, and the versions where only
the target is a metric space is primed. We follow the same convention here, thus giving lemmas
with primes.
-/
namespace uniform
variables [uniform_space α]
theorem tendsto_nhds_right {f : filter β} {u : β → α} {a : α} :
tendsto u f (𝓝 a) ↔ tendsto (λ x, (a, u x)) f (𝓤 α) :=
⟨λ H, tendsto_left_nhds_uniformity.comp H,
λ H s hs, by simpa [mem_of_mem_nhds hs] using H (mem_nhds_uniformity_iff_right.1 hs)⟩
theorem tendsto_nhds_left {f : filter β} {u : β → α} {a : α} :
tendsto u f (𝓝 a) ↔ tendsto (λ x, (u x, a)) f (𝓤 α) :=
⟨λ H, tendsto_right_nhds_uniformity.comp H,
λ H s hs, by simpa [mem_of_mem_nhds hs] using H (mem_nhds_uniformity_iff_left.1 hs)⟩
theorem continuous_at_iff'_right [topological_space β] {f : β → α} {b : β} :
continuous_at f b ↔ tendsto (λ x, (f b, f x)) (𝓝 b) (𝓤 α) :=
by rw [continuous_at, tendsto_nhds_right]
theorem continuous_at_iff'_left [topological_space β] {f : β → α} {b : β} :
continuous_at f b ↔ tendsto (λ x, (f x, f b)) (𝓝 b) (𝓤 α) :=
by rw [continuous_at, tendsto_nhds_left]
theorem continuous_at_iff_prod [topological_space β] {f : β → α} {b : β} :
continuous_at f b ↔ tendsto (λ x : β × β, (f x.1, f x.2)) (𝓝 (b, b)) (𝓤 α) :=
⟨λ H, le_trans (H.prod_map' H) (nhds_le_uniformity _),
λ H, continuous_at_iff'_left.2 $ H.comp $ tendsto_id.prod_mk_nhds tendsto_const_nhds⟩
theorem continuous_within_at_iff'_right [topological_space β] {f : β → α} {b : β} {s : set β} :
continuous_within_at f s b ↔ tendsto (λ x, (f b, f x)) (𝓝[s] b) (𝓤 α) :=
by rw [continuous_within_at, tendsto_nhds_right]
theorem continuous_within_at_iff'_left [topological_space β] {f : β → α} {b : β} {s : set β} :
continuous_within_at f s b ↔ tendsto (λ x, (f x, f b)) (𝓝[s] b) (𝓤 α) :=
by rw [continuous_within_at, tendsto_nhds_left]
theorem continuous_on_iff'_right [topological_space β] {f : β → α} {s : set β} :
continuous_on f s ↔ ∀ b ∈ s, tendsto (λ x, (f b, f x)) (𝓝[s] b) (𝓤 α) :=
by simp [continuous_on, continuous_within_at_iff'_right]
theorem continuous_on_iff'_left [topological_space β] {f : β → α} {s : set β} :
continuous_on f s ↔ ∀ b ∈ s, tendsto (λ x, (f x, f b)) (𝓝[s] b) (𝓤 α) :=
by simp [continuous_on, continuous_within_at_iff'_left]
theorem continuous_iff'_right [topological_space β] {f : β → α} :
continuous f ↔ ∀ b, tendsto (λ x, (f b, f x)) (𝓝 b) (𝓤 α) :=
continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_right
theorem continuous_iff'_left [topological_space β] {f : β → α} :
continuous f ↔ ∀ b, tendsto (λ x, (f x, f b)) (𝓝 b) (𝓤 α) :=
continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_left
end uniform
lemma filter.tendsto.congr_uniformity {α β} [uniform_space β] {f g : α → β} {l : filter α} {b : β}
(hf : tendsto f l (𝓝 b)) (hg : tendsto (λ x, (f x, g x)) l (𝓤 β)) :
tendsto g l (𝓝 b) :=
uniform.tendsto_nhds_right.2 $ (uniform.tendsto_nhds_right.1 hf).uniformity_trans hg
lemma uniform.tendsto_congr {α β} [uniform_space β] {f g : α → β} {l : filter α} {b : β}
(hfg : tendsto (λ x, (f x, g x)) l (𝓤 β)) :
tendsto f l (𝓝 b) ↔ tendsto g l (𝓝 b) :=
⟨λ h, h.congr_uniformity hfg, λ h, h.congr_uniformity hfg.uniformity_symm⟩
|
bd0c49b5403f287daf40fd4aa20911101e02cf35 | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch5/ex0510.lean | 7520a6f790961c368e58673254a954f8ff9c0607 | [] | 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 | 140 | lean | example (p q r : Prop) (hp : p) (hq : q) (hr : r) : p ∧ q ∧ r :=
begin
repeat { any_goals { split } },
all_goals { assumption }
end
|
2c653f9cae7bf938ae30c279b01364922d8cc5ff | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /tests/compiler/rbmap_library.lean | 4455cd46593ea2d682056c3411f8e2a170b80e9e | [
"Apache-2.0"
] | permissive | mhuisi/lean4 | 28d35a4febc2e251c7f05492e13f3b05d6f9b7af | dda44bc47f3e5d024508060dac2bcb59fd12e4c0 | refs/heads/master | 1,621,225,489,283 | 1,585,142,689,000 | 1,585,142,689,000 | 250,590,438 | 0 | 2 | Apache-2.0 | 1,602,443,220,000 | 1,585,327,814,000 | C | UTF-8 | Lean | false | false | 2,274 | lean | def check (b : Bool) : IO Unit :=
unless b $ IO.println "ERROR"
def sz {α β : Type} {lt : α → α → Bool} (m : RBMap α β lt) : Nat :=
m.fold (fun sz _ _ => sz+1) 0
def depth {α β : Type} {lt : α → α → Bool} (m : RBMap α β lt) : Nat :=
m.depth Nat.max
def tst1 : IO Unit :=
do let Map := RBMap String Nat (fun a b => a < b);
let m : Map := {};
let m := m.insert "hello" 0;
let m := m.insert "world" 1;
check (m.find? "hello" == some 0);
check (m.find? "world" == some 1);
let m := m.erase "hello";
check (m.find? "hello" == none);
check (m.find? "world" == some 1);
pure ()
def tst2 : IO Unit :=
do let Map := RBMap Nat Nat (fun a b => a < b);
let m : Map := {};
let n : Nat := 10000;
let m := n.fold (fun i (m : Map) => m.insert i (i*10)) m;
check (m.all (fun k v => v == k*10));
check (sz m == n);
IO.println (">> " ++ toString (depth m) ++ ", " ++ toString (sz m));
let m := (n/2).fold (fun i (m : Map) => m.erase (2*i)) m;
check (m.all (fun k v => v == k*10));
check (sz m == n / 2);
IO.println (">> " ++ toString (depth m) ++ ", " ++ toString (sz m));
pure ()
abbrev Map := RBMap Nat Nat (fun a b => a < b)
def mkRandMap (max : Nat) : Nat → Map → Array (Nat × Nat) → IO (Map × Array (Nat × Nat))
| 0, m, a => pure (m, a)
| n+1, m, a => do
k ← IO.rand 0 max;
v ← IO.rand 0 max;
if m.find? k == none then do
let m := m.insert k v;
let a := a.push (k, v);
mkRandMap n m a
else
mkRandMap n m a
def tst3 (seed : Nat) (n : Nat) (max : Nat) : IO Unit :=
do IO.setRandSeed seed;
(m, a) ← mkRandMap max n {} Array.empty;
check (sz m == a.size);
check (a.all (fun ⟨k, v⟩ => m.find? k == some v));
IO.println ("tst3 size: " ++ toString a.size);
let m := a.iterate m (fun i ⟨k, v⟩ m => if i.val % 2 == 0 then m.erase k else m);
check (sz m == a.size / 2);
a.iterateM () (fun i ⟨k, v⟩ _ => when (i.val % 2 == 1) (check (m.find? k == some v)));
IO.println ("tst3 after, depth: " ++ toString (depth m) ++ ", size: " ++ toString (sz m));
pure ()
def main (xs : List String) : IO Unit :=
tst1 *> tst2 *>
tst3 1 1000 20000 *>
tst3 2 1000 40000 *>
tst3 3 100 4000 *>
tst3 4 5000 100000 *>
tst3 5 1000 40000 *>
pure ()
|
f19e06ef1d2be9a9eb86f137971e4dfd1e3cc084 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/data/multiset/functor.lean | 588cf6028971918a04d7df7b70b6d48e31ad5579 | [
"Apache-2.0"
] | permissive | Lix0120/mathlib | 0020745240315ed0e517cbf32e738d8f9811dd80 | e14c37827456fc6707f31b4d1d16f1f3a3205e91 | refs/heads/master | 1,673,102,855,024 | 1,604,151,044,000 | 1,604,151,044,000 | 308,930,245 | 0 | 0 | Apache-2.0 | 1,604,164,710,000 | 1,604,163,547,000 | null | UTF-8 | Lean | false | false | 4,249 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro, Johannes Hölzl, Simon Hudon, Kenny Lau
-/
import data.multiset.basic
import control.traversable.lemmas
import control.traversable.instances
/-!
# Functoriality of `multiset`.
-/
universes u
namespace multiset
open list
instance : functor multiset :=
{ map := @map }
@[simp] lemma fmap_def {α' β'} {s : multiset α'} (f : α' → β') : f <$> s = s.map f := rfl
instance : is_lawful_functor multiset :=
by refine { .. }; intros; simp
open is_lawful_traversable is_comm_applicative
variables {F : Type u → Type u} [applicative F] [is_comm_applicative F]
variables {α' β' : Type u} (f : α' → F β')
def traverse : multiset α' → F (multiset β') :=
quotient.lift (functor.map coe ∘ traversable.traverse f)
begin
introv p, unfold function.comp,
induction p,
case perm.nil { refl },
case perm.cons {
have : multiset.cons <$> f p_x <*> (coe <$> traverse f p_l₁) =
multiset.cons <$> f p_x <*> (coe <$> traverse f p_l₂),
{ rw [p_ih] },
simpa with functor_norm },
case perm.swap {
have : (λa b (l:list β'), (↑(a :: b :: l) : multiset β')) <$> f p_y <*> f p_x =
(λa b l, ↑(a :: b :: l)) <$> f p_x <*> f p_y,
{ rw [is_comm_applicative.commutative_map],
congr, funext a b l, simpa [flip] using perm.swap b a l },
simp [(∘), this] with functor_norm },
case perm.trans { simp [*] }
end
instance : monad multiset :=
{ pure := λ α x, x ::ₘ 0,
bind := @bind,
.. multiset.functor }
@[simp] lemma pure_def {α} : (pure : α → multiset α) = (λ x, x ::ₘ 0) := rfl
@[simp] lemma bind_def {α β} : (>>=) = @bind α β := rfl
instance : is_lawful_monad multiset :=
{ bind_pure_comp_eq_map := λ α β f s, multiset.induction_on s rfl $ λ a s ih, by simp,
pure_bind := λ α β x f, by simp,
bind_assoc := @bind_assoc }
open functor
open traversable is_lawful_traversable
@[simp]
lemma lift_beta {α β : Type*} (x : list α) (f : list α → β)
(h : ∀ a b : list α, a ≈ b → f a = f b) :
quotient.lift f h (x : multiset α) = f x :=
quotient.lift_beta _ _ _
@[simp]
lemma map_comp_coe {α β} (h : α → β) :
functor.map h ∘ coe = (coe ∘ functor.map h : list α → multiset β) :=
by funext; simp [functor.map]
lemma id_traverse {α : Type*} (x : multiset α) :
traverse id.mk x = x :=
quotient.induction_on x begin intro, simp [traverse], refl end
lemma comp_traverse {G H : Type* → Type*}
[applicative G] [applicative H]
[is_comm_applicative G] [is_comm_applicative H]
{α β γ : Type*}
(g : α → G β) (h : β → H γ) (x : multiset α) :
traverse (comp.mk ∘ functor.map h ∘ g) x =
comp.mk (functor.map (traverse h) (traverse g x)) :=
quotient.induction_on x
(by intro;
simp [traverse,comp_traverse] with functor_norm;
simp [(<$>),(∘)] with functor_norm)
lemma map_traverse {G : Type* → Type*}
[applicative G] [is_comm_applicative G]
{α β γ : Type*}
(g : α → G β) (h : β → γ)
(x : multiset α) :
functor.map (functor.map h) (traverse g x) =
traverse (functor.map h ∘ g) x :=
quotient.induction_on x
(by intro; simp [traverse] with functor_norm;
rw [comp_map,map_traverse])
lemma traverse_map {G : Type* → Type*}
[applicative G] [is_comm_applicative G]
{α β γ : Type*}
(g : α → β) (h : β → G γ)
(x : multiset α) :
traverse h (map g x) =
traverse (h ∘ g) x :=
quotient.induction_on x
(by intro; simp [traverse];
rw [← traversable.traverse_map h g];
[ refl, apply_instance ])
lemma naturality {G H : Type* → Type*}
[applicative G] [applicative H]
[is_comm_applicative G] [is_comm_applicative H]
(eta : applicative_transformation G H)
{α β : Type*} (f : α → G β) (x : multiset α) :
eta (traverse f x) = traverse (@eta _ ∘ f) x :=
quotient.induction_on x
(by intro; simp [traverse,is_lawful_traversable.naturality] with functor_norm)
end multiset
|
5d5b5b72d841d9db8fca923ffc6f0b19bb990bba | 432d948a4d3d242fdfb44b81c9e1b1baacd58617 | /src/data/set/intervals/image_preimage.lean | dad65888833c868fc95df68667f21674b36b6acf | [
"Apache-2.0"
] | permissive | JLimperg/aesop3 | 306cc6570c556568897ed2e508c8869667252e8a | a4a116f650cc7403428e72bd2e2c4cda300fe03f | refs/heads/master | 1,682,884,916,368 | 1,620,320,033,000 | 1,620,320,033,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 20,341 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov, Patrick Massot
-/
import data.set.intervals.basic
import data.equiv.mul_add
import algebra.pointwise
/-!
# (Pre)images of intervals
In this file we prove a bunch of trivial lemmas like “if we add `a` to all points of `[b, c]`,
then we get `[a + b, a + c]`”. For the functions `x ↦ x ± a`, `x ↦ a ± x`, and `x ↦ -x` we prove
lemmas about preimages and images of all intervals. We also prove a few lemmas about images under
`x ↦ a * x`, `x ↦ x * a` and `x ↦ x⁻¹`.
-/
universe u
namespace set
section has_exists_add_of_le
/-!
The lemmas in this section state that addition maps intervals bijectively. The typeclass
`has_exists_add_of_le` is defined specifically to make them work when combined with
`ordered_cancel_add_comm_monoid`; the lemmas below therefore apply to all
`ordered_add_comm_group`, but also to `ℕ` and `ℝ≥0`, which are not groups.
TODO : move as much as possible in this file to the setting of this weaker typeclass.
-/
variables {α : Type u} [ordered_cancel_add_comm_monoid α] [has_exists_add_of_le α] (a b d : α)
lemma Icc_add_bij : bij_on (+d) (Icc a b) (Icc (a + d) (b + d)) :=
begin
refine ⟨λ _ h, ⟨add_le_add_right h.1 _, add_le_add_right h.2 _⟩,
λ _ _ _ _ h, add_right_cancel h,
λ _ h, _⟩,
obtain ⟨c, rfl⟩ := exists_add_of_le h.1,
rw [mem_Icc, add_right_comm, add_le_add_iff_right, add_le_add_iff_right] at h,
exact ⟨a + c, h, by rw add_right_comm⟩,
end
lemma Ioo_add_bij : bij_on (+d) (Ioo a b) (Ioo (a + d) (b + d)) :=
begin
refine ⟨λ _ h, ⟨add_lt_add_right h.1 _, add_lt_add_right h.2 _⟩,
λ _ _ _ _ h, add_right_cancel h,
λ _ h, _⟩,
obtain ⟨c, rfl⟩ := exists_add_of_le h.1.le,
rw [mem_Ioo, add_right_comm, add_lt_add_iff_right, add_lt_add_iff_right] at h,
exact ⟨a + c, h, by rw add_right_comm⟩,
end
lemma Ioc_add_bij : bij_on (+d) (Ioc a b) (Ioc (a + d) (b + d)) :=
begin
refine ⟨λ _ h, ⟨add_lt_add_right h.1 _, add_le_add_right h.2 _⟩,
λ _ _ _ _ h, add_right_cancel h,
λ _ h, _⟩,
obtain ⟨c, rfl⟩ := exists_add_of_le h.1.le,
rw [mem_Ioc, add_right_comm, add_lt_add_iff_right, add_le_add_iff_right] at h,
exact ⟨a + c, h, by rw add_right_comm⟩,
end
lemma Ico_add_bij : bij_on (+d) (Ico a b) (Ico (a + d) (b + d)) :=
begin
refine ⟨λ _ h, ⟨add_le_add_right h.1 _, add_lt_add_right h.2 _⟩,
λ _ _ _ _ h, add_right_cancel h,
λ _ h, _⟩,
obtain ⟨c, rfl⟩ := exists_add_of_le h.1,
rw [mem_Ico, add_right_comm, add_le_add_iff_right, add_lt_add_iff_right] at h,
exact ⟨a + c, h, by rw add_right_comm⟩,
end
lemma Ici_add_bij : bij_on (+d) (Ici a) (Ici (a + d)) :=
begin
refine ⟨λ x h, add_le_add_right (mem_Ici.mp h) _, λ _ _ _ _ h, add_right_cancel h, λ _ h, _⟩,
obtain ⟨c, rfl⟩ := exists_add_of_le (mem_Ici.mp h),
rw [mem_Ici, add_right_comm, add_le_add_iff_right] at h,
exact ⟨a + c, h, by rw add_right_comm⟩,
end
lemma Ioi_add_bij : bij_on (+d) (Ioi a) (Ioi (a + d)) :=
begin
refine ⟨λ x h, add_lt_add_right (mem_Ioi.mp h) _, λ _ _ _ _ h, add_right_cancel h, λ _ h, _⟩,
obtain ⟨c, rfl⟩ := exists_add_of_le (mem_Ioi.mp h).le,
rw [mem_Ioi, add_right_comm, add_lt_add_iff_right] at h,
exact ⟨a + c, h, by rw add_right_comm⟩,
end
end has_exists_add_of_le
section ordered_add_comm_group
variables {G : Type u} [ordered_add_comm_group G] (a b c : G)
/-!
### Preimages under `x ↦ a + x`
-/
@[simp] lemma preimage_const_add_Ici : (λ x, a + x) ⁻¹' (Ici b) = Ici (b - a) :=
ext $ λ x, sub_le_iff_le_add'.symm
@[simp] lemma preimage_const_add_Ioi : (λ x, a + x) ⁻¹' (Ioi b) = Ioi (b - a) :=
ext $ λ x, sub_lt_iff_lt_add'.symm
@[simp] lemma preimage_const_add_Iic : (λ x, a + x) ⁻¹' (Iic b) = Iic (b - a) :=
ext $ λ x, le_sub_iff_add_le'.symm
@[simp] lemma preimage_const_add_Iio : (λ x, a + x) ⁻¹' (Iio b) = Iio (b - a) :=
ext $ λ x, lt_sub_iff_add_lt'.symm
@[simp] lemma preimage_const_add_Icc : (λ x, a + x) ⁻¹' (Icc b c) = Icc (b - a) (c - a) :=
by simp [← Ici_inter_Iic]
@[simp] lemma preimage_const_add_Ico : (λ x, a + x) ⁻¹' (Ico b c) = Ico (b - a) (c - a) :=
by simp [← Ici_inter_Iio]
@[simp] lemma preimage_const_add_Ioc : (λ x, a + x) ⁻¹' (Ioc b c) = Ioc (b - a) (c - a) :=
by simp [← Ioi_inter_Iic]
@[simp] lemma preimage_const_add_Ioo : (λ x, a + x) ⁻¹' (Ioo b c) = Ioo (b - a) (c - a) :=
by simp [← Ioi_inter_Iio]
/-!
### Preimages under `x ↦ x + a`
-/
@[simp] lemma preimage_add_const_Ici : (λ x, x + a) ⁻¹' (Ici b) = Ici (b - a) :=
ext $ λ x, sub_le_iff_le_add.symm
@[simp] lemma preimage_add_const_Ioi : (λ x, x + a) ⁻¹' (Ioi b) = Ioi (b - a) :=
ext $ λ x, sub_lt_iff_lt_add.symm
@[simp] lemma preimage_add_const_Iic : (λ x, x + a) ⁻¹' (Iic b) = Iic (b - a) :=
ext $ λ x, le_sub_iff_add_le.symm
@[simp] lemma preimage_add_const_Iio : (λ x, x + a) ⁻¹' (Iio b) = Iio (b - a) :=
ext $ λ x, lt_sub_iff_add_lt.symm
@[simp] lemma preimage_add_const_Icc : (λ x, x + a) ⁻¹' (Icc b c) = Icc (b - a) (c - a) :=
by simp [← Ici_inter_Iic]
@[simp] lemma preimage_add_const_Ico : (λ x, x + a) ⁻¹' (Ico b c) = Ico (b - a) (c - a) :=
by simp [← Ici_inter_Iio]
@[simp] lemma preimage_add_const_Ioc : (λ x, x + a) ⁻¹' (Ioc b c) = Ioc (b - a) (c - a) :=
by simp [← Ioi_inter_Iic]
@[simp] lemma preimage_add_const_Ioo : (λ x, x + a) ⁻¹' (Ioo b c) = Ioo (b - a) (c - a) :=
by simp [← Ioi_inter_Iio]
/-!
### Preimages under `x ↦ -x`
-/
@[simp] lemma preimage_neg_Ici : - Ici a = Iic (-a) := ext $ λ x, le_neg
@[simp] lemma preimage_neg_Iic : - Iic a = Ici (-a) := ext $ λ x, neg_le
@[simp] lemma preimage_neg_Ioi : - Ioi a = Iio (-a) := ext $ λ x, lt_neg
@[simp] lemma preimage_neg_Iio : - Iio a = Ioi (-a) := ext $ λ x, neg_lt
@[simp] lemma preimage_neg_Icc : - Icc a b = Icc (-b) (-a) :=
by simp [← Ici_inter_Iic, inter_comm]
@[simp] lemma preimage_neg_Ico : - Ico a b = Ioc (-b) (-a) :=
by simp [← Ici_inter_Iio, ← Ioi_inter_Iic, inter_comm]
@[simp] lemma preimage_neg_Ioc : - Ioc a b = Ico (-b) (-a) :=
by simp [← Ioi_inter_Iic, ← Ici_inter_Iio, inter_comm]
@[simp] lemma preimage_neg_Ioo : - Ioo a b = Ioo (-b) (-a) :=
by simp [← Ioi_inter_Iio, inter_comm]
/-!
### Preimages under `x ↦ x - a`
-/
@[simp] lemma preimage_sub_const_Ici : (λ x, x - a) ⁻¹' (Ici b) = Ici (b + a) :=
by simp [sub_eq_add_neg]
@[simp] lemma preimage_sub_const_Ioi : (λ x, x - a) ⁻¹' (Ioi b) = Ioi (b + a) :=
by simp [sub_eq_add_neg]
@[simp] lemma preimage_sub_const_Iic : (λ x, x - a) ⁻¹' (Iic b) = Iic (b + a) :=
by simp [sub_eq_add_neg]
@[simp] lemma preimage_sub_const_Iio : (λ x, x - a) ⁻¹' (Iio b) = Iio (b + a) :=
by simp [sub_eq_add_neg]
@[simp] lemma preimage_sub_const_Icc : (λ x, x - a) ⁻¹' (Icc b c) = Icc (b + a) (c + a) :=
by simp [sub_eq_add_neg]
@[simp] lemma preimage_sub_const_Ico : (λ x, x - a) ⁻¹' (Ico b c) = Ico (b + a) (c + a) :=
by simp [sub_eq_add_neg]
@[simp] lemma preimage_sub_const_Ioc : (λ x, x - a) ⁻¹' (Ioc b c) = Ioc (b + a) (c + a) :=
by simp [sub_eq_add_neg]
@[simp] lemma preimage_sub_const_Ioo : (λ x, x - a) ⁻¹' (Ioo b c) = Ioo (b + a) (c + a) :=
by simp [sub_eq_add_neg]
/-!
### Preimages under `x ↦ a - x`
-/
@[simp] lemma preimage_const_sub_Ici : (λ x, a - x) ⁻¹' (Ici b) = Iic (a - b) :=
ext $ λ x, le_sub
@[simp] lemma preimage_const_sub_Iic : (λ x, a - x) ⁻¹' (Iic b) = Ici (a - b) :=
ext $ λ x, sub_le
@[simp] lemma preimage_const_sub_Ioi : (λ x, a - x) ⁻¹' (Ioi b) = Iio (a - b) :=
ext $ λ x, lt_sub
@[simp] lemma preimage_const_sub_Iio : (λ x, a - x) ⁻¹' (Iio b) = Ioi (a - b) :=
ext $ λ x, sub_lt
@[simp] lemma preimage_const_sub_Icc : (λ x, a - x) ⁻¹' (Icc b c) = Icc (a - c) (a - b) :=
by simp [← Ici_inter_Iic, inter_comm]
@[simp] lemma preimage_const_sub_Ico : (λ x, a - x) ⁻¹' (Ico b c) = Ioc (a - c) (a - b) :=
by simp [← Ioi_inter_Iic, ← Ici_inter_Iio, inter_comm]
@[simp] lemma preimage_const_sub_Ioc : (λ x, a - x) ⁻¹' (Ioc b c) = Ico (a - c) (a - b) :=
by simp [← Ioi_inter_Iic, ← Ici_inter_Iio, inter_comm]
@[simp] lemma preimage_const_sub_Ioo : (λ x, a - x) ⁻¹' (Ioo b c) = Ioo (a - c) (a - b) :=
by simp [← Ioi_inter_Iio, inter_comm]
/-!
### Images under `x ↦ a + x`
-/
@[simp] lemma image_const_add_Ici : (λ x, a + x) '' Ici b = Ici (a + b) :=
by simp [add_comm]
@[simp] lemma image_const_add_Iic : (λ x, a + x) '' Iic b = Iic (a + b) :=
by simp [add_comm]
@[simp] lemma image_const_add_Iio : (λ x, a + x) '' Iio b = Iio (a + b) :=
by simp [add_comm]
@[simp] lemma image_const_add_Ioi : (λ x, a + x) '' Ioi b = Ioi (a + b) :=
by simp [add_comm]
@[simp] lemma image_const_add_Icc : (λ x, a + x) '' Icc b c = Icc (a + b) (a + c) :=
by simp [add_comm]
@[simp] lemma image_const_add_Ico : (λ x, a + x) '' Ico b c = Ico (a + b) (a + c) :=
by simp [add_comm]
@[simp] lemma image_const_add_Ioc : (λ x, a + x) '' Ioc b c = Ioc (a + b) (a + c) :=
by simp [add_comm]
@[simp] lemma image_const_add_Ioo : (λ x, a + x) '' Ioo b c = Ioo (a + b) (a + c) :=
by simp [add_comm]
/-!
### Images under `x ↦ x + a`
-/
@[simp] lemma image_add_const_Ici : (λ x, x + a) '' Ici b = Ici (b + a) := by simp
@[simp] lemma image_add_const_Iic : (λ x, x + a) '' Iic b = Iic (b + a) := by simp
@[simp] lemma image_add_const_Iio : (λ x, x + a) '' Iio b = Iio (b + a) := by simp
@[simp] lemma image_add_const_Ioi : (λ x, x + a) '' Ioi b = Ioi (b + a) := by simp
@[simp] lemma image_add_const_Icc : (λ x, x + a) '' Icc b c = Icc (b + a) (c + a) :=
by simp
@[simp] lemma image_add_const_Ico : (λ x, x + a) '' Ico b c = Ico (b + a) (c + a) :=
by simp
@[simp] lemma image_add_const_Ioc : (λ x, x + a) '' Ioc b c = Ioc (b + a) (c + a) :=
by simp
@[simp] lemma image_add_const_Ioo : (λ x, x + a) '' Ioo b c = Ioo (b + a) (c + a) :=
by simp
/-!
### Images under `x ↦ -x`
-/
lemma image_neg_Ici : has_neg.neg '' (Ici a) = Iic (-a) := by simp
lemma image_neg_Iic : has_neg.neg '' (Iic a) = Ici (-a) := by simp
lemma image_neg_Ioi : has_neg.neg '' (Ioi a) = Iio (-a) := by simp
lemma image_neg_Iio : has_neg.neg '' (Iio a) = Ioi (-a) := by simp
lemma image_neg_Icc : has_neg.neg '' (Icc a b) = Icc (-b) (-a) := by simp
lemma image_neg_Ico : has_neg.neg '' (Ico a b) = Ioc (-b) (-a) := by simp
lemma image_neg_Ioc : has_neg.neg '' (Ioc a b) = Ico (-b) (-a) := by simp
lemma image_neg_Ioo : has_neg.neg '' (Ioo a b) = Ioo (-b) (-a) := by simp
/-!
### Images under `x ↦ a - x`
-/
@[simp] lemma image_const_sub_Ici : (λ x, a - x) '' Ici b = Iic (a - b) :=
by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)]
@[simp] lemma image_const_sub_Iic : (λ x, a - x) '' Iic b = Ici (a - b) :=
by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)]
@[simp] lemma image_const_sub_Ioi : (λ x, a - x) '' Ioi b = Iio (a - b) :=
by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)]
@[simp] lemma image_const_sub_Iio : (λ x, a - x) '' Iio b = Ioi (a - b) :=
by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)]
@[simp] lemma image_const_sub_Icc : (λ x, a - x) '' Icc b c = Icc (a - c) (a - b) :=
by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)]
@[simp] lemma image_const_sub_Ico : (λ x, a - x) '' Ico b c = Ioc (a - c) (a - b) :=
by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)]
@[simp] lemma image_const_sub_Ioc : (λ x, a - x) '' Ioc b c = Ico (a - c) (a - b) :=
by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)]
@[simp] lemma image_const_sub_Ioo : (λ x, a - x) '' Ioo b c = Ioo (a - c) (a - b) :=
by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)]
/-!
### Images under `x ↦ x - a`
-/
@[simp] lemma image_sub_const_Ici : (λ x, x - a) '' Ici b = Ici (b - a) := by simp [sub_eq_neg_add]
@[simp] lemma image_sub_const_Iic : (λ x, x - a) '' Iic b = Iic (b - a) := by simp [sub_eq_neg_add]
@[simp] lemma image_sub_const_Ioi : (λ x, x - a) '' Ioi b = Ioi (b - a) := by simp [sub_eq_neg_add]
@[simp] lemma image_sub_const_Iio : (λ x, x - a) '' Iio b = Iio (b - a) := by simp [sub_eq_neg_add]
@[simp] lemma image_sub_const_Icc : (λ x, x - a) '' Icc b c = Icc (b - a) (c - a) :=
by simp [sub_eq_neg_add]
@[simp] lemma image_sub_const_Ico : (λ x, x - a) '' Ico b c = Ico (b - a) (c - a) :=
by simp [sub_eq_neg_add]
@[simp] lemma image_sub_const_Ioc : (λ x, x - a) '' Ioc b c = Ioc (b - a) (c - a) :=
by simp [sub_eq_neg_add]
@[simp] lemma image_sub_const_Ioo : (λ x, x - a) '' Ioo b c = Ioo (b - a) (c - a) :=
by simp [sub_eq_neg_add]
/-!
### Bijections
-/
lemma Iic_add_bij : bij_on (+a) (Iic b) (Iic (b + a)) :=
begin
refine ⟨λ x h, add_le_add_right (mem_Iic.mp h) _, λ _ _ _ _ h, add_right_cancel h, λ _ h, _⟩,
simpa [add_comm a] using h,
end
lemma Iio_add_bij : bij_on (+a) (Iio b) (Iio (b + a)) :=
begin
refine ⟨λ x h, add_lt_add_right (mem_Iio.mp h) _, λ _ _ _ _ h, add_right_cancel h, λ _ h, _⟩,
simpa [add_comm a] using h,
end
end ordered_add_comm_group
/-!
### Multiplication and inverse in a field
-/
section linear_ordered_field
variables {k : Type u} [linear_ordered_field k]
@[simp] lemma preimage_mul_const_Iio (a : k) {c : k} (h : 0 < c) :
(λ x, x * c) ⁻¹' (Iio a) = Iio (a / c) :=
ext $ λ x, (lt_div_iff h).symm
@[simp] lemma preimage_mul_const_Ioi (a : k) {c : k} (h : 0 < c) :
(λ x, x * c) ⁻¹' (Ioi a) = Ioi (a / c) :=
ext $ λ x, (div_lt_iff h).symm
@[simp] lemma preimage_mul_const_Iic (a : k) {c : k} (h : 0 < c) :
(λ x, x * c) ⁻¹' (Iic a) = Iic (a / c) :=
ext $ λ x, (le_div_iff h).symm
@[simp] lemma preimage_mul_const_Ici (a : k) {c : k} (h : 0 < c) :
(λ x, x * c) ⁻¹' (Ici a) = Ici (a / c) :=
ext $ λ x, (div_le_iff h).symm
@[simp] lemma preimage_mul_const_Ioo (a b : k) {c : k} (h : 0 < c) :
(λ x, x * c) ⁻¹' (Ioo a b) = Ioo (a / c) (b / c) :=
by simp [← Ioi_inter_Iio, h]
@[simp] lemma preimage_mul_const_Ioc (a b : k) {c : k} (h : 0 < c) :
(λ x, x * c) ⁻¹' (Ioc a b) = Ioc (a / c) (b / c) :=
by simp [← Ioi_inter_Iic, h]
@[simp] lemma preimage_mul_const_Ico (a b : k) {c : k} (h : 0 < c) :
(λ x, x * c) ⁻¹' (Ico a b) = Ico (a / c) (b / c) :=
by simp [← Ici_inter_Iio, h]
@[simp] lemma preimage_mul_const_Icc (a b : k) {c : k} (h : 0 < c) :
(λ x, x * c) ⁻¹' (Icc a b) = Icc (a / c) (b / c) :=
by simp [← Ici_inter_Iic, h]
@[simp] lemma preimage_mul_const_Iio_of_neg (a : k) {c : k} (h : c < 0) :
(λ x, x * c) ⁻¹' (Iio a) = Ioi (a / c) :=
ext $ λ x, (div_lt_iff_of_neg h).symm
@[simp] lemma preimage_mul_const_Ioi_of_neg (a : k) {c : k} (h : c < 0) :
(λ x, x * c) ⁻¹' (Ioi a) = Iio (a / c) :=
ext $ λ x, (lt_div_iff_of_neg h).symm
@[simp] lemma preimage_mul_const_Iic_of_neg (a : k) {c : k} (h : c < 0) :
(λ x, x * c) ⁻¹' (Iic a) = Ici (a / c) :=
ext $ λ x, (div_le_iff_of_neg h).symm
@[simp] lemma preimage_mul_const_Ici_of_neg (a : k) {c : k} (h : c < 0) :
(λ x, x * c) ⁻¹' (Ici a) = Iic (a / c) :=
ext $ λ x, (le_div_iff_of_neg h).symm
@[simp] lemma preimage_mul_const_Ioo_of_neg (a b : k) {c : k} (h : c < 0) :
(λ x, x * c) ⁻¹' (Ioo a b) = Ioo (b / c) (a / c) :=
by simp [← Ioi_inter_Iio, h, inter_comm]
@[simp] lemma preimage_mul_const_Ioc_of_neg (a b : k) {c : k} (h : c < 0) :
(λ x, x * c) ⁻¹' (Ioc a b) = Ico (b / c) (a / c) :=
by simp [← Ioi_inter_Iic, ← Ici_inter_Iio, h, inter_comm]
@[simp] lemma preimage_mul_const_Ico_of_neg (a b : k) {c : k} (h : c < 0) :
(λ x, x * c) ⁻¹' (Ico a b) = Ioc (b / c) (a / c) :=
by simp [← Ici_inter_Iio, ← Ioi_inter_Iic, h, inter_comm]
@[simp] lemma preimage_mul_const_Icc_of_neg (a b : k) {c : k} (h : c < 0) :
(λ x, x * c) ⁻¹' (Icc a b) = Icc (b / c) (a / c) :=
by simp [← Ici_inter_Iic, h, inter_comm]
@[simp] lemma preimage_const_mul_Iio (a : k) {c : k} (h : 0 < c) :
((*) c) ⁻¹' (Iio a) = Iio (a / c) :=
ext $ λ x, (lt_div_iff' h).symm
@[simp] lemma preimage_const_mul_Ioi (a : k) {c : k} (h : 0 < c) :
((*) c) ⁻¹' (Ioi a) = Ioi (a / c) :=
ext $ λ x, (div_lt_iff' h).symm
@[simp] lemma preimage_const_mul_Iic (a : k) {c : k} (h : 0 < c) :
((*) c) ⁻¹' (Iic a) = Iic (a / c) :=
ext $ λ x, (le_div_iff' h).symm
@[simp] lemma preimage_const_mul_Ici (a : k) {c : k} (h : 0 < c) :
((*) c) ⁻¹' (Ici a) = Ici (a / c) :=
ext $ λ x, (div_le_iff' h).symm
@[simp] lemma preimage_const_mul_Ioo (a b : k) {c : k} (h : 0 < c) :
((*) c) ⁻¹' (Ioo a b) = Ioo (a / c) (b / c) :=
by simp [← Ioi_inter_Iio, h]
@[simp] lemma preimage_const_mul_Ioc (a b : k) {c : k} (h : 0 < c) :
((*) c) ⁻¹' (Ioc a b) = Ioc (a / c) (b / c) :=
by simp [← Ioi_inter_Iic, h]
@[simp] lemma preimage_const_mul_Ico (a b : k) {c : k} (h : 0 < c) :
((*) c) ⁻¹' (Ico a b) = Ico (a / c) (b / c) :=
by simp [← Ici_inter_Iio, h]
@[simp] lemma preimage_const_mul_Icc (a b : k) {c : k} (h : 0 < c) :
((*) c) ⁻¹' (Icc a b) = Icc (a / c) (b / c) :=
by simp [← Ici_inter_Iic, h]
@[simp] lemma preimage_const_mul_Iio_of_neg (a : k) {c : k} (h : c < 0) :
((*) c) ⁻¹' (Iio a) = Ioi (a / c) :=
by simpa only [mul_comm] using preimage_mul_const_Iio_of_neg a h
@[simp] lemma preimage_const_mul_Ioi_of_neg (a : k) {c : k} (h : c < 0) :
((*) c) ⁻¹' (Ioi a) = Iio (a / c) :=
by simpa only [mul_comm] using preimage_mul_const_Ioi_of_neg a h
@[simp] lemma preimage_const_mul_Iic_of_neg (a : k) {c : k} (h : c < 0) :
((*) c) ⁻¹' (Iic a) = Ici (a / c) :=
by simpa only [mul_comm] using preimage_mul_const_Iic_of_neg a h
@[simp] lemma preimage_const_mul_Ici_of_neg (a : k) {c : k} (h : c < 0) :
((*) c) ⁻¹' (Ici a) = Iic (a / c) :=
by simpa only [mul_comm] using preimage_mul_const_Ici_of_neg a h
@[simp] lemma preimage_const_mul_Ioo_of_neg (a b : k) {c : k} (h : c < 0) :
((*) c) ⁻¹' (Ioo a b) = Ioo (b / c) (a / c) :=
by simpa only [mul_comm] using preimage_mul_const_Ioo_of_neg a b h
@[simp] lemma preimage_const_mul_Ioc_of_neg (a b : k) {c : k} (h : c < 0) :
((*) c) ⁻¹' (Ioc a b) = Ico (b / c) (a / c) :=
by simpa only [mul_comm] using preimage_mul_const_Ioc_of_neg a b h
@[simp] lemma preimage_const_mul_Ico_of_neg (a b : k) {c : k} (h : c < 0) :
((*) c) ⁻¹' (Ico a b) = Ioc (b / c) (a / c) :=
by simpa only [mul_comm] using preimage_mul_const_Ico_of_neg a b h
@[simp] lemma preimage_const_mul_Icc_of_neg (a b : k) {c : k} (h : c < 0) :
((*) c) ⁻¹' (Icc a b) = Icc (b / c) (a / c) :=
by simpa only [mul_comm] using preimage_mul_const_Icc_of_neg a b h
lemma image_mul_right_Icc' (a b : k) {c : k} (h : 0 < c) :
(λ x, x * c) '' Icc a b = Icc (a * c) (b * c) :=
begin
refine ((units.mk0 c (ne_of_gt h)).mul_right.image_eq_preimage _).trans _,
simp [h, division_def]
end
lemma image_mul_right_Icc {a b c : k} (hab : a ≤ b) (hc : 0 ≤ c) :
(λ x, x * c) '' Icc a b = Icc (a * c) (b * c) :=
begin
cases eq_or_lt_of_le hc,
{ subst c,
simp [(nonempty_Icc.2 hab).image_const] },
exact image_mul_right_Icc' a b ‹0 < c›
end
lemma image_mul_left_Icc' {a : k} (h : 0 < a) (b c : k) :
((*) a) '' Icc b c = Icc (a * b) (a * c) :=
by { convert image_mul_right_Icc' b c h using 1; simp only [mul_comm _ a] }
lemma image_mul_left_Icc {a b c : k} (ha : 0 ≤ a) (hbc : b ≤ c) :
((*) a) '' Icc b c = Icc (a * b) (a * c) :=
by { convert image_mul_right_Icc hbc ha using 1; simp only [mul_comm _ a] }
/-- The image under `inv` of `Ioo 0 a` is `Ioi a⁻¹`. -/
lemma image_inv_Ioo_0_left {a : k} (ha : 0 < a) : has_inv.inv '' Ioo 0 a = Ioi a⁻¹ :=
begin
ext x,
split,
{ rintros ⟨y, ⟨hy0, hya⟩, hyx⟩,
exact hyx ▸ (inv_lt_inv ha hy0).2 hya },
{ exact λ h, ⟨x⁻¹, ⟨inv_pos.2 (lt_trans (inv_pos.2 ha) h),
(inv_lt ha (lt_trans (inv_pos.2 ha) h)).1 h⟩,
inv_inv' x⟩ }
end
/-!
### Images under `x ↦ a * x + b`
-/
@[simp] lemma image_affine_Icc' {a : k} (h : 0 < a) (b c d : k) :
(λ x, a * x + b) '' Icc c d = Icc (a * c + b) (a * d + b) :=
begin
suffices : (λ x, x + b) '' ((λ x, a * x) '' Icc c d) = Icc (a * c + b) (a * d + b),
{ rwa set.image_image at this, },
rw [image_mul_left_Icc' h, image_add_const_Icc],
end
end linear_ordered_field
end set
|
379fc528ed50bdf94e2b69751e13f7435eea661e | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/lake/examples/deps/a/lakefile.lean | 1371c0ce1ea3f36806064cb25d35129e487d54e8 | [
"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 | 103 | lean | import Lake
open System Lake DSL
package a
require root from ".."/"root"
@[default_target] lean_lib A
|
c6e0e04f635e8b21697f4459411bd44c11709792 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/group_theory/group_action/basic.lean | 4a536bbd7f335c236433e9095985a11344d5e432 | [
"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 | 14,085 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import algebra.hom.group_action
import group_theory.group_action.defs
import group_theory.group_action.group
import data.setoid.basic
import data.fintype.card
/-!
# Basic properties of group actions
This file primarily concerns itself with orbits, stabilizers, and other objects defined in terms of
actions. Despite this file being called `basic`, low-level helper lemmas for algebraic manipulation
of `•` belong elsewhere.
## Main definitions
* `mul_action.orbit`
* `mul_action.fixed_points`
* `mul_action.fixed_by`
* `mul_action.stabilizer`
-/
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
open_locale big_operators pointwise
open function
namespace mul_action
variables (α) [monoid α] [mul_action α β]
/-- The orbit of an element under an action. -/
@[to_additive "The orbit of an element under an action."]
def orbit (b : β) := set.range (λ x : α, x • b)
variable {α}
@[to_additive] lemma mem_orbit_iff {b₁ b₂ : β} : b₂ ∈ orbit α b₁ ↔ ∃ x : α, x • b₁ = b₂ :=
iff.rfl
@[simp, to_additive] lemma mem_orbit (b : β) (x : α) : x • b ∈ orbit α b :=
⟨x, rfl⟩
@[simp, to_additive] lemma mem_orbit_self (b : β) : b ∈ orbit α b :=
⟨1, by simp [mul_action.one_smul]⟩
@[to_additive] lemma orbit_nonempty (b : β) : set.nonempty (orbit α b) := set.range_nonempty _
@[to_additive] lemma maps_to_smul_orbit (a : α) (b : β) :
set.maps_to ((•) a) (orbit α b) (orbit α b) :=
set.range_subset_iff.2 $ λ a', ⟨a * a', mul_smul _ _ _⟩
@[to_additive] lemma smul_orbit_subset (a : α) (b : β) : a • orbit α b ⊆ orbit α b :=
(maps_to_smul_orbit a b).image_subset
@[to_additive] lemma orbit_smul_subset (a : α) (b : β) : orbit α (a • b) ⊆ orbit α b :=
set.range_subset_iff.2 $ λ a', mul_smul a' a b ▸ mem_orbit _ _
@[to_additive] instance {b : β} : mul_action α (orbit α b) :=
{ smul := λ a, (maps_to_smul_orbit a b).restrict _ _ _,
one_smul := λ a, subtype.ext (one_smul α a),
mul_smul := λ a a' b', subtype.ext (mul_smul a a' b') }
@[simp, to_additive] lemma orbit.coe_smul {b : β} {a : α} {b' : orbit α b} :
↑(a • b') = a • (b' : β) :=
rfl
variables (α) (β)
/-- The set of elements fixed under the whole action. -/
@[to_additive "The set of elements fixed under the whole action."]
def fixed_points : set β := {b : β | ∀ x : α, x • b = b}
/-- `fixed_by g` is the subfield of elements fixed by `g`. -/
@[to_additive "`fixed_by g` is the subfield of elements fixed by `g`."]
def fixed_by (g : α) : set β :=
{ x | g • x = x }
@[to_additive] theorem fixed_eq_Inter_fixed_by : fixed_points α β = ⋂ g : α, fixed_by α β g :=
set.ext $ λ x, ⟨λ hx, set.mem_Inter.2 $ λ g, hx g, λ hx g, by exact (set.mem_Inter.1 hx g : _)⟩
variables {α} (β)
@[simp, to_additive] lemma mem_fixed_points {b : β} :
b ∈ fixed_points α β ↔ ∀ x : α, x • b = b := iff.rfl
@[simp, to_additive] lemma mem_fixed_by {g : α} {b : β} :
b ∈ fixed_by α β g ↔ g • b = b := iff.rfl
@[to_additive] lemma mem_fixed_points' {b : β} : b ∈ fixed_points α β ↔
(∀ b', b' ∈ orbit α b → b' = b) :=
⟨λ h b h₁, let ⟨x, hx⟩ := mem_orbit_iff.1 h₁ in hx ▸ h x,
λ h b, h _ (mem_orbit _ _)⟩
variables (α) {β}
/-- The stabilizer of a point `b` as a submonoid of `α`. -/
@[to_additive "The stabilizer of a point `b` as an additive submonoid of `α`."]
def stabilizer.submonoid (b : β) : submonoid α :=
{ carrier := { a | a • b = b },
one_mem' := one_smul _ b,
mul_mem' := λ a a' (ha : a • b = b) (hb : a' • b = b),
show (a * a') • b = b, by rw [←smul_smul, hb, ha] }
@[simp, to_additive] lemma mem_stabilizer_submonoid_iff {b : β} {a : α} :
a ∈ stabilizer.submonoid α b ↔ a • b = b := iff.rfl
@[to_additive] lemma orbit_eq_univ [is_pretransitive α β] (x : β) :
orbit α x = set.univ :=
(surjective_smul α x).range_eq
variables {α} {β}
@[to_additive] lemma mem_fixed_points_iff_card_orbit_eq_one {a : β}
[fintype (orbit α a)] : a ∈ fixed_points α β ↔ fintype.card (orbit α a) = 1 :=
begin
rw [fintype.card_eq_one_iff, mem_fixed_points],
split,
{ exact λ h, ⟨⟨a, mem_orbit_self _⟩, λ ⟨b, ⟨x, hx⟩⟩, subtype.eq $ by simp [h x, hx.symm]⟩ },
{ assume h x,
rcases h with ⟨⟨z, hz⟩, hz₁⟩,
calc x • a = z : subtype.mk.inj (hz₁ ⟨x • a, mem_orbit _ _⟩)
... = a : (subtype.mk.inj (hz₁ ⟨a, mem_orbit_self _⟩)).symm }
end
end mul_action
namespace mul_action
variable (α)
variables [group α] [mul_action α β]
/-- The stabilizer of an element under an action, i.e. what sends the element to itself.
A subgroup. -/
@[to_additive "The stabilizer of an element under an action, i.e. what sends the element to itself.
An additive subgroup."]
def stabilizer (b : β) : subgroup α :=
{ inv_mem' := λ a (ha : a • b = b), show a⁻¹ • b = b, by rw [inv_smul_eq_iff, ha]
..stabilizer.submonoid α b }
variables {α} {β}
@[simp, to_additive] lemma mem_stabilizer_iff {b : β} {a : α} :
a ∈ stabilizer α b ↔ a • b = b := iff.rfl
@[simp, to_additive] lemma smul_orbit (a : α) (b : β) :
a • orbit α b = orbit α b :=
(smul_orbit_subset a b).antisymm $
calc orbit α b = a • a⁻¹ • orbit α b : (smul_inv_smul _ _).symm
... ⊆ a • orbit α b : set.image_subset _ (smul_orbit_subset _ _)
@[simp, to_additive] lemma orbit_smul (a : α) (b : β) : orbit α (a • b) = orbit α b :=
(orbit_smul_subset a b).antisymm $
calc orbit α b = orbit α (a⁻¹ • a • b) : by rw inv_smul_smul
... ⊆ orbit α (a • b) : orbit_smul_subset _ _
/-- The action of a group on an orbit is transitive. -/
@[to_additive "The action of an additive group on an orbit is transitive."]
instance (x : β) : is_pretransitive α (orbit α x) :=
⟨by { rintro ⟨_, a, rfl⟩ ⟨_, b, rfl⟩, use b * a⁻¹, ext1, simp [mul_smul] }⟩
@[to_additive] lemma orbit_eq_iff {a b : β} :
orbit α a = orbit α b ↔ a ∈ orbit α b:=
⟨λ h, h ▸ mem_orbit_self _, λ ⟨c, hc⟩, hc ▸ orbit_smul _ _⟩
variables (α) {β}
@[to_additive] lemma mem_orbit_smul (g : α) (a : β) : a ∈ orbit α (g • a) :=
by simp only [orbit_smul, mem_orbit_self]
@[to_additive] lemma smul_mem_orbit_smul (g h : α) (a : β) : g • a ∈ orbit α (h • a) :=
by simp only [orbit_smul, mem_orbit]
variables (α) (β)
/-- The relation 'in the same orbit'. -/
@[to_additive "The relation 'in the same orbit'."]
def orbit_rel : setoid β :=
{ r := λ a b, a ∈ orbit α b,
iseqv := ⟨mem_orbit_self, λ a b, by simp [orbit_eq_iff.symm, eq_comm],
λ a b, by simp [orbit_eq_iff.symm, eq_comm] {contextual := tt}⟩ }
local attribute [instance] orbit_rel
variables {α} {β}
/-- When you take a set `U` in `β`, push it down to the quotient, and pull back, you get the union
of the orbit of `U` under `α`. -/
@[to_additive "When you take a set `U` in `β`, push it down to the quotient, and pull back, you get
the union of the orbit of `U` under `α`."] lemma quotient_preimage_image_eq_union_mul (U : set β) :
quotient.mk ⁻¹' (quotient.mk '' U) = ⋃ a : α, ((•) a) '' U :=
begin
set f : β → quotient (mul_action.orbit_rel α β) := quotient.mk,
ext,
split,
{ rintros ⟨y , hy, hxy⟩,
obtain ⟨a, rfl⟩ := quotient.exact hxy,
rw set.mem_Union,
exact ⟨a⁻¹, a • x, hy, inv_smul_smul a x⟩ },
{ intros hx,
rw set.mem_Union at hx,
obtain ⟨a, u, hu₁, hu₂⟩ := hx,
rw [set.mem_preimage, set.mem_image_iff_bex],
refine ⟨a⁻¹ • x, _, by simp only [quotient.eq]; use a⁻¹⟩,
rw ← hu₂,
convert hu₁,
simp only [inv_smul_smul], },
end
@[to_additive] lemma disjoint_image_image_iff {U V : set β} :
disjoint (quotient.mk '' U) (quotient.mk '' V) ↔ ∀ x ∈ U, ∀ a : α, a • x ∉ V :=
begin
set f : β → quotient (mul_action.orbit_rel α β) := quotient.mk,
refine ⟨λ h x x_in_U a a_in_V, h ⟨⟨x, x_in_U, quotient.sound ⟨a⁻¹, _⟩⟩, ⟨a • x, a_in_V, rfl⟩⟩, _⟩,
{ simp },
{ rintro h x ⟨⟨y, hy₁, hy₂⟩, ⟨z, hz₁, hz₂⟩⟩,
obtain ⟨a, rfl⟩ := quotient.exact (hz₂.trans hy₂.symm),
exact h y hy₁ a hz₁ }
end
@[to_additive]
lemma image_inter_image_iff (U V : set β) :
(quotient.mk '' U) ∩ (quotient.mk '' V) = ∅ ↔ ∀ x ∈ U, ∀ a : α, a • x ∉ V :=
set.disjoint_iff_inter_eq_empty.symm.trans disjoint_image_image_iff
variables (α β)
/-- The quotient by `mul_action.orbit_rel`, given a name to enable dot notation. -/
@[reducible, to_additive "The quotient by `add_action.orbit_rel`, given a name to enable dot
notation."]
def orbit_rel.quotient : Type* := quotient $ orbit_rel α β
variables {α β}
/-- The orbit corresponding to an element of the quotient by `mul_action.orbit_rel` -/
@[to_additive "The orbit corresponding to an element of the quotient by `add_action.orbit_rel`"]
def orbit_rel.quotient.orbit (x : orbit_rel.quotient α β) : set β :=
quotient.lift_on' x (orbit α) $ λ _ _, mul_action.orbit_eq_iff.2
@[simp, to_additive]
lemma orbit_rel.quotient.orbit_mk (b : β) :
orbit_rel.quotient.orbit (quotient.mk' b : orbit_rel.quotient α β) = orbit α b := rfl
@[to_additive]
lemma orbit_rel.quotient.mem_orbit {b : β} {x : orbit_rel.quotient α β} :
b ∈ x.orbit ↔ quotient.mk' b = x :=
by { induction x using quotient.induction_on', rw quotient.eq', refl }
/-- Note that `hφ = quotient.out_eq'` is a useful choice here. -/
@[to_additive "Note that `hφ = quotient.out_eq'` is a useful choice here."]
lemma orbit_rel.quotient.orbit_eq_orbit_out (x : orbit_rel.quotient α β)
{φ : orbit_rel.quotient α β → β} (hφ : right_inverse φ quotient.mk') :
orbit_rel.quotient.orbit x = orbit α (φ x) :=
begin
conv_lhs { rw ←hφ x },
induction x using quotient.induction_on',
refl,
end
variables (α) (β)
local notation `Ω` := orbit_rel.quotient α β
/-- Decomposition of a type `X` as a disjoint union of its orbits under a group action.
This version is expressed in terms of `mul_action.orbit_rel.quotient.orbit` instead of
`mul_action.orbit`, to avoid mentioning `quotient.out'`. -/
@[to_additive "Decomposition of a type `X` as a disjoint union of its orbits under an additive group
action.
This version is expressed in terms of `add_action.orbit_rel.quotient.orbit` instead of
`add_action.orbit`, to avoid mentioning `quotient.out'`. "]
def self_equiv_sigma_orbits' : β ≃ Σ ω : Ω, ω.orbit :=
calc β
≃ Σ (ω : Ω), {b // quotient.mk' b = ω} : (equiv.sigma_fiber_equiv quotient.mk').symm
... ≃ Σ (ω : Ω), ω.orbit :
equiv.sigma_congr_right $ λ ω, equiv.subtype_equiv_right $ λ x,
orbit_rel.quotient.mem_orbit.symm
/-- Decomposition of a type `X` as a disjoint union of its orbits under a group action. -/
@[to_additive "Decomposition of a type `X` as a disjoint union of its orbits under an additive group
action."]
def self_equiv_sigma_orbits : β ≃ Σ (ω : Ω), orbit α ω.out' :=
(self_equiv_sigma_orbits' α β).trans $ equiv.sigma_congr_right $ λ i,
equiv.set.of_eq $ orbit_rel.quotient.orbit_eq_orbit_out _ quotient.out_eq'
variables {α β}
/-- If the stabilizer of `x` is `S`, then the stabilizer of `g • x` is `gSg⁻¹`. -/
lemma stabilizer_smul_eq_stabilizer_map_conj (g : α) (x : β) :
(stabilizer α (g • x) = (stabilizer α x).map (mul_aut.conj g).to_monoid_hom) :=
begin
ext h,
rw [mem_stabilizer_iff, ← smul_left_cancel_iff g⁻¹, smul_smul, smul_smul, smul_smul, mul_left_inv,
one_smul, ← mem_stabilizer_iff, subgroup.mem_map_equiv, mul_aut.conj_symm_apply]
end
/-- A bijection between the stabilizers of two elements in the same orbit. -/
noncomputable def stabilizer_equiv_stabilizer_of_orbit_rel {x y : β} (h : (orbit_rel α β).rel x y) :
stabilizer α x ≃* stabilizer α y :=
let g : α := classical.some h in
have hg : g • y = x := classical.some_spec h,
have this : stabilizer α x = (stabilizer α y).map (mul_aut.conj g).to_monoid_hom,
by rw [← hg, stabilizer_smul_eq_stabilizer_map_conj],
(mul_equiv.subgroup_congr this).trans ((mul_aut.conj g).subgroup_map $ stabilizer α y).symm
end mul_action
namespace add_action
variables [add_group α] [add_action α β]
/-- If the stabilizer of `x` is `S`, then the stabilizer of `g +ᵥ x` is `g + S + (-g)`. -/
lemma stabilizer_vadd_eq_stabilizer_map_conj (g : α) (x : β) :
(stabilizer α (g +ᵥ x) = (stabilizer α x).map (add_aut.conj g).to_add_monoid_hom) :=
begin
ext h,
rw [mem_stabilizer_iff, ← vadd_left_cancel_iff (-g) , vadd_vadd, vadd_vadd, vadd_vadd,
add_left_neg, zero_vadd, ← mem_stabilizer_iff, add_subgroup.mem_map_equiv,
add_aut.conj_symm_apply]
end
/-- A bijection between the stabilizers of two elements in the same orbit. -/
noncomputable def stabilizer_equiv_stabilizer_of_orbit_rel {x y : β}
(h : (orbit_rel α β).rel x y) :
stabilizer α x ≃+ stabilizer α y :=
let g : α := classical.some h in
have hg : g +ᵥ y = x := classical.some_spec h,
have this : stabilizer α x = (stabilizer α y).map (add_aut.conj g).to_add_monoid_hom,
by rw [← hg, stabilizer_vadd_eq_stabilizer_map_conj],
(add_equiv.add_subgroup_congr this).trans
((add_aut.conj g).add_subgroup_map $ stabilizer α y).symm
end add_action
/-- `smul` by a `k : M` over a ring is injective, if `k` is not a zero divisor.
The general theory of such `k` is elaborated by `is_smul_regular`.
The typeclass that restricts all terms of `M` to have this property is `no_zero_smul_divisors`. -/
lemma smul_cancel_of_non_zero_divisor {M R : Type*}
[monoid M] [non_unital_non_assoc_ring R] [distrib_mul_action M R]
(k : M) (h : ∀ (x : R), k • x = 0 → x = 0) {a b : R} (h' : k • a = k • b) :
a = b :=
begin
rw ←sub_eq_zero,
refine h _ _,
rw [smul_sub, h', sub_self]
end
|
d2c824fe3d1ba3e9be23caf821930f05ecfd434f | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/data/option/basic.lean | 191596d0ea473ec67d95413daf62511232292983 | [
"Apache-2.0"
] | permissive | fpvandoorn/mathlib | b21ab4068db079cbb8590b58fda9cc4bc1f35df4 | b3433a51ea8bc07c4159c1073838fc0ee9b8f227 | refs/heads/master | 1,624,791,089,608 | 1,556,715,231,000 | 1,556,715,231,000 | 165,722,980 | 5 | 0 | Apache-2.0 | 1,552,657,455,000 | 1,547,494,646,000 | Lean | UTF-8 | Lean | false | false | 4,742 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import logic.basic data.bool data.option.defs tactic.ext tactic.simpa
namespace option
variables {α : Type*} {β : Type*}
@[simp] theorem get_mem : ∀ {o : option α} (h : is_some o), option.get h ∈ o
| (some a) _ := rfl
theorem get_of_mem {a : α} : ∀ {o : option α} (h : is_some o), a ∈ o → option.get h = a
| _ _ rfl := rfl
theorem mem_unique {o : option α} {a b : α} (ha : a ∈ o) (hb : b ∈ o) : a = b :=
option.some.inj $ ha.symm.trans hb
theorem injective_some (α : Type*) : function.injective (@some α) :=
λ _ _, some_inj.mp
@[extensionality] theorem ext : ∀ {o₁ o₂ : option α}, (∀ a, a ∈ o₁ ↔ a ∈ o₂) → o₁ = o₂
| none none H := rfl
| (some a) o H := ((H _).1 rfl).symm
| o (some b) H := (H _).2 rfl
theorem eq_none_iff_forall_not_mem {o : option α} :
o = none ↔ (∀ a, a ∉ o) :=
⟨λ e a h, by rw e at h; cases h, λ h, ext $ by simpa⟩
@[simp] theorem none_bind {α β} (f : α → option β) : none >>= f = none := rfl
@[simp] theorem some_bind {α β} (a : α) (f : α → option β) : some a >>= f = f a := rfl
@[simp] theorem none_bind' (f : α → option β) : none.bind f = none := rfl
@[simp] theorem some_bind' (a : α) (f : α → option β) : (some a).bind f = f a := rfl
@[simp] theorem bind_some : ∀ x : option α, x >>= some = x :=
@bind_pure α option _ _
@[simp] theorem bind_eq_some {α β} {x : option α} {f : α → option β} {b : β} : x >>= f = some b ↔ ∃ a, x = some a ∧ f a = some b :=
by cases x; simp
@[simp] theorem bind_eq_some' {x : option α} {f : α → option β} {b : β} : x.bind f = some b ↔ ∃ a, x = some a ∧ f a = some b :=
by cases x; simp
lemma bind_comm {α β γ} {f : α → β → option γ} (a : option α) (b : option β) :
a.bind (λx, b.bind (f x)) = b.bind (λy, a.bind (λx, f x y)) :=
by cases a; cases b; refl
@[simp] theorem map_none {α β} {f : α → β} : f <$> none = none := rfl
@[simp] theorem map_some {α β} {a : α} {f : α → β} : f <$> some a = some (f a) := rfl
@[simp] theorem map_none' {f : α → β} : option.map f none = none := rfl
@[simp] theorem map_some' {a : α} {f : α → β} : option.map f (some a) = some (f a) := rfl
@[simp] theorem map_eq_some {α β} {x : option α} {f : α → β} {b : β} : f <$> x = some b ↔ ∃ a, x = some a ∧ f a = b :=
by cases x; simp
@[simp] theorem map_eq_some' {x : option α} {f : α → β} {b : β} : x.map f = some b ↔ ∃ a, x = some a ∧ f a = b :=
by cases x; simp
@[simp] theorem map_id' : option.map (@id α) = id := map_id
@[simp] theorem seq_some {α β} {a : α} {f : α → β} : some f <*> some a = some (f a) := rfl
@[simp] theorem some_orelse' (a : α) (x : option α) : (some a).orelse x = some a := rfl
@[simp] theorem some_orelse (a : α) (x : option α) : (some a <|> x) = some a := rfl
@[simp] theorem none_orelse' (x : option α) : none.orelse x = x :=
by cases x; refl
@[simp] theorem none_orelse (x : option α) : (none <|> x) = x := none_orelse' x
@[simp] theorem orelse_none' (x : option α) : x.orelse none = x :=
by cases x; refl
@[simp] theorem orelse_none (x : option α) : (x <|> none) = x := orelse_none' x
@[simp] theorem is_some_none : @is_some α none = ff := rfl
@[simp] theorem is_some_some {a : α} : is_some (some a) = tt := rfl
theorem is_some_iff_exists {x : option α} : is_some x ↔ ∃ a, x = some a :=
by cases x; simp [is_some]; exact ⟨_, rfl⟩
@[simp] theorem is_none_none : @is_none α none = tt := rfl
@[simp] theorem is_none_some {a : α} : is_none (some a) = ff := rfl
@[simp] theorem not_is_some {a : option α} : is_some a = ff ↔ a.is_none = tt :=
by cases a; simp
theorem iget_mem [inhabited α] : ∀ {o : option α}, is_some o → o.iget ∈ o
| (some a) _ := rfl
theorem iget_of_mem [inhabited α] {a : α} : ∀ {o : option α}, a ∈ o → o.iget = a
| _ rfl := rfl
@[simp] theorem guard_eq_some {p : α → Prop} [decidable_pred p] {a b : α} :
guard p a = some b ↔ a = b ∧ p a :=
by by_cases p a; simp [option.guard, h]; intro; contradiction
@[simp] theorem guard_eq_some' {p : Prop} [decidable p] :
∀ u, _root_.guard p = some u ↔ p
| () := by by_cases p; simp [guard, h, pure]; intro; contradiction
theorem lift_or_get_choice {f : α → α → α} (h : ∀ a b, f a b = a ∨ f a b = b) :
∀ o₁ o₂, lift_or_get f o₁ o₂ = o₁ ∨ lift_or_get f o₁ o₂ = o₂
| none none := or.inl rfl
| (some a) none := or.inl rfl
| none (some b) := or.inr rfl
| (some a) (some b) := by simpa [lift_or_get] using h a b
end option
|
b5a19c3d10a4073362924240c9054e18121e2b5a | 4d3f29a7b2eff44af8fd0d3176232e039acb9ee3 | /LAMR/Examples/using_lean/examples3.lean | 4249880c3531396ee7a6d640413d9d0a616e80e9 | [] | no_license | marijnheule/lamr | 5fc5d69d326ff92e321242cfd7f72e78d7f99d7e | 28cc4114c7361059bb54f407fa312bf38b48728b | refs/heads/main | 1,689,338,013,620 | 1,630,359,632,000 | 1,630,359,632,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 584 | lean | -- textbook: BinTree
import Init
inductive BinTree
| empty : BinTree
| node : BinTree → BinTree → BinTree
open BinTree
-- end textbook: BinTree
-- textbook: recursion on BinTree
def size : BinTree → Nat
| empty => 0
| node a b => 1 + size a + size b
def depth : BinTree → Nat
| empty => 0
| node a b => 1 + Nat.max (depth a) (depth b)
def example_tree := node (node empty empty) (node empty (node empty empty))
#eval size example_tree
#eval depth example_tree
-- end textbook: recursion on BinTree
-- textbook: List
#print List
-- end textbook: List
|
ba337794b41d870937a2e12f50c1cc5dae24109f | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/analysis/calculus/tangent_cone.lean | c9c5a5b788ba5845a7166e8a42c4cef997f5bf40 | [
"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 | 18,798 | 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 analysis.convex.basic
import analysis.normed_space.bounded_linear_maps
import analysis.specific_limits
/-!
# Tangent cone
In this file, we define two predicates `unique_diff_within_at 𝕜 s x` and `unique_diff_on 𝕜 s`
ensuring that, if a function has two derivatives, then they have to coincide. As a direct
definition of this fact (quantifying on all target types and all functions) would depend on
universes, we use a more intrinsic definition: if all the possible tangent directions to the set
`s` at the point `x` span a dense subset of the whole subset, it is easy to check that the
derivative has to be unique.
Therefore, we introduce the set of all tangent directions, named `tangent_cone_at`,
and express `unique_diff_within_at` and `unique_diff_on` in terms of it.
One should however think of this definition as an implementation detail: the only reason to
introduce the predicates `unique_diff_within_at` and `unique_diff_on` is to ensure the uniqueness
of the derivative. This is why their names reflect their uses, and not how they are defined.
## Implementation details
Note that this file is imported by `fderiv.lean`. Hence, derivatives are not defined yet. The
property of uniqueness of the derivative is therefore proved in `fderiv.lean`, but based on the
properties of the tangent cone we prove here.
-/
variables (𝕜 : Type*) [nondiscrete_normed_field 𝕜]
open filter set
open_locale topological_space
section tangent_cone
variables {E : Type*} [add_comm_monoid E] [module 𝕜 E] [topological_space E]
/-- The set of all tangent directions to the set `s` at the point `x`. -/
def tangent_cone_at (s : set E) (x : E) : set E :=
{y : E | ∃(c : ℕ → 𝕜) (d : ℕ → E), (∀ᶠ n in at_top, x + d n ∈ s) ∧
(tendsto (λn, ∥c n∥) at_top at_top) ∧ (tendsto (λn, c n • d n) at_top (𝓝 y))}
/-- A property ensuring that the tangent cone to `s` at `x` spans a dense subset of the whole space.
The main role of this property is to ensure that the differential within `s` at `x` is unique,
hence this name. The uniqueness it asserts is proved in `unique_diff_within_at.eq` in `fderiv.lean`.
To avoid pathologies in dimension 0, we also require that `x` belongs to the closure of `s` (which
is automatic when `E` is not `0`-dimensional).
-/
@[mk_iff] structure unique_diff_within_at (s : set E) (x : E) : Prop :=
(dense_tangent_cone : dense ((submodule.span 𝕜 (tangent_cone_at 𝕜 s x)) : set E))
(mem_closure : x ∈ closure s)
/-- A property ensuring that the tangent cone to `s` at any of its points spans a dense subset of
the whole space. The main role of this property is to ensure that the differential along `s` is
unique, hence this name. The uniqueness it asserts is proved in `unique_diff_on.eq` in
`fderiv.lean`. -/
def unique_diff_on (s : set E) : Prop :=
∀x ∈ s, unique_diff_within_at 𝕜 s x
end tangent_cone
variables {E : Type*} [normed_group E] [normed_space 𝕜 E]
variables {F : Type*} [normed_group F] [normed_space 𝕜 F]
variables {G : Type*} [normed_group G] [normed_space ℝ G]
variables {𝕜} {x y : E} {s t : set E}
section tangent_cone
/- This section is devoted to the properties of the tangent cone. -/
open normed_field
lemma tangent_cone_univ : tangent_cone_at 𝕜 univ x = univ :=
begin
refine univ_subset_iff.1 (λy hy, _),
rcases exists_one_lt_norm 𝕜 with ⟨w, hw⟩,
refine ⟨λn, w^n, λn, (w^n)⁻¹ • y, univ_mem_sets' (λn, mem_univ _), _, _⟩,
{ simp only [norm_pow],
exact tendsto_pow_at_top_at_top_of_one_lt hw },
{ convert tendsto_const_nhds,
ext n,
have : w ^ n * (w ^ n)⁻¹ = 1,
{ apply mul_inv_cancel,
apply pow_ne_zero,
simpa [norm_eq_zero] using (ne_of_lt (lt_trans zero_lt_one hw)).symm },
rw [smul_smul, this, one_smul] }
end
lemma tangent_cone_mono (h : s ⊆ t) :
tangent_cone_at 𝕜 s x ⊆ tangent_cone_at 𝕜 t x :=
begin
rintros y ⟨c, d, ds, ctop, clim⟩,
exact ⟨c, d, mem_sets_of_superset ds (λn hn, h hn), ctop, clim⟩
end
/-- Auxiliary lemma ensuring that, under the assumptions defining the tangent cone,
the sequence `d` tends to 0 at infinity. -/
lemma tangent_cone_at.lim_zero {α : Type*} (l : filter α) {c : α → 𝕜} {d : α → E}
(hc : tendsto (λn, ∥c n∥) l at_top) (hd : tendsto (λn, c n • d n) l (𝓝 y)) :
tendsto d l (𝓝 0) :=
begin
have A : tendsto (λn, ∥c n∥⁻¹) l (𝓝 0) := tendsto_inv_at_top_zero.comp hc,
have B : tendsto (λn, ∥c n • d n∥) l (𝓝 ∥y∥) :=
(continuous_norm.tendsto _).comp hd,
have C : tendsto (λn, ∥c n∥⁻¹ * ∥c n • d n∥) l (𝓝 (0 * ∥y∥)) := A.mul B,
rw zero_mul at C,
have : ∀ᶠ n in l, ∥c n∥⁻¹ * ∥c n • d n∥ = ∥d n∥,
{ apply (eventually_ne_of_tendsto_norm_at_top hc 0).mono (λn hn, _),
rw [norm_smul, ← mul_assoc, inv_mul_cancel, one_mul],
rwa [ne.def, norm_eq_zero] },
have D : tendsto (λ n, ∥d n∥) l (𝓝 0) :=
tendsto.congr' this C,
rw tendsto_zero_iff_norm_tendsto_zero,
exact D
end
lemma tangent_cone_mono_nhds (h : 𝓝[s] x ≤ 𝓝[t] x) :
tangent_cone_at 𝕜 s x ⊆ tangent_cone_at 𝕜 t x :=
begin
rintros y ⟨c, d, ds, ctop, clim⟩,
refine ⟨c, d, _, ctop, clim⟩,
suffices : tendsto (λ n, x + d n) at_top (𝓝[t] x),
from tendsto_principal.1 (tendsto_inf.1 this).2,
refine (tendsto_inf.2 ⟨_, tendsto_principal.2 ds⟩).mono_right h,
simpa only [add_zero] using tendsto_const_nhds.add (tangent_cone_at.lim_zero at_top ctop clim)
end
/-- Tangent cone of `s` at `x` depends only on `𝓝[s] x`. -/
lemma tangent_cone_congr (h : 𝓝[s] x = 𝓝[t] x) :
tangent_cone_at 𝕜 s x = tangent_cone_at 𝕜 t x :=
subset.antisymm
(tangent_cone_mono_nhds $ le_of_eq h)
(tangent_cone_mono_nhds $ le_of_eq h.symm)
/-- Intersecting with a neighborhood of the point does not change the tangent cone. -/
lemma tangent_cone_inter_nhds (ht : t ∈ 𝓝 x) :
tangent_cone_at 𝕜 (s ∩ t) x = tangent_cone_at 𝕜 s x :=
tangent_cone_congr (nhds_within_restrict' _ ht).symm
/-- The tangent cone of a product contains the tangent cone of its left factor. -/
lemma subset_tangent_cone_prod_left {t : set F} {y : F} (ht : y ∈ closure t) :
linear_map.inl 𝕜 E F '' (tangent_cone_at 𝕜 s x) ⊆ tangent_cone_at 𝕜 (set.prod s t) (x, y) :=
begin
rintros _ ⟨v, ⟨c, d, hd, hc, hy⟩, rfl⟩,
have : ∀n, ∃d', y + d' ∈ t ∧ ∥c n • d'∥ < ((1:ℝ)/2)^n,
{ assume n,
rcases mem_closure_iff_nhds.1 ht _ (eventually_nhds_norm_smul_sub_lt (c n) y
(pow_pos one_half_pos n)) with ⟨z, hz, hzt⟩,
exact ⟨z - y, by simpa using hzt, by simpa using hz⟩ },
choose d' hd' using this,
refine ⟨c, λn, (d n, d' n), _, hc, _⟩,
show ∀ᶠ n in at_top, (x, y) + (d n, d' n) ∈ set.prod s t,
{ filter_upwards [hd],
assume n hn,
simp [hn, (hd' n).1] },
{ apply tendsto.prod_mk_nhds hy _,
refine squeeze_zero_norm (λn, (hd' n).2.le) _,
exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one }
end
/-- The tangent cone of a product contains the tangent cone of its right factor. -/
lemma subset_tangent_cone_prod_right {t : set F} {y : F}
(hs : x ∈ closure s) :
linear_map.inr 𝕜 E F '' (tangent_cone_at 𝕜 t y) ⊆ tangent_cone_at 𝕜 (set.prod s t) (x, y) :=
begin
rintros _ ⟨w, ⟨c, d, hd, hc, hy⟩, rfl⟩,
have : ∀n, ∃d', x + d' ∈ s ∧ ∥c n • d'∥ < ((1:ℝ)/2)^n,
{ assume n,
rcases mem_closure_iff_nhds.1 hs _ (eventually_nhds_norm_smul_sub_lt (c n) x
(pow_pos one_half_pos n)) with ⟨z, hz, hzs⟩,
exact ⟨z - x, by simpa using hzs, by simpa using hz⟩ },
choose d' hd' using this,
refine ⟨c, λn, (d' n, d n), _, hc, _⟩,
show ∀ᶠ n in at_top, (x, y) + (d' n, d n) ∈ set.prod s t,
{ filter_upwards [hd],
assume n hn,
simp [hn, (hd' n).1] },
{ apply tendsto.prod_mk_nhds _ hy,
refine squeeze_zero_norm (λn, (hd' n).2.le) _,
exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one }
end
/-- The tangent cone of a product contains the tangent cone of each factor. -/
lemma maps_to_tangent_cone_pi {ι : Type*} [decidable_eq ι] {E : ι → Type*}
[Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)]
{s : Π i, set (E i)} {x : Π i, E i} {i : ι} (hi : ∀ j ≠ i, x j ∈ closure (s j)) :
maps_to (linear_map.single i : E i →ₗ[𝕜] Π j, E j) (tangent_cone_at 𝕜 (s i) (x i))
(tangent_cone_at 𝕜 (set.pi univ s) x) :=
begin
rintros w ⟨c, d, hd, hc, hy⟩,
have : ∀ n (j ≠ i), ∃ d', x j + d' ∈ s j ∧ ∥c n • d'∥ < (1 / 2 : ℝ) ^ n,
{ assume n j hj,
rcases mem_closure_iff_nhds.1 (hi j hj) _ (eventually_nhds_norm_smul_sub_lt (c n) (x j)
(pow_pos one_half_pos n)) with ⟨z, hz, hzs⟩,
exact ⟨z - x j, by simpa using hzs, by simpa using hz⟩ },
choose! d' hd's hcd',
refine ⟨c, λ n, function.update (d' n) i (d n), hd.mono (λ n hn j hj', _), hc,
tendsto_pi.2 $ λ j, _⟩,
{ rcases em (j = i) with rfl|hj; simp * },
{ rcases em (j = i) with rfl|hj,
{ simp [hy] },
{ suffices : tendsto (λ n, c n • d' n j) at_top (𝓝 0), by simpa [hj],
refine squeeze_zero_norm (λ n, (hcd' n j hj).le) _,
exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one } }
end
/-- If a subset of a real vector space contains a segment, then the direction of this
segment belongs to the tangent cone at its endpoints. -/
lemma mem_tangent_cone_of_segment_subset {s : set G} {x y : G} (h : segment x y ⊆ s) :
y - x ∈ tangent_cone_at ℝ s x :=
begin
let c := λn:ℕ, (2:ℝ)^n,
let d := λn:ℕ, (c n)⁻¹ • (y-x),
refine ⟨c, d, filter.univ_mem_sets' (λn, h _), _, _⟩,
show x + d n ∈ segment x y,
{ rw segment_eq_image,
refine ⟨(c n)⁻¹, ⟨_, _⟩, _⟩,
{ rw inv_nonneg, apply pow_nonneg, norm_num },
{ apply inv_le_one, apply one_le_pow_of_one_le, norm_num },
{ simp only [d, sub_smul, smul_sub, one_smul], abel } },
show filter.tendsto (λ (n : ℕ), ∥c n∥) filter.at_top filter.at_top,
{ have : (λ (n : ℕ), ∥c n∥) = c,
by { ext n, exact abs_of_nonneg (pow_nonneg (by norm_num) _) },
rw this,
exact tendsto_pow_at_top_at_top_of_one_lt (by norm_num) },
show filter.tendsto (λ (n : ℕ), c n • d n) filter.at_top (𝓝 (y - x)),
{ have : (λ (n : ℕ), c n • d n) = (λn, y - x),
{ ext n,
simp only [d, smul_smul],
rw [mul_inv_cancel, one_smul],
exact pow_ne_zero _ (by norm_num) },
rw this,
apply tendsto_const_nhds }
end
end tangent_cone
section unique_diff
/-!
### Properties of `unique_diff_within_at` and `unique_diff_on`
This section is devoted to properties of the predicates
`unique_diff_within_at` and `unique_diff_on`. -/
lemma unique_diff_on.unique_diff_within_at {s : set E} {x} (hs : unique_diff_on 𝕜 s) (h : x ∈ s) :
unique_diff_within_at 𝕜 s x :=
hs x h
lemma unique_diff_within_at_univ : unique_diff_within_at 𝕜 univ x :=
by { rw [unique_diff_within_at_iff, tangent_cone_univ], simp }
lemma unique_diff_on_univ : unique_diff_on 𝕜 (univ : set E) :=
λx hx, unique_diff_within_at_univ
lemma unique_diff_on_empty : unique_diff_on 𝕜 (∅ : set E) :=
λ x hx, hx.elim
lemma unique_diff_within_at.mono_nhds (h : unique_diff_within_at 𝕜 s x)
(st : 𝓝[s] x ≤ 𝓝[t] x) :
unique_diff_within_at 𝕜 t x :=
begin
simp only [unique_diff_within_at_iff] at *,
rw [mem_closure_iff_nhds_within_ne_bot] at h ⊢,
exact ⟨h.1.mono $ submodule.span_mono $ tangent_cone_mono_nhds st,
h.2.mono st⟩
end
lemma unique_diff_within_at.mono (h : unique_diff_within_at 𝕜 s x) (st : s ⊆ t) :
unique_diff_within_at 𝕜 t x :=
h.mono_nhds $ nhds_within_mono _ st
lemma unique_diff_within_at_congr (st : 𝓝[s] x = 𝓝[t] x) :
unique_diff_within_at 𝕜 s x ↔ unique_diff_within_at 𝕜 t x :=
⟨λ h, h.mono_nhds $ le_of_eq st, λ h, h.mono_nhds $ le_of_eq st.symm⟩
lemma unique_diff_within_at_inter (ht : t ∈ 𝓝 x) :
unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x :=
unique_diff_within_at_congr $ (nhds_within_restrict' _ ht).symm
lemma unique_diff_within_at.inter (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ 𝓝 x) :
unique_diff_within_at 𝕜 (s ∩ t) x :=
(unique_diff_within_at_inter ht).2 hs
lemma unique_diff_within_at_inter' (ht : t ∈ 𝓝[s] x) :
unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x :=
unique_diff_within_at_congr $ (nhds_within_restrict'' _ ht).symm
lemma unique_diff_within_at.inter' (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ 𝓝[s] x) :
unique_diff_within_at 𝕜 (s ∩ t) x :=
(unique_diff_within_at_inter' ht).2 hs
lemma unique_diff_within_at_of_mem_nhds (h : s ∈ 𝓝 x) : unique_diff_within_at 𝕜 s x :=
by simpa only [univ_inter] using unique_diff_within_at_univ.inter h
lemma is_open.unique_diff_within_at (hs : is_open s) (xs : x ∈ s) : unique_diff_within_at 𝕜 s x :=
unique_diff_within_at_of_mem_nhds (is_open.mem_nhds hs xs)
lemma unique_diff_on.inter (hs : unique_diff_on 𝕜 s) (ht : is_open t) : unique_diff_on 𝕜 (s ∩ t) :=
λx hx, (hs x hx.1).inter (is_open.mem_nhds ht hx.2)
lemma is_open.unique_diff_on (hs : is_open s) : unique_diff_on 𝕜 s :=
λx hx, is_open.unique_diff_within_at hs hx
/-- The product of two sets of unique differentiability at points `x` and `y` has unique
differentiability at `(x, y)`. -/
lemma unique_diff_within_at.prod {t : set F} {y : F}
(hs : unique_diff_within_at 𝕜 s x) (ht : unique_diff_within_at 𝕜 t y) :
unique_diff_within_at 𝕜 (set.prod s t) (x, y) :=
begin
rw [unique_diff_within_at_iff] at ⊢ hs ht,
rw [closure_prod_eq],
refine ⟨_, hs.2, ht.2⟩,
have : _ ≤ submodule.span 𝕜 (tangent_cone_at 𝕜 (s.prod t) (x, y)) :=
submodule.span_mono (union_subset (subset_tangent_cone_prod_left ht.2)
(subset_tangent_cone_prod_right hs.2)),
rw [linear_map.span_inl_union_inr, set_like.le_def] at this,
exact (hs.1.prod ht.1).mono this
end
lemma unique_diff_within_at.univ_pi (ι : Type*) [fintype ι] (E : ι → Type*)
[Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)]
(s : Π i, set (E i)) (x : Π i, E i) (h : ∀ i, unique_diff_within_at 𝕜 (s i) (x i)) :
unique_diff_within_at 𝕜 (set.pi univ s) x :=
begin
classical,
simp only [unique_diff_within_at_iff, closure_pi_set] at h ⊢,
refine ⟨(dense_pi univ (λ i _, (h i).1)).mono _, λ i _, (h i).2⟩,
norm_cast,
simp only [← submodule.supr_map_single, supr_le_iff, linear_map.map_span, submodule.span_le,
← maps_to'],
exact λ i, (maps_to_tangent_cone_pi $ λ j hj, (h j).2).mono subset.rfl submodule.subset_span
end
lemma unique_diff_within_at.pi (ι : Type*) [fintype ι] (E : ι → Type*)
[Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)]
(s : Π i, set (E i)) (x : Π i, E i) (I : set ι)
(h : ∀ i ∈ I, unique_diff_within_at 𝕜 (s i) (x i)) :
unique_diff_within_at 𝕜 (set.pi I s) x :=
begin
classical,
rw [← set.univ_pi_piecewise],
refine unique_diff_within_at.univ_pi _ _ _ _ (λ i, _),
by_cases hi : i ∈ I; simp [*, unique_diff_within_at_univ],
end
/-- The product of two sets of unique differentiability is a set of unique differentiability. -/
lemma unique_diff_on.prod {t : set F} (hs : unique_diff_on 𝕜 s) (ht : unique_diff_on 𝕜 t) :
unique_diff_on 𝕜 (set.prod s t) :=
λ ⟨x, y⟩ h, unique_diff_within_at.prod (hs x h.1) (ht y h.2)
/-- The finite product of a family of sets of unique differentiability is a set of unique
differentiability. -/
lemma unique_diff_on.pi (ι : Type*) [fintype ι] (E : ι → Type*)
[Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)]
(s : Π i, set (E i)) (I : set ι) (h : ∀ i ∈ I, unique_diff_on 𝕜 (s i)) :
unique_diff_on 𝕜 (set.pi I s) :=
λ x hx, unique_diff_within_at.pi _ _ _ _ _ $ λ i hi, h i hi (x i) (hx i hi)
/-- The finite product of a family of sets of unique differentiability is a set of unique
differentiability. -/
lemma unique_diff_on.univ_pi (ι : Type*) [fintype ι] (E : ι → Type*)
[Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)]
(s : Π i, set (E i)) (h : ∀ i, unique_diff_on 𝕜 (s i)) :
unique_diff_on 𝕜 (set.pi univ s) :=
unique_diff_on.pi _ _ _ _ $ λ i _, h i
/-- In a real vector space, a convex set with nonempty interior is a set of unique
differentiability. -/
theorem unique_diff_on_convex {s : set G} (conv : convex s) (hs : (interior s).nonempty) :
unique_diff_on ℝ s :=
begin
assume x xs,
rcases hs with ⟨y, hy⟩,
suffices : y - x ∈ interior (tangent_cone_at ℝ s x),
{ refine ⟨dense.of_closure _, subset_closure xs⟩,
simp [(submodule.span ℝ (tangent_cone_at ℝ s x)).eq_top_of_nonempty_interior'
⟨y - x, interior_mono submodule.subset_span this⟩] },
rw [mem_interior_iff_mem_nhds] at hy ⊢,
apply mem_sets_of_superset ((is_open_map_sub_right x).image_mem_nhds hy),
rintros _ ⟨z, zs, rfl⟩,
exact mem_tangent_cone_of_segment_subset (conv.segment_subset xs zs)
end
lemma unique_diff_on_Ici (a : ℝ) : unique_diff_on ℝ (Ici a) :=
unique_diff_on_convex (convex_Ici a) $ by simp only [interior_Ici, nonempty_Ioi]
lemma unique_diff_on_Iic (a : ℝ) : unique_diff_on ℝ (Iic a) :=
unique_diff_on_convex (convex_Iic a) $ by simp only [interior_Iic, nonempty_Iio]
lemma unique_diff_on_Ioi (a : ℝ) : unique_diff_on ℝ (Ioi a) :=
is_open_Ioi.unique_diff_on
lemma unique_diff_on_Iio (a : ℝ) : unique_diff_on ℝ (Iio a) :=
is_open_Iio.unique_diff_on
lemma unique_diff_on_Icc {a b : ℝ} (hab : a < b) : unique_diff_on ℝ (Icc a b) :=
unique_diff_on_convex (convex_Icc a b) $ by simp only [interior_Icc, nonempty_Ioo, hab]
lemma unique_diff_on_Ico (a b : ℝ) : unique_diff_on ℝ (Ico a b) :=
if hab : a < b
then unique_diff_on_convex (convex_Ico a b) $ by simp only [interior_Ico, nonempty_Ioo, hab]
else by simp only [Ico_eq_empty (le_of_not_lt hab), unique_diff_on_empty]
lemma unique_diff_on_Ioc (a b : ℝ) : unique_diff_on ℝ (Ioc a b) :=
if hab : a < b
then unique_diff_on_convex (convex_Ioc a b) $ by simp only [interior_Ioc, nonempty_Ioo, hab]
else by simp only [Ioc_eq_empty (le_of_not_lt hab), unique_diff_on_empty]
lemma unique_diff_on_Ioo (a b : ℝ) : unique_diff_on ℝ (Ioo a b) :=
is_open_Ioo.unique_diff_on
/-- The real interval `[0, 1]` is a set of unique differentiability. -/
lemma unique_diff_on_Icc_zero_one : unique_diff_on ℝ (Icc (0:ℝ) 1) :=
unique_diff_on_Icc zero_lt_one
end unique_diff
|
a3a23f7c7d7ba63688e7fc7b380455b6b406af69 | ed544fdbb470075305eb2a01b0491ce8a6ba05c8 | /src/certigrad/predicates.lean | 0b7ca23f23ba322a107d4ee09a36af6b614b3c1d | [
"Apache-2.0"
] | permissive | gazimahmud/certigrad | d12caa30c6fc3adf9bb1fcd61479af0faad8b6c3 | 38cc6377dbd5025eb074188a1acd02147a92bdba | refs/heads/master | 1,606,977,759,336 | 1,498,686,571,000 | 1,498,686,571,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,354 | lean | /-
Copyright (c) 2017 Daniel Selsam. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Daniel Selsam
Predicates.
-/
import .util .id .reference .graph .compute_grad
open list
namespace certigrad
def is_downstream (cost : ID) : reference → list node → Prop
| _ [] := false
| tgt (⟨ref, parents, _⟩ :: nodes) :=
if ref.1 = cost
then true
else (tgt ∈ parents ∧ is_downstream ref nodes) ∨ is_downstream tgt nodes
instance decidable_is_downstream (cost : ID) : Π (tgt : reference) (nodes : list node), decidable (is_downstream cost tgt nodes)
| _ [] := decidable.false
| tgt (⟨ref, parents, _⟩ :: nodes) :=
show decidable (if ref.1 = cost then true else (tgt ∈ parents ∧ is_downstream cost ref nodes) ∨ is_downstream cost tgt nodes), from
have H₁ : decidable (is_downstream cost ref nodes), from begin apply decidable_is_downstream end,
have H₂ : decidable (is_downstream cost tgt nodes), from begin apply decidable_is_downstream end,
by tactic.apply_instance
def all_parents_in_env : Π (inputs : env) (nodes : list node), Prop
| _ [] := true
| inputs (⟨ref, parents, _⟩ :: nodes) :=
(∀ (parent : reference), parent ∈ parents → env.has_key parent inputs)
∧ (∀ (x : T ref.2), all_parents_in_env (env.insert ref x inputs) nodes)
def all_costs_scalars (costs : list ID) : Π (nodes : list node), Prop
| [] := true
| (⟨ref, _, _⟩ :: nodes) := (ref.1 ∈ costs → ref.2 = []) ∧ all_costs_scalars nodes
-- We group the decidable properties
structure well_formed_at (costs : list ID) (nodes : list node) (inputs : env) (tgt : reference) : Prop :=
(uids : uniq_ids nodes inputs)
(ps_in_env : all_parents_in_env inputs nodes)
(costs_scalars : all_costs_scalars costs nodes)
(m_contains_tgt : env.has_key tgt inputs)
(tgt_cost_scalar : tgt.1 ∈ costs → tgt.2 = [])
def grads_exist_at : list node → env → reference → Prop
| [] _ _ := true
| (⟨ref, parents, operator.det op⟩ :: nodes) m tgt :=
let m' := env.insert ref (op^.f (env.get_ks parents m)) m in
grads_exist_at nodes m' tgt
∧ (tgt ∈ parents → op^.pre (env.get_ks parents m) ∧ grads_exist_at nodes (env.insert ref (op^.f (env.get_ks parents m)) m) ref)
| (⟨ref, parents, operator.rand op⟩ :: nodes) m tgt :=
let m' := (λ (y : T ref.2), env.insert ref y m) in
(tgt ∈ parents → op^.pre (env.get_ks parents m)) ∧ (∀ y, grads_exist_at nodes (m' y) tgt)
def pdfs_exist_at : list node → env → Prop
| [] _ := true
| (⟨ref, parents, operator.det op⟩ :: nodes) m := pdfs_exist_at nodes (env.insert ref (op^.f (env.get_ks parents m)) m )
| (⟨ref, parents, operator.rand op⟩ :: nodes) m :=
let m' := (λ (y : T ref.2), env.insert ref y m) in
(op^.pre (env.get_ks parents m)) ∧ (∀ y, pdfs_exist_at nodes (m' y))
-- TODO(dhs): these conditions are really nitty-gritty
noncomputable def can_differentiate_under_integrals (costs : list ID) : list node → env → reference → Prop
| [] _ _ := true
| (⟨ref, parents, operator.det op⟩ :: nodes) inputs tgt :=
let inputs' := env.insert ref (op^.f (env.get_ks parents inputs)) inputs in
can_differentiate_under_integrals nodes inputs' tgt
∧ (tgt ∈ parents → can_differentiate_under_integrals nodes (env.insert ref (op^.f (env.get_ks parents inputs)) inputs) ref)
| (⟨ref, parents, operator.rand op⟩ :: nodes) inputs tgt :=
let θ : T tgt.2 := env.get tgt inputs in
let g : T ref.2 → T tgt.2 → ℝ :=
(λ (x : T ref.2) (θ₀ : T tgt.2),
E (graph.to_dist (λ (inputs : env), ⟦sum_costs inputs costs⟧)
(env.insert ref x (env.insert tgt θ₀ inputs))
nodes)
dvec.head) in
let next_inputs := (λ (y : T ref.2), env.insert ref y inputs) in
-- Note: these conditions are redundant, but it is convenient to collect all the variations we need in one place
( (T.is_uniformly_integrable_around (λ (θ₀ : T (tgt.snd)) (x : T (ref.snd)), rand.op.pdf op (env.get_ks parents (env.insert tgt θ₀ inputs)) x ⬝ g x θ₀) θ
∧ T.is_uniformly_integrable_around (λ (θ₀ : T (tgt.snd)) (x : T (ref.snd)), rand.op.pdf op (env.get_ks parents (env.insert tgt θ inputs)) x ⬝ g x θ₀) θ)
∧ (T.is_uniformly_integrable_around (λ (θ₀ : T (tgt.snd)) (x : T (ref.snd)), ∇ (λ (θ₁ : T (tgt.snd)), rand.op.pdf op (env.get_ks parents (env.insert tgt θ₁ inputs)) x ⬝ g x θ₁) θ₀) θ
∧ T.is_uniformly_integrable_around (λ (θ₀ : T (tgt.snd)) (x : T (ref.snd)), ∇ (λ (θ₁ : T (tgt.snd)), rand.op.pdf op (env.get_ks parents (env.insert tgt θ inputs)) x ⬝ g x θ₁) θ₀) θ)
∧ (∀ (idx : ℕ), at_idx parents idx tgt →
T.is_uniformly_integrable_around (λ (θ₀ : T (tgt.snd)) (x : T (ref.snd)), rand.op.pdf op (dvec.update_at θ₀ (env.get_ks parents (env.insert tgt θ inputs)) idx) x ⬝ g x θ) θ)
∧ (∀ (idx : ℕ), at_idx parents idx tgt →
T.is_uniformly_integrable_around (λ (θ₀ : T (tgt.snd)) (x : T (ref.snd)),
∇ (λ (θ₀ : T (tgt.snd)), rand.op.pdf op (dvec.update_at θ₀ (env.get_ks parents (env.insert tgt θ inputs)) idx) x ⬝ g x θ) θ₀) θ))
∧ (∀ y, can_differentiate_under_integrals nodes (next_inputs y) tgt)
end certigrad
|
d8044b4c3e82dd623a41b846be853f51637522e9 | 3dd1b66af77106badae6edb1c4dea91a146ead30 | /tests/lean/run/tactic21.lean | 2f721b8c9ec0a6b53e0274ba3d678112c7efc846 | [
"Apache-2.0"
] | permissive | silky/lean | 79c20c15c93feef47bb659a2cc139b26f3614642 | df8b88dca2f8da1a422cb618cd476ef5be730546 | refs/heads/master | 1,610,737,587,697 | 1,406,574,534,000 | 1,406,574,534,000 | 22,362,176 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 477 | lean | import standard
using tactic
definition assump := eassumption
theorem tst1 {A : Type} {a b c : A} {p : A → A → Prop} (H1 : p a b) (H2 : p b c) : ∃ x, p a x ∧ p x c
:= by apply exists_intro; apply and_intro; assump; assump
theorem tst2 {A : Type} {a b c d : A} {p : A → A → Prop} (Ha : p a c) (H1 : p a b) (Hb : p b d) (H2 : p b c) : ∃ x, p a x ∧ p x c
:= by apply exists_intro; apply and_intro; assump; assump
(*
print(get_env():find("tst2"):value())
*)
|
274200623dc1a3e939424d0ceaee901d0c388d12 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/ring_theory/valuation/integers.lean | 1f8c31afc06659672deed8c45dc958d5be4d5320 | [
"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 | 4,220 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import ring_theory.valuation.basic
/-!
# Ring of integers under a given valuation
The elements with valuation less than or equal to 1.
TODO: Define characteristic predicate.
-/
universes u v w
namespace valuation
section ring
variables {R : Type u} {Γ₀ : Type v} [ring R] [linear_ordered_comm_group_with_zero Γ₀]
variables (v : valuation R Γ₀)
/-- The ring of integers under a given valuation is the subring of elements with valuation ≤ 1. -/
def integer : subring R :=
{ carrier := { x | v x ≤ 1 },
one_mem' := le_of_eq v.map_one,
mul_mem' := λ x y hx hy, trans_rel_right (≤) (v.map_mul x y) (mul_le_one' hx hy),
zero_mem' := trans_rel_right (≤) v.map_zero zero_le_one,
add_mem' := λ x y hx hy, le_trans (v.map_add x y) (max_le hx hy),
neg_mem' := λ x hx, trans_rel_right (≤) (v.map_neg x) hx }
end ring
section comm_ring
variables {R : Type u} {Γ₀ : Type v} [comm_ring R] [linear_ordered_comm_group_with_zero Γ₀]
variables (v : valuation R Γ₀)
variables (O : Type w) [comm_ring O] [algebra O R]
/-- Given a valuation v : R → Γ₀ and a ring homomorphism O →+* R, we say that O is the integers of v
if f is injective, and its range is exactly `v.integer`. -/
structure integers : Prop :=
(hom_inj : function.injective (algebra_map O R))
(map_le_one : ∀ x, v (algebra_map O R x) ≤ 1)
(exists_of_le_one : ∀ ⦃r⦄, v r ≤ 1 → ∃ x, algebra_map O R x = r)
-- typeclass shortcut
instance : algebra v.integer R :=
algebra.of_subring v.integer
theorem integer.integers : v.integers v.integer :=
{ hom_inj := subtype.coe_injective,
map_le_one := λ r, r.2,
exists_of_le_one := λ r hr, ⟨⟨r, hr⟩, rfl⟩ }
namespace integers
variables {v O} (hv : integers v O)
include hv
lemma one_of_is_unit {x : O} (hx : is_unit x) : v (algebra_map O R x) = 1 :=
let ⟨u, hu⟩ := hx in le_antisymm (hv.2 _) $
by { rw [← v.map_one, ← (algebra_map O R).map_one, ← u.mul_inv, ← mul_one (v (algebra_map O R x)),
hu, (algebra_map O R).map_mul, v.map_mul], exact mul_le_mul_left' (hv.2 (u⁻¹ : units O)) _ }
lemma is_unit_of_one {x : O} (hx : is_unit (algebra_map O R x)) (hvx : v (algebra_map O R x) = 1) :
is_unit x :=
let ⟨u, hu⟩ := hx in
have h1 : v u ≤ 1, from hu.symm ▸ hv.2 x,
have h2 : v (u⁻¹ : Rˣ) ≤ 1,
by rw [← one_mul (v _), ← hvx, ← v.map_mul, ← hu, u.mul_inv, hu, hvx, v.map_one],
let ⟨r1, hr1⟩ := hv.3 h1, ⟨r2, hr2⟩ := hv.3 h2 in
⟨⟨r1, r2, hv.1 $ by rw [ring_hom.map_mul, ring_hom.map_one, hr1, hr2, units.mul_inv],
hv.1 $ by rw [ring_hom.map_mul, ring_hom.map_one, hr1, hr2, units.inv_mul]⟩,
hv.1 $ hr1.trans hu⟩
lemma le_of_dvd {x y : O} (h : x ∣ y) : v (algebra_map O R y) ≤ v (algebra_map O R x) :=
let ⟨z, hz⟩ := h in by { rw [← mul_one (v (algebra_map O R x)), hz, ring_hom.map_mul, v.map_mul],
exact mul_le_mul_left' (hv.2 z) _ }
end integers
end comm_ring
section field
variables {F : Type u} {Γ₀ : Type v} [field F] [linear_ordered_comm_group_with_zero Γ₀]
variables {v : valuation F Γ₀} {O : Type w} [comm_ring O] [algebra O F] (hv : integers v O)
include hv
namespace integers
lemma dvd_of_le {x y : O} (h : v (algebra_map O F x) ≤ v (algebra_map O F y)) : y ∣ x :=
classical.by_cases (λ hy : algebra_map O F y = 0, have hx : x = 0,
from hv.1 $ (algebra_map O F).map_zero.symm ▸
(v.zero_iff.1 $ le_zero_iff.1 (v.map_zero ▸ hy ▸ h)),
hx.symm ▸ dvd_zero y) $
λ hy : algebra_map O F y ≠ 0,
have v ((algebra_map O F y)⁻¹ * algebra_map O F x) ≤ 1,
by { rw [← v.map_one, ← inv_mul_cancel hy, v.map_mul, v.map_mul], exact mul_le_mul_left' h _ },
let ⟨z, hz⟩ := hv.3 this in
⟨z, hv.1 $ ((algebra_map O F).map_mul y z).symm ▸ hz.symm ▸ (mul_inv_cancel_left₀ hy _).symm⟩
lemma dvd_iff_le {x y : O} : x ∣ y ↔ v (algebra_map O F y) ≤ v (algebra_map O F x) :=
⟨hv.le_of_dvd, hv.dvd_of_le⟩
lemma le_iff_dvd {x y : O} : v (algebra_map O F x) ≤ v (algebra_map O F y) ↔ y ∣ x :=
⟨hv.dvd_of_le, hv.le_of_dvd⟩
end integers
end field
end valuation
|
2b6728bd0060f03f862529dbece0c2a3ef6e84f3 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/435b.lean | 9acb748d318bfabf510d9cb50bd3205e97a7a0c6 | [
"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 | 267 | lean | structure Op (α : Type _) where op : α → α → α
variable {α}
variable (s : Op α)
local infixr:60 " ∙ " => s.op
local infixr:60 " ∙1 " => s.op1 -- TODO: generate error
def x := 0
notation "x++" => x.succ
notation "x++" => x.foo -- TODO: generate error
|
45162f033092a1d508b511236c570a26c334fea7 | c31182a012eec69da0a1f6c05f42b0f0717d212d | /src/normed_snake.lean | 00bf3e6264af931e26a35b2d935e39fd0227da0d | [] | no_license | Ja1941/lean-liquid | fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc | 8e80ed0cbdf5145d6814e833a674eaf05a1495c1 | refs/heads/master | 1,689,437,983,362 | 1,628,362,719,000 | 1,628,362,719,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,371 | lean | import system_of_complexes.basic
universe variables u
noncomputable theory
open_locale nnreal
open category_theory opposite normed_group_hom system_of_complexes
variables (M M' N : system_of_complexes.{u}) (f : M ⟶ M') (g : M' ⟶ N)
/-- The normed snake lemma, weak version. See Proposition 9.10 from Analytic.pdf -/
--TODO Add the non weak version for complete system of complexes
lemma weak_normed_snake {k k' k'' K K' K'' : ℝ≥0}
[hk : fact (1 ≤ k)] [hk' : fact (1 ≤ k')] [hk'' : fact (1 ≤ k'')]
{m : ℕ} {c₀ : ℝ≥0}
(hM : M.is_weak_bounded_exact k K (m+1) c₀)
(hM' : M'.is_weak_bounded_exact k' K' (m+1) c₀)
(hM'_adm : M'.admissible)
(hf : ∀ c i, (f.apply : M c i ⟶ M' c i).norm_noninc)
(Hf : ∀ (c : ℝ≥0) [fact (c₀ ≤ c)] (i : ℕ) (hi : i ≤ m+1+1) (x : M (k'' * c) i),
∥(res x : M c i)∥ ≤ K'' * ∥f x∥)
(hg : ∀ c i, (g.apply : M' c i ⟶ N c i).ker = f.apply.range)
(hgquot : system_of_complexes.is_quotient g) :
N.is_weak_bounded_exact (k''*k*k') (K'*(K*K'' + 1)) m c₀ :=
begin
introsI c hc i hi,
let c₁ := k'' * (k * (k' * c)),
suffices : ∀ n : N c₁ i, ∀ ε > 0,
∃ i₀ (hi₀ : i₀ = i - 1) (y : N c i₀),
∥res n - N.d _ _ y∥ ≤ K' * (K * K'' + 1) * ∥N.d i (i+1) n∥ + ε,
{ dsimp [c₁] at this,
intros n₁ ε hε,
haveI hc : fact (k'' * k * k' * c = c₁) :=
{ out := (mul_assoc _ _ _).trans ((mul_assoc _ _ _).trans rfl) },
rcases this (res n₁) ε hε with ⟨i₀, hi₀, y, hy⟩,
rw [res_res, d_res] at hy,
refine ⟨i₀, _, hi₀, rfl, _⟩,
refine ⟨y, hy.trans (add_le_add_right (mul_le_mul_of_nonneg_left _ _) ε)⟩,
{ apply (admissible_of_quotient hgquot hM'_adm).res_norm_noninc },
{ exact (nnreal.zero_le_coe : 0 ≤ K' * (K * K'' + 1)) } },
intros n ε hε,
let ε₁ := ε/(K' * (K * K'' + 2) + 1),
have hε₁ : 0 < ε₁ :=
div_pos hε (lt_of_lt_of_le zero_lt_one (nnreal.one_le_add'.out : 1 ≤ K' * (K * K'' + 2) + 1)),
obtain ⟨m' : M' c₁ i, rfl : g m' = n⟩ := (hgquot _ _).surjective _,
let m₁' := M'.d i (i+1) m',
have hm₁' : g m₁' = N.d i (i+1) (g m') := (d_apply _ _ g m').symm,
obtain ⟨m₁'' : M' c₁ (i+1),
hgm₁'' : g m₁'' = N.d i (i+1) (g m'),
hnorm_m₁'' : ∥m₁''∥ < ∥N.d i (i+1) (g m')∥ + ε₁⟩ :=
(hgquot _ _).norm_lift hε₁ (N.d i (i+1) (g m')),
obtain ⟨m₁, hm₁⟩ : ∃ m₁ : M c₁ (i+1), f m₁ + m₁'' = m₁',
{ have hrange : m₁' - m₁'' ∈ f.apply.range,
{ rw [← hg _ _, mem_ker _ _, normed_group_hom.map_sub],
change g m₁' - g m₁'' = 0,
rw [hm₁', hgm₁'', sub_self] },
obtain ⟨m₁, hm₁ : f m₁ = m₁' - m₁''⟩ := (mem_range _ _).1 hrange,
exact ⟨m₁, by rw [hm₁, sub_add_cancel]⟩ },
have him : i+2 ≤ m+2 := add_le_add_right hi _,
have hm₂ : f (M.d (i+1) (i+2) m₁) = -M'.d (i+1) (i+2) m₁'',
{ rw [← d_apply, eq_sub_of_add_eq hm₁, normed_group_hom.map_sub, ← category_theory.comp_apply,
d_comp_d, coe_zero, ← neg_inj, pi.zero_apply, zero_sub], },
have hle : ∥res (M.d (i+1) (i+2) m₁)∥ ≤ K'' * ∥m₁''∥,
{ calc ∥res (M.d (i+1) (i+2) m₁)∥
≤ K'' * ∥f (M.d (i+1) (i+2) m₁)∥ : Hf _ _ him _
... = K'' * ∥M'.d (i+1) (i+2) m₁''∥ : by rw [hm₂, norm_neg]
... ≤ K'' * ∥m₁''∥ : (mul_le_mul_of_nonneg_left
(hM'_adm.d_norm_noninc _ _ _ _ m₁'') $ nnreal.coe_nonneg K'') },
obtain ⟨i', j, hi', rfl, m₀, hm₀⟩ :=
hM _ ⟨hc.out.trans $ le_mul_of_one_le_left' hk'.out⟩ _ (nat.succ_le_succ hi) (res m₁) ε₁ hε₁,
rw [← nat.pred_eq_sub_one, i.pred_succ] at hi', subst i',
replace hm₀ : ∥res m₁ - M.d i (i+1) m₀∥ ≤ K * K'' * ∥N.d i (i+1) (g m')∥ + K*K''*ε₁ + ε₁,
{ calc ∥res m₁ - M.d i (i+1) m₀∥ = ∥res (res m₁) - M.d i (i+1) m₀∥ : by rw res_res
... ≤ K * ∥M.d (i+1) (i+2) (res m₁)∥ + ε₁ : hm₀
... = K * ∥res (M.d (i+1) (i+2) m₁)∥ + ε₁ : by rw d_res
... ≤ K*(K'' * ∥m₁''∥) + ε₁ : add_le_add_right (mul_le_mul_of_nonneg_left hle nnreal.zero_le_coe) _
... ≤ K*(K'' * (∥N.d i (i+1) (g m')∥ + ε₁)) + ε₁ : add_le_add_right (mul_le_mul_of_nonneg_left
(mul_le_mul_of_nonneg_left hnorm_m₁''.le nnreal.zero_le_coe)
nnreal.zero_le_coe) ε₁
... = K * K'' * ∥N.d i (i+1) (g m')∥ + K*K''*ε₁ + ε₁ : by ring },
let mnew₁' := M'.d i (i+1) (res m' - f m₀),
have hmnew' : mnew₁' = res m₁'' + f (res m₁ - M.d i (i+1) m₀),
{ calc mnew₁'
= M'.d i (i+1) (res m' - f m₀) : rfl
... = res (M'.d i (i+1) m') - (f (M.d i (i+1) m₀)) : by rw [normed_group_hom.map_sub, d_res _, d_apply]
... = res (M'.d i (i+1) m') - (f (res m₁)) + (f (res m₁) - f (M.d i (i+1) m₀)) : by abel
... = res m₁'' + f ((res m₁) - (M.d i (i+1) m₀)) : by
{ rw [← system_of_complexes.map_sub, ← res_apply,
← normed_group_hom.map_sub, ← sub_eq_of_eq_add' hm₁.symm] } },
have hnormle : ∥mnew₁'∥ ≤ (K*K'' + 1)*∥N.d i (i+1) (g m')∥ + (K*K'' + 2) * ε₁,
{ calc ∥mnew₁'∥
= ∥res m₁'' + f (res m₁ - M.d i (i+1) m₀)∥ : by rw [hmnew']
... ≤ ∥res m₁''∥ + ∥f (res m₁ - M.d i (i+1) m₀)∥ : norm_add_le _ _
... ≤ ∥m₁''∥ + ∥f (res m₁ - M.d i (i+1) m₀)∥ : add_le_add_right
(hM'_adm.res_norm_noninc _ _ _ _ m₁'') _
... ≤ ∥m₁''∥ + ∥res m₁ - M.d i (i+1) m₀∥ : add_le_add_left (hf _ _ _) _
... ≤ ∥N.d i (i+1) (g m')∥ + ε₁ + ∥res m₁ - M.d i (i+1) m₀∥ : add_le_add_right (le_of_lt hnorm_m₁'') _
... ≤ ∥N.d i (i+1) (g m')∥ + ε₁ + (K * K'' * ∥N.d i (i+1) (g m')∥ + K * K'' * ε₁ + ε₁) : add_le_add_left hm₀ _
... = (K*K'' + 1)*∥d _ _ (i+1) (g m')∥ + (K*K'' + 2) * ε₁ : by ring },
obtain ⟨i₀, _, hi₀, rfl, mnew₀, hmnew₀⟩ := hM' _ hc _ (hi.trans m.le_succ) (res m' - f m₀) _ hε₁,
replace hmnew₀ : ∥res (res m' - f m₀) - d _ _ _ mnew₀∥ ≤
K' * ((K * K'' + 1) * ∥N.d i (i+1) (g m')∥ + (K * K'' + 2) * ε₁) + ε₁ :=
hmnew₀.trans (add_le_add_right (mul_le_mul_of_nonneg_left hnormle nnreal.zero_le_coe) ε₁),
let nnew₀ : ↥(N c i₀) := g mnew₀,
have hmnewlift : g (res (res m' - f m₀) - M'.d i₀ i mnew₀) = res (g m') - N.d i₀ i nnew₀,
{ suffices h : g (res m' - f m₀) = res (g m'),
{ rw [system_of_complexes.map_sub, ← res_apply, ← d_apply, h, res_res] },
rw system_of_complexes.map_sub,
have hker : f m₀ ∈ g.apply.ker,
{ rw [hg _ _, mem_range _ _],
exact ⟨m₀, rfl⟩ },
replace hker : g (f m₀) = 0, { rwa mem_ker at hker },
rw [hker, sub_zero, ← res_apply] },
refine ⟨i₀, hi₀, nnew₀, _⟩,
rw ← hmnewlift,
refine ((hgquot _ _).norm_le _).trans (hmnew₀.trans (le_of_eq _)),
have hε₁_ε : (K' * (K * K'' + 2) + 1 : ℝ)*ε₁ = ε := mul_div_cancel' _
(by { refine (lt_of_lt_of_le zero_lt_one _).ne',
exact (nnreal.one_le_add'.out : 1 ≤ K' * (K * K'' + 2) + 1) }),
rw ← hε₁_ε,
ring,
end
|
769ced5eba60fc4af971621258d8b557ba2bf01f | 367134ba5a65885e863bdc4507601606690974c1 | /src/analysis/convex/specific_functions.lean | 43317b577b7d97d97bfd0f3f60e9bb284a690a6b | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 6,657 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Sébastien Gouëzel
-/
import analysis.calculus.mean_value
import data.nat.parity
import analysis.special_functions.pow
/-!
# Collection of convex functions
In this file we prove that the following functions are convex:
* `convex_on_exp` : the exponential function is convex on $(-∞, +∞)$;
* `convex_on_pow_of_even` : given an even natural number $n$, the function $f(x)=x^n$
is convex on $(-∞, +∞)$;
* `convex_on_pow` : for a natural $n$, the function $f(x)=x^n$ is convex on $[0, +∞)$;
* `convex_on_fpow` : for an integer $m$, the function $f(x)=x^m$ is convex on $(0, +∞)$.
* `convex_on_rpow : ∀ p : ℝ, 1 ≤ p → convex_on (Ici 0) (λ x, x ^ p)`
* `concave_on_log_Ioi` and `concave_on_log_Iio`: log is concave on `Ioi 0` and `Iio 0` respectively.
-/
open real set
open_locale big_operators
/-- `exp` is convex on the whole real line -/
lemma convex_on_exp : convex_on univ exp :=
convex_on_univ_of_deriv2_nonneg differentiable_exp (by simp)
(assume x, (iter_deriv_exp 2).symm ▸ le_of_lt (exp_pos x))
/-- `x^n`, `n : ℕ` is convex on the whole real line whenever `n` is even -/
lemma convex_on_pow_of_even {n : ℕ} (hn : even n) : convex_on set.univ (λ x : ℝ, x^n) :=
begin
apply convex_on_univ_of_deriv2_nonneg differentiable_pow,
{ simp only [deriv_pow', differentiable.mul, differentiable_const, differentiable_pow] },
{ intro x,
rcases nat.even.sub_even hn (nat.even_bit0 1) with ⟨k, hk⟩,
simp only [iter_deriv_pow, finset.prod_range_succ, finset.prod_range_zero, nat.sub_zero,
mul_one, hk, pow_mul', pow_two],
exact mul_nonneg (nat.cast_nonneg _) (mul_self_nonneg _) }
end
/-- `x^n`, `n : ℕ` is convex on `[0, +∞)` for all `n` -/
lemma convex_on_pow (n : ℕ) : convex_on (Ici 0) (λ x : ℝ, x^n) :=
begin
apply convex_on_of_deriv2_nonneg (convex_Ici _) (continuous_pow n).continuous_on;
simp only [interior_Ici, differentiable_on_pow, deriv_pow',
differentiable_on_const, differentiable_on.mul, iter_deriv_pow],
intros x hx,
exact mul_nonneg (nat.cast_nonneg _) (pow_nonneg (le_of_lt hx) _)
end
lemma finset.prod_nonneg_of_card_nonpos_even
{α β : Type*} [linear_ordered_comm_ring β]
{f : α → β} [decidable_pred (λ x, f x ≤ 0)]
{s : finset α} (h0 : even (s.filter (λ x, f x ≤ 0)).card) :
0 ≤ ∏ x in s, f x :=
calc 0 ≤ (∏ x in s, ((if f x ≤ 0 then (-1:β) else 1) * f x)) :
finset.prod_nonneg (λ x _, by
{ split_ifs with hx hx, by simp [hx], simp at hx ⊢, exact le_of_lt hx })
... = _ : by rw [finset.prod_mul_distrib, finset.prod_ite, finset.prod_const_one,
mul_one, finset.prod_const, neg_one_pow_eq_pow_mod_two, nat.even_iff.1 h0, pow_zero, one_mul]
lemma int_prod_range_nonneg (m : ℤ) (n : ℕ) (hn : even n) :
0 ≤ ∏ k in finset.range n, (m - k) :=
begin
cases (le_or_lt ↑n m) with hnm hmn,
{ exact finset.prod_nonneg (λ k hk, sub_nonneg.2 (le_trans
(int.coe_nat_le.2 $ le_of_lt $ finset.mem_range.1 hk) hnm)) },
cases le_or_lt 0 m with hm hm,
{ lift m to ℕ using hm,
exact le_of_eq (eq.symm $ finset.prod_eq_zero
(finset.mem_range.2 $ int.coe_nat_lt.1 hmn) (sub_self _)) },
clear hmn,
apply finset.prod_nonneg_of_card_nonpos_even,
convert hn,
convert finset.card_range n,
ext k,
simp only [finset.mem_filter, finset.mem_range],
refine ⟨and.left, λ hk, ⟨hk, sub_nonpos.2 $ le_trans (le_of_lt hm) _⟩⟩,
exact int.coe_nat_nonneg k
end
/-- `x^m`, `m : ℤ` is convex on `(0, +∞)` for all `m` -/
lemma convex_on_fpow (m : ℤ) : convex_on (Ioi 0) (λ x : ℝ, x^m) :=
begin
apply convex_on_of_deriv2_nonneg (convex_Ioi 0); try { rw [interior_Ioi] },
{ exact (differentiable_on_fpow $ lt_irrefl _).continuous_on },
{ exact differentiable_on_fpow (lt_irrefl _) },
{ have : eq_on (deriv (λx:ℝ, x^m)) (λx, ↑m * x^(m-1)) (Ioi 0),
from λ x hx, deriv_fpow (ne_of_gt hx),
refine (differentiable_on_congr this).2 _,
exact (differentiable_on_fpow (lt_irrefl _)).const_mul _ },
{ intros x hx,
simp only [iter_deriv_fpow (ne_of_gt hx)],
refine mul_nonneg (int.cast_nonneg.2 _) (fpow_nonneg_of_nonneg (le_of_lt hx) _),
exact int_prod_range_nonneg _ _ (nat.even_bit0 1) }
end
lemma convex_on_rpow {p : ℝ} (hp : 1 ≤ p) : convex_on (Ici 0) (λ x : ℝ, x^p) :=
begin
have A : deriv (λ (x : ℝ), x ^ p) = λ x, p * x^(p-1), by { ext x, simp [hp] },
apply convex_on_of_deriv2_nonneg (convex_Ici 0),
{ apply (continuous_rpow_of_pos (λ _, lt_of_lt_of_le zero_lt_one hp)
continuous_id continuous_const).continuous_on },
{ apply differentiable.differentiable_on, simp [hp] },
{ rw A,
assume x hx,
replace hx : x ≠ 0, by { simp at hx, exact ne_of_gt hx },
simp [differentiable_at.differentiable_within_at, hx] },
{ assume x hx,
replace hx : 0 < x, by simpa using hx,
suffices : 0 ≤ p * ((p - 1) * x ^ (p - 1 - 1)), by simpa [ne_of_gt hx, A],
apply mul_nonneg (le_trans zero_le_one hp),
exact mul_nonneg (sub_nonneg_of_le hp) (rpow_nonneg_of_nonneg (le_of_lt hx) _) }
end
lemma concave_on_log_Ioi : concave_on (Ioi 0) log :=
begin
have h₁ : Ioi 0 ⊆ ({0} : set ℝ)ᶜ,
{ intros x hx hx',
rw [mem_singleton_iff] at hx',
rw [hx'] at hx,
exact lt_irrefl 0 hx },
refine concave_on_open_of_deriv2_nonpos (convex_Ioi 0) is_open_Ioi _ _ _,
{ exact differentiable_on_log.mono h₁ },
{ refine ((times_cont_diff_on_log.deriv_of_open _ le_top).differentiable_on le_top).mono h₁,
exact is_open_compl_singleton },
{ intros x hx,
rw [function.iterate_succ, function.iterate_one],
change (deriv (deriv log)) x ≤ 0,
rw [deriv_log', deriv_inv (show x ≠ 0, by {rintro rfl, exact lt_irrefl 0 hx})],
exact neg_nonpos.mpr (inv_nonneg.mpr (pow_two_nonneg x)) }
end
lemma concave_on_log_Iio : concave_on (Iio 0) log :=
begin
have h₁ : Iio 0 ⊆ ({0} : set ℝ)ᶜ,
{ intros x hx hx',
rw [mem_singleton_iff] at hx',
rw [hx'] at hx,
exact lt_irrefl 0 hx },
refine concave_on_open_of_deriv2_nonpos (convex_Iio 0) is_open_Iio _ _ _,
{ exact differentiable_on_log.mono h₁ },
{ refine ((times_cont_diff_on_log.deriv_of_open _ le_top).differentiable_on le_top).mono h₁,
exact is_open_compl_singleton },
{ intros x hx,
rw [function.iterate_succ, function.iterate_one],
change (deriv (deriv log)) x ≤ 0,
rw [deriv_log', deriv_inv (show x ≠ 0, by {rintro rfl, exact lt_irrefl 0 hx})],
exact neg_nonpos.mpr (inv_nonneg.mpr (pow_two_nonneg x)) }
end
|
4e81395e4fc75aa1b562e0209fb082e1534d2eca | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/hott/ind_tac3.hlean | 40370f87d978a8c7cabe5d267a75e47face229ff | [
"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 | 176 | hlean | open eq
set_option pp.implicit true
set_option pp.universes true
set_option pp.notation false
check @idp_rec_on
attribute idp_rec_on [recursor]
print [recursor] idp_rec_on
|
610428af1d94940473a2198fb667bf6ae31c4403 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/char_p/char_and_card.lean | 10d61ec6c37081009365042ffdd1d367056b6ca6 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 3,286 | lean | /-
Copyright (c) 2022 Michael Stoll. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Michael Stoll
-/
import algebra.char_p.basic
import group_theory.perm.cycle.type
/-!
# Characteristic and cardinality
We prove some results relating characteristic and cardinality of finite rings
## Tags
characterstic, cardinality, ring
-/
/-- A prime `p` is a unit in a commutative ring `R` of nonzero characterstic iff it does not divide
the characteristic. -/
lemma is_unit_iff_not_dvd_char_of_ring_char_ne_zero (R : Type*) [comm_ring R] (p : ℕ) [fact p.prime]
(hR : ring_char R ≠ 0) :
is_unit (p : R) ↔ ¬ p ∣ ring_char R :=
begin
have hch := char_p.cast_eq_zero R (ring_char R),
have hp : p.prime := fact.out p.prime,
split,
{ rintros h₁ ⟨q, hq⟩,
rcases is_unit.exists_left_inv h₁ with ⟨a, ha⟩,
have h₃ : ¬ ring_char R ∣ q :=
begin
rintro ⟨r, hr⟩,
rw [hr, ← mul_assoc, mul_comm p, mul_assoc] at hq,
nth_rewrite 0 ← mul_one (ring_char R) at hq,
exact nat.prime.not_dvd_one hp ⟨r, mul_left_cancel₀ hR hq⟩,
end,
have h₄ := mt (char_p.int_cast_eq_zero_iff R (ring_char R) q).mp,
apply_fun (coe : ℕ → R) at hq,
apply_fun ((*) a) at hq,
rw [nat.cast_mul, hch, mul_zero, ← mul_assoc, ha, one_mul] at hq,
norm_cast at h₄,
exact h₄ h₃ hq.symm, },
{ intro h,
rcases (hp.coprime_iff_not_dvd.mpr h).is_coprime with ⟨a, b, hab⟩,
apply_fun (coe : ℤ → R) at hab,
push_cast at hab,
rw [hch, mul_zero, add_zero, mul_comm] at hab,
exact is_unit_of_mul_eq_one (p : R) a hab, },
end
/-- A prime `p` is a unit in a finite commutative ring `R`
iff it does not divide the characteristic. -/
lemma is_unit_iff_not_dvd_char (R : Type*) [comm_ring R] (p : ℕ) [fact p.prime] [finite R] :
is_unit (p : R) ↔ ¬ p ∣ ring_char R :=
is_unit_iff_not_dvd_char_of_ring_char_ne_zero R p $ char_p.char_ne_zero_of_finite R (ring_char R)
/-- The prime divisors of the characteristic of a finite commutative ring are exactly
the prime divisors of its cardinality. -/
lemma prime_dvd_char_iff_dvd_card {R : Type*} [comm_ring R] [fintype R] (p : ℕ) [fact p.prime] :
p ∣ ring_char R ↔ p ∣ fintype.card R :=
begin
refine ⟨λ h, h.trans $ int.coe_nat_dvd.mp $ (char_p.int_cast_eq_zero_iff R (ring_char R)
(fintype.card R)).mp $ by exact_mod_cast char_p.cast_card_eq_zero R, λ h, _⟩,
by_contra h₀,
rcases exists_prime_add_order_of_dvd_card p h with ⟨r, hr⟩,
have hr₁ := add_order_of_nsmul_eq_zero r,
rw [hr, nsmul_eq_mul] at hr₁,
rcases is_unit.exists_left_inv ((is_unit_iff_not_dvd_char R p).mpr h₀) with ⟨u, hu⟩,
apply_fun ((*) u) at hr₁,
rw [mul_zero, ← mul_assoc, hu, one_mul] at hr₁,
exact mt add_monoid.order_of_eq_one_iff.mpr
(ne_of_eq_of_ne hr (nat.prime.ne_one (fact.out p.prime))) hr₁,
end
/-- A prime that does not divide the cardinality of a finite commutative ring `R`
is a unit in `R`. -/
lemma not_is_unit_prime_of_dvd_card {R : Type*} [comm_ring R] [fintype R] (p : ℕ) [fact p.prime]
(hp : p ∣ fintype.card R) : ¬ is_unit (p : R) :=
mt (is_unit_iff_not_dvd_char R p).mp (not_not.mpr ((prime_dvd_char_iff_dvd_card p).mpr hp))
|
aa6118bd08d8f19929713e7ac27c01660990845f | 437dc96105f48409c3981d46fb48e57c9ac3a3e4 | /src/algebra/midpoint.lean | e9da35f1e76f57094a3c93b64ca595a4a0317e12 | [
"Apache-2.0"
] | permissive | dan-c-k/mathlib | 08efec79bd7481ee6da9cc44c24a653bff4fbe0d | 96efc220f6225bc7a5ed8349900391a33a38cc56 | refs/heads/master | 1,658,082,847,093 | 1,589,013,201,000 | 1,589,013,201,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,247 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Yury Kudryashov
-/
import algebra.module
import algebra.invertible
/-!
# Midpoint of a segment
## Main definitions
* `midpoint R x y`: midpoint of the segment `[x, y]`. We define it for `x` and `y`
in a module over a ring `R` with invertible `2`.
* `add_monoid_hom.of_map_midpoint`: construct an `add_monoid_hom` given a map `f` such that
`f` sends zero to zero and midpoints to midpoints.
## Main theorems
* `midpoint_eq_iff`: `z` is the midpoint of `[x, y]` if and only if `x + y = z + z`,
* `midpoint_unique`: `midpoint R x y` does not depend on `R`;
* `midpoint x y` is linear both in `x` and `y`;
* `reflection_midpoint_left`, `reflection_midpoint_right`: `equiv.reflection (midpoint R x y)`
swaps `x` and `y`.
We do not mark most lemmas as `@[simp]` because it is hard to tell which side is simpler.
## Tags
midpoint, add_monoid_hom
-/
variables (R : Type*) {E : Type*}
section monoid
variables [semiring R] [invertible (2:R)] [add_comm_monoid E] [semimodule R E]
/-- `midpoint x y` is the midpoint of the segment `[x, y]`. -/
def midpoint (x y : E) : E := (⅟2:R) • (x + y)
lemma midpoint_eq_iff {x y z : E} : midpoint R x y = z ↔ x + y = z + z :=
⟨λ h, h ▸ calc x + y = (2 * ⅟2:R) • (x + y) : by rw [mul_inv_of_self, one_smul]
... = midpoint R x y + midpoint R x y : by rw [two_mul, add_smul, midpoint],
λ h, by rw [midpoint, h, ← two_smul R z, smul_smul, inv_of_mul_self, one_smul]⟩
@[simp] lemma midpoint_add_self (x y : E) : midpoint R x y + midpoint R x y = x + y :=
((midpoint_eq_iff R).1 rfl).symm
/-- `midpoint` does not depend on the ring `R`. -/
lemma midpoint_unique (R' : Type*) [semiring R'] [invertible (2:R')] [semimodule R' E] (x y : E) :
midpoint R x y = midpoint R' x y :=
(midpoint_eq_iff R).2 $ (midpoint_eq_iff R').1 rfl
@[simp] lemma midpoint_self (x : E) : midpoint R x x = x :=
by rw [midpoint, smul_add, ← two_smul R, smul_smul, mul_inv_of_self, one_smul]
variable {R}
lemma midpoint_def (x y : E) : midpoint R x y = (⅟2:R) • (x + y) := rfl
lemma midpoint_comm (x y : E) : midpoint R x y = midpoint R y x :=
by simp only [midpoint_def, add_comm]
lemma midpoint_zero_add (x y : E) : midpoint R 0 (x + y) = midpoint R x y :=
(midpoint_eq_iff R).2 $ (zero_add (x + y)).symm ▸ (midpoint_eq_iff R).1 rfl
lemma midpoint_add_add (x y x' y' : E) :
midpoint R (x + x') (y + y') = midpoint R x y + midpoint R x' y' :=
by { simp only [midpoint_def, ← smul_add, add_assoc, add_left_comm x'] }
lemma midpoint_add_right (x y z : E) : midpoint R (x + z) (y + z) = midpoint R x y + z :=
by rw [midpoint_add_add, midpoint_self]
lemma midpoint_add_left (x y z : E) : midpoint R (x + y) (x + z) = x + midpoint R y z :=
by rw [midpoint_add_add, midpoint_self]
lemma midpoint_smul_smul (c : R) (x y : E) : midpoint R (c • x) (c • y) = c • midpoint R x y :=
(midpoint_eq_iff R).2 $ by rw [← smul_add, ← smul_add, (midpoint_eq_iff R).1 rfl]
end monoid
section group
variables [ring R] [invertible (2:R)] [add_comm_group E] [module R E]
lemma midpoint_neg_neg (x y : E) : midpoint R (-x) (-y) = -midpoint R x y :=
by simpa only [neg_one_smul] using midpoint_smul_smul (-1:R) x y
lemma midpoint_sub_sub (x y x' y' : E) :
midpoint R (x - x') (y - y') = midpoint R x y - midpoint R x' y' :=
by simp only [sub_eq_add_neg, midpoint_add_add, midpoint_neg_neg]
lemma midpoint_sub_right (x y z : E) : midpoint R (x - z) (y - z) = midpoint R x y - z :=
by rw [midpoint_sub_sub, midpoint_self]
lemma midpoint_sub_left (x y z : E) : midpoint R (x - y) (x - z) = x - midpoint R y z :=
by rw [midpoint_sub_sub, midpoint_self]
end group
namespace add_monoid_hom
variables (R) (R' : Type*) {F : Type*}
[semiring R] [invertible (2:R)] [add_comm_monoid E] [semimodule R E]
[semiring R'] [invertible (2:R')] [add_comm_monoid F] [semimodule R' F]
/-- A map `f : E → F` sending zero to zero and midpoints to midpoints is an `add_monoid_hom`. -/
def of_map_midpoint (f : E → F) (h0 : f 0 = 0)
(hm : ∀ x y, f (midpoint R x y) = midpoint R' (f x) (f y)) :
E →+ F :=
{ to_fun := f,
map_zero' := h0,
map_add' := λ x y, -- by rw [← midpoint_self R (x + y), ← midpoint_zero_add, hm, h0]
calc f (x + y) = f 0 + f (x + y) : by rw [h0, zero_add]
... = midpoint R' (f 0) (f (x + y)) + midpoint R' (f 0) (f (x + y)) :
(midpoint_add_self _ _ _).symm
... = f (midpoint R x y) + f (midpoint R x y) : by rw [← hm, midpoint_zero_add]
... = f x + f y : by rw [hm, midpoint_add_self] }
@[simp] lemma coe_of_map_midpoint (f : E → F) (h0 : f 0 = 0)
(hm : ∀ x y, f (midpoint R x y) = midpoint R' (f x) (f y)) :
⇑(of_map_midpoint R R' f h0 hm) = f := rfl
end add_monoid_hom
namespace equiv
variables [ring R] [invertible (2:R)] [add_comm_group E] [module R E]
@[simp] lemma reflection_midpoint_left (x y : E) : (reflection (midpoint R x y) : E → E) x = y :=
by rw [reflection_apply, midpoint_add_self, add_sub_cancel']
@[simp] lemma reflection_midpoint_right (x y : E) : (reflection (midpoint R x y) : E → E) y = x :=
by rw [reflection_apply, midpoint_add_self, add_sub_cancel]
end equiv
|
ddf9346771cc76ad3ee6ed7a75d9a25d86ad76da | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/ring_theory/matrix_algebra_auto.lean | 3028966e1c6363257ea4778e29da5493fd42bedb | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,232 | 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 Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.ring_theory.tensor_product
import Mathlib.PostPort
universes u v w
namespace Mathlib
/-!
We provide the `R`-algebra structure on `matrix n n A` when `A` is an `R`-algebra,
and show `matrix n n A ≃ₐ[R] (A ⊗[R] matrix n n R)`.
-/
protected instance matrix.algebra {R : Type u} [comm_semiring R] {A : Type v} [semiring A]
[algebra R A] {n : Type w} [fintype n] [DecidableEq n] : algebra R (matrix n n A) :=
algebra.mk
(ring_hom.mk (ring_hom.to_fun (ring_hom.comp (matrix.scalar n) (algebra_map R A))) sorry sorry
sorry sorry)
sorry sorry
theorem algebra_map_matrix_apply {R : Type u} [comm_semiring R] {A : Type v} [semiring A]
[algebra R A] {n : Type w} [fintype n] [DecidableEq n] {r : R} {i : n} {j : n} :
coe_fn (algebra_map R (matrix n n A)) r i j = ite (i = j) (coe_fn (algebra_map R A) r) 0 :=
sorry
namespace matrix_equiv_tensor
/--
(Implementation detail).
The bare function underlying `(A ⊗[R] matrix n n R) →ₐ[R] matrix n n A`, on pure tensors.
-/
def to_fun (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w)
[fintype n] (a : A) (m : matrix n n R) : matrix n n A :=
fun (i j : n) => a * coe_fn (algebra_map R A) (m i j)
/--
(Implementation detail).
The function underlying `(A ⊗[R] matrix n n R) →ₐ[R] matrix n n A`,
as an `R`-linear map in the second tensor factor.
-/
def to_fun_right_linear (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A]
(n : Type w) [fintype n] (a : A) : linear_map R (matrix n n R) (matrix n n A) :=
linear_map.mk (to_fun R A n a) sorry sorry
/--
(Implementation detail).
The function underlying `(A ⊗[R] matrix n n R) →ₐ[R] matrix n n A`,
as an `R`-bilinear map.
-/
def to_fun_bilinear (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A]
(n : Type w) [fintype n] : linear_map R A (linear_map R (matrix n n R) (matrix n n A)) :=
linear_map.mk (to_fun_right_linear R A n) sorry sorry
/--
(Implementation detail).
The function underlying `(A ⊗[R] matrix n n R) →ₐ[R] matrix n n A`,
as an `R`-linear map.
-/
def to_fun_linear (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A]
(n : Type w) [fintype n] : linear_map R (tensor_product R A (matrix n n R)) (matrix n n A) :=
tensor_product.lift (to_fun_bilinear R A n)
/--
The function `(A ⊗[R] matrix n n R) →ₐ[R] matrix n n A`, as an algebra homomorphism.
-/
def to_fun_alg_hom (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A]
(n : Type w) [fintype n] [DecidableEq n] :
alg_hom R (tensor_product R A (matrix n n R)) (matrix n n A) :=
algebra.tensor_product.alg_hom_of_linear_map_tensor_product (to_fun_linear R A n) sorry sorry
@[simp] theorem to_fun_alg_hom_apply (R : Type u) [comm_semiring R] (A : Type v) [semiring A]
[algebra R A] (n : Type w) [fintype n] [DecidableEq n] (a : A) (m : matrix n n R) :
coe_fn (to_fun_alg_hom R A n) (tensor_product.tmul R a m) =
fun (i j : n) => a * coe_fn (algebra_map R A) (m i j) :=
sorry
/--
(Implementation detail.)
The bare function `matrix n n A → A ⊗[R] matrix n n R`.
(We don't need to show that it's an algebra map, thankfully --- just that it's an inverse.)
-/
def inv_fun (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w)
[fintype n] [DecidableEq n] (M : matrix n n A) : tensor_product R A (matrix n n R) :=
finset.sum finset.univ
fun (p : n × n) =>
tensor_product.tmul R (M (prod.fst p) (prod.snd p))
(matrix.std_basis_matrix (prod.fst p) (prod.snd p) 1)
@[simp] theorem inv_fun_zero (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A]
(n : Type w) [fintype n] [DecidableEq n] : inv_fun R A n 0 = 0 :=
sorry
@[simp] theorem inv_fun_add (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A]
(n : Type w) [fintype n] [DecidableEq n] (M : matrix n n A) (N : matrix n n A) :
inv_fun R A n (M + N) = inv_fun R A n M + inv_fun R A n N :=
sorry
@[simp] theorem inv_fun_smul (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A]
(n : Type w) [fintype n] [DecidableEq n] (a : A) (M : matrix n n A) :
(inv_fun R A n fun (i j : n) => a * M i j) = tensor_product.tmul R a 1 * inv_fun R A n M :=
sorry
@[simp] theorem inv_fun_algebra_map (R : Type u) [comm_semiring R] (A : Type v) [semiring A]
[algebra R A] (n : Type w) [fintype n] [DecidableEq n] (M : matrix n n R) :
(inv_fun R A n fun (i j : n) => coe_fn (algebra_map R A) (M i j)) = tensor_product.tmul R 1 M :=
sorry
theorem right_inv (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A]
(n : Type w) [fintype n] [DecidableEq n] (M : matrix n n A) :
coe_fn (to_fun_alg_hom R A n) (inv_fun R A n M) = M :=
sorry
theorem left_inv (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w)
[fintype n] [DecidableEq n] (M : tensor_product R A (matrix n n R)) :
inv_fun R A n (coe_fn (to_fun_alg_hom R A n) M) = M :=
sorry
/--
(Implementation detail)
The equivalence, ignoring the algebra structure, `(A ⊗[R] matrix n n R) ≃ matrix n n A`.
-/
def equiv (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w)
[fintype n] [DecidableEq n] : tensor_product R A (matrix n n R) ≃ matrix n n A :=
equiv.mk (⇑(to_fun_alg_hom R A n)) (inv_fun R A n) sorry sorry
end matrix_equiv_tensor
/--
The `R`-algebra isomorphism `matrix n n A ≃ₐ[R] (A ⊗[R] matrix n n R)`.
-/
def matrix_equiv_tensor (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A]
(n : Type w) [fintype n] [DecidableEq n] :
alg_equiv R (matrix n n A) (tensor_product R A (matrix n n R)) :=
alg_equiv.symm
(alg_equiv.mk (alg_hom.to_fun sorry) (equiv.inv_fun sorry) sorry sorry sorry sorry sorry)
@[simp] theorem matrix_equiv_tensor_apply (R : Type u) [comm_semiring R] (A : Type v) [semiring A]
[algebra R A] (n : Type w) [fintype n] [DecidableEq n] (M : matrix n n A) :
coe_fn (matrix_equiv_tensor R A n) M =
finset.sum finset.univ
fun (p : n × n) =>
tensor_product.tmul R (M (prod.fst p) (prod.snd p))
(matrix.std_basis_matrix (prod.fst p) (prod.snd p) 1) :=
rfl
@[simp] theorem matrix_equiv_tensor_apply_std_basis (R : Type u) [comm_semiring R] (A : Type v)
[semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (i : n) (j : n) (x : A) :
coe_fn (matrix_equiv_tensor R A n) (matrix.std_basis_matrix i j x) =
tensor_product.tmul R x (matrix.std_basis_matrix i j 1) :=
sorry
@[simp] theorem matrix_equiv_tensor_apply_symm (R : Type u) [comm_semiring R] (A : Type v)
[semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (a : A) (M : matrix n n R) :
coe_fn (alg_equiv.symm (matrix_equiv_tensor R A n)) (tensor_product.tmul R a M) =
fun (i j : n) => a * coe_fn (algebra_map R A) (M i j) :=
sorry
end Mathlib |
acae21a86a81860c8b2a46af3d432a5555bf4448 | 853df553b1d6ca524e3f0a79aedd32dde5d27ec3 | /src/measure_theory/outer_measure.lean | 57e561d1bba7a5abc1cb8640427aee15af43aed6 | [
"Apache-2.0"
] | permissive | DanielFabian/mathlib | efc3a50b5dde303c59eeb6353ef4c35a345d7112 | f520d07eba0c852e96fe26da71d85bf6d40fcc2a | refs/heads/master | 1,668,739,922,971 | 1,595,201,756,000 | 1,595,201,756,000 | 279,469,476 | 0 | 0 | null | 1,594,696,604,000 | 1,594,696,604,000 | null | UTF-8 | Lean | false | false | 20,993 | 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
Outer measures -- overapproximations of measures
-/
import analysis.specific_limits
import measure_theory.measurable_space
/-!
# Outer Measures
An outer measure is a function `μ : set α → ennreal`, from the powerset of a type to the extended
nonnegative real numbers that satisfies the following conditions:
1. `μ ∅ = 0`;
2. `μ` is monotone;
3. `μ` is countably subadditive. This means that the outer measure of a countable union is at most
the sum of the outer measure on the individual sets.
Note that we do not need `α` to be measurable to define an outer measure.
The outer measures on a type `α` form a complete lattice.
Given an arbitrary function `m : set α → ennreal` that sends `∅` to `0` we can define an outer
measure on `α` that on `s` is defined to be the infimum of `∑ᵢ, m (sᵢ)` for all collections of sets
`sᵢ` that cover `s`. This is the unique maximal outer measure that is at most the given function.
Given an outer measure `m`, the Carathéodory-measurable sets are the sets `s` such that
for all sets `t` we have `m t = m (t ∩ s) + m (t \ s)`. This forms a measurable space.
## Main statements
* `outer_measure.of_function` is the greatest outer measure that is at most the given function.
* `caratheodory` is the Carathéodory-measurable space of an outer measure.
* `Inf_eq_of_function_Inf_gen` is a characterization of the infimum of outer measures.
## References
* <https://en.wikipedia.org/wiki/Outer_measure>
* <https://en.wikipedia.org/wiki/Carath%C3%A9odory%27s_criterion>
## Tags
outer measure, Carathéodory-measurable, Carathéodory's criterion
-/
noncomputable theory
open set finset function filter encodable
open_locale classical big_operators
namespace measure_theory
/-- An outer measure is a countably subadditive monotone function that sends `∅` to `0`. -/
structure outer_measure (α : Type*) :=
(measure_of : set α → ennreal)
(empty : measure_of ∅ = 0)
(mono : ∀{s₁ s₂}, s₁ ⊆ s₂ → measure_of s₁ ≤ measure_of s₂)
(Union_nat : ∀(s:ℕ → set α), measure_of (⋃i, s i) ≤ (∑'i, measure_of (s i)))
namespace outer_measure
instance {α} : has_coe_to_fun (outer_measure α) := ⟨_, λ m, m.measure_of⟩
section basic
variables {α : Type*} {ms : set (outer_measure α)} {m : outer_measure α}
@[simp] theorem empty' (m : outer_measure α) : m ∅ = 0 := m.empty
theorem mono' (m : outer_measure α) {s₁ s₂}
(h : s₁ ⊆ s₂) : m s₁ ≤ m s₂ := m.mono h
theorem Union_aux (m : set α → ennreal) (m0 : m ∅ = 0)
{β} [encodable β] (s : β → set α) :
(∑' b, m (s b)) = ∑' i, m (⋃ b ∈ decode2 β i, s b) :=
begin
have H : ∀ n, m (⋃ b ∈ decode2 β n, s b) ≠ 0 → (decode2 β n).is_some,
{ intros n h,
cases decode2 β n with b,
{ exact (h (by simp [m0])).elim },
{ exact rfl } },
refine tsum_eq_tsum_of_ne_zero_bij (λ n h, option.get (H n h)) _ _ _,
{ intros m n hm hn e,
have := mem_decode2.1 (option.get_mem (H n hn)),
rwa [← e, mem_decode2.1 (option.get_mem (H m hm))] at this },
{ intros b h,
refine ⟨encode b, _, _⟩,
{ convert h, simp [set.ext_iff, encodek2] },
{ exact option.get_of_mem _ (encodek2 _) } },
{ intros n h,
transitivity, swap,
rw [show decode2 β n = _, from option.get_mem (H n h)],
congr, simp [set.ext_iff, -option.some_get] }
end
protected theorem Union (m : outer_measure α)
{β} [encodable β] (s : β → set α) :
m (⋃i, s i) ≤ (∑'i, m (s i)) :=
by rw [Union_decode2, Union_aux _ m.empty' s]; exact m.Union_nat _
lemma Union_null (m : outer_measure α)
{β} [encodable β] {s : β → set α} (h : ∀ i, m (s i) = 0) : m (⋃i, s i) = 0 :=
by simpa [h] using m.Union s
protected lemma union (m : outer_measure α) (s₁ s₂ : set α) :
m (s₁ ∪ s₂) ≤ m s₁ + m s₂ :=
begin
convert m.Union (λ b, cond b s₁ s₂),
{ simp [union_eq_Union] },
{ rw tsum_fintype, change _ = _ + _, simp }
end
lemma union_null (m : outer_measure α) {s₁ s₂ : set α}
(h₁ : m s₁ = 0) (h₂ : m s₂ = 0) : m (s₁ ∪ s₂) = 0 :=
by simpa [h₁, h₂] using m.union s₁ s₂
@[ext] lemma ext : ∀{μ₁ μ₂ : outer_measure α},
(∀s, μ₁ s = μ₂ s) → μ₁ = μ₂
| ⟨m₁, e₁, _, u₁⟩ ⟨m₂, e₂, _, u₂⟩ h := by congr; exact funext h
instance : has_zero (outer_measure α) :=
⟨{ measure_of := λ_, 0,
empty := rfl,
mono := assume _ _ _, le_refl 0,
Union_nat := assume s, zero_le _ }⟩
@[simp] theorem zero_apply (s : set α) : (0 : outer_measure α) s = 0 := rfl
instance : inhabited (outer_measure α) := ⟨0⟩
instance : has_add (outer_measure α) :=
⟨λm₁ m₂,
{ measure_of := λs, m₁ s + m₂ s,
empty := show m₁ ∅ + m₂ ∅ = 0, by simp [outer_measure.empty],
mono := assume s₁ s₂ h, add_le_add (m₁.mono h) (m₂.mono h),
Union_nat := assume s,
calc m₁ (⋃i, s i) + m₂ (⋃i, s i) ≤
(∑'i, m₁ (s i)) + (∑'i, m₂ (s i)) :
add_le_add (m₁.Union_nat s) (m₂.Union_nat s)
... = _ : ennreal.tsum_add.symm}⟩
@[simp] theorem add_apply (m₁ m₂ : outer_measure α) (s : set α) :
(m₁ + m₂) s = m₁ s + m₂ s := rfl
instance add_comm_monoid : add_comm_monoid (outer_measure α) :=
{ zero := 0,
add := (+),
add_comm := assume a b, ext $ assume s, add_comm _ _,
add_assoc := assume a b c, ext $ assume s, add_assoc _ _ _,
add_zero := assume a, ext $ assume s, add_zero _,
zero_add := assume a, ext $ assume s, by simp }
instance : has_bot (outer_measure α) := ⟨0⟩
instance outer_measure.order_bot : order_bot (outer_measure α) :=
{ le := λm₁ m₂, ∀s, m₁ s ≤ m₂ s,
bot := 0,
le_refl := assume a s, le_refl _,
le_trans := assume a b c hab hbc s, le_trans (hab s) (hbc s),
le_antisymm := assume a b hab hba, ext $ assume s, le_antisymm (hab s) (hba s),
bot_le := assume a s, zero_le _ }
section supremum
instance : has_Sup (outer_measure α) :=
⟨λms, {
measure_of := λs, ⨆ m ∈ ms, (m : outer_measure α) s,
empty := le_zero_iff_eq.1 $ bsupr_le $ λ m h, le_of_eq m.empty,
mono := assume s₁ s₂ hs, bsupr_le_bsupr $ assume m hm, m.mono hs,
Union_nat := assume f, bsupr_le $ assume m hm,
calc m (⋃i, f i) ≤ (∑' (i : ℕ), m (f i)) : m.Union_nat _
... ≤ (∑'i, ⨆ m ∈ ms, (m : outer_measure α) (f i)) :
ennreal.tsum_le_tsum $ assume i, le_bsupr m hm }⟩
instance : complete_lattice (outer_measure α) :=
{ .. outer_measure.order_bot, .. complete_lattice_of_Sup (outer_measure α)
(λ ms, ⟨λ m hm s, le_bsupr m hm, λ m hm s, bsupr_le (λ m' hm', hm hm' s)⟩) }
@[simp] theorem Sup_apply (ms : set (outer_measure α)) (s : set α) :
(Sup ms) s = ⨆ m ∈ ms, (m : outer_measure α) s := rfl
@[simp] theorem supr_apply {ι} (f : ι → outer_measure α) (s : set α) :
(⨆ i : ι, f i) s = ⨆ i, f i s :=
by rw [supr, Sup_apply, supr_range, supr]
@[norm_cast] theorem coe_supr {ι} (f : ι → outer_measure α) :
⇑(⨆ i, f i) = ⨆ i, f i :=
funext $ λ s, by rw [supr_apply, _root_.supr_apply]
@[simp] theorem sup_apply (m₁ m₂ : outer_measure α) (s : set α) :
(m₁ ⊔ m₂) s = m₁ s ⊔ m₂ s :=
by have := supr_apply (λ b, cond b m₁ m₂) s;
rwa [supr_bool_eq, supr_bool_eq] at this
end supremum
/-- The pushforward of `m` along `f`. The outer measure on `s` is defined to be `m (f ⁻¹' s)`. -/
def map {β} (f : α → β) (m : outer_measure α) : outer_measure β :=
{ measure_of := λs, m (f ⁻¹' s),
empty := m.empty,
mono := λ s t h, m.mono (preimage_mono h),
Union_nat := λ s, by rw [preimage_Union]; exact
m.Union_nat (λ i, f ⁻¹' s i) }
@[simp] theorem map_apply {β} (f : α → β)
(m : outer_measure α) (s : set β) : map f m s = m (f ⁻¹' s) := rfl
@[simp] theorem map_id (m : outer_measure α) : map id m = m :=
ext $ λ s, rfl
@[simp] theorem map_map {β γ} (f : α → β) (g : β → γ)
(m : outer_measure α) : map g (map f m) = map (g ∘ f) m :=
ext $ λ s, rfl
instance : functor outer_measure := {map := λ α β, map}
instance : is_lawful_functor outer_measure :=
{ id_map := λ α, map_id,
comp_map := λ α β γ f g m, (map_map f g m).symm }
/-- The dirac outer measure. -/
def dirac (a : α) : outer_measure α :=
{ measure_of := λs, ⨆ h : a ∈ s, 1,
empty := by simp,
mono := λ s t h, supr_le_supr2 (λ h', ⟨h h', le_refl _⟩),
Union_nat := λ s, supr_le $ λ h,
let ⟨i, h⟩ := mem_Union.1 h in
le_trans (by exact le_supr _ h) (ennreal.le_tsum i) }
@[simp] theorem dirac_apply (a : α) (s : set α) :
dirac a s = ⨆ h : a ∈ s, 1 := rfl
/-- The sum of an (arbitrary) collection of outer measures. -/
def sum {ι} (f : ι → outer_measure α) : outer_measure α :=
{ measure_of := λs, ∑' i, f i s,
empty := by simp,
mono := λ s t h, ennreal.tsum_le_tsum (λ i, (f i).mono' h),
Union_nat := λ s, by rw ennreal.tsum_comm; exact
ennreal.tsum_le_tsum (λ i, (f i).Union_nat _) }
@[simp] theorem sum_apply {ι} (f : ι → outer_measure α) (s : set α) :
sum f s = ∑' i, f i s := rfl
instance : has_scalar ennreal (outer_measure α) :=
⟨λ a m, {
measure_of := λs, a * m s,
empty := by simp,
mono := λ s t h, canonically_ordered_semiring.mul_le_mul (le_refl _) (m.mono' h),
Union_nat := λ s, by rw ennreal.tsum_mul_left; exact
canonically_ordered_semiring.mul_le_mul (le_refl _) (m.Union_nat _) }⟩
@[simp] theorem smul_apply (a : ennreal) (m : outer_measure α) (s : set α) :
(a • m) s = a * m s := rfl
instance : semimodule ennreal (outer_measure α) :=
{ smul_add := λ a m₁ m₂, ext $ λ s, mul_add _ _ _,
add_smul := λ a b m, ext $ λ s, add_mul _ _ _,
mul_smul := λ a b m, ext $ λ s, mul_assoc _ _ _,
one_smul := λ m, ext $ λ s, one_mul _,
zero_smul := λ m, ext $ λ s, zero_mul _,
smul_zero := λ a, ext $ λ s, mul_zero _,
..outer_measure.has_scalar }
theorem smul_dirac_apply (a : ennreal) (b : α) (s : set α) :
(a • dirac b) s = ⨆ h : b ∈ s, a :=
by by_cases b ∈ s; simp [h]
theorem top_apply {s : set α} (h : s.nonempty) : (⊤ : outer_measure α) s = ⊤ :=
let ⟨a, as⟩ := h in
top_unique $ le_trans (by simp [smul_dirac_apply, as]) (le_bsupr ((⊤ : ennreal) • dirac a) trivial)
end basic
section of_function
set_option eqn_compiler.zeta true
/-- Given any function `m` assigning measures to sets satisying `m ∅ = 0`, there is
a unique maximal outer measure `μ` satisfying `μ s ≤ m s` for all `s : set α`. -/
protected def of_function {α : Type*} (m : set α → ennreal) (m_empty : m ∅ = 0) :
outer_measure α :=
let μ := λs, ⨅{f : ℕ → set α} (h : s ⊆ ⋃i, f i), ∑'i, m (f i) in
{ measure_of := μ,
empty := le_antisymm
(infi_le_of_le (λ_, ∅) $ infi_le_of_le (empty_subset _) $ by simp [m_empty])
(zero_le _),
mono := assume s₁ s₂ hs, infi_le_infi $ assume f,
infi_le_infi2 $ assume hb, ⟨subset.trans hs hb, le_refl _⟩,
Union_nat := assume s, ennreal.le_of_forall_epsilon_le $ begin
assume ε hε (hb : (∑'i, μ (s i)) < ⊤),
rcases ennreal.exists_pos_sum_of_encodable (ennreal.coe_lt_coe.2 hε) ℕ with ⟨ε', hε', hl⟩,
refine le_trans _ (add_le_add_left (le_of_lt hl) _),
rw ← ennreal.tsum_add,
choose f hf using show
∀i, ∃f:ℕ → set α, s i ⊆ (⋃i, f i) ∧ (∑'i, m (f i)) < μ (s i) + ε' i,
{ intro,
have : μ (s i) < μ (s i) + ε' i :=
ennreal.lt_add_right
(lt_of_le_of_lt (by apply ennreal.le_tsum) hb)
(by simpa using hε' i),
simpa [μ, infi_lt_iff] },
refine le_trans _ (ennreal.tsum_le_tsum $ λ i, le_of_lt (hf i).2),
rw [← ennreal.tsum_prod, ← tsum_equiv equiv.nat_prod_nat_equiv_nat.symm],
swap, {apply_instance},
refine infi_le_of_le _ (infi_le _ _),
exact Union_subset (λ i, subset.trans (hf i).1 $
Union_subset $ λ j, subset.trans (by simp) $
subset_Union _ $ equiv.nat_prod_nat_equiv_nat (i, j)),
end }
theorem of_function_le {α : Type*} (m : set α → ennreal) (m_empty s) :
outer_measure.of_function m m_empty s ≤ m s :=
let f : ℕ → set α := λi, nat.rec_on i s (λn s, ∅) in
infi_le_of_le f $ infi_le_of_le (subset_Union f 0) $ le_of_eq $
calc (∑'i, m (f i)) = ∑ i in {0}, m (f i) :
tsum_eq_sum $ by intro i; cases i; simp [m_empty]
... = m s : by simp; refl
theorem le_of_function {α : Type*} {m m_empty} {μ : outer_measure α} :
μ ≤ outer_measure.of_function m m_empty ↔ ∀ s, μ s ≤ m s :=
⟨λ H s, le_trans (H _) (of_function_le _ _ _),
λ H s, le_infi $ λ f, le_infi $ λ hs,
le_trans (μ.mono hs) $ le_trans (μ.Union f) $
ennreal.tsum_le_tsum $ λ i, H _⟩
end of_function
section caratheodory_measurable
universe u
parameters {α : Type u} (m : outer_measure α)
include m
local attribute [simp] set.inter_comm set.inter_left_comm set.inter_assoc
variables {s s₁ s₂ : set α}
/-- A set `s` is Carathéodory-measurable for an outer measure `m` if for all sets `t` we have
`m t = m (t ∩ s) + m (t \ s)`. -/
private def C (s : set α) : Prop := ∀t, m t = m (t ∩ s) + m (t \ s)
private lemma C_iff_le {s : set α} : C s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t :=
forall_congr $ λ t, le_antisymm_iff.trans $ and_iff_right $
by convert m.union _ _; rw inter_union_diff t s
@[simp] private lemma C_empty : C ∅ := by simp [C, m.empty, diff_empty]
private lemma C_compl : C s₁ → C s₁ᶜ := by simp [C, diff_eq, add_comm]
@[simp] private lemma C_compl_iff : C sᶜ ↔ C s :=
⟨λ h, by simpa using C_compl m h, C_compl⟩
private lemma C_union (h₁ : C s₁) (h₂ : C s₂) : C (s₁ ∪ s₂) :=
λ t, begin
rw [h₁ t, h₂ (t ∩ s₁), h₂ (t \ s₁), h₁ (t ∩ (s₁ ∪ s₂)),
inter_diff_assoc _ _ s₁, set.inter_assoc _ _ s₁,
inter_eq_self_of_subset_right (set.subset_union_left _ _),
union_diff_left, h₂ (t ∩ s₁)],
simp [diff_eq, add_assoc]
end
private lemma measure_inter_union (h : s₁ ∩ s₂ ⊆ ∅) (h₁ : C s₁) {t : set α} :
m (t ∩ (s₁ ∪ s₂)) = m (t ∩ s₁) + m (t ∩ s₂) :=
by rw [h₁, set.inter_assoc, set.union_inter_cancel_left,
inter_diff_assoc, union_diff_cancel_left h]
private lemma C_Union_lt {s : ℕ → set α} : ∀{n:ℕ}, (∀i<n, C (s i)) → C (⋃i<n, s i)
| 0 h := by simp [nat.not_lt_zero]
| (n + 1) h := by rw Union_lt_succ; exact C_union m
(h n (le_refl (n + 1)))
(C_Union_lt $ assume i hi, h i $ lt_of_lt_of_le hi $ nat.le_succ _)
private lemma C_inter (h₁ : C s₁) (h₂ : C s₂) : C (s₁ ∩ s₂) :=
by rw [← C_compl_iff, compl_inter]; from C_union _ (C_compl _ h₁) (C_compl _ h₂)
private lemma C_sum {s : ℕ → set α} (h : ∀i, C (s i)) (hd : pairwise (disjoint on s)) {t : set α} :
∀ {n}, ∑ i in finset.range n, m (t ∩ s i) = m (t ∩ ⋃i<n, s i)
| 0 := by simp [nat.not_lt_zero, m.empty]
| (nat.succ n) := begin
simp [Union_lt_succ, range_succ],
rw [measure_inter_union m _ (h n), C_sum],
intro a, simpa [range_succ] using λ h₁ i hi h₂, hd _ _ (ne_of_gt hi) ⟨h₁, h₂⟩
end
private lemma C_Union_nat {s : ℕ → set α} (h : ∀i, C (s i))
(hd : pairwise (disjoint on s)) : C (⋃i, s i) :=
C_iff_le.2 $ λ t, begin
have hp : m (t ∩ ⋃i, s i) ≤ (⨆n, m (t ∩ ⋃i<n, s i)),
{ convert m.Union (λ i, t ∩ s i),
{ rw inter_Union },
{ simp [ennreal.tsum_eq_supr_nat, C_sum m h hd] } },
refine le_trans (add_le_add_right hp _) _,
rw ennreal.supr_add,
refine supr_le (λ n, le_trans (add_le_add_left _ _)
(ge_of_eq (C_Union_lt m (λ i _, h i) _))),
refine m.mono (diff_subset_diff_right _),
exact bUnion_subset (λ i _, subset_Union _ i),
end
private lemma f_Union {s : ℕ → set α} (h : ∀i, C (s i))
(hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑'i, m (s i) :=
begin
refine le_antisymm (m.Union_nat s) _,
rw ennreal.tsum_eq_supr_nat,
refine supr_le (λ n, _),
have := @C_sum _ m _ h hd univ n,
simp at this, simp [this],
exact m.mono (bUnion_subset (λ i _, subset_Union _ i)),
end
/-- The Carathéodory-measurable sets for an outer measure `m` form a Dynkin system. -/
private def caratheodory_dynkin : measurable_space.dynkin_system α :=
{ has := C,
has_empty := C_empty,
has_compl := assume s, C_compl,
has_Union_nat := assume f hf hn, C_Union_nat hn hf }
/-- Given an outer measure `μ`, the Carathéodory-measurable space is
defined such that `s` is measurable if `∀t, μ t = μ (t ∩ s) + μ (t \ s)`. -/
protected def caratheodory : measurable_space α :=
caratheodory_dynkin.to_measurable_space $ assume s₁ s₂, C_inter
lemma is_caratheodory {s : set α} :
caratheodory.is_measurable s ↔ ∀t, m t = m (t ∩ s) + m (t \ s) :=
iff.rfl
lemma is_caratheodory_le {s : set α} :
caratheodory.is_measurable s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t :=
C_iff_le
protected lemma Union_eq_of_caratheodory {s : ℕ → set α}
(h : ∀i, caratheodory.is_measurable (s i)) (hd : pairwise (disjoint on s)) :
m (⋃i, s i) = ∑'i, m (s i) :=
f_Union h hd
end caratheodory_measurable
variables {α : Type*}
lemma caratheodory_is_measurable {m : set α → ennreal} {s : set α}
{h₀ : m ∅ = 0} (hs : ∀t, m (t ∩ s) + m (t \ s) ≤ m t) :
(outer_measure.of_function m h₀).caratheodory.is_measurable s :=
let o := (outer_measure.of_function m h₀) in
(is_caratheodory_le o).2 $ λ t,
le_infi $ λ f, le_infi $ λ hf, begin
refine le_trans (add_le_add
(infi_le_of_le (λi, f i ∩ s) $ infi_le _ _)
(infi_le_of_le (λi, f i \ s) $ infi_le _ _)) _,
{ rw ← Union_inter,
exact inter_subset_inter_left _ hf },
{ rw ← Union_diff,
exact diff_subset_diff_left hf },
{ rw ← ennreal.tsum_add,
exact ennreal.tsum_le_tsum (λ i, hs _) }
end
@[simp] theorem zero_caratheodory : (0 : outer_measure α).caratheodory = ⊤ :=
top_unique $ λ s _ t, (add_zero _).symm
theorem top_caratheodory : (⊤ : outer_measure α).caratheodory = ⊤ :=
top_unique $ assume s hs, (is_caratheodory_le _).2 $ assume t,
t.eq_empty_or_nonempty.elim (λ ht, by simp [ht])
(λ ht, by simp only [ht, top_apply, le_top])
theorem le_add_caratheodory (m₁ m₂ : outer_measure α) :
m₁.caratheodory ⊓ m₂.caratheodory ≤ (m₁ + m₂ : outer_measure α).caratheodory :=
λ s ⟨hs₁, hs₂⟩ t, by simp [hs₁ t, hs₂ t, add_left_comm, add_assoc]
theorem le_sum_caratheodory {ι} (m : ι → outer_measure α) :
(⨅ i, (m i).caratheodory) ≤ (sum m).caratheodory :=
λ s h t, by simp [λ i,
measurable_space.is_measurable_infi.1 h i t, ennreal.tsum_add]
theorem le_smul_caratheodory (a : ennreal) (m : outer_measure α) :
m.caratheodory ≤ (a • m).caratheodory :=
λ s h t, by simp [h t, mul_add]
@[simp] theorem dirac_caratheodory (a : α) : (dirac a).caratheodory = ⊤ :=
top_unique $ λ s _ t, begin
by_cases a ∈ t; simp [h],
by_cases a ∈ s; simp [h]
end
section Inf_gen
/-- Given a set of outer measures, we define a new function that on a set `s` is defined to be the
infimum of `μ(s)` for the outer measures `μ` in the collection. We ensure that this
function is defined to be `0` on `∅`, even if the collection of outer measures is empty.
The outer measure generated by this function is the infimum of the given outer measures. -/
def Inf_gen (m : set (outer_measure α)) (s : set α) : ennreal :=
⨆(h : s.nonempty), ⨅ (μ : outer_measure α) (h : μ ∈ m), μ s
@[simp] lemma Inf_gen_empty (m : set (outer_measure α)) : Inf_gen m ∅ = 0 :=
by simp [Inf_gen, empty_not_nonempty]
lemma Inf_gen_nonempty1 (m : set (outer_measure α)) (t : set α) (h : t.nonempty) :
Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) :=
by rw [Inf_gen, supr_pos h]
lemma Inf_gen_nonempty2 (m : set (outer_measure α)) (μ) (h : μ ∈ m) (t) :
Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) :=
begin
cases t.eq_empty_or_nonempty with ht ht,
{ simp [ht],
refine (bot_unique $ infi_le_of_le μ $ _).symm,
refine infi_le_of_le h (le_refl ⊥) },
{ exact Inf_gen_nonempty1 m t ht }
end
lemma Inf_eq_of_function_Inf_gen (m : set (outer_measure α)) :
Inf m = outer_measure.of_function (Inf_gen m) (Inf_gen_empty m) :=
begin
refine le_antisymm
(assume t', le_of_function.2 (assume t, _) _)
(_root_.le_Inf $ assume μ hμ t, le_trans (outer_measure.of_function_le _ _ _) _);
cases t.eq_empty_or_nonempty with ht ht; simp [ht, Inf_gen_nonempty1],
{ assume μ hμ, exact (show Inf m ≤ μ, from _root_.Inf_le hμ) t },
{ exact infi_le_of_le μ (infi_le _ hμ) }
end
end Inf_gen
end outer_measure
end measure_theory
|
56ee0d3d34b1b1d47bc618c2dd850a18e12a6924 | a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7 | /src/topology/uniform_space/completion.lean | dfe0d963e30f24705cdb2115d463500410bf0184 | [
"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 | 23,262 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Johannes Hölzl
Hausdorff completions of uniform spaces.
The goal is to construct a left-adjoint to the inclusion of complete Hausdorff uniform spaces
into all uniform spaces. Any uniform space `α` gets a completion `completion α` and a morphism
(ie. uniformly continuous map) `completion : α → completion α` which solves the universal
mapping problem of factorizing morphisms from `α` to any complete Hausdorff uniform space `β`.
It means any uniformly continuous `f : α → β` gives rise to a unique morphism
`completion.extension f : completion α → β` such that `f = completion.extension f ∘ completion α`.
Actually `completion.extension f` is defined for all maps from `α` to `β` but it has the desired
properties only if `f` is uniformly continuous.
Beware that `completion α` is not injective if `α` is not Hausdorff. But its image is always
dense. The adjoint functor acting on morphisms is then constructed by the usual abstract nonsense.
For every uniform spaces `α` and `β`, it turns `f : α → β` into a morphism
`completion.map f : completion α → completion β`
such that
`coe ∘ f = (completion.map f) ∘ coe`
provided `f` is uniformly continuous. This construction is compatible with composition.
In this file we introduce the following concepts:
* `Cauchy α` the uniform completion of the uniform space `α` (using Cauchy filters). These are not
minimal filters.
* `completion α := quotient (separation_setoid (Cauchy α))` the Hausdorff completion.
This formalization is mostly based on
N. Bourbaki: General Topology
I. M. James: Topologies and Uniformities
From a slightly different perspective in order to reuse material in topology.uniform_space.basic.
-/
import topology.uniform_space.abstract_completion
noncomputable theory
open filter set
universes u v w x
open_locale uniformity classical topological_space filter
/-- Space of Cauchy filters
This is essentially the completion of a uniform space. The embeddings are the neighbourhood filters.
This space is not minimal, the separated uniform space (i.e. quotiented on the intersection of all
entourages) is necessary for this.
-/
def Cauchy (α : Type u) [uniform_space α] : Type u := { f : filter α // cauchy f }
namespace Cauchy
section
parameters {α : Type u} [uniform_space α]
variables {β : Type v} {γ : Type w}
variables [uniform_space β] [uniform_space γ]
def gen (s : set (α × α)) : set (Cauchy α × Cauchy α) :=
{p | s ∈ filter.prod (p.1.val) (p.2.val) }
lemma monotone_gen : monotone gen :=
monotone_set_of $ assume p, @monotone_mem_sets (α×α) (filter.prod (p.1.val) (p.2.val))
private lemma symm_gen : map prod.swap ((𝓤 α).lift' gen) ≤ (𝓤 α).lift' gen :=
calc map prod.swap ((𝓤 α).lift' gen) =
(𝓤 α).lift' (λs:set (α×α), {p | s ∈ filter.prod (p.2.val) (p.1.val) }) :
begin
delta gen,
simp [map_lift'_eq, monotone_set_of, monotone_mem_sets,
function.comp, image_swap_eq_preimage_swap, -subtype.val_eq_coe]
end
... ≤ (𝓤 α).lift' gen :
uniformity_lift_le_swap
(monotone_principal.comp (monotone_set_of $ assume p,
@monotone_mem_sets (α×α) ((filter.prod ((p.2).val) ((p.1).val)))))
begin
have h := λ(p:Cauchy α×Cauchy α), @filter.prod_comm _ _ (p.2.val) (p.1.val),
simp [function.comp, h, -subtype.val_eq_coe],
exact le_refl _
end
private lemma comp_rel_gen_gen_subset_gen_comp_rel {s t : set (α×α)} : comp_rel (gen s) (gen t) ⊆
(gen (comp_rel s t) : set (Cauchy α × Cauchy α)) :=
assume ⟨f, g⟩ ⟨h, h₁, h₂⟩,
let ⟨t₁, (ht₁ : t₁ ∈ f.val), t₂, (ht₂ : t₂ ∈ h.val), (h₁ : set.prod t₁ t₂ ⊆ s)⟩ :=
mem_prod_iff.mp h₁ in
let ⟨t₃, (ht₃ : t₃ ∈ h.val), t₄, (ht₄ : t₄ ∈ g.val), (h₂ : set.prod t₃ t₄ ⊆ t)⟩ :=
mem_prod_iff.mp h₂ in
have t₂ ∩ t₃ ∈ h.val,
from inter_mem_sets ht₂ ht₃,
let ⟨x, xt₂, xt₃⟩ :=
nonempty_of_mem_sets (h.property.left) this in
(filter.prod f.val g.val).sets_of_superset
(prod_mem_prod ht₁ ht₄)
(assume ⟨a, b⟩ ⟨(ha : a ∈ t₁), (hb : b ∈ t₄)⟩,
⟨x,
h₁ (show (a, x) ∈ set.prod t₁ t₂, from ⟨ha, xt₂⟩),
h₂ (show (x, b) ∈ set.prod t₃ t₄, from ⟨xt₃, hb⟩)⟩)
private lemma comp_gen :
((𝓤 α).lift' gen).lift' (λs, comp_rel s s) ≤ (𝓤 α).lift' gen :=
calc ((𝓤 α).lift' gen).lift' (λs, comp_rel s s) =
(𝓤 α).lift' (λs, comp_rel (gen s) (gen s)) :
begin
rw [lift'_lift'_assoc],
exact monotone_gen,
exact (monotone_comp_rel monotone_id monotone_id)
end
... ≤ (𝓤 α).lift' (λs, gen $ comp_rel s s) :
lift'_mono' $ assume s hs, comp_rel_gen_gen_subset_gen_comp_rel
... = ((𝓤 α).lift' $ λs:set(α×α), comp_rel s s).lift' gen :
begin
rw [lift'_lift'_assoc],
exact (monotone_comp_rel monotone_id monotone_id),
exact monotone_gen
end
... ≤ (𝓤 α).lift' gen : lift'_mono comp_le_uniformity (le_refl _)
instance : uniform_space (Cauchy α) :=
uniform_space.of_core
{ uniformity := (𝓤 α).lift' gen,
refl := principal_le_lift' $ assume s hs ⟨a, b⟩ (a_eq_b : a = b),
a_eq_b ▸ a.property.right hs,
symm := symm_gen,
comp := comp_gen }
theorem mem_uniformity {s : set (Cauchy α × Cauchy α)} :
s ∈ 𝓤 (Cauchy α) ↔ ∃ t ∈ 𝓤 α, gen t ⊆ s :=
mem_lift'_sets monotone_gen
theorem mem_uniformity' {s : set (Cauchy α × Cauchy α)} :
s ∈ 𝓤 (Cauchy α) ↔ ∃ t ∈ 𝓤 α,
∀ f g : Cauchy α, t ∈ filter.prod f.1 g.1 → (f, g) ∈ s :=
mem_uniformity.trans $ bex_congr $ λ t h, prod.forall
/-- Embedding of `α` into its completion -/
def pure_cauchy (a : α) : Cauchy α :=
⟨pure a, cauchy_pure⟩
lemma uniform_inducing_pure_cauchy : uniform_inducing (pure_cauchy : α → Cauchy α) :=
⟨have (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) = id,
from funext $ assume s, set.ext $ assume ⟨a₁, a₂⟩,
by simp [preimage, gen, pure_cauchy, prod_principal_principal],
calc comap (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ((𝓤 α).lift' gen)
= (𝓤 α).lift' (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) :
comap_lift'_eq monotone_gen
... = 𝓤 α : by simp [this]⟩
lemma uniform_embedding_pure_cauchy : uniform_embedding (pure_cauchy : α → Cauchy α) :=
{ inj := assume a₁ a₂ h, pure_injective $ subtype.ext_iff_val.1 h,
..uniform_inducing_pure_cauchy }
lemma pure_cauchy_dense : ∀x, x ∈ closure (range pure_cauchy) :=
assume f,
have h_ex : ∀ s ∈ 𝓤 (Cauchy α), ∃y:α, (f, pure_cauchy y) ∈ s, from
assume s hs,
let ⟨t'', ht''₁, (ht''₂ : gen t'' ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in
let ⟨t', ht'₁, ht'₂⟩ := comp_mem_uniformity_sets ht''₁ in
have t' ∈ filter.prod (f.val) (f.val),
from f.property.right ht'₁,
let ⟨t, ht, (h : set.prod t t ⊆ t')⟩ := mem_prod_same_iff.mp this in
let ⟨x, (hx : x ∈ t)⟩ := nonempty_of_mem_sets f.property.left ht in
have t'' ∈ filter.prod f.val (pure x),
from mem_prod_iff.mpr ⟨t, ht, {y:α | (x, y) ∈ t'},
h $ mk_mem_prod hx hx,
assume ⟨a, b⟩ ⟨(h₁ : a ∈ t), (h₂ : (x, b) ∈ t')⟩,
ht'₂ $ prod_mk_mem_comp_rel (@h (a, x) ⟨h₁, hx⟩) h₂⟩,
⟨x, ht''₂ $ by dsimp [gen]; exact this⟩,
begin
simp [closure_eq_cluster_pts, cluster_pt, nhds_eq_uniformity, lift'_inf_principal_eq, set.inter_comm],
exact (lift'_ne_bot_iff $ monotone_inter monotone_const monotone_preimage).mpr
(assume s hs,
let ⟨y, hy⟩ := h_ex s hs in
have pure_cauchy y ∈ range pure_cauchy ∩ {y : Cauchy α | (f, y) ∈ s},
from ⟨mem_range_self y, hy⟩,
⟨_, this⟩)
end
lemma dense_inducing_pure_cauchy : dense_inducing pure_cauchy :=
uniform_inducing_pure_cauchy.dense_inducing pure_cauchy_dense
lemma dense_embedding_pure_cauchy : dense_embedding pure_cauchy :=
uniform_embedding_pure_cauchy.dense_embedding pure_cauchy_dense
lemma nonempty_Cauchy_iff : nonempty (Cauchy α) ↔ nonempty α :=
begin
split ; rintro ⟨c⟩,
{ have := eq_univ_iff_forall.1 dense_embedding_pure_cauchy.to_dense_inducing.closure_range c,
obtain ⟨_, ⟨_, a, _⟩⟩ := mem_closure_iff.1 this _ is_open_univ trivial,
exact ⟨a⟩ },
{ exact ⟨pure_cauchy c⟩ }
end
section
set_option eqn_compiler.zeta true
instance : complete_space (Cauchy α) :=
complete_space_extension
uniform_inducing_pure_cauchy
pure_cauchy_dense $
assume f hf,
let f' : Cauchy α := ⟨f, hf⟩ in
have map pure_cauchy f ≤ (𝓤 $ Cauchy α).lift' (preimage (prod.mk f')),
from le_lift' $ assume s hs,
let ⟨t, ht₁, (ht₂ : gen t ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in
let ⟨t', ht', (h : set.prod t' t' ⊆ t)⟩ := mem_prod_same_iff.mp (hf.right ht₁) in
have t' ⊆ { y : α | (f', pure_cauchy y) ∈ gen t },
from assume x hx, (filter.prod f (pure x)).sets_of_superset (prod_mem_prod ht' hx) h,
f.sets_of_superset ht' $ subset.trans this (preimage_mono ht₂),
⟨f', by simp [nhds_eq_uniformity]; assumption⟩
end
instance [inhabited α] : inhabited (Cauchy α) :=
⟨pure_cauchy $ default α⟩
instance [h : nonempty α] : nonempty (Cauchy α) :=
h.rec_on $ assume a, nonempty.intro $ Cauchy.pure_cauchy a
section extend
def extend (f : α → β) : (Cauchy α → β) :=
if uniform_continuous f then
dense_inducing_pure_cauchy.extend f
else
λ x, f (classical.inhabited_of_nonempty $ nonempty_Cauchy_iff.1 ⟨x⟩).default
variables [separated_space β]
lemma extend_pure_cauchy {f : α → β} (hf : uniform_continuous f) (a : α) :
extend f (pure_cauchy a) = f a :=
begin
rw [extend, if_pos hf],
exact uniformly_extend_of_ind uniform_inducing_pure_cauchy pure_cauchy_dense hf _
end
variables [_root_.complete_space β]
lemma uniform_continuous_extend {f : α → β} : uniform_continuous (extend f) :=
begin
by_cases hf : uniform_continuous f,
{ rw [extend, if_pos hf],
exact uniform_continuous_uniformly_extend uniform_inducing_pure_cauchy pure_cauchy_dense hf },
{ rw [extend, if_neg hf],
exact uniform_continuous_of_const (assume a b, by congr) }
end
end extend
end
theorem Cauchy_eq
{α : Type*} [inhabited α] [uniform_space α] [complete_space α] [separated_space α] {f g : Cauchy α} :
Lim f.1 = Lim g.1 ↔ (f, g) ∈ separation_rel (Cauchy α) :=
begin
split,
{ intros e s hs,
rcases Cauchy.mem_uniformity'.1 hs with ⟨t, tu, ts⟩,
apply ts,
rcases comp_mem_uniformity_sets tu with ⟨d, du, dt⟩,
refine mem_prod_iff.2
⟨_, f.2.le_nhds_Lim (mem_nhds_right (Lim f.1) du),
_, g.2.le_nhds_Lim (mem_nhds_left (Lim g.1) du), λ x h, _⟩,
cases x with a b, cases h with h₁ h₂,
rw ← e at h₂,
exact dt ⟨_, h₁, h₂⟩ },
{ intros H,
refine separated_def.1 (by apply_instance) _ _ (λ t tu, _),
rcases mem_uniformity_is_closed tu with ⟨d, du, dc, dt⟩,
refine H {p | (Lim p.1.1, Lim p.2.1) ∈ t}
(Cauchy.mem_uniformity'.2 ⟨d, du, λ f g h, _⟩),
rcases mem_prod_iff.1 h with ⟨x, xf, y, yg, h⟩,
have limc : ∀ (f : Cauchy α) (x ∈ f.1), Lim f.1 ∈ closure x,
{ intros f x xf,
rw closure_eq_cluster_pts,
exact ne_bot_of_le_ne_bot f.2.1
(le_inf f.2.le_nhds_Lim (le_principal_iff.2 xf)) },
have := dc.closure_subset_iff.2 h,
rw closure_prod_eq at this,
refine dt (this ⟨_, _⟩); dsimp; apply limc; assumption }
end
section
local attribute [instance] uniform_space.separation_setoid
lemma separated_pure_cauchy_injective {α : Type*} [uniform_space α] [s : separated_space α] :
function.injective (λa:α, ⟦pure_cauchy a⟧) | a b h :=
separated_def.1 s _ _ $ assume s hs,
let ⟨t, ht, hts⟩ :=
by rw [← (@uniform_embedding_pure_cauchy α _).comap_uniformity, filter.mem_comap_sets] at hs; exact hs in
have (pure_cauchy a, pure_cauchy b) ∈ t, from quotient.exact h t ht,
@hts (a, b) this
end
end Cauchy
local attribute [instance] uniform_space.separation_setoid
open Cauchy set
namespace uniform_space
variables (α : Type*) [uniform_space α]
variables {β : Type*} [uniform_space β]
variables {γ : Type*} [uniform_space γ]
instance complete_space_separation [h : complete_space α] :
complete_space (quotient (separation_setoid α)) :=
⟨assume f, assume hf : cauchy f,
have cauchy (f.comap (λx, ⟦x⟧)), from
cauchy_comap comap_quotient_le_uniformity hf $
comap_ne_bot_of_surj hf.left $ assume b, quotient.exists_rep _,
let ⟨x, (hx : f.comap (λx, ⟦x⟧) ≤ 𝓝 x)⟩ := complete_space.complete this in
⟨⟦x⟧, calc f = map (λx, ⟦x⟧) (f.comap (λx, ⟦x⟧)) :
(map_comap $ univ_mem_sets' $ assume b, quotient.exists_rep _).symm
... ≤ map (λx, ⟦x⟧) (𝓝 x) : map_mono hx
... ≤ _ : continuous_iff_continuous_at.mp uniform_continuous_quotient_mk.continuous _⟩⟩
/-- Hausdorff completion of `α` -/
def completion := quotient (separation_setoid $ Cauchy α)
namespace completion
instance [inhabited α] : inhabited (completion α) :=
by unfold completion; apply_instance
@[priority 50]
instance : uniform_space (completion α) := by dunfold completion ; apply_instance
instance : complete_space (completion α) := by dunfold completion ; apply_instance
instance : separated_space (completion α) := by dunfold completion ; apply_instance
instance : regular_space (completion α) := separated_regular
/-- Automatic coercion from `α` to its completion. Not always injective. -/
instance : has_coe_t α (completion α) := ⟨quotient.mk ∘ pure_cauchy⟩ -- note [use has_coe_t]
protected lemma coe_eq : (coe : α → completion α) = quotient.mk ∘ pure_cauchy := rfl
lemma comap_coe_eq_uniformity :
(𝓤 _).comap (λ(p:α×α), ((p.1 : completion α), (p.2 : completion α))) = 𝓤 α :=
begin
have : (λx:α×α, ((x.1 : completion α), (x.2 : completion α))) =
(λx:(Cauchy α)×(Cauchy α), (⟦x.1⟧, ⟦x.2⟧)) ∘ (λx:α×α, (pure_cauchy x.1, pure_cauchy x.2)),
{ ext ⟨a, b⟩; simp; refl },
rw [this, ← filter.comap_comap_comp],
change filter.comap _ (filter.comap _ (𝓤 $ quotient $ separation_setoid $ Cauchy α)) = 𝓤 α,
rw [comap_quotient_eq_uniformity, uniform_embedding_pure_cauchy.comap_uniformity]
end
lemma uniform_inducing_coe : uniform_inducing (coe : α → completion α) :=
⟨comap_coe_eq_uniformity α⟩
variables {α}
lemma dense : dense_range (coe : α → completion α) :=
begin
rw [dense_range_iff_closure_range, completion.coe_eq, range_comp],
exact quotient_dense_of_dense pure_cauchy_dense
end
variables (α)
def cpkg {α : Type*} [uniform_space α] : abstract_completion α :=
{ space := completion α,
coe := coe,
uniform_struct := by apply_instance,
complete := by apply_instance,
separation := by apply_instance,
uniform_inducing := completion.uniform_inducing_coe α,
dense := completion.dense }
instance abstract_completion.inhabited : inhabited (abstract_completion α) :=
⟨cpkg⟩
local attribute [instance]
abstract_completion.uniform_struct abstract_completion.complete abstract_completion.separation
lemma nonempty_completion_iff : nonempty (completion α) ↔ nonempty α :=
(dense_range.nonempty (cpkg.dense)).symm
lemma uniform_continuous_coe : uniform_continuous (coe : α → completion α) :=
cpkg.uniform_continuous_coe
lemma continuous_coe : continuous (coe : α → completion α) :=
cpkg.continuous_coe
lemma uniform_embedding_coe [separated_space α] : uniform_embedding (coe : α → completion α) :=
{ comap_uniformity := comap_coe_eq_uniformity α,
inj := separated_pure_cauchy_injective }
variable {α}
lemma dense_inducing_coe : dense_inducing (coe : α → completion α) :=
{ dense := dense,
..(uniform_inducing_coe α).inducing }
lemma dense_embedding_coe [separated_space α]: dense_embedding (coe : α → completion α) :=
{ inj := separated_pure_cauchy_injective,
..dense_inducing_coe }
lemma dense₂ : dense_range (λx:α × β, ((x.1 : completion α), (x.2 : completion β))) :=
dense.prod dense
lemma dense₃ :
dense_range (λx:α × (β × γ), ((x.1 : completion α), ((x.2.1 : completion β), (x.2.2 : completion γ)))) :=
dense.prod dense₂
@[elab_as_eliminator]
lemma induction_on {p : completion α → Prop}
(a : completion α) (hp : is_closed {a | p a}) (ih : ∀a:α, p a) : p a :=
is_closed_property dense hp ih a
@[elab_as_eliminator]
lemma induction_on₂ {p : completion α → completion β → Prop}
(a : completion α) (b : completion β)
(hp : is_closed {x : completion α × completion β | p x.1 x.2})
(ih : ∀(a:α) (b:β), p a b) : p a b :=
have ∀x : completion α × completion β, p x.1 x.2, from
is_closed_property dense₂ hp $ assume ⟨a, b⟩, ih a b,
this (a, b)
@[elab_as_eliminator]
lemma induction_on₃ {p : completion α → completion β → completion γ → Prop}
(a : completion α) (b : completion β) (c : completion γ)
(hp : is_closed {x : completion α × completion β × completion γ | p x.1 x.2.1 x.2.2})
(ih : ∀(a:α) (b:β) (c:γ), p a b c) : p a b c :=
have ∀x : completion α × completion β × completion γ, p x.1 x.2.1 x.2.2, from
is_closed_property dense₃ hp $ assume ⟨a, b, c⟩, ih a b c,
this (a, b, c)
lemma ext [t2_space β] {f g : completion α → β} (hf : continuous f) (hg : continuous g)
(h : ∀a:α, f a = g a) : f = g :=
cpkg.funext hf hg h
section extension
variables {f : α → β}
/-- "Extension" to the completion. It is defined for any map `f` but
returns an arbitrary constant value if `f` is not uniformly continuous -/
protected def extension (f : α → β) : completion α → β :=
cpkg.extend f
variables [separated_space β]
@[simp] lemma extension_coe (hf : uniform_continuous f) (a : α) : (completion.extension f) a = f a :=
cpkg.extend_coe hf a
variables [complete_space β]
lemma uniform_continuous_extension : uniform_continuous (completion.extension f) :=
cpkg.uniform_continuous_extend
lemma continuous_extension : continuous (completion.extension f) :=
cpkg.continuous_extend
lemma extension_unique (hf : uniform_continuous f) {g : completion α → β} (hg : uniform_continuous g)
(h : ∀ a : α, f a = g (a : completion α)) : completion.extension f = g :=
cpkg.extend_unique hf hg h
@[simp] lemma extension_comp_coe {f : completion α → β} (hf : uniform_continuous f) :
completion.extension (f ∘ coe) = f :=
cpkg.extend_comp_coe hf
end extension
section map
variables {f : α → β}
/-- Completion functor acting on morphisms -/
protected def map (f : α → β) : completion α → completion β :=
cpkg.map cpkg f
lemma uniform_continuous_map : uniform_continuous (completion.map f) :=
cpkg.uniform_continuous_map cpkg f
lemma continuous_map : continuous (completion.map f) :=
cpkg.continuous_map cpkg f
@[simp] lemma map_coe (hf : uniform_continuous f) (a : α) : (completion.map f) a = f a :=
cpkg.map_coe cpkg hf a
lemma map_unique {f : α → β} {g : completion α → completion β}
(hg : uniform_continuous g) (h : ∀a:α, ↑(f a) = g a) : completion.map f = g :=
cpkg.map_unique cpkg hg h
@[simp] lemma map_id : completion.map (@id α) = id :=
cpkg.map_id
lemma extension_map [complete_space γ] [separated_space γ] {f : β → γ} {g : α → β}
(hf : uniform_continuous f) (hg : uniform_continuous g) :
completion.extension f ∘ completion.map g = completion.extension (f ∘ g) :=
completion.ext (continuous_extension.comp continuous_map) continuous_extension $
by intro a; simp only [hg, hf, hf.comp hg, (∘), map_coe, extension_coe]
lemma map_comp {g : β → γ} {f : α → β} (hg : uniform_continuous g) (hf : uniform_continuous f) :
completion.map g ∘ completion.map f = completion.map (g ∘ f) :=
extension_map ((uniform_continuous_coe _).comp hg) hf
end map
/- In this section we construct isomorphisms between the completion of a uniform space and the
completion of its separation quotient -/
section separation_quotient_completion
def completion_separation_quotient_equiv (α : Type u) [uniform_space α] :
completion (separation_quotient α) ≃ completion α :=
begin
refine ⟨completion.extension (separation_quotient.lift (coe : α → completion α)),
completion.map quotient.mk, _, _⟩,
{ assume a,
refine induction_on a (is_closed_eq (continuous_map.comp continuous_extension) continuous_id) _,
rintros ⟨a⟩,
show completion.map quotient.mk (completion.extension (separation_quotient.lift coe) ↑⟦a⟧) = ↑⟦a⟧,
rw [extension_coe (separation_quotient.uniform_continuous_lift _),
separation_quotient.lift_mk (uniform_continuous_coe α),
completion.map_coe uniform_continuous_quotient_mk] ; apply_instance },
{ assume a,
refine completion.induction_on a (is_closed_eq (continuous_extension.comp continuous_map) continuous_id) _,
assume a,
rw [map_coe uniform_continuous_quotient_mk,
extension_coe (separation_quotient.uniform_continuous_lift _),
separation_quotient.lift_mk (uniform_continuous_coe α) _] ; apply_instance }
end
lemma uniform_continuous_completion_separation_quotient_equiv :
uniform_continuous ⇑(completion_separation_quotient_equiv α) :=
uniform_continuous_extension
lemma uniform_continuous_completion_separation_quotient_equiv_symm :
uniform_continuous ⇑(completion_separation_quotient_equiv α).symm :=
uniform_continuous_map
end separation_quotient_completion
section extension₂
variables (f : α → β → γ)
open function
protected def extension₂ (f : α → β → γ) : completion α → completion β → γ :=
cpkg.extend₂ cpkg f
variables [separated_space γ] {f}
@[simp] lemma extension₂_coe_coe (hf : uniform_continuous₂ f) (a : α) (b : β) :
completion.extension₂ f a b = f a b :=
cpkg.extension₂_coe_coe cpkg hf a b
variables [complete_space γ] (f)
lemma uniform_continuous_extension₂ : uniform_continuous₂ (completion.extension₂ f) :=
cpkg.uniform_continuous_extension₂ cpkg f
end extension₂
section map₂
open function
protected def map₂ (f : α → β → γ) : completion α → completion β → completion γ :=
cpkg.map₂ cpkg cpkg f
lemma uniform_continuous_map₂ (f : α → β → γ) : uniform_continuous₂ (completion.map₂ f) :=
cpkg.uniform_continuous_map₂ cpkg cpkg f
lemma continuous_map₂ {δ} [topological_space δ] {f : α → β → γ}
{a : δ → completion α} {b : δ → completion β} (ha : continuous a) (hb : continuous b) :
continuous (λd:δ, completion.map₂ f (a d) (b d)) :=
cpkg.continuous_map₂ cpkg cpkg ha hb
lemma map₂_coe_coe (a : α) (b : β) (f : α → β → γ) (hf : uniform_continuous₂ f) :
completion.map₂ f (a : completion α) (b : completion β) = f a b :=
cpkg.map₂_coe_coe cpkg cpkg a b f hf
end map₂
end completion
end uniform_space
|
1d24d036506048abc2443c6a5e21dcb35a70d739 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/blast_congr_bug.lean | 0604421da1d67f92b2db441d190644d54bf58528 | [
"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 | 270 | lean | constant P : Type₁
constant P_sub : subsingleton P
attribute P_sub [instance]
constant q : P → Prop
section
example (h₁ h₂ : P) : q h₁ = q h₂ :=
by simp
end
section
set_option blast.strategy "cc"
example (h₁ h₂ : P) : q h₁ = q h₂ :=
by blast
end
|
51321ae7d036a942fc6931892321385ddd6e4b1e | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/data/nat/modeq.lean | 0ca861770fde338c33134380ef831bd661490095 | [
"Apache-2.0"
] | permissive | vaibhavkarve/mathlib | a574aaf68c0a431a47fa82ce0637f0f769826bfe | 17f8340912468f49bdc30acdb9a9fa02eeb0473a | refs/heads/master | 1,621,263,802,637 | 1,585,399,588,000 | 1,585,399,588,000 | 250,833,447 | 0 | 0 | Apache-2.0 | 1,585,410,341,000 | 1,585,410,341,000 | null | UTF-8 | Lean | false | false | 12,140 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
Modular equality relation.
-/
import data.int.gcd algebra.ordered_ring tactic.abel
namespace nat
/-- Modular equality. `modeq n a b`, or `a ≡ b [MOD n]`, means
that `a - b` is a multiple of `n`. -/
@[derive decidable]
def modeq (n a b : ℕ) := a % n = b % n
notation a ` ≡ `:50 b ` [MOD `:50 n `]`:0 := modeq n a b
namespace modeq
variables {n m a b c d : ℕ}
@[refl] protected theorem refl (a : ℕ) : a ≡ a [MOD n] := @rfl _ _
@[symm] protected theorem symm : a ≡ b [MOD n] → b ≡ a [MOD n] := eq.symm
@[trans] protected theorem trans : a ≡ b [MOD n] → b ≡ c [MOD n] → a ≡ c [MOD n] := eq.trans
theorem modeq_zero_iff : a ≡ 0 [MOD n] ↔ n ∣ a :=
by rw [modeq, zero_mod, dvd_iff_mod_eq_zero]
theorem modeq_iff_dvd : a ≡ b [MOD n] ↔ (n:ℤ) ∣ b - a :=
by rw [modeq, eq_comm, ← int.coe_nat_inj', int.coe_nat_mod, int.coe_nat_mod,
int.mod_eq_mod_iff_mod_sub_eq_zero, int.dvd_iff_mod_eq_zero]
theorem modeq_of_dvd : (n:ℤ) ∣ b - a → a ≡ b [MOD n] := modeq_iff_dvd.2
theorem dvd_of_modeq : a ≡ b [MOD n] → (n:ℤ) ∣ b - a := modeq_iff_dvd.1
theorem mod_modeq (a n) : a % n ≡ a [MOD n] := nat.mod_mod _ _
theorem modeq_of_dvd_of_modeq (d : m ∣ n) (h : a ≡ b [MOD n]) : a ≡ b [MOD m] :=
modeq_of_dvd $ dvd_trans (int.coe_nat_dvd.2 d) (dvd_of_modeq h)
theorem modeq_mul_left' (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD (c * n)] :=
by unfold modeq at *; rw [mul_mod_mul_left, mul_mod_mul_left, h]
theorem modeq_mul_left (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD n] :=
modeq_of_dvd_of_modeq (dvd_mul_left _ _) $ modeq_mul_left' _ h
theorem modeq_mul_right' (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD (n * c)] :=
by rw [mul_comm a, mul_comm b, mul_comm n]; exact modeq_mul_left' c h
theorem modeq_mul_right (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD n] :=
by rw [mul_comm a, mul_comm b]; exact modeq_mul_left c h
theorem modeq_mul (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a * c ≡ b * d [MOD n] :=
(modeq_mul_left _ h₂).trans (modeq_mul_right _ h₁)
theorem modeq_add (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a + c ≡ b + d [MOD n] :=
modeq_of_dvd begin
convert dvd_add (dvd_of_modeq h₁) (dvd_of_modeq h₂) using 1,
simp [sub_eq_add_neg, add_left_comm, add_comm],
end
theorem modeq_add_cancel_left (h₁ : a ≡ b [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : c ≡ d [MOD n] :=
begin
simp only [modeq_iff_dvd] at *,
convert _root_.dvd_sub h₂ h₁ using 1,
simp [sub_eq_add_neg],
abel
end
theorem modeq_add_cancel_right (h₁ : c ≡ d [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : a ≡ b [MOD n] :=
by rw [add_comm a, add_comm b] at h₂; exact modeq_add_cancel_left h₁ h₂
theorem modeq_of_modeq_mul_left (m : ℕ) (h : a ≡ b [MOD m * n]) : a ≡ b [MOD n] :=
by rw [modeq_iff_dvd] at *; exact dvd.trans (dvd_mul_left (n : ℤ) (m : ℤ)) h
theorem modeq_of_modeq_mul_right (m : ℕ) : a ≡ b [MOD n * m] → a ≡ b [MOD n] :=
mul_comm m n ▸ modeq_of_modeq_mul_left _
def chinese_remainder (co : coprime n m) (a b : ℕ) : {k // k ≡ a [MOD n] ∧ k ≡ b [MOD m]} :=
⟨let (c, d) := xgcd n m in int.to_nat ((b * c * n + a * d * m) % (n * m)), begin
rw xgcd_val, dsimp [chinese_remainder._match_1],
rw [modeq_iff_dvd, modeq_iff_dvd],
rw [int.to_nat_of_nonneg], swap,
{ by_cases h₁ : n = 0, {simp [coprime, h₁] at co, substs m n, simp},
by_cases h₂ : m = 0, {simp [coprime, h₂] at co, substs m n, simp},
exact int.mod_nonneg _
(mul_ne_zero (int.coe_nat_ne_zero.2 h₁) (int.coe_nat_ne_zero.2 h₂)) },
have := gcd_eq_gcd_ab n m, simp [co.gcd_eq_one, mul_comm] at this,
rw [int.mod_def, ← sub_add, ← sub_add]; split,
{ refine dvd_add _ (dvd_trans (dvd_mul_right _ _) (dvd_mul_right _ _)),
rw [add_comm, ← sub_sub], refine _root_.dvd_sub _ (dvd_mul_left _ _),
have := congr_arg ((*) ↑a) this,
exact ⟨_, by rwa [mul_add, ← mul_assoc, ← mul_assoc, mul_one, mul_comm,
← sub_eq_iff_eq_add] at this⟩ },
{ refine dvd_add _ (dvd_trans (dvd_mul_left _ _) (dvd_mul_right _ _)),
rw [← sub_sub], refine _root_.dvd_sub _ (dvd_mul_left _ _),
have := congr_arg ((*) ↑b) this,
exact ⟨_, by rwa [mul_add, ← mul_assoc, ← mul_assoc, mul_one, mul_comm _ ↑m,
← sub_eq_iff_eq_add'] at this⟩ }
end⟩
lemma modeq_and_modeq_iff_modeq_mul {a b m n : ℕ} (hmn : coprime m n) :
a ≡ b [MOD m] ∧ a ≡ b [MOD n] ↔ (a ≡ b [MOD m * n]) :=
⟨λ h, begin
rw [nat.modeq.modeq_iff_dvd, nat.modeq.modeq_iff_dvd, ← int.dvd_nat_abs,
int.coe_nat_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd] at h,
rw [nat.modeq.modeq_iff_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd],
exact hmn.mul_dvd_of_dvd_of_dvd h.1 h.2
end,
λ h, ⟨nat.modeq.modeq_of_modeq_mul_right _ h, nat.modeq.modeq_of_modeq_mul_left _ h⟩⟩
lemma coprime_of_mul_modeq_one (b : ℕ) {a n : ℕ} (h : a * b ≡ 1 [MOD n]) : coprime a n :=
nat.coprime_of_dvd' (λ k ⟨ka, hka⟩ ⟨kb, hkb⟩, int.coe_nat_dvd.1 begin
rw [hka, hkb, modeq_iff_dvd] at h,
cases h with z hz,
rw [sub_eq_iff_eq_add] at hz,
rw [hz, int.coe_nat_mul, mul_assoc, mul_assoc, int.coe_nat_mul, ← mul_add],
exact dvd_mul_right _ _,
end)
end modeq
@[simp] lemma mod_mul_right_mod (a b c : ℕ) : a % (b * c) % b = a % b :=
modeq.modeq_of_modeq_mul_right _ (modeq.mod_modeq _ _)
@[simp] lemma mod_mul_left_mod (a b c : ℕ) : a % (b * c) % c = a % c :=
modeq.modeq_of_modeq_mul_left _ (modeq.mod_modeq _ _)
lemma div_mod_eq_mod_mul_div (a b c : ℕ) : a / b % c = a % (b * c) / b :=
if hb0 : b = 0 then by simp [hb0]
else by rw [← @add_right_cancel_iff _ _ (c * (a / b / c)), mod_add_div, nat.div_div_eq_div_mul,
← nat.mul_left_inj (nat.pos_of_ne_zero hb0),← @add_left_cancel_iff _ _ (a % b), mod_add_div,
mul_add, ← @add_left_cancel_iff _ _ (a % (b * c) % b), add_left_comm,
← add_assoc (a % (b * c) % b), mod_add_div, ← mul_assoc, mod_add_div, mod_mul_right_mod]
lemma add_mod_add_ite (a b c : ℕ) :
(a + b) % c + (if c ≤ a % c + b % c then c else 0) = a % c + b % c :=
have (a + b) % c = (a % c + b % c) % c,
from nat.modeq.modeq_add (nat.modeq.mod_modeq _ _).symm (nat.modeq.mod_modeq _ _).symm,
if hc0 : c = 0 then by simp [hc0]
else
begin
rw this,
split_ifs,
{ have h2 : (a % c + b % c) / c < 2,
from nat.div_lt_of_lt_mul (by rw mul_two;
exact add_lt_add (nat.mod_lt _ (nat.pos_of_ne_zero hc0))
(nat.mod_lt _ (nat.pos_of_ne_zero hc0))),
have h0 : 0 < (a % c + b % c) / c, from nat.div_pos h (nat.pos_of_ne_zero hc0),
rw [← @add_right_cancel_iff _ _ (c * ((a % c + b % c) / c)), add_comm _ c, add_assoc,
mod_add_div, le_antisymm (le_of_lt_succ h2) h0, mul_one, add_comm] },
{ rw [nat.mod_eq_of_lt (lt_of_not_ge h), add_zero] }
end
lemma add_mod_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) :
(a + b) % c = a % c + b % c :=
by rw [← add_mod_add_ite, if_neg (not_le_of_lt hc), add_zero]
lemma add_mod_add_of_le_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) :
(a + b) % c + c = a % c + b % c :=
by rw [← add_mod_add_ite, if_pos hc]
lemma add_div {a b c : ℕ} (hc0 : 0 < c) : (a + b) / c = a / c + b / c +
if c ≤ a % c + b % c then 1 else 0 :=
begin
rw [← nat.mul_left_inj hc0, ← @add_left_cancel_iff _ _ ((a + b) % c + a % c + b % c)],
suffices : (a + b) % c + c * ((a + b) / c) + a % c + b % c =
a % c + c * (a / c) + (b % c + c * (b / c)) + c * (if c ≤ a % c + b % c then 1 else 0) + (a + b) % c,
{ simpa only [mul_add, add_comm, add_left_comm, add_assoc] },
rw [mod_add_div, mod_add_div, mod_add_div, mul_ite, add_assoc, add_assoc],
conv_lhs { rw ← add_mod_add_ite },
simp, ac_refl
end
lemma add_div_eq_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) :
(a + b) / c = a / c + b / c :=
if hc0 : c = 0 then by simp [hc0]
else by rw [add_div (nat.pos_of_ne_zero hc0), if_neg (not_le_of_lt hc), add_zero]
lemma add_div_eq_of_le_mod_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) (hc0 : 0 < c) :
(a + b) / c = a / c + b / c + 1 :=
by rw [add_div hc0, if_pos hc]
lemma add_div_le_add_div (a b c : ℕ) : a / c + b / c ≤ (a + b) / c :=
if hc0 : c = 0 then by simp [hc0]
else by rw [nat.add_div (nat.pos_of_ne_zero hc0)]; exact le_add_right _ _
lemma le_mod_add_mod_of_dvd_add_of_not_dvd {a b c : ℕ} (h : c ∣ a + b) (ha : ¬ c ∣ a) :
c ≤ a % c + b % c :=
by_contradiction $ λ hc,
have (a + b) % c = a % c + b % c, from add_mod_of_add_mod_lt (lt_of_not_ge hc),
by simp [dvd_iff_mod_eq_zero, *] at *
lemma odd_mul_odd {n m : ℕ} (hn1 : n % 2 = 1) (hm1 : m % 2 = 1) : (n * m) % 2 = 1 :=
show (n * m) % 2 = (1 * 1) % 2, from nat.modeq.modeq_mul hn1 hm1
lemma odd_mul_odd_div_two {m n : ℕ} (hm1 : m % 2 = 1) (hn1 : n % 2 = 1) :
(m * n) / 2 = m * (n / 2) + m / 2 :=
have hm0 : 0 < m := nat.pos_of_ne_zero (λ h, by simp * at *),
have hn0 : 0 < n := nat.pos_of_ne_zero (λ h, by simp * at *),
(nat.mul_left_inj (show 0 < 2, from dec_trivial)).1 $
by rw [mul_add, two_mul_odd_div_two hm1, mul_left_comm, two_mul_odd_div_two hn1,
two_mul_odd_div_two (nat.odd_mul_odd hm1 hn1), nat.mul_sub_left_distrib, mul_one,
← nat.add_sub_assoc hm0, nat.sub_add_cancel (le_mul_of_one_le_right' (nat.zero_le _) hn0)]
lemma odd_of_mod_four_eq_one {n : ℕ} (h : n % 4 = 1) : n % 2 = 1 :=
@modeq.modeq_of_modeq_mul_left 2 n 1 2 h
lemma odd_of_mod_four_eq_three {n : ℕ} (h : n % 4 = 3) : n % 2 = 1 :=
@modeq.modeq_of_modeq_mul_left 2 n 3 2 h
end nat
namespace list
variable {α : Type*}
lemma nth_rotate : ∀ {l : list α} {n m : ℕ} (hml : m < l.length),
(l.rotate n).nth m = l.nth ((m + n) % l.length)
| [] n m hml := (nat.not_lt_zero _ hml).elim
| l 0 m hml := by simp [nat.mod_eq_of_lt hml]
| (a::l) (n+1) m hml :=
have h₃ : m < list.length (l ++ [a]), by simpa using hml,
(lt_or_eq_of_le (nat.le_of_lt_succ $ nat.mod_lt (m + n)
(lt_of_le_of_lt (nat.zero_le _) hml))).elim
(λ hml',
have h₁ : (m + (n + 1)) % ((a :: l : list α).length) =
(m + n) % ((a :: l : list α).length) + 1,
from calc (m + (n + 1)) % (l.length + 1) =
((m + n) % (l.length + 1) + 1) % (l.length + 1) :
add_assoc m n 1 ▸ nat.modeq.modeq_add (nat.mod_mod _ _).symm rfl
... = (m + n) % (l.length + 1) + 1 : nat.mod_eq_of_lt (nat.succ_lt_succ hml'),
have h₂ : (m + n) % (l ++ [a]).length < l.length, by simpa [nat.add_one] using hml',
by rw [list.rotate_cons_succ, nth_rotate h₃, list.nth_append h₂, h₁, list.nth]; simp)
(λ hml',
have h₁ : (m + (n + 1)) % (l.length + 1) = 0,
from calc (m + (n + 1)) % (l.length + 1) = (l.length + 1) % (l.length + 1) :
add_assoc m n 1 ▸ nat.modeq.modeq_add
(hml'.trans (nat.mod_eq_of_lt (nat.lt_succ_self _)).symm) rfl
... = 0 : by simp,
have h₂ : l.length < (l ++ [a]).length, by simp [nat.lt_succ_self],
by rw [list.length, list.rotate_cons_succ, nth_rotate h₃, list.length_append,
list.length_cons, list.length, zero_add, hml', h₁, list.nth_concat_length]; refl)
lemma rotate_eq_self_iff_eq_repeat [hα : nonempty α] : ∀ {l : list α},
(∀ n, l.rotate n = l) ↔ ∃ a, l = list.repeat a l.length
| [] := ⟨λ h, nonempty.elim hα (λ a, ⟨a, by simp⟩), by simp⟩
| (a::l) :=
⟨λ h, ⟨a, list.ext_le (by simp) $ λ n hn h₁,
begin
rw [← option.some_inj, ← list.nth_le_nth],
conv {to_lhs, rw ← h ((list.length (a :: l)) - n)},
rw [nth_rotate hn, nat.add_sub_cancel' (le_of_lt hn),
nat.mod_self, nth_le_repeat _ hn], refl
end⟩,
λ ⟨a, ha⟩ n, ha.symm ▸ list.ext_le (by simp)
(λ m hm h,
have hm' : (m + n) % (list.repeat a (list.length (a :: l))).length < list.length (a :: l),
by rw list.length_repeat; exact nat.mod_lt _ (nat.succ_pos _),
by rw [nth_le_repeat, ← option.some_inj, ← list.nth_le_nth, nth_rotate h, list.nth_le_nth,
nth_le_repeat]; simp * at *)⟩
end list
|
f6d795e7883392f032fb40f0e3da0c922e2bb613 | 32c054a763e4aa96bcb6e8bc87775e0f403a1804 | /src/smtexpr.lean | 2fc267089b9c15036f081ee0b1e10dde0b2a1bb8 | [
"LicenseRef-scancode-generic-cla",
"MIT"
] | permissive | Claudiusgonzo/AliveInLean | 7fac3f82722c27acc5551260ea12a36519f6e24e | a21bfb90dee0c6c6e00a955b6de92c631198c5ba | refs/heads/master | 1,635,381,727,801 | 1,555,783,536,000 | 1,555,783,536,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,284 | lean | -- Copyright (c) Microsoft Corporation. All rights reserved.
-- Licensed under the MIT license.
import .common
import .ops
import .bitvector
import smt2.syntax
import smt2.builder
universes u
mutual inductive sbool, sbitvec
with sbool : Type
| tt: sbool | ff:sbool
| var: string → sbool -- unbound variable
| and: sbool → sbool → sbool
| or: sbool → sbool → sbool
| xor: sbool → sbool → sbool
| eqb: sbool → sbool → sbool
| neb: sbool → sbool → sbool
| ite: sbool → sbool → sbool → sbool
| not: sbool → sbool
| eqbv: Π {sz:size}, sbitvec sz → sbitvec sz → sbool
| nebv: Π {sz:size}, sbitvec sz → sbitvec sz → sbool
| sle: Π {sz:size}, sbitvec sz → sbitvec sz → sbool
| slt: Π {sz:size}, sbitvec sz → sbitvec sz → sbool
| ule: Π {sz:size}, sbitvec sz → sbitvec sz → sbool
| ult: Π {sz:size}, sbitvec sz → sbitvec sz → sbool
with sbitvec : size → Type
| const: Π (sz:size) (n:nat), sbitvec sz
| var: Π (sz:size), string → sbitvec sz -- unbound variable
| add: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| sub: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| mul: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| udiv: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| urem: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| sdiv: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| srem: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| and: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| or: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| xor: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| shl: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| lshr: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| ashr: Π {sz:size}, sbitvec sz → sbitvec sz → sbitvec sz
| zext: Π {sz:size} (sz':size), sbitvec sz → sbitvec sz'
| sext: Π {sz:size} (sz':size), sbitvec sz → sbitvec sz'
| trunc: Π {sz:size} (sz':size), sbitvec sz → sbitvec sz'
| extract: Π {sz sz':size} (highbit lowbit:nat)
(H:sz'.val = highbit - lowbit + 1), sbitvec sz → sbitvec sz'
| ite: Π {sz:size}, sbool → sbitvec sz → sbitvec sz → sbitvec sz
namespace sbool
open smt2.term
open smt2.builder
def of_bool (b:bool):sbool :=
cond b sbool.tt sbool.ff
def and_simpl (t1 t2:sbool):sbool :=
match t1 with
| sbool.tt := t2
| sbool.ff := sbool.ff
| _ :=
match t2 with
| sbool.tt := t1
| sbool.ff := sbool.ff
| _ := sbool.and t1 t2
end
end
end sbool
instance sbool_is_bool_like : bool_like sbool :=
⟨sbool.tt, sbool.ff, sbool.and, sbool.or, sbool.xor, sbool.not⟩
instance sbool_has_ite : has_ite sbool sbool :=
⟨sbool.ite⟩
namespace sbitvec
open smt2.term
open smt2.builder
def of_int (sz:size) : ℤ → sbitvec sz
| x@(int.of_nat q) := sbitvec.const sz (q % (2^sz.val))
| x@(int.neg_succ_of_nat p) :=
sbitvec.const sz (((2 ^ sz.val) - p - 1) % (2^sz.val))
@[simp]
def one (sz:size) : sbitvec sz := of_int sz 1
@[simp]
def zero (sz:size) : sbitvec sz := of_int sz 0
@[simp]
def intmin (sz:size) : sbitvec sz := of_int sz (int_min_nat sz)
@[simp]
def intmax (sz:size) : sbitvec sz := of_int sz (int.of_nat ((2^(sz.val-1))-1))
@[simp]
def uintmax (sz:size) : sbitvec sz := of_int sz (all_one_nat sz)
def of_bool (b:sbool) : sbitvec (size.one) :=
sbitvec.ite b (sbitvec.one size.one) (sbitvec.zero size.one)
def of_bv {sz:size} (b:bitvector sz) : sbitvec sz := of_int sz b.1
private theorem add_size_eq (n m:size):
subtype.mk ((n.1 + m.1 - 1) - n.1 + 1) dec_trivial = m :=
begin apply subtype.eq, simp,
rewrite nat.add_sub_assoc,
{ -- 1 + (n.val + (m.val - 1) - n.val) = m.val
rewrite nat.add_sub_cancel_left, -- 1 + (m.val - 1) = m.val
rewrite ← nat.add_sub_assoc, -- 1 + m.val - 1 = m.val
rewrite nat.add_sub_cancel_left, -- 1 ≤ m.val
apply m.2
},
{ -- 1 ≤ m.val
apply m.2 }
end
theorem decr_sbitvec: ∀ (sz:size) (v:sbitvec sz), 0 < sbitvec.sizeof sz v
:= begin
intros, induction v,
any_goals { unfold sbitvec.sizeof, try {simp}, rw nat.one_add, exact dec_trivial }
end
variable {sz:size}
@[simp]
def mk_zext (m:size) (a:sbitvec sz): sbitvec (sz.add m) :=
sbitvec.zext (sz.add m) a
@[simp]
def mk_sext (m:size) (a:sbitvec sz): sbitvec (sz.add m) :=
sbitvec.sext (sz.add m) a
-- Returns false if it overflows.
def overflow_chk (f:Π {sz':size}, sbitvec sz' → sbitvec sz' → sbitvec sz')
(m:size) (signed:bool)
(a b:sbitvec sz):sbool :=
if signed then
let a' := sbitvec.mk_sext m a,
b' := sbitvec.mk_sext m b,
r' := f a' b',
r := f a b,
r'' := sbitvec.mk_sext m r in
sbool.nebv r' r''
else
let a' := sbitvec.mk_zext m a,
b' := sbitvec.mk_zext m b,
r' := f a' b' in
let H:(m.val = (sz.val + m.val - 1) - sz.val + 1) := begin
rw nat.add_comm sz.val m.val,
rw nat.sub.right_comm,
rw nat.add_sub_cancel,
rw nat.sub_add_cancel,
cases m, assumption
end in
let padding := sbitvec.extract (sz+m-1) sz H r' in
sbool.nebv padding (sbitvec.zero m)
def shl_overflow_chk (m:size) (signed:bool) (a b:sbitvec sz):sbool :=
overflow_chk (@sbitvec.shl) sz signed a (sbitvec.urem b (sbitvec.of_int sz sz.val))
end sbitvec
instance sbitvec_is_uint_like
: uint_like (sbitvec) :=
⟨@sbitvec.add, @sbitvec.sub, @sbitvec.mul,
@sbitvec.udiv, @sbitvec.urem, @sbitvec.sdiv, @sbitvec.srem,
@sbitvec.and, @sbitvec.or, @sbitvec.xor,
@sbitvec.shl, @sbitvec.lshr, @sbitvec.ashr,
sbitvec.zero, sbitvec.uintmax, sbitvec.intmin,
sbitvec.of_int, @sbitvec.zext, @sbitvec.sext, @sbitvec.trunc⟩
instance sbool_sbitvec_has_coe: has_coe sbool (sbitvec size.one) :=
⟨sbitvec.of_bool⟩
@[reducible]
instance sbitvec_has_comp
: has_comp sbitvec sbool :=
⟨@sbool.eqbv, @sbool.nebv, @sbool.sle, @sbool.slt, @sbool.ule, @sbool.ult⟩
instance sbitvec_has_ite {sz:size} : has_ite sbool (sbitvec sz) :=
⟨sbitvec.ite⟩
instance sbitvec_has_overflow_check
: has_overflow_check sbitvec sbool :=
⟨(λ {sz:size}, @sbitvec.overflow_chk sz @sbitvec.add size.one),
(λ {sz:size}, @sbitvec.overflow_chk sz @sbitvec.sub size.one),
(λ {sz:size}, @sbitvec.overflow_chk sz @sbitvec.mul sz),
(λ {sz:size}, @sbitvec.shl_overflow_chk sz sz)⟩
|
597e0f6d21af416eae43e6baaf8dcaa8a0076da7 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/order/extension/well.lean | 2aeeb6c1b5e949d60ea9b986182482147fbf89de | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,380 | lean | /-
Copyright (c) 2022 Yaël Dillies, Junyan Xu. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yaël Dillies, Junyan Xu
-/
import data.prod.lex
import set_theory.ordinal.arithmetic
/-!
# Extend a well-founded order to a well-order
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file constructs a well-order (linear well-founded order) which is an extension of a given
well-founded order.
## Proof idea
We can map our order into two well-orders:
* the first map respects the order but isn't necessarily injective. Namely, this is the *rank*
function `rank : α → ordinal`.
* the second map is injective but doesn't necessarily respect the order. This is an arbitrary
well-order on `α`.
Then their lexicographic product is a well-founded linear order which our original order injects in.
-/
universe u
variables {α : Type u} {r : α → α → Prop}
namespace well_founded
variable (hwf : well_founded r)
include hwf
/-- An arbitrary well order on `α` that extends `r`.
The construction maps `r` into two well-orders: the first map is `well_founded.rank`, which is not
necessarily injective but respects the order `r`; the other map is the identity (with an arbitrarily
chosen well-order on `α`), which is injective but doesn't respect `r`.
By taking the lexicographic product of the two, we get both properties, so we can pull it back and
get an well-order that extend our original order `r`. Another way to view this is that we choose an
arbitrary well-order to serve as a tiebreak between two elements of same rank.
-/
noncomputable def well_order_extension : linear_order α :=
let l : linear_order α := is_well_order.linear_order well_ordering_rel in by exactI
@linear_order.lift' α (ordinal ×ₗ α) _
(λ a : α, (well_founded.rank.{u} hwf a, a)) (λ _ _, congr_arg prod.snd)
instance well_order_extension.is_well_founded_lt : is_well_founded α hwf.well_order_extension.lt :=
⟨inv_image.wf _ $ prod.lex_wf ordinal.well_founded_lt.wf well_ordering_rel.is_well_order.wf⟩
/-- Any well-founded relation can be extended to a well-ordering on that type. -/
lemma exists_well_order_ge : ∃ s, r ≤ s ∧ is_well_order α s :=
⟨hwf.well_order_extension.lt, λ a b h, prod.lex.left _ _ (hwf.rank_lt_of_rel h), by split⟩
end well_founded
/-- A type alias for `α`, intended to extend a well-founded order on `α` to a well-order. -/
def well_order_extension (α) : Type* := α
instance [inhabited α] : inhabited (well_order_extension α) := ‹inhabited (well_order_extension α)›
/-- "Identity" equivalence between a well-founded order and its well-order extension. -/
def to_well_order_extension : α ≃ well_order_extension α := equiv.refl _
noncomputable instance [has_lt α] [well_founded_lt α] : linear_order (well_order_extension α) :=
(is_well_founded.wf : @well_founded α (<)).well_order_extension
instance well_order_extension.well_founded_lt [has_lt α] [well_founded_lt α] :
well_founded_lt (well_order_extension α) :=
well_founded.well_order_extension.is_well_founded_lt _
lemma to_well_order_extension_strict_mono [preorder α] [well_founded_lt α] :
strict_mono (to_well_order_extension : α → well_order_extension α) :=
λ a b h, prod.lex.left _ _ $ well_founded.rank_lt_of_rel _ h
|
8ee2bb7181dccfb512d41a0d0e9fc0a58bc98b6c | b074a51e20fdb737b2d4c635dd292fc54685e010 | /src/ring_theory/algebra.lean | 358fbcde7c630bfbabdd219261410872ad16d599 | [
"Apache-2.0"
] | permissive | minchaowu/mathlib | 2daf6ffdb5a56eeca403e894af88bcaaf65aec5e | 879da1cf04c2baa9eaa7bd2472100bc0335e5c73 | refs/heads/master | 1,609,628,676,768 | 1,564,310,105,000 | 1,564,310,105,000 | 99,461,307 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 19,835 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
Algebra over Commutative Ring (under category)
-/
import data.polynomial data.mv_polynomial
import data.complex.basic
import linear_algebra.tensor_product
import ring_theory.subring
universes u v w u₁ v₁
open lattice
local infix ` ⊗ `:100 := tensor_product
/-- The category of R-algebras where R is a commutative
ring is the under category R ↓ CRing. In the categorical
setting we have a forgetful functor R-Alg ⥤ R-Mod.
However here it extends module in order to preserve
definitional equality in certain cases. -/
class algebra (R : Type u) (A : Type v) [comm_ring R] [ring A] extends module R A :=
(to_fun : R → A) [hom : is_ring_hom to_fun]
(commutes' : ∀ r x, x * to_fun r = to_fun r * x)
(smul_def' : ∀ r x, r • x = to_fun r * x)
attribute [instance] algebra.hom
def algebra_map {R : Type u} (A : Type v) [comm_ring R] [ring A] [algebra R A] (x : R) : A :=
algebra.to_fun A x
namespace algebra
variables {R : Type u} {S : Type v} {A : Type w}
variables [comm_ring R] [comm_ring S] [ring A] [algebra R A]
/-- The codomain of an algebra. -/
instance : module R A := infer_instance
instance : has_scalar R A := infer_instance
instance {F : Type u} {K : Type v} [discrete_field F] [ring K] [algebra F K] :
vector_space F K :=
@vector_space.mk F _ _ _ algebra.module
include R
instance : is_ring_hom (algebra_map A : R → A) := algebra.hom _ A
variables (A)
@[simp] lemma map_add (r s : R) : algebra_map A (r + s) = algebra_map A r + algebra_map A s :=
is_ring_hom.map_add _
@[simp] lemma map_neg (r : R) : algebra_map A (-r) = -algebra_map A r :=
is_ring_hom.map_neg _
@[simp] lemma map_sub (r s : R) : algebra_map A (r - s) = algebra_map A r - algebra_map A s :=
is_ring_hom.map_sub _
@[simp] lemma map_mul (r s : R) : algebra_map A (r * s) = algebra_map A r * algebra_map A s :=
is_ring_hom.map_mul _
variables (R)
@[simp] lemma map_zero : algebra_map A (0 : R) = 0 :=
is_ring_hom.map_zero _
@[simp] lemma map_one : algebra_map A (1 : R) = 1 :=
is_ring_hom.map_one _
variables {R A}
/-- Creating an algebra from a morphism in CRing. -/
def of_ring_hom (i : R → S) (hom : is_ring_hom i) : algebra R S :=
{ smul := λ c x, i c * x,
smul_zero := λ x, mul_zero (i x),
smul_add := λ r x y, mul_add (i r) x y,
add_smul := λ r s x, show i (r + s) * x = _, by rw [hom.3, add_mul],
mul_smul := λ r s x, show i (r * s) * x = _, by rw [hom.2, mul_assoc],
one_smul := λ x, show i 1 * x = _, by rw [hom.1, one_mul],
zero_smul := λ x, show i 0 * x = _, by rw [@@is_ring_hom.map_zero _ _ i hom, zero_mul],
to_fun := i,
commutes' := λ _ _, mul_comm _ _,
smul_def' := λ c x, rfl }
theorem smul_def (r : R) (x : A) : r • x = algebra_map A r * x :=
algebra.smul_def' r x
theorem commutes (r : R) (x : A) : x * algebra_map A r = algebra_map A r * x :=
algebra.commutes' r x
theorem left_comm (r : R) (x y : A) : x * (algebra_map A r * y) = algebra_map A r * (x * y) :=
by rw [← mul_assoc, commutes, mul_assoc]
@[simp] lemma mul_smul_comm (s : R) (x y : A) :
x * (s • y) = s • (x * y) :=
by rw [smul_def, smul_def, left_comm]
@[simp] lemma smul_mul_assoc (r : R) (x y : A) :
(r • x) * y = r • (x * y) :=
by rw [smul_def, smul_def, mul_assoc]
/-- R[X] is the generator of the category R-Alg. -/
instance polynomial (R : Type u) [comm_ring R] [decidable_eq R] : algebra R (polynomial R) :=
{ to_fun := polynomial.C,
commutes' := λ _ _, mul_comm _ _,
smul_def' := λ c p, (polynomial.C_mul' c p).symm,
.. polynomial.module }
/-- The algebra of multivariate polynomials. -/
instance mv_polynomial (R : Type u) [comm_ring R] [decidable_eq R]
(ι : Type v) [decidable_eq ι] : algebra R (mv_polynomial ι R) :=
{ to_fun := mv_polynomial.C,
commutes' := λ _ _, mul_comm _ _,
smul_def' := λ c p, (mv_polynomial.C_mul' c p).symm,
.. mv_polynomial.module }
/-- Creating an algebra from a subring. This is the dual of ring extension. -/
instance of_subring (S : set R) [is_subring S] : algebra S R :=
of_ring_hom subtype.val ⟨rfl, λ _ _, rfl, λ _ _, rfl⟩
variables (R A)
/-- The multiplication in an algebra is a bilinear map. -/
def lmul : A →ₗ A →ₗ A :=
linear_map.mk₂ R (*)
(λ x y z, add_mul x y z)
(λ c x y, by rw [smul_def, smul_def, mul_assoc _ x y])
(λ x y z, mul_add x y z)
(λ c x y, by rw [smul_def, smul_def, left_comm])
set_option class.instance_max_depth 39
def lmul_left (r : A) : A →ₗ A :=
lmul R A r
def lmul_right (r : A) : A →ₗ A :=
(lmul R A).flip r
variables {R A}
@[simp] lemma lmul_apply (p q : A) : lmul R A p q = p * q := rfl
@[simp] lemma lmul_left_apply (p q : A) : lmul_left R A p q = p * q := rfl
@[simp] lemma lmul_right_apply (p q : A) : lmul_right R A p q = q * p := rfl
end algebra
/-- Defining the homomorphism in the category R-Alg. -/
structure alg_hom {R : Type u} (A : Type v) (B : Type w)
[comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] :=
(to_fun : A → B) [hom : is_ring_hom to_fun]
(commutes' : ∀ r : R, to_fun (algebra_map A r) = algebra_map B r)
infixr ` →ₐ `:25 := alg_hom
notation A ` →ₐ[`:25 R `] ` B := @alg_hom R A B _ _ _ _ _
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁}
variables [comm_ring R] [ring A] [ring B] [ring C] [ring D]
variables [algebra R A] [algebra R B] [algebra R C] [algebra R D]
include R
instance : has_coe_to_fun (A →ₐ[R] B) := ⟨λ _, A → B, to_fun⟩
variables (φ : A →ₐ[R] B)
instance : is_ring_hom ⇑φ := hom φ
@[extensionality]
theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ :=
by cases φ₁; cases φ₂; congr' 1; ext; apply H
theorem commutes (r : R) : φ (algebra_map A r) = algebra_map B r := φ.commutes' r
@[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s :=
is_ring_hom.map_add _
@[simp] lemma map_zero : φ 0 = 0 :=
is_ring_hom.map_zero _
@[simp] lemma map_neg (x) : φ (-x) = -φ x :=
is_ring_hom.map_neg _
@[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y :=
is_ring_hom.map_sub _
@[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y :=
is_ring_hom.map_mul _
@[simp] lemma map_one : φ 1 = 1 :=
is_ring_hom.map_one _
/-- R-Alg ⥤ R-Mod -/
def to_linear_map : A →ₗ B :=
{ to_fun := φ,
add := φ.map_add,
smul := λ c x, by rw [algebra.smul_def, φ.map_mul, φ.commutes c, algebra.smul_def] }
@[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl
theorem to_linear_map_inj {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁.to_linear_map = φ₂.to_linear_map) : φ₁ = φ₂ :=
ext $ λ x, show φ₁.to_linear_map x = φ₂.to_linear_map x, by rw H
variables (R A)
protected def id : A →ₐ[R] A :=
{ to_fun := id, commutes' := λ _, rfl }
variables {R A}
@[simp] lemma id_to_linear_map :
(alg_hom.id R A).to_linear_map = @linear_map.id R A _ _ _ := rfl
@[simp] lemma id_apply (p : A) : alg_hom.id R A p = p := rfl
def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ C :=
{ to_fun := φ₁ ∘ φ₂,
commutes' := λ r, by rw [function.comp_apply, φ₂.commutes, φ₁.commutes] }
@[simp] lemma comp_to_linear_map (f : A →ₐ[R] B) (g : B →ₐ[R] C) :
(g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl
@[simp] lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) :
φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl
@[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ :=
ext $ λ x, rfl
@[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ :=
ext $ λ x, rfl
theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) :
(φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) :=
ext $ λ x, rfl
end alg_hom
namespace algebra
variables (R : Type u) (S : Type v) (A : Type w)
variables [comm_ring R] [comm_ring S] [ring A] [algebra R S] [algebra S A]
include R S A
def comap : Type w := A
def comap.to_comap : A → comap R S A := id
def comap.of_comap : comap R S A → A := id
omit R S A
instance comap.ring : ring (comap R S A) := _inst_3
instance comap.comm_ring (R : Type u) (S : Type v) (A : Type w)
[comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] :
comm_ring (comap R S A) := _inst_8
instance comap.module : module S (comap R S A) := _inst_5.to_module
instance comap.has_scalar : has_scalar S (comap R S A) := _inst_5.to_module.to_has_scalar
set_option class.instance_max_depth 40
/-- R ⟶ S induces S-Alg ⥤ R-Alg -/
instance comap.algebra : algebra R (comap R S A) :=
{ smul := λ r x, (algebra_map S r • x : A),
smul_add := λ _ _ _, smul_add _ _ _,
add_smul := λ _ _ _, by simp only [algebra.map_add]; from add_smul _ _ _,
mul_smul := λ _ _ _, by simp only [algebra.map_mul]; from mul_smul _ _ _,
one_smul := λ _, by simp only [algebra.map_one]; from one_smul _ _,
zero_smul := λ _, by simp only [algebra.map_zero]; from zero_smul _ _,
smul_zero := λ _, smul_zero _,
to_fun := (algebra_map A : S → A) ∘ algebra_map S,
hom := by letI : is_ring_hom (algebra_map A) := _inst_5.hom; apply_instance,
commutes' := λ r x, algebra.commutes _ _,
smul_def' := λ _ _, algebra.smul_def _ _ }
def to_comap : S →ₐ[R] comap R S A :=
{ to_fun := (algebra_map A : S → A),
hom := _inst_5.hom,
commutes' := λ r, rfl }
theorem to_comap_apply (x) : to_comap R S A x = (algebra_map A : S → A) x := rfl
end algebra
namespace alg_hom
variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁}
variables [comm_ring R] [comm_ring S] [ring A] [ring B]
variables [algebra R S] [algebra S A] [algebra S B] (φ : A →ₐ[S] B)
include R
/-- R ⟶ S induces S-Alg ⥤ R-Alg -/
def comap : algebra.comap R S A →ₐ[R] algebra.comap R S B :=
{ to_fun := φ,
hom := alg_hom.is_ring_hom _,
commutes' := λ r, φ.commutes (algebra_map S r) }
end alg_hom
namespace polynomial
variables (R : Type u) (A : Type v)
variables [comm_ring R] [comm_ring A] [algebra R A]
variables [decidable_eq R] (x : A)
/-- A → Hom[R-Alg](R[X],A) -/
def aeval : polynomial R →ₐ[R] A :=
{ to_fun := eval₂ (algebra_map A) x,
hom := ⟨eval₂_one _ x, λ _ _, eval₂_mul _ x, λ _ _, eval₂_add _ x⟩,
commutes' := λ r, eval₂_C _ _ }
theorem aeval_def (p : polynomial R) : aeval R A x p = eval₂ (algebra_map A) x p := rfl
instance aeval.is_ring_hom : is_ring_hom (aeval R A x) :=
alg_hom.hom _
theorem eval_unique (φ : polynomial R →ₐ[R] A) (p) :
φ p = eval₂ (algebra_map A) (φ X) p :=
begin
apply polynomial.induction_on p,
{ intro r, rw eval₂_C, exact φ.commutes r },
{ intros f g ih1 ih2,
rw [is_ring_hom.map_add φ, ih1, ih2, eval₂_add] },
{ intros n r ih,
rw [pow_succ', ← mul_assoc, is_ring_hom.map_mul φ, eval₂_mul (algebra_map A : R → A), eval₂_X, ih] }
end
end polynomial
namespace mv_polynomial
variables (R : Type u) (A : Type v)
variables [comm_ring R] [comm_ring A] [algebra R A]
variables [decidable_eq R] [decidable_eq A] (σ : set A)
/-- (ι → A) → Hom[R-Alg](R[ι],A) -/
def aeval : mv_polynomial σ R →ₐ[R] A :=
{ to_fun := eval₂ (algebra_map A) subtype.val,
hom := ⟨eval₂_one _ _, λ _ _, eval₂_mul _ _, λ _ _, eval₂_add _ _⟩,
commutes' := λ r, eval₂_C _ _ _ }
theorem aeval_def (p : mv_polynomial σ R) : aeval R A σ p = eval₂ (algebra_map A) subtype.val p := rfl
instance aeval.is_ring_hom : is_ring_hom (aeval R A σ) :=
alg_hom.hom _
variables (ι : Type w) [decidable_eq ι]
theorem eval_unique (φ : mv_polynomial ι R →ₐ[R] A) (p) :
φ p = eval₂ (algebra_map A) (φ ∘ X) p :=
begin
apply mv_polynomial.induction_on p,
{ intro r, rw eval₂_C, exact φ.commutes r },
{ intros f g ih1 ih2,
rw [is_ring_hom.map_add φ, ih1, ih2, eval₂_add] },
{ intros p j ih,
rw [is_ring_hom.map_mul φ, eval₂_mul, eval₂_X, ih] }
end
end mv_polynomial
namespace complex
instance algebra_over_reals : algebra ℝ ℂ :=
algebra.of_ring_hom coe $ by constructor; intros; simp [one_re]
instance : has_scalar ℝ ℂ := { smul := λ r c, ↑r * c}
end complex
structure subalgebra (R : Type u) (A : Type v)
[comm_ring R] [ring A] [algebra R A] : Type v :=
(carrier : set A) [subring : is_subring carrier]
(range_le : set.range (algebra_map A : R → A) ≤ carrier)
attribute [instance] subalgebra.subring
namespace subalgebra
variables {R : Type u} {A : Type v}
variables [comm_ring R] [ring A] [algebra R A]
include R
instance : has_coe (subalgebra R A) (set A) :=
⟨λ S, S.carrier⟩
instance : has_mem A (subalgebra R A) :=
⟨λ x S, x ∈ S.carrier⟩
variables {A}
theorem mem_coe {x : A} {s : subalgebra R A} : x ∈ (s : set A) ↔ x ∈ s :=
iff.rfl
@[extensionality] theorem ext {S T : subalgebra R A}
(h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T :=
by cases S; cases T; congr; ext x; exact h x
variables (S : subalgebra R A)
instance : is_subring (S : set A) := S.subring
instance : ring S := @@subtype.ring _ S.is_subring
instance (R : Type u) (A : Type v) [comm_ring R] [comm_ring A]
[algebra R A] (S : subalgebra R A) : comm_ring S := @@subtype.comm_ring _ S.is_subring
instance algebra : algebra R S :=
{ smul := λ (c:R) x, ⟨c • x.1,
by rw algebra.smul_def; exact @@is_submonoid.mul_mem _ S.2.2 (S.3 ⟨c, rfl⟩) x.2⟩,
smul_add := λ c x y, subtype.eq $ smul_add _ _ _,
add_smul := λ c x y, subtype.eq $ add_smul _ _ _,
mul_smul := λ c x y, subtype.eq $ mul_smul _ _ _,
one_smul := λ x, subtype.eq $ one_smul _ _,
zero_smul := λ x, subtype.eq $ zero_smul _ _,
smul_zero := λ x, subtype.eq $ smul_zero _,
to_fun := λ r, ⟨algebra_map A r, S.range_le ⟨r, rfl⟩⟩,
hom := ⟨subtype.eq $ algebra.map_one R A, λ x y, subtype.eq $ algebra.map_mul A x y,
λ x y, subtype.eq $ algebra.map_add A x y⟩,
commutes' := λ c x, subtype.eq $ by apply _inst_3.4,
smul_def' := λ c x, subtype.eq $ by apply _inst_3.5 }
instance to_algebra (R : Type u) (A : Type v) [comm_ring R] [comm_ring A]
[algebra R A] (S : subalgebra R A) : algebra S A :=
algebra.of_subring _
def val : S →ₐ[R] A :=
{ to_fun := subtype.val,
hom := ⟨rfl, λ _ _, rfl, λ _ _, rfl⟩,
commutes' := λ r, rfl }
def to_submodule : submodule R A :=
{ carrier := S.carrier,
zero := (0:S).2,
add := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2,
smul := λ c x hx, (algebra.smul_def c x).symm ▸ (⟨algebra_map A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 }
instance coe_to_submodule : has_coe (subalgebra R A) (submodule R A) :=
⟨to_submodule⟩
instance to_submodule.is_subring : is_subring ((S : submodule R A) : set A) := S.2
instance : partial_order (subalgebra R A) :=
{ le := λ S T, S.carrier ≤ T.carrier,
le_refl := λ _, le_refl _,
le_trans := λ _ _ _, le_trans,
le_antisymm := λ S T hst hts, ext $ λ x, ⟨@hst x, @hts x⟩ }
def comap {R : Type u} {S : Type v} {A : Type w}
[comm_ring R] [comm_ring S] [ring A] [algebra R S] [algebra S A]
(iSB : subalgebra S A) : subalgebra R (algebra.comap R S A) :=
{ carrier := (iSB : set A),
subring := iSB.is_subring,
range_le := λ a ⟨r, hr⟩, hr ▸ iSB.range_le ⟨_, rfl⟩ }
set_option class.instance_max_depth 48
def under {R : Type u} {A : Type v} [comm_ring R] [comm_ring A]
{i : algebra R A} (S : subalgebra R A)
(T : subalgebra S A) : subalgebra R A :=
{ carrier := T,
range_le := (λ a ⟨r, hr⟩, hr ▸ T.range_le ⟨⟨algebra_map A r, S.range_le ⟨r, rfl⟩⟩, rfl⟩) }
end subalgebra
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w}
variables [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B]
variables (φ : A →ₐ[R] B)
protected def range : subalgebra R B :=
{ carrier := set.range φ,
subring :=
{ one_mem := ⟨1, φ.map_one⟩,
mul_mem := λ y₁ y₂ ⟨x₁, hx₁⟩ ⟨x₂, hx₂⟩, ⟨x₁ * x₂, hx₁ ▸ hx₂ ▸ φ.map_mul x₁ x₂⟩ },
range_le := λ y ⟨r, hr⟩, ⟨algebra_map A r, hr ▸ φ.commutes r⟩ }
end alg_hom
namespace algebra
variables {R : Type u} (A : Type v)
variables [comm_ring R] [ring A] [algebra R A]
include R
variables (R)
instance id : algebra R R :=
algebra.of_ring_hom id $ by apply_instance
def of_id : R →ₐ A :=
{ to_fun := algebra_map A, commutes' := λ _, rfl }
variables {R}
theorem of_id_apply (r) : of_id R A r = algebra_map A r := rfl
variables (R) {A}
def adjoin (s : set A) : subalgebra R A :=
{ carrier := ring.closure (set.range (algebra_map A : R → A) ∪ s),
range_le := le_trans (set.subset_union_left _ _) ring.subset_closure }
variables {R}
protected def gc : galois_connection (adjoin R : set A → subalgebra R A) coe :=
λ s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) ring.subset_closure) H,
λ H, ring.closure_subset $ set.union_subset S.range_le H⟩
protected def gi : galois_insertion (adjoin R : set A → subalgebra R A) coe :=
{ choice := λ s hs, adjoin R s,
gc := algebra.gc,
le_l_u := λ S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_refl _,
choice_eq := λ _ _, rfl }
instance : complete_lattice (subalgebra R A) :=
galois_insertion.lift_complete_lattice algebra.gi
theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map A : R → A) :=
suffices (⊥ : subalgebra R A) = (of_id R A).range, by rw this; refl,
le_antisymm bot_le $ subalgebra.range_le _
theorem mem_top {x : A} : x ∈ (⊤ : subalgebra R A) :=
ring.mem_closure $ or.inr trivial
def to_top : A →ₐ[R] (⊤ : subalgebra R A) :=
{ to_fun := λ x, ⟨x, mem_top⟩,
hom := ⟨rfl, λ _ _, rfl, λ _ _, rfl⟩,
commutes' := λ _, rfl }
end algebra
section int
variables (R : Type*) [comm_ring R]
/-- CRing ⥤ ℤ-Alg -/
def alg_hom_int
{R : Type u} [comm_ring R] [algebra ℤ R]
{S : Type v} [comm_ring S] [algebra ℤ S]
(f : R → S) [is_ring_hom f] : R →ₐ[ℤ] S :=
{ to_fun := f, hom := by apply_instance,
commutes' := λ i, int.induction_on i (by rw [algebra.map_zero, algebra.map_zero, is_ring_hom.map_zero f])
(λ i ih, by rw [algebra.map_add, algebra.map_add, algebra.map_one, algebra.map_one];
rw [is_ring_hom.map_add f, is_ring_hom.map_one f, ih])
(λ i ih, by rw [algebra.map_sub, algebra.map_sub, algebra.map_one, algebra.map_one];
rw [is_ring_hom.map_sub f, is_ring_hom.map_one f, ih]) }
/-- CRing ⥤ ℤ-Alg -/
instance algebra_int : algebra ℤ R :=
algebra.of_ring_hom coe $ by constructor; intros; simp
variables {R}
/-- CRing ⥤ ℤ-Alg -/
def subalgebra_of_subring (S : set R) [is_subring S] : subalgebra ℤ R :=
{ carrier := S, range_le := λ x ⟨i, h⟩, h ▸ int.induction_on i
(by rw algebra.map_zero; exact is_add_submonoid.zero_mem _)
(λ i hi, by rw [algebra.map_add, algebra.map_one]; exact is_add_submonoid.add_mem hi (is_submonoid.one_mem _))
(λ i hi, by rw [algebra.map_sub, algebra.map_one]; exact is_add_subgroup.sub_mem _ _ _ hi (is_submonoid.one_mem _)) }
@[simp] lemma mem_subalgebra_of_subring {x : R} {S : set R} [is_subring S] :
x ∈ subalgebra_of_subring S ↔ x ∈ S :=
iff.rfl
section span_int
open submodule
lemma span_int_eq_add_group_closure (s : set R) :
↑(span ℤ s) = add_group.closure s :=
set.subset.antisymm (λ x hx, span_induction hx
(λ _, add_group.mem_closure)
(is_add_submonoid.zero_mem _)
(λ a b ha hb, is_add_submonoid.add_mem ha hb)
(λ n a ha, by { erw [show n • a = gsmul n a, from (gsmul_eq_mul a n).symm],
exact is_add_subgroup.gsmul_mem ha}))
(add_group.closure_subset subset_span)
@[simp] lemma span_int_eq (s : set R) [is_add_subgroup s] :
(↑(span ℤ s) : set R) = s :=
by rw [span_int_eq_add_group_closure, add_group.closure_add_subgroup]
end span_int
end int
|
569a29417287671aec8e7baad0cc513ede10a83a | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/tactic/push_neg.lean | 0444e836c074106289d0b72d27070491794ff6ac | [
"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 | 7,941 | lean | /-
Copyright (c) 2019 Patrick Massot All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Patrick Massot, Simon Hudon
A tactic pushing negations into an expression
-/
import logic.basic
import algebra.order
open tactic expr
namespace push_neg
section
universe u
variable {α : Sort u}
variables (p q : Prop)
variable (s : α → Prop)
local attribute [instance, priority 10] classical.prop_decidable
theorem not_not_eq : (¬ ¬ p) = p := propext not_not
theorem not_and_eq : (¬ (p ∧ q)) = (p → ¬ q) := propext not_and
theorem not_or_eq : (¬ (p ∨ q)) = (¬ p ∧ ¬ q) := propext not_or_distrib
theorem not_forall_eq : (¬ ∀ x, s x) = (∃ x, ¬ s x) := propext not_forall
theorem not_exists_eq : (¬ ∃ x, s x) = (∀ x, ¬ s x) := propext not_exists
theorem not_implies_eq : (¬ (p → q)) = (p ∧ ¬ q) := propext not_imp
theorem classical.implies_iff_not_or : (p → q) ↔ (¬ p ∨ q) := imp_iff_not_or
theorem not_eq (a b : α) : (¬ a = b) ↔ (a ≠ b) := iff.rfl
variable {β : Type u}
variable [linear_order β]
theorem not_le_eq (a b : β) : (¬ (a ≤ b)) = (b < a) := propext not_le
theorem not_lt_eq (a b : β) : (¬ (a < b)) = (b ≤ a) := propext not_lt
end
meta def whnf_reducible (e : expr) : tactic expr := whnf e reducible
private meta def transform_negation_step (e : expr) :
tactic (option (expr × expr)) :=
do e ← whnf_reducible e,
match e with
| `(¬ %%ne) :=
(do ne ← whnf_reducible ne,
match ne with
| `(¬ %%a) := do pr ← mk_app ``not_not_eq [a],
return (some (a, pr))
| `(%%a ∧ %%b) := do pr ← mk_app ``not_and_eq [a, b],
return (some (`((%%a : Prop) → ¬ %%b), pr))
| `(%%a ∨ %%b) := do pr ← mk_app ``not_or_eq [a, b],
return (some (`(¬ %%a ∧ ¬ %%b), pr))
| `(%%a ≤ %%b) := do e ← to_expr ``(%%b < %%a),
pr ← mk_app ``not_le_eq [a, b],
return (some (e, pr))
| `(%%a < %%b) := do e ← to_expr ``(%%b ≤ %%a),
pr ← mk_app ``not_lt_eq [a, b],
return (some (e, pr))
| `(Exists %%p) := do pr ← mk_app ``not_exists_eq [p],
e ← match p with
| (lam n bi typ bo) := do
body ← mk_app ``not [bo],
return (pi n bi typ body)
| _ := tactic.fail "Unexpected failure negating ∃"
end,
return (some (e, pr))
| (pi n bi d p) := if p.has_var then do
pr ← mk_app ``not_forall_eq [lam n bi d p],
body ← mk_app ``not [p],
e ← mk_app ``Exists [lam n bi d body],
return (some (e, pr))
else do
pr ← mk_app ``not_implies_eq [d, p],
`(%%_ = %%e') ← infer_type pr,
return (some (e', pr))
| _ := return none
end)
| _ := return none
end
private meta def transform_negation : expr → tactic (option (expr × expr))
| e :=
do (some (e', pr)) ← transform_negation_step e | return none,
(some (e'', pr')) ← transform_negation e' | return (some (e', pr)),
pr'' ← mk_eq_trans pr pr',
return (some (e'', pr''))
meta def normalize_negations (t : expr) : tactic (expr × expr) :=
do (_, e, pr) ← simplify_top_down ()
(λ _, λ e, do
oepr ← transform_negation e,
match oepr with
| (some (e', pr)) := return ((), e', pr)
| none := do pr ← mk_eq_refl e, return ((), e, pr)
end)
t { eta := ff },
return (e, pr)
meta def push_neg_at_hyp (h : name) : tactic unit :=
do H ← get_local h,
t ← infer_type H,
(e, pr) ← normalize_negations t,
replace_hyp H e pr,
skip
meta def push_neg_at_goal : tactic unit :=
do H ← target,
(e, pr) ← normalize_negations H,
replace_target e pr
end push_neg
open interactive (parse loc.ns loc.wildcard)
open interactive.types (location texpr)
open lean.parser (tk ident many) interactive.loc
local postfix `?`:9001 := optional
local postfix *:9001 := many
open push_neg
/--
Push negations in the goal of some assumption.
For instance, a hypothesis `h : ¬ ∀ x, ∃ y, x ≤ y` will be transformed by `push_neg at h` into
`h : ∃ x, ∀ y, y < x`. Variables names are conserved.
This tactic pushes negations inside expressions. For instance, given an assumption
```lean
h : ¬ ∀ ε > 0, ∃ δ > 0, ∀ x, |x - x₀| ≤ δ → |f x - y₀| ≤ ε)
```
writing `push_neg at h` will turn `h` into
```lean
h : ∃ ε, ε > 0 ∧ ∀ δ, δ > 0 → (∃ x, |x - x₀| ≤ δ ∧ ε < |f x - y₀|),
```
(the pretty printer does *not* use the abreviations `∀ δ > 0` and `∃ ε > 0` but this issue
has nothing to do with `push_neg`).
Note that names are conserved by this tactic, contrary to what would happen with `simp`
using the relevant lemmas. One can also use this tactic at the goal using `push_neg`,
at every assumption and the goal using `push_neg at *` or at selected assumptions and the goal
using say `push_neg at h h' ⊢` as usual.
-/
meta def tactic.interactive.push_neg : parse location → tactic unit
| (loc.ns loc_l) := loc_l.mmap'
(λ l, match l with
| some h := do push_neg_at_hyp h,
try `[simp only [push_neg.not_eq] at h { eta := ff }]
| none := do push_neg_at_goal,
try `[simp only [push_neg.not_eq] { eta := ff }]
end)
| loc.wildcard := do
push_neg_at_goal,
local_context >>= mmap' (λ h, push_neg_at_hyp (local_pp_name h)) ,
try `[simp only [push_neg.not_eq] at * { eta := ff }]
add_tactic_doc
{ name := "push_neg",
category := doc_category.tactic,
decl_names := [`tactic.interactive.push_neg],
tags := ["logic"] }
lemma imp_of_not_imp_not (P Q : Prop) : (¬ Q → ¬ P) → (P → Q) :=
λ h hP, classical.by_contradiction (λ h', h h' hP)
/-- Matches either an identifier "h" or a pair of identifiers "h with k" -/
meta def name_with_opt : lean.parser (name × option name) :=
prod.mk <$> ident <*> (some <$> (tk "with" >> ident) <|> return none)
/--
Transforms the goal into its contrapositive.
* `contrapose` turns a goal `P → Q` into `¬ Q → ¬ P`
* `contrapose!` turns a goal `P → Q` into `¬ Q → ¬ P` and pushes negations inside `P` and `Q`
using `push_neg`
* `contrapose h` first reverts the local assumption `h`, and then uses `contrapose` and `intro h`
* `contrapose! h` first reverts the local assumption `h`, and then uses `contrapose!` and `intro h`
* `contrapose h with new_h` uses the name `new_h` for the introduced hypothesis
-/
meta def tactic.interactive.contrapose (push : parse (tk "!" )?) : parse name_with_opt? → tactic unit
| (some (h, h')) := get_local h >>= revert >> tactic.interactive.contrapose none >> intro (h'.get_or_else h) >> skip
| none :=
do `(%%P → %%Q) ← target | fail "The goal is not an implication, and you didn't specify an assumption",
cp ← mk_mapp ``imp_of_not_imp_not [P, Q] <|> fail "contrapose only applies to nondependent arrows between props",
apply cp,
when push.is_some $ try (tactic.interactive.push_neg (loc.ns [none]))
add_tactic_doc
{ name := "contrapose",
category := doc_category.tactic,
decl_names := [`tactic.interactive.contrapose],
tags := ["logic"] }
|
793a9db1e25b5babb72f4f7bd69caff8b3e6fce5 | f3849be5d845a1cb97680f0bbbe03b85518312f0 | /old_library/init/logic.lean | ac6eefbade516fcf5d711cceed76e620aab077c5 | [
"Apache-2.0"
] | permissive | bjoeris/lean | 0ed95125d762b17bfcb54dad1f9721f953f92eeb | 4e496b78d5e73545fa4f9a807155113d8e6b0561 | refs/heads/master | 1,611,251,218,281 | 1,495,337,658,000 | 1,495,337,658,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 34,391 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad, Floris van Doorn
-/
prelude
import init.datatypes
universe variables u v
attribute [reducible]
definition id {A : Type u} (a : A) : A :=
a
/- TODO(Leo): for new elaborator
- Support for partially applied recursors (use eta-expansion)
- checkpoints when processing calc.
-/
/- implication -/
definition implies (a b : Prop) := a → b
attribute [trans]
lemma implies.trans {p q r : Prop} (h₁ : implies p q) (h₂ : implies q r) : implies p r :=
assume hp, h₂ (h₁ hp)
definition trivial : true := ⟨⟩
definition not (a : Prop) := a → false
prefix `¬` := not
attribute [inline]
definition absurd {a : Prop} {b : Type v} (h₁ : a) (h₂ : ¬a) : b :=
false.rec b (h₂ h₁)
attribute [intro!]
lemma not.intro {a : Prop} (h : a → false) : ¬ a :=
h
theorem mt {a b : Prop} (h₁ : a → b) (h₂ : ¬b) : ¬a :=
assume ha : a, absurd (h₁ ha) h₂
definition implies.resolve {a b : Prop} (h : a → b) (nb : ¬ b) : ¬ a := assume ha, nb (h ha)
/- not -/
theorem not_false : ¬false :=
assume h : false, h
definition non_contradictory (a : Prop) : Prop := ¬¬a
theorem non_contradictory_intro {a : Prop} (ha : a) : ¬¬a :=
assume hna : ¬a, absurd ha hna
/- false -/
theorem false.elim {c : Prop} (h : false) : c :=
false.rec c h
/- eq -/
-- proof irrelevance is built in
theorem proof_irrel {a : Prop} (h₁ h₂ : a) : h₁ = h₂ :=
rfl
attribute [defeq]
definition id.def {A : Type u} (a : A) : id a = a := rfl
-- Remark: we provide the universe levels explicitly to make sure `eq.drec` has the same type of `eq.rec` in the hoTT library
attribute [elab_as_eliminator]
protected theorem {u₁ u₂} eq.drec {A : Type u₂} {a : A} {C : Π (x : A), a = x → Type u₁} (h₁ : C a (eq.refl a)) {b : A} (h₂ : a = b) : C b h₂ :=
eq.rec (λ h₂ : a = a, show C a h₂, from h₁) h₂ h₂
namespace eq
variables {A : Type u}
variables {a b c a': A}
attribute [elab_as_eliminator]
protected theorem drec_on {a : A} {C : Π (x : A), a = x → Type v} {b : A} (h₂ : a = b) (h₁ : C a (refl a)) : C b h₂ :=
eq.drec h₁ h₂
theorem substr {p : A → Prop} (h₁ : b = a) : p a → p b :=
subst (symm h₁)
theorem mp {A B : Type u} : (A = B) → A → B :=
eq.rec_on
theorem mpr {A B : Type u} : (A = B) → B → A :=
assume h₁ h₂, eq.rec_on (eq.symm h₁) h₂
end eq
open eq
theorem congr {A : Type u} {B : Type v} {f₁ f₂ : A → B} {a₁ a₂ : A} (h₁ : f₁ = f₂) (h₂ : a₁ = a₂) : f₁ a₁ = f₂ a₂ :=
eq.subst h₁ (eq.subst h₂ rfl)
theorem congr_fun {A : Type u} {B : A → Type v} {f g : Π x, B x} (h : f = g) (a : A) : f a = g a :=
eq.subst h (eq.refl (f a))
theorem congr_arg {A : Type u} {B : Type v} {a₁ a₂ : A} (f : A → B) : a₁ = a₂ → f a₁ = f a₂ :=
congr rfl
section
variables {A : Type u} {a b c: A}
theorem trans_rel_left (r : A → A → Prop) (h₁ : r a b) (h₂ : b = c) : r a c :=
h₂ ▸ h₁
theorem trans_rel_right (r : A → A → Prop) (h₁ : a = b) (h₂ : r b c) : r a c :=
symm h₁ ▸ h₂
end
section
variable {p : Prop}
theorem of_eq_true (h : p = true) : p :=
symm h ▸ trivial
theorem not_of_eq_false (h : p = false) : ¬p :=
assume hp, h ▸ hp
end
attribute [inline]
definition cast {A B : Type u} (h : A = B) (a : A) : B :=
eq.rec a h
theorem cast_proof_irrel {A B : Type u} (h₁ h₂ : A = B) (a : A) : cast h₁ a = cast h₂ a :=
rfl
theorem cast_eq {A : Type u} (h : A = A) (a : A) : cast h a = a :=
rfl
/- ne -/
attribute [reducible]
definition ne {A : Type u} (a b : A) := ¬(a = b)
attribute [defeq]
definition ne.def {A : Type u} (a b : A) : ne a b = ¬ (a = b) := rfl
notation a ≠ b := ne a b
namespace ne
variable {A : Type u}
variables {a b : A}
theorem intro (h : a = b → false) : a ≠ b := h
theorem elim (h : a ≠ b) : a = b → false := h
theorem irrefl (h : a ≠ a) : false := h rfl
theorem symm (h : a ≠ b) : b ≠ a :=
assume (h₁ : b = a), h (symm h₁)
end ne
theorem false_of_ne {A : Type u} {a : A} : a ≠ a → false := ne.irrefl
section
variables {p : Prop}
theorem ne_false_of_self : p → p ≠ false :=
assume (hp : p) (heq : p = false), heq ▸ hp
theorem ne_true_of_not : ¬p → p ≠ true :=
assume (hnp : ¬p) (heq : p = true), (heq ▸ hnp) trivial
theorem true_ne_false : ¬true = false :=
ne_false_of_self trivial
end
infixl ` == `:50 := heq
section
variables {A B C : Type u} {a a' : A} {b b' : B} {c : C}
theorem eq_of_heq (h : a == a') : a = a' :=
have ∀ (A' : Type u) (a' : A') (h₁ : @heq A a A' a') (h₂ : A = A'), (eq.rec_on h₂ a : A') = a', from
λ (A' : Type u) (a' : A') (h₁ : @heq A a A' a'), heq.rec_on h₁ (λ h₂ : A = A, rfl),
show (eq.rec_on (eq.refl A) a : A) = a', from
this A a' h (eq.refl A)
theorem heq.elim {A : Type u} {a : A} {p : A → Type v} {b : A} (h₁ : a == b)
: p a → p b := eq.rec_on (eq_of_heq h₁)
theorem heq.subst {p : ∀ T : Type u, T → Prop} : a == b → p A a → p B b :=
heq.rec_on
theorem heq.symm (h : a == b) : b == a :=
heq.rec_on h (heq.refl a)
theorem heq_of_eq (h : a = a') : a == a' :=
eq.subst h (heq.refl a)
theorem heq.trans (h₁ : a == b) (h₂ : b == c) : a == c :=
heq.subst h₂ h₁
theorem heq_of_heq_of_eq (h₁ : a == b) (h₂ : b = b') : a == b' :=
heq.trans h₁ (heq_of_eq h₂)
theorem heq_of_eq_of_heq (h₁ : a = a') (h₂ : a' == b) : a == b :=
heq.trans (heq_of_eq h₁) h₂
definition type_eq_of_heq (h : a == b) : A = B :=
heq.rec_on h (eq.refl A)
end
theorem eq_rec_heq {A : Type u} {C : A → Type v} : ∀ {a a' : A} (h : a = a') (p : C a), (eq.rec_on h p : C a') == p
| a .a rfl p := heq.refl p
theorem heq_of_eq_rec_left {A : Type u} {C : A → Type v} : ∀ {a a' : A} {p₁ : C a} {p₂ : C a'} (e : a = a') (h₂ : (eq.rec_on e p₁ : C a') = p₂), p₁ == p₂
| a .a p₁ p₂ (eq.refl .a) h := eq.rec_on h (heq.refl p₁)
theorem heq_of_eq_rec_right {A : Type u} {C : A → Type v} : ∀ {a a' : A} {p₁ : C a} {p₂ : C a'} (e : a' = a) (h₂ : p₁ = eq.rec_on e p₂), p₁ == p₂
| a .a p₁ p₂ (eq.refl .a) h :=
have p₁ = p₂, from h,
this ▸ heq.refl p₁
theorem of_heq_true {a : Prop} (h : a == true) : a :=
of_eq_true (eq_of_heq h)
theorem eq_rec_compose : ∀ {A B C : Type u} (p₁ : B = C) (p₂ : A = B) (a : A), (eq.rec_on p₁ (eq.rec_on p₂ a : B) : C) = eq.rec_on (eq.trans p₂ p₁) a
| A .A .A (eq.refl .A) (eq.refl .A) a := rfl
theorem eq_rec_eq_eq_rec : ∀ {A₁ A₂ : Type u} {p : A₁ = A₂} {a₁ : A₁} {a₂ : A₂}, (eq.rec_on p a₁ : A₂) = a₂ → a₁ = eq.rec_on (eq.symm p) a₂
| A .A rfl a .a rfl := rfl
theorem eq_rec_of_heq_left : ∀ {A₁ A₂ : Type u} {a₁ : A₁} {a₂ : A₂} (h : a₁ == a₂), (eq.rec_on (type_eq_of_heq h) a₁ : A₂) = a₂
| A .A a .a (heq.refl .a) := rfl
theorem eq_rec_of_heq_right {A₁ A₂ : Type u} {a₁ : A₁} {a₂ : A₂} (h : a₁ == a₂) : a₁ = eq.rec_on (eq.symm (type_eq_of_heq h)) a₂ :=
eq_rec_eq_eq_rec (eq_rec_of_heq_left h)
attribute heq.refl [refl]
attribute heq.trans [trans]
attribute heq_of_heq_of_eq [trans]
attribute heq_of_eq_of_heq [trans]
attribute heq.symm [symm]
theorem cast_heq : ∀ {A B : Type u} (h : A = B) (a : A), cast h a == a
| A .A (eq.refl .A) a := heq.refl a
/- and -/
notation a /\ b := and a b
notation a ∧ b := and a b
variables {a b c d : Prop}
attribute and.rec [elim]
attribute and.intro [intro!]
theorem and.elim (h₁ : a ∧ b) (h₂ : a → b → c) : c :=
and.rec h₂ h₁
theorem and.swap : a ∧ b → b ∧ a :=
assume ⟨ha, hb⟩, ⟨hb, ha⟩
/- or -/
notation a \/ b := or a b
notation a ∨ b := or a b
attribute or.rec [elim]
namespace or
theorem elim (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → c) : c :=
or.rec h₂ h₃ h₁
end or
theorem non_contradictory_em (a : Prop) : ¬¬(a ∨ ¬a) :=
assume not_em : ¬(a ∨ ¬a),
have neg_a : ¬a, from
assume pos_a : a, absurd (or.inl pos_a) not_em,
absurd (or.inr neg_a) not_em
theorem or.swap : a ∨ b → b ∨ a := or.rec or.inr or.inl
/- xor -/
definition xor (a b : Prop) := (a ∧ ¬ b) ∨ (b ∧ ¬ a)
/- iff -/
definition iff (a b : Prop) := (a → b) ∧ (b → a)
notation a <-> b := iff a b
notation a ↔ b := iff a b
theorem iff.intro : (a → b) → (b → a) → (a ↔ b) := and.intro
attribute iff.intro [intro!]
theorem iff.elim : ((a → b) → (b → a) → c) → (a ↔ b) → c := and.rec
attribute iff.elim [recursor 5, elim]
theorem iff.elim_left : (a ↔ b) → a → b := and.left
definition iff.mp := @iff.elim_left
theorem iff.elim_right : (a ↔ b) → b → a := and.right
definition iff.mpr := @iff.elim_right
attribute [refl]
theorem iff.refl (a : Prop) : a ↔ a :=
iff.intro (assume h, h) (assume h, h)
theorem iff.rfl {a : Prop} : a ↔ a :=
iff.refl a
attribute [trans]
theorem iff.trans (h₁ : a ↔ b) (h₂ : b ↔ c) : a ↔ c :=
iff.intro
(assume ha, iff.mp h₂ (iff.mp h₁ ha))
(assume hc, iff.mpr h₁ (iff.mpr h₂ hc))
attribute [symm]
theorem iff.symm (h : a ↔ b) : b ↔ a :=
iff.intro (iff.elim_right h) (iff.elim_left h)
theorem iff.comm : (a ↔ b) ↔ (b ↔ a) :=
iff.intro iff.symm iff.symm
theorem iff.of_eq {a b : Prop} (h : a = b) : a ↔ b :=
eq.rec_on h iff.rfl
theorem not_iff_not_of_iff (h₁ : a ↔ b) : ¬a ↔ ¬b :=
iff.intro
(assume (hna : ¬ a) (hb : b), hna (iff.elim_right h₁ hb))
(assume (hnb : ¬ b) (ha : a), hnb (iff.elim_left h₁ ha))
theorem of_iff_true (h : a ↔ true) : a :=
iff.mp (iff.symm h) trivial
theorem not_of_iff_false : (a ↔ false) → ¬a := iff.mp
theorem iff_true_intro (h : a) : a ↔ true :=
iff.intro
(λ hl, trivial)
(λ hr, h)
theorem iff_false_intro (h : ¬a) : a ↔ false :=
iff.intro h (false.rec a)
theorem not_non_contradictory_iff_absurd (a : Prop) : ¬¬¬a ↔ ¬a :=
iff.intro
(λ (hl : ¬¬¬a) (ha : a), hl (non_contradictory_intro ha))
absurd
theorem imp_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a → b) ↔ (c → d) :=
iff.intro
(λ hab hc, iff.mp h₂ (hab (iff.mpr h₁ hc)))
(λ hcd ha, iff.mpr h₂ (hcd (iff.mp h₁ ha)))
theorem imp_congr_ctx (h₁ : a ↔ c) (h₂ : c → (b ↔ d)) : (a → b) ↔ (c → d) :=
iff.intro
(λ hab hc, have ha : a, from iff.mpr h₁ hc,
have hb : b, from hab ha,
iff.mp (h₂ hc) hb)
(λ hcd ha, have hc : c, from iff.mp h₁ ha,
have hd : d, from hcd hc,
iff.mpr (h₂ hc) hd)
theorem imp_congr_right (h : a → (b ↔ c)) : (a → b) ↔ (a → c) :=
iff.intro
(take hab ha, iff.elim_left (h ha) (hab ha))
(take hab ha, iff.elim_right (h ha) (hab ha))
theorem not_not_intro (ha : a) : ¬¬a :=
assume hna : ¬a, hna ha
theorem not_of_not_not_not (h : ¬¬¬a) : ¬a :=
λ ha, absurd (not_not_intro ha) h
attribute [simp]
theorem not_true : (¬ true) ↔ false :=
iff_false_intro (not_not_intro trivial)
attribute [simp]
theorem not_false_iff : (¬ false) ↔ true :=
iff_true_intro not_false
attribute [congr]
theorem not_congr (h : a ↔ b) : ¬a ↔ ¬b :=
iff.intro (λ h₁ h₂, h₁ (iff.mpr h h₂)) (λ h₁ h₂, h₁ (iff.mp h h₂))
attribute [simp]
theorem ne_self_iff_false {A : Type u} (a : A) : (not (a = a)) ↔ false :=
iff.intro false_of_ne false.elim
attribute [simp]
theorem eq_self_iff_true {A : Type u} (a : A) : (a = a) ↔ true :=
iff_true_intro rfl
attribute [simp]
theorem heq_self_iff_true {A : Type u} (a : A) : (a == a) ↔ true :=
iff_true_intro (heq.refl a)
attribute [simp]
theorem iff_not_self (a : Prop) : (a ↔ ¬a) ↔ false :=
iff_false_intro (λ h,
have h' : ¬a, from (λ ha, (iff.mp h ha) ha),
h' (iff.mpr h h'))
attribute [simp]
theorem not_iff_self (a : Prop) : (¬a ↔ a) ↔ false :=
iff_false_intro (λ h,
have h' : ¬a, from (λ ha, (iff.mpr h ha) ha),
h' (iff.mp h h'))
attribute [simp]
theorem true_iff_false : (true ↔ false) ↔ false :=
iff_false_intro (λ h, iff.mp h trivial)
attribute [simp]
theorem false_iff_true : (false ↔ true) ↔ false :=
iff_false_intro (λ h, iff.mpr h trivial)
theorem false_of_true_iff_false : (true ↔ false) → false :=
assume h, iff.mp h trivial
/- and simp rules -/
theorem and.imp (hac : a → c) (hbd : b → d) : a ∧ b → c ∧ d :=
assume ⟨ha, hb⟩, ⟨hac ha, hbd hb⟩
attribute [congr]
theorem and_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∧ b) ↔ (c ∧ d) :=
iff.intro (and.imp (iff.mp h₁) (iff.mp h₂)) (and.imp (iff.mpr h₁) (iff.mpr h₂))
theorem and_congr_right (h : a → (b ↔ c)) : (a ∧ b) ↔ (a ∧ c) :=
iff.intro
(assume ⟨ha, hb⟩, ⟨ha, iff.elim_left (h ha) hb⟩)
(assume ⟨ha, hc⟩, ⟨ha, iff.elim_right (h ha) hc⟩)
attribute [simp]
theorem and.comm : a ∧ b ↔ b ∧ a :=
iff.intro and.swap and.swap
attribute [simp]
theorem and.assoc : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) :=
iff.intro
(assume ⟨⟨ha, hb⟩, hc⟩, ⟨ha, ⟨hb, hc⟩⟩)
(assume ⟨ha, ⟨hb, hc⟩⟩, ⟨⟨ha, hb⟩, hc⟩)
attribute [simp]
theorem and.left_comm : a ∧ (b ∧ c) ↔ b ∧ (a ∧ c) :=
iff.trans (iff.symm and.assoc) (iff.trans (and_congr and.comm (iff.refl c)) and.assoc)
theorem and_iff_left {a b : Prop} (hb : b) : (a ∧ b) ↔ a :=
iff.intro and.left (λ ha, ⟨ha, hb⟩)
theorem and_iff_right {a b : Prop} (ha : a) : (a ∧ b) ↔ b :=
iff.intro and.right (and.intro ha)
attribute [simp]
theorem and_true (a : Prop) : a ∧ true ↔ a :=
and_iff_left trivial
attribute [simp]
theorem true_and (a : Prop) : true ∧ a ↔ a :=
and_iff_right trivial
attribute [simp]
theorem and_false (a : Prop) : a ∧ false ↔ false :=
iff_false_intro and.right
attribute [simp]
theorem false_and (a : Prop) : false ∧ a ↔ false :=
iff_false_intro and.left
attribute [simp]
theorem not_and_self (a : Prop) : (¬a ∧ a) ↔ false :=
iff_false_intro (λ h, and.elim h (λ h₁ h₂, absurd h₂ h₁))
attribute [simp]
theorem and_not_self (a : Prop) : (a ∧ ¬a) ↔ false :=
iff_false_intro (assume ⟨h₁, h₂⟩, absurd h₁ h₂)
attribute [simp]
theorem and_self (a : Prop) : a ∧ a ↔ a :=
iff.intro and.left (assume h, ⟨h, h⟩)
/- or simp rules -/
theorem or.imp (h₂ : a → c) (h₃ : b → d) : a ∨ b → c ∨ d :=
or.rec (λ h, or.inl (h₂ h)) (λ h, or.inr (h₃ h))
theorem or.imp_left (h : a → b) : a ∨ c → b ∨ c :=
or.imp h id
theorem or.imp_right (h : a → b) : c ∨ a → c ∨ b :=
or.imp id h
attribute [congr]
theorem or_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∨ b) ↔ (c ∨ d) :=
iff.intro (or.imp (iff.mp h₁) (iff.mp h₂)) (or.imp (iff.mpr h₁) (iff.mpr h₂))
attribute [simp]
theorem or.comm : a ∨ b ↔ b ∨ a := iff.intro or.swap or.swap
attribute [simp]
theorem or.assoc : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) :=
iff.intro
(or.rec (or.imp_right or.inl) (λ h, or.inr (or.inr h)))
(or.rec (λ h, or.inl (or.inl h)) (or.imp_left or.inr))
attribute [simp]
theorem or.left_comm : a ∨ (b ∨ c) ↔ b ∨ (a ∨ c) :=
iff.trans (iff.symm or.assoc) (iff.trans (or_congr or.comm (iff.refl c)) or.assoc)
attribute [simp]
theorem or_true (a : Prop) : a ∨ true ↔ true :=
iff_true_intro (or.inr trivial)
attribute [simp]
theorem true_or (a : Prop) : true ∨ a ↔ true :=
iff_true_intro (or.inl trivial)
attribute [simp]
theorem or_false (a : Prop) : a ∨ false ↔ a :=
iff.intro (or.rec id false.elim) or.inl
attribute [simp]
theorem false_or (a : Prop) : false ∨ a ↔ a :=
iff.trans or.comm (or_false a)
attribute [simp]
theorem or_self (a : Prop) : a ∨ a ↔ a :=
iff.intro (or.rec id id) or.inl
/- or resolution rulse -/
definition or.resolve_left {a b : Prop} (h : a ∨ b) (na : ¬ a) : b :=
or.elim h (λ ha, absurd ha na) id
definition or.neg_resolve_left {a b : Prop} (h : ¬ a ∨ b) (ha : a) : b :=
or.elim h (λ na, absurd ha na) id
definition or.resolve_right {a b : Prop} (h : a ∨ b) (nb : ¬ b) : a :=
or.elim h id (λ hb, absurd hb nb)
definition or.neg_resolve_right {a b : Prop} (h : a ∨ ¬ b) (hb : b) : a :=
or.elim h id (λ nb, absurd hb nb)
/- iff simp rules -/
attribute [simp]
theorem iff_true (a : Prop) : (a ↔ true) ↔ a :=
iff.intro (assume h, iff.mpr h trivial) iff_true_intro
attribute [simp]
theorem true_iff (a : Prop) : (true ↔ a) ↔ a :=
iff.trans iff.comm (iff_true a)
attribute [simp]
theorem iff_false (a : Prop) : (a ↔ false) ↔ ¬ a :=
iff.intro and.left iff_false_intro
attribute [simp]
theorem false_iff (a : Prop) : (false ↔ a) ↔ ¬ a :=
iff.trans iff.comm (iff_false a)
attribute [simp]
theorem iff_self (a : Prop) : (a ↔ a) ↔ true :=
iff_true_intro iff.rfl
attribute [congr]
theorem iff_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ↔ b) ↔ (c ↔ d) :=
and_congr (imp_congr h₁ h₂) (imp_congr h₂ h₁)
/- exists -/
inductive Exists {A : Type u} (p : A → Prop) : Prop
| intro : ∀ (a : A), p a → Exists
attribute Exists.intro [intro]
definition exists.intro := @Exists.intro
notation `exists` binders `, ` r:(scoped P, Exists P) := r
notation `∃` binders `, ` r:(scoped P, Exists P) := r
attribute Exists.rec [elim]
theorem exists.elim {A : Type u} {p : A → Prop} {b : Prop}
(h₁ : ∃ x, p x) (h₂ : ∀ (a : A), p a → b) : b :=
Exists.rec h₂ h₁
/- exists unique -/
definition exists_unique {A : Type u} (p : A → Prop) :=
∃ x, p x ∧ ∀ y, p y → y = x
notation `∃!` binders `, ` r:(scoped P, exists_unique P) := r
attribute [intro]
theorem exists_unique.intro {A : Type u} {p : A → Prop} (w : A) (h₁ : p w) (h₂ : ∀ y, p y → y = w) :
∃! x, p x :=
exists.intro w ⟨h₁, h₂⟩
attribute [recursor 4, elim]
theorem exists_unique.elim {A : Type u} {p : A → Prop} {b : Prop}
(h₂ : ∃! x, p x) (h₁ : ∀ x, p x → (∀ y, p y → y = x) → b) : b :=
exists.elim h₂ (λ w hw, h₁ w (and.left hw) (and.right hw))
theorem exists_unique_of_exists_of_unique {A : Type u} {p : A → Prop}
(hex : ∃ x, p x) (hunique : ∀ y₁ y₂, p y₁ → p y₂ → y₁ = y₂) : ∃! x, p x :=
exists.elim hex (λ x px, exists_unique.intro x px (take y, suppose p y, hunique y x this px))
theorem exists_of_exists_unique {A : Type u} {p : A → Prop} (h : ∃! x, p x) : ∃ x, p x :=
exists.elim h (λ x hx, ⟨x, and.left hx⟩)
theorem unique_of_exists_unique {A : Type u} {p : A → Prop}
(h : ∃! x, p x) {y₁ y₂ : A} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ :=
exists_unique.elim h
(take x, suppose p x,
assume unique : ∀ y, p y → y = x,
show y₁ = y₂, from eq.trans (unique _ py₁) (eq.symm (unique _ py₂)))
/- exists, forall, exists unique congruences -/
attribute [congr]
theorem forall_congr {A : Type u} {p q : A → Prop} (h : ∀ a, (p a ↔ q a)) : (∀ a, p a) ↔ ∀ a, q a :=
iff.intro (λ p a, iff.mp (h a) (p a)) (λ q a, iff.mpr (h a) (q a))
theorem exists_imp_exists {A : Type u} {p q : A → Prop} (h : ∀ a, (p a → q a)) (p : ∃ a, p a) : ∃ a, q a :=
exists.elim p (λ a hp, ⟨a, h a hp⟩)
attribute [congr]
theorem exists_congr {A : Type u} {p q : A → Prop} (h : ∀ a, (p a ↔ q a)) : (Exists p) ↔ ∃ a, q a :=
iff.intro
(exists_imp_exists (λ a, iff.mp (h a)))
(exists_imp_exists (λ a, iff.mpr (h a)))
attribute [congr]
theorem exists_unique_congr {A : Type u} {p₁ p₂ : A → Prop} (h : ∀ x, p₁ x ↔ p₂ x) : (exists_unique p₁) ↔ (∃! x, p₂ x) := --
exists_congr (λ x, and_congr (h x) (forall_congr (λ y, imp_congr (h y) iff.rfl)))
/- decidable -/
inductive [class] decidable (p : Prop)
| is_false : ¬p → decidable
| is_true : p → decidable
export decidable (is_true is_false)
attribute [instance]
definition decidable_true : decidable true :=
is_true trivial
attribute [instance]
definition decidable_false : decidable false :=
is_false not_false
-- We use "dependent" if-then-else to be able to communicate the if-then-else condition
-- to the branches
attribute [inline]
definition dite (c : Prop) [h : decidable c] {A : Type u} : (c → A) → (¬ c → A) → A :=
λ t e, decidable.rec_on h e t
/- if-then-else -/
attribute [inline]
definition ite (c : Prop) [h : decidable c] {A : Type u} (t e : A) : A :=
decidable.rec_on h (λ hnc, e) (λ hc, t)
namespace decidable
variables {p q : Prop}
definition rec_on_true [h : decidable p] {h₁ : p → Type u} {h₂ : ¬p → Type u} (h₃ : p) (h₄ : h₁ h₃)
: decidable.rec_on h h₂ h₁ :=
decidable.rec_on h (λ h, false.rec _ (h h₃)) (λ h, h₄)
definition rec_on_false [h : decidable p] {h₁ : p → Type u} {h₂ : ¬p → Type u} (h₃ : ¬p) (h₄ : h₂ h₃)
: decidable.rec_on h h₂ h₁ :=
decidable.rec_on h (λ h, h₄) (λ h, false.rec _ (h₃ h))
definition by_cases {q : Type u} [C : decidable p] : (p → q) → (¬p → q) → q := dite _
theorem em (p : Prop) [decidable p] : p ∨ ¬p := by_cases or.inl or.inr
theorem by_contradiction [decidable p] (h : ¬p → false) : p :=
if h₁ : p then h₁ else false.rec _ (h h₁)
definition to_bool (p : Prop) [h : decidable p] : bool :=
decidable.cases_on h (λ h₁, bool.ff) (λ h₂, bool.tt)
end decidable
section
variables {p q : Prop}
definition decidable_of_decidable_of_iff (hp : decidable p) (h : p ↔ q) : decidable q :=
if hp : p then is_true (iff.mp h hp)
else is_false (iff.mp (not_iff_not_of_iff h) hp)
definition decidable_of_decidable_of_eq (hp : decidable p) (h : p = q) : decidable q :=
decidable_of_decidable_of_iff hp (iff.of_eq h)
protected definition or.by_cases [decidable p] [decidable q] {A : Type u}
(h : p ∨ q) (h₁ : p → A) (h₂ : q → A) : A :=
if hp : p then h₁ hp else
if hq : q then h₂ hq else
false.rec _ (or.elim h hp hq)
end
section
variables {p q : Prop}
attribute [instance]
definition decidable_and [decidable p] [decidable q] : decidable (p ∧ q) :=
if hp : p then
if hq : q then is_true ⟨hp, hq⟩
else is_false (assume h : p ∧ q, hq (and.right h))
else is_false (assume h : p ∧ q, hp (and.left h))
attribute [instance]
definition decidable_or [decidable p] [decidable q] : decidable (p ∨ q) :=
if hp : p then is_true (or.inl hp) else
if hq : q then is_true (or.inr hq) else
is_false (or.rec hp hq)
attribute [instance]
definition decidable_not [decidable p] : decidable (¬p) :=
if hp : p then is_false (absurd hp) else is_true hp
attribute [instance]
definition decidable_implies [decidable p] [decidable q] : decidable (p → q) :=
if hp : p then
if hq : q then is_true (assume h, hq)
else is_false (assume h : p → q, absurd (h hp) hq)
else is_true (assume h, absurd h hp)
attribute [instance]
definition decidable_iff [decidable p] [decidable q] : decidable (p ↔ q) :=
decidable_and
end
attribute [reducible]
definition decidable_pred {A : Type u} (r : A → Prop) := Π (a : A), decidable (r a)
attribute [reducible]
definition decidable_rel {A : Type u} (r : A → A → Prop) := Π (a b : A), decidable (r a b)
attribute [reducible]
definition decidable_eq (A : Type u) := decidable_rel (@eq A)
attribute [instance]
definition decidable_ne {A : Type u} [decidable_eq A] (a b : A) : decidable (a ≠ b) :=
decidable_implies
theorem bool.ff_ne_tt : ff = tt → false
.
definition is_dec_eq {A : Type u} (p : A → A → bool) : Prop := ∀ ⦃x y : A⦄, p x y = tt → x = y
definition is_dec_refl {A : Type u} (p : A → A → bool) : Prop := ∀ x, p x x = tt
open decidable
attribute [instance]
protected definition bool.has_decidable_eq : ∀ a b : bool, decidable (a = b)
| ff ff := is_true rfl
| ff tt := is_false bool.ff_ne_tt
| tt ff := is_false (ne.symm bool.ff_ne_tt)
| tt tt := is_true rfl
definition decidable_eq_of_bool_pred {A : Type u} {p : A → A → bool} (h₁ : is_dec_eq p) (h₂ : is_dec_refl p) : decidable_eq A :=
take x y : A,
if hp : p x y = tt then is_true (h₁ hp)
else is_false (assume hxy : x = y, absurd (h₂ y) (@eq.rec_on _ _ (λ z, ¬p z y = tt) _ hxy hp))
theorem decidable_eq_inl_refl {A : Type u} [h : decidable_eq A] (a : A) : h a a = is_true (eq.refl a) :=
match (h a a) with
| (is_true e) := rfl
| (is_false n) := absurd rfl n
end
theorem decidable_eq_inr_neg {A : Type u} [h : decidable_eq A] {a b : A} : Π n : a ≠ b, h a b = is_false n :=
assume n,
match (h a b) with
| (is_true e) := absurd e n
| (is_false n₁) := proof_irrel n n₁ ▸ eq.refl (is_false n)
end
/- inhabited -/
inductive [class] inhabited (A : Type u)
| mk : A → inhabited
attribute [inline]
protected definition inhabited.value {A : Type u} : inhabited A → A :=
λ h, inhabited.rec (λ a, a) h
attribute [inline]
protected definition inhabited.destruct {A : Type u} {B : Type v} (h₁ : inhabited A) (h₂ : A → B) : B :=
inhabited.rec h₂ h₁
attribute [inline]
definition default (A : Type u) [h : inhabited A] : A :=
inhabited.value h
attribute [inline, irreducible]
definition arbitrary (A : Type u) [h : inhabited A] : A :=
inhabited.value h
attribute [instance]
definition Prop.is_inhabited : inhabited Prop :=
⟨true⟩
attribute [instance]
definition inhabited_fun (A : Type u) {B : Type v} [h : inhabited B] : inhabited (A → B) :=
inhabited.rec_on h (λ b, ⟨λ a, b⟩)
attribute [instance]
definition inhabited_Pi (A : Type u) {B : A → Type v} [Π x, inhabited (B x)] :
inhabited (Π x, B x) :=
⟨λ a, default (B a)⟩
attribute [inline, instance]
protected definition bool.is_inhabited : inhabited bool :=
⟨ff⟩
attribute [inline, instance]
protected definition pos_num.is_inhabited : inhabited pos_num :=
⟨pos_num.one⟩
attribute [inline, instance]
protected definition num.is_inhabited : inhabited num :=
⟨num.zero⟩
inductive [class] nonempty (A : Type u) : Prop
| intro : A → nonempty
protected definition nonempty.elim {A : Type u} {p : Prop} (h₁ : nonempty A) (h₂ : A → p) : p :=
nonempty.rec h₂ h₁
attribute [instance]
theorem nonempty_of_inhabited {A : Type u} [inhabited A] : nonempty A :=
⟨default A⟩
theorem nonempty_of_exists {A : Type u} {p : A → Prop} : (∃ x, p x) → nonempty A
| ⟨w, h⟩ := ⟨w⟩
/- subsingleton -/
inductive [class] subsingleton (A : Type u) : Prop
| intro : (∀ a b : A, a = b) → subsingleton
protected definition subsingleton.elim {A : Type u} [h : subsingleton A] : ∀ (a b : A), a = b :=
subsingleton.rec (λ p, p) h
protected definition subsingleton.helim {A B : Type u} [h : subsingleton A] (h : A = B) : ∀ (a : A) (b : B), a == b :=
eq.rec_on h (λ a b : A, heq_of_eq (subsingleton.elim a b))
attribute [instance]
definition subsingleton_prop (p : Prop) : subsingleton p :=
⟨λ a b, proof_irrel a b⟩
attribute [instance]
definition subsingleton_decidable (p : Prop) : subsingleton (decidable p) :=
subsingleton.intro (λ d₁,
match d₁ with
| (is_true t₁) := (λ d₂,
match d₂ with
| (is_true t₂) := eq.rec_on (proof_irrel t₁ t₂) rfl
| (is_false f₂) := absurd t₁ f₂
end)
| (is_false f₁) := (λ d₂,
match d₂ with
| (is_true t₂) := absurd t₂ f₁
| (is_false f₂) := eq.rec_on (proof_irrel f₁ f₂) rfl
end)
end)
protected theorem rec_subsingleton {p : Prop} [h : decidable p]
{h₁ : p → Type u} {h₂ : ¬p → Type u}
[h₃ : Π (h : p), subsingleton (h₁ h)] [h₄ : Π (h : ¬p), subsingleton (h₂ h)]
: subsingleton (decidable.rec_on h h₂ h₁) :=
match h with
| (is_true h) := h₃ h
| (is_false h) := h₄ h
end
theorem if_pos {c : Prop} [h : decidable c] (hc : c) {A : Type u} {t e : A} : (ite c t e) = t :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := absurd hc hnc
end
theorem if_neg {c : Prop} [h : decidable c] (hnc : ¬c) {A : Type u} {t e : A} : (ite c t e) = e :=
match h with
| (is_true hc) := absurd hc hnc
| (is_false hnc) := rfl
end
attribute [simp]
theorem if_t_t (c : Prop) [h : decidable c] {A : Type u} (t : A) : (ite c t t) = t :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := rfl
end
theorem implies_of_if_pos {c t e : Prop} [decidable c] (h : ite c t e) : c → t :=
assume hc, eq.rec_on (if_pos hc : ite c t e = t) h
theorem implies_of_if_neg {c t e : Prop} [decidable c] (h : ite c t e) : ¬c → e :=
assume hnc, eq.rec_on (if_neg hnc : ite c t e = e) h
theorem if_ctx_congr {A : Type u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x y u v : A}
(h_c : b ↔ c) (h_t : c → x = u) (h_e : ¬c → y = v) :
ite b x y = ite c u v :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
attribute [congr]
theorem if_congr {A : Type u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x y u v : A}
(h_c : b ↔ c) (h_t : x = u) (h_e : y = v) :
ite b x y = ite c u v :=
@if_ctx_congr A b c dec_b dec_c x y u v h_c (λ h, h_t) (λ h, h_e)
theorem if_ctx_simp_congr {A : Type u} {b c : Prop} [dec_b : decidable b] {x y u v : A}
(h_c : b ↔ c) (h_t : c → x = u) (h_e : ¬c → y = v) :
ite b x y = (@ite c (decidable_of_decidable_of_iff dec_b h_c) A u v) :=
@if_ctx_congr A b c dec_b (decidable_of_decidable_of_iff dec_b h_c) x y u v h_c h_t h_e
attribute [congr]
theorem if_simp_congr {A : Type u} {b c : Prop} [dec_b : decidable b] {x y u v : A}
(h_c : b ↔ c) (h_t : x = u) (h_e : y = v) :
ite b x y = (@ite c (decidable_of_decidable_of_iff dec_b h_c) A u v) :=
@if_ctx_simp_congr A b c dec_b x y u v h_c (λ h, h_t) (λ h, h_e)
attribute [simp]
definition if_true {A : Type u} (t e : A) : (if true then t else e) = t :=
if_pos trivial
attribute [simp]
definition if_false {A : Type u} (t e : A) : (if false then t else e) = e :=
if_neg not_false
theorem if_ctx_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c]
(h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) :
ite b x y ↔ ite c u v :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
attribute [congr]
theorem if_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c]
(h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) :
ite b x y ↔ ite c u v :=
if_ctx_congr_prop h_c (λ h, h_t) (λ h, h_e)
theorem if_ctx_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b]
(h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) :
ite b x y ↔ (@ite c (decidable_of_decidable_of_iff dec_b h_c) Prop u v) :=
@if_ctx_congr_prop b c x y u v dec_b (decidable_of_decidable_of_iff dec_b h_c) h_c h_t h_e
attribute [congr]
theorem if_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b]
(h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) :
ite b x y ↔ (@ite c (decidable_of_decidable_of_iff dec_b h_c) Prop u v) :=
@if_ctx_simp_congr_prop b c x y u v dec_b h_c (λ h, h_t) (λ h, h_e)
theorem dif_pos {c : Prop} [h : decidable c] (hc : c) {A : Type u} {t : c → A} {e : ¬ c → A} : dite c t e = t hc :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := absurd hc hnc
end
theorem dif_neg {c : Prop} [h : decidable c] (hnc : ¬c) {A : Type u} {t : c → A} {e : ¬ c → A} : dite c t e = e hnc :=
match h with
| (is_true hc) := absurd hc hnc
| (is_false hnc) := rfl
end
theorem dif_ctx_congr {A : Type u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x : b → A} {u : c → A} {y : ¬b → A} {v : ¬c → A}
(h_c : b ↔ c)
(h_t : ∀ (h : c), x (iff.mpr h_c h) = u h)
(h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) :
(@dite b dec_b A x y) = (@dite c dec_c A u v) :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
theorem dif_ctx_simp_congr {A : Type u} {b c : Prop} [dec_b : decidable b]
{x : b → A} {u : c → A} {y : ¬b → A} {v : ¬c → A}
(h_c : b ↔ c)
(h_t : ∀ (h : c), x (iff.mpr h_c h) = u h)
(h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) :
(@dite b dec_b A x y) = (@dite c (decidable_of_decidable_of_iff dec_b h_c) A u v) :=
@dif_ctx_congr A b c dec_b (decidable_of_decidable_of_iff dec_b h_c) x u y v h_c h_t h_e
-- Remark: dite and ite are "definitionally equal" when we ignore the proofs.
theorem dite_ite_eq (c : Prop) [h : decidable c] {A : Type u} (t : A) (e : A) : dite c (λ h, t) (λ h, e) = ite c t e :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := rfl
end
-- The following symbols should not be considered in the pattern inference procedure used by
-- heuristic instantiation.
attribute and or not iff ite dite eq ne heq [no_pattern]
definition as_true (c : Prop) [decidable c] : Prop :=
if c then true else false
definition as_false (c : Prop) [decidable c] : Prop :=
if c then false else true
definition of_as_true {c : Prop} [h₁ : decidable c] (h₂ : as_true c) : c :=
match h₁, h₂ with
| (is_true h_c), h₂ := h_c
| (is_false h_c), h₂ := false.elim h₂
end
-- namespace used to collect congruence rules for "contextual simplification"
namespace contextual
attribute if_ctx_simp_congr [congr]
attribute if_ctx_simp_congr_prop [congr]
attribute dif_ctx_simp_congr [congr]
end contextual
|
4628ec69d55cc7aec1fc5366fa6b3bf5ffd4f541 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/tactic/lint/basic_auto.lean | dd29e9704541f9bc24e20596009e8fa424c9d7b7 | [] | 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 | 2,095 | lean | /-
Copyright (c) 2020 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.tactic.core
import Mathlib.PostPort
namespace Mathlib
/-!
# Basic linter types and attributes
This file defines the basic types and attributes used by the linting
framework. A linter essentially consists of a function
`declaration → tactic (option string)`, this function together with some
metadata is stored in the `linter` structure. We define two attributes:
* `@[linter]` applies to a declaration of type `linter` and adds it to the default linter set.
* `@[nolint linter_name]` omits the tagged declaration from being checked by
the linter with name `linter_name`.
-/
/--
We store the list of nolint names as `@id (list name) (Prop simp_nf doc_blame has_coe_t)`
See Note [user attribute parameters]
-/
/-- Defines the user attribute `nolint` for skipping `#lint` -/
/-- `should_be_linted linter decl` returns true if `decl` should be checked
using `linter`, i.e., if there is no `nolint` attribute. -/
/--
A linting test for the `#lint` command.
`test` defines a test to perform on every declaration. It should never fail. Returning `none`
signifies a passing test. Returning `some msg` reports a failing test with error `msg`.
`no_errors_found` is the message printed when all tests are negative, and `errors_found` is printed
when at least one test is positive.
If `is_fast` is false, this test will be omitted from `#lint-`.
If `auto_decls` is true, this test will also be executed on automatically generated declarations.
-/
/-- Takes a list of names that resolve to declarations of type `linter`,
and produces a list of linters. -/
/-- Defines the user attribute `linter` for adding a linter to the default set.
Linters should be defined in the `linter` namespace.
A linter `linter.my_new_linter` is referred to as `my_new_linter` (without the `linter` namespace)
when used in `#lint`.
-/
end Mathlib |
3d8f0d4fd8b6a6b743c9898d6cfcc5b44da85282 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/order/category/PartialOrder.lean | 37ff957e819a44f10b7478a3acbd4eb08e18465e | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 873 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import order.category.Preorder
/-! # Category of partially ordered types -/
open category_theory
/-- The category of partially ordered types. -/
def PartialOrder := bundled partial_order
namespace PartialOrder
instance : bundled_hom.parent_projection @partial_order.to_preorder := ⟨⟩
attribute [derive [large_category, concrete_category]] PartialOrder
instance : has_coe_to_sort PartialOrder Type* := bundled.has_coe_to_sort
/-- Construct a bundled PartialOrder from the underlying type and typeclass. -/
def of (α : Type*) [partial_order α] : PartialOrder := bundled.of α
instance : inhabited PartialOrder := ⟨of punit⟩
instance (α : PartialOrder) : partial_order α := α.str
end PartialOrder
|
cead5ac87ead124bb14fb478f890d256c8efed79 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/def_inaccessible_issue.lean | 422e21a0781336150aee6e39d10f012fc2c496cc | [
"Apache-2.0"
] | permissive | soonhokong/lean-osx | 4a954262c780e404c1369d6c06516161d07fcb40 | 3670278342d2f4faa49d95b46d86642d7875b47c | refs/heads/master | 1,611,410,334,552 | 1,474,425,686,000 | 1,474,425,686,000 | 12,043,103 | 5 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 764 | lean | set_option eqn_compiler.dsimp true
open nat
set_option pp.binder_types true
inductive bv : nat → Type
| nil : bv 0
| cons : ∀ (n) (hd : bool) (tl : bv n), bv (succ n)
open bv
variable (f : bool → bool → bool)
definition map2 : ∀ {n}, bv n → bv n → bv n
| 0 nil nil := nil
| (n+1) (cons .n b1 v1) (cons .n b2 v2) := cons n (f b1 b2) (map2 v1 v2)
check map2._main.equations.eqn_2
set_option eqn_compiler.dsimp false
/- defining function again without simplifying the type of automatically generated lemmas -/
definition map2' : ∀ {n}, bv n → bv n → bv n
| 0 nil nil := nil
| (n+1) (cons .n b1 v1) (cons .n b2 v2) := cons n (f b1 b2) (map2' v1 v2)
check map2'._main.equations.eqn_2
|
9b2d63a73729eb9e6373d700ba6d2f1851801ad2 | 3984ab8555ab1e1084e22ef652544acdfc231f27 | /src/script.lean | 7fd9e5d649e1ab0caf64f74cac470f8c23af20e6 | [] | no_license | mrakgr/CFR-in-Lean | a35c7a478795cc794cc0caff3199cf28c8ee5448 | 720a3260297bcc158e08833d38964450dcaad2eb | refs/heads/master | 1,598,515,917,940 | 1,572,612,355,000 | 1,572,612,355,000 | 217,296,108 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 73 | lean | def T : Type := ∃ (q : nat), {i // i = q}
def q : T := sorry
#check q |
8c2d45e2d2fad9a63df3c35747f43d2195c5eb3b | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/continued_fractions/basic.lean | 6d2c43d0f36d74cd51bf60e0886ce376eaf30d84 | [
"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 | 13,777 | 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.seq.seq
import algebra.field.defs
/-!
# Basic Definitions/Theorems for Continued Fractions
## Summary
We define generalised, simple, and regular continued fractions and functions to evaluate their
convergents. We follow the naming conventions from Wikipedia and [wall2018analytic], Chapter 1.
## Main definitions
1. Generalised continued fractions (gcfs)
2. Simple continued fractions (scfs)
3. (Regular) continued fractions ((r)cfs)
4. Computation of convergents using the recurrence relation in `convergents`.
5. Computation of convergents by directly evaluating the fraction described by the gcf in
`convergents'`.
## Implementation notes
1. The most commonly used kind of continued fractions in the literature are regular continued
fractions. We hence just call them `continued_fractions` in the library.
2. We use sequences from `data.seq` to encode potentially infinite sequences.
## References
- <https://en.wikipedia.org/wiki/Generalized_continued_fraction>
- [Wall, H.S., *Analytic Theory of Continued Fractions*][wall2018analytic]
## Tags
numerics, number theory, approximations, fractions
-/
-- Fix a carrier `α`.
variable (α : Type*)
/-!### Definitions-/
/-- We collect a partial numerator `aᵢ` and partial denominator `bᵢ` in a pair `⟨aᵢ,bᵢ⟩`. -/
@[derive inhabited]
protected structure generalized_continued_fraction.pair := (a : α) (b : α)
open generalized_continued_fraction
/-! Interlude: define some expected coercions and instances. -/
namespace generalized_continued_fraction.pair
variable {α}
/-- Make a `gcf.pair` printable. -/
instance [has_repr α] : has_repr (pair α) :=
⟨λ p, "(a : " ++ (repr p.a) ++ ", b : " ++ (repr p.b) ++ ")"⟩
/-- Maps a function `f` on both components of a given pair. -/
def map {β : Type*} (f : α → β) (gp : pair α) : pair β :=
⟨f gp.a, f gp.b⟩
section coe
/- Fix another type `β` which we will convert to. -/
variables {β : Type*} [has_coe α β]
/-- Coerce a pair by elementwise coercion. -/
instance has_coe_to_generalized_continued_fraction_pair : has_coe (pair α) (pair β) :=
⟨map coe⟩
@[simp, norm_cast]
lemma coe_to_generalized_continued_fraction_pair {a b : α} :
(↑(pair.mk a b) : pair β) = pair.mk (a : β) (b : β) := rfl
end coe
end generalized_continued_fraction.pair
variable (α)
/--
A *generalised continued fraction* (gcf) is a potentially infinite expression of the form
a₀
h + ---------------------------
a₁
b₀ + --------------------
a₂
b₁ + --------------
a₃
b₂ + --------
b₃ + ...
where `h` is called the *head term* or *integer part*, the `aᵢ` are called the
*partial numerators* and the `bᵢ` the *partial denominators* of the gcf.
We store the sequence of partial numerators and denominators in a sequence of
generalized_continued_fraction.pairs `s`.
For convenience, one often writes `[h; (a₀, b₀), (a₁, b₁), (a₂, b₂),...]`.
-/
structure generalized_continued_fraction :=
(h : α) (s : seq $ pair α)
variable {α}
namespace generalized_continued_fraction
/-- Constructs a generalized continued fraction without fractional part. -/
def of_integer (a : α) : generalized_continued_fraction α :=
⟨a, seq.nil⟩
instance [inhabited α] : inhabited (generalized_continued_fraction α) := ⟨of_integer default⟩
/-- Returns the sequence of partial numerators `aᵢ` of `g`. -/
def partial_numerators (g : generalized_continued_fraction α) : seq α :=
g.s.map pair.a
/-- Returns the sequence of partial denominators `bᵢ` of `g`. -/
def partial_denominators (g : generalized_continued_fraction α) : seq α :=
g.s.map pair.b
/-- A gcf terminated at position `n` if its sequence terminates at position `n`. -/
def terminated_at (g : generalized_continued_fraction α) (n : ℕ) : Prop := g.s.terminated_at n
/-- It is decidable whether a gcf terminated at a given position. -/
instance terminated_at_decidable (g : generalized_continued_fraction α) (n : ℕ) :
decidable (g.terminated_at n) :=
by { unfold terminated_at, apply_instance }
/-- A gcf terminates if its sequence terminates. -/
def terminates (g : generalized_continued_fraction α) : Prop := g.s.terminates
section coe
/-! Interlude: define some expected coercions. -/
/- Fix another type `β` which we will convert to. -/
variables {β : Type*} [has_coe α β]
/-- Coerce a gcf by elementwise coercion. -/
instance has_coe_to_generalized_continued_fraction :
has_coe (generalized_continued_fraction α) (generalized_continued_fraction β) :=
⟨λ g, ⟨(g.h : β), (g.s.map coe : seq $ pair β)⟩⟩
@[simp, norm_cast]
lemma coe_to_generalized_continued_fraction {g : generalized_continued_fraction α} :
(↑(g : generalized_continued_fraction α) : generalized_continued_fraction β) =
⟨(g.h : β), (g.s.map coe : seq $ pair β)⟩ :=
rfl
end coe
end generalized_continued_fraction
/--
A generalized continued fraction is a *simple continued fraction* if all partial numerators are
equal to one.
1
h + ---------------------------
1
b₀ + --------------------
1
b₁ + --------------
1
b₂ + --------
b₃ + ...
-/
def generalized_continued_fraction.is_simple_continued_fraction
(g : generalized_continued_fraction α) [has_one α] : Prop :=
∀ (n : ℕ) (aₙ : α), g.partial_numerators.nth n = some aₙ → aₙ = 1
variable (α)
/--
A *simple continued fraction* (scf) is a generalized continued fraction (gcf) whose partial
numerators are equal to one.
1
h + ---------------------------
1
b₀ + --------------------
1
b₁ + --------------
1
b₂ + --------
b₃ + ...
For convenience, one often writes `[h; b₀, b₁, b₂,...]`.
It is encoded as the subtype of gcfs that satisfy
`generalized_continued_fraction.is_simple_continued_fraction`.
-/
def simple_continued_fraction [has_one α] :=
{g : generalized_continued_fraction α // g.is_simple_continued_fraction}
variable {α}
/- Interlude: define some expected coercions. -/
namespace simple_continued_fraction
variable [has_one α]
/-- Constructs a simple continued fraction without fractional part. -/
def of_integer (a : α) : simple_continued_fraction α :=
⟨generalized_continued_fraction.of_integer a, λ n aₙ h, by cases h⟩
instance : inhabited (simple_continued_fraction α) := ⟨of_integer 1⟩
/-- Lift a scf to a gcf using the inclusion map. -/
instance has_coe_to_generalized_continued_fraction :
has_coe (simple_continued_fraction α) (generalized_continued_fraction α) :=
by {unfold simple_continued_fraction, apply_instance}
lemma coe_to_generalized_continued_fraction {s : simple_continued_fraction α} :
(↑s : generalized_continued_fraction α) = s.val := rfl
end simple_continued_fraction
/--
A simple continued fraction is a *(regular) continued fraction* ((r)cf) if all partial denominators
`bᵢ` are positive, i.e. `0 < bᵢ`.
-/
def simple_continued_fraction.is_continued_fraction [has_one α] [has_zero α] [has_lt α]
(s : simple_continued_fraction α) : Prop :=
∀ (n : ℕ) (bₙ : α),
(↑s : generalized_continued_fraction α).partial_denominators.nth n = some bₙ → 0 < bₙ
variable (α)
/--
A *(regular) continued fraction* ((r)cf) is a simple continued fraction (scf) whose partial
denominators are all positive. It is the subtype of scfs that satisfy
`simple_continued_fraction.is_continued_fraction`.
-/
def continued_fraction [has_one α] [has_zero α] [has_lt α] :=
{s : simple_continued_fraction α // s.is_continued_fraction}
variable {α}
/-! Interlude: define some expected coercions. -/
namespace continued_fraction
variables [has_one α] [has_zero α] [has_lt α]
/-- Constructs a continued fraction without fractional part. -/
def of_integer (a : α) : continued_fraction α :=
⟨simple_continued_fraction.of_integer a, λ n bₙ h, by cases h⟩
instance : inhabited (continued_fraction α) := ⟨of_integer 0⟩
/-- Lift a cf to a scf using the inclusion map. -/
instance has_coe_to_simple_continued_fraction :
has_coe (continued_fraction α) (simple_continued_fraction α) :=
by {unfold continued_fraction, apply_instance}
lemma coe_to_simple_continued_fraction {c : continued_fraction α} :
(↑c : simple_continued_fraction α) = c.val := rfl
/-- Lift a cf to a scf using the inclusion map. -/
instance has_coe_to_generalized_continued_fraction :
has_coe (continued_fraction α) (generalized_continued_fraction α) :=
⟨λ c, ↑(↑c : simple_continued_fraction α)⟩
lemma coe_to_generalized_continued_fraction {c : continued_fraction α} :
(↑c : generalized_continued_fraction α) = c.val := rfl
end continued_fraction
namespace generalized_continued_fraction
/-!
### Computation of Convergents
We now define how to compute the convergents of a gcf. There are two standard ways to do this:
directly evaluating the (infinite) fraction described by the gcf or using a recurrence relation.
For (r)cfs, these computations are equivalent as shown in
`algebra.continued_fractions.convergents_equiv`.
-/
-- Fix a division ring for the computations.
variables {K : Type*} [division_ring K]
/-!
We start with the definition of the recurrence relation. Given a gcf `g`, for all `n ≥ 1`, we define
- `A₋₁ = 1, A₀ = h, Aₙ = bₙ₋₁ * Aₙ₋₁ + aₙ₋₁ * Aₙ₋₂`, and
- `B₋₁ = 0, B₀ = 1, Bₙ = bₙ₋₁ * Bₙ₋₁ + aₙ₋₁ * Bₙ₋₂`.
`Aₙ, `Bₙ` are called the *nth continuants*, Aₙ the *nth numerator*, and `Bₙ` the
*nth denominator* of `g`. The *nth convergent* of `g` is given by `Aₙ / Bₙ`.
-/
/--
Returns the next numerator `Aₙ = bₙ₋₁ * Aₙ₋₁ + aₙ₋₁ * Aₙ₋₂`, where `predA` is `Aₙ₋₁`,
`ppredA` is `Aₙ₋₂`, `a` is `aₙ₋₁`, and `b` is `bₙ₋₁`.
-/
def next_numerator (a b ppredA predA : K) : K := b * predA + a * ppredA
/--
Returns the next denominator `Bₙ = bₙ₋₁ * Bₙ₋₁ + aₙ₋₁ * Bₙ₋₂``, where `predB` is `Bₙ₋₁` and
`ppredB` is `Bₙ₋₂`, `a` is `aₙ₋₁`, and `b` is `bₙ₋₁`.
-/
def next_denominator (aₙ bₙ ppredB predB : K) : K := bₙ * predB + aₙ * ppredB
/--
Returns the next continuants `⟨Aₙ, Bₙ⟩` using `next_numerator` and `next_denominator`, where `pred`
is `⟨Aₙ₋₁, Bₙ₋₁⟩`, `ppred` is `⟨Aₙ₋₂, Bₙ₋₂⟩`, `a` is `aₙ₋₁`, and `b` is `bₙ₋₁`.
-/
def next_continuants (a b : K) (ppred pred : pair K) : pair K :=
⟨next_numerator a b ppred.a pred.a, next_denominator a b ppred.b pred.b⟩
/-- Returns the continuants `⟨Aₙ₋₁, Bₙ₋₁⟩` of `g`. -/
def continuants_aux (g : generalized_continued_fraction K) : stream (pair K)
| 0 := ⟨1, 0⟩
| 1 := ⟨g.h, 1⟩
| (n + 2) :=
match g.s.nth n with
| none := continuants_aux (n + 1)
| some gp := next_continuants gp.a gp.b (continuants_aux n) (continuants_aux $ n + 1)
end
/-- Returns the continuants `⟨Aₙ, Bₙ⟩` of `g`. -/
def continuants (g : generalized_continued_fraction K) : stream (pair K) :=
g.continuants_aux.tail
/-- Returns the numerators `Aₙ` of `g`. -/
def numerators (g : generalized_continued_fraction K) : stream K :=
g.continuants.map pair.a
/-- Returns the denominators `Bₙ` of `g`. -/
def denominators (g : generalized_continued_fraction K) : stream K :=
g.continuants.map pair.b
/-- Returns the convergents `Aₙ / Bₙ` of `g`, where `Aₙ, Bₙ` are the nth continuants of `g`. -/
def convergents (g : generalized_continued_fraction K) : stream K :=
λ (n : ℕ), (g.numerators n) / (g.denominators n)
/--
Returns the approximation of the fraction described by the given sequence up to a given position n.
For example, `convergents'_aux [(1, 2), (3, 4), (5, 6)] 2 = 1 / (2 + 3 / 4)` and
`convergents'_aux [(1, 2), (3, 4), (5, 6)] 0 = 0`.
-/
def convergents'_aux : seq (pair K) → ℕ → K
| s 0 := 0
| s (n + 1) := match s.head with
| none := 0
| some gp := gp.a / (gp.b + convergents'_aux s.tail n)
end
/--
Returns the convergents of `g` by evaluating the fraction described by `g` up to a given
position `n`. For example, `convergents' [9; (1, 2), (3, 4), (5, 6)] 2 = 9 + 1 / (2 + 3 / 4)` and
`convergents' [9; (1, 2), (3, 4), (5, 6)] 0 = 9`
-/
def convergents' (g : generalized_continued_fraction K) (n : ℕ) : K := g.h + convergents'_aux g.s n
end generalized_continued_fraction
-- Now, some basic, general theorems
namespace generalized_continued_fraction
/-- Two gcfs `g` and `g'` are equal if and only if their components are equal. -/
protected lemma ext_iff {g g' : generalized_continued_fraction α} :
g = g' ↔ g.h = g'.h ∧ g.s = g'.s :=
by { cases g, cases g', simp }
@[ext]
protected lemma ext {g g' : generalized_continued_fraction α} (hyp : g.h = g'.h ∧ g.s = g'.s) :
g = g' :=
generalized_continued_fraction.ext_iff.elim_right hyp
end generalized_continued_fraction
|
3ad5a3b109a91f0e70f3d0eb128545fe8e4788d2 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/group_theory/abelianization.lean | e7be75497aac901a5a17e411bdf1c3b84fb9099b | [
"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 | 7,242 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Michael Howes
-/
import group_theory.commutator
import group_theory.quotient_group
/-!
# The abelianization of a group
This file defines the commutator and the abelianization of a group. It furthermore prepares for the
result that the abelianization is left adjoint to the forgetful functor from abelian groups to
groups, which can be found in `algebra/category/Group/adjunctions`.
## Main definitions
* `commutator`: defines the commutator of a group `G` as a subgroup of `G`.
* `abelianization`: defines the abelianization of a group `G` as the quotient of a group by its
commutator subgroup.
* `abelianization.map`: lifts a group homomorphism to a homomorphism between the abelianizations
* `mul_equiv.abelianization_congr`: Equivalent groups have equivalent abelianizations
-/
universes u v w
-- Let G be a group.
variables (G : Type u) [group G]
/-- The commutator subgroup of a group G is the normal subgroup
generated by the commutators [p,q]=`p*q*p⁻¹*q⁻¹`. -/
@[derive subgroup.normal]
def commutator : subgroup G :=
⁅(⊤ : subgroup G), ⊤⁆
lemma commutator_def : commutator G = ⁅(⊤ : subgroup G), ⊤⁆ := rfl
lemma commutator_eq_closure : commutator G = subgroup.closure {g | ∃ g₁ g₂ : G, ⁅g₁, g₂⁆ = g} :=
by simp_rw [commutator, subgroup.commutator_def, subgroup.mem_top, exists_true_left]
lemma commutator_eq_normal_closure :
commutator G = subgroup.normal_closure {g | ∃ g₁ g₂ : G, ⁅g₁, g₂⁆ = g} :=
by simp_rw [commutator, subgroup.commutator_def', subgroup.mem_top, exists_true_left]
instance commutator_characteristic : (commutator G).characteristic :=
subgroup.commutator_characteristic ⊤ ⊤
lemma commutator_centralizer_commutator_le_center :
⁅(commutator G).centralizer, (commutator G).centralizer⁆ ≤ subgroup.center G :=
begin
rw [←subgroup.centralizer_top, ←subgroup.commutator_eq_bot_iff_le_centralizer],
suffices : ⁅⁅⊤, (commutator G).centralizer⁆, (commutator G).centralizer⁆ = ⊥,
{ refine subgroup.commutator_commutator_eq_bot_of_rotate _ this,
rwa subgroup.commutator_comm (commutator G).centralizer },
rw [subgroup.commutator_comm, subgroup.commutator_eq_bot_iff_le_centralizer],
exact set.centralizer_subset (subgroup.commutator_mono le_top le_top),
end
/-- The abelianization of G is the quotient of G by its commutator subgroup. -/
def abelianization : Type u :=
G ⧸ (commutator G)
namespace abelianization
local attribute [instance] quotient_group.left_rel
instance : comm_group (abelianization G) :=
{ mul_comm := λ x y, quotient.induction_on₂' x y $ λ a b, quotient.sound' $
quotient_group.left_rel_apply.mpr $
subgroup.subset_closure ⟨b⁻¹, subgroup.mem_top b⁻¹, a⁻¹, subgroup.mem_top a⁻¹, by group⟩,
.. quotient_group.quotient.group _ }
instance : inhabited (abelianization G) := ⟨1⟩
instance [fintype G] [decidable_pred (∈ commutator G)] :
fintype (abelianization G) :=
quotient_group.fintype (commutator G)
variable {G}
/-- `of` is the canonical projection from G to its abelianization. -/
def of : G →* abelianization G :=
{ to_fun := quotient_group.mk,
map_one' := rfl,
map_mul' := λ x y, rfl }
@[simp] lemma mk_eq_of (a : G) : quot.mk _ a = of a := rfl
section lift
-- So far we have built Gᵃᵇ and proved it's an abelian group.
-- Furthremore we defined the canonical projection `of : G → Gᵃᵇ`
-- Let `A` be an abelian group and let `f` be a group homomorphism from `G` to `A`.
variables {A : Type v} [comm_group A] (f : G →* A)
lemma commutator_subset_ker : commutator G ≤ f.ker :=
begin
rw [commutator_eq_closure, subgroup.closure_le],
rintros x ⟨p, q, rfl⟩,
simp [monoid_hom.mem_ker, mul_right_comm (f p) (f q), commutator_element_def],
end
/-- If `f : G → A` is a group homomorphism to an abelian group, then `lift f` is the unique map from
the abelianization of a `G` to `A` that factors through `f`. -/
def lift : (G →* A) ≃ (abelianization G →* A) :=
{ to_fun := λ f, quotient_group.lift _ f (λ x h, f.mem_ker.2 $ commutator_subset_ker _ h),
inv_fun := λ F, F.comp of,
left_inv := λ f, monoid_hom.ext $ λ x, rfl,
right_inv := λ F, monoid_hom.ext $ λ x, quotient_group.induction_on x $ λ z, rfl }
@[simp] lemma lift.of (x : G) : lift f (of x) = f x :=
rfl
theorem lift.unique
(φ : abelianization G →* A)
-- hφ : φ agrees with f on the image of G in Gᵃᵇ
(hφ : ∀ (x : G), φ (of x) = f x)
{x : abelianization G} :
φ x = lift f x :=
quotient_group.induction_on x hφ
@[simp] lemma lift_of : lift of = monoid_hom.id (abelianization G) :=
lift.apply_symm_apply $ monoid_hom.id _
end lift
variables {A : Type v} [monoid A]
/-- See note [partially-applied ext lemmas]. -/
@[ext]
theorem hom_ext (φ ψ : abelianization G →* A)
(h : φ.comp of = ψ.comp of) : φ = ψ :=
monoid_hom.ext $ λ x, quotient_group.induction_on x $ monoid_hom.congr_fun h
section map
variables {H : Type v} [group H] (f : G →* H)
/-- The map operation of the `abelianization` functor -/
def map : abelianization G →* abelianization H := lift (of.comp f)
@[simp]
lemma map_of (x : G) : map f (of x) = of (f x) := rfl
@[simp]
lemma map_id : map (monoid_hom.id G) = monoid_hom.id (abelianization G) := hom_ext _ _ rfl
@[simp]
lemma map_comp {I : Type w} [group I] (g : H →* I) :
(map g).comp (map f) = map (g.comp f) := hom_ext _ _ rfl
@[simp]
lemma map_map_apply {I : Type w} [group I] {g : H →* I} {x : abelianization G}:
map g (map f x) = map (g.comp f) x := monoid_hom.congr_fun (map_comp _ _) x
end map
end abelianization
section abelianization_congr
variables {G} {H : Type v} [group H] (e : G ≃* H)
/-- Equivalent groups have equivalent abelianizations -/
def mul_equiv.abelianization_congr : abelianization G ≃* abelianization H :=
{ to_fun := abelianization.map e.to_monoid_hom,
inv_fun := abelianization.map e.symm.to_monoid_hom,
left_inv := by { rintros ⟨a⟩, simp },
right_inv := by { rintros ⟨a⟩, simp },
map_mul' := monoid_hom.map_mul _ }
@[simp]
lemma abelianization_congr_of (x : G) :
(e.abelianization_congr) (abelianization.of x) = abelianization.of (e x) := rfl
@[simp]
lemma abelianization_congr_refl :
(mul_equiv.refl G).abelianization_congr = mul_equiv.refl (abelianization G) :=
mul_equiv.to_monoid_hom_injective abelianization.lift_of
@[simp]
lemma abelianization_congr_symm :
e.abelianization_congr.symm = e.symm.abelianization_congr := rfl
@[simp]
lemma abelianization_congr_trans {I : Type v} [group I] (e₂ : H ≃* I) :
e.abelianization_congr.trans e₂.abelianization_congr = (e.trans e₂).abelianization_congr :=
mul_equiv.to_monoid_hom_injective (abelianization.hom_ext _ _ rfl)
end abelianization_congr
/-- An Abelian group is equivalent to its own abelianization. -/
@[simps] def abelianization.equiv_of_comm {H : Type*} [comm_group H] :
H ≃* abelianization H :=
{ to_fun := abelianization.of,
inv_fun := abelianization.lift (monoid_hom.id H),
left_inv := λ a, rfl,
right_inv := by { rintros ⟨a⟩, refl, },
.. abelianization.of }
|
d6c8ba31d3df3eed4fa8819f6d5bf615200ea7b5 | 36c7a18fd72e5b57229bd8ba36493daf536a19ce | /library/algebra/simplifier.lean | e766982525b92f6eaed18589b467385bed8e653f | [
"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 | 3,809 | lean | /-
Copyright (c) 2015 Daniel Selsam. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Daniel Selsam
-/
import algebra.ring algebra.numeral
namespace simplifier
namespace empty
end empty
namespace iff
attribute eq_self_iff_true [simp]
end iff
-- TODO(dhs): make these [simp] rules in the global namespace
namespace neg_helper
open algebra
variables {A : Type} [s : ring A] (a b : A)
include s
lemma neg_mul1 : (- a) * b = - (a * b) := eq.symm !algebra.neg_mul_eq_neg_mul
lemma neg_mul2 : a * (- b) = - (a * b) := eq.symm !algebra.neg_mul_eq_mul_neg
lemma sub_def : a - b = a + (- b) := rfl
end neg_helper
namespace neg
attribute neg_helper.neg_mul1 [simp]
attribute neg_helper.neg_mul2 [simp]
attribute neg_helper.sub_def [simp]
attribute algebra.neg_neg [simp]
end neg
namespace unit
attribute algebra.zero_add [simp]
attribute algebra.add_zero [simp]
attribute algebra.zero_mul [simp]
attribute algebra.mul_zero [simp]
attribute algebra.one_mul [simp]
attribute algebra.mul_one [simp]
end unit
namespace ac
export simplifier.iff simplifier.neg simplifier.unit
attribute algebra.add.assoc [simp]
attribute algebra.add.comm [simp]
attribute algebra.add.left_comm [simp]
attribute algebra.mul.left_comm [simp]
attribute algebra.mul.comm [simp]
attribute algebra.mul.assoc [simp]
end ac
namespace distrib
attribute algebra.left_distrib [simp]
attribute algebra.right_distrib [simp]
end distrib
namespace som
export simplifier.ac simplifier.distrib
end som
namespace numeral
-- TODO(dhs): remove `add1` from the original lemmas and delete this
namespace numeral_helper
open algebra
theorem bit1_add_bit1 {A : Type} [s : add_comm_semigroup A]
[s' : has_one A] (a b : A) : bit1 a + bit1 b = bit0 ((a + b) + 1)
:= norm_num.bit1_add_bit1 a b
theorem bit1_add_one {A : Type} [s : add_comm_semigroup A] [s' : has_one A] (a : A)
: bit1 a + one = bit0 (a + 1) := norm_num.add1_bit1 a
theorem one_add_bit1 {A : Type} [s : add_comm_semigroup A] [s' : has_one A] (a : A)
: one + bit1 a = bit0 (a + 1) := by rewrite [!add.comm, bit1_add_one]
lemma one_add_bit0 [simp] {A : Type} [s : add_comm_semigroup A] [s' : has_one A] (a : A)
: 1 + bit0 a = bit1 a := norm_num.one_add_bit0 a
lemma bit0_add_one [simp] {A : Type} [s : add_comm_semigroup A] [s' : has_one A] (a : A)
: bit0 a + 1 = bit1 a := norm_num.bit0_add_one a
lemma mul_bit0_helper0 [simp] {A : Type} [s : comm_ring A] (a b : A)
: bit0 a * bit0 b = bit0 (bit0 a * b) := norm_num.mul_bit0_helper (bit0 a) b (bit0 a * b) rfl
lemma mul_bit0_helper1 [simp] {A : Type} [s : comm_ring A] (a b : A)
: bit1 a * bit0 b = bit0 (bit1 a * b) := norm_num.mul_bit0_helper (bit1 a) b (bit1 a * b) rfl
lemma mul_bit1_helper0 [simp] {A : Type} [s : comm_ring A] (a b : A)
: bit0 a * bit1 b = bit0 (bit0 a * b) + bit0 a := norm_num.mul_bit1_helper (bit0 a) b (bit0 a * b) (bit0 (bit0 a * b) + bit0 a) rfl rfl
lemma mul_bit1_helper1 [simp] {A : Type} [s : comm_ring A] (a b : A)
: bit1 a * bit1 b = bit0 (bit1 a * b) + bit1 a := norm_num.mul_bit1_helper (bit1 a) b (bit1 a * b) (bit0 (bit1 a * b) + bit1 a) rfl rfl
end numeral_helper
export simplifier.ac
attribute norm_num.bit0_add_bit0 [simp]
attribute numeral_helper.bit1_add_one [simp]
attribute norm_num.bit1_add_bit0 [simp]
attribute numeral_helper.bit1_add_bit1 [simp]
attribute norm_num.bit0_add_bit1 [simp]
attribute numeral_helper.one_add_bit1 [simp]
attribute norm_num.one_add_one [simp]
attribute numeral_helper.one_add_bit0 [simp]
attribute numeral_helper.bit0_add_one [simp]
attribute numeral_helper.mul_bit0_helper0 [simp]
attribute numeral_helper.mul_bit0_helper1 [simp]
attribute numeral_helper.mul_bit1_helper0 [simp]
attribute numeral_helper.mul_bit1_helper1 [simp]
end numeral
end simplifier
|
5a1332ad8d69c9e15503689b2429a41e3e10c4af | 0845ae2ca02071debcfd4ac24be871236c01784f | /library/init/data/rbtree/basic.lean | 52d3a940ed08e837d7557fb25a1f4f986b7e4ff8 | [
"Apache-2.0"
] | permissive | GaloisInc/lean4 | 74c267eb0e900bfaa23df8de86039483ecbd60b7 | 228ddd5fdcd98dd4e9c009f425284e86917938aa | refs/heads/master | 1,643,131,356,301 | 1,562,715,572,000 | 1,562,715,572,000 | 192,390,898 | 0 | 0 | null | 1,560,792,750,000 | 1,560,792,749,000 | null | UTF-8 | Lean | false | false | 2,907 | 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
-/
prelude
import init.data.rbmap.basic
universes u v w
def RBTree (α : Type u) (lt : α → α → Bool) : Type u :=
RBMap α Unit lt
@[inline] def mkRBTree (α : Type u) (lt : α → α → Bool) : RBTree α lt :=
mkRBMap α Unit lt
instance (α : Type u) (lt : α → α → Bool) : HasEmptyc (RBTree α lt) :=
⟨mkRBTree α lt⟩
namespace RBTree
variables {α : Type u} {β : Type v} {lt : α → α → Bool}
@[inline] def depth (f : Nat → Nat → Nat) (t : RBTree α lt) : Nat :=
RBMap.depth f t
@[inline] def fold (f : β → α → β) (b : β) (t : RBTree α lt) : β :=
RBMap.fold (fun r a _ => f r a) b t
@[inline] def revFold (f : β → α → β) (b : β) (t : RBTree α lt) : β :=
RBMap.revFold (fun r a _ => f r a) b t
@[inline] def mfold {m : Type v → Type w} [Monad m] (f : β → α → m β) (b : β) (t : RBTree α lt) : m β :=
RBMap.mfold (fun r a _ => f r a) b t
@[inline] def mfor {m : Type v → Type w} [Monad m] (f : α → m β) (t : RBTree α lt) : m PUnit :=
t.mfold (fun _ a => f a *> pure ⟨⟩) ⟨⟩
@[inline] def isEmpty (t : RBTree α lt) : Bool :=
RBMap.isEmpty t
@[specialize] def toList (t : RBTree α lt) : List α :=
t.revFold (fun as a => a::as) []
@[inline] protected def min (t : RBTree α lt) : Option α :=
match RBMap.min t with
| some ⟨a, _⟩ => some a
| none => none
@[inline] protected def max (t : RBTree α lt) : Option α :=
match RBMap.max t with
| some ⟨a, _⟩ => some a
| none => none
instance [HasRepr α] : HasRepr (RBTree α lt) :=
⟨fun t => "rbtreeOf " ++ repr t.toList⟩
@[inline] def insert (t : RBTree α lt) (a : α) : RBTree α lt :=
RBMap.insert t a ()
instance : HasInsert α (RBTree α lt) := ⟨fun a s => s.insert a⟩
@[inline] def erase (t : RBTree α lt) (a : α) : RBTree α lt :=
RBMap.erase t a
@[specialize] def ofList : List α → RBTree α lt
| [] := mkRBTree _ _
| (x::xs) := (ofList xs).insert x
@[inline] def find (t : RBTree α lt) (a : α) : Option α :=
match RBMap.findCore t a with
| some ⟨a, _⟩ => some a
| none => none
@[inline] def contains (t : RBTree α lt) (a : α) : Bool :=
(t.find a).isSome
def fromList (l : List α) (lt : α → α → Bool) : RBTree α lt :=
l.foldl insert (mkRBTree α lt)
@[inline] def all (t : RBTree α lt) (p : α → Bool) : Bool :=
RBMap.all t (fun a _ => p a)
@[inline] def any (t : RBTree α lt) (p : α → Bool) : Bool :=
RBMap.any t (fun a _ => p a)
def subset (t₁ t₂ : RBTree α lt) : Bool :=
t₁.all $ fun a => (t₂.find a).toBool
def seteq (t₁ t₂ : RBTree α lt) : Bool :=
subset t₁ t₂ && subset t₂ t₁
end RBTree
def rbtreeOf {α : Type u} (l : List α) (lt : α → α → Bool) : RBTree α lt :=
RBTree.fromList l lt
|
de4a3ce90c0db91e6e28f9cd061afc16d9db0178 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/dynamics/flow.lean | 7cb4766756e7b3462d91c81f3d0c156e3af3f217 | [
"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 | 6,140 | lean | /-
Copyright (c) 2020 Jean Lo. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jean Lo
-/
import topology.algebra.group
import logic.function.iterate
/-!
# Flows and invariant sets
This file defines a flow on a topological space `α` by a topological
monoid `τ` as a continuous monoid-act of `τ` on `α`. Anticipating the
cases where `τ` is one of `ℕ`, `ℤ`, `ℝ⁺`, or `ℝ`, we use additive
notation for the monoids, though the definition does not require
commutativity.
A subset `s` of `α` is invariant under a family of maps `ϕₜ : α → α`
if `ϕₜ s ⊆ s` for all `t`. In many cases `ϕ` will be a flow on
`α`. For the cases where `ϕ` is a flow by an ordered (additive,
commutative) monoid, we additionally define forward invariance, where
`t` ranges over those elements which are nonnegative.
Additionally, we define such constructions as the restriction of a
flow onto an invariant subset, and the time-reveral of a flow by a
group.
-/
open set function filter
/-!
### Invariant sets
-/
section invariant
variables {τ : Type*} {α : Type*}
/-- A set `s ⊆ α` is invariant under `ϕ : τ → α → α` if
`ϕ t s ⊆ s` for all `t` in `τ`. -/
def is_invariant (ϕ : τ → α → α) (s : set α): Prop := ∀ t, maps_to (ϕ t) s s
variables (ϕ : τ → α → α) (s : set α)
lemma is_invariant_iff_image : is_invariant ϕ s ↔ ∀ t, ϕ t '' s ⊆ s :=
by simp_rw [is_invariant, maps_to']
/-- A set `s ⊆ α` is forward-invariant under `ϕ : τ → α → α` if
`ϕ t s ⊆ s` for all `t ≥ 0`. -/
def is_fw_invariant [preorder τ] [has_zero τ] (ϕ : τ → α → α) (s : set α): Prop :=
∀ ⦃t⦄, 0 ≤ t → maps_to (ϕ t) s s
lemma is_invariant.is_fw_invariant [preorder τ] [has_zero τ] {ϕ : τ → α → α} {s : set α}
(h : is_invariant ϕ s) : is_fw_invariant ϕ s :=
λ t ht, h t
/-- If `τ` is a `canonically_ordered_add_monoid` (e.g., `ℕ` or `ℝ≥0`), then the notions
`is_fw_invariant` and `is_invariant` are equivalent. -/
lemma is_fw_invariant.is_invariant [canonically_ordered_add_monoid τ] {ϕ : τ → α → α} {s : set α}
(h : is_fw_invariant ϕ s) : is_invariant ϕ s :=
λ t, h (zero_le t)
/-- If `τ` is a `canonically_ordered_add_monoid` (e.g., `ℕ` or `ℝ≥0`), then the notions
`is_fw_invariant` and `is_invariant` are equivalent. -/
lemma is_fw_invariant_iff_is_invariant [canonically_ordered_add_monoid τ]
{ϕ : τ → α → α} {s : set α} :
is_fw_invariant ϕ s ↔ is_invariant ϕ s :=
⟨is_fw_invariant.is_invariant, is_invariant.is_fw_invariant⟩
end invariant
/-!
### Flows
-/
/-- A flow on a topological space `α` by an a additive topological
monoid `τ` is a continuous monoid action of `τ` on `α`.-/
structure flow
(τ : Type*) [topological_space τ] [add_monoid τ] [has_continuous_add τ]
(α : Type*) [topological_space α] :=
(to_fun : τ → α → α)
(cont' : continuous (uncurry to_fun))
(map_add' : ∀ t₁ t₂ x, to_fun (t₁ + t₂) x = to_fun t₁ (to_fun t₂ x))
(map_zero' : ∀ x, to_fun 0 x = x)
namespace flow
variables
{τ : Type*} [add_monoid τ] [topological_space τ] [has_continuous_add τ]
{α : Type*} [topological_space α]
(ϕ : flow τ α)
instance : inhabited (flow τ α) :=
⟨{ to_fun := λ _ x, x,
cont' := continuous_snd,
map_add' := λ _ _ _, rfl,
map_zero' := λ _, rfl }⟩
instance : has_coe_to_fun (flow τ α) (λ _, τ → α → α) := ⟨flow.to_fun⟩
@[ext]
lemma ext : ∀ {ϕ₁ ϕ₂ : flow τ α}, (∀ t x, ϕ₁ t x = ϕ₂ t x) → ϕ₁ = ϕ₂
| ⟨f₁, _, _, _⟩ ⟨f₂, _, _, _⟩ h := by { congr, funext, exact h _ _ }
@[continuity]
protected lemma continuous {β : Type*} [topological_space β]
{t : β → τ} (ht : continuous t) {f : β → α} (hf : continuous f) :
continuous (λ x, ϕ (t x) (f x)) :=
ϕ.cont'.comp (ht.prod_mk hf)
alias flow.continuous ← _root_.continuous.flow
lemma map_add (t₁ t₂ : τ) (x : α) : ϕ (t₁ + t₂) x = ϕ t₁ (ϕ t₂ x) :=
ϕ.map_add' _ _ _
@[simp] lemma map_zero : ϕ 0 = id := funext ϕ.map_zero'
lemma map_zero_apply (x : α) : ϕ 0 x = x := ϕ.map_zero' x
/-- Iterations of a continuous function from a topological space `α`
to itself defines a semiflow by `ℕ` on `α`. -/
def from_iter {g : α → α} (h : continuous g) : flow ℕ α :=
{ to_fun := λ n x, g^[n] x,
cont' := continuous_uncurry_of_discrete_topology_left (continuous.iterate h),
map_add' := iterate_add_apply _,
map_zero' := λ x, rfl }
/-- Restriction of a flow onto an invariant set. -/
def restrict {s : set α} (h : is_invariant ϕ s) : flow τ ↥s :=
{ to_fun := λ t, (h t).restrict _ _ _,
cont' := (ϕ.continuous continuous_fst continuous_subtype_coe.snd').subtype_mk _,
map_add' := λ _ _ _, subtype.ext (map_add _ _ _ _),
map_zero' := λ _, subtype.ext (map_zero_apply _ _)}
end flow
namespace flow
variables
{τ : Type*} [add_comm_group τ] [topological_space τ] [topological_add_group τ]
{α : Type*} [topological_space α]
(ϕ : flow τ α)
lemma is_invariant_iff_image_eq (s : set α) :
is_invariant ϕ s ↔ ∀ t, ϕ t '' s = s :=
(is_invariant_iff_image _ _).trans (iff.intro
(λ h t, subset.antisymm (h t) (λ _ hx, ⟨_, h (-t) ⟨_, hx, rfl⟩, by simp [← map_add]⟩))
(λ h t, by rw h t))
/-- The time-reversal of a flow `ϕ` by a (commutative, additive) group
is defined `ϕ.reverse t x = ϕ (-t) x`. -/
def reverse : flow τ α :=
{ to_fun := λ t, ϕ (-t),
cont' := ϕ.continuous continuous_fst.neg continuous_snd,
map_add' := λ _ _ _, by rw [neg_add, map_add],
map_zero' := λ _, by rw [neg_zero, map_zero_apply] }
/-- The map `ϕ t` as a homeomorphism. -/
def to_homeomorph (t : τ) : α ≃ₜ α :=
{ to_fun := ϕ t,
inv_fun := ϕ (-t),
left_inv := λ x, by rw [← map_add, neg_add_self, map_zero_apply],
right_inv := λ x, by rw [← map_add, add_neg_self, map_zero_apply] }
lemma image_eq_preimage (t : τ) (s : set α) : ϕ t '' s = ϕ (-t) ⁻¹' s :=
(ϕ.to_homeomorph t).to_equiv.image_eq_preimage s
end flow
|
fec48e765d5a9d86c521f04f8e5133623d790cea | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/algebra/category/CommRing/colimits.lean | 4b5560526a8dafc5bcb31245762e0191496b2af0 | [
"Apache-2.0"
] | permissive | vaibhavkarve/mathlib | a574aaf68c0a431a47fa82ce0637f0f769826bfe | 17f8340912468f49bdc30acdb9a9fa02eeb0473a | refs/heads/master | 1,621,263,802,637 | 1,585,399,588,000 | 1,585,399,588,000 | 250,833,447 | 0 | 0 | Apache-2.0 | 1,585,410,341,000 | 1,585,410,341,000 | null | UTF-8 | Lean | false | false | 13,323 | 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
import category_theory.limits.limits
/-!
# 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 `Mon` by constructing the
free monoid on the disjoint union of all the monoids in the diagram,
then taking the quotient by the monoid laws within each monoid,
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
{ rw cocone.naturality_concrete, },
-- zero
{ erw ring_hom.map_zero ((s.ι).app r), refl },
-- one
{ erw ring_hom.map_one ((s.ι).app r), refl },
-- neg
{ rw ring_hom.map_neg ((s.ι).app r_j) },
-- add
{ rw ring_hom.map_add ((s.ι).app r_j) },
-- mul
{ rw ring_hom.map_mul ((s.ι).app r_j) },
-- 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 only [desc_morphism, quot_zero],
erw ring_hom.map_zero m,
refl, },
{ simp only [desc_morphism, quot_one],
erw ring_hom.map_one m,
refl, },
{ simp only [desc_morphism, quot_neg],
erw ring_hom.map_neg m,
rw [x_ih],
refl, },
{ simp only [desc_morphism, quot_add],
erw ring_hom.map_add m,
rw [x_ih_a, x_ih_a_1],
refl, },
{ simp only [desc_morphism, quot_mul],
erw ring_hom.map_mul m,
rw [x_ih_a, x_ih_a_1],
refl, },
refl
end }.
instance has_colimits_CommRing : has_colimits.{v} CommRing.{v} :=
{ has_colimits_of_shape := λ J 𝒥,
{ has_colimit := λ F, by exactI
{ cocone := colimit_cocone F,
is_colimit := colimit_is_colimit F } } }
end CommRing.colimits
|
5c3e0f2e3943f4cee07ada1783810974927d0441 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /src/Init/Data/Array/InsertionSort.lean | 0f8a76610b4d0968811773ce00b11b88a98ea9ae | [
"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 | 929 | lean | /-
Copyright (c) 2018 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Data.Array.Basic
@[inline] def Array.insertionSort (a : Array α) (lt : α → α → Bool) : Array α :=
traverse a 0 a.size
where
@[specialize] traverse (a : Array α) (i : Nat) (fuel : Nat) : Array α :=
match fuel with
| 0 => a
| fuel+1 =>
if h : i < a.size then
traverse (swapLoop a i h) (i+1) fuel
else
a
@[specialize] swapLoop (a : Array α) (j : Nat) (h : j < a.size) : Array α :=
match he:j with
| 0 => a
| j'+1 =>
have h' : j' < a.size := by subst j; exact Nat.lt_trans (Nat.lt_succ_self _) h
if lt (a.get ⟨j, h⟩) (a.get ⟨j', h'⟩) then
swapLoop (a.swap ⟨j, h⟩ ⟨j', h'⟩) j' (by rw [size_swap]; assumption done)
else
a
|
b22280aaf0588e431be83d04728e3f365d831aa0 | b815abf92ce063fe0d1fabf5b42da483552aa3e8 | /library/init/meta/expr.lean | 8231d3128cc89bd997a41c8296ae425b4773127a | [
"Apache-2.0"
] | permissive | yodalee/lean | a368d842df12c63e9f79414ed7bbee805b9001ef | 317989bf9ef6ae1dec7488c2363dbfcdc16e0756 | refs/heads/master | 1,610,551,176,860 | 1,481,430,138,000 | 1,481,646,441,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,032 | 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.level
inductive binder_info
| default | implicit | strict_implicit | inst_implicit | other
meta constant macro_def : Type
/- Reflect a C++ expr object. The VM replaces it with the C++ implementation. -/
meta inductive expr
| var : unsigned → expr
| sort : level → expr
| const : name → list level → expr
| mvar : name → expr → expr
| local_const : name → name → binder_info → expr → expr
| app : expr → expr → expr
| lam : name → binder_info → expr → expr → expr
| pi : name → binder_info → expr → expr → expr
| elet : name → expr → expr → expr → expr
| macro : macro_def → ∀ n : unsigned, (fin (unsigned.to_nat n) → expr) → expr
meta instance : inhabited expr :=
⟨expr.sort level.zero⟩
meta constant expr.mk_macro (d : macro_def) : list expr → expr
meta constant expr.macro_def_name (d : macro_def) : name
meta def expr.mk_var (n : nat) : expr :=
expr.var (unsigned.of_nat n)
-- Compares expressions, including binder names.
meta constant expr.has_decidable_eq : decidable_eq expr
attribute [instance] expr.has_decidable_eq
-- Compares expressions while ignoring binder names.
meta constant expr.alpha_eqv : expr → expr → bool
notation a ` =ₐ `:50 b:50 := expr.alpha_eqv a b = bool.tt
meta constant expr.to_string : expr → string
meta instance : has_to_string expr :=
has_to_string.mk expr.to_string
/- Coercion for letting users write (f a) instead of (expr.app f a) -/
meta instance : has_coe_to_fun expr :=
{ F := λ e, expr → expr, coe := λ e, expr.app e }
meta constant expr.hash : expr → nat
-- Compares expressions, ignoring binder names, and sorting by hash.
meta constant expr.lt : expr → expr → bool
-- Compares expressions, ignoring binder names.
meta constant expr.lex_lt : expr → expr → bool
-- Compares expressions, ignoring binder names, and sorting by hash.
meta def expr.cmp (a b : expr) : ordering :=
if expr.lt a b then ordering.lt
else if a =ₐ b then ordering.eq
else ordering.gt
meta constant expr.fold {α : Type} : expr → α → (expr → nat → α → α) → α
meta constant expr.replace : expr → (expr → nat → option expr) → expr
meta constant expr.abstract_local : expr → name → expr
meta constant expr.abstract_locals : expr → list name → expr
meta def expr.abstract : expr → expr → expr
| e (expr.local_const n m bi t) := e^.abstract_local n
| e _ := e
meta constant expr.instantiate_var : expr → expr → expr
meta constant expr.instantiate_vars : expr → list expr → expr
meta constant expr.has_var : expr → bool
meta constant expr.has_var_idx : expr → nat → bool
meta constant expr.has_local : expr → bool
meta constant expr.has_meta_var : expr → bool
meta constant expr.lift_vars : expr → nat → nat → expr
meta constant expr.lower_vars : expr → nat → nat → expr
/- (copy_pos_info src tgt) copy position information from src to tgt. -/
meta constant expr.copy_pos_info : expr → expr → expr
meta constant expr.is_internal_cnstr : expr → option unsigned
meta constant expr.get_nat_value : expr → option nat
namespace expr
open decidable
-- Compares expressions, ignoring binder names, and sorting by hash.
meta instance : has_ordering expr :=
⟨ expr.cmp ⟩
meta def app_of_list : expr → list expr → expr
| f [] := f
| f (p::ps) := app_of_list (f p) ps
meta def is_app : expr → bool
| (app f a) := tt
| e := ff
meta def app_fn : expr → expr
| (app f a) := f
| a := a
meta def app_arg : expr → expr
| (app f a) := a
| a := a
meta def get_app_fn : expr → expr
| (app f a) := get_app_fn f
| a := a
meta def get_app_num_args : expr → nat
| (app f a) := get_app_num_args f + 1
| e := 0
meta def get_app_args_aux : list expr → expr → list expr
| r (app f a) := get_app_args_aux (a::r) f
| r e := r
meta def get_app_args : expr → list expr :=
get_app_args_aux []
meta def const_name : expr → name
| (const n ls) := n
| e := name.anonymous
meta def is_constant : expr → bool
| (const n ls) := tt
| e := ff
meta def is_local_constant : expr → bool
| (local_const n m bi t) := tt
| e := ff
meta def local_uniq_name : expr → name
| (local_const n m bi t) := n
| e := name.anonymous
meta def local_pp_name : expr → name
| (local_const x n bi t) := n
| e := name.anonymous
meta def is_constant_of : expr → name → bool
| (const n₁ ls) n₂ := to_bool (n₁ = n₂)
| e n := ff
meta def is_app_of (e : expr) (n : name) : bool :=
is_constant_of (get_app_fn e) n
meta def is_napp_of (e : expr) (c : name) (n : nat) : bool :=
to_bool (is_app_of e c ∧ get_app_num_args e = n)
meta def is_false (e : expr) : bool :=
is_constant_of e `false
meta def is_not : expr → option expr
| (app f a) := if is_constant_of f `not then some a else none
| (pi n bi a b) := if is_false b then some a else none
| e := none
meta def is_eq (e : expr) : option (expr × expr) :=
if is_napp_of e `eq 3
then some (app_arg (app_fn e), app_arg e)
else none
meta def is_ne (e : expr) : option (expr × expr) :=
if is_napp_of e `ne 3
then some (app_arg (app_fn e), app_arg e)
else none
meta def is_bin_arith_app (e : expr) (op : name) : option (expr × expr) :=
if is_napp_of e op 4
then some (app_arg (app_fn e), app_arg e)
else none
meta def is_lt (e : expr) : option (expr × expr) :=
is_bin_arith_app e `lt
meta def is_gt (e : expr) : option (expr × expr) :=
is_bin_arith_app e `gt
meta def is_le (e : expr) : option (expr × expr) :=
is_bin_arith_app e `le
meta def is_ge (e : expr) : option (expr × expr) :=
is_bin_arith_app e `ge
meta def is_heq (e : expr) : option (expr × expr × expr × expr) :=
if is_napp_of e `heq 4
then some (app_arg (app_fn (app_fn (app_fn e))),
app_arg (app_fn (app_fn e)),
app_arg (app_fn e),
app_arg e)
else none
meta def is_pi : expr → bool
| (pi n bi d b) := tt
| e := ff
meta def is_arrow : expr → bool
| (pi n bi d b) := bnot (has_var b)
| e := ff
meta def is_let : expr → bool
| (elet n t v b) := tt
| e := ff
meta def binding_name : expr → name
| (pi n m d b) := n
| (lam n m d b) := n
| e := name.anonymous
meta def binding_info : expr → binder_info
| (pi n bi d b) := bi
| (lam n bi d b) := bi
| e := binder_info.default
meta def binding_domain : expr → expr
| (pi n bi d b) := d
| (lam n bi d b) := d
| e := e
meta def binding_body : expr → expr
| (pi n bi d b) := b
| (lam n bi d b) := b
| e := e
meta def prop : expr := expr.sort level.zero
end expr
|
f446bbbc323021f32754838e715f3c0d46fdf7d5 | 26ac254ecb57ffcb886ff709cf018390161a9225 | /src/group_theory/submonoid/membership.lean | f06a5c8c6c36f37f0e3d00654c8992fce85a6cd1 | [
"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 | 7,428 | 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, Yury Kudryashov
-/
import group_theory.submonoid.operations
import algebra.big_operators
import algebra.free_monoid
/-!
# Submonoids: membership criteria
In this file we prove various facts about membership in a submonoid:
* `list_prod_mem`, `multiset_prod_mem`, `prod_mem`: if each element of a collection belongs
to a multiplicative submonoid, then so does their product;
* `list_sum_mem`, `multiset_sum_mem`, `sum_mem`: if each element of a collection belongs
to an additive submonoid, then so does their sum;
* `pow_mem`, `nsmul_mem`: if `x ∈ S` where `S` is a multiplicative (resp., additive) submonoid and
`n` is a natural number, then `x^n` (resp., `n •ℕ x`) belongs to `S`;
* `mem_supr_of_directed`, `coe_supr_of_directed`, `mem_Sup_of_directed_on`,
`coe_Sup_of_directed_on`: the supremum of a directed collection of submonoid is their union.
* `sup_eq_range`, `mem_sup`: supremum of two submonoids `S`, `T` of a commutative monoid is the set
of products;
* `closure_singleton_eq`, `mem_closure_singleton`: the multiplicative (resp., additive) closure
of `{x}` consists of powers (resp., natural multiples) of `x`.
## Tags
submonoid, submonoids
-/
open_locale big_operators
variables {M : Type*} [monoid M] {s : set M}
variables {A : Type*} [add_monoid A] {t : set A}
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 rwa [list.prod_cons],
have a ∈ S ∧ (∀ x ∈ l, x ∈ S), from list.forall_mem_cons.1 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 prod_mem {M : Type*} [comm_monoid M] (S : submonoid M)
{ι : Type*} {t : finset ι} {f : ι → M} (h : ∀c ∈ t, f c ∈ S) :
∏ c in t, f c ∈ S :=
S.multiset_prod_mem (t.1.map f) $ λ x hx, let ⟨i, hi, hix⟩ := multiset.mem_map.1 hx in hix ▸ h i hi
lemma pow_mem {x : M} (hx : x ∈ S) : ∀ n:ℕ, x^n ∈ S
| 0 := S.one_mem
| (n+1) := S.mul_mem hx (pow_mem n)
open set
@[to_additive]
lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S)
{x : M} :
x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i :=
begin
refine ⟨_, λ ⟨i, hi⟩, (le_def.1 $ le_supr S i) hi⟩,
suffices : x ∈ closure (⋃ i, (S i : set M)) → ∃ i, x ∈ S i,
by simpa only [closure_Union, closure_eq (S _)] using this,
refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _),
{ exact hι.elim (λ i, ⟨i, (S i).one_mem⟩) },
{ rintros x y ⟨i, hi⟩ ⟨j, hj⟩,
rcases hS i j with ⟨k, hki, hkj⟩,
exact ⟨k, (S k).mul_mem (hki hi) (hkj hj)⟩ }
end
@[to_additive]
lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S) :
((⨆ i, S i : submonoid M) : set M) = ⋃ i, ↑(S i) :=
set.ext $ λ x, by simp [mem_supr_of_directed hS]
@[to_additive]
lemma mem_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty)
(hS : directed_on (≤) S) {x : M} :
x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s :=
begin
haveI : nonempty S := Sne.to_subtype,
simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk]
end
@[to_additive]
lemma coe_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≤) S) :
(↑(Sup S) : set M) = ⋃ s ∈ S, ↑s :=
set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS]
variables {N : Type*} [monoid N] {P : Type*} [monoid P]
end submonoid
namespace free_monoid
variables {α : Type*}
open submonoid
@[to_additive]
theorem closure_range_of : closure (set.range $ @of α) = ⊤ :=
eq_top_iff.2 $ λ x hx, free_monoid.rec_on x (one_mem _) $ λ x xs hxs,
mul_mem _ (subset_closure $ set.mem_range_self _) hxs
end free_monoid
namespace submonoid
variables {N : Type*} [monoid N]
open monoid_hom
lemma closure_singleton_eq (x : M) : closure ({x} : set M) = (powers_hom M x).mrange :=
closure_eq_of_le (set.singleton_subset_iff.2 ⟨multiplicative.of_add 1, trivial, pow_one x⟩) $
λ x ⟨n, _, hn⟩, hn ▸ pow_mem _ (subset_closure $ set.mem_singleton _) _
/-- The submonoid generated by an element of a monoid equals the set of natural number powers of
the element. -/
lemma mem_closure_singleton {x y : M} : y ∈ closure ({x} : set M) ↔ ∃ n:ℕ, x^n=y :=
by rw [closure_singleton_eq, mem_mrange]; refl
@[to_additive]
lemma closure_eq_mrange (s : set M) : closure s = (free_monoid.lift (coe : s → M)).mrange :=
by rw [mrange, ← free_monoid.closure_range_of, map_mclosure, ← set.range_comp,
free_monoid.lift_comp_of, subtype.range_coe]
@[to_additive]
lemma exists_list_of_mem_closure {s : set M} {x : M} (hx : x ∈ closure s) :
∃ (l : list M) (hl : ∀ y ∈ l, y ∈ s), l.prod = x :=
begin
rw [closure_eq_mrange, mem_mrange] at hx,
rcases hx with ⟨l, hx⟩,
exact ⟨list.map coe l, λ y hy, let ⟨z, hz, hy⟩ := list.mem_map.1 hy in hy ▸ z.2, hx⟩
end
end submonoid
namespace submonoid
variables {N : Type*} [comm_monoid N]
open monoid_hom
@[to_additive]
lemma sup_eq_range (s t : submonoid N) : s ⊔ t = (s.subtype.coprod t.subtype).mrange :=
by rw [mrange, ← mrange_inl_sup_mrange_inr, map_sup, map_mrange, coprod_comp_inl,
map_mrange, coprod_comp_inr, range_subtype, range_subtype]
@[to_additive]
lemma mem_sup {s t : submonoid N} {x : N} :
x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x :=
by simp only [sup_eq_range, mem_mrange, coprod_apply, prod.exists, submonoid.exists,
coe_subtype, subtype.coe_mk]
end submonoid
namespace add_submonoid
open set
lemma nsmul_mem (S : add_submonoid A) {x : A} (hx : x ∈ S) :
∀ n : ℕ, n •ℕ x ∈ S
| 0 := S.zero_mem
| (n+1) := S.add_mem hx (nsmul_mem n)
lemma closure_singleton_eq (x : A) : closure ({x} : set A) = (multiples_hom A x).mrange :=
closure_eq_of_le (set.singleton_subset_iff.2 ⟨1, trivial, one_nsmul x⟩) $
λ x ⟨n, _, hn⟩, hn ▸ nsmul_mem _ (subset_closure $ set.mem_singleton _) _
/-- The `add_submonoid` generated by an element of an `add_monoid` equals the set of
natural number multiples of the element. -/
lemma mem_closure_singleton {x y : A} :
y ∈ closure ({x} : set A) ↔ ∃ n:ℕ, n •ℕ x = y :=
by rw [closure_singleton_eq, add_monoid_hom.mem_mrange]; refl
end add_submonoid
|
021c3255b93bbfd812047b770bbd6b8fdbcd102c | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/mem_nil.lean | 2848d9581d3c0b2b92572fe0151724b59fa84326 | [
"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 | 85 | lean | universe variables u
example {α : Type u} (a : α) : a ∉ ([] : list α) :=
sorry
|
446f97ea3535aa8fa2694ffe104ab48991a8add6 | 5d166a16ae129621cb54ca9dde86c275d7d2b483 | /tests/lean/run/check_constants.lean | 194ac667224003d2f15d4ead54945167bb49726e | [
"Apache-2.0"
] | permissive | jcarlson23/lean | b00098763291397e0ac76b37a2dd96bc013bd247 | 8de88701247f54d325edd46c0eed57aeacb64baf | refs/heads/master | 1,611,571,813,719 | 1,497,020,963,000 | 1,497,021,515,000 | 93,882,536 | 1 | 0 | null | 1,497,029,896,000 | 1,497,029,896,000 | null | UTF-8 | Lean | false | false | 14,805 | lean | -- DO NOT EDIT, automatically generated file, generator scripts/gen_constants_cpp.py
import smt system.io
open tactic
meta def script_check_id (n : name) : tactic unit :=
do env ← get_env, (env^.get n >> return ()) <|> (guard $ env^.is_namespace n) <|> (attribute.get_instances n >> return ()) <|> fail ("identifier '" ++ to_string n ++ "' is not a constant, namespace nor attribute")
run_cmd script_check_id `abs
run_cmd script_check_id `absurd
run_cmd script_check_id `acc.cases_on
run_cmd script_check_id `add_comm_group
run_cmd script_check_id `add_comm_semigroup
run_cmd script_check_id `add_group
run_cmd script_check_id `add_monoid
run_cmd script_check_id `and
run_cmd script_check_id `and.elim_left
run_cmd script_check_id `and.elim_right
run_cmd script_check_id `and.intro
run_cmd script_check_id `and.rec
run_cmd script_check_id `and.cases_on
run_cmd script_check_id `auto_param
run_cmd script_check_id `bit0
run_cmd script_check_id `bit1
run_cmd script_check_id `bool
run_cmd script_check_id `bool.ff
run_cmd script_check_id `bool.tt
run_cmd script_check_id `combinator.K
run_cmd script_check_id `caching_user_attribute
run_cmd script_check_id `cast
run_cmd script_check_id `cast_heq
run_cmd script_check_id `char
run_cmd script_check_id `char.of_nat
run_cmd script_check_id `char.of_nat_ne_of_ne
run_cmd script_check_id `classical.prop_decidable
run_cmd script_check_id `classical.type_decidable_eq
run_cmd script_check_id `coe
run_cmd script_check_id `coe_fn
run_cmd script_check_id `coe_sort
run_cmd script_check_id `coe_to_lift
run_cmd script_check_id `congr
run_cmd script_check_id `congr_arg
run_cmd script_check_id `congr_fun
run_cmd script_check_id `decidable
run_cmd script_check_id `decidable.to_bool
run_cmd script_check_id `distrib
run_cmd script_check_id `dite
run_cmd script_check_id `id
run_cmd script_check_id `empty
run_cmd script_check_id `Exists
run_cmd script_check_id `eq
run_cmd script_check_id `eq.cases_on
run_cmd script_check_id `eq.drec
run_cmd script_check_id `eq.mp
run_cmd script_check_id `eq.mpr
run_cmd script_check_id `eq.rec
run_cmd script_check_id `eq.refl
run_cmd script_check_id `eq.subst
run_cmd script_check_id `eq.symm
run_cmd script_check_id `eq.trans
run_cmd script_check_id `eq_of_heq
run_cmd script_check_id `eq_rec_heq
run_cmd script_check_id `eq_true_intro
run_cmd script_check_id `eq_false_intro
run_cmd script_check_id `eq_self_iff_true
run_cmd script_check_id `expr
run_cmd script_check_id `expr.subst
run_cmd script_check_id `format
run_cmd script_check_id `false
run_cmd script_check_id `false_of_true_iff_false
run_cmd script_check_id `false_of_true_eq_false
run_cmd script_check_id `true_eq_false_of_false
run_cmd script_check_id `false.rec
run_cmd script_check_id `field
run_cmd script_check_id `fin.mk
run_cmd script_check_id `fin.ne_of_vne
run_cmd script_check_id `forall_congr
run_cmd script_check_id `forall_congr_eq
run_cmd script_check_id `forall_not_of_not_exists
run_cmd script_check_id `funext
run_cmd script_check_id `ge
run_cmd script_check_id `gt
run_cmd script_check_id `has_add
run_cmd script_check_id `has_add.add
run_cmd script_check_id `andthen
run_cmd script_check_id `has_bind.and_then
run_cmd script_check_id `has_bind.bind
run_cmd script_check_id `has_bind.seq
run_cmd script_check_id `has_div
run_cmd script_check_id `has_div.div
run_cmd script_check_id `has_emptyc.emptyc
run_cmd script_check_id `has_mod.mod
run_cmd script_check_id `has_mul
run_cmd script_check_id `has_mul.mul
run_cmd script_check_id `has_insert.insert
run_cmd script_check_id `has_inv
run_cmd script_check_id `has_inv.inv
run_cmd script_check_id `has_le
run_cmd script_check_id `has_le.le
run_cmd script_check_id `has_lt
run_cmd script_check_id `has_lt.lt
run_cmd script_check_id `has_neg
run_cmd script_check_id `has_neg.neg
run_cmd script_check_id `has_one
run_cmd script_check_id `has_one.one
run_cmd script_check_id `has_orelse.orelse
run_cmd script_check_id `has_sep.sep
run_cmd script_check_id `has_sizeof
run_cmd script_check_id `has_sizeof.mk
run_cmd script_check_id `has_sub
run_cmd script_check_id `has_sub.sub
run_cmd script_check_id `has_to_format
run_cmd script_check_id `has_to_string
run_cmd script_check_id `has_well_founded
run_cmd script_check_id `has_well_founded.r
run_cmd script_check_id `has_well_founded.wf
run_cmd script_check_id `has_zero
run_cmd script_check_id `has_zero.zero
run_cmd script_check_id `has_coe_t
run_cmd script_check_id `heq
run_cmd script_check_id `heq.refl
run_cmd script_check_id `heq.symm
run_cmd script_check_id `heq.trans
run_cmd script_check_id `heq_of_eq
run_cmd script_check_id `id_locked
run_cmd script_check_id `if_neg
run_cmd script_check_id `if_pos
run_cmd script_check_id `iff
run_cmd script_check_id `iff_false_intro
run_cmd script_check_id `iff.intro
run_cmd script_check_id `iff.mp
run_cmd script_check_id `iff.mpr
run_cmd script_check_id `iff.refl
run_cmd script_check_id `iff.symm
run_cmd script_check_id `iff.trans
run_cmd script_check_id `iff_true_intro
run_cmd script_check_id `imp_congr
run_cmd script_check_id `imp_congr_eq
run_cmd script_check_id `imp_congr_ctx
run_cmd script_check_id `imp_congr_ctx_eq
run_cmd script_check_id `implies
run_cmd script_check_id `implies_of_if_neg
run_cmd script_check_id `implies_of_if_pos
run_cmd script_check_id `int
run_cmd script_check_id `int.has_add
run_cmd script_check_id `int.has_mul
run_cmd script_check_id `int.has_sub
run_cmd script_check_id `int.has_div
run_cmd script_check_id `int.has_le
run_cmd script_check_id `int.has_lt
run_cmd script_check_id `int.has_neg
run_cmd script_check_id `int.has_mod
run_cmd script_check_id `int.bit0_nonneg
run_cmd script_check_id `int.bit1_nonneg
run_cmd script_check_id `int.one_nonneg
run_cmd script_check_id `int.zero_nonneg
run_cmd script_check_id `int.bit0_pos
run_cmd script_check_id `int.bit1_pos
run_cmd script_check_id `int.one_pos
run_cmd script_check_id `int.nat_abs_zero
run_cmd script_check_id `int.nat_abs_one
run_cmd script_check_id `int.nat_abs_bit0_step
run_cmd script_check_id `int.nat_abs_bit1_nonneg_step
run_cmd script_check_id `int.ne_of_nat_ne_nonneg_case
run_cmd script_check_id `int.ne_neg_of_ne
run_cmd script_check_id `int.neg_ne_of_pos
run_cmd script_check_id `int.ne_neg_of_pos
run_cmd script_check_id `int.neg_ne_zero_of_ne
run_cmd script_check_id `int.zero_ne_neg_of_ne
run_cmd script_check_id `int.decidable_linear_ordered_comm_group
run_cmd script_check_id `interactive.param_desc
run_cmd script_check_id `interactive.parse
run_cmd script_check_id `io
run_cmd script_check_id `io.interface
run_cmd script_check_id `is_associative
run_cmd script_check_id `is_associative.assoc
run_cmd script_check_id `is_commutative
run_cmd script_check_id `is_commutative.comm
run_cmd script_check_id `ite
run_cmd script_check_id `lean.parser
run_cmd script_check_id `lean.parser.pexpr
run_cmd script_check_id `lean.parser.tk
run_cmd script_check_id `left_distrib
run_cmd script_check_id `left_comm
run_cmd script_check_id `le_refl
run_cmd script_check_id `linear_ordered_ring
run_cmd script_check_id `linear_ordered_semiring
run_cmd script_check_id `list
run_cmd script_check_id `list.nil
run_cmd script_check_id `list.cons
run_cmd script_check_id `match_failed
run_cmd script_check_id `monad
run_cmd script_check_id `monad_fail
run_cmd script_check_id `monoid
run_cmd script_check_id `mul_one
run_cmd script_check_id `mul_zero
run_cmd script_check_id `mul_zero_class
run_cmd script_check_id `name.anonymous
run_cmd script_check_id `name.mk_numeral
run_cmd script_check_id `name.mk_string
run_cmd script_check_id `nat
run_cmd script_check_id `nat.succ
run_cmd script_check_id `nat.zero
run_cmd script_check_id `nat.has_zero
run_cmd script_check_id `nat.has_one
run_cmd script_check_id `nat.has_add
run_cmd script_check_id `nat.add
run_cmd script_check_id `nat.cases_on
run_cmd script_check_id `nat.bit0_ne
run_cmd script_check_id `nat.bit0_ne_bit1
run_cmd script_check_id `nat.bit0_ne_zero
run_cmd script_check_id `nat.bit0_ne_one
run_cmd script_check_id `nat.bit1_ne
run_cmd script_check_id `nat.bit1_ne_bit0
run_cmd script_check_id `nat.bit1_ne_zero
run_cmd script_check_id `nat.bit1_ne_one
run_cmd script_check_id `nat.zero_ne_one
run_cmd script_check_id `nat.zero_ne_bit0
run_cmd script_check_id `nat.zero_ne_bit1
run_cmd script_check_id `nat.one_ne_zero
run_cmd script_check_id `nat.one_ne_bit0
run_cmd script_check_id `nat.one_ne_bit1
run_cmd script_check_id `nat.bit0_lt
run_cmd script_check_id `nat.bit1_lt
run_cmd script_check_id `nat.bit0_lt_bit1
run_cmd script_check_id `nat.bit1_lt_bit0
run_cmd script_check_id `nat.zero_lt_one
run_cmd script_check_id `nat.zero_lt_bit1
run_cmd script_check_id `nat.zero_lt_bit0
run_cmd script_check_id `nat.one_lt_bit0
run_cmd script_check_id `nat.one_lt_bit1
run_cmd script_check_id `nat.le_of_lt
run_cmd script_check_id `nat.le_refl
run_cmd script_check_id `ne
run_cmd script_check_id `neq_of_not_iff
run_cmd script_check_id `norm_num.add1
run_cmd script_check_id `norm_num.add1_bit0
run_cmd script_check_id `norm_num.add1_bit1_helper
run_cmd script_check_id `norm_num.add1_one
run_cmd script_check_id `norm_num.add1_zero
run_cmd script_check_id `norm_num.add_div_helper
run_cmd script_check_id `norm_num.bin_add_zero
run_cmd script_check_id `norm_num.bin_zero_add
run_cmd script_check_id `norm_num.bit0_add_bit0_helper
run_cmd script_check_id `norm_num.bit0_add_bit1_helper
run_cmd script_check_id `norm_num.bit0_add_one
run_cmd script_check_id `norm_num.bit1_add_bit0_helper
run_cmd script_check_id `norm_num.bit1_add_bit1_helper
run_cmd script_check_id `norm_num.bit1_add_one_helper
run_cmd script_check_id `norm_num.div_add_helper
run_cmd script_check_id `norm_num.div_eq_div_helper
run_cmd script_check_id `norm_num.div_helper
run_cmd script_check_id `norm_num.div_mul_helper
run_cmd script_check_id `norm_num.mk_cong
run_cmd script_check_id `norm_num.mul_bit0_helper
run_cmd script_check_id `norm_num.mul_bit1_helper
run_cmd script_check_id `norm_num.mul_div_helper
run_cmd script_check_id `norm_num.neg_add_neg_helper
run_cmd script_check_id `norm_num.neg_add_pos_helper1
run_cmd script_check_id `norm_num.neg_add_pos_helper2
run_cmd script_check_id `norm_num.neg_mul_neg_helper
run_cmd script_check_id `norm_num.neg_mul_pos_helper
run_cmd script_check_id `norm_num.neg_neg_helper
run_cmd script_check_id `norm_num.neg_zero_helper
run_cmd script_check_id `norm_num.nonneg_bit0_helper
run_cmd script_check_id `norm_num.nonneg_bit1_helper
run_cmd script_check_id `norm_num.nonzero_of_div_helper
run_cmd script_check_id `norm_num.nonzero_of_neg_helper
run_cmd script_check_id `norm_num.nonzero_of_pos_helper
run_cmd script_check_id `norm_num.one_add_bit0
run_cmd script_check_id `norm_num.one_add_bit1_helper
run_cmd script_check_id `norm_num.one_add_one
run_cmd script_check_id `norm_num.pos_add_neg_helper
run_cmd script_check_id `norm_num.pos_bit0_helper
run_cmd script_check_id `norm_num.pos_bit1_helper
run_cmd script_check_id `norm_num.pos_mul_neg_helper
run_cmd script_check_id `norm_num.sub_nat_zero_helper
run_cmd script_check_id `norm_num.sub_nat_pos_helper
run_cmd script_check_id `norm_num.subst_into_div
run_cmd script_check_id `norm_num.subst_into_prod
run_cmd script_check_id `norm_num.subst_into_subtr
run_cmd script_check_id `norm_num.subst_into_sum
run_cmd script_check_id `not
run_cmd script_check_id `not_of_iff_false
run_cmd script_check_id `not_of_eq_false
run_cmd script_check_id `of_eq_true
run_cmd script_check_id `of_iff_true
run_cmd script_check_id `opt_param
run_cmd script_check_id `or
run_cmd script_check_id `inout_param
run_cmd script_check_id `punit
run_cmd script_check_id `punit.star
run_cmd script_check_id `prod.mk
run_cmd script_check_id `pprod
run_cmd script_check_id `pprod.mk
run_cmd script_check_id `pprod.fst
run_cmd script_check_id `pprod.snd
run_cmd script_check_id `propext
run_cmd script_check_id `to_pexpr
run_cmd script_check_id `quot.mk
run_cmd script_check_id `quot.lift
run_cmd script_check_id `real
run_cmd script_check_id `real.of_int
run_cmd script_check_id `real.to_int
run_cmd script_check_id `real.is_int
run_cmd script_check_id `real.has_neg
run_cmd script_check_id `real.has_div
run_cmd script_check_id `real.has_add
run_cmd script_check_id `real.has_mul
run_cmd script_check_id `real.has_sub
run_cmd script_check_id `real.has_lt
run_cmd script_check_id `real.has_le
run_cmd script_check_id `reflected
run_cmd script_check_id `reflected.subst
run_cmd script_check_id `rfl
run_cmd script_check_id `right_distrib
run_cmd script_check_id `ring
run_cmd script_check_id `scope_trace
run_cmd script_check_id `set_of
run_cmd script_check_id `semiring
run_cmd script_check_id `psigma
run_cmd script_check_id `psigma.cases_on
run_cmd script_check_id `psigma.mk
run_cmd script_check_id `psigma.fst
run_cmd script_check_id `psigma.snd
run_cmd script_check_id `singleton
run_cmd script_check_id `sizeof
run_cmd script_check_id `smt.array
run_cmd script_check_id `smt.select
run_cmd script_check_id `smt.store
run_cmd script_check_id `smt.prove
run_cmd script_check_id `string
run_cmd script_check_id `string.empty
run_cmd script_check_id `string.str
run_cmd script_check_id `string.empty_ne_str
run_cmd script_check_id `string.str_ne_empty
run_cmd script_check_id `string.str_ne_str_left
run_cmd script_check_id `string.str_ne_str_right
run_cmd script_check_id `subsingleton
run_cmd script_check_id `subsingleton.elim
run_cmd script_check_id `subsingleton.helim
run_cmd script_check_id `subtype
run_cmd script_check_id `subtype.mk
run_cmd script_check_id `subtype.val
run_cmd script_check_id `subtype.rec
run_cmd script_check_id `psum
run_cmd script_check_id `psum.cases_on
run_cmd script_check_id `psum.inl
run_cmd script_check_id `psum.inr
run_cmd script_check_id `tactic
run_cmd script_check_id `tactic.try
run_cmd script_check_id `tactic.triv
run_cmd script_check_id `thunk
run_cmd script_check_id `to_fmt
run_cmd script_check_id `to_string
run_cmd script_check_id `trans_rel_left
run_cmd script_check_id `trans_rel_right
run_cmd script_check_id `true
run_cmd script_check_id `true.intro
run_cmd script_check_id `unification_hint
run_cmd script_check_id `unification_hint.mk
run_cmd script_check_id `unit
run_cmd script_check_id `unit.cases_on
run_cmd script_check_id `unit.star
run_cmd script_check_id `unsafe_monad_from_pure_bind
run_cmd script_check_id `user_attribute
run_cmd script_check_id `vm_monitor
run_cmd script_check_id `weak_order
run_cmd script_check_id `well_founded.fix
run_cmd script_check_id `well_founded.fix_eq
run_cmd script_check_id `well_founded_tactics
run_cmd script_check_id `well_founded_tactics.default
run_cmd script_check_id `well_founded_tactics.rel_tac
run_cmd script_check_id `well_founded_tactics.dec_tac
run_cmd script_check_id `xor
run_cmd script_check_id `zero_le_one
run_cmd script_check_id `zero_lt_one
run_cmd script_check_id `zero_mul
|
b15fbe410d68fcc460fbbb223590b6ced3384dc5 | 4fc5f02f6ed9423b87987589bcc202f985d7e9ff | /src/game/world7/level1.lean | ceee09f657f01818e32506a59289ca1b90f2e18f | [
"Apache-2.0"
] | permissive | grthomson/natural_number_game | 2937533df0b83e73e6a873c0779ee21e30f09778 | 0a23a327ca724b95406c510ee27c55f5b97e612f | refs/heads/master | 1,599,994,591,355 | 1,588,869,073,000 | 1,588,869,073,000 | 222,750,177 | 0 | 0 | Apache-2.0 | 1,574,183,960,000 | 1,574,183,960,000 | null | UTF-8 | Lean | false | false | 1,609 | lean | -- World name : Advanced Proposition world
/-
# Advanced proposition world.
In this world we will learn five key tactics needed to solve all the
levels of the Natural Number Game, namely `split`, `cases`, `left`, `right`, and `exfalso`.
These, and `use` (which we'll get to in Inequality World) are all the
tactics you will need to beat all the levels of the game.
## Level 1: the `split` tactic.
The logical symbol `∧` means "and". If $P$ and $Q$ are propositions, then
$P\land Q$ is the proposition "$P$ and $Q$". If your *goal* is `P ∧ Q` then
you can make progress with the `split` tactic, which turns one goal `⊢ P ∧ Q`
into two goals, namely `⊢ P` and `⊢ Q`. In the level below, after a `split`,
you will be able to finish off the goals with the `exact` tactic.
-/
/- Lemma : no-side-bar
If $P$ and $Q$ are true, then $P\land Q$ is true.
-/
example (P Q : Prop) (p : P) (q : Q) : P ∧ Q :=
begin
split,
exact p,
exact q,
end
/- Tactic : split
## Summary:
If the goal is `P ∧ Q` or `P ↔ Q` then `split` will break it into two goals.
## Details
If `P Q : Prop` and the goal is `⊢ P ∧ Q`, then `split` will change it into
two goals, namely `⊢ P` and `⊢ Q`.
If `P Q : Prop` and the goal is `⊢ P ↔ Q`, then `split` will change it into
two goals, namely `⊢ P → Q` and `⊢ Q → P`.
## Example:
If your local context (the top right window) looks like this
```
a b : mynat,
⊢ a = b ↔ a + 3 = b + 3
```
then after
`split,`
it will look like this:
```
2 goals
a b : mynat
⊢ a = b → a + 3 = b + 3
a b : mynat
⊢ a + 3 = b + 3 → a = b
-/ |
f61e7ce939db9b21be4861b97405813b8d3226d4 | 64874bd1010548c7f5a6e3e8902efa63baaff785 | /hott/init/hedberg.hlean | ce9c2799b076a107af950f714ba3ecf9d85fc527 | [
"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 | 1,499 | hlean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
Hedberg's Theorem: every type with decidable equality is a hset
-/
prelude
import init.trunc
open eq eq.ops nat truncation sigma
-- TODO(Leo): move const coll and path_coll to a different file?
private definition const {A B : Type} (f : A → B) := ∀ x y, f x = f y
private definition coll (A : Type) := Σ f : A → A, const f
private definition path_coll (A : Type) := ∀ x y : A, coll (x = y)
context
parameter {A : Type}
hypothesis (h : decidable_eq A)
variables {x y : A}
private definition pc [reducible] : path_coll A :=
λ a b, decidable.rec_on (h a b)
(λ p : a = b, ⟨(λ q, p), λ q r, rfl⟩)
(λ np : ¬ a = b, ⟨(λ q, q), λ q r, absurd q np⟩)
private definition f [reducible] : x = y → x = y :=
sigma.rec_on (pc x y) (λ f c, f)
private definition f_const (p q : x = y) : f p = f q :=
sigma.rec_on (pc x y) (λ f c, c p q)
private definition aux (p : x = y) : p = (f (refl x))⁻¹ ⬝ (f p) :=
have aux : refl x = (f (refl x))⁻¹ ⬝ (f (refl x)), from
eq.rec_on (f (refl x)) rfl,
eq.rec_on p aux
definition is_hset_of_decidable_eq : is_hset A :=
is_hset.mk A (λ x y p q, calc
p = (f (refl x))⁻¹ ⬝ (f p) : aux
... = (f (refl x))⁻¹ ⬝ (f q) : f_const
... = q : aux)
end
attribute is_hset_of_decidable_eq [instance]
|
0bf7211b26592b965fe1d998b778be1e9742ae31 | 03bd658c402412f41d3026d1040ee8ca8c0fc579 | /src/list/simple_value.lean | 484ebb7691e2158f1e95a6bd62aa62f4498f05b5 | [] | no_license | ImperialCollegeLondon/dots_and_boxes | c205f6dbad8af9625f56715e4d1bed96b0ac1022 | f7bd0b1603674a657170c5395adb717c4f670220 | refs/heads/master | 1,663,752,058,476 | 1,591,438,614,000 | 1,591,438,614,000 | 139,707,103 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,878 | lean | import tactic.linarith list.min.basic
-- tf = 2 if we are talking about chains and 4 otherwise
-- all games are assumed to consist only of loops or only of chains
/-- Let G be all chains or loops. If m is the length of a component of G, and
vL = value of game G with that component removed, list.aux_fun this is the
value of G given that we're playing in that component -/
def list.aux_fun (m tf vL : ℤ) := m - tf + abs(tf - vL)
/--if two components M1, M2 differ by at most d in length
and v(G1;M1) = v(G2;M2), then the value given we are playing
in any of them differs by at most d-/
theorem list.aux_fun_L1 {m1 m2 tf vL d : ℤ} (hm : abs (m1 - m2) ≤ d) :
abs(list.aux_fun m1 tf vL - list.aux_fun m2 tf vL) ≤ d :=
begin
unfold list.aux_fun, finish, -- Lean can prove this by itself
end
/--if we have two games with a component of length m of the same
type and the values of each game with such a component removed
differ by at most d then the value given we are playing in such
a component in any of them differs by at most d-/
theorem list.aux_fun_L2 {m tf vL1 vL2 d : ℤ} (hm : abs (vL1 - vL2) ≤ d) :
abs(list.aux_fun m tf vL1 - list.aux_fun m tf vL2) ≤ d :=
begin
unfold list.aux_fun,
rw sub_add_eq_sub_sub_swap,
/- the goal is : abs (m - tf + abs (tf - vL1) - abs (tf - vL2) - (m - tf)) ≤ d-/
/- As we are dealing with integers, this is equivalent to
abs (abs (tf - vL1) - abs (tf - vL2)) ≤ d -/
rw add_comm,
show abs (abs (tf + -vL1) + (m + -tf) + -abs (tf + -vL2) + -(m + -tf)) ≤ d,
rw add_assoc,
rw add_assoc,
rw add_comm (m + -tf),
rw add_assoc,
rw add_comm (-(m+-tf)),
rw add_neg_self (m+-tf),
rw add_zero,
/- we want to solve the goal by using that by hm, abs(tf - vL1 - (tf - vL2)) ≤ d
and by another lemma abs (abs (tf - vL1) - abs (tf - vL2)) ≤ abs(tf - vL1 - (tf - vL2))
through transitivity of ≤ -/
have Q: abs(tf - vL1 - (tf - vL2)) ≤ d,
{ rw ← abs_neg, -- abs(tf - vL1 - (tf - vL2)) = abs(-(tf - vL1 - (tf - vL2)))
convert hm, -- abs(-(tf - vL1 - (tf - vL2))) ≤ d is basically hm
-- so what is left to prove for Q is that -(tf - vL1 - (tf - vL2)) = vL1 - vL2
ring,}, -- using that ( ℤ, *, +) is a ring (so commutativity and associativity hold)
have R: abs (abs (tf - vL1) - abs (tf - vL2)) ≤ abs ((tf - vL1) - (tf - vL2)),
{exact abs_abs_sub_abs_le_abs_sub (tf - vL1) (tf - vL2)}, -- see misc_lemmas
exact le_trans R Q, -- proven by Q, R and transitivity of ≤
end
/-- Value of all-chain or all-loop game L with n components
given that we are opening the i'th component first-/
definition list.value_i (tf : ℤ) :
∀ (n : ℕ) (L : list ℤ) (i : fin n) (hL : L.length = n), ℤ
| (0) L i h := begin exfalso, exact fin.elim0 i, end /- i.is_lt gives a contradiction (i < 0 is impossible)-/
| (d + 1) L i h := list.aux_fun (L.nth_le i.val (by rw h; exact i.is_lt)) tf (list.min' (list.of_fn $
λ (j : fin d), list.value_i d (L.remove_nth i.val) j begin
rw list.length_remove_nth L i.val _,
rw h, simp,
rw h, exact i.is_lt,
end))
/- The first case being a contradiction also makes sense for our definition.
we cannot open a component first if the game is empty
list.value_i tf n L i hL is defined inductively on the number of components as
L.nth_le i.val _ - tf + abs ( tf - v ),
where v is the minimum value of list.value_i (n - 1) (L.remove_nth i.val) j _over all j in fin (n - 1).
As we define he minimum element of the empty list to be zero, if our 'game' has only one component,
(ie. n = 1) , we have list.value_i tf 1 L ⟨0,_⟩ hL =
L.nth_le 0 _ - tf + abs ( tf - 0 ), which is the length of the component, as it should be.
(i = ⟨0,_⟩ is the only possibility as we must have i.val < n)
So this recursion terminates.-/
|
2877e70e226484541cb5f5a3f8eb1c37faed4337 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/data/list/nodup_equiv_fin.lean | ed043c98fccf0b5cbbf0b2251de565a14f7e166d | [
"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 | 2,382 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import data.list.sort
import data.fin
/-!
# Isomorphism between `fin (length l)` and `{x // x ∈ l}`
Given a list `l,
* if `l` has no duplicates, then `list.nodup.nth_le_equiv` is the bijection between `fin (length l)`
and `{x // x ∈ l}` sending `⟨i, hi⟩` to `⟨nth_le l i hi, _⟩` with the inverse sending `⟨x, hx⟩` to
`⟨index_of x l, _⟩`;
* if `l` is sorted w.r.t. `(<)`, then `list.sorted.nth_le_iso` is the same bijection reinterpreted
as an `order_iso`.
-/
namespace list
variable {α : Type*}
namespace nodup
variable [decidable_eq α]
/-- If `l` has no duplicates, then `list.nth_le` defines a bijection between `fin (length l)` and
the set of elements of `l`. -/
def nth_le_equiv (l : list α) (H : nodup l) : fin (length l) ≃ {x // x ∈ l} :=
{ to_fun := λ i, ⟨nth_le l i i.2, nth_le_mem l i i.2⟩,
inv_fun := λ x, ⟨index_of ↑x l, index_of_lt_length.2 x.2⟩,
left_inv := λ i, by simp [H],
right_inv := λ x, by simp }
variables {l : list α} (H : nodup l) (x : {x // x ∈ l}) (i : fin (length l))
@[simp] lemma coe_nth_le_equiv_apply : (H.nth_le_equiv l i : α) = nth_le l i i.2 := rfl
@[simp] lemma coe_nth_le_equiv_symm_apply : ((H.nth_le_equiv l).symm x : ℕ) = index_of ↑x l := rfl
end nodup
namespace sorted
variables [preorder α] {l : list α}
lemma nth_le_mono (h : l.sorted (≤)) :
monotone (λ i : fin l.length, l.nth_le i i.2) :=
λ i j, h.rel_nth_le_of_le _ _
lemma nth_le_strict_mono (h : l.sorted (<)) :
strict_mono (λ i : fin l.length, l.nth_le i i.2) :=
λ i j, h.rel_nth_le_of_lt _ _
variable [decidable_eq α]
/-- If `l` is a list sorted w.r.t. `(<)`, then `list.nth_le` defines an order isomorphism between
`fin (length l)` and the set of elements of `l`. -/
def nth_le_iso (l : list α) (H : sorted (<) l) : fin (length l) ≃o {x // x ∈ l} :=
{ to_equiv := H.nodup.nth_le_equiv l,
map_rel_iff' := λ i j, H.nth_le_strict_mono.le_iff_le }
variables (H : sorted (<) l) {x : {x // x ∈ l}} {i : fin l.length}
@[simp] lemma coe_nth_le_iso_apply : (H.nth_le_iso l i : α) = nth_le l i i.2 := rfl
@[simp] lemma coe_nth_le_iso_symm_apply : ((H.nth_le_iso l).symm x : ℕ) = index_of ↑x l := rfl
end sorted
end list
|
eebc4d6c659e0f7b011cfbaac84398d92e46686e | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/data/list/basic.lean | 325fb426f705b0eb97a5da8a761cef963888dc35 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 160,491 | lean | /-
Copyright (c) 2014 Parikshit Khanna. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro
-/
import control.monad.basic
import data.nat.basic
/-!
# Basic properties of lists
-/
open function nat
namespace list
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x}
attribute [inline] list.head
instance : is_left_id (list α) has_append.append [] :=
⟨ nil_append ⟩
instance : is_right_id (list α) has_append.append [] :=
⟨ append_nil ⟩
instance : is_associative (list α) has_append.append :=
⟨ append_assoc ⟩
theorem cons_ne_nil (a : α) (l : list α) : a::l ≠ [].
theorem cons_ne_self (a : α) (l : list α) : a::l ≠ l :=
mt (congr_arg length) (nat.succ_ne_self _)
theorem head_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} :
(h₁::t₁) = (h₂::t₂) → h₁ = h₂ :=
assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq)
theorem tail_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} :
(h₁::t₁) = (h₂::t₂) → t₁ = t₂ :=
assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq)
@[simp] theorem cons_injective {a : α} : injective (cons a) :=
assume l₁ l₂, assume Pe, tail_eq_of_cons_eq Pe
theorem cons_inj (a : α) {l l' : list α} : a::l = a::l' ↔ l = l' :=
cons_injective.eq_iff
theorem exists_cons_of_ne_nil {l : list α} (h : l ≠ nil) : ∃ b L, l = b :: L :=
by { induction l with c l', contradiction, use [c,l'], }
/-! ### mem -/
theorem mem_singleton_self (a : α) : a ∈ [a] := mem_cons_self _ _
theorem eq_of_mem_singleton {a b : α} : a ∈ [b] → a = b :=
assume : a ∈ [b], or.elim (eq_or_mem_of_mem_cons this)
(assume : a = b, this)
(assume : a ∈ [], absurd this (not_mem_nil a))
@[simp] theorem mem_singleton {a b : α} : a ∈ [b] ↔ a = b :=
⟨eq_of_mem_singleton, or.inl⟩
theorem mem_of_mem_cons_of_mem {a b : α} {l : list α} : a ∈ b::l → b ∈ l → a ∈ l :=
assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl)
(assume : a = b, begin subst a, exact binl end)
(assume : a ∈ l, this)
theorem _root_.decidable.list.eq_or_ne_mem_of_mem [decidable_eq α]
{a b : α} {l : list α} (h : a ∈ b :: l) : a = b ∨ (a ≠ b ∧ a ∈ l) :=
decidable.by_cases or.inl $ assume : a ≠ b, h.elim or.inl $ assume h, or.inr ⟨this, h⟩
theorem eq_or_ne_mem_of_mem {a b : α} {l : list α} : a ∈ b :: l → a = b ∨ (a ≠ b ∧ a ∈ l) :=
by classical; exact decidable.list.eq_or_ne_mem_of_mem
theorem not_mem_append {a : α} {s t : list α} (h₁ : a ∉ s) (h₂ : a ∉ t) : a ∉ s ++ t :=
mt mem_append.1 $ not_or_distrib.2 ⟨h₁, h₂⟩
theorem ne_nil_of_mem {a : α} {l : list α} (h : a ∈ l) : l ≠ [] :=
by intro e; rw e at h; cases h
theorem mem_split {a : α} {l : list α} (h : a ∈ l) : ∃ s t : list α, l = s ++ a :: t :=
begin
induction l with b l ih, {cases h}, rcases h with rfl | h,
{ exact ⟨[], l, rfl⟩ },
{ rcases ih h with ⟨s, t, rfl⟩,
exact ⟨b::s, t, rfl⟩ }
end
theorem mem_of_ne_of_mem {a y : α} {l : list α} (h₁ : a ≠ y) (h₂ : a ∈ y :: l) : a ∈ l :=
or.elim (eq_or_mem_of_mem_cons h₂) (λe, absurd e h₁) (λr, r)
theorem ne_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ≠ b :=
assume nin aeqb, absurd (or.inl aeqb) nin
theorem not_mem_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ∉ l :=
assume nin nainl, absurd (or.inr nainl) nin
theorem not_mem_cons_of_ne_of_not_mem {a y : α} {l : list α} : a ≠ y → a ∉ l → a ∉ y::l :=
assume p1 p2, not.intro (assume Pain, absurd (eq_or_mem_of_mem_cons Pain) (not_or p1 p2))
theorem ne_and_not_mem_of_not_mem_cons {a y : α} {l : list α} : a ∉ y::l → a ≠ y ∧ a ∉ l :=
assume p, and.intro (ne_of_not_mem_cons p) (not_mem_of_not_mem_cons p)
@[simp] theorem mem_map {f : α → β} {b : β} {l : list α} : b ∈ map f l ↔ ∃ a, a ∈ l ∧ f a = b :=
begin
-- This proof uses no axioms, that's why it's longer that `induction`; simp [...]
induction l with a l ihl,
{ split, { rintro ⟨_⟩ }, { rintro ⟨a, ⟨_⟩, _⟩ } },
{ refine (or_congr eq_comm ihl).trans _,
split,
{ rintro (h|⟨c, hcl, h⟩),
exacts [⟨a, or.inl rfl, h⟩, ⟨c, or.inr hcl, h⟩] },
{ rintro ⟨c, (hc|hc), h⟩,
exacts [or.inl $ (congr_arg f hc.symm).trans h, or.inr ⟨c, hc, h⟩] } }
end
alias mem_map ↔ list.exists_of_mem_map _
theorem mem_map_of_mem (f : α → β) {a : α} {l : list α} (h : a ∈ l) : f a ∈ map f l :=
mem_map.2 ⟨a, h, rfl⟩
theorem mem_map_of_injective {f : α → β} (H : injective f) {a : α} {l : list α} :
f a ∈ map f l ↔ a ∈ l :=
⟨λ m, let ⟨a', m', e⟩ := exists_of_mem_map m in H e ▸ m', mem_map_of_mem _⟩
lemma forall_mem_map_iff {f : α → β} {l : list α} {P : β → Prop} :
(∀ i ∈ l.map f, P i) ↔ ∀ j ∈ l, P (f j) :=
begin
split,
{ assume H j hj,
exact H (f j) (mem_map_of_mem f hj) },
{ assume H i hi,
rcases mem_map.1 hi with ⟨j, hj, ji⟩,
rw ← ji,
exact H j hj }
end
@[simp] lemma map_eq_nil {f : α → β} {l : list α} : list.map f l = [] ↔ l = [] :=
⟨by cases l; simp only [forall_prop_of_true, map, forall_prop_of_false, not_false_iff],
λ h, h.symm ▸ rfl⟩
@[simp] theorem mem_join {a : α} : ∀ {L : list (list α)}, a ∈ join L ↔ ∃ l, l ∈ L ∧ a ∈ l
| [] := ⟨false.elim, λ⟨_, h, _⟩, false.elim h⟩
| (c :: L) := by simp only [join, mem_append, @mem_join L, mem_cons_iff, or_and_distrib_right,
exists_or_distrib, exists_eq_left]
theorem exists_of_mem_join {a : α} {L : list (list α)} : a ∈ join L → ∃ l, l ∈ L ∧ a ∈ l :=
mem_join.1
theorem mem_join_of_mem {a : α} {L : list (list α)} {l} (lL : l ∈ L) (al : a ∈ l) : a ∈ join L :=
mem_join.2 ⟨l, lL, al⟩
@[simp]
theorem mem_bind {b : β} {l : list α} {f : α → list β} : b ∈ list.bind l f ↔ ∃ a ∈ l, b ∈ f a :=
iff.trans mem_join
⟨λ ⟨l', h1, h2⟩, let ⟨a, al, fa⟩ := exists_of_mem_map h1 in ⟨a, al, fa.symm ▸ h2⟩,
λ ⟨a, al, bfa⟩, ⟨f a, mem_map_of_mem _ al, bfa⟩⟩
theorem exists_of_mem_bind {b : β} {l : list α} {f : α → list β} :
b ∈ list.bind l f → ∃ a ∈ l, b ∈ f a :=
mem_bind.1
theorem mem_bind_of_mem {b : β} {l : list α} {f : α → list β} {a} (al : a ∈ l) (h : b ∈ f a) :
b ∈ list.bind l f :=
mem_bind.2 ⟨a, al, h⟩
lemma bind_map {g : α → list β} {f : β → γ} :
∀(l : list α), list.map f (l.bind g) = l.bind (λa, (g a).map f)
| [] := rfl
| (a::l) := by simp only [cons_bind, map_append, bind_map l]
lemma map_bind (g : β → list γ) (f : α → β) :
∀ l : list α, (list.map f l).bind g = l.bind (λ a, g (f a))
| [] := rfl
| (a::l) := by simp only [cons_bind, map_cons, map_bind l]
/-- If each element of a list can be lifted to some type, then the whole list can be lifted to this
type. -/
instance [h : can_lift α β] : can_lift (list α) (list β) :=
{ coe := list.map h.coe,
cond := λ l, ∀ x ∈ l, can_lift.cond β x,
prf := λ l H,
begin
induction l with a l ihl, { exact ⟨[], rfl⟩ },
rcases ihl (λ x hx, H x (or.inr hx)) with ⟨l, rfl⟩,
rcases can_lift.prf a (H a (or.inl rfl)) with ⟨a, rfl⟩,
exact ⟨a :: l, rfl⟩
end}
/-! ### length -/
theorem length_eq_zero {l : list α} : length l = 0 ↔ l = [] :=
⟨eq_nil_of_length_eq_zero, λ h, h.symm ▸ rfl⟩
@[simp] lemma length_singleton (a : α) : length [a] = 1 := rfl
theorem length_pos_of_mem {a : α} : ∀ {l : list α}, a ∈ l → 0 < length l
| (b::l) _ := zero_lt_succ _
theorem exists_mem_of_length_pos : ∀ {l : list α}, 0 < length l → ∃ a, a ∈ l
| (b::l) _ := ⟨b, mem_cons_self _ _⟩
theorem length_pos_iff_exists_mem {l : list α} : 0 < length l ↔ ∃ a, a ∈ l :=
⟨exists_mem_of_length_pos, λ ⟨a, h⟩, length_pos_of_mem h⟩
theorem ne_nil_of_length_pos {l : list α} : 0 < length l → l ≠ [] :=
λ h1 h2, lt_irrefl 0 ((length_eq_zero.2 h2).subst h1)
theorem length_pos_of_ne_nil {l : list α} : l ≠ [] → 0 < length l :=
λ h, pos_iff_ne_zero.2 $ λ h0, h $ length_eq_zero.1 h0
theorem length_pos_iff_ne_nil {l : list α} : 0 < length l ↔ l ≠ [] :=
⟨ne_nil_of_length_pos, length_pos_of_ne_nil⟩
lemma exists_mem_of_ne_nil (l : list α) (h : l ≠ []) : ∃ x, x ∈ l :=
exists_mem_of_length_pos (length_pos_of_ne_nil h)
theorem length_eq_one {l : list α} : length l = 1 ↔ ∃ a, l = [a] :=
⟨match l with [a], _ := ⟨a, rfl⟩ end, λ ⟨a, e⟩, e.symm ▸ rfl⟩
lemma exists_of_length_succ {n} :
∀ l : list α, l.length = n + 1 → ∃ h t, l = h :: t
| [] H := absurd H.symm $ succ_ne_zero n
| (h :: t) H := ⟨h, t, rfl⟩
@[simp] lemma length_injective_iff : injective (list.length : list α → ℕ) ↔ subsingleton α :=
begin
split,
{ intro h, refine ⟨λ x y, _⟩, suffices : [x] = [y], { simpa using this }, apply h, refl },
{ intros hα l1 l2 hl, induction l1 generalizing l2; cases l2,
{ refl }, { cases hl }, { cases hl },
congr, exactI subsingleton.elim _ _, apply l1_ih, simpa using hl }
end
@[simp] lemma length_injective [subsingleton α] : injective (length : list α → ℕ) :=
length_injective_iff.mpr $ by apply_instance
/-! ### set-theoretic notation of lists -/
lemma empty_eq : (∅ : list α) = [] := by refl
lemma singleton_eq (x : α) : ({x} : list α) = [x] := rfl
lemma insert_neg [decidable_eq α] {x : α} {l : list α} (h : x ∉ l) :
has_insert.insert x l = x :: l :=
if_neg h
lemma insert_pos [decidable_eq α] {x : α} {l : list α} (h : x ∈ l) :
has_insert.insert x l = l :=
if_pos h
lemma doubleton_eq [decidable_eq α] {x y : α} (h : x ≠ y) : ({x, y} : list α) = [x, y] :=
by { rw [insert_neg, singleton_eq], rwa [singleton_eq, mem_singleton] }
/-! ### bounded quantifiers over lists -/
theorem forall_mem_nil (p : α → Prop) : ∀ x ∈ @nil α, p x.
theorem forall_mem_cons : ∀ {p : α → Prop} {a : α} {l : list α},
(∀ x ∈ a :: l, p x) ↔ p a ∧ ∀ x ∈ l, p x :=
ball_cons
theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : list α}
(h : ∀ x ∈ a :: l, p x) :
∀ x ∈ l, p x :=
(forall_mem_cons.1 h).2
theorem forall_mem_singleton {p : α → Prop} {a : α} : (∀ x ∈ [a], p x) ↔ p a :=
by simp only [mem_singleton, forall_eq]
theorem forall_mem_append {p : α → Prop} {l₁ l₂ : list α} :
(∀ x ∈ l₁ ++ l₂, p x) ↔ (∀ x ∈ l₁, p x) ∧ (∀ x ∈ l₂, p x) :=
by simp only [mem_append, or_imp_distrib, forall_and_distrib]
theorem not_exists_mem_nil (p : α → Prop) : ¬ ∃ x ∈ @nil α, p x.
theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : list α) (h : p a) :
∃ x ∈ a :: l, p x :=
bex.intro a (mem_cons_self _ _) h
theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ l, p x) :
∃ x ∈ a :: l, p x :=
bex.elim h (λ x xl px, bex.intro x (mem_cons_of_mem _ xl) px)
theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ a :: l, p x) :
p a ∨ ∃ x ∈ l, p x :=
bex.elim h (λ x xal px,
or.elim (eq_or_mem_of_mem_cons xal)
(assume : x = a, begin rw ←this, left, exact px end)
(assume : x ∈ l, or.inr (bex.intro x this px)))
theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : list α) :
(∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x :=
iff.intro or_exists_of_exists_mem_cons
(assume h, or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists)
/-! ### list subset -/
theorem subset_def {l₁ l₂ : list α} : l₁ ⊆ l₂ ↔ ∀ ⦃a : α⦄, a ∈ l₁ → a ∈ l₂ := iff.rfl
theorem subset_append_of_subset_left (l l₁ l₂ : list α) : l ⊆ l₁ → l ⊆ l₁++l₂ :=
λ s, subset.trans s $ subset_append_left _ _
theorem subset_append_of_subset_right (l l₁ l₂ : list α) : l ⊆ l₂ → l ⊆ l₁++l₂ :=
λ s, subset.trans s $ subset_append_right _ _
@[simp] theorem cons_subset {a : α} {l m : list α} :
a::l ⊆ m ↔ a ∈ m ∧ l ⊆ m :=
by simp only [subset_def, mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq]
theorem cons_subset_of_subset_of_mem {a : α} {l m : list α}
(ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m :=
cons_subset.2 ⟨ainm, lsubm⟩
theorem append_subset_of_subset_of_subset {l₁ l₂ l : list α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) :
l₁ ++ l₂ ⊆ l :=
λ a h, (mem_append.1 h).elim (@l₁subl _) (@l₂subl _)
@[simp] theorem append_subset_iff {l₁ l₂ l : list α} :
l₁ ++ l₂ ⊆ l ↔ l₁ ⊆ l ∧ l₂ ⊆ l :=
begin
split,
{ intro h, simp only [subset_def] at *, split; intros; simp* },
{ rintro ⟨h1, h2⟩, apply append_subset_of_subset_of_subset h1 h2 }
end
theorem eq_nil_of_subset_nil : ∀ {l : list α}, l ⊆ [] → l = []
| [] s := rfl
| (a::l) s := false.elim $ s $ mem_cons_self a l
theorem eq_nil_iff_forall_not_mem {l : list α} : l = [] ↔ ∀ a, a ∉ l :=
show l = [] ↔ l ⊆ [], from ⟨λ e, e ▸ subset.refl _, eq_nil_of_subset_nil⟩
theorem map_subset {l₁ l₂ : list α} (f : α → β) (H : l₁ ⊆ l₂) : map f l₁ ⊆ map f l₂ :=
λ x, by simp only [mem_map, not_and, exists_imp_distrib, and_imp]; exact λ a h e, ⟨a, H h, e⟩
theorem map_subset_iff {l₁ l₂ : list α} (f : α → β) (h : injective f) :
map f l₁ ⊆ map f l₂ ↔ l₁ ⊆ l₂ :=
begin
refine ⟨_, map_subset f⟩, intros h2 x hx,
rcases mem_map.1 (h2 (mem_map_of_mem f hx)) with ⟨x', hx', hxx'⟩,
cases h hxx', exact hx'
end
/-! ### append -/
lemma append_eq_has_append {L₁ L₂ : list α} : list.append L₁ L₂ = L₁ ++ L₂ := rfl
@[simp] lemma singleton_append {x : α} {l : list α} : [x] ++ l = x :: l := rfl
theorem append_ne_nil_of_ne_nil_left (s t : list α) : s ≠ [] → s ++ t ≠ [] :=
by induction s; intros; contradiction
theorem append_ne_nil_of_ne_nil_right (s t : list α) : t ≠ [] → s ++ t ≠ [] :=
by induction s; intros; contradiction
@[simp] lemma append_eq_nil {p q : list α} : (p ++ q) = [] ↔ p = [] ∧ q = [] :=
by cases p; simp only [nil_append, cons_append, eq_self_iff_true, true_and, false_and]
@[simp] lemma nil_eq_append_iff {a b : list α} : [] = a ++ b ↔ a = [] ∧ b = [] :=
by rw [eq_comm, append_eq_nil]
lemma append_eq_cons_iff {a b c : list α} {x : α} :
a ++ b = x :: c ↔ (a = [] ∧ b = x :: c) ∨ (∃a', a = x :: a' ∧ c = a' ++ b) :=
by cases a; simp only [and_assoc, @eq_comm _ c, nil_append, cons_append, eq_self_iff_true,
true_and, false_and, exists_false, false_or, or_false, exists_and_distrib_left, exists_eq_left']
lemma cons_eq_append_iff {a b c : list α} {x : α} :
(x :: c : list α) = a ++ b ↔ (a = [] ∧ b = x :: c) ∨ (∃a', a = x :: a' ∧ c = a' ++ b) :=
by rw [eq_comm, append_eq_cons_iff]
lemma append_eq_append_iff {a b c d : list α} :
a ++ b = c ++ d ↔ (∃a', c = a ++ a' ∧ b = a' ++ d) ∨ (∃c', a = c ++ c' ∧ d = c' ++ b) :=
begin
induction a generalizing c,
case nil { rw nil_append, split,
{ rintro rfl, left, exact ⟨_, rfl, rfl⟩ },
{ rintro (⟨a', rfl, rfl⟩ | ⟨a', H, rfl⟩), {refl}, {rw [← append_assoc, ← H], refl} } },
case cons : a as ih {
cases c,
{ simp only [cons_append, nil_append, false_and, exists_false, false_or, exists_eq_left'],
exact eq_comm },
{ simp only [cons_append, @eq_comm _ a, ih, and_assoc, and_or_distrib_left,
exists_and_distrib_left] } }
end
@[simp] theorem take_append_drop : ∀ (n : ℕ) (l : list α), take n l ++ drop n l = l
| 0 a := rfl
| (succ n) [] := rfl
| (succ n) (x :: xs) := congr_arg (cons x) $ take_append_drop n xs
-- TODO(Leo): cleanup proof after arith dec proc
theorem append_inj :
∀ {s₁ s₂ t₁ t₂ : list α}, s₁ ++ t₁ = s₂ ++ t₂ → length s₁ = length s₂ → s₁ = s₂ ∧ t₁ = t₂
| [] [] t₁ t₂ h hl := ⟨rfl, h⟩
| (a::s₁) [] t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl
| [] (b::s₂) t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl.symm
| (a::s₁) (b::s₂) t₁ t₂ h hl := list.no_confusion h $ λab hap,
let ⟨e1, e2⟩ := @append_inj s₁ s₂ t₁ t₂ hap (succ.inj hl) in
by rw [ab, e1, e2]; exact ⟨rfl, rfl⟩
theorem append_inj_right {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length s₁ = length s₂) : t₁ = t₂ :=
(append_inj h hl).right
theorem append_inj_left {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length s₁ = length s₂) : s₁ = s₂ :=
(append_inj h hl).left
theorem append_inj' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) :
s₁ = s₂ ∧ t₁ = t₂ :=
append_inj h $ @nat.add_right_cancel _ (length t₁) _ $
let hap := congr_arg length h in by simp only [length_append] at hap; rwa [← hl] at hap
theorem append_inj_right' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length t₁ = length t₂) : t₁ = t₂ :=
(append_inj' h hl).right
theorem append_inj_left' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length t₁ = length t₂) : s₁ = s₂ :=
(append_inj' h hl).left
theorem append_left_cancel {s t₁ t₂ : list α} (h : s ++ t₁ = s ++ t₂) : t₁ = t₂ :=
append_inj_right h rfl
theorem append_right_cancel {s₁ s₂ t : list α} (h : s₁ ++ t = s₂ ++ t) : s₁ = s₂ :=
append_inj_left' h rfl
theorem append_right_injective (s : list α) : function.injective (λ t, s ++ t) :=
λ t₁ t₂, append_left_cancel
theorem append_right_inj {t₁ t₂ : list α} (s) : s ++ t₁ = s ++ t₂ ↔ t₁ = t₂ :=
(append_right_injective s).eq_iff
theorem append_left_injective (t : list α) : function.injective (λ s, s ++ t) :=
λ s₁ s₂, append_right_cancel
theorem append_left_inj {s₁ s₂ : list α} (t) : s₁ ++ t = s₂ ++ t ↔ s₁ = s₂ :=
(append_left_injective t).eq_iff
theorem map_eq_append_split {f : α → β} {l : list α} {s₁ s₂ : list β}
(h : map f l = s₁ ++ s₂) : ∃ l₁ l₂, l = l₁ ++ l₂ ∧ map f l₁ = s₁ ∧ map f l₂ = s₂ :=
begin
have := h, rw [← take_append_drop (length s₁) l] at this ⊢,
rw map_append at this,
refine ⟨_, _, rfl, append_inj this _⟩,
rw [length_map, length_take, min_eq_left],
rw [← length_map f l, h, length_append],
apply nat.le_add_right
end
/-! ### repeat -/
@[simp] theorem repeat_succ (a : α) (n) : repeat a (n + 1) = a :: repeat a n := rfl
theorem mem_repeat {a b : α} : ∀ {n}, b ∈ repeat a n ↔ n ≠ 0 ∧ b = a
| 0 := by simp
| (n + 1) := by simp [mem_repeat]
theorem eq_of_mem_repeat {a b : α} {n} (h : b ∈ repeat a n) : b = a :=
(mem_repeat.1 h).2
theorem eq_repeat_of_mem {a : α} : ∀ {l : list α}, (∀ b ∈ l, b = a) → l = repeat a l.length
| [] H := rfl
| (b::l) H := by cases forall_mem_cons.1 H with H₁ H₂;
unfold length repeat; congr; [exact H₁, exact eq_repeat_of_mem H₂]
theorem eq_repeat' {a : α} {l : list α} : l = repeat a l.length ↔ ∀ b ∈ l, b = a :=
⟨λ h, h.symm ▸ λ b, eq_of_mem_repeat, eq_repeat_of_mem⟩
theorem eq_repeat {a : α} {n} {l : list α} : l = repeat a n ↔ length l = n ∧ ∀ b ∈ l, b = a :=
⟨λ h, h.symm ▸ ⟨length_repeat _ _, λ b, eq_of_mem_repeat⟩,
λ ⟨e, al⟩, e ▸ eq_repeat_of_mem al⟩
theorem repeat_add (a : α) (m n) : repeat a (m + n) = repeat a m ++ repeat a n :=
by induction m; simp only [*, zero_add, succ_add, repeat]; split; refl
theorem repeat_subset_singleton (a : α) (n) : repeat a n ⊆ [a] :=
λ b h, mem_singleton.2 (eq_of_mem_repeat h)
@[simp] theorem map_const (l : list α) (b : β) : map (function.const α b) l = repeat b l.length :=
by induction l; [refl, simp only [*, map]]; split; refl
theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) :
b₁ = b₂ :=
by rw map_const at h; exact eq_of_mem_repeat h
@[simp] theorem map_repeat (f : α → β) (a : α) (n) : map f (repeat a n) = repeat (f a) n :=
by induction n; [refl, simp only [*, repeat, map]]; split; refl
@[simp] theorem tail_repeat (a : α) (n) : tail (repeat a n) = repeat a n.pred :=
by cases n; refl
@[simp] theorem join_repeat_nil (n : ℕ) : join (repeat [] n) = @nil α :=
by induction n; [refl, simp only [*, repeat, join, append_nil]]
lemma repeat_left_injective {n : ℕ} (hn : n ≠ 0) :
function.injective (λ a : α, repeat a n) :=
λ a b h, (eq_repeat.1 h).2 _ $ mem_repeat.2 ⟨hn, rfl⟩
lemma repeat_left_inj {a b : α} {n : ℕ} (hn : n ≠ 0) :
repeat a n = repeat b n ↔ a = b :=
(repeat_left_injective hn).eq_iff
@[simp] lemma repeat_left_inj' {a b : α} :
∀ {n}, repeat a n = repeat b n ↔ n = 0 ∨ a = b
| 0 := by simp
| (n + 1) := (repeat_left_inj n.succ_ne_zero).trans $ by simp only [n.succ_ne_zero, false_or]
lemma repeat_right_injective (a : α) : function.injective (repeat a) :=
function.left_inverse.injective (length_repeat a)
@[simp] lemma repeat_right_inj {a : α} {n m : ℕ} :
repeat a n = repeat a m ↔ n = m :=
(repeat_right_injective a).eq_iff
/-! ### pure -/
@[simp] theorem mem_pure {α} (x y : α) :
x ∈ (pure y : list α) ↔ x = y := by simp! [pure,list.ret]
/-! ### bind -/
@[simp] theorem bind_eq_bind {α β} (f : α → list β) (l : list α) :
l >>= f = l.bind f := rfl
-- TODO: duplicate of a lemma in core
theorem bind_append (f : α → list β) (l₁ l₂ : list α) :
(l₁ ++ l₂).bind f = l₁.bind f ++ l₂.bind f :=
append_bind _ _ _
@[simp] theorem bind_singleton (f : α → list β) (x : α) : [x].bind f = f x :=
append_nil (f x)
@[simp] theorem bind_singleton' (l : list α) : l.bind (λ x, [x]) = l := bind_pure l
theorem map_eq_bind {α β} (f : α → β) (l : list α) : map f l = l.bind (λ x, [f x]) :=
by { transitivity, rw [← bind_singleton' l, bind_map], refl }
theorem bind_assoc {α β} (l : list α) (f : α → list β) (g : β → list γ) :
(l.bind f).bind g = l.bind (λ x, (f x).bind g) :=
by induction l; simp *
/-! ### concat -/
theorem concat_nil (a : α) : concat [] a = [a] := rfl
theorem concat_cons (a b : α) (l : list α) : concat (a :: l) b = a :: concat l b := rfl
@[simp] theorem concat_eq_append (a : α) (l : list α) : concat l a = l ++ [a] :=
by induction l; simp only [*, concat]; split; refl
theorem init_eq_of_concat_eq {a : α} {l₁ l₂ : list α} : concat l₁ a = concat l₂ a → l₁ = l₂ :=
begin
intro h,
rw [concat_eq_append, concat_eq_append] at h,
exact append_right_cancel h
end
theorem last_eq_of_concat_eq {a b : α} {l : list α} : concat l a = concat l b → a = b :=
begin
intro h,
rw [concat_eq_append, concat_eq_append] at h,
exact head_eq_of_cons_eq (append_left_cancel h)
end
theorem concat_ne_nil (a : α) (l : list α) : concat l a ≠ [] :=
by simp
theorem concat_append (a : α) (l₁ l₂ : list α) : concat l₁ a ++ l₂ = l₁ ++ a :: l₂ :=
by simp
theorem length_concat (a : α) (l : list α) : length (concat l a) = succ (length l) :=
by simp only [concat_eq_append, length_append, length]
theorem append_concat (a : α) (l₁ l₂ : list α) : l₁ ++ concat l₂ a = concat (l₁ ++ l₂) a :=
by simp
/-! ### reverse -/
@[simp] theorem reverse_nil : reverse (@nil α) = [] := rfl
local attribute [simp] reverse_core
@[simp] theorem reverse_cons (a : α) (l : list α) : reverse (a::l) = reverse l ++ [a] :=
have aux : ∀ l₁ l₂, reverse_core l₁ l₂ ++ [a] = reverse_core l₁ (l₂ ++ [a]),
by intro l₁; induction l₁; intros; [refl, simp only [*, reverse_core, cons_append]],
(aux l nil).symm
theorem reverse_core_eq (l₁ l₂ : list α) : reverse_core l₁ l₂ = reverse l₁ ++ l₂ :=
by induction l₁ generalizing l₂; [refl, simp only [*, reverse_core, reverse_cons, append_assoc]];
refl
theorem reverse_cons' (a : α) (l : list α) : reverse (a::l) = concat (reverse l) a :=
by simp only [reverse_cons, concat_eq_append]
@[simp] theorem reverse_singleton (a : α) : reverse [a] = [a] := rfl
@[simp] theorem reverse_append (s t : list α) : reverse (s ++ t) = (reverse t) ++ (reverse s) :=
by induction s; [rw [nil_append, reverse_nil, append_nil],
simp only [*, cons_append, reverse_cons, append_assoc]]
theorem reverse_concat (l : list α) (a : α) : reverse (concat l a) = a :: reverse l :=
by rw [concat_eq_append, reverse_append, reverse_singleton, singleton_append]
@[simp] theorem reverse_reverse (l : list α) : reverse (reverse l) = l :=
by induction l; [refl, simp only [*, reverse_cons, reverse_append]]; refl
@[simp] theorem reverse_involutive : involutive (@reverse α) :=
λ l, reverse_reverse l
@[simp] theorem reverse_injective : injective (@reverse α) :=
reverse_involutive.injective
@[simp] theorem reverse_inj {l₁ l₂ : list α} : reverse l₁ = reverse l₂ ↔ l₁ = l₂ :=
reverse_injective.eq_iff
lemma reverse_eq_iff {l l' : list α} :
l.reverse = l' ↔ l = l'.reverse :=
reverse_involutive.eq_iff
@[simp] theorem reverse_eq_nil {l : list α} : reverse l = [] ↔ l = [] :=
@reverse_inj _ l []
theorem concat_eq_reverse_cons (a : α) (l : list α) : concat l a = reverse (a :: reverse l) :=
by simp only [concat_eq_append, reverse_cons, reverse_reverse]
@[simp] theorem length_reverse (l : list α) : length (reverse l) = length l :=
by induction l; [refl, simp only [*, reverse_cons, length_append, length]]
@[simp] theorem map_reverse (f : α → β) (l : list α) : map f (reverse l) = reverse (map f l) :=
by induction l; [refl, simp only [*, map, reverse_cons, map_append]]
theorem map_reverse_core (f : α → β) (l₁ l₂ : list α) :
map f (reverse_core l₁ l₂) = reverse_core (map f l₁) (map f l₂) :=
by simp only [reverse_core_eq, map_append, map_reverse]
@[simp] theorem mem_reverse {a : α} {l : list α} : a ∈ reverse l ↔ a ∈ l :=
by induction l; [refl, simp only [*, reverse_cons, mem_append, mem_singleton, mem_cons_iff,
not_mem_nil, false_or, or_false, or_comm]]
@[simp] theorem reverse_repeat (a : α) (n) : reverse (repeat a n) = repeat a n :=
eq_repeat.2 ⟨by simp only [length_reverse, length_repeat],
λ b h, eq_of_mem_repeat (mem_reverse.1 h)⟩
/-! ### empty -/
attribute [simp] list.empty
lemma empty_iff_eq_nil {l : list α} : l.empty ↔ l = [] :=
list.cases_on l (by simp) (by simp)
/-! ### init -/
@[simp] theorem length_init : ∀ (l : list α), length (init l) = length l - 1
| [] := rfl
| [a] := rfl
| (a :: b :: l) :=
begin
rw init,
simp only [add_left_inj, length, succ_add_sub_one],
exact length_init (b :: l)
end
/-! ### last -/
@[simp] theorem last_cons {a : α} {l : list α} :
∀ (h₁ : a :: l ≠ nil) (h₂ : l ≠ nil), last (a :: l) h₁ = last l h₂ :=
by {induction l; intros, contradiction, reflexivity}
@[simp] theorem last_append {a : α} (l : list α) (h : l ++ [a] ≠ []) : last (l ++ [a]) h = a :=
by induction l;
[refl, simp only [cons_append, last_cons _ (λ H, cons_ne_nil _ _ (append_eq_nil.1 H).2), *]]
theorem last_concat {a : α} (l : list α) (h : concat l a ≠ []) : last (concat l a) h = a :=
by simp only [concat_eq_append, last_append]
@[simp] theorem last_singleton (a : α) (h : [a] ≠ []) : last [a] h = a := rfl
@[simp] theorem last_cons_cons (a₁ a₂ : α) (l : list α) (h : a₁::a₂::l ≠ []) :
last (a₁::a₂::l) h = last (a₂::l) (cons_ne_nil a₂ l) := rfl
theorem init_append_last : ∀ {l : list α} (h : l ≠ []), init l ++ [last l h] = l
| [] h := absurd rfl h
| [a] h := rfl
| (a::b::l) h :=
begin
rw [init, cons_append, last_cons (cons_ne_nil _ _) (cons_ne_nil _ _)],
congr,
exact init_append_last (cons_ne_nil b l)
end
theorem last_congr {l₁ l₂ : list α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) :
last l₁ h₁ = last l₂ h₂ :=
by subst l₁
theorem last_mem : ∀ {l : list α} (h : l ≠ []), last l h ∈ l
| [] h := absurd rfl h
| [a] h := or.inl rfl
| (a::b::l) h := or.inr $ by { rw [last_cons_cons], exact last_mem (cons_ne_nil b l) }
lemma last_repeat_succ (a m : ℕ) :
(repeat a m.succ).last (ne_nil_of_length_eq_succ
(show (repeat a m.succ).length = m.succ, by rw length_repeat)) = a :=
begin
induction m with k IH,
{ simp },
{ simpa only [repeat_succ, last] }
end
/-! ### last' -/
@[simp] theorem last'_is_none :
∀ {l : list α}, (last' l).is_none ↔ l = []
| [] := by simp
| [a] := by simp
| (a::b::l) := by simp [@last'_is_none (b::l)]
@[simp] theorem last'_is_some : ∀ {l : list α}, l.last'.is_some ↔ l ≠ []
| [] := by simp
| [a] := by simp
| (a::b::l) := by simp [@last'_is_some (b::l)]
theorem mem_last'_eq_last : ∀ {l : list α} {x : α}, x ∈ l.last' → ∃ h, x = last l h
| [] x hx := false.elim $ by simpa using hx
| [a] x hx := have a = x, by simpa using hx, this ▸ ⟨cons_ne_nil a [], rfl⟩
| (a::b::l) x hx :=
begin
rw last' at hx,
rcases mem_last'_eq_last hx with ⟨h₁, h₂⟩,
use cons_ne_nil _ _,
rwa [last_cons]
end
theorem last'_eq_last_of_ne_nil : ∀ {l : list α} (h : l ≠ []), l.last' = some (l.last h)
| [] h := (h rfl).elim
| [a] _ := by {unfold last, unfold last'}
| (a::b::l) _ := @last'_eq_last_of_ne_nil (b::l) (cons_ne_nil _ _)
theorem mem_last'_cons {x y : α} : ∀ {l : list α} (h : x ∈ l.last'), x ∈ (y :: l).last'
| [] _ := by contradiction
| (a::l) h := h
theorem mem_of_mem_last' {l : list α} {a : α} (ha : a ∈ l.last') : a ∈ l :=
let ⟨h₁, h₂⟩ := mem_last'_eq_last ha in h₂.symm ▸ last_mem _
theorem init_append_last' : ∀ {l : list α} (a ∈ l.last'), init l ++ [a] = l
| [] a ha := (option.not_mem_none a ha).elim
| [a] _ rfl := rfl
| (a :: b :: l) c hc := by { rw [last'] at hc, rw [init, cons_append, init_append_last' _ hc] }
theorem ilast_eq_last' [inhabited α] : ∀ l : list α, l.ilast = l.last'.iget
| [] := by simp [ilast, arbitrary]
| [a] := rfl
| [a, b] := rfl
| [a, b, c] := rfl
| (a :: b :: c :: l) := by simp [ilast, ilast_eq_last' (c :: l)]
@[simp] theorem last'_append_cons : ∀ (l₁ : list α) (a : α) (l₂ : list α),
last' (l₁ ++ a :: l₂) = last' (a :: l₂)
| [] a l₂ := rfl
| [b] a l₂ := rfl
| (b::c::l₁) a l₂ := by rw [cons_append, cons_append, last', ← cons_append, last'_append_cons]
@[simp] theorem last'_cons_cons (x y : α) (l : list α) :
last' (x :: y :: l) = last' (y :: l) := rfl
theorem last'_append_of_ne_nil (l₁ : list α) : ∀ {l₂ : list α} (hl₂ : l₂ ≠ []),
last' (l₁ ++ l₂) = last' l₂
| [] hl₂ := by contradiction
| (b::l₂) _ := last'_append_cons l₁ b l₂
/-! ### head(') and tail -/
theorem head_eq_head' [inhabited α] (l : list α) : head l = (head' l).iget :=
by cases l; refl
theorem mem_of_mem_head' {x : α} : ∀ {l : list α}, x ∈ l.head' → x ∈ l
| [] h := (option.not_mem_none _ h).elim
| (a::l) h := by { simp only [head', option.mem_def] at h, exact h ▸ or.inl rfl }
@[simp] theorem head_cons [inhabited α] (a : α) (l : list α) : head (a::l) = a := rfl
@[simp] theorem tail_nil : tail (@nil α) = [] := rfl
@[simp] theorem tail_cons (a : α) (l : list α) : tail (a::l) = l := rfl
@[simp] theorem head_append [inhabited α] (t : list α) {s : list α} (h : s ≠ []) :
head (s ++ t) = head s :=
by {induction s, contradiction, refl}
theorem head'_append {s t : list α} {x : α} (h : x ∈ s.head') :
x ∈ (s ++ t).head' :=
by { cases s, contradiction, exact h }
theorem tail_append_singleton_of_ne_nil {a : α} {l : list α} (h : l ≠ nil) :
tail (l ++ [a]) = tail l ++ [a] :=
by { induction l, contradiction, rw [tail,cons_append,tail], }
theorem cons_head'_tail : ∀ {l : list α} {a : α} (h : a ∈ head' l), a :: tail l = l
| [] a h := by contradiction
| (b::l) a h := by { simp at h, simp [h] }
theorem head_mem_head' [inhabited α] : ∀ {l : list α} (h : l ≠ []), head l ∈ head' l
| [] h := by contradiction
| (a::l) h := rfl
theorem cons_head_tail [inhabited α] {l : list α} (h : l ≠ []) : (head l)::(tail l) = l :=
cons_head'_tail (head_mem_head' h)
lemma head_mem_self [inhabited α] {l : list α} (h : l ≠ nil) : l.head ∈ l :=
begin
have h' := mem_cons_self l.head l.tail,
rwa cons_head_tail h at h',
end
@[simp] theorem head'_map (f : α → β) (l) : head' (map f l) = (head' l).map f := by cases l; refl
lemma tail_append_of_ne_nil (l l' : list α) (h : l ≠ []) :
(l ++ l').tail = l.tail ++ l' :=
begin
cases l,
{ contradiction },
{ simp }
end
@[simp]
lemma nth_le_tail (l : list α) (i) (h : i < l.tail.length)
(h' : i + 1 < l.length := by simpa [←lt_tsub_iff_right] using h) :
l.tail.nth_le i h = l.nth_le (i + 1) h' :=
begin
cases l,
{ cases h, },
{ simpa }
end
/-! ### Induction from the right -/
/-- Induction principle from the right for lists: if a property holds for the empty list, and
for `l ++ [a]` if it holds for `l`, then it holds for all lists. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
@[elab_as_eliminator] def reverse_rec_on {C : list α → Sort*}
(l : list α) (H0 : C [])
(H1 : ∀ (l : list α) (a : α), C l → C (l ++ [a])) : C l :=
begin
rw ← reverse_reverse l,
induction reverse l,
{ exact H0 },
{ rw reverse_cons, exact H1 _ _ ih }
end
/-- Bidirectional induction principle for lists: if a property holds for the empty list, the
singleton list, and `a :: (l ++ [b])` from `l`, then it holds for all lists. This can be used to
prove statements about palindromes. The principle is given for a `Sort`-valued predicate, i.e., it
can also be used to construct data. -/
def bidirectional_rec {C : list α → Sort*}
(H0 : C []) (H1 : ∀ (a : α), C [a])
(Hn : ∀ (a : α) (l : list α) (b : α), C l → C (a :: (l ++ [b]))) : ∀ l, C l
| [] := H0
| [a] := H1 a
| (a :: b :: l) :=
let l' := init (b :: l), b' := last (b :: l) (cons_ne_nil _ _) in
have length l' < length (a :: b :: l), by { change _ < length l + 2, simp },
begin
rw ←init_append_last (cons_ne_nil b l),
have : C l', from bidirectional_rec l',
exact Hn a l' b' ‹C l'›
end
using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf list.length⟩] }
/-- Like `bidirectional_rec`, but with the list parameter placed first. -/
@[elab_as_eliminator] def bidirectional_rec_on {C : list α → Sort*}
(l : list α) (H0 : C []) (H1 : ∀ (a : α), C [a])
(Hn : ∀ (a : α) (l : list α) (b : α), C l → C (a :: (l ++ [b]))) : C l :=
bidirectional_rec H0 H1 Hn l
/-! ### sublists -/
@[simp] theorem nil_sublist : Π (l : list α), [] <+ l
| [] := sublist.slnil
| (a :: l) := sublist.cons _ _ a (nil_sublist l)
@[refl, simp] theorem sublist.refl : Π (l : list α), l <+ l
| [] := sublist.slnil
| (a :: l) := sublist.cons2 _ _ a (sublist.refl l)
@[trans] theorem sublist.trans {l₁ l₂ l₃ : list α} (h₁ : l₁ <+ l₂) (h₂ : l₂ <+ l₃) : l₁ <+ l₃ :=
sublist.rec_on h₂ (λ_ s, s)
(λl₂ l₃ a h₂ IH l₁ h₁, sublist.cons _ _ _ (IH l₁ h₁))
(λl₂ l₃ a h₂ IH l₁ h₁, @sublist.cases_on _ (λl₁ l₂', l₂' = a :: l₂ → l₁ <+ a :: l₃) _ _ h₁
(λ_, nil_sublist _)
(λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ :=
sublist.cons _ _ _ (IH _ h₁) end)
(λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ :=
sublist.cons2 _ _ _ (IH _ h₁) end) rfl)
l₁ h₁
@[simp] theorem sublist_cons (a : α) (l : list α) : l <+ a::l :=
sublist.cons _ _ _ (sublist.refl l)
theorem sublist_of_cons_sublist {a : α} {l₁ l₂ : list α} : a::l₁ <+ l₂ → l₁ <+ l₂ :=
sublist.trans (sublist_cons a l₁)
theorem sublist.cons_cons {l₁ l₂ : list α} (a : α) (s : l₁ <+ l₂) : a::l₁ <+ a::l₂ :=
sublist.cons2 _ _ _ s
@[simp] theorem sublist_append_left : Π (l₁ l₂ : list α), l₁ <+ l₁++l₂
| [] l₂ := nil_sublist _
| (a::l₁) l₂ := (sublist_append_left l₁ l₂).cons_cons _
@[simp] theorem sublist_append_right : Π (l₁ l₂ : list α), l₂ <+ l₁++l₂
| [] l₂ := sublist.refl _
| (a::l₁) l₂ := sublist.cons _ _ _ (sublist_append_right l₁ l₂)
theorem sublist_cons_of_sublist (a : α) {l₁ l₂ : list α} : l₁ <+ l₂ → l₁ <+ a::l₂ :=
sublist.cons _ _ _
theorem sublist_append_of_sublist_left {l l₁ l₂ : list α} (s : l <+ l₁) : l <+ l₁++l₂ :=
s.trans $ sublist_append_left _ _
theorem sublist_append_of_sublist_right {l l₁ l₂ : list α} (s : l <+ l₂) : l <+ l₁++l₂ :=
s.trans $ sublist_append_right _ _
theorem sublist_of_cons_sublist_cons {l₁ l₂ : list α} : ∀ {a : α}, a::l₁ <+ a::l₂ → l₁ <+ l₂
| ._ (sublist.cons ._ ._ a s) := sublist_of_cons_sublist s
| ._ (sublist.cons2 ._ ._ a s) := s
theorem cons_sublist_cons_iff {l₁ l₂ : list α} {a : α} : a::l₁ <+ a::l₂ ↔ l₁ <+ l₂ :=
⟨sublist_of_cons_sublist_cons, sublist.cons_cons _⟩
@[simp] theorem append_sublist_append_left {l₁ l₂ : list α} : ∀ l, l++l₁ <+ l++l₂ ↔ l₁ <+ l₂
| [] := iff.rfl
| (a::l) := cons_sublist_cons_iff.trans (append_sublist_append_left l)
theorem sublist.append_right {l₁ l₂ : list α} (h : l₁ <+ l₂) (l) : l₁++l <+ l₂++l :=
begin
induction h with _ _ a _ ih _ _ a _ ih,
{ refl },
{ apply sublist_cons_of_sublist a ih },
{ apply ih.cons_cons a }
end
theorem sublist_or_mem_of_sublist {l l₁ l₂ : list α} {a : α} (h : l <+ l₁ ++ a::l₂) :
l <+ l₁ ++ l₂ ∨ a ∈ l :=
begin
induction l₁ with b l₁ IH generalizing l,
{ cases h, { left, exact ‹l <+ l₂› }, { right, apply mem_cons_self } },
{ cases h with _ _ _ h _ _ _ h,
{ exact or.imp_left (sublist_cons_of_sublist _) (IH h) },
{ exact (IH h).imp (sublist.cons_cons _) (mem_cons_of_mem _) } }
end
theorem sublist.reverse {l₁ l₂ : list α} (h : l₁ <+ l₂) : l₁.reverse <+ l₂.reverse :=
begin
induction h with _ _ _ _ ih _ _ a _ ih, {refl},
{ rw reverse_cons, exact sublist_append_of_sublist_left ih },
{ rw [reverse_cons, reverse_cons], exact ih.append_right [a] }
end
@[simp] theorem reverse_sublist_iff {l₁ l₂ : list α} : l₁.reverse <+ l₂.reverse ↔ l₁ <+ l₂ :=
⟨λ h, l₁.reverse_reverse ▸ l₂.reverse_reverse ▸ h.reverse, sublist.reverse⟩
@[simp] theorem append_sublist_append_right {l₁ l₂ : list α} (l) : l₁++l <+ l₂++l ↔ l₁ <+ l₂ :=
⟨λ h, by simpa only [reverse_append, append_sublist_append_left, reverse_sublist_iff]
using h.reverse,
λ h, h.append_right l⟩
theorem sublist.append {l₁ l₂ r₁ r₂ : list α}
(hl : l₁ <+ l₂) (hr : r₁ <+ r₂) : l₁ ++ r₁ <+ l₂ ++ r₂ :=
(hl.append_right _).trans ((append_sublist_append_left _).2 hr)
theorem sublist.subset : Π {l₁ l₂ : list α}, l₁ <+ l₂ → l₁ ⊆ l₂
| ._ ._ sublist.slnil b h := h
| ._ ._ (sublist.cons l₁ l₂ a s) b h := mem_cons_of_mem _ (sublist.subset s h)
| ._ ._ (sublist.cons2 l₁ l₂ a s) b h :=
match eq_or_mem_of_mem_cons h with
| or.inl h := h ▸ mem_cons_self _ _
| or.inr h := mem_cons_of_mem _ (sublist.subset s h)
end
@[simp] theorem singleton_sublist {a : α} {l} : [a] <+ l ↔ a ∈ l :=
⟨λ h, h.subset (mem_singleton_self _), λ h,
let ⟨s, t, e⟩ := mem_split h in e.symm ▸
((nil_sublist _).cons_cons _ ).trans (sublist_append_right _ _)⟩
theorem eq_nil_of_sublist_nil {l : list α} (s : l <+ []) : l = [] :=
eq_nil_of_subset_nil $ s.subset
@[simp] theorem sublist_nil_iff_eq_nil {l : list α} : l <+ [] ↔ l = [] :=
⟨eq_nil_of_sublist_nil, λ H, H ▸ sublist.refl _⟩
@[simp] theorem repeat_sublist_repeat (a : α) {m n} : repeat a m <+ repeat a n ↔ m ≤ n :=
⟨λ h, by simpa only [length_repeat] using length_le_of_sublist h,
λ h, by induction h; [refl, simp only [*, repeat_succ, sublist.cons]] ⟩
theorem eq_of_sublist_of_length_eq : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → length l₁ = length l₂ → l₁ = l₂
| ._ ._ sublist.slnil h := rfl
| ._ ._ (sublist.cons l₁ l₂ a s) h :=
absurd (length_le_of_sublist s) $ not_le_of_gt $ by rw h; apply lt_succ_self
| ._ ._ (sublist.cons2 l₁ l₂ a s) h :=
by rw [length, length] at h; injection h with h; rw eq_of_sublist_of_length_eq s h
theorem eq_of_sublist_of_length_le {l₁ l₂ : list α} (s : l₁ <+ l₂) (h : length l₂ ≤ length l₁) :
l₁ = l₂ :=
eq_of_sublist_of_length_eq s (le_antisymm (length_le_of_sublist s) h)
theorem sublist.antisymm {l₁ l₂ : list α} (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ :=
eq_of_sublist_of_length_le s₁ (length_le_of_sublist s₂)
instance decidable_sublist [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+ l₂)
| [] l₂ := is_true $ nil_sublist _
| (a::l₁) [] := is_false $ λh, list.no_confusion $ eq_nil_of_sublist_nil h
| (a::l₁) (b::l₂) :=
if h : a = b then
decidable_of_decidable_of_iff (decidable_sublist l₁ l₂) $
by rw [← h]; exact ⟨sublist.cons_cons _, sublist_of_cons_sublist_cons⟩
else decidable_of_decidable_of_iff (decidable_sublist (a::l₁) l₂)
⟨sublist_cons_of_sublist _, λs, match a, l₁, s, h with
| a, l₁, sublist.cons ._ ._ ._ s', h := s'
| ._, ._, sublist.cons2 t ._ ._ s', h := absurd rfl h
end⟩
/-! ### index_of -/
section index_of
variable [decidable_eq α]
@[simp] theorem index_of_nil (a : α) : index_of a [] = 0 := rfl
theorem index_of_cons (a b : α) (l : list α) :
index_of a (b::l) = if a = b then 0 else succ (index_of a l) := rfl
theorem index_of_cons_eq {a b : α} (l : list α) : a = b → index_of a (b::l) = 0 :=
assume e, if_pos e
@[simp] theorem index_of_cons_self (a : α) (l : list α) : index_of a (a::l) = 0 :=
index_of_cons_eq _ rfl
@[simp, priority 990]
theorem index_of_cons_ne {a b : α} (l : list α) : a ≠ b → index_of a (b::l) = succ (index_of a l) :=
assume n, if_neg n
theorem index_of_eq_length {a : α} {l : list α} : index_of a l = length l ↔ a ∉ l :=
begin
induction l with b l ih,
{ exact iff_of_true rfl (not_mem_nil _) },
simp only [length, mem_cons_iff, index_of_cons], split_ifs,
{ exact iff_of_false (by rintro ⟨⟩) (λ H, H $ or.inl h) },
{ simp only [h, false_or], rw ← ih, exact succ_inj' }
end
@[simp, priority 980]
theorem index_of_of_not_mem {l : list α} {a : α} : a ∉ l → index_of a l = length l :=
index_of_eq_length.2
theorem index_of_le_length {a : α} {l : list α} : index_of a l ≤ length l :=
begin
induction l with b l ih, {refl},
simp only [length, index_of_cons],
by_cases h : a = b, {rw if_pos h, exact nat.zero_le _},
rw if_neg h, exact succ_le_succ ih
end
theorem index_of_lt_length {a} {l : list α} : index_of a l < length l ↔ a ∈ l :=
⟨λh, decidable.by_contradiction $ λ al, ne_of_lt h $ index_of_eq_length.2 al,
λal, lt_of_le_of_ne index_of_le_length $ λ h, index_of_eq_length.1 h al⟩
end index_of
/-! ### nth element -/
theorem nth_le_of_mem : ∀ {a} {l : list α}, a ∈ l → ∃ n h, nth_le l n h = a
| a (_ :: l) (or.inl rfl) := ⟨0, succ_pos _, rfl⟩
| a (b :: l) (or.inr m) :=
let ⟨n, h, e⟩ := nth_le_of_mem m in ⟨n+1, succ_lt_succ h, e⟩
theorem nth_le_nth : ∀ {l : list α} {n} h, nth l n = some (nth_le l n h)
| (a :: l) 0 h := rfl
| (a :: l) (n+1) h := @nth_le_nth l n _
theorem nth_len_le : ∀ {l : list α} {n}, length l ≤ n → nth l n = none
| [] n h := rfl
| (a :: l) (n+1) h := nth_len_le (le_of_succ_le_succ h)
theorem nth_eq_some {l : list α} {n a} : nth l n = some a ↔ ∃ h, nth_le l n h = a :=
⟨λ e,
have h : n < length l, from lt_of_not_ge $ λ hn,
by rw nth_len_le hn at e; contradiction,
⟨h, by rw nth_le_nth h at e;
injection e with e; apply nth_le_mem⟩,
λ ⟨h, e⟩, e ▸ nth_le_nth _⟩
@[simp]
theorem nth_eq_none_iff : ∀ {l : list α} {n}, nth l n = none ↔ length l ≤ n :=
begin
intros, split,
{ intro h, by_contradiction h',
have h₂ : ∃ h, l.nth_le n h = l.nth_le n (lt_of_not_ge h') := ⟨lt_of_not_ge h', rfl⟩,
rw [← nth_eq_some, h] at h₂, cases h₂ },
{ solve_by_elim [nth_len_le] },
end
theorem nth_of_mem {a} {l : list α} (h : a ∈ l) : ∃ n, nth l n = some a :=
let ⟨n, h, e⟩ := nth_le_of_mem h in ⟨n, by rw [nth_le_nth, e]⟩
theorem nth_le_mem : ∀ (l : list α) n h, nth_le l n h ∈ l
| (a :: l) 0 h := mem_cons_self _ _
| (a :: l) (n+1) h := mem_cons_of_mem _ (nth_le_mem l _ _)
theorem nth_mem {l : list α} {n a} (e : nth l n = some a) : a ∈ l :=
let ⟨h, e⟩ := nth_eq_some.1 e in e ▸ nth_le_mem _ _ _
theorem mem_iff_nth_le {a} {l : list α} : a ∈ l ↔ ∃ n h, nth_le l n h = a :=
⟨nth_le_of_mem, λ ⟨n, h, e⟩, e ▸ nth_le_mem _ _ _⟩
theorem mem_iff_nth {a} {l : list α} : a ∈ l ↔ ∃ n, nth l n = some a :=
mem_iff_nth_le.trans $ exists_congr $ λ n, nth_eq_some.symm
lemma nth_zero (l : list α) : l.nth 0 = l.head' := by cases l; refl
lemma nth_injective {α : Type u} {xs : list α} {i j : ℕ}
(h₀ : i < xs.length)
(h₁ : nodup xs)
(h₂ : xs.nth i = xs.nth j) : i = j :=
begin
induction xs with x xs generalizing i j,
{ cases h₀ },
{ cases i; cases j,
case nat.zero nat.zero
{ refl },
case nat.succ nat.succ
{ congr, cases h₁,
apply xs_ih;
solve_by_elim [lt_of_succ_lt_succ] },
iterate 2
{ dsimp at h₂,
cases h₁ with _ _ h h',
cases h x _ rfl,
rw mem_iff_nth,
exact ⟨_, h₂.symm⟩ <|>
exact ⟨_, h₂⟩ } },
end
@[simp] theorem nth_map (f : α → β) : ∀ l n, nth (map f l) n = (nth l n).map f
| [] n := rfl
| (a :: l) 0 := rfl
| (a :: l) (n+1) := nth_map l n
theorem nth_le_map (f : α → β) {l n} (H1 H2) : nth_le (map f l) n H1 = f (nth_le l n H2) :=
option.some.inj $ by rw [← nth_le_nth, nth_map, nth_le_nth]; refl
/-- A version of `nth_le_map` that can be used for rewriting. -/
theorem nth_le_map_rev (f : α → β) {l n} (H) :
f (nth_le l n H) = nth_le (map f l) n ((length_map f l).symm ▸ H) :=
(nth_le_map f _ _).symm
@[simp] theorem nth_le_map' (f : α → β) {l n} (H) :
nth_le (map f l) n H = f (nth_le l n (length_map f l ▸ H)) :=
nth_le_map f _ _
/-- If one has `nth_le L i hi` in a formula and `h : L = L'`, one can not `rw h` in the formula as
`hi` gives `i < L.length` and not `i < L'.length`. The lemma `nth_le_of_eq` can be used to make
such a rewrite, with `rw (nth_le_of_eq h)`. -/
lemma nth_le_of_eq {L L' : list α} (h : L = L') {i : ℕ} (hi : i < L.length) :
nth_le L i hi = nth_le L' i (h ▸ hi) :=
by { congr, exact h}
@[simp] lemma nth_le_singleton (a : α) {n : ℕ} (hn : n < 1) :
nth_le [a] n hn = a :=
have hn0 : n = 0 := le_zero_iff.1 (le_of_lt_succ hn),
by subst hn0; refl
lemma nth_le_zero [inhabited α] {L : list α} (h : 0 < L.length) :
L.nth_le 0 h = L.head :=
by { cases L, cases h, simp, }
lemma nth_le_append : ∀ {l₁ l₂ : list α} {n : ℕ} (hn₁) (hn₂),
(l₁ ++ l₂).nth_le n hn₁ = l₁.nth_le n hn₂
| [] _ n hn₁ hn₂ := (nat.not_lt_zero _ hn₂).elim
| (a::l) _ 0 hn₁ hn₂ := rfl
| (a::l) _ (n+1) hn₁ hn₂ := by simp only [nth_le, cons_append];
exact nth_le_append _ _
lemma nth_le_append_right_aux {l₁ l₂ : list α} {n : ℕ}
(h₁ : l₁.length ≤ n) (h₂ : n < (l₁ ++ l₂).length) : n - l₁.length < l₂.length :=
begin
rw list.length_append at h₂,
convert (tsub_lt_tsub_iff_right h₁).mpr h₂,
simp,
end
lemma nth_le_append_right : ∀ {l₁ l₂ : list α} {n : ℕ} (h₁ : l₁.length ≤ n) (h₂),
(l₁ ++ l₂).nth_le n h₂ = l₂.nth_le (n - l₁.length) (nth_le_append_right_aux h₁ h₂)
| [] _ n h₁ h₂ := rfl
| (a :: l) _ (n+1) h₁ h₂ :=
begin
dsimp,
conv { to_rhs, congr, skip,
rw [tsub_add_eq_tsub_tsub, tsub_right_comm, add_tsub_cancel_right], },
rw nth_le_append_right (nat.lt_succ_iff.mp h₁),
end
@[simp] lemma nth_le_repeat (a : α) {n m : ℕ} (h : m < (list.repeat a n).length) :
(list.repeat a n).nth_le m h = a :=
eq_of_mem_repeat (nth_le_mem _ _ _)
lemma nth_append {l₁ l₂ : list α} {n : ℕ} (hn : n < l₁.length) :
(l₁ ++ l₂).nth n = l₁.nth n :=
have hn' : n < (l₁ ++ l₂).length := lt_of_lt_of_le hn
(by rw length_append; exact nat.le_add_right _ _),
by rw [nth_le_nth hn, nth_le_nth hn', nth_le_append]
lemma nth_append_right {l₁ l₂ : list α} {n : ℕ} (hn : l₁.length ≤ n) :
(l₁ ++ l₂).nth n = l₂.nth (n - l₁.length) :=
begin
by_cases hl : n < (l₁ ++ l₂).length,
{ rw [nth_le_nth hl, nth_le_nth, nth_le_append_right hn] },
{ rw [nth_len_le (le_of_not_lt hl), nth_len_le],
rw [not_lt, length_append] at hl,
exact le_tsub_of_add_le_left hl }
end
lemma last_eq_nth_le : ∀ (l : list α) (h : l ≠ []),
last l h = l.nth_le (l.length - 1) (nat.sub_lt (length_pos_of_ne_nil h) one_pos)
| [] h := rfl
| [a] h := by rw [last_singleton, nth_le_singleton]
| (a :: b :: l) h := by { rw [last_cons, last_eq_nth_le (b :: l)],
refl, exact cons_ne_nil b l }
@[simp] lemma nth_concat_length : ∀ (l : list α) (a : α), (l ++ [a]).nth l.length = some a
| [] a := rfl
| (b::l) a := by rw [cons_append, length_cons, nth, nth_concat_length]
lemma nth_le_cons_length (x : α) (xs : list α) (n : ℕ) (h : n = xs.length) :
(x :: xs).nth_le n (by simp [h]) = (x :: xs).last (cons_ne_nil x xs) :=
begin
rw last_eq_nth_le,
congr,
simp [h]
end
@[ext]
theorem ext : ∀ {l₁ l₂ : list α}, (∀n, nth l₁ n = nth l₂ n) → l₁ = l₂
| [] [] h := rfl
| (a::l₁) [] h := by have h0 := h 0; contradiction
| [] (a'::l₂) h := by have h0 := h 0; contradiction
| (a::l₁) (a'::l₂) h := by have h0 : some a = some a' := h 0; injection h0 with aa;
simp only [aa, ext (λn, h (n+1))]; split; refl
theorem ext_le {l₁ l₂ : list α} (hl : length l₁ = length l₂)
(h : ∀n h₁ h₂, nth_le l₁ n h₁ = nth_le l₂ n h₂) : l₁ = l₂ :=
ext $ λn, if h₁ : n < length l₁
then by rw [nth_le_nth, nth_le_nth, h n h₁ (by rwa [← hl])]
else let h₁ := le_of_not_gt h₁ in by { rw [nth_len_le h₁, nth_len_le], rwa [←hl], }
@[simp] theorem index_of_nth_le [decidable_eq α] {a : α} :
∀ {l : list α} h, nth_le l (index_of a l) h = a
| (b::l) h := by by_cases h' : a = b;
simp only [h', if_pos, if_false, index_of_cons, nth_le, @index_of_nth_le l]
@[simp] theorem index_of_nth [decidable_eq α] {a : α} {l : list α} (h : a ∈ l) :
nth l (index_of a l) = some a :=
by rw [nth_le_nth, index_of_nth_le (index_of_lt_length.2 h)]
theorem nth_le_reverse_aux1 :
∀ (l r : list α) (i h1 h2), nth_le (reverse_core l r) (i + length l) h1 = nth_le r i h2
| [] r i := λh1 h2, rfl
| (a :: l) r i :=
by rw (show i + length (a :: l) = i + 1 + length l, from add_right_comm i (length l) 1);
exact λh1 h2, nth_le_reverse_aux1 l (a :: r) (i+1) h1 (succ_lt_succ h2)
lemma index_of_inj [decidable_eq α] {l : list α} {x y : α}
(hx : x ∈ l) (hy : y ∈ l) : index_of x l = index_of y l ↔ x = y :=
⟨λ h, have nth_le l (index_of x l) (index_of_lt_length.2 hx) =
nth_le l (index_of y l) (index_of_lt_length.2 hy),
by simp only [h],
by simpa only [index_of_nth_le],
λ h, by subst h⟩
theorem nth_le_reverse_aux2 : ∀ (l r : list α) (i : nat) (h1) (h2),
nth_le (reverse_core l r) (length l - 1 - i) h1 = nth_le l i h2
| [] r i h1 h2 := absurd h2 (nat.not_lt_zero _)
| (a :: l) r 0 h1 h2 := begin
have aux := nth_le_reverse_aux1 l (a :: r) 0,
rw zero_add at aux,
exact aux _ (zero_lt_succ _)
end
| (a :: l) r (i+1) h1 h2 := begin
have aux := nth_le_reverse_aux2 l (a :: r) i,
have heq := calc length (a :: l) - 1 - (i + 1)
= length l - (1 + i) : by rw add_comm; refl
... = length l - 1 - i : by rw ← tsub_add_eq_tsub_tsub,
rw [← heq] at aux,
apply aux
end
@[simp] theorem nth_le_reverse (l : list α) (i : nat) (h1 h2) :
nth_le (reverse l) (length l - 1 - i) h1 = nth_le l i h2 :=
nth_le_reverse_aux2 _ _ _ _ _
lemma nth_le_reverse' (l : list α) (n : ℕ) (hn : n < l.reverse.length) (hn') :
l.reverse.nth_le n hn = l.nth_le (l.length - 1 - n) hn' :=
begin
rw eq_comm,
convert nth_le_reverse l.reverse _ _ _ using 1,
{ simp },
{ simpa }
end
lemma eq_cons_of_length_one {l : list α} (h : l.length = 1) :
l = [l.nth_le 0 (h.symm ▸ zero_lt_one)] :=
begin
refine ext_le (by convert h) (λ n h₁ h₂, _),
simp only [nth_le_singleton],
congr,
exact eq_bot_iff.mpr (nat.lt_succ_iff.mp h₂)
end
lemma modify_nth_tail_modify_nth_tail {f g : list α → list α} (m : ℕ) :
∀n (l:list α), (l.modify_nth_tail f n).modify_nth_tail g (m + n) =
l.modify_nth_tail (λl, (f l).modify_nth_tail g m) n
| 0 l := rfl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_modify_nth_tail n l)
lemma modify_nth_tail_modify_nth_tail_le
{f g : list α → list α} (m n : ℕ) (l : list α) (h : n ≤ m) :
(l.modify_nth_tail f n).modify_nth_tail g m =
l.modify_nth_tail (λl, (f l).modify_nth_tail g (m - n)) n :=
begin
rcases le_iff_exists_add.1 h with ⟨m, rfl⟩,
rw [add_tsub_cancel_left, add_comm, modify_nth_tail_modify_nth_tail]
end
lemma modify_nth_tail_modify_nth_tail_same {f g : list α → list α} (n : ℕ) (l:list α) :
(l.modify_nth_tail f n).modify_nth_tail g n = l.modify_nth_tail (g ∘ f) n :=
by rw [modify_nth_tail_modify_nth_tail_le n n l (le_refl n), tsub_self]; refl
lemma modify_nth_tail_id :
∀n (l:list α), l.modify_nth_tail id n = l
| 0 l := rfl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_id n l)
theorem remove_nth_eq_nth_tail : ∀ n (l : list α), remove_nth l n = modify_nth_tail tail n l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (cons _) (remove_nth_eq_nth_tail _ _)
theorem update_nth_eq_modify_nth (a : α) : ∀ n (l : list α),
update_nth l n a = modify_nth (λ _, a) n l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (b::l) := congr_arg (cons _) (update_nth_eq_modify_nth _ _)
theorem modify_nth_eq_update_nth (f : α → α) : ∀ n (l : list α),
modify_nth f n l = ((λ a, update_nth l n (f a)) <$> nth l n).get_or_else l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (b::l) := (congr_arg (cons b)
(modify_nth_eq_update_nth n l)).trans $ by cases nth l n; refl
theorem nth_modify_nth (f : α → α) : ∀ n (l : list α) m,
nth (modify_nth f n l) m = (λ a, if n = m then f a else a) <$> nth l m
| n l 0 := by cases l; cases n; refl
| n [] (m+1) := by cases n; refl
| 0 (a::l) (m+1) := by cases nth l m; refl
| (n+1) (a::l) (m+1) := (nth_modify_nth n l m).trans $
by cases nth l m with b; by_cases n = m;
simp only [h, if_pos, if_true, if_false, option.map_none, option.map_some, mt succ.inj,
not_false_iff]
theorem modify_nth_tail_length (f : list α → list α) (H : ∀ l, length (f l) = length l) :
∀ n l, length (modify_nth_tail f n l) = length l
| 0 l := H _
| (n+1) [] := rfl
| (n+1) (a::l) := @congr_arg _ _ _ _ (+1) (modify_nth_tail_length _ _)
@[simp] theorem modify_nth_length (f : α → α) :
∀ n l, length (modify_nth f n l) = length l :=
modify_nth_tail_length _ (λ l, by cases l; refl)
@[simp] theorem update_nth_length (l : list α) (n) (a : α) :
length (update_nth l n a) = length l :=
by simp only [update_nth_eq_modify_nth, modify_nth_length]
@[simp] theorem nth_modify_nth_eq (f : α → α) (n) (l : list α) :
nth (modify_nth f n l) n = f <$> nth l n :=
by simp only [nth_modify_nth, if_pos]
@[simp] theorem nth_modify_nth_ne (f : α → α) {m n} (l : list α) (h : m ≠ n) :
nth (modify_nth f m l) n = nth l n :=
by simp only [nth_modify_nth, if_neg h, id_map']
theorem nth_update_nth_eq (a : α) (n) (l : list α) :
nth (update_nth l n a) n = (λ _, a) <$> nth l n :=
by simp only [update_nth_eq_modify_nth, nth_modify_nth_eq]
theorem nth_update_nth_of_lt (a : α) {n} {l : list α} (h : n < length l) :
nth (update_nth l n a) n = some a :=
by rw [nth_update_nth_eq, nth_le_nth h]; refl
theorem nth_update_nth_ne (a : α) {m n} (l : list α) (h : m ≠ n) :
nth (update_nth l m a) n = nth l n :=
by simp only [update_nth_eq_modify_nth, nth_modify_nth_ne _ _ h]
@[simp] lemma update_nth_nil (n : ℕ) (a : α) : [].update_nth n a = [] := rfl
@[simp] lemma update_nth_succ (x : α) (xs : list α) (n : ℕ) (a : α) :
(x :: xs).update_nth n.succ a = x :: xs.update_nth n a := rfl
lemma update_nth_comm (a b : α) : Π {n m : ℕ} (l : list α) (h : n ≠ m),
(l.update_nth n a).update_nth m b = (l.update_nth m b).update_nth n a
| _ _ [] _ := by simp
| 0 0 (x :: t) h := absurd rfl h
| (n + 1) 0 (x :: t) h := by simp [list.update_nth]
| 0 (m + 1) (x :: t) h := by simp [list.update_nth]
| (n + 1) (m + 1) (x :: t) h := by { simp only [update_nth, true_and, eq_self_iff_true],
exact update_nth_comm t (λ h', h $ nat.succ_inj'.mpr h'), }
@[simp] lemma nth_le_update_nth_eq (l : list α) (i : ℕ) (a : α)
(h : i < (l.update_nth i a).length) : (l.update_nth i a).nth_le i h = a :=
by rw [← option.some_inj, ← nth_le_nth, nth_update_nth_eq, nth_le_nth]; simp * at *
@[simp] lemma nth_le_update_nth_of_ne {l : list α} {i j : ℕ} (h : i ≠ j) (a : α)
(hj : j < (l.update_nth i a).length) :
(l.update_nth i a).nth_le j hj = l.nth_le j (by simpa using hj) :=
by rw [← option.some_inj, ← list.nth_le_nth, list.nth_update_nth_ne _ _ h, list.nth_le_nth]
lemma mem_or_eq_of_mem_update_nth : ∀ {l : list α} {n : ℕ} {a b : α}
(h : a ∈ l.update_nth n b), a ∈ l ∨ a = b
| [] n a b h := false.elim h
| (c::l) 0 a b h := ((mem_cons_iff _ _ _).1 h).elim
or.inr (or.inl ∘ mem_cons_of_mem _)
| (c::l) (n+1) a b h := ((mem_cons_iff _ _ _).1 h).elim
(λ h, h ▸ or.inl (mem_cons_self _ _))
(λ h, (mem_or_eq_of_mem_update_nth h).elim
(or.inl ∘ mem_cons_of_mem _) or.inr)
section insert_nth
variable {a : α}
@[simp] lemma insert_nth_zero (s : list α) (x : α) : insert_nth 0 x s = x :: s := rfl
@[simp] lemma insert_nth_succ_nil (n : ℕ) (a : α) : insert_nth (n + 1) a [] = [] := rfl
@[simp] lemma insert_nth_succ_cons (s : list α) (hd x : α) (n : ℕ) :
insert_nth (n + 1) x (hd :: s) = hd :: (insert_nth n x s) := rfl
lemma length_insert_nth : ∀n as, n ≤ length as → length (insert_nth n a as) = length as + 1
| 0 as h := rfl
| (n+1) [] h := (nat.not_succ_le_zero _ h).elim
| (n+1) (a'::as) h := congr_arg nat.succ $ length_insert_nth n as (nat.le_of_succ_le_succ h)
lemma remove_nth_insert_nth (n:ℕ) (l : list α) : (l.insert_nth n a).remove_nth n = l :=
by rw [remove_nth_eq_nth_tail, insert_nth, modify_nth_tail_modify_nth_tail_same];
from modify_nth_tail_id _ _
lemma insert_nth_remove_nth_of_ge : ∀n m as, n < length as → n ≤ m →
insert_nth m a (as.remove_nth n) = (as.insert_nth (m + 1) a).remove_nth n
| 0 0 [] has _ := (lt_irrefl _ has).elim
| 0 0 (a::as) has hmn := by simp [remove_nth, insert_nth]
| 0 (m+1) (a::as) has hmn := rfl
| (n+1) (m+1) (a::as) has hmn :=
congr_arg (cons a) $
insert_nth_remove_nth_of_ge n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn)
lemma insert_nth_remove_nth_of_le : ∀n m as, n < length as → m ≤ n →
insert_nth m a (as.remove_nth n) = (as.insert_nth m a).remove_nth (n + 1)
| n 0 (a :: as) has hmn := rfl
| (n + 1) (m + 1) (a :: as) has hmn :=
congr_arg (cons a) $
insert_nth_remove_nth_of_le n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn)
lemma insert_nth_comm (a b : α) :
∀(i j : ℕ) (l : list α) (h : i ≤ j) (hj : j ≤ length l),
(l.insert_nth i a).insert_nth (j + 1) b = (l.insert_nth j b).insert_nth i a
| 0 j l := by simp [insert_nth]
| (i + 1) 0 l := assume h, (nat.not_lt_zero _ h).elim
| (i + 1) (j+1) [] := by simp
| (i + 1) (j+1) (c::l) :=
assume h₀ h₁,
by simp [insert_nth];
exact insert_nth_comm i j l (nat.le_of_succ_le_succ h₀) (nat.le_of_succ_le_succ h₁)
lemma mem_insert_nth {a b : α} : ∀ {n : ℕ} {l : list α} (hi : n ≤ l.length),
a ∈ l.insert_nth n b ↔ a = b ∨ a ∈ l
| 0 as h := iff.rfl
| (n+1) [] h := (nat.not_succ_le_zero _ h).elim
| (n+1) (a'::as) h := begin
dsimp [list.insert_nth],
erw [list.mem_cons_iff, mem_insert_nth (nat.le_of_succ_le_succ h), list.mem_cons_iff,
← or.assoc, or_comm (a = a'), or.assoc]
end
lemma inj_on_insert_nth_index_of_not_mem (l : list α) (x : α) (hx : x ∉ l) :
set.inj_on (λ k, insert_nth k x l) {n | n ≤ l.length} :=
begin
induction l with hd tl IH,
{ intros n hn m hm h,
simp only [set.mem_singleton_iff, set.set_of_eq_eq_singleton, length, nonpos_iff_eq_zero]
at hn hm,
simp [hn, hm] },
{ intros n hn m hm h,
simp only [length, set.mem_set_of_eq] at hn hm,
simp only [mem_cons_iff, not_or_distrib] at hx,
cases n;
cases m,
{ refl },
{ simpa [hx.left] using h },
{ simpa [ne.symm hx.left] using h },
{ simp only [true_and, eq_self_iff_true, insert_nth_succ_cons] at h,
rw nat.succ_inj',
refine IH hx.right _ _ h,
{ simpa [nat.succ_le_succ_iff] using hn },
{ simpa [nat.succ_le_succ_iff] using hm } } }
end
lemma insert_nth_of_length_lt (l : list α) (x : α) (n : ℕ) (h : l.length < n) :
insert_nth n x l = l :=
begin
induction l with hd tl IH generalizing n,
{ cases n,
{ simpa using h },
{ simp } },
{ cases n,
{ simpa using h },
{ simp only [nat.succ_lt_succ_iff, length] at h,
simpa using IH _ h } }
end
@[simp] lemma insert_nth_length_self (l : list α) (x : α) :
insert_nth l.length x l = l ++ [x] :=
begin
induction l with hd tl IH,
{ simp },
{ simpa using IH }
end
lemma length_le_length_insert_nth (l : list α) (x : α) (n : ℕ) :
l.length ≤ (insert_nth n x l).length :=
begin
cases le_or_lt n l.length with hn hn,
{ rw length_insert_nth _ _ hn,
exact (nat.lt_succ_self _).le },
{ rw insert_nth_of_length_lt _ _ _ hn }
end
lemma length_insert_nth_le_succ (l : list α) (x : α) (n : ℕ) :
(insert_nth n x l).length ≤ l.length + 1 :=
begin
cases le_or_lt n l.length with hn hn,
{ rw length_insert_nth _ _ hn },
{ rw insert_nth_of_length_lt _ _ _ hn,
exact (nat.lt_succ_self _).le }
end
lemma nth_le_insert_nth_of_lt (l : list α) (x : α) (n k : ℕ) (hn : k < n)
(hk : k < l.length)
(hk' : k < (insert_nth n x l).length := hk.trans_le (length_le_length_insert_nth _ _ _)):
(insert_nth n x l).nth_le k hk' = l.nth_le k hk :=
begin
induction n with n IH generalizing k l,
{ simpa using hn },
{ cases l with hd tl,
{ simp },
{ cases k,
{ simp },
{ rw nat.succ_lt_succ_iff at hn,
simpa using IH _ _ hn _ } } }
end
@[simp] lemma nth_le_insert_nth_self (l : list α) (x : α) (n : ℕ)
(hn : n ≤ l.length) (hn' : n < (insert_nth n x l).length :=
by rwa [length_insert_nth _ _ hn, nat.lt_succ_iff]) :
(insert_nth n x l).nth_le n hn' = x :=
begin
induction l with hd tl IH generalizing n,
{ simp only [length, nonpos_iff_eq_zero] at hn,
simp [hn] },
{ cases n,
{ simp },
{ simp only [nat.succ_le_succ_iff, length] at hn,
simpa using IH _ hn } }
end
lemma nth_le_insert_nth_add_succ (l : list α) (x : α) (n k : ℕ)
(hk' : n + k < l.length)
(hk : n + k + 1 < (insert_nth n x l).length :=
by rwa [length_insert_nth _ _ (le_self_add.trans hk'.le), nat.succ_lt_succ_iff]) :
(insert_nth n x l).nth_le (n + k + 1) hk = nth_le l (n + k) hk' :=
begin
induction l with hd tl IH generalizing n k,
{ simpa using hk' },
{ cases n,
{ simpa },
{ simpa [succ_add] using IH _ _ _ } }
end
lemma insert_nth_injective (n : ℕ) (x : α) : function.injective (insert_nth n x) :=
begin
induction n with n IH,
{ have : insert_nth 0 x = cons x := funext (λ _, rfl),
simp [this] },
{ rintros (_|⟨a, as⟩) (_|⟨b, bs⟩) h;
simpa [IH.eq_iff] using h <|> refl }
end
end insert_nth
/-! ### map -/
@[simp] lemma map_nil (f : α → β) : map f [] = [] := rfl
theorem map_eq_foldr (f : α → β) (l : list α) :
map f l = foldr (λ a bs, f a :: bs) [] l :=
by induction l; simp *
lemma map_congr {f g : α → β} : ∀ {l : list α}, (∀ x ∈ l, f x = g x) → map f l = map g l
| [] _ := rfl
| (a::l) h := let ⟨h₁, h₂⟩ := forall_mem_cons.1 h in
by rw [map, map, h₁, map_congr h₂]
lemma map_eq_map_iff {f g : α → β} {l : list α} : map f l = map g l ↔ (∀ x ∈ l, f x = g x) :=
begin
refine ⟨_, map_congr⟩, intros h x hx,
rw [mem_iff_nth_le] at hx, rcases hx with ⟨n, hn, rfl⟩,
rw [nth_le_map_rev f, nth_le_map_rev g], congr, exact h
end
theorem map_concat (f : α → β) (a : α) (l : list α) : map f (concat l a) = concat (map f l) (f a) :=
by induction l; [refl, simp only [*, concat_eq_append, cons_append, map, map_append]]; split; refl
theorem map_id' {f : α → α} (h : ∀ x, f x = x) (l : list α) : map f l = l :=
by induction l; [refl, simp only [*, map]]; split; refl
theorem eq_nil_of_map_eq_nil {f : α → β} {l : list α} (h : map f l = nil) : l = nil :=
eq_nil_of_length_eq_zero $ by rw [← length_map f l, h]; refl
@[simp] theorem map_join (f : α → β) (L : list (list α)) :
map f (join L) = join (map (map f) L) :=
by induction L; [refl, simp only [*, join, map, map_append]]
theorem bind_ret_eq_map (f : α → β) (l : list α) :
l.bind (list.ret ∘ f) = map f l :=
by unfold list.bind; induction l; simp only [map, join, list.ret, cons_append, nil_append, *];
split; refl
@[simp] theorem map_eq_map {α β} (f : α → β) (l : list α) : f <$> l = map f l := rfl
@[simp] theorem map_tail (f : α → β) (l) : map f (tail l) = tail (map f l) :=
by cases l; refl
@[simp] theorem map_injective_iff {f : α → β} : injective (map f) ↔ injective f :=
begin
split; intros h x y hxy,
{ suffices : [x] = [y], { simpa using this }, apply h, simp [hxy] },
{ induction y generalizing x, simpa using hxy,
cases x, simpa using hxy, simp at hxy, simp [y_ih hxy.2, h hxy.1] }
end
/--
A single `list.map` of a composition of functions is equal to
composing a `list.map` with another `list.map`, fully applied.
This is the reverse direction of `list.map_map`.
-/
lemma comp_map (h : β → γ) (g : α → β) (l : list α) :
map (h ∘ g) l = map h (map g l) := (map_map _ _ _).symm
/--
Composing a `list.map` with another `list.map` is equal to
a single `list.map` of composed functions.
-/
@[simp] lemma map_comp_map (g : β → γ) (f : α → β) :
map g ∘ map f = map (g ∘ f) :=
by { ext l, rw comp_map }
theorem map_filter_eq_foldr (f : α → β) (p : α → Prop) [decidable_pred p] (as : list α) :
map f (filter p as) = foldr (λ a bs, if p a then f a :: bs else bs) [] as :=
by { induction as, { refl }, { simp! [*, apply_ite (map f)] } }
lemma last_map (f : α → β) {l : list α} (hl : l ≠ []) :
(l.map f).last (mt eq_nil_of_map_eq_nil hl) = f (l.last hl) :=
begin
induction l with l_ih l_tl l_ih,
{ apply (hl rfl).elim },
{ cases l_tl,
{ simp },
{ simpa using l_ih } }
end
/-! ### map₂ -/
theorem nil_map₂ (f : α → β → γ) (l : list β) : map₂ f [] l = [] :=
by cases l; refl
theorem map₂_nil (f : α → β → γ) (l : list α) : map₂ f l [] = [] :=
by cases l; refl
@[simp] theorem map₂_flip (f : α → β → γ) :
∀ as bs, map₂ (flip f) bs as = map₂ f as bs
| [] [] := rfl
| [] (b :: bs) := rfl
| (a :: as) [] := rfl
| (a :: as) (b :: bs) := by { simp! [map₂_flip], refl }
/-! ### take, drop -/
@[simp] theorem take_zero (l : list α) : take 0 l = [] := rfl
@[simp] theorem take_nil : ∀ n, take n [] = ([] : list α)
| 0 := rfl
| (n+1) := rfl
theorem take_cons (n) (a : α) (l : list α) : take (succ n) (a::l) = a :: take n l := rfl
@[simp] theorem take_length : ∀ (l : list α), take (length l) l = l
| [] := rfl
| (a::l) := begin change a :: (take (length l) l) = a :: l, rw take_length end
theorem take_all_of_le : ∀ {n} {l : list α}, length l ≤ n → take n l = l
| 0 [] h := rfl
| 0 (a::l) h := absurd h (not_le_of_gt (zero_lt_succ _))
| (n+1) [] h := rfl
| (n+1) (a::l) h :=
begin
change a :: take n l = a :: l,
rw [take_all_of_le (le_of_succ_le_succ h)]
end
@[simp] theorem take_left : ∀ l₁ l₂ : list α, take (length l₁) (l₁ ++ l₂) = l₁
| [] l₂ := rfl
| (a::l₁) l₂ := congr_arg (cons a) (take_left l₁ l₂)
theorem take_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) :
take n (l₁ ++ l₂) = l₁ :=
by rw ← h; apply take_left
theorem take_take : ∀ (n m) (l : list α), take n (take m l) = take (min n m) l
| n 0 l := by rw [min_zero, take_zero, take_nil]
| 0 m l := by rw [zero_min, take_zero, take_zero]
| (succ n) (succ m) nil := by simp only [take_nil]
| (succ n) (succ m) (a::l) := by simp only [take, min_succ_succ, take_take n m l]; split; refl
theorem take_repeat (a : α) : ∀ (n m : ℕ), take n (repeat a m) = repeat a (min n m)
| n 0 := by simp
| 0 m := by simp
| (succ n) (succ m) := by simp [min_succ_succ, take_repeat]
lemma map_take {α β : Type*} (f : α → β) :
∀ (L : list α) (i : ℕ), (L.take i).map f = (L.map f).take i
| [] i := by simp
| L 0 := by simp
| (h :: t) (n+1) := by { dsimp, rw [map_take], }
/-- Taking the first `n` elements in `l₁ ++ l₂` is the same as appending the first `n` elements
of `l₁` to the first `n - l₁.length` elements of `l₂`. -/
lemma take_append_eq_append_take {l₁ l₂ : list α} {n : ℕ} :
take n (l₁ ++ l₂) = take n l₁ ++ take (n - l₁.length) l₂ :=
begin
induction l₁ generalizing n, { simp },
cases n, { simp }, simp *
end
lemma take_append_of_le_length {l₁ l₂ : list α} {n : ℕ} (h : n ≤ l₁.length) :
(l₁ ++ l₂).take n = l₁.take n :=
by simp [take_append_eq_append_take, tsub_eq_zero_iff_le.mpr h]
/-- Taking the first `l₁.length + i` elements in `l₁ ++ l₂` is the same as appending the first
`i` elements of `l₂` to `l₁`. -/
lemma take_append {l₁ l₂ : list α} (i : ℕ) :
take (l₁.length + i) (l₁ ++ l₂) = l₁ ++ (take i l₂) :=
by simp [take_append_eq_append_take, take_all_of_le le_self_add]
/-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of
length `> i`. Version designed to rewrite from the big list to the small list. -/
lemma nth_le_take (L : list α) {i j : ℕ} (hi : i < L.length) (hj : i < j) :
nth_le L i hi = nth_le (L.take j) i (by { rw length_take, exact lt_min hj hi }) :=
by { rw nth_le_of_eq (take_append_drop j L).symm hi, exact nth_le_append _ _ }
/-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of
length `> i`. Version designed to rewrite from the small list to the big list. -/
lemma nth_le_take' (L : list α) {i j : ℕ} (hi : i < (L.take j).length) :
nth_le (L.take j) i hi = nth_le L i (lt_of_lt_of_le hi (by simp [le_refl])) :=
by { simp at hi, rw nth_le_take L _ hi.1 }
lemma nth_take {l : list α} {n m : ℕ} (h : m < n) :
(l.take n).nth m = l.nth m :=
begin
induction n with n hn generalizing l m,
{ simp only [nat.nat_zero_eq_zero] at h,
exact absurd h (not_lt_of_le m.zero_le) },
{ cases l with hd tl,
{ simp only [take_nil] },
{ cases m,
{ simp only [nth, take] },
{ simpa only using hn (nat.lt_of_succ_lt_succ h) } } },
end
@[simp] lemma nth_take_of_succ {l : list α} {n : ℕ} :
(l.take (n + 1)).nth n = l.nth n :=
nth_take (nat.lt_succ_self n)
lemma take_succ {l : list α} {n : ℕ} :
l.take (n + 1) = l.take n ++ (l.nth n).to_list :=
begin
induction l with hd tl hl generalizing n,
{ simp only [option.to_list, nth, take_nil, append_nil]},
{ cases n,
{ simp only [option.to_list, nth, eq_self_iff_true, and_self, take, nil_append] },
{ simp only [hl, cons_append, nth, eq_self_iff_true, and_self, take] } }
end
@[simp] lemma take_eq_nil_iff {l : list α} {k : ℕ} :
l.take k = [] ↔ l = [] ∨ k = 0 :=
by { cases l; cases k; simp [nat.succ_ne_zero] }
lemma init_eq_take (l : list α) : l.init = l.take l.length.pred :=
begin
cases l with x l,
{ simp [init] },
{ induction l with hd tl hl generalizing x,
{ simp [init], },
{ simp [init, hl] } }
end
lemma init_take {n : ℕ} {l : list α} (h : n < l.length) :
(l.take n).init = l.take n.pred :=
by simp [init_eq_take, min_eq_left_of_lt h, take_take, pred_le]
@[simp] lemma init_cons_of_ne_nil {α : Type*} {x : α} :
∀ {l : list α} (h : l ≠ []), (x :: l).init = x :: l.init
| [] h := false.elim (h rfl)
| (a :: l) _ := by simp [init]
@[simp] lemma init_append_of_ne_nil {α : Type*} {l : list α} :
∀ (l' : list α) (h : l ≠ []), (l' ++ l).init = l' ++ l.init
| [] _ := by simp only [nil_append]
| (a :: l') h := by simp [append_ne_nil_of_ne_nil_right l' l h, init_append_of_ne_nil l' h]
@[simp] lemma drop_eq_nil_of_le {l : list α} {k : ℕ} (h : l.length ≤ k) :
l.drop k = [] :=
by simpa [←length_eq_zero] using tsub_eq_zero_iff_le.mpr h
lemma drop_eq_nil_iff_le {l : list α} {k : ℕ} :
l.drop k = [] ↔ l.length ≤ k :=
begin
refine ⟨λ h, _, drop_eq_nil_of_le⟩,
induction k with k hk generalizing l,
{ simp only [drop] at h,
simp [h] },
{ cases l,
{ simp },
{ simp only [drop] at h,
simpa [nat.succ_le_succ_iff] using hk h } }
end
lemma tail_drop (l : list α) (n : ℕ) : (l.drop n).tail = l.drop (n + 1) :=
begin
induction l with hd tl hl generalizing n,
{ simp },
{ cases n,
{ simp },
{ simp [hl] } }
end
lemma cons_nth_le_drop_succ {l : list α} {n : ℕ} (hn : n < l.length) :
l.nth_le n hn :: l.drop (n + 1) = l.drop n :=
begin
induction l with hd tl hl generalizing n,
{ exact absurd n.zero_le (not_le_of_lt (by simpa using hn)) },
{ cases n,
{ simp },
{ simp only [nat.succ_lt_succ_iff, list.length] at hn,
simpa [list.nth_le, list.drop] using hl hn } }
end
theorem drop_nil : ∀ n, drop n [] = ([] : list α) :=
λ _, drop_eq_nil_of_le (nat.zero_le _)
@[simp] theorem drop_one : ∀ l : list α, drop 1 l = tail l
| [] := rfl
| (a :: l) := rfl
theorem drop_add : ∀ m n (l : list α), drop (m + n) l = drop m (drop n l)
| m 0 l := rfl
| m (n+1) [] := (drop_nil _).symm
| m (n+1) (a::l) := drop_add m n _
@[simp] theorem drop_left : ∀ l₁ l₂ : list α, drop (length l₁) (l₁ ++ l₂) = l₂
| [] l₂ := rfl
| (a::l₁) l₂ := drop_left l₁ l₂
theorem drop_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) :
drop n (l₁ ++ l₂) = l₂ :=
by rw ← h; apply drop_left
theorem drop_eq_nth_le_cons : ∀ {n} {l : list α} h,
drop n l = nth_le l n h :: drop (n+1) l
| 0 (a::l) h := rfl
| (n+1) (a::l) h := @drop_eq_nth_le_cons n _ _
@[simp] lemma drop_length (l : list α) : l.drop l.length = [] :=
calc l.drop l.length = (l ++ []).drop l.length : by simp
... = [] : drop_left _ _
/-- Dropping the elements up to `n` in `l₁ ++ l₂` is the same as dropping the elements up to `n`
in `l₁`, dropping the elements up to `n - l₁.length` in `l₂`, and appending them. -/
lemma drop_append_eq_append_drop {l₁ l₂ : list α} {n : ℕ} :
drop n (l₁ ++ l₂) = drop n l₁ ++ drop (n - l₁.length) l₂ :=
begin
induction l₁ generalizing n, { simp },
cases n, { simp }, simp *
end
lemma drop_append_of_le_length {l₁ l₂ : list α} {n : ℕ} (h : n ≤ l₁.length) :
(l₁ ++ l₂).drop n = l₁.drop n ++ l₂ :=
by simp [drop_append_eq_append_drop, tsub_eq_zero_iff_le.mpr h]
/-- Dropping the elements up to `l₁.length + i` in `l₁ + l₂` is the same as dropping the elements
up to `i` in `l₂`. -/
lemma drop_append {l₁ l₂ : list α} (i : ℕ) :
drop (l₁.length + i) (l₁ ++ l₂) = drop i l₂ :=
by simp [drop_append_eq_append_drop, take_all_of_le le_self_add]
/-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by
dropping the first `i` elements. Version designed to rewrite from the big list to the small list. -/
lemma nth_le_drop (L : list α) {i j : ℕ} (h : i + j < L.length) :
nth_le L (i + j) h = nth_le (L.drop i) j
begin
have A : i < L.length := lt_of_le_of_lt (nat.le.intro rfl) h,
rw (take_append_drop i L).symm at h,
simpa only [le_of_lt A, min_eq_left, add_lt_add_iff_left, length_take, length_append] using h
end :=
begin
have A : length (take i L) = i, by simp [le_of_lt (lt_of_le_of_lt (nat.le.intro rfl) h)],
rw [nth_le_of_eq (take_append_drop i L).symm h, nth_le_append_right];
simp [A]
end
/-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by
dropping the first `i` elements. Version designed to rewrite from the small list to the big list. -/
lemma nth_le_drop' (L : list α) {i j : ℕ} (h : j < (L.drop i).length) :
nth_le (L.drop i) j h = nth_le L (i + j) (lt_tsub_iff_left.mp ((length_drop i L) ▸ h)) :=
by rw nth_le_drop
lemma nth_drop (L : list α) (i j : ℕ) :
nth (L.drop i) j = nth L (i + j) :=
begin
ext,
simp only [nth_eq_some, nth_le_drop', option.mem_def],
split;
exact λ ⟨h, ha⟩, ⟨by simpa [lt_tsub_iff_left] using h, ha⟩
end
@[simp] theorem drop_drop (n : ℕ) : ∀ (m) (l : list α), drop n (drop m l) = drop (n + m) l
| m [] := by simp
| 0 l := by simp
| (m+1) (a::l) :=
calc drop n (drop (m + 1) (a :: l)) = drop n (drop m l) : rfl
... = drop (n + m) l : drop_drop m l
... = drop (n + (m + 1)) (a :: l) : rfl
theorem drop_take : ∀ (m : ℕ) (n : ℕ) (l : list α),
drop m (take (m + n) l) = take n (drop m l)
| 0 n _ := by simp
| (m+1) n nil := by simp
| (m+1) n (_::l) :=
have h: m + 1 + n = (m+n) + 1, by ac_refl,
by simpa [take_cons, h] using drop_take m n l
lemma map_drop {α β : Type*} (f : α → β) :
∀ (L : list α) (i : ℕ), (L.drop i).map f = (L.map f).drop i
| [] i := by simp
| L 0 := by simp
| (h :: t) (n+1) := by { dsimp, rw [map_drop], }
theorem modify_nth_tail_eq_take_drop (f : list α → list α) (H : f [] = []) :
∀ n l, modify_nth_tail f n l = take n l ++ f (drop n l)
| 0 l := rfl
| (n+1) [] := H.symm
| (n+1) (b::l) := congr_arg (cons b) (modify_nth_tail_eq_take_drop n l)
theorem modify_nth_eq_take_drop (f : α → α) :
∀ n l, modify_nth f n l = take n l ++ modify_head f (drop n l) :=
modify_nth_tail_eq_take_drop _ rfl
theorem modify_nth_eq_take_cons_drop (f : α → α) {n l} (h) :
modify_nth f n l = take n l ++ f (nth_le l n h) :: drop (n+1) l :=
by rw [modify_nth_eq_take_drop, drop_eq_nth_le_cons h]; refl
theorem update_nth_eq_take_cons_drop (a : α) {n l} (h : n < length l) :
update_nth l n a = take n l ++ a :: drop (n+1) l :=
by rw [update_nth_eq_modify_nth, modify_nth_eq_take_cons_drop _ h]
lemma reverse_take {α} {xs : list α} (n : ℕ)
(h : n ≤ xs.length) :
xs.reverse.take n = (xs.drop (xs.length - n)).reverse :=
begin
induction xs generalizing n;
simp only [reverse_cons, drop, reverse_nil, zero_tsub, length, take_nil],
cases h.lt_or_eq_dec with h' h',
{ replace h' := le_of_succ_le_succ h',
rwa [take_append_of_le_length, xs_ih _ h'],
rw [show xs_tl.length + 1 - n = succ (xs_tl.length - n), from _, drop],
{ rwa [succ_eq_add_one, ← tsub_add_eq_add_tsub] },
{ rwa length_reverse } },
{ subst h', rw [length, tsub_self, drop],
suffices : xs_tl.length + 1 = (xs_tl.reverse ++ [xs_hd]).length,
by rw [this, take_length, reverse_cons],
rw [length_append, length_reverse], refl }
end
@[simp] lemma update_nth_eq_nil (l : list α) (n : ℕ) (a : α) : l.update_nth n a = [] ↔ l = [] :=
by cases l; cases n; simp only [update_nth]
section take'
variable [inhabited α]
@[simp] theorem take'_length : ∀ n l, length (@take' α _ n l) = n
| 0 l := rfl
| (n+1) l := congr_arg succ (take'_length _ _)
@[simp] theorem take'_nil : ∀ n, take' n (@nil α) = repeat (default _) n
| 0 := rfl
| (n+1) := congr_arg (cons _) (take'_nil _)
theorem take'_eq_take : ∀ {n} {l : list α},
n ≤ length l → take' n l = take n l
| 0 l h := rfl
| (n+1) (a::l) h := congr_arg (cons _) $
take'_eq_take $ le_of_succ_le_succ h
@[simp] theorem take'_left (l₁ l₂ : list α) : take' (length l₁) (l₁ ++ l₂) = l₁ :=
(take'_eq_take (by simp only [length_append, nat.le_add_right])).trans (take_left _ _)
theorem take'_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) :
take' n (l₁ ++ l₂) = l₁ :=
by rw ← h; apply take'_left
end take'
/-! ### foldl, foldr -/
lemma foldl_ext (f g : α → β → α) (a : α)
{l : list β} (H : ∀ a : α, ∀ b ∈ l, f a b = g a b) :
foldl f a l = foldl g a l :=
begin
induction l with hd tl ih generalizing a, {refl},
unfold foldl,
rw [ih (λ a b bin, H a b $ mem_cons_of_mem _ bin), H a hd (mem_cons_self _ _)]
end
lemma foldr_ext (f g : α → β → β) (b : β)
{l : list α} (H : ∀ a ∈ l, ∀ b : β, f a b = g a b) :
foldr f b l = foldr g b l :=
begin
induction l with hd tl ih, {refl},
simp only [mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] at H,
simp only [foldr, ih H.2, H.1]
end
@[simp] theorem foldl_nil (f : α → β → α) (a : α) : foldl f a [] = a := rfl
@[simp] theorem foldl_cons (f : α → β → α) (a : α) (b : β) (l : list β) :
foldl f a (b::l) = foldl f (f a b) l := rfl
@[simp] theorem foldr_nil (f : α → β → β) (b : β) : foldr f b [] = b := rfl
@[simp] theorem foldr_cons (f : α → β → β) (b : β) (a : α) (l : list α) :
foldr f b (a::l) = f a (foldr f b l) := rfl
@[simp] theorem foldl_append (f : α → β → α) :
∀ (a : α) (l₁ l₂ : list β), foldl f a (l₁++l₂) = foldl f (foldl f a l₁) l₂
| a [] l₂ := rfl
| a (b::l₁) l₂ := by simp only [cons_append, foldl_cons, foldl_append (f a b) l₁ l₂]
@[simp] theorem foldr_append (f : α → β → β) :
∀ (b : β) (l₁ l₂ : list α), foldr f b (l₁++l₂) = foldr f (foldr f b l₂) l₁
| b [] l₂ := rfl
| b (a::l₁) l₂ := by simp only [cons_append, foldr_cons, foldr_append b l₁ l₂]
@[simp] theorem foldl_join (f : α → β → α) :
∀ (a : α) (L : list (list β)), foldl f a (join L) = foldl (foldl f) a L
| a [] := rfl
| a (l::L) := by simp only [join, foldl_append, foldl_cons, foldl_join (foldl f a l) L]
@[simp] theorem foldr_join (f : α → β → β) :
∀ (b : β) (L : list (list α)), foldr f b (join L) = foldr (λ l b, foldr f b l) b L
| a [] := rfl
| a (l::L) := by simp only [join, foldr_append, foldr_join a L, foldr_cons]
theorem foldl_reverse (f : α → β → α) (a : α) (l : list β) :
foldl f a (reverse l) = foldr (λx y, f y x) a l :=
by induction l; [refl, simp only [*, reverse_cons, foldl_append, foldl_cons, foldl_nil, foldr]]
theorem foldr_reverse (f : α → β → β) (a : β) (l : list α) :
foldr f a (reverse l) = foldl (λx y, f y x) a l :=
let t := foldl_reverse (λx y, f y x) a (reverse l) in
by rw reverse_reverse l at t; rwa t
@[simp] theorem foldr_eta : ∀ (l : list α), foldr cons [] l = l
| [] := rfl
| (x::l) := by simp only [foldr_cons, foldr_eta l]; split; refl
@[simp] theorem reverse_foldl {l : list α} : reverse (foldl (λ t h, h :: t) [] l) = l :=
by rw ←foldr_reverse; simp
@[simp] theorem foldl_map (g : β → γ) (f : α → γ → α) (a : α) (l : list β) :
foldl f a (map g l) = foldl (λx y, f x (g y)) a l :=
by revert a; induction l; intros; [refl, simp only [*, map, foldl]]
@[simp] theorem foldr_map (g : β → γ) (f : γ → α → α) (a : α) (l : list β) :
foldr f a (map g l) = foldr (f ∘ g) a l :=
by revert a; induction l; intros; [refl, simp only [*, map, foldr]]
theorem foldl_map' {α β: Type u} (g : α → β) (f : α → α → α) (f' : β → β → β)
(a : α) (l : list α) (h : ∀ x y, f' (g x) (g y) = g (f x y)) :
list.foldl f' (g a) (l.map g) = g (list.foldl f a l) :=
begin
induction l generalizing a,
{ simp }, { simp [l_ih, h] }
end
theorem foldr_map' {α β: Type u} (g : α → β) (f : α → α → α) (f' : β → β → β)
(a : α) (l : list α) (h : ∀ x y, f' (g x) (g y) = g (f x y)) :
list.foldr f' (g a) (l.map g) = g (list.foldr f a l) :=
begin
induction l generalizing a,
{ simp }, { simp [l_ih, h] }
end
theorem foldl_hom (l : list γ) (f : α → β) (op : α → γ → α) (op' : β → γ → β) (a : α)
(h : ∀a x, f (op a x) = op' (f a) x) : foldl op' (f a) l = f (foldl op a l) :=
eq.symm $ by { revert a, induction l; intros; [refl, simp only [*, foldl]] }
theorem foldr_hom (l : list γ) (f : α → β) (op : γ → α → α) (op' : γ → β → β) (a : α)
(h : ∀x a, f (op x a) = op' x (f a)) : foldr op' (f a) l = f (foldr op a l) :=
by { revert a, induction l; intros; [refl, simp only [*, foldr]] }
lemma injective_foldl_comp {α : Type*} {l : list (α → α)} {f : α → α}
(hl : ∀ f ∈ l, function.injective f) (hf : function.injective f):
function.injective (@list.foldl (α → α) (α → α) function.comp f l) :=
begin
induction l generalizing f,
{ exact hf },
{ apply l_ih (λ _ h, hl _ (list.mem_cons_of_mem _ h)),
apply function.injective.comp hf,
apply hl _ (list.mem_cons_self _ _) }
end
/-- Induction principle for values produced by a `foldr`: if a property holds
for the seed element `b : β` and for all incremental `op : α → β → β`
performed on the elements `(a : α) ∈ l`. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
def foldr_rec_on {C : β → Sort*} (l : list α) (op : α → β → β) (b : β) (hb : C b)
(hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ l), C (op a b)) :
C (foldr op b l) :=
begin
induction l with hd tl IH,
{ exact hb },
{ refine hl _ _ hd (mem_cons_self hd tl),
refine IH _,
intros y hy x hx,
exact hl y hy x (mem_cons_of_mem hd hx) }
end
/-- Induction principle for values produced by a `foldl`: if a property holds
for the seed element `b : β` and for all incremental `op : β → α → β`
performed on the elements `(a : α) ∈ l`. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
def foldl_rec_on {C : β → Sort*} (l : list α) (op : β → α → β) (b : β) (hb : C b)
(hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ l), C (op b a)) :
C (foldl op b l) :=
begin
induction l with hd tl IH generalizing b,
{ exact hb },
{ refine IH _ _ _,
{ intros y hy x hx,
exact hl y hy x (mem_cons_of_mem hd hx) },
{ exact hl b hb hd (mem_cons_self hd tl) } }
end
@[simp] lemma foldr_rec_on_nil {C : β → Sort*} (op : α → β → β) (b) (hb : C b) (hl) :
foldr_rec_on [] op b hb hl = hb := rfl
@[simp] lemma foldr_rec_on_cons {C : β → Sort*} (x : α) (l : list α)
(op : α → β → β) (b) (hb : C b)
(hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ (x :: l)), C (op a b)) :
foldr_rec_on (x :: l) op b hb hl = hl _ (foldr_rec_on l op b hb
(λ b hb a ha, hl b hb a (mem_cons_of_mem _ ha))) x (mem_cons_self _ _) := rfl
@[simp] lemma foldl_rec_on_nil {C : β → Sort*} (op : β → α → β) (b) (hb : C b) (hl) :
foldl_rec_on [] op b hb hl = hb := rfl
/- scanl -/
section scanl
variables {f : β → α → β} {b : β} {a : α} {l : list α}
lemma length_scanl :
∀ a l, length (scanl f a l) = l.length + 1
| a [] := rfl
| a (x :: l) := by erw [length_cons, length_cons, length_scanl]
@[simp] lemma scanl_nil (b : β) : scanl f b nil = [b] := rfl
@[simp] lemma scanl_cons :
scanl f b (a :: l) = [b] ++ scanl f (f b a) l :=
by simp only [scanl, eq_self_iff_true, singleton_append, and_self]
@[simp] lemma nth_zero_scanl : (scanl f b l).nth 0 = some b :=
begin
cases l,
{ simp only [nth, scanl_nil] },
{ simp only [nth, scanl_cons, singleton_append] }
end
@[simp] lemma nth_le_zero_scanl {h : 0 < (scanl f b l).length} :
(scanl f b l).nth_le 0 h = b :=
begin
cases l,
{ simp only [nth_le, scanl_nil] },
{ simp only [nth_le, scanl_cons, singleton_append] }
end
lemma nth_succ_scanl {i : ℕ} :
(scanl f b l).nth (i + 1) = ((scanl f b l).nth i).bind (λ x, (l.nth i).map (λ y, f x y)) :=
begin
induction l with hd tl hl generalizing b i,
{ symmetry,
simp only [option.bind_eq_none', nth, forall_2_true_iff, not_false_iff, option.map_none',
scanl_nil, option.not_mem_none, forall_true_iff] },
{ simp only [nth, scanl_cons, singleton_append],
cases i,
{ simp only [option.map_some', nth_zero_scanl, nth, option.some_bind'] },
{ simp only [hl, nth] } }
end
lemma nth_le_succ_scanl {i : ℕ} {h : i + 1 < (scanl f b l).length} :
(scanl f b l).nth_le (i + 1) h =
f ((scanl f b l).nth_le i (nat.lt_of_succ_lt h))
(l.nth_le i (nat.lt_of_succ_lt_succ (lt_of_lt_of_le h (le_of_eq (length_scanl b l))))) :=
begin
induction i with i hi generalizing b l,
{ cases l,
{ simp only [length, zero_add, scanl_nil] at h,
exact absurd h (lt_irrefl 1) },
{ simp only [scanl_cons, singleton_append, nth_le_zero_scanl, nth_le] } },
{ cases l,
{ simp only [length, add_lt_iff_neg_right, scanl_nil] at h,
exact absurd h (not_lt_of_lt nat.succ_pos') },
{ simp_rw scanl_cons,
rw nth_le_append_right _,
{ simpa only [hi, length, succ_add_sub_one] },
{ simp only [length, nat.zero_le, le_add_iff_nonneg_left] } } }
end
end scanl
/- scanr -/
@[simp] theorem scanr_nil (f : α → β → β) (b : β) : scanr f b [] = [b] := rfl
@[simp] theorem scanr_aux_cons (f : α → β → β) (b : β) : ∀ (a : α) (l : list α),
scanr_aux f b (a::l) = (foldr f b (a::l), scanr f b l)
| a [] := rfl
| a (x::l) := let t := scanr_aux_cons x l in
by simp only [scanr, scanr_aux, t, foldr_cons]
@[simp] theorem scanr_cons (f : α → β → β) (b : β) (a : α) (l : list α) :
scanr f b (a::l) = foldr f b (a::l) :: scanr f b l :=
by simp only [scanr, scanr_aux_cons, foldr_cons]; split; refl
section foldl_eq_foldr
-- foldl and foldr coincide when f is commutative and associative
variables {f : α → α → α} (hcomm : commutative f) (hassoc : associative f)
include hassoc
theorem foldl1_eq_foldr1 : ∀ a b l, foldl f a (l++[b]) = foldr f b (a::l)
| a b nil := rfl
| a b (c :: l) :=
by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l]; rw hassoc
include hcomm
theorem foldl_eq_of_comm_of_assoc : ∀ a b l, foldl f a (b::l) = f b (foldl f a l)
| a b nil := hcomm a b
| a b (c::l) := by simp only [foldl_cons];
rw [← foldl_eq_of_comm_of_assoc, right_comm _ hcomm hassoc]; refl
theorem foldl_eq_foldr : ∀ a l, foldl f a l = foldr f a l
| a nil := rfl
| a (b :: l) :=
by simp only [foldr_cons, foldl_eq_of_comm_of_assoc hcomm hassoc]; rw (foldl_eq_foldr a l)
end foldl_eq_foldr
section foldl_eq_foldlr'
variables {f : α → β → α}
variables hf : ∀ a b c, f (f a b) c = f (f a c) b
include hf
theorem foldl_eq_of_comm' : ∀ a b l, foldl f a (b::l) = f (foldl f a l) b
| a b [] := rfl
| a b (c :: l) := by rw [foldl,foldl,foldl,← foldl_eq_of_comm',foldl,hf]
theorem foldl_eq_foldr' : ∀ a l, foldl f a l = foldr (flip f) a l
| a [] := rfl
| a (b :: l) := by rw [foldl_eq_of_comm' hf,foldr,foldl_eq_foldr']; refl
end foldl_eq_foldlr'
section foldl_eq_foldlr'
variables {f : α → β → β}
variables hf : ∀ a b c, f a (f b c) = f b (f a c)
include hf
theorem foldr_eq_of_comm' : ∀ a b l, foldr f a (b::l) = foldr f (f b a) l
| a b [] := rfl
| a b (c :: l) := by rw [foldr,foldr,foldr,hf,← foldr_eq_of_comm']; refl
end foldl_eq_foldlr'
section
variables {op : α → α → α} [ha : is_associative α op] [hc : is_commutative α op]
local notation a * b := op a b
local notation l <*> a := foldl op a l
include ha
lemma foldl_assoc : ∀ {l : list α} {a₁ a₂}, l <*> (a₁ * a₂) = a₁ * (l <*> a₂)
| [] a₁ a₂ := rfl
| (a :: l) a₁ a₂ :=
calc a::l <*> (a₁ * a₂) = l <*> (a₁ * (a₂ * a)) : by simp only [foldl_cons, ha.assoc]
... = a₁ * (a::l <*> a₂) : by rw [foldl_assoc, foldl_cons]
lemma foldl_op_eq_op_foldr_assoc : ∀{l : list α} {a₁ a₂}, (l <*> a₁) * a₂ = a₁ * l.foldr (*) a₂
| [] a₁ a₂ := rfl
| (a :: l) a₁ a₂ := by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc];
rw [foldl_op_eq_op_foldr_assoc]
include hc
lemma foldl_assoc_comm_cons {l : list α} {a₁ a₂} : (a₁ :: l) <*> a₂ = a₁ * (l <*> a₂) :=
by rw [foldl_cons, hc.comm, foldl_assoc]
end
/-! ### mfoldl, mfoldr, mmap -/
section mfoldl_mfoldr
variables {m : Type v → Type w} [monad m]
@[simp] theorem mfoldl_nil (f : β → α → m β) {b} : mfoldl f b [] = pure b := rfl
@[simp] theorem mfoldr_nil (f : α → β → m β) {b} : mfoldr f b [] = pure b := rfl
@[simp] theorem mfoldl_cons {f : β → α → m β} {b a l} :
mfoldl f b (a :: l) = f b a >>= λ b', mfoldl f b' l := rfl
@[simp] theorem mfoldr_cons {f : α → β → m β} {b a l} :
mfoldr f b (a :: l) = mfoldr f b l >>= f a := rfl
theorem mfoldr_eq_foldr (f : α → β → m β) (b l) :
mfoldr f b l = foldr (λ a mb, mb >>= f a) (pure b) l :=
by induction l; simp *
attribute [simp] mmap mmap'
variables [is_lawful_monad m]
theorem mfoldl_eq_foldl (f : β → α → m β) (b l) :
mfoldl f b l = foldl (λ mb a, mb >>= λ b, f b a) (pure b) l :=
begin
suffices h : ∀ (mb : m β),
(mb >>= λ b, mfoldl f b l) = foldl (λ mb a, mb >>= λ b, f b a) mb l,
by simp [←h (pure b)],
induction l; intro,
{ simp },
{ simp only [mfoldl, foldl, ←l_ih] with monad_norm }
end
@[simp] theorem mfoldl_append {f : β → α → m β} : ∀ {b l₁ l₂},
mfoldl f b (l₁ ++ l₂) = mfoldl f b l₁ >>= λ x, mfoldl f x l₂
| _ [] _ := by simp only [nil_append, mfoldl_nil, pure_bind]
| _ (_::_) _ := by simp only [cons_append, mfoldl_cons, mfoldl_append, is_lawful_monad.bind_assoc]
@[simp] theorem mfoldr_append {f : α → β → m β} : ∀ {b l₁ l₂},
mfoldr f b (l₁ ++ l₂) = mfoldr f b l₂ >>= λ x, mfoldr f x l₁
| _ [] _ := by simp only [nil_append, mfoldr_nil, bind_pure]
| _ (_::_) _ := by simp only [mfoldr_cons, cons_append, mfoldr_append, is_lawful_monad.bind_assoc]
end mfoldl_mfoldr
/-! ### intersperse -/
@[simp] lemma intersperse_nil {α : Type u} (a : α) : intersperse a [] = [] := rfl
@[simp] lemma intersperse_singleton {α : Type u} (a b : α) : intersperse a [b] = [b] := rfl
@[simp] lemma intersperse_cons_cons {α : Type u} (a b c : α) (tl : list α) :
intersperse a (b :: c :: tl) = b :: a :: intersperse a (c :: tl) := rfl
/-! ### split_at and split_on -/
@[simp] theorem split_at_eq_take_drop : ∀ (n : ℕ) (l : list α), split_at n l = (take n l, drop n l)
| 0 a := rfl
| (succ n) [] := rfl
| (succ n) (x :: xs) := by simp only [split_at, split_at_eq_take_drop n xs, take, drop]
@[simp] lemma split_on_nil {α : Type u} [decidable_eq α] (a : α) : [].split_on a = [[]] := rfl
/-- An auxiliary definition for proving a specification lemma for `split_on_p`.
`split_on_p_aux' P xs ys` splits the list `ys ++ xs` at every element satisfying `P`,
where `ys` is an accumulating parameter for the initial segment of elements not satisfying `P`.
-/
def split_on_p_aux' {α : Type u} (P : α → Prop) [decidable_pred P] : list α → list α → list (list α)
| [] xs := [xs]
| (h :: t) xs :=
if P h then xs :: split_on_p_aux' t []
else split_on_p_aux' t (xs ++ [h])
lemma split_on_p_aux_eq {α : Type u} (P : α → Prop) [decidable_pred P] (xs ys : list α) :
split_on_p_aux' P xs ys = split_on_p_aux P xs ((++) ys) :=
begin
induction xs with a t ih generalizing ys; simp! only [append_nil, eq_self_iff_true, and_self],
split_ifs; rw ih,
{ refine ⟨rfl, rfl⟩ },
{ congr, ext, simp }
end
lemma split_on_p_aux_nil {α : Type u} (P : α → Prop) [decidable_pred P] (xs : list α) :
split_on_p_aux P xs id = split_on_p_aux' P xs [] :=
by { rw split_on_p_aux_eq, refl }
/-- The original list `L` can be recovered by joining the lists produced by `split_on_p p L`,
interspersed with the elements `L.filter p`. -/
lemma split_on_p_spec {α : Type u} (p : α → Prop) [decidable_pred p] (as : list α) :
join (zip_with (++) (split_on_p p as) ((as.filter p).map (λ x, [x]) ++ [[]])) = as :=
begin
rw [split_on_p, split_on_p_aux_nil],
suffices : ∀ xs,
join (zip_with (++) (split_on_p_aux' p as xs) ((as.filter p).map(λ x, [x]) ++ [[]])) = xs ++ as,
{ rw this, refl },
induction as; intro; simp! only [split_on_p_aux', append_nil],
split_ifs; simp [zip_with, join, *],
end
/-! ### all & any -/
@[simp] theorem all_nil (p : α → bool) : all [] p = tt := rfl
@[simp] theorem all_cons (p : α → bool) (a : α) (l : list α) :
all (a::l) p = (p a && all l p) := rfl
theorem all_iff_forall {p : α → bool} {l : list α} : all l p ↔ ∀ a ∈ l, p a :=
begin
induction l with a l ih,
{ exact iff_of_true rfl (forall_mem_nil _) },
simp only [all_cons, band_coe_iff, ih, forall_mem_cons]
end
theorem all_iff_forall_prop {p : α → Prop} [decidable_pred p]
{l : list α} : all l (λ a, p a) ↔ ∀ a ∈ l, p a :=
by simp only [all_iff_forall, bool.of_to_bool_iff]
@[simp] theorem any_nil (p : α → bool) : any [] p = ff := rfl
@[simp] theorem any_cons (p : α → bool) (a : α) (l : list α) :
any (a::l) p = (p a || any l p) := rfl
theorem any_iff_exists {p : α → bool} {l : list α} : any l p ↔ ∃ a ∈ l, p a :=
begin
induction l with a l ih,
{ exact iff_of_false bool.not_ff (not_exists_mem_nil _) },
simp only [any_cons, bor_coe_iff, ih, exists_mem_cons_iff]
end
theorem any_iff_exists_prop {p : α → Prop} [decidable_pred p]
{l : list α} : any l (λ a, p a) ↔ ∃ a ∈ l, p a :=
by simp [any_iff_exists]
theorem any_of_mem {p : α → bool} {a : α} {l : list α} (h₁ : a ∈ l) (h₂ : p a) : any l p :=
any_iff_exists.2 ⟨_, h₁, h₂⟩
@[priority 500] instance decidable_forall_mem {p : α → Prop} [decidable_pred p] (l : list α) :
decidable (∀ x ∈ l, p x) :=
decidable_of_iff _ all_iff_forall_prop
instance decidable_exists_mem {p : α → Prop} [decidable_pred p] (l : list α) :
decidable (∃ x ∈ l, p x) :=
decidable_of_iff _ any_iff_exists_prop
/-! ### map for partial functions -/
/-- Partial map. If `f : Π a, p a → β` is a partial function defined on
`a : α` satisfying `p`, then `pmap f l h` is essentially the same as `map f l`
but is defined only when all members of `l` satisfy `p`, using the proof
to apply `f`. -/
@[simp] def pmap {p : α → Prop} (f : Π a, p a → β) : Π l : list α, (∀ a ∈ l, p a) → list β
| [] H := []
| (a::l) H := f a (forall_mem_cons.1 H).1 :: pmap l (forall_mem_cons.1 H).2
/-- "Attach" the proof that the elements of `l` are in `l` to produce a new list
with the same elements but in the type `{x // x ∈ l}`. -/
def attach (l : list α) : list {x // x ∈ l} := pmap subtype.mk l (λ a, id)
theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {l : list α} (hx : x ∈ l) :
sizeof x < sizeof l :=
begin
induction l with h t ih; cases hx,
{ rw hx, exact lt_add_of_lt_of_nonneg (lt_one_add _) (nat.zero_le _) },
{ exact lt_add_of_pos_of_le (zero_lt_one_add _) (le_of_lt (ih hx)) }
end
@[simp] theorem pmap_eq_map (p : α → Prop) (f : α → β) (l : list α) (H) :
@pmap _ _ p (λ a _, f a) l H = map f l :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_congr {p q : α → Prop} {f : Π a, p a → β} {g : Π a, q a → β}
(l : list α) {H₁ H₂} (h : ∀ a h₁ h₂, f a h₁ = g a h₂) :
pmap f l H₁ = pmap g l H₂ :=
by induction l with _ _ ih; [refl, rw [pmap, pmap, h, ih]]
theorem map_pmap {p : α → Prop} (g : β → γ) (f : Π a, p a → β)
(l H) : map g (pmap f l H) = pmap (λ a h, g (f a h)) l H :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_map {p : β → Prop} (g : ∀ b, p b → γ) (f : α → β)
(l H) : pmap g (map f l) H = pmap (λ a h, g (f a) h) l (λ a h, H _ (mem_map_of_mem _ h)) :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_eq_map_attach {p : α → Prop} (f : Π a, p a → β)
(l H) : pmap f l H = l.attach.map (λ x, f x.1 (H _ x.2)) :=
by rw [attach, map_pmap]; exact pmap_congr l (λ a h₁ h₂, rfl)
theorem attach_map_val (l : list α) : l.attach.map subtype.val = l :=
by rw [attach, map_pmap]; exact (pmap_eq_map _ _ _ _).trans (map_id l)
@[simp] theorem mem_attach (l : list α) : ∀ x, x ∈ l.attach | ⟨a, h⟩ :=
by have := mem_map.1 (by rw [attach_map_val]; exact h);
{ rcases this with ⟨⟨_, _⟩, m, rfl⟩, exact m }
@[simp] theorem mem_pmap {p : α → Prop} {f : Π a, p a → β}
{l H b} : b ∈ pmap f l H ↔ ∃ a (h : a ∈ l), f a (H a h) = b :=
by simp only [pmap_eq_map_attach, mem_map, mem_attach, true_and, subtype.exists]
@[simp] theorem length_pmap {p : α → Prop} {f : Π a, p a → β}
{l H} : length (pmap f l H) = length l :=
by induction l; [refl, simp only [*, pmap, length]]
@[simp] lemma length_attach (L : list α) : L.attach.length = L.length := length_pmap
@[simp] lemma pmap_eq_nil {p : α → Prop} {f : Π a, p a → β}
{l H} : pmap f l H = [] ↔ l = [] :=
by rw [← length_eq_zero, length_pmap, length_eq_zero]
@[simp] lemma attach_eq_nil (l : list α) : l.attach = [] ↔ l = [] := pmap_eq_nil
lemma last_pmap {α β : Type*} (p : α → Prop) (f : Π a, p a → β)
(l : list α) (hl₁ : ∀ a ∈ l, p a) (hl₂ : l ≠ []) :
(l.pmap f hl₁).last (mt list.pmap_eq_nil.1 hl₂) = f (l.last hl₂) (hl₁ _ (list.last_mem hl₂)) :=
begin
induction l with l_hd l_tl l_ih,
{ apply (hl₂ rfl).elim },
{ cases l_tl,
{ simp },
{ apply l_ih } }
end
lemma nth_pmap {p : α → Prop} (f : Π a, p a → β) {l : list α} (h : ∀ a ∈ l, p a) (n : ℕ) :
nth (pmap f l h) n = option.pmap f (nth l n) (λ x H, h x (nth_mem H)) :=
begin
induction l with hd tl hl generalizing n,
{ simp },
{ cases n; simp [hl] }
end
lemma nth_le_pmap {p : α → Prop} (f : Π a, p a → β) {l : list α} (h : ∀ a ∈ l, p a) {n : ℕ}
(hn : n < (pmap f l h).length) :
nth_le (pmap f l h) n hn = f (nth_le l n (@length_pmap _ _ p f l h ▸ hn))
(h _ (nth_le_mem l n (@length_pmap _ _ p f l h ▸ hn))) :=
begin
induction l with hd tl hl generalizing n,
{ simp only [length, pmap] at hn,
exact absurd hn (not_lt_of_le n.zero_le) },
{ cases n,
{ simp },
{ simpa [hl] } }
end
/-! ### find -/
section find
variables {p : α → Prop} [decidable_pred p] {l : list α} {a : α}
@[simp] theorem find_nil (p : α → Prop) [decidable_pred p] : find p [] = none :=
rfl
@[simp] theorem find_cons_of_pos (l) (h : p a) : find p (a::l) = some a :=
if_pos h
@[simp] theorem find_cons_of_neg (l) (h : ¬ p a) : find p (a::l) = find p l :=
if_neg h
@[simp] theorem find_eq_none : find p l = none ↔ ∀ x ∈ l, ¬ p x :=
begin
induction l with a l IH,
{ exact iff_of_true rfl (forall_mem_nil _) },
rw forall_mem_cons, by_cases h : p a,
{ simp only [find_cons_of_pos _ h, h, not_true, false_and] },
{ rwa [find_cons_of_neg _ h, iff_true_intro h, true_and] }
end
theorem find_some (H : find p l = some a) : p a :=
begin
induction l with b l IH, {contradiction},
by_cases h : p b,
{ rw find_cons_of_pos _ h at H, cases H, exact h },
{ rw find_cons_of_neg _ h at H, exact IH H }
end
@[simp] theorem find_mem (H : find p l = some a) : a ∈ l :=
begin
induction l with b l IH, {contradiction},
by_cases h : p b,
{ rw find_cons_of_pos _ h at H, cases H, apply mem_cons_self },
{ rw find_cons_of_neg _ h at H, exact mem_cons_of_mem _ (IH H) }
end
end find
/-! ### lookmap -/
section lookmap
variables (f : α → option α)
@[simp] theorem lookmap_nil : [].lookmap f = [] := rfl
@[simp] theorem lookmap_cons_none {a : α} (l : list α) (h : f a = none) :
(a :: l).lookmap f = a :: l.lookmap f :=
by simp [lookmap, h]
@[simp] theorem lookmap_cons_some {a b : α} (l : list α) (h : f a = some b) :
(a :: l).lookmap f = b :: l :=
by simp [lookmap, h]
theorem lookmap_some : ∀ l : list α, l.lookmap some = l
| [] := rfl
| (a::l) := rfl
theorem lookmap_none : ∀ l : list α, l.lookmap (λ _, none) = l
| [] := rfl
| (a::l) := congr_arg (cons a) (lookmap_none l)
theorem lookmap_congr {f g : α → option α} :
∀ {l : list α}, (∀ a ∈ l, f a = g a) → l.lookmap f = l.lookmap g
| [] H := rfl
| (a::l) H := begin
cases forall_mem_cons.1 H with H₁ H₂,
cases h : g a with b,
{ simp [h, H₁.trans h, lookmap_congr H₂] },
{ simp [lookmap_cons_some _ _ h, lookmap_cons_some _ _ (H₁.trans h)] }
end
theorem lookmap_of_forall_not {l : list α} (H : ∀ a ∈ l, f a = none) : l.lookmap f = l :=
(lookmap_congr H).trans (lookmap_none l)
theorem lookmap_map_eq (g : α → β) (h : ∀ a (b ∈ f a), g a = g b) :
∀ l : list α, map g (l.lookmap f) = map g l
| [] := rfl
| (a::l) := begin
cases h' : f a with b,
{ simp [h', lookmap_map_eq] },
{ simp [lookmap_cons_some _ _ h', h _ _ h'] }
end
theorem lookmap_id' (h : ∀ a (b ∈ f a), a = b) (l : list α) : l.lookmap f = l :=
by rw [← map_id (l.lookmap f), lookmap_map_eq, map_id]; exact h
theorem length_lookmap (l : list α) : length (l.lookmap f) = length l :=
by rw [← length_map, lookmap_map_eq _ (λ _, ()), length_map]; simp
end lookmap
/-! ### filter_map -/
@[simp] theorem filter_map_nil (f : α → option β) : filter_map f [] = [] := rfl
@[simp] theorem filter_map_cons_none {f : α → option β} (a : α) (l : list α) (h : f a = none) :
filter_map f (a :: l) = filter_map f l :=
by simp only [filter_map, h]
@[simp] theorem filter_map_cons_some (f : α → option β)
(a : α) (l : list α) {b : β} (h : f a = some b) :
filter_map f (a :: l) = b :: filter_map f l :=
by simp only [filter_map, h]; split; refl
theorem filter_map_cons (f : α → option β) (a : α) (l : list α) :
filter_map f (a :: l) = option.cases_on (f a) (filter_map f l) (λb, b :: filter_map f l) :=
begin
generalize eq : f a = b,
cases b,
{ rw filter_map_cons_none _ _ eq },
{ rw filter_map_cons_some _ _ _ eq },
end
lemma filter_map_append {α β : Type*} (l l' : list α) (f : α → option β) :
filter_map f (l ++ l') = filter_map f l ++ filter_map f l' :=
begin
induction l with hd tl hl generalizing l',
{ simp },
{ rw [cons_append, filter_map, filter_map],
cases f hd;
simp only [filter_map, hl, cons_append, eq_self_iff_true, and_self] }
end
theorem filter_map_eq_map (f : α → β) : filter_map (some ∘ f) = map f :=
begin
funext l,
induction l with a l IH, {refl},
simp only [filter_map_cons_some (some ∘ f) _ _ rfl, IH, map_cons], split; refl
end
theorem filter_map_eq_filter (p : α → Prop) [decidable_pred p] :
filter_map (option.guard p) = filter p :=
begin
funext l,
induction l with a l IH, {refl},
by_cases pa : p a,
{ simp only [filter_map, option.guard, IH, if_pos pa, filter_cons_of_pos _ pa], split; refl },
{ simp only [filter_map, option.guard, IH, if_neg pa, filter_cons_of_neg _ pa] }
end
theorem filter_map_filter_map (f : α → option β) (g : β → option γ) (l : list α) :
filter_map g (filter_map f l) = filter_map (λ x, (f x).bind g) l :=
begin
induction l with a l IH, {refl},
cases h : f a with b,
{ rw [filter_map_cons_none _ _ h, filter_map_cons_none, IH],
simp only [h, option.none_bind'] },
rw filter_map_cons_some _ _ _ h,
cases h' : g b with c;
[ rw [filter_map_cons_none _ _ h', filter_map_cons_none, IH],
rw [filter_map_cons_some _ _ _ h', filter_map_cons_some, IH] ];
simp only [h, h', option.some_bind']
end
theorem map_filter_map (f : α → option β) (g : β → γ) (l : list α) :
map g (filter_map f l) = filter_map (λ x, (f x).map g) l :=
by rw [← filter_map_eq_map, filter_map_filter_map]; refl
theorem filter_map_map (f : α → β) (g : β → option γ) (l : list α) :
filter_map g (map f l) = filter_map (g ∘ f) l :=
by rw [← filter_map_eq_map, filter_map_filter_map]; refl
theorem filter_filter_map (f : α → option β) (p : β → Prop) [decidable_pred p] (l : list α) :
filter p (filter_map f l) = filter_map (λ x, (f x).filter p) l :=
by rw [← filter_map_eq_filter, filter_map_filter_map]; refl
theorem filter_map_filter (p : α → Prop) [decidable_pred p] (f : α → option β) (l : list α) :
filter_map f (filter p l) = filter_map (λ x, if p x then f x else none) l :=
begin
rw [← filter_map_eq_filter, filter_map_filter_map], congr,
funext x,
show (option.guard p x).bind f = ite (p x) (f x) none,
by_cases h : p x,
{ simp only [option.guard, if_pos h, option.some_bind'] },
{ simp only [option.guard, if_neg h, option.none_bind'] }
end
@[simp] theorem filter_map_some (l : list α) : filter_map some l = l :=
by rw filter_map_eq_map; apply map_id
@[simp] theorem mem_filter_map (f : α → option β) (l : list α) {b : β} :
b ∈ filter_map f l ↔ ∃ a, a ∈ l ∧ f a = some b :=
begin
induction l with a l IH,
{ split, { intro H, cases H }, { rintro ⟨_, H, _⟩, cases H } },
cases h : f a with b',
{ have : f a ≠ some b, {rw h, intro, contradiction},
simp only [filter_map_cons_none _ _ h, IH, mem_cons_iff,
or_and_distrib_right, exists_or_distrib, exists_eq_left, this, false_or] },
{ have : f a = some b ↔ b = b',
{ split; intro t, {rw t at h; injection h}, {exact t.symm ▸ h} },
simp only [filter_map_cons_some _ _ _ h, IH, mem_cons_iff,
or_and_distrib_right, exists_or_distrib, this, exists_eq_left] }
end
theorem map_filter_map_of_inv (f : α → option β) (g : β → α)
(H : ∀ x : α, (f x).map g = some x) (l : list α) :
map g (filter_map f l) = l :=
by simp only [map_filter_map, H, filter_map_some]
theorem sublist.filter_map (f : α → option β) {l₁ l₂ : list α}
(s : l₁ <+ l₂) : filter_map f l₁ <+ filter_map f l₂ :=
by induction s with l₁ l₂ a s IH l₁ l₂ a s IH;
simp only [filter_map]; cases f a with b;
simp only [filter_map, IH, sublist.cons, sublist.cons2]
theorem sublist.map (f : α → β) {l₁ l₂ : list α}
(s : l₁ <+ l₂) : map f l₁ <+ map f l₂ :=
filter_map_eq_map f ▸ s.filter_map _
/-! ### reduce_option -/
@[simp] lemma reduce_option_cons_of_some (x : α) (l : list (option α)) :
reduce_option (some x :: l) = x :: l.reduce_option :=
by simp only [reduce_option, filter_map, id.def, eq_self_iff_true, and_self]
@[simp] lemma reduce_option_cons_of_none (l : list (option α)) :
reduce_option (none :: l) = l.reduce_option :=
by simp only [reduce_option, filter_map, id.def]
@[simp] lemma reduce_option_nil : @reduce_option α [] = [] := rfl
@[simp] lemma reduce_option_map {l : list (option α)} {f : α → β} :
reduce_option (map (option.map f) l) = map f (reduce_option l) :=
begin
induction l with hd tl hl,
{ simp only [reduce_option_nil, map_nil] },
{ cases hd;
simpa only [true_and, option.map_some', map, eq_self_iff_true,
reduce_option_cons_of_some] using hl },
end
lemma reduce_option_append (l l' : list (option α)) :
(l ++ l').reduce_option = l.reduce_option ++ l'.reduce_option :=
filter_map_append l l' id
lemma reduce_option_length_le (l : list (option α)) :
l.reduce_option.length ≤ l.length :=
begin
induction l with hd tl hl,
{ simp only [reduce_option_nil, length] },
{ cases hd,
{ exact nat.le_succ_of_le hl },
{ simpa only [length, add_le_add_iff_right, reduce_option_cons_of_some] using hl} }
end
lemma reduce_option_length_eq_iff {l : list (option α)} :
l.reduce_option.length = l.length ↔ ∀ x ∈ l, option.is_some x :=
begin
induction l with hd tl hl,
{ simp only [forall_const, reduce_option_nil, not_mem_nil,
forall_prop_of_false, eq_self_iff_true, length, not_false_iff] },
{ cases hd,
{ simp only [mem_cons_iff, forall_eq_or_imp, bool.coe_sort_ff, false_and,
reduce_option_cons_of_none, length, option.is_some_none, iff_false],
intro H,
have := reduce_option_length_le tl,
rw H at this,
exact absurd (nat.lt_succ_self _) (not_lt_of_le this) },
{ simp only [hl, true_and, mem_cons_iff, forall_eq_or_imp, add_left_inj,
bool.coe_sort_tt, length, option.is_some_some, reduce_option_cons_of_some] } }
end
lemma reduce_option_length_lt_iff {l : list (option α)} :
l.reduce_option.length < l.length ↔ none ∈ l :=
begin
rw [(reduce_option_length_le l).lt_iff_ne, ne, reduce_option_length_eq_iff],
induction l; simp *,
rw [eq_comm, ← option.not_is_some_iff_eq_none, decidable.imp_iff_not_or]
end
lemma reduce_option_singleton (x : option α) :
[x].reduce_option = x.to_list :=
by cases x; refl
lemma reduce_option_concat (l : list (option α)) (x : option α) :
(l.concat x).reduce_option = l.reduce_option ++ x.to_list :=
begin
induction l with hd tl hl generalizing x,
{ cases x;
simp [option.to_list] },
{ simp only [concat_eq_append, reduce_option_append] at hl,
cases hd;
simp [hl, reduce_option_append] }
end
lemma reduce_option_concat_of_some (l : list (option α)) (x : α) :
(l.concat (some x)).reduce_option = l.reduce_option.concat x :=
by simp only [reduce_option_nil, concat_eq_append, reduce_option_append, reduce_option_cons_of_some]
lemma reduce_option_mem_iff {l : list (option α)} {x : α} :
x ∈ l.reduce_option ↔ (some x) ∈ l :=
by simp only [reduce_option, id.def, mem_filter_map, exists_eq_right]
lemma reduce_option_nth_iff {l : list (option α)} {x : α} :
(∃ i, l.nth i = some (some x)) ↔ ∃ i, l.reduce_option.nth i = some x :=
by rw [←mem_iff_nth, ←mem_iff_nth, reduce_option_mem_iff]
/-! ### filter -/
section filter
variables {p : α → Prop} [decidable_pred p]
theorem filter_eq_foldr (p : α → Prop) [decidable_pred p] (l : list α) :
filter p l = foldr (λ a out, if p a then a :: out else out) [] l :=
by induction l; simp [*, filter]
lemma filter_congr {p q : α → Prop} [decidable_pred p] [decidable_pred q]
: ∀ {l : list α}, (∀ x ∈ l, p x ↔ q x) → filter p l = filter q l
| [] _ := rfl
| (a::l) h := by rw forall_mem_cons at h; by_cases pa : p a;
[simp only [filter_cons_of_pos _ pa, filter_cons_of_pos _ (h.1.1 pa), filter_congr h.2],
simp only [filter_cons_of_neg _ pa, filter_cons_of_neg _ (mt h.1.2 pa), filter_congr h.2]];
split; refl
@[simp] theorem filter_subset (l : list α) : filter p l ⊆ l :=
(filter_sublist l).subset
theorem of_mem_filter {a : α} : ∀ {l}, a ∈ filter p l → p a
| (b::l) ain :=
if pb : p b then
have a ∈ b :: filter p l, by simpa only [filter_cons_of_pos _ pb] using ain,
or.elim (eq_or_mem_of_mem_cons this)
(assume : a = b, begin rw [← this] at pb, exact pb end)
(assume : a ∈ filter p l, of_mem_filter this)
else
begin simp only [filter_cons_of_neg _ pb] at ain, exact (of_mem_filter ain) end
theorem mem_of_mem_filter {a : α} {l} (h : a ∈ filter p l) : a ∈ l :=
filter_subset l h
theorem mem_filter_of_mem {a : α} : ∀ {l}, a ∈ l → p a → a ∈ filter p l
| (_::l) (or.inl rfl) pa := by rw filter_cons_of_pos _ pa; apply mem_cons_self
| (b::l) (or.inr ain) pa := if pb : p b
then by rw [filter_cons_of_pos _ pb]; apply mem_cons_of_mem; apply mem_filter_of_mem ain pa
else by rw [filter_cons_of_neg _ pb]; apply mem_filter_of_mem ain pa
@[simp] theorem mem_filter {a : α} {l} : a ∈ filter p l ↔ a ∈ l ∧ p a :=
⟨λ h, ⟨mem_of_mem_filter h, of_mem_filter h⟩, λ ⟨h₁, h₂⟩, mem_filter_of_mem h₁ h₂⟩
lemma monotone_filter_left (p : α → Prop) [decidable_pred p]
⦃l l' : list α⦄ (h : l ⊆ l') : filter p l ⊆ filter p l' :=
begin
intros x hx,
rw [mem_filter] at hx ⊢,
exact ⟨h hx.left, hx.right⟩
end
theorem filter_eq_self {l} : filter p l = l ↔ ∀ a ∈ l, p a :=
begin
induction l with a l ih,
{ exact iff_of_true rfl (forall_mem_nil _) },
rw forall_mem_cons, by_cases p a,
{ rw [filter_cons_of_pos _ h, cons_inj, ih, and_iff_right h] },
{ rw [filter_cons_of_neg _ h],
refine iff_of_false _ (mt and.left h), intro e,
have := filter_sublist l, rw e at this,
exact not_lt_of_ge (length_le_of_sublist this) (lt_succ_self _) }
end
theorem filter_eq_nil {l} : filter p l = [] ↔ ∀ a ∈ l, ¬p a :=
by simp only [eq_nil_iff_forall_not_mem, mem_filter, not_and]
variable (p)
theorem sublist.filter {l₁ l₂} (s : l₁ <+ l₂) : filter p l₁ <+ filter p l₂ :=
filter_map_eq_filter p ▸ s.filter_map _
lemma monotone_filter_right (l : list α) ⦃p q : α → Prop⦄ [decidable_pred p] [decidable_pred q]
(h : p ≤ q) : l.filter p <+ l.filter q :=
begin
induction l with hd tl IH,
{ refl },
{ by_cases hp : p hd,
{ rw [filter_cons_of_pos _ hp, filter_cons_of_pos _ (h _ hp)],
exact IH.cons_cons hd },
{ rw filter_cons_of_neg _ hp,
by_cases hq : q hd,
{ rw filter_cons_of_pos _ hq,
exact sublist_cons_of_sublist hd IH },
{ rw filter_cons_of_neg _ hq,
exact IH } } }
end
theorem map_filter (f : β → α) (l : list β) :
filter p (map f l) = map f (filter (p ∘ f) l) :=
by rw [← filter_map_eq_map, filter_filter_map, filter_map_filter]; refl
@[simp] theorem filter_filter (q) [decidable_pred q] : ∀ l,
filter p (filter q l) = filter (λ a, p a ∧ q a) l
| [] := rfl
| (a :: l) := by by_cases hp : p a; by_cases hq : q a; simp only [hp, hq, filter, if_true, if_false,
true_and, false_and, filter_filter l, eq_self_iff_true]
@[simp] lemma filter_true {h : decidable_pred (λ a : α, true)} (l : list α) :
@filter α (λ _, true) h l = l :=
by convert filter_eq_self.2 (λ _ _, trivial)
@[simp] lemma filter_false {h : decidable_pred (λ a : α, false)} (l : list α) :
@filter α (λ _, false) h l = [] :=
by convert filter_eq_nil.2 (λ _ _, id)
@[simp] theorem span_eq_take_drop : ∀ (l : list α), span p l = (take_while p l, drop_while p l)
| [] := rfl
| (a::l) :=
if pa : p a then by simp only [span, if_pos pa, span_eq_take_drop l, take_while, drop_while]
else by simp only [span, take_while, drop_while, if_neg pa]
@[simp] theorem take_while_append_drop : ∀ (l : list α), take_while p l ++ drop_while p l = l
| [] := rfl
| (a::l) := if pa : p a then by rw [take_while, drop_while, if_pos pa, if_pos pa, cons_append,
take_while_append_drop l]
else by rw [take_while, drop_while, if_neg pa, if_neg pa, nil_append]
end filter
/-! ### prefix, suffix, infix -/
@[simp] theorem prefix_append (l₁ l₂ : list α) : l₁ <+: l₁ ++ l₂ := ⟨l₂, rfl⟩
@[simp] theorem suffix_append (l₁ l₂ : list α) : l₂ <:+ l₁ ++ l₂ := ⟨l₁, rfl⟩
theorem infix_append (l₁ l₂ l₃ : list α) : l₂ <:+: l₁ ++ l₂ ++ l₃ := ⟨l₁, l₃, rfl⟩
@[simp] theorem infix_append' (l₁ l₂ l₃ : list α) : l₂ <:+: l₁ ++ (l₂ ++ l₃) :=
by rw ← list.append_assoc; apply infix_append
theorem nil_prefix (l : list α) : [] <+: l := ⟨l, rfl⟩
theorem nil_suffix (l : list α) : [] <:+ l := ⟨l, append_nil _⟩
@[refl] theorem prefix_refl (l : list α) : l <+: l := ⟨[], append_nil _⟩
@[refl] theorem suffix_refl (l : list α) : l <:+ l := ⟨[], rfl⟩
@[simp] theorem suffix_cons (a : α) : ∀ l, l <:+ a :: l := suffix_append [a]
theorem prefix_concat (a : α) (l) : l <+: concat l a := by simp
theorem is_prefix.is_infix {l₁ l₂ : list α} : l₁ <+: l₂ → l₁ <:+: l₂ :=
λ⟨t, h⟩, ⟨[], t, h⟩
theorem is_suffix.is_infix {l₁ l₂ : list α} : l₁ <:+ l₂ → l₁ <:+: l₂ :=
λ⟨t, h⟩, ⟨t, [], by simp only [h, append_nil]⟩
@[refl] theorem infix_refl (l : list α) : l <:+: l := (prefix_refl l).is_infix
theorem nil_infix (l : list α) : [] <:+: l := (nil_prefix l).is_infix
theorem infix_cons {L₁ L₂ : list α} {x : α} : L₁ <:+: L₂ → L₁ <:+: x :: L₂ :=
λ⟨LP, LS, H⟩, ⟨x :: LP, LS, H ▸ rfl⟩
@[trans] theorem is_prefix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <+: l₂ → l₂ <+: l₃ → l₁ <+: l₃
| l ._ ._ ⟨r₁, rfl⟩ ⟨r₂, rfl⟩ := ⟨r₁ ++ r₂, (append_assoc _ _ _).symm⟩
@[trans] theorem is_suffix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <:+ l₂ → l₂ <:+ l₃ → l₁ <:+ l₃
| l ._ ._ ⟨l₁, rfl⟩ ⟨l₂, rfl⟩ := ⟨l₂ ++ l₁, append_assoc _ _ _⟩
@[trans] theorem is_infix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <:+: l₂ → l₂ <:+: l₃ → l₁ <:+: l₃
| l ._ ._ ⟨l₁, r₁, rfl⟩ ⟨l₂, r₂, rfl⟩ := ⟨l₂ ++ l₁, r₁ ++ r₂, by simp only [append_assoc]⟩
protected theorem is_infix.sublist {l₁ l₂ : list α} : l₁ <:+: l₂ → l₁ <+ l₂ :=
λ ⟨s, t, h⟩, by rw [← h]; exact (sublist_append_right _ _).trans (sublist_append_left _ _)
protected theorem is_prefix.sublist {l₁ l₂ : list α} (h : l₁ <+: l₂) : l₁ <+ l₂ :=
h.is_infix.sublist
protected theorem is_suffix.sublist {l₁ l₂ : list α} (h : l₁ <:+ l₂) : l₁ <+ l₂ :=
h.is_infix.sublist
@[simp] theorem reverse_suffix {l₁ l₂ : list α} : reverse l₁ <:+ reverse l₂ ↔ l₁ <+: l₂ :=
⟨λ ⟨r, e⟩, ⟨reverse r,
by rw [← reverse_reverse l₁, ← reverse_append, e, reverse_reverse]⟩,
λ ⟨r, e⟩, ⟨reverse r, by rw [← reverse_append, e]⟩⟩
@[simp] theorem reverse_prefix {l₁ l₂ : list α} : reverse l₁ <+: reverse l₂ ↔ l₁ <:+ l₂ :=
by rw ← reverse_suffix; simp only [reverse_reverse]
theorem infix.length_le {l₁ l₂ : list α} (s : l₁ <:+: l₂) : length l₁ ≤ length l₂ :=
length_le_of_sublist s.sublist
theorem eq_nil_of_infix_nil {l : list α} (s : l <:+: []) : l = [] :=
eq_nil_of_sublist_nil s.sublist
@[simp] theorem eq_nil_iff_infix_nil {l : list α} : l <:+: [] ↔ l = [] :=
⟨eq_nil_of_infix_nil, λ h, h ▸ infix_refl _⟩
theorem eq_nil_of_prefix_nil {l : list α} (s : l <+: []) : l = [] :=
eq_nil_of_infix_nil s.is_infix
@[simp] theorem eq_nil_iff_prefix_nil {l : list α} : l <+: [] ↔ l = [] :=
⟨eq_nil_of_prefix_nil, λ h, h ▸ prefix_refl _⟩
theorem eq_nil_of_suffix_nil {l : list α} (s : l <:+ []) : l = [] :=
eq_nil_of_infix_nil s.is_infix
@[simp] theorem eq_nil_iff_suffix_nil {l : list α} : l <:+ [] ↔ l = [] :=
⟨eq_nil_of_suffix_nil, λ h, h ▸ suffix_refl _⟩
theorem infix_iff_prefix_suffix (l₁ l₂ : list α) : l₁ <:+: l₂ ↔ ∃ t, l₁ <+: t ∧ t <:+ l₂ :=
⟨λ⟨s, t, e⟩, ⟨l₁ ++ t, ⟨_, rfl⟩, by rw [← e, append_assoc]; exact ⟨_, rfl⟩⟩,
λ⟨._, ⟨t, rfl⟩, ⟨s, e⟩⟩, ⟨s, t, by rw append_assoc; exact e⟩⟩
theorem eq_of_infix_of_length_eq {l₁ l₂ : list α} (s : l₁ <:+: l₂) :
length l₁ = length l₂ → l₁ = l₂ :=
eq_of_sublist_of_length_eq s.sublist
theorem eq_of_prefix_of_length_eq {l₁ l₂ : list α} (s : l₁ <+: l₂) :
length l₁ = length l₂ → l₁ = l₂ :=
eq_of_sublist_of_length_eq s.sublist
theorem eq_of_suffix_of_length_eq {l₁ l₂ : list α} (s : l₁ <:+ l₂) :
length l₁ = length l₂ → l₁ = l₂ :=
eq_of_sublist_of_length_eq s.sublist
theorem prefix_of_prefix_length_le : ∀ {l₁ l₂ l₃ : list α},
l₁ <+: l₃ → l₂ <+: l₃ → length l₁ ≤ length l₂ → l₁ <+: l₂
| [] l₂ l₃ h₁ h₂ _ := nil_prefix _
| (a::l₁) (b::l₂) _ ⟨r₁, rfl⟩ ⟨r₂, e⟩ ll := begin
injection e with _ e', subst b,
rcases prefix_of_prefix_length_le ⟨_, rfl⟩ ⟨_, e'⟩
(le_of_succ_le_succ ll) with ⟨r₃, rfl⟩,
exact ⟨r₃, rfl⟩
end
theorem prefix_or_prefix_of_prefix {l₁ l₂ l₃ : list α}
(h₁ : l₁ <+: l₃) (h₂ : l₂ <+: l₃) : l₁ <+: l₂ ∨ l₂ <+: l₁ :=
(le_total (length l₁) (length l₂)).imp
(prefix_of_prefix_length_le h₁ h₂)
(prefix_of_prefix_length_le h₂ h₁)
theorem suffix_of_suffix_length_le {l₁ l₂ l₃ : list α}
(h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) (ll : length l₁ ≤ length l₂) : l₁ <:+ l₂ :=
reverse_prefix.1 $ prefix_of_prefix_length_le
(reverse_prefix.2 h₁) (reverse_prefix.2 h₂) (by simp [ll])
theorem suffix_or_suffix_of_suffix {l₁ l₂ l₃ : list α}
(h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) : l₁ <:+ l₂ ∨ l₂ <:+ l₁ :=
(prefix_or_prefix_of_prefix (reverse_prefix.2 h₁) (reverse_prefix.2 h₂)).imp
reverse_prefix.1 reverse_prefix.1
theorem suffix_cons_iff {x : α} {l₁ l₂ : list α} :
l₁ <:+ x :: l₂ ↔ l₁ = x :: l₂ ∨ l₁ <:+ l₂ :=
begin
split,
{ rintro ⟨⟨hd, tl⟩, hl₃⟩,
{ exact or.inl hl₃ },
{ simp only [cons_append] at hl₃,
exact or.inr ⟨_, hl₃.2⟩ } },
{ rintro (rfl | hl₁),
{ exact (x :: l₂).suffix_refl },
{ exact hl₁.trans (l₂.suffix_cons _) } }
end
theorem infix_of_mem_join : ∀ {L : list (list α)} {l}, l ∈ L → l <:+: join L
| (_ :: L) l (or.inl rfl) := infix_append [] _ _
| (l' :: L) l (or.inr h) :=
is_infix.trans (infix_of_mem_join h) $ (suffix_append _ _).is_infix
theorem prefix_append_right_inj {l₁ l₂ : list α} (l) : l ++ l₁ <+: l ++ l₂ ↔ l₁ <+: l₂ :=
exists_congr $ λ r, by rw [append_assoc, append_right_inj]
theorem prefix_cons_inj {l₁ l₂ : list α} (a) : a :: l₁ <+: a :: l₂ ↔ l₁ <+: l₂ :=
prefix_append_right_inj [a]
theorem take_prefix (n) (l : list α) : take n l <+: l := ⟨_, take_append_drop _ _⟩
theorem take_sublist (n) (l : list α) : take n l <+ l := (take_prefix n l).sublist
theorem take_subset (n) (l : list α) : take n l ⊆ l := (take_sublist n l).subset
theorem mem_of_mem_take {n} {l : list α} {x : α} (h : x ∈ l.take n) : x ∈ l := take_subset n l h
theorem drop_suffix (n) (l : list α) : drop n l <:+ l := ⟨_, take_append_drop _ _⟩
theorem drop_sublist (n) (l : list α) : drop n l <+ l := (drop_suffix n l).sublist
theorem drop_subset (n) (l : list α) : drop n l ⊆ l := (drop_sublist n l).subset
theorem mem_of_mem_drop {n} {l : list α} {x : α} (h : x ∈ l.drop n) : x ∈ l := drop_subset n l h
theorem init_prefix : ∀ (l : list α), l.init <+: l
| [] := ⟨nil, by rw [init, list.append_nil]⟩
| (a :: l) := ⟨_, init_append_last (cons_ne_nil a l)⟩
theorem init_sublist (l : list α) : l.init <+ l := (init_prefix l).sublist
theorem init_subset (l : list α) : l.init ⊆ l := (init_sublist l).subset
theorem mem_of_mem_init {l : list α} {a : α} (h : a ∈ l.init) : a ∈ l := init_subset l h
theorem tail_suffix (l : list α) : tail l <:+ l := by rw ← drop_one; apply drop_suffix
theorem tail_sublist (l : list α) : l.tail <+ l := (tail_suffix l).sublist
theorem tail_subset (l : list α) : tail l ⊆ l := (tail_sublist l).subset
theorem mem_of_mem_tail {l : list α} {a : α} (h : a ∈ l.tail) : a ∈ l := tail_subset l h
theorem prefix_iff_eq_append {l₁ l₂ : list α} : l₁ <+: l₂ ↔ l₁ ++ drop (length l₁) l₂ = l₂ :=
⟨by rintros ⟨r, rfl⟩; rw drop_left, λ e, ⟨_, e⟩⟩
theorem suffix_iff_eq_append {l₁ l₂ : list α} :
l₁ <:+ l₂ ↔ take (length l₂ - length l₁) l₂ ++ l₁ = l₂ :=
⟨by rintros ⟨r, rfl⟩; simp only [length_append, add_tsub_cancel_right, take_left], λ e, ⟨_, e⟩⟩
theorem prefix_iff_eq_take {l₁ l₂ : list α} : l₁ <+: l₂ ↔ l₁ = take (length l₁) l₂ :=
⟨λ h, append_right_cancel $
(prefix_iff_eq_append.1 h).trans (take_append_drop _ _).symm,
λ e, e.symm ▸ take_prefix _ _⟩
theorem suffix_iff_eq_drop {l₁ l₂ : list α} : l₁ <:+ l₂ ↔ l₁ = drop (length l₂ - length l₁) l₂ :=
⟨λ h, append_left_cancel $
(suffix_iff_eq_append.1 h).trans (take_append_drop _ _).symm,
λ e, e.symm ▸ drop_suffix _ _⟩
instance decidable_prefix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+: l₂)
| [] l₂ := is_true ⟨l₂, rfl⟩
| (a::l₁) [] := is_false $ λ ⟨t, te⟩, list.no_confusion te
| (a::l₁) (b::l₂) :=
if h : a = b then
@decidable_of_iff _ _ (by rw [← h, prefix_cons_inj])
(decidable_prefix l₁ l₂)
else
is_false $ λ ⟨t, te⟩, h $ by injection te
-- Alternatively, use mem_tails
instance decidable_suffix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+ l₂)
| [] l₂ := is_true ⟨l₂, append_nil _⟩
| (a::l₁) [] := is_false $ mt (length_le_of_sublist ∘ is_suffix.sublist) dec_trivial
| l₁ l₂ := let len1 := length l₁, len2 := length l₂ in
if hl : len1 ≤ len2 then
decidable_of_iff' (l₁ = drop (len2-len1) l₂) suffix_iff_eq_drop
else is_false $ λ h, hl $ length_le_of_sublist $ h.sublist
lemma prefix_take_le_iff {L : list (list (option α))} {m n : ℕ} (hm : m < L.length) :
(take m L) <+: (take n L) ↔ m ≤ n :=
begin
simp only [prefix_iff_eq_take, length_take],
induction m with m IH generalizing L n,
{ simp only [min_eq_left, eq_self_iff_true, nat.zero_le, take] },
{ cases n,
{ simp only [nat.nat_zero_eq_zero, nonpos_iff_eq_zero, take, take_nil],
split,
{ cases L,
{ exact absurd hm (not_lt_of_le m.succ.zero_le) },
{ simp only [forall_prop_of_false, not_false_iff, take] } },
{ intro h,
contradiction } },
{ cases L with l ls,
{ exact absurd hm (not_lt_of_le m.succ.zero_le) },
{ simp only [length] at hm,
specialize @IH ls n (nat.lt_of_succ_lt_succ hm),
simp only [le_of_lt (nat.lt_of_succ_lt_succ hm), min_eq_left] at IH,
simp only [le_of_lt hm, IH, true_and, min_eq_left, eq_self_iff_true, length, take],
exact ⟨nat.succ_le_succ, nat.le_of_succ_le_succ⟩ } } },
end
lemma cons_prefix_iff {l l' : list α} {x y : α} :
x :: l <+: y :: l' ↔ x = y ∧ l <+: l' :=
begin
split,
{ rintro ⟨L, hL⟩,
simp only [cons_append] at hL,
exact ⟨hL.left, ⟨L, hL.right⟩⟩ },
{ rintro ⟨rfl, h⟩,
rwa [prefix_cons_inj] },
end
lemma map_prefix {l l' : list α} (f : α → β) (h : l <+: l') :
l.map f <+: l'.map f :=
begin
induction l with hd tl hl generalizing l',
{ simp only [nil_prefix, map_nil] },
{ cases l' with hd' tl',
{ simpa only using eq_nil_of_prefix_nil h },
{ rw cons_prefix_iff at h,
simp only [h, prefix_cons_inj, hl, map] } },
end
lemma is_prefix.filter_map {l l' : list α} (h : l <+: l') (f : α → option β) :
l.filter_map f <+: l'.filter_map f :=
begin
induction l with hd tl hl generalizing l',
{ simp only [nil_prefix, filter_map_nil] },
{ cases l' with hd' tl',
{ simpa only using eq_nil_of_prefix_nil h },
{ rw cons_prefix_iff at h,
rw [←@singleton_append _ hd _, ←@singleton_append _ hd' _, filter_map_append,
filter_map_append, h.left, prefix_append_right_inj],
exact hl h.right } },
end
lemma is_prefix.reduce_option {l l' : list (option α)} (h : l <+: l') :
l.reduce_option <+: l'.reduce_option :=
h.filter_map id
lemma is_prefix.filter (p : α → Prop) [decidable_pred p]
⦃l l' : list α⦄ (h : l <+: l') : filter p l <+: filter p l' :=
begin
obtain ⟨xs, rfl⟩ := h,
rw filter_append,
exact prefix_append _ _
end
lemma is_suffix.filter (p : α → Prop) [decidable_pred p]
⦃l l' : list α⦄ (h : l <:+ l') : filter p l <:+ filter p l' :=
begin
obtain ⟨xs, rfl⟩ := h,
rw filter_append,
exact suffix_append _ _
end
lemma is_infix.filter (p : α → Prop) [decidable_pred p]
⦃l l' : list α⦄ (h : l <:+: l') : filter p l <:+: filter p l' :=
begin
obtain ⟨xs, ys, rfl⟩ := h,
rw [filter_append, filter_append],
exact infix_append _ _ _
end
@[simp] theorem mem_inits : ∀ (s t : list α), s ∈ inits t ↔ s <+: t
| s [] := suffices s = nil ↔ s <+: nil, by simpa only [inits, mem_singleton],
⟨λh, h.symm ▸ prefix_refl [], eq_nil_of_prefix_nil⟩
| s (a::t) :=
suffices (s = nil ∨ ∃ l ∈ inits t, a :: l = s) ↔ s <+: a :: t, by simpa,
⟨λo, match s, o with
| ._, or.inl rfl := ⟨_, rfl⟩
| s, or.inr ⟨r, hr, hs⟩ := let ⟨s, ht⟩ := (mem_inits _ _).1 hr in
by rw [← hs, ← ht]; exact ⟨s, rfl⟩
end, λmi, match s, mi with
| [], ⟨._, rfl⟩ := or.inl rfl
| (b::s), ⟨r, hr⟩ := list.no_confusion hr $ λba (st : s++r = t), or.inr $
by rw ba; exact ⟨_, (mem_inits _ _).2 ⟨_, st⟩, rfl⟩
end⟩
@[simp] theorem mem_tails : ∀ (s t : list α), s ∈ tails t ↔ s <:+ t
| s [] := by simp only [tails, mem_singleton];
exact ⟨λh, by rw h; exact suffix_refl [], eq_nil_of_suffix_nil⟩
| s (a::t) := by simp only [tails, mem_cons_iff, mem_tails s t];
exact show s = a :: t ∨ s <:+ t ↔ s <:+ a :: t, from
⟨λo, match s, t, o with
| ._, t, or.inl rfl := suffix_refl _
| s, ._, or.inr ⟨l, rfl⟩ := ⟨a::l, rfl⟩
end, λe, match s, t, e with
| ._, t, ⟨[], rfl⟩ := or.inl rfl
| s, t, ⟨b::l, he⟩ := list.no_confusion he (λab lt, or.inr ⟨l, lt⟩)
end⟩
lemma inits_cons (a : α) (l : list α) : inits (a :: l) = [] :: l.inits.map (λ t, a :: t) :=
by simp
lemma tails_cons (a : α) (l : list α) : tails (a :: l) = (a :: l) :: l.tails :=
by simp
@[simp]
lemma inits_append : ∀ (s t : list α), inits (s ++ t) = s.inits ++ t.inits.tail.map (λ l, s ++ l)
| [] [] := by simp
| [] (a::t) := by simp
| (a::s) t := by simp [inits_append s t]
@[simp]
lemma tails_append : ∀ (s t : list α), tails (s ++ t) = s.tails.map (λ l, l ++ t) ++ t.tails.tail
| [] [] := by simp
| [] (a::t) := by simp
| (a::s) t := by simp [tails_append s t]
-- the lemma names `inits_eq_tails` and `tails_eq_inits` are like `sublists_eq_sublists'`
lemma inits_eq_tails :
∀ (l : list α), l.inits = (reverse $ map reverse $ tails $ reverse l)
| [] := by simp
| (a :: l) := by simp [inits_eq_tails l, map_eq_map_iff]
lemma tails_eq_inits :
∀ (l : list α), l.tails = (reverse $ map reverse $ inits $ reverse l)
| [] := by simp
| (a :: l) := by simp [tails_eq_inits l, append_left_inj]
lemma inits_reverse (l : list α) : inits (reverse l) = reverse (map reverse l.tails) :=
by { rw tails_eq_inits l, simp [reverse_involutive.comp_self], }
lemma tails_reverse (l : list α) : tails (reverse l) = reverse (map reverse l.inits) :=
by { rw inits_eq_tails l, simp [reverse_involutive.comp_self], }
lemma map_reverse_inits (l : list α) : map reverse l.inits = (reverse $ tails $ reverse l) :=
by { rw inits_eq_tails l, simp [reverse_involutive.comp_self], }
lemma map_reverse_tails (l : list α) : map reverse l.tails = (reverse $ inits $ reverse l) :=
by { rw tails_eq_inits l, simp [reverse_involutive.comp_self], }
@[simp] lemma length_tails (l : list α) : length (tails l) = length l + 1 :=
begin
induction l with x l IH,
{ simp },
{ simpa using IH }
end
@[simp] lemma length_inits (l : list α) : length (inits l) = length l + 1 :=
by simp [inits_eq_tails]
@[simp] lemma nth_le_tails (l : list α) (n : ℕ) (hn : n < length (tails l)) :
nth_le (tails l) n hn = l.drop n :=
begin
induction l with x l IH generalizing n,
{ simp },
{ cases n,
{ simp },
{ simpa using IH n _ } },
end
@[simp] lemma nth_le_inits (l : list α) (n : ℕ) (hn : n < length (inits l)) :
nth_le (inits l) n hn = l.take n :=
begin
induction l with x l IH generalizing n,
{ simp },
{ cases n,
{ simp },
{ simpa using IH n _ } }
end
instance decidable_infix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+: l₂)
| [] l₂ := is_true ⟨[], l₂, rfl⟩
| (a::l₁) [] := is_false $ λ⟨s, t, te⟩, absurd te $ append_ne_nil_of_ne_nil_left _ _ $
append_ne_nil_of_ne_nil_right _ _ $ λh, list.no_confusion h
| l₁ l₂ := decidable_of_decidable_of_iff (list.decidable_bex (λt, l₁ <+: t) (tails l₂)) $
by refine (exists_congr (λt, _)).trans (infix_iff_prefix_suffix _ _).symm;
exact ⟨λ⟨h1, h2⟩, ⟨h2, (mem_tails _ _).1 h1⟩, λ⟨h2, h1⟩, ⟨(mem_tails _ _).2 h1, h2⟩⟩
/-! ### insert -/
section insert
variable [decidable_eq α]
@[simp] theorem insert_nil (a : α) : insert a nil = [a] := rfl
theorem insert.def (a : α) (l : list α) : insert a l = if a ∈ l then l else a :: l := rfl
@[simp, priority 980]
theorem insert_of_mem {a : α} {l : list α} (h : a ∈ l) : insert a l = l :=
by simp only [insert.def, if_pos h]
@[simp, priority 970]
theorem insert_of_not_mem {a : α} {l : list α} (h : a ∉ l) : insert a l = a :: l :=
by simp only [insert.def, if_neg h]; split; refl
@[simp] theorem mem_insert_iff {a b : α} {l : list α} : a ∈ insert b l ↔ a = b ∨ a ∈ l :=
begin
by_cases h' : b ∈ l,
{ simp only [insert_of_mem h'],
apply (or_iff_right_of_imp _).symm,
exact λ e, e.symm ▸ h' },
simp only [insert_of_not_mem h', mem_cons_iff]
end
@[simp] theorem suffix_insert (a : α) (l : list α) : l <:+ insert a l :=
by by_cases a ∈ l; [simp only [insert_of_mem h], simp only [insert_of_not_mem h, suffix_cons]]
theorem infix_insert (a : α) (l : list α) : l <:+: insert a l := (suffix_insert a l).is_infix
theorem sublist_insert (a : α) (l : list α) : l <+ insert a l := (suffix_insert a l).sublist
theorem subset_insert (a : α) (l : list α) : l ⊆ insert a l := (sublist_insert a l).subset
@[simp] theorem mem_insert_self (a : α) (l : list α) : a ∈ insert a l :=
mem_insert_iff.2 (or.inl rfl)
theorem mem_insert_of_mem {a b : α} {l : list α} (h : a ∈ l) : a ∈ insert b l :=
mem_insert_iff.2 (or.inr h)
theorem eq_or_mem_of_mem_insert {a b : α} {l : list α} (h : a ∈ insert b l) : a = b ∨ a ∈ l :=
mem_insert_iff.1 h
@[simp] theorem length_insert_of_mem {a : α} {l : list α} (h : a ∈ l) :
length (insert a l) = length l :=
by rw insert_of_mem h
@[simp] theorem length_insert_of_not_mem {a : α} {l : list α} (h : a ∉ l) :
length (insert a l) = length l + 1 :=
by rw insert_of_not_mem h; refl
end insert
/-! ### erasep -/
section erasep
variables {p : α → Prop} [decidable_pred p]
@[simp] theorem erasep_nil : [].erasep p = [] := rfl
theorem erasep_cons (a : α) (l : list α) :
(a :: l).erasep p = if p a then l else a :: l.erasep p := rfl
@[simp] theorem erasep_cons_of_pos {a : α} {l : list α} (h : p a) : (a :: l).erasep p = l :=
by simp [erasep_cons, h]
@[simp] theorem erasep_cons_of_neg {a : α} {l : list α} (h : ¬ p a) :
(a::l).erasep p = a :: l.erasep p :=
by simp [erasep_cons, h]
theorem erasep_of_forall_not {l : list α}
(h : ∀ a ∈ l, ¬ p a) : l.erasep p = l :=
by induction l with _ _ ih; [refl,
simp [h _ (or.inl rfl), ih (forall_mem_of_forall_mem_cons h)]]
theorem exists_of_erasep {l : list α} {a} (al : a ∈ l) (pa : p a) :
∃ a l₁ l₂, (∀ b ∈ l₁, ¬ p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ l.erasep p = l₁ ++ l₂ :=
begin
induction l with b l IH, {cases al},
by_cases pb : p b,
{ exact ⟨b, [], l, forall_mem_nil _, pb, by simp [pb]⟩ },
{ rcases al with rfl | al, {exact pb.elim pa},
rcases IH al with ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩,
exact ⟨c, b::l₁, l₂, forall_mem_cons.2 ⟨pb, h₁⟩,
h₂, by rw h₃; refl, by simp [pb, h₄]⟩ }
end
theorem exists_or_eq_self_of_erasep (p : α → Prop) [decidable_pred p] (l : list α) :
l.erasep p = l ∨ ∃ a l₁ l₂, (∀ b ∈ l₁, ¬ p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ l.erasep p = l₁ ++ l₂ :=
begin
by_cases h : ∃ a ∈ l, p a,
{ rcases h with ⟨a, ha, pa⟩,
exact or.inr (exists_of_erasep ha pa) },
{ simp at h, exact or.inl (erasep_of_forall_not h) }
end
@[simp] theorem length_erasep_of_mem {l : list α} {a} (al : a ∈ l) (pa : p a) :
length (l.erasep p) = pred (length l) :=
by rcases exists_of_erasep al pa with ⟨_, l₁, l₂, _, _, e₁, e₂⟩;
rw e₂; simp [-add_comm, e₁]; refl
theorem erasep_append_left {a : α} (pa : p a) :
∀ {l₁ : list α} (l₂), a ∈ l₁ → (l₁++l₂).erasep p = l₁.erasep p ++ l₂
| (x::xs) l₂ h := begin
by_cases h' : p x; simp [h'],
rw erasep_append_left l₂ (mem_of_ne_of_mem (mt _ h') h),
rintro rfl, exact pa
end
theorem erasep_append_right :
∀ {l₁ : list α} (l₂), (∀ b ∈ l₁, ¬ p b) → (l₁++l₂).erasep p = l₁ ++ l₂.erasep p
| [] l₂ h := rfl
| (x::xs) l₂ h := by simp [(forall_mem_cons.1 h).1,
erasep_append_right _ (forall_mem_cons.1 h).2]
theorem erasep_sublist (l : list α) : l.erasep p <+ l :=
by rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩;
[rw h, {rw [h₄, h₃], simp}]
theorem erasep_subset (l : list α) : l.erasep p ⊆ l :=
(erasep_sublist l).subset
theorem sublist.erasep {l₁ l₂ : list α} (s : l₁ <+ l₂) : l₁.erasep p <+ l₂.erasep p :=
begin
induction s,
case list.sublist.slnil { refl },
case list.sublist.cons : l₁ l₂ a s IH {
by_cases h : p a; simp [h],
exacts [IH.trans (erasep_sublist _), IH.cons _ _ _] },
case list.sublist.cons2 : l₁ l₂ a s IH {
by_cases h : p a; simp [h],
exacts [s, IH.cons2 _ _ _] }
end
theorem mem_of_mem_erasep {a : α} {l : list α} : a ∈ l.erasep p → a ∈ l :=
@erasep_subset _ _ _ _ _
@[simp] theorem mem_erasep_of_neg {a : α} {l : list α} (pa : ¬ p a) : a ∈ l.erasep p ↔ a ∈ l :=
⟨mem_of_mem_erasep, λ al, begin
rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩,
{ rwa h },
{ rw h₄, rw h₃ at al,
have : a ≠ c, {rintro rfl, exact pa.elim h₂},
simpa [this] using al }
end⟩
theorem erasep_map (f : β → α) :
∀ (l : list β), (map f l).erasep p = map f (l.erasep (p ∘ f))
| [] := rfl
| (b::l) := by by_cases p (f b); simp [h, erasep_map l]
@[simp] theorem extractp_eq_find_erasep :
∀ l : list α, extractp p l = (find p l, erasep p l)
| [] := rfl
| (a::l) := by by_cases pa : p a; simp [extractp, pa, extractp_eq_find_erasep l]
end erasep
/-! ### erase -/
section erase
variable [decidable_eq α]
@[simp] theorem erase_nil (a : α) : [].erase a = [] := rfl
theorem erase_cons (a b : α) (l : list α) :
(b :: l).erase a = if b = a then l else b :: l.erase a := rfl
@[simp] theorem erase_cons_head (a : α) (l : list α) : (a :: l).erase a = l :=
by simp only [erase_cons, if_pos rfl]
@[simp] theorem erase_cons_tail {a b : α} (l : list α) (h : b ≠ a) :
(b::l).erase a = b :: l.erase a :=
by simp only [erase_cons, if_neg h]; split; refl
theorem erase_eq_erasep (a : α) (l : list α) : l.erase a = l.erasep (eq a) :=
by { induction l with b l, {refl},
by_cases a = b; [simp [h], simp [h, ne.symm h, *]] }
@[simp, priority 980]
theorem erase_of_not_mem {a : α} {l : list α} (h : a ∉ l) : l.erase a = l :=
by rw [erase_eq_erasep, erasep_of_forall_not]; rintro b h' rfl; exact h h'
theorem exists_erase_eq {a : α} {l : list α} (h : a ∈ l) :
∃ l₁ l₂, a ∉ l₁ ∧ l = l₁ ++ a :: l₂ ∧ l.erase a = l₁ ++ l₂ :=
by rcases exists_of_erasep h rfl with ⟨_, l₁, l₂, h₁, rfl, h₂, h₃⟩;
rw erase_eq_erasep; exact ⟨l₁, l₂, λ h, h₁ _ h rfl, h₂, h₃⟩
@[simp] theorem length_erase_of_mem {a : α} {l : list α} (h : a ∈ l) :
length (l.erase a) = pred (length l) :=
by rw erase_eq_erasep; exact length_erasep_of_mem h rfl
theorem erase_append_left {a : α} {l₁ : list α} (l₂) (h : a ∈ l₁) :
(l₁++l₂).erase a = l₁.erase a ++ l₂ :=
by simp [erase_eq_erasep]; exact erasep_append_left (by refl) l₂ h
theorem erase_append_right {a : α} {l₁ : list α} (l₂) (h : a ∉ l₁) :
(l₁++l₂).erase a = l₁ ++ l₂.erase a :=
by rw [erase_eq_erasep, erase_eq_erasep, erasep_append_right];
rintro b h' rfl; exact h h'
theorem erase_sublist (a : α) (l : list α) : l.erase a <+ l :=
by rw erase_eq_erasep; apply erasep_sublist
theorem erase_subset (a : α) (l : list α) : l.erase a ⊆ l :=
(erase_sublist a l).subset
theorem sublist.erase (a : α) {l₁ l₂ : list α} (h : l₁ <+ l₂) : l₁.erase a <+ l₂.erase a :=
by simp [erase_eq_erasep]; exact sublist.erasep h
theorem mem_of_mem_erase {a b : α} {l : list α} : a ∈ l.erase b → a ∈ l :=
@erase_subset _ _ _ _ _
@[simp] theorem mem_erase_of_ne {a b : α} {l : list α} (ab : a ≠ b) : a ∈ l.erase b ↔ a ∈ l :=
by rw erase_eq_erasep; exact mem_erasep_of_neg ab.symm
theorem erase_comm (a b : α) (l : list α) : (l.erase a).erase b = (l.erase b).erase a :=
if ab : a = b then by rw ab else
if ha : a ∈ l then
if hb : b ∈ l then match l, l.erase a, exists_erase_eq ha, hb with
| ._, ._, ⟨l₁, l₂, ha', rfl, rfl⟩, hb :=
if h₁ : b ∈ l₁ then
by rw [erase_append_left _ h₁, erase_append_left _ h₁,
erase_append_right _ (mt mem_of_mem_erase ha'), erase_cons_head]
else
by rw [erase_append_right _ h₁, erase_append_right _ h₁, erase_append_right _ ha',
erase_cons_tail _ ab, erase_cons_head]
end
else by simp only [erase_of_not_mem hb, erase_of_not_mem (mt mem_of_mem_erase hb)]
else by simp only [erase_of_not_mem ha, erase_of_not_mem (mt mem_of_mem_erase ha)]
theorem map_erase [decidable_eq β] {f : α → β} (finj : injective f) {a : α}
(l : list α) : map f (l.erase a) = (map f l).erase (f a) :=
by rw [erase_eq_erasep, erase_eq_erasep, erasep_map]; congr;
ext b; simp [finj.eq_iff]
theorem map_foldl_erase [decidable_eq β] {f : α → β} (finj : injective f) {l₁ l₂ : list α} :
map f (foldl list.erase l₁ l₂) = foldl (λ l a, l.erase (f a)) (map f l₁) l₂ :=
by induction l₂ generalizing l₁; [refl,
simp only [foldl_cons, map_erase finj, *]]
end erase
/-! ### diff -/
section diff
variable [decidable_eq α]
@[simp] theorem diff_nil (l : list α) : l.diff [] = l := rfl
@[simp] theorem diff_cons (l₁ l₂ : list α) (a : α) : l₁.diff (a::l₂) = (l₁.erase a).diff l₂ :=
if h : a ∈ l₁ then by simp only [list.diff, if_pos h]
else by simp only [list.diff, if_neg h, erase_of_not_mem h]
lemma diff_cons_right (l₁ l₂ : list α) (a : α) : l₁.diff (a::l₂) = (l₁.diff l₂).erase a :=
begin
induction l₂ with b l₂ ih generalizing l₁ a,
{ simp_rw [diff_cons, diff_nil] },
{ rw [diff_cons, diff_cons, erase_comm, ← diff_cons, ih, ← diff_cons] }
end
lemma diff_erase (l₁ l₂ : list α) (a : α) : (l₁.diff l₂).erase a = (l₁.erase a).diff l₂ :=
by rw [← diff_cons_right, diff_cons]
@[simp] theorem nil_diff (l : list α) : [].diff l = [] :=
by induction l; [refl, simp only [*, diff_cons, erase_of_not_mem (not_mem_nil _)]]
theorem diff_eq_foldl : ∀ (l₁ l₂ : list α), l₁.diff l₂ = foldl list.erase l₁ l₂
| l₁ [] := rfl
| l₁ (a::l₂) := (diff_cons l₁ l₂ a).trans (diff_eq_foldl _ _)
@[simp] theorem diff_append (l₁ l₂ l₃ : list α) : l₁.diff (l₂ ++ l₃) = (l₁.diff l₂).diff l₃ :=
by simp only [diff_eq_foldl, foldl_append]
@[simp] theorem map_diff [decidable_eq β] {f : α → β} (finj : injective f) {l₁ l₂ : list α} :
map f (l₁.diff l₂) = (map f l₁).diff (map f l₂) :=
by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj]
theorem diff_sublist : ∀ l₁ l₂ : list α, l₁.diff l₂ <+ l₁
| l₁ [] := sublist.refl _
| l₁ (a::l₂) := calc l₁.diff (a :: l₂) = (l₁.erase a).diff l₂ : diff_cons _ _ _
... <+ l₁.erase a : diff_sublist _ _
... <+ l₁ : list.erase_sublist _ _
theorem diff_subset (l₁ l₂ : list α) : l₁.diff l₂ ⊆ l₁ :=
(diff_sublist _ _).subset
theorem mem_diff_of_mem {a : α} : ∀ {l₁ l₂ : list α}, a ∈ l₁ → a ∉ l₂ → a ∈ l₁.diff l₂
| l₁ [] h₁ h₂ := h₁
| l₁ (b::l₂) h₁ h₂ := by rw diff_cons; exact
mem_diff_of_mem ((mem_erase_of_ne (ne_of_not_mem_cons h₂)).2 h₁) (not_mem_of_not_mem_cons h₂)
theorem sublist.diff_right : ∀ {l₁ l₂ l₃: list α}, l₁ <+ l₂ → l₁.diff l₃ <+ l₂.diff l₃
| l₁ l₂ [] h := h
| l₁ l₂ (a::l₃) h := by simp only
[diff_cons, (h.erase _).diff_right]
theorem erase_diff_erase_sublist_of_sublist {a : α} : ∀ {l₁ l₂ : list α},
l₁ <+ l₂ → (l₂.erase a).diff (l₁.erase a) <+ l₂.diff l₁
| [] l₂ h := erase_sublist _ _
| (b::l₁) l₂ h := if heq : b = a then by simp only [heq, erase_cons_head, diff_cons]
else by simpa only [erase_cons_head, erase_cons_tail _ heq, diff_cons,
erase_comm a b l₂]
using erase_diff_erase_sublist_of_sublist (h.erase b)
end diff
/-! ### enum -/
theorem length_enum_from : ∀ n (l : list α), length (enum_from n l) = length l
| n [] := rfl
| n (a::l) := congr_arg nat.succ (length_enum_from _ _)
theorem length_enum : ∀ (l : list α), length (enum l) = length l := length_enum_from _
@[simp] theorem enum_from_nth : ∀ n (l : list α) m,
nth (enum_from n l) m = (λ a, (n + m, a)) <$> nth l m
| n [] m := rfl
| n (a :: l) 0 := rfl
| n (a :: l) (m+1) := (enum_from_nth (n+1) l m).trans $
by rw [add_right_comm]; refl
@[simp] theorem enum_nth : ∀ (l : list α) n,
nth (enum l) n = (λ a, (n, a)) <$> nth l n :=
by simp only [enum, enum_from_nth, zero_add]; intros; refl
@[simp] theorem enum_from_map_snd : ∀ n (l : list α),
map prod.snd (enum_from n l) = l
| n [] := rfl
| n (a :: l) := congr_arg (cons _) (enum_from_map_snd _ _)
@[simp] theorem enum_map_snd : ∀ (l : list α),
map prod.snd (enum l) = l := enum_from_map_snd _
theorem mem_enum_from {x : α} {i : ℕ} :
∀ {j : ℕ} (xs : list α), (i, x) ∈ xs.enum_from j → j ≤ i ∧ i < j + xs.length ∧ x ∈ xs
| j [] := by simp [enum_from]
| j (y :: ys) :=
suffices i = j ∧ x = y ∨ (i, x) ∈ enum_from (j + 1) ys →
j ≤ i ∧ i < j + (length ys + 1) ∧ (x = y ∨ x ∈ ys),
by simpa [enum_from, mem_enum_from ys],
begin
rintro (h|h),
{ refine ⟨le_of_eq h.1.symm,h.1 ▸ _,or.inl h.2⟩,
apply nat.lt_add_of_pos_right; simp },
{ obtain ⟨hji, hijlen, hmem⟩ := mem_enum_from _ h,
refine ⟨_, _, _⟩,
{ exact le_trans (nat.le_succ _) hji },
{ convert hijlen using 1, ac_refl },
{ simp [hmem] } }
end
section choose
variables (p : α → Prop) [decidable_pred p] (l : list α)
lemma choose_spec (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) :=
(choose_x p l hp).property
lemma choose_mem (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1
lemma choose_property (hp : ∃ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2
end choose
/-! ### map₂_left' -/
section map₂_left'
-- The definitional equalities for `map₂_left'` can already be used by the
-- simplifie because `map₂_left'` is marked `@[simp]`.
@[simp] theorem map₂_left'_nil_right (f : α → option β → γ) (as) :
map₂_left' f as [] = (as.map (λ a, f a none), []) :=
by cases as; refl
end map₂_left'
/-! ### map₂_right' -/
section map₂_right'
variables (f : option α → β → γ) (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem map₂_right'_nil_left :
map₂_right' f [] bs = (bs.map (f none), []) :=
by cases bs; refl
@[simp] theorem map₂_right'_nil_right :
map₂_right' f as [] = ([], as) :=
rfl
@[simp] theorem map₂_right'_nil_cons :
map₂_right' f [] (b :: bs) = (f none b :: bs.map (f none), []) :=
rfl
@[simp] theorem map₂_right'_cons_cons :
map₂_right' f (a :: as) (b :: bs) =
let rec := map₂_right' f as bs in
(f (some a) b :: rec.fst, rec.snd) :=
rfl
end map₂_right'
/-! ### zip_left' -/
section zip_left'
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_left'_nil_right :
zip_left' as ([] : list β) = (as.map (λ a, (a, none)), []) :=
by cases as; refl
@[simp] theorem zip_left'_nil_left :
zip_left' ([] : list α) bs = ([], bs) :=
rfl
@[simp] theorem zip_left'_cons_nil :
zip_left' (a :: as) ([] : list β) = ((a, none) :: as.map (λ a, (a, none)), []) :=
rfl
@[simp] theorem zip_left'_cons_cons :
zip_left' (a :: as) (b :: bs) =
let rec := zip_left' as bs in
((a, some b) :: rec.fst, rec.snd) :=
rfl
end zip_left'
/-! ### zip_right' -/
section zip_right'
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_right'_nil_left :
zip_right' ([] : list α) bs = (bs.map (λ b, (none, b)), []) :=
by cases bs; refl
@[simp] theorem zip_right'_nil_right :
zip_right' as ([] : list β) = ([], as) :=
rfl
@[simp] theorem zip_right'_nil_cons :
zip_right' ([] : list α) (b :: bs) = ((none, b) :: bs.map (λ b, (none, b)), []) :=
rfl
@[simp] theorem zip_right'_cons_cons :
zip_right' (a :: as) (b :: bs) =
let rec := zip_right' as bs in
((some a, b) :: rec.fst, rec.snd) :=
rfl
end zip_right'
/-! ### map₂_left -/
section map₂_left
variables (f : α → option β → γ) (as : list α)
-- The definitional equalities for `map₂_left` can already be used by the
-- simplifier because `map₂_left` is marked `@[simp]`.
@[simp] theorem map₂_left_nil_right :
map₂_left f as [] = as.map (λ a, f a none) :=
by cases as; refl
theorem map₂_left_eq_map₂_left' : ∀ as bs,
map₂_left f as bs = (map₂_left' f as bs).fst
| [] bs := by simp!
| (a :: as) [] := by simp!
| (a :: as) (b :: bs) := by simp! [*]
theorem map₂_left_eq_map₂ : ∀ as bs,
length as ≤ length bs →
map₂_left f as bs = map₂ (λ a b, f a (some b)) as bs
| [] [] h := by simp!
| [] (b :: bs) h := by simp!
| (a :: as) [] h := by { simp at h, contradiction }
| (a :: as) (b :: bs) h := by { simp at h, simp! [*] }
end map₂_left
/-! ### map₂_right -/
section map₂_right
variables (f : option α → β → γ) (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem map₂_right_nil_left :
map₂_right f [] bs = bs.map (f none) :=
by cases bs; refl
@[simp] theorem map₂_right_nil_right :
map₂_right f as [] = [] :=
rfl
@[simp] theorem map₂_right_nil_cons :
map₂_right f [] (b :: bs) = f none b :: bs.map (f none) :=
rfl
@[simp] theorem map₂_right_cons_cons :
map₂_right f (a :: as) (b :: bs) = f (some a) b :: map₂_right f as bs :=
rfl
theorem map₂_right_eq_map₂_right' :
map₂_right f as bs = (map₂_right' f as bs).fst :=
by simp only [map₂_right, map₂_right', map₂_left_eq_map₂_left']
theorem map₂_right_eq_map₂ (h : length bs ≤ length as) :
map₂_right f as bs = map₂ (λ a b, f (some a) b) as bs :=
begin
have : (λ a b, flip f a (some b)) = (flip (λ a b, f (some a) b)) := rfl,
simp only [map₂_right, map₂_left_eq_map₂, map₂_flip, *]
end
end map₂_right
/-! ### zip_left -/
section zip_left
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_left_nil_right :
zip_left as ([] : list β) = as.map (λ a, (a, none)) :=
by cases as; refl
@[simp] theorem zip_left_nil_left :
zip_left ([] : list α) bs = [] :=
rfl
@[simp] theorem zip_left_cons_nil :
zip_left (a :: as) ([] : list β) = (a, none) :: as.map (λ a, (a, none)) :=
rfl
@[simp] theorem zip_left_cons_cons :
zip_left (a :: as) (b :: bs) = (a, some b) :: zip_left as bs :=
rfl
theorem zip_left_eq_zip_left' :
zip_left as bs = (zip_left' as bs).fst :=
by simp only [zip_left, zip_left', map₂_left_eq_map₂_left']
end zip_left
/-! ### zip_right -/
section zip_right
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_right_nil_left :
zip_right ([] : list α) bs = bs.map (λ b, (none, b)) :=
by cases bs; refl
@[simp] theorem zip_right_nil_right :
zip_right as ([] : list β) = [] :=
rfl
@[simp] theorem zip_right_nil_cons :
zip_right ([] : list α) (b :: bs) = (none, b) :: bs.map (λ b, (none, b)) :=
rfl
@[simp] theorem zip_right_cons_cons :
zip_right (a :: as) (b :: bs) = (some a, b) :: zip_right as bs :=
rfl
theorem zip_right_eq_zip_right' :
zip_right as bs = (zip_right' as bs).fst :=
by simp only [zip_right, zip_right', map₂_right_eq_map₂_right']
end zip_right
/-! ### to_chunks -/
section to_chunks
@[simp] theorem to_chunks_nil (n) : @to_chunks α n [] = [] := by cases n; refl
theorem to_chunks_aux_eq (n) : ∀ xs i,
@to_chunks_aux α n xs i = (xs.take i, (xs.drop i).to_chunks (n+1))
| [] i := by cases i; refl
| (x::xs) 0 := by rw [to_chunks_aux, drop, to_chunks]; cases to_chunks_aux n xs n; refl
| (x::xs) (i+1) := by rw [to_chunks_aux, to_chunks_aux_eq]; refl
theorem to_chunks_eq_cons' (n) : ∀ {xs : list α} (h : xs ≠ []),
xs.to_chunks (n+1) = xs.take (n+1) :: (xs.drop (n+1)).to_chunks (n+1)
| [] e := (e rfl).elim
| (x::xs) _ := by rw [to_chunks, to_chunks_aux_eq]; refl
theorem to_chunks_eq_cons : ∀ {n} {xs : list α} (n0 : n ≠ 0) (x0 : xs ≠ []),
xs.to_chunks n = xs.take n :: (xs.drop n).to_chunks n
| 0 _ e := (e rfl).elim
| (n+1) xs _ := to_chunks_eq_cons' _
theorem to_chunks_aux_join {n} : ∀ {xs i l L}, @to_chunks_aux α n xs i = (l, L) → l ++ L.join = xs
| [] _ _ _ rfl := rfl
| (x::xs) i l L e := begin
cases i; [
cases e' : to_chunks_aux n xs n with l L,
cases e' : to_chunks_aux n xs i with l L];
{ rw [to_chunks_aux, e', to_chunks_aux] at e, cases e,
exact (congr_arg (cons x) (to_chunks_aux_join e') : _) }
end
@[simp] theorem to_chunks_join : ∀ n xs, (@to_chunks α n xs).join = xs
| n [] := by cases n; refl
| 0 (x::xs) := by simp only [to_chunks, join]; rw append_nil
| (n+1) (x::xs) := begin
rw to_chunks,
cases e : to_chunks_aux n xs n with l L,
exact (congr_arg (cons x) (to_chunks_aux_join e) : _),
end
theorem to_chunks_length_le : ∀ n xs, n ≠ 0 → ∀ l : list α,
l ∈ @to_chunks α n xs → l.length ≤ n
| 0 _ e _ := (e rfl).elim
| (n+1) xs _ l := begin
refine (measure_wf length).induction xs _, intros xs IH h,
by_cases x0 : xs = [], {subst xs, cases h},
rw to_chunks_eq_cons' _ x0 at h, rcases h with rfl|h,
{ apply length_take_le },
{ refine IH _ _ h,
simp only [measure, inv_image, length_drop],
exact tsub_lt_self (length_pos_iff_ne_nil.2 x0) (succ_pos _) },
end
end to_chunks
/-! ### Retroattributes
The list definitions happen earlier than `to_additive`, so here we tag the few multiplicative
definitions that couldn't be tagged earlier.
-/
attribute [to_additive] list.prod -- `list.sum`
attribute [to_additive] alternating_prod -- `list.alternating_sum`
/-! ### Miscellaneous lemmas -/
theorem ilast'_mem : ∀ a l, @ilast' α a l ∈ a :: l
| a [] := or.inl rfl
| a (b::l) := or.inr (ilast'_mem b l)
@[simp] lemma nth_le_attach (L : list α) (i) (H : i < L.attach.length) :
(L.attach.nth_le i H).1 = L.nth_le i (length_attach L ▸ H) :=
calc (L.attach.nth_le i H).1
= (L.attach.map subtype.val).nth_le i (by simpa using H) : by rw nth_le_map'
... = L.nth_le i _ : by congr; apply attach_map_val
@[simp]
theorem mem_map_swap (x : α) (y : β) (xs : list (α × β)) :
(y, x) ∈ map prod.swap xs ↔ (x, y) ∈ xs :=
begin
induction xs with x xs,
{ simp only [not_mem_nil, map_nil] },
{ cases x with a b,
simp only [mem_cons_iff, prod.mk.inj_iff, map, prod.swap_prod_mk,
prod.exists, xs_ih, and_comm] },
end
lemma slice_eq (xs : list α) (n m : ℕ) :
slice n m xs = xs.take n ++ xs.drop (n+m) :=
begin
induction n generalizing xs,
{ simp [slice] },
{ cases xs; simp [slice, *, nat.succ_add], }
end
lemma sizeof_slice_lt [has_sizeof α] (i j : ℕ) (hj : 0 < j) (xs : list α) (hi : i < xs.length) :
sizeof (list.slice i j xs) < sizeof xs :=
begin
induction xs generalizing i j,
case list.nil : i j h
{ cases hi },
case list.cons : x xs xs_ih i j h
{ cases i; simp only [-slice_eq, list.slice],
{ cases j, cases h,
dsimp only [drop], unfold_wf,
apply @lt_of_le_of_lt _ _ _ xs.sizeof,
{ clear_except,
induction xs generalizing j; unfold_wf,
case list.nil : j
{ refl },
case list.cons : xs_hd xs_tl xs_ih j
{ cases j; unfold_wf, refl,
transitivity, apply xs_ih,
simp }, },
unfold_wf, apply zero_lt_one_add, },
{ unfold_wf, apply xs_ih _ _ h,
apply lt_of_succ_lt_succ hi, } },
end
end list
|
01fd7c351ec50d59479969a71ec409f3dcc4e8f3 | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /stage0/src/Lean/Data/Lsp/Basic.lean | a114197c7b96b29db81b0d664b6044b99ddfaccd | [
"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 | tobiasgrosser/lean4 | ce0fd9cca0feba1100656679bf41f0bffdbabb71 | ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f | refs/heads/master | 1,673,103,412,948 | 1,664,930,501,000 | 1,664,930,501,000 | 186,870,185 | 0 | 0 | Apache-2.0 | 1,665,129,237,000 | 1,557,939,901,000 | Lean | UTF-8 | Lean | false | false | 12,677 | lean | /-
Copyright (c) 2020 Marc Huisinga. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Marc Huisinga, Wojciech Nawrocki
-/
import Lean.Data.Json
import Lean.Data.JsonRpc
/-! Defines most of the 'Basic Structures' in the LSP specification
(https://microsoft.github.io/language-server-protocol/specifications/specification-current/),
as well as some utilities.
Since LSP is Json-based, Ints/Nats are represented by Floats on the wire. -/
namespace Lean
namespace Lsp
open Json
structure CancelParams where
id : JsonRpc.RequestID
deriving Inhabited, BEq, ToJson, FromJson
abbrev DocumentUri := String
/-- We adopt the convention that zero-based UTF-16 positions as sent by LSP clients
are represented by `Lsp.Position` while internally we mostly use `String.Pos` UTF-8
offsets. For diagnostics, one-based `Lean.Position`s are used internally.
`character` is accepted liberally: actual character := min(line length, character) -/
structure Position where
line : Nat
character : Nat
deriving Inhabited, BEq, Ord, Hashable, ToJson, FromJson
instance : ToString Position := ⟨fun p =>
"(" ++ toString p.line ++ ", " ++ toString p.character ++ ")"⟩
instance : LT Position := ltOfOrd
instance : LE Position := leOfOrd
structure Range where
start : Position
«end» : Position
deriving Inhabited, BEq, Hashable, ToJson, FromJson, Ord
instance : LT Range := ltOfOrd
instance : LE Range := leOfOrd
/-- A `Location` is a `DocumentUri` and a `Range`. -/
structure Location where
uri : DocumentUri
range : Range
deriving Inhabited, BEq, ToJson, FromJson
structure LocationLink where
originSelectionRange? : Option Range
targetUri : DocumentUri
targetRange : Range
targetSelectionRange : Range
deriving ToJson, FromJson
-- NOTE: Diagnostic defined in Diagnostics.lean
/-- Represents a reference to a client editor command.
NOTE: No specific commands are specified by LSP, hence
possible commands need to be announced as capabilities.
[reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#command)
-/
structure Command where
/-- Title of the command, like `save`. -/
title : String
/-- The identifier of the actual command handler. -/
command : String
/-- Arguments that the command handler should be invoked with. -/
arguments? : Option (Array Json) := none
deriving ToJson, FromJson
/-- A textual edit applicable to a text document.
[reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEdit) -/
structure TextEdit where
/-- The range of the text document to be manipulated.
To insert text into a document create a range where `start = end`. -/
range : Range
/-- The string to be inserted. For delete operations use an empty string. -/
newText : String
/-- Identifier for annotated edit.
`WorkspaceEdit` has a `changeAnnotations` field that maps these identifiers to a `ChangeAnnotation`.
By annotating an edit you can add a description of what the edit will do and also control whether the
user is presented with a prompt before applying the edit.
[reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEdit).
-/
annotationId? : Option String := none
deriving ToJson, FromJson
/-- An array of `TextEdit`s to be performed in sequence. -/
def TextEditBatch := Array TextEdit
instance : FromJson TextEditBatch :=
⟨@fromJson? (Array TextEdit) _⟩
instance : ToJson TextEditBatch :=
⟨@toJson (Array TextEdit) _⟩
instance : EmptyCollection TextEditBatch := ⟨#[]⟩
instance : Append TextEditBatch :=
inferInstanceAs (Append (Array _))
instance : Coe TextEdit TextEditBatch where
coe te := #[te]
structure TextDocumentIdentifier where
uri : DocumentUri
deriving ToJson, FromJson
structure VersionedTextDocumentIdentifier where
uri : DocumentUri
version? : Option Nat := none
deriving ToJson, FromJson
/-- A batch of `TextEdit`s to perform on a versioned text document.
[reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentEdit) -/
structure TextDocumentEdit where
textDocument : VersionedTextDocumentIdentifier
edits : TextEditBatch
deriving ToJson, FromJson
/-- Additional information that describes document changes.
[reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEdit) -/
structure ChangeAnnotation where
/-- A human-readable string describing the actual change.
The string is rendered prominent in the user interface. -/
label : String
/-- A flag which indicates that user confirmation is needed before applying the change. -/
needsConfirmation : Bool := false
/-- A human-readable string which is rendered less prominent in the user interface. -/
description? : Option String := none
deriving ToJson, FromJson
/-- Options for `CreateFile` and `RenameFile`. -/
structure CreateFile.Options where
overwrite : Bool := false
ignoreIfExists : Bool := false
deriving ToJson, FromJson
/-- Options for `DeleteFile`. -/
structure DeleteFile.Options where
recursive : Bool := false
ignoreIfNotExists := false
deriving ToJson, FromJson
structure CreateFile where
uri : DocumentUri
options? : Option CreateFile.Options := none
annotationId? : Option String := none
deriving ToJson, FromJson
structure RenameFile where
oldUri : DocumentUri
newUri : DocumentUri
options? : Option CreateFile.Options := none
annotationId? : Option String := none
deriving ToJson, FromJson
structure DeleteFile where
uri : DocumentUri
options? : Option DeleteFile.Options := none
annotationId? : Option String := none
deriving ToJson, FromJson
/-- A change to a file resource.
[reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#resourceChanges) -/
inductive DocumentChange where
| create : CreateFile → DocumentChange
| rename : RenameFile → DocumentChange
| delete : DeleteFile → DocumentChange
| edit : TextDocumentEdit → DocumentChange
instance : ToJson DocumentChange := ⟨fun
| .create x => Json.setObjVal! (toJson x) "kind" "create"
| .rename x => Json.setObjVal! (toJson x) "kind" "rename"
| .delete x => Json.setObjVal! (toJson x) "kind" "delete"
| .edit x => toJson x
⟩
instance : FromJson DocumentChange where
fromJson? j := (do
let kind ← j.getObjVal? "kind"
match kind with
| "create" => return DocumentChange.create <|← fromJson? j
| "rename" => return DocumentChange.rename <|← fromJson? j
| "delete" => return DocumentChange.delete <|← fromJson? j
| kind => throw s!"Unrecognized kind: {kind}")
<|> (DocumentChange.edit <$> fromJson? j)
/-- A workspace edit represents changes to many resources managed in the workspace.
[reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspaceEdit) -/
structure WorkspaceEdit where
/-- Changes to existing resources. -/
changes : RBMap DocumentUri TextEditBatch compare := ∅
/-- Depending on the client capability
`workspace.workspaceEdit.resourceOperations` document changes are either
an array of `TextDocumentEdit`s to express changes to n different text
documents where each text document edit addresses a specific version of
a text document. Or it can contain above `TextDocumentEdit`s mixed with
create, rename and delete file / folder operations.
Whether a client supports versioned document edits is expressed via
`workspace.workspaceEdit.documentChanges` client capability.
If a client neither supports `documentChanges` nor
`workspace.workspaceEdit.resourceOperations` then only plain `TextEdit`s
using the `changes` property are supported. -/
documentChanges : Array DocumentChange := ∅
/-- A map of change annotations that can be referenced in
`AnnotatedTextEdit`s or create, rename and delete file / folder
operations.
Whether clients honor this property depends on the client capability
`workspace.changeAnnotationSupport`. -/
changeAnnotations : RBMap String ChangeAnnotation compare := ∅
deriving ToJson, FromJson
namespace WorkspaceEdit
instance : EmptyCollection WorkspaceEdit := ⟨{}⟩
instance : Append WorkspaceEdit where
append x y := {
changes := x.changes.mergeBy (fun _ v₁ v₂ => v₁ ++ v₂) y.changes
documentChanges := x.documentChanges ++ y.documentChanges
changeAnnotations := x.changeAnnotations.mergeBy (fun _ _v₁ v₂ => v₂) y.changeAnnotations
}
def ofTextDocumentEdit (e : TextDocumentEdit) : WorkspaceEdit :=
{ documentChanges := #[DocumentChange.edit e]}
end WorkspaceEdit
/-- An item to transfer a text document from the client to the server.
[reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentItem)
-/
structure TextDocumentItem where
/-- The text document's URI. -/
uri : DocumentUri
/-- The text document's language identifier. -/
languageId : String
/-- The version number of this document (it will increase after each change, including undo/redo). -/
version : Nat
/-- The content of the opened text document. -/
text : String
deriving ToJson, FromJson
structure TextDocumentPositionParams where
textDocument : TextDocumentIdentifier
position : Position
deriving ToJson, FromJson
instance : ToString TextDocumentPositionParams where
toString p := s!"{p.textDocument.uri}:{p.position.line}:{p.position.character}"
structure DocumentFilter where
language? : Option String := none
scheme? : Option String := none
pattern? : Option String := none
deriving ToJson, FromJson
def DocumentSelector := Array DocumentFilter
instance : FromJson DocumentSelector :=
⟨@fromJson? (Array DocumentFilter) _⟩
instance : ToJson DocumentSelector :=
⟨@toJson (Array DocumentFilter) _⟩
structure StaticRegistrationOptions where
id? : Option String := none
deriving ToJson, FromJson
structure TextDocumentRegistrationOptions where
documentSelector? : Option DocumentSelector := none
deriving ToJson, FromJson
inductive MarkupKind where
| plaintext | markdown
instance : FromJson MarkupKind := ⟨fun
| str "plaintext" => Except.ok MarkupKind.plaintext
| str "markdown" => Except.ok MarkupKind.markdown
| _ => throw "unknown MarkupKind"⟩
instance : ToJson MarkupKind := ⟨fun
| MarkupKind.plaintext => str "plaintext"
| MarkupKind.markdown => str "markdown"⟩
structure MarkupContent where
kind : MarkupKind
value : String
deriving ToJson, FromJson
/-- Reference to the progress of some in-flight piece of work.
[reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#progress)
-/
abbrev ProgressToken := String -- do we need integers?
/-- Params for JSON-RPC method `$/progress` request.
[reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#progress) -/
structure ProgressParams (α : Type) where
token : ProgressToken
value : α
deriving ToJson
structure WorkDoneProgressReport where
kind := "report"
/-- More detailed associated progress message. -/
message? : Option String := none
/-- Controls if a cancel button should show to allow the user to cancel the operation. -/
cancellable := false
/-- Optional progress percentage to display (value 100 is considered 100%).
If not provided infinite progress is assumed. -/
percentage? : Option Nat := none
deriving ToJson
/-- Notification to signal the start of progress reporting. -/
structure WorkDoneProgressBegin extends WorkDoneProgressReport where
kind := "begin"
title : String
deriving ToJson
/-- Signals the end of progress reporting. -/
structure WorkDoneProgressEnd where
kind := "end"
message? : Option String := none
deriving ToJson
structure WorkDoneProgressParams where
workDoneToken? : Option ProgressToken := none
deriving ToJson, FromJson
structure PartialResultParams where
partialResultToken? : Option ProgressToken := none
deriving ToJson, FromJson
/-- [reference](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workDoneProgressOptions) -/
structure WorkDoneProgressOptions where
workDoneProgress := false
deriving ToJson, FromJson
end Lsp
end Lean
|
affb91d6d49c60b9ade928ee6465d29ed557d6a3 | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/algebra/group_with_zero_power.lean | 9fc34d80bdb6cd92d7c83b78efd289b40874f49e | [
"Apache-2.0"
] | permissive | agjftucker/mathlib | d634cd0d5256b6325e3c55bb7fb2403548371707 | 87fe50de17b00af533f72a102d0adefe4a2285e8 | refs/heads/master | 1,625,378,131,941 | 1,599,166,526,000 | 1,599,166,526,000 | 160,748,509 | 0 | 0 | Apache-2.0 | 1,544,141,789,000 | 1,544,141,789,000 | null | UTF-8 | Lean | false | false | 7,870 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import algebra.group_power
/-!
# Powers of elements of groups with an adjoined zero element
In this file we define integer power functions for groups with an adjoined zero element.
This generalises the integer power function on a division ring.
-/
@[simp] lemma zero_pow' {M : Type*} [monoid_with_zero M] :
∀ n : ℕ, n ≠ 0 → (0 : M) ^ n = 0
| 0 h := absurd rfl h
| (k+1) h := zero_mul _
@[simp] lemma zero_pow_eq_zero {M : Type*} [monoid_with_zero M] [nontrivial M] {n : ℕ} :
(0 : M) ^ n = 0 ↔ 0 < n :=
begin
split; intro h,
{ rw [nat.pos_iff_ne_zero], rintro rfl, simpa using h },
{ exact zero_pow' n h.ne.symm }
end
section group_with_zero
variables {G₀ : Type*} [group_with_zero G₀]
section nat_pow
@[simp, field_simps] theorem inv_pow' (a : G₀) (n : ℕ) : (a⁻¹) ^ n = (a ^ n)⁻¹ :=
by induction n with n ih; [exact inv_one.symm,
rw [pow_succ', pow_succ, ih, mul_inv_rev']]
theorem pow_eq_zero' {g : G₀} {n : ℕ} (H : g ^ n = 0) : g = 0 :=
begin
induction n with n ih,
{ rw pow_zero at H,
rw [← mul_one g, H, mul_zero] },
exact or.cases_on (mul_eq_zero.1 H) id ih
end
@[field_simps] theorem pow_ne_zero' {g : G₀} (n : ℕ) (h : g ≠ 0) : g ^ n ≠ 0 :=
mt pow_eq_zero' h
theorem pow_sub' (a : G₀) {m n : ℕ} (ha : a ≠ 0) (h : n ≤ m) : a ^ (m - n) = a ^ m * (a ^ n)⁻¹ :=
have h1 : m - n + n = m, from nat.sub_add_cancel h,
have h2 : a ^ (m - n) * a ^ n = a ^ m, by rw [←pow_add, h1],
eq_div_of_mul_eq (pow_ne_zero' _ ha) h2
theorem pow_inv_comm' (a : G₀) (m n : ℕ) : (a⁻¹) ^ m * a ^ n = a ^ n * (a⁻¹) ^ m :=
(commute.refl a).inv_left'.pow_pow m n
end nat_pow
end group_with_zero
section int_pow
open int
variables {G₀ : Type*} [group_with_zero G₀]
/--
The power operation in a group with zero.
This extends `monoid.pow` to negative integers
with the definition `a ^ (-n) = (a ^ n)⁻¹`.
-/
def fpow (a : G₀) : ℤ → G₀
| (of_nat n) := a ^ n
| -[1+n] := (a ^ (nat.succ n))⁻¹
@[priority 10] instance : has_pow G₀ ℤ := ⟨fpow⟩
@[simp] theorem fpow_coe_nat (a : G₀) (n : ℕ) : a ^ (n:ℤ) = a ^ n := rfl
theorem fpow_of_nat (a : G₀) (n : ℕ) : a ^ of_nat n = a ^ n := rfl
@[simp] theorem fpow_neg_succ_of_nat (a : G₀) (n : ℕ) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl
local attribute [ematch] le_of_lt
@[simp] theorem fpow_zero (a : G₀) : a ^ (0:ℤ) = 1 := rfl
@[simp] theorem fpow_one (a : G₀) : a ^ (1:ℤ) = a := mul_one _
@[simp] theorem one_fpow : ∀ (n : ℤ), (1 : G₀) ^ n = 1
| (n : ℕ) := one_pow _
| -[1+ n] := show _⁻¹=(1:G₀), by rw [one_pow, inv_one]
lemma zero_fpow : ∀ z : ℤ, z ≠ 0 → (0 : G₀) ^ z = 0
| (of_nat n) h := zero_pow' _ $ by rintro rfl; exact h rfl
| -[1+n] h := show (0*0 ^ n)⁻¹ = (0 : G₀), by simp
@[simp] theorem fpow_neg (a : G₀) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹
| (n+1:ℕ) := rfl
| 0 := inv_one.symm
| -[1+ n] := (inv_inv' _).symm
theorem fpow_neg_one (x : G₀) : x ^ (-1:ℤ) = x⁻¹ := congr_arg has_inv.inv $ pow_one x
theorem inv_fpow (a : G₀) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹
| (n : ℕ) := inv_pow' a n
| -[1+ n] := congr_arg has_inv.inv $ inv_pow' a (n+1)
lemma fpow_add_one {a : G₀} (ha : a ≠ 0) : ∀ n : ℤ, a ^ (n + 1) = a ^ n * a
| (of_nat n) := by simp [← int.coe_nat_succ, pow_succ']
| -[1+0] := by simp [int.neg_succ_of_nat_eq, ha]
| -[1+(n+1)] := by rw [int.neg_succ_of_nat_eq, fpow_neg, neg_add, neg_add_cancel_right, fpow_neg,
← int.coe_nat_succ, fpow_coe_nat, fpow_coe_nat, pow_succ _ (n + 1), mul_inv_rev', mul_assoc,
inv_mul_cancel ha, mul_one]
lemma fpow_sub_one {a : G₀} (ha : a ≠ 0) (n : ℤ) : a ^ (n - 1) = a ^ n * a⁻¹ :=
calc a ^ (n - 1) = a ^ (n - 1) * a * a⁻¹ : by rw [mul_assoc, mul_inv_cancel ha, mul_one]
... = a^n * a⁻¹ : by rw [← fpow_add_one ha, sub_add_cancel]
lemma fpow_add {a : G₀} (ha : a ≠ 0) (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
theorem fpow_one_add {a : G₀} (h : a ≠ 0) (i : ℤ) : a ^ (1 + i) = a * a ^ i :=
by rw [fpow_add h, fpow_one]
theorem semiconj_by.fpow_right {a x y : G₀} (h : semiconj_by a x y) :
∀ m : ℤ, semiconj_by a (x^m) (y^m)
| (n : ℕ) := h.pow_right n
| -[1+n] := (h.pow_right (n + 1)).inv_right'
theorem commute.fpow_right {a b : G₀} (h : commute a b) : ∀ m : ℤ, commute a (b^m) :=
h.fpow_right
theorem commute.fpow_left {a b : G₀} (h : commute a b) (m : ℤ) : commute (a^m) b :=
(h.symm.fpow_right m).symm
theorem commute.fpow_fpow {a b : G₀} (h : commute a b) (m n : ℤ) : commute (a^m) (b^n) :=
(h.fpow_left m).fpow_right n
theorem commute.fpow_self (a : G₀) (n : ℤ) : commute (a^n) a := (commute.refl a).fpow_left n
theorem commute.self_fpow (a : G₀) (n : ℤ) : commute a (a^n) := (commute.refl a).fpow_right n
theorem commute.fpow_fpow_self (a : G₀) (m n : ℤ) : commute (a^m) (a^n) := (commute.refl a).fpow_fpow m n
theorem fpow_mul (a : G₀) : ∀ m n : ℤ, a ^ (m * n) = (a ^ m) ^ n
| (m : ℕ) (n : ℕ) := pow_mul _ _ _
| (m : ℕ) -[1+ n] := (fpow_neg _ (m * n.succ)).trans $
show (a ^ (m * n.succ))⁻¹ = _, by rw pow_mul; refl
| -[1+ m] (n : ℕ) := (fpow_neg _ (m.succ * n)).trans $
show (a ^ (m.succ * n))⁻¹ = _, by rw [pow_mul, ← inv_pow']; refl
| -[1+ m] -[1+ n] := (pow_mul a m.succ n.succ).trans $
show _ = (_⁻¹ ^ _)⁻¹, by rw [inv_pow', inv_inv']
theorem fpow_mul' (a : G₀) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m :=
by rw [mul_comm, fpow_mul]
@[simp, norm_cast] lemma units.coe_gpow' (u : units G₀) :
∀ (n : ℤ), ((u ^ n : units G₀) : G₀) = u ^ n
| (n : ℕ) := u.coe_pow n
| -[1+k] := by rw [gpow_neg_succ_of_nat, fpow_neg_succ_of_nat, units.coe_inv', u.coe_pow]
lemma fpow_ne_zero_of_ne_zero {a : G₀} (ha : a ≠ 0) : ∀ (z : ℤ), a ^ z ≠ 0
| (of_nat n) := pow_ne_zero' _ ha
| -[1+n] := inv_ne_zero $ pow_ne_zero' _ ha
lemma fpow_sub {a : G₀} (ha : a ≠ 0) (z1 z2 : ℤ) : a ^ (z1 - z2) = a ^ z1 / a ^ z2 :=
by rw [sub_eq_add_neg, fpow_add ha, fpow_neg]; refl
lemma commute.mul_fpow {a b : G₀} (h : commute a b) :
∀ (i : ℤ), (a * b) ^ i = (a ^ i) * (b ^ i)
| (n : ℕ) := h.mul_pow n
| -[1+n] := by simp [h.mul_pow, (h.pow_pow _ _).eq, mul_inv_rev']
lemma mul_fpow {G₀ : Type*} [comm_group_with_zero G₀] (a b : G₀) (m : ℤ):
(a * b) ^ m = (a ^ m) * (b ^ m) :=
(commute.all a b).mul_fpow m
lemma fpow_eq_zero {x : G₀} {n : ℤ} (h : x ^ n = 0) : x = 0 :=
classical.by_contradiction $ λ hx, fpow_ne_zero_of_ne_zero hx n h
lemma fpow_ne_zero {x : G₀} (n : ℤ) : x ≠ 0 → x ^ n ≠ 0 :=
mt fpow_eq_zero
theorem fpow_neg_mul_fpow_self (n : ℤ) {x : G₀} (h : x ≠ 0) :
x ^ (-n) * x ^ n = 1 :=
begin
rw [fpow_neg],
exact inv_mul_cancel (fpow_ne_zero n h)
end
theorem one_div_pow {a : G₀} (n : ℕ) :
(1 / a) ^ n = 1 / a ^ n :=
by simp only [one_div, inv_pow']
theorem one_div_fpow {a : G₀} (n : ℤ) :
(1 / a) ^ n = 1 / a ^ n :=
by simp only [one_div, inv_fpow]
end int_pow
section
variables {G₀ : Type*} [comm_group_with_zero G₀]
@[simp] theorem div_pow (a b : G₀) (n : ℕ) :
(a / b) ^ n = a ^ n / b ^ n :=
by simp only [div_eq_mul_inv, mul_pow, inv_pow']
@[simp] theorem div_fpow (a : G₀) {b : G₀} (n : ℤ) :
(a / b) ^ n = a ^ n / b ^ n :=
by simp only [div_eq_mul_inv, mul_fpow, inv_fpow]
lemma div_sq_cancel {a : G₀} (ha : a ≠ 0) (b : G₀) : a ^ 2 * b / a = a * b :=
by rw [pow_two, mul_assoc, mul_div_cancel_left _ ha]
end
|
0c2fcfdf9d0e8594d712b5dca5f26f72a5b7b383 | 9cb9db9d79fad57d80ca53543dc07efb7c4f3838 | /src/pseudo_normed_group/LC.lean | 7b564d21a28d8255b9eb325f9a40bb51b4e7a4f9 | [] | no_license | mr-infty/lean-liquid | 3ff89d1f66244b434654c59bdbd6b77cb7de0109 | a8db559073d2101173775ccbd85729d3a4f1ed4d | refs/heads/master | 1,678,465,145,334 | 1,614,565,310,000 | 1,614,565,310,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 14,062 | lean | import pseudo_normed_group.FiltrationPow
import locally_constant.NormedGroup
import locally_constant.Vhat
open_locale classical nnreal big_operators
noncomputable theory
local attribute [instance] type_pow
open NormedGroup opposite Profinite pseudo_normed_group category_theory breen_deligne
open profinitely_filtered_pseudo_normed_group
universe variable u
variables (r : ℝ≥0) (V : NormedGroup)
variables (r' : ℝ≥0) {M M₁ M₂ M₃ : Type u}
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M]
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₁]
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₂]
variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₃]
variables (c c₁ c₂ c₃ c₄ : ℝ≥0) (l m n : ℕ)
variables (f : profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₂)
variables (g : profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₂ M₃)
/-- The "functor" that sends `M` and `c` to `V((filtration M c)^n)` -/
def LCFP (V : NormedGroup) (r' : ℝ≥0) (M : Type*) (c : ℝ≥0) (n : ℕ)
[profinitely_filtered_pseudo_normed_group_with_Tinv r' M] :
NormedGroup :=
(LocallyConstant.obj V).obj (op $ FiltrationPow r' M c n)
namespace LCFP
@[simps]
def map : LCFP V r' M₂ c n ⟶ LCFP V r' M₁ c n :=
(LocallyConstant.obj V).map (FiltrationPow.map r' c n f).op
variables (M)
@[simp] lemma map_id :
map V r' c n (profinitely_filtered_pseudo_normed_group_with_Tinv_hom.id) =
𝟙 (LCFP V r' M c n) :=
by { delta map, rw FiltrationPow.map_id, apply category_theory.functor.map_id, }
variables {M}
lemma map_comp : map V r' c n (g.comp f) = map V r' c n g ≫ map V r' c n f :=
by { delta map, rw [FiltrationPow.map_comp, op_comp], apply category_theory.functor.map_comp }
@[simps]
def res [fact (c₁ ≤ c₂)] : LCFP V r' M c₂ n ⟶ LCFP V r' M c₁ n :=
(LocallyConstant.obj V).map (FiltrationPow.cast_le r' c₁ c₂ n).op
@[simp] lemma res_refl : res V r' c c n = 𝟙 (LCFP V r' M c n) :=
by { delta res, rw FiltrationPow.cast_le_refl, apply category_theory.functor.map_id }
lemma res_comp_res [fact (c₁ ≤ c₂)] [fact (c₂ ≤ c₃)] [fact (c₁ ≤ c₃)] :
res V r' c₂ c₃ n ≫ res V r' c₁ c₂ n = @res V r' M _ c₁ c₃ n _ :=
by simp only [res, ← category_theory.functor.map_comp, ← op_comp, FiltrationPow.cast_le_trans]
lemma map_comp_res [fact (c₁ ≤ c₂)] :
map V r' c₂ n f ≫ res V r' c₁ c₂ n = res V r' c₁ c₂ n ≫ map V r' c₁ n f :=
by simp only [map, res, ← category_theory.functor.map_comp, ← op_comp,
FiltrationPow.map_comp_cast_le]
section Tinv
open profinitely_filtered_pseudo_normed_group_with_Tinv
variables [fact (0 < r')]
@[simps]
def Tinv : LCFP V r' M c n ⟶ LCFP V r' M (r' * c) n :=
res V r' _ _ n ≫ (LocallyConstant.obj V).map (FiltrationPow.Tinv r' (r' * c) n).op
lemma map_comp_Tinv :
map V r' c n f ≫ Tinv V r' c n = Tinv V r' c n ≫ map V r' (r' * c) n f :=
begin
delta Tinv,
rw [← category.assoc, map_comp_res, category.assoc, category.assoc],
delta map,
simp only [← category_theory.functor.map_comp, ← op_comp, FiltrationPow.map_comp_Tinv]
end
lemma res_comp_Tinv [fact (c₁ ≤ c₂)] :
res V r' c₁ c₂ n ≫ (@Tinv V r' M _ c₁ n _) =
Tinv V r' c₂ n ≫ res V r' (r' * c₁) (r' * c₂) n :=
begin
delta Tinv res,
simp only [← category_theory.functor.map_comp, ← op_comp],
refl
end
end Tinv
section normed_with_aut
variables [normed_with_aut r V]
instance [fact (0 < r)] : normed_with_aut r (LCFP V r' M c n) :=
NormedGroup.normed_with_aut_LocallyConstant _ _ _
def T_inv [fact (0 < r)] : LCFP V r' M c n ⟶ LCFP V r' M c n :=
normed_with_aut.T.inv
lemma T_inv_eq [fact (0 < r)] :
T_inv r V r' c n =
(LocallyConstant.map (normed_with_aut.T.inv : V ⟶ V)).app (op $ FiltrationPow r' M c n) :=
rfl
variables [fact (0 < r)]
lemma map_comp_T_inv :
map V r' c n f ≫ T_inv r V r' c n = T_inv r V r' c n ≫ map V r' c n f :=
(LocallyConstant.map (normed_with_aut.T.inv : V ⟶ V)).naturality _
lemma res_comp_T_inv [fact (c₁ ≤ c₂)] :
res V r' c₁ c₂ n ≫ (@T_inv r V r' M _ c₁ n _ _) = T_inv r V r' c₂ n ≫ res V r' c₁ c₂ n :=
(LocallyConstant.map (normed_with_aut.T.inv : V ⟶ V)).naturality _
end normed_with_aut
end LCFP
namespace breen_deligne
open LCFP
variables (M) {l m n}
namespace basic_universal_map
variables (ϕ : basic_universal_map m n)
def eval_LCFP : LCFP V r' M c₂ n ⟶ LCFP V r' M c₁ m :=
if H : ϕ.suitable c₁ c₂
then by exactI (LocallyConstant.obj V).map (ϕ.eval_FP r' M c₁ c₂).op
else 0
lemma eval_LCFP_def [h : ϕ.suitable c₁ c₂] :
ϕ.eval_LCFP V r' M c₁ c₂ = (LocallyConstant.obj V).map (ϕ.eval_FP r' M c₁ c₂).op :=
dif_pos h
lemma eval_LCFP_comp (g : basic_universal_map m n) (f : basic_universal_map l m)
[hg : g.suitable c₂ c₃] [hf : f.suitable c₁ c₂] :
(g.comp f).eval_LCFP V r' M c₁ c₃ =
g.eval_LCFP V r' M c₂ c₃ ≫ f.eval_LCFP V r' M c₁ c₂ :=
begin
haveI : (g.comp f).suitable c₁ c₃ := suitable_comp c₂,
simp only [eval_LCFP_def],
rw [← category_theory.functor.map_comp, ← op_comp],
congr' 2,
simp [eval_FP_comp r' M _ c₂],
end
lemma map_comp_eval_LCFP [ϕ.suitable c₁ c₂] :
map V r' c₂ n f ≫ ϕ.eval_LCFP V r' M₁ c₁ c₂ = ϕ.eval_LCFP V r' M₂ c₁ c₂ ≫ map V r' c₁ m f :=
begin
delta map,
simp only [eval_LCFP_def, ← category_theory.functor.map_comp, ← op_comp, map_comp_eval_FP]
end
lemma res_comp_eval_LCFP
[fact (c₁ ≤ c₂)] [ϕ.suitable c₂ c₄] [ϕ.suitable c₁ c₃] [fact (c₃ ≤ c₄)] :
res V r' c₃ c₄ n ≫ ϕ.eval_LCFP V r' M c₁ c₃ = ϕ.eval_LCFP V r' M c₂ c₄ ≫ res V r' c₁ c₂ m :=
begin
delta res,
simp only [eval_LCFP_def, ← category_theory.functor.map_comp, ← op_comp,
cast_le_comp_eval_FP _ _ c₁ c₂ c₃ c₄]
end
lemma Tinv_comp_eval_LCFP [fact (0 < r')] [ϕ.suitable c₁ c₂] :
Tinv V r' c₂ n ≫ ϕ.eval_LCFP V r' M (r' * c₁) (r' * c₂) = ϕ.eval_LCFP V r' M c₁ c₂ ≫ Tinv V r' c₁ m :=
begin
dsimp only [Tinv],
rw [← category.assoc, ← res_comp_eval_LCFP V _ _ (r'⁻¹ * (r' * c₁)) c₁ (r'⁻¹ * (r' * c₂)) c₂,
category.assoc, category.assoc],
simp only [eval_LCFP_def, res, ← category_theory.functor.map_comp, ← op_comp,
← category.assoc, Tinv_comp_eval_FP _ _ (r' * c₁) (r' * c₂)],
end
lemma T_inv_comp_eval_LCFP [normed_with_aut r V] [fact (0 < r)] [ϕ.suitable c₁ c₂] :
T_inv r V r' c₂ n ≫ ϕ.eval_LCFP V r' M₁ c₁ c₂ =
ϕ.eval_LCFP V r' M₁ c₁ c₂ ≫ T_inv r V r' c₁ m :=
begin
simp only [eval_LCFP_def],
exact ((LocallyConstant.map (normed_with_aut.T.inv : V ⟶ V)).naturality _).symm
end
end basic_universal_map
namespace universal_map
open free_abelian_group
variables (ϕ : universal_map m n)
def eval_LCFP : LCFP V r' M c₂ n ⟶ LCFP V r' M c₁ m :=
if H : (ϕ.suitable c₁ c₂)
then by exactI
∑ g in ϕ.support, coeff g ϕ • (g.eval_LCFP V r' M c₁ c₂)
else 0
lemma eval_LCFP_def {m n : ℕ} (f : universal_map m n) [H : f.suitable c₁ c₂] :
f.eval_LCFP V r' M c₁ c₂ = ∑ g in f.support, coeff g f • (g.eval_LCFP V r' M c₁ c₂) :=
dif_pos H
@[simp] lemma eval_LCFP_of (f : basic_universal_map m n) [f.suitable c₁ c₂] :
eval_LCFP V r' M c₁ c₂ (of f) = f.eval_LCFP V r' M c₁ c₂ :=
by simp only [eval_LCFP_def, support_of, coeff_of_self, one_smul, finset.sum_singleton]
@[simp] lemma eval_LCFP_zero :
(0 : universal_map m n).eval_LCFP V r' M c₁ c₂ = 0 :=
by rw [eval_LCFP_def, support_zero, finset.sum_empty]
@[simp] lemma eval_LCFP_neg (f : universal_map m n) :
eval_LCFP V r' M c₁ c₂ (-f) = -f.eval_LCFP V r' M c₁ c₂ :=
begin
rw eval_LCFP,
split_ifs,
{ rw suitable_neg_iff at h,
rw [eval_LCFP, dif_pos h],
simp only [add_monoid_hom.map_neg, finset.sum_neg_distrib, neg_smul, support_neg] },
{ rw suitable_neg_iff at h,
rw [eval_LCFP, dif_neg h, neg_zero] }
end
lemma eval_LCFP_add (f g : universal_map m n)
[hf : f.suitable c₁ c₂] [hg : g.suitable c₁ c₂] :
eval_LCFP V r' M c₁ c₂ (f + g) =
f.eval_LCFP V r' M c₁ c₂ + g.eval_LCFP V r' M c₁ c₂ :=
begin
simp only [eval_LCFP_def],
rw finset.sum_subset (support_add f g), -- two goals
simp only [add_monoid_hom.map_add _ f g, add_smul],
convert finset.sum_add_distrib using 2, -- three goals
apply finset.sum_subset (finset.subset_union_left _ _), swap,
apply finset.sum_subset (finset.subset_union_right _ _),
all_goals { rintros x - h, rw not_mem_support_iff at h, simp [h] },
end
lemma eval_LCFP_comp_of (g : basic_universal_map m n) (f : basic_universal_map l m)
[hg : g.suitable c₂ c₃] [hf : f.suitable c₁ c₂] :
eval_LCFP V r' M c₁ c₃ ((comp (of g)) (of f)) =
eval_LCFP V r' M c₂ c₃ (of g) ≫ eval_LCFP V r' M c₁ c₂ (of f) :=
begin
haveI hfg : (g.comp f).suitable c₁ c₃ := basic_universal_map.suitable_comp c₂,--hg.comp hf,
simp only [comp_of, eval_LCFP_of],
rw ← basic_universal_map.eval_LCFP_comp,
end
open category_theory.limits
lemma eval_LCFP_comp (g : universal_map m n) (f : universal_map l m)
[hg : g.suitable c₂ c₃] [hf : f.suitable c₁ c₂] :
(comp g f).eval_LCFP V r' M c₁ c₃ =
g.eval_LCFP V r' M c₂ c₃ ≫ f.eval_LCFP V r' M c₁ c₂ :=
begin
unfreezingI { revert hf },
apply free_abelian_group.induction_on_free_predicate
(suitable c₂ c₃) (suitable_free_predicate c₂ c₃) g hg; unfreezingI { clear_dependent g },
{ intros h₂,
simp only [eval_LCFP_zero, zero_comp, pi.zero_apply,
add_monoid_hom.coe_zero, add_monoid_hom.map_zero] },
{ intros g hg hf,
-- now do another nested induction on `f`
apply free_abelian_group.induction_on_free_predicate
(suitable c₁ c₂) (suitable_free_predicate c₁ c₂) f hf; unfreezingI { clear_dependent f },
{ simp only [eval_LCFP_zero, comp_zero, add_monoid_hom.map_zero] },
{ intros f hf,
rw suitable_of_iff at hf,
resetI,
apply eval_LCFP_comp_of },
{ intros f hf IH,
show _ = normed_group_hom.comp_hom _ _,
simp only [IH, pi.neg_apply, add_monoid_hom.map_neg, eval_LCFP_neg,
add_monoid_hom.coe_neg, neg_inj],
refl },
{ rintros (f₁ : universal_map l m) (f₂ : universal_map l m) hf₁ hf₂ IH₁ IH₂, resetI,
haveI Hg₁f : (comp (of g) f₁).suitable c₁ c₃ := suitable.comp c₂,
haveI Hg₂f : (comp (of g) f₂).suitable c₁ c₃ := suitable.comp c₂,
simp only [add_monoid_hom.map_add, add_monoid_hom.add_apply, eval_LCFP_add, IH₁, IH₂],
show _ = normed_group_hom.comp_hom _ _,
simpa [add_monoid_hom.map_add] } },
{ intros g hg IH hf, resetI, specialize IH,
show _ = normed_group_hom.comp_hom _ _,
simp only [IH, pi.neg_apply, add_monoid_hom.map_neg, eval_LCFP_neg,
add_monoid_hom.coe_neg, neg_inj],
refl },
{ rintros (g₁ : universal_map m n) (g₂ : universal_map m n) hg₁ hg₂ IH₁ IH₂ hf, resetI,
haveI Hg₁f : (comp g₁ f).suitable c₁ c₃ := suitable.comp c₂,
haveI Hg₂f : (comp g₂ f).suitable c₁ c₃ := suitable.comp c₂,
simp only [add_monoid_hom.map_add, add_monoid_hom.add_apply, eval_LCFP_add, IH₁, IH₂],
show _ = normed_group_hom.comp_hom _ _,
simpa [add_monoid_hom.map_add] }
end
lemma map_comp_eval_LCFP [ϕ.suitable c₁ c₂] :
map V r' c₂ n f ≫ ϕ.eval_LCFP V r' M₁ c₁ c₂ = ϕ.eval_LCFP V r' M₂ c₁ c₂ ≫ map V r' c₁ m f :=
begin
show normed_group_hom.comp_hom _ _ = normed_group_hom.comp_hom _ _,
simp only [eval_LCFP_def, add_monoid_hom.map_sum, add_monoid_hom.sum_apply],
apply finset.sum_congr rfl,
intros g hg,
haveI : g.suitable c₁ c₂ := suitable_of_mem_support ϕ c₁ c₂ g hg,
simp only [← gsmul_eq_smul, add_monoid_hom.map_gsmul, add_monoid_hom.gsmul_apply],
congr' 1,
exact g.map_comp_eval_LCFP V r' _ _ _
end
lemma res_comp_eval_LCFP
[fact (c₁ ≤ c₂)] [ϕ.suitable c₂ c₄] [ϕ.suitable c₁ c₃] [fact (c₃ ≤ c₄)] :
res V r' c₃ c₄ n ≫ ϕ.eval_LCFP V r' M c₁ c₃ = ϕ.eval_LCFP V r' M c₂ c₄ ≫ res V r' c₁ c₂ m :=
begin
show normed_group_hom.comp_hom _ _ = normed_group_hom.comp_hom _ _,
simp only [eval_LCFP_def, add_monoid_hom.map_sum, add_monoid_hom.sum_apply],
apply finset.sum_congr rfl,
intros g hg,
simp only [← gsmul_eq_smul, add_monoid_hom.map_gsmul, add_monoid_hom.gsmul_apply],
haveI : g.suitable c₂ c₄ := suitable_of_mem_support ϕ _ _ g hg,
haveI : g.suitable c₁ c₃ := suitable_of_mem_support ϕ _ _ g hg,
congr' 1,
exact g.res_comp_eval_LCFP V r' M c₁ c₂ c₃ c₄
end
lemma Tinv_comp_eval_LCFP [fact (0 < r')] [ϕ.suitable c₁ c₂] :
Tinv V r' c₂ n ≫ ϕ.eval_LCFP V r' M (r' * c₁) (r' * c₂) = ϕ.eval_LCFP V r' M c₁ c₂ ≫ Tinv V r' c₁ m :=
begin
show normed_group_hom.comp_hom _ _ = normed_group_hom.comp_hom _ _,
simp only [eval_LCFP_def, add_monoid_hom.map_sum, add_monoid_hom.sum_apply],
apply finset.sum_congr rfl,
intros g hg,
haveI : g.suitable c₁ c₂ := suitable_of_mem_support ϕ c₁ c₂ g hg,
simp only [← gsmul_eq_smul, add_monoid_hom.map_gsmul, add_monoid_hom.gsmul_apply],
congr' 1,
exact g.Tinv_comp_eval_LCFP V r' _ _ _
end
lemma T_inv_comp_eval_LCFP [normed_with_aut r V] [fact (0 < r)] [ϕ.suitable c₁ c₂] :
T_inv r V r' c₂ n ≫ ϕ.eval_LCFP V r' M₁ c₁ c₂ =
ϕ.eval_LCFP V r' M₁ c₁ c₂ ≫ T_inv r V r' c₁ m :=
begin
show normed_group_hom.comp_hom _ _ = normed_group_hom.comp_hom _ _,
simp only [eval_LCFP_def, add_monoid_hom.map_sum, add_monoid_hom.sum_apply],
apply finset.sum_congr rfl,
intros g hg,
haveI : g.suitable c₁ c₂ := suitable_of_mem_support ϕ c₁ c₂ g hg,
simp only [← gsmul_eq_smul, add_monoid_hom.map_gsmul, add_monoid_hom.gsmul_apply],
congr' 1,
exact g.T_inv_comp_eval_LCFP r V r' _ _
end
end universal_map
end breen_deligne
|
1a7b640a1601be3331d48c97abb858319ff8cc0c | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/data/buffer/parser/basic.lean | 00905223fea94c9a302b891b0e9210aadf0e1502 | [
"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 | 110,110 | lean | /-
Copyright (c) 2020 Yakov Pechersky. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yakov Pechersky
-/
import data.string.basic
import data.buffer.basic
import data.nat.digits
/-!
# Parsers
`parser α` is the type that describes a computation that can ingest a `char_buffer`
and output, if successful, a term of type `α`.
This file expands on the definitions in the core library, proving that all the core library
parsers are `mono`. There are also lemmas on the composability of parsers.
## Main definitions
* `parse_result.pos` : The position of a `char_buffer` at which a `parser α` has finished.
* `parser.mono` : The property that a parser only moves forward within a buffer,
in both cases of success or failure.
## Implementation details
Lemmas about how parsers are mono are in the `mono` namespace. That allows using projection
notation for shorter term proofs that are parallel to the definitions of the parsers in structure.
-/
open parser parse_result
/--
For some `parse_result α`, give the position at which the result was provided, in either the
`done` or the `fail` case.
-/
@[simp] def parse_result.pos {α} : parse_result α → ℕ
| (done n _) := n
| (fail n _) := n
namespace parser
section defn_lemmas
variables {α β : Type} (msgs : thunk (list string)) (msg : thunk string)
variables (p q : parser α) (cb : char_buffer) (n n' : ℕ) {err : dlist string}
variables {a : α} {b : β}
/--
A `p : parser α` is defined to be `mono` if the result `p cb n` it gives,
for some `cb : char_buffer` and `n : ℕ`, (whether `done` or `fail`),
is always at a `parse_result.pos` that is at least `n`.
The `mono` property is used mainly for proper `orelse` behavior.
-/
class mono : Prop :=
(le' : ∀ (cb : char_buffer) (n : ℕ), n ≤ (p cb n).pos)
lemma mono.le [p.mono] : n ≤ (p cb n).pos := mono.le' cb n
/--
A `parser α` is defined to be `static` if it does not move on success.
-/
class static : Prop :=
(of_done : ∀ {cb : char_buffer} {n n' : ℕ} {a : α}, p cb n = done n' a → n = n')
/--
A `parser α` is defined to be `err_static` if it does not move on error.
-/
class err_static : Prop :=
(of_fail : ∀ {cb : char_buffer} {n n' : ℕ} {err : dlist string}, p cb n = fail n' err → n = n')
/--
A `parser α` is defined to be `step` if it always moves exactly one char forward on success.
-/
class step : Prop :=
(of_done : ∀ {cb : char_buffer} {n n' : ℕ} {a : α}, p cb n = done n' a → n' = n + 1)
/--
A `parser α` is defined to be `prog` if it always moves forward on success.
-/
class prog : Prop :=
(of_done : ∀ {cb : char_buffer} {n n' : ℕ} {a : α}, p cb n = done n' a → n < n')
/--
A `parser a` is defined to be `bounded` if it produces a
`fail` `parse_result` when it is parsing outside the provided `char_buffer`.
-/
class bounded : Prop :=
(ex' : ∀ {cb : char_buffer} {n : ℕ}, cb.size ≤ n → ∃ (n' : ℕ) (err : dlist string),
p cb n = fail n' err)
lemma bounded.exists (p : parser α) [p.bounded] {cb : char_buffer} {n : ℕ} (h : cb.size ≤ n) :
∃ (n' : ℕ) (err : dlist string), p cb n = fail n' err :=
bounded.ex' h
/--
A `parser a` is defined to be `unfailing` if it always produces a `done` `parse_result`.
-/
class unfailing : Prop :=
(ex' : ∀ (cb : char_buffer) (n : ℕ), ∃ (n' : ℕ) (a : α), p cb n = done n' a)
/--
A `parser a` is defined to be `conditionally_unfailing` if it produces a
`done` `parse_result` as long as it is parsing within the provided `char_buffer`.
-/
class conditionally_unfailing : Prop :=
(ex' : ∀ {cb : char_buffer} {n : ℕ}, n < cb.size → ∃ (n' : ℕ) (a : α), p cb n = done n' a)
lemma fail_iff :
(∀ pos' result, p cb n ≠ done pos' result) ↔
∃ (pos' : ℕ) (err : dlist string), p cb n = fail pos' err :=
by cases p cb n; simp
lemma success_iff :
(∀ pos' err, p cb n ≠ fail pos' err) ↔ ∃ (pos' : ℕ) (result : α), p cb n = done pos' result :=
by cases p cb n; simp
variables {p q cb n n' msgs msg}
lemma mono.of_done [p.mono] (h : p cb n = done n' a) : n ≤ n' :=
by simpa [h] using mono.le p cb n
lemma mono.of_fail [p.mono] (h : p cb n = fail n' err) : n ≤ n' :=
by simpa [h] using mono.le p cb n
lemma bounded.of_done [p.bounded] (h : p cb n = done n' a) : n < cb.size :=
begin
contrapose! h,
obtain ⟨np, err, hp⟩ := bounded.exists p h,
simp [hp]
end
lemma static.iff :
static p ↔ (∀ (cb : char_buffer) (n n' : ℕ) (a : α), p cb n = done n' a → n = n') :=
⟨λ h _ _ _ _ hp, by { haveI := h, exact static.of_done hp}, λ h, ⟨h⟩⟩
lemma exists_done (p : parser α) [p.unfailing] (cb : char_buffer) (n : ℕ) :
∃ (n' : ℕ) (a : α), p cb n = done n' a :=
unfailing.ex' cb n
lemma unfailing.of_fail [p.unfailing] (h : p cb n = fail n' err) : false :=
begin
obtain ⟨np, a, hp⟩ := p.exists_done cb n,
simpa [hp] using h
end
@[priority 100] -- see Note [lower instance priority]
instance conditionally_unfailing_of_unfailing [p.unfailing] : conditionally_unfailing p :=
⟨λ _ _ _, p.exists_done _ _⟩
lemma exists_done_in_bounds (p : parser α) [p.conditionally_unfailing] {cb : char_buffer} {n : ℕ}
(h : n < cb.size) : ∃ (n' : ℕ) (a : α), p cb n = done n' a :=
conditionally_unfailing.ex' h
lemma conditionally_unfailing.of_fail [p.conditionally_unfailing] (h : p cb n = fail n' err)
(hn : n < cb.size) : false :=
begin
obtain ⟨np, a, hp⟩ := p.exists_done_in_bounds hn,
simpa [hp] using h
end
lemma decorate_errors_fail (h : p cb n = fail n' err) :
@decorate_errors α msgs p cb n = fail n ((dlist.lazy_of_list (msgs ()))) :=
by simp [decorate_errors, h]
lemma decorate_errors_success (h : p cb n = done n' a) :
@decorate_errors α msgs p cb n = done n' a :=
by simp [decorate_errors, h]
lemma decorate_error_fail (h : p cb n = fail n' err) :
@decorate_error α msg p cb n = fail n ((dlist.lazy_of_list ([msg ()]))) :=
decorate_errors_fail h
lemma decorate_error_success (h : p cb n = done n' a) :
@decorate_error α msg p cb n = done n' a :=
decorate_errors_success h
@[simp] lemma decorate_errors_eq_done :
@decorate_errors α msgs p cb n = done n' a ↔ p cb n = done n' a :=
by cases h : p cb n; simp [decorate_errors, h]
@[simp] lemma decorate_error_eq_done :
@decorate_error α msg p cb n = done n' a ↔ p cb n = done n' a :=
decorate_errors_eq_done
@[simp] lemma decorate_errors_eq_fail :
@decorate_errors α msgs p cb n = fail n' err ↔
n = n' ∧ err = dlist.lazy_of_list (msgs ()) ∧ ∃ np err', p cb n = fail np err' :=
by cases h : p cb n; simp [decorate_errors, h, eq_comm]
@[simp] lemma decorate_error_eq_fail :
@decorate_error α msg p cb n = fail n' err ↔
n = n' ∧ err = dlist.lazy_of_list ([msg ()]) ∧ ∃ np err', p cb n = fail np err' :=
decorate_errors_eq_fail
@[simp] lemma return_eq_pure : (@return parser _ _ a) = pure a := rfl
lemma pure_eq_done : (@pure parser _ _ a) = λ _ n, done n a := rfl
@[simp] lemma pure_ne_fail : (pure a : parser α) cb n ≠ fail n' err := by simp [pure_eq_done]
section bind
variable (f : α → parser β)
@[simp] lemma bind_eq_bind : p.bind f = p >>= f := rfl
variable {f}
@[simp] lemma bind_eq_done :
(p >>= f) cb n = done n' b ↔
∃ (np : ℕ) (a : α), p cb n = done np a ∧ f a cb np = done n' b :=
by cases hp : p cb n; simp [hp, ←bind_eq_bind, parser.bind, and_assoc]
@[simp] lemma bind_eq_fail :
(p >>= f) cb n = fail n' err ↔
(p cb n = fail n' err) ∨ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ f a cb np = fail n' err) :=
by cases hp : p cb n; simp [hp, ←bind_eq_bind, parser.bind, and_assoc]
@[simp] lemma and_then_eq_bind {α β : Type} {m : Type → Type} [monad m] (a : m α) (b : m β) :
a >> b = a >>= (λ _, b) := rfl
lemma and_then_fail :
(p >> return ()) cb n = parse_result.fail n' err ↔ p cb n = fail n' err :=
by simp [pure_eq_done]
lemma and_then_success :
(p >> return ()) cb n = parse_result.done n' () ↔ ∃ a, p cb n = done n' a:=
by simp [pure_eq_done]
end bind
section map
variable {f : α → β}
@[simp] lemma map_eq_done : (f <$> p) cb n = done n' b ↔
∃ (a : α), p cb n = done n' a ∧ f a = b :=
by cases hp : p cb n; simp [←is_lawful_monad.bind_pure_comp_eq_map, hp, and_assoc, pure_eq_done]
@[simp] lemma map_eq_fail : (f <$> p) cb n = fail n' err ↔ p cb n = fail n' err :=
by simp [←bind_pure_comp_eq_map, pure_eq_done]
@[simp] lemma map_const_eq_done {b'} : (b <$ p) cb n = done n' b' ↔
∃ (a : α), p cb n = done n' a ∧ b = b' :=
by simp [map_const_eq]
@[simp] lemma map_const_eq_fail : (b <$ p) cb n = fail n' err ↔ p cb n = fail n' err :=
by simp only [map_const_eq, map_eq_fail]
lemma map_const_rev_eq_done {b'} : (p $> b) cb n = done n' b' ↔
∃ (a : α), p cb n = done n' a ∧ b = b' :=
map_const_eq_done
lemma map_rev_const_eq_fail : (p $> b) cb n = fail n' err ↔ p cb n = fail n' err :=
map_const_eq_fail
end map
@[simp] lemma orelse_eq_orelse : p.orelse q = (p <|> q) := rfl
@[simp] lemma orelse_eq_done : (p <|> q) cb n = done n' a ↔
(p cb n = done n' a ∨ (q cb n = done n' a ∧ ∃ err, p cb n = fail n err)) :=
begin
cases hp : p cb n with np resp np errp,
{ simp [hp, ←orelse_eq_orelse, parser.orelse] },
{ by_cases hn : np = n,
{ cases hq : q cb n with nq resq nq errq,
{ simp [hp, hn, hq, ←orelse_eq_orelse, parser.orelse] },
{ rcases lt_trichotomy nq n with H|rfl|H;
simp [hp, hn, hq, H, not_lt_of_lt H, lt_irrefl, ←orelse_eq_orelse, parser.orelse] <|>
simp [hp, hn, hq, lt_irrefl, ←orelse_eq_orelse, parser.orelse] } },
{ simp [hp, hn, ←orelse_eq_orelse, parser.orelse] } }
end
@[simp] lemma orelse_eq_fail_eq : (p <|> q) cb n = fail n err ↔
(p cb n = fail n err ∧ ∃ (nq errq), n < nq ∧ q cb n = fail nq errq) ∨
(∃ (errp errq), p cb n = fail n errp ∧ q cb n = fail n errq ∧ errp ++ errq = err)
:=
begin
cases hp : p cb n with np resp np errp,
{ simp [hp, ←orelse_eq_orelse, parser.orelse] },
{ by_cases hn : np = n,
{ cases hq : q cb n with nq resq nq errq,
{ simp [hp, hn, hq, ←orelse_eq_orelse, parser.orelse] },
{ rcases lt_trichotomy nq n with H|rfl|H;
simp [hp, hq, hn, ←orelse_eq_orelse, parser.orelse, H,
ne_of_gt H, ne_of_lt H, not_lt_of_lt H] <|>
simp [hp, hq, hn, ←orelse_eq_orelse, parser.orelse, lt_irrefl] } },
{ simp [hp, hn, ←orelse_eq_orelse, parser.orelse] } }
end
lemma orelse_eq_fail_not_mono_lt (hn : n' < n) : (p <|> q) cb n = fail n' err ↔
(p cb n = fail n' err) ∨
(q cb n = fail n' err ∧ (∃ (errp), p cb n = fail n errp)) :=
begin
cases hp : p cb n with np resp np errp,
{ simp [hp, ←orelse_eq_orelse, parser.orelse] },
{ by_cases h : np = n,
{ cases hq : q cb n with nq resq nq errq,
{ simp [hp, h, hn, hq, ne_of_gt hn, ←orelse_eq_orelse, parser.orelse] },
{ rcases lt_trichotomy nq n with H|H|H,
{ simp [hp, hq, h, H, ne_of_gt hn, not_lt_of_lt H, ←orelse_eq_orelse, parser.orelse] },
{ simp [hp, hq, h, H, ne_of_gt hn, lt_irrefl, ←orelse_eq_orelse, parser.orelse] },
{ simp [hp, hq, h, H, ne_of_gt (hn.trans H), ←orelse_eq_orelse, parser.orelse] } } },
{ simp [hp, h, ←orelse_eq_orelse, parser.orelse] } }
end
lemma orelse_eq_fail_of_mono_ne [q.mono] (hn : n ≠ n') :
(p <|> q) cb n = fail n' err ↔ p cb n = fail n' err :=
begin
cases hp : p cb n with np resp np errp,
{ simp [hp, ←orelse_eq_orelse, parser.orelse] },
{ by_cases h : np = n,
{ cases hq : q cb n with nq resq nq errq,
{ simp [hp, h, hn, hq, hn, ←orelse_eq_orelse, parser.orelse] },
{ have : n ≤ nq := mono.of_fail hq,
rcases eq_or_lt_of_le this with rfl|H,
{ simp [hp, hq, h, hn, lt_irrefl, ←orelse_eq_orelse, parser.orelse] },
{ simp [hp, hq, h, hn, H, ←orelse_eq_orelse, parser.orelse] } } },
{ simp [hp, h, ←orelse_eq_orelse, parser.orelse] } },
end
@[simp] lemma failure_eq_failure : @parser.failure α = failure := rfl
@[simp] lemma failure_def : (failure : parser α) cb n = fail n dlist.empty := rfl
lemma not_failure_eq_done : ¬ (failure : parser α) cb n = done n' a :=
by simp
lemma failure_eq_fail : (failure : parser α) cb n = fail n' err ↔ n = n' ∧ err = dlist.empty :=
by simp [eq_comm]
lemma seq_eq_done {f : parser (α → β)} {p : parser α} : (f <*> p) cb n = done n' b ↔
∃ (nf : ℕ) (f' : α → β) (a : α), f cb n = done nf f' ∧ p cb nf = done n' a ∧ f' a = b :=
by simp [seq_eq_bind_map]
lemma seq_eq_fail {f : parser (α → β)} {p : parser α} : (f <*> p) cb n = fail n' err ↔
(f cb n = fail n' err) ∨ (∃ (nf : ℕ) (f' : α → β), f cb n = done nf f' ∧ p cb nf = fail n' err) :=
by simp [seq_eq_bind_map]
lemma seq_left_eq_done {p : parser α} {q : parser β} : (p <* q) cb n = done n' a ↔
∃ (np : ℕ) (b : β), p cb n = done np a ∧ q cb np = done n' b :=
begin
have : ∀ (p q : ℕ → α → Prop),
(∃ (np : ℕ) (x : α), p np x ∧ q np x ∧ x = a) ↔ ∃ (np : ℕ), p np a ∧ q np a :=
λ _ _, ⟨λ ⟨np, x, hp, hq, rfl⟩, ⟨np, hp, hq⟩, λ ⟨np, hp, hq⟩, ⟨np, a, hp, hq, rfl⟩⟩,
simp [seq_left_eq, seq_eq_done, map_eq_done, this]
end
lemma seq_left_eq_fail {p : parser α} {q : parser β} : (p <* q) cb n = fail n' err ↔
(p cb n = fail n' err) ∨ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ q cb np = fail n' err) :=
by simp [seq_left_eq, seq_eq_fail]
lemma seq_right_eq_done {p : parser α} {q : parser β} : (p *> q) cb n = done n' b ↔
∃ (np : ℕ) (a : α), p cb n = done np a ∧ q cb np = done n' b :=
by simp [seq_right_eq, seq_eq_done, map_eq_done, and.comm, and.assoc]
lemma seq_right_eq_fail {p : parser α} {q : parser β} : (p *> q) cb n = fail n' err ↔
(p cb n = fail n' err) ∨ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ q cb np = fail n' err) :=
by simp [seq_right_eq, seq_eq_fail]
lemma mmap_eq_done {f : α → parser β} {a : α} {l : list α} {b : β} {l' : list β} :
(a :: l).mmap f cb n = done n' (b :: l') ↔
∃ (np : ℕ), f a cb n = done np b ∧ l.mmap f cb np = done n' l' :=
by simp [mmap, and.comm, and.assoc, and.left_comm, pure_eq_done]
lemma mmap'_eq_done {f : α → parser β} {a : α} {l : list α} :
(a :: l).mmap' f cb n = done n' () ↔
∃ (np : ℕ) (b : β), f a cb n = done np b ∧ l.mmap' f cb np = done n' () :=
by simp [mmap']
lemma guard_eq_done {p : Prop} [decidable p] {u : unit} :
@guard parser _ p _ cb n = done n' u ↔ p ∧ n = n' :=
by { by_cases hp : p; simp [guard, hp, pure_eq_done] }
lemma guard_eq_fail {p : Prop} [decidable p] :
@guard parser _ p _ cb n = fail n' err ↔ (¬ p) ∧ n = n' ∧ err = dlist.empty :=
by { by_cases hp : p; simp [guard, hp, eq_comm, pure_eq_done] }
namespace mono
variables {sep : parser unit}
instance pure : mono (pure a) :=
⟨λ _ _, by simp [pure_eq_done]⟩
instance bind {f : α → parser β} [p.mono] [∀ a, (f a).mono] :
(p >>= f).mono :=
begin
constructor,
intros cb n,
cases hx : (p >>= f) cb n,
{ obtain ⟨n', a, h, h'⟩ := bind_eq_done.mp hx,
refine le_trans (of_done h) _,
simpa [h'] using of_done h' },
{ obtain h | ⟨n', a, h, h'⟩ := bind_eq_fail.mp hx,
{ simpa [h] using of_fail h },
{ refine le_trans (of_done h) _,
simpa [h'] using of_fail h' } }
end
instance and_then {q : parser β} [p.mono] [q.mono] : (p >> q).mono := mono.bind
instance map [p.mono] {f : α → β} : (f <$> p).mono := mono.bind
instance seq {f : parser (α → β)} [f.mono] [p.mono] : (f <*> p).mono := mono.bind
instance mmap : Π {l : list α} {f : α → parser β} [∀ a ∈ l, (f a).mono],
(l.mmap f).mono
| [] _ _ := mono.pure
| (a :: l) f h := begin
convert mono.bind,
{ exact h _ (list.mem_cons_self _ _) },
{ intro,
convert mono.map,
convert mmap,
exact (λ _ ha, h _ (list.mem_cons_of_mem _ ha)) }
end
instance mmap' : Π {l : list α} {f : α → parser β} [∀ a ∈ l, (f a).mono],
(l.mmap' f).mono
| [] _ _ := mono.pure
| (a :: l) f h := begin
convert mono.and_then,
{ exact h _ (list.mem_cons_self _ _) },
{ convert mmap',
exact (λ _ ha, h _ (list.mem_cons_of_mem _ ha)) }
end
instance failure : (failure : parser α).mono :=
⟨by simp [le_refl]⟩
instance guard {p : Prop} [decidable p] : mono (guard p) :=
⟨by { by_cases h : p; simp [h, pure_eq_done, le_refl] }⟩
instance orelse [p.mono] [q.mono] : (p <|> q).mono :=
begin
constructor,
intros cb n,
cases hx : (p <|> q) cb n with posx resx posx errx,
{ obtain h | ⟨h, -, -⟩ := orelse_eq_done.mp hx;
simpa [h] using of_done h },
{ by_cases h : n = posx,
{ simp [hx, h] },
{ simp only [orelse_eq_fail_of_mono_ne h] at hx,
exact of_fail hx } }
end
instance decorate_errors [p.mono] :
(@decorate_errors α msgs p).mono :=
begin
constructor,
intros cb n,
cases h : p cb n,
{ simpa [decorate_errors, h] using of_done h },
{ simp [decorate_errors, h] }
end
instance decorate_error [p.mono] : (@decorate_error α msg p).mono :=
mono.decorate_errors
instance any_char : mono any_char :=
begin
constructor,
intros cb n,
by_cases h : n < cb.size;
simp [any_char, h],
end
instance sat {p : char → Prop} [decidable_pred p] : mono (sat p) :=
begin
constructor,
intros cb n,
simp only [sat],
split_ifs;
simp
end
instance eps : mono eps := mono.pure
instance ch {c : char} : mono (ch c) := mono.decorate_error
instance char_buf {s : char_buffer} : mono (char_buf s) :=
mono.decorate_error
instance one_of {cs : list char} : (one_of cs).mono :=
mono.decorate_errors
instance one_of' {cs : list char} : (one_of' cs).mono :=
mono.and_then
instance str {s : string} : (str s).mono :=
mono.decorate_error
instance remaining : remaining.mono :=
⟨λ _ _, le_refl _⟩
instance eof : eof.mono :=
mono.decorate_error
instance foldr_core {f : α → β → β} {b : β} [p.mono] :
∀ {reps : ℕ}, (foldr_core f p b reps).mono
| 0 := mono.failure
| (reps + 1) := begin
convert mono.orelse,
{ convert mono.bind,
{ apply_instance },
{ exact λ _, @mono.bind _ _ _ _ foldr_core _ } },
{ exact mono.pure }
end
instance foldr {f : α → β → β} [p.mono] : mono (foldr f p b) :=
⟨λ _ _, by { convert mono.le (foldr_core f p b _) _ _, exact mono.foldr_core }⟩
instance foldl_core {f : α → β → α} {p : parser β} [p.mono] :
∀ {a : α} {reps : ℕ}, (foldl_core f a p reps).mono
| _ 0 := mono.failure
| _ (reps + 1) := begin
convert mono.orelse,
{ convert mono.bind,
{ apply_instance },
{ exact λ _, foldl_core } },
{ exact mono.pure }
end
instance foldl {f : α → β → α} {p : parser β} [p.mono] : mono (foldl f a p) :=
⟨λ _ _, by { convert mono.le (foldl_core f a p _) _ _, exact mono.foldl_core }⟩
instance many [p.mono] : p.many.mono :=
mono.foldr
instance many_char {p : parser char} [p.mono] : p.many_char.mono :=
mono.map
instance many' [p.mono] : p.many'.mono :=
mono.and_then
instance many1 [p.mono] : p.many1.mono :=
mono.seq
instance many_char1 {p : parser char} [p.mono] : p.many_char1.mono :=
mono.map
instance sep_by1 [p.mono] [sep.mono] : mono (sep_by1 sep p) :=
mono.seq
instance sep_by [p.mono] [hs : sep.mono] : mono (sep_by sep p) :=
mono.orelse
lemma fix_core {F : parser α → parser α} (hF : ∀ (p : parser α), p.mono → (F p).mono) :
∀ (max_depth : ℕ), mono (fix_core F max_depth)
| 0 := mono.failure
| (max_depth + 1) := hF _ (fix_core _)
instance digit : digit.mono :=
mono.decorate_error
instance nat : nat.mono :=
mono.decorate_error
lemma fix {F : parser α → parser α} (hF : ∀ (p : parser α), p.mono → (F p).mono) :
mono (fix F) :=
⟨λ _ _, by { convert mono.le (parser.fix_core F _) _ _, exact fix_core hF _ }⟩
end mono
@[simp] lemma orelse_pure_eq_fail : (p <|> pure a) cb n = fail n' err ↔
p cb n = fail n' err ∧ n ≠ n' :=
begin
by_cases hn : n = n',
{ simp [hn, pure_eq_done] },
{ simp [orelse_eq_fail_of_mono_ne, hn] }
end
end defn_lemmas
section done
variables {α β : Type} {cb : char_buffer} {n n' : ℕ} {a a' : α} {b : β} {c : char} {u : unit}
{err : dlist string}
lemma any_char_eq_done : any_char cb n = done n' c ↔
∃ (hn : n < cb.size), n' = n + 1 ∧ cb.read ⟨n, hn⟩ = c :=
begin
simp_rw [any_char],
split_ifs with h;
simp [h, eq_comm]
end
lemma any_char_eq_fail : any_char cb n = fail n' err ↔ n = n' ∧ err = dlist.empty ∧ cb.size ≤ n :=
begin
simp_rw [any_char],
split_ifs with h;
simp [←not_lt, h, eq_comm]
end
lemma sat_eq_done {p : char → Prop} [decidable_pred p] : sat p cb n = done n' c ↔
∃ (hn : n < cb.size), p c ∧ n' = n + 1 ∧ cb.read ⟨n, hn⟩ = c :=
begin
by_cases hn : n < cb.size,
{ by_cases hp : p (cb.read ⟨n, hn⟩),
{ simp only [sat, hn, hp, dif_pos, if_true, exists_prop_of_true],
split,
{ rintro ⟨rfl, rfl⟩, simp [hp] },
{ rintro ⟨-, rfl, rfl⟩, simp } },
{ simp only [sat, hn, hp, dif_pos, false_iff, not_and, exists_prop_of_true, if_false],
rintro H - rfl,
exact hp H } },
{ simp [sat, hn] }
end
lemma sat_eq_fail {p : char → Prop} [decidable_pred p] : sat p cb n = fail n' err ↔
n = n' ∧ err = dlist.empty ∧ ∀ (h : n < cb.size), ¬ p (cb.read ⟨n, h⟩) :=
begin
dsimp only [sat],
split_ifs;
simp [*, eq_comm]
end
lemma eps_eq_done : eps cb n = done n' u ↔ n = n' := by simp [eps, pure_eq_done]
lemma ch_eq_done : ch c cb n = done n' u ↔ ∃ (hn : n < cb.size), n' = n + 1 ∧ cb.read ⟨n, hn⟩ = c :=
by simp [ch, eps_eq_done, sat_eq_done, and.comm, @eq_comm _ n']
lemma char_buf_eq_done {cb' : char_buffer} : char_buf cb' cb n = done n' u ↔
n + cb'.size = n' ∧ cb'.to_list <+: (cb.to_list.drop n) :=
begin
simp only [char_buf, decorate_error_eq_done, ne.def, ←buffer.length_to_list],
induction cb'.to_list with hd tl hl generalizing cb n n',
{ simp [pure_eq_done, mmap'_eq_done, -buffer.length_to_list, list.nil_prefix] },
{ simp only [ch_eq_done, and.comm, and.assoc, and.left_comm, hl, mmap', and_then_eq_bind,
bind_eq_done, list.length, exists_and_distrib_left, exists_const],
split,
{ rintro ⟨np, h, rfl, rfl, hn, rfl⟩,
simp only [add_comm, add_left_comm, h, true_and, eq_self_iff_true, and_true],
have : n < cb.to_list.length := by simpa using hn,
rwa [←buffer.nth_le_to_list _ this, ←list.cons_nth_le_drop_succ this, list.prefix_cons_inj] },
{ rintro ⟨h, rfl⟩,
by_cases hn : n < cb.size,
{ have : n < cb.to_list.length := by simpa using hn,
rw [←list.cons_nth_le_drop_succ this, list.cons_prefix_iff] at h,
use [n + 1, h.right],
simpa [buffer.nth_le_to_list, add_comm, add_left_comm, add_assoc, hn] using h.left.symm },
{ have : cb.to_list.length ≤ n := by simpa using hn,
rw list.drop_eq_nil_of_le this at h,
simpa using h } } }
end
lemma one_of_eq_done {cs : list char} : one_of cs cb n = done n' c ↔
∃ (hn : n < cb.size), c ∈ cs ∧ n' = n + 1 ∧ cb.read ⟨n, hn⟩ = c :=
by simp [one_of, sat_eq_done]
lemma one_of'_eq_done {cs : list char} : one_of' cs cb n = done n' u ↔
∃ (hn : n < cb.size), cb.read ⟨n, hn⟩ ∈ cs ∧ n' = n + 1 :=
begin
simp only [one_of', one_of_eq_done, eps_eq_done, and.comm, and_then_eq_bind, bind_eq_done,
exists_eq_left, exists_and_distrib_left],
split,
{ rintro ⟨c, hc, rfl, hn, rfl⟩,
exact ⟨rfl, hn, hc⟩ },
{ rintro ⟨rfl, hn, hc⟩,
exact ⟨cb.read ⟨n, hn⟩, hc, rfl, hn, rfl⟩ }
end
lemma str_eq_char_buf (s : string) : str s = char_buf s.to_list.to_buffer :=
begin
ext cb n,
rw [str, char_buf],
congr,
{ simp [buffer.to_string, string.as_string_inv_to_list] },
{ simp }
end
lemma str_eq_done {s : string} : str s cb n = done n' u ↔
n + s.length = n' ∧ s.to_list <+: (cb.to_list.drop n) :=
by simp [str_eq_char_buf, char_buf_eq_done]
lemma remaining_eq_done {r : ℕ} : remaining cb n = done n' r ↔ n = n' ∧ cb.size - n = r :=
by simp [remaining]
lemma remaining_ne_fail : remaining cb n ≠ fail n' err :=
by simp [remaining]
lemma eof_eq_done {u : unit} : eof cb n = done n' u ↔ n = n' ∧ cb.size ≤ n :=
by simp [eof, guard_eq_done, remaining_eq_done, tsub_eq_zero_iff_le, and_comm, and_assoc]
@[simp] lemma foldr_core_zero_eq_done {f : α → β → β} {p : parser α} {b' : β} :
foldr_core f p b 0 cb n ≠ done n' b' :=
by simp [foldr_core]
lemma foldr_core_eq_done {f : α → β → β} {p : parser α} {reps : ℕ} {b' : β} :
foldr_core f p b (reps + 1) cb n = done n' b' ↔
(∃ (np : ℕ) (a : α) (xs : β), p cb n = done np a ∧ foldr_core f p b reps cb np = done n' xs
∧ f a xs = b') ∨
(n = n' ∧ b = b' ∧ ∃ (err), (p cb n = fail n err) ∨
(∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldr_core f p b reps cb np = fail n err)) :=
by simp [foldr_core, and.comm, and.assoc, pure_eq_done]
@[simp] lemma foldr_core_zero_eq_fail {f : α → β → β} {p : parser α} {err : dlist string} :
foldr_core f p b 0 cb n = fail n' err ↔ n = n' ∧ err = dlist.empty :=
by simp [foldr_core, eq_comm]
lemma foldr_core_succ_eq_fail {f : α → β → β} {p : parser α} {reps : ℕ} {err : dlist string} :
foldr_core f p b (reps + 1) cb n = fail n' err ↔ n ≠ n' ∧
(p cb n = fail n' err ∨
∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldr_core f p b reps cb np = fail n' err) :=
by simp [foldr_core, and_comm]
lemma foldr_eq_done {f : α → β → β} {p : parser α} {b' : β} :
foldr f p b cb n = done n' b' ↔
((∃ (np : ℕ) (a : α) (x : β), p cb n = done np a ∧
foldr_core f p b (cb.size - n) cb np = done n' x ∧ f a x = b') ∨
(n = n' ∧ b = b' ∧ (∃ (err), p cb n = parse_result.fail n err ∨
∃ (np : ℕ) (x : α), p cb n = done np x ∧ foldr_core f p b (cb.size - n) cb np = fail n err))) :=
by simp [foldr, foldr_core_eq_done]
lemma foldr_eq_fail_iff_mono_at_end {f : α → β → β} {p : parser α} {err : dlist string}
[p.mono] (hc : cb.size ≤ n) : foldr f p b cb n = fail n' err ↔
n < n' ∧ (p cb n = fail n' err ∨ ∃ (a : α), p cb n = done n' a ∧ err = dlist.empty) :=
begin
have : cb.size - n = 0 := tsub_eq_zero_iff_le.mpr hc,
simp only [foldr, foldr_core_succ_eq_fail, this, and.left_comm, foldr_core_zero_eq_fail,
ne_iff_lt_iff_le, exists_and_distrib_right, exists_eq_left, and.congr_left_iff,
exists_and_distrib_left],
rintro (h | ⟨⟨a, h⟩, rfl⟩),
{ exact mono.of_fail h },
{ exact mono.of_done h }
end
lemma foldr_eq_fail {f : α → β → β} {p : parser α} {err : dlist string} :
foldr f p b cb n = fail n' err ↔ n ≠ n' ∧ (p cb n = fail n' err ∨
∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldr_core f p b (cb.size - n) cb np = fail n' err) :=
by simp [foldr, foldr_core_succ_eq_fail]
@[simp] lemma foldl_core_zero_eq_done {f : β → α → β} {p : parser α} {b' : β} :
foldl_core f b p 0 cb n = done n' b' ↔ false :=
by simp [foldl_core]
lemma foldl_core_eq_done {f : β → α → β} {p : parser α} {reps : ℕ} {b' : β} :
foldl_core f b p (reps + 1) cb n = done n' b' ↔
(∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldl_core f (f b a) p reps cb np = done n' b') ∨
(n = n' ∧ b = b' ∧ ∃ (err), (p cb n = fail n err) ∨
(∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldl_core f (f b a) p reps cb np = fail n err)) :=
by simp [foldl_core, and.assoc, pure_eq_done]
@[simp] lemma foldl_core_zero_eq_fail {f : β → α → β} {p : parser α} {err : dlist string} :
foldl_core f b p 0 cb n = fail n' err ↔ n = n' ∧ err = dlist.empty :=
by simp [foldl_core, eq_comm]
lemma foldl_core_succ_eq_fail {f : β → α → β} {p : parser α} {reps : ℕ} {err : dlist string} :
foldl_core f b p (reps + 1) cb n = fail n' err ↔ n ≠ n' ∧
(p cb n = fail n' err ∨
∃ (np : ℕ) (a : α), p cb n = done np a ∧ foldl_core f (f b a) p reps cb np = fail n' err) :=
by simp [foldl_core, and_comm]
lemma foldl_eq_done {f : β → α → β} {p : parser α} {b' : β} :
foldl f b p cb n = done n' b' ↔
(∃ (np : ℕ) (a : α), p cb n = done np a ∧
foldl_core f (f b a) p (cb.size - n) cb np = done n' b') ∨
(n = n' ∧ b = b' ∧ ∃ (err), (p cb n = fail n err) ∨
(∃ (np : ℕ) (a : α), p cb n = done np a ∧
foldl_core f (f b a) p (cb.size - n) cb np = fail n err)) :=
by simp [foldl, foldl_core_eq_done]
lemma foldl_eq_fail {f : β → α → β} {p : parser α} {err : dlist string} :
foldl f b p cb n = fail n' err ↔ n ≠ n' ∧ (p cb n = fail n' err ∨
∃ (np : ℕ) (a : α), p cb n = done np a ∧
foldl_core f (f b a) p (cb.size - n) cb np = fail n' err) :=
by simp [foldl, foldl_core_succ_eq_fail]
lemma foldl_eq_fail_iff_mono_at_end {f : β → α → β} {p : parser α} {err : dlist string}
[p.mono] (hc : cb.size ≤ n) : foldl f b p cb n = fail n' err ↔
n < n' ∧ (p cb n = fail n' err ∨ ∃ (a : α), p cb n = done n' a ∧ err = dlist.empty) :=
begin
have : cb.size - n = 0 := tsub_eq_zero_iff_le.mpr hc,
simp only [foldl, foldl_core_succ_eq_fail, this, and.left_comm, ne_iff_lt_iff_le, exists_eq_left,
exists_and_distrib_right, and.congr_left_iff, exists_and_distrib_left,
foldl_core_zero_eq_fail],
rintro (h | ⟨⟨a, h⟩, rfl⟩),
{ exact mono.of_fail h },
{ exact mono.of_done h }
end
lemma many_eq_done_nil {p : parser α} : many p cb n = done n' (@list.nil α) ↔ n = n' ∧
∃ (err), p cb n = fail n err ∨ ∃ (np : ℕ) (a : α), p cb n = done np a ∧
foldr_core list.cons p [] (cb.size - n) cb np = fail n err :=
by simp [many, foldr_eq_done]
lemma many_eq_done {p : parser α} {x : α} {xs : list α} :
many p cb n = done n' (x :: xs) ↔ ∃ (np : ℕ), p cb n = done np x
∧ foldr_core list.cons p [] (cb.size - n) cb np = done n' xs :=
by simp [many, foldr_eq_done, and.comm, and.assoc, and.left_comm]
lemma many_eq_fail {p : parser α} {err : dlist string} :
many p cb n = fail n' err ↔ n ≠ n' ∧ (p cb n = fail n' err ∨
∃ (np : ℕ) (a : α), p cb n = done np a ∧
foldr_core list.cons p [] (cb.size - n) cb np = fail n' err) :=
by simp [many, foldr_eq_fail]
lemma many_char_eq_done_empty {p : parser char} : many_char p cb n = done n' string.empty ↔ n = n' ∧
∃ (err), p cb n = fail n err ∨ ∃ (np : ℕ) (c : char), p cb n = done np c ∧
foldr_core list.cons p [] (cb.size - n) cb np = fail n err :=
by simp [many_char, many_eq_done_nil, map_eq_done, list.as_string_eq]
lemma many_char_eq_done_not_empty {p : parser char} {s : string} (h : s ≠ "") :
many_char p cb n = done n' s ↔ ∃ (np : ℕ), p cb n = done np s.head ∧
foldr_core list.cons p list.nil (buffer.size cb - n) cb np = done n' (s.popn 1).to_list :=
by simp [many_char, list.as_string_eq, string.to_list_nonempty h, many_eq_done]
lemma many_char_eq_many_of_to_list {p : parser char} {s : string} :
many_char p cb n = done n' s ↔ many p cb n = done n' s.to_list :=
by simp [many_char, list.as_string_eq]
lemma many'_eq_done {p : parser α} : many' p cb n = done n' u ↔
many p cb n = done n' [] ∨ ∃ (np : ℕ) (a : α) (l : list α), many p cb n = done n' (a :: l)
∧ p cb n = done np a ∧ foldr_core list.cons p [] (buffer.size cb - n) cb np = done n' l :=
begin
simp only [many', eps_eq_done, many, foldr, and_then_eq_bind, exists_and_distrib_right,
bind_eq_done, exists_eq_right],
split,
{ rintro ⟨_ | ⟨hd, tl⟩, hl⟩,
{ exact or.inl hl },
{ have hl2 := hl,
simp only [foldr_core_eq_done, or_false, exists_and_distrib_left, and_false, false_and,
exists_eq_right_right] at hl,
obtain ⟨np, hp, h⟩ := hl,
refine or.inr ⟨np, _, _, hl2, hp, h⟩ } },
{ rintro (h | ⟨np, a, l, hp, h⟩),
{ exact ⟨[], h⟩ },
{ refine ⟨a :: l, hp⟩ } }
end
@[simp] lemma many1_ne_done_nil {p : parser α} : many1 p cb n ≠ done n' [] :=
by simp [many1, seq_eq_done]
lemma many1_eq_done {p : parser α} {l : list α} : many1 p cb n = done n' (a :: l) ↔
∃ (np : ℕ), p cb n = done np a ∧ many p cb np = done n' l :=
by simp [many1, seq_eq_done, map_eq_done]
lemma many1_eq_fail {p : parser α} {err : dlist string} : many1 p cb n = fail n' err ↔
p cb n = fail n' err ∨ (∃ (np : ℕ) (a : α), p cb n = done np a ∧ many p cb np = fail n' err) :=
by simp [many1, seq_eq_fail]
@[simp] lemma many_char1_ne_empty {p : parser char} : many_char1 p cb n ≠ done n' "" :=
by simp [many_char1, ←string.nil_as_string_eq_empty]
lemma many_char1_eq_done {p : parser char} {s : string} (h : s ≠ "") :
many_char1 p cb n = done n' s ↔
∃ (np : ℕ), p cb n = done np s.head ∧ many_char p cb np = done n' (s.popn 1) :=
by simp [many_char1, list.as_string_eq, string.to_list_nonempty h, many1_eq_done,
many_char_eq_many_of_to_list]
@[simp] lemma sep_by1_ne_done_nil {sep : parser unit} {p : parser α} :
sep_by1 sep p cb n ≠ done n' [] :=
by simp [sep_by1, seq_eq_done]
lemma sep_by1_eq_done {sep : parser unit} {p : parser α} {l : list α} :
sep_by1 sep p cb n = done n' (a :: l) ↔ ∃ (np : ℕ), p cb n = done np a ∧
(sep >> p).many cb np = done n' l :=
by simp [sep_by1, seq_eq_done]
lemma sep_by_eq_done_nil {sep : parser unit} {p : parser α} :
sep_by sep p cb n = done n' [] ↔ n = n' ∧ ∃ (err), sep_by1 sep p cb n = fail n err :=
by simp [sep_by, pure_eq_done]
@[simp] lemma fix_core_ne_done_zero {F : parser α → parser α} :
fix_core F 0 cb n ≠ done n' a :=
by simp [fix_core]
lemma fix_core_eq_done {F : parser α → parser α} {max_depth : ℕ} :
fix_core F (max_depth + 1) cb n = done n' a ↔ F (fix_core F max_depth) cb n = done n' a :=
by simp [fix_core]
lemma digit_eq_done {k : ℕ} : digit cb n = done n' k ↔ ∃ (hn : n < cb.size), n' = n + 1 ∧ k ≤ 9 ∧
(cb.read ⟨n, hn⟩).to_nat - '0'.to_nat = k ∧ '0' ≤ cb.read ⟨n, hn⟩ ∧ cb.read ⟨n, hn⟩ ≤ '9' :=
begin
have c9 : '9'.to_nat - '0'.to_nat = 9 := rfl,
have l09 : '0'.to_nat ≤ '9'.to_nat := dec_trivial,
have le_iff_le : ∀ {c c' : char}, c ≤ c' ↔ c.to_nat ≤ c'.to_nat := λ _ _, iff.rfl,
split,
{ simp only [digit, sat_eq_done, pure_eq_done, decorate_error_eq_done, bind_eq_done, ←c9],
rintro ⟨np, c, ⟨hn, ⟨ge0, le9⟩, rfl, rfl⟩, rfl, rfl⟩,
simpa [hn, ge0, le9, true_and, and_true, eq_self_iff_true, exists_prop_of_true,
tsub_le_tsub_iff_right, l09] using (le_iff_le.mp le9) },
{ simp only [digit, sat_eq_done, pure_eq_done, decorate_error_eq_done, bind_eq_done, ←c9,
le_iff_le],
rintro ⟨hn, rfl, -, rfl, ge0, le9⟩,
use [n + 1, cb.read ⟨n, hn⟩],
simp [hn, ge0, le9] }
end
lemma digit_eq_fail : digit cb n = fail n' err ↔ n = n' ∧ err = dlist.of_list ["<digit>"] ∧
∀ (h : n < cb.size), ¬ ((λ c, '0' ≤ c ∧ c ≤ '9') (cb.read ⟨n, h⟩)) :=
by simp [digit, sat_eq_fail]
end done
namespace static
variables {α β : Type} {p q : parser α} {msgs : thunk (list string)} {msg : thunk string}
{cb : char_buffer} {n' n : ℕ} {err : dlist string} {a : α} {b : β} {sep : parser unit}
lemma not_of_ne (h : p cb n = done n' a) (hne : n ≠ n') : ¬ static p :=
by { introI, exact hne (of_done h) }
instance pure : static (pure a) :=
⟨λ _ _ _ _, by { simp_rw pure_eq_done, rw [and.comm], simp }⟩
instance bind {f : α → parser β} [p.static] [∀ a, (f a).static] :
(p >>= f).static :=
⟨λ _ _ _ _, by { rw bind_eq_done, rintro ⟨_, _, hp, hf⟩, exact trans (of_done hp) (of_done hf) }⟩
instance and_then {q : parser β} [p.static] [q.static] : (p >> q).static := static.bind
instance map [p.static] {f : α → β} : (f <$> p).static :=
⟨λ _ _ _ _, by { simp_rw map_eq_done, rintro ⟨_, hp, _⟩, exact of_done hp }⟩
instance seq {f : parser (α → β)} [f.static] [p.static] : (f <*> p).static := static.bind
instance mmap : Π {l : list α} {f : α → parser β} [∀ a, (f a).static], (l.mmap f).static
| [] _ _ := static.pure
| (a :: l) _ h := begin
convert static.bind,
{ exact h _ },
{ intro,
convert static.bind,
{ convert mmap,
exact h },
{ exact λ _, static.pure } }
end
instance mmap' : Π {l : list α} {f : α → parser β} [∀ a, (f a).static], (l.mmap' f).static
| [] _ _ := static.pure
| (a :: l) _ h := begin
convert static.and_then,
{ exact h _ },
{ convert mmap',
exact h }
end
instance failure : @parser.static α failure :=
⟨λ _ _ _ _, by simp⟩
instance guard {p : Prop} [decidable p] : static (guard p) :=
⟨λ _ _ _ _, by simp [guard_eq_done]⟩
instance orelse [p.static] [q.static] : (p <|> q).static :=
⟨λ _ _ _ _, by { simp_rw orelse_eq_done, rintro (h | ⟨h, -⟩); exact of_done h }⟩
instance decorate_errors [p.static] :
(@decorate_errors α msgs p).static :=
⟨λ _ _ _ _, by { rw decorate_errors_eq_done, exact of_done }⟩
instance decorate_error [p.static] : (@decorate_error α msg p).static :=
static.decorate_errors
lemma any_char : ¬ static any_char :=
begin
have : any_char "s".to_char_buffer 0 = done 1 's',
{ have : 0 < "s".to_char_buffer.size := dec_trivial,
simpa [any_char_eq_done, this] },
exact not_of_ne this zero_ne_one
end
lemma sat_iff {p : char → Prop} [decidable_pred p] : static (sat p) ↔ ∀ c, ¬ p c :=
begin
split,
{ introI,
intros c hc,
have : sat p [c].to_buffer 0 = done 1 c := by simp [sat_eq_done, hc],
exact zero_ne_one (of_done this) },
{ contrapose!,
simp only [iff, sat_eq_done, and_imp, exists_prop, exists_and_distrib_right,
exists_and_distrib_left, exists_imp_distrib, not_forall],
rintros _ _ _ a h hne rfl hp -,
exact ⟨a, hp⟩ }
end
instance sat : static (sat (λ _, false)) :=
by { apply sat_iff.mpr, simp }
instance eps : static eps := static.pure
lemma ch (c : char) : ¬ static (ch c) :=
begin
have : ch c [c].to_buffer 0 = done 1 (),
{ have : 0 < [c].to_buffer.size := dec_trivial,
simp [ch_eq_done, this] },
exact not_of_ne this zero_ne_one
end
lemma char_buf_iff {cb' : char_buffer} : static (char_buf cb') ↔ cb' = buffer.nil :=
begin
rw ←buffer.size_eq_zero_iff,
have : char_buf cb' cb' 0 = done cb'.size () := by simp [char_buf_eq_done],
cases hc : cb'.size with n,
{ simp only [eq_self_iff_true, iff_true],
exact ⟨λ _ _ _ _ h, by simpa [hc] using (char_buf_eq_done.mp h).left⟩ },
{ rw hc at this,
simpa [nat.succ_ne_zero] using not_of_ne this (nat.succ_ne_zero n).symm }
end
lemma one_of_iff {cs : list char} : static (one_of cs) ↔ cs = [] :=
begin
cases cs with hd tl,
{ simp [one_of, static.decorate_errors] },
{ have : one_of (hd :: tl) (hd :: tl).to_buffer 0 = done 1 hd,
{ simp [one_of_eq_done] },
simpa using not_of_ne this zero_ne_one }
end
instance one_of : static (one_of []) :=
by { apply one_of_iff.mpr, refl }
lemma one_of'_iff {cs : list char} : static (one_of' cs) ↔ cs = [] :=
begin
cases cs with hd tl,
{ simp [one_of', static.bind], },
{ have : one_of' (hd :: tl) (hd :: tl).to_buffer 0 = done 1 (),
{ simp [one_of'_eq_done] },
simpa using not_of_ne this zero_ne_one }
end
instance one_of' : static (one_of []) :=
by { apply one_of_iff.mpr, refl }
lemma str_iff {s : string} : static (str s) ↔ s = "" :=
by simp [str_eq_char_buf, char_buf_iff, ←string.to_list_inj, buffer.ext_iff]
instance remaining : remaining.static :=
⟨λ _ _ _ _ h, (remaining_eq_done.mp h).left⟩
instance eof : eof.static :=
static.decorate_error
instance foldr_core {f : α → β → β} [p.static] :
∀ {b : β} {reps : ℕ}, (foldr_core f p b reps).static
| _ 0 := static.failure
| _ (reps + 1) := begin
simp_rw parser.foldr_core,
convert static.orelse,
{ convert static.bind,
{ apply_instance },
{ intro,
convert static.bind,
{ exact foldr_core },
{ apply_instance } } },
{ exact static.pure }
end
instance foldr {f : α → β → β} [p.static] : static (foldr f p b) :=
⟨λ _ _ _ _, by { dsimp [foldr], exact of_done }⟩
instance foldl_core {f : α → β → α} {p : parser β} [p.static] :
∀ {a : α} {reps : ℕ}, (foldl_core f a p reps).static
| _ 0 := static.failure
| _ (reps + 1) := begin
convert static.orelse,
{ convert static.bind,
{ apply_instance },
{ exact λ _, foldl_core } },
{ exact static.pure }
end
instance foldl {f : α → β → α} {p : parser β} [p.static] : static (foldl f a p) :=
⟨λ _ _ _ _, by { dsimp [foldl], exact of_done }⟩
instance many [p.static] : p.many.static :=
static.foldr
instance many_char {p : parser char} [p.static] : p.many_char.static :=
static.map
instance many' [p.static] : p.many'.static :=
static.and_then
instance many1 [p.static] : p.many1.static :=
static.seq
instance many_char1 {p : parser char} [p.static] : p.many_char1.static :=
static.map
instance sep_by1 [p.static] [sep.static] : static (sep_by1 sep p) :=
static.seq
instance sep_by [p.static] [sep.static] : static (sep_by sep p) :=
static.orelse
lemma fix_core {F : parser α → parser α} (hF : ∀ (p : parser α), p.static → (F p).static) :
∀ (max_depth : ℕ), static (fix_core F max_depth)
| 0 := static.failure
| (max_depth + 1) := hF _ (fix_core _)
lemma digit : ¬ digit.static :=
begin
have : digit "1".to_char_buffer 0 = done 1 1,
{ have : 0 < "s".to_char_buffer.size := dec_trivial,
simpa [this] },
exact not_of_ne this zero_ne_one
end
lemma nat : ¬ nat.static :=
begin
have : nat "1".to_char_buffer 0 = done 1 1,
{ have : 0 < "s".to_char_buffer.size := dec_trivial,
simpa [this] },
exact not_of_ne this zero_ne_one
end
lemma fix {F : parser α → parser α} (hF : ∀ (p : parser α), p.static → (F p).static) :
static (fix F) :=
⟨λ cb n _ _ h,
by { haveI := fix_core hF (cb.size - n + 1), dsimp [fix] at h, exact static.of_done h }⟩
end static
namespace bounded
variables {α β : Type} {msgs : thunk (list string)} {msg : thunk string}
variables {p q : parser α} {cb : char_buffer} {n n' : ℕ} {err : dlist string}
variables {a : α} {b : β}
lemma done_of_unbounded (h : ¬p.bounded) : ∃ (cb : char_buffer) (n n' : ℕ) (a : α),
p cb n = done n' a ∧ cb.size ≤ n :=
begin
contrapose! h,
constructor,
intros cb n hn,
cases hp : p cb n,
{ exact absurd hn (h _ _ _ _ hp).not_le },
{ simp [hp] }
end
lemma pure : ¬ bounded (pure a) :=
begin
introI,
have : (pure a : parser α) buffer.nil 0 = done 0 a := by simp [pure_eq_done],
exact absurd (bounded.of_done this) (lt_irrefl _)
end
instance bind {f : α → parser β} [p.bounded] :
(p >>= f).bounded :=
begin
constructor,
intros cb n hn,
obtain ⟨_, _, hp⟩ := bounded.exists p hn,
simp [hp]
end
instance and_then {q : parser β} [p.bounded] : (p >> q).bounded :=
bounded.bind
instance map [p.bounded] {f : α → β} : (f <$> p).bounded :=
bounded.bind
instance seq {f : parser (α → β)} [f.bounded] : (f <*> p).bounded :=
bounded.bind
instance mmap {a : α} {l : list α} {f : α → parser β} [∀ a, (f a).bounded] :
((a :: l).mmap f).bounded :=
bounded.bind
instance mmap' {a : α} {l : list α} {f : α → parser β} [∀ a, (f a).bounded] :
((a :: l).mmap' f).bounded :=
bounded.and_then
instance failure : @parser.bounded α failure :=
⟨by simp⟩
lemma guard_iff {p : Prop} [decidable p] : bounded (guard p) ↔ ¬ p :=
by simpa [guard, apply_ite bounded, pure, failure] using λ _, bounded.failure
instance orelse [p.bounded] [q.bounded] : (p <|> q).bounded :=
begin
constructor,
intros cb n hn,
cases hx : (p <|> q) cb n with posx resx posx errx,
{ obtain h | ⟨h, -, -⟩ := orelse_eq_done.mp hx;
exact absurd hn (of_done h).not_le },
{ simp }
end
instance decorate_errors [p.bounded] :
(@decorate_errors α msgs p).bounded :=
begin
constructor,
intros _ _,
simpa using bounded.exists p
end
lemma decorate_errors_iff : (@parser.decorate_errors α msgs p).bounded ↔ p.bounded :=
begin
split,
{ introI,
constructor,
intros _ _ hn,
obtain ⟨_, _, h⟩ := bounded.exists (@parser.decorate_errors α msgs p) hn,
simp [decorate_errors_eq_fail] at h,
exact h.right.right },
{ introI,
constructor,
intros _ _ hn,
obtain ⟨_, _, h⟩ := bounded.exists p hn,
simp [h] }
end
instance decorate_error [p.bounded] : (@decorate_error α msg p).bounded :=
bounded.decorate_errors
lemma decorate_error_iff : (@parser.decorate_error α msg p).bounded ↔ p.bounded :=
decorate_errors_iff
instance any_char : bounded any_char :=
⟨λ cb n hn, by simp [any_char, hn]⟩
instance sat {p : char → Prop} [decidable_pred p] : bounded (sat p) :=
⟨λ cb n hn, by simp [sat, hn]⟩
lemma eps : ¬ bounded eps := pure
instance ch {c : char} : bounded (ch c) :=
bounded.decorate_error
lemma char_buf_iff {cb' : char_buffer} : bounded (char_buf cb') ↔ cb' ≠ buffer.nil :=
begin
have : cb' ≠ buffer.nil ↔ cb'.to_list ≠ [] :=
not_iff_not_of_iff ⟨λ h, by simp [h], λ h, by simpa using congr_arg list.to_buffer h⟩,
rw [char_buf, decorate_error_iff, this],
cases cb'.to_list,
{ simp [pure, ch] },
{ simp only [iff_true, ne.def, not_false_iff],
apply_instance }
end
instance one_of {cs : list char} : (one_of cs).bounded :=
bounded.decorate_errors
instance one_of' {cs : list char} : (one_of' cs).bounded :=
bounded.and_then
lemma str_iff {s : string} : (str s).bounded ↔ s ≠ "" :=
begin
rw [str, decorate_error_iff],
cases hs : s.to_list,
{ have : s = "",
{ cases s, rw [string.to_list] at hs, simpa [hs] },
simp [pure, this] },
{ have : s ≠ "",
{ intro H, simpa [H] using hs },
simp only [this, iff_true, ne.def, not_false_iff],
apply_instance }
end
lemma remaining : ¬ remaining.bounded :=
begin
introI,
have : remaining buffer.nil 0 = done 0 0 := by simp [remaining_eq_done],
exact absurd (bounded.of_done this) (lt_irrefl _)
end
lemma eof : ¬ eof.bounded :=
begin
introI,
have : eof buffer.nil 0 = done 0 () := by simp [eof_eq_done],
exact absurd (bounded.of_done this) (lt_irrefl _)
end
section fold
instance foldr_core_zero {f : α → β → β} : (foldr_core f p b 0).bounded :=
bounded.failure
instance foldl_core_zero {f : β → α → β} {b : β} : (foldl_core f b p 0).bounded :=
bounded.failure
variables {reps : ℕ} [hpb : p.bounded] (he : ∀ cb n n' err, p cb n = fail n' err → n ≠ n')
include hpb he
lemma foldr_core {f : α → β → β} : (foldr_core f p b reps).bounded :=
begin
cases reps,
{ exact bounded.foldr_core_zero },
constructor,
intros cb n hn,
obtain ⟨np, errp, hp⟩ := bounded.exists p hn,
simpa [foldr_core_succ_eq_fail, hp] using he cb n np errp,
end
lemma foldr {f : α → β → β} : bounded (foldr f p b) :=
begin
constructor,
intros cb n hn,
haveI : (parser.foldr_core f p b (cb.size - n + 1)).bounded := foldr_core he,
obtain ⟨np, errp, hp⟩ := bounded.exists (parser.foldr_core f p b (cb.size - n + 1)) hn,
simp [foldr, hp]
end
lemma foldl_core {f : β → α → β} :
(foldl_core f b p reps).bounded :=
begin
cases reps,
{ exact bounded.foldl_core_zero },
constructor,
intros cb n hn,
obtain ⟨np, errp, hp⟩ := bounded.exists p hn,
simpa [foldl_core_succ_eq_fail, hp] using he cb n np errp,
end
lemma foldl {f : β → α → β} : bounded (foldl f b p) :=
begin
constructor,
intros cb n hn,
haveI : (parser.foldl_core f b p (cb.size - n + 1)).bounded := foldl_core he,
obtain ⟨np, errp, hp⟩ := bounded.exists (parser.foldl_core f b p (cb.size - n + 1)) hn,
simp [foldl, hp]
end
lemma many : p.many.bounded :=
foldr he
omit hpb
lemma many_char {pc : parser char} [pc.bounded]
(he : ∀ cb n n' err, pc cb n = fail n' err → n ≠ n'): pc.many_char.bounded :=
by { convert bounded.map, exact many he }
include hpb
lemma many' : p.many'.bounded :=
by { convert bounded.and_then, exact many he }
end fold
instance many1 [p.bounded] : p.many1.bounded :=
bounded.seq
instance many_char1 {p : parser char} [p.bounded] : p.many_char1.bounded :=
bounded.map
instance sep_by1 {sep : parser unit} [p.bounded] : bounded (sep_by1 sep p) :=
bounded.seq
lemma fix_core {F : parser α → parser α} (hF : ∀ (p : parser α), p.bounded → (F p).bounded) :
∀ (max_depth : ℕ), bounded (fix_core F max_depth)
| 0 := bounded.failure
| (max_depth + 1) := hF _ (fix_core _)
instance digit : digit.bounded :=
bounded.decorate_error
instance nat : nat.bounded :=
bounded.decorate_error
lemma fix {F : parser α → parser α} (hF : ∀ (p : parser α), p.bounded → (F p).bounded) :
bounded (fix F) :=
begin
constructor,
intros cb n hn,
haveI : (parser.fix_core F (cb.size - n + 1)).bounded := fix_core hF _,
obtain ⟨np, errp, hp⟩ := bounded.exists (parser.fix_core F (cb.size - n + 1)) hn,
simp [fix, hp]
end
end bounded
namespace unfailing
variables {α β : Type} {p q : parser α} {msgs : thunk (list string)} {msg : thunk string}
{cb : char_buffer} {n' n : ℕ} {err : dlist string} {a : α} {b : β} {sep : parser unit}
lemma of_bounded [p.bounded] : ¬ unfailing p :=
begin
introI,
cases h : p buffer.nil 0,
{ simpa [lt_irrefl] using bounded.of_done h },
{ exact of_fail h }
end
instance pure : unfailing (pure a) :=
⟨λ _ _, by simp [pure_eq_done]⟩
instance bind {f : α → parser β} [p.unfailing] [∀ a, (f a).unfailing] :
(p >>= f).unfailing :=
⟨λ cb n, begin
obtain ⟨np, a, hp⟩ := exists_done p cb n,
simpa [hp, and.comm, and.left_comm, and.assoc] using exists_done (f a) cb np
end⟩
instance and_then {q : parser β} [p.unfailing] [q.unfailing] : (p >> q).unfailing := unfailing.bind
instance map [p.unfailing] {f : α → β} : (f <$> p).unfailing := unfailing.bind
instance seq {f : parser (α → β)} [f.unfailing] [p.unfailing] : (f <*> p).unfailing :=
unfailing.bind
instance mmap {l : list α} {f : α → parser β} [∀ a, (f a).unfailing] : (l.mmap f).unfailing :=
begin
constructor,
induction l with hd tl hl,
{ intros,
simp [pure_eq_done] },
{ intros,
obtain ⟨np, a, hp⟩ := exists_done (f hd) cb n,
obtain ⟨n', b, hf⟩ := hl cb np,
simp [hp, hf, and.comm, and.left_comm, and.assoc, pure_eq_done] }
end
instance mmap' {l : list α} {f : α → parser β} [∀ a, (f a).unfailing] : (l.mmap' f).unfailing :=
begin
constructor,
induction l with hd tl hl,
{ intros,
simp [pure_eq_done] },
{ intros,
obtain ⟨np, a, hp⟩ := exists_done (f hd) cb n,
obtain ⟨n', b, hf⟩ := hl cb np,
simp [hp, hf, and.comm, and.left_comm, and.assoc, pure_eq_done] }
end
lemma failure : ¬ @parser.unfailing α failure :=
begin
introI h,
have : (failure : parser α) buffer.nil 0 = fail 0 dlist.empty := by simp,
exact of_fail this
end
instance guard_true : unfailing (guard true) := unfailing.pure
lemma guard : ¬ unfailing (guard false) :=
unfailing.failure
instance orelse [p.unfailing] : (p <|> q).unfailing :=
⟨λ cb n, by { obtain ⟨_, _, h⟩ := p.exists_done cb n, simp [success_iff, h] }⟩
instance decorate_errors [p.unfailing] :
(@decorate_errors α msgs p).unfailing :=
⟨λ cb n, by { obtain ⟨_, _, h⟩ := p.exists_done cb n, simp [success_iff, h] }⟩
instance decorate_error [p.unfailing] : (@decorate_error α msg p).unfailing :=
unfailing.decorate_errors
instance any_char : conditionally_unfailing any_char :=
⟨λ _ _ hn, by simp [success_iff, any_char_eq_done, hn]⟩
lemma sat : conditionally_unfailing (sat (λ _, true)) :=
⟨λ _ _ hn, by simp [success_iff, sat_eq_done, hn]⟩
instance eps : unfailing eps := unfailing.pure
instance remaining : remaining.unfailing :=
⟨λ _ _, by simp [success_iff, remaining_eq_done]⟩
lemma foldr_core_zero {f : α → β → β} {b : β} : ¬ (foldr_core f p b 0).unfailing :=
unfailing.failure
instance foldr_core_of_static {f : α → β → β} {b : β} {reps : ℕ} [p.static] [p.unfailing] :
(foldr_core f p b (reps + 1)).unfailing :=
begin
induction reps with reps hr,
{ constructor,
intros cb n,
obtain ⟨np, a, h⟩ := p.exists_done cb n,
simpa [foldr_core_eq_done, h] using (static.of_done h).symm },
{ constructor,
haveI := hr,
intros cb n,
obtain ⟨np, a, h⟩ := p.exists_done cb n,
have : n = np := static.of_done h,
subst this,
obtain ⟨np, b', hf⟩ := exists_done (foldr_core f p b (reps + 1)) cb n,
have : n = np := static.of_done hf,
subst this,
refine ⟨n, f a b', _⟩,
rw foldr_core_eq_done,
simp [h, hf, and.comm, and.left_comm, and.assoc] }
end
instance foldr_core_one_of_err_static {f : α → β → β} {b : β} [p.static] [p.err_static] :
(foldr_core f p b 1).unfailing :=
begin
constructor,
intros cb n,
cases h : p cb n,
{ simpa [foldr_core_eq_done, h] using (static.of_done h).symm },
{ simpa [foldr_core_eq_done, h] using (err_static.of_fail h).symm }
end
-- TODO: add foldr and foldl, many, etc, fix_core
lemma digit : ¬ digit.unfailing :=
of_bounded
lemma nat : ¬ nat.unfailing :=
of_bounded
end unfailing
namespace err_static
variables {α β : Type} {p q : parser α} {msgs : thunk (list string)} {msg : thunk string}
{cb : char_buffer} {n' n : ℕ} {err : dlist string} {a : α} {b : β} {sep : parser unit}
lemma not_of_ne (h : p cb n = fail n' err) (hne : n ≠ n') : ¬ err_static p :=
by { introI, exact hne (of_fail h) }
instance pure : err_static (pure a) :=
⟨λ _ _ _ _, by { simp [pure_eq_done] }⟩
instance bind {f : α → parser β} [p.static] [p.err_static] [∀ a, (f a).err_static] :
(p >>= f).err_static :=
⟨λ cb n n' err, begin
rw bind_eq_fail,
rintro (hp | ⟨_, _, hp, hf⟩),
{ exact of_fail hp },
{ exact trans (static.of_done hp) (of_fail hf) }
end⟩
instance bind_of_unfailing {f : α → parser β} [p.err_static] [∀ a, (f a).unfailing] :
(p >>= f).err_static :=
⟨λ cb n n' err, begin
rw bind_eq_fail,
rintro (hp | ⟨_, _, hp, hf⟩),
{ exact of_fail hp },
{ exact false.elim (unfailing.of_fail hf) }
end⟩
instance and_then {q : parser β} [p.static] [p.err_static] [q.err_static] : (p >> q).err_static :=
err_static.bind
instance and_then_of_unfailing {q : parser β} [p.err_static] [q.unfailing] : (p >> q).err_static :=
err_static.bind_of_unfailing
instance map [p.err_static] {f : α → β} : (f <$> p).err_static :=
⟨λ _ _ _ _, by { rw map_eq_fail, exact of_fail }⟩
instance seq {f : parser (α → β)} [f.static] [f.err_static] [p.err_static] : (f <*> p).err_static :=
err_static.bind
instance seq_of_unfailing {f : parser (α → β)} [f.err_static] [p.unfailing] :
(f <*> p).err_static :=
err_static.bind_of_unfailing
instance mmap : Π {l : list α} {f : α → parser β}
[∀ a, (f a).static] [∀ a, (f a).err_static], (l.mmap f).err_static
| [] _ _ _ := err_static.pure
| (a :: l) _ h h' := begin
convert err_static.bind,
{ exact h _ },
{ exact h' _ },
{ intro,
convert err_static.bind,
{ convert static.mmap,
exact h },
{ apply mmap,
{ exact h },
{ exact h' } },
{ exact λ _, err_static.pure } }
end
instance mmap_of_unfailing : Π {l : list α} {f : α → parser β}
[∀ a, (f a).unfailing] [∀ a, (f a).err_static], (l.mmap f).err_static
| [] _ _ _ := err_static.pure
| (a :: l) _ h h' := begin
convert err_static.bind_of_unfailing,
{ exact h' _ },
{ intro,
convert unfailing.bind,
{ convert unfailing.mmap,
exact h },
{ exact λ _, unfailing.pure } }
end
instance mmap' : Π {l : list α} {f : α → parser β}
[∀ a, (f a).static] [∀ a, (f a).err_static], (l.mmap' f).err_static
| [] _ _ _ := err_static.pure
| (a :: l) _ h h' := begin
convert err_static.and_then,
{ exact h _ },
{ exact h' _ },
{ convert mmap',
{ exact h },
{ exact h' } }
end
instance mmap'_of_unfailing : Π {l : list α} {f : α → parser β}
[∀ a, (f a).unfailing] [∀ a, (f a).err_static], (l.mmap' f).err_static
| [] _ _ _ := err_static.pure
| (a :: l) _ h h' := begin
convert err_static.and_then_of_unfailing,
{ exact h' _ },
{ convert unfailing.mmap',
exact h }
end
instance failure : @parser.err_static α failure :=
⟨λ _ _ _ _ h, (failure_eq_fail.mp h).left⟩
instance guard {p : Prop} [decidable p] : err_static (guard p) :=
⟨λ _ _ _ _ h, (guard_eq_fail.mp h).right.left⟩
instance orelse [p.err_static] [q.mono] : (p <|> q).err_static :=
⟨λ _ n n' _, begin
by_cases hn : n = n',
{ exact λ _, hn },
{ rw orelse_eq_fail_of_mono_ne hn,
{ exact of_fail },
{ apply_instance } }
end⟩
instance decorate_errors :
(@decorate_errors α msgs p).err_static :=
⟨λ _ _ _ _ h, (decorate_errors_eq_fail.mp h).left⟩
instance decorate_error : (@decorate_error α msg p).err_static :=
err_static.decorate_errors
instance any_char : err_static any_char :=
⟨λ _ _ _ _, by { rw [any_char_eq_fail, and.comm], simp }⟩
instance sat_iff {p : char → Prop} [decidable_pred p] : err_static (sat p) :=
⟨λ _ _ _ _ h, (sat_eq_fail.mp h).left⟩
instance eps : err_static eps := err_static.pure
instance ch (c : char) : err_static (ch c) :=
err_static.decorate_error
instance char_buf {cb' : char_buffer} : err_static (char_buf cb') :=
err_static.decorate_error
instance one_of {cs : list char} : err_static (one_of cs) :=
err_static.decorate_errors
instance one_of' {cs : list char} : err_static (one_of' cs) :=
err_static.and_then_of_unfailing
instance str {s : string} : err_static (str s) :=
err_static.decorate_error
instance remaining : remaining.err_static :=
⟨λ _ _ _ _, by simp [remaining_ne_fail]⟩
instance eof : eof.err_static :=
err_static.decorate_error
-- TODO: add foldr and foldl, many, etc, fix_core
lemma fix_core {F : parser α → parser α} (hF : ∀ (p : parser α), p.err_static → (F p).err_static) :
∀ (max_depth : ℕ), err_static (fix_core F max_depth)
| 0 := err_static.failure
| (max_depth + 1) := hF _ (fix_core _)
instance digit : digit.err_static :=
err_static.decorate_error
instance nat : nat.err_static :=
err_static.decorate_error
lemma fix {F : parser α → parser α} (hF : ∀ (p : parser α), p.err_static → (F p).err_static) :
err_static (fix F) :=
⟨λ cb n _ _ h,
by { haveI := fix_core hF (cb.size - n + 1), dsimp [fix] at h, exact err_static.of_fail h }⟩
end err_static
namespace step
variables {α β : Type} {p q : parser α} {msgs : thunk (list string)} {msg : thunk string}
{cb : char_buffer} {n' n : ℕ} {err : dlist string} {a : α} {b : β} {sep : parser unit}
lemma not_step_of_static_done [static p] (h : ∃ cb n n' a, p cb n = done n' a) : ¬ step p :=
begin
introI,
rcases h with ⟨cb, n, n', a, h⟩,
have hs := static.of_done h,
simpa [←hs] using of_done h
end
lemma pure (a : α) : ¬ step (pure a) :=
begin
apply not_step_of_static_done,
simp [pure_eq_done]
end
instance bind {f : α → parser β} [p.step] [∀ a, (f a).static] :
(p >>= f).step :=
⟨λ _ _ _ _, by { simp_rw bind_eq_done, rintro ⟨_, _, hp, hf⟩,
exact (static.of_done hf) ▸ (of_done hp) }⟩
instance bind' {f : α → parser β} [p.static] [∀ a, (f a).step] :
(p >>= f).step :=
⟨λ _ _ _ _, by { simp_rw bind_eq_done, rintro ⟨_, _, hp, hf⟩,
rw static.of_done hp, exact of_done hf }⟩
instance and_then {q : parser β} [p.step] [q.static] : (p >> q).step := step.bind
instance and_then' {q : parser β} [p.static] [q.step] : (p >> q).step := step.bind'
instance map [p.step] {f : α → β} : (f <$> p).step :=
⟨λ _ _ _ _, by { simp_rw map_eq_done, rintro ⟨_, hp, _⟩, exact of_done hp }⟩
instance seq {f : parser (α → β)} [f.step] [p.static] : (f <*> p).step := step.bind
instance seq' {f : parser (α → β)} [f.static] [p.step] : (f <*> p).step := step.bind'
instance mmap {f : α → parser β} [(f a).step] :
([a].mmap f).step :=
begin
convert step.bind,
{ apply_instance },
{ intro,
convert static.bind,
{ exact static.pure },
{ exact λ _, static.pure } }
end
instance mmap' {f : α → parser β} [(f a).step] :
([a].mmap' f).step :=
begin
convert step.and_then,
{ apply_instance },
{ exact static.pure }
end
instance failure : @parser.step α failure :=
⟨λ _ _ _ _, by simp⟩
lemma guard_true : ¬ step (guard true) := pure _
instance guard : step (guard false) :=
step.failure
instance orelse [p.step] [q.step] : (p <|> q).step :=
⟨λ _ _ _ _, by { simp_rw orelse_eq_done, rintro (h | ⟨h, -⟩); exact of_done h }⟩
lemma decorate_errors_iff : (@parser.decorate_errors α msgs p).step ↔ p.step :=
begin
split,
{ introI,
constructor,
intros cb n n' a h,
have : (@parser.decorate_errors α msgs p) cb n = done n' a := by simpa using h,
exact of_done this },
{ introI,
constructor,
intros _ _ _ _ h,
rw decorate_errors_eq_done at h,
exact of_done h }
end
instance decorate_errors [p.step] :
(@decorate_errors α msgs p).step :=
⟨λ _ _ _ _, by { rw decorate_errors_eq_done, exact of_done }⟩
lemma decorate_error_iff : (@parser.decorate_error α msg p).step ↔ p.step :=
decorate_errors_iff
instance decorate_error [p.step] : (@decorate_error α msg p).step :=
step.decorate_errors
instance any_char : step any_char :=
begin
constructor,
intros cb n,
simp_rw [any_char_eq_done],
rintro _ _ ⟨_, rfl, -⟩,
simp
end
instance sat {p : char → Prop} [decidable_pred p] : step (sat p) :=
begin
constructor,
intros cb n,
simp_rw [sat_eq_done],
rintro _ _ ⟨_, _, rfl, -⟩,
simp
end
lemma eps : ¬ step eps := step.pure ()
instance ch {c : char} : step (ch c) := step.decorate_error
lemma char_buf_iff {cb' : char_buffer} : (char_buf cb').step ↔ cb'.size = 1 :=
begin
have : char_buf cb' cb' 0 = done cb'.size () := by simp [char_buf_eq_done],
split,
{ introI,
simpa using of_done this },
{ intro h,
constructor,
intros cb n n' _,
rw [char_buf_eq_done, h],
rintro ⟨rfl, -⟩,
refl }
end
instance one_of {cs : list char} : (one_of cs).step :=
step.decorate_errors
instance one_of' {cs : list char} : (one_of' cs).step :=
step.and_then
lemma str_iff {s : string} : (str s).step ↔ s.length = 1 :=
by simp [str_eq_char_buf, char_buf_iff, ←string.to_list_inj, buffer.ext_iff]
lemma remaining : ¬ remaining.step :=
begin
apply not_step_of_static_done,
simp [remaining_eq_done]
end
lemma eof : ¬ eof.step :=
begin
apply not_step_of_static_done,
simp only [eof_eq_done, exists_eq_left', exists_const],
use [buffer.nil, 0],
simp
end
-- TODO: add foldr and foldl, many, etc, fix_core
lemma fix_core {F : parser α → parser α} (hF : ∀ (p : parser α), p.step → (F p).step) :
∀ (max_depth : ℕ), step (fix_core F max_depth)
| 0 := step.failure
| (max_depth + 1) := hF _ (fix_core _)
instance digit : digit.step :=
step.decorate_error
lemma fix {F : parser α → parser α} (hF : ∀ (p : parser α), p.step → (F p).step) :
step (fix F) :=
⟨λ cb n _ _ h,
by { haveI := fix_core hF (cb.size - n + 1), dsimp [fix] at h, exact of_done h }⟩
end step
section step
variables {α β : Type} {p q : parser α} {msgs : thunk (list string)} {msg : thunk string}
{cb : char_buffer} {n' n : ℕ} {err : dlist string} {a : α} {b : β} {sep : parser unit}
lemma many1_eq_done_iff_many_eq_done [p.step] [p.bounded] {x : α} {xs : list α} :
many1 p cb n = done n' (x :: xs) ↔ many p cb n = done n' (x :: xs) :=
begin
induction hx : (x :: xs) with hd tl IH generalizing x xs n n',
{ simpa using hx },
split,
{ simp only [many1_eq_done, and_imp, exists_imp_distrib],
intros np hp hm,
have : np = n + 1 := step.of_done hp,
have hn : n < cb.size := bounded.of_done hp,
subst this,
obtain ⟨k, hk⟩ : ∃ k, cb.size - n = k + 1 :=
nat.exists_eq_succ_of_ne_zero (ne_of_gt (tsub_pos_of_lt hn)),
cases k,
{ cases tl;
simpa [many_eq_done_nil, nat.sub_succ, hk, many_eq_done, hp, foldr_core_eq_done] using hm },
cases tl with hd' tl',
{ simpa [many_eq_done_nil, nat.sub_succ, hk, many_eq_done, hp, foldr_core_eq_done] using hm },
{ rw ←@IH hd' tl' at hm, swap, refl,
simp only [many1_eq_done, many, foldr] at hm,
obtain ⟨np, hp', hf⟩ := hm,
have : np = n + 1 + 1 := step.of_done hp',
subst this,
simpa [nat.sub_succ, many_eq_done, hp, hk, foldr_core_eq_done, hp'] using hf } },
{ simp only [many_eq_done, many1_eq_done, and_imp, exists_imp_distrib],
intros np hp hm,
have : np = n + 1 := step.of_done hp,
have hn : n < cb.size := bounded.of_done hp,
subst this,
obtain ⟨k, hk⟩ : ∃ k, cb.size - n = k + 1 :=
nat.exists_eq_succ_of_ne_zero (ne_of_gt (tsub_pos_of_lt hn)),
cases k,
{ cases tl;
simpa [many_eq_done_nil, nat.sub_succ, hk, many_eq_done, hp, foldr_core_eq_done] using hm },
cases tl with hd' tl',
{ simpa [many_eq_done_nil, nat.sub_succ, hk, many_eq_done, hp, foldr_core_eq_done] using hm },
{ simp [hp],
rw ←@IH hd' tl' (n + 1) n', swap, refl,
rw [hk, foldr_core_eq_done, or.comm] at hm,
obtain (hm | ⟨np, hd', tl', hp', hf, hm⟩) := hm,
{ simpa using hm },
simp only at hm,
obtain ⟨rfl, rfl⟩ := hm,
have : np = n + 1 + 1 := step.of_done hp',
subst this,
simp [nat.sub_succ, many, many1_eq_done, hp, hk, foldr_core_eq_done, hp', ←hf, foldr] } }
end
end step
namespace prog
variables {α β : Type} {p q : parser α} {msgs : thunk (list string)} {msg : thunk string}
{cb : char_buffer} {n' n : ℕ} {err : dlist string} {a : α} {b : β} {sep : parser unit}
@[priority 100] -- see Note [lower instance priority]
instance of_step [step p] : prog p :=
⟨λ _ _ _ _ h, by { rw step.of_done h, exact nat.lt_succ_self _ }⟩
lemma pure (a : α) : ¬ prog (pure a) :=
begin
introI h,
have : (pure a : parser α) buffer.nil 0 = done 0 a := by simp [pure_eq_done],
replace this : 0 < 0 := prog.of_done this,
exact (lt_irrefl _) this
end
instance bind {f : α → parser β} [p.prog] [∀ a, (f a).mono] :
(p >>= f).prog :=
⟨λ _ _ _ _, by { simp_rw bind_eq_done, rintro ⟨_, _, hp, hf⟩,
exact lt_of_lt_of_le (of_done hp) (mono.of_done hf) }⟩
instance and_then {q : parser β} [p.prog] [q.mono] : (p >> q).prog := prog.bind
instance map [p.prog] {f : α → β} : (f <$> p).prog :=
⟨λ _ _ _ _, by { simp_rw map_eq_done, rintro ⟨_, hp, _⟩, exact of_done hp }⟩
instance seq {f : parser (α → β)} [f.prog] [p.mono] : (f <*> p).prog := prog.bind
instance mmap {l : list α} {f : α → parser β} [(f a).prog] [∀ a, (f a).mono] :
((a :: l).mmap f).prog :=
begin
constructor,
simp only [and_imp, bind_eq_done, return_eq_pure, mmap, exists_imp_distrib, pure_eq_done],
rintro _ _ _ _ _ _ h _ _ hp rfl rfl,
exact lt_of_lt_of_le (of_done h) (mono.of_done hp)
end
instance mmap' {l : list α} {f : α → parser β} [(f a).prog] [∀ a, (f a).mono] :
((a :: l).mmap' f).prog :=
begin
constructor,
simp only [and_imp, bind_eq_done, mmap', exists_imp_distrib, and_then_eq_bind],
intros _ _ _ _ _ _ h hm,
exact lt_of_lt_of_le (of_done h) (mono.of_done hm)
end
instance failure : @parser.prog α failure :=
prog.of_step
lemma guard_true : ¬ prog (guard true) := pure _
instance guard : prog (guard false) :=
prog.failure
instance orelse [p.prog] [q.prog] : (p <|> q).prog :=
⟨λ _ _ _ _, by { simp_rw orelse_eq_done, rintro (h | ⟨h, -⟩); exact of_done h }⟩
lemma decorate_errors_iff : (@parser.decorate_errors α msgs p).prog ↔ p.prog :=
begin
split,
{ introI,
constructor,
intros cb n n' a h,
have : (@parser.decorate_errors α msgs p) cb n = done n' a := by simpa using h,
exact of_done this },
{ introI,
constructor,
intros _ _ _ _ h,
rw decorate_errors_eq_done at h,
exact of_done h }
end
instance decorate_errors [p.prog] :
(@decorate_errors α msgs p).prog :=
⟨λ _ _ _ _, by { rw decorate_errors_eq_done, exact of_done }⟩
lemma decorate_error_iff : (@parser.decorate_error α msg p).prog ↔ p.prog :=
decorate_errors_iff
instance decorate_error [p.prog] : (@decorate_error α msg p).prog :=
prog.decorate_errors
instance any_char : prog any_char :=
prog.of_step
instance sat {p : char → Prop} [decidable_pred p] : prog (sat p) :=
prog.of_step
lemma eps : ¬ prog eps := prog.pure ()
instance ch {c : char} : prog (ch c) :=
prog.of_step
lemma char_buf_iff {cb' : char_buffer} : (char_buf cb').prog ↔ cb' ≠ buffer.nil :=
begin
have : cb' ≠ buffer.nil ↔ cb'.to_list ≠ [] :=
not_iff_not_of_iff ⟨λ h, by simp [h], λ h, by simpa using congr_arg list.to_buffer h⟩,
rw [char_buf, this, decorate_error_iff],
cases cb'.to_list,
{ simp [pure] },
{ simp only [iff_true, ne.def, not_false_iff],
apply_instance }
end
instance one_of {cs : list char} : (one_of cs).prog :=
prog.decorate_errors
instance one_of' {cs : list char} : (one_of' cs).prog :=
prog.and_then
lemma str_iff {s : string} : (str s).prog ↔ s ≠ "" :=
by simp [str_eq_char_buf, char_buf_iff, ←string.to_list_inj, buffer.ext_iff]
lemma remaining : ¬ remaining.prog :=
begin
introI h,
have : remaining buffer.nil 0 = done 0 0 := by simp [remaining_eq_done],
replace this : 0 < 0 := prog.of_done this,
exact (lt_irrefl _) this
end
lemma eof : ¬ eof.prog :=
begin
introI h,
have : eof buffer.nil 0 = done 0 () := by simpa [remaining_eq_done],
replace this : 0 < 0 := prog.of_done this,
exact (lt_irrefl _) this
end
-- TODO: add foldr and foldl, many, etc, fix_core
instance many1 [p.mono] [p.prog] : p.many1.prog :=
begin
constructor,
rintro cb n n' (_ | ⟨hd, tl⟩),
{ simp },
{ rw many1_eq_done,
rintro ⟨np, hp, h⟩,
exact (of_done hp).trans_le (mono.of_done h) }
end
lemma fix_core {F : parser α → parser α} (hF : ∀ (p : parser α), p.prog → (F p).prog) :
∀ (max_depth : ℕ), prog (fix_core F max_depth)
| 0 := prog.failure
| (max_depth + 1) := hF _ (fix_core _)
instance digit : digit.prog :=
prog.of_step
instance nat : nat.prog :=
prog.decorate_error
lemma fix {F : parser α → parser α} (hF : ∀ (p : parser α), p.prog → (F p).prog) :
prog (fix F) :=
⟨λ cb n _ _ h,
by { haveI := fix_core hF (cb.size - n + 1), dsimp [fix] at h, exact of_done h }⟩
end prog
variables {α β : Type} {msgs : thunk (list string)} {msg : thunk string}
variables {p q : parser α} {cb : char_buffer} {n n' : ℕ} {err : dlist string}
variables {a : α} {b : β}
section many
-- TODO: generalize to p.prog instead of p.step
lemma many_sublist_of_done [p.step] [p.bounded] {l : list α}
(h : p.many cb n = done n' l) :
∀ k < n' - n, p.many cb (n + k) = done n' (l.drop k) :=
begin
induction l with hd tl hl generalizing n,
{ rw many_eq_done_nil at h,
simp [h.left] },
intros m hm,
cases m,
{ exact h },
rw [list.drop, nat.add_succ, ←nat.succ_add],
apply hl,
{ rw [←many1_eq_done_iff_many_eq_done, many1_eq_done] at h,
obtain ⟨_, hp, h⟩ := h,
convert h,
exact (step.of_done hp).symm },
{ exact nat.lt_pred_iff.mpr hm },
end
lemma many_eq_nil_of_done [p.step] [p.bounded] {l : list α}
(h : p.many cb n = done n' l) :
p.many cb n' = done n' [] :=
begin
induction l with hd tl hl generalizing n,
{ convert h,
rw many_eq_done_nil at h,
exact h.left.symm },
{ rw [←many1_eq_done_iff_many_eq_done, many1_eq_done] at h,
obtain ⟨_, -, h⟩ := h,
exact hl h }
end
lemma many_eq_nil_of_out_of_bound [p.bounded] {l : list α}
(h : p.many cb n = done n' l) (hn : cb.size < n) :
n' = n ∧ l = [] :=
begin
cases l,
{ rw many_eq_done_nil at h,
exact ⟨h.left.symm, rfl⟩ },
{ rw many_eq_done at h,
obtain ⟨np, hp, -⟩ := h,
exact absurd (bounded.of_done hp) hn.not_lt }
end
lemma many1_length_of_done [p.mono] [p.step] [p.bounded] {l : list α}
(h : many1 p cb n = done n' l) :
l.length = n' - n :=
begin
induction l with hd tl hl generalizing n n',
{ simpa using h },
{ obtain ⟨k, hk⟩ : ∃ k, n' = n + k + 1 := nat.exists_eq_add_of_lt (prog.of_done h),
subst hk,
simp only [many1_eq_done] at h,
obtain ⟨_, hp, h⟩ := h,
have := step.of_done hp,
subst this,
cases tl,
{ simp only [many_eq_done_nil, add_left_inj, exists_and_distrib_right, self_eq_add_right] at h,
rcases h with ⟨rfl, -⟩,
simp },
rw ←many1_eq_done_iff_many_eq_done at h,
specialize hl h,
simp [hl, add_comm, add_assoc, nat.sub_succ] }
end
lemma many1_bounded_of_done [p.step] [p.bounded] {l : list α}
(h : many1 p cb n = done n' l) :
n' ≤ cb.size :=
begin
induction l with hd tl hl generalizing n n',
{ simpa using h },
{ simp only [many1_eq_done] at h,
obtain ⟨np, hp, h⟩ := h,
have := step.of_done hp,
subst this,
cases tl,
{ simp only [many_eq_done_nil, exists_and_distrib_right] at h,
simpa [←h.left] using bounded.of_done hp },
{ rw ←many1_eq_done_iff_many_eq_done at h,
exact hl h } }
end
end many
section nat
/--
The `val : ℕ` produced by a successful parse of a `cb : char_buffer` is the numerical value
represented by the string of decimal digits (possibly padded with 0s on the left)
starting from the parsing position `n` and ending at position `n'`. The number
of characters parsed in is necessarily `n' - n`.
This is one of the directions of `nat_eq_done`.
-/
lemma nat_of_done {val : ℕ} (h : nat cb n = done n' val) :
val = (nat.of_digits 10 ((((cb.to_list.drop n).take (n' - n)).reverse.map
(λ c, c.to_nat - '0'.to_nat)))) :=
begin
/- The parser `parser.nat` that generates a decimal number from a string of digit characters does
several things. First it ingests in as many digits as it can with `many1 digit`. Then, it folds
over the resulting `list ℕ` using a helper function that keeps track of both the running sum an
and the magnitude so far, using a `(sum, magnitude) : (ℕ × ℕ)` pair. The final sum is extracted
using a `prod.fst`.
To prove that the value that `parser.nat` produces, after moving precisely `n' - n` steps, is
precisely what `nat.of_digits` would give, if supplied the string that is in the ingested
`char_buffer` (modulo conversion from `char` to `ℕ ), we need to induct over the length `n' - n`
of `cb : char_buffer` ingested, and prove that the parser must have terminated due to hitting
either the end of the `char_buffer` or a non-digit character.
The statement of the lemma is phrased using a combination of `list.drop` and `list.map` because
there is no currently better way to extract an "interval" from a `char_buffer`. Additionally, the
statement uses a `list.reverse` because `nat.of_digits` is little-endian.
We try to stop referring to the `cb : char_buffer` as soon as possible, so that we can instead
regard a `list char` instead, which lends itself better to proofs via induction.
-/
/- We first prove some helper lemmas about the definition of `parser.nat`. Since it is defined
in core, we have to work with how it is defined instead of changing its definition.
In its definition, the function that folds over the parsed in digits is defined internally,
as a lambda with anonymous destructor syntax, which leads to an unpleasant `nat._match_1` term
when rewriting the definition of `parser.nat` away. Since we know exactly what the function is,
we have a `rfl`-like lemma here to rewrite it back into a readable form.
-/
have natm : nat._match_1 = (λ (d : ℕ) p, ⟨p.1 + d * p.2, p.2 * 10⟩),
{ ext1, ext1 ⟨⟩, refl },
-- We also have to prove what is the `prod.snd` of the result of the fold of a `list (ℕ × ℕ)` with
-- the function above. We use this lemma later when we finish our inductive case.
have hpow : ∀ l, (list.foldr (λ (digit : ℕ) (x : ℕ × ℕ), (x.fst + digit * x.snd, x.snd * 10))
(0, 1) l).snd = 10 ^ l.length,
{ intro l,
induction l with hd tl hl,
{ simp },
{ simp [hl, pow_succ, mul_comm] } },
-- We convert the hypothesis that `parser.nat` has succeeded into an existential that there is
-- some list of digits that it has parsed in, and that those digits, when folded over by the
-- function above, give the value at hand.
simp only [nat, pure_eq_done, natm, decorate_error_eq_done, bind_eq_done] at h,
obtain ⟨n', l, hp, rfl, rfl⟩ := h,
-- We now want to stop working with the `cb : char_buffer` and parse positions `n` and `n'`,
-- and just deal with the parsed digit list `l : list ℕ`. To do so, we have to show that
-- this is precisely the list that could have been parsed in, no smaller and no greater.
induction l with lhd ltl IH generalizing n n' cb,
{ -- Base case: we parsed in no digits whatsoever. But this is impossible because `parser.many1`
-- must produce a list that is not `list.nil`, by `many1_ne_done_nil`.
simpa using hp },
-- Inductive case:
-- We must prove that the first digit parsed in `lhd : ℕ` is precisely the digit that is
-- represented by the character at position `n` in `cb : char_buffer`.
-- We will also prove the correspondence between the subsequent digits `ltl : list ℕ` and the
-- remaining characters past position `n` up to position `n'`.
cases hx : (list.drop n (buffer.to_list cb)) with chd ctl,
{ -- Are there even characters left to parse, at position `n` in the `cb : char_buffer`? In other
-- words, are we already out of bounds, and thus could not have parsed in any value
-- successfully. But this must be a contradiction because `parser.digit` is a `bounded` parser,
-- (due to its being defined via `parser.decorate_error`), which means it only succeeds
-- in-bounds, and the `many1` parser combinator retains that property.
have : cb.size ≤ n := by simpa using list.drop_eq_nil_iff_le.mp hx,
exact absurd (bounded.of_done hp) this.not_lt },
-- We prove that the first digit parsed in is precisely the digit that is represented by the
-- character at position `n`, which we now call `chd : char`.
have chdh : chd.to_nat - '0'.to_nat = lhd,
{ simp only [many1_eq_done] at hp,
-- We know that `parser.digit` succeeded, so it has moved to a possibly different position.
-- In fact, we know that this new position is `n + 1`, by the `step` property of
-- `parser.digit`.
obtain ⟨_, hp, -⟩ := hp,
have := step.of_done hp,
subst this,
-- We now unfold what it means for `parser.digit` to succeed, which means that the character
-- parsed in was "numeric" (for some definition of that property), and, more importantly,
-- that the `n`th character of `cb`, let's say `c`, when converted to a `ℕ` via
-- `char.to_nat c - '0'.to_nat`, must be equal to the resulting value, `lhd` in our case.
simp only [digit_eq_done, buffer.read_eq_nth_le_to_list, hx, buffer.length_to_list, true_and,
add_left_inj, list.length, list.nth_le, eq_self_iff_true, exists_and_distrib_left,
fin.coe_mk] at hp,
rcases hp with ⟨_, hn, rfl, _, _⟩,
-- But we already know the list corresponding to `cb : char_buffer` from position `n` and on
-- is equal to `(chd :: ctl) : list char`, so our `c` above must satisfy `c = chd`.
have hn' : n < cb.to_list.length := by simpa using hn,
rw ←list.cons_nth_le_drop_succ hn' at hx,
-- We can ignore proving any correspondence of `ctl : list char` to the other portions of the
-- `cb : char_buffer`.
simp only at hx,
simp [hx] },
-- We know that we parsed in more than one character because of the `prog` property of
-- `parser.digit`, which the `many1` parser combinator retains. In other words, we know that
-- `n < n'`, and so, the list of digits `ltl` must correspond to the list of digits that
-- `digit.many1 cb (n + 1)` would produce. We know that the shift of `1` in `n ↦ n + 1` holds
-- due to the `step` property of `parser.digit`.
-- We also get here `k : ℕ` which will indicate how many characters we parsed in past position
-- `n`. We will prove later that this must be the number of digits we produced as well in `ltl`.
obtain ⟨k, hk⟩ : ∃ k, n' = n + k + 1 := nat.exists_eq_add_of_lt (prog.of_done hp),
have hdm : ltl = [] ∨ digit.many1 cb (n + 1) = done n' ltl,
{ cases ltl,
{ simp },
{ rw many1_eq_done at hp,
obtain ⟨_, hp, hp'⟩ := hp,
simpa [step.of_done hp, many1_eq_done_iff_many_eq_done] using hp' } },
-- Now we case on the two possibilities, that there was only a single digit parsed in, and
-- `ltl = []`, or, had we started parsing at `n + 1` instead, we'd parse in the value associated
-- with `ltl`.
-- We prove that the LHS, which is a fold over a `list ℕ` is equal to the RHS, which is that
-- the `val : ℕ` that `nat.of_digits` produces when supplied a `list ℕ that has been produced
-- via mapping a `list char` using `char.to_nat`. Specifically, that `list char` are the
-- characters in the `cb : char_buffer`, from position `n` to position `n'` (excluding `n'`),
-- in reverse.
rcases hdm with rfl|hdm,
{ -- Case that `ltl = []`.
simp only [many1_eq_done, many_eq_done_nil, exists_and_distrib_right] at hp,
-- This means we must have failed parsing with `parser.digit` at some other position,
-- which we prove must be `n + 1` via the `step` property.
obtain ⟨_, hp, rfl, hp'⟩ := hp,
have := step.of_done hp,
subst this,
-- Now we rely on the simplifier, which simplfies the LHS, which is a fold over a singleton
-- list. On the RHS, `list.take (n + 1 - n)` also produces a singleton list, which, when
-- reversed, is the same list. `nat.of_digits` of a singleton list is precisely the value in
-- the list. And we already have that `chd.to_nat - '0'.to_nat = lhd`.
simp [chdh] },
-- We now have to deal with the case where we parsed in more than one digit, and thus
-- `n + 1 < n'`, which means `ctl` has one or more elements. Similarly, `ltl` has one or more
-- elements.
-- We finish ridding ourselves of references to `cb : char_buffer`, by relying on the fact that
-- our `ctl : list char` must be the appropriate portion of `cb` once enough elements have been
-- dropped and taken.
have rearr :
list.take (n + (k + 1) - (n + 1)) (list.drop (n + 1) (buffer.to_list cb)) = ctl.take k,
{ simp [←list.tail_drop, hx, nat.sub_succ, hk] },
-- We have to prove that the number of digits produced (given by `ltl`) is equal to the number
-- of characters parsed in, as given by `ctl.take k`, and that this is precisely `k`. We phrase it
-- in the statement using `min`, because lemmas about `list.length (list.take ...)` simplify to
-- a statement that uses `min`. The `list.length` term appears from the reduction of the folding
-- function, as proven above.
have ltll : min k ctl.length = ltl.length,
{ -- Here is an example of how statements about the `list.length` of `list.take` simplify.
have : (ctl.take k).length = min k ctl.length := by simp,
-- We bring back the underlying definition of `ctl` as the result of a sequence of `list.take`
-- and `list.drop`, so that lemmas about `list.length` of those can fire.
rw [←this, ←rearr, many1_length_of_done hdm],
-- Likewise, we rid ourselves of the `k` we generated earlier.
have : k = n' - n - 1,
{ simp [hk, add_assoc] },
subst this,
simp only [nat.sub_succ, add_comm, ←nat.pred_sub, buffer.length_to_list, nat.pred_one_add,
min_eq_left_iff, list.length_drop, add_tsub_cancel_left, list.length_take,
tsub_zero],
-- We now have a goal of proving an inequality dealing with `nat` subtraction and `nat.pred`,
-- both of which require special care to provide positivity hypotheses.
rw [tsub_le_tsub_iff_right, nat.pred_le_iff],
{ -- We know that `n' ≤ cb.size` because of the `bounded` property, that a parser will not
-- produce a `done` result at a position farther than the size of the underlying
-- `char_buffer`.
convert many1_bounded_of_done hp,
-- What is now left to prove is that `0 < cb.size`, which can be rephrased
-- as proving that it is nonempty.
cases hc : cb.size,
{ -- Proof by contradiction. Let's say that `cb.size = 0`. But we know that we succeeded
-- parsing in at position `n` using a `bounded` parser, so we must have that
-- `n < cb.size`.
have := bounded.of_done hp,
rw hc at this,
-- But then `n < 0`, a contradiction.
exact absurd n.zero_le this.not_le },
{ simp } },
{ -- Here, we use the same result as above, that `n < cb.size`, and relate it to
-- `n ≤ cb.size.pred`.
exact nat.le_pred_of_lt (bounded.of_done hp) } },
-- Finally, we simplify. On the LHS, we have a fold over `lhd :: ltl`, which simplifies to
-- the operation of the summing folding function on `lhd` and the fold over `ltl`. To that we can
-- apply the induction hypothesis, because we know that our parser would have succeeded had we
-- started at position `n + 1`. We replace mentions of `cb : char_buffer` with the appropriate
-- `chd :: ctl`, replace `lhd` with the appropriate statement of how it is calculated from `chd`,
-- and use the lemmas describing the length of `ltl` and how it is associated with `k`. We also
-- remove mentions of `n'` and replace with an expression using solely `n + k + 1`.
-- We use the lemma we proved above about how the folding function produces the
-- `prod.snd` value, which is `10` to the power of the length of the list provided to the fold.
-- Finally, we rely on `nat.of_digits_append` for the related statement of how digits given
-- are used in the `nat.of_digits` calculation, which also involves `10 ^ list.length ...`.
-- The `list.append` operation appears due to the `list.reverse (chd :: ctl)`.
-- We include some addition and multiplication lemmas to help the simplifier rearrange terms.
simp [IH _ hdm, hx, hk, rearr, ←chdh, ←ltll, hpow, add_assoc, nat.of_digits_append, mul_comm]
end
/--
If we know that `parser.nat` was successful, starting at position `n` and ending at position `n'`,
then it must be the case that for all `k : ℕ`, `n ≤ k`, `k < n'`, the character at the `k`th
position in `cb : char_buffer` is "numeric", that is, is between `'0'` and `'9'` inclusive.
This is a necessary part of proving one of the directions of `nat_eq_done`.
-/
lemma nat_of_done_as_digit {val : ℕ} (h : nat cb n = done n' val) :
∀ (hn : n' ≤ cb.size) k (hk : k < n'), n ≤ k →
'0' ≤ cb.read ⟨k, hk.trans_le hn⟩ ∧ cb.read ⟨k, hk.trans_le hn⟩ ≤ '9' :=
begin
-- The properties to be shown for the characters involved rely solely on the success of
-- `parser.digit` at the relevant positions, and not on the actual value `parser.nat` produced.
-- We break done the success of `parser.nat` into the `parser.digit` success and throw away
-- the resulting value given by `parser.nat`, and focus solely on the `list ℕ` generated by
-- `parser.digit.many1`.
simp only [nat, pure_eq_done, and.left_comm, decorate_error_eq_done, bind_eq_done,
exists_eq_left, exists_and_distrib_left] at h,
obtain ⟨xs, h, -⟩ := h,
-- We want to avoid having to make statements about the `cb : char_buffer` itself. Instead, we
-- induct on the `xs : list ℕ` that `parser.digit.many1` produced.
induction xs with hd tl hl generalizing n n',
{ -- Base case: `xs` is empty. But this is a contradiction because `many1` always produces a
-- nonempty list, as proven by `many1_ne_done_nil`.
simpa using h },
-- Inductive case: we prove that the `parser.digit.many1` produced a valid `(hd :: tl) : list ℕ`,
-- by showing that is the case for the character at position `n`, which gave `hd`, and use the
-- induction hypothesis on the remaining `tl`.
-- We break apart a `many1` success into a success of the underlying `parser.digit` to give `hd`
-- and a `parser.digit.many` which gives `tl`. We first deal with the `hd`.
rw many1_eq_done at h,
-- Right away, we can throw away the information about the "new" position that `parser.digit`
-- ended on because we will soon prove that it must have been `n + 1`.
obtain ⟨_, hp, h⟩ := h,
-- The main lemma here is `digit_eq_done`, which already proves the necessary conditions about
-- the character at hand. What is left to do is properly unpack the information.
simp only [digit_eq_done, and.comm, and.left_comm, digit_eq_fail, true_and, exists_eq_left,
eq_self_iff_true, exists_and_distrib_left, exists_and_distrib_left] at hp,
obtain ⟨rfl, -, hn, ge0, le9, rfl⟩ := hp,
-- Let's now consider a position `k` between `n` and `n'`, excluding `n'`.
intros hn k hk hk',
-- What if we are at `n`? What if we are past `n`? We case on the `n ≤ k`.
rcases hk'.eq_or_lt with rfl|hk',
{ -- The `n = k` case. But this is exactly what we know already, so we provide the
-- relevant hypotheses.
exact ⟨ge0, le9⟩ },
-- The `n < k` case. First, we check if there would have even been digits parsed in. So, we
-- case on `tl : list ℕ`
cases tl,
{ -- Case where `tl = []`. But that means `many` gave us a `[]` so either the character at
-- position `k` was not "numeric" or we are out of bounds. More importantly, when `many`
-- successfully produces a `[]`, it does not progress the parser head, so we have that
-- `n + 1 = n'`. This will lead to a contradiction because now we have `n < k` and `k < n + 1`.
simp only [many_eq_done_nil, exists_and_distrib_right] at h,
-- Extract out just the `n + 1 = n'`.
obtain ⟨rfl, -⟩ := h,
-- Form the contradictory hypothesis, and discharge the goal.
have : k < k := hk.trans_le (nat.succ_le_of_lt hk'),
exact absurd this (lt_irrefl _) },
{ -- Case where `tl ≠ []`. But that means that `many` produced a nonempty list as a result, so
-- `many1` would have successfully parsed at this position too. We use this statement to
-- rewrite our hypothesis into something that works with the induction hypothesis, and apply it.
rw ←many1_eq_done_iff_many_eq_done at h,
apply hl h,
-- All that is left to prove is that our `k` is at least our new "lower bound" `n + 1`, which
-- we have from our original split of the `n ≤ k`, since we are now on the `n < k` case.
exact nat.succ_le_of_lt hk' }
end
/--
If we know that `parser.nat` was successful, starting at position `n` and ending at position `n'`,
then it must be the case that for the ending position `n'`, either it is beyond the end of the
`cb : char_buffer`, or the character at that position is not "numeric", that is, between `'0'` and
`'9'` inclusive.
This is a necessary part of proving one of the directions of `nat_eq_done`.
-/
lemma nat_of_done_bounded {val : ℕ} (h : nat cb n = done n' val) :
∀ (hn : n' < cb.size), '0' ≤ cb.read ⟨n', hn⟩ → '9' < cb.read ⟨n', hn⟩ :=
begin
-- The properties to be shown for the characters involved rely solely on the success of
-- `parser.digit` at the relevant positions, and not on the actual value `parser.nat` produced.
-- We break done the success of `parser.nat` into the `parser.digit` success and throw away
-- the resulting value given by `parser.nat`, and focus solely on the `list ℕ` generated by
-- `parser.digit.many1`.
-- We deal with the case of `n'` is "out-of-bounds" right away by requiring that
-- `∀ (hn : n' < cb.size)`. Thus we only have to prove the lemma for the cases where `n'` is still
-- "in-bounds".
simp only [nat, pure_eq_done, and.left_comm, decorate_error_eq_done, bind_eq_done,
exists_eq_left, exists_and_distrib_left] at h,
obtain ⟨xs, h, -⟩ := h,
-- We want to avoid having to make statements about the `cb : char_buffer` itself. Instead, we
-- induct on the `xs : list ℕ` that `parser.digit.many1` produced.
induction xs with hd tl hl generalizing n n',
{ -- Base case: `xs` is empty. But this is a contradiction because `many1` always produces a
-- nonempty list, as proven by `many1_ne_done_nil`.
simpa using h },
-- Inductive case: at least one character has been parsed in, starting at position `n`.
-- We know that the size of `cb : char_buffer` must be at least `n + 1` because
-- `parser.digit.many1` is `bounded` (`n < cb.size`).
-- We show that either we parsed in just that one character, or we use the inductive hypothesis.
obtain ⟨k, hk⟩ : ∃ k, cb.size = n + k + 1 := nat.exists_eq_add_of_lt (bounded.of_done h),
cases tl,
{ -- Case where `tl = []`, so we parsed in only `hd`. That must mean that `parser.digit` failed
-- at `n + 1`.
simp only [many1_eq_done, many_eq_done_nil, and.left_comm, exists_and_distrib_right,
exists_eq_left] at h,
-- We throw away the success information of what happened at position `n`, and we do not need
-- the "error" value that the failure produced.
obtain ⟨-, _, h⟩ := h,
-- If `parser.digit` failed at `n + 1`, then either we hit a non-numeric character, or
-- we are out of bounds. `digit_eq_fail` provides us with those two cases.
simp only [digit_eq_done, and.comm, and.left_comm, digit_eq_fail, true_and, exists_eq_left,
eq_self_iff_true, exists_and_distrib_left] at h,
obtain (⟨rfl, h⟩ | ⟨h, -⟩) := h,
{ -- First case: we are still in bounds, but the character is not numeric. We must prove
-- that we are still in bounds. But we know that from our initial requirement.
intro hn,
simpa using h hn },
{ -- Second case: we are out of bounds, and somehow the fold that `many1` relied on failed.
-- But we know that `parser.digit` is mono, that is, it never goes backward in position,
-- in neither success nor in failure. We also have that `foldr_core` respects `mono`.
-- But in this case, `foldr_core` is starting at position `n' + 1` but failing at
-- position `n'`, which is a contradiction, because otherwise we would have `n' + 1 ≤ n'`.
simpa using mono.of_fail h } },
{ -- Case where `tl ≠ []`. But that means that `many` produced a nonempty list as a result, so
-- `many1` would have successfully parsed at this position too. We use this statement to
-- rewrite our hypothesis into something that works with the induction hypothesis, and apply it.
rw many1_eq_done at h,
obtain ⟨_, -, h⟩ := h,
rw ←many1_eq_done_iff_many_eq_done at h,
exact hl h }
end
/--
The `val : ℕ` produced by a successful parse of a `cb : char_buffer` is the numerical value
represented by the string of decimal digits (possibly padded with 0s on the left)
starting from the parsing position `n` and ending at position `n'`, where `n < n'`. The number
of characters parsed in is necessarily `n' - n`. Additionally, all of the characters in the `cb`
starting at position `n` (inclusive) up to position `n'` (exclusive) are "numeric", in that they
are between `'0'` and `'9'` inclusive. Such a `char_buffer` would produce the `ℕ` value encoded
by its decimal characters.
-/
lemma nat_eq_done {val : ℕ} : nat cb n = done n' val ↔ ∃ (hn : n < n'),
val = (nat.of_digits 10 ((((cb.to_list.drop n).take (n' - n)).reverse.map
(λ c, c.to_nat - '0'.to_nat)))) ∧ (∀ (hn' : n' < cb.size),
('0' ≤ cb.read ⟨n', hn'⟩ → '9' < cb.read ⟨n', hn'⟩)) ∧ ∃ (hn'' : n' ≤ cb.size),
(∀ k (hk : k < n'), n ≤ k →
'0' ≤ cb.read ⟨k, hk.trans_le hn''⟩ ∧ cb.read ⟨k, hk.trans_le hn''⟩ ≤ '9') :=
begin
-- To prove this iff, we have most of the way in the forward direction, using the lemmas proven
-- above. First, we must use that `parser.nat` is `prog`, which means that on success, it must
-- move forward. We also have to prove the statement that a success means the parsed in
-- characters were properly "numeric". It involves first generating ane existential witness
-- that the parse was completely "in-bounds".
-- For the reverse direction, we first discharge the goals that deal with proving that our parser
-- succeeded because it encountered characters with the proper "numeric" properties, was
-- "in-bounds" and hit a nonnumeric character. The more difficult portion is proving that the
-- list of characters from positions `n` to `n'`, when folded over by the function defined inside
-- `parser.nat` gives exactly the same value as `nat.of_digits` when supplied with the same
-- (modulo rearrangement) list. To reach this goal, we try to remove any reliance on the
-- underlying `cb : char_buffer` or parsers as soon as possible, via a cased-induction.
refine ⟨λ h, ⟨prog.of_done h, nat_of_done h, nat_of_done_bounded h, _⟩, _⟩,
{ -- To provide the existential witness that `n'` is within the bounds of the `cb : char_buffer`,
-- we rely on the fact that `parser.nat` is primarily a `parser.digit.many1`, and that `many1`,
-- must finish with the bounds of the `cb`, as long as the underlying parser is `step` and
-- `bounded`, which `digit` is. We do not prove this as a separate lemma about `parser.nat`
-- because it would almost always be only relevant in this larger theorem.
-- We clone the success hypothesis `h` so that we can supply it back later.
have H := h,
-- We unwrap the `parser.nat` success down to the `many1` success, throwing away other info.
rw [nat] at h,
simp only [decorate_error_eq_done, bind_eq_done, pure_eq_done, and.left_comm, exists_eq_left,
exists_and_distrib_left] at h,
obtain ⟨_, h, -⟩ := h,
-- Now we get our existential witness that `n' ≤ cb.size`.
replace h := many1_bounded_of_done h,
-- With that, we can use the lemma proved above that our characters are "numeric"
exact ⟨h, nat_of_done_as_digit H h⟩ },
-- We now prove that given the `cb : char_buffer` with characters within the `n ≤ k < n'` interval
-- properly "numeric" and such that their `nat.of_digits` generates the `val : ℕ`, `parser.nat`
-- of that `cb`, when starting at `n`, will finish at `n'` and produce the same `val`.
-- We first introduce the relevant hypotheses, including the fact that we have a valid interval
-- where `n < n'` and that characters at `n'` and beyond are no longer numeric.
rintro ⟨hn, hv, hb, hn', ho⟩,
-- We first unwrap the `parser.nat` definition to the underlying `parser.digit.many1` success
-- and the fold function of the digits.
rw nat,
simp only [and.left_comm, pure_eq_done, hv, decorate_error_eq_done, list.map_reverse,
bind_eq_done, exists_eq_left, exists_and_distrib_left],
-- We won't actually need the `val : ℕ` itself, since it is entirely characterized by the
-- underlying characters. Instead, we will induct over the `list char` of characters from
-- position `n` onwards, showing that if we could have provided a list at `n`, we could have
-- provided a valid list of characters at `n + 1` too.
clear hv val,
/- We first prove some helper lemmas about the definition of `parser.nat`. Since it is defined
in core, we have to work with how it is defined instead of changing its definition.
In its definition, the function that folds over the parsed in digits is defined internally,
as a lambda with anonymous destructor syntax, which leads to an unpleasant `nat._match_1` term
when rewriting the definition of `parser.nat` away. Since we know exactly what the function is,
we have a `rfl`-like lemma here to rewrite it back into a readable form.
-/
have natm : nat._match_1 = (λ (d : ℕ) p, ⟨p.1 + d * p.2, p.2 * 10⟩),
{ ext1, ext1 ⟨⟩, refl },
-- We induct over the characters available at position `n` and onwards. Because `cb` is used
-- in other expressions, we utilize the `induction H : ...` tactic to induct separately from
-- destructing `cb` itself.
induction H : (cb.to_list.drop n) with hd tl IH generalizing n,
{ -- Base case: there are no characters at position `n` or onwards, which means that
-- `cb.size ≤ n`. But this is a contradiction, since we have `n < n' ≤ cb.size`.
rw list.drop_eq_nil_iff_le at H,
refine absurd ((lt_of_le_of_lt H hn).trans_le hn') _,
simp },
{ -- Inductive case: we prove that if we could have parsed from `n + 1`, we could have also parsed
-- from `n`, if there was a valid numerical character at `n`. Most of the body
-- of this inductive case is generating the appropriate conditions for use of the inductive
-- hypothesis.
specialize @IH (n + 1),
-- We have, by the inductive case, that there is at least one character `hd` at position `n`,
-- with the rest at `tl`. We rearrange our inductive case to make `tl` be expressed as
-- list.drop (n + 1), which fits out induction hypothesis conditions better. To use the
-- rearranging lemma, we must prove that we are "dropping" in bounds, which we supply on-the-fly
simp only [←list.cons_nth_le_drop_succ
(show n < cb.to_list.length, by simpa using hn.trans_le hn')] at H,
-- We prove that parsing our `n`th character, `hd`, would have resulted in a success from
-- `parser.digit`, with the appropriate `ℕ` success value. We use this later to simplify the
-- unwrapped fold, since `hd` is our head character.
have hdigit : digit cb n = done (n + 1) (hd.to_nat - '0'.to_nat),
{ -- By our necessary condition, we know that `n` is in bounds, and that the `n`th character
-- has the necessary "numeric" properties.
specialize ho n hn (le_refl _),
-- We prove an additional result that the conversion of `hd : char` to a `ℕ` would give a
-- value `x ≤ 9`, since that is part of the iff statement in the `digit_eq_done` lemma.
have : (buffer.read cb ⟨n, hn.trans_le hn'⟩).to_nat - '0'.to_nat ≤ 9,
{ -- We rewrite the statement to be a statement about characters instead, and split the
-- inequality into the case that our hypotheses prove, and that `'0' ≤ '9'`, which
-- is true by computation, handled by `dec_trivial`.
rw [show 9 = '9'.to_nat - '0'.to_nat, from dec_trivial, tsub_le_tsub_iff_right],
{ exact ho.right },
{ dec_trivial } },
-- We rely on the simplifier, mostly powered by `digit_eq_done`, and supply all the
-- necessary conditions of bounds and identities about `hd`.
simp [digit_eq_done, this, ←H.left, buffer.nth_le_to_list, hn.trans_le hn', ho] },
-- We now case on whether we've moved to the end of our parse or not. We phrase this as
-- casing on either `n + 1 < n` or `n ≤ n + 1`. The more difficult goal comes first.
cases lt_or_ge (n + 1) n' with hn'' hn'',
{ -- Case `n + 1 < n'`. We can directly supply this to our induction hypothesis.
-- We now have to prove, for the induction hypothesis, that the characters at positions `k`,
-- `n + 1 ≤ k < n'` are "numeric". We already had this for `n ≤ k < n`, so we just rearrange
-- the hypotheses we already have.
specialize IH hn'' _ H.right,
{ intros k hk hk',
apply ho,
exact nat.le_of_succ_le hk' },
-- With the induction hypothesis conditions satisfier, we can extract out a list that
-- `parser.digit.many1` would have generated from position `n + 1`, as well as the associated
-- property of the list, that it folds into what `nat.of_digits` generates from the
-- characters in `cb : char_buffer`, now known as `hd :: tl`.
obtain ⟨l, hdl, hvl⟩ := IH,
-- Of course, the parsed in list from position `n` would be `l` prepended with the result
-- of parsing in `hd`, which is provided explicitly.
use (hd.to_nat - '0'.to_nat) :: l,
-- We case on `l : list ℕ` so that we can make statements about the fold on `l`
cases l with lhd ltl,
{ -- As before, if `l = []` then `many1` produced a `[]` success, which is a contradiction.
simpa using hdl },
-- Case `l = lhd :: ltl`. We can rewrite the fold of the function inside `parser.nat` on
-- `lhd :: ltl`, which will be used to rewrite in the goal.
simp only [natm, list.foldr] at hvl,
-- We also expand the fold in the goal, using the expanded fold from our hypothesis, powered
-- by `many1_eq_done` to proceed in the parsing. We know exactly what the next `many` will
-- produce from `many1_eq_done_iff_many_eq_done.mp` of our `hdl` hypothesis. Finally,
-- we also use `hdigit` to express what the single `parser.digit` result would be at `n`.
simp only [natm, hvl, many1_eq_done, hdigit, many1_eq_done_iff_many_eq_done.mp hdl, true_and,
and_true, eq_self_iff_true, list.foldr, exists_eq_left'],
-- Now our goal is solely about the equality of two different folding functions, one from the
-- function defined inside `parser.nat` and the other as `nat.of_digits`, when applied to
-- similar list inputs.
-- First, we rid ourselves of `n'` by replacing with `n + m + 1`, which allows us to
-- simplify the term of how many elements we are keeping using a `list.take`.
obtain ⟨m, rfl⟩ : ∃ m, n' = n + m + 1 := nat.exists_eq_add_of_lt hn,
-- The following rearrangement lemma is to simplify the `list.take (n' - n)` expression we had
have : n + m + 1 - n = m + 1,
{ rw [add_assoc, tsub_eq_iff_eq_add_of_le, add_comm],
exact nat.le_add_right _ _ },
-- We also have to prove what is the `prod.snd` of the result of the fold of a `list (ℕ × ℕ)`
-- with the function above. We use this lemma to finish our inductive case.
have hpow : ∀ l, (list.foldr (λ (digit : ℕ) (x : ℕ × ℕ),
(x.fst + digit * x.snd, x.snd * 10)) (0, 1) l).snd = 10 ^ l.length,
{ intro l,
induction l with hd tl hl,
{ simp },
{ simp [hl, pow_succ, mul_comm] } },
-- We prove that the parsed list of digits `(lhd :: ltl) : list ℕ` must be of length `m`
-- which is used later when the `parser.nat` fold places `ltl.length` in the exponent.
have hml : ltl.length + 1 = m := by simpa using many1_length_of_done hdl,
-- A simplified `list.length (list.take ...)` expression refers to the minimum of the
-- underlying length and the amount of elements taken. We know that `m ≤ tl.length`, so
-- we provide this auxiliary lemma so that the simplified "take-length" can simplify further
have ltll : min m tl.length = m,
{ -- On the way to proving this, we have to actually show that `m ≤ tl.length`, by showing
-- that since `tl` was a subsequence in `cb`, and was retrieved from `n + 1` to `n + m + 1`,
-- then since `n + m + 1 ≤ cb.size`, we have that `tl` must be at least `m` in length.
simpa [←H.right, le_tsub_iff_right (hn''.trans_le hn').le, add_comm, add_assoc,
add_left_comm] using hn' },
-- Finally, we rely on the simplifier. We already expressions of `nat.of_digits` on both
-- the LHS and RHS. All that is left to do is to prove that the summand on the LHS is produced
-- by the fold of `nat.of_digits` on the RHS of `hd :: tl`. The `nat.of_digits_append` is used
-- because of the append that forms from the included `list.reverse`. The lengths of the lists
-- are placed in the exponents with `10` as a base, and are combined using `←pow_succ 10`.
-- Any complicated expression about list lengths is further simplified by the auxiliary
-- lemmas we just proved. Finally, we assist the simplifier by rearranging terms with our
-- `n + m + 1 - n = m + 1` proof and `mul_comm`.
simp [this, hpow, nat.of_digits_append, mul_comm, ←pow_succ 10, hml, ltll] },
{ -- Consider the case that `n' ≤ n + 1`. But then since `n < n' ≤ n + 1`, `n' = n + 1`.
have : n' = n + 1 := le_antisymm hn'' (nat.succ_le_of_lt hn),
subst this,
-- This means we have only parsed in a single character, so the resulting parsed in list
-- is explicitly formed from an expression we can construct from `hd`.
use [[hd.to_nat - '0'.to_nat]],
-- Our list expression simplifies nicely because it is a fold over a singleton, so we
-- do not have to supply any auxiliary lemmas for it, other than what we already know about
-- `hd` and the function defined in `parser.nat`. However, we will have to prove that our
-- parse ended because of a good reason: either we are out of bounds or we hit a nonnumeric
-- character.
simp only [many1_eq_done, many_eq_done_nil, digit_eq_fail, natm, and.comm, and.left_comm,
hdigit, true_and, mul_one, nat.of_digits_singleton, list.take, exists_eq_left,
exists_and_distrib_right, add_tsub_cancel_left, eq_self_iff_true,
list.reverse_singleton, zero_add, list.foldr, list.map],
-- We take the route of proving that we hit a nonnumeric character, since we already have
-- a hypothesis that says that characters at `n'` and past it are nonnumeric. (Note, by now
-- we have substituted `n + 1` for `n'.
-- We are also asked to provide the error value that our failed parse would report. But
-- `digit_eq_fail` already knows what it is, so we can discharge that with an inline `rfl`.
refine ⟨_, or.inl ⟨rfl, _⟩⟩,
-- The nonnumeric condition looks almost exactly like the hypothesis we already have, so
-- we let the simplifier align them for us
simpa using hb } }
end
end nat
end parser
|
6dbdd7d00dd3d5b3a97b4192b4213d0673c64291 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/real/nnreal_auto.lean | e3fafa56ce7f9a07472d10d1f8b845b25b5596c2 | [] | 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 | 26,018 | lean | /-
Copyright (c) 2018 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.linear_ordered_comm_group_with_zero
import Mathlib.algebra.big_operators.ring
import Mathlib.data.real.basic
import Mathlib.data.indicator_function
import Mathlib.PostPort
universes u_1
namespace Mathlib
/-!
# Nonnegative real numbers
In this file we define `nnreal` (notation: `ℝ≥0`) to be the type of non-negative real numbers,
a.k.a. the interval `[0, ∞)`. We also define the following operations and structures on `ℝ≥0`:
* the order on `ℝ≥0` is the restriction of the order on `ℝ`; these relations define a conditionally
complete linear order with a bottom element, `conditionally_complete_linear_order_bot`;
* `a + b` and `a * b` are the restrictions of addition and multiplication of real numbers to `ℝ≥0`;
these operations together with `0 = ⟨0, _⟩` and `1 = ⟨1, _⟩` turn `ℝ≥0` into a linear ordered
archimedean commutative semifield; we have no typeclass for this in `mathlib` yet, so we define
the following instances instead:
- `linear_ordered_semiring ℝ≥0`;
- `comm_semiring ℝ≥0`;
- `canonically_ordered_comm_semiring ℝ≥0`;
- `linear_ordered_comm_group_with_zero ℝ≥0`;
- `archimedean ℝ≥0`.
* `nnreal.of_real x` is defined as `⟨max x 0, _⟩`, i.e. `↑(nnreal.of_real x) = x` when `0 ≤ x` and
`↑(nnreal.of_real x) = 0` otherwise.
We also define an instance `can_lift ℝ ℝ≥0`. This instance can be used by the `lift` tactic to
replace `x : ℝ` and `hx : 0 ≤ x` in the proof context with `x : ℝ≥0` while replacing all occurences
of `x` with `↑x`. This tactic also works for a function `f : α → ℝ` with a hypothesis
`hf : ∀ x, 0 ≤ f x`.
## Notations
This file defines `ℝ≥0` as a localized notation for `nnreal`.
-/
/-- Nonnegative real numbers. -/
def nnreal := Subtype fun (r : ℝ) => 0 ≤ r
namespace nnreal
protected instance real.has_coe : has_coe nnreal ℝ := has_coe.mk subtype.val
/- Simp lemma to put back `n.val` into the normal form given by the coercion. -/
@[simp] theorem val_eq_coe (n : nnreal) : subtype.val n = ↑n := rfl
protected instance can_lift : can_lift ℝ nnreal := can_lift.mk coe (fun (r : ℝ) => 0 ≤ r) sorry
protected theorem eq {n : nnreal} {m : nnreal} : ↑n = ↑m → n = m := subtype.eq
protected theorem eq_iff {n : nnreal} {m : nnreal} : ↑n = ↑m ↔ n = m :=
{ mp := nnreal.eq, mpr := congr_arg coe }
theorem ne_iff {x : nnreal} {y : nnreal} : ↑x ≠ ↑y ↔ x ≠ y := not_iff_not_of_iff nnreal.eq_iff
/-- Reinterpret a real number `r` as a non-negative real number. Returns `0` if `r < 0`. -/
protected def of_real (r : ℝ) : nnreal := { val := max r 0, property := sorry }
theorem coe_of_real (r : ℝ) (hr : 0 ≤ r) : ↑(nnreal.of_real r) = r := max_eq_left hr
theorem le_coe_of_real (r : ℝ) : r ≤ ↑(nnreal.of_real r) := le_max_left r 0
theorem coe_nonneg (r : nnreal) : 0 ≤ ↑r := subtype.property r
theorem coe_mk (a : ℝ) (ha : 0 ≤ a) : ↑{ val := a, property := ha } = a := rfl
protected instance has_zero : HasZero nnreal := { zero := { val := 0, property := sorry } }
protected instance has_one : HasOne nnreal := { one := { val := 1, property := zero_le_one } }
protected instance has_add : Add nnreal :=
{ add := fun (a b : nnreal) => { val := ↑a + ↑b, property := sorry } }
protected instance has_sub : Sub nnreal := { sub := fun (a b : nnreal) => nnreal.of_real (↑a - ↑b) }
protected instance has_mul : Mul nnreal :=
{ mul := fun (a b : nnreal) => { val := ↑a * ↑b, property := sorry } }
protected instance has_inv : has_inv nnreal :=
has_inv.mk fun (a : nnreal) => { val := subtype.val a⁻¹, property := sorry }
protected instance has_le : HasLessEq nnreal := { LessEq := fun (r s : nnreal) => ↑r ≤ ↑s }
protected instance has_bot : has_bot nnreal := has_bot.mk 0
protected instance inhabited : Inhabited nnreal := { default := 0 }
protected theorem injective_coe : function.injective coe := subtype.coe_injective
@[simp] protected theorem coe_eq {r₁ : nnreal} {r₂ : nnreal} : ↑r₁ = ↑r₂ ↔ r₁ = r₂ :=
function.injective.eq_iff nnreal.injective_coe
@[simp] protected theorem coe_zero : ↑0 = 0 := rfl
@[simp] protected theorem coe_one : ↑1 = 1 := rfl
@[simp] protected theorem coe_add (r₁ : nnreal) (r₂ : nnreal) : ↑(r₁ + r₂) = ↑r₁ + ↑r₂ := rfl
@[simp] protected theorem coe_mul (r₁ : nnreal) (r₂ : nnreal) : ↑(r₁ * r₂) = ↑r₁ * ↑r₂ := rfl
@[simp] protected theorem coe_inv (r : nnreal) : ↑(r⁻¹) = (↑r⁻¹) := rfl
@[simp] protected theorem coe_bit0 (r : nnreal) : ↑(bit0 r) = bit0 ↑r := rfl
@[simp] protected theorem coe_bit1 (r : nnreal) : ↑(bit1 r) = bit1 ↑r := rfl
@[simp] protected theorem coe_sub {r₁ : nnreal} {r₂ : nnreal} (h : r₂ ≤ r₁) :
↑(r₁ - r₂) = ↑r₁ - ↑r₂ :=
sorry
-- TODO: setup semifield!
@[simp] protected theorem coe_eq_zero (r : nnreal) : ↑r = 0 ↔ r = 0 := sorry
theorem coe_ne_zero {r : nnreal} : ↑r ≠ 0 ↔ r ≠ 0 := sorry
protected instance comm_semiring : comm_semiring nnreal :=
comm_semiring.mk Add.add sorry 0 sorry sorry sorry Mul.mul sorry 1 sorry sorry sorry sorry sorry
sorry sorry
/-- Coercion `ℝ≥0 → ℝ` as a `ring_hom`. -/
def to_real_hom : nnreal →+* ℝ :=
ring_hom.mk coe nnreal.coe_one nnreal.coe_mul nnreal.coe_zero nnreal.coe_add
@[simp] theorem coe_to_real_hom : ⇑to_real_hom = coe := rfl
protected instance comm_group_with_zero : comm_group_with_zero nnreal :=
comm_group_with_zero.mk comm_semiring.mul comm_semiring.mul_assoc comm_semiring.one
comm_semiring.one_mul comm_semiring.mul_one comm_semiring.mul_comm comm_semiring.zero
comm_semiring.zero_mul comm_semiring.mul_zero has_inv.inv
(group_with_zero.div._default comm_semiring.mul comm_semiring.mul_assoc comm_semiring.one
comm_semiring.one_mul comm_semiring.mul_one has_inv.inv)
sorry sorry sorry
@[simp] theorem coe_indicator {α : Type u_1} (s : set α) (f : α → nnreal) (a : α) :
↑(set.indicator s f a) = set.indicator s (fun (x : α) => ↑(f x)) a :=
add_monoid_hom.map_indicator (↑to_real_hom) s f a
@[simp] protected theorem coe_div (r₁ : nnreal) (r₂ : nnreal) : ↑(r₁ / r₂) = ↑r₁ / ↑r₂ := rfl
@[simp] theorem coe_pow (r : nnreal) (n : ℕ) : ↑(r ^ n) = ↑r ^ n := ring_hom.map_pow to_real_hom r n
theorem coe_list_sum (l : List nnreal) : ↑(list.sum l) = list.sum (list.map coe l) :=
ring_hom.map_list_sum to_real_hom l
theorem coe_list_prod (l : List nnreal) : ↑(list.prod l) = list.prod (list.map coe l) :=
ring_hom.map_list_prod to_real_hom l
theorem coe_multiset_sum (s : multiset nnreal) :
↑(multiset.sum s) = multiset.sum (multiset.map coe s) :=
ring_hom.map_multiset_sum to_real_hom s
theorem coe_multiset_prod (s : multiset nnreal) :
↑(multiset.prod s) = multiset.prod (multiset.map coe s) :=
ring_hom.map_multiset_prod to_real_hom s
theorem coe_sum {α : Type u_1} {s : finset α} {f : α → nnreal} :
↑(finset.sum s fun (a : α) => f a) = finset.sum s fun (a : α) => ↑(f a) :=
ring_hom.map_sum to_real_hom (fun (a : α) => f a) s
theorem of_real_sum_of_nonneg {α : Type u_1} {s : finset α} {f : α → ℝ}
(hf : ∀ (a : α), a ∈ s → 0 ≤ f a) :
nnreal.of_real (finset.sum s fun (a : α) => f a) =
finset.sum s fun (a : α) => nnreal.of_real (f a) :=
sorry
theorem coe_prod {α : Type u_1} {s : finset α} {f : α → nnreal} :
↑(finset.prod s fun (a : α) => f a) = finset.prod s fun (a : α) => ↑(f a) :=
ring_hom.map_prod to_real_hom (fun (a : α) => f a) s
theorem of_real_prod_of_nonneg {α : Type u_1} {s : finset α} {f : α → ℝ}
(hf : ∀ (a : α), a ∈ s → 0 ≤ f a) :
nnreal.of_real (finset.prod s fun (a : α) => f a) =
finset.prod s fun (a : α) => nnreal.of_real (f a) :=
sorry
theorem nsmul_coe (r : nnreal) (n : ℕ) : ↑(n •ℕ r) = n •ℕ ↑r :=
add_monoid_hom.map_nsmul (ring_hom.to_add_monoid_hom to_real_hom) r n
@[simp] protected theorem coe_nat_cast (n : ℕ) : ↑↑n = ↑n := ring_hom.map_nat_cast to_real_hom n
protected instance linear_order : linear_order nnreal := linear_order.lift coe nnreal.injective_coe
@[simp] protected theorem coe_le_coe {r₁ : nnreal} {r₂ : nnreal} : ↑r₁ ≤ ↑r₂ ↔ r₁ ≤ r₂ := iff.rfl
@[simp] protected theorem coe_lt_coe {r₁ : nnreal} {r₂ : nnreal} : ↑r₁ < ↑r₂ ↔ r₁ < r₂ := iff.rfl
@[simp] protected theorem coe_pos {r : nnreal} : 0 < ↑r ↔ 0 < r := iff.rfl
protected theorem coe_mono : monotone coe := fun (_x _x_1 : nnreal) => iff.mpr nnreal.coe_le_coe
protected theorem of_real_mono : monotone nnreal.of_real :=
fun (x y : ℝ) (h : x ≤ y) => max_le_max h (le_refl 0)
@[simp] theorem of_real_coe {r : nnreal} : nnreal.of_real ↑r = r :=
nnreal.eq (max_eq_left (subtype.property r))
@[simp] theorem mk_coe_nat (n : ℕ) : { val := ↑n, property := nat.cast_nonneg n } = ↑n :=
nnreal.eq (Eq.symm (nnreal.coe_nat_cast n))
@[simp] theorem of_real_coe_nat (n : ℕ) : nnreal.of_real ↑n = ↑n := sorry
/-- `nnreal.of_real` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/
protected def gi : galois_insertion nnreal.of_real coe :=
galois_insertion.monotone_intro nnreal.coe_mono nnreal.of_real_mono le_coe_of_real sorry
protected instance order_bot : order_bot nnreal :=
order_bot.mk ⊥ linear_order.le linear_order.lt linear_order.le_refl linear_order.le_trans
linear_order.le_antisymm sorry
protected instance canonically_ordered_add_monoid : canonically_ordered_add_monoid nnreal :=
canonically_ordered_add_monoid.mk comm_semiring.add comm_semiring.add_assoc comm_semiring.zero
comm_semiring.zero_add comm_semiring.add_zero comm_semiring.add_comm order_bot.le order_bot.lt
order_bot.le_refl order_bot.le_trans order_bot.le_antisymm sorry sorry order_bot.bot
order_bot.bot_le sorry
protected instance distrib_lattice : distrib_lattice nnreal :=
Mathlib.distrib_lattice_of_linear_order
protected instance semilattice_inf_bot : semilattice_inf_bot nnreal :=
semilattice_inf_bot.mk order_bot.bot order_bot.le order_bot.lt order_bot.le_refl
order_bot.le_trans order_bot.le_antisymm order_bot.bot_le distrib_lattice.inf
distrib_lattice.inf_le_left distrib_lattice.inf_le_right distrib_lattice.le_inf
protected instance semilattice_sup_bot : semilattice_sup_bot nnreal :=
semilattice_sup_bot.mk order_bot.bot order_bot.le order_bot.lt order_bot.le_refl
order_bot.le_trans order_bot.le_antisymm order_bot.bot_le distrib_lattice.sup
distrib_lattice.le_sup_left distrib_lattice.le_sup_right distrib_lattice.sup_le
protected instance linear_ordered_semiring : linear_ordered_semiring nnreal :=
linear_ordered_semiring.mk canonically_ordered_add_monoid.add
canonically_ordered_add_monoid.add_assoc canonically_ordered_add_monoid.zero
canonically_ordered_add_monoid.zero_add canonically_ordered_add_monoid.add_zero
canonically_ordered_add_monoid.add_comm comm_semiring.mul comm_semiring.mul_assoc
comm_semiring.one comm_semiring.one_mul comm_semiring.mul_one comm_semiring.zero_mul
comm_semiring.mul_zero comm_semiring.left_distrib comm_semiring.right_distrib sorry sorry
linear_order.le linear_order.lt linear_order.le_refl linear_order.le_trans
linear_order.le_antisymm canonically_ordered_add_monoid.add_le_add_left sorry zero_le_one sorry
sorry linear_order.le_total linear_order.decidable_le linear_order.decidable_eq
linear_order.decidable_lt sorry
protected instance linear_ordered_comm_group_with_zero :
linear_ordered_comm_group_with_zero nnreal :=
linear_ordered_comm_group_with_zero.mk linear_ordered_semiring.le linear_ordered_semiring.lt
linear_ordered_semiring.le_refl linear_ordered_semiring.le_trans
linear_ordered_semiring.le_antisymm linear_ordered_semiring.le_total
linear_ordered_semiring.decidable_le linear_ordered_semiring.decidable_eq
linear_ordered_semiring.decidable_lt linear_ordered_semiring.mul
linear_ordered_semiring.mul_assoc linear_ordered_semiring.one linear_ordered_semiring.one_mul
linear_ordered_semiring.mul_one comm_group_with_zero.mul_comm linear_ordered_semiring.zero
linear_ordered_semiring.zero_mul linear_ordered_semiring.mul_zero sorry sorry sorry
comm_group_with_zero.inv comm_group_with_zero.div linear_ordered_semiring.exists_pair_ne
comm_group_with_zero.inv_zero comm_group_with_zero.mul_inv_cancel
protected instance canonically_ordered_comm_semiring : canonically_ordered_comm_semiring nnreal :=
canonically_ordered_comm_semiring.mk canonically_ordered_add_monoid.add
canonically_ordered_add_monoid.add_assoc canonically_ordered_add_monoid.zero
canonically_ordered_add_monoid.zero_add canonically_ordered_add_monoid.add_zero
canonically_ordered_add_monoid.add_comm canonically_ordered_add_monoid.le
canonically_ordered_add_monoid.lt canonically_ordered_add_monoid.le_refl
canonically_ordered_add_monoid.le_trans canonically_ordered_add_monoid.le_antisymm
canonically_ordered_add_monoid.add_le_add_left
canonically_ordered_add_monoid.lt_of_add_lt_add_left canonically_ordered_add_monoid.bot
canonically_ordered_add_monoid.bot_le canonically_ordered_add_monoid.le_iff_exists_add
comm_semiring.mul comm_semiring.mul_assoc comm_semiring.one comm_semiring.one_mul
comm_semiring.mul_one comm_semiring.zero_mul comm_semiring.mul_zero comm_semiring.left_distrib
comm_semiring.right_distrib comm_semiring.mul_comm sorry
protected instance densely_ordered : densely_ordered nnreal :=
densely_ordered.mk fun (a b : nnreal) (h : ↑a < ↑b) => sorry
protected instance no_top_order : no_top_order nnreal := no_top_order.mk fun (a : nnreal) => sorry
theorem bdd_above_coe {s : set nnreal} : bdd_above (coe '' s) ↔ bdd_above s := sorry
theorem bdd_below_coe (s : set nnreal) : bdd_below (coe '' s) := sorry
protected instance has_Sup : has_Sup nnreal :=
has_Sup.mk fun (s : set nnreal) => { val := Sup (coe '' s), property := sorry }
protected instance has_Inf : has_Inf nnreal :=
has_Inf.mk fun (s : set nnreal) => { val := Inf (coe '' s), property := sorry }
theorem coe_Sup (s : set nnreal) : ↑(Sup s) = Sup (coe '' s) := rfl
theorem coe_Inf (s : set nnreal) : ↑(Inf s) = Inf (coe '' s) := rfl
protected instance conditionally_complete_linear_order_bot :
conditionally_complete_linear_order_bot nnreal :=
conditionally_complete_linear_order_bot.mk lattice.sup linear_ordered_semiring.le
linear_ordered_semiring.lt linear_ordered_semiring.le_refl linear_ordered_semiring.le_trans
linear_ordered_semiring.le_antisymm sorry sorry sorry lattice.inf sorry sorry sorry Sup Inf
sorry sorry sorry sorry linear_ordered_semiring.le_total
(id fun (x y : nnreal) => classical.dec (x ≤ y)) linear_ordered_semiring.decidable_eq
linear_ordered_semiring.decidable_lt order_bot.bot order_bot.bot_le sorry
protected instance archimedean : archimedean nnreal :=
archimedean.mk fun (x y : nnreal) (pos_y : 0 < y) => sorry
theorem le_of_forall_pos_le_add {a : nnreal} {b : nnreal} (h : ∀ (ε : nnreal), 0 < ε → a ≤ b + ε) :
a ≤ b :=
sorry
theorem lt_iff_exists_rat_btwn (a : nnreal) (b : nnreal) :
a < b ↔ ∃ (q : ℚ), 0 ≤ q ∧ a < nnreal.of_real ↑q ∧ nnreal.of_real ↑q < b :=
sorry
theorem bot_eq_zero : ⊥ = 0 := rfl
theorem mul_sup (a : nnreal) (b : nnreal) (c : nnreal) : a * (b ⊔ c) = a * b ⊔ a * c := sorry
theorem mul_finset_sup {α : Type u_1} {f : α → nnreal} {s : finset α} (r : nnreal) :
r * finset.sup s f = finset.sup s fun (a : α) => r * f a :=
sorry
@[simp] theorem coe_max (x : nnreal) (y : nnreal) : ↑(max x y) = max ↑x ↑y := sorry
@[simp] theorem coe_min (x : nnreal) (y : nnreal) : ↑(min x y) = min ↑x ↑y := sorry
@[simp] theorem zero_le_coe {q : nnreal} : 0 ≤ ↑q := subtype.property q
@[simp] theorem of_real_zero : nnreal.of_real 0 = 0 := sorry
@[simp] theorem of_real_one : nnreal.of_real 1 = 1 := sorry
@[simp] theorem of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := sorry
@[simp] theorem of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≤ 0 := sorry
theorem of_real_of_nonpos {r : ℝ} : r ≤ 0 → nnreal.of_real r = 0 := iff.mpr of_real_eq_zero
@[simp] theorem of_real_le_of_real_iff {r : ℝ} {p : ℝ} (hp : 0 ≤ p) :
nnreal.of_real r ≤ nnreal.of_real p ↔ r ≤ p :=
sorry
@[simp] theorem of_real_lt_of_real_iff' {r : ℝ} {p : ℝ} :
nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p :=
sorry
theorem of_real_lt_of_real_iff {r : ℝ} {p : ℝ} (h : 0 < p) :
nnreal.of_real r < nnreal.of_real p ↔ r < p :=
iff.trans of_real_lt_of_real_iff' (and_iff_left h)
theorem of_real_lt_of_real_iff_of_nonneg {r : ℝ} {p : ℝ} (hr : 0 ≤ r) :
nnreal.of_real r < nnreal.of_real p ↔ r < p :=
iff.trans of_real_lt_of_real_iff'
{ mp := and.left, mpr := fun (h : r < p) => { left := h, right := lt_of_le_of_lt hr h } }
@[simp] theorem of_real_add {r : ℝ} {p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) :
nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p :=
sorry
theorem of_real_add_of_real {r : ℝ} {p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) :
nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) :=
Eq.symm (of_real_add hr hp)
theorem of_real_le_of_real {r : ℝ} {p : ℝ} (h : r ≤ p) : nnreal.of_real r ≤ nnreal.of_real p :=
nnreal.of_real_mono h
theorem of_real_add_le {r : ℝ} {p : ℝ} :
nnreal.of_real (r + p) ≤ nnreal.of_real r + nnreal.of_real p :=
iff.mp nnreal.coe_le_coe (max_le (add_le_add (le_max_left r 0) (le_max_left p 0)) zero_le_coe)
theorem of_real_le_iff_le_coe {r : ℝ} {p : nnreal} : nnreal.of_real r ≤ p ↔ r ≤ ↑p :=
galois_insertion.gc nnreal.gi r p
theorem le_of_real_iff_coe_le {r : nnreal} {p : ℝ} (hp : 0 ≤ p) : r ≤ nnreal.of_real p ↔ ↑r ≤ p :=
eq.mpr
(id
(Eq._oldrec (Eq.refl (r ≤ nnreal.of_real p ↔ ↑r ≤ p)) (Eq.symm (propext nnreal.coe_le_coe))))
(eq.mpr (id (Eq._oldrec (Eq.refl (↑r ≤ ↑(nnreal.of_real p) ↔ ↑r ≤ p)) (coe_of_real p hp)))
(iff.refl (↑r ≤ p)))
theorem le_of_real_iff_coe_le' {r : nnreal} {p : ℝ} (hr : 0 < r) : r ≤ nnreal.of_real p ↔ ↑r ≤ p :=
sorry
theorem of_real_lt_iff_lt_coe {r : ℝ} {p : nnreal} (ha : 0 ≤ r) : nnreal.of_real r < p ↔ r < ↑p :=
eq.mpr
(id
(Eq._oldrec (Eq.refl (nnreal.of_real r < p ↔ r < ↑p)) (Eq.symm (propext nnreal.coe_lt_coe))))
(eq.mpr (id (Eq._oldrec (Eq.refl (↑(nnreal.of_real r) < ↑p ↔ r < ↑p)) (coe_of_real r ha)))
(iff.refl (r < ↑p)))
theorem lt_of_real_iff_coe_lt {r : nnreal} {p : ℝ} : r < nnreal.of_real p ↔ ↑r < p := sorry
theorem mul_eq_mul_left {a : nnreal} {b : nnreal} {c : nnreal} (h : a ≠ 0) :
a * b = a * c ↔ b = c :=
sorry
theorem of_real_mul {p : ℝ} {q : ℝ} (hp : 0 ≤ p) :
nnreal.of_real (p * q) = nnreal.of_real p * nnreal.of_real q :=
sorry
theorem sub_def {r : nnreal} {p : nnreal} : r - p = nnreal.of_real (↑r - ↑p) := rfl
theorem sub_eq_zero {r : nnreal} {p : nnreal} (h : r ≤ p) : r - p = 0 := sorry
@[simp] theorem sub_self {r : nnreal} : r - r = 0 := sub_eq_zero (le_refl r)
@[simp] theorem sub_zero {r : nnreal} : r - 0 = r := sorry
theorem sub_pos {r : nnreal} {p : nnreal} : 0 < r - p ↔ p < r :=
iff.trans of_real_pos (iff.trans sub_pos nnreal.coe_lt_coe)
protected theorem sub_lt_self {r : nnreal} {p : nnreal} : 0 < r → 0 < p → r - p < r := sorry
@[simp] theorem sub_le_iff_le_add {r : nnreal} {p : nnreal} {q : nnreal} : r - p ≤ q ↔ r ≤ q + p :=
sorry
@[simp] theorem sub_le_self {r : nnreal} {p : nnreal} : r - p ≤ r :=
iff.mpr sub_le_iff_le_add (le_add_right (le_refl r))
theorem add_sub_cancel {r : nnreal} {p : nnreal} : p + r - r = p := sorry
theorem add_sub_cancel' {r : nnreal} {p : nnreal} : r + p - r = p :=
eq.mpr (id (Eq._oldrec (Eq.refl (r + p - r = p)) (add_comm r p)))
(eq.mpr (id (Eq._oldrec (Eq.refl (p + r - r = p)) add_sub_cancel)) (Eq.refl p))
theorem sub_add_eq_max {r : nnreal} {p : nnreal} : r - p + p = max r p := sorry
theorem add_sub_eq_max {r : nnreal} {p : nnreal} : p + (r - p) = max p r :=
eq.mpr (id (Eq._oldrec (Eq.refl (p + (r - p) = max p r)) (add_comm p (r - p))))
(eq.mpr (id (Eq._oldrec (Eq.refl (r - p + p = max p r)) sub_add_eq_max))
(eq.mpr (id (Eq._oldrec (Eq.refl (max r p = max p r)) (max_comm r p))) (Eq.refl (max p r))))
@[simp] theorem sub_add_cancel_of_le {a : nnreal} {b : nnreal} (h : b ≤ a) : a - b + b = a :=
eq.mpr (id (Eq._oldrec (Eq.refl (a - b + b = a)) sub_add_eq_max))
(eq.mpr (id (Eq._oldrec (Eq.refl (max a b = a)) (max_eq_left h))) (Eq.refl a))
theorem sub_sub_cancel_of_le {r : nnreal} {p : nnreal} (h : r ≤ p) : p - (p - r) = r := sorry
theorem lt_sub_iff_add_lt {p : nnreal} {q : nnreal} {r : nnreal} : p < q - r ↔ p + r < q := sorry
theorem sub_lt_iff_lt_add {a : nnreal} {b : nnreal} {c : nnreal} (h : b ≤ a) :
a - b < c ↔ a < b + c :=
sorry
theorem sub_eq_iff_eq_add {a : nnreal} {b : nnreal} {c : nnreal} (h : b ≤ a) :
a - b = c ↔ a = c + b :=
sorry
theorem sum_div {ι : Type u_1} (s : finset ι) (f : ι → nnreal) (b : nnreal) :
(finset.sum s fun (i : ι) => f i) / b = finset.sum s fun (i : ι) => f i / b :=
sorry
@[simp] theorem inv_pos {r : nnreal} : 0 < (r⁻¹) ↔ 0 < r := sorry
theorem div_pos {r : nnreal} {p : nnreal} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := sorry
protected theorem mul_inv {r : nnreal} {p : nnreal} : r * p⁻¹ = p⁻¹ * (r⁻¹) :=
nnreal.eq (mul_inv_rev' ↑r ↑p)
theorem div_self_le (r : nnreal) : r / r ≤ 1 := sorry
@[simp] theorem inv_le {r : nnreal} {p : nnreal} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p :=
eq.mpr
(id
(Eq._oldrec (Eq.refl (r⁻¹ ≤ p ↔ 1 ≤ r * p))
(Eq.symm (propext (mul_le_mul_left (iff.mpr pos_iff_ne_zero h))))))
(eq.mpr (id (Eq._oldrec (Eq.refl (r * (r⁻¹) ≤ r * p ↔ 1 ≤ r * p)) (mul_inv_cancel h)))
(iff.refl (1 ≤ r * p)))
theorem inv_le_of_le_mul {r : nnreal} {p : nnreal} (h : 1 ≤ r * p) : r⁻¹ ≤ p := sorry
@[simp] theorem le_inv_iff_mul_le {r : nnreal} {p : nnreal} (h : p ≠ 0) : r ≤ (p⁻¹) ↔ r * p ≤ 1 :=
sorry
@[simp] theorem lt_inv_iff_mul_lt {r : nnreal} {p : nnreal} (h : p ≠ 0) : r < (p⁻¹) ↔ r * p < 1 :=
sorry
theorem mul_le_iff_le_inv {a : nnreal} {b : nnreal} {r : nnreal} (hr : r ≠ 0) :
r * a ≤ b ↔ a ≤ r⁻¹ * b :=
sorry
theorem le_div_iff_mul_le {a : nnreal} {b : nnreal} {r : nnreal} (hr : r ≠ 0) :
a ≤ b / r ↔ a * r ≤ b :=
eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ b / r ↔ a * r ≤ b)) div_eq_inv_mul))
(eq.mpr
(id
(Eq._oldrec (Eq.refl (a ≤ r⁻¹ * b ↔ a * r ≤ b)) (Eq.symm (propext (mul_le_iff_le_inv hr)))))
(eq.mpr (id (Eq._oldrec (Eq.refl (r * a ≤ b ↔ a * r ≤ b)) (mul_comm r a)))
(iff.refl (a * r ≤ b))))
theorem div_le_iff {a : nnreal} {b : nnreal} {r : nnreal} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ b * r :=
div_le_iff (iff.mpr pos_iff_ne_zero hr)
theorem lt_div_iff {a : nnreal} {b : nnreal} {r : nnreal} (hr : r ≠ 0) : a < b / r ↔ a * r < b :=
lt_iff_lt_of_le_iff_le (div_le_iff hr)
theorem mul_lt_of_lt_div {a : nnreal} {b : nnreal} {r : nnreal} (h : a < b / r) : a * r < b := sorry
theorem le_of_forall_lt_one_mul_le {x : nnreal} {y : nnreal}
(h : ∀ (a : nnreal), a < 1 → a * x ≤ y) : x ≤ y :=
sorry
theorem div_add_div_same (a : nnreal) (b : nnreal) (c : nnreal) : a / c + b / c = (a + b) / c :=
Eq.symm (right_distrib a b (c⁻¹))
theorem half_pos {a : nnreal} (h : 0 < a) : 0 < a / bit0 1 := div_pos h zero_lt_two
theorem add_halves (a : nnreal) : a / bit0 1 + a / bit0 1 = a := nnreal.eq (add_halves ↑a)
theorem half_lt_self {a : nnreal} (h : a ≠ 0) : a / bit0 1 < a :=
eq.mpr (id (Eq._oldrec (Eq.refl (a / bit0 1 < a)) (Eq.symm (propext nnreal.coe_lt_coe))))
(eq.mpr (id (Eq._oldrec (Eq.refl (↑(a / bit0 1) < ↑a)) (nnreal.coe_div a (bit0 1))))
(half_lt_self (iff.mpr bot_lt_iff_ne_bot h)))
theorem two_inv_lt_one : bit0 1⁻¹ < 1 := sorry
theorem div_lt_iff {a : nnreal} {b : nnreal} {c : nnreal} (hc : c ≠ 0) : b / c < a ↔ b < a * c :=
lt_iff_lt_of_le_iff_le (le_div_iff_mul_le hc)
theorem div_lt_one_of_lt {a : nnreal} {b : nnreal} (h : a < b) : a / b < 1 :=
eq.mpr
(id
(Eq._oldrec (Eq.refl (a / b < 1))
(propext (div_lt_iff (ne_of_gt (lt_of_le_of_lt (zero_le a) h))))))
(eq.mpr (id (Eq._oldrec (Eq.refl (a < 1 * b)) (one_mul b))) h)
theorem div_add_div (a : nnreal) {b : nnreal} (c : nnreal) {d : nnreal} (hb : b ≠ 0) (hd : d ≠ 0) :
a / b + c / d = (a * d + b * c) / (b * d) :=
sorry
theorem add_div' (a : nnreal) (b : nnreal) (c : nnreal) (hc : c ≠ 0) :
b + a / c = (b * c + a) / c :=
sorry
theorem div_add' (a : nnreal) (b : nnreal) (c : nnreal) (hc : c ≠ 0) :
a / c + b = (a + b * c) / c :=
sorry
theorem of_real_inv {x : ℝ} : nnreal.of_real (x⁻¹) = (nnreal.of_real x⁻¹) := sorry
theorem of_real_div {x : ℝ} {y : ℝ} (hx : 0 ≤ x) :
nnreal.of_real (x / y) = nnreal.of_real x / nnreal.of_real y :=
sorry
theorem of_real_div' {x : ℝ} {y : ℝ} (hy : 0 ≤ y) :
nnreal.of_real (x / y) = nnreal.of_real x / nnreal.of_real y :=
sorry
@[simp] theorem abs_eq (x : nnreal) : abs ↑x = ↑x := abs_of_nonneg (subtype.property x)
end nnreal
/-- The absolute value on `ℝ` as a map to `ℝ≥0`. -/
def real.nnabs (x : ℝ) : nnreal := { val := abs x, property := abs_nonneg x }
@[simp] theorem nnreal.coe_nnabs (x : ℝ) : ↑(real.nnabs x) = abs x := sorry
end Mathlib |
0c855989356947f212886fea043bc7dbebafbe4d | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/tactic/cache.lean | 69d850c521afe8afaac3767fcc2fe0762980b2a1 | [
"Apache-2.0"
] | permissive | waynemunro/mathlib | e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552 | 065a70810b5480d584033f7bbf8e0409480c2118 | refs/heads/master | 1,693,417,182,397 | 1,634,644,781,000 | 1,634,644,781,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,234 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import tactic.doc_commands
/-!
# Instance cache tactics
For performance reasons, Lean does not automatically update its database
of class instances during a proof. The group of tactics in this file
helps to force such updates.
-/
open lean.parser
local postfix `?`:9001 := optional
local postfix *:9001 := many
namespace tactic
/-- Reset the instance cache for the main goal. -/
meta def reset_instance_cache : tactic unit := do
unfreeze_local_instances,
freeze_local_instances
/-- Unfreeze the local instances while executing `tac` on the main goal. -/
meta def unfreezing {α} (tac : tactic α) : tactic α :=
focus1 $ unfreeze_local_instances *> tac <* all_goals freeze_local_instances
/--
Unfreeze local instances while executing `tac`,
if the passed expression is amongst the frozen instances.
-/
meta def unfreezing_hyp (h : expr) (tac : tactic unit) : tactic unit :=
do frozen ← frozen_local_instances,
if h ∈ frozen.get_or_else [] then unfreezing tac else tac
namespace interactive
open interactive interactive.types
/--
`unfreezingI { tac }` executes tac while temporarily unfreezing the instance cache.
-/
meta def unfreezingI (tac : itactic) :=
unfreezing tac
/-- Reset the instance cache. This allows any new instances
added to the context to be used in typeclass inference. -/
meta def resetI := reset_instance_cache
/-- Like `subst`, but can also substitute in instance arguments. -/
meta def substI (q : parse texpr) : tactic unit :=
unfreezingI (subst q)
/-- Like `cases`, but can also be used with instance arguments. -/
meta def casesI (p : parse cases_arg_p) (q : parse with_ident_list) : tactic unit :=
unfreezingI (cases p q)
/-- Like `intro`, but uses the introduced variable
in typeclass inference. -/
meta def introI (p : parse ident_?) : tactic unit :=
intro p >> reset_instance_cache
/-- Like `intros`, but uses the introduced variable(s)
in typeclass inference. -/
meta def introsI (p : parse ident_*) : tactic unit :=
intros p >> reset_instance_cache
/-- Used to add typeclasses to the context so that they can
be used in typeclass inference. The syntax is the same as `have`. -/
meta def haveI (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse (tk ":=" *> texpr)?) :
tactic unit :=
do h ← match h with
| none := get_unused_name "_inst"
| some a := return a
end,
«have» (some h) q₁ q₂,
match q₂ with
| none := swap >> reset_instance_cache >> swap
| some p₂ := reset_instance_cache
end
/-- Used to add typeclasses to the context so that they can
be used in typeclass inference. The syntax is the same as `let`. -/
meta def letI
(h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) :
tactic unit :=
do h ← match h with
| none := get_unused_name "_inst"
| some a := return a
end,
«let» (some h) q₁ q₂,
match q₂ with
| none := swap >> reset_instance_cache >> swap
| some p₂ := reset_instance_cache
end
/-- Like `exact`, but uses all variables in the context
for typeclass inference. -/
meta def exactI (q : parse texpr) : tactic unit :=
reset_instance_cache >> exact q
/--
For performance reasons, Lean does not automatically update its database
of class instances during a proof. The group of tactics described below
helps to force such updates. For a simple (but very artificial) example,
consider the function `default` from the core library. It has type
`Π (α : Sort u) [inhabited α], α`, so one can use `default α` only if Lean
can find a registered instance of `inhabited α`. Because the database of
such instance is not automatically updated during a proof, the following
attempt won't work (Lean will not pick up the instance from the local
context):
```lean
def my_id (α : Type) : α → α :=
begin
intro x,
have : inhabited α := ⟨x⟩,
exact default α, -- Won't work!
end
```
However, it will work, producing the identity function, if one replaces `have`
by its variant `haveI` described below.
* `resetI`: Reset the instance cache. This allows any instances
currently in the context to be used in typeclass inference.
* `unfreezingI { tac }`: Unfreeze local instances while executing the tactic `tac`.
* `introI`/`introsI`: `intro`/`intros` followed by `resetI`. Like
`intro`/`intros`, but uses the introduced variable in typeclass inference.
* `casesI`: like `cases`, but can also be used with instance arguments.
* `substI`: like `subst`, but can also substitute in type-class arguments
* `haveI`/`letI`: `have`/`let` followed by `resetI`. Used to add typeclasses
to the context so that they can be used in typeclass inference.
* `exactI`: `resetI` followed by `exact`. Like `exact`, but uses all
variables in the context for typeclass inference.
-/
add_tactic_doc
{ name := "Instance cache tactics",
category := doc_category.tactic,
decl_names := [``resetI, ``unfreezingI, ``casesI, ``substI, ``introI, ``introsI, ``haveI, ``letI,
``exactI],
tags := ["type class", "context management"] }
end interactive
end tactic
|
3f40a1de74d38d6da3946ce1348b688737357ffd | efa51dd2edbbbbd6c34bd0ce436415eb405832e7 | /20170116_POPL/super/heapex.lean | e183d13030d1564131a36fd8caa56df9d4a0b6be | [
"Apache-2.0"
] | permissive | leanprover/presentations | dd031a05bcb12c8855676c77e52ed84246bd889a | 3ce2d132d299409f1de269fa8e95afa1333d644e | refs/heads/master | 1,688,703,388,796 | 1,686,838,383,000 | 1,687,465,742,000 | 29,750,158 | 12 | 9 | Apache-2.0 | 1,540,211,670,000 | 1,422,042,683,000 | Lean | UTF-8 | Lean | false | false | 1,072 | lean | import tools.super
-- This is the same example we used for ematching. (see ???)
constant heap : Type
constant ptr : Type
constant val : Type
constant pt : ptr → val → heap
constant hunion : heap → heap → heap
constant is_def : heap → Prop
infix ` ∙ `:60 := hunion
infix `↣`:70 := pt
axiom hcomm : ∀ x y, x ∙ y = y ∙ x
axiom hassoc : ∀ x y z, (x ∙ y) ∙ z = x ∙ (y ∙ z)
-- The development is very similar, except that I simplified the hnoalias
-- assumption a bit from previously: is_def (h ⬝ y₁↣w₁ ⬝ y₂↣w₂) → y₁ ≠ y₂
axiom hnoalias : ∀ h y w₁ w₂, ¬is_def (h ∙ y ↣ w₁ ∙ y ↣ w₂)
-- (Usually superposition provers do this simplification automatically, but it's
-- not implemented in super yet.)
set_option trace.super true
lemma ex (h₁ h₂ : heap) (x₁ x₂ x₃ x₄ : ptr) (v₁ v₂ v₃ : val)
: is_def (h₁ ∙ (x₁ ↣ v₁ ∙ x₂ ↣ v₂) ∙ h₂ ∙ (x₃ ↣ v₃)) → x₁ ≠ x₃ ∧ x₁ ≠ x₂ ∧ x₂ ≠ x₃ :=
by super hnoalias with hassoc hcomm
|
4c48bb02b9ca79c304cab562421926503f54cf41 | 8cb37a089cdb4af3af9d8bf1002b417e407a8e9e | /library/init/logic.lean | b054cd1add6a2f549702570ee92f733812c81b59 | [
"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 | 39,006 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad, Floris van Doorn
-/
prelude
import init.core
universes u v w
@[simp] lemma opt_param_eq (α : Sort u) (default : α) : opt_param α default = α :=
rfl
@[inline] def id {α : Sort u} (a : α) : α := a
def flip {α : Sort u} {β : Sort v} {φ : Sort w} (f : α → β → φ) : β → α → φ :=
λ b a, f a b
/- implication -/
def implies (a b : Prop) := a → b
/-- Implication `→` is transitive. If `P → Q` and `Q → R` then `P → R`. -/
@[trans] lemma implies.trans {p q r : Prop} (h₁ : implies p q) (h₂ : implies q r) : implies p r :=
assume hp, h₂ (h₁ hp)
lemma trivial : true := ⟨⟩
/-- We can't have `a` and `¬a`, that would be absurd!-/
@[inline] def absurd {a : Prop} {b : Sort v} (h₁ : a) (h₂ : ¬a) : b :=
false.rec b (h₂ h₁)
lemma not.intro {a : Prop} (h : a → false) : ¬ a :=
h
/-- Modus tollens. If an implication is true, then so is its contrapositive. -/
lemma mt {a b : Prop} (h₁ : a → b) (h₂ : ¬b) : ¬a := assume ha : a, h₂ (h₁ ha)
/- not -/
lemma not_false : ¬false := id
def non_contradictory (a : Prop) : Prop := ¬¬a
lemma non_contradictory_intro {a : Prop} (ha : a) : ¬¬a :=
assume hna : ¬a, absurd ha hna
/- false -/
@[inline] def false.elim {C : Sort u} (h : false) : C :=
false.rec C h
/- eq -/
-- proof irrelevance is built in
lemma proof_irrel {a : Prop} (h₁ h₂ : a) : h₁ = h₂ := rfl
@[simp] lemma id.def {α : Sort u} (a : α) : id a = a := rfl
@[inline] def eq.mp {α β : Sort u} : (α = β) → α → β :=
eq.rec_on
@[inline] def eq.mpr {α β : Sort u} : (α = β) → β → α :=
λ h₁ h₂, eq.rec_on (eq.symm h₁) h₂
@[elab_as_eliminator]
lemma eq.substr {α : Sort u} {p : α → Prop} {a b : α} (h₁ : b = a) : p a → p b :=
eq.subst (eq.symm h₁)
lemma congr {α : Sort u} {β : Sort v} {f₁ f₂ : α → β} {a₁ a₂ : α} (h₁ : f₁ = f₂) (h₂ : a₁ = a₂) : f₁ a₁ = f₂ a₂ :=
eq.subst h₁ (eq.subst h₂ rfl)
lemma congr_fun {α : Sort u} {β : α → Sort v} {f g : Π x, β x} (h : f = g) (a : α) : f a = g a :=
eq.subst h (eq.refl (f a))
lemma congr_arg {α : Sort u} {β : Sort v} {a₁ a₂ : α} (f : α → β) : a₁ = a₂ → f a₁ = f a₂ :=
congr rfl
lemma trans_rel_left {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : r a b) (h₂ : b = c) : r a c :=
h₂ ▸ h₁
lemma trans_rel_right {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : a = b) (h₂ : r b c) : r a c :=
h₁.symm ▸ h₂
lemma of_eq_true {p : Prop} (h : p = true) : p :=
h.symm ▸ trivial
lemma not_of_eq_false {p : Prop} (h : p = false) : ¬p :=
assume hp, h ▸ hp
@[inline] def cast {α β : Sort u} (h : α = β) (a : α) : β :=
eq.rec a h
lemma cast_proof_irrel {α β : Sort u} (h₁ h₂ : α = β) (a : α) : cast h₁ a = cast h₂ a := rfl
lemma cast_eq {α : Sort u} (h : α = α) (a : α) : cast h a = a := rfl
/- ne -/
@[reducible] def ne {α : Sort u} (a b : α) := ¬(a = b)
notation a ≠ b := ne a b
@[simp] lemma ne.def {α : Sort u} (a b : α) : a ≠ b = ¬ (a = b) := rfl
namespace ne
variable {α : Sort u}
variables {a b : α}
lemma intro (h : a = b → false) : a ≠ b := h
lemma elim (h : a ≠ b) : a = b → false := h
lemma irrefl (h : a ≠ a) : false := h rfl
lemma symm (h : a ≠ b) : b ≠ a :=
assume (h₁ : b = a), h (h₁.symm)
end ne
lemma false_of_ne {α : Sort u} {a : α} : a ≠ a → false := ne.irrefl
section
variables {p : Prop}
lemma ne_false_of_self : p → p ≠ false :=
assume (hp : p) (heq : p = false), heq ▸ hp
lemma ne_true_of_not : ¬p → p ≠ true :=
assume (hnp : ¬p) (heq : p = true), (heq ▸ hnp) trivial
lemma true_ne_false : ¬true = false :=
ne_false_of_self trivial
end
attribute [refl] heq.refl
section
variables {α β φ : Sort u} {a a' : α} {b b' : β} {c : φ}
def heq.elim {α : Sort u} {a : α} {p : α → Sort v} {b : α} (h₁ : a == b) : p a → p b :=
eq.rec_on (eq_of_heq h₁)
lemma heq.subst {p : ∀ T : Sort u, T → Prop} : a == b → p α a → p β b :=
heq.rec_on
@[symm] lemma heq.symm (h : a == b) : b == a :=
heq.rec_on h (heq.refl a)
lemma heq_of_eq (h : a = a') : a == a' :=
eq.subst h (heq.refl a)
@[trans] lemma heq.trans (h₁ : a == b) (h₂ : b == c) : a == c :=
heq.subst h₂ h₁
@[trans] lemma heq_of_heq_of_eq (h₁ : a == b) (h₂ : b = b') : a == b' :=
heq.trans h₁ (heq_of_eq h₂)
@[trans] lemma heq_of_eq_of_heq (h₁ : a = a') (h₂ : a' == b) : a == b :=
heq.trans (heq_of_eq h₁) h₂
lemma type_eq_of_heq (h : a == b) : α = β :=
heq.rec_on h (eq.refl α)
end
lemma eq_rec_heq {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} (h : a = a') (p : φ a), (eq.rec_on h p : φ a') == p
| a _ rfl p := heq.refl p
lemma heq_of_eq_rec_left {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} {p₁ : φ a} {p₂ : φ a'} (e : a = a') (h₂ : (eq.rec_on e p₁ : φ a') = p₂), p₁ == p₂
| a _ p₁ p₂ rfl h := eq.rec_on h (heq.refl p₁)
lemma heq_of_eq_rec_right {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} {p₁ : φ a} {p₂ : φ a'} (e : a' = a) (h₂ : p₁ = eq.rec_on e p₂), p₁ == p₂
| a _ p₁ p₂ rfl h :=
have p₁ = p₂, from h,
this ▸ heq.refl p₁
lemma of_heq_true {a : Prop} (h : a == true) : a :=
of_eq_true (eq_of_heq h)
lemma eq_rec_compose : ∀ {α β φ : Sort u} (p₁ : β = φ) (p₂ : α = β) (a : α), (eq.rec_on p₁ (eq.rec_on p₂ a : β) : φ) = eq.rec_on (eq.trans p₂ p₁) a
| α _ _ rfl rfl a := rfl
lemma cast_heq : ∀ {α β : Sort u} (h : α = β) (a : α), cast h a == a
| α _ rfl a := heq.refl a
/- and -/
notation a /\ b := and a b
notation a ∧ b := and a b
variables {a b c d : Prop}
lemma and.elim (h₁ : a ∧ b) (h₂ : a → b → c) : c :=
and.rec h₂ h₁
lemma and.swap : a ∧ b → b ∧ a :=
assume ⟨ha, hb⟩, ⟨hb, ha⟩
lemma and.symm : a ∧ b → b ∧ a := and.swap
/- or -/
notation a \/ b := or a b
notation a ∨ b := or a b
namespace or
lemma elim (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → c) : c :=
or.rec h₂ h₃ h₁
end or
lemma non_contradictory_em (a : Prop) : ¬¬(a ∨ ¬a) :=
assume not_em : ¬(a ∨ ¬a),
have neg_a : ¬a, from
assume pos_a : a, absurd (or.inl pos_a) not_em,
absurd (or.inr neg_a) not_em
lemma or.swap : a ∨ b → b ∨ a := or.rec or.inr or.inl
lemma or.symm : a ∨ b → b ∨ a := or.swap
/- xor -/
def xor (a b : Prop) := (a ∧ ¬ b) ∨ (b ∧ ¬ a)
/- iff -/
/-- `iff P Q`, with notation `P ↔ Q`, is the proposition asserting that `P` and `Q` are equivalent,
that is, have the same truth value. -/
structure iff (a b : Prop) : Prop :=
intro :: (mp : a → b) (mpr : b → a)
notation a <-> b := iff a b
notation a ↔ b := iff a b
lemma iff.elim : ((a → b) → (b → a) → c) → (a ↔ b) → c := iff.rec
attribute [recursor 5] iff.elim
lemma iff.elim_left : (a ↔ b) → a → b := iff.mp
lemma iff.elim_right : (a ↔ b) → b → a := iff.mpr
lemma iff_iff_implies_and_implies (a b : Prop) : (a ↔ b) ↔ (a → b) ∧ (b → a) :=
iff.intro (λ h, and.intro h.mp h.mpr) (λ h, iff.intro h.left h.right)
@[refl]
lemma iff.refl (a : Prop) : a ↔ a :=
iff.intro (assume h, h) (assume h, h)
lemma iff.rfl {a : Prop} : a ↔ a :=
iff.refl a
@[trans]
lemma iff.trans (h₁ : a ↔ b) (h₂ : b ↔ c) : a ↔ c :=
iff.intro
(assume ha, iff.mp h₂ (iff.mp h₁ ha))
(assume hc, iff.mpr h₁ (iff.mpr h₂ hc))
@[symm]
lemma iff.symm (h : a ↔ b) : b ↔ a :=
iff.intro (iff.elim_right h) (iff.elim_left h)
lemma iff.comm : (a ↔ b) ↔ (b ↔ a) :=
iff.intro iff.symm iff.symm
lemma eq.to_iff {a b : Prop} (h : a = b) : a ↔ b :=
eq.rec_on h iff.rfl
lemma neq_of_not_iff {a b : Prop} : ¬(a ↔ b) → a ≠ b :=
λ h₁ h₂,
have a ↔ b, from eq.subst h₂ (iff.refl a),
absurd this h₁
lemma not_iff_not_of_iff (h₁ : a ↔ b) : ¬a ↔ ¬b :=
iff.intro
(assume (hna : ¬ a) (hb : b), hna (iff.elim_right h₁ hb))
(assume (hnb : ¬ b) (ha : a), hnb (iff.elim_left h₁ ha))
lemma of_iff_true (h : a ↔ true) : a :=
iff.mp (iff.symm h) trivial
lemma not_of_iff_false : (a ↔ false) → ¬a := iff.mp
lemma iff_true_intro (h : a) : a ↔ true :=
iff.intro
(λ hl, trivial)
(λ hr, h)
lemma iff_false_intro (h : ¬a) : a ↔ false :=
iff.intro h (false.rec a)
lemma not_non_contradictory_iff_absurd (a : Prop) : ¬¬¬a ↔ ¬a :=
iff.intro
(λ (hl : ¬¬¬a) (ha : a), hl (non_contradictory_intro ha))
absurd
lemma imp_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a → b) ↔ (c → d) :=
iff.intro
(λ hab hc, iff.mp h₂ (hab (iff.mpr h₁ hc)))
(λ hcd ha, iff.mpr h₂ (hcd (iff.mp h₁ ha)))
lemma imp_congr_ctx (h₁ : a ↔ c) (h₂ : c → (b ↔ d)) : (a → b) ↔ (c → d) :=
iff.intro
(λ hab hc, have ha : a, from iff.mpr h₁ hc,
have hb : b, from hab ha,
iff.mp (h₂ hc) hb)
(λ hcd ha, have hc : c, from iff.mp h₁ ha,
have hd : d, from hcd hc,
iff.mpr (h₂ hc) hd)
lemma imp_congr_right (h : a → (b ↔ c)) : (a → b) ↔ (a → c) :=
iff.intro
(assume hab ha, iff.elim_left (h ha) (hab ha))
(assume hab ha, iff.elim_right (h ha) (hab ha))
lemma not_not_intro (ha : a) : ¬¬a :=
assume hna : ¬a, hna ha
lemma not_of_not_not_not (h : ¬¬¬a) : ¬a :=
λ ha, absurd (not_not_intro ha) h
@[simp] lemma not_true : (¬ true) ↔ false :=
iff_false_intro (not_not_intro trivial)
@[simp] lemma not_false_iff : (¬ false) ↔ true :=
iff_true_intro not_false
@[congr] lemma not_congr (h : a ↔ b) : ¬a ↔ ¬b :=
iff.intro (λ h₁ h₂, h₁ (iff.mpr h h₂)) (λ h₁ h₂, h₁ (iff.mp h h₂))
lemma ne_self_iff_false {α : Sort u} (a : α) : (not (a = a)) ↔ false :=
iff.intro false_of_ne false.elim
@[simp] lemma eq_self_iff_true {α : Sort u} (a : α) : (a = a) ↔ true :=
iff_true_intro rfl
lemma heq_self_iff_true {α : Sort u} (a : α) : (a == a) ↔ true :=
iff_true_intro (heq.refl a)
@[simp] lemma iff_not_self (a : Prop) : (a ↔ ¬a) ↔ false :=
iff_false_intro (λ h,
have h' : ¬a, from (λ ha, (iff.mp h ha) ha),
h' (iff.mpr h h'))
@[simp] lemma not_iff_self (a : Prop) : (¬a ↔ a) ↔ false :=
iff_false_intro (λ h,
have h' : ¬a, from (λ ha, (iff.mpr h ha) ha),
h' (iff.mp h h'))
lemma true_iff_false : (true ↔ false) ↔ false :=
iff_false_intro (λ h, iff.mp h trivial)
lemma false_iff_true : (false ↔ true) ↔ false :=
iff_false_intro (λ h, iff.mpr h trivial)
lemma false_of_true_iff_false : (true ↔ false) → false :=
assume h, iff.mp h trivial
lemma false_of_true_eq_false : (true = false) → false :=
assume h, h ▸ trivial
lemma true_eq_false_of_false : false → (true = false) :=
false.elim
lemma eq_comm {α : Sort u} {a b : α} : a = b ↔ b = a :=
⟨eq.symm, eq.symm⟩
/- and simp rules -/
lemma and.imp (hac : a → c) (hbd : b → d) : a ∧ b → c ∧ d :=
assume ⟨ha, hb⟩, ⟨hac ha, hbd hb⟩
lemma and_implies (hac : a → c) (hbd : b → d) : a ∧ b → c ∧ d := and.imp hac hbd
@[congr] lemma and_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∧ b) ↔ (c ∧ d) :=
iff.intro (and.imp (iff.mp h₁) (iff.mp h₂)) (and.imp (iff.mpr h₁) (iff.mpr h₂))
lemma and_congr_right (h : a → (b ↔ c)) : (a ∧ b) ↔ (a ∧ c) :=
iff.intro
(assume ⟨ha, hb⟩, ⟨ha, iff.elim_left (h ha) hb⟩)
(assume ⟨ha, hc⟩, ⟨ha, iff.elim_right (h ha) hc⟩)
lemma and.comm : a ∧ b ↔ b ∧ a :=
iff.intro and.swap and.swap
lemma and_comm (a b : Prop) : a ∧ b ↔ b ∧ a := and.comm
lemma and.assoc : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) :=
iff.intro
(assume ⟨⟨ha, hb⟩, hc⟩, ⟨ha, ⟨hb, hc⟩⟩)
(assume ⟨ha, ⟨hb, hc⟩⟩, ⟨⟨ha, hb⟩, hc⟩)
lemma and_assoc (a b : Prop) : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) := and.assoc
lemma and.left_comm : a ∧ (b ∧ c) ↔ b ∧ (a ∧ c) :=
iff.trans (iff.symm and.assoc) (iff.trans (and_congr and.comm (iff.refl c)) and.assoc)
lemma and_iff_left {a b : Prop} (hb : b) : (a ∧ b) ↔ a :=
iff.intro and.left (λ ha, ⟨ha, hb⟩)
lemma and_iff_right {a b : Prop} (ha : a) : (a ∧ b) ↔ b :=
iff.intro and.right (and.intro ha)
@[simp] lemma and_true (a : Prop) : a ∧ true ↔ a :=
and_iff_left trivial
@[simp] lemma true_and (a : Prop) : true ∧ a ↔ a :=
and_iff_right trivial
@[simp] lemma and_false (a : Prop) : a ∧ false ↔ false :=
iff_false_intro and.right
@[simp] lemma false_and (a : Prop) : false ∧ a ↔ false :=
iff_false_intro and.left
@[simp] lemma not_and_self (a : Prop) : (¬a ∧ a) ↔ false :=
iff_false_intro (λ h, and.elim h (λ h₁ h₂, absurd h₂ h₁))
@[simp] lemma and_not_self (a : Prop) : (a ∧ ¬a) ↔ false :=
iff_false_intro (assume ⟨h₁, h₂⟩, absurd h₁ h₂)
@[simp] lemma and_self (a : Prop) : a ∧ a ↔ a :=
iff.intro and.left (assume h, ⟨h, h⟩)
/- or simp rules -/
lemma or.imp (h₂ : a → c) (h₃ : b → d) : a ∨ b → c ∨ d :=
or.rec (λ h, or.inl (h₂ h)) (λ h, or.inr (h₃ h))
lemma or.imp_left (h : a → b) : a ∨ c → b ∨ c :=
or.imp h id
lemma or.imp_right (h : a → b) : c ∨ a → c ∨ b :=
or.imp id h
@[congr] lemma or_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∨ b) ↔ (c ∨ d) :=
iff.intro (or.imp (iff.mp h₁) (iff.mp h₂)) (or.imp (iff.mpr h₁) (iff.mpr h₂))
lemma or.comm : a ∨ b ↔ b ∨ a := iff.intro or.swap or.swap
lemma or_comm (a b : Prop) : a ∨ b ↔ b ∨ a := or.comm
lemma or.assoc : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) :=
iff.intro
(or.rec (or.imp_right or.inl) (λ h, or.inr (or.inr h)))
(or.rec (λ h, or.inl (or.inl h)) (or.imp_left or.inr))
lemma or_assoc (a b : Prop) : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) :=
or.assoc
lemma or.left_comm : a ∨ (b ∨ c) ↔ b ∨ (a ∨ c) :=
iff.trans (iff.symm or.assoc) (iff.trans (or_congr or.comm (iff.refl c)) or.assoc)
theorem or_iff_right_of_imp (ha : a → b) : (a ∨ b) ↔ b :=
iff.intro (or.rec ha id) or.inr
theorem or_iff_left_of_imp (hb : b → a) : (a ∨ b) ↔ a :=
iff.intro (or.rec id hb) or.inl
@[simp] lemma or_true (a : Prop) : a ∨ true ↔ true :=
iff_true_intro (or.inr trivial)
@[simp] lemma true_or (a : Prop) : true ∨ a ↔ true :=
iff_true_intro (or.inl trivial)
@[simp] lemma or_false (a : Prop) : a ∨ false ↔ a :=
iff.intro (or.rec id false.elim) or.inl
@[simp] lemma false_or (a : Prop) : false ∨ a ↔ a :=
iff.trans or.comm (or_false a)
@[simp] lemma or_self (a : Prop) : a ∨ a ↔ a :=
iff.intro (or.rec id id) or.inl
lemma not_or {a b : Prop} : ¬ a → ¬ b → ¬ (a ∨ b)
| hna hnb (or.inl ha) := absurd ha hna
| hna hnb (or.inr hb) := absurd hb hnb
/- or resolution rulse -/
lemma or.resolve_left {a b : Prop} (h : a ∨ b) (na : ¬ a) : b :=
or.elim h (λ ha, absurd ha na) id
lemma or.neg_resolve_left {a b : Prop} (h : ¬ a ∨ b) (ha : a) : b :=
or.elim h (λ na, absurd ha na) id
lemma or.resolve_right {a b : Prop} (h : a ∨ b) (nb : ¬ b) : a :=
or.elim h id (λ hb, absurd hb nb)
lemma or.neg_resolve_right {a b : Prop} (h : a ∨ ¬ b) (hb : b) : a :=
or.elim h id (λ nb, absurd hb nb)
/- iff simp rules -/
@[simp] lemma iff_true (a : Prop) : (a ↔ true) ↔ a :=
iff.intro (assume h, iff.mpr h trivial) iff_true_intro
@[simp] lemma true_iff (a : Prop) : (true ↔ a) ↔ a :=
iff.trans iff.comm (iff_true a)
@[simp] lemma iff_false (a : Prop) : (a ↔ false) ↔ ¬ a :=
iff.intro iff.mp iff_false_intro
@[simp] lemma false_iff (a : Prop) : (false ↔ a) ↔ ¬ a :=
iff.trans iff.comm (iff_false a)
@[simp] lemma iff_self (a : Prop) : (a ↔ a) ↔ true :=
iff_true_intro iff.rfl
@[congr] lemma iff_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ↔ b) ↔ (c ↔ d) :=
(iff_iff_implies_and_implies a b).trans
((and_congr (imp_congr h₁ h₂) (imp_congr h₂ h₁)).trans
(iff_iff_implies_and_implies c d).symm)
/- implies simp rule -/
@[simp] lemma implies_true_iff (α : Sort u) : (α → true) ↔ true :=
iff.intro (λ h, trivial) (λ ha h, trivial)
lemma false_implies_iff (a : Prop) : (false → a) ↔ true :=
iff.intro (λ h, trivial) (λ ha h, false.elim h)
theorem true_implies_iff (α : Prop) : (true → α) ↔ α :=
iff.intro (λ h, h trivial) (λ h h', h)
/--
The existential quantifier.
To prove a goal of the form `⊢ ∃ x, p x`, you can provide a witness `y` with the tactic `existsi y`.
If you are working in a project that depends on mathlib, then we recommend the `use` tactic
instead.
You'll then be left with the goal `⊢ p y`.
To extract a witness `x` and proof `hx : p x` from a hypothesis `h : ∃ x, p x`,
use the tactic `cases h with x hx`. See also the mathlib tactics `obtain` and `rcases`.
-/
inductive Exists {α : Sort u} (p : α → Prop) : Prop
| intro (w : α) (h : p w) : Exists
attribute [intro] Exists.intro
notation `exists` binders `, ` r:(scoped P, Exists P) := r
notation `∃` binders `, ` r:(scoped P, Exists P) := r
@[pattern]
lemma exists.intro {α : Sort u} {p : α → Prop} (w : α) (h : p w) : ∃ x, p x := ⟨w, h⟩
lemma exists.elim {α : Sort u} {p : α → Prop} {b : Prop}
(h₁ : ∃ x, p x) (h₂ : ∀ (a : α), p a → b) : b :=
Exists.rec h₂ h₁
/- exists unique -/
def exists_unique {α : Sort u} (p : α → Prop) :=
∃ x, p x ∧ ∀ y, p y → y = x
notation `∃!` binders `, ` r:(scoped P, exists_unique P) := r
@[intro]
lemma exists_unique.intro {α : Sort u} {p : α → Prop} (w : α) (h₁ : p w) (h₂ : ∀ y, p y → y = w) :
∃! x, p x :=
exists.intro w ⟨h₁, h₂⟩
attribute [recursor 4]
lemma exists_unique.elim {α : Sort u} {p : α → Prop} {b : Prop}
(h₂ : ∃! x, p x) (h₁ : ∀ x, p x → (∀ y, p y → y = x) → b) : b :=
exists.elim h₂ (λ w hw, h₁ w (and.left hw) (and.right hw))
lemma exists_unique_of_exists_of_unique {α : Sort u} {p : α → Prop}
(hex : ∃ x, p x) (hunique : ∀ y₁ y₂, p y₁ → p y₂ → y₁ = y₂) : ∃! x, p x :=
exists.elim hex (λ x px, exists_unique.intro x px (assume y, assume : p y, hunique y x this px))
lemma exists_of_exists_unique {α : Sort u} {p : α → Prop} (h : ∃! x, p x) : ∃ x, p x :=
exists.elim h (λ x hx, ⟨x, and.left hx⟩)
lemma unique_of_exists_unique {α : Sort u} {p : α → Prop}
(h : ∃! x, p x) {y₁ y₂ : α} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ :=
exists_unique.elim h
(assume x, assume : p x,
assume unique : ∀ y, p y → y = x,
show y₁ = y₂, from eq.trans (unique _ py₁) (eq.symm (unique _ py₂)))
/- exists, forall, exists unique congruences -/
@[congr] lemma forall_congr {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a ↔ q a)) : (∀ a, p a) ↔ ∀ a, q a :=
iff.intro (λ p a, iff.mp (h a) (p a)) (λ q a, iff.mpr (h a) (q a))
lemma exists_imp_exists {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a → q a)) (p : ∃ a, p a) : ∃ a, q a :=
exists.elim p (λ a hp, ⟨a, h a hp⟩)
@[congr] lemma exists_congr {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a ↔ q a)) : (Exists p) ↔ ∃ a, q a :=
iff.intro
(exists_imp_exists (λ a, iff.mp (h a)))
(exists_imp_exists (λ a, iff.mpr (h a)))
@[congr] lemma exists_unique_congr {α : Sort u} {p₁ p₂ : α → Prop} (h : ∀ x, p₁ x ↔ p₂ x) : (exists_unique p₁) ↔ (∃! x, p₂ x) := --
exists_congr (λ x, and_congr (h x) (forall_congr (λ y, imp_congr (h y) iff.rfl)))
lemma forall_not_of_not_exists {α : Sort u} {p : α → Prop} : ¬(∃ x, p x) → (∀ x, ¬p x) :=
λ hne x hp, hne ⟨x, hp⟩
/- decidable -/
def decidable.to_bool (p : Prop) [h : decidable p] : bool :=
decidable.cases_on h (λ h₁, bool.ff) (λ h₂, bool.tt)
export decidable (is_true is_false to_bool)
@[simp] lemma to_bool_true_eq_tt (h : decidable true) : @to_bool true h = tt :=
decidable.cases_on h (λ h, false.elim (iff.mp not_true h)) (λ _, rfl)
@[simp] lemma to_bool_false_eq_ff (h : decidable false) : @to_bool false h = ff :=
decidable.cases_on h (λ h, rfl) (λ h, false.elim h)
instance decidable.true : decidable true :=
is_true trivial
instance decidable.false : decidable false :=
is_false not_false
-- We use "dependent" if-then-else to be able to communicate the if-then-else condition
-- to the branches
@[inline] def dite {α : Sort u} (c : Prop) [h : decidable c] : (c → α) → (¬ c → α) → α :=
λ t e, decidable.rec_on h e t
/- if-then-else -/
@[inline] def ite {α : Sort u} (c : Prop) [h : decidable c] (t e : α) : α :=
decidable.rec_on h (λ hnc, e) (λ hc, t)
namespace decidable
variables {p q : Prop}
def rec_on_true [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} (h₃ : p) (h₄ : h₁ h₃)
: decidable.rec_on h h₂ h₁ :=
decidable.rec_on h (λ h, false.rec _ (h h₃)) (λ h, h₄)
def rec_on_false [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} (h₃ : ¬p) (h₄ : h₂ h₃)
: decidable.rec_on h h₂ h₁ :=
decidable.rec_on h (λ h, h₄) (λ h, false.rec _ (h₃ h))
def by_cases {q : Sort u} [φ : decidable p] : (p → q) → (¬p → q) → q := dite _
/-- Law of Excluded Middle. -/
lemma em (p : Prop) [decidable p] : p ∨ ¬p := by_cases or.inl or.inr
lemma by_contradiction [decidable p] (h : ¬p → false) : p :=
if h₁ : p then h₁ else false.rec _ (h h₁)
lemma of_not_not [decidable p] : ¬ ¬ p → p :=
λ hnn, by_contradiction (λ hn, absurd hn hnn)
lemma not_not_iff (p) [decidable p] : (¬ ¬ p) ↔ p :=
iff.intro of_not_not not_not_intro
lemma not_and_iff_or_not (p q : Prop) [d₁ : decidable p] [d₂ : decidable q] : ¬ (p ∧ q) ↔ ¬ p ∨ ¬ q :=
iff.intro
(λ h, match d₁ with
| is_true h₁ :=
match d₂ with
| is_true h₂ := absurd (and.intro h₁ h₂) h
| is_false h₂ := or.inr h₂
end
| is_false h₁ := or.inl h₁
end)
(λ h ⟨hp, hq⟩, or.elim h (λ h, h hp) (λ h, h hq))
lemma not_or_iff_and_not (p q) [d₁ : decidable p] [d₂ : decidable q] : ¬ (p ∨ q) ↔ ¬ p ∧ ¬ q :=
iff.intro
(λ h, match d₁ with
| is_true h₁ := false.elim $ h (or.inl h₁)
| is_false h₁ :=
match d₂ with
| is_true h₂ := false.elim $ h (or.inr h₂)
| is_false h₂ := ⟨h₁, h₂⟩
end
end)
(λ ⟨np, nq⟩ h, or.elim h np nq)
end decidable
section
variables {p q : Prop}
def decidable_of_decidable_of_iff (hp : decidable p) (h : p ↔ q) : decidable q :=
if hp : p then is_true (iff.mp h hp)
else is_false (iff.mp (not_iff_not_of_iff h) hp)
def decidable_of_decidable_of_eq (hp : decidable p) (h : p = q) : decidable q :=
decidable_of_decidable_of_iff hp h.to_iff
protected def or.by_cases [decidable p] [decidable q] {α : Sort u}
(h : p ∨ q) (h₁ : p → α) (h₂ : q → α) : α :=
if hp : p then h₁ hp else
if hq : q then h₂ hq else
false.rec _ (or.elim h hp hq)
end
section
variables {p q : Prop}
instance [decidable p] [decidable q] : decidable (p ∧ q) :=
if hp : p then
if hq : q then is_true ⟨hp, hq⟩
else is_false (assume h : p ∧ q, hq (and.right h))
else is_false (assume h : p ∧ q, hp (and.left h))
instance [decidable p] [decidable q] : decidable (p ∨ q) :=
if hp : p then is_true (or.inl hp) else
if hq : q then is_true (or.inr hq) else
is_false (or.rec hp hq)
instance [decidable p] : decidable (¬p) :=
if hp : p then is_false (absurd hp) else is_true hp
instance implies.decidable [decidable p] [decidable q] : decidable (p → q) :=
if hp : p then
if hq : q then is_true (assume h, hq)
else is_false (assume h : p → q, absurd (h hp) hq)
else is_true (assume h, absurd h hp)
instance [decidable p] [decidable q] : decidable (p ↔ q) :=
if hp : p then
if hq : q then is_true ⟨λ_, hq, λ_, hp⟩
else is_false $ λh, hq (h.1 hp)
else
if hq : q then is_false $ λh, hp (h.2 hq)
else is_true $ ⟨λh, absurd h hp, λh, absurd h hq⟩
instance [decidable p] [decidable q] : decidable (xor p q) :=
if hp : p then
if hq : q then is_false (or.rec (λ ⟨_, h⟩, h hq : ¬(p ∧ ¬ q)) (λ ⟨_, h⟩, h hp : ¬(q ∧ ¬ p)))
else is_true $ or.inl ⟨hp, hq⟩
else
if hq : q then is_true $ or.inr ⟨hq, hp⟩
else is_false (or.rec (λ ⟨h, _⟩, hp h : ¬(p ∧ ¬ q)) (λ ⟨h, _⟩, hq h : ¬(q ∧ ¬ p)))
instance exists_prop_decidable {p} (P : p → Prop)
[Dp : decidable p] [DP : ∀ h, decidable (P h)] : decidable (∃ h, P h) :=
if h : p then decidable_of_decidable_of_iff (DP h)
⟨λ h2, ⟨h, h2⟩, λ⟨h', h2⟩, h2⟩ else is_false (mt (λ⟨h, _⟩, h) h)
instance forall_prop_decidable {p} (P : p → Prop)
[Dp : decidable p] [DP : ∀ h, decidable (P h)] : decidable (∀ h, P h) :=
if h : p then decidable_of_decidable_of_iff (DP h)
⟨λ h2 _, h2, λal, al h⟩ else is_true (λ h2, absurd h2 h)
end
instance {α : Sort u} [decidable_eq α] (a b : α) : decidable (a ≠ b) :=
implies.decidable
lemma bool.ff_ne_tt : ff = tt → false
.
def is_dec_eq {α : Sort u} (p : α → α → bool) : Prop := ∀ ⦃x y : α⦄, p x y = tt → x = y
def is_dec_refl {α : Sort u} (p : α → α → bool) : Prop := ∀ x, p x x = tt
open decidable
instance : decidable_eq bool
| ff ff := is_true rfl
| ff tt := is_false bool.ff_ne_tt
| tt ff := is_false (ne.symm bool.ff_ne_tt)
| tt tt := is_true rfl
def decidable_eq_of_bool_pred {α : Sort u} {p : α → α → bool} (h₁ : is_dec_eq p) (h₂ : is_dec_refl p) : decidable_eq α :=
assume x y : α,
if hp : p x y = tt then is_true (h₁ hp)
else is_false (assume hxy : x = y, absurd (h₂ y) (@eq.rec_on _ _ (λ z, ¬p z y = tt) _ hxy hp))
lemma decidable_eq_inl_refl {α : Sort u} [h : decidable_eq α] (a : α) : h a a = is_true (eq.refl a) :=
match (h a a) with
| (is_true e) := rfl
| (is_false n) := absurd rfl n
end
lemma decidable_eq_inr_neg {α : Sort u} [h : decidable_eq α] {a b : α} : Π n : a ≠ b, h a b = is_false n :=
assume n,
match (h a b) with
| (is_true e) := absurd e n
| (is_false n₁) := proof_irrel n n₁ ▸ eq.refl (is_false n)
end
/- inhabited -/
class inhabited (α : Sort u) :=
(default [] : α)
export inhabited (default)
@[inline, irreducible] def arbitrary (α : Sort u) [inhabited α] : α :=
default α
instance prop.inhabited : inhabited Prop :=
⟨true⟩
instance pi.inhabited (α : Sort u) {β : α → Sort v} [Π x, inhabited (β x)] : inhabited (Π x, β x) :=
⟨λ a, default (β a)⟩
instance : inhabited bool := ⟨ff⟩
instance : inhabited true := ⟨trivial⟩
class inductive nonempty (α : Sort u) : Prop
| intro (val : α) : nonempty
protected lemma nonempty.elim {α : Sort u} {p : Prop} (h₁ : nonempty α) (h₂ : α → p) : p :=
nonempty.rec h₂ h₁
@[priority 100]
instance nonempty_of_inhabited {α : Sort u} [inhabited α] : nonempty α :=
⟨default α⟩
lemma nonempty_of_exists {α : Sort u} {p : α → Prop} : (∃ x, p x) → nonempty α
| ⟨w, h⟩ := ⟨w⟩
/- subsingleton -/
class inductive subsingleton (α : Sort u) : Prop
| intro (h : ∀ a b : α, a = b) : subsingleton
protected lemma subsingleton.elim {α : Sort u} [h : subsingleton α] : ∀ (a b : α), a = b :=
subsingleton.rec (λ p, p) h
protected lemma subsingleton.helim {α β : Sort u} [h : subsingleton α] (h : α = β) :
∀ (a : α) (b : β), a == b :=
eq.rec_on h (λ a b : α, heq_of_eq (subsingleton.elim a b))
instance subsingleton_prop (p : Prop) : subsingleton p :=
⟨λ a b, proof_irrel a b⟩
instance (p : Prop) : subsingleton (decidable p) :=
subsingleton.intro (λ d₁,
match d₁ with
| (is_true t₁) := (λ d₂,
match d₂ with
| (is_true t₂) := eq.rec_on (proof_irrel t₁ t₂) rfl
| (is_false f₂) := absurd t₁ f₂
end)
| (is_false f₁) := (λ d₂,
match d₂ with
| (is_true t₂) := absurd t₂ f₁
| (is_false f₂) := eq.rec_on (proof_irrel f₁ f₂) rfl
end)
end)
protected lemma rec_subsingleton {p : Prop} [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u}
[h₃ : Π (h : p), subsingleton (h₁ h)] [h₄ : Π (h : ¬p), subsingleton (h₂ h)]
: subsingleton (decidable.rec_on h h₂ h₁) :=
match h with
| (is_true h) := h₃ h
| (is_false h) := h₄ h
end
lemma if_pos {c : Prop} [h : decidable c] (hc : c) {α : Sort u} {t e : α} : (ite c t e) = t :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := absurd hc hnc
end
lemma if_neg {c : Prop} [h : decidable c] (hnc : ¬c) {α : Sort u} {t e : α} : (ite c t e) = e :=
match h with
| (is_true hc) := absurd hc hnc
| (is_false hnc) := rfl
end
@[simp]
lemma if_t_t (c : Prop) [h : decidable c] {α : Sort u} (t : α) : (ite c t t) = t :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := rfl
end
lemma implies_of_if_pos {c t e : Prop} [decidable c] (h : ite c t e) : c → t :=
assume hc, eq.rec_on (if_pos hc : ite c t e = t) h
lemma implies_of_if_neg {c t e : Prop} [decidable c] (h : ite c t e) : ¬c → e :=
assume hnc, eq.rec_on (if_neg hnc : ite c t e = e) h
lemma if_ctx_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x y u v : α}
(h_c : b ↔ c) (h_t : c → x = u) (h_e : ¬c → y = v) :
ite b x y = ite c u v :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
@[congr]
lemma if_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x y u v : α}
(h_c : b ↔ c) (h_t : x = u) (h_e : y = v) :
ite b x y = ite c u v :=
@if_ctx_congr α b c dec_b dec_c x y u v h_c (λ h, h_t) (λ h, h_e)
@[simp]
lemma if_true {α : Sort u} {h : decidable true} (t e : α) : (@ite α true h t e) = t :=
if_pos trivial
@[simp]
lemma if_false {α : Sort u} {h : decidable false} (t e : α) : (@ite α false h t e) = e :=
if_neg not_false
lemma if_ctx_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c]
(h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) :
ite b x y ↔ ite c u v :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
@[congr]
lemma if_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c]
(h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) :
ite b x y ↔ ite c u v :=
if_ctx_congr_prop h_c (λ h, h_t) (λ h, h_e)
lemma if_ctx_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b]
(h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) :
ite b x y ↔ (@ite Prop c (decidable_of_decidable_of_iff dec_b h_c) u v) :=
@if_ctx_congr_prop b c x y u v dec_b (decidable_of_decidable_of_iff dec_b h_c) h_c h_t h_e
@[congr]
lemma if_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b]
(h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) :
ite b x y ↔ (@ite Prop c (decidable_of_decidable_of_iff dec_b h_c) u v) :=
@if_ctx_simp_congr_prop b c x y u v dec_b h_c (λ h, h_t) (λ h, h_e)
@[simp] lemma dif_pos {c : Prop} [h : decidable c] (hc : c) {α : Sort u} {t : c → α} {e : ¬ c → α} : dite c t e = t hc :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := absurd hc hnc
end
@[simp] lemma dif_neg {c : Prop} [h : decidable c] (hnc : ¬c) {α : Sort u} {t : c → α} {e : ¬ c → α} : dite c t e = e hnc :=
match h with
| (is_true hc) := absurd hc hnc
| (is_false hnc) := rfl
end
@[congr]
lemma dif_ctx_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x : b → α} {u : c → α} {y : ¬b → α} {v : ¬c → α}
(h_c : b ↔ c)
(h_t : ∀ (h : c), x (iff.mpr h_c h) = u h)
(h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) :
(@dite α b dec_b x y) = (@dite α c dec_c u v) :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
lemma dif_ctx_simp_congr {α : Sort u} {b c : Prop} [dec_b : decidable b]
{x : b → α} {u : c → α} {y : ¬b → α} {v : ¬c → α}
(h_c : b ↔ c)
(h_t : ∀ (h : c), x (iff.mpr h_c h) = u h)
(h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) :
(@dite α b dec_b x y) = (@dite α c (decidable_of_decidable_of_iff dec_b h_c) u v) :=
@dif_ctx_congr α b c dec_b (decidable_of_decidable_of_iff dec_b h_c) x u y v h_c h_t h_e
-- Remark: dite and ite are "defally equal" when we ignore the proofs.
lemma dif_eq_if (c : Prop) [h : decidable c] {α : Sort u} (t : α) (e : α) : dite c (λ h, t) (λ h, e) = ite c t e :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := rfl
end
instance {c t e : Prop} [d_c : decidable c] [d_t : decidable t] [d_e : decidable e] : decidable (if c then t else e) :=
match d_c with
| (is_true hc) := d_t
| (is_false hc) := d_e
end
instance {c : Prop} {t : c → Prop} {e : ¬c → Prop} [d_c : decidable c] [d_t : ∀ h, decidable (t h)] [d_e : ∀ h, decidable (e h)] : decidable (if h : c then t h else e h) :=
match d_c with
| (is_true hc) := d_t hc
| (is_false hc) := d_e hc
end
def as_true (c : Prop) [decidable c] : Prop :=
if c then true else false
def as_false (c : Prop) [decidable c] : Prop :=
if c then false else true
lemma of_as_true {c : Prop} [h₁ : decidable c] (h₂ : as_true c) : c :=
match h₁, h₂ with
| (is_true h_c), h₂ := h_c
| (is_false h_c), h₂ := false.elim h₂
end
/-- Universe lifting operation -/
structure {r s} ulift (α : Type s) : Type (max s r) :=
up :: (down : α)
namespace ulift
/- Bijection between α and ulift.{v} α -/
lemma up_down {α : Type u} : ∀ (b : ulift.{v} α), up (down b) = b
| (up a) := rfl
lemma down_up {α : Type u} (a : α) : down (up.{v} a) = a := rfl
end ulift
/-- Universe lifting operation from Sort to Type -/
structure plift (α : Sort u) : Type u :=
up :: (down : α)
namespace plift
/- Bijection between α and plift α -/
lemma up_down {α : Sort u} : ∀ (b : plift α), up (down b) = b
| (up a) := rfl
lemma down_up {α : Sort u} (a : α) : down (up a) = a := rfl
end plift
/- Equalities for rewriting let-expressions -/
lemma let_value_eq {α : Sort u} {β : Sort v} {a₁ a₂ : α} (b : α → β) :
a₁ = a₂ → (let x : α := a₁ in b x) = (let x : α := a₂ in b x) :=
λ h, eq.rec_on h rfl
lemma let_value_heq {α : Sort v} {β : α → Sort u} {a₁ a₂ : α} (b : Π x : α, β x) :
a₁ = a₂ → (let x : α := a₁ in b x) == (let x : α := a₂ in b x) :=
λ h, eq.rec_on h (heq.refl (b a₁))
lemma let_body_eq {α : Sort v} {β : α → Sort u} (a : α) {b₁ b₂ : Π x : α, β x} :
(∀ x, b₁ x = b₂ x) → (let x : α := a in b₁ x) = (let x : α := a in b₂ x) :=
λ h, h a
lemma let_eq {α : Sort v} {β : Sort u} {a₁ a₂ : α} {b₁ b₂ : α → β} :
a₁ = a₂ → (∀ x, b₁ x = b₂ x) → (let x : α := a₁ in b₁ x) = (let x : α := a₂ in b₂ x) :=
λ h₁ h₂, eq.rec_on h₁ (h₂ a₁)
section relation
variables {α : Sort u} {β : Sort v} (r : β → β → Prop)
local infix `≺`:50 := r
def reflexive := ∀ x, x ≺ x
def symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x
def transitive := ∀ ⦃x y z⦄, x ≺ y → y ≺ z → x ≺ z
def equivalence := reflexive r ∧ symmetric r ∧ transitive r
def total := ∀ x y, x ≺ y ∨ y ≺ x
lemma mk_equivalence (rfl : reflexive r) (symm : symmetric r) (trans : transitive r) :
equivalence r :=
⟨rfl, symm, trans⟩
def irreflexive := ∀ x, ¬ x ≺ x
def anti_symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x → x = y
def empty_relation := λ a₁ a₂ : α, false
def subrelation (q r : β → β → Prop) := ∀ ⦃x y⦄, q x y → r x y
def inv_image (f : α → β) : α → α → Prop :=
λ a₁ a₂, f a₁ ≺ f a₂
lemma inv_image.trans (f : α → β) (h : transitive r) : transitive (inv_image r f) :=
λ (a₁ a₂ a₃ : α) (h₁ : inv_image r f a₁ a₂) (h₂ : inv_image r f a₂ a₃), h h₁ h₂
lemma inv_image.irreflexive (f : α → β) (h : irreflexive r) : irreflexive (inv_image r f) :=
λ (a : α) (h₁ : inv_image r f a a), h (f a) h₁
inductive tc {α : Sort u} (r : α → α → Prop) : α → α → Prop
| base : ∀ a b, r a b → tc a b
| trans : ∀ a b c, tc a b → tc b c → tc a c
end relation
section binary
variables {α : Type u} {β : Type v}
variable f : α → α → α
variable inv : α → α
variable one : α
local notation a * b := f a b
local notation a ⁻¹ := inv a
variable g : α → α → α
local notation a + b := g a b
def commutative := ∀ a b, a * b = b * a
def associative := ∀ a b c, (a * b) * c = a * (b * c)
def left_identity := ∀ a, one * a = a
def right_identity := ∀ a, a * one = a
def right_inverse := ∀ a, a * a⁻¹ = one
def left_cancelative := ∀ a b c, a * b = a * c → b = c
def right_cancelative := ∀ a b c, a * b = c * b → a = c
def left_distributive := ∀ a b c, a * (b + c) = a * b + a * c
def right_distributive := ∀ a b c, (a + b) * c = a * c + b * c
def right_commutative (h : β → α → β) := ∀ b a₁ a₂, h (h b a₁) a₂ = h (h b a₂) a₁
def left_commutative (h : α → β → β) := ∀ a₁ a₂ b, h a₁ (h a₂ b) = h a₂ (h a₁ b)
lemma left_comm : commutative f → associative f → left_commutative f :=
assume hcomm hassoc, assume a b c, calc
a*(b*c) = (a*b)*c : eq.symm (hassoc a b c)
... = (b*a)*c : hcomm a b ▸ rfl
... = b*(a*c) : hassoc b a c
lemma right_comm : commutative f → associative f → right_commutative f :=
assume hcomm hassoc, assume a b c, calc
(a*b)*c = a*(b*c) : hassoc a b c
... = a*(c*b) : hcomm b c ▸ rfl
... = (a*c)*b : eq.symm (hassoc a c b)
end binary
|
77d983ed602d777e4ac237adc2776834be51ff32 | 41ebf3cb010344adfa84907b3304db00e02db0a6 | /uexp/src/uexp/rules/decorrelateExists.lean | 80a5b53ecaa4d020b7bebfefdb59a74922bbe174 | [
"BSD-2-Clause"
] | permissive | ReinierKoops/Cosette | e061b2ba58b26f4eddf4cd052dcf7abd16dfe8fb | eb8dadd06ee05fe7b6b99de431dd7c4faef5cb29 | refs/heads/master | 1,686,483,953,198 | 1,624,293,498,000 | 1,624,293,498,000 | 378,997,885 | 0 | 0 | BSD-2-Clause | 1,624,293,485,000 | 1,624,293,484,000 | null | UTF-8 | Lean | false | false | 1,641 | lean | import ..sql
import ..tactics
import ..u_semiring
import ..meta.cosette_lemmas
import ..extra_constants
import ..meta.TDP
import ..meta.cosette_tactics
open Expr
open Proj
open Pred
open SQL
variable i : const datatypes.int
theorem rule :
forall (Γ scm_emp : Schema)
(rel_emp : relation scm_emp)
(emp_empno : Column datatypes.int scm_emp)
(emp_ename : Column datatypes.int scm_emp)
(emp_job : Column datatypes.int scm_emp)
(emp_mgr : Column datatypes.int scm_emp)
(emp_hiredate : Column datatypes.int scm_emp)
(emp_comm : Column datatypes.int scm_emp)
(emp_sal : Column datatypes.int scm_emp)
(emp_deptno : Column datatypes.int scm_emp)
(emp_slacker : Column datatypes.int scm_emp),
denoteSQL
(SELECT *
FROM1 (table rel_emp
WHERE EXISTS (SELECT *
FROM1 table rel_emp
WHERE (equal (uvariable (left⋅right⋅emp_deptno)) (uvariable (right⋅emp_deptno)))))
: SQL Γ _) =
denoteSQL
(SELECT1 (right⋅left⋅star)
FROM1 (product (table rel_emp)
(DISTINCT (SELECT1 (combine (right⋅emp_deptno)
(e2p (constantExpr i)))
FROM1 table rel_emp))
WHERE (equal (uvariable (right⋅left⋅emp_deptno))
(uvariable (right⋅right⋅left)))) : SQL Γ _) :=
begin
intros,
unfold_all_denotations,
funext,
simp,
print_size,
apply ueq_symm,
-- remove_dup_sigs_lhs,
sorry
end |
036eab9ca4560b20084764d0a7dccdb84e1dea84 | 471bedbd023d35c9d078c2f936dd577ace7f5813 | /library/init/logic.lean | f1d67aef5226788755f3747eeb0b4eb5024b86ec | [
"Apache-2.0"
] | permissive | lambdaxymox/lean | e06f0fa503666df827edd9867d7f49ca017aae64 | fc13c8c72a15dab71a2c2b31410c2cadc3526bd7 | refs/heads/master | 1,666,785,407,985 | 1,666,153,673,000 | 1,666,153,673,000 | 310,165,986 | 0 | 0 | Apache-2.0 | 1,604,542,096,000 | 1,604,542,095,000 | null | UTF-8 | Lean | false | false | 38,948 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Jeremy Avigad, Floris van Doorn
-/
prelude
import init.core
universes u v w
@[simp] lemma opt_param_eq (α : Sort u) (default : α) : opt_param α default = α :=
rfl
@[inline] def id {α : Sort u} (a : α) : α := a
def flip {α : Sort u} {β : Sort v} {φ : Sort w} (f : α → β → φ) : β → α → φ :=
λ b a, f a b
/- implication -/
def implies (a b : Prop) := a → b
/-- Implication `→` is transitive. If `P → Q` and `Q → R` then `P → R`. -/
@[trans] lemma implies.trans {p q r : Prop} (h₁ : implies p q) (h₂ : implies q r) : implies p r :=
assume hp, h₂ (h₁ hp)
lemma trivial : true := ⟨⟩
/-- We can't have `a` and `¬a`, that would be absurd!-/
@[inline] def absurd {a : Prop} {b : Sort v} (h₁ : a) (h₂ : ¬a) : b :=
false.rec b (h₂ h₁)
lemma not.intro {a : Prop} (h : a → false) : ¬ a :=
h
/-- Modus tollens. If an implication is true, then so is its contrapositive. -/
lemma mt {a b : Prop} (h₁ : a → b) (h₂ : ¬b) : ¬a := assume ha : a, h₂ (h₁ ha)
/- not -/
lemma not_false : ¬false := id
def non_contradictory (a : Prop) : Prop := ¬¬a
lemma non_contradictory_intro {a : Prop} (ha : a) : ¬¬a :=
assume hna : ¬a, absurd ha hna
/- false -/
@[inline] def false.elim {C : Sort u} (h : false) : C :=
false.rec C h
/- eq -/
-- proof irrelevance is built in
lemma proof_irrel {a : Prop} (h₁ h₂ : a) : h₁ = h₂ := rfl
@[simp] lemma id.def {α : Sort u} (a : α) : id a = a := rfl
@[inline] def eq.mp {α β : Sort u} : (α = β) → α → β :=
eq.rec_on
@[inline] def eq.mpr {α β : Sort u} : (α = β) → β → α :=
λ h₁ h₂, eq.rec_on (eq.symm h₁) h₂
@[elab_as_eliminator]
lemma eq.substr {α : Sort u} {p : α → Prop} {a b : α} (h₁ : b = a) : p a → p b :=
eq.subst (eq.symm h₁)
lemma congr {α : Sort u} {β : Sort v} {f₁ f₂ : α → β} {a₁ a₂ : α} (h₁ : f₁ = f₂) (h₂ : a₁ = a₂) : f₁ a₁ = f₂ a₂ :=
eq.subst h₁ (eq.subst h₂ rfl)
lemma congr_fun {α : Sort u} {β : α → Sort v} {f g : Π x, β x} (h : f = g) (a : α) : f a = g a :=
eq.subst h (eq.refl (f a))
lemma congr_arg {α : Sort u} {β : Sort v} {a₁ a₂ : α} (f : α → β) : a₁ = a₂ → f a₁ = f a₂ :=
congr rfl
lemma trans_rel_left {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : r a b) (h₂ : b = c) : r a c :=
h₂ ▸ h₁
lemma trans_rel_right {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : a = b) (h₂ : r b c) : r a c :=
h₁.symm ▸ h₂
lemma of_eq_true {p : Prop} (h : p = true) : p :=
h.symm ▸ trivial
lemma not_of_eq_false {p : Prop} (h : p = false) : ¬p :=
assume hp, h ▸ hp
@[inline] def cast {α β : Sort u} (h : α = β) (a : α) : β :=
eq.rec a h
lemma cast_proof_irrel {α β : Sort u} (h₁ h₂ : α = β) (a : α) : cast h₁ a = cast h₂ a := rfl
lemma cast_eq {α : Sort u} (h : α = α) (a : α) : cast h a = a := rfl
/- ne -/
@[reducible] def ne {α : Sort u} (a b : α) := ¬(a = b)
infix ` ≠ `:50 := ne
@[simp] lemma ne.def {α : Sort u} (a b : α) : a ≠ b = ¬ (a = b) := rfl
namespace ne
variable {α : Sort u}
variables {a b : α}
lemma intro (h : a = b → false) : a ≠ b := h
lemma elim (h : a ≠ b) : a = b → false := h
lemma irrefl (h : a ≠ a) : false := h rfl
lemma symm (h : a ≠ b) : b ≠ a :=
assume (h₁ : b = a), h (h₁.symm)
end ne
lemma false_of_ne {α : Sort u} {a : α} : a ≠ a → false := ne.irrefl
section
variables {p : Prop}
lemma ne_false_of_self : p → p ≠ false :=
assume (hp : p) (heq : p = false), heq ▸ hp
lemma ne_true_of_not : ¬p → p ≠ true :=
assume (hnp : ¬p) (heq : p = true), (heq ▸ hnp) trivial
lemma true_ne_false : ¬true = false :=
ne_false_of_self trivial
end
attribute [refl] heq.refl
section
variables {α β φ : Sort u} {a a' : α} {b b' : β} {c : φ}
def heq.elim {α : Sort u} {a : α} {p : α → Sort v} {b : α} (h₁ : a == b) : p a → p b :=
eq.rec_on (eq_of_heq h₁)
lemma heq.subst {p : ∀ T : Sort u, T → Prop} : a == b → p α a → p β b :=
heq.rec_on
@[symm] lemma heq.symm (h : a == b) : b == a :=
heq.rec_on h (heq.refl a)
lemma heq_of_eq (h : a = a') : a == a' :=
eq.subst h (heq.refl a)
@[trans] lemma heq.trans (h₁ : a == b) (h₂ : b == c) : a == c :=
heq.subst h₂ h₁
@[trans] lemma heq_of_heq_of_eq (h₁ : a == b) (h₂ : b = b') : a == b' :=
heq.trans h₁ (heq_of_eq h₂)
@[trans] lemma heq_of_eq_of_heq (h₁ : a = a') (h₂ : a' == b) : a == b :=
heq.trans (heq_of_eq h₁) h₂
lemma type_eq_of_heq (h : a == b) : α = β :=
heq.rec_on h (eq.refl α)
end
lemma eq_rec_heq {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} (h : a = a') (p : φ a), (eq.rec_on h p : φ a') == p
| a _ rfl p := heq.refl p
lemma heq_of_eq_rec_left {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} {p₁ : φ a} {p₂ : φ a'} (e : a = a') (h₂ : (eq.rec_on e p₁ : φ a') = p₂), p₁ == p₂
| a _ p₁ p₂ rfl h := eq.rec_on h (heq.refl p₁)
lemma heq_of_eq_rec_right {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} {p₁ : φ a} {p₂ : φ a'} (e : a' = a) (h₂ : p₁ = eq.rec_on e p₂), p₁ == p₂
| a _ p₁ p₂ rfl h :=
have p₁ = p₂, from h,
this ▸ heq.refl p₁
lemma of_heq_true {a : Prop} (h : a == true) : a :=
of_eq_true (eq_of_heq h)
lemma eq_rec_compose : ∀ {α β φ : Sort u} (p₁ : β = φ) (p₂ : α = β) (a : α), (eq.rec_on p₁ (eq.rec_on p₂ a : β) : φ) = eq.rec_on (eq.trans p₂ p₁) a
| α _ _ rfl rfl a := rfl
lemma cast_heq : ∀ {α β : Sort u} (h : α = β) (a : α), cast h a == a
| α _ rfl a := heq.refl a
/- and -/
infixr ` /\ `:35 := and
infixr ` ∧ `:35 := and
variables {a b c d : Prop}
lemma and.elim (h₁ : a ∧ b) (h₂ : a → b → c) : c :=
and.rec h₂ h₁
lemma and.swap : a ∧ b → b ∧ a :=
assume ⟨ha, hb⟩, ⟨hb, ha⟩
lemma and.symm : a ∧ b → b ∧ a := and.swap
/- or -/
infixr ` \/ `:30 := or
infixr ` ∨ `:30 := or
namespace or
lemma elim (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → c) : c :=
or.rec h₂ h₃ h₁
end or
lemma non_contradictory_em (a : Prop) : ¬¬(a ∨ ¬a) :=
assume not_em : ¬(a ∨ ¬a),
have neg_a : ¬a, from
assume pos_a : a, absurd (or.inl pos_a) not_em,
absurd (or.inr neg_a) not_em
lemma or.swap : a ∨ b → b ∨ a := or.rec or.inr or.inl
lemma or.symm : a ∨ b → b ∨ a := or.swap
/- xor -/
def xor (a b : Prop) := (a ∧ ¬ b) ∨ (b ∧ ¬ a)
/- iff -/
/-- `iff P Q`, with notation `P ↔ Q`, is the proposition asserting that `P` and `Q` are equivalent,
that is, have the same truth value. -/
structure iff (a b : Prop) : Prop :=
intro :: (mp : a → b) (mpr : b → a)
infix ` <-> `:20 := iff
infix ` ↔ `:20 := iff
lemma iff.elim : ((a → b) → (b → a) → c) → (a ↔ b) → c := iff.rec
attribute [recursor 5] iff.elim
lemma iff.elim_left : (a ↔ b) → a → b := iff.mp
lemma iff.elim_right : (a ↔ b) → b → a := iff.mpr
lemma iff_iff_implies_and_implies (a b : Prop) : (a ↔ b) ↔ (a → b) ∧ (b → a) :=
iff.intro (λ h, and.intro h.mp h.mpr) (λ h, iff.intro h.left h.right)
@[refl]
lemma iff.refl (a : Prop) : a ↔ a :=
iff.intro (assume h, h) (assume h, h)
lemma iff.rfl {a : Prop} : a ↔ a :=
iff.refl a
@[trans]
lemma iff.trans (h₁ : a ↔ b) (h₂ : b ↔ c) : a ↔ c :=
iff.intro
(assume ha, iff.mp h₂ (iff.mp h₁ ha))
(assume hc, iff.mpr h₁ (iff.mpr h₂ hc))
@[symm]
lemma iff.symm (h : a ↔ b) : b ↔ a :=
iff.intro (iff.elim_right h) (iff.elim_left h)
lemma iff.comm : (a ↔ b) ↔ (b ↔ a) :=
iff.intro iff.symm iff.symm
lemma eq.to_iff {a b : Prop} (h : a = b) : a ↔ b :=
eq.rec_on h iff.rfl
lemma neq_of_not_iff {a b : Prop} : ¬(a ↔ b) → a ≠ b :=
λ h₁ h₂,
have a ↔ b, from eq.subst h₂ (iff.refl a),
absurd this h₁
lemma not_iff_not_of_iff (h₁ : a ↔ b) : ¬a ↔ ¬b :=
iff.intro
(assume (hna : ¬ a) (hb : b), hna (iff.elim_right h₁ hb))
(assume (hnb : ¬ b) (ha : a), hnb (iff.elim_left h₁ ha))
lemma of_iff_true (h : a ↔ true) : a :=
iff.mp (iff.symm h) trivial
lemma not_of_iff_false : (a ↔ false) → ¬a := iff.mp
lemma iff_true_intro (h : a) : a ↔ true :=
iff.intro
(λ hl, trivial)
(λ hr, h)
lemma iff_false_intro (h : ¬a) : a ↔ false :=
iff.intro h (false.rec a)
lemma not_non_contradictory_iff_absurd (a : Prop) : ¬¬¬a ↔ ¬a :=
iff.intro
(λ (hl : ¬¬¬a) (ha : a), hl (non_contradictory_intro ha))
absurd
lemma imp_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a → b) ↔ (c → d) :=
iff.intro
(λ hab hc, iff.mp h₂ (hab (iff.mpr h₁ hc)))
(λ hcd ha, iff.mpr h₂ (hcd (iff.mp h₁ ha)))
lemma imp_congr_ctx (h₁ : a ↔ c) (h₂ : c → (b ↔ d)) : (a → b) ↔ (c → d) :=
iff.intro
(λ hab hc, have ha : a, from iff.mpr h₁ hc,
have hb : b, from hab ha,
iff.mp (h₂ hc) hb)
(λ hcd ha, have hc : c, from iff.mp h₁ ha,
have hd : d, from hcd hc,
iff.mpr (h₂ hc) hd)
lemma imp_congr_right (h : a → (b ↔ c)) : (a → b) ↔ (a → c) :=
iff.intro
(assume hab ha, iff.elim_left (h ha) (hab ha))
(assume hab ha, iff.elim_right (h ha) (hab ha))
lemma not_not_intro (ha : a) : ¬¬a :=
assume hna : ¬a, hna ha
lemma not_of_not_not_not (h : ¬¬¬a) : ¬a :=
λ ha, absurd (not_not_intro ha) h
@[simp] lemma not_true : (¬ true) ↔ false :=
iff_false_intro (not_not_intro trivial)
@[simp] lemma not_false_iff : (¬ false) ↔ true :=
iff_true_intro not_false
@[congr] lemma not_congr (h : a ↔ b) : ¬a ↔ ¬b :=
iff.intro (λ h₁ h₂, h₁ (iff.mpr h h₂)) (λ h₁ h₂, h₁ (iff.mp h h₂))
lemma ne_self_iff_false {α : Sort u} (a : α) : (not (a = a)) ↔ false :=
iff.intro false_of_ne false.elim
@[simp] lemma eq_self_iff_true {α : Sort u} (a : α) : (a = a) ↔ true :=
iff_true_intro rfl
lemma heq_self_iff_true {α : Sort u} (a : α) : (a == a) ↔ true :=
iff_true_intro (heq.refl a)
@[simp] lemma iff_not_self (a : Prop) : (a ↔ ¬a) ↔ false :=
iff_false_intro (λ h,
have h' : ¬a, from (λ ha, (iff.mp h ha) ha),
h' (iff.mpr h h'))
@[simp] lemma not_iff_self (a : Prop) : (¬a ↔ a) ↔ false :=
iff_false_intro (λ h,
have h' : ¬a, from (λ ha, (iff.mpr h ha) ha),
h' (iff.mp h h'))
lemma true_iff_false : (true ↔ false) ↔ false :=
iff_false_intro (λ h, iff.mp h trivial)
lemma false_iff_true : (false ↔ true) ↔ false :=
iff_false_intro (λ h, iff.mpr h trivial)
lemma false_of_true_iff_false : (true ↔ false) → false :=
assume h, iff.mp h trivial
lemma false_of_true_eq_false : (true = false) → false :=
assume h, h ▸ trivial
lemma true_eq_false_of_false : false → (true = false) :=
false.elim
lemma eq_comm {α : Sort u} {a b : α} : a = b ↔ b = a :=
⟨eq.symm, eq.symm⟩
/- and simp rules -/
lemma and.imp (hac : a → c) (hbd : b → d) : a ∧ b → c ∧ d :=
assume ⟨ha, hb⟩, ⟨hac ha, hbd hb⟩
lemma and_implies (hac : a → c) (hbd : b → d) : a ∧ b → c ∧ d := and.imp hac hbd
@[congr] lemma and_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∧ b) ↔ (c ∧ d) :=
iff.intro (and.imp (iff.mp h₁) (iff.mp h₂)) (and.imp (iff.mpr h₁) (iff.mpr h₂))
lemma and_congr_right (h : a → (b ↔ c)) : (a ∧ b) ↔ (a ∧ c) :=
iff.intro
(assume ⟨ha, hb⟩, ⟨ha, iff.elim_left (h ha) hb⟩)
(assume ⟨ha, hc⟩, ⟨ha, iff.elim_right (h ha) hc⟩)
lemma and.comm : a ∧ b ↔ b ∧ a :=
iff.intro and.swap and.swap
lemma and_comm (a b : Prop) : a ∧ b ↔ b ∧ a := and.comm
lemma and.assoc : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) :=
iff.intro
(assume ⟨⟨ha, hb⟩, hc⟩, ⟨ha, ⟨hb, hc⟩⟩)
(assume ⟨ha, ⟨hb, hc⟩⟩, ⟨⟨ha, hb⟩, hc⟩)
lemma and_assoc (a b : Prop) : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) := and.assoc
lemma and.left_comm : a ∧ (b ∧ c) ↔ b ∧ (a ∧ c) :=
iff.trans (iff.symm and.assoc) (iff.trans (and_congr and.comm (iff.refl c)) and.assoc)
lemma and_iff_left {a b : Prop} (hb : b) : (a ∧ b) ↔ a :=
iff.intro and.left (λ ha, ⟨ha, hb⟩)
lemma and_iff_right {a b : Prop} (ha : a) : (a ∧ b) ↔ b :=
iff.intro and.right (and.intro ha)
@[simp] lemma and_true (a : Prop) : a ∧ true ↔ a :=
and_iff_left trivial
@[simp] lemma true_and (a : Prop) : true ∧ a ↔ a :=
and_iff_right trivial
@[simp] lemma and_false (a : Prop) : a ∧ false ↔ false :=
iff_false_intro and.right
@[simp] lemma false_and (a : Prop) : false ∧ a ↔ false :=
iff_false_intro and.left
@[simp] lemma not_and_self (a : Prop) : (¬a ∧ a) ↔ false :=
iff_false_intro (λ h, and.elim h (λ h₁ h₂, absurd h₂ h₁))
@[simp] lemma and_not_self (a : Prop) : (a ∧ ¬a) ↔ false :=
iff_false_intro (assume ⟨h₁, h₂⟩, absurd h₁ h₂)
@[simp] lemma and_self (a : Prop) : a ∧ a ↔ a :=
iff.intro and.left (assume h, ⟨h, h⟩)
/- or simp rules -/
lemma or.imp (h₂ : a → c) (h₃ : b → d) : a ∨ b → c ∨ d :=
or.rec (λ h, or.inl (h₂ h)) (λ h, or.inr (h₃ h))
lemma or.imp_left (h : a → b) : a ∨ c → b ∨ c :=
or.imp h id
lemma or.imp_right (h : a → b) : c ∨ a → c ∨ b :=
or.imp id h
@[congr] lemma or_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ∨ b) ↔ (c ∨ d) :=
iff.intro (or.imp (iff.mp h₁) (iff.mp h₂)) (or.imp (iff.mpr h₁) (iff.mpr h₂))
lemma or.comm : a ∨ b ↔ b ∨ a := iff.intro or.swap or.swap
lemma or_comm (a b : Prop) : a ∨ b ↔ b ∨ a := or.comm
lemma or.assoc : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) :=
iff.intro
(or.rec (or.imp_right or.inl) (λ h, or.inr (or.inr h)))
(or.rec (λ h, or.inl (or.inl h)) (or.imp_left or.inr))
lemma or_assoc (a b : Prop) : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) :=
or.assoc
lemma or.left_comm : a ∨ (b ∨ c) ↔ b ∨ (a ∨ c) :=
iff.trans (iff.symm or.assoc) (iff.trans (or_congr or.comm (iff.refl c)) or.assoc)
theorem or_iff_right_of_imp (ha : a → b) : (a ∨ b) ↔ b :=
iff.intro (or.rec ha id) or.inr
theorem or_iff_left_of_imp (hb : b → a) : (a ∨ b) ↔ a :=
iff.intro (or.rec id hb) or.inl
@[simp] lemma or_true (a : Prop) : a ∨ true ↔ true :=
iff_true_intro (or.inr trivial)
@[simp] lemma true_or (a : Prop) : true ∨ a ↔ true :=
iff_true_intro (or.inl trivial)
@[simp] lemma or_false (a : Prop) : a ∨ false ↔ a :=
iff.intro (or.rec id false.elim) or.inl
@[simp] lemma false_or (a : Prop) : false ∨ a ↔ a :=
iff.trans or.comm (or_false a)
@[simp] lemma or_self (a : Prop) : a ∨ a ↔ a :=
iff.intro (or.rec id id) or.inl
lemma not_or {a b : Prop} : ¬ a → ¬ b → ¬ (a ∨ b)
| hna hnb (or.inl ha) := absurd ha hna
| hna hnb (or.inr hb) := absurd hb hnb
/- or resolution rulse -/
lemma or.resolve_left {a b : Prop} (h : a ∨ b) (na : ¬ a) : b :=
or.elim h (λ ha, absurd ha na) id
lemma or.neg_resolve_left {a b : Prop} (h : ¬ a ∨ b) (ha : a) : b :=
or.elim h (λ na, absurd ha na) id
lemma or.resolve_right {a b : Prop} (h : a ∨ b) (nb : ¬ b) : a :=
or.elim h id (λ hb, absurd hb nb)
lemma or.neg_resolve_right {a b : Prop} (h : a ∨ ¬ b) (hb : b) : a :=
or.elim h id (λ nb, absurd hb nb)
/- iff simp rules -/
@[simp] lemma iff_true (a : Prop) : (a ↔ true) ↔ a :=
iff.intro (assume h, iff.mpr h trivial) iff_true_intro
@[simp] lemma true_iff (a : Prop) : (true ↔ a) ↔ a :=
iff.trans iff.comm (iff_true a)
@[simp] lemma iff_false (a : Prop) : (a ↔ false) ↔ ¬ a :=
iff.intro iff.mp iff_false_intro
@[simp] lemma false_iff (a : Prop) : (false ↔ a) ↔ ¬ a :=
iff.trans iff.comm (iff_false a)
@[simp] lemma iff_self (a : Prop) : (a ↔ a) ↔ true :=
iff_true_intro iff.rfl
@[congr] lemma iff_congr (h₁ : a ↔ c) (h₂ : b ↔ d) : (a ↔ b) ↔ (c ↔ d) :=
(iff_iff_implies_and_implies a b).trans
((and_congr (imp_congr h₁ h₂) (imp_congr h₂ h₁)).trans
(iff_iff_implies_and_implies c d).symm)
/- implies simp rule -/
@[simp] lemma implies_true_iff (α : Sort u) : (α → true) ↔ true :=
iff.intro (λ h, trivial) (λ ha h, trivial)
lemma false_implies_iff (a : Prop) : (false → a) ↔ true :=
iff.intro (λ h, trivial) (λ ha h, false.elim h)
theorem true_implies_iff (α : Prop) : (true → α) ↔ α :=
iff.intro (λ h, h trivial) (λ h h', h)
/--
The existential quantifier.
To prove a goal of the form `⊢ ∃ x, p x`, you can provide a witness `y` with the tactic `existsi y`.
If you are working in a project that depends on mathlib, then we recommend the `use` tactic
instead.
You'll then be left with the goal `⊢ p y`.
To extract a witness `x` and proof `hx : p x` from a hypothesis `h : ∃ x, p x`,
use the tactic `cases h with x hx`. See also the mathlib tactics `obtain` and `rcases`.
-/
inductive Exists {α : Sort u} (p : α → Prop) : Prop
| intro (w : α) (h : p w) : Exists
attribute [intro] Exists.intro
notation `exists` binders `, ` r:(scoped P, Exists P) := r
notation `∃` binders `, ` r:(scoped P, Exists P) := r
/- This is a `def`, so that it can be used as pattern in the equation compiler. -/
@[pattern] def exists.intro {α : Sort u} {p : α → Prop} (w : α) (h : p w) : ∃ x, p x := ⟨w, h⟩
lemma exists.elim {α : Sort u} {p : α → Prop} {b : Prop}
(h₁ : ∃ x, p x) (h₂ : ∀ (a : α), p a → b) : b :=
Exists.rec h₂ h₁
/- exists unique -/
def exists_unique {α : Sort u} (p : α → Prop) :=
∃ x, p x ∧ ∀ y, p y → y = x
notation `∃!` binders `, ` r:(scoped P, exists_unique P) := r
@[intro]
lemma exists_unique.intro {α : Sort u} {p : α → Prop} (w : α) (h₁ : p w) (h₂ : ∀ y, p y → y = w) :
∃! x, p x :=
exists.intro w ⟨h₁, h₂⟩
attribute [recursor 4]
lemma exists_unique.elim {α : Sort u} {p : α → Prop} {b : Prop}
(h₂ : ∃! x, p x) (h₁ : ∀ x, p x → (∀ y, p y → y = x) → b) : b :=
exists.elim h₂ (λ w hw, h₁ w (and.left hw) (and.right hw))
lemma exists_unique_of_exists_of_unique {α : Sort u} {p : α → Prop}
(hex : ∃ x, p x) (hunique : ∀ y₁ y₂, p y₁ → p y₂ → y₁ = y₂) : ∃! x, p x :=
exists.elim hex (λ x px, exists_unique.intro x px (assume y, assume : p y, hunique y x this px))
lemma exists_of_exists_unique {α : Sort u} {p : α → Prop} (h : ∃! x, p x) : ∃ x, p x :=
exists.elim h (λ x hx, ⟨x, and.left hx⟩)
lemma unique_of_exists_unique {α : Sort u} {p : α → Prop}
(h : ∃! x, p x) {y₁ y₂ : α} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ :=
exists_unique.elim h
(assume x, assume : p x,
assume unique : ∀ y, p y → y = x,
show y₁ = y₂, from eq.trans (unique _ py₁) (eq.symm (unique _ py₂)))
/- exists, forall, exists unique congruences -/
@[congr] lemma forall_congr {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a ↔ q a)) : (∀ a, p a) ↔ ∀ a, q a :=
iff.intro (λ p a, iff.mp (h a) (p a)) (λ q a, iff.mpr (h a) (q a))
lemma exists_imp_exists {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a → q a)) (p : ∃ a, p a) : ∃ a, q a :=
exists.elim p (λ a hp, ⟨a, h a hp⟩)
@[congr] lemma exists_congr {α : Sort u} {p q : α → Prop} (h : ∀ a, (p a ↔ q a)) : (Exists p) ↔ ∃ a, q a :=
iff.intro
(exists_imp_exists (λ a, iff.mp (h a)))
(exists_imp_exists (λ a, iff.mpr (h a)))
@[congr] lemma exists_unique_congr {α : Sort u} {p₁ p₂ : α → Prop} (h : ∀ x, p₁ x ↔ p₂ x) : (exists_unique p₁) ↔ (∃! x, p₂ x) := --
exists_congr (λ x, and_congr (h x) (forall_congr (λ y, imp_congr (h y) iff.rfl)))
lemma forall_not_of_not_exists {α : Sort u} {p : α → Prop} : ¬(∃ x, p x) → (∀ x, ¬p x) :=
λ hne x hp, hne ⟨x, hp⟩
/- decidable -/
def decidable.to_bool (p : Prop) [h : decidable p] : bool :=
decidable.cases_on h (λ h₁, bool.ff) (λ h₂, bool.tt)
export decidable (is_true is_false to_bool)
@[simp] lemma to_bool_true_eq_tt (h : decidable true) : @to_bool true h = tt :=
decidable.cases_on h (λ h, false.elim (iff.mp not_true h)) (λ _, rfl)
@[simp] lemma to_bool_false_eq_ff (h : decidable false) : @to_bool false h = ff :=
decidable.cases_on h (λ h, rfl) (λ h, false.elim h)
instance decidable.true : decidable true :=
is_true trivial
instance decidable.false : decidable false :=
is_false not_false
-- We use "dependent" if-then-else to be able to communicate the if-then-else condition
-- to the branches
@[inline] def dite {α : Sort u} (c : Prop) [h : decidable c] : (c → α) → (¬ c → α) → α :=
λ t e, decidable.rec_on h e t
/- if-then-else -/
@[inline] def ite {α : Sort u} (c : Prop) [h : decidable c] (t e : α) : α :=
decidable.rec_on h (λ hnc, e) (λ hc, t)
namespace decidable
variables {p q : Prop}
def rec_on_true [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} (h₃ : p) (h₄ : h₁ h₃)
: decidable.rec_on h h₂ h₁ :=
decidable.rec_on h (λ h, false.rec _ (h h₃)) (λ h, h₄)
def rec_on_false [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} (h₃ : ¬p) (h₄ : h₂ h₃)
: decidable.rec_on h h₂ h₁ :=
decidable.rec_on h (λ h, h₄) (λ h, false.rec _ (h₃ h))
def by_cases {q : Sort u} [φ : decidable p] : (p → q) → (¬p → q) → q := dite _
/-- Law of Excluded Middle. -/
lemma em (p : Prop) [decidable p] : p ∨ ¬p := by_cases or.inl or.inr
lemma by_contradiction [decidable p] (h : ¬p → false) : p :=
if h₁ : p then h₁ else false.rec _ (h h₁)
lemma of_not_not [decidable p] : ¬ ¬ p → p :=
λ hnn, by_contradiction (λ hn, absurd hn hnn)
lemma not_not_iff (p) [decidable p] : (¬ ¬ p) ↔ p :=
iff.intro of_not_not not_not_intro
lemma not_and_iff_or_not (p q : Prop) [d₁ : decidable p] [d₂ : decidable q] : ¬ (p ∧ q) ↔ ¬ p ∨ ¬ q :=
iff.intro
(λ h, match d₁ with
| is_true h₁ :=
match d₂ with
| is_true h₂ := absurd (and.intro h₁ h₂) h
| is_false h₂ := or.inr h₂
end
| is_false h₁ := or.inl h₁
end)
(λ h ⟨hp, hq⟩, or.elim h (λ h, h hp) (λ h, h hq))
lemma not_or_iff_and_not (p q) [d₁ : decidable p] [d₂ : decidable q] : ¬ (p ∨ q) ↔ ¬ p ∧ ¬ q :=
iff.intro
(λ h, match d₁ with
| is_true h₁ := false.elim $ h (or.inl h₁)
| is_false h₁ :=
match d₂ with
| is_true h₂ := false.elim $ h (or.inr h₂)
| is_false h₂ := ⟨h₁, h₂⟩
end
end)
(λ ⟨np, nq⟩ h, or.elim h np nq)
end decidable
section
variables {p q : Prop}
def decidable_of_decidable_of_iff (hp : decidable p) (h : p ↔ q) : decidable q :=
if hp : p then is_true (iff.mp h hp)
else is_false (iff.mp (not_iff_not_of_iff h) hp)
def decidable_of_decidable_of_eq (hp : decidable p) (h : p = q) : decidable q :=
decidable_of_decidable_of_iff hp h.to_iff
/-- A version of `or.elim` in `Type`. If both `p` and `q` are true, `h₁` is used. -/
protected def or.by_cases [decidable p] {α : Sort u}
(h : p ∨ q) (h₁ : p → α) (h₂ : q → α) : α :=
if hp : p then h₁ hp else h₂ (h.resolve_left hp)
end
section
variables {p q : Prop}
instance [decidable p] [decidable q] : decidable (p ∧ q) :=
if hp : p then
if hq : q then is_true ⟨hp, hq⟩
else is_false (assume h : p ∧ q, hq (and.right h))
else is_false (assume h : p ∧ q, hp (and.left h))
instance [decidable p] [decidable q] : decidable (p ∨ q) :=
if hp : p then is_true (or.inl hp) else
if hq : q then is_true (or.inr hq) else
is_false (or.rec hp hq)
instance [decidable p] : decidable (¬p) :=
if hp : p then is_false (absurd hp) else is_true hp
instance implies.decidable [decidable p] [decidable q] : decidable (p → q) :=
if hp : p then
if hq : q then is_true (assume h, hq)
else is_false (assume h : p → q, absurd (h hp) hq)
else is_true (assume h, absurd h hp)
instance [decidable p] [decidable q] : decidable (p ↔ q) :=
if hp : p then
if hq : q then is_true ⟨λ_, hq, λ_, hp⟩
else is_false $ λh, hq (h.1 hp)
else
if hq : q then is_false $ λh, hp (h.2 hq)
else is_true $ ⟨λh, absurd h hp, λh, absurd h hq⟩
instance [decidable p] [decidable q] : decidable (xor p q) :=
if hp : p then
if hq : q then is_false (or.rec (λ ⟨_, h⟩, h hq : ¬(p ∧ ¬ q)) (λ ⟨_, h⟩, h hp : ¬(q ∧ ¬ p)))
else is_true $ or.inl ⟨hp, hq⟩
else
if hq : q then is_true $ or.inr ⟨hq, hp⟩
else is_false (or.rec (λ ⟨h, _⟩, hp h : ¬(p ∧ ¬ q)) (λ ⟨h, _⟩, hq h : ¬(q ∧ ¬ p)))
instance exists_prop_decidable {p} (P : p → Prop)
[Dp : decidable p] [DP : ∀ h, decidable (P h)] : decidable (∃ h, P h) :=
if h : p then decidable_of_decidable_of_iff (DP h)
⟨λ h2, ⟨h, h2⟩, λ⟨h', h2⟩, h2⟩ else is_false (mt (λ⟨h, _⟩, h) h)
instance forall_prop_decidable {p} (P : p → Prop)
[Dp : decidable p] [DP : ∀ h, decidable (P h)] : decidable (∀ h, P h) :=
if h : p then decidable_of_decidable_of_iff (DP h)
⟨λ h2 _, h2, λal, al h⟩ else is_true (λ h2, absurd h2 h)
end
instance {α : Sort u} [decidable_eq α] (a b : α) : decidable (a ≠ b) :=
implies.decidable
lemma bool.ff_ne_tt : ff = tt → false
.
def is_dec_eq {α : Sort u} (p : α → α → bool) : Prop := ∀ ⦃x y : α⦄, p x y = tt → x = y
def is_dec_refl {α : Sort u} (p : α → α → bool) : Prop := ∀ x, p x x = tt
open decidable
instance : decidable_eq bool
| ff ff := is_true rfl
| ff tt := is_false bool.ff_ne_tt
| tt ff := is_false (ne.symm bool.ff_ne_tt)
| tt tt := is_true rfl
def decidable_eq_of_bool_pred {α : Sort u} {p : α → α → bool} (h₁ : is_dec_eq p) (h₂ : is_dec_refl p) : decidable_eq α :=
assume x y : α,
if hp : p x y = tt then is_true (h₁ hp)
else is_false (assume hxy : x = y, absurd (h₂ y) (@eq.rec_on _ _ (λ z, ¬p z y = tt) _ hxy hp))
lemma decidable_eq_inl_refl {α : Sort u} [h : decidable_eq α] (a : α) : h a a = is_true (eq.refl a) :=
match (h a a) with
| (is_true e) := rfl
| (is_false n) := absurd rfl n
end
lemma decidable_eq_inr_neg {α : Sort u} [h : decidable_eq α] {a b : α} : Π n : a ≠ b, h a b = is_false n :=
assume n,
match (h a b) with
| (is_true e) := absurd e n
| (is_false n₁) := proof_irrel n n₁ ▸ eq.refl (is_false n)
end
/- inhabited -/
class inhabited (α : Sort u) :=
(default : α)
export inhabited (default)
@[inline, irreducible] def arbitrary (α : Sort u) [inhabited α] : α :=
default
instance prop.inhabited : inhabited Prop :=
⟨true⟩
instance pi.inhabited (α : Sort u) {β : α → Sort v} [Π x, inhabited (β x)] : inhabited (Π x, β x) :=
⟨λ a, default⟩
instance : inhabited bool := ⟨ff⟩
instance : inhabited true := ⟨trivial⟩
class inductive nonempty (α : Sort u) : Prop
| intro (val : α) : nonempty
protected lemma nonempty.elim {α : Sort u} {p : Prop} (h₁ : nonempty α) (h₂ : α → p) : p :=
nonempty.rec h₂ h₁
@[priority 100]
instance nonempty_of_inhabited {α : Sort u} [inhabited α] : nonempty α :=
⟨default⟩
lemma nonempty_of_exists {α : Sort u} {p : α → Prop} : (∃ x, p x) → nonempty α
| ⟨w, h⟩ := ⟨w⟩
/- subsingleton -/
class inductive subsingleton (α : Sort u) : Prop
| intro (h : ∀ a b : α, a = b) : subsingleton
protected lemma subsingleton.elim {α : Sort u} [h : subsingleton α] : ∀ (a b : α), a = b :=
subsingleton.rec (λ p, p) h
protected lemma subsingleton.helim {α β : Sort u} [h : subsingleton α] (h : α = β) :
∀ (a : α) (b : β), a == b :=
eq.rec_on h (λ a b : α, heq_of_eq (subsingleton.elim a b))
instance subsingleton_prop (p : Prop) : subsingleton p :=
⟨λ a b, proof_irrel a b⟩
instance (p : Prop) : subsingleton (decidable p) :=
subsingleton.intro (λ d₁,
match d₁ with
| (is_true t₁) := (λ d₂,
match d₂ with
| (is_true t₂) := eq.rec_on (proof_irrel t₁ t₂) rfl
| (is_false f₂) := absurd t₁ f₂
end)
| (is_false f₁) := (λ d₂,
match d₂ with
| (is_true t₂) := absurd t₂ f₁
| (is_false f₂) := eq.rec_on (proof_irrel f₁ f₂) rfl
end)
end)
protected lemma rec_subsingleton {p : Prop} [h : decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u}
[h₃ : Π (h : p), subsingleton (h₁ h)] [h₄ : Π (h : ¬p), subsingleton (h₂ h)]
: subsingleton (decidable.rec_on h h₂ h₁) :=
match h with
| (is_true h) := h₃ h
| (is_false h) := h₄ h
end
lemma if_pos {c : Prop} [h : decidable c] (hc : c) {α : Sort u} {t e : α} : (ite c t e) = t :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := absurd hc hnc
end
lemma if_neg {c : Prop} [h : decidable c] (hnc : ¬c) {α : Sort u} {t e : α} : (ite c t e) = e :=
match h with
| (is_true hc) := absurd hc hnc
| (is_false hnc) := rfl
end
@[simp]
lemma if_t_t (c : Prop) [h : decidable c] {α : Sort u} (t : α) : (ite c t t) = t :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := rfl
end
lemma implies_of_if_pos {c t e : Prop} [decidable c] (h : ite c t e) : c → t :=
assume hc, eq.rec_on (if_pos hc : ite c t e = t) h
lemma implies_of_if_neg {c t e : Prop} [decidable c] (h : ite c t e) : ¬c → e :=
assume hnc, eq.rec_on (if_neg hnc : ite c t e = e) h
lemma if_ctx_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x y u v : α}
(h_c : b ↔ c) (h_t : c → x = u) (h_e : ¬c → y = v) :
ite b x y = ite c u v :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
lemma if_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x y u v : α}
(h_c : b ↔ c) (h_t : x = u) (h_e : y = v) :
ite b x y = ite c u v :=
@if_ctx_congr α b c dec_b dec_c x y u v h_c (λ h, h_t) (λ h, h_e)
@[simp]
lemma if_true {α : Sort u} {h : decidable true} (t e : α) : (@ite α true h t e) = t :=
if_pos trivial
@[simp]
lemma if_false {α : Sort u} {h : decidable false} (t e : α) : (@ite α false h t e) = e :=
if_neg not_false
lemma if_ctx_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c]
(h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) :
ite b x y ↔ ite c u v :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
@[congr]
lemma if_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c]
(h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) :
ite b x y ↔ ite c u v :=
if_ctx_congr_prop h_c (λ h, h_t) (λ h, h_e)
lemma if_ctx_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b]
(h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) :
ite b x y ↔ (@ite Prop c (decidable_of_decidable_of_iff dec_b h_c) u v) :=
@if_ctx_congr_prop b c x y u v dec_b (decidable_of_decidable_of_iff dec_b h_c) h_c h_t h_e
@[congr]
lemma if_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b]
(h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) :
ite b x y ↔ (@ite Prop c (decidable_of_decidable_of_iff dec_b h_c) u v) :=
@if_ctx_simp_congr_prop b c x y u v dec_b h_c (λ h, h_t) (λ h, h_e)
@[simp] lemma dif_pos {c : Prop} [h : decidable c] (hc : c) {α : Sort u} {t : c → α} {e : ¬ c → α} : dite c t e = t hc :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := absurd hc hnc
end
@[simp] lemma dif_neg {c : Prop} [h : decidable c] (hnc : ¬c) {α : Sort u} {t : c → α} {e : ¬ c → α} : dite c t e = e hnc :=
match h with
| (is_true hc) := absurd hc hnc
| (is_false hnc) := rfl
end
@[congr]
lemma dif_ctx_congr {α : Sort u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c]
{x : b → α} {u : c → α} {y : ¬b → α} {v : ¬c → α}
(h_c : b ↔ c)
(h_t : ∀ (h : c), x (iff.mpr h_c h) = u h)
(h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) :
(@dite α b dec_b x y) = (@dite α c dec_c u v) :=
match dec_b, dec_c with
| (is_false h₁), (is_false h₂) := h_e h₂
| (is_true h₁), (is_true h₂) := h_t h₂
| (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁)
| (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂)
end
lemma dif_ctx_simp_congr {α : Sort u} {b c : Prop} [dec_b : decidable b]
{x : b → α} {u : c → α} {y : ¬b → α} {v : ¬c → α}
(h_c : b ↔ c)
(h_t : ∀ (h : c), x (iff.mpr h_c h) = u h)
(h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) :
(@dite α b dec_b x y) = (@dite α c (decidable_of_decidable_of_iff dec_b h_c) u v) :=
@dif_ctx_congr α b c dec_b (decidable_of_decidable_of_iff dec_b h_c) x u y v h_c h_t h_e
-- Remark: dite and ite are "defally equal" when we ignore the proofs.
lemma dif_eq_if (c : Prop) [h : decidable c] {α : Sort u} (t : α) (e : α) : dite c (λ h, t) (λ h, e) = ite c t e :=
match h with
| (is_true hc) := rfl
| (is_false hnc) := rfl
end
instance {c t e : Prop} [d_c : decidable c] [d_t : decidable t] [d_e : decidable e] : decidable (if c then t else e) :=
match d_c with
| (is_true hc) := d_t
| (is_false hc) := d_e
end
instance {c : Prop} {t : c → Prop} {e : ¬c → Prop} [d_c : decidable c] [d_t : ∀ h, decidable (t h)] [d_e : ∀ h, decidable (e h)] : decidable (if h : c then t h else e h) :=
match d_c with
| (is_true hc) := d_t hc
| (is_false hc) := d_e hc
end
def as_true (c : Prop) [decidable c] : Prop :=
if c then true else false
def as_false (c : Prop) [decidable c] : Prop :=
if c then false else true
lemma of_as_true {c : Prop} [h₁ : decidable c] (h₂ : as_true c) : c :=
match h₁, h₂ with
| (is_true h_c), h₂ := h_c
| (is_false h_c), h₂ := false.elim h₂
end
/-- Universe lifting operation -/
structure {r s} ulift (α : Type s) : Type (max s r) :=
up :: (down : α)
namespace ulift
/- Bijection between α and ulift.{v} α -/
lemma up_down {α : Type u} : ∀ (b : ulift.{v} α), up (down b) = b
| (up a) := rfl
lemma down_up {α : Type u} (a : α) : down (up.{v} a) = a := rfl
end ulift
/-- Universe lifting operation from Sort to Type -/
structure plift (α : Sort u) : Type u :=
up :: (down : α)
namespace plift
/- Bijection between α and plift α -/
lemma up_down {α : Sort u} : ∀ (b : plift α), up (down b) = b
| (up a) := rfl
lemma down_up {α : Sort u} (a : α) : down (up a) = a := rfl
end plift
/- Equalities for rewriting let-expressions -/
lemma let_value_eq {α : Sort u} {β : Sort v} {a₁ a₂ : α} (b : α → β) :
a₁ = a₂ → (let x : α := a₁ in b x) = (let x : α := a₂ in b x) :=
λ h, eq.rec_on h rfl
lemma let_value_heq {α : Sort v} {β : α → Sort u} {a₁ a₂ : α} (b : Π x : α, β x) :
a₁ = a₂ → (let x : α := a₁ in b x) == (let x : α := a₂ in b x) :=
λ h, eq.rec_on h (heq.refl (b a₁))
lemma let_body_eq {α : Sort v} {β : α → Sort u} (a : α) {b₁ b₂ : Π x : α, β x} :
(∀ x, b₁ x = b₂ x) → (let x : α := a in b₁ x) = (let x : α := a in b₂ x) :=
λ h, h a
lemma let_eq {α : Sort v} {β : Sort u} {a₁ a₂ : α} {b₁ b₂ : α → β} :
a₁ = a₂ → (∀ x, b₁ x = b₂ x) → (let x : α := a₁ in b₁ x) = (let x : α := a₂ in b₂ x) :=
λ h₁ h₂, eq.rec_on h₁ (h₂ a₁)
section relation
variables {α : Sort u} {β : Sort v} (r : β → β → Prop)
local infix `≺`:50 := r
def reflexive := ∀ x, x ≺ x
def symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x
def transitive := ∀ ⦃x y z⦄, x ≺ y → y ≺ z → x ≺ z
def equivalence := reflexive r ∧ symmetric r ∧ transitive r
def total := ∀ x y, x ≺ y ∨ y ≺ x
lemma mk_equivalence (rfl : reflexive r) (symm : symmetric r) (trans : transitive r) :
equivalence r :=
⟨rfl, symm, trans⟩
def irreflexive := ∀ x, ¬ x ≺ x
def anti_symmetric := ∀ ⦃x y⦄, x ≺ y → y ≺ x → x = y
def empty_relation := λ a₁ a₂ : α, false
def subrelation (q r : β → β → Prop) := ∀ ⦃x y⦄, q x y → r x y
def inv_image (f : α → β) : α → α → Prop :=
λ a₁ a₂, f a₁ ≺ f a₂
lemma inv_image.trans (f : α → β) (h : transitive r) : transitive (inv_image r f) :=
λ (a₁ a₂ a₃ : α) (h₁ : inv_image r f a₁ a₂) (h₂ : inv_image r f a₂ a₃), h h₁ h₂
lemma inv_image.irreflexive (f : α → β) (h : irreflexive r) : irreflexive (inv_image r f) :=
λ (a : α) (h₁ : inv_image r f a a), h (f a) h₁
end relation
section binary
variables {α : Type u} {β : Type v}
variable f : α → α → α
variable inv : α → α
variable one : α
local notation (name := f) a * b := f a b
local notation (name := inv) a ⁻¹ := inv a
variable g : α → α → α
local notation (name := g) a + b := g a b
def commutative := ∀ a b, a * b = b * a
def associative := ∀ a b c, (a * b) * c = a * (b * c)
def left_identity := ∀ a, one * a = a
def right_identity := ∀ a, a * one = a
def right_inverse := ∀ a, a * a⁻¹ = one
def left_cancelative := ∀ a b c, a * b = a * c → b = c
def right_cancelative := ∀ a b c, a * b = c * b → a = c
def left_distributive := ∀ a b c, a * (b + c) = a * b + a * c
def right_distributive := ∀ a b c, (a + b) * c = a * c + b * c
def right_commutative (h : β → α → β) := ∀ b a₁ a₂, h (h b a₁) a₂ = h (h b a₂) a₁
def left_commutative (h : α → β → β) := ∀ a₁ a₂ b, h a₁ (h a₂ b) = h a₂ (h a₁ b)
lemma left_comm : commutative f → associative f → left_commutative f :=
assume hcomm hassoc, assume a b c, calc
a*(b*c) = (a*b)*c : eq.symm (hassoc a b c)
... = (b*a)*c : hcomm a b ▸ rfl
... = b*(a*c) : hassoc b a c
lemma right_comm : commutative f → associative f → right_commutative f :=
assume hcomm hassoc, assume a b c, calc
(a*b)*c = a*(b*c) : hassoc a b c
... = a*(c*b) : hcomm b c ▸ rfl
... = (a*c)*b : eq.symm (hassoc a c b)
end binary
|
8b546166e4bc4559ce4fdca4f5fee28789df7eac | 97f752b44fd85ec3f635078a2dd125ddae7a82b6 | /hott/hit/pushout.hlean | 4d99b9d2c07aa943dbed06414fb61084df6042d6 | [
"Apache-2.0"
] | permissive | tectronics/lean | ab977ba6be0fcd46047ddbb3c8e16e7c26710701 | f38af35e0616f89c6e9d7e3eb1d48e47ee666efe | refs/heads/master | 1,532,358,526,384 | 1,456,276,623,000 | 1,456,276,623,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,617 | 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
Declaration of the pushout
-/
import .quotient cubical.square types.sigma
open quotient eq sum equiv equiv.ops is_trunc
namespace pushout
section
parameters {TL BL TR : Type} (f : TL → BL) (g : TL → TR)
local abbreviation A := BL + TR
inductive pushout_rel : A → A → Type :=
| Rmk : Π(x : TL), pushout_rel (inl (f x)) (inr (g x))
open pushout_rel
local abbreviation R := pushout_rel
definition pushout : Type := quotient R -- TODO: define this in root namespace
parameters {f g}
definition inl (x : BL) : pushout :=
class_of R (inl x)
definition inr (x : TR) : pushout :=
class_of R (inr x)
definition glue (x : TL) : inl (f x) = inr (g x) :=
eq_of_rel pushout_rel (Rmk f g x)
protected definition rec {P : pushout → Type} (Pinl : Π(x : BL), P (inl x))
(Pinr : Π(x : TR), P (inr x)) (Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x))
(y : pushout) : P y :=
begin
induction y,
{ cases a,
apply Pinl,
apply Pinr},
{ cases H, apply Pglue}
end
protected definition rec_on [reducible] {P : pushout → Type} (y : pushout)
(Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x))
(Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x)) : P y :=
rec Pinl Pinr Pglue y
theorem rec_glue {P : pushout → Type} (Pinl : Π(x : BL), P (inl x))
(Pinr : Π(x : TR), P (inr x)) (Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x))
(x : TL) : apdo (rec Pinl Pinr Pglue) (glue x) = Pglue x :=
!rec_eq_of_rel
protected definition elim {P : Type} (Pinl : BL → P) (Pinr : TR → P)
(Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) (y : pushout) : P :=
rec Pinl Pinr (λx, pathover_of_eq (Pglue x)) y
protected definition elim_on [reducible] {P : Type} (y : pushout) (Pinl : BL → P)
(Pinr : TR → P) (Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) : P :=
elim Pinl Pinr Pglue y
theorem elim_glue {P : Type} (Pinl : BL → P) (Pinr : TR → P)
(Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) (x : TL)
: ap (elim Pinl Pinr Pglue) (glue x) = Pglue x :=
begin
apply eq_of_fn_eq_fn_inv !(pathover_constant (glue x)),
rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑pushout.elim,rec_glue],
end
protected definition elim_type (Pinl : BL → Type) (Pinr : TR → Type)
(Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) (y : pushout) : Type :=
elim Pinl Pinr (λx, ua (Pglue x)) y
protected definition elim_type_on [reducible] (y : pushout) (Pinl : BL → Type)
(Pinr : TR → Type) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) : Type :=
elim_type Pinl Pinr Pglue y
theorem elim_type_glue (Pinl : BL → Type) (Pinr : TR → Type)
(Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) (x : TL)
: transport (elim_type Pinl Pinr Pglue) (glue x) = Pglue x :=
by rewrite [tr_eq_cast_ap_fn,↑elim_type,elim_glue];apply cast_ua_fn
protected definition rec_prop {P : pushout → Type} [H : Πx, is_prop (P x)]
(Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x)) (y : pushout) :=
rec Pinl Pinr (λx, !is_prop.elimo) y
protected definition elim_prop {P : Type} [H : is_prop P] (Pinl : BL → P) (Pinr : TR → P)
(y : pushout) : P :=
elim Pinl Pinr (λa, !is_prop.elim) y
end
end pushout
attribute pushout.inl pushout.inr [constructor]
attribute pushout.rec pushout.elim [unfold 10] [recursor 10]
attribute pushout.elim_type [unfold 9]
attribute pushout.rec_on pushout.elim_on [unfold 7]
attribute pushout.elim_type_on [unfold 6]
open sigma
namespace pushout
variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR)
/- The non-dependent universal property -/
definition pushout_arrow_equiv (C : Type)
: (pushout f g → C) ≃ (Σ(i : BL → C) (j : TR → C), Πc, i (f c) = j (g c)) :=
begin
fapply equiv.MK,
{ intro f, exact ⟨λx, f (inl x), λx, f (inr x), λx, ap f (glue x)⟩},
{ intro v x, induction v with i w, induction w with j p, induction x,
exact (i a), exact (j a), exact (p x)},
{ intro v, induction v with i w, induction w with j p, esimp,
apply ap (λp, ⟨i, j, p⟩), apply eq_of_homotopy, intro x, apply elim_glue},
{ intro f, apply eq_of_homotopy, intro x, induction x: esimp,
apply eq_pathover, apply hdeg_square, esimp, apply elim_glue},
end
end pushout
open function sigma.ops
namespace pushout
/- The flattening lemma -/
section
universe variable u
parameters {TL BL TR : Type} (f : TL → BL) (g : TL → TR)
(Pinl : BL → Type.{u}) (Pinr : TR → Type.{u})
(Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x))
include Pglue
local abbreviation A := BL + TR
local abbreviation R : A → A → Type := pushout_rel f g
local abbreviation P [unfold 5] := pushout.elim_type Pinl Pinr Pglue
local abbreviation F : sigma (Pinl ∘ f) → sigma Pinl :=
λz, ⟨ f z.1 , z.2 ⟩
local abbreviation G : sigma (Pinl ∘ f) → sigma Pinr :=
λz, ⟨ g z.1 , Pglue z.1 z.2 ⟩
local abbreviation Pglue' : Π ⦃a a' : A⦄,
R a a' → sum.rec Pinl Pinr a ≃ sum.rec Pinl Pinr a' :=
@pushout_rel.rec TL BL TR f g
(λ ⦃a a' ⦄ (r : R a a'),
(sum.rec Pinl Pinr a) ≃ (sum.rec Pinl Pinr a')) Pglue
protected definition flattening : sigma P ≃ pushout F G :=
begin
assert H : Πz, P z ≃ quotient.elim_type (sum.rec Pinl Pinr) Pglue' z,
{ intro z, apply equiv_of_eq,
assert H1 : pushout.elim_type Pinl Pinr Pglue
= quotient.elim_type (sum.rec Pinl Pinr) Pglue',
{ change
quotient.rec (sum.rec Pinl Pinr)
(λa a' r, pushout_rel.cases_on r (λx, pathover_of_eq (ua (Pglue x))))
= quotient.rec (sum.rec Pinl Pinr)
(λa a' r, pathover_of_eq (ua (pushout_rel.cases_on r Pglue))),
assert H2 : Π⦃a a'⦄ r : pushout_rel f g a a',
pushout_rel.cases_on r (λx, pathover_of_eq (ua (Pglue x)))
= pathover_of_eq (ua (pushout_rel.cases_on r Pglue))
:> sum.rec Pinl Pinr a =[eq_of_rel (pushout_rel f g) r]
sum.rec Pinl Pinr a',
{ intros a a' r, cases r, reflexivity },
rewrite (eq_of_homotopy3 H2) },
apply ap10 H1 },
apply equiv.trans (sigma_equiv_sigma_right H),
apply equiv.trans (quotient.flattening.flattening_lemma R (sum.rec Pinl Pinr) Pglue'),
fapply equiv.MK,
{ intro q, induction q with z z z' fr,
{ induction z with a p, induction a with x x,
{ exact inl ⟨x, p⟩ },
{ exact inr ⟨x, p⟩ } },
{ induction fr with a a' r p, induction r with x,
exact glue ⟨x, p⟩ } },
{ intro q, induction q with xp xp xp,
{ exact class_of _ ⟨sum.inl xp.1, xp.2⟩ },
{ exact class_of _ ⟨sum.inr xp.1, xp.2⟩ },
{ apply eq_of_rel, constructor } },
{ intro q, induction q with xp xp xp: induction xp with x p,
{ apply ap inl, reflexivity },
{ apply ap inr, reflexivity },
{ unfold F, unfold G, apply eq_pathover,
rewrite [ap_id,ap_compose' (quotient.elim _ _)],
krewrite elim_glue, krewrite elim_eq_of_rel, apply hrefl } },
{ intro q, induction q with z z z' fr,
{ induction z with a p, induction a with x x,
{ reflexivity },
{ reflexivity } },
{ induction fr with a a' r p, induction r with x,
esimp, apply eq_pathover,
rewrite [ap_id,ap_compose' (pushout.elim _ _ _)],
krewrite elim_eq_of_rel, krewrite elim_glue, apply hrefl } }
end
end
-- Commutativity of pushouts
section
variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR)
protected definition transpose [constructor] : pushout f g → pushout g f :=
begin
intro x, induction x, apply inr a, apply inl a, apply !glue⁻¹
end
--TODO prove without krewrite?
protected definition transpose_involutive (x : pushout f g) :
pushout.transpose g f (pushout.transpose f g x) = x :=
begin
induction x, apply idp, apply idp,
apply eq_pathover, refine _ ⬝hp !ap_id⁻¹,
refine !(ap_compose (pushout.transpose _ _)) ⬝ph _, esimp[pushout.transpose],
krewrite [elim_glue, ap_inv, elim_glue, inv_inv], apply hrfl
end
protected definition symm : pushout f g ≃ pushout g f :=
begin
fapply equiv.MK, do 2 exact !pushout.transpose,
do 2 (intro x; apply pushout.transpose_involutive),
end
end
end pushout
|
e5b4815b3ce8395516a35c4dfc0872fbee7e8f4a | 8eeb99d0fdf8125f5d39a0ce8631653f588ee817 | /src/topology/uniform_space/cauchy.lean | 46ecf007939e85599955ac0be62c42b415d27d7c | [
"Apache-2.0"
] | permissive | jesse-michael-han/mathlib | a15c58378846011b003669354cbab7062b893cfe | fa6312e4dc971985e6b7708d99a5bc3062485c89 | refs/heads/master | 1,625,200,760,912 | 1,602,081,753,000 | 1,602,081,753,000 | 181,787,230 | 0 | 0 | null | 1,555,460,682,000 | 1,555,460,682,000 | null | UTF-8 | Lean | false | false | 26,223 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import topology.uniform_space.basic
import topology.bases
import data.set.intervals
/-!
# Theory of Cauchy filters in uniform spaces. Complete uniform spaces. Totally bounded subsets.
-/
universes u v
open filter topological_space set classical uniform_space
open_locale classical uniformity topological_space filter
variables {α : Type u} {β : Type v} [uniform_space α]
/-- A filter `f` is Cauchy if for every entourage `r`, there exists an
`s ∈ f` such that `s × s ⊆ r`. This is a generalization of Cauchy
sequences, because if `a : ℕ → α` then the filter of sets containing
cofinitely many of the `a n` is Cauchy iff `a` is a Cauchy sequence. -/
def cauchy (f : filter α) := ne_bot f ∧ f ×ᶠ f ≤ (𝓤 α)
/-- A set `s` is called *complete*, if any Cauchy filter `f` such that `s ∈ f`
has a limit in `s` (formally, it satisfies `f ≤ 𝓝 x` for some `x ∈ s`). -/
def is_complete (s : set α) := ∀f, cauchy f → f ≤ 𝓟 s → ∃x∈s, f ≤ 𝓝 x
lemma filter.has_basis.cauchy_iff {p : β → Prop} {s : β → set (α × α)} (h : (𝓤 α).has_basis p s)
{f : filter α} :
cauchy f ↔ (ne_bot f ∧ (∀ i, p i → ∃ t ∈ f, ∀ x y ∈ t, (x, y) ∈ s i)) :=
and_congr iff.rfl $ (f.basis_sets.prod_self.le_basis_iff h).trans $
by simp only [subset_def, prod.forall, mem_prod_eq, and_imp, id]
lemma cauchy_iff' {f : filter α} :
cauchy f ↔ (ne_bot f ∧ (∀ s ∈ 𝓤 α, ∃t∈f, ∀ x y ∈ t, (x, y) ∈ s)) :=
(𝓤 α).basis_sets.cauchy_iff
lemma cauchy_iff {f : filter α} :
cauchy f ↔ (ne_bot f ∧ (∀ s ∈ 𝓤 α, ∃t∈f, (set.prod t t) ⊆ s)) :=
(𝓤 α).basis_sets.cauchy_iff.trans $
by simp only [subset_def, prod.forall, mem_prod_eq, and_imp, id]
lemma cauchy_map_iff {l : filter β} {f : β → α} :
cauchy (l.map f) ↔ (ne_bot l ∧ tendsto (λp:β×β, (f p.1, f p.2)) (l ×ᶠ l) (𝓤 α)) :=
by rw [cauchy, map_ne_bot_iff, prod_map_map_eq, tendsto]
lemma cauchy_map_iff' {l : filter β} [hl : ne_bot l] {f : β → α} :
cauchy (l.map f) ↔ tendsto (λp:β×β, (f p.1, f p.2)) (l ×ᶠ l) (𝓤 α) :=
cauchy_map_iff.trans $ and_iff_right hl
lemma cauchy.mono {f g : filter α} [hg : ne_bot g] (h_c : cauchy f) (h_le : g ≤ f) : cauchy g :=
⟨hg, le_trans (filter.prod_mono h_le h_le) h_c.right⟩
lemma cauchy.mono' {f g : filter α} (h_c : cauchy f) (hg : ne_bot g) (h_le : g ≤ f) : cauchy g :=
h_c.mono h_le
lemma cauchy_nhds {a : α} : cauchy (𝓝 a) :=
⟨nhds_ne_bot,
calc 𝓝 a ×ᶠ 𝓝 a =
(𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set(α×α),
set.prod {y : α | (y, a) ∈ s} {y : α | (a, y) ∈ t})) : nhds_nhds_eq_uniformity_uniformity_prod
... ≤ (𝓤 α).lift' (λs:set (α×α), comp_rel s s) :
le_infi $ assume s, le_infi $ assume hs,
infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le_of_le hs $
principal_mono.mpr $
assume ⟨x, y⟩ ⟨(hx : (x, a) ∈ s), (hy : (a, y) ∈ s)⟩, ⟨a, hx, hy⟩
... ≤ 𝓤 α : comp_le_uniformity⟩
lemma cauchy_pure {a : α} : cauchy (pure a) :=
cauchy_nhds.mono (pure_le_nhds a)
lemma filter.tendsto.cauchy_map {l : filter β} [ne_bot l] {f : β → α} {a : α}
(h : tendsto f l (𝓝 a)) :
cauchy (map f l) :=
cauchy_nhds.mono h
/-- The common part of the proofs of `le_nhds_of_cauchy_adhp` and
`sequentially_complete.le_nhds_of_seq_tendsto_nhds`: if for any entourage `s`
one can choose a set `t ∈ f` of diameter `s` such that it contains a point `y`
with `(x, y) ∈ s`, then `f` converges to `x`. -/
lemma le_nhds_of_cauchy_adhp_aux {f : filter α} {x : α}
(adhs : ∀ s ∈ 𝓤 α, ∃ t ∈ f, (set.prod t t ⊆ s) ∧ ∃ y, (x, y) ∈ s ∧ y ∈ t) :
f ≤ 𝓝 x :=
begin
-- Consider a neighborhood `s` of `x`
assume s hs,
-- Take an entourage twice smaller than `s`
rcases comp_mem_uniformity_sets (mem_nhds_uniformity_iff_right.1 hs) with ⟨U, U_mem, hU⟩,
-- Take a set `t ∈ f`, `t × t ⊆ U`, and a point `y ∈ t` such that `(x, y) ∈ U`
rcases adhs U U_mem with ⟨t, t_mem, ht, y, hxy, hy⟩,
apply mem_sets_of_superset t_mem,
-- Given a point `z ∈ t`, we have `(x, y) ∈ U` and `(y, z) ∈ t × t ⊆ U`, hence `z ∈ s`
exact (λ z hz, hU (prod_mk_mem_comp_rel hxy (ht $ mk_mem_prod hy hz)) rfl)
end
/-- If `x` is an adherent (cluster) point for a Cauchy filter `f`, then it is a limit point
for `f`. -/
lemma le_nhds_of_cauchy_adhp {f : filter α} {x : α} (hf : cauchy f)
(adhs : cluster_pt x f) : f ≤ 𝓝 x :=
le_nhds_of_cauchy_adhp_aux
begin
assume s hs,
obtain ⟨t, t_mem, ht⟩ : ∃ t ∈ f, set.prod t t ⊆ s,
from (cauchy_iff.1 hf).2 s hs,
use [t, t_mem, ht],
exact (forall_sets_nonempty_iff_ne_bot.2 adhs _
(inter_mem_inf_sets (mem_nhds_left x hs) t_mem ))
end
lemma le_nhds_iff_adhp_of_cauchy {f : filter α} {x : α} (hf : cauchy f) :
f ≤ 𝓝 x ↔ cluster_pt x f :=
⟨assume h, cluster_pt.of_le_nhds' h hf.1, le_nhds_of_cauchy_adhp hf⟩
lemma cauchy.map [uniform_space β] {f : filter α} {m : α → β}
(hf : cauchy f) (hm : uniform_continuous m) : cauchy (map m f) :=
⟨hf.1.map _,
calc map m f ×ᶠ map m f = map (λp:α×α, (m p.1, m p.2)) (f ×ᶠ f) : filter.prod_map_map_eq
... ≤ map (λp:α×α, (m p.1, m p.2)) (𝓤 α) : map_mono hf.right
... ≤ 𝓤 β : hm⟩
lemma cauchy.comap [uniform_space β] {f : filter β} {m : α → β}
(hf : cauchy f) (hm : comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) ≤ 𝓤 α)
[ne_bot (comap m f)] : cauchy (comap m f) :=
⟨‹_›,
calc comap m f ×ᶠ comap m f = comap (λp:α×α, (m p.1, m p.2)) (f ×ᶠ f) : filter.prod_comap_comap_eq
... ≤ comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) : comap_mono hf.right
... ≤ 𝓤 α : hm⟩
lemma cauchy.comap' [uniform_space β] {f : filter β} {m : α → β}
(hf : cauchy f) (hm : comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) ≤ 𝓤 α)
(hb : ne_bot (comap m f)) : cauchy (comap m f) :=
hf.comap hm
/-- Cauchy sequences. Usually defined on ℕ, but often it is also useful to say that a function
defined on ℝ is Cauchy at +∞ to deduce convergence. Therefore, we define it in a type class that
is general enough to cover both ℕ and ℝ, which are the main motivating examples. -/
def cauchy_seq [semilattice_sup β] (u : β → α) := cauchy (at_top.map u)
lemma cauchy_seq.mem_entourage {ι : Type*} [nonempty ι] [decidable_linear_order ι] {u : ι → α}
(h : cauchy_seq u) {V : set (α × α)} (hV : V ∈ 𝓤 α) :
∃ k₀, ∀ i j, k₀ ≤ i → k₀ ≤ j → (u i, u j) ∈ V :=
begin
have := h.right hV,
obtain ⟨⟨i₀, j₀⟩, H⟩ : ∃ a, ∀ b : ι × ι, b ≥ a → prod.map u u b ∈ V,
by rwa [prod_map_at_top_eq, mem_map, mem_at_top_sets] at this,
refine ⟨max i₀ j₀, _⟩,
intros i j hi hj,
exact H (i, j) ⟨le_of_max_le_left hi, le_of_max_le_right hj⟩,
end
lemma filter.tendsto.cauchy_seq [semilattice_sup β] [nonempty β] {f : β → α} {x}
(hx : tendsto f at_top (𝓝 x)) :
cauchy_seq f :=
hx.cauchy_map
lemma cauchy_seq_iff_tendsto [nonempty β] [semilattice_sup β] {u : β → α} :
cauchy_seq u ↔ tendsto (prod.map u u) at_top (𝓤 α) :=
cauchy_map_iff'.trans $ by simp only [prod_at_top_at_top_eq, prod.map_def]
/-- If a Cauchy sequence has a convergent subsequence, then it converges. -/
lemma tendsto_nhds_of_cauchy_seq_of_subseq
[semilattice_sup β] {u : β → α} (hu : cauchy_seq u)
{ι : Type*} {f : ι → β} {p : filter ι} [ne_bot p]
(hf : tendsto f p at_top) {a : α} (ha : tendsto (u ∘ f) p (𝓝 a)) :
tendsto u at_top (𝓝 a) :=
le_nhds_of_cauchy_adhp hu (map_cluster_pt_of_comp hf ha)
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma filter.has_basis.cauchy_seq_iff {γ} [nonempty β] [semilattice_sup β] {u : β → α}
{p : γ → Prop} {s : γ → set (α × α)} (h : (𝓤 α).has_basis p s) :
cauchy_seq u ↔ ∀ i, p i → ∃N, ∀m n≥N, (u m, u n) ∈ s i :=
begin
rw [cauchy_seq_iff_tendsto, ← prod_at_top_at_top_eq],
refine (at_top_basis.prod_self.tendsto_iff h).trans _,
simp only [exists_prop, true_and, maps_to, preimage, subset_def, prod.forall,
mem_prod_eq, mem_set_of_eq, mem_Ici, and_imp, prod.map]
end
lemma filter.has_basis.cauchy_seq_iff' {γ} [nonempty β] [semilattice_sup β] {u : β → α}
{p : γ → Prop} {s : γ → set (α × α)} (H : (𝓤 α).has_basis p s) :
cauchy_seq u ↔ ∀ i, p i → ∃N, ∀n≥N, (u n, u N) ∈ s i :=
begin
refine H.cauchy_seq_iff.trans ⟨λ h i hi, _, λ h i hi, _⟩,
{ exact (h i hi).imp (λ N hN n hn, hN n N hn (le_refl N)) },
{ rcases comp_symm_of_uniformity (H.mem_of_mem hi) with ⟨t, ht, ht', hts⟩,
rcases H.mem_iff.1 ht with ⟨j, hj, hjt⟩,
refine (h j hj).imp (λ N hN m n hm hn, hts ⟨u N, hjt _, ht' $ hjt _⟩),
{ exact hN m hm },
{ exact hN n hn } }
end
lemma cauchy_seq_of_controlled [semilattice_sup β] [nonempty β]
(U : β → set (α × α)) (hU : ∀ s ∈ 𝓤 α, ∃ n, U n ⊆ s)
{f : β → α} (hf : ∀ {N m n : β}, N ≤ m → N ≤ n → (f m, f n) ∈ U N) :
cauchy_seq f :=
cauchy_seq_iff_tendsto.2
begin
assume s hs,
rw [mem_map, mem_at_top_sets],
cases hU s hs with N hN,
refine ⟨(N, N), λ mn hmn, _⟩,
cases mn with m n,
exact hN (hf hmn.1 hmn.2)
end
/-- A complete space is defined here using uniformities. A uniform space
is complete if every Cauchy filter converges. -/
class complete_space (α : Type u) [uniform_space α] : Prop :=
(complete : ∀{f:filter α}, cauchy f → ∃x, f ≤ 𝓝 x)
lemma complete_univ {α : Type u} [uniform_space α] [complete_space α] :
is_complete (univ : set α) :=
begin
assume f hf _,
rcases complete_space.complete hf with ⟨x, hx⟩,
exact ⟨x, mem_univ x, hx⟩
end
lemma cauchy_prod [uniform_space β] {f : filter α} {g : filter β} :
cauchy f → cauchy g → cauchy (f ×ᶠ g)
| ⟨f_proper, hf⟩ ⟨g_proper, hg⟩ := ⟨filter.prod_ne_bot.2 ⟨f_proper, g_proper⟩,
let p_α := λp:(α×β)×(α×β), (p.1.1, p.2.1), p_β := λp:(α×β)×(α×β), (p.1.2, p.2.2) in
suffices (f.prod f).comap p_α ⊓ (g.prod g).comap p_β ≤ (𝓤 α).comap p_α ⊓ (𝓤 β).comap p_β,
by simpa [uniformity_prod, filter.prod, filter.comap_inf, filter.comap_comap, (∘),
inf_assoc, inf_comm, inf_left_comm],
inf_le_inf (filter.comap_mono hf) (filter.comap_mono hg)⟩
instance complete_space.prod [uniform_space β] [complete_space α] [complete_space β] :
complete_space (α × β) :=
{ complete := λ f hf,
let ⟨x1, hx1⟩ := complete_space.complete $ hf.map uniform_continuous_fst in
let ⟨x2, hx2⟩ := complete_space.complete $ hf.map uniform_continuous_snd in
⟨(x1, x2), by rw [nhds_prod_eq, filter.prod_def];
from filter.le_lift (λ s hs, filter.le_lift' $ λ t ht,
have H1 : prod.fst ⁻¹' s ∈ f.sets := hx1 hs,
have H2 : prod.snd ⁻¹' t ∈ f.sets := hx2 ht,
filter.inter_mem_sets H1 H2)⟩ }
/--If `univ` is complete, the space is a complete space -/
lemma complete_space_of_is_complete_univ (h : is_complete (univ : set α)) : complete_space α :=
⟨λ f hf, let ⟨x, _, hx⟩ := h f hf ((@principal_univ α).symm ▸ le_top) in ⟨x, hx⟩⟩
lemma complete_space_iff_is_complete_univ :
complete_space α ↔ is_complete (univ : set α) :=
⟨@complete_univ α _, complete_space_of_is_complete_univ⟩
lemma cauchy_iff_exists_le_nhds [complete_space α] {l : filter α} [ne_bot l] :
cauchy l ↔ (∃x, l ≤ 𝓝 x) :=
⟨complete_space.complete, assume ⟨x, hx⟩, cauchy_nhds.mono hx⟩
lemma cauchy_map_iff_exists_tendsto [complete_space α] {l : filter β} {f : β → α} [ne_bot l] :
cauchy (l.map f) ↔ (∃x, tendsto f l (𝓝 x)) :=
cauchy_iff_exists_le_nhds
/-- A Cauchy sequence in a complete space converges -/
theorem cauchy_seq_tendsto_of_complete [semilattice_sup β] [complete_space α]
{u : β → α} (H : cauchy_seq u) : ∃x, tendsto u at_top (𝓝 x) :=
complete_space.complete H
/-- If `K` is a complete subset, then any cauchy sequence in `K` converges to a point in `K` -/
lemma cauchy_seq_tendsto_of_is_complete [semilattice_sup β] {K : set α} (h₁ : is_complete K)
{u : β → α} (h₂ : ∀ n, u n ∈ K) (h₃ : cauchy_seq u) : ∃ v ∈ K, tendsto u at_top (𝓝 v) :=
h₁ _ h₃ $ le_principal_iff.2 $ mem_map_sets_iff.2 ⟨univ, univ_mem_sets,
by { simp only [image_univ], rintros _ ⟨n, rfl⟩, exact h₂ n }⟩
theorem cauchy.le_nhds_Lim [complete_space α] [nonempty α] {f : filter α} (hf : cauchy f) :
f ≤ 𝓝 (Lim f) :=
Lim_spec (complete_space.complete hf)
theorem cauchy_seq.tendsto_lim [semilattice_sup β] [complete_space α] [nonempty α] {u : β → α}
(h : cauchy_seq u) :
tendsto u at_top (𝓝 $ lim at_top u) :=
h.le_nhds_Lim
lemma is_closed.is_complete [complete_space α] {s : set α}
(h : is_closed s) : is_complete s :=
λ f cf fs, let ⟨x, hx⟩ := complete_space.complete cf in
⟨x, is_closed_iff_cluster_pt.mp h x (ne_bot_of_le_ne_bot cf.left (le_inf hx fs)), hx⟩
/-- A set `s` is totally bounded if for every entourage `d` there is a finite
set of points `t` such that every element of `s` is `d`-near to some element of `t`. -/
def totally_bounded (s : set α) : Prop :=
∀d ∈ 𝓤 α, ∃t : set α, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d})
theorem totally_bounded_iff_subset {s : set α} : totally_bounded s ↔
∀d ∈ 𝓤 α, ∃t ⊆ s, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) :=
⟨λ H d hd, begin
rcases comp_symm_of_uniformity hd with ⟨r, hr, rs, rd⟩,
rcases H r hr with ⟨k, fk, ks⟩,
let u := {y ∈ k | ∃ x, x ∈ s ∧ (x, y) ∈ r},
let f : u → α := λ x, classical.some x.2.2,
have : ∀ x : u, f x ∈ s ∧ (f x, x.1) ∈ r := λ x, classical.some_spec x.2.2,
refine ⟨range f, _, _, _⟩,
{ exact range_subset_iff.2 (λ x, (this x).1) },
{ have : finite u := fk.subset (λ x h, h.1),
exact ⟨@set.fintype_range _ _ _ _ this.fintype⟩ },
{ intros x xs,
have := ks xs, simp at this,
rcases this with ⟨y, hy, xy⟩,
let z : coe_sort u := ⟨y, hy, x, xs, xy⟩,
exact mem_bUnion_iff.2 ⟨_, ⟨z, rfl⟩, rd $ mem_comp_rel.2 ⟨_, xy, rs (this z).2⟩⟩ }
end,
λ H d hd, let ⟨t, _, ht⟩ := H d hd in ⟨t, ht⟩⟩
lemma totally_bounded_of_forall_symm {s : set α}
(h : ∀ V ∈ 𝓤 α, symmetric_rel V → ∃ t : set α, finite t ∧ s ⊆ ⋃ y ∈ t, ball y V) :
totally_bounded s :=
begin
intros V V_in,
rcases h _ (symmetrize_mem_uniformity V_in) (symmetric_symmetrize_rel V) with ⟨t, tfin, h⟩,
refine ⟨t, tfin, subset.trans h _⟩,
mono,
intros x x_in z z_in,
exact z_in.right
end
lemma totally_bounded_subset {s₁ s₂ : set α} (hs : s₁ ⊆ s₂)
(h : totally_bounded s₂) : totally_bounded s₁ :=
assume d hd, let ⟨t, ht₁, ht₂⟩ := h d hd in ⟨t, ht₁, subset.trans hs ht₂⟩
lemma totally_bounded_empty : totally_bounded (∅ : set α) :=
λ d hd, ⟨∅, finite_empty, empty_subset _⟩
/-- The closure of a totally bounded set is totally bounded. -/
lemma totally_bounded.closure {s : set α} (h : totally_bounded s) :
totally_bounded (closure s) :=
assume t ht,
let ⟨t', ht', hct', htt'⟩ := mem_uniformity_is_closed ht, ⟨c, hcf, hc⟩ := h t' ht' in
⟨c, hcf,
calc closure s ⊆ closure (⋃ (y : α) (H : y ∈ c), {x : α | (x, y) ∈ t'}) : closure_mono hc
... = _ : is_closed.closure_eq $ is_closed_bUnion hcf $ assume i hi,
continuous_iff_is_closed.mp (continuous_id.prod_mk continuous_const) _ hct'
... ⊆ _ : bUnion_subset $ assume i hi, subset.trans (assume x, @htt' (x, i))
(subset_bUnion_of_mem hi)⟩
/-- The image of a totally bounded set under a unifromly continuous map is totally bounded. -/
lemma totally_bounded.image [uniform_space β] {f : α → β} {s : set α}
(hs : totally_bounded s) (hf : uniform_continuous f) : totally_bounded (f '' s) :=
assume t ht,
have {p:α×α | (f p.1, f p.2) ∈ t} ∈ 𝓤 α,
from hf ht,
let ⟨c, hfc, hct⟩ := hs _ this in
⟨f '' c, hfc.image f,
begin
simp [image_subset_iff],
simp [subset_def] at hct,
intros x hx, simp,
exact hct x hx
end⟩
lemma cauchy_of_totally_bounded_of_ultrafilter {s : set α} {f : filter α}
(hs : totally_bounded s) (hf : is_ultrafilter f) (h : f ≤ 𝓟 s) : cauchy f :=
⟨hf.left, assume t ht,
let ⟨t', ht'₁, ht'_symm, ht'_t⟩ := comp_symm_of_uniformity ht in
let ⟨i, hi, hs_union⟩ := hs t' ht'₁ in
have (⋃y∈i, {x | (x,y) ∈ t'}) ∈ f.sets,
from mem_sets_of_superset (le_principal_iff.mp h) hs_union,
have ∃y∈i, {x | (x,y) ∈ t'} ∈ f.sets,
from mem_of_finite_Union_ultrafilter hf hi this,
let ⟨y, hy, hif⟩ := this in
have set.prod {x | (x,y) ∈ t'} {x | (x,y) ∈ t'} ⊆ comp_rel t' t',
from assume ⟨x₁, x₂⟩ ⟨(h₁ : (x₁, y) ∈ t'), (h₂ : (x₂, y) ∈ t')⟩,
⟨y, h₁, ht'_symm h₂⟩,
(f ×ᶠ f).sets_of_superset (prod_mem_prod hif hif) (subset.trans this ht'_t)⟩
lemma totally_bounded_iff_filter {s : set α} :
totally_bounded s ↔ (∀f, ne_bot f → f ≤ 𝓟 s → ∃c ≤ f, cauchy c) :=
⟨assume : totally_bounded s, assume f hf hs,
⟨ultrafilter_of f, ultrafilter_of_le,
cauchy_of_totally_bounded_of_ultrafilter this
(ultrafilter_ultrafilter_of' hf) (le_trans ultrafilter_of_le hs)⟩,
assume h : ∀f, ne_bot f → f ≤ 𝓟 s → ∃c ≤ f, cauchy c, assume d hd,
classical.by_contradiction $ assume hs,
have hd_cover : ∀{t:set α}, finite t → ¬ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}),
by simpa using hs,
let
f := ⨅t:{t : set α // finite t}, 𝓟 (s \ (⋃y∈t.val, {x | (x,y) ∈ d}))
in
have ne_bot f,
from infi_ne_bot_of_directed'
(assume ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, ⟨⟨t₁ ∪ t₂, ht₁.union ht₂⟩,
principal_mono.mpr $ diff_subset_diff_right $ Union_subset_Union $
assume t, Union_subset_Union_const or.inl,
principal_mono.mpr $ diff_subset_diff_right $ Union_subset_Union $
assume t, Union_subset_Union_const or.inr⟩)
(assume ⟨t, ht⟩, by simp [ne_bot, diff_eq_empty]; exact hd_cover ht),
have f ≤ 𝓟 s, from infi_le_of_le ⟨∅, finite_empty⟩ $ by simp; exact subset.refl s,
let
⟨c, (hc₁ : c ≤ f), (hc₂ : cauchy c)⟩ := h f ‹f ≠ ⊥› this,
⟨m, hm, (hmd : set.prod m m ⊆ d)⟩ := (@mem_prod_same_iff α c d).mp $ hc₂.right hd
in
have c ≤ 𝓟 s, from le_trans ‹c ≤ f› this,
have m ∩ s ∈ c.sets, from inter_mem_sets hm $ le_principal_iff.mp this,
let ⟨y, hym, hys⟩ := hc₂.left.nonempty_of_mem this in
let ys := (⋃y'∈({y}:set α), {x | (x, y') ∈ d}) in
have m ⊆ ys,
from assume y' hy',
show y' ∈ (⋃y'∈({y}:set α), {x | (x, y') ∈ d}),
by simp; exact @hmd (y', y) ⟨hy', hym⟩,
have c ≤ 𝓟 (s \ ys),
from le_trans hc₁ $ infi_le_of_le ⟨{y}, finite_singleton _⟩ $ le_refl _,
have (s \ ys) ∩ (m ∩ s) ∈ c.sets,
from inter_mem_sets (le_principal_iff.mp this) ‹m ∩ s ∈ c.sets›,
have ∅ ∈ c.sets,
from c.sets_of_superset this $ assume x ⟨⟨hxs, hxys⟩, hxm, _⟩, hxys $ ‹m ⊆ ys› hxm,
hc₂.left $ empty_in_sets_eq_bot.mp this⟩
lemma totally_bounded_iff_ultrafilter {s : set α} :
totally_bounded s ↔ (∀f, is_ultrafilter f → f ≤ 𝓟 s → cauchy f) :=
⟨assume hs f, cauchy_of_totally_bounded_of_ultrafilter hs,
assume h, totally_bounded_iff_filter.mpr $ assume f hf hfs,
have cauchy (ultrafilter_of f),
from h (ultrafilter_of f) (ultrafilter_ultrafilter_of' hf) (le_trans ultrafilter_of_le hfs),
⟨ultrafilter_of f, ultrafilter_of_le, this⟩⟩
lemma compact_iff_totally_bounded_complete {s : set α} :
is_compact s ↔ totally_bounded s ∧ is_complete s :=
⟨λ hs, ⟨totally_bounded_iff_ultrafilter.2 (λ f hf1 hf2,
let ⟨x, xs, fx⟩ := compact_iff_ultrafilter_le_nhds.1 hs f hf1 hf2 in
cauchy_nhds.mono' hf1.1 fx),
λ f fc fs,
let ⟨a, as, fa⟩ := @hs f fc.1 fs in
⟨a, as, le_nhds_of_cauchy_adhp fc fa⟩⟩,
λ ⟨ht, hc⟩, compact_iff_ultrafilter_le_nhds.2
(λf hf hfs, hc _ (totally_bounded_iff_ultrafilter.1 ht _ hf hfs) hfs)⟩
@[priority 100] -- see Note [lower instance priority]
instance complete_of_compact {α : Type u} [uniform_space α] [compact_space α] : complete_space α :=
⟨λf hf, by simpa [principal_univ] using (compact_iff_totally_bounded_complete.1 compact_univ).2 f hf⟩
lemma compact_of_totally_bounded_is_closed [complete_space α] {s : set α}
(ht : totally_bounded s) (hc : is_closed s) : is_compact s :=
(@compact_iff_totally_bounded_complete α _ s).2 ⟨ht, hc.is_complete⟩
/-!
### Sequentially complete space
In this section we prove that a uniform space is complete provided that it is sequentially complete
(i.e., any Cauchy sequence converges) and its uniformity filter admits a countable generating set.
In particular, this applies to (e)metric spaces, see the files `topology/metric_space/emetric_space`
and `topology/metric_space/basic`.
More precisely, we assume that there is a sequence of entourages `U_n` such that any other
entourage includes one of `U_n`. Then any Cauchy filter `f` generates a decreasing sequence of
sets `s_n ∈ f` such that `s_n × s_n ⊆ U_n`. Choose a sequence `x_n∈s_n`. It is easy to show
that this is a Cauchy sequence. If this sequence converges to some `a`, then `f ≤ 𝓝 a`. -/
namespace sequentially_complete
variables {f : filter α} (hf : cauchy f) {U : ℕ → set (α × α)}
(U_mem : ∀ n, U n ∈ 𝓤 α) (U_le : ∀ s ∈ 𝓤 α, ∃ n, U n ⊆ s)
open set finset
noncomputable theory
/-- An auxiliary sequence of sets approximating a Cauchy filter. -/
def set_seq_aux (n : ℕ) : {s : set α // ∃ (_ : s ∈ f), s.prod s ⊆ U n } :=
indefinite_description _ $ (cauchy_iff.1 hf).2 (U n) (U_mem n)
/-- Given a Cauchy filter `f` and a sequence `U` of entourages, `set_seq` provides
a sequence of monotonically decreasing sets `s n ∈ f` such that `(s n).prod (s n) ⊆ U`. -/
def set_seq (n : ℕ) : set α := ⋂ m ∈ Iic n, (set_seq_aux hf U_mem m).val
lemma set_seq_mem (n : ℕ) : set_seq hf U_mem n ∈ f :=
Inter_mem_sets (finite_le_nat n) (λ m _, (set_seq_aux hf U_mem m).2.fst)
lemma set_seq_mono ⦃m n : ℕ⦄ (h : m ≤ n) : set_seq hf U_mem n ⊆ set_seq hf U_mem m :=
bInter_subset_bInter_left (λ k hk, le_trans hk h)
lemma set_seq_sub_aux (n : ℕ) : set_seq hf U_mem n ⊆ set_seq_aux hf U_mem n :=
bInter_subset_of_mem right_mem_Iic
lemma set_seq_prod_subset {N m n} (hm : N ≤ m) (hn : N ≤ n) :
(set_seq hf U_mem m).prod (set_seq hf U_mem n) ⊆ U N :=
begin
assume p hp,
refine (set_seq_aux hf U_mem N).2.snd ⟨_, _⟩;
apply set_seq_sub_aux,
exact set_seq_mono hf U_mem hm hp.1,
exact set_seq_mono hf U_mem hn hp.2
end
/-- A sequence of points such that `seq n ∈ set_seq n`. Here `set_seq` is a monotonically
decreasing sequence of sets `set_seq n ∈ f` with diameters controlled by a given sequence
of entourages. -/
def seq (n : ℕ) : α := some $ hf.1.nonempty_of_mem (set_seq_mem hf U_mem n)
lemma seq_mem (n : ℕ) : seq hf U_mem n ∈ set_seq hf U_mem n :=
some_spec $ hf.1.nonempty_of_mem (set_seq_mem hf U_mem n)
lemma seq_pair_mem ⦃N m n : ℕ⦄ (hm : N ≤ m) (hn : N ≤ n) :
(seq hf U_mem m, seq hf U_mem n) ∈ U N :=
set_seq_prod_subset hf U_mem hm hn ⟨seq_mem hf U_mem m, seq_mem hf U_mem n⟩
include U_le
theorem seq_is_cauchy_seq : cauchy_seq $ seq hf U_mem :=
cauchy_seq_of_controlled U U_le $ seq_pair_mem hf U_mem
/-- If the sequence `sequentially_complete.seq` converges to `a`, then `f ≤ 𝓝 a`. -/
theorem le_nhds_of_seq_tendsto_nhds ⦃a : α⦄ (ha : tendsto (seq hf U_mem) at_top (𝓝 a)) :
f ≤ 𝓝 a :=
le_nhds_of_cauchy_adhp_aux
begin
assume s hs,
rcases U_le s hs with ⟨m, hm⟩,
rcases (tendsto_at_top' _ _).1 ha _ (mem_nhds_left a (U_mem m)) with ⟨n, hn⟩,
refine ⟨set_seq hf U_mem (max m n), set_seq_mem hf U_mem _, _,
seq hf U_mem (max m n), _, seq_mem hf U_mem _⟩,
{ have := le_max_left m n,
exact set.subset.trans (set_seq_prod_subset hf U_mem this this) hm },
{ exact hm (hn _ $ le_max_right m n) }
end
end sequentially_complete
namespace uniform_space
open sequentially_complete
variables (H : is_countably_generated (𝓤 α))
include H
/-- A uniform space is complete provided that (a) its uniformity filter has a countable basis;
(b) any sequence satisfying a "controlled" version of the Cauchy condition converges. -/
theorem complete_of_convergent_controlled_sequences (U : ℕ → set (α × α)) (U_mem : ∀ n, U n ∈ 𝓤 α)
(HU : ∀ u : ℕ → α, (∀ N m n, N ≤ m → N ≤ n → (u m, u n) ∈ U N) → ∃ a, tendsto u at_top (𝓝 a)) :
complete_space α :=
begin
rcases H.exists_antimono_seq' with ⟨U', U'_mono, hU'⟩,
have Hmem : ∀ n, U n ∩ U' n ∈ 𝓤 α,
from λ n, inter_mem_sets (U_mem n) (hU'.2 ⟨n, subset.refl _⟩),
refine ⟨λ f hf, (HU (seq hf Hmem) (λ N m n hm hn, _)).imp $
le_nhds_of_seq_tendsto_nhds _ _ (λ s hs, _)⟩,
{ rcases (hU'.1 hs) with ⟨N, hN⟩,
exact ⟨N, subset.trans (inter_subset_right _ _) hN⟩ },
{ exact inter_subset_left _ _ (seq_pair_mem hf Hmem hm hn) }
end
/-- A sequentially complete uniform space with a countable basis of the uniformity filter is
complete. -/
theorem complete_of_cauchy_seq_tendsto
(H' : ∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) :
complete_space α :=
let ⟨U', U'_mono, hU'⟩ := H.exists_antimono_seq' in
complete_of_convergent_controlled_sequences H U' (λ n, hU'.2 ⟨n, subset.refl _⟩)
(λ u hu, H' u $ cauchy_seq_of_controlled U' (λ s hs, hU'.1 hs) hu)
protected lemma first_countable_topology : first_countable_topology α :=
⟨λ a, by { rw nhds_eq_comap_uniformity, exact H.comap (prod.mk a) }⟩
end uniform_space
|
323fb9b1dae6cd1c29e0eb53f8068f7c3c945ee4 | 6329dd15b8fd567a4737f2dacd02bd0e8c4b3ae4 | /src/game/intro.lean | 4da3239e6ef5f01ec30d7d9b275ff49750219517 | [
"Apache-2.0"
] | permissive | agusakov/mathematics_in_lean_game | 76e455a688a8826b05160c16c0490b9e3d39f071 | ad45fd42148f2203b973537adec7e8a48677ba2a | refs/heads/master | 1,666,147,402,274 | 1,592,119,137,000 | 1,592,119,137,000 | 272,111,226 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,419 | lean | /-
# Mathematics in Lean, The Game
## version Beta maybe idk
## By <a href="https://youtu.be/gXlfXirQF3A" title="Plagiarize!!" target="blank">Nikolai Ivanovich Lobachevsky</a>
*Just kidding it's mostly Kevin Buzzard and Mohammad Pedramfar*
# What is this game?
Well, it's not the natural number game. It's much worse in fact.
This game is an attempt to transform the Mathematics in Lean textbook
into something more playable. It's a work in progress.
## Don't play any of the levels past the first one for now
I'm too lazy to figure out how to disable them without deleting.
As usual, blue nodes on the graph are ones that you are ready to enter. Grey nodes you should stay away
from -- a grey node turns blue when *all* nodes above it are complete. Green nodes are completed.
(Actually you can try any level at any time, but you might not know enough to complete it if it's grey).
In this game, you get own version of the natural numbers, called `mynat`, in an interactive
theorem prover called Lean. Your version of the natural numbers satisfies something called
the principle of mathematical induction, and a couple of other things too (Peano's axioms).
Unfortunately, nobody has proved any theorems about these
natural numbers yet! For example, addition will be defined for you,
but nobody has proved that `x + y = y + x` yet. This is your job. You're going to
prove mathematical theorems using the Lean theorem prover. In other words, you're going to solve
levels in a computer game.
You're going to prove these theorems using *tactics*. The introductory world, Tutorial World,
will take you through some of these tactics. During your proofs, your "goal" (i.e. what you're
supposed to be proving) will be displayed with a `⊢` symbol in front of it. If the top
right hand box reports "Theorem Proved!", you have closed all the goals in the level
and can move on to the next level in the world you're in. When you've finished a world,
hit "main menu" in the top left to get back here.
For more info, see the <a href="http://wwwf.imperial.ac.uk/~buzzard/xena/natural_number_game/FAQ.html" target="blank">FAQ</a>.
# What's new in v1.3?
The game now saves your progress! Thanks to everyone who asked for it,
and to Mohammad for making it happen :-)
Cute little clipboard to copy your solutions.
# Thanks
Special thanks to Rob Lewis for tactic hackery, Bryan Gin-Ge Chen for
javascript hackery, Patrick Massot for his
<a href="https://github.com/leanprover-community/format_lean" target="blank">Lean to html formatter</a>,
Sian Carey for Power World,
and, last but not least, all the people who fed back comments, including
the 2019-20 Imperial College 1st year maths beta tester students, Marie-Amélie Lawn,
Toby Gee, Joseph Myers, and all the people who have been in touch
via the <a href="https://leanprover.zulipchat.com/" target="blank">Lean Zulip chat</a>
or the <a href="https://xenaproject.wordpress.com/" target="blank">Xena Project blog</a>
or via <a href="https://twitter.com/XenaProject" target="blank">Twitter</a>.
The natural number game is brought to you by the Xena project, a project based at Imperial College London
whose aim is to get mathematics undergraduates using computer theorem provers.
Lean is a computer theorem prover being developed at Microsoft Research.
Prove a theorem. Write a function. <a href="https://twitter.com/XenaProject" target="blank">@XenaProject</a>.
-/
|
cc132cb7642bee759b7858f59305d4834d3e0bc3 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/category_theory/monad/equiv_mon.lean | 44745ffeda1d739a1782902ad6204b9ce4dbbaa7 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 3,554 | lean | /-
Copyright (c) 2020 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz
-/
import category_theory.monad.basic
import category_theory.monoidal.End
import category_theory.monoidal.Mon_
import category_theory.category.Cat
/-!
# The equivalence between `Monad C` and `Mon_ (C ⥤ C)`.
A monad "is just" a monoid in the category of endofunctors.
# Definitions/Theorems
1. `to_Mon` associates a monoid object in `C ⥤ C` to any monad on `C`.
2. `Monad_to_Mon` is the functorial version of `to_Mon`.
3. `of_Mon` associates a monad on `C` to any monoid object in `C ⥤ C`.
4. `Monad_Mon_equiv` is the equivalence between `Monad C` and `Mon_ (C ⥤ C)`.
-/
namespace category_theory
open category
universes v u -- morphism levels before object levels. See note [category_theory universes].
variables {C : Type u} [category.{v} C]
namespace Monad
local attribute [instance, reducible] endofunctor_monoidal_category
/-- To every `Monad C` we associated a monoid object in `C ⥤ C`.-/
@[simps]
def to_Mon : monad C → Mon_ (C ⥤ C) := λ M,
{ X := (M : C ⥤ C),
one := M.η,
mul := M.μ,
mul_assoc' := by { ext, dsimp, simp [M.assoc] } }
variable (C)
/-- Passing from `Monad C` to `Mon_ (C ⥤ C)` is functorial. -/
@[simps]
def Monad_to_Mon : monad C ⥤ Mon_ (C ⥤ C) :=
{ obj := to_Mon,
map := λ _ _ f, { hom := f.to_nat_trans } }
variable {C}
/-- To every monoid object in `C ⥤ C` we associate a `Monad C`. -/
@[simps]
def of_Mon : Mon_ (C ⥤ C) → monad C := λ M,
{ to_functor := M.X,
η' := M.one,
μ' := M.mul,
left_unit' := λ X, by { rw [←M.one.id_hcomp_app, ←nat_trans.comp_app, M.mul_one], refl },
right_unit' := λ X, by { rw [←M.one.hcomp_id_app, ←nat_trans.comp_app, M.one_mul], refl },
assoc' := λ X, by { rw [←nat_trans.hcomp_id_app, ←nat_trans.comp_app], simp } }
variable (C)
/-- Passing from `Mon_ (C ⥤ C)` to `Monad C` is functorial. -/
@[simps]
def Mon_to_Monad : Mon_ (C ⥤ C) ⥤ monad C :=
{ obj := of_Mon,
map := λ _ _ f,
{ app_η' := begin
intro X,
erw [←nat_trans.comp_app, f.one_hom],
refl,
end,
app_μ' := begin
intro X,
erw [←nat_trans.comp_app, f.mul_hom],
finish,
end,
..f.hom } }
namespace Monad_Mon_equiv
variable {C}
/-- Isomorphism of functors used in `Monad_Mon_equiv` -/
@[simps {rhs_md := semireducible}]
def counit_iso : Mon_to_Monad C ⋙ Monad_to_Mon C ≅ 𝟭 _ :=
{ hom := { app := λ _, { hom := 𝟙 _ } },
inv := { app := λ _, { hom := 𝟙 _ } } }
/-- Auxiliary definition for `Monad_Mon_equiv` -/
@[simps]
def unit_iso_hom : 𝟭 _ ⟶ Monad_to_Mon C ⋙ Mon_to_Monad C :=
{ app := λ _, { app := λ _, 𝟙 _ } }
/-- Auxiliary definition for `Monad_Mon_equiv` -/
@[simps]
def unit_iso_inv : Monad_to_Mon C ⋙ Mon_to_Monad C ⟶ 𝟭 _ :=
{ app := λ _, { app := λ _, 𝟙 _ } }
/-- Isomorphism of functors used in `Monad_Mon_equiv` -/
@[simps]
def unit_iso : 𝟭 _ ≅ Monad_to_Mon C ⋙ Mon_to_Monad C :=
{ hom := unit_iso_hom,
inv := unit_iso_inv }
end Monad_Mon_equiv
open Monad_Mon_equiv
/-- Oh, monads are just monoids in the category of endofunctors (equivalence of categories). -/
@[simps]
def Monad_Mon_equiv : (monad C) ≌ (Mon_ (C ⥤ C)) :=
{ functor := Monad_to_Mon _,
inverse := Mon_to_Monad _,
unit_iso := unit_iso,
counit_iso := counit_iso }
-- Sanity check
example (A : monad C) {X : C} : ((Monad_Mon_equiv C).unit_iso.app A).hom.app X = 𝟙 _ := rfl
end Monad
end category_theory
|
374c9711f216a05838d70d7ce00a781bb56fc322 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/data/bool.lean | b76d50eee9d4d42172f1d2f141dee27db5ce69d5 | [
"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 | 6,574 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura, Jeremy Avigad
-/
/-!
# booleans
This file proves various trivial lemmas about booleans and their
relation to decidable propositions.
## Notations
This file introduces the notation `!b` for `bnot b`, the boolean "not".
## Tags
bool, boolean, De Morgan
-/
prefix `!`:90 := bnot
namespace bool
@[simp] theorem coe_sort_tt : coe_sort.{1 1} tt = true := eq_true_intro rfl
@[simp] theorem coe_sort_ff : coe_sort.{1 1} ff = false := eq_false_intro ff_ne_tt
@[simp] theorem to_bool_true {h} : @to_bool true h = tt :=
show _ = to_bool true, by congr
@[simp] theorem to_bool_false {h} : @to_bool false h = ff :=
show _ = to_bool false, by congr
@[simp] theorem to_bool_coe (b:bool) {h} : @to_bool b h = b :=
(show _ = to_bool b, by congr).trans (by cases b; refl)
theorem coe_to_bool (p : Prop) [decidable p] : to_bool p ↔ p := to_bool_iff _
@[simp] lemma of_to_bool_iff {p : Prop} [decidable p] : to_bool p ↔ p :=
⟨of_to_bool_true, _root_.to_bool_true⟩
@[simp] lemma tt_eq_to_bool_iff {p : Prop} [decidable p] : tt = to_bool p ↔ p :=
eq_comm.trans of_to_bool_iff
@[simp] lemma ff_eq_to_bool_iff {p : Prop} [decidable p] : ff = to_bool p ↔ ¬ p :=
eq_comm.trans (to_bool_ff_iff _)
@[simp] theorem to_bool_not (p : Prop) [decidable p] : to_bool (¬ p) = bnot (to_bool p) :=
by by_cases p; simp *
@[simp] theorem to_bool_and (p q : Prop) [decidable p] [decidable q] :
to_bool (p ∧ q) = p && q :=
by by_cases p; by_cases q; simp *
@[simp] theorem to_bool_or (p q : Prop) [decidable p] [decidable q] :
to_bool (p ∨ q) = p || q :=
by by_cases p; by_cases q; simp *
@[simp] theorem to_bool_eq {p q : Prop} [decidable p] [decidable q] :
to_bool p = to_bool q ↔ (p ↔ q) :=
⟨λ h, (coe_to_bool p).symm.trans $ by simp [h], to_bool_congr⟩
lemma not_ff : ¬ ff := by simp
@[simp] theorem default_bool : default bool = ff := rfl
theorem dichotomy (b : bool) : b = ff ∨ b = tt :=
by cases b; simp
@[simp] theorem forall_bool {p : bool → Prop} : (∀ b, p b) ↔ p ff ∧ p tt :=
⟨λ h, by simp [h], λ ⟨h₁, h₂⟩ b, by cases b; assumption⟩
@[simp] theorem exists_bool {p : bool → Prop} : (∃ b, p b) ↔ p ff ∨ p tt :=
⟨λ ⟨b, h⟩, by cases b; [exact or.inl h, exact or.inr h],
λ h, by cases h; exact ⟨_, h⟩⟩
/-- If `p b` is decidable for all `b : bool`, then `∀ b, p b` is decidable -/
instance decidable_forall_bool {p : bool → Prop} [∀ b, decidable (p b)] : decidable (∀ b, p b) :=
decidable_of_decidable_of_iff and.decidable forall_bool.symm
/-- If `p b` is decidable for all `b : bool`, then `∃ b, p b` is decidable -/
instance decidable_exists_bool {p : bool → Prop} [∀ b, decidable (p b)] : decidable (∃ b, p b) :=
decidable_of_decidable_of_iff or.decidable exists_bool.symm
@[simp] theorem cond_ff {α} (t e : α) : cond ff t e = e := rfl
@[simp] theorem cond_tt {α} (t e : α) : cond tt t e = t := rfl
@[simp] theorem cond_to_bool {α} (p : Prop) [decidable p] (t e : α) :
cond (to_bool p) t e = if p then t else e :=
by by_cases p; simp *
theorem coe_bool_iff : ∀ {a b : bool}, (a ↔ b) ↔ a = b := dec_trivial
theorem eq_tt_of_ne_ff : ∀ {a : bool}, a ≠ ff → a = tt := dec_trivial
theorem eq_ff_of_ne_tt : ∀ {a : bool}, a ≠ tt → a = ff := dec_trivial
theorem bor_comm : ∀ a b, a || b = b || a := dec_trivial
@[simp] theorem bor_assoc : ∀ a b c, (a || b) || c = a || (b || c) := dec_trivial
theorem bor_left_comm : ∀ a b c, a || (b || c) = b || (a || c) := dec_trivial
theorem bor_inl {a b : bool} (H : a) : a || b :=
by simp [H]
theorem bor_inr {a b : bool} (H : b) : a || b :=
by simp [H]
theorem band_comm : ∀ a b, a && b = b && a := dec_trivial
@[simp] theorem band_assoc : ∀ a b c, (a && b) && c = a && (b && c) := dec_trivial
theorem band_left_comm : ∀ a b c, a && (b && c) = b && (a && c) := dec_trivial
theorem band_elim_left : ∀ {a b : bool}, a && b → a := dec_trivial
theorem band_intro : ∀ {a b : bool}, a → b → a && b := dec_trivial
theorem band_elim_right : ∀ {a b : bool}, a && b → b := dec_trivial
@[simp] theorem bnot_false : bnot ff = tt := rfl
@[simp] theorem bnot_true : bnot tt = ff := rfl
theorem eq_tt_of_bnot_eq_ff : ∀ {a : bool}, bnot a = ff → a = tt := dec_trivial
theorem eq_ff_of_bnot_eq_tt : ∀ {a : bool}, bnot a = tt → a = ff := dec_trivial
theorem bxor_comm : ∀ a b, bxor a b = bxor b a := dec_trivial
@[simp] theorem bxor_assoc : ∀ a b c, bxor (bxor a b) c = bxor a (bxor b c) := dec_trivial
theorem bxor_left_comm : ∀ a b c, bxor a (bxor b c) = bxor b (bxor a c) := dec_trivial
@[simp] theorem bxor_bnot_left : ∀ a, bxor (!a) a = tt := dec_trivial
@[simp] theorem bxor_bnot_right : ∀ a, bxor a (!a) = tt := dec_trivial
@[simp] theorem bxor_bnot_bnot : ∀ a b, bxor (!a) (!b) = bxor a b := dec_trivial
@[simp] theorem bxor_ff_left : ∀ a, bxor ff a = a := dec_trivial
@[simp] theorem bxor_ff_right : ∀ a, bxor a ff = a := dec_trivial
lemma bxor_iff_ne : ∀ {x y : bool}, bxor x y = tt ↔ x ≠ y := dec_trivial
/-! ### De Morgan's laws for booleans-/
@[simp] lemma bnot_band : ∀ (a b : bool), !(a && b) = !a || !b := dec_trivial
@[simp] lemma bnot_bor : ∀ (a b : bool), !(a || b) = !a && !b := dec_trivial
lemma bnot_inj : ∀ {a b : bool}, !a = !b → a = b := dec_trivial
end bool
instance : linear_order bool :=
{ le := λ a b, a = ff ∨ b = tt,
le_refl := dec_trivial,
le_trans := dec_trivial,
le_antisymm := dec_trivial,
le_total := dec_trivial,
decidable_le := infer_instance,
decidable_eq := infer_instance,
decidable_lt := infer_instance }
namespace bool
/-- convert a `bool` to a `ℕ`, `false -> 0`, `true -> 1` -/
def to_nat (b : bool) : ℕ :=
cond b 1 0
/-- convert a `ℕ` to a `bool`, `0 -> false`, everything else -> `true` -/
def of_nat (n : ℕ) : bool :=
to_bool (n ≠ 0)
lemma of_nat_le_of_nat {n m : ℕ} (h : n ≤ m) : of_nat n ≤ of_nat m :=
begin
simp [of_nat];
cases nat.decidable_eq n 0;
cases nat.decidable_eq m 0;
simp only [to_bool],
{ subst m, have h := le_antisymm h (nat.zero_le _),
contradiction },
{ left, refl }
end
lemma to_nat_le_to_nat {b₀ b₁ : bool} (h : b₀ ≤ b₁) : to_nat b₀ ≤ to_nat b₁ :=
by cases h; subst h; [cases b₁, cases b₀]; simp [to_nat,nat.zero_le]
lemma of_nat_to_nat (b : bool) : of_nat (to_nat b) = b :=
by cases b; simp only [of_nat,to_nat]; exact dec_trivial
end bool
|
c29ac22cdaedc7c1819ca0c911ca1d84333168da | 367134ba5a65885e863bdc4507601606690974c1 | /src/topology/metric_space/completion.lean | 8c410f49190ea52a6857b8a9edc94af526ddb240 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 8,602 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Sébastien Gouëzel
-/
import topology.uniform_space.completion
import topology.metric_space.isometry
/-!
# The completion of a metric space
Completion of uniform spaces are already defined in `topology.uniform_space.completion`. We show
here that the uniform space completion of a metric space inherits a metric space structure,
by extending the distance to the completion and checking that it is indeed a distance, and that
it defines the same uniformity as the already defined uniform structure on the completion
-/
open set filter uniform_space uniform_space.completion
open_locale filter
noncomputable theory
universes u
variables {α : Type u} [metric_space α]
namespace metric
/-- The distance on the completion is obtained by extending the distance on the original space,
by uniform continuity. -/
instance : has_dist (completion α) :=
⟨completion.extension₂ dist⟩
/-- The new distance is uniformly continuous. -/
protected lemma completion.uniform_continuous_dist :
uniform_continuous (λp:completion α × completion α, dist p.1 p.2) :=
uniform_continuous_extension₂ dist
/-- The new distance is an extension of the original distance. -/
protected lemma completion.dist_eq (x y : α) : dist (x : completion α) y = dist x y :=
completion.extension₂_coe_coe uniform_continuous_dist _ _
/- Let us check that the new distance satisfies the axioms of a distance, by starting from the
properties on α and extending them to `completion α` by continuity. -/
protected lemma completion.dist_self (x : completion α) : dist x x = 0 :=
begin
apply induction_on x,
{ refine is_closed_eq _ continuous_const,
exact (completion.uniform_continuous_dist.continuous.comp
(continuous.prod_mk continuous_id continuous_id : _) : _) },
{ assume a,
rw [completion.dist_eq, dist_self] }
end
protected lemma completion.dist_comm (x y : completion α) : dist x y = dist y x :=
begin
apply induction_on₂ x y,
{ refine is_closed_eq completion.uniform_continuous_dist.continuous _,
exact completion.uniform_continuous_dist.continuous.comp
(@continuous_swap (completion α) (completion α) _ _) },
{ assume a b,
rw [completion.dist_eq, completion.dist_eq, dist_comm] }
end
protected lemma completion.dist_triangle (x y z : completion α) : dist x z ≤ dist x y + dist y z :=
begin
apply induction_on₃ x y z,
{ refine is_closed_le _ (continuous.add _ _),
{ have : continuous (λp : completion α × completion α × completion α, (p.1, p.2.2)) :=
continuous.prod_mk continuous_fst (continuous.comp continuous_snd continuous_snd),
exact (completion.uniform_continuous_dist.continuous.comp this : _) },
{ have : continuous (λp : completion α × completion α × completion α, (p.1, p.2.1)) :=
continuous.prod_mk continuous_fst (continuous_fst.comp continuous_snd),
exact (completion.uniform_continuous_dist.continuous.comp this : _) },
{ have : continuous (λp : completion α × completion α × completion α, (p.2.1, p.2.2)) :=
continuous.prod_mk (continuous_fst.comp continuous_snd)
(continuous.comp continuous_snd continuous_snd),
exact (continuous.comp completion.uniform_continuous_dist.continuous this : _) } },
{ assume a b c,
rw [completion.dist_eq, completion.dist_eq, completion.dist_eq],
exact dist_triangle a b c }
end
/-- Elements of the uniformity (defined generally for completions) can be characterized in terms
of the distance. -/
protected lemma completion.mem_uniformity_dist (s : set (completion α × completion α)) :
s ∈ uniformity (completion α) ↔ (∃ε>0, ∀{a b}, dist a b < ε → (a, b) ∈ s) :=
begin
split,
{ /- Start from an entourage `s`. It contains a closed entourage `t`. Its pullback in α is an
entourage, so it contains an ε-neighborhood of the diagonal by definition of the entourages
in metric spaces. Then `t` contains an ε-neighborhood of the diagonal in `completion α`, as
closed properties pass to the completion. -/
assume hs,
rcases mem_uniformity_is_closed hs with ⟨t, ht, ⟨tclosed, ts⟩⟩,
have A : {x : α × α | (coe (x.1), coe (x.2)) ∈ t} ∈ uniformity α :=
uniform_continuous_def.1 (uniform_continuous_coe α) t ht,
rcases mem_uniformity_dist.1 A with ⟨ε, εpos, hε⟩,
refine ⟨ε, εpos, λx y hxy, _⟩,
have : ε ≤ dist x y ∨ (x, y) ∈ t,
{ apply induction_on₂ x y,
{ have : {x : completion α × completion α | ε ≤ dist (x.fst) (x.snd) ∨ (x.fst, x.snd) ∈ t}
= {p : completion α × completion α | ε ≤ dist p.1 p.2} ∪ t, by ext; simp,
rw this,
apply is_closed_union _ tclosed,
exact is_closed_le continuous_const completion.uniform_continuous_dist.continuous },
{ assume x y,
rw completion.dist_eq,
by_cases h : ε ≤ dist x y,
{ exact or.inl h },
{ have Z := hε (not_le.1 h),
simp only [set.mem_set_of_eq] at Z,
exact or.inr Z }}},
simp only [not_le.mpr hxy, false_or, not_le] at this,
exact ts this },
{ /- Start from a set `s` containing an ε-neighborhood of the diagonal in `completion α`. To show
that it is an entourage, we use the fact that `dist` is uniformly continuous on
`completion α × completion α` (this is a general property of the extension of uniformly
continuous functions). Therefore, the preimage of the ε-neighborhood of the diagonal in ℝ
is an entourage in `completion α × completion α`. Massaging this property, it follows that
the ε-neighborhood of the diagonal is an entourage in `completion α`, and therefore this is
also the case of `s`. -/
rintros ⟨ε, εpos, hε⟩,
let r : set (ℝ × ℝ) := {p | dist p.1 p.2 < ε},
have : r ∈ uniformity ℝ := metric.dist_mem_uniformity εpos,
have T := uniform_continuous_def.1 (@completion.uniform_continuous_dist α _) r this,
simp only [uniformity_prod_eq_prod, mem_prod_iff, exists_prop,
filter.mem_map, set.mem_set_of_eq] at T,
rcases T with ⟨t1, ht1, t2, ht2, ht⟩,
refine mem_sets_of_superset ht1 _,
have A : ∀a b : completion α, (a, b) ∈ t1 → dist a b < ε,
{ assume a b hab,
have : ((a, b), (a, a)) ∈ set.prod t1 t2 := ⟨hab, refl_mem_uniformity ht2⟩,
have I := ht this,
simp [completion.dist_self, real.dist_eq, completion.dist_comm] at I,
exact lt_of_le_of_lt (le_abs_self _) I },
show t1 ⊆ s,
{ rintros ⟨a, b⟩ hp,
have : dist a b < ε := A a b hp,
exact hε this }}
end
/-- If two points are at distance 0, then they coincide. -/
protected lemma completion.eq_of_dist_eq_zero (x y : completion α) (h : dist x y = 0) : x = y :=
begin
/- This follows from the separation of `completion α` and from the description of
entourages in terms of the distance. -/
have : separated_space (completion α) := by apply_instance,
refine separated_def.1 this x y (λs hs, _),
rcases (completion.mem_uniformity_dist s).1 hs with ⟨ε, εpos, hε⟩,
rw ← h at εpos,
exact hε εpos
end
/-- Reformulate `completion.mem_uniformity_dist` in terms that are suitable for the definition
of the metric space structure. -/
protected lemma completion.uniformity_dist' :
uniformity (completion α) = (⨅ε:{ε : ℝ // 0 < ε}, 𝓟 {p | dist p.1 p.2 < ε.val}) :=
begin
ext s, rw mem_infi,
{ simp [completion.mem_uniformity_dist, subset_def] },
{ rintro ⟨r, hr⟩ ⟨p, hp⟩, use ⟨min r p, lt_min hr hp⟩,
simp [lt_min_iff, (≥)] {contextual := tt} }
end
protected lemma completion.uniformity_dist :
uniformity (completion α) = (⨅ ε>0, 𝓟 {p | dist p.1 p.2 < ε}) :=
by simpa [infi_subtype] using @completion.uniformity_dist' α _
/-- Metric space structure on the completion of a metric space. -/
instance completion.metric_space : metric_space (completion α) :=
{ dist_self := completion.dist_self,
eq_of_dist_eq_zero := completion.eq_of_dist_eq_zero,
dist_comm := completion.dist_comm,
dist_triangle := completion.dist_triangle,
to_uniform_space := by apply_instance,
uniformity_dist := completion.uniformity_dist }
/-- The embedding of a metric space in its completion is an isometry. -/
lemma completion.coe_isometry : isometry (coe : α → completion α) :=
isometry_emetric_iff_metric.2 completion.dist_eq
end metric
|
1ad4dea53062f329ba80d9321d6b8245a34a6dee | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/category/Groupoid.lean | cac379bf9cb2239ad3766c537ebcd0ca406eb30c | [
"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 | 4,054 | lean | /-
Copyright (c) 2019 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import category_theory.single_obj
import category_theory.limits.shapes.products
import category_theory.pi.basic
import category_theory.limits.is_limit
/-!
# Category of groupoids
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file contains the definition of the category `Groupoid` of all groupoids.
In this category objects are groupoids and morphisms are functors
between these groupoids.
We also provide two “forgetting” functors: `objects : Groupoid ⥤ Type`
and `forget_to_Cat : Groupoid ⥤ Cat`.
## Implementation notes
Though `Groupoid` is not a concrete category, we use `bundled` to define
its carrier type.
-/
universes v u
namespace category_theory
/-- Category of groupoids -/
@[nolint check_univs] -- intended to be used with explicit universe parameters
def Groupoid := bundled groupoid.{v u}
namespace Groupoid
instance : inhabited Groupoid := ⟨bundled.of (single_obj punit)⟩
instance str (C : Groupoid.{v u}) : groupoid.{v u} C.α := C.str
instance : has_coe_to_sort Groupoid Type* := bundled.has_coe_to_sort
/-- Construct a bundled `Groupoid` from the underlying type and the typeclass. -/
def of (C : Type u) [groupoid.{v} C] : Groupoid.{v u} := bundled.of C
@[simp] lemma coe_of (C : Type u) [groupoid C] : (of C : Type u) = C := rfl
/-- Category structure on `Groupoid` -/
instance category : large_category.{max v u} Groupoid.{v u} :=
{ hom := λ C D, C ⥤ D,
id := λ C, 𝟭 C,
comp := λ C D E F G, F ⋙ G,
id_comp' := λ C D F, by cases F; refl,
comp_id' := λ C D F, by cases F; refl,
assoc' := by intros; refl }
/-- Functor that gets the set of objects of a groupoid. It is not
called `forget`, because it is not a faithful functor. -/
def objects : Groupoid.{v u} ⥤ Type u :=
{ obj := bundled.α,
map := λ C D F, F.obj }
/-- Forgetting functor to `Cat` -/
def forget_to_Cat : Groupoid.{v u} ⥤ Cat.{v u} :=
{ obj := λ C, Cat.of C,
map := λ C D, id }
instance forget_to_Cat_full : full forget_to_Cat :=
{ preimage := λ C D, id }
instance forget_to_Cat_faithful : faithful forget_to_Cat := { }
/-- Convert arrows in the category of groupoids to functors,
which sometimes helps in applying simp lemmas -/
lemma hom_to_functor {C D E : Groupoid.{v u}} (f : C ⟶ D) (g : D ⟶ E) : f ≫ g = f ⋙ g := rfl
/-- Converts identity in the category of groupoids to the functor identity -/
lemma id_to_functor {C : Groupoid.{v u}} : 𝟭 C = 𝟙 C := rfl
section products
local attribute [tidy] tactic.discrete_cases
/-- Construct the product over an indexed family of groupoids, as a fan. -/
def pi_limit_fan ⦃J : Type u⦄ (F : J → Groupoid.{u u}) : limits.fan F :=
limits.fan.mk (@of (Π j : J, F j) _) (λ j, category_theory.pi.eval _ j)
/-- The product fan over an indexed family of groupoids, is a limit cone. -/
def pi_limit_fan_is_limit ⦃J : Type u⦄ (F : J → Groupoid.{u u}) :
limits.is_limit (pi_limit_fan F) :=
limits.mk_fan_limit (pi_limit_fan F)
(λ s, functor.pi' (λ j, s.proj j))
(by { intros, dunfold pi_limit_fan, simp [hom_to_functor], })
begin
intros s m w,
apply functor.pi_ext,
intro j, specialize w j,
simpa,
end
instance has_pi : limits.has_products Groupoid.{u u} :=
limits.has_products_of_limit_fans pi_limit_fan pi_limit_fan_is_limit
/-- The product of a family of groupoids is isomorphic
to the product object in the category of Groupoids -/
noncomputable def pi_iso_pi (J : Type u) (f : J → Groupoid.{u u}) : @of (Π j, f j) _ ≅ ∏ f :=
limits.is_limit.cone_point_unique_up_to_iso
(pi_limit_fan_is_limit f)
(limits.limit.is_limit (discrete.functor f))
@[simp]
lemma pi_iso_pi_hom_π (J : Type u) (f : J → Groupoid.{u u}) (j : J) :
(pi_iso_pi J f).hom ≫ (limits.pi.π f j) = category_theory.pi.eval _ j :=
by { simp [pi_iso_pi], refl, }
end products
end Groupoid
end category_theory
|
ff5c44521c3da24c0cda081ac38aafdff58a4881 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/topology/path_connected.lean | d830dcd2ca5dfa52d7c4e9e99b27691982fd5cb6 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 28,792 | lean | /-
Copyright (c) 2020 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.topology.instances.real
import Mathlib.topology.algebra.ordered.proj_Icc
import Mathlib.PostPort
universes u_1 l u_2 u_3
namespace Mathlib
/-!
# Path connectedness
## Main definitions
In the file the unit interval `[0, 1]` in `ℝ` is denoted by `I`, and `X` is a topological space.
* `path (x y : X)` is the type of paths from `x` to `y`, i.e., continuous maps from `I` to `X`
mapping `0` to `x` and `1` to `y`.
* `path.map` is the image of a path under a continuous map.
* `joined (x y : X)` means there is a path between `x` and `y`.
* `joined.some_path (h : joined x y)` selects some path between two points `x` and `y`.
* `path_component (x : X)` is the set of points joined to `x`.
* `path_connected_space X` is a predicate class asserting that `X` is non-empty and every two
points of `X` are joined.
Then there are corresponding relative notions for `F : set X`.
* `joined_in F (x y : X)` means there is a path `γ` joining `x` to `y` with values in `F`.
* `joined_in.some_path (h : joined_in F x y)` selects a path from `x` to `y` inside `F`.
* `path_component_in F (x : X)` is the set of points joined to `x` in `F`.
* `is_path_connected F` asserts that `F` is non-empty and every two
points of `F` are joined in `F`.
* `loc_path_connected_space X` is a predicate class asserting that `X` is locally path-connected:
each point has a basis of path-connected neighborhoods (we do *not* ask these to be open).
## Main theorems
* `joined` and `joined_in F` are transitive relations.
One can link the absolute and relative version in two directions, using `(univ : set X)` or the
subtype `↥F`.
* `path_connected_space_iff_univ : path_connected_space X ↔ is_path_connected (univ : set X)`
* `is_path_connected_iff_path_connected_space : is_path_connected F ↔ path_connected_space ↥F`
For locally path connected spaces, we have
* `path_connected_space_iff_connected_space : path_connected_space X ↔ connected_space X`
* `is_connected_iff_is_path_connected (U_op : is_open U) : is_path_connected U ↔ is_connected U`
## Implementation notes
By default, all paths have `I` as their source and `X` as their target, but there is an
operation `set.Icc_extend` that will extend any continuous map `γ : I → X` into a continuous map
`Icc_extend zero_le_one γ : ℝ → X` that is constant before `0` and after `1`.
This is used to define `path.extend` that turns `γ : path x y` into a continuous map
`γ.extend : ℝ → X` whose restriction to `I` is the original `γ`, and is equal to `x`
on `(-∞, 0]` and to `y` on `[1, +∞)`.
-/
/-! ### The unit interval -/
theorem Icc_zero_one_symm {t : ℝ} : t ∈ set.Icc 0 1 ↔ 1 - t ∈ set.Icc 0 1 := sorry
protected instance I_has_zero : HasZero ↥(set.Icc 0 1) :=
{ zero := { val := 0, property := sorry } }
@[simp] theorem coe_I_zero : ↑0 = 0 :=
rfl
protected instance I_has_one : HasOne ↥(set.Icc 0 1) :=
{ one := { val := 1, property := sorry } }
@[simp] theorem coe_I_one : ↑1 = 1 :=
rfl
/-- Unit interval central symmetry. -/
def I_symm : ↥(set.Icc 0 1) → ↥(set.Icc 0 1) :=
fun (t : ↥(set.Icc 0 1)) => { val := 1 - subtype.val t, property := sorry }
@[simp] theorem I_symm_zero : I_symm 0 = 1 := sorry
@[simp] theorem I_symm_one : I_symm 1 = 0 := sorry
theorem continuous_I_symm : continuous I_symm :=
continuous_subtype_mk (fun (x : ↥(set.Icc 0 1)) => I_symm._proof_1 x)
(continuous.sub continuous_const continuous_subtype_val)
protected instance set.Icc.connected_space : connected_space ↥(set.Icc 0 1) :=
subtype.connected_space { left := iff.mpr set.nonempty_Icc zero_le_one, right := is_preconnected_Icc }
protected instance set.Icc.compact_space : compact_space ↥(set.Icc 0 1) :=
iff.mp compact_iff_compact_space compact_Icc
/-! ### Paths -/
/-- Continuous path connecting two points `x` and `y` in a topological space -/
structure path {X : Type u_1} [topological_space X] (x : X) (y : X)
where
to_fun : ↥(set.Icc 0 1) → X
continuous' : continuous to_fun
source' : to_fun 0 = x
target' : to_fun 1 = y
protected instance path.has_coe_to_fun {X : Type u_1} [topological_space X] {x : X} {y : X} : has_coe_to_fun (path x y) :=
has_coe_to_fun.mk (fun (x : path x y) => ↥(set.Icc 0 1) → X) path.to_fun
protected theorem path.ext {X : Type u_1} [topological_space X] {x : X} {y : X} {γ₁ : path x y} {γ₂ : path x y} : ⇑γ₁ = ⇑γ₂ → γ₁ = γ₂ := sorry
namespace path
@[simp] theorem coe_mk {X : Type u_1} [topological_space X] {x : X} {y : X} (f : ↥(set.Icc 0 1) → X) (h₁ : continuous f) (h₂ : f 0 = x) (h₃ : f 1 = y) : ⇑(mk f h₁ h₂ h₃) = f :=
rfl
protected theorem continuous {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : continuous ⇑γ :=
continuous' γ
@[simp] protected theorem source {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : coe_fn γ 0 = x :=
source' γ
@[simp] protected theorem target {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : coe_fn γ 1 = y :=
target' γ
/-- Any function `φ : Π (a : α), path (x a) (y a)` can be seen as a function `α × I → X`. -/
protected instance has_uncurry_path {X : Type u_1} {α : Type u_2} [topological_space X] {x : α → X} {y : α → X} : function.has_uncurry ((a : α) → path (x a) (y a)) (α × ↥(set.Icc 0 1)) X :=
function.has_uncurry.mk
fun (φ : (a : α) → path (x a) (y a)) (p : α × ↥(set.Icc 0 1)) => coe_fn (φ (prod.fst p)) (prod.snd p)
/-- The constant path from a point to itself -/
def refl {X : Type u_1} [topological_space X] (x : X) : path x x :=
mk (fun (t : ↥(set.Icc 0 1)) => x) sorry rfl rfl
@[simp] theorem refl_range {X : Type u_1} [topological_space X] {a : X} : set.range ⇑(refl a) = singleton a := sorry
/-- The reverse of a path from `x` to `y`, as a path from `y` to `x` -/
def symm {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : path y x :=
mk (⇑γ ∘ I_symm) sorry sorry sorry
@[simp] theorem refl_symm {X : Type u_1} [topological_space X] {a : X} : symm (refl a) = refl a :=
path.ext (funext fun (x : ↥(set.Icc 0 1)) => Eq.refl (coe_fn (symm (refl a)) x))
@[simp] theorem symm_range {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : set.range ⇑(symm γ) = set.range ⇑γ := sorry
/-- A continuous map extending a path to `ℝ`, constant before `0` and after `1`. -/
def extend {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : ℝ → X :=
set.Icc_extend zero_le_one ⇑γ
theorem continuous_extend {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : continuous (extend γ) :=
continuous.Icc_extend (path.continuous γ)
@[simp] theorem extend_zero {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : extend γ 0 = x :=
Eq.trans (set.Icc_extend_left zero_le_one ⇑γ) (path.source γ)
@[simp] theorem extend_one {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : extend γ 1 = y :=
Eq.trans (set.Icc_extend_right zero_le_one ⇑γ) (path.target γ)
@[simp] theorem extend_extends {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) {t : ℝ} (ht : t ∈ set.Icc 0 1) : extend γ t = coe_fn γ { val := t, property := ht } :=
set.Icc_extend_of_mem zero_le_one (⇑γ) ht
@[simp] theorem extend_extends' {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) (t : ↥(set.Icc 0 1)) : extend γ ↑t = coe_fn γ t :=
set.Icc_extend_coe zero_le_one (⇑γ) t
@[simp] theorem extend_range {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : set.range (extend γ) = set.range ⇑γ :=
set.Icc_extend_range zero_le_one ⇑γ
theorem extend_of_le_zero {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) {t : ℝ} (ht : t ≤ 0) : extend γ t = a :=
Eq.trans (set.Icc_extend_of_le_left zero_le_one (⇑γ) ht) (path.source γ)
theorem extend_of_one_le {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) {t : ℝ} (ht : 1 ≤ t) : extend γ t = b :=
Eq.trans (set.Icc_extend_of_right_le zero_le_one (⇑γ) ht) (path.target γ)
@[simp] theorem refl_extend {X : Type u_1} [topological_space X] {a : X} : extend (refl a) = fun (_x : ℝ) => a :=
rfl
/-- The path obtained from a map defined on `ℝ` by restriction to the unit interval. -/
def of_line {X : Type u_1} [topological_space X] {x : X} {y : X} {f : ℝ → X} (hf : continuous_on f (set.Icc 0 1)) (h₀ : f 0 = x) (h₁ : f 1 = y) : path x y :=
mk (f ∘ coe) sorry h₀ h₁
theorem of_line_mem {X : Type u_1} [topological_space X] {x : X} {y : X} {f : ℝ → X} (hf : continuous_on f (set.Icc 0 1)) (h₀ : f 0 = x) (h₁ : f 1 = y) (t : ↥(set.Icc 0 1)) : coe_fn (of_line hf h₀ h₁) t ∈ f '' set.Icc 0 1 := sorry
/-- Concatenation of two paths from `x` to `y` and from `y` to `z`, putting the first
path on `[0, 1/2]` and the second one on `[1/2, 1]`. -/
def trans {X : Type u_1} [topological_space X] {x : X} {y : X} {z : X} (γ : path x y) (γ' : path y z) : path x z :=
mk ((fun (t : ℝ) => ite (t ≤ 1 / bit0 1) (extend γ (bit0 1 * t)) (extend γ' (bit0 1 * t - 1))) ∘ coe) sorry sorry sorry
@[simp] theorem refl_trans_refl {X : Type u_1} [topological_space X] {a : X} : trans (refl a) (refl a) = refl a := sorry
theorem trans_range {X : Type u_1} [topological_space X] {a : X} {b : X} {c : X} (γ₁ : path a b) (γ₂ : path b c) : set.range ⇑(trans γ₁ γ₂) = set.range ⇑γ₁ ∪ set.range ⇑γ₂ := sorry
/-- Image of a path from `x` to `y` by a continuous map -/
def map {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) {Y : Type u_2} [topological_space Y] {f : X → Y} (h : continuous f) : path (f x) (f y) :=
mk (f ∘ ⇑γ) sorry sorry sorry
@[simp] theorem map_coe {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) {Y : Type u_2} [topological_space Y] {f : X → Y} (h : continuous f) : ⇑(map γ h) = f ∘ ⇑γ :=
funext fun (t : ↥(set.Icc 0 1)) => Eq.refl (coe_fn (map γ h) t)
/-- Casting a path from `x` to `y` to a path from `x'` to `y'` when `x' = x` and `y' = y` -/
def cast {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) {x' : X} {y' : X} (hx : x' = x) (hy : y' = y) : path x' y' :=
mk (⇑γ) (path.continuous γ) sorry sorry
@[simp] theorem symm_cast {X : Type u_1} [topological_space X] {a₁ : X} {a₂ : X} {b₁ : X} {b₂ : X} (γ : path a₂ b₂) (ha : a₁ = a₂) (hb : b₁ = b₂) : symm (cast γ ha hb) = cast (symm γ) hb ha :=
rfl
@[simp] theorem trans_cast {X : Type u_1} [topological_space X] {a₁ : X} {a₂ : X} {b₁ : X} {b₂ : X} {c₁ : X} {c₂ : X} (γ : path a₂ b₂) (γ' : path b₂ c₂) (ha : a₁ = a₂) (hb : b₁ = b₂) (hc : c₁ = c₂) : trans (cast γ ha hb) (cast γ' hb hc) = cast (trans γ γ') ha hc :=
rfl
@[simp] theorem cast_coe {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) {x' : X} {y' : X} (hx : x' = x) (hy : y' = y) : ⇑(cast γ hx hy) = ⇑γ :=
rfl
theorem symm_continuous_family {X : Type u_1} {ι : Type u_2} [topological_space X] [topological_space ι] {a : ι → X} {b : ι → X} (γ : (t : ι) → path (a t) (b t)) (h : continuous ↿γ) : continuous ↿fun (t : ι) => symm (γ t) :=
continuous.comp h (continuous.prod_map continuous_id continuous_I_symm)
theorem continuous_uncurry_extend_of_continuous_family {X : Type u_1} {ι : Type u_2} [topological_space X] [topological_space ι] {a : ι → X} {b : ι → X} (γ : (t : ι) → path (a t) (b t)) (h : continuous ↿γ) : continuous ↿fun (t : ι) => extend (γ t) :=
continuous.comp h (continuous.prod_map continuous_id continuous_proj_Icc)
theorem trans_continuous_family {X : Type u_1} {ι : Type u_2} [topological_space X] [topological_space ι] {a : ι → X} {b : ι → X} {c : ι → X} (γ₁ : (t : ι) → path (a t) (b t)) (h₁ : continuous ↿γ₁) (γ₂ : (t : ι) → path (b t) (c t)) (h₂ : continuous ↿γ₂) : continuous ↿fun (t : ι) => trans (γ₁ t) (γ₂ t) := sorry
/-! #### Truncating a path -/
/-- `γ.truncate t₀ t₁` is the path which follows the path `γ` on the
time interval `[t₀, t₁]` and stays still otherwise. -/
def truncate {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) (t₀ : ℝ) (t₁ : ℝ) : path (extend γ (min t₀ t₁)) (extend γ t₁) :=
mk (fun (s : ↥(set.Icc 0 1)) => extend γ (min (max (↑s) t₀) t₁)) sorry sorry sorry
/-- `γ.truncate_of_le t₀ t₁ h`, where `h : t₀ ≤ t₁` is `γ.truncate t₀ t₁`
casted as a path from `γ.extend t₀` to `γ.extend t₁`. -/
def truncate_of_le {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) {t₀ : ℝ} {t₁ : ℝ} (h : t₀ ≤ t₁) : path (extend γ t₀) (extend γ t₁) :=
cast (truncate γ t₀ t₁) sorry sorry
theorem truncate_range {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) {t₀ : ℝ} {t₁ : ℝ} : set.range ⇑(truncate γ t₀ t₁) ⊆ set.range ⇑γ := sorry
/-- For a path `γ`, `γ.truncate` gives a "continuous family of paths", by which we
mean the uncurried function which maps `(t₀, t₁, s)` to `γ.truncate t₀ t₁ s` is continuous. -/
theorem truncate_continuous_family {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : continuous
fun (x : ℝ × ℝ × ↥(set.Icc 0 1)) => coe_fn (truncate γ (prod.fst x) (prod.fst (prod.snd x))) (prod.snd (prod.snd x)) := sorry
/- TODO : When `continuity` gets quicker, change the proof back to :
`begin`
`simp only [has_coe_to_fun.coe, coe_fn, path.truncate],`
`continuity,`
`exact continuous_subtype_coe`
`end` -/
theorem truncate_const_continuous_family {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) (t : ℝ) : continuous ↿(truncate γ t) := sorry
@[simp] theorem truncate_self {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) (t : ℝ) : truncate γ t t =
cast (refl (extend γ t))
(eq.mpr (id (Eq._oldrec (Eq.refl (extend γ (min t t) = extend γ t)) (min_self t))) (Eq.refl (extend γ t))) rfl := sorry
@[simp] theorem truncate_zero_zero {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : truncate γ 0 0 =
cast (refl a)
(eq.mpr (id (Eq._oldrec (Eq.refl (extend γ (min 0 0) = a)) (min_self 0)))
(eq.mpr (id (Eq._oldrec (Eq.refl (extend γ 0 = a)) (extend_zero γ))) (Eq.refl a)))
(extend_zero γ) := sorry
@[simp] theorem truncate_one_one {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : truncate γ 1 1 =
cast (refl b)
(eq.mpr (id (Eq._oldrec (Eq.refl (extend γ (min 1 1) = b)) (min_self 1)))
(eq.mpr (id (Eq._oldrec (Eq.refl (extend γ 1 = b)) (extend_one γ))) (Eq.refl b)))
(extend_one γ) := sorry
@[simp] theorem truncate_zero_one {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : truncate γ 0 1 =
cast γ
(eq.mpr
(id
(Eq.trans
((fun (a a_1 : X) (e_1 : a = a_1) (ᾰ ᾰ_1 : X) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2)
(extend γ (min 0 1)) a
(Eq.trans
((fun (γ γ_1 : path a b) (e_1 : γ = γ_1) (ᾰ ᾰ_1 : ℝ) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg extend e_1) e_2)
γ γ (Eq.refl γ) (min 0 1) 0 (min_eq_left (iff.mpr (iff_true_intro zero_le_one) True.intro)))
(extend_zero γ))
a a (Eq.refl a))
(propext (eq_self_iff_true a))))
trivial)
(eq.mpr
(id
(Eq.trans
((fun (a a_1 : X) (e_1 : a = a_1) (ᾰ ᾰ_1 : X) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (extend γ 1) b
(extend_one γ) b b (Eq.refl b))
(propext (eq_self_iff_true b))))
trivial) := sorry
end path
/-! ### Being joined by a path -/
/-- The relation "being joined by a path". This is an equivalence relation. -/
def joined {X : Type u_1} [topological_space X] (x : X) (y : X) :=
Nonempty (path x y)
theorem joined.refl {X : Type u_1} [topological_space X] (x : X) : joined x x :=
Nonempty.intro (path.refl x)
/-- When two points are joined, choose some path from `x` to `y`. -/
def joined.some_path {X : Type u_1} [topological_space X] {x : X} {y : X} (h : joined x y) : path x y :=
nonempty.some h
theorem joined.symm {X : Type u_1} [topological_space X] {x : X} {y : X} (h : joined x y) : joined y x :=
Nonempty.intro (path.symm (joined.some_path h))
theorem joined.trans {X : Type u_1} [topological_space X] {x : X} {y : X} {z : X} (hxy : joined x y) (hyz : joined y z) : joined x z :=
Nonempty.intro (path.trans (joined.some_path hxy) (joined.some_path hyz))
/-- The setoid corresponding the equivalence relation of being joined by a continuous path. -/
def path_setoid (X : Type u_1) [topological_space X] : setoid X :=
setoid.mk joined sorry
/-- The quotient type of points of a topological space modulo being joined by a continuous path. -/
def zeroth_homotopy (X : Type u_1) [topological_space X] :=
quotient (path_setoid X)
protected instance zeroth_homotopy.inhabited : Inhabited (zeroth_homotopy ℝ) :=
{ default := quotient.mk 0 }
/-! ### Being joined by a path inside a set -/
/-- The relation "being joined by a path in `F`". Not quite an equivalence relation since it's not
reflexive for points that do not belong to `F`. -/
def joined_in {X : Type u_1} [topological_space X] (F : set X) (x : X) (y : X) :=
∃ (γ : path x y), ∀ (t : ↥(set.Icc 0 1)), coe_fn γ t ∈ F
theorem joined_in.mem {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : x ∈ F ∧ y ∈ F := sorry
theorem joined_in.source_mem {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : x ∈ F :=
and.left (joined_in.mem h)
theorem joined_in.target_mem {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : y ∈ F :=
and.right (joined_in.mem h)
/-- When `x` and `y` are joined in `F`, choose a path from `x` to `y` inside `F` -/
def joined_in.some_path {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : path x y :=
classical.some h
theorem joined_in.some_path_mem {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) (t : ↥(set.Icc 0 1)) : coe_fn (joined_in.some_path h) t ∈ F :=
classical.some_spec h t
/-- If `x` and `y` are joined in the set `F`, then they are joined in the subtype `F`. -/
theorem joined_in.joined_subtype {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : joined { val := x, property := joined_in.source_mem h } { val := y, property := joined_in.target_mem h } := sorry
theorem joined_in.of_line {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} {f : ℝ → X} (hf : continuous_on f (set.Icc 0 1)) (h₀ : f 0 = x) (h₁ : f 1 = y) (hF : f '' set.Icc 0 1 ⊆ F) : joined_in F x y :=
Exists.intro (path.of_line hf h₀ h₁) fun (t : ↥(set.Icc 0 1)) => hF (path.of_line_mem hf h₀ h₁ t)
theorem joined_in.joined {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : joined x y :=
Nonempty.intro (joined_in.some_path h)
theorem joined_in_iff_joined {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (x_in : x ∈ F) (y_in : y ∈ F) : joined_in F x y ↔ joined { val := x, property := x_in } { val := y, property := y_in } := sorry
@[simp] theorem joined_in_univ {X : Type u_1} [topological_space X] {x : X} {y : X} : joined_in set.univ x y ↔ joined x y := sorry
theorem joined_in.mono {X : Type u_1} [topological_space X] {x : X} {y : X} {U : set X} {V : set X} (h : joined_in U x y) (hUV : U ⊆ V) : joined_in V x y :=
Exists.intro (joined_in.some_path h) fun (t : ↥(set.Icc 0 1)) => hUV (joined_in.some_path_mem h t)
theorem joined_in.refl {X : Type u_1} [topological_space X] {x : X} {F : set X} (h : x ∈ F) : joined_in F x x :=
Exists.intro (path.refl x) fun (t : ↥(set.Icc 0 1)) => h
theorem joined_in.symm {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : joined_in F y x := sorry
theorem joined_in.trans {X : Type u_1} [topological_space X] {x : X} {y : X} {z : X} {F : set X} (hxy : joined_in F x y) (hyz : joined_in F y z) : joined_in F x z := sorry
/-! ### Path component -/
/-- The path component of `x` is the set of points that can be joined to `x`. -/
def path_component {X : Type u_1} [topological_space X] (x : X) : set X :=
set_of fun (y : X) => joined x y
@[simp] theorem mem_path_component_self {X : Type u_1} [topological_space X] (x : X) : x ∈ path_component x :=
joined.refl x
@[simp] theorem path_component.nonempty {X : Type u_1} [topological_space X] (x : X) : set.nonempty (path_component x) :=
Exists.intro x (mem_path_component_self x)
theorem mem_path_component_of_mem {X : Type u_1} [topological_space X] {x : X} {y : X} (h : x ∈ path_component y) : y ∈ path_component x :=
joined.symm h
theorem path_component_symm {X : Type u_1} [topological_space X] {x : X} {y : X} : x ∈ path_component y ↔ y ∈ path_component x :=
{ mp := fun (h : x ∈ path_component y) => mem_path_component_of_mem h,
mpr := fun (h : y ∈ path_component x) => mem_path_component_of_mem h }
theorem path_component_congr {X : Type u_1} [topological_space X] {x : X} {y : X} (h : x ∈ path_component y) : path_component x = path_component y := sorry
theorem path_component_subset_component {X : Type u_1} [topological_space X] (x : X) : path_component x ⊆ connected_component x := sorry
/-- The path component of `x` in `F` is the set of points that can be joined to `x` in `F`. -/
def path_component_in {X : Type u_1} [topological_space X] (x : X) (F : set X) : set X :=
set_of fun (y : X) => joined_in F x y
@[simp] theorem path_component_in_univ {X : Type u_1} [topological_space X] (x : X) : path_component_in x set.univ = path_component x := sorry
theorem joined.mem_path_component {X : Type u_1} [topological_space X] {x : X} {y : X} {z : X} (hyz : joined y z) (hxy : y ∈ path_component x) : z ∈ path_component x :=
joined.trans hxy hyz
/-! ### Path connected sets -/
/-- A set `F` is path connected if it contains a point that can be joined to all other in `F`. -/
def is_path_connected {X : Type u_1} [topological_space X] (F : set X) :=
∃ (x : X), ∃ (H : x ∈ F), ∀ {y : X}, y ∈ F → joined_in F x y
theorem is_path_connected_iff_eq {X : Type u_1} [topological_space X] {F : set X} : is_path_connected F ↔ ∃ (x : X), ∃ (H : x ∈ F), path_component_in x F = F := sorry
theorem is_path_connected.joined_in {X : Type u_1} [topological_space X] {F : set X} (h : is_path_connected F) (x : X) (y : X) (H : x ∈ F) : y ∈ F → joined_in F x y := sorry
theorem is_path_connected_iff {X : Type u_1} [topological_space X] {F : set X} : is_path_connected F ↔ set.nonempty F ∧ ∀ (x y : X), x ∈ F → y ∈ F → joined_in F x y := sorry
theorem is_path_connected.image {X : Type u_1} [topological_space X] {F : set X} {Y : Type u_2} [topological_space Y] (hF : is_path_connected F) {f : X → Y} (hf : continuous f) : is_path_connected (f '' F) := sorry
theorem is_path_connected.mem_path_component {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : is_path_connected F) (x_in : x ∈ F) (y_in : y ∈ F) : y ∈ path_component x :=
joined_in.joined (is_path_connected.joined_in h x y x_in y_in)
theorem is_path_connected.subset_path_component {X : Type u_1} [topological_space X] {x : X} {F : set X} (h : is_path_connected F) (x_in : x ∈ F) : F ⊆ path_component x :=
fun (y : X) (y_in : y ∈ F) => is_path_connected.mem_path_component h x_in y_in
theorem is_path_connected.union {X : Type u_1} [topological_space X] {U : set X} {V : set X} (hU : is_path_connected U) (hV : is_path_connected V) (hUV : set.nonempty (U ∩ V)) : is_path_connected (U ∪ V) := sorry
/-- If a set `W` is path-connected, then it is also path-connected when seen as a set in a smaller
ambient type `U` (when `U` contains `W`). -/
theorem is_path_connected.preimage_coe {X : Type u_1} [topological_space X] {U : set X} {W : set X} (hW : is_path_connected W) (hWU : W ⊆ U) : is_path_connected (coe ⁻¹' W) := sorry
theorem is_path_connected.exists_path_through_family {X : Type u_1} [topological_space X] {n : ℕ} {s : set X} (h : is_path_connected s) (p : fin (n + 1) → X) (hp : ∀ (i : fin (n + 1)), p i ∈ s) : ∃ (γ : path (p 0) (p ↑n)), set.range ⇑γ ⊆ s ∧ ∀ (i : fin (n + 1)), p i ∈ set.range ⇑γ := sorry
theorem is_path_connected.exists_path_through_family' {X : Type u_1} [topological_space X] {n : ℕ} {s : set X} (h : is_path_connected s) (p : fin (n + 1) → X) (hp : ∀ (i : fin (n + 1)), p i ∈ s) : ∃ (γ : path (p 0) (p ↑n)),
∃ (t : fin (n + 1) → ↥(set.Icc 0 1)),
(∀ (t : ↥(set.Icc 0 1)), coe_fn γ t ∈ s) ∧ ∀ (i : fin (n + 1)), coe_fn γ (t i) = p i := sorry
/-! ### Path connected spaces -/
/-- A topological space is path-connected if it is non-empty and every two points can be
joined by a continuous path. -/
class path_connected_space (X : Type u_3) [topological_space X]
where
nonempty : Nonempty X
joined : ∀ (x y : X), joined x y
theorem path_connected_space_iff_zeroth_homotopy {X : Type u_1} [topological_space X] : path_connected_space X ↔ Nonempty (zeroth_homotopy X) ∧ subsingleton (zeroth_homotopy X) := sorry
namespace path_connected_space
/-- Use path-connectedness to build a path between two points. -/
def some_path {X : Type u_1} [topological_space X] [path_connected_space X] (x : X) (y : X) : path x y :=
nonempty.some (joined x y)
end path_connected_space
theorem is_path_connected_iff_path_connected_space {X : Type u_1} [topological_space X] {F : set X} : is_path_connected F ↔ path_connected_space ↥F := sorry
theorem path_connected_space_iff_univ {X : Type u_1} [topological_space X] : path_connected_space X ↔ is_path_connected set.univ := sorry
theorem path_connected_space_iff_eq {X : Type u_1} [topological_space X] : path_connected_space X ↔ ∃ (x : X), path_component x = set.univ := sorry
protected instance path_connected_space.connected_space {X : Type u_1} [topological_space X] [path_connected_space X] : connected_space X := sorry
namespace path_connected_space
theorem exists_path_through_family {X : Type u_1} [topological_space X] [path_connected_space X] {n : ℕ} (p : fin (n + 1) → X) : ∃ (γ : path (p 0) (p ↑n)), ∀ (i : fin (n + 1)), p i ∈ set.range ⇑γ := sorry
theorem exists_path_through_family' {X : Type u_1} [topological_space X] [path_connected_space X] {n : ℕ} (p : fin (n + 1) → X) : ∃ (γ : path (p 0) (p ↑n)), ∃ (t : fin (n + 1) → ↥(set.Icc 0 1)), ∀ (i : fin (n + 1)), coe_fn γ (t i) = p i := sorry
end path_connected_space
/-! ### Locally path connected spaces -/
/-- A topological space is locally path connected, at every point, path connected
neighborhoods form a neighborhood basis. -/
class loc_path_connected_space (X : Type u_3) [topological_space X]
where
path_connected_basis : ∀ (x : X), filter.has_basis (nhds x) (fun (s : set X) => s ∈ nhds x ∧ is_path_connected s) id
theorem loc_path_connected_of_bases {X : Type u_1} [topological_space X] {ι : Type u_2} {p : ι → Prop} {s : X → ι → set X} (h : ∀ (x : X), filter.has_basis (nhds x) p (s x)) (h' : ∀ (x : X) (i : ι), p i → is_path_connected (s x i)) : loc_path_connected_space X := sorry
theorem path_connected_space_iff_connected_space {X : Type u_1} [topological_space X] [loc_path_connected_space X] : path_connected_space X ↔ connected_space X := sorry
theorem path_connected_subset_basis {X : Type u_1} [topological_space X] {x : X} [loc_path_connected_space X] {U : set X} (h : is_open U) (hx : x ∈ U) : filter.has_basis (nhds x) (fun (s : set X) => s ∈ nhds x ∧ is_path_connected s ∧ s ⊆ U) id :=
filter.has_basis.has_basis_self_subset (path_connected_basis x) (mem_nhds_sets h hx)
theorem loc_path_connected_of_is_open {X : Type u_1} [topological_space X] [loc_path_connected_space X] {U : set X} (h : is_open U) : loc_path_connected_space ↥U := sorry
theorem is_open.is_connected_iff_is_path_connected {X : Type u_1} [topological_space X] [loc_path_connected_space X] {U : set X} (U_op : is_open U) : is_path_connected U ↔ is_connected U := sorry
|
adbf9c9fe9ff6b915f7ca35761def615cadbaab8 | 556aeb81a103e9e0ac4e1fe0ce1bc6e6161c3c5e | /src/starkware/cairo/common/bool_spec.lean | cd1b180dc1e466ffbfc806a906055c702898c30d | [] | permissive | starkware-libs/formal-proofs | d6b731604461bf99e6ba820e68acca62a21709e8 | f5fa4ba6a471357fd171175183203d0b437f6527 | refs/heads/master | 1,691,085,444,753 | 1,690,507,386,000 | 1,690,507,386,000 | 410,476,629 | 32 | 9 | Apache-2.0 | 1,690,506,773,000 | 1,632,639,790,000 | Lean | UTF-8 | Lean | false | false | 720 | lean | /-
Specifications file for bool_spec.cairo
Do not modify the constant definitions, structure definitions, or automatic specifications.
Do not change the name or arguments of the user specifications and soundness theorems.
You may freely move the definitions around in the file.
You may add definitions and theorems wherever you wish in this file.
-/
import starkware.cairo.lean.semantics.soundness.prelude
namespace starkware.cairo.common.bool
variables {F : Type} [field F] [decidable_eq F] [prelude_hyps F]
-- End of automatically generated prelude.
-- Main scope definitions.
@[reducible] def FALSE := 0
@[reducible] def TRUE := 1
-- End of main scope definitions.
end starkware.cairo.common.bool
|
377cc9e3e936308fccfa6f47aa90603c0113a673 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/ring_theory/euclidean_domain.lean | 7cd5517f9684bb7b10feb8f82cb759889a0965a0 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,360 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Chris Hughes
-/
import algebra.gcd_monoid.basic
import ring_theory.coprime.basic
import ring_theory.ideal.basic
/-!
# Lemmas about Euclidean domains
Various about Euclidean domains are proved; all of them seem to be true
more generally for principal ideal domains, so these lemmas should
probably be reproved in more generality and this file perhaps removed?
## Tags
euclidean domain
-/
noncomputable theory
open_locale classical
open euclidean_domain set ideal
section gcd_monoid
variables {R : Type*} [euclidean_domain R] [gcd_monoid R]
open gcd_monoid
lemma left_div_gcd_ne_zero {p q : R} (hp : p ≠ 0) :
p / gcd_monoid.gcd p q ≠ 0 :=
begin
obtain ⟨r, hr⟩ := gcd_monoid.gcd_dvd_left p q,
obtain ⟨pq0, r0⟩ : gcd_monoid.gcd p q ≠ 0 ∧ r ≠ 0 := mul_ne_zero_iff.mp (hr ▸ hp),
rw [hr, mul_comm, mul_div_cancel _ pq0] { occs := occurrences.pos [1] },
exact r0,
end
lemma right_div_gcd_ne_zero {p q : R} (hq : q ≠ 0) :
q / gcd_monoid.gcd p q ≠ 0 :=
begin
obtain ⟨r, hr⟩ := gcd_monoid.gcd_dvd_right p q,
obtain ⟨pq0, r0⟩ : gcd_monoid.gcd p q ≠ 0 ∧ r ≠ 0 := mul_ne_zero_iff.mp (hr ▸ hq),
rw [hr, mul_comm, mul_div_cancel _ pq0] { occs := occurrences.pos [1] },
exact r0,
end
end gcd_monoid
variables {α : Type*} [euclidean_domain α] [decidable_eq α]
theorem span_gcd {α} [euclidean_domain α] (x y : α) :
span ({gcd x y} : set α) = span ({x, y} : set α) :=
begin
apply le_antisymm,
{ refine span_le.2 (λ x, _),
simp only [set.mem_singleton_iff, set_like.mem_coe, mem_span_pair],
rintro rfl,
exact ⟨gcd_a x y, gcd_b x y, by simp [gcd_eq_gcd_ab, mul_comm]⟩ },
{ assume z ,
simp [mem_span_singleton, euclidean_domain.gcd_dvd_left, mem_span_pair,
@eq_comm _ _ z] {contextual := tt},
exact λ a b _, dvd_add ((gcd_dvd_left x y).mul_left _)
((gcd_dvd_right x y).mul_left _) }
end
-- this should be proved for PIDs?
theorem gcd_is_unit_iff {α} [euclidean_domain α] {x y : α} :
is_unit (gcd x y) ↔ is_coprime x y :=
⟨λ h, let ⟨b, hb⟩ := is_unit_iff_exists_inv'.1 h in ⟨b * gcd_a x y, b * gcd_b x y,
by rw [← hb, gcd_eq_gcd_ab, mul_comm x, mul_comm y, mul_add, mul_assoc, mul_assoc]⟩,
λ ⟨a, b, h⟩, is_unit_iff_dvd_one.2 $ h ▸ dvd_add ((gcd_dvd_left x y).mul_left _)
((gcd_dvd_right x y).mul_left _)⟩
-- this should be proved for UFDs surely?
theorem is_coprime_of_dvd {α} [euclidean_domain α] {x y : α}
(z : ¬ (x = 0 ∧ y = 0)) (H : ∀ z ∈ nonunits α, z ≠ 0 → z ∣ x → ¬ z ∣ y) :
is_coprime x y :=
begin
rw [← gcd_is_unit_iff],
by_contra h,
refine H _ h _ (gcd_dvd_left _ _) (gcd_dvd_right _ _),
rwa [ne, euclidean_domain.gcd_eq_zero_iff]
end
-- this should be proved for UFDs surely?
theorem dvd_or_coprime {α} [euclidean_domain α] (x y : α)
(h : irreducible x) : x ∣ y ∨ is_coprime x y :=
begin
refine or_iff_not_imp_left.2 (λ h', _),
apply is_coprime_of_dvd,
{ unfreezingI { rintro ⟨rfl, rfl⟩ }, simpa using h },
{ unfreezingI { rintro z nu nz ⟨w, rfl⟩ dy },
refine h' (dvd_trans _ dy),
simpa using mul_dvd_mul_left z (is_unit_iff_dvd_one.1 $
(of_irreducible_mul h).resolve_left nu) }
end
|
f4550ea375b081278c55f654c049061c5bc1fa2e | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/group_with_zero/units/lemmas.lean | b299f7ce0cffa8ac6d2d0b6b3cb77fe7dc32cd12 | [
"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 | 7,349 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import algebra.group_with_zero.commute
import algebra.hom.units
import group_theory.group_action.units
/-!
# Further lemmas about units in a `monoid_with_zero` or a `group_with_zero`.
-/
variables {α M₀ G₀ M₀' G₀' F F' : Type*}
variables [monoid_with_zero M₀]
section group_with_zero
variables [group_with_zero G₀] {a b c : G₀}
@[simp] lemma div_self (h : a ≠ 0) : a / a = 1 := h.is_unit.div_self
lemma eq_mul_inv_iff_mul_eq₀ (hc : c ≠ 0) : a = b * c⁻¹ ↔ a * c = b :=
hc.is_unit.eq_mul_inv_iff_mul_eq
lemma eq_inv_mul_iff_mul_eq₀ (hb : b ≠ 0) : a = b⁻¹ * c ↔ b * a = c :=
hb.is_unit.eq_inv_mul_iff_mul_eq
lemma inv_mul_eq_iff_eq_mul₀ (ha : a ≠ 0) : a⁻¹ * b = c ↔ b = a * c :=
ha.is_unit.inv_mul_eq_iff_eq_mul
lemma mul_inv_eq_iff_eq_mul₀ (hb : b ≠ 0) : a * b⁻¹ = c ↔ a = c * b :=
hb.is_unit.mul_inv_eq_iff_eq_mul
lemma mul_inv_eq_one₀ (hb : b ≠ 0) : a * b⁻¹ = 1 ↔ a = b := hb.is_unit.mul_inv_eq_one
lemma inv_mul_eq_one₀ (ha : a ≠ 0) : a⁻¹ * b = 1 ↔ a = b := ha.is_unit.inv_mul_eq_one
lemma mul_eq_one_iff_eq_inv₀ (hb : b ≠ 0) : a * b = 1 ↔ a = b⁻¹ := hb.is_unit.mul_eq_one_iff_eq_inv
lemma mul_eq_one_iff_inv_eq₀ (ha : a ≠ 0) : a * b = 1 ↔ a⁻¹ = b := ha.is_unit.mul_eq_one_iff_inv_eq
@[simp] lemma div_mul_cancel (a : G₀) (h : b ≠ 0) : a / b * b = a := h.is_unit.div_mul_cancel _
@[simp] lemma mul_div_cancel (a : G₀) (h : b ≠ 0) : a * b / b = a := h.is_unit.mul_div_cancel _
lemma mul_one_div_cancel (h : a ≠ 0) : a * (1 / a) = 1 := h.is_unit.mul_one_div_cancel
lemma one_div_mul_cancel (h : a ≠ 0) : (1 / a) * a = 1 := h.is_unit.one_div_mul_cancel
lemma div_left_inj' (hc : c ≠ 0) : a / c = b / c ↔ a = b := hc.is_unit.div_left_inj
@[field_simps] lemma div_eq_iff (hb : b ≠ 0) : a / b = c ↔ a = c * b := hb.is_unit.div_eq_iff
@[field_simps] lemma eq_div_iff (hb : b ≠ 0) : c = a / b ↔ c * b = a := hb.is_unit.eq_div_iff
lemma div_eq_iff_mul_eq (hb : b ≠ 0) : a / b = c ↔ c * b = a := hb.is_unit.div_eq_iff.trans eq_comm
lemma eq_div_iff_mul_eq (hc : c ≠ 0) : a = b / c ↔ a * c = b := hc.is_unit.eq_div_iff
lemma div_eq_of_eq_mul (hb : b ≠ 0) : a = c * b → a / b = c := hb.is_unit.div_eq_of_eq_mul
lemma eq_div_of_mul_eq (hc : c ≠ 0) : a * c = b → a = b / c := hc.is_unit.eq_div_of_mul_eq
lemma div_eq_one_iff_eq (hb : b ≠ 0) : a / b = 1 ↔ a = b := hb.is_unit.div_eq_one_iff_eq
lemma div_mul_left (hb : b ≠ 0) : b / (a * b) = 1 / a := hb.is_unit.div_mul_left
lemma mul_div_mul_right (a b : G₀) (hc : c ≠ 0) : (a * c) / (b * c) = a / b :=
hc.is_unit.mul_div_mul_right _ _
lemma mul_mul_div (a : G₀) (hb : b ≠ 0) : a = a * b * (1 / b) := (hb.is_unit.mul_mul_div _).symm
lemma div_div_div_cancel_right (a : G₀) (hc : c ≠ 0) : (a / c) / (b / c) = a / b :=
by rw [div_div_eq_mul_div, div_mul_cancel _ hc]
lemma div_mul_div_cancel (a : G₀) (hc : c ≠ 0) : (a / c) * (c / b) = a / b :=
by rw [← mul_div_assoc, div_mul_cancel _ hc]
lemma div_mul_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a / b * b = a :=
classical.by_cases (λ hb : b = 0, by simp [*]) (div_mul_cancel a)
lemma mul_div_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a * b / b = a :=
classical.by_cases (λ hb : b = 0, by simp [*]) (mul_div_cancel a)
@[simp] theorem divp_mk0 (a : G₀) {b : G₀} (hb : b ≠ 0) :
a /ₚ units.mk0 b hb = a / b :=
divp_eq_div _ _
end group_with_zero
section comm_group_with_zero -- comm
variables [comm_group_with_zero G₀] {a b c d : G₀}
lemma div_mul_right (b : G₀) (ha : a ≠ 0) : a / (a * b) = 1 / b := ha.is_unit.div_mul_right _
lemma mul_div_cancel_left_of_imp {a b : G₀} (h : a = 0 → b = 0) : a * b / a = b :=
by rw [mul_comm, mul_div_cancel_of_imp h]
lemma mul_div_cancel_left (b : G₀) (ha : a ≠ 0) : a * b / a = b := ha.is_unit.mul_div_cancel_left _
lemma mul_div_cancel_of_imp' {a b : G₀} (h : b = 0 → a = 0) : b * (a / b) = a :=
by rw [mul_comm, div_mul_cancel_of_imp h]
lemma mul_div_cancel' (a : G₀) (hb : b ≠ 0) : b * (a / b) = a := hb.is_unit.mul_div_cancel' _
lemma mul_div_mul_left (a b : G₀) (hc : c ≠ 0) : (c * a) / (c * b) = a / b :=
hc.is_unit.mul_div_mul_left _ _
lemma mul_eq_mul_of_div_eq_div (a : G₀) {b : G₀} (c : G₀) {d : G₀} (hb : b ≠ 0) (hd : d ≠ 0)
(h : a / b = c / d) : a * d = c * b :=
by rw [←mul_one a, ←div_self hb, ←mul_comm_div, h, div_mul_eq_mul_div, div_mul_cancel _ hd]
@[field_simps] lemma div_eq_div_iff (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b :=
hb.is_unit.div_eq_div_iff hd.is_unit
lemma div_div_cancel' (ha : a ≠ 0) : a / (a / b) = b := ha.is_unit.div_div_cancel
lemma div_helper (b : G₀) (h : a ≠ 0) : 1 / (a * b) * a = 1 / b :=
by rw [div_mul_eq_mul_div, one_mul, div_mul_right _ h]
end comm_group_with_zero
section monoid_with_zero
variables [group_with_zero G₀] [nontrivial M₀]
[monoid_with_zero M₀'] [monoid_with_zero_hom_class F G₀ M₀]
[monoid_with_zero_hom_class F' G₀ M₀'] (f : F) {a : G₀}
include M₀
lemma map_ne_zero : f a ≠ 0 ↔ a ≠ 0 :=
⟨λ hfa ha, hfa $ ha.symm ▸ map_zero f, λ ha, ((is_unit.mk0 a ha).map f).ne_zero⟩
@[simp] lemma map_eq_zero : f a = 0 ↔ a = 0 := not_iff_not.1 (map_ne_zero f)
omit M₀
include M₀'
lemma eq_on_inv₀ (f g : F') (h : f a = g a) : f a⁻¹ = g a⁻¹ :=
begin
rcases eq_or_ne a 0 with rfl|ha,
{ rw [inv_zero, map_zero, map_zero] },
{ exact (is_unit.mk0 a ha).eq_on_inv f g h }
end
end monoid_with_zero
section group_with_zero
variables [group_with_zero G₀] [group_with_zero G₀'] [monoid_with_zero_hom_class F G₀ G₀']
(f : F) (a b : G₀)
include G₀'
/-- A monoid homomorphism between groups with zeros sending `0` to `0` sends `a⁻¹` to `(f a)⁻¹`. -/
@[simp] lemma map_inv₀ : f a⁻¹ = (f a)⁻¹ :=
begin
by_cases h : a = 0, by simp [h],
apply eq_inv_of_mul_eq_one_left,
rw [← map_mul, inv_mul_cancel h, map_one]
end
@[simp] lemma map_div₀ : f (a / b) = f a / f b := map_div' f (map_inv₀ f) a b
end group_with_zero
/-- We define the inverse as a `monoid_with_zero_hom` by extending the inverse map by zero
on non-units. -/
noncomputable
def monoid_with_zero.inverse {M : Type*} [comm_monoid_with_zero M] :
M →*₀ M :=
{ to_fun := ring.inverse,
map_zero' := ring.inverse_zero _,
map_one' := ring.inverse_one _,
map_mul' := λ x y, (ring.mul_inverse_rev x y).trans (mul_comm _ _) }
@[simp]
lemma monoid_with_zero.coe_inverse {M : Type*} [comm_monoid_with_zero M] :
(monoid_with_zero.inverse : M → M) = ring.inverse := rfl
@[simp]
lemma monoid_with_zero.inverse_apply {M : Type*} [comm_monoid_with_zero M] (a : M) :
monoid_with_zero.inverse a = ring.inverse a := rfl
/-- Inversion on a commutative group with zero, considered as a monoid with zero homomorphism. -/
def inv_monoid_with_zero_hom {G₀ : Type*} [comm_group_with_zero G₀] : G₀ →*₀ G₀ :=
{ map_zero' := inv_zero,
..inv_monoid_hom }
namespace units
variables [group_with_zero G₀]
variables {a b : G₀}
@[simp] lemma smul_mk0 {α : Type*} [has_smul G₀ α] {g : G₀} (hg : g ≠ 0) (a : α) :
(mk0 g hg) • a = g • a :=
rfl
end units
|
dd6e4357dfa5594c073e21937e1f49adef09d380 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/fingerprint.lean | 8296af5ea95aef1271d8c129fba4213652451937 | [
"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 | 744 | lean | open tactic
meta def nat.to_expr (n : nat) : expr := reflect n
run_cmd attribute.fingerprint `reducible >>= trace
definition ex0 : nat :=
by nat.to_expr <$> attribute.fingerprint `reducible >>= exact
attribute [reducible]
definition f : nat := 10
run_cmd attribute.fingerprint `reducible >>= trace
definition ex1 : nat :=
by nat.to_expr <$> attribute.fingerprint `reducible >>= exact
#eval ex1
definition g : nat := 20
run_cmd attribute.fingerprint `reducible >>= trace
definition ex2 : nat :=
by nat.to_expr <$> attribute.fingerprint `reducible >>= exact
#eval ex2
example : ex1 = ex2 :=
rfl
definition h : nat := 20
definition ex3 : nat :=
by nat.to_expr <$> attribute.fingerprint `reducible >>= exact
example : ex1 = ex3 :=
rfl
|
09f1115f00cd3909c3da1fa9892f5c141d46cb9c | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/ring_theory/witt_vector/frobenius.lean | 2f9692e4afaa40f21a7819bbe16da626484f0e2d | [
"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 | 13,571 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import data.nat.multiplicity
import data.zmod.algebra
import ring_theory.witt_vector.basic
import ring_theory.witt_vector.is_poly
import field_theory.perfect_closure
/-!
## The Frobenius operator
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
If `R` has characteristic `p`, then there is a ring endomorphism `frobenius R p`
that raises `r : R` to the power `p`.
By applying `witt_vector.map` to `frobenius R p`, we obtain a ring endomorphism `𝕎 R →+* 𝕎 R`.
It turns out that this endomorphism can be described by polynomials over `ℤ`
that do not depend on `R` or the fact that it has characteristic `p`.
In this way, we obtain a Frobenius endomorphism `witt_vector.frobenius_fun : 𝕎 R → 𝕎 R`
for every commutative ring `R`.
Unfortunately, the aforementioned polynomials can not be obtained using the machinery
of `witt_structure_int` that was developed in `structure_polynomial.lean`.
We therefore have to define the polynomials by hand, and check that they have the required property.
In case `R` has characteristic `p`, we show in `frobenius_fun_eq_map_frobenius`
that `witt_vector.frobenius_fun` is equal to `witt_vector.map (frobenius R p)`.
### Main definitions and results
* `frobenius_poly`: the polynomials that describe the coefficients of `frobenius_fun`;
* `frobenius_fun`: the Frobenius endomorphism on Witt vectors;
* `frobenius_fun_is_poly`: the tautological assertion that Frobenius is a polynomial function;
* `frobenius_fun_eq_map_frobenius`: the fact that in characteristic `p`, Frobenius is equal to
`witt_vector.map (frobenius R p)`.
TODO: Show that `witt_vector.frobenius_fun` is a ring homomorphism,
and bundle it into `witt_vector.frobenius`.
## References
* [Hazewinkel, *Witt Vectors*][Haze09]
* [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21]
-/
namespace witt_vector
variables {p : ℕ} {R S : Type*} [hp : fact p.prime] [comm_ring R] [comm_ring S]
local notation `𝕎` := witt_vector p -- type as `\bbW`
noncomputable theory
open mv_polynomial finset
open_locale big_operators
variables (p)
include hp
/-- The rational polynomials that give the coefficients of `frobenius x`,
in terms of the coefficients of `x`.
These polynomials actually have integral coefficients,
see `frobenius_poly` and `map_frobenius_poly`. -/
def frobenius_poly_rat (n : ℕ) : mv_polynomial ℕ ℚ :=
bind₁ (witt_polynomial p ℚ ∘ λ n, n + 1) (X_in_terms_of_W p ℚ n)
lemma bind₁_frobenius_poly_rat_witt_polynomial (n : ℕ) :
bind₁ (frobenius_poly_rat p) (witt_polynomial p ℚ n) = (witt_polynomial p ℚ (n+1)) :=
begin
delta frobenius_poly_rat,
rw [← bind₁_bind₁, bind₁_X_in_terms_of_W_witt_polynomial, bind₁_X_right],
end
/-- An auxiliary definition, to avoid an excessive amount of finiteness proofs
for `multiplicity p n`. -/
private def pnat_multiplicity (n : ℕ+) : ℕ :=
(multiplicity p n).get $ multiplicity.finite_nat_iff.mpr $ ⟨ne_of_gt hp.1.one_lt, n.2⟩
local notation `v` := pnat_multiplicity
/-- An auxiliary polynomial over the integers, that satisfies
`p * (frobenius_poly_aux p n) + X n ^ p = frobenius_poly p n`.
This makes it easy to show that `frobenius_poly p n` is congruent to `X n ^ p`
modulo `p`. -/
noncomputable def frobenius_poly_aux : ℕ → mv_polynomial ℕ ℤ
| n := X (n + 1) - ∑ i : fin n, have _ := i.is_lt,
∑ j in range (p ^ (n - i)),
(X i ^ p) ^ (p ^ (n - i) - (j + 1)) *
(frobenius_poly_aux i) ^ (j + 1) *
C ↑((p ^ (n - i)).choose (j + 1) / (p ^ (n - i - v p ⟨j + 1, nat.succ_pos j⟩)) *
↑p ^ (j - v p ⟨j + 1, nat.succ_pos j⟩) : ℕ)
lemma frobenius_poly_aux_eq (n : ℕ) :
frobenius_poly_aux p n =
X (n + 1) - ∑ i in range n, ∑ j in range (p ^ (n - i)),
(X i ^ p) ^ (p ^ (n - i) - (j + 1)) *
(frobenius_poly_aux p i) ^ (j + 1) *
C ↑((p ^ (n - i)).choose (j + 1) / (p ^ (n - i - v p ⟨j + 1, nat.succ_pos j⟩)) *
↑p ^ (j - v p ⟨j + 1, nat.succ_pos j⟩) : ℕ) :=
by { rw [frobenius_poly_aux, ← fin.sum_univ_eq_sum_range] }
/-- The polynomials that give the coefficients of `frobenius x`,
in terms of the coefficients of `x`. -/
def frobenius_poly (n : ℕ) : mv_polynomial ℕ ℤ :=
X n ^ p + C ↑p * (frobenius_poly_aux p n)
/-
Our next goal is to prove
```
lemma map_frobenius_poly (n : ℕ) :
mv_polynomial.map (int.cast_ring_hom ℚ) (frobenius_poly p n) = frobenius_poly_rat p n
```
This lemma has a rather long proof, but it mostly boils down to applying induction,
and then using the following two key facts at the right point.
-/
/-- A key divisibility fact for the proof of `witt_vector.map_frobenius_poly`. -/
lemma map_frobenius_poly.key₁ (n j : ℕ) (hj : j < p ^ (n)) :
p ^ (n - v p ⟨j + 1, j.succ_pos⟩) ∣ (p ^ n).choose (j + 1) :=
begin
apply multiplicity.pow_dvd_of_le_multiplicity,
rw [hp.out.multiplicity_choose_prime_pow hj j.succ_ne_zero],
refl,
end
/-- A key numerical identity needed for the proof of `witt_vector.map_frobenius_poly`. -/
lemma map_frobenius_poly.key₂ {n i j : ℕ} (hi : i ≤ n) (hj : j < p ^ (n - i)) :
j - v p ⟨j + 1, j.succ_pos⟩ + n = i + j + (n - i - v p ⟨j + 1, j.succ_pos⟩) :=
begin
generalize h : (v p ⟨j + 1, j.succ_pos⟩) = m,
rsuffices ⟨h₁, h₂⟩ : m ≤ n - i ∧ m ≤ j,
{ rw [tsub_add_eq_add_tsub h₂, add_comm i j,
add_tsub_assoc_of_le (h₁.trans (nat.sub_le n i)), add_assoc, tsub_right_comm, add_comm i,
tsub_add_cancel_of_le (le_tsub_of_add_le_right ((le_tsub_iff_left hi).mp h₁))] },
have hle : p ^ m ≤ j + 1,
from h ▸ nat.le_of_dvd j.succ_pos (multiplicity.pow_multiplicity_dvd _),
exact ⟨(pow_le_pow_iff hp.1.one_lt).1 (hle.trans hj),
nat.le_of_lt_succ ((nat.lt_pow_self hp.1.one_lt m).trans_le hle)⟩
end
lemma map_frobenius_poly (n : ℕ) :
mv_polynomial.map (int.cast_ring_hom ℚ) (frobenius_poly p n) = frobenius_poly_rat p n :=
begin
rw [frobenius_poly, ring_hom.map_add, ring_hom.map_mul, ring_hom.map_pow, map_C, map_X,
eq_int_cast, int.cast_coe_nat, frobenius_poly_rat],
apply nat.strong_induction_on n, clear n,
intros n IH,
rw [X_in_terms_of_W_eq],
simp only [alg_hom.map_sum, alg_hom.map_sub, alg_hom.map_mul, alg_hom.map_pow, bind₁_C_right],
have h1 : (↑p ^ n) * (⅟ (↑p : ℚ) ^ n) = 1 := by rw [←mul_pow, mul_inv_of_self, one_pow],
rw [bind₁_X_right, function.comp_app, witt_polynomial_eq_sum_C_mul_X_pow, sum_range_succ,
sum_range_succ, tsub_self, add_tsub_cancel_left, pow_zero, pow_one, pow_one, sub_mul,
add_mul, add_mul, mul_right_comm, mul_right_comm (C (↑p ^ (n + 1))), ←C_mul, ←C_mul, pow_succ,
mul_assoc ↑p (↑p ^ n), h1, mul_one, C_1, one_mul, add_comm _ (X n ^ p), add_assoc, ←add_sub,
add_right_inj, frobenius_poly_aux_eq, ring_hom.map_sub, map_X, mul_sub, sub_eq_add_neg,
add_comm _ (C ↑p * X (n + 1)), ←add_sub, add_right_inj, neg_eq_iff_eq_neg, neg_sub, eq_comm],
simp only [ring_hom.map_sum, mul_sum, sum_mul, ←sum_sub_distrib],
apply sum_congr rfl,
intros i hi,
rw mem_range at hi,
rw [← IH i hi],
clear IH,
rw [add_comm (X i ^ p), add_pow, sum_range_succ', pow_zero, tsub_zero, nat.choose_zero_right,
one_mul, nat.cast_one, mul_one, mul_add, add_mul, nat.succ_sub (le_of_lt hi),
nat.succ_eq_add_one (n - i), pow_succ, pow_mul, add_sub_cancel, mul_sum, sum_mul],
apply sum_congr rfl,
intros j hj,
rw mem_range at hj,
rw [ring_hom.map_mul, ring_hom.map_mul, ring_hom.map_pow, ring_hom.map_pow, ring_hom.map_pow,
ring_hom.map_pow, ring_hom.map_pow, map_C, map_X, mul_pow],
rw [mul_comm (C ↑p ^ i), mul_comm _ ((X i ^ p) ^ _), mul_comm (C ↑p ^ (j + 1)), mul_comm (C ↑p)],
simp only [mul_assoc],
apply congr_arg,
apply congr_arg,
rw [←C_eq_coe_nat],
simp only [←ring_hom.map_pow, ←C_mul],
rw C_inj,
simp only [inv_of_eq_inv, eq_int_cast, inv_pow, int.cast_coe_nat, nat.cast_mul,
int.cast_mul],
rw [rat.coe_nat_div _ _ (map_frobenius_poly.key₁ p (n - i) j hj)],
simp only [nat.cast_pow, pow_add, pow_one],
suffices : ((p ^ (n - i)).choose (j + 1) * p ^ (j - v p ⟨j + 1, j.succ_pos⟩) * p * p ^ n : ℚ) =
p ^ j * p * ((p ^ (n - i)).choose (j + 1) * p ^ i) * p ^ (n - i - v p ⟨j + 1, j.succ_pos⟩),
{ have aux : ∀ k : ℕ, (p ^ k : ℚ) ≠ 0,
{ intro, apply pow_ne_zero, exact_mod_cast hp.1.ne_zero },
simpa [aux, -one_div] with field_simps using this.symm },
rw [mul_comm _ (p : ℚ), mul_assoc, mul_assoc, ← pow_add, map_frobenius_poly.key₂ p hi.le hj],
ring_exp
end
lemma frobenius_poly_zmod (n : ℕ) :
mv_polynomial.map (int.cast_ring_hom (zmod p)) (frobenius_poly p n) = X n ^ p :=
begin
rw [frobenius_poly, ring_hom.map_add, ring_hom.map_pow, ring_hom.map_mul, map_X, map_C],
simp only [int.cast_coe_nat, add_zero, eq_int_cast, zmod.nat_cast_self, zero_mul, C_0],
end
@[simp]
lemma bind₁_frobenius_poly_witt_polynomial (n : ℕ) :
bind₁ (frobenius_poly p) (witt_polynomial p ℤ n) = (witt_polynomial p ℤ (n+1)) :=
begin
apply mv_polynomial.map_injective (int.cast_ring_hom ℚ) int.cast_injective,
simp only [map_bind₁, map_frobenius_poly, bind₁_frobenius_poly_rat_witt_polynomial,
map_witt_polynomial],
end
variables {p}
/-- `frobenius_fun` is the function underlying the ring endomorphism
`frobenius : 𝕎 R →+* frobenius 𝕎 R`. -/
def frobenius_fun (x : 𝕎 R) : 𝕎 R :=
mk p $ λ n, mv_polynomial.aeval x.coeff (frobenius_poly p n)
lemma coeff_frobenius_fun (x : 𝕎 R) (n : ℕ) :
coeff (frobenius_fun x) n = mv_polynomial.aeval x.coeff (frobenius_poly p n) :=
by rw [frobenius_fun, coeff_mk]
variables (p)
/-- `frobenius_fun` is tautologically a polynomial function.
See also `frobenius_is_poly`. -/
@[is_poly] lemma frobenius_fun_is_poly : is_poly p (λ R _Rcr, @frobenius_fun p R _ _Rcr) :=
⟨⟨frobenius_poly p, by { introsI, funext n, apply coeff_frobenius_fun }⟩⟩
variable {p}
@[ghost_simps] lemma ghost_component_frobenius_fun (n : ℕ) (x : 𝕎 R) :
ghost_component n (frobenius_fun x) = ghost_component (n + 1) x :=
by simp only [ghost_component_apply, frobenius_fun, coeff_mk,
← bind₁_frobenius_poly_witt_polynomial, aeval_bind₁]
/--
If `R` has characteristic `p`, then there is a ring endomorphism
that raises `r : R` to the power `p`.
By applying `witt_vector.map` to this endomorphism,
we obtain a ring endomorphism `frobenius R p : 𝕎 R →+* 𝕎 R`.
The underlying function of this morphism is `witt_vector.frobenius_fun`.
-/
def frobenius : 𝕎 R →+* 𝕎 R :=
{ to_fun := frobenius_fun,
map_zero' :=
begin
refine is_poly.ext
((frobenius_fun_is_poly p).comp (witt_vector.zero_is_poly))
((witt_vector.zero_is_poly).comp (frobenius_fun_is_poly p)) _ _ 0,
ghost_simp
end,
map_one' :=
begin
refine is_poly.ext
((frobenius_fun_is_poly p).comp (witt_vector.one_is_poly))
((witt_vector.one_is_poly).comp (frobenius_fun_is_poly p)) _ _ 0,
ghost_simp
end,
map_add' := by ghost_calc _ _; ghost_simp,
map_mul' := by ghost_calc _ _; ghost_simp }
lemma coeff_frobenius (x : 𝕎 R) (n : ℕ) :
coeff (frobenius x) n = mv_polynomial.aeval x.coeff (frobenius_poly p n) :=
coeff_frobenius_fun _ _
@[ghost_simps] lemma ghost_component_frobenius (n : ℕ) (x : 𝕎 R) :
ghost_component n (frobenius x) = ghost_component (n + 1) x :=
ghost_component_frobenius_fun _ _
variables (p)
/-- `frobenius` is tautologically a polynomial function. -/
@[is_poly] lemma frobenius_is_poly : is_poly p (λ R _Rcr, @frobenius p R _ _Rcr) :=
frobenius_fun_is_poly _
section char_p
variables [char_p R p]
@[simp]
lemma coeff_frobenius_char_p (x : 𝕎 R) (n : ℕ) :
coeff (frobenius x) n = (x.coeff n) ^ p :=
begin
rw [coeff_frobenius],
letI : algebra (zmod p) R := zmod.algebra _ _,
-- outline of the calculation, proofs follow below
calc aeval (λ k, x.coeff k) (frobenius_poly p n)
= aeval (λ k, x.coeff k)
(mv_polynomial.map (int.cast_ring_hom (zmod p)) (frobenius_poly p n)) : _
... = aeval (λ k, x.coeff k) (X n ^ p : mv_polynomial ℕ (zmod p)) : _
... = (x.coeff n) ^ p : _,
{ conv_rhs { rw [aeval_eq_eval₂_hom, eval₂_hom_map_hom] },
apply eval₂_hom_congr (ring_hom.ext_int _ _) rfl rfl },
{ rw frobenius_poly_zmod },
{ rw [alg_hom.map_pow, aeval_X] }
end
lemma frobenius_eq_map_frobenius :
@frobenius p R _ _ = map (_root_.frobenius R p) :=
begin
ext x n,
simp only [coeff_frobenius_char_p, map_coeff, frobenius_def],
end
@[simp]
lemma frobenius_zmodp (x : 𝕎 (zmod p)) :
(frobenius x) = x :=
by simp only [ext_iff, coeff_frobenius_char_p, zmod.pow_card, eq_self_iff_true, forall_const]
variables (p R)
/-- `witt_vector.frobenius` as an equiv. -/
@[simps {fully_applied := ff}]
def frobenius_equiv [perfect_ring R p] : witt_vector p R ≃+* witt_vector p R :=
{ to_fun := witt_vector.frobenius,
inv_fun := map (pth_root R p),
left_inv := λ f, ext $ λ n, by { rw frobenius_eq_map_frobenius, exact pth_root_frobenius _ },
right_inv := λ f, ext $ λ n, by { rw frobenius_eq_map_frobenius, exact frobenius_pth_root _ },
..(witt_vector.frobenius : witt_vector p R →+* witt_vector p R) }
lemma frobenius_bijective [perfect_ring R p] :
function.bijective (@witt_vector.frobenius p R _ _) :=
(frobenius_equiv p R).bijective
end char_p
end witt_vector
|
d318487671e0af02f63ac9089bdc83809753d7de | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /stage0/src/Init/Data/ToString/Basic.lean | 865e916cc9dbf5ec5ea076344f9e0f43493ae00d | [
"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 | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 4,457 | 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.String.Basic
import Init.Data.UInt
import Init.Data.Nat.Div
import Init.Data.Repr
import Init.Data.Int.Basic
import Init.Data.Format.Basic
import Init.Control.Id
import Init.Control.Option
open Sum Subtype Nat
open Std
class ToString (α : Type u) where
toString : α → String
export ToString (toString)
-- This instance is needed because `id` is not reducible
instance {α} [ToString α] : ToString (id α) :=
inferInstanceAs (ToString α)
instance {α} [ToString α] : ToString (Id α) :=
inferInstanceAs (ToString α)
instance : ToString String :=
⟨fun s => s⟩
instance : ToString Substring :=
⟨fun s => s.toString⟩
instance : ToString String.Iterator :=
⟨fun it => it.remainingToString⟩
instance : ToString Bool :=
⟨fun b => cond b "true" "false"⟩
instance {p : Prop} : ToString (Decidable p) := ⟨fun h =>
match h with
| Decidable.isTrue _ => "true"
| Decidable.isFalse _ => "false"⟩
protected def List.toStringAux {α : Type u} [ToString α] : Bool → List α → String
| _, [] => ""
| true, x::xs => toString x ++ List.toStringAux false xs
| false, x::xs => ", " ++ toString x ++ List.toStringAux false xs
protected def List.toString {α : Type u} [ToString α] : List α → String
| [] => "[]"
| x::xs => "[" ++ List.toStringAux true (x::xs) ++ "]"
instance {α : Type u} [ToString α] : ToString (List α) :=
⟨List.toString⟩
instance : ToString PUnit.{u+1} :=
⟨fun _ => "()"⟩
instance {α : Type u} [ToString α] : ToString (ULift.{v} α) :=
⟨fun v => toString v.1⟩
instance : ToString Unit :=
⟨fun _ => "()"⟩
instance : ToString Nat :=
⟨fun n => Nat.repr n⟩
instance : ToString String.Pos :=
⟨fun p => Nat.repr p.byteIdx⟩
instance : ToString Int where
toString
| Int.ofNat m => toString m
| Int.negSucc m => "-" ++ toString (succ m)
instance : ToString Char :=
⟨fun c => c.toString⟩
instance (n : Nat) : ToString (Fin n) :=
⟨fun f => toString (Fin.val f)⟩
instance : ToString UInt8 :=
⟨fun n => toString n.toNat⟩
instance : ToString UInt16 :=
⟨fun n => toString n.toNat⟩
instance : ToString UInt32 :=
⟨fun n => toString n.toNat⟩
instance : ToString UInt64 :=
⟨fun n => toString n.toNat⟩
instance : ToString USize :=
⟨fun n => toString n.toNat⟩
instance : ToString Format where
toString f := f.pretty
def addParenHeuristic (s : String) : String :=
if "(".isPrefixOf s || "[".isPrefixOf s || "{".isPrefixOf s || "#[".isPrefixOf s then s
else if !s.any Char.isWhitespace then s
else "(" ++ s ++ ")"
instance {α : Type u} [ToString α] : ToString (Option α) := ⟨fun
| none => "none"
| (some a) => "(some " ++ addParenHeuristic (toString a) ++ ")"⟩
instance {α : Type u} {β : Type v} [ToString α] [ToString β] : ToString (Sum α β) := ⟨fun
| (inl a) => "(inl " ++ addParenHeuristic (toString a) ++ ")"
| (inr b) => "(inr " ++ addParenHeuristic (toString b) ++ ")"⟩
instance {α : Type u} {β : Type v} [ToString α] [ToString β] : ToString (α × β) := ⟨fun (a, b) =>
"(" ++ toString a ++ ", " ++ toString b ++ ")"⟩
instance {α : Type u} {β : α → Type v} [ToString α] [∀ x, ToString (β x)] : ToString (Sigma β) := ⟨fun ⟨a, b⟩ =>
"⟨" ++ toString a ++ ", " ++ toString b ++ "⟩"⟩
instance {α : Type u} {p : α → Prop} [ToString α] : ToString (Subtype p) := ⟨fun s =>
toString (val s)⟩
def String.toInt? (s : String) : Option Int := do
if s.get 0 = '-' then do
let v ← (s.toSubstring.drop 1).toNat?;
pure <| - Int.ofNat v
else
Int.ofNat <$> s.toNat?
def String.isInt (s : String) : Bool :=
if s.get 0 = '-' then
(s.toSubstring.drop 1).isNat
else
s.isNat
def String.toInt! (s : String) : Int :=
match s.toInt? with
| some v => v
| none => panic "Int expected"
instance [ToString ε] [ToString α] : ToString (Except ε α) where
toString
| Except.error e => "error: " ++ toString e
| Except.ok a => "ok: " ++ toString a
instance [Repr ε] [Repr α] : Repr (Except ε α) where
reprPrec
| Except.error e, prec => Repr.addAppParen ("Except.error " ++ reprArg e) prec
| Except.ok a, prec => Repr.addAppParen ("Except.ok " ++ reprArg a) prec
|
0329f724a8ef139a9fbc1f36ad63cbb9b289b42c | d7189ea2ef694124821b033e533f18905b5e87ef | /galois/crypto/merkle/merkle.lean | 8c045e84eefbaaaeda9a6f4722edee4ec69abd26 | [
"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 | 6,273 | lean | import galois.data.bounded_list
import galois.list.take_drop_lemmas
import galois.list.member
universes u v
namespace merkle
structure combine_result (Link Data : Type) :=
(root : Data)
(left : Link)
(right : Link)
/-- Operations needed to build a Merkle tree.-/
structure ops (Leaf Link Data : Type) :=
(empty_root : Data)
(leaf_data : Leaf → Data)
(combine_data : Data -> Data → combine_result Link Data)
/-- This stores information about a path from a leaf to a root.
max_req_level bounds the length of the links.
-/
structure path (Leaf Link : Type) (max_req_level : ℕ) :=
(leaf : Leaf)
(links : bounded_list Link max_req_level)
/-- This represents a result of computing a merkle tree-/
structure result (Leaf Link Data : Type) (max_req_level : ℕ) :=
-- what we are aggregating
(root : Data)
-- the paths through the tree to get to this point
(paths : list (path Leaf Link max_req_level))
-- what we have not yet aggregated
(remaining : list Leaf)
section path
parameters {Leaf Link : Type}
def path.extend_none {n:ℕ} (p : path Leaf Link n)
: path Leaf Link n.succ :=
{ links := p.links.extend 1
, leaf := p.leaf
}
theorem path.leaf_extend_none (n : ℕ)
: path.leaf ∘ @path.extend_none n = path.leaf :=
begin
apply funext,
intro p,
cases p,
simp [function.comp, path.extend_none],
end
def path.extend_sibling {n : ℕ}
(l : Link)
(p : path Leaf Link n)
: path Leaf Link n.succ :=
{ links := p.links.cons l
, leaf := p.leaf
}
theorem path.leaf_extend_sibling (n : ℕ) (l : Link)
: path.leaf ∘ @path.extend_sibling n l = path.leaf :=
begin
apply funext,
intro p,
cases p,
simp [function.comp, path.extend_sibling],
end
end path
section merkle
-- This contains the types for the leaves of the tree and link elements
parameters {Leaf Link Data : Type}
parameter (ops : ops Leaf Link Data)
def merkle_core
: Π (n : ℕ) (e : Leaf) (rest : list Leaf), result Leaf Link Data n
| 0 e rest :=
{ root := ops.leaf_data e
, paths := [{ leaf := e
, links := bounded_list.nil
}]
, remaining := rest
}
| (nat.succ n) e rest := do
let left_result := merkle_core n e rest in
match left_result.remaining with
| [] :=
{ root := left_result.root
, paths := left_result.paths.map path.extend_none
, remaining := []
}
| (e' :: rest') :=
let right_result := merkle_core n e' rest' in
let z := ops.combine_data left_result.root right_result.root in
{ root := z.root
, paths := left_result.paths.map (path.extend_sibling z.left)
++ right_result.paths.map (path.extend_sibling z.right)
, remaining := right_result.remaining
}
end
section proofs
theorem merkle_core_ind
{P : ℕ → Leaf → list Leaf → Prop}
(n : ℕ) (e : Leaf) (l : list Leaf)
(base : ∀ (e:Leaf) (l:list Leaf), P 0 e l)
(ind1 : ∀ (n:ℕ) (e:Leaf) (l:list Leaf)
(pr : (merkle_core n e l).remaining = list.nil)
(ind : P n e l), P n.succ e l)
(ind2 : ∀ (n:ℕ) (e:Leaf) (l:list Leaf) (v : Leaf) (r : list Leaf)
(pr : (merkle_core n e l).remaining = v :: r)
(ind : P n e l), P n.succ e l)
: P n e l :=
begin
induction n,
case nat.zero { apply base },
case nat.succ m ind {
destruct (merkle_core ops m e l).remaining,
{ intro rl,
exact ind1 m e l rl ind,
},
{
intros v r rl,
exact ind2 m e l v r rl ind,
}
}
end
parameters (n :ℕ) (e : Leaf) (l : list Leaf)
theorem merkle_core_paths_ne : (merkle_core n e l).paths ≠ [] :=
begin
induction n,
{ simp [merkle_core], },
case nat.succ n ind {
simp [merkle_core],
cases (merkle_core ops n e l).remaining,
all_goals {
simp [merkle_core],
cases (merkle_core ops n e l).paths,
{ contradiction, },
{ simp, },
},
}
end
theorem merkle_core_remaining (n:ℕ) (e : Leaf) (l : list Leaf)
: (merkle_core n e l).remaining = l.drop (2^n-1) :=
begin
revert e l,
induction n,
case nat.zero {
intros e l,
simp [merkle_core],
},
case nat.succ n ind {
intros e l,
simp only [merkle_core, ind],
have h : (2^nat.succ n - 1) = (2^n-1) + 2^n,
{
simp [nat.pow, nat.succ_mul],
simp [nat.add_sub_assoc, nat.one_le_pow, nat.succ_le_succ_iff
, nat.zero_le],
},
rw [h, list.drop_add],
cases (l.drop (2^n - 1)) with r rest,
{ simp [merkle_core], },
{ simp [merkle_core, ind, nat.pow_is_zero_iff], },
},
end
theorem merkle_core_list_lengths (n:ℕ) (e : Leaf) (l : list Leaf)
: (merkle_core n e l).paths.length + (merkle_core n e l).remaining.length
= l.length + 1 :=
begin
revert e l,
induction n,
case nat.zero {
intros e l,
simp [merkle_core],
},
case nat.succ n ind {
intros e l,
simp only [merkle_core],
destruct (merkle_core ops n e l).remaining,
case list.nil {
intro eq,
rw [eq],
simp only [merkle_core, list.length_map],
have pr := ind e l, rw [eq] at pr,
apply pr,
},
case list.cons {
intros r rest eq, rw [eq],
simp only [merkle_core, list.length_append, list.length_map],
rw [ add_assoc, ind],
have final := ind e l,
rw [eq] at final,
exact final,
}
},
end
end proofs
def merkle
: Π (n : ℕ) (l : list Leaf), result Leaf Link Data n
| n [] :=
{ root := ops.empty_root
, paths := []
, remaining := []
}
| n (e::r) := merkle_core n e r
section merkel_proofs
parameters (n : ℕ) (l : list Leaf)
theorem merkle_paths_ne (pr : l ≠ []) : (merkle n l).paths ≠ [] :=
begin
cases l,
{ contradiction, },
case list.cons e r {
simp [merkle],
apply merkle_core_paths_ne,
}
end
theorem merkle_paths_length (n:ℕ) (l : list Leaf)
: (merkle n l).paths.length = min (2^n) l.length :=
begin
cases l,
case list.nil { simp [merkle], },
case list.cons e l {
simp [merkle],
have pr := merkle_core_list_lengths ops n e l,
simp [merkle_core_remaining] at pr,
admit,
},
end
theorem merkel_paths_nth_leaf {n:ℕ} {l : list Leaf}
{idx : ℕ}
{r : Leaf}
(r_at_idx : l.nth idx = some r)
{p : path Leaf Link n}
(pr : (merkle n l).paths.nth idx = some p)
: p.leaf = r :=
begin
admit
end
end merkel_proofs
end merkle
end merkle
|
97aeeb4dd413defd5b4a4416261db064b1295a35 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/algebra/group/basic.lean | 3f3ba7f1dea99b5952f06dc2b8da338a6a2537a6 | [
"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 | 13,603 | 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, Simon Hudon, Mario Carneiro
-/
import algebra.group.defs
import logic.function.basic
universe u
section monoid
variables {M : Type u} [monoid M]
@[to_additive]
lemma ite_mul_one {P : Prop} [decidable P] {a b : M} :
ite P (a * b) 1 = ite P a 1 * ite P b 1 :=
by { by_cases h : P; simp [h], }
end monoid
section comm_semigroup
variables {G : Type u} [comm_semigroup G]
@[no_rsimp, to_additive]
lemma mul_left_comm : ∀ a b c : G, a * (b * c) = b * (a * c) :=
left_comm has_mul.mul mul_comm mul_assoc
attribute [no_rsimp] add_left_comm
@[to_additive]
lemma mul_right_comm : ∀ a b c : G, a * b * c = a * c * b :=
right_comm has_mul.mul mul_comm mul_assoc
@[to_additive]
theorem mul_mul_mul_comm (a b c d : G) : (a * b) * (c * d) = (a * c) * (b * d) :=
by simp only [mul_left_comm, mul_assoc]
end comm_semigroup
local attribute [simp] mul_assoc sub_eq_add_neg
section add_monoid
variables {M : Type u} [add_monoid M] {a b c : M}
@[simp] lemma bit0_zero : bit0 (0 : M) = 0 := add_zero _
@[simp] lemma bit1_zero [has_one M] : bit1 (0 : M) = 1 :=
by rw [bit1, bit0_zero, zero_add]
end add_monoid
section comm_monoid
variables {M : Type u} [comm_monoid M] {x y z : M}
@[to_additive] lemma inv_unique (hy : x * y = 1) (hz : x * z = 1) : y = z :=
left_inv_eq_right_inv (trans (mul_comm _ _) hy) hz
end comm_monoid
section left_cancel_monoid
variables {M : Type u} [left_cancel_monoid M]
@[to_additive] lemma eq_one_of_mul_self_left_cancel {a : M} (h : a * a = a) : a = 1 :=
mul_left_cancel (show a * a = a * 1, by rwa mul_one)
@[to_additive] lemma eq_one_of_left_cancel_mul_self {a : M} (h : a = a * a) : a = 1 :=
mul_left_cancel (show a * a = a * 1, by rwa [mul_one, eq_comm])
end left_cancel_monoid
section right_cancel_monoid
variables {M : Type u} [right_cancel_monoid M]
@[to_additive] lemma eq_one_of_mul_self_right_cancel {a : M} (h : a * a = a) : a = 1 :=
mul_right_cancel (show a * a = 1 * a, by rwa one_mul)
@[to_additive] lemma eq_one_of_right_cancel_mul_self {a : M} (h : a = a * a) : a = 1 :=
mul_right_cancel (show a * a = 1 * a, by rwa [one_mul, eq_comm])
end right_cancel_monoid
section group
variables {G : Type u} [group G] {a b c : G}
@[simp, to_additive]
lemma inv_mul_cancel_right (a b : G) : a * b⁻¹ * b = a :=
by simp [mul_assoc]
@[simp, to_additive neg_zero]
lemma one_inv : 1⁻¹ = (1 : G) :=
inv_eq_of_mul_eq_one (one_mul 1)
@[to_additive]
theorem left_inverse_inv (G) [group G] :
function.left_inverse (λ a : G, a⁻¹) (λ a, a⁻¹) :=
inv_inv
@[simp, to_additive]
lemma inv_involutive : function.involutive (has_inv.inv : G → G) := inv_inv
@[to_additive]
lemma inv_injective : function.injective (has_inv.inv : G → G) :=
inv_involutive.injective
@[simp, to_additive] theorem inv_inj : a⁻¹ = b⁻¹ ↔ a = b := inv_injective.eq_iff
@[simp, to_additive]
lemma mul_inv_cancel_left (a b : G) : a * (a⁻¹ * b) = b :=
by rw [← mul_assoc, mul_right_inv, one_mul]
@[to_additive]
theorem mul_left_surjective (a : G) : function.surjective ((*) a) :=
λ x, ⟨a⁻¹ * x, mul_inv_cancel_left a x⟩
@[to_additive]
theorem mul_right_surjective (a : G) : function.surjective (λ x, x * a) :=
λ x, ⟨x * a⁻¹, inv_mul_cancel_right x a⟩
@[simp, to_additive neg_add_rev]
lemma mul_inv_rev (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ :=
inv_eq_of_mul_eq_one $ by simp
@[to_additive]
lemma eq_inv_of_eq_inv (h : a = b⁻¹) : b = a⁻¹ :=
by simp [h]
@[to_additive]
lemma eq_inv_of_mul_eq_one (h : a * b = 1) : a = b⁻¹ :=
have a⁻¹ = b, from inv_eq_of_mul_eq_one h,
by simp [this.symm]
@[to_additive]
lemma eq_mul_inv_of_mul_eq (h : a * c = b) : a = b * c⁻¹ :=
by simp [h.symm]
@[to_additive]
lemma eq_inv_mul_of_mul_eq (h : b * a = c) : a = b⁻¹ * c :=
by simp [h.symm]
@[to_additive]
lemma inv_mul_eq_of_eq_mul (h : b = a * c) : a⁻¹ * b = c :=
by simp [h]
@[to_additive]
lemma mul_inv_eq_of_eq_mul (h : a = c * b) : a * b⁻¹ = c :=
by simp [h]
@[to_additive]
lemma eq_mul_of_mul_inv_eq (h : a * c⁻¹ = b) : a = b * c :=
by simp [h.symm]
@[to_additive]
lemma eq_mul_of_inv_mul_eq (h : b⁻¹ * a = c) : a = b * c :=
by simp [h.symm, mul_inv_cancel_left]
@[to_additive]
lemma mul_eq_of_eq_inv_mul (h : b = a⁻¹ * c) : a * b = c :=
by rw [h, mul_inv_cancel_left]
@[to_additive]
lemma mul_eq_of_eq_mul_inv (h : a = c * b⁻¹) : a * b = c :=
by simp [h]
@[to_additive]
theorem mul_self_iff_eq_one : a * a = a ↔ a = 1 :=
by have := @mul_right_inj _ _ a a 1; rwa mul_one at this
@[simp, to_additive]
theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 :=
by rw [← @inv_inj _ _ a 1, one_inv]
@[simp, to_additive]
theorem one_eq_inv : 1 = a⁻¹ ↔ a = 1 :=
by rw [eq_comm, inv_eq_one]
@[to_additive]
theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 :=
not_congr inv_eq_one
@[to_additive]
theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ :=
⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩
@[to_additive]
theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a :=
eq_comm.trans $ eq_inv_iff_eq_inv.trans eq_comm
@[to_additive]
theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ :=
by simpa [mul_left_inv, -mul_left_inj] using @mul_left_inj _ _ b a (b⁻¹)
@[to_additive]
theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b :=
by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm]
@[to_additive]
theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 :=
mul_eq_one_iff_eq_inv.symm
@[to_additive]
theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 :=
mul_eq_one_iff_inv_eq.symm
@[to_additive]
theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b :=
⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩
@[to_additive]
theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c :=
⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩
@[to_additive]
theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c :=
⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩
@[to_additive]
theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b :=
⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩
@[to_additive]
theorem mul_inv_eq_one : a * b⁻¹ = 1 ↔ a = b :=
by rw [mul_eq_one_iff_eq_inv, inv_inv]
@[to_additive]
theorem inv_mul_eq_one : a⁻¹ * b = 1 ↔ a = b :=
by rw [mul_eq_one_iff_eq_inv, inv_inj]
@[simp, to_additive]
lemma mul_left_eq_self : a * b = b ↔ a = 1 :=
⟨λ h, @mul_right_cancel _ _ a b 1 (by simp [h]), λ h, by simp [h]⟩
@[simp, to_additive]
lemma mul_right_eq_self : a * b = a ↔ b = 1 :=
⟨λ h, @mul_left_cancel _ _ a b 1 (by simp [h]), λ h, by simp [h]⟩
end group
section add_group
variables {G : Type u} [add_group G] {a b c d : G}
@[simp] lemma sub_self (a : G) : a - a = 0 :=
add_right_neg a
@[simp] lemma sub_add_cancel (a b : G) : a - b + b = a :=
neg_add_cancel_right a b
@[simp] lemma add_sub_cancel (a b : G) : a + b - b = a :=
add_neg_cancel_right a b
lemma add_sub_assoc (a b c : G) : a + b - c = a + (b - c) :=
by rw [sub_eq_add_neg, add_assoc, ←sub_eq_add_neg]
lemma eq_of_sub_eq_zero (h : a - b = 0) : a = b :=
have 0 + b = b, by rw zero_add,
have (a - b) + b = b, by rwa h,
by rwa [sub_eq_add_neg, neg_add_cancel_right] at this
lemma sub_eq_zero_of_eq (h : a = b) : a - b = 0 :=
by rw [h, sub_self]
lemma sub_eq_zero_iff_eq : a - b = 0 ↔ a = b :=
⟨eq_of_sub_eq_zero, sub_eq_zero_of_eq⟩
@[simp] lemma zero_sub (a : G) : 0 - a = -a :=
zero_add (-a)
@[simp] lemma sub_zero (a : G) : a - 0 = a :=
by rw [sub_eq_add_neg, neg_zero, add_zero]
lemma sub_ne_zero_of_ne (h : a ≠ b) : a - b ≠ 0 :=
begin
intro hab,
apply h,
apply eq_of_sub_eq_zero hab
end
@[simp] lemma sub_neg_eq_add (a b : G) : a - (-b) = a + b :=
by rw [sub_eq_add_neg, neg_neg]
@[simp] lemma neg_sub (a b : G) : -(a - b) = b - a :=
neg_eq_of_add_eq_zero (by rw [sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left,
add_right_neg])
local attribute [simp] add_assoc
lemma add_sub (a b c : G) : a + (b - c) = a + b - c :=
by simp
lemma sub_add_eq_sub_sub_swap (a b c : G) : a - (b + c) = a - c - b :=
by simp
@[simp] lemma add_sub_add_right_eq_sub (a b c : G) : (a + c) - (b + c) = a - b :=
by rw [sub_add_eq_sub_sub_swap]; simp
lemma eq_sub_of_add_eq (h : a + c = b) : a = b - c :=
by simp [h.symm]
lemma sub_eq_of_eq_add (h : a = c + b) : a - b = c :=
by simp [h]
lemma eq_add_of_sub_eq (h : a - c = b) : a = b + c :=
by simp [h.symm]
lemma add_eq_of_eq_sub (h : a = c - b) : a + b = c :=
by simp [h]
@[simp] lemma sub_right_inj : a - b = a - c ↔ b = c :=
(add_right_inj _).trans neg_inj
@[simp] lemma sub_left_inj : b - a = c - a ↔ b = c :=
add_left_inj _
lemma sub_add_sub_cancel (a b c : G) : (a - b) + (b - c) = a - c :=
by rw [← add_sub_assoc, sub_add_cancel]
lemma sub_sub_sub_cancel_right (a b c : G) : (a - c) - (b - c) = a - b :=
by rw [← neg_sub c b, sub_neg_eq_add, sub_add_sub_cancel]
theorem sub_sub_assoc_swap : a - (b - c) = a + c - b :=
by simp
theorem sub_eq_zero : a - b = 0 ↔ a = b :=
⟨eq_of_sub_eq_zero, λ h, by rw [h, sub_self]⟩
theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b :=
not_congr sub_eq_zero
theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b :=
eq_add_neg_iff_add_eq
theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b :=
add_neg_eq_iff_eq_add
theorem eq_iff_eq_of_sub_eq_sub (H : a - b = c - d) : a = b ↔ c = d :=
by rw [← sub_eq_zero, H, sub_eq_zero]
theorem left_inverse_sub_add_left (c : G) : function.left_inverse (λ x, x - c) (λ x, x + c) :=
assume x, add_sub_cancel x c
theorem left_inverse_add_left_sub (c : G) : function.left_inverse (λ x, x + c) (λ x, x - c) :=
assume x, sub_add_cancel x c
theorem left_inverse_add_right_neg_add (c : G) :
function.left_inverse (λ x, c + x) (λ x, - c + x) :=
assume x, add_neg_cancel_left c x
theorem left_inverse_neg_add_add_right (c : G) :
function.left_inverse (λ x, - c + x) (λ x, c + x) :=
assume x, neg_add_cancel_left c x
end add_group
section comm_group
variables {G : Type u} [comm_group G]
@[to_additive neg_add]
lemma mul_inv (a b : G) : (a * b)⁻¹ = a⁻¹ * b⁻¹ :=
by rw [mul_inv_rev, mul_comm]
end comm_group
section add_comm_group
variables {G : Type u} [add_comm_group G] {a b c d : G}
local attribute [simp] add_assoc add_comm add_left_comm sub_eq_add_neg
lemma sub_add_eq_sub_sub (a b c : G) : a - (b + c) = a - b - c :=
by simp
lemma neg_add_eq_sub (a b : G) : -a + b = b - a :=
by simp
lemma sub_add_eq_add_sub (a b c : G) : a - b + c = a + c - b :=
by simp
lemma sub_sub (a b c : G) : a - b - c = a - (b + c) :=
by simp
lemma sub_add (a b c : G) : a - b + c = a - (b - c) :=
by simp
@[simp] lemma add_sub_add_left_eq_sub (a b c : G) : (c + a) - (c + b) = a - b :=
by simp
lemma eq_sub_of_add_eq' (h : c + a = b) : a = b - c :=
by simp [h.symm]
lemma sub_eq_of_eq_add' (h : a = b + c) : a - b = c :=
begin simp [h], rw [add_left_comm], simp end
lemma eq_add_of_sub_eq' (h : a - b = c) : a = b + c :=
by simp [h.symm]
lemma add_eq_of_eq_sub' (h : b = c - a) : a + b = c :=
begin simp [h], rw [add_comm c, add_neg_cancel_left] end
lemma sub_sub_self (a b : G) : a - (a - b) = b :=
begin simp, rw [add_comm b, add_neg_cancel_left] end
lemma add_sub_comm (a b c d : G) : a + b - (c + d) = (a - c) + (b - d) :=
by simp
lemma sub_eq_sub_add_sub (a b c : G) : a - b = c - b + (a - c) :=
begin simp, rw [add_left_comm c], simp end
lemma neg_neg_sub_neg (a b : G) : - (-a - -b) = a - b :=
by simp
@[simp] lemma sub_sub_cancel (a b : G) : a - (a - b) = b := sub_sub_self a b
lemma sub_eq_neg_add (a b : G) : a - b = -b + a :=
add_comm _ _
theorem neg_add' (a b : G) : -(a + b) = -a - b := neg_add a b
@[simp]
lemma neg_sub_neg (a b : G) : -a - -b = b - a :=
by simp [sub_eq_neg_add, add_comm]
lemma eq_sub_iff_add_eq' : a = b - c ↔ c + a = b :=
by rw [eq_sub_iff_add_eq, add_comm]
lemma sub_eq_iff_eq_add' : a - b = c ↔ a = b + c :=
by rw [sub_eq_iff_eq_add, add_comm]
@[simp]
lemma add_sub_cancel' (a b : G) : a + b - a = b :=
by rw [sub_eq_neg_add, neg_add_cancel_left]
@[simp]
lemma add_sub_cancel'_right (a b : G) : a + (b - a) = b :=
by rw [← add_sub_assoc, add_sub_cancel']
-- This lemma is in the `simp` set under the name `add_neg_cancel_comm_assoc`,
-- defined in `algebra/group/commute`
lemma add_add_neg_cancel'_right (a b : G) : a + (b + -a) = b :=
add_sub_cancel'_right a b
lemma sub_right_comm (a b c : G) : a - b - c = a - c - b :=
add_right_comm _ _ _
@[simp] lemma add_add_sub_cancel (a b c : G) : (a + c) + (b - c) = a + b :=
by rw [add_assoc, add_sub_cancel'_right]
@[simp] lemma sub_add_add_cancel (a b c : G) : (a - c) + (b + c) = a + b :=
by rw [add_left_comm, sub_add_cancel, add_comm]
@[simp] lemma sub_add_sub_cancel' (a b c : G) : (a - b) + (c - a) = c - b :=
by rw add_comm; apply sub_add_sub_cancel
@[simp] lemma add_sub_sub_cancel (a b c : G) : (a + b) - (a - c) = b + c :=
by rw [← sub_add, add_sub_cancel']
@[simp] lemma sub_sub_sub_cancel_left (a b c : G) : (c - a) - (c - b) = b - a :=
by rw [← neg_sub b c, sub_neg_eq_add, add_comm, sub_add_sub_cancel]
lemma sub_eq_sub_iff_add_eq_add : a - b = c - d ↔ a + d = c + b :=
begin
rw [sub_eq_iff_eq_add, sub_add_eq_add_sub, eq_comm, sub_eq_iff_eq_add'],
simp only [add_comm, eq_comm]
end
lemma sub_eq_sub_iff_sub_eq_sub : a - b = c - d ↔ a - c = b - d :=
by simp [-sub_eq_add_neg, sub_eq_sub_iff_add_eq_add, add_comm]
end add_comm_group
|
ff838e641cdb7c096673149c537491f2ec516b6b | bb31430994044506fa42fd667e2d556327e18dfe | /src/measure_theory/function/locally_integrable.lean | fc98e48967e7584b6081003d9c25f60957c9ab4d | [
"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 | 12,586 | lean | /-
Copyright (c) 2022 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import measure_theory.integral.integrable_on
/-!
# Locally integrable functions
A function is called *locally integrable* (`measure_theory.locally_integrable`) if it is integrable
on a neighborhood of every point.
This file contains properties of locally integrable functions and integrability results
on compact sets.
## Main statements
* `continuous.locally_integrable`: A continuous function is locally integrable.
-/
open measure_theory measure_theory.measure set function topological_space
open_locale topological_space interval
variables {X Y E R : Type*} [measurable_space X] [topological_space X]
variables [measurable_space Y] [topological_space Y]
variables [normed_add_comm_group E] {f : X → E} {μ : measure X}
namespace measure_theory
/-- A function `f : X → E` is locally integrable if it is integrable on a neighborhood of every
point. In particular, it is integrable on all compact sets,
see `locally_integrable.integrable_on_is_compact`. -/
def locally_integrable (f : X → E) (μ : measure X . volume_tac) : Prop :=
∀ (x : X), integrable_at_filter f (𝓝 x) μ
lemma integrable.locally_integrable (hf : integrable f μ) : locally_integrable f μ :=
λ x, hf.integrable_at_filter _
/-- If a function is locally integrable, then it is integrable on an open neighborhood of any
compact set. -/
lemma locally_integrable.integrable_on_nhds_is_compact (hf : locally_integrable f μ) {k : set X}
(hk : is_compact k) : ∃ u, is_open u ∧ k ⊆ u ∧ integrable_on f u μ :=
begin
refine is_compact.induction_on hk _ _ _ _,
{ refine ⟨∅, is_open_empty, subset.rfl, integrable_on_empty⟩ },
{ rintros s t hst ⟨u, u_open, tu, hu⟩,
exact ⟨u, u_open, hst.trans tu, hu⟩ },
{ rintros s t ⟨u, u_open, su, hu⟩ ⟨v, v_open, tv, hv⟩,
exact ⟨u ∪ v, u_open.union v_open, union_subset_union su tv, hu.union hv⟩ },
{ assume x hx,
rcases hf x with ⟨u, ux, hu⟩,
rcases mem_nhds_iff.1 ux with ⟨v, vu, v_open, xv⟩,
exact ⟨v, nhds_within_le_nhds (v_open.mem_nhds xv), v, v_open, subset.rfl, hu.mono_set vu⟩ }
end
/-- If a function is locally integrable, then it is integrable on any compact set. -/
lemma locally_integrable.integrable_on_is_compact {k : set X} (hf : locally_integrable f μ)
(hk : is_compact k) : integrable_on f k μ :=
begin
rcases hf.integrable_on_nhds_is_compact hk with ⟨u, u_open, ku, hu⟩,
exact hu.mono_set ku
end
lemma locally_integrable_iff [locally_compact_space X] :
locally_integrable f μ ↔ ∀ (k : set X), is_compact k → integrable_on f k μ :=
begin
refine ⟨λ hf k hk, hf.integrable_on_is_compact hk, λ hf x, _⟩,
obtain ⟨K, hK, h2K⟩ := exists_compact_mem_nhds x,
exact ⟨K, h2K, hf K hK⟩,
end
lemma locally_integrable.ae_strongly_measurable [second_countable_topology X]
(hf : locally_integrable f μ) : ae_strongly_measurable f μ :=
begin
have : ∀ x, ∃ u, is_open u ∧ x ∈ u ∧ integrable_on f u μ,
{ assume x,
rcases hf x with ⟨s, hs, h's⟩,
rcases mem_nhds_iff.1 hs with ⟨u, us, u_open, xu⟩,
exact ⟨u, u_open, xu, h's.mono_set us⟩ },
choose u u_open xu hu using this,
obtain ⟨T, T_count, hT⟩ : ∃ (T : set X), T.countable ∧ (⋃ (i : T), u i) = univ,
{ have : (⋃ x, u x) = univ, from eq_univ_of_forall (λ x, mem_Union_of_mem x (xu x)),
rw ← this,
simp only [Union_coe_set, subtype.coe_mk],
exact is_open_Union_countable u u_open },
haveI : countable T, from countable_coe_iff.mpr T_count,
rw [← @restrict_univ _ _ μ, ← hT, ae_strongly_measurable_Union_iff],
exact λ i, (hu i).ae_strongly_measurable,
end
lemma locally_integrable_const [is_locally_finite_measure μ] (c : E) :
locally_integrable (λ x, c) μ :=
begin
assume x,
rcases μ.finite_at_nhds x with ⟨U, hU, h'U⟩,
refine ⟨U, hU, _⟩,
simp only [h'U, integrable_on_const, or_true],
end
lemma locally_integrable.indicator (hf : locally_integrable f μ)
{s : set X} (hs : measurable_set s) : locally_integrable (s.indicator f) μ :=
begin
assume x,
rcases hf x with ⟨U, hU, h'U⟩,
exact ⟨U, hU, h'U.indicator hs⟩,
end
theorem locally_integrable_map_homeomorph [borel_space X] [borel_space Y]
(e : X ≃ₜ Y) {f : Y → E} {μ : measure X} :
locally_integrable f (measure.map e μ) ↔ locally_integrable (f ∘ e) μ :=
begin
refine ⟨λ h x, _, λ h x, _⟩,
{ rcases h (e x) with ⟨U, hU, h'U⟩,
refine ⟨e ⁻¹' U, e.continuous.continuous_at.preimage_mem_nhds hU, _⟩,
exact (integrable_on_map_equiv e.to_measurable_equiv).1 h'U },
{ rcases h (e.symm x) with ⟨U, hU, h'U⟩,
refine ⟨e.symm ⁻¹' U, e.symm.continuous.continuous_at.preimage_mem_nhds hU, _⟩,
apply (integrable_on_map_equiv e.to_measurable_equiv).2,
simp only [homeomorph.to_measurable_equiv_coe],
convert h'U,
ext x,
simp only [mem_preimage, homeomorph.symm_apply_apply] }
end
section mul
variables [opens_measurable_space X] [normed_ring R] [second_countable_topology_either X R]
{A K : set X} {g g' : X → R}
lemma integrable_on.mul_continuous_on_of_subset
(hg : integrable_on g A μ) (hg' : continuous_on g' K)
(hA : measurable_set A) (hK : is_compact K) (hAK : A ⊆ K) :
integrable_on (λ x, g x * g' x) A μ :=
begin
rcases is_compact.exists_bound_of_continuous_on hK hg' with ⟨C, hC⟩,
rw [integrable_on, ← mem_ℒp_one_iff_integrable] at hg ⊢,
have : ∀ᵐ x ∂(μ.restrict A), ‖g x * g' x‖ ≤ C * ‖g x‖,
{ filter_upwards [ae_restrict_mem hA] with x hx,
refine (norm_mul_le _ _).trans _,
rw mul_comm,
apply mul_le_mul_of_nonneg_right (hC x (hAK hx)) (norm_nonneg _), },
exact mem_ℒp.of_le_mul hg (hg.ae_strongly_measurable.mul $
(hg'.mono hAK).ae_strongly_measurable hA) this,
end
lemma integrable_on.mul_continuous_on [t2_space X]
(hg : integrable_on g K μ) (hg' : continuous_on g' K) (hK : is_compact K) :
integrable_on (λ x, g x * g' x) K μ :=
hg.mul_continuous_on_of_subset hg' hK.measurable_set hK (subset.refl _)
lemma integrable_on.continuous_on_mul_of_subset
(hg : continuous_on g K) (hg' : integrable_on g' A μ)
(hK : is_compact K) (hA : measurable_set A) (hAK : A ⊆ K) :
integrable_on (λ x, g x * g' x) A μ :=
begin
rcases is_compact.exists_bound_of_continuous_on hK hg with ⟨C, hC⟩,
rw [integrable_on, ← mem_ℒp_one_iff_integrable] at hg' ⊢,
have : ∀ᵐ x ∂(μ.restrict A), ‖g x * g' x‖ ≤ C * ‖g' x‖,
{ filter_upwards [ae_restrict_mem hA] with x hx,
refine (norm_mul_le _ _).trans _,
apply mul_le_mul_of_nonneg_right (hC x (hAK hx)) (norm_nonneg _), },
exact mem_ℒp.of_le_mul hg' (((hg.mono hAK).ae_strongly_measurable hA).mul
hg'.ae_strongly_measurable) this,
end
lemma integrable_on.continuous_on_mul [t2_space X]
(hg : continuous_on g K) (hg' : integrable_on g' K μ) (hK : is_compact K) :
integrable_on (λ x, g x * g' x) K μ :=
hg'.continuous_on_mul_of_subset hg hK hK.measurable_set subset.rfl
end mul
end measure_theory
open measure_theory
/-- If a function is integrable at `𝓝[s] x` for each point `x` of a compact set `s`, then it is
integrable on `s`. -/
lemma is_compact.integrable_on_of_nhds_within {K : set X} (hK : is_compact K)
(hf : ∀ x ∈ K, integrable_at_filter f (𝓝[K] x) μ) : integrable_on f K μ :=
is_compact.induction_on hK integrable_on_empty (λ s t hst ht, ht.mono_set hst)
(λ s t hs ht, hs.union ht) hf
section borel
variables [opens_measurable_space X] [is_locally_finite_measure μ]
variables {K : set X} {a b : X}
/-- A continuous function `f` is locally integrable with respect to any locally finite measure. -/
lemma continuous.locally_integrable [second_countable_topology_either X E]
(hf : continuous f) : locally_integrable f μ :=
hf.integrable_at_nhds
variables [metrizable_space X]
/-- A function `f` continuous on a compact set `K` is integrable on this set with respect to any
locally finite measure. -/
lemma continuous_on.integrable_on_compact (hK : is_compact K) (hf : continuous_on f K) :
integrable_on f K μ :=
begin
letI := metrizable_space_metric X,
apply hK.integrable_on_of_nhds_within (λ x hx, _),
exact hf.integrable_at_nhds_within_of_is_separable hK.measurable_set hK.is_separable hx,
end
lemma continuous_on.integrable_on_Icc [preorder X] [compact_Icc_space X]
(hf : continuous_on f (Icc a b)) : integrable_on f (Icc a b) μ :=
hf.integrable_on_compact is_compact_Icc
lemma continuous.integrable_on_Icc [preorder X] [compact_Icc_space X] (hf : continuous f) :
integrable_on f (Icc a b) μ :=
hf.continuous_on.integrable_on_Icc
lemma continuous.integrable_on_Ioc [preorder X] [compact_Icc_space X] (hf : continuous f) :
integrable_on f (Ioc a b) μ :=
hf.integrable_on_Icc.mono_set Ioc_subset_Icc_self
lemma continuous_on.integrable_on_uIcc [linear_order X] [compact_Icc_space X]
(hf : continuous_on f [a, b]) : integrable_on f [a, b] μ :=
hf.integrable_on_Icc
lemma continuous.integrable_on_uIcc [linear_order X] [compact_Icc_space X] (hf : continuous f) :
integrable_on f [a, b] μ :=
hf.integrable_on_Icc
lemma continuous.integrable_on_uIoc [linear_order X] [compact_Icc_space X]
(hf : continuous f) : integrable_on f (Ι a b) μ :=
hf.integrable_on_Ioc
/-- A continuous function with compact support is integrable on the whole space. -/
lemma continuous.integrable_of_has_compact_support
(hf : continuous f) (hcf : has_compact_support f) : integrable f μ :=
(integrable_on_iff_integrable_of_support_subset (subset_tsupport f) measurable_set_closure).mp $
hf.continuous_on.integrable_on_compact hcf
end borel
open_locale ennreal
section monotone
variables [borel_space X]
[conditionally_complete_linear_order X] [conditionally_complete_linear_order E]
[order_topology X] [order_topology E] [second_countable_topology E]
{s : set X}
lemma monotone_on.integrable_on_of_measure_ne_top
(hmono : monotone_on f s) {a b : X} (ha : is_least s a) (hb : is_greatest s b) (hs : μ s ≠ ∞)
(h's : measurable_set s) :
integrable_on f s μ :=
begin
borelize E,
obtain rfl | h := s.eq_empty_or_nonempty,
{ exact integrable_on_empty },
have hbelow : bdd_below (f '' s) := ⟨f a, λ x ⟨y, hy, hyx⟩, hyx ▸ hmono ha.1 hy (ha.2 hy)⟩,
have habove : bdd_above (f '' s) := ⟨f b, λ x ⟨y, hy, hyx⟩, hyx ▸ hmono hy hb.1 (hb.2 hy)⟩,
have : metric.bounded (f '' s) := metric.bounded_of_bdd_above_of_bdd_below habove hbelow,
rcases bounded_iff_forall_norm_le.mp this with ⟨C, hC⟩,
have A : integrable_on (λ x, C) s μ, by simp only [hs.lt_top, integrable_on_const, or_true],
refine integrable.mono' A
(ae_measurable_restrict_of_monotone_on h's hmono).ae_strongly_measurable
((ae_restrict_iff' h's).mpr $ ae_of_all _ $
λ y hy, hC (f y) (mem_image_of_mem f hy)),
end
lemma monotone_on.integrable_on_is_compact [is_finite_measure_on_compacts μ]
(hs : is_compact s) (hmono : monotone_on f s) :
integrable_on f s μ :=
begin
obtain rfl | h := s.eq_empty_or_nonempty,
{ exact integrable_on_empty },
{ exact hmono.integrable_on_of_measure_ne_top (hs.is_least_Inf h) (hs.is_greatest_Sup h)
hs.measure_lt_top.ne hs.measurable_set }
end
lemma antitone_on.integrable_on_of_measure_ne_top
(hanti : antitone_on f s) {a b : X} (ha : is_least s a) (hb : is_greatest s b) (hs : μ s ≠ ∞)
(h's : measurable_set s) :
integrable_on f s μ :=
hanti.dual_right.integrable_on_of_measure_ne_top ha hb hs h's
lemma antione_on.integrable_on_is_compact [is_finite_measure_on_compacts μ]
(hs : is_compact s) (hanti : antitone_on f s) :
integrable_on f s μ :=
hanti.dual_right.integrable_on_is_compact hs
lemma monotone.locally_integrable [is_locally_finite_measure μ] (hmono : monotone f) :
locally_integrable f μ :=
begin
assume x,
rcases μ.finite_at_nhds x with ⟨U, hU, h'U⟩,
obtain ⟨a, b, xab, hab, abU⟩ : ∃ (a b : X), x ∈ Icc a b ∧ Icc a b ∈ 𝓝 x ∧ Icc a b ⊆ U,
from exists_Icc_mem_subset_of_mem_nhds hU,
have ab : a ≤ b := xab.1.trans xab.2,
refine ⟨Icc a b, hab, _⟩,
exact (hmono.monotone_on _).integrable_on_of_measure_ne_top (is_least_Icc ab)
(is_greatest_Icc ab) ((measure_mono abU).trans_lt h'U).ne measurable_set_Icc,
end
lemma antitone.locally_integrable [is_locally_finite_measure μ] (hanti : antitone f) :
locally_integrable f μ :=
hanti.dual_right.locally_integrable
end monotone
|
c3931566fac2515595be2709b77c1d2080efe7c3 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/pkg/user_attr/lakefile.lean | 89e82affdff50fe42a093cb2adf96cba0e70d6a1 | [
"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 | 88 | lean | import Lake
open System Lake DSL
package user_attr
@[default_target] lean_lib UserAttr
|
17971a5b13eba7b2d94533c33ab55030a903d9fa | 96e0c08045ccd89f1e6556c1437c2ba0f560218b | /src/annihilator.lean | 95d657c0fc7527fa58a91b0e98417f48f212f237 | [] | no_license | JasonKYi/bilinear_sesquilinear_forms | 8fc78ea6716cc4b884c9ff7e27ddaa77bbd1f3f0 | d54ff4537426e180473c8091375df2e3e528693d | refs/heads/master | 1,678,081,159,270 | 1,613,904,502,000 | 1,613,904,502,000 | 329,987,442 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,734 | lean | import linear_algebra.projection
import linear_algebra.dual
universes u v w
variables {R : Type u} {M : Type v} [comm_ring R] [add_comm_group M] [module R M]
/-- The first isomorphism theorem for surjective linear maps. -/
noncomputable def linear_map.quot_ker_equiv_of_surjective
{R : Type u} {M : Type v} {M₂ : Type w} [comm_ring R]
[add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂]
(f : M →ₗ[R] M₂) (hf : function.surjective f) : f.ker.quotient ≃ₗ[R] M₂ :=
f.quot_ker_equiv_range.trans
(linear_equiv.of_top f.range (linear_map.range_eq_top.2 hf))
namespace submodule
variable {W : submodule R M}
/-- The `dual_annihilator` of a submodule `W` is the set of linear maps `φ` such
that `φ w = 0` for all `w ∈ W`. -/
def dual_annihilator {R : Type u} {M : Type v} [comm_ring R] [add_comm_group M]
[module R M] (W : submodule R M) : submodule R $ module.dual R M :=
{ carrier := { φ | ∀ w ∈ W, φ w = 0 },
zero_mem' := by simp,
add_mem' := by { intros φ ψ hφ hψ w hw,
rw [linear_map.add_apply, hφ w hw, hψ w hw, add_zero] },
smul_mem' := by { intros c φ hφ w hw,
rw [linear_map.smul_apply, hφ w hw, smul_zero] } }
@[simp] lemma mem_dual_annihilator (φ : module.dual R M) :
φ ∈ W.dual_annihilator ↔ ∀ w ∈ W, φ w = 0 := iff.rfl
/-- The `dual_restrict` of a submodule `W` of `M` is the linear map from the
dual of `M` to the dual of `W` such that the domain of each linear map is
restricted to `W`. -/
def dual_restrict (W : submodule R M) :
module.dual R M →ₗ[R] module.dual R W :=
{ to_fun := λ φ, φ.dom_restrict W,
map_add' := by simp [linear_map.ext_iff],
map_smul' := by simp [linear_map.ext_iff] }.
@[simp] lemma dual_restrict_apply
(W : submodule R M) (φ : module.dual R M) (x : W) :
W.dual_restrict φ x = φ x.1 := rfl
lemma dual_restrict_ker_eq_dual_anihilator
(W : submodule R M) : W.dual_restrict.ker = W.dual_annihilator :=
begin
ext φ, split; intro hφ,
{ intros w hw,
rw linear_map.mem_ker at hφ,
rw [← W.dual_restrict_apply φ ⟨w, hw⟩, hφ], refl },
{ ext, exact hφ x.1 x.2 }
end
end submodule
namespace subspace
open submodule linear_map
variables {K : Type u} {V : Type v} [field K] [add_comm_group V] [vector_space K V]
-- We work in vector spaces because `exists_is_compl` only hold for vector spaces
/-- Given a subspace `W` of `V` and an element of its dual `φ`, `dual_lift W φ` is
the natural extenstion of `φ` to an element of the dual of `V`. -/
noncomputable def dual_lift
(W : subspace K V) (φ : module.dual K W) : module.dual K V :=
let h := classical.indefinite_description _ W.exists_is_compl in of_is_compl h.2 φ 0
variable {W : subspace K V}
@[simp] lemma dual_lift_of_subtype {φ : module.dual K W} (w : W) :
W.dual_lift φ (w : V) = φ w :=
by erw of_is_compl_left_apply _ w
lemma dual_lift_of_mem {φ : module.dual K W} {w : V} (hw : w ∈ W) :
W.dual_lift φ w = φ ⟨w, hw⟩ :=
dual_lift_of_subtype ⟨w, hw⟩
@[simp] lemma dual_lift_zero : W.dual_lift 0 = 0 := by simp [dual_lift]
@[simp] lemma dual_lift_add (φ ψ : module.dual K W) :
W.dual_lift (φ + ψ) = W.dual_lift φ + W.dual_lift ψ :=
begin
-- `change` is significantly slower than `show`
show of_is_compl _ _ 0 = of_is_compl _ φ 0 + of_is_compl _ ψ 0,
rw [← zero_add (0 : _ →ₗ[K] _), of_is_compl_add], simp
end
@[simp] lemma dual_lift_smul (c : K) (φ : module.dual K W) :
W.dual_lift (c • φ) = c • W.dual_lift φ :=
begin
show of_is_compl _ _ 0 = c • of_is_compl _ _ 0,
rw [← smul_zero c, of_is_compl_smul], simp
end
lemma dual_restrict_surjective :
function.surjective W.dual_restrict :=
begin
intros φ, refine ⟨W.dual_lift φ, _⟩, ext,
erw [dual_restrict_apply, dual_lift, of_is_compl_left_apply],
end
lemma dual_lift_injective : function.injective W.dual_lift :=
begin
rintro _ _ h,
ext, rw [← dual_lift_of_subtype, h, dual_lift_of_subtype],
end
-- V* / U∘ ≅ U*
/-- The quotient by the `dual_annihilator` of a subspace is isomorphic to the
dual of that subspace. -/
noncomputable def quot_annihilator_equiv (W : subspace K V) :
W.dual_annihilator.quotient ≃ₗ[K] module.dual K W :=
(quot_equiv_of_eq _ _ W.dual_restrict_ker_eq_dual_anihilator).symm.trans $
W.dual_restrict.quot_ker_equiv_of_surjective dual_restrict_surjective
/-- The representation of the dual of a subspace `W` of `V` as a subspace of
the dual of `V`. -/
def dual (W : subspace K V) : subspace K (module.dual K V) :=
{ carrier := { φ | ∃ ψ : module.dual K W, φ = W.dual_lift ψ },
zero_mem' := ⟨0, dual_lift_zero.symm⟩,
add_mem' :=
by { rintro _ _ ⟨ψ₁, rfl⟩ ⟨ψ₂, rfl⟩,
exact ⟨ψ₁ + ψ₂, (dual_lift_add ψ₁ ψ₂).symm⟩ },
smul_mem' :=
by { rintro c _ ⟨ψ, rfl⟩,
exact ⟨c • ψ, (dual_lift_smul c ψ).symm⟩ } }
@[simp] lemma mem_dual_iff (φ : module.dual K V) : φ ∈ W.dual ↔
∃ ψ : module.dual K W, φ = W.dual_lift ψ := iff.rfl
/-- The natural linear map from the dual of a subspace `W` to `W.dual`. -/
noncomputable def dual_to_subspace_dual (W : subspace K V) :
module.dual K W →ₗ[K] W.dual :=
{ to_fun := λ φ, ⟨W.dual_lift φ, ⟨φ, rfl⟩⟩,
map_add' := by { intros _ _, simp_rw [dual_lift_add], refl },
map_smul' := by { intros _ _, simp_rw [dual_lift_smul], refl } }
@[simp] lemma dual_to_subspace_dual_apply (φ : module.dual K W) :
W.dual_to_subspace_dual φ = ⟨W.dual_lift φ, ⟨φ, rfl⟩⟩ := rfl
lemma dual_to_subspace_ker_eq_bot :
W.dual_to_subspace_dual.ker = ⊥ :=
linear_map.ker_eq_bot.2 $ λ φ ψ h, dual_lift_injective (subtype.mk_eq_mk.1 h)
lemma dual_to_subspace_range_eq_top :
W.dual_to_subspace_dual.range = ⊤ :=
linear_map.range_eq_top.2 $ λ ⟨φ, hφ⟩, let ⟨ψ, hψ⟩ := hφ in
⟨ψ, by rw [dual_to_subspace_dual_apply, subtype.mk_eq_mk, hψ]⟩
/-- The natural isomorphism forom the dual of a subspace `W` to `W.dual`. -/
noncomputable def dual_equiv_dual (W : subspace K V) :
module.dual K W ≃ₗ[K] W.dual :=
linear_equiv.of_bijective W.dual_to_subspace_dual
dual_to_subspace_ker_eq_bot dual_to_subspace_range_eq_top
/- Next step
V* / U∘ ≅ U*
→ V* / U* ≅ U∘
→ V / U ≅ U∘ ≅ U⊥
-/
section
open_locale classical
open finite_dimensional
variables {V₁ : Type*} [add_comm_group V₁] [vector_space K V₁]
variables [finite_dimensional K V] [finite_dimensional K V₁]
/-- Given isomorphic subspaces `p q` of vector spaces `V` and `V₁` respectively,
`p.quotient` is isomorphic to `q.quotient`. -/
noncomputable def linear_equiv.quot_equiv_of_equiv
{p : subspace K V} {q : subspace K V₁}
(f₁ : p ≃ₗ[K] q) (f₂ : V ≃ₗ[K] V₁) : p.quotient ≃ₗ[K] q.quotient :=
linear_equiv.of_findim_eq _ _
begin
rw [← @add_right_cancel_iff _ _ (findim K p), findim_quotient_add_findim,
linear_equiv.findim_eq f₁, findim_quotient_add_findim, linear_equiv.findim_eq f₂],
end
/-- Given the subspaces `p q`, if `p.quotient ≃ₗ[K] q`, then `q.quotient ≃ₗ[K] p` -/
noncomputable def linear_equiv.quot_equiv_of_quot_equiv
{p q : subspace K V} (f : p.quotient ≃ₗ[K] q) : q.quotient ≃ₗ[K] p :=
linear_equiv.of_findim_eq _ _
begin
rw [← @add_right_cancel_iff _ _ (findim K q), findim_quotient_add_findim,
← linear_equiv.findim_eq f, add_comm, findim_quotient_add_findim]
end
-- dependency
instance [H : finite_dimensional K V] : finite_dimensional K (module.dual K V) :=
begin
refine @linear_equiv.finite_dimensional _ _ _ _ _ _ _ _ _ H,
have hB := classical.some_spec (exists_is_basis_finite K V),
haveI := classical.choice hB.2,
exact is_basis.to_dual_equiv _ hB.1
end
/-- The quotient by the dual is isomorphic to its dual annihilator. -/
noncomputable def quot_dual_equiv_annihilator (W : subspace K V) :
W.dual.quotient ≃ₗ[K] W.dual_annihilator :=
linear_equiv.quot_equiv_of_quot_equiv $
linear_equiv.trans W.quot_annihilator_equiv W.dual_equiv_dual
/-- The quotient by a subspace is isomorphic to its dual annihilator. -/
noncomputable def quot_equiv_annihilator (W : subspace K V) :
W.quotient ≃ₗ[K] W.dual_annihilator :=
begin
refine linear_equiv.trans _ W.quot_dual_equiv_annihilator,
refine linear_equiv.quot_equiv_of_equiv _ _,
{ refine linear_equiv.trans _ W.dual_equiv_dual,
have hB := classical.some_spec (exists_is_basis_finite K W),
haveI := classical.choice hB.2,
exact is_basis.to_dual_equiv _ hB.1 },
{ have hB := classical.some_spec (exists_is_basis_finite K V),
haveI := classical.choice hB.2,
exact is_basis.to_dual_equiv _ hB.1 },
end
end
end subspace |
d6087e4d219a1242a25f4395b8e7e9ea6712aa54 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/discrete_category.lean | f5278f965861abc52bfa0f5e9556f49373ab54d5 | [
"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 | 6,574 | 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, Floris van Doorn
-/
import category_theory.eq_to_hom
import data.ulift
/-!
# Discrete categories
We define `discrete α := α` for any type `α`, and use this type alias
to provide a `small_category` instance whose only morphisms are the identities.
There is an annoying technical difficulty that it has turned out to be inconvenient
to allow categories with morphisms living in `Prop`,
so instead of defining `X ⟶ Y` in `discrete α` as `X = Y`,
one might define it as `plift (X = Y)`.
In fact, to allow `discrete α` to be a `small_category`
(i.e. with morphisms in the same universe as the objects),
we actually define the hom type `X ⟶ Y` as `ulift (plift (X = Y))`.
`discrete.functor` promotes a function `f : I → C` (for any category `C`) to a functor
`discrete.functor f : discrete I ⥤ C`.
Similarly, `discrete.nat_trans` and `discrete.nat_iso` promote `I`-indexed families of morphisms,
or `I`-indexed families of isomorphisms to natural transformations or natural isomorphism.
We show equivalences of types are the same as (categorical) equivalences of the corresponding
discrete categories.
-/
namespace category_theory
-- morphism levels before object levels. See note [category_theory universes].
universes v₁ v₂ v₃ u₁ u₂ u₃
/--
A type synonym for promoting any type to a category,
with the only morphisms being equalities.
-/
def discrete (α : Type u₁) := α
/--
The "discrete" category on a type, whose morphisms are equalities.
Because we do not allow morphisms in `Prop` (only in `Type`),
somewhat annoyingly we have to define `X ⟶ Y` as `ulift (plift (X = Y))`.
See <https://stacks.math.columbia.edu/tag/001A>
-/
instance discrete_category (α : Type u₁) : small_category (discrete α) :=
{ hom := λ X Y, ulift (plift (X = Y)),
id := λ X, ulift.up (plift.up rfl),
comp := λ X Y Z g f, by { rcases f with ⟨⟨rfl⟩⟩, exact g } }
namespace discrete
variables {α : Type u₁}
instance [inhabited α] : inhabited (discrete α) :=
by { dsimp [discrete], apply_instance }
instance [subsingleton α] : subsingleton (discrete α) :=
by { dsimp [discrete], apply_instance }
/-- Extract the equation from a morphism in a discrete category. -/
lemma eq_of_hom {X Y : discrete α} (i : X ⟶ Y) : X = Y := i.down.down
@[simp] lemma id_def (X : discrete α) : ulift.up (plift.up (eq.refl X)) = 𝟙 X := rfl
variables {C : Type u₂} [category.{v₂} C]
instance {I : Type u₁} {i j : discrete I} (f : i ⟶ j) : is_iso f :=
⟨⟨eq_to_hom (eq_of_hom f).symm, by tidy⟩⟩
/--
Any function `I → C` gives a functor `discrete I ⥤ C`.
-/
def functor {I : Type u₁} (F : I → C) : discrete I ⥤ C :=
{ obj := F,
map := λ X Y f, begin cases f, cases f, cases f, exact 𝟙 (F X) end }
@[simp] lemma functor_obj {I : Type u₁} (F : I → C) (i : I) :
(discrete.functor F).obj i = F i := rfl
lemma functor_map {I : Type u₁} (F : I → C) {i : discrete I} (f : i ⟶ i) :
(discrete.functor F).map f = 𝟙 (F i) :=
by { cases f, cases f, cases f, refl }
/--
For functors out of a discrete category,
a natural transformation is just a collection of maps,
as the naturality squares are trivial.
-/
def nat_trans {I : Type u₁} {F G : discrete I ⥤ C}
(f : Π i : discrete I, F.obj i ⟶ G.obj i) : F ⟶ G :=
{ app := f }
@[simp] lemma nat_trans_app {I : Type u₁} {F G : discrete I ⥤ C}
(f : Π i : discrete I, F.obj i ⟶ G.obj i) (i) : (discrete.nat_trans f).app i = f i :=
rfl
/--
For functors out of a discrete category,
a natural isomorphism is just a collection of isomorphisms,
as the naturality squares are trivial.
-/
def nat_iso {I : Type u₁} {F G : discrete I ⥤ C}
(f : Π i : discrete I, F.obj i ≅ G.obj i) : F ≅ G :=
nat_iso.of_components f (by tidy)
@[simp]
lemma nat_iso_hom_app {I : Type u₁} {F G : discrete I ⥤ C}
(f : Π i : discrete I, F.obj i ≅ G.obj i) (i : I) :
(discrete.nat_iso f).hom.app i = (f i).hom :=
rfl
@[simp]
lemma nat_iso_inv_app {I : Type u₁} {F G : discrete I ⥤ C}
(f : Π i : discrete I, F.obj i ≅ G.obj i) (i : I) :
(discrete.nat_iso f).inv.app i = (f i).inv :=
rfl
@[simp]
lemma nat_iso_app {I : Type u₁} {F G : discrete I ⥤ C}
(f : Π i : discrete I, F.obj i ≅ G.obj i) (i : I) :
(discrete.nat_iso f).app i = f i :=
by tidy
/-- Every functor `F` from a discrete category is naturally isomorphic (actually, equal) to
`discrete.functor (F.obj)`. -/
@[simp]
def nat_iso_functor {I : Type u₁} {F : discrete I ⥤ C} : F ≅ discrete.functor (F.obj) :=
nat_iso $ λ i, iso.refl _
/-- Composing `discrete.functor F` with another functor `G` amounts to composing `F` with `G.obj` -/
@[simp]
def comp_nat_iso_discrete {I : Type u₁} {D : Type u₃} [category.{v₃} D]
(F : I → C) (G : C ⥤ D) : discrete.functor F ⋙ G ≅ discrete.functor (G.obj ∘ F) :=
nat_iso $ λ i, iso.refl _
/--
We can promote a type-level `equiv` to
an equivalence between the corresponding `discrete` categories.
-/
@[simps]
def equivalence {I : Type u₁} {J : Type u₂} (e : I ≃ J) : discrete I ≌ discrete J :=
{ functor := discrete.functor (e : I → J),
inverse := discrete.functor (e.symm : J → I),
unit_iso := discrete.nat_iso (λ i, eq_to_iso (by simp)),
counit_iso := discrete.nat_iso (λ j, eq_to_iso (by simp)), }
/-- We can convert an equivalence of `discrete` categories to a type-level `equiv`. -/
@[simps]
def equiv_of_equivalence {α : Type u₁} {β : Type u₂} (h : discrete α ≌ discrete β) : α ≃ β :=
{ to_fun := h.functor.obj,
inv_fun := h.inverse.obj,
left_inv := λ a, eq_of_hom (h.unit_iso.app a).2,
right_inv := λ a, eq_of_hom (h.counit_iso.app a).1 }
end discrete
namespace discrete
variables {J : Type v₁}
open opposite
/-- A discrete category is equivalent to its opposite category. -/
protected def opposite (α : Type u₁) : (discrete α)ᵒᵖ ≌ discrete α :=
let F : discrete α ⥤ (discrete α)ᵒᵖ := discrete.functor (λ x, op x) in
begin
refine equivalence.mk (functor.left_op F) F _ (discrete.nat_iso $ λ X, by simp [F]),
refine nat_iso.of_components (λ X, by simp [F]) _,
tidy
end
variables {C : Type u₂} [category.{v₂} C]
@[simp] lemma functor_map_id
(F : discrete J ⥤ C) {j : discrete J} (f : j ⟶ j) : F.map f = 𝟙 (F.obj j) :=
begin
have h : f = 𝟙 j, { cases f, cases f, ext, },
rw h,
simp,
end
end discrete
end category_theory
|
8de1f7c387b329e010d8d42c7fb5b128302cf495 | e898bfefd5cb60a60220830c5eba68cab8d02c79 | /uexp/src/uexp/rules/transitiveInferenceNoPullUpExprs.lean | c74f0fba557450b516a7f5ac8b96d45b6ab2cc07 | [
"BSD-2-Clause"
] | permissive | kkpapa/Cosette | 9ed09e2dc4c1ecdef815c30b5501f64a7383a2ce | fda8fdbbf0de6c1be9b4104b87bbb06cede46329 | refs/heads/master | 1,584,573,128,049 | 1,526,370,422,000 | 1,526,370,422,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,549 | lean | import ..sql
import ..tactics
import ..u_semiring
import ..extra_constants
import ..ucongr
import ..TDP
open Expr
open Proj
open Pred
open SQL
open tree
open binary_operators
set_option profiler true
notation `int` := datatypes.int
variable integer_1: const datatypes.int
variable integer_7: const datatypes.int
variable integer_9: const datatypes.int
variable integer_10: const datatypes.int
theorem rule:
forall ( Γ scm_t scm_account scm_bonus scm_dept scm_emp: Schema) (rel_t: relation scm_t) (rel_account: relation scm_account) (rel_bonus: relation scm_bonus) (rel_dept: relation scm_dept) (rel_emp: relation scm_emp) (t_k0 : Column int scm_t) (t_c1 : Column int scm_t) (t_f1_a0 : Column int scm_t) (t_f2_a0 : Column int scm_t) (t_f0_c0 : Column int scm_t) (t_f1_c0 : Column int scm_t) (t_f0_c1 : Column int scm_t) (t_f1_c2 : Column int scm_t) (t_f2_c3 : Column int scm_t) (account_acctno : Column int scm_account) (account_type : Column int scm_account) (account_balance : Column int scm_account) (bonus_ename : Column int scm_bonus) (bonus_job : Column int scm_bonus) (bonus_sal : Column int scm_bonus) (bonus_comm : Column int scm_bonus) (dept_deptno : Column int scm_dept) (dept_name : Column int scm_dept) (emp_empno : Column int scm_emp) (emp_ename : Column int scm_emp) (emp_job : Column int scm_emp) (emp_mgr : Column int scm_emp) (emp_hiredate : Column int scm_emp) (emp_comm : Column int scm_emp) (emp_sal : Column int scm_emp) (emp_deptno : Column int scm_emp) (emp_slacker : Column int scm_emp),
denoteSQL ((SELECT1 (e2p (constantExpr integer_1)) FROM1 (product ((SELECT * FROM1 (table rel_emp) WHERE (or (or (equal (uvariable (right⋅emp_deptno)) (constantExpr integer_7)) (equal (uvariable (right⋅emp_deptno)) (constantExpr integer_9))) (castPred (combine (right⋅emp_comm) (e2p (constantExpr integer_10)) ) predicates.gt)))) (table rel_emp)) WHERE (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅emp_deptno)))) :SQL Γ _)
=
denoteSQL ((SELECT1 (e2p (constantExpr integer_1)) FROM1 (product ((SELECT * FROM1 (table rel_emp) WHERE (or (or (equal (uvariable (right⋅emp_deptno)) (constantExpr integer_7)) (equal (uvariable (right⋅emp_deptno)) (constantExpr integer_9))) (castPred (combine (right⋅emp_comm) (e2p (constantExpr integer_10)) ) predicates.gt)))) (table rel_emp)) WHERE (equal (uvariable (right⋅left⋅emp_deptno)) (uvariable (right⋅right⋅emp_deptno)))) :SQL Γ _) :=
begin
intros,
print_size,
unfold_all_denotations,
funext,
try {simp},
try {TDP' ucongr},
end |
0dc7387d496d244ce1ef5f6b3a1176b3ac69e6c4 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/number_theory/von_mangoldt.lean | cbff35d3fb5e20b3591949a88b05307a8f8430b8 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 5,779 | lean | /-
Copyright (c) 2022 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import algebra.is_prime_pow
import number_theory.arithmetic_function
import analysis.special_functions.log.basic
/-!
# The von Mangoldt Function
In this file we define the von Mangoldt function: the function on natural numbers that returns
`log p` if the input can be expressed as `p^k` for a prime `p`.
## Main Results
The main definition for this file is
- `nat.arithmetic_function.von_mangoldt`: The von Mangoldt function `Λ`.
We then prove the classical summation property of the von Mangoldt function in
`nat.arithmetic_function.von_mangoldt_sum`, that `∑ i in n.divisors, Λ i = real.log n`, and use this
to deduce alternative expressions for the von Mangoldt function via Möbius inversion, see
`nat.arithmetic_function.sum_moebius_mul_log_eq`.
## Notation
We use the standard notation `Λ` to represent the von Mangoldt function.
-/
namespace nat
namespace arithmetic_function
open finset
open_locale arithmetic_function
/-- `log` as an arithmetic function `ℕ → ℝ`. Note this is in the `nat.arithmetic_function`
namespace to indicate that it is bundled as an `arithmetic_function` rather than being the usual
real logarithm. -/
noncomputable def log : arithmetic_function ℝ :=
⟨λ n, real.log n, by simp⟩
@[simp] lemma log_apply {n : ℕ} : log n = real.log n := rfl
/--
The `von_mangoldt` function is the function on natural numbers that returns `log p` if the input can
be expressed as `p^k` for a prime `p`.
In the case when `n` is a prime power, `min_fac` will give the appropriate prime, as it is the
smallest prime factor.
In the `arithmetic_function` locale, we have the notation `Λ` for this function.
-/
noncomputable def von_mangoldt : arithmetic_function ℝ :=
⟨λ n, if is_prime_pow n then real.log (min_fac n) else 0, if_neg not_is_prime_pow_zero⟩
localized "notation (name := von_mangoldt)
`Λ` := nat.arithmetic_function.von_mangoldt" in arithmetic_function
lemma von_mangoldt_apply {n : ℕ} :
Λ n = if is_prime_pow n then real.log (min_fac n) else 0 := rfl
@[simp] lemma von_mangoldt_apply_one : Λ 1 = 0 := by simp [von_mangoldt_apply]
@[simp] lemma von_mangoldt_nonneg {n : ℕ} : 0 ≤ Λ n :=
begin
rw [von_mangoldt_apply],
split_ifs,
{ exact real.log_nonneg (one_le_cast.2 (nat.min_fac_pos n)) },
refl
end
lemma von_mangoldt_apply_pow {n k : ℕ} (hk : k ≠ 0) : Λ (n ^ k) = Λ n :=
by simp only [von_mangoldt_apply, is_prime_pow_pow_iff hk, pow_min_fac hk]
lemma von_mangoldt_apply_prime {p : ℕ} (hp : p.prime) : Λ p = real.log p :=
by rw [von_mangoldt_apply, prime.min_fac_eq hp, if_pos hp.prime.is_prime_pow]
lemma von_mangoldt_ne_zero_iff {n : ℕ} : Λ n ≠ 0 ↔ is_prime_pow n :=
begin
rcases eq_or_ne n 1 with rfl | hn, { simp [not_is_prime_pow_one] },
exact (real.log_pos (one_lt_cast.2 (min_fac_prime hn).one_lt)).ne'.ite_ne_right_iff
end
lemma von_mangoldt_pos_iff {n : ℕ} : 0 < Λ n ↔ is_prime_pow n :=
von_mangoldt_nonneg.lt_iff_ne.trans (ne_comm.trans von_mangoldt_ne_zero_iff)
lemma von_mangoldt_eq_zero_iff {n : ℕ} : Λ n = 0 ↔ ¬is_prime_pow n :=
von_mangoldt_ne_zero_iff.not_right
open_locale big_operators
lemma von_mangoldt_sum {n : ℕ} :
∑ i in n.divisors, Λ i = real.log n :=
begin
refine rec_on_prime_coprime _ _ _ n,
{ simp },
{ intros p k hp,
rw [sum_divisors_prime_pow hp, cast_pow, real.log_pow, finset.sum_range_succ', pow_zero,
von_mangoldt_apply_one],
simp [von_mangoldt_apply_pow (nat.succ_ne_zero _), von_mangoldt_apply_prime hp] },
intros a b ha' hb' hab ha hb,
simp only [von_mangoldt_apply, ←sum_filter] at ha hb ⊢,
rw [mul_divisors_filter_prime_pow hab, filter_union,
sum_union (disjoint_divisors_filter_prime_pow hab), ha, hb, nat.cast_mul,
real.log_mul (cast_ne_zero.2 (pos_of_gt ha').ne') (cast_ne_zero.2 (pos_of_gt hb').ne')],
end
@[simp] lemma von_mangoldt_mul_zeta : Λ * ζ = log :=
by { ext n, rw [coe_mul_zeta_apply, von_mangoldt_sum], refl }
@[simp] lemma zeta_mul_von_mangoldt : (ζ : arithmetic_function ℝ) * Λ = log :=
by { rw [mul_comm], simp }
@[simp] lemma log_mul_moebius_eq_von_mangoldt : log * μ = Λ :=
by rw [←von_mangoldt_mul_zeta, mul_assoc, coe_zeta_mul_coe_moebius, mul_one]
@[simp] lemma moebius_mul_log_eq_von_mangoldt : (μ : arithmetic_function ℝ) * log = Λ :=
by { rw [mul_comm], simp }
lemma sum_moebius_mul_log_eq {n : ℕ} :
∑ d in n.divisors, (μ d : ℝ) * log d = - Λ n :=
begin
simp only [←log_mul_moebius_eq_von_mangoldt, mul_comm log, mul_apply, log_apply, int_coe_apply,
←finset.sum_neg_distrib, neg_mul_eq_mul_neg],
rw sum_divisors_antidiagonal (λ i j, (μ i : ℝ) * -real.log j),
have : ∑ (i : ℕ) in n.divisors, (μ i : ℝ) * -real.log (n / i : ℕ) =
∑ (i : ℕ) in n.divisors, ((μ i : ℝ) * real.log i - μ i * real.log n),
{ apply sum_congr rfl,
simp only [and_imp, int.cast_eq_zero, mul_eq_mul_left_iff, ne.def, neg_inj, mem_divisors],
intros m mn hn,
have : (m : ℝ) ≠ 0,
{ rw [cast_ne_zero],
rintro rfl,
exact hn (by simpa using mn) },
rw [nat.cast_div mn this, real.log_div (cast_ne_zero.2 hn) this, neg_sub, mul_sub] },
rw [this, sum_sub_distrib, ←sum_mul, ←int.cast_sum, ←coe_mul_zeta_apply, eq_comm, sub_eq_self,
moebius_mul_coe_zeta, mul_eq_zero, int.cast_eq_zero],
rcases eq_or_ne n 1 with hn | hn;
simp [hn],
end
lemma von_mangoldt_le_log : ∀ {n : ℕ}, Λ n ≤ real.log (n : ℝ)
| 0 := by simp
| (n+1) :=
begin
rw ←von_mangoldt_sum,
exact single_le_sum (λ _ _, von_mangoldt_nonneg) (mem_divisors_self _ n.succ_ne_zero),
end
end arithmetic_function
end nat
|
eef2cc1ef770167e4d491212772d8e6648468910 | 88fb7558b0636ec6b181f2a548ac11ad3919f8a5 | /library/init/meta/smt/rsimp.lean | 073e64cf9c6d11622bf3ca538bd1595a995545f5 | [
"Apache-2.0"
] | permissive | moritayasuaki/lean | 9f666c323cb6fa1f31ac597d777914aed41e3b7a | ae96ebf6ee953088c235ff7ae0e8c95066ba8001 | refs/heads/master | 1,611,135,440,814 | 1,493,852,869,000 | 1,493,852,869,000 | 90,269,903 | 0 | 0 | null | 1,493,906,291,000 | 1,493,906,291,000 | null | UTF-8 | Lean | false | false | 6,578 | 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
-/
prelude
import init.meta.smt.smt_tactic init.meta.fun_info init.meta.rb_map
open tactic
private meta def add_lemma (m : transparency) (h : name) (hs : hinst_lemmas) : tactic hinst_lemmas :=
(do h ← hinst_lemma.mk_from_decl_core m h tt, return $ hs.add h) <|> return hs
private meta def to_hinst_lemmas (m : transparency) (ex : name_set) : list name → hinst_lemmas → tactic hinst_lemmas
| [] hs := return hs
| (n::ns) hs :=
if ex.contains n then to_hinst_lemmas ns hs else
let add n := add_lemma m n hs >>= to_hinst_lemmas ns
in do eqns ← tactic.get_eqn_lemmas_for tt n,
match eqns with
| [] := add n
| _ := mcond (is_prop_decl n) (add n) (to_hinst_lemmas eqns hs >>= to_hinst_lemmas ns)
end
/-- Create a rsimp attribute named `attr_name`, the attribute declaration is named `attr_decl_name`.
The cached hinst_lemmas structure is built using the lemmas marked with simp attribute `simp_attr_name`,
but *not* marked with `ex_attr_name`.
We say `ex_attr_name` is the "exception set". It is useful for excluding lemmas in `simp_attr_name`
which are not good or redundant for ematching. -/
meta def mk_hinst_lemma_attr_from_simp_attr (attr_decl_name attr_name : name) (simp_attr_name : name) (ex_attr_name : name) : command :=
do let t := ```(caching_user_attribute hinst_lemmas),
v ← to_expr ``({name := %%(quote attr_name),
descr := "hinst_lemma attribute derived from '" ++ to_string %%(quote simp_attr_name) ++ "'",
mk_cache := λ ns,
let aux := %%(quote simp_attr_name) in
let ex_attr := %%(quote ex_attr_name) in
do {
hs ← to_hinst_lemmas reducible mk_name_set ns hinst_lemmas.mk,
ss ← attribute.get_instances aux,
ex ← get_name_set_for_attr ex_attr,
to_hinst_lemmas reducible ex ss hs
},
dependencies := [`reducibility, %%(quote simp_attr_name)]} : caching_user_attribute hinst_lemmas),
add_decl (declaration.defn attr_decl_name [] t v reducibility_hints.abbrev ff),
attribute.register attr_decl_name
run_cmd mk_name_set_attr `no_rsimp
run_cmd mk_hinst_lemma_attr_from_simp_attr `rsimp_attr `rsimp `simp `no_rsimp
/- The following lemmas are not needed by rsimp, and they actually hurt performance since they generate a lot of
instances. -/
attribute [no_rsimp]
id.def ne.def not_true not_false_iff ne_self_iff_false eq_self_iff_true heq_self_iff_true iff_not_self not_iff_self
true_iff_false false_iff_true and.comm and.assoc and.left_comm and_true true_and and_false false_and not_and_self and_not_self
and_self or.comm or.assoc or.left_comm or_true true_or or_false false_or or_self iff_true true_iff iff_false false_iff
iff_self implies_true_iff false_implies_iff if_t_t if_true if_false
namespace rsimp
meta def is_value_like : expr → bool
| e :=
if ¬ e.is_app then ff
else let fn := e.get_app_fn in
if ¬ fn.is_constant then ff
else let nargs := e.get_app_num_args,
fname := fn.const_name in
if fname = ``has_zero.zero ∧ nargs = 2 then tt
else if fname = ``has_one.one ∧ nargs = 2 then tt
else if fname = ``bit0 ∧ nargs = 3 then is_value_like e.app_arg
else if fname = ``bit1 ∧ nargs = 4 then is_value_like e.app_arg
else if fname = ``char.of_nat ∧ nargs = 1 then is_value_like e.app_arg
else ff
/-- Return the size of term by considering only explicit arguments. -/
meta def explicit_size : expr → tactic nat
| e :=
if ¬ e.is_app then return 1
else if is_value_like e then return 1
else fold_explicit_args e 1
(λ n arg, do r ← explicit_size arg, return $ r + n)
/-- Choose smallest element (with respect to explicit_size) in `e`s equivalence class. -/
meta def choose (ccs : cc_state) (e : expr) : tactic expr :=
do sz ← explicit_size e,
p ← ccs.mfold_eqc e (e, sz) $ λ p e',
if p.2 = 1 then return p
else do {
sz' ← explicit_size e',
if sz' < p.2 then return (e', sz')
else return p
},
return p.1
meta def repr_map := expr_map expr
meta def mk_repr_map := expr_map.mk expr
meta def to_repr_map (ccs : cc_state) : tactic repr_map :=
ccs.roots.mfoldl (λ S e, do r ← choose ccs e, return $ S.insert e r) mk_repr_map
meta def rsimplify (ccs : cc_state) (e : expr) (m : option repr_map := none) : tactic (expr × expr) :=
do m ← match m with
| none := to_repr_map ccs
| some m := return m
end,
r ← simplify_top_down () (λ _ t,
do root ← return $ ccs.root t,
new_t ← m.find root,
guard (¬ new_t =ₐ t),
prf ← ccs.eqv_proof t new_t,
return ((), new_t, prf))
e,
return r.2
structure config :=
(attr_name := `rsimp_attr)
(max_rounds := 8)
open smt_tactic
meta def collect_implied_eqs (cfg : config := {}) (extra := hinst_lemmas.mk) : tactic cc_state :=
do focus1 $ using_smt_with {em_attr := cfg.attr_name} $
do
add_lemmas_from_facts,
add_lemmas extra,
repeat_at_most cfg.max_rounds (ematch >> try smt_tactic.close),
(done >> return cc_state.mk)
<|>
to_cc_state
meta def rsimplify_goal (ccs : cc_state) (m : option repr_map := none) : tactic unit :=
do t ← target,
(new_t, pr) ← rsimplify ccs t m,
try (replace_target new_t pr)
meta def rsimplify_at (ccs : cc_state) (h : expr) (m : option repr_map := none) : tactic unit :=
do when (expr.is_local_constant h = ff) (tactic.fail "tactic rsimplify_at failed, the given expression is not a hypothesis"),
htype ← infer_type h,
(new_htype, heq) ← rsimplify ccs htype m,
try $ do assert (expr.local_pp_name h) new_htype,
mk_eq_mp heq h >>= exact,
try $ clear h
end rsimp
open rsimp
namespace tactic
meta def rsimp (cfg : config := {}) (extra := hinst_lemmas.mk) : tactic unit :=
do ccs ← collect_implied_eqs cfg extra,
try $ rsimplify_goal ccs
meta def rsimp_at (h : expr) (cfg : config := {}) (extra := hinst_lemmas.mk) : tactic unit :=
do ccs ← collect_implied_eqs cfg extra,
try $ rsimplify_at ccs h
namespace interactive
/- TODO(Leo): allow user to provide extra lemmas manually -/
meta def rsimp : tactic unit :=
tactic.rsimp
end interactive
end tactic
|
db726786ff5010e1025a98fa31dafec7b5a8bfc3 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/ginductive_pred.lean | 2db4af17d8611e03c7712560b1596fc9d5b9cd13 | [
"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 | 711 | lean | inductive term : Type
| var : nat → term
| app : string → list term → term
/- In the current system, it will be hard to work with mutually inductive predicates.
Reason: we cannot use well-founded recursion, and the induction principle for it
is too weak. -/
mutual inductive var_occ, var_occ_list
with var_occ : nat → term → Prop
| leaf (n : nat) : var_occ n (term.var n)
| app (n : nat) (s : string) (ts : list term) : var_occ_list n ts → var_occ n (term.app s ts)
with var_occ_list : nat → list term → Prop
| head (n : nat) (t : term) (ts : list term) : var_occ n t → var_occ_list n (t::ts)
| tail (n : nat) (t : term) (ts : list term) : var_occ_list n ts → var_occ_list n (t::ts)
|
b49b36c7a458b5e0732bad7eb10e6590523f93b8 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/ring_theory/hahn_series.lean | 03abade5fa66326cd4fa6ba8269977b362baa47d | [
"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 | 61,625 | lean | /-
Copyright (c) 2021 Aaron Anderson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson
-/
import order.well_founded_set
import algebra.big_operators.finprod
import ring_theory.valuation.basic
import ring_theory.power_series.basic
import data.finsupp.pwo
import data.finset.mul_antidiagonal
import algebra.order.group.with_top
/-!
# Hahn Series
If `Γ` is ordered and `R` has zero, then `hahn_series Γ R` consists of formal series over `Γ` with
coefficients in `R`, whose supports are partially well-ordered. With further structure on `R` and
`Γ`, we can add further structure on `hahn_series Γ R`, with the most studied case being when `Γ` is
a linearly ordered abelian group and `R` is a field, in which case `hahn_series Γ R` is a
valued field, with value group `Γ`.
These generalize Laurent series (with value group `ℤ`), and Laurent series are implemented that way
in the file `ring_theory/laurent_series`.
## Main Definitions
* If `Γ` is ordered and `R` has zero, then `hahn_series Γ R` consists of
formal series over `Γ` with coefficients in `R`, whose supports are partially well-ordered.
* If `R` is a (commutative) additive monoid or group, then so is `hahn_series Γ R`.
* If `R` is a (comm_)(semi)ring, then so is `hahn_series Γ R`.
* `hahn_series.add_val Γ R` defines an `add_valuation` on `hahn_series Γ R` when `Γ` is linearly
ordered.
* A `hahn_series.summable_family` is a family of Hahn series such that the union of their supports
is well-founded and only finitely many are nonzero at any given coefficient. They have a formal
sum, `hahn_series.summable_family.hsum`, which can be bundled as a `linear_map` as
`hahn_series.summable_family.lsum`. Note that this is different from `summable` in the valuation
topology, because there are topologically summable families that do not satisfy the axioms of
`hahn_series.summable_family`, and formally summable families whose sums do not converge
topologically.
* Laurent series over `R` are implemented as `hahn_series ℤ R` in the file
`ring_theory/laurent_series`.
## TODO
* Build an API for the variable `X` (defined to be `single 1 1 : hahn_series Γ R`) in analogy to
`X : R[X]` and `X : power_series R`
## References
- [J. van der Hoeven, *Operators on Generalized Power Series*][van_der_hoeven]
-/
open finset function
open_locale big_operators classical pointwise polynomial
noncomputable theory
/-- If `Γ` is linearly ordered and `R` has zero, then `hahn_series Γ R` consists of
formal series over `Γ` with coefficients in `R`, whose supports are well-founded. -/
@[ext]
structure hahn_series (Γ : Type*) (R : Type*) [partial_order Γ] [has_zero R] :=
(coeff : Γ → R)
(is_pwo_support' : (support coeff).is_pwo)
variables {Γ : Type*} {R : Type*}
namespace hahn_series
section zero
variables [partial_order Γ] [has_zero R]
lemma coeff_injective : injective (coeff : hahn_series Γ R → (Γ → R)) := ext
@[simp] lemma coeff_inj {x y : hahn_series Γ R} : x.coeff = y.coeff ↔ x = y :=
coeff_injective.eq_iff
/-- The support of a Hahn series is just the set of indices whose coefficients are nonzero.
Notably, it is well-founded. -/
def support (x : hahn_series Γ R) : set Γ := support x.coeff
@[simp]
lemma is_pwo_support (x : hahn_series Γ R) : x.support.is_pwo := x.is_pwo_support'
@[simp]
lemma is_wf_support (x : hahn_series Γ R) : x.support.is_wf := x.is_pwo_support.is_wf
@[simp]
lemma mem_support (x : hahn_series Γ R) (a : Γ) : a ∈ x.support ↔ x.coeff a ≠ 0 := iff.refl _
instance : has_zero (hahn_series Γ R) :=
⟨{ coeff := 0,
is_pwo_support' := by simp }⟩
instance : inhabited (hahn_series Γ R) := ⟨0⟩
instance [subsingleton R] : subsingleton (hahn_series Γ R) :=
⟨λ a b, a.ext b (subsingleton.elim _ _)⟩
@[simp] lemma zero_coeff {a : Γ} : (0 : hahn_series Γ R).coeff a = 0 := rfl
@[simp] lemma coeff_fun_eq_zero_iff {x : hahn_series Γ R} : x.coeff = 0 ↔ x = 0 :=
coeff_injective.eq_iff' rfl
lemma ne_zero_of_coeff_ne_zero {x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) :
x ≠ 0 :=
mt (λ x0, (x0.symm ▸ zero_coeff : x.coeff g = 0)) h
@[simp] lemma support_zero : support (0 : hahn_series Γ R) = ∅ := function.support_zero
@[simp] lemma support_nonempty_iff {x : hahn_series Γ R} : x.support.nonempty ↔ x ≠ 0 :=
by rw [support, support_nonempty_iff, ne.def, coeff_fun_eq_zero_iff]
@[simp] lemma support_eq_empty_iff {x : hahn_series Γ R} : x.support = ∅ ↔ x = 0 :=
support_eq_empty_iff.trans coeff_fun_eq_zero_iff
/-- `single a r` is the Hahn series which has coefficient `r` at `a` and zero otherwise. -/
def single (a : Γ) : zero_hom R (hahn_series Γ R) :=
{ to_fun := λ r, { coeff := pi.single a r,
is_pwo_support' := (set.is_pwo_singleton a).mono pi.support_single_subset },
map_zero' := ext _ _ (pi.single_zero _) }
variables {a b : Γ} {r : R}
@[simp]
theorem single_coeff_same (a : Γ) (r : R) : (single a r).coeff a = r := pi.single_eq_same a r
@[simp]
theorem single_coeff_of_ne (h : b ≠ a) : (single a r).coeff b = 0 := pi.single_eq_of_ne h r
theorem single_coeff : (single a r).coeff b = if (b = a) then r else 0 :=
by { split_ifs with h; simp [h] }
@[simp]
lemma support_single_of_ne (h : r ≠ 0) : support (single a r) = {a} :=
pi.support_single_of_ne h
lemma support_single_subset : support (single a r) ⊆ {a} :=
pi.support_single_subset
lemma eq_of_mem_support_single {b : Γ} (h : b ∈ support (single a r)) : b = a :=
support_single_subset h
@[simp]
lemma single_eq_zero : (single a (0 : R)) = 0 := (single a).map_zero
lemma single_injective (a : Γ) : function.injective (single a : R → hahn_series Γ R) :=
λ r s rs, by rw [← single_coeff_same a r, ← single_coeff_same a s, rs]
lemma single_ne_zero (h : r ≠ 0) : single a r ≠ 0 :=
λ con, h (single_injective a (con.trans single_eq_zero.symm))
@[simp] lemma single_eq_zero_iff {a : Γ} {r : R} :
single a r = 0 ↔ r = 0 :=
begin
split,
{ contrapose!,
exact single_ne_zero },
{ simp {contextual := tt} }
end
instance [nonempty Γ] [nontrivial R] : nontrivial (hahn_series Γ R) :=
⟨begin
obtain ⟨r, s, rs⟩ := exists_pair_ne R,
inhabit Γ,
refine ⟨single (arbitrary Γ) r, single (arbitrary Γ) s, λ con, rs _⟩,
rw [← single_coeff_same (arbitrary Γ) r, con, single_coeff_same],
end⟩
section order
variable [has_zero Γ]
/-- The order of a nonzero Hahn series `x` is a minimal element of `Γ` where `x` has a
nonzero coefficient, the order of 0 is 0. -/
def order (x : hahn_series Γ R) : Γ :=
if h : x = 0 then 0 else x.is_wf_support.min (support_nonempty_iff.2 h)
@[simp]
lemma order_zero : order (0 : hahn_series Γ R) = 0 := dif_pos rfl
lemma order_of_ne {x : hahn_series Γ R} (hx : x ≠ 0) :
order x = x.is_wf_support.min (support_nonempty_iff.2 hx) := dif_neg hx
lemma coeff_order_ne_zero {x : hahn_series Γ R} (hx : x ≠ 0) :
x.coeff x.order ≠ 0 :=
begin
rw order_of_ne hx,
exact x.is_wf_support.min_mem (support_nonempty_iff.2 hx)
end
lemma order_le_of_coeff_ne_zero {Γ} [linear_ordered_cancel_add_comm_monoid Γ]
{x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) :
x.order ≤ g :=
le_trans (le_of_eq (order_of_ne (ne_zero_of_coeff_ne_zero h)))
(set.is_wf.min_le _ _ ((mem_support _ _).2 h))
@[simp]
lemma order_single (h : r ≠ 0) : (single a r).order = a :=
(order_of_ne (single_ne_zero h)).trans (support_single_subset ((single a r).is_wf_support.min_mem
(support_nonempty_iff.2 (single_ne_zero h))))
lemma coeff_eq_zero_of_lt_order {x : hahn_series Γ R} {i : Γ} (hi : i < x.order) : x.coeff i = 0 :=
begin
rcases eq_or_ne x 0 with rfl|hx,
{ simp },
contrapose! hi,
rw [←ne.def, ←mem_support] at hi,
rw [order_of_ne hx],
exact set.is_wf.not_lt_min _ _ hi
end
end order
section domain
variables {Γ' : Type*} [partial_order Γ']
/-- Extends the domain of a `hahn_series` by an `order_embedding`. -/
def emb_domain (f : Γ ↪o Γ') : hahn_series Γ R → hahn_series Γ' R :=
λ x, { coeff := λ (b : Γ'),
if h : b ∈ f '' x.support then x.coeff (classical.some h) else 0,
is_pwo_support' := (x.is_pwo_support.image_of_monotone f.monotone).mono (λ b hb, begin
contrapose! hb,
rw [function.mem_support, dif_neg hb, not_not],
end) }
@[simp]
lemma emb_domain_coeff {f : Γ ↪o Γ'} {x : hahn_series Γ R} {a : Γ} :
(emb_domain f x).coeff (f a) = x.coeff a :=
begin
rw emb_domain,
dsimp only,
by_cases ha : a ∈ x.support,
{ rw dif_pos (set.mem_image_of_mem f ha),
exact congr rfl (f.injective (classical.some_spec (set.mem_image_of_mem f ha)).2) },
{ rw [dif_neg, not_not.1 (λ c, ha ((mem_support _ _).2 c))],
contrapose! ha,
obtain ⟨b, hb1, hb2⟩ := (set.mem_image _ _ _).1 ha,
rwa f.injective hb2 at hb1 }
end
@[simp]
lemma emb_domain_mk_coeff {f : Γ → Γ'}
(hfi : function.injective f) (hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g')
{x : hahn_series Γ R} {a : Γ} :
(emb_domain ⟨⟨f, hfi⟩, hf⟩ x).coeff (f a) = x.coeff a :=
emb_domain_coeff
lemma emb_domain_notin_image_support {f : Γ ↪o Γ'} {x : hahn_series Γ R} {b : Γ'}
(hb : b ∉ f '' x.support) : (emb_domain f x).coeff b = 0 :=
dif_neg hb
lemma support_emb_domain_subset {f : Γ ↪o Γ'} {x : hahn_series Γ R} :
support (emb_domain f x) ⊆ f '' x.support :=
begin
intros g hg,
contrapose! hg,
rw [mem_support, emb_domain_notin_image_support hg, not_not],
end
lemma emb_domain_notin_range {f : Γ ↪o Γ'} {x : hahn_series Γ R} {b : Γ'}
(hb : b ∉ set.range f) : (emb_domain f x).coeff b = 0 :=
emb_domain_notin_image_support (λ con, hb (set.image_subset_range _ _ con))
@[simp]
lemma emb_domain_zero {f : Γ ↪o Γ'} : emb_domain f (0 : hahn_series Γ R) = 0 :=
by { ext, simp [emb_domain_notin_image_support] }
@[simp]
lemma emb_domain_single {f : Γ ↪o Γ'} {g : Γ} {r : R} :
emb_domain f (single g r) = single (f g) r :=
begin
ext g',
by_cases h : g' = f g,
{ simp [h] },
rw [emb_domain_notin_image_support, single_coeff_of_ne h],
by_cases hr : r = 0,
{ simp [hr] },
rwa [support_single_of_ne hr, set.image_singleton, set.mem_singleton_iff],
end
lemma emb_domain_injective {f : Γ ↪o Γ'} :
function.injective (emb_domain f : hahn_series Γ R → hahn_series Γ' R) :=
λ x y xy, begin
ext g,
rw [ext_iff, function.funext_iff] at xy,
have xyg := xy (f g),
rwa [emb_domain_coeff, emb_domain_coeff] at xyg,
end
end domain
end zero
section addition
variable [partial_order Γ]
section add_monoid
variable [add_monoid R]
instance : has_add (hahn_series Γ R) :=
{ add := λ x y, { coeff := x.coeff + y.coeff,
is_pwo_support' := (x.is_pwo_support.union y.is_pwo_support).mono
(function.support_add _ _) } }
instance : add_monoid (hahn_series Γ R) :=
{ zero := 0,
add := (+),
add_assoc := λ x y z, by { ext, apply add_assoc },
zero_add := λ x, by { ext, apply zero_add },
add_zero := λ x, by { ext, apply add_zero } }
@[simp]
lemma add_coeff' {x y : hahn_series Γ R} :
(x + y).coeff = x.coeff + y.coeff := rfl
lemma add_coeff {x y : hahn_series Γ R} {a : Γ} :
(x + y).coeff a = x.coeff a + y.coeff a := rfl
lemma support_add_subset {x y : hahn_series Γ R} :
support (x + y) ⊆ support x ∪ support y :=
λ a ha, begin
rw [mem_support, add_coeff] at ha,
rw [set.mem_union, mem_support, mem_support],
contrapose! ha,
rw [ha.1, ha.2, add_zero],
end
lemma min_order_le_order_add {Γ} [linear_ordered_cancel_add_comm_monoid Γ] {x y : hahn_series Γ R}
(hxy : x + y ≠ 0) :
min x.order y.order ≤ (x + y).order :=
begin
by_cases hx : x = 0, { simp [hx], },
by_cases hy : y = 0, { simp [hy], },
rw [order_of_ne hx, order_of_ne hy, order_of_ne hxy],
refine le_trans _ (set.is_wf.min_le_min_of_subset support_add_subset),
{ exact x.is_wf_support.union y.is_wf_support },
{ exact set.nonempty.mono (set.subset_union_left _ _) (support_nonempty_iff.2 hx) },
rw set.is_wf.min_union,
end
/-- `single` as an additive monoid/group homomorphism -/
@[simps] def single.add_monoid_hom (a : Γ) : R →+ (hahn_series Γ R) :=
{ map_add' := λ x y, by { ext b, by_cases h : b = a; simp [h] },
..single a }
/-- `coeff g` as an additive monoid/group homomorphism -/
@[simps] def coeff.add_monoid_hom (g : Γ) : (hahn_series Γ R) →+ R :=
{ to_fun := λ f, f.coeff g,
map_zero' := zero_coeff,
map_add' := λ x y, add_coeff }
section domain
variables {Γ' : Type*} [partial_order Γ']
lemma emb_domain_add (f : Γ ↪o Γ') (x y : hahn_series Γ R) :
emb_domain f (x + y) = emb_domain f x + emb_domain f y :=
begin
ext g,
by_cases hg : g ∈ set.range f,
{ obtain ⟨a, rfl⟩ := hg,
simp },
{ simp [emb_domain_notin_range, hg] }
end
end domain
end add_monoid
instance [add_comm_monoid R] : add_comm_monoid (hahn_series Γ R) :=
{ add_comm := λ x y, by { ext, apply add_comm }
.. hahn_series.add_monoid }
section add_group
variable [add_group R]
instance : add_group (hahn_series Γ R) :=
{ neg := λ x, { coeff := λ a, - x.coeff a,
is_pwo_support' := by { rw function.support_neg,
exact x.is_pwo_support }, },
add_left_neg := λ x, by { ext, apply add_left_neg },
.. hahn_series.add_monoid }
@[simp]
lemma neg_coeff' {x : hahn_series Γ R} : (- x).coeff = - x.coeff := rfl
lemma neg_coeff {x : hahn_series Γ R} {a : Γ} : (- x).coeff a = - x.coeff a := rfl
@[simp]
lemma support_neg {x : hahn_series Γ R} : (- x).support = x.support :=
by { ext, simp }
@[simp]
lemma sub_coeff' {x y : hahn_series Γ R} :
(x - y).coeff = x.coeff - y.coeff := by { ext, simp [sub_eq_add_neg] }
lemma sub_coeff {x y : hahn_series Γ R} {a : Γ} :
(x - y).coeff a = x.coeff a - y.coeff a := by simp
@[simp] lemma order_neg [has_zero Γ] {f : hahn_series Γ R} : (- f).order = f.order :=
by { by_cases hf : f = 0, { simp only [hf, neg_zero] },
simp only [order, support_neg, neg_eq_zero] }
end add_group
instance [add_comm_group R] : add_comm_group (hahn_series Γ R) :=
{ .. hahn_series.add_comm_monoid,
.. hahn_series.add_group }
end addition
section distrib_mul_action
variables [partial_order Γ] {V : Type*} [monoid R] [add_monoid V] [distrib_mul_action R V]
instance : has_smul R (hahn_series Γ V) :=
⟨λ r x, { coeff := r • x.coeff,
is_pwo_support' := x.is_pwo_support.mono (function.support_smul_subset_right r x.coeff) }⟩
@[simp]
lemma smul_coeff {r : R} {x : hahn_series Γ V} {a : Γ} : (r • x).coeff a = r • (x.coeff a) := rfl
instance : distrib_mul_action R (hahn_series Γ V) :=
{ smul := (•),
one_smul := λ _, by { ext, simp },
smul_zero := λ _, by { ext, simp },
smul_add := λ _ _ _, by { ext, simp [smul_add] },
mul_smul := λ _ _ _, by { ext, simp [mul_smul] } }
variables {S : Type*} [monoid S] [distrib_mul_action S V]
instance [has_smul R S] [is_scalar_tower R S V] :
is_scalar_tower R S (hahn_series Γ V) :=
⟨λ r s a, by { ext, simp }⟩
instance [smul_comm_class R S V] :
smul_comm_class R S (hahn_series Γ V) :=
⟨λ r s a, by { ext, simp [smul_comm] }⟩
end distrib_mul_action
section module
variables [partial_order Γ] [semiring R] {V : Type*} [add_comm_monoid V] [module R V]
instance : module R (hahn_series Γ V) :=
{ zero_smul := λ _, by { ext, simp },
add_smul := λ _ _ _, by { ext, simp [add_smul] },
.. hahn_series.distrib_mul_action }
/-- `single` as a linear map -/
@[simps] def single.linear_map (a : Γ) : R →ₗ[R] (hahn_series Γ R) :=
{ map_smul' := λ r s, by { ext b, by_cases h : b = a; simp [h] },
..single.add_monoid_hom a }
/-- `coeff g` as a linear map -/
@[simps] def coeff.linear_map (g : Γ) : (hahn_series Γ R) →ₗ[R] R :=
{ map_smul' := λ r s, rfl,
..coeff.add_monoid_hom g }
section domain
variables {Γ' : Type*} [partial_order Γ']
lemma emb_domain_smul (f : Γ ↪o Γ') (r : R) (x : hahn_series Γ R) :
emb_domain f (r • x) = r • emb_domain f x :=
begin
ext g,
by_cases hg : g ∈ set.range f,
{ obtain ⟨a, rfl⟩ := hg,
simp },
{ simp [emb_domain_notin_range, hg] }
end
/-- Extending the domain of Hahn series is a linear map. -/
@[simps] def emb_domain_linear_map (f : Γ ↪o Γ') : hahn_series Γ R →ₗ[R] hahn_series Γ' R :=
{ to_fun := emb_domain f, map_add' := emb_domain_add f, map_smul' := emb_domain_smul f }
end domain
end module
section multiplication
variable [ordered_cancel_add_comm_monoid Γ]
instance [has_zero R] [has_one R] : has_one (hahn_series Γ R) :=
⟨single 0 1⟩
@[simp]
lemma one_coeff [has_zero R] [has_one R] {a : Γ} :
(1 : hahn_series Γ R).coeff a = if a = 0 then 1 else 0 := single_coeff
@[simp]
lemma single_zero_one [has_zero R] [has_one R] : (single 0 (1 : R)) = 1 := rfl
@[simp]
lemma support_one [mul_zero_one_class R] [nontrivial R] :
support (1 : hahn_series Γ R) = {0} :=
support_single_of_ne one_ne_zero
@[simp]
lemma order_one [mul_zero_one_class R] :
order (1 : hahn_series Γ R) = 0 :=
begin
cases subsingleton_or_nontrivial R with h h; haveI := h,
{ rw [subsingleton.elim (1 : hahn_series Γ R) 0, order_zero] },
{ exact order_single one_ne_zero }
end
instance [non_unital_non_assoc_semiring R] : has_mul (hahn_series Γ R) :=
{ mul := λ x y, { coeff := λ a,
∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support a),
x.coeff ij.fst * y.coeff ij.snd,
is_pwo_support' := begin
have h : {a : Γ | ∑ (ij : Γ × Γ) in add_antidiagonal x.is_pwo_support
y.is_pwo_support a, x.coeff ij.fst * y.coeff ij.snd ≠ 0} ⊆
{a : Γ | (add_antidiagonal x.is_pwo_support y.is_pwo_support a).nonempty},
{ intros a ha,
contrapose! ha,
simp [not_nonempty_iff_eq_empty.1 ha] },
exact is_pwo_support_add_antidiagonal.mono h,
end, }, }
@[simp]
lemma mul_coeff [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} :
(x * y).coeff a = ∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support a),
x.coeff ij.fst * y.coeff ij.snd := rfl
lemma mul_coeff_right' [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} {s : set Γ}
(hs : s.is_pwo) (hys : y.support ⊆ s) :
(x * y).coeff a = ∑ ij in (add_antidiagonal x.is_pwo_support hs a),
x.coeff ij.fst * y.coeff ij.snd :=
begin
rw mul_coeff,
apply sum_subset_zero_on_sdiff (add_antidiagonal_mono_right hys) _ (λ _ _, rfl),
intros b hb,
simp only [not_and, mem_sdiff, mem_add_antidiagonal, mem_support, not_imp_not] at hb,
rw [hb.2 hb.1.1 hb.1.2.2, mul_zero],
end
lemma mul_coeff_left' [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} {s : set Γ}
(hs : s.is_pwo) (hxs : x.support ⊆ s) :
(x * y).coeff a = ∑ ij in (add_antidiagonal hs y.is_pwo_support a),
x.coeff ij.fst * y.coeff ij.snd :=
begin
rw mul_coeff,
apply sum_subset_zero_on_sdiff (add_antidiagonal_mono_left hxs) _ (λ _ _, rfl),
intros b hb,
simp only [not_and', mem_sdiff, mem_add_antidiagonal, mem_support, not_ne_iff] at hb,
rw [hb.2 ⟨hb.1.2.1, hb.1.2.2⟩, zero_mul],
end
instance [non_unital_non_assoc_semiring R] : distrib (hahn_series Γ R) :=
{ left_distrib := λ x y z, begin
ext a,
have hwf := (y.is_pwo_support.union z.is_pwo_support),
rw [mul_coeff_right' hwf, add_coeff, mul_coeff_right' hwf (set.subset_union_right _ _),
mul_coeff_right' hwf (set.subset_union_left _ _)],
{ simp only [add_coeff, mul_add, sum_add_distrib] },
{ intro b,
simp only [add_coeff, ne.def, set.mem_union, set.mem_set_of_eq, mem_support],
contrapose!,
intro h,
rw [h.1, h.2, add_zero], }
end,
right_distrib := λ x y z, begin
ext a,
have hwf := (x.is_pwo_support.union y.is_pwo_support),
rw [mul_coeff_left' hwf, add_coeff, mul_coeff_left' hwf (set.subset_union_right _ _),
mul_coeff_left' hwf (set.subset_union_left _ _)],
{ simp only [add_coeff, add_mul, sum_add_distrib] },
{ intro b,
simp only [add_coeff, ne.def, set.mem_union, set.mem_set_of_eq, mem_support],
contrapose!,
intro h,
rw [h.1, h.2, add_zero], },
end,
.. hahn_series.has_mul,
.. hahn_series.has_add }
lemma single_mul_coeff_add [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ}
{b : Γ} :
((single b r) * x).coeff (a + b) = r * x.coeff a :=
begin
by_cases hr : r = 0,
{ simp [hr] },
simp only [hr, smul_coeff, mul_coeff, support_single_of_ne, ne.def, not_false_iff, smul_eq_mul],
by_cases hx : x.coeff a = 0,
{ simp only [hx, mul_zero],
rw [sum_congr _ (λ _ _, rfl), sum_empty],
ext ⟨a1, a2⟩,
simp only [not_mem_empty, not_and, set.mem_singleton_iff, not_not,
mem_add_antidiagonal, set.mem_set_of_eq, iff_false],
rintro rfl h2 h1,
rw add_comm at h1,
rw ← add_right_cancel h1 at hx,
exact h2 hx, },
transitivity ∑ (ij : Γ × Γ) in {(b, a)}, (single b r).coeff ij.fst * x.coeff ij.snd,
{ apply sum_congr _ (λ _ _, rfl),
ext ⟨a1, a2⟩,
simp only [set.mem_singleton_iff, prod.mk.inj_iff, mem_add_antidiagonal,
mem_singleton, set.mem_set_of_eq],
split,
{ rintro ⟨rfl, h2, h1⟩,
rw add_comm at h1,
refine ⟨rfl, add_right_cancel h1⟩ },
{ rintro ⟨rfl, rfl⟩,
exact ⟨rfl, by simp [hx], add_comm _ _⟩ } },
{ simp }
end
lemma mul_single_coeff_add [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ}
{b : Γ} :
(x * (single b r)).coeff (a + b) = x.coeff a * r :=
begin
by_cases hr : r = 0,
{ simp [hr] },
simp only [hr, smul_coeff, mul_coeff, support_single_of_ne, ne.def, not_false_iff, smul_eq_mul],
by_cases hx : x.coeff a = 0,
{ simp only [hx, zero_mul],
rw [sum_congr _ (λ _ _, rfl), sum_empty],
ext ⟨a1, a2⟩,
simp only [not_mem_empty, not_and, set.mem_singleton_iff, not_not,
mem_add_antidiagonal, set.mem_set_of_eq, iff_false],
rintro h2 rfl h1,
rw ← add_right_cancel h1 at hx,
exact h2 hx, },
transitivity ∑ (ij : Γ × Γ) in {(a,b)}, x.coeff ij.fst * (single b r).coeff ij.snd,
{ apply sum_congr _ (λ _ _, rfl),
ext ⟨a1, a2⟩,
simp only [set.mem_singleton_iff, prod.mk.inj_iff, mem_add_antidiagonal,
mem_singleton, set.mem_set_of_eq],
split,
{ rintro ⟨h2, rfl, h1⟩,
refine ⟨add_right_cancel h1, rfl⟩ },
{ rintro ⟨rfl, rfl⟩,
simp [hx] } },
{ simp }
end
@[simp]
lemma mul_single_zero_coeff [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R}
{a : Γ} :
(x * (single 0 r)).coeff a = x.coeff a * r :=
by rw [← add_zero a, mul_single_coeff_add, add_zero]
lemma single_zero_mul_coeff [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R}
{a : Γ} :
((single 0 r) * x).coeff a = r * x.coeff a :=
by rw [← add_zero a, single_mul_coeff_add, add_zero]
@[simp]
lemma single_zero_mul_eq_smul [semiring R] {r : R} {x : hahn_series Γ R} :
(single 0 r) * x = r • x :=
by { ext, exact single_zero_mul_coeff }
theorem support_mul_subset_add_support [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} :
support (x * y) ⊆ support x + support y :=
begin
apply set.subset.trans (λ x hx, _) support_add_antidiagonal_subset_add,
{ exact x.is_pwo_support },
{ exact y.is_pwo_support },
contrapose! hx,
simp only [not_nonempty_iff_eq_empty, ne.def, set.mem_set_of_eq] at hx,
simp [hx],
end
lemma mul_coeff_order_add_order {Γ} [linear_ordered_cancel_add_comm_monoid Γ]
[non_unital_non_assoc_semiring R]
(x y : hahn_series Γ R) :
(x * y).coeff (x.order + y.order) = x.coeff x.order * y.coeff y.order :=
begin
by_cases hx : x = 0, { simp [hx], },
by_cases hy : y = 0, { simp [hy], },
rw [order_of_ne hx, order_of_ne hy, mul_coeff, finset.add_antidiagonal_min_add_min,
finset.sum_singleton],
end
private lemma mul_assoc' [non_unital_semiring R] (x y z : hahn_series Γ R) :
x * y * z = x * (y * z) :=
begin
ext b,
rw [mul_coeff_left' (x.is_pwo_support.add y.is_pwo_support) support_mul_subset_add_support,
mul_coeff_right' (y.is_pwo_support.add z.is_pwo_support) support_mul_subset_add_support],
simp only [mul_coeff, add_coeff, sum_mul, mul_sum, sum_sigma'],
refine sum_bij_ne_zero (λ a has ha0, ⟨⟨a.2.1, a.2.2 + a.1.2⟩, ⟨a.2.2, a.1.2⟩⟩) _ _ _ _,
{ rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2,
simp only [and_true, set.image2_add, eq_self_iff_true, mem_add_antidiagonal, ne.def,
set.image_prod, mem_sigma, set.mem_set_of_eq] at H1 H2 ⊢,
obtain ⟨⟨H3, nz, rfl⟩, nx, ny, rfl⟩ := H1,
exact ⟨⟨nx, set.add_mem_add ny nz, (add_assoc _ _ _).symm⟩, ny, nz⟩ },
{ rintros ⟨⟨i1,j1⟩, k1,l1⟩ ⟨⟨i2,j2⟩, k2,l2⟩ H1 H2 H3 H4 H5,
simp only [set.image2_add, prod.mk.inj_iff, mem_add_antidiagonal, ne.def,
set.image_prod, mem_sigma, set.mem_set_of_eq, heq_iff_eq] at H1 H3 H5,
obtain ⟨⟨rfl, H⟩, rfl, rfl⟩ := H5,
simp only [and_true, prod.mk.inj_iff, eq_self_iff_true, heq_iff_eq, ←H1.2.2.2, ←H3.2.2.2] },
{ rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2,
simp only [exists_prop, set.image2_add, prod.mk.inj_iff, mem_add_antidiagonal,
sigma.exists, ne.def, set.image_prod, mem_sigma, set.mem_set_of_eq, heq_iff_eq,
prod.exists] at H1 H2 ⊢,
obtain ⟨⟨nx, H, rfl⟩, ny, nz, rfl⟩ := H1,
exact ⟨i + k, l, i, k, ⟨⟨set.add_mem_add nx ny, nz, add_assoc _ _ _⟩, nx, ny, rfl⟩,
λ con, H2 ((mul_assoc _ _ _).symm.trans con), ⟨rfl, rfl⟩, rfl, rfl⟩ },
{ rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2,
simp [mul_assoc], }
end
instance [non_unital_non_assoc_semiring R] : non_unital_non_assoc_semiring (hahn_series Γ R) :=
{ zero := 0,
add := (+),
mul := (*),
zero_mul := λ _, by { ext, simp },
mul_zero := λ _, by { ext, simp },
.. hahn_series.add_comm_monoid,
.. hahn_series.distrib }
instance [non_unital_semiring R] : non_unital_semiring (hahn_series Γ R) :=
{ zero := 0,
add := (+),
mul := (*),
mul_assoc := mul_assoc',
.. hahn_series.non_unital_non_assoc_semiring }
instance [non_assoc_semiring R] : non_assoc_semiring (hahn_series Γ R) :=
{ zero := 0,
one := 1,
add := (+),
mul := (*),
one_mul := λ x, by { ext, exact single_zero_mul_coeff.trans (one_mul _) },
mul_one := λ x, by { ext, exact mul_single_zero_coeff.trans (mul_one _) },
.. add_monoid_with_one.unary,
.. hahn_series.non_unital_non_assoc_semiring }
instance [semiring R] : semiring (hahn_series Γ R) :=
{ zero := 0,
one := 1,
add := (+),
mul := (*),
.. hahn_series.non_assoc_semiring,
.. hahn_series.non_unital_semiring }
instance [non_unital_comm_semiring R] : non_unital_comm_semiring (hahn_series Γ R) :=
{ mul_comm := λ x y, begin
ext,
simp_rw [mul_coeff, mul_comm],
refine sum_bij (λ a ha, a.swap) (λ a ha, _) (λ a ha, rfl) (λ _ _ _ _, prod.swap_inj.1)
(λ a ha, ⟨a.swap, _, a.swap_swap.symm⟩);
rwa swap_mem_add_antidiagonal,
end,
.. hahn_series.non_unital_semiring }
instance [comm_semiring R] : comm_semiring (hahn_series Γ R) :=
{ .. hahn_series.non_unital_comm_semiring,
.. hahn_series.semiring }
instance [non_unital_non_assoc_ring R] : non_unital_non_assoc_ring (hahn_series Γ R) :=
{ .. hahn_series.non_unital_non_assoc_semiring,
.. hahn_series.add_group }
instance [non_unital_ring R] : non_unital_ring (hahn_series Γ R) :=
{ .. hahn_series.non_unital_non_assoc_ring,
.. hahn_series.non_unital_semiring }
instance [non_assoc_ring R] : non_assoc_ring (hahn_series Γ R) :=
{ .. hahn_series.non_unital_non_assoc_ring,
.. hahn_series.non_assoc_semiring }
instance [ring R] : ring (hahn_series Γ R) :=
{ .. hahn_series.semiring,
.. hahn_series.add_comm_group }
instance [non_unital_comm_ring R] : non_unital_comm_ring (hahn_series Γ R) :=
{ .. hahn_series.non_unital_comm_semiring,
.. hahn_series.non_unital_ring }
instance [comm_ring R] : comm_ring (hahn_series Γ R) :=
{ .. hahn_series.comm_semiring,
.. hahn_series.ring }
instance {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [non_unital_non_assoc_semiring R]
[no_zero_divisors R] : no_zero_divisors (hahn_series Γ R) :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := λ x y xy, begin
by_cases hx : x = 0,
{ left, exact hx },
right,
contrapose! xy,
rw [hahn_series.ext_iff, function.funext_iff, not_forall],
refine ⟨x.order + y.order, _⟩,
rw [mul_coeff_order_add_order x y, zero_coeff, mul_eq_zero],
simp [coeff_order_ne_zero, hx, xy],
end }
instance {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [ring R] [is_domain R] :
is_domain (hahn_series Γ R) :=
no_zero_divisors.to_is_domain _
@[simp]
lemma order_mul {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [non_unital_non_assoc_semiring R]
[no_zero_divisors R] {x y : hahn_series Γ R} (hx : x ≠ 0) (hy : y ≠ 0) :
(x * y).order = x.order + y.order :=
begin
apply le_antisymm,
{ apply order_le_of_coeff_ne_zero,
rw [mul_coeff_order_add_order x y],
exact mul_ne_zero (coeff_order_ne_zero hx) (coeff_order_ne_zero hy) },
{ rw [order_of_ne hx, order_of_ne hy, order_of_ne (mul_ne_zero hx hy), ← set.is_wf.min_add],
exact set.is_wf.min_le_min_of_subset (support_mul_subset_add_support) },
end
@[simp]
lemma order_pow {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [semiring R] [no_zero_divisors R]
(x : hahn_series Γ R) (n : ℕ) : (x ^ n).order = n • x.order :=
begin
induction n with h IH,
{ simp },
rcases eq_or_ne x 0 with rfl|hx,
{ simp },
rw [pow_succ', order_mul (pow_ne_zero _ hx) hx, succ_nsmul', IH]
end
section non_unital_non_assoc_semiring
variables [non_unital_non_assoc_semiring R]
@[simp]
lemma single_mul_single {a b : Γ} {r s : R} :
single a r * single b s = single (a + b) (r * s) :=
begin
ext x,
by_cases h : x = a + b,
{ rw [h, mul_single_coeff_add],
simp },
{ rw [single_coeff_of_ne h, mul_coeff, sum_eq_zero],
simp_rw mem_add_antidiagonal,
rintro ⟨y, z⟩ ⟨hy, hz, rfl⟩,
rw [eq_of_mem_support_single hy, eq_of_mem_support_single hz] at h,
exact (h rfl).elim }
end
end non_unital_non_assoc_semiring
section non_assoc_semiring
variables [non_assoc_semiring R]
/-- `C a` is the constant Hahn Series `a`. `C` is provided as a ring homomorphism. -/
@[simps] def C : R →+* (hahn_series Γ R) :=
{ to_fun := single 0,
map_zero' := single_eq_zero,
map_one' := rfl,
map_add' := λ x y, by { ext a, by_cases h : a = 0; simp [h] },
map_mul' := λ x y, by rw [single_mul_single, zero_add] }
@[simp]
lemma C_zero : C (0 : R) = (0 : hahn_series Γ R) := C.map_zero
@[simp]
lemma C_one : C (1 : R) = (1 : hahn_series Γ R) := C.map_one
lemma C_injective : function.injective (C : R → hahn_series Γ R) :=
begin
intros r s rs,
rw [ext_iff, function.funext_iff] at rs,
have h := rs 0,
rwa [C_apply, single_coeff_same, C_apply, single_coeff_same] at h,
end
lemma C_ne_zero {r : R} (h : r ≠ 0) : (C r : hahn_series Γ R) ≠ 0 :=
begin
contrapose! h,
rw ← C_zero at h,
exact C_injective h,
end
lemma order_C {r : R} : order (C r : hahn_series Γ R) = 0 :=
begin
by_cases h : r = 0,
{ rw [h, C_zero, order_zero] },
{ exact order_single h }
end
end non_assoc_semiring
section semiring
variables [semiring R]
lemma C_mul_eq_smul {r : R} {x : hahn_series Γ R} : C r * x = r • x :=
single_zero_mul_eq_smul
end semiring
section domain
variables {Γ' : Type*} [ordered_cancel_add_comm_monoid Γ']
lemma emb_domain_mul [non_unital_non_assoc_semiring R]
(f : Γ ↪o Γ') (hf : ∀ x y, f (x + y) = f x + f y) (x y : hahn_series Γ R) :
emb_domain f (x * y) = emb_domain f x * emb_domain f y :=
begin
ext g,
by_cases hg : g ∈ set.range f,
{ obtain ⟨g, rfl⟩ := hg,
simp only [mul_coeff, emb_domain_coeff],
transitivity ∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support g).map
(function.embedding.prod_map f.to_embedding f.to_embedding),
(emb_domain f x).coeff (ij.1) *
(emb_domain f y).coeff (ij.2),
{ simp },
apply sum_subset,
{ rintro ⟨i, j⟩ hij,
simp only [exists_prop, mem_map, prod.mk.inj_iff, mem_add_antidiagonal,
function.embedding.coe_prod_map, mem_support, prod.exists] at hij,
obtain ⟨i, j, ⟨hx, hy, rfl⟩, rfl, rfl⟩ := hij,
simp [hx, hy, hf], },
{ rintro ⟨_, _⟩ h1 h2,
contrapose! h2,
obtain ⟨i, hi, rfl⟩ := support_emb_domain_subset (ne_zero_and_ne_zero_of_mul h2).1,
obtain ⟨j, hj, rfl⟩ := support_emb_domain_subset (ne_zero_and_ne_zero_of_mul h2).2,
simp only [exists_prop, mem_map, prod.mk.inj_iff, mem_add_antidiagonal,
function.embedding.coe_prod_map, mem_support, prod.exists],
simp only [mem_add_antidiagonal, emb_domain_coeff, mem_support, ←hf,
order_embedding.eq_iff_eq] at h1,
exact ⟨i, j, h1, rfl⟩ } },
{ rw [emb_domain_notin_range hg, eq_comm],
contrapose! hg,
obtain ⟨_, _, hi, hj, rfl⟩ := support_mul_subset_add_support ((mem_support _ _).2 hg),
obtain ⟨i, hi, rfl⟩ := support_emb_domain_subset hi,
obtain ⟨j, hj, rfl⟩ := support_emb_domain_subset hj,
refine ⟨i + j, hf i j⟩, }
end
lemma emb_domain_one [non_assoc_semiring R] (f : Γ ↪o Γ') (hf : f 0 = 0):
emb_domain f (1 : hahn_series Γ R) = (1 : hahn_series Γ' R) :=
emb_domain_single.trans $ hf.symm ▸ rfl
/-- Extending the domain of Hahn series is a ring homomorphism. -/
@[simps] def emb_domain_ring_hom [non_assoc_semiring R] (f : Γ →+ Γ') (hfi : function.injective f)
(hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g') :
hahn_series Γ R →+* hahn_series Γ' R :=
{ to_fun := emb_domain ⟨⟨f, hfi⟩, hf⟩,
map_one' := emb_domain_one _ f.map_zero,
map_mul' := emb_domain_mul _ f.map_add,
map_zero' := emb_domain_zero,
map_add' := emb_domain_add _}
lemma emb_domain_ring_hom_C [non_assoc_semiring R] {f : Γ →+ Γ'} {hfi : function.injective f}
{hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g'} {r : R} :
emb_domain_ring_hom f hfi hf (C r) = C r :=
emb_domain_single.trans (by simp)
end domain
section algebra
variables [comm_semiring R] {A : Type*} [semiring A] [algebra R A]
instance : algebra R (hahn_series Γ A) :=
{ to_ring_hom := C.comp (algebra_map R A),
smul_def' := λ r x, by { ext, simp },
commutes' := λ r x, by { ext, simp only [smul_coeff, single_zero_mul_eq_smul, ring_hom.coe_comp,
ring_hom.to_fun_eq_coe, C_apply, function.comp_app, algebra_map_smul, mul_single_zero_coeff],
rw [← algebra.commutes, algebra.smul_def], }, }
theorem C_eq_algebra_map : C = (algebra_map R (hahn_series Γ R)) := rfl
theorem algebra_map_apply {r : R} :
algebra_map R (hahn_series Γ A) r = C (algebra_map R A r) := rfl
instance [nontrivial Γ] [nontrivial R] : nontrivial (subalgebra R (hahn_series Γ R)) :=
⟨⟨⊥, ⊤, begin
rw [ne.def, set_like.ext_iff, not_forall],
obtain ⟨a, ha⟩ := exists_ne (0 : Γ),
refine ⟨single a 1, _⟩,
simp only [algebra.mem_bot, not_exists, set.mem_range, iff_true, algebra.mem_top],
intros x,
rw [ext_iff, function.funext_iff, not_forall],
refine ⟨a, _⟩,
rw [single_coeff_same, algebra_map_apply, C_apply, single_coeff_of_ne ha],
exact zero_ne_one
end⟩⟩
section domain
variables {Γ' : Type*} [ordered_cancel_add_comm_monoid Γ']
/-- Extending the domain of Hahn series is an algebra homomorphism. -/
@[simps] def emb_domain_alg_hom (f : Γ →+ Γ') (hfi : function.injective f)
(hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g') :
hahn_series Γ A →ₐ[R] hahn_series Γ' A :=
{ commutes' := λ r, emb_domain_ring_hom_C,
.. emb_domain_ring_hom f hfi hf }
end domain
end algebra
end multiplication
section semiring
variables [semiring R]
/-- The ring `hahn_series ℕ R` is isomorphic to `power_series R`. -/
@[simps] def to_power_series : hahn_series ℕ R ≃+* power_series R :=
{ to_fun := λ f, power_series.mk f.coeff,
inv_fun := λ f, ⟨λ n, power_series.coeff R n f, (nat.lt_wf.is_wf _).is_pwo⟩,
left_inv := λ f, by { ext, simp },
right_inv := λ f, by { ext, simp },
map_add' := λ f g, by { ext, simp },
map_mul' := λ f g, begin
ext n,
simp only [power_series.coeff_mul, power_series.coeff_mk, mul_coeff, is_pwo_support],
classical,
refine sum_filter_ne_zero.symm.trans ((sum_congr _ $ λ _ _, rfl).trans sum_filter_ne_zero),
ext m,
simp only [nat.mem_antidiagonal, mem_add_antidiagonal, and.congr_left_iff, mem_filter,
mem_support],
rintro h,
rw [and_iff_right (left_ne_zero_of_mul h), and_iff_right (right_ne_zero_of_mul h)],
end }
lemma coeff_to_power_series {f : hahn_series ℕ R} {n : ℕ} :
power_series.coeff R n f.to_power_series = f.coeff n :=
power_series.coeff_mk _ _
lemma coeff_to_power_series_symm {f : power_series R} {n : ℕ} :
(hahn_series.to_power_series.symm f).coeff n = power_series.coeff R n f := rfl
variables (Γ R) [strict_ordered_semiring Γ]
/-- Casts a power series as a Hahn series with coefficients from an `strict_ordered_semiring`. -/
def of_power_series : (power_series R) →+* hahn_series Γ R :=
(hahn_series.emb_domain_ring_hom (nat.cast_add_monoid_hom Γ) nat.strict_mono_cast.injective
(λ _ _, nat.cast_le)).comp
(ring_equiv.to_ring_hom to_power_series.symm)
variables {Γ} {R}
lemma of_power_series_injective : function.injective (of_power_series Γ R) :=
emb_domain_injective.comp to_power_series.symm.injective
@[simp] lemma of_power_series_apply (x : power_series R) :
of_power_series Γ R x = hahn_series.emb_domain
⟨⟨(coe : ℕ → Γ), nat.strict_mono_cast.injective⟩, λ a b, begin
simp only [function.embedding.coe_fn_mk],
exact nat.cast_le,
end⟩ (to_power_series.symm x) := rfl
lemma of_power_series_apply_coeff (x : power_series R) (n : ℕ) :
(of_power_series Γ R x).coeff n = power_series.coeff R n x :=
by simp
@[simp] lemma of_power_series_C (r : R) :
of_power_series Γ R (power_series.C R r) = hahn_series.C r :=
begin
ext n,
simp only [C, single_coeff, of_power_series_apply, ring_hom.coe_mk],
split_ifs with hn hn,
{ subst hn,
convert @emb_domain_coeff _ _ _ _ _ _ _ _ 0; simp },
{ rw emb_domain_notin_image_support,
simp only [not_exists, set.mem_image, to_power_series_symm_apply_coeff, mem_support,
power_series.coeff_C],
intro,
simp [ne.symm hn] {contextual := tt} }
end
@[simp] lemma of_power_series_X :
of_power_series Γ R power_series.X = single 1 1 :=
begin
ext n,
simp only [single_coeff, of_power_series_apply, ring_hom.coe_mk],
split_ifs with hn hn,
{ rw hn,
convert @emb_domain_coeff _ _ _ _ _ _ _ _ 1;
simp },
{ rw emb_domain_notin_image_support,
simp only [not_exists, set.mem_image, to_power_series_symm_apply_coeff, mem_support,
power_series.coeff_X],
intro,
simp [ne.symm hn] {contextual := tt} }
end
@[simp] lemma of_power_series_X_pow {R} [comm_semiring R] (n : ℕ) :
of_power_series Γ R (power_series.X ^ n) = single (n : Γ) 1 :=
begin
rw ring_hom.map_pow,
induction n with n ih,
{ simp, refl },
rw [pow_succ, ih, of_power_series_X, mul_comm, single_mul_single, one_mul, nat.cast_succ]
end
-- Lemmas about converting hahn_series over fintype to and from mv_power_series
/-- The ring `hahn_series (σ →₀ ℕ) R` is isomorphic to `mv_power_series σ R` for a `fintype` `σ`.
We take the index set of the hahn series to be `finsupp` rather than `pi`,
even though we assume `fintype σ` as this is more natural for alignment with `mv_power_series`.
After importing `algebra.order.pi` the ring `hahn_series (σ → ℕ) R` could be constructed instead.
-/
@[simps] def to_mv_power_series {σ : Type*} [fintype σ] :
hahn_series (σ →₀ ℕ) R ≃+* mv_power_series σ R :=
{ to_fun := λ f, f.coeff,
inv_fun := λ f, ⟨(f : (σ →₀ ℕ) → R), finsupp.is_pwo _⟩,
left_inv := λ f, by { ext, simp },
right_inv := λ f, by { ext, simp },
map_add' := λ f g, by { ext, simp },
map_mul' := λ f g, begin
ext n,
simp only [mv_power_series.coeff_mul],
classical,
change (f * g).coeff n = _,
simp_rw [mul_coeff],
refine sum_filter_ne_zero.symm.trans ((sum_congr _ (λ _ _, rfl)).trans sum_filter_ne_zero),
ext m,
simp only [and.congr_left_iff, mem_add_antidiagonal, mem_filter, mem_support,
finsupp.mem_antidiagonal],
rintro h,
rw [and_iff_right (left_ne_zero_of_mul h), and_iff_right (right_ne_zero_of_mul h)],
end }
variables {σ : Type*} [fintype σ]
lemma coeff_to_mv_power_series {f : hahn_series (σ →₀ ℕ) R} {n : σ →₀ ℕ} :
mv_power_series.coeff R n f.to_mv_power_series = f.coeff n :=
rfl
lemma coeff_to_mv_power_series_symm {f : mv_power_series σ R} {n : σ →₀ ℕ} :
(hahn_series.to_mv_power_series.symm f).coeff n = mv_power_series.coeff R n f := rfl
end semiring
section algebra
variables (R) [comm_semiring R] {A : Type*} [semiring A] [algebra R A]
/-- The `R`-algebra `hahn_series ℕ A` is isomorphic to `power_series A`. -/
@[simps] def to_power_series_alg : (hahn_series ℕ A) ≃ₐ[R] power_series A :=
{ commutes' := λ r, begin
ext n,
simp only [algebra_map_apply, power_series.algebra_map_apply, ring_equiv.to_fun_eq_coe, C_apply,
coeff_to_power_series],
cases n,
{ simp only [power_series.coeff_zero_eq_constant_coeff, single_coeff_same],
refl },
{ simp only [n.succ_ne_zero, ne.def, not_false_iff, single_coeff_of_ne],
rw [power_series.coeff_C, if_neg n.succ_ne_zero] }
end,
.. to_power_series }
variables (Γ R) [strict_ordered_semiring Γ]
/-- Casting a power series as a Hahn series with coefficients from an `strict_ordered_semiring`
is an algebra homomorphism. -/
@[simps] def of_power_series_alg : (power_series A) →ₐ[R] hahn_series Γ A :=
(hahn_series.emb_domain_alg_hom (nat.cast_add_monoid_hom Γ) nat.strict_mono_cast.injective
(λ _ _, nat.cast_le)).comp
(alg_equiv.to_alg_hom (to_power_series_alg R).symm)
instance power_series_algebra {S : Type*} [comm_semiring S] [algebra S (power_series R)] :
algebra S (hahn_series Γ R) :=
ring_hom.to_algebra $ (of_power_series Γ R).comp (algebra_map S (power_series R))
variables {R} {S : Type*} [comm_semiring S] [algebra S (power_series R)]
lemma algebra_map_apply' (x : S) :
algebra_map S (hahn_series Γ R) x = of_power_series Γ R (algebra_map S (power_series R) x) := rfl
@[simp] lemma _root_.polynomial.algebra_map_hahn_series_apply (f : R[X]) :
algebra_map R[X] (hahn_series Γ R) f = of_power_series Γ R f := rfl
lemma _root_.polynomial.algebra_map_hahn_series_injective :
function.injective (algebra_map R[X] (hahn_series Γ R)) :=
of_power_series_injective.comp (polynomial.coe_injective R)
end algebra
section valuation
variables (Γ R) [linear_ordered_cancel_add_comm_monoid Γ] [ring R] [is_domain R]
/-- The additive valuation on `hahn_series Γ R`, returning the smallest index at which
a Hahn Series has a nonzero coefficient, or `⊤` for the 0 series. -/
def add_val : add_valuation (hahn_series Γ R) (with_top Γ) :=
add_valuation.of (λ x, if x = (0 : hahn_series Γ R) then (⊤ : with_top Γ) else x.order)
(if_pos rfl)
((if_neg one_ne_zero).trans (by simp [order_of_ne]))
(λ x y, begin
by_cases hx : x = 0,
{ by_cases hy : y = 0; { simp [hx, hy] } },
{ by_cases hy : y = 0,
{ simp [hx, hy] },
{ simp only [hx, hy, support_nonempty_iff, if_neg, not_false_iff, is_wf_support],
by_cases hxy : x + y = 0,
{ simp [hxy] },
rw [if_neg hxy, ← with_top.coe_min, with_top.coe_le_coe],
exact min_order_le_order_add hxy } },
end)
(λ x y, begin
by_cases hx : x = 0,
{ simp [hx] },
by_cases hy : y = 0,
{ simp [hy] },
rw [if_neg hx, if_neg hy, if_neg (mul_ne_zero hx hy),
← with_top.coe_add, with_top.coe_eq_coe, order_mul hx hy],
end)
variables {Γ} {R}
lemma add_val_apply {x : hahn_series Γ R} :
add_val Γ R x = if x = (0 : hahn_series Γ R) then (⊤ : with_top Γ) else x.order :=
add_valuation.of_apply _
@[simp]
lemma add_val_apply_of_ne {x : hahn_series Γ R} (hx : x ≠ 0) :
add_val Γ R x = x.order :=
if_neg hx
lemma add_val_le_of_coeff_ne_zero {x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) :
add_val Γ R x ≤ g :=
begin
rw [add_val_apply_of_ne (ne_zero_of_coeff_ne_zero h), with_top.coe_le_coe],
exact order_le_of_coeff_ne_zero h
end
end valuation
lemma is_pwo_Union_support_powers
[linear_ordered_cancel_add_comm_monoid Γ] [ring R] [is_domain R]
{x : hahn_series Γ R} (hx : 0 < add_val Γ R x) :
(⋃ n : ℕ, (x ^ n).support).is_pwo :=
begin
apply (x.is_wf_support.is_pwo.add_submonoid_closure (λ g hg, _)).mono _,
{ exact with_top.coe_le_coe.1 (le_trans (le_of_lt hx) (add_val_le_of_coeff_ne_zero hg)) },
refine set.Union_subset (λ n, _),
induction n with n ih;
intros g hn,
{ simp only [exists_prop, and_true, set.mem_singleton_iff, set.set_of_eq_eq_singleton,
mem_support, ite_eq_right_iff, ne.def, not_false_iff, one_ne_zero,
pow_zero, not_forall, one_coeff] at hn,
rw [hn, set_like.mem_coe],
exact add_submonoid.zero_mem _ },
{ obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support hn,
exact set_like.mem_coe.2 (add_submonoid.add_mem _ (add_submonoid.subset_closure hi) (ih hj)) }
end
section
variables (Γ) (R) [partial_order Γ] [add_comm_monoid R]
/-- An infinite family of Hahn series which has a formal coefficient-wise sum.
The requirements for this are that the union of the supports of the series is well-founded,
and that only finitely many series are nonzero at any given coefficient. -/
structure summable_family (α : Type*) :=
(to_fun : α → hahn_series Γ R)
(is_pwo_Union_support' : set.is_pwo (⋃ (a : α), (to_fun a).support))
(finite_co_support' : ∀ (g : Γ), ({a | (to_fun a).coeff g ≠ 0}).finite)
end
namespace summable_family
section add_comm_monoid
variables [partial_order Γ] [add_comm_monoid R] {α : Type*}
instance : has_coe_to_fun (summable_family Γ R α) (λ _, α → hahn_series Γ R):=
⟨to_fun⟩
lemma is_pwo_Union_support (s : summable_family Γ R α) : set.is_pwo (⋃ (a : α), (s a).support) :=
s.is_pwo_Union_support'
lemma finite_co_support (s : summable_family Γ R α) (g : Γ) :
(function.support (λ a, (s a).coeff g)).finite :=
s.finite_co_support' g
lemma coe_injective : @function.injective (summable_family Γ R α) (α → hahn_series Γ R) coe_fn
| ⟨f1, hU1, hf1⟩ ⟨f2, hU2, hf2⟩ h :=
begin
change f1 = f2 at h,
subst h,
end
@[ext]
lemma ext {s t : summable_family Γ R α} (h : ∀ (a : α), s a = t a) : s = t :=
coe_injective $ funext h
instance : has_add (summable_family Γ R α) :=
⟨λ x y, { to_fun := x + y,
is_pwo_Union_support' := (x.is_pwo_Union_support.union y.is_pwo_Union_support).mono (begin
rw ← set.Union_union_distrib,
exact set.Union_mono (λ a, support_add_subset)
end),
finite_co_support' := λ g, ((x.finite_co_support g).union (y.finite_co_support g)).subset begin
intros a ha,
change (x a).coeff g + (y a).coeff g ≠ 0 at ha,
rw [set.mem_union, function.mem_support, function.mem_support],
contrapose! ha,
rw [ha.1, ha.2, add_zero]
end }⟩
instance : has_zero (summable_family Γ R α) :=
⟨⟨0, by simp, by simp⟩⟩
instance : inhabited (summable_family Γ R α) := ⟨0⟩
@[simp]
lemma coe_add {s t : summable_family Γ R α} : ⇑(s + t) = s + t := rfl
lemma add_apply {s t : summable_family Γ R α} {a : α} : (s + t) a = s a + t a := rfl
@[simp]
lemma coe_zero : ((0 : summable_family Γ R α) : α → hahn_series Γ R) = 0 := rfl
lemma zero_apply {a : α} : (0 : summable_family Γ R α) a = 0 := rfl
instance : add_comm_monoid (summable_family Γ R α) :=
{ add := (+),
zero := 0,
zero_add := λ s, by { ext, apply zero_add },
add_zero := λ s, by { ext, apply add_zero },
add_comm := λ s t, by { ext, apply add_comm },
add_assoc := λ r s t, by { ext, apply add_assoc } }
/-- The infinite sum of a `summable_family` of Hahn series. -/
def hsum (s : summable_family Γ R α) :
hahn_series Γ R :=
{ coeff := λ g, ∑ᶠ i, (s i).coeff g,
is_pwo_support' := s.is_pwo_Union_support.mono (λ g, begin
contrapose,
rw [set.mem_Union, not_exists, function.mem_support, not_not],
simp_rw [mem_support, not_not],
intro h,
rw [finsum_congr h, finsum_zero],
end) }
@[simp]
lemma hsum_coeff {s : summable_family Γ R α} {g : Γ} :
s.hsum.coeff g = ∑ᶠ i, (s i).coeff g := rfl
lemma support_hsum_subset {s : summable_family Γ R α} :
s.hsum.support ⊆ ⋃ (a : α), (s a).support :=
λ g hg, begin
rw [mem_support, hsum_coeff, finsum_eq_sum _ (s.finite_co_support _)] at hg,
obtain ⟨a, h1, h2⟩ := exists_ne_zero_of_sum_ne_zero hg,
rw [set.mem_Union],
exact ⟨a, h2⟩,
end
@[simp]
lemma hsum_add {s t : summable_family Γ R α} : (s + t).hsum = s.hsum + t.hsum :=
begin
ext g,
simp only [hsum_coeff, add_coeff, add_apply],
exact finsum_add_distrib (s.finite_co_support _) (t.finite_co_support _)
end
end add_comm_monoid
section add_comm_group
variables [partial_order Γ] [add_comm_group R] {α : Type*} {s t : summable_family Γ R α} {a : α}
instance : add_comm_group (summable_family Γ R α) :=
{ neg := λ s, { to_fun := λ a, - s a,
is_pwo_Union_support' := by { simp_rw [support_neg], exact s.is_pwo_Union_support' },
finite_co_support' := λ g, by { simp only [neg_coeff', pi.neg_apply, ne.def, neg_eq_zero],
exact s.finite_co_support g } },
add_left_neg := λ a, by { ext, apply add_left_neg },
.. summable_family.add_comm_monoid }
@[simp]
lemma coe_neg : ⇑(-s) = - s := rfl
lemma neg_apply : (-s) a = - (s a) := rfl
@[simp]
lemma coe_sub : ⇑(s - t) = s - t := rfl
lemma sub_apply : (s - t) a = s a - t a := rfl
end add_comm_group
section semiring
variables [ordered_cancel_add_comm_monoid Γ] [semiring R] {α : Type*}
instance : has_smul (hahn_series Γ R) (summable_family Γ R α) :=
{ smul := λ x s, { to_fun := λ a, x * (s a),
is_pwo_Union_support' := begin
apply (x.is_pwo_support.add s.is_pwo_Union_support).mono,
refine set.subset.trans (set.Union_mono (λ a, support_mul_subset_add_support)) _,
intro g,
simp only [set.mem_Union, exists_imp_distrib],
exact λ a ha, (set.add_subset_add (set.subset.refl _) (set.subset_Union _ a)) ha,
end,
finite_co_support' := λ g, begin
refine ((add_antidiagonal x.is_pwo_support s.is_pwo_Union_support g).finite_to_set.bUnion'
(λ ij hij, _)).subset (λ a ha, _),
{ exact λ ij hij, function.support (λ a, (s a).coeff ij.2) },
{ apply s.finite_co_support },
{ obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support ha,
simp only [exists_prop, set.mem_Union, mem_add_antidiagonal, mul_coeff, mem_support,
is_pwo_support, prod.exists],
exact ⟨i, j, mem_coe.2 (mem_add_antidiagonal.2 ⟨hi, set.mem_Union.2 ⟨a, hj⟩, rfl⟩), hj⟩ }
end } }
@[simp]
lemma smul_apply {x : hahn_series Γ R} {s : summable_family Γ R α} {a : α} :
(x • s) a = x * (s a) := rfl
instance : module (hahn_series Γ R) (summable_family Γ R α) :=
{ smul := (•),
smul_zero := λ x, ext (λ a, mul_zero _),
zero_smul := λ x, ext (λ a, zero_mul _),
one_smul := λ x, ext (λ a, one_mul _),
add_smul := λ x y s, ext (λ a, add_mul _ _ _),
smul_add := λ x s t, ext (λ a, mul_add _ _ _),
mul_smul := λ x y s, ext (λ a, mul_assoc _ _ _) }
@[simp]
lemma hsum_smul {x : hahn_series Γ R} {s : summable_family Γ R α} :
(x • s).hsum = x * s.hsum :=
begin
ext g,
simp only [mul_coeff, hsum_coeff, smul_apply],
have h : ∀ i, (s i).support ⊆ ⋃ j, (s j).support := set.subset_Union _,
refine (eq.trans (finsum_congr (λ a, _))
(finsum_sum_comm (add_antidiagonal x.is_pwo_support s.is_pwo_Union_support g)
(λ i ij, x.coeff (prod.fst ij) * (s i).coeff ij.snd) _)).trans _,
{ refine sum_subset (add_antidiagonal_mono_right (set.subset_Union _ a)) _,
rintro ⟨i, j⟩ hU ha,
rw mem_add_antidiagonal at *,
rw [not_not.1 (λ con, ha ⟨hU.1, con, hU.2.2⟩), mul_zero] },
{ rintro ⟨i, j⟩ hij,
refine (s.finite_co_support j).subset _,
simp_rw [function.support_subset_iff', function.mem_support, not_not],
intros a ha,
rw [ha, mul_zero] },
{ refine (sum_congr rfl _).trans (sum_subset (add_antidiagonal_mono_right _) _).symm,
{ rintro ⟨i, j⟩ hij,
rw mul_finsum,
apply s.finite_co_support, },
{ intros x hx,
simp only [set.mem_Union, ne.def, mem_support],
contrapose! hx,
simp [hx] },
{ rintro ⟨i, j⟩ hU ha,
rw mem_add_antidiagonal at *,
rw [← hsum_coeff, not_not.1 (λ con, ha ⟨hU.1, con, hU.2.2⟩), mul_zero] } }
end
/-- The summation of a `summable_family` as a `linear_map`. -/
@[simps] def lsum : (summable_family Γ R α) →ₗ[hahn_series Γ R] (hahn_series Γ R) :=
{ to_fun := hsum, map_add' := λ _ _, hsum_add, map_smul' := λ _ _, hsum_smul }
@[simp]
lemma hsum_sub {R : Type*} [ring R] {s t : summable_family Γ R α} :
(s - t).hsum = s.hsum - t.hsum :=
by rw [← lsum_apply, linear_map.map_sub, lsum_apply, lsum_apply]
end semiring
section of_finsupp
variables [partial_order Γ] [add_comm_monoid R] {α : Type*}
/-- A family with only finitely many nonzero elements is summable. -/
def of_finsupp (f : α →₀ (hahn_series Γ R)) :
summable_family Γ R α :=
{ to_fun := f,
is_pwo_Union_support' := begin
apply (f.support.is_pwo_bUnion.2 $ λ a ha, (f a).is_pwo_support).mono,
refine set.Union_subset_iff.2 (λ a g hg, _),
have haf : a ∈ f.support,
{ rw [finsupp.mem_support_iff, ← support_nonempty_iff],
exact ⟨g, hg⟩ },
exact set.mem_bUnion haf hg
end,
finite_co_support' := λ g, begin
refine f.support.finite_to_set.subset (λ a ha, _),
simp only [coeff.add_monoid_hom_apply, mem_coe, finsupp.mem_support_iff,
ne.def, function.mem_support],
contrapose! ha,
simp [ha]
end }
@[simp]
lemma coe_of_finsupp {f : α →₀ (hahn_series Γ R)} : ⇑(summable_family.of_finsupp f) = f := rfl
@[simp]
lemma hsum_of_finsupp {f : α →₀ (hahn_series Γ R)} :
(of_finsupp f).hsum = f.sum (λ a, id) :=
begin
ext g,
simp only [hsum_coeff, coe_of_finsupp, finsupp.sum, ne.def],
simp_rw [← coeff.add_monoid_hom_apply, id.def],
rw [add_monoid_hom.map_sum, finsum_eq_sum_of_support_subset],
intros x h,
simp only [coeff.add_monoid_hom_apply, mem_coe, finsupp.mem_support_iff, ne.def],
contrapose! h,
simp [h]
end
end of_finsupp
section emb_domain
variables [partial_order Γ] [add_comm_monoid R] {α β : Type*}
/-- A summable family can be reindexed by an embedding without changing its sum. -/
def emb_domain (s : summable_family Γ R α) (f : α ↪ β) : summable_family Γ R β :=
{ to_fun := λ b, if h : b ∈ set.range f then s (classical.some h) else 0,
is_pwo_Union_support' := begin
refine s.is_pwo_Union_support.mono (set.Union_subset (λ b g h, _)),
by_cases hb : b ∈ set.range f,
{ rw dif_pos hb at h,
exact set.mem_Union.2 ⟨classical.some hb, h⟩ },
{ contrapose! h,
simp [hb] }
end,
finite_co_support' := λ g, ((s.finite_co_support g).image f).subset begin
intros b h,
by_cases hb : b ∈ set.range f,
{ simp only [ne.def, set.mem_set_of_eq, dif_pos hb] at h,
exact ⟨classical.some hb, h, classical.some_spec hb⟩ },
{ contrapose! h,
simp only [ne.def, set.mem_set_of_eq, dif_neg hb, not_not, zero_coeff] }
end }
variables (s : summable_family Γ R α) (f : α ↪ β) {a : α} {b : β}
lemma emb_domain_apply :
s.emb_domain f b = if h : b ∈ set.range f then s (classical.some h) else 0 := rfl
@[simp] lemma emb_domain_image : s.emb_domain f (f a) = s a :=
begin
rw [emb_domain_apply, dif_pos (set.mem_range_self a)],
exact congr rfl (f.injective (classical.some_spec (set.mem_range_self a)))
end
@[simp] lemma emb_domain_notin_range (h : b ∉ set.range f) : s.emb_domain f b = 0 :=
by rw [emb_domain_apply, dif_neg h]
@[simp] lemma hsum_emb_domain :
(s.emb_domain f).hsum = s.hsum :=
begin
ext g,
simp only [hsum_coeff, emb_domain_apply, apply_dite hahn_series.coeff, dite_apply, zero_coeff],
exact finsum_emb_domain f (λ a, (s a).coeff g)
end
end emb_domain
section powers
variables [linear_ordered_cancel_add_comm_monoid Γ] [comm_ring R] [is_domain R]
/-- The powers of an element of positive valuation form a summable family. -/
def powers (x : hahn_series Γ R) (hx : 0 < add_val Γ R x) :
summable_family Γ R ℕ :=
{ to_fun := λ n, x ^ n,
is_pwo_Union_support' := is_pwo_Union_support_powers hx,
finite_co_support' := λ g, begin
have hpwo := (is_pwo_Union_support_powers hx),
by_cases hg : g ∈ ⋃ n : ℕ, {g | (x ^ n).coeff g ≠ 0 },
swap, { exact set.finite_empty.subset (λ n hn, hg (set.mem_Union.2 ⟨n, hn⟩)) },
apply hpwo.is_wf.induction hg,
intros y ys hy,
refine ((((add_antidiagonal x.is_pwo_support hpwo y).finite_to_set.bUnion (λ ij hij,
hy ij.snd _ _)).image nat.succ).union (set.finite_singleton 0)).subset _,
{ exact (mem_add_antidiagonal.1 (mem_coe.1 hij)).2.1 },
{ obtain ⟨hi, hj, rfl⟩ := mem_add_antidiagonal.1 (mem_coe.1 hij),
rw [← zero_add ij.snd, ← add_assoc, add_zero],
exact add_lt_add_right (with_top.coe_lt_coe.1
(lt_of_lt_of_le hx (add_val_le_of_coeff_ne_zero hi))) _, },
{ rintro (_ | n) hn,
{ exact set.mem_union_right _ (set.mem_singleton 0) },
{ obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support hn,
refine set.mem_union_left _ ⟨n, set.mem_Union.2 ⟨⟨i, j⟩, set.mem_Union.2 ⟨_, hj⟩⟩, rfl⟩,
simp only [and_true, set.mem_Union, mem_add_antidiagonal, mem_coe, eq_self_iff_true,
ne.def, mem_support, set.mem_set_of_eq],
exact ⟨hi, n, hj⟩ } }
end }
variables {x : hahn_series Γ R} (hx : 0 < add_val Γ R x)
@[simp] lemma coe_powers : ⇑(powers x hx) = pow x := rfl
lemma emb_domain_succ_smul_powers :
(x • powers x hx).emb_domain ⟨nat.succ, nat.succ_injective⟩ =
powers x hx - of_finsupp (finsupp.single 0 1) :=
begin
apply summable_family.ext (λ n, _),
cases n,
{ rw [emb_domain_notin_range, sub_apply, coe_powers, pow_zero, coe_of_finsupp,
finsupp.single_eq_same, sub_self],
rw [set.mem_range, not_exists],
exact nat.succ_ne_zero },
{ refine eq.trans (emb_domain_image _ ⟨nat.succ, nat.succ_injective⟩) _,
simp only [pow_succ, coe_powers, coe_sub, smul_apply, coe_of_finsupp, pi.sub_apply],
rw [finsupp.single_eq_of_ne (n.succ_ne_zero).symm, sub_zero] }
end
lemma one_sub_self_mul_hsum_powers :
(1 - x) * (powers x hx).hsum = 1 :=
begin
rw [← hsum_smul, sub_smul, one_smul, hsum_sub,
← hsum_emb_domain (x • powers x hx) ⟨nat.succ, nat.succ_injective⟩,
emb_domain_succ_smul_powers],
simp,
end
end powers
end summable_family
section inversion
variables [linear_ordered_add_comm_group Γ]
section is_domain
variables [comm_ring R] [is_domain R]
lemma unit_aux (x : hahn_series Γ R) {r : R} (hr : r * x.coeff x.order = 1) :
0 < add_val Γ R (1 - C r * (single (- x.order) 1) * x) :=
begin
have h10 : (1 : R) ≠ 0 := one_ne_zero,
have x0 : x ≠ 0 := ne_zero_of_coeff_ne_zero (right_ne_zero_of_mul_eq_one hr),
refine lt_of_le_of_ne ((add_val Γ R).map_le_sub (ge_of_eq (add_val Γ R).map_one) _) _,
{ simp only [add_valuation.map_mul],
rw [add_val_apply_of_ne x0, add_val_apply_of_ne (single_ne_zero h10),
add_val_apply_of_ne _, order_C, order_single h10, with_top.coe_zero, zero_add,
← with_top.coe_add, neg_add_self, with_top.coe_zero],
{ exact le_refl 0 },
{ exact C_ne_zero (left_ne_zero_of_mul_eq_one hr) } },
{ rw [add_val_apply, ← with_top.coe_zero],
split_ifs,
{ apply with_top.coe_ne_top },
rw [ne.def, with_top.coe_eq_coe],
intro con,
apply coeff_order_ne_zero h,
rw [← con, mul_assoc, sub_coeff, one_coeff, if_pos rfl, C_mul_eq_smul, smul_coeff, smul_eq_mul,
← add_neg_self x.order, single_mul_coeff_add, one_mul, hr, sub_self] }
end
lemma is_unit_iff {x : hahn_series Γ R} :
is_unit x ↔ is_unit (x.coeff x.order) :=
begin
split,
{ rintro ⟨⟨u, i, ui, iu⟩, rfl⟩,
refine is_unit_of_mul_eq_one (u.coeff u.order) (i.coeff i.order)
((mul_coeff_order_add_order u i).symm.trans _),
rw [ui, one_coeff, if_pos],
rw [← order_mul (left_ne_zero_of_mul_eq_one ui)
(right_ne_zero_of_mul_eq_one ui), ui, order_one] },
{ rintro ⟨⟨u, i, ui, iu⟩, h⟩,
rw [units.coe_mk] at h,
rw h at iu,
have h := summable_family.one_sub_self_mul_hsum_powers (unit_aux x iu),
rw [sub_sub_cancel] at h,
exact is_unit_of_mul_is_unit_right (is_unit_of_mul_eq_one _ _ h) },
end
end is_domain
instance [field R] : field (hahn_series Γ R) :=
{ inv := λ x, if x0 : x = 0 then 0 else (C (x.coeff x.order)⁻¹ * (single (-x.order)) 1 *
(summable_family.powers _ (unit_aux x (inv_mul_cancel (coeff_order_ne_zero x0)))).hsum),
inv_zero := dif_pos rfl,
mul_inv_cancel := λ x x0, begin
refine (congr rfl (dif_neg x0)).trans _,
have h := summable_family.one_sub_self_mul_hsum_powers
(unit_aux x (inv_mul_cancel (coeff_order_ne_zero x0))),
rw [sub_sub_cancel] at h,
rw [← mul_assoc, mul_comm x, h],
end,
.. hahn_series.is_domain,
.. hahn_series.comm_ring }
end inversion
end hahn_series
|
7b5ff40563f1b1a507cab6c3ecdcc83fd3d265ed | 4727251e0cd73359b15b664c3170e5d754078599 | /src/set_theory/game/ordinal.lean | 93409d6988639012be8d790986d19fec95de3825 | [
"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,872 | lean | /-
Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta Hernández Palacios
-/
import set_theory.game.pgame
import set_theory.ordinal.basic
/-!
# Ordinals as games
We define the canonical map `ordinal → pgame`, where every ordinal is mapped to the game whose left
set consists of all previous ordinals.
# Main declarations
- `ordinal.to_pgame`: The canonical map between ordinals and pre-games.
- `ordinal.to_pgame_embedding`: The order embedding version of the previous map.
# Todo
- Extend this map to `game` and `surreal`.
-/
local infix ` ≈ ` := pgame.equiv
universe u
namespace ordinal
/-- Converts an ordinal into the corresponding pre-game. -/
noncomputable! def to_pgame : Π o : ordinal.{u}, pgame.{u}
| o := ⟨o.out.α, pempty, λ x, let hwf := ordinal.typein_lt_self x in
(typein (<) x).to_pgame, pempty.elim⟩
using_well_founded { dec_tac := tactic.assumption }
theorem to_pgame_def (o : ordinal) :
o.to_pgame = ⟨o.out.α, pempty, λ x, (typein (<) x).to_pgame, pempty.elim⟩ :=
by rw to_pgame
@[simp] theorem to_pgame_left_moves (o : ordinal) : o.to_pgame.left_moves = o.out.α :=
by rw [to_pgame, pgame.left_moves]
@[simp] theorem to_pgame_right_moves (o : ordinal) : o.to_pgame.right_moves = pempty :=
by rw [to_pgame, pgame.right_moves]
instance : is_empty (to_pgame 0).left_moves :=
by { rw to_pgame_left_moves, apply_instance }
instance (o : ordinal) : is_empty o.to_pgame.right_moves :=
by { rw to_pgame_right_moves, apply_instance }
/-- Converts an ordinal less than `o` into a move for the `pgame` corresponding to `o`, and vice
versa. -/
noncomputable def to_left_moves_to_pgame {o : ordinal} : {o' // o' < o} ≃ o.to_pgame.left_moves :=
(out_equiv_lt o).trans (equiv.cast (to_pgame_left_moves o).symm)
@[simp] theorem to_left_moves_to_pgame_symm_lt {o : ordinal} (i : o.to_pgame.left_moves) :
↑(to_left_moves_to_pgame.symm i) < o :=
(to_left_moves_to_pgame.symm i).prop
theorem to_pgame_move_left_heq {o : ordinal} :
o.to_pgame.move_left == λ x : o.out.α, (typein (<) x).to_pgame :=
by { rw to_pgame, refl }
@[simp] theorem to_pgame_move_left' {o : ordinal} (i) :
o.to_pgame.move_left i = (to_left_moves_to_pgame.symm i).val.to_pgame :=
(congr_heq to_pgame_move_left_heq.symm (cast_heq _ i)).symm
theorem to_pgame_move_left {o : ordinal} (i) :
o.to_pgame.move_left (to_left_moves_to_pgame i) = i.val.to_pgame :=
by simp
theorem to_pgame_lt {a b : ordinal} (h : a < b) : a.to_pgame < b.to_pgame :=
by { convert pgame.move_left_lt (to_left_moves_to_pgame ⟨a, h⟩), rw to_pgame_move_left }
theorem to_pgame_le {a b : ordinal} (h : a ≤ b) : a.to_pgame ≤ b.to_pgame :=
pgame.le_def.2 ⟨λ i, or.inl ⟨to_left_moves_to_pgame
⟨_, (to_left_moves_to_pgame_symm_lt i).trans_le h⟩, by simp⟩, is_empty_elim⟩
@[simp] theorem to_pgame_lt_iff {a b : ordinal} : a.to_pgame < b.to_pgame ↔ a < b :=
⟨by { contrapose, rw [not_lt, pgame.not_lt], exact to_pgame_le }, to_pgame_lt⟩
@[simp] theorem to_pgame_le_iff {a b : ordinal} : a.to_pgame ≤ b.to_pgame ↔ a ≤ b :=
⟨by { contrapose, rw [not_le, pgame.not_le], exact to_pgame_lt }, to_pgame_le⟩
@[simp] theorem to_pgame_equiv_iff {a b : ordinal} : a.to_pgame ≈ b.to_pgame ↔ a = b :=
by rw [pgame.equiv, le_antisymm_iff, to_pgame_le_iff, to_pgame_le_iff]
theorem to_pgame_injective : function.injective ordinal.to_pgame :=
λ a b h, begin
by_contra hne,
cases lt_or_gt_of_ne hne with hlt hlt;
{ have := to_pgame_lt hlt,
rw h at this,
exact pgame.lt_irrefl _ this }
end
/-- The order embedding version of `to_pgame`. -/
@[simps] noncomputable def to_pgame_embedding : ordinal.{u} ↪o pgame.{u} :=
{ to_fun := ordinal.to_pgame,
inj' := to_pgame_injective,
map_rel_iff' := @to_pgame_le_iff }
end ordinal
|
d6422fb5cdb4eb8cb0fb710012e38c0039f0b141 | 6b45072eb2b3db3ecaace2a7a0241ce81f815787 | /tools/auto/experiments/test1.lean | edef53f867659cd3cc0a8172bf634c9b85916047 | [] | no_license | avigad/library_dev | 27b47257382667b5eb7e6476c4f5b0d685dd3ddc | 9d8ac7c7798ca550874e90fed585caad030bbfac | refs/heads/master | 1,610,452,468,791 | 1,500,712,839,000 | 1,500,713,478,000 | 69,311,142 | 1 | 0 | null | 1,474,942,903,000 | 1,474,942,902,000 | null | UTF-8 | Lean | false | false | 4,960 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
Examples to test finish. (The designations "clarify," "safe," "iauto," etc. are
from a previous tableau prover.)
-/
import ..finish
open auto
section
variables A B C D : Prop
/- clarify -/
example (H : ¬ A) (H' : A) : C := by finish
example (H₁ : A ∧ A ∧ B) (H₂ : A ∧ C ∧ B) : A := by finish
/- safe -/
example (H : A) (H₁ : ¬ B) : ¬ (A → B) := by finish
example : A ∨ B → B ∨ A := by finish
example : A ∧ B → B ∧ A := by finish
example (H : A) (H₁ : A → B) (H₂ : B → C) : C := by finish
example (H₁ : A ∧ B) (H₂ : C ∧ B) : C := by finish
example (HA : A) (HB : B) (HC : C) (HD : D) : (A ∧ B) ∧ (C ∧ D) := by finish
example (H₁ : A ∧ B) (H₂ : B ∧ ¬ C) : A ∨ C := by finish
example : (A → B) ∧ (B → C) → A → C := by finish
example : (A → B) ∨ (B → A) := by finish
example : ((A → B) → A) → A := by finish
/- iauto -/
example (H : A) (H₁ : ¬ B) : ¬ (A → B) := by finish
example : ¬ (A ↔ ¬ A) := by finish
example (H : A ↔ B) (H₁ : A ∧ B → C) (H₂ : ¬ A ∧ ¬ B → C) : C := by finish
example (H : A ↔ B) (H₁ : (A ∧ ¬ B) ∨ (¬ A ∧ B)) : C := by finish
example (H : A → B) (H₁ : A) : B := by finish
example (H : A → B) (H₁ : B → C) : A → C := by finish
example (H : A → B ∨ C) (H₁ : B → D) (H₂ : C → D) : A → D := by finish
example : A ∨ B → B ∨ A := by finish
/- using injectivity -/
section
open nat
example (x y : ℕ) : succ x = succ y → x = y ∨ x = succ y := by finish
example (x y z : ℕ) : succ (succ x) = succ y ∧ y = succ z →
y = succ x ∧ succ x = succ z :=
by finish
end
/-
-- Examples with quantifiers
-/
section
variables (X : Type) (P Q R : X → Prop) (T : X → X → Prop) (a b : X)
/- auto -/
example (H : ∀ x, P x → Q x) (H₁ : ∀ x, P x) : Q a := by finish
example (H : ∀ x, P x → Q x) (H₁ : P a) : Q a := by finish
/- iauto -/
example (H₁ : P a) (H₂ : P b) : ∃ x, P x := by finish
example (H₁ : P a) (H₂ : P b) (H₃ : Q b) : ∃ x, P x ∧ Q x := by finish
example (H₁ : P b) (H₂ : Q b) (H₃ : P a) : ∃ x, P x ∧ Q x := by finish
example (H : ∀ x, P x → Q x ∧ R x) (a : X) (H₁ : P a) : R a ∧ Q a := by finish
example (H : ∃ x, P x ∧ Q x) : ∃ x, Q x ∧ P x := by finish
-- not valid in dependent type theory!
-- example : ∃ x, ((∃ y, P y) → P x) :=
-- by auto'
/- Beyond the scope of finish.
example (H : ∃ x : X, x = x) : ∃ x, ((∃ y, P y) → P x) := by finish
example : (∃ x, ∀ y, T x y) → ∀ y, ∃ x, T x y := by finish
-/
end
example (x y z : ℕ) (p : ℕ → Prop) (h₀ : x = y) (h₁ : y = z) (h₂ : ∀ w, w = z → p w) : p x :=
by finish
end
/-
more examples
-/
constant foo : Prop
axiom not_foo : ¬ foo
section
variables a b c d : Prop
example : a ∧ b → a := by finish
example : a → (a → b) → (b → c) ∧ (d → ¬ c) → ¬ d := by finish
example : a ∨ b → b ∨ a := by finish
example : ¬ (a ↔ ¬ a) :=
begin
finish
end
/- examples of tactics that leave goals -/
/-
example : a ∨ b ∨ foo → b ∨ a :=
begin
clarify,
admit
end
example : a ∨ b ∨ foo ∨ foo → b ∨ a :=
begin
safe,
admit
end
example : a ∨ b ∨ c → ¬ a → ¬ b → d :=
begin
safe,
admit
end
-/
end
section
variables (a b c : ℕ) (p q : ℕ → Prop) (r : ℕ → ℕ → Prop)
variables (P Q R : Prop)
variable (g : bool → nat)
example (h₁ : ∀ x, p x → q x) (h₂ : ∀ x, p x) : q a :=
by finish
example (h₁ : p a) : ∃ x, p x :=
by finish
example (h₁ : p a) (h₂ : p b) (h₃ : q b) : ∃ x, p x ∧ q x :=
by finish
example (h : ∃ x, p x ∧ r x x) (h' : ∀ x, r x x → q x) : ∃ x, p x ∧ q x :=
by finish
example (h : ∃ x, q x ∧ p x) : ∃ x, p x ∧ q x :=
by finish
example (h₁ : ∀ x, q x → p x) (h₃ : q a) : ∃ x, p x :=
by finish
example (h₁ : ∀ x, p x → q x → false) (h₂ : p a) (h₃ : p b) (h₄ : q b) : false :=
by finish
example (h : ∀ x, p x) (h₁ : ∀ x, p x → q x) : ∀ x, q x :=
by finish
example (h : ∃ x, p x) (h₁ : ∀ x, p x → q x) : ∃ x, q x :=
by finish
example (h : ¬ ∀ x, ¬ p x) (h₁ : ∀ x, p x → q x) (h₂ : ∀ x, ¬ q x) : false :=
by finish
/-
example (h : p a) (h' : p a → false) : false :=
by finish
-/
end
section
variables a b c d : Prop
variables (p q : ℕ → Prop) (r : ℕ → ℕ → Prop)
example (h₁ : ¬ (a → b ∨ c)) (h₂ : ¬ (b ∨ ¬ c)) : true :=
begin
normalize_hyps {classical := false},
trivial
end
example (h : ¬ ∀ x, (∃ y, r x y) → p x) : true :=
begin
normalize_hyps {},
trivial
end
example (h₁ : a → b ∨ c) (h₂ : ¬ b) : a → c :=
begin
simp * at *,
assumption
end
end
|
9a42ab6f857ecd166833dea28914abb40ff0c052 | 5e3548e65f2c037cb94cd5524c90c623fbd6d46a | /IMO_2007_Pb_5.lean | c9afc5b2bba529f5a8ee7d773be8c72c80a1f06e | [] | no_license | ahayat16/lean_exos | d4f08c30adb601a06511a71b5ffb4d22d12ef77f | 682f2552d5b04a8c8eb9e4ab15f875a91b03845c | refs/heads/main | 1,693,101,073,585 | 1,636,479,336,000 | 1,636,479,336,000 | 415,000,441 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 167 | lean |
import data.pnat.basic
import number_theory.divisors
theorem IMO_2007_Pb_5 (a b: pnat) (h: ↑(4*a*b-1) ∈ (nat.divisors ((4*a^2-1)^2))): a=b :=
begin
sorry
end
|
72d0643364ed80b87829d5951b92976bb2e17232 | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/linear_algebra/finite_dimensional.lean | 48c38619fea1e32015923bb5ea8e322829f60ac1 | [
"Apache-2.0"
] | permissive | agjftucker/mathlib | d634cd0d5256b6325e3c55bb7fb2403548371707 | 87fe50de17b00af533f72a102d0adefe4a2285e8 | refs/heads/master | 1,625,378,131,941 | 1,599,166,526,000 | 1,599,166,526,000 | 160,748,509 | 0 | 0 | Apache-2.0 | 1,544,141,789,000 | 1,544,141,789,000 | null | UTF-8 | Lean | false | false | 40,665 | lean | /-
Copyright (c) 2019 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import linear_algebra.dimension
import ring_theory.principal_ideal_domain
/-!
# Finite dimensional vector spaces
Definition and basic properties of finite dimensional vector spaces, of their dimensions, and
of linear maps on such spaces.
## Main definitions
Assume `V` is a vector space over a field `K`. There are (at least) three equivalent definitions of
finite-dimensionality of `V`:
- it admits a finite basis.
- it is finitely generated.
- it is noetherian, i.e., every subspace is finitely generated.
We introduce a typeclass `finite_dimensional K V` capturing this property. For ease of transfer of
proof, it is defined using the third point of view, i.e., as `is_noetherian`. However, we prove
that all these points of view are equivalent, with the following lemmas
(in the namespace `finite_dimensional`):
- `exists_is_basis_finite` states that a finite-dimensional vector space has a finite basis
- `of_finite_basis` states that the existence of a finite basis implies finite-dimensionality
- `iff_fg` states that the space is finite-dimensional if and only if it is finitely generated
Also defined is `findim`, the dimension of a finite dimensional space, returning a `nat`,
as opposed to `dim`, which returns a `cardinal`. When the space has infinite dimension, its
`findim` is by convention set to `0`.
Preservation of finite-dimensionality and formulas for the dimension are given for
- submodules
- quotients (for the dimension of a quotient, see `findim_quotient_add_findim`)
- linear equivs, in `linear_equiv.finite_dimensional` and `linear_equiv.findim_eq`
- image under a linear map (the rank-nullity formula is in `findim_range_add_findim_ker`)
Basic properties of linear maps of a finite-dimensional vector space are given. Notably, the
equivalence of injectivity and surjectivity is proved in `linear_map.injective_iff_surjective`,
and the equivalence between left-inverse and right-inverse in `mul_eq_one_comm` and
`comp_eq_id_comm`.
## Implementation notes
Most results are deduced from the corresponding results for the general dimension (as a cardinal),
in `dimension.lean`. Not all results have been ported yet.
One of the characterizations of finite-dimensionality is in terms of finite generation. This
property is currently defined only for submodules, so we express it through the fact that the
maximal submodule (which, as a set, coincides with the whole space) is finitely generated. This is
not very convenient to use, although there are some helper functions. However, this becomes very
convenient when speaking of submodules which are finite-dimensional, as this notion coincides with
the fact that the submodule is finitely generated (as a submodule of the whole space). This
equivalence is proved in `submodule.fg_iff_finite_dimensional`.
-/
universes u v v' w
open_locale classical
open vector_space cardinal submodule module function
variables {K : Type u} {V : Type v} [field K] [add_comm_group V] [vector_space K V]
{V₂ : Type v'} [add_comm_group V₂] [vector_space K V₂]
/-- `finite_dimensional` vector spaces are defined to be noetherian modules.
Use `finite_dimensional.iff_fg` or `finite_dimensional.of_finite_basis` to prove finite dimension
from a conventional definition. -/
@[reducible] def finite_dimensional (K V : Type*) [field K]
[add_comm_group V] [vector_space K V] := is_noetherian K V
namespace finite_dimensional
open is_noetherian
/-- A vector space is finite-dimensional if and only if its dimension (as a cardinal) is strictly
less than the first infinite cardinal `omega`. -/
lemma finite_dimensional_iff_dim_lt_omega : finite_dimensional K V ↔ dim K V < omega.{v} :=
begin
cases exists_is_basis K V with b hb,
have := is_basis.mk_eq_dim hb,
simp only [lift_id] at this,
rw [← this, lt_omega_iff_fintype, ← @set.set_of_mem_eq _ b, ← subtype.range_coe_subtype],
split,
{ intro, resetI, convert finite_of_linear_independent hb.1, simp },
{ assume hbfinite,
refine @is_noetherian_of_linear_equiv K (⊤ : submodule K V) V _
_ _ _ _ (linear_equiv.of_top _ rfl) (id _),
refine is_noetherian_of_fg_of_noetherian _ ⟨set.finite.to_finset hbfinite, _⟩,
rw [set.finite.coe_to_finset, ← hb.2], refl }
end
/-- The dimension of a finite-dimensional vector space, as a cardinal, is strictly less than the
first infinite cardinal `omega`. -/
lemma dim_lt_omega (K V : Type*) [field K] [add_comm_group V] [vector_space K V] :
∀ [finite_dimensional K V], dim K V < omega.{v} :=
finite_dimensional_iff_dim_lt_omega.1
/-- In a finite dimensional space, there exists a finite basis. A basis is in general given as a
function from an arbitrary type to the vector space. Here, we think of a basis as a set (instead of
a function), and use as parametrizing type this set (and as a function the coercion
`coe : s → V`).
-/
variables (K V)
lemma exists_is_basis_finite [finite_dimensional K V] :
∃ s : set V, (is_basis K (coe : s → V)) ∧ s.finite :=
begin
cases exists_is_basis K V with s hs,
exact ⟨s, hs, finite_of_linear_independent hs.1⟩
end
/-- In a finite dimensional space, there exists a finite basis. Provides the basis as a finset.
This is in contrast to `exists_is_basis_finite`, which provides a set and a `set.finite`.
-/
lemma exists_is_basis_finset [finite_dimensional K V] :
∃ b : finset V, is_basis K (coe : (↑b : set V) → V) :=
begin
obtain ⟨s, s_basis, s_finite⟩ := exists_is_basis_finite K V,
refine ⟨s_finite.to_finset, _⟩,
rw set.finite.coe_to_finset,
exact s_basis,
end
variables {K V}
/-- A vector space is finite-dimensional if and only if it is finitely generated. As the
finitely-generated property is a property of submodules, we formulate this in terms of the
maximal submodule, equal to the whole space as a set by definition.-/
lemma iff_fg :
finite_dimensional K V ↔ (⊤ : submodule K V).fg :=
begin
split,
{ introI h,
rcases exists_is_basis_finite K V with ⟨s, s_basis, s_finite⟩,
exact ⟨s_finite.to_finset, by { convert s_basis.2, simp }⟩ },
{ rintros ⟨s, hs⟩,
rw [finite_dimensional_iff_dim_lt_omega, ← dim_top, ← hs],
exact lt_of_le_of_lt (dim_span_le _) (lt_omega_iff_finite.2 (set.finite_mem_finset s)) }
end
/-- If a vector space has a finite basis, then it is finite-dimensional. -/
lemma of_finite_basis {ι : Type w} [fintype ι] {b : ι → V} (h : is_basis K b) :
finite_dimensional K V :=
iff_fg.2 $ ⟨finset.univ.image b, by {convert h.2, simp} ⟩
/-- If a vector space has a finite basis, then it is finite-dimensional, finset style. -/
lemma of_finset_basis {b : finset V} (h : is_basis K (coe : (↑b : set V) -> V)) :
finite_dimensional K V :=
iff_fg.2 $ ⟨b, by {convert h.2, simp} ⟩
/-- A subspace of a finite-dimensional space is also finite-dimensional. -/
instance finite_dimensional_submodule [finite_dimensional K V] (S : submodule K V) :
finite_dimensional K S :=
finite_dimensional_iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_submodule_le _) (dim_lt_omega K V))
/-- A quotient of a finite-dimensional space is also finite-dimensional. -/
instance finite_dimensional_quotient [finite_dimensional K V] (S : submodule K V) :
finite_dimensional K (quotient S) :=
finite_dimensional_iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_quotient_le _) (dim_lt_omega K V))
/-- The dimension of a finite-dimensional vector space as a natural number. Defined by convention to
be `0` if the space is infinite-dimensional. -/
noncomputable def findim (K V : Type*) [field K]
[add_comm_group V] [vector_space K V] : ℕ :=
if h : dim K V < omega.{v} then classical.some (lt_omega.1 h) else 0
/-- In a finite-dimensional space, its dimension (seen as a cardinal) coincides with its `findim`. -/
lemma findim_eq_dim (K : Type u) (V : Type v) [field K]
[add_comm_group V] [vector_space K V] [finite_dimensional K V] :
(findim K V : cardinal.{v}) = dim K V :=
begin
have : findim K V = classical.some (lt_omega.1 (dim_lt_omega K V)) :=
dif_pos (dim_lt_omega K V),
rw this,
exact (classical.some_spec (lt_omega.1 (dim_lt_omega K V))).symm
end
/-- If a vector space has a finite basis, then its dimension (seen as a cardinal) is equal to the
cardinality of the basis. -/
lemma dim_eq_card_basis {ι : Type w} [fintype ι] {b : ι → V} (h : is_basis K b) :
dim K V = fintype.card ι :=
by rw [←h.mk_range_eq_dim, cardinal.fintype_card,
set.card_range_of_injective h.injective]
/-- If a vector space has a finite basis, then its dimension is equal to the cardinality of the
basis. -/
lemma findim_eq_card_basis {ι : Type w} [fintype ι] {b : ι → V} (h : is_basis K b) :
findim K V = fintype.card ι :=
begin
haveI : finite_dimensional K V := of_finite_basis h,
have := dim_eq_card_basis h,
rw ← findim_eq_dim at this,
exact_mod_cast this
end
/-- If a vector space is finite-dimensional, then the cardinality of any basis is equal to its
`findim`. -/
lemma findim_eq_card_basis' [finite_dimensional K V] {ι : Type w} {b : ι → V} (h : is_basis K b) :
(findim K V : cardinal.{w}) = cardinal.mk ι :=
begin
rcases exists_is_basis_finite K V with ⟨s, s_basis, s_finite⟩,
letI: fintype s := s_finite.fintype,
have A : cardinal.mk s = fintype.card s := fintype_card _,
have B : findim K V = fintype.card s := findim_eq_card_basis s_basis,
have C : cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{v w} (cardinal.mk s) :=
mk_eq_mk_of_basis h s_basis,
rw [A, ← B, lift_nat_cast] at C,
have : cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{w v} (findim K V),
by { simp, exact C },
exact (lift_inj.mp this).symm
end
/-- If a vector space has a finite basis, then its dimension is equal to the cardinality of the
basis. This lemma uses a `finset` instead of indexed types. -/
lemma findim_eq_card_finset_basis {b : finset V}
(h : is_basis K (subtype.val : (↑b : set V) -> V)) :
findim K V = finset.card b :=
by { rw [findim_eq_card_basis h, fintype.subtype_card], intros x, refl }
lemma cardinal_mk_le_findim_of_linear_independent
[finite_dimensional K V] {ι : Type w} {b : ι → V} (h : linear_independent K b) :
cardinal.mk ι ≤ findim K V :=
begin
rw ← lift_le.{_ (max v w)},
simpa [← findim_eq_dim K V] using
cardinal_lift_le_dim_of_linear_independent.{_ _ _ (max v w)} h
end
lemma fintype_card_le_findim_of_linear_independent
[finite_dimensional K V] {ι : Type*} [fintype ι] {b : ι → V} (h : linear_independent K b) :
fintype.card ι ≤ findim K V :=
by simpa [fintype_card] using cardinal_mk_le_findim_of_linear_independent h
lemma finset_card_le_findim_of_linear_independent [finite_dimensional K V] {b : finset V}
(h : linear_independent K (λ x, x : (↑b : set V) → V)) :
b.card ≤ findim K V :=
begin
rw ←fintype.card_coe,
exact fintype_card_le_findim_of_linear_independent h,
end
lemma lt_omega_of_linear_independent {ι : Type w} [finite_dimensional K V]
{v : ι → V} (h : linear_independent K v) :
cardinal.mk ι < cardinal.omega :=
begin
apply cardinal.lift_lt.1,
apply lt_of_le_of_lt,
apply linear_independent_le_dim h,
rw [←findim_eq_dim, cardinal.lift_omega, cardinal.lift_nat_cast],
apply cardinal.nat_lt_omega,
end
lemma not_linear_independent_of_infinite {ι : Type w} [inf : infinite ι] [finite_dimensional K V]
(v : ι → V) : ¬ linear_independent K v :=
begin
intro h_lin_indep,
have : ¬ omega ≤ mk ι := not_le.mpr (lt_omega_of_linear_independent h_lin_indep),
have : omega ≤ mk ι := infinite_iff.mp inf,
contradiction
end
/-- A finite dimensional space has positive `findim` iff it has a nonzero element. -/
lemma findim_pos_iff_exists_ne_zero [finite_dimensional K V] : 0 < findim K V ↔ ∃ x : V, x ≠ 0 :=
iff.trans (by { rw ← findim_eq_dim, norm_cast }) (@dim_pos_iff_exists_ne_zero K V _ _ _)
/-- A finite dimensional space has positive `findim` iff it is nontrivial. -/
lemma findim_pos_iff [finite_dimensional K V] : 0 < findim K V ↔ nontrivial V :=
iff.trans (by { rw ← findim_eq_dim, norm_cast }) (@dim_pos_iff_nontrivial K V _ _ _)
/-- A nontrivial finite dimensional space has positive `findim`. -/
lemma findim_pos [finite_dimensional K V] [h : nontrivial V] : 0 < findim K V :=
findim_pos_iff.mpr h
section
open_locale big_operators
open finset
/--
If a finset has cardinality larger than the dimension of the space,
then there is a nontrivial linear relation amongst its elements.
-/
lemma exists_nontrivial_relation_of_dim_lt_card
[finite_dimensional K V] {t : finset V} (h : findim K V < t.card) :
∃ f : V → K, ∑ e in t, f e • e = 0 ∧ ∃ x ∈ t, f x ≠ 0 :=
begin
have := mt finset_card_le_findim_of_linear_independent (by { simpa using h }),
rw linear_dependent_iff at this,
obtain ⟨s, g, sum, z, zm, nonzero⟩ := this,
-- Now we have to extend `g` to all of `t`, then to all of `V`.
let f : V → K := λ x, if h : x ∈ t then if (⟨x, h⟩ : (↑t : set V)) ∈ s then g ⟨x, h⟩ else 0 else 0,
-- and finally clean up the mess caused by the extension.
refine ⟨f, _, _⟩,
{ dsimp [f],
rw ← sum,
fapply sum_bij_ne_zero (λ v hvt _, (⟨v, hvt⟩ : {v // v ∈ t})),
{ intros v hvt H, dsimp,
rw [dif_pos hvt] at H,
contrapose! H,
rw [if_neg H, zero_smul], },
{ intros _ _ _ _ _ _, exact subtype.mk.inj, },
{ intros b hbs hb,
use b,
simpa only [hbs, exists_prop, dif_pos, mk_coe, and_true, if_true, finset.coe_mem,
eq_self_iff_true, exists_prop_of_true, ne.def] using hb, },
{ intros a h₁, dsimp, rw [dif_pos h₁],
intro h₂, rw [if_pos], contrapose! h₂,
rw [if_neg h₂, zero_smul], }, },
{ refine ⟨z, z.2, _⟩, dsimp only [f], erw [dif_pos z.2, if_pos]; rwa [subtype.coe_eta] },
end
/--
If a finset has cardinality larger than `findim + 1`,
then there is a nontrivial linear relation amongst its elements,
such that the coefficients of the relation sum to zero.
-/
lemma exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card
[finite_dimensional K V] {t : finset V} (h : findim K V + 1 < t.card) :
∃ f : V → K, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, f x ≠ 0 :=
begin
-- Pick an element x₀ ∈ t,
have card_pos : 0 < t.card := lt_trans (nat.succ_pos _) h,
obtain ⟨x₀, m⟩ := (finset.card_pos.1 card_pos).bex,
-- and apply the previous lemma to the {xᵢ - x₀}
let shift : V ↪ V := ⟨λ x, x - x₀, add_left_injective (-x₀)⟩,
let t' := (t.erase x₀).map shift,
have h' : findim K V < t'.card,
{ simp only [t', card_map, finset.card_erase_of_mem m],
exact nat.lt_pred_iff.mpr h, },
-- to obtain a function `g`.
obtain ⟨g, gsum, x₁, x₁_mem, nz⟩ := exists_nontrivial_relation_of_dim_lt_card h',
-- Then obtain `f` by translating back by `x₀`,
-- and setting the value of `f` at `x₀` to ensure `∑ e in t, f e = 0`.
let f : V → K := λ z, if z = x₀ then - ∑ z in (t.erase x₀), g (z - x₀) else g (z - x₀),
refine ⟨f, _ ,_ ,_⟩,
-- After this, it's a matter of verifiying the properties,
-- based on the corresponding properties for `g`.
{ show ∑ (e : V) in t, f e • e = 0,
-- We prove this by splitting off the `x₀` term of the sum,
-- which is itself a sum over `t.erase x₀`,
-- combining the two sums, and
-- observing that after reindexing we have exactly
-- ∑ (x : V) in t', g x • x = 0.
simp only [f],
conv_lhs { apply_congr, skip, rw [ite_smul], },
rw [finset.sum_ite],
conv { congr, congr, apply_congr, simp [filter_eq', m], },
conv { congr, congr, skip, apply_congr, simp [filter_ne'], },
rw [sum_singleton, neg_smul, add_comm, ←sub_eq_add_neg, sum_smul, ←sum_sub_distrib],
simp only [←smul_sub],
-- At the end we have to reindex the sum, so we use `change` to
-- express the summand using `shift`.
change (∑ (x : V) in t.erase x₀, (λ e, g e • e) (shift x)) = 0,
rw ←sum_map _ shift,
exact gsum, },
{ show ∑ (e : V) in t, f e = 0,
-- Again we split off the `x₀` term,
-- observing that it exactly cancels the other terms.
rw [← insert_erase m, sum_insert (not_mem_erase x₀ t)],
dsimp [f],
rw [if_pos rfl],
conv_lhs { congr, skip, apply_congr, skip, rw if_neg (show x ≠ x₀, from (mem_erase.mp H).1), },
exact neg_add_self _, },
{ show ∃ (x : V) (H : x ∈ t), f x ≠ 0,
-- We can use x₁ + x₀.
refine ⟨x₁ + x₀, _, _⟩,
{ rw finset.mem_map at x₁_mem,
rcases x₁_mem with ⟨x₁, x₁_mem, rfl⟩,
rw mem_erase at x₁_mem,
simp only [x₁_mem, sub_add_cancel, function.embedding.coe_fn_mk], },
{ dsimp only [f],
rwa [if_neg, add_sub_cancel],
rw [add_left_eq_self], rintro rfl,
simpa only [sub_eq_zero, exists_prop, finset.mem_map, embedding.coe_fn_mk, eq_self_iff_true,
mem_erase, not_true, exists_eq_right, ne.def, false_and] using x₁_mem, } },
end
section
variables {L : Type*} [discrete_linear_ordered_field L]
variables {W : Type v} [add_comm_group W] [vector_space L W]
/--
A slight strengthening of `exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card`
available when working over an ordered field:
we can ensure a positive coefficient, not just a nonzero coefficient.
-/
lemma exists_relation_sum_zero_pos_coefficient_of_dim_succ_lt_card
[finite_dimensional L W] {t : finset W} (h : findim L W + 1 < t.card) :
∃ f : W → L, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, 0 < f x :=
begin
obtain ⟨f, sum, total, nonzero⟩ := exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card h,
exact ⟨f, sum, total, exists_pos_of_sum_zero_of_exists_nonzero f total nonzero⟩,
end
end
end
/-- If a submodule has maximal dimension in a finite dimensional space, then it is equal to the
whole space. -/
lemma eq_top_of_findim_eq [finite_dimensional K V] {S : submodule K V}
(h : findim K S = findim K V) : S = ⊤ :=
begin
cases exists_is_basis K S with bS hbS,
have : linear_independent K (subtype.val : (subtype.val '' bS : set V) → V),
from @linear_independent.image_subtype _ _ _ _ _ _ _ _ _
(submodule.subtype S) hbS.1 (by simp),
cases exists_subset_is_basis this with b hb,
letI : fintype b := classical.choice (finite_of_linear_independent hb.2.1),
letI : fintype (subtype.val '' bS) := classical.choice (finite_of_linear_independent this),
letI : fintype bS := classical.choice (finite_of_linear_independent hbS.1),
have : subtype.val '' bS = b, from set.eq_of_subset_of_card_le hb.1
(by rw [set.card_image_of_injective _ subtype.val_injective, ← findim_eq_card_basis hbS,
← findim_eq_card_basis hb.2, h]; apply_instance),
erw [← hb.2.2, subtype.range_coe, ← this, ← subtype_eq_val, span_image],
have := hbS.2,
erw [subtype.range_coe] at this,
rw [this, map_top (submodule.subtype S), range_subtype],
end
variable (K)
/-- A field is one-dimensional as a vector space over itself. -/
@[simp] lemma findim_of_field : findim K K = 1 :=
begin
have := dim_of_field K,
rw [← findim_eq_dim] at this,
exact_mod_cast this
end
/-- The vector space of functions on a fintype has finite dimension. -/
instance finite_dimensional_fintype_fun {ι : Type*} [fintype ι] :
finite_dimensional K (ι → K) :=
by { rw [finite_dimensional_iff_dim_lt_omega, dim_fun'], exact nat_lt_omega _ }
/-- The vector space of functions on a fintype ι has findim equal to the cardinality of ι. -/
@[simp] lemma findim_fintype_fun_eq_card {ι : Type v} [fintype ι] :
findim K (ι → K) = fintype.card ι :=
begin
have : vector_space.dim K (ι → K) = fintype.card ι := dim_fun',
rwa [← findim_eq_dim, nat_cast_inj] at this,
end
/-- The vector space of functions on `fin n` has findim equal to `n`. -/
@[simp] lemma findim_fin_fun {n : ℕ} : findim K (fin n → K) = n :=
by simp
/-- The submodule generated by a finite set is finite-dimensional. -/
theorem span_of_finite {A : set V} (hA : set.finite A) : finite_dimensional K (submodule.span K A) :=
is_noetherian_span_of_finite K hA
/-- The submodule generated by a single element is finite-dimensional. -/
instance (x : V) : finite_dimensional K (submodule.span K ({x} : set V)) := by {apply span_of_finite, simp}
end finite_dimensional
section zero_dim
open vector_space finite_dimensional
lemma finite_dimensional_of_dim_eq_zero (h : vector_space.dim K V = 0) : finite_dimensional K V :=
by rw [finite_dimensional_iff_dim_lt_omega, h]; exact cardinal.omega_pos
lemma findim_eq_zero_of_dim_eq_zero [finite_dimensional K V] (h : vector_space.dim K V = 0) :
findim K V = 0 :=
begin
convert findim_eq_dim K V,
rw h, norm_cast
end
variables (K V)
lemma finite_dimensional_bot : finite_dimensional K (⊥ : submodule K V) :=
finite_dimensional_of_dim_eq_zero $ by simp
@[simp] lemma findim_bot : findim K (⊥ : submodule K V) = 0 :=
begin
haveI := finite_dimensional_bot K V,
convert findim_eq_dim K (⊥ : submodule K V),
rw dim_bot, norm_cast
end
variables {K V}
lemma bot_eq_top_of_dim_eq_zero (h : vector_space.dim K V = 0) : (⊥ : submodule K V) = ⊤ :=
begin
haveI := finite_dimensional_of_dim_eq_zero h,
apply eq_top_of_findim_eq,
rw [findim_bot, findim_eq_zero_of_dim_eq_zero h]
end
@[simp] theorem dim_eq_zero {S : submodule K V} : dim K S = 0 ↔ S = ⊥ :=
⟨λ h, (submodule.eq_bot_iff _).2 $ λ x hx, congr_arg subtype.val $
((submodule.eq_bot_iff _).1 $ eq.symm $ bot_eq_top_of_dim_eq_zero h) ⟨x, hx⟩ submodule.mem_top,
λ h, by rw [h, dim_bot]⟩
@[simp] theorem findim_eq_zero {S : submodule K V} [finite_dimensional K S] : findim K S = 0 ↔ S = ⊥ :=
by rw [← dim_eq_zero, ← findim_eq_dim, ← @nat.cast_zero cardinal, cardinal.nat_cast_inj]
end zero_dim
namespace submodule
open finite_dimensional
/-- A submodule is finitely generated if and only if it is finite-dimensional -/
theorem fg_iff_finite_dimensional (s : submodule K V) :
s.fg ↔ finite_dimensional K s :=
⟨λh, is_noetherian_of_fg_of_noetherian s h,
λh, by { rw ← map_subtype_top s, exact fg_map (iff_fg.1 h) }⟩
/-- In a finite-dimensional vector space, the dimensions of a submodule and of the corresponding
quotient add up to the dimension of the space. -/
theorem findim_quotient_add_findim [finite_dimensional K V] (s : submodule K V) :
findim K s.quotient + findim K s = findim K V :=
begin
have := dim_quotient_add_dim s,
rw [← findim_eq_dim, ← findim_eq_dim, ← findim_eq_dim] at this,
exact_mod_cast this
end
/-- The dimension of a submodule is bounded by the dimension of the ambient space. -/
lemma findim_le [finite_dimensional K V] (s : submodule K V) : findim K s ≤ findim K V :=
by { rw ← s.findim_quotient_add_findim, exact nat.le_add_left _ _ }
/-- The dimension of a strict submodule is strictly bounded by the dimension of the ambient space. -/
lemma findim_lt [finite_dimensional K V] {s : submodule K V} (h : s < ⊤) :
findim K s < findim K V :=
begin
rw [← s.findim_quotient_add_findim, add_comm],
exact nat.lt_add_of_zero_lt_left _ _ (findim_pos_iff.mpr (quotient.nontrivial_of_lt_top _ h))
end
/-- The dimension of a quotient is bounded by the dimension of the ambient space. -/
lemma findim_quotient_le [finite_dimensional K V] (s : submodule K V) :
findim K s.quotient ≤ findim K V :=
by { rw ← s.findim_quotient_add_findim, exact nat.le_add_right _ _ }
/-- The sum of the dimensions of s + t and s ∩ t is the sum of the dimensions of s and t -/
theorem dim_sup_add_dim_inf_eq [finite_dimensional K V] (s t : submodule K V) :
findim K ↥(s ⊔ t) + findim K ↥(s ⊓ t) = findim K ↥s + findim K ↥t :=
begin
have key : dim K ↥(s ⊔ t) + dim K ↥(s ⊓ t) = dim K s + dim K t := dim_sup_add_dim_inf_eq s t,
repeat { rw ←findim_eq_dim at key },
norm_cast at key,
exact key
end
lemma eq_top_of_disjoint [finite_dimensional K V] (s t : submodule K V)
(hdim : findim K s + findim K t = findim K V)
(hdisjoint : disjoint s t) : s ⊔ t = ⊤ :=
begin
have h_findim_inf : findim K ↥(s ⊓ t) = 0,
{ rw [disjoint, le_bot_iff] at hdisjoint,
rw [hdisjoint, findim_bot] },
apply eq_top_of_findim_eq,
rw ←hdim,
convert s.dim_sup_add_dim_inf_eq t,
rw h_findim_inf,
refl,
end
end submodule
namespace linear_equiv
open finite_dimensional
/-- Finite dimensionality is preserved under linear equivalence. -/
protected theorem finite_dimensional (f : V ≃ₗ[K] V₂) [finite_dimensional K V] :
finite_dimensional K V₂ :=
is_noetherian_of_linear_equiv f
/-- The dimension of a finite dimensional space is preserved under linear equivalence. -/
theorem findim_eq (f : V ≃ₗ[K] V₂) [finite_dimensional K V] :
findim K V = findim K V₂ :=
begin
haveI : finite_dimensional K V₂ := f.finite_dimensional,
rcases exists_is_basis_finite K V with ⟨s, s_basis, s_finite⟩,
letI : fintype s := s_finite.fintype,
have A : findim K V = fintype.card s := findim_eq_card_basis s_basis,
have : is_basis K (λx:s, f (subtype.val x)) := f.is_basis s_basis,
have B : findim K V₂ = fintype.card s := findim_eq_card_basis this,
rw [A, B]
end
end linear_equiv
namespace finite_dimensional
/-- If a submodule is less than or equal to a finite-dimensional
submodule with the same dimension, they are equal. -/
lemma eq_of_le_of_findim_eq {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (hle : S₁ ≤ S₂)
(hd : findim K S₁ = findim K S₂) : S₁ = S₂ :=
begin
rw ←linear_equiv.findim_eq (submodule.comap_subtype_equiv_of_le hle) at hd,
exact le_antisymm hle (submodule.comap_subtype_eq_top.1 (eq_top_of_findim_eq hd))
end
end finite_dimensional
namespace linear_map
open finite_dimensional
/-- On a finite-dimensional space, an injective linear map is surjective. -/
lemma surjective_of_injective [finite_dimensional K V] {f : V →ₗ[K] V}
(hinj : injective f) : surjective f :=
begin
have h := dim_eq_of_injective _ hinj,
rw [← findim_eq_dim, ← findim_eq_dim, nat_cast_inj] at h,
exact range_eq_top.1 (eq_top_of_findim_eq h.symm)
end
/-- On a finite-dimensional space, a linear map is injective if and only if it is surjective. -/
lemma injective_iff_surjective [finite_dimensional K V] {f : V →ₗ[K] V} :
injective f ↔ surjective f :=
⟨surjective_of_injective,
λ hsurj, let ⟨g, hg⟩ := f.exists_right_inverse_of_surjective (range_eq_top.2 hsurj) in
have function.right_inverse g f, from linear_map.ext_iff.1 hg,
(left_inverse_of_surjective_of_right_inverse
(surjective_of_injective this.injective) this).injective⟩
lemma ker_eq_bot_iff_range_eq_top [finite_dimensional K V] {f : V →ₗ[K] V} :
f.ker = ⊥ ↔ f.range = ⊤ :=
by rw [range_eq_top, ker_eq_bot, injective_iff_surjective]
/-- In a finite-dimensional space, if linear maps are inverse to each other on one side then they
are also inverse to each other on the other side. -/
lemma mul_eq_one_of_mul_eq_one [finite_dimensional K V] {f g : V →ₗ[K] V} (hfg : f * g = 1) :
g * f = 1 :=
have ginj : injective g, from has_left_inverse.injective
⟨f, (λ x, show (f * g) x = (1 : V →ₗ[K] V) x, by rw hfg; refl)⟩,
let ⟨i, hi⟩ := g.exists_right_inverse_of_surjective
(range_eq_top.2 (injective_iff_surjective.1 ginj)) in
have f * (g * i) = f * 1, from congr_arg _ hi,
by rw [← mul_assoc, hfg, one_mul, mul_one] at this; rwa ← this
/-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if
they are inverse to each other on the other side. -/
lemma mul_eq_one_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f * g = 1 ↔ g * f = 1 :=
⟨mul_eq_one_of_mul_eq_one, mul_eq_one_of_mul_eq_one⟩
/-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if
they are inverse to each other on the other side. -/
lemma comp_eq_id_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f.comp g = id ↔ g.comp f = id :=
mul_eq_one_comm
/-- The image under an onto linear map of a finite-dimensional space is also finite-dimensional. -/
lemma finite_dimensional_of_surjective [h : finite_dimensional K V]
(f : V →ₗ[K] V₂) (hf : f.range = ⊤) : finite_dimensional K V₂ :=
is_noetherian_of_surjective V f hf
/-- The range of a linear map defined on a finite-dimensional space is also finite-dimensional. -/
instance finite_dimensional_range [h : finite_dimensional K V] (f : V →ₗ[K] V₂) :
finite_dimensional K f.range :=
f.quot_ker_equiv_range.finite_dimensional
/-- rank-nullity theorem : the dimensions of the kernel and the range of a linear map add up to
the dimension of the source space. -/
theorem findim_range_add_findim_ker [finite_dimensional K V] (f : V →ₗ[K] V₂) :
findim K f.range + findim K f.ker = findim K V :=
by { rw [← f.quot_ker_equiv_range.findim_eq], exact submodule.findim_quotient_add_findim _ }
end linear_map
namespace linear_equiv
open finite_dimensional
variables [finite_dimensional K V]
/-- The linear equivalence corresponging to an injective endomorphism. -/
noncomputable def of_injective_endo (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) : V ≃ₗ[K] V :=
(linear_equiv.of_injective f h_inj).trans (linear_equiv.of_top _ (linear_map.ker_eq_bot_iff_range_eq_top.1 h_inj))
lemma of_injective_endo_to_fun (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) :
(of_injective_endo f h_inj).to_fun = f := rfl
lemma of_injective_endo_right_inv (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) :
f * (of_injective_endo f h_inj).symm = 1 :=
begin
ext,
simp only [linear_map.one_app, linear_map.mul_app],
change f ((of_injective_endo f h_inj).symm x) = x,
rw ← linear_equiv.inv_fun_apply (of_injective_endo f h_inj),
apply (of_injective_endo f h_inj).right_inv,
end
lemma of_injective_endo_left_inv (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) :
((of_injective_endo f h_inj).symm : V →ₗ[K] V) * f = 1 :=
begin
ext,
simp only [linear_map.one_app, linear_map.mul_app],
change (of_injective_endo f h_inj).symm (f x) = x,
rw ← linear_equiv.inv_fun_apply (of_injective_endo f h_inj),
apply (of_injective_endo f h_inj).left_inv,
end
end linear_equiv
namespace linear_map
lemma is_unit_iff [finite_dimensional K V] (f : V →ₗ[K] V): is_unit f ↔ f.ker = ⊥ :=
begin
split,
{ intro h_is_unit,
rcases h_is_unit with ⟨u, hu⟩,
rw [←hu, linear_map.ker_eq_bot'],
intros x hx,
change (1 : V →ₗ[K] V) x = 0,
rw ← u.inv_val,
change u.inv (u x) = 0,
simp [hx] },
{ intro h_inj,
use ⟨f, (linear_equiv.of_injective_endo f h_inj).symm.to_linear_map,
linear_equiv.of_injective_endo_right_inv f h_inj, linear_equiv.of_injective_endo_left_inv f h_inj⟩,
refl }
end
end linear_map
open vector_space finite_dimensional
section top
@[simp]
theorem findim_top [finite_dimensional K V] : findim K (⊤ : submodule K V) = findim K V :=
linear_equiv.findim_eq $ linear_equiv.of_top ⊤ rfl
end top
namespace linear_map
theorem injective_iff_surjective_of_findim_eq_findim [finite_dimensional K V]
[finite_dimensional K V₂] (H : findim K V = findim K V₂) {f : V →ₗ[K] V₂} :
function.injective f ↔ function.surjective f :=
begin
have := findim_range_add_findim_ker f,
rw [← ker_eq_bot, ← range_eq_top], refine ⟨λ h, _, λ h, _⟩,
{ rw [h, findim_bot, add_zero, H] at this, exact eq_top_of_findim_eq this },
{ rw [h, findim_top, H] at this, exact findim_eq_zero.1 (add_right_injective _ this) }
end
theorem findim_le_findim_of_injective [finite_dimensional K V] [finite_dimensional K V₂]
{f : V →ₗ[K] V₂} (hf : function.injective f) : findim K V ≤ findim K V₂ :=
calc findim K V
= findim K f.range + findim K f.ker : (findim_range_add_findim_ker f).symm
... = findim K f.range : by rw [ker_eq_bot.2 hf, findim_bot, add_zero]
... ≤ findim K V₂ : submodule.findim_le _
end linear_map
section
/-- An integral domain that is module-finite as an algebra over a field is a field. -/
noncomputable def field_of_finite_dimensional (F K : Type*) [field F] [integral_domain K]
[algebra F K] [finite_dimensional F K] : field K :=
{ inv := λ x, if H : x = 0 then 0 else classical.some $
(show function.surjective (algebra.lmul_left F K x), from
linear_map.injective_iff_surjective.1 $ λ _ _, (mul_right_inj' H).1) 1,
mul_inv_cancel := λ x hx, show x * dite _ _ _ = _, by { rw dif_neg hx,
exact classical.some_spec ((show function.surjective (algebra.lmul_left F K x), from
linear_map.injective_iff_surjective.1 $ λ _ _, (mul_right_inj' hx).1) 1) },
inv_zero := dif_pos rfl,
.. ‹integral_domain K› }
end
namespace submodule
lemma findim_mono [finite_dimensional K V] :
monotone (λ (s : submodule K V), findim K s) :=
λ s t hst,
calc findim K s = findim K (comap t.subtype s)
: linear_equiv.findim_eq (comap_subtype_equiv_of_le hst).symm
... ≤ findim K t : submodule.findim_le _
lemma lt_of_le_of_findim_lt_findim {s t : submodule K V}
(le : s ≤ t) (lt : findim K s < findim K t) : s < t :=
lt_of_le_of_ne le (λ h, ne_of_lt lt (by rw h))
lemma lt_top_of_findim_lt_findim {s : submodule K V}
(lt : findim K s < findim K V) : s < ⊤ :=
begin
by_cases fin : (finite_dimensional K V),
{ haveI := fin,
rw ← @findim_top K V at lt,
exact lt_of_le_of_findim_lt_findim le_top lt },
{ exfalso,
have : findim K V = 0 := dif_neg (mt finite_dimensional_iff_dim_lt_omega.mpr fin),
rw this at lt,
exact nat.not_lt_zero _ lt }
end
end submodule
section span
open submodule
lemma findim_span_le_card (s : set V) [fin : fintype s] :
findim K (span K s) ≤ s.to_finset.card :=
begin
haveI := span_of_finite K ⟨fin⟩,
have : dim K (span K s) ≤ (mk s : cardinal) := dim_span_le s,
rw [←findim_eq_dim, cardinal.fintype_card, ←set.to_finset_card] at this,
exact_mod_cast this
end
lemma findim_span_eq_card {ι : Type*} [fintype ι] {b : ι → V}
(hb : linear_independent K b) :
findim K (span K (set.range b)) = fintype.card ι :=
begin
haveI : finite_dimensional K (span K (set.range b)) := span_of_finite K (set.finite_range b),
have : dim K (span K (set.range b)) = (mk (set.range b) : cardinal) := dim_span hb,
rwa [←findim_eq_dim, ←lift_inj, mk_range_eq_of_injective hb.injective,
cardinal.fintype_card, lift_nat_cast, lift_nat_cast, nat_cast_inj] at this,
end
lemma findim_span_set_eq_card (s : set V) [fin : fintype s]
(hs : linear_independent K (coe : s → V)) :
findim K (span K s) = s.to_finset.card :=
begin
haveI := span_of_finite K ⟨fin⟩,
have : dim K (span K s) = (mk s : cardinal) := dim_span_set hs,
rw [←findim_eq_dim, cardinal.fintype_card, ←set.to_finset_card] at this,
exact_mod_cast this
end
lemma span_lt_of_subset_of_card_lt_findim {s : set V} [fintype s] {t : submodule K V}
(subset : s ⊆ t) (card_lt : s.to_finset.card < findim K t) : span K s < t :=
lt_of_le_of_findim_lt_findim (span_le.mpr subset) (lt_of_le_of_lt (findim_span_le_card _) card_lt)
lemma span_lt_top_of_card_lt_findim {s : set V} [fintype s]
(card_lt : s.to_finset.card < findim K V) : span K s < ⊤ :=
lt_top_of_findim_lt_findim (lt_of_le_of_lt (findim_span_le_card _) card_lt)
end span
section is_basis
lemma linear_independent_of_span_eq_top_of_card_eq_findim {ι : Type*} [fintype ι] {b : ι → V}
(span_eq : span K (set.range b) = ⊤) (card_eq : fintype.card ι = findim K V) :
linear_independent K b :=
linear_independent_iff'.mpr $ λ s g dependent i i_mem_s,
begin
by_contra gx_ne_zero,
-- We'll derive a contradiction by showing `b '' (univ \ {i})` of cardinality `n - 1`
-- spans a vector space of dimension `n`.
refine ne_of_lt (span_lt_top_of_card_lt_findim
(show (b '' (set.univ \ {i})).to_finset.card < findim K V, from _)) _,
{ calc (b '' (set.univ \ {i})).to_finset.card = ((set.univ \ {i}).to_finset.image b).card
: by rw [set.to_finset_card, fintype.card_of_finset]
... ≤ (set.univ \ {i}).to_finset.card : finset.card_image_le
... = (finset.univ.erase i).card : congr_arg finset.card (finset.ext (by simp [and_comm]))
... < finset.univ.card : finset.card_erase_lt_of_mem (finset.mem_univ i)
... = findim K V : card_eq },
-- We already have that `b '' univ` spans the whole space,
-- so we only need to show that the span of `b '' (univ \ {i})` contains each `b j`.
refine trans (le_antisymm (span_mono (set.image_subset_range _ _)) (span_le.mpr _)) span_eq,
rintros _ ⟨j, rfl, rfl⟩,
-- The case that `j ≠ i` is easy because `b j ∈ b '' (univ \ {i})`.
by_cases j_eq : j = i,
swap,
{ refine subset_span ⟨j, (set.mem_diff _).mpr ⟨set.mem_univ _, _⟩, rfl⟩,
exact mt set.mem_singleton_iff.mp j_eq },
-- To show `b i ∈ span (b '' (univ \ {i}))`, we use that it's a weighted sum
-- of the other `b j`s.
rw [j_eq, mem_coe, show b i = -((g i)⁻¹ • (s.erase i).sum (λ j, g j • b j)), from _],
{ refine submodule.neg_mem _ (smul_mem _ _ (sum_mem _ (λ k hk, _))),
obtain ⟨k_ne_i, k_mem⟩ := finset.mem_erase.mp hk,
refine smul_mem _ _ (subset_span ⟨k, _, rfl⟩),
simpa using k_mem },
-- To show `b i` is a weighted sum of the other `b j`s, we'll rewrite this sum
-- to have the form of the assumption `dependent`.
apply eq_neg_of_add_eq_zero,
calc b i + (g i)⁻¹ • (s.erase i).sum (λ j, g j • b j)
= (g i)⁻¹ • (g i • b i + (s.erase i).sum (λ j, g j • b j))
: by rw [smul_add, ←mul_smul, inv_mul_cancel gx_ne_zero, one_smul]
... = (g i)⁻¹ • 0 : congr_arg _ _
... = 0 : smul_zero _,
-- And then it's just a bit of manipulation with finite sums.
rwa [← finset.insert_erase i_mem_s, finset.sum_insert (finset.not_mem_erase _ _)] at dependent
end
lemma is_basis_of_span_eq_top_of_card_eq_findim {ι : Type*} [fintype ι] {b : ι → V}
(span_eq : span K (set.range b) = ⊤) (card_eq : fintype.card ι = findim K V) :
is_basis K b :=
⟨linear_independent_of_span_eq_top_of_card_eq_findim span_eq card_eq, span_eq⟩
lemma finset_is_basis_of_span_eq_top_of_card_eq_findim {s : finset V}
(span_eq : span K (↑s : set V) = ⊤) (card_eq : s.card = findim K V) :
is_basis K (coe : (↑s : set V) → V) :=
is_basis_of_span_eq_top_of_card_eq_findim
((@subtype.range_coe_subtype _ (λ x, x ∈ s)).symm ▸ span_eq)
(trans (fintype.card_coe _) card_eq)
lemma set_is_basis_of_span_eq_top_of_card_eq_findim {s : set V} [fintype s]
(span_eq : span K s = ⊤) (card_eq : s.to_finset.card = findim K V) :
is_basis K (λ (x : s), (x : V)) :=
is_basis_of_span_eq_top_of_card_eq_findim
((@subtype.range_coe_subtype _ s).symm ▸ span_eq)
(trans s.to_finset_card.symm card_eq)
lemma span_eq_top_of_linear_independent_of_card_eq_findim
{ι : Type*} [hι : nonempty ι] [fintype ι] {b : ι → V}
(lin_ind : linear_independent K b) (card_eq : fintype.card ι = findim K V) :
span K (set.range b) = ⊤ :=
begin
by_cases fin : (finite_dimensional K V),
{ haveI := fin,
by_contra ne_top,
have lt_top : span K (set.range b) < ⊤ := lt_of_le_of_ne le_top ne_top,
exact ne_of_lt (submodule.findim_lt lt_top) (trans (findim_span_eq_card lin_ind) card_eq) },
{ exfalso,
apply ne_of_lt (fintype.card_pos_iff.mpr hι),
symmetry,
calc fintype.card ι = findim K V : card_eq
... = 0 : dif_neg (mt finite_dimensional_iff_dim_lt_omega.mpr fin) }
end
lemma is_basis_of_linear_independent_of_card_eq_findim
{ι : Type*} [nonempty ι] [fintype ι] {b : ι → V}
(lin_ind : linear_independent K b) (card_eq : fintype.card ι = findim K V) :
is_basis K b :=
⟨lin_ind, span_eq_top_of_linear_independent_of_card_eq_findim lin_ind card_eq⟩
lemma finset_is_basis_of_linear_independent_of_card_eq_findim
{s : finset V} (hs : s.nonempty)
(lin_ind : linear_independent K (coe : (↑s : set V) → V)) (card_eq : s.card = findim K V) :
is_basis K (coe : (↑s : set V) → V) :=
@is_basis_of_linear_independent_of_card_eq_findim _ _ _ _ _ _
⟨(⟨hs.some, hs.some_spec⟩ : (↑s : set V))⟩ _ _
lin_ind
(trans (fintype.card_coe _) card_eq)
lemma set_is_basis_of_linear_independent_of_card_eq_findim
{s : set V} [nonempty s] [fintype s]
(lin_ind : linear_independent K (coe : s → V)) (card_eq : s.to_finset.card = findim K V) :
is_basis K (coe : s → V) :=
is_basis_of_linear_independent_of_card_eq_findim lin_ind (trans s.to_finset_card.symm card_eq)
end is_basis
|
ef3b42a84c3abe853eb458b30ab6722998c2e0ff | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/tactic/converter/old_conv.lean | c0b9db07729becf7e2307a6a237a8d83501ebd61 | [] | 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 | 340 | 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
Converter monad for building simplifiers.
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.control.basic
import Mathlib.PostPort
namespace Mathlib
|
d78836b0a1b450433af469d0e4552d6e8b48b3d2 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/tactic13.lean | d77e4981236114e6e45e739f97cc36b8a0343994 | [
"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 | 244 | lean | import logic
open tactic
theorem tst (a b : Prop) (H : ¬ a ∨ ¬ b) (Hb : b) : ¬ a ∧ b :=
begin
apply and.intro,
assume Ha, or.elim H
(assume Hna, @absurd _ false Ha Hna)
(assume Hnb, @absurd _ false Hb Hnb),
assumption
end
|
241c77c6e0cf771080c32ee842ae639282734fbf | 31f556cdeb9239ffc2fad8f905e33987ff4feab9 | /src/Lean/Compiler/LCNF/Basic.lean | 589b5fc61268b21181cec18e5409d90ec7d1f56f | [
"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 | tobiasgrosser/lean4 | ce0fd9cca0feba1100656679bf41f0bffdbabb71 | ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f | refs/heads/master | 1,673,103,412,948 | 1,664,930,501,000 | 1,664,930,501,000 | 186,870,185 | 0 | 0 | Apache-2.0 | 1,665,129,237,000 | 1,557,939,901,000 | Lean | UTF-8 | Lean | false | false | 19,034 | lean | /-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Expr
namespace Lean.Compiler.LCNF
/-!
# Lean Compiler Normal Form (LCNF)
It is based on the [A-normal form](https://en.wikipedia.org/wiki/A-normal_form),
and the approach described in the paper
[Compiling without continuations](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/11/compiling-without-continuations.pdf).
-/
structure Param where
fvarId : FVarId
binderName : Name
type : Expr
borrow : Bool
deriving Inhabited, BEq
def Param.toExpr (p : Param) : Expr :=
.fvar p.fvarId
inductive AltCore (Code : Type) where
| alt (ctorName : Name) (params : Array Param) (code : Code)
| default (code : Code)
deriving Inhabited
structure LetDecl where
fvarId : FVarId
binderName : Name
type : Expr
value : Expr
deriving Inhabited, BEq
structure FunDeclCore (Code : Type) where
fvarId : FVarId
binderName : Name
params : Array Param
type : Expr
value : Code
deriving Inhabited
def FunDeclCore.getArity (decl : FunDeclCore Code) : Nat :=
decl.params.size
structure CasesCore (Code : Type) where
typeName : Name
resultType : Expr
discr : FVarId
alts : Array (AltCore Code)
deriving Inhabited
inductive Code where
| let (decl : LetDecl) (k : Code)
| fun (decl : FunDeclCore Code) (k : Code)
| jp (decl : FunDeclCore Code) (k : Code)
| jmp (fvarId : FVarId) (args : Array Expr)
| cases (cases : CasesCore Code)
| return (fvarId : FVarId)
| unreach (type : Expr)
deriving Inhabited
abbrev Alt := AltCore Code
abbrev FunDecl := FunDeclCore Code
abbrev Cases := CasesCore Code
/--
Return the constructor names that have an explicit (non-default) alternative.
-/
def CasesCore.getCtorNames (c : Cases) : NameSet :=
c.alts.foldl (init := {}) fun ctorNames alt =>
match alt with
| .default _ => ctorNames
| .alt ctorName .. => ctorNames.insert ctorName
inductive CodeDecl where
| let (decl : LetDecl)
| fun (decl : FunDecl)
| jp (decl : FunDecl)
deriving Inhabited
def CodeDecl.fvarId : CodeDecl → FVarId
| .let decl | .fun decl | .jp decl => decl.fvarId
def attachCodeDecls (decls : Array CodeDecl) (code : Code) : Code :=
go decls.size code
where
go (i : Nat) (code : Code) : Code :=
if i > 0 then
match decls[i-1]! with
| .let decl => go (i-1) (.let decl code)
| .fun decl => go (i-1) (.fun decl code)
| .jp decl => go (i-1) (.jp decl code)
else
code
mutual
private unsafe def eqImp (c₁ c₂ : Code) : Bool :=
if ptrEq c₁ c₂ then
true
else match c₁, c₂ with
| .let d₁ k₁, .let d₂ k₂ => d₁ == d₂ && eqImp k₁ k₂
| .fun d₁ k₁, .fun d₂ k₂
| .jp d₁ k₁, .jp d₂ k₂ => eqFunDecl d₁ d₂ && eqImp k₁ k₂
| .cases c₁, .cases c₂ => eqCases c₁ c₂
| .jmp j₁ as₁, .jmp j₂ as₂ => j₁ == j₂ && as₁ == as₂
| .return r₁, .return r₂ => r₁ == r₂
| .unreach t₁, .unreach t₂ => t₁ == t₂
| _, _ => false
private unsafe def eqFunDecl (d₁ d₂ : FunDecl) : Bool :=
if ptrEq d₁ d₂ then
true
else
d₁.fvarId == d₂.fvarId && d₁.binderName == d₂.binderName &&
d₁.params == d₂.params && d₁.type == d₂.type &&
eqImp d₁.value d₂.value
private unsafe def eqCases (c₁ c₂ : Cases) : Bool :=
c₁.resultType == c₂.resultType && c₁.discr == c₂.discr &&
c₁.typeName == c₂.typeName && c₁.alts.isEqv c₂.alts eqAlt
private unsafe def eqAlt (a₁ a₂ : Alt) : Bool :=
match a₁, a₂ with
| .default k₁, .default k₂ => eqImp k₁ k₂
| .alt c₁ ps₁ k₁, .alt c₂ ps₂ k₂ => c₁ == c₂ && ps₁ == ps₂ && eqImp k₁ k₂
| _, _ => false
end
@[implementedBy eqImp] protected opaque Code.beq : Code → Code → Bool
instance : BEq Code where
beq := Code.beq
@[implementedBy eqFunDecl] protected opaque FunDecl.beq : FunDecl → FunDecl → Bool
instance : BEq FunDecl where
beq := FunDecl.beq
def AltCore.getCode : Alt → Code
| .default k => k
| .alt _ _ k => k
def AltCore.getParams : Alt → Array Param
| .default _ => #[]
| .alt _ ps _ => ps
def AltCore.forCodeM [Monad m] (alt : Alt) (f : Code → m Unit) : m Unit := do
match alt with
| .default k => f k
| .alt _ _ k => f k
private unsafe def updateAltCodeImp (alt : Alt) (k' : Code) : Alt :=
match alt with
| .default k => if ptrEq k k' then alt else .default k'
| .alt ctorName ps k => if ptrEq k k' then alt else .alt ctorName ps k'
@[implementedBy updateAltCodeImp] opaque AltCore.updateCode (alt : Alt) (c : Code) : Alt
private unsafe def updateAltImp (alt : Alt) (ps' : Array Param) (k' : Code) : Alt :=
match alt with
| .alt ctorName ps k => if ptrEq k k' && ptrEq ps ps' then alt else .alt ctorName ps' k'
| _ => unreachable!
@[implementedBy updateAltImp] opaque AltCore.updateAlt! (alt : Alt) (ps' : Array Param) (k' : Code) : Alt
@[inline] private unsafe def updateAltsImp (c : Code) (alts : Array Alt) : Code :=
match c with
| .cases cs => if ptrEq cs.alts alts then c else .cases { cs with alts }
| _ => unreachable!
@[implementedBy updateAltsImp] opaque Code.updateAlts! (c : Code) (alts : Array Alt) : Code
@[inline] private unsafe def updateCasesImp (c : Code) (resultType : Expr) (discr : FVarId) (alts : Array Alt) : Code :=
match c with
| .cases cs => if ptrEq cs.alts alts && ptrEq cs.resultType resultType && cs.discr == discr then c else .cases { cs with discr, resultType, alts }
| _ => unreachable!
@[implementedBy updateCasesImp] opaque Code.updateCases! (c : Code) (resultType : Expr) (discr : FVarId) (alts : Array Alt) : Code
@[inline] private unsafe def updateLetImp (c : Code) (decl' : LetDecl) (k' : Code) : Code :=
match c with
| .let decl k => if ptrEq k k' && ptrEq decl decl' then c else .let decl' k'
| _ => unreachable!
@[implementedBy updateLetImp] opaque Code.updateLet! (c : Code) (decl' : LetDecl) (k' : Code) : Code
@[inline] private unsafe def updateContImp (c : Code) (k' : Code) : Code :=
match c with
| .let decl k => if ptrEq k k' then c else .let decl k'
| .fun decl k => if ptrEq k k' then c else .fun decl k'
| .jp decl k => if ptrEq k k' then c else .jp decl k'
| _ => unreachable!
@[implementedBy updateContImp] opaque Code.updateCont! (c : Code) (k' : Code) : Code
@[inline] private unsafe def updateFunImp (c : Code) (decl' : FunDecl) (k' : Code) : Code :=
match c with
| .fun decl k => if ptrEq k k' && ptrEq decl decl' then c else .fun decl' k'
| .jp decl k => if ptrEq k k' && ptrEq decl decl' then c else .jp decl' k'
| _ => unreachable!
@[implementedBy updateFunImp] opaque Code.updateFun! (c : Code) (decl' : FunDecl) (k' : Code) : Code
@[inline] private unsafe def updateReturnImp (c : Code) (fvarId' : FVarId) : Code :=
match c with
| .return fvarId => if fvarId == fvarId' then c else .return fvarId'
| _ => unreachable!
@[implementedBy updateReturnImp] opaque Code.updateReturn! (c : Code) (fvarId' : FVarId) : Code
@[inline] private unsafe def updateJmpImp (c : Code) (fvarId' : FVarId) (args' : Array Expr) : Code :=
match c with
| .jmp fvarId args => if fvarId == fvarId' && ptrEq args args' then c else .jmp fvarId' args'
| _ => unreachable!
@[implementedBy updateJmpImp] opaque Code.updateJmp! (c : Code) (fvarId' : FVarId) (args' : Array Expr) : Code
@[inline] private unsafe def updateUnreachImp (c : Code) (type' : Expr) : Code :=
match c with
| .unreach type => if ptrEq type type' then c else .unreach type'
| _ => unreachable!
@[implementedBy updateUnreachImp] opaque Code.updateUnreach! (c : Code) (type' : Expr) : Code
private unsafe def updateParamCoreImp (p : Param) (type : Expr) : Param :=
if ptrEq type p.type then
p
else
{ p with type }
/--
Low-level update `Param` function. It does not update the local context.
Consider using `Param.update : Param → Expr → CompilerM Param` if you want the local context
to be updated.
-/
@[implementedBy updateParamCoreImp] opaque Param.updateCore (p : Param) (type : Expr) : Param
private unsafe def updateLetDeclCoreImp (decl : LetDecl) (type : Expr) (value : Expr) : LetDecl :=
if ptrEq type decl.type && ptrEq value decl.value then
decl
else
{ decl with type, value }
/--
Low-level update `LetDecl` function. It does not update the local context.
Consider using `LetDecl.update : LetDecl → Expr → Expr → CompilerM LetDecl` if you want the local context
to be updated.
-/
@[implementedBy updateLetDeclCoreImp] opaque LetDecl.updateCore (decl : LetDecl) (type : Expr) (value : Expr) : LetDecl
private unsafe def updateFunDeclCoreImp (decl: FunDecl) (type : Expr) (params : Array Param) (value : Code) : FunDecl :=
if ptrEq type decl.type && ptrEq params decl.params && ptrEq value decl.value then
decl
else
{ decl with type, params, value }
/--
Low-level update `FunDecl` function. It does not update the local context.
Consider using `FunDecl.update : LetDecl → Expr → Array Param → Code → CompilerM FunDecl` if you want the local context
to be updated.
-/
@[implementedBy updateFunDeclCoreImp] opaque FunDeclCore.updateCore (decl: FunDecl) (type : Expr) (params : Array Param) (value : Code) : FunDecl
def CasesCore.extractAlt! (cases : Cases) (ctorName : Name) : Alt × Cases :=
let found (i : Nat) := (cases.alts[i]!, { cases with alts := cases.alts.eraseIdx i })
if let some i := cases.alts.findIdx? fun | .alt ctorName' .. => ctorName == ctorName' | _ => false then
found i
else if let some i := cases.alts.findIdx? fun | .default _ => true | _ => false then
found i
else
unreachable!
def AltCore.mapCodeM [Monad m] (alt : Alt) (f : Code → m Code) : m Alt := do
return alt.updateCode (← f alt.getCode)
def Code.isDecl : Code → Bool
| .let .. | .fun .. | .jp .. => true
| _ => false
def Code.isFun : Code → Bool
| .fun .. => true
| _ => false
def Code.isReturnOf : Code → FVarId → Bool
| .return fvarId, fvarId' => fvarId == fvarId'
| _, _ => false
partial def Code.size (c : Code) : Nat :=
go c 0
where
go (c : Code) (n : Nat) : Nat :=
match c with
| .let _ k => go k (n+1)
| .jp decl k | .fun decl k => go k <| go decl.value n
| .cases c => c.alts.foldl (init := n+1) fun n alt => go alt.getCode (n+1)
| .jmp .. => n+1
| .return .. | unreach .. => n -- `return` & `unreach` have weight zero
/-- Return true iff `c.size ≤ n` -/
partial def Code.sizeLe (c : Code) (n : Nat) : Bool :=
match go c |>.run 0 with
| .ok .. => true
| .error .. => false
where
inc : EStateM Unit Nat Unit := do
modify (·+1)
unless (← get) <= n do throw ()
go (c : Code) : EStateM Unit Nat Unit := do
match c with
| .let _ k => inc; go k
| .jp decl k | .fun decl k => inc; go decl.value; go k
| .cases c => inc; c.alts.forM fun alt => go alt.getCode
| .jmp .. => inc
| .return .. | unreach .. => return ()
partial def Code.forM [Monad m] (c : Code) (f : Code → m Unit) : m Unit :=
go c
where
go (c : Code) : m Unit := do
f c
match c with
| .let _ k => go k
| .fun decl k | .jp decl k => go decl.value; go k
| .cases c => c.alts.forM fun alt => go alt.getCode
| .unreach .. | .return .. | .jmp .. => return ()
/--
Declaration being processed by the Lean to Lean compiler passes.
-/
structure Decl where
/--
The name of the declaration from the `Environment` it came from
-/
name : Name
/--
Universe level parameter names.
-/
levelParams : List Name
/--
The type of the declaration. Note that this is an erased LCNF type
instead of the fully dependent one that might have been the original
type of the declaration in the `Environment`.
-/
type : Expr
/--
Parameters.
-/
params : Array Param
/--
The body of the declaration, usually changes as it progresses
through compiler passes.
-/
value : Code
/--
We set this flag to true during LCNF conversion. When we receive
a block of functions to be compiled, we set this flag to `true`
if there is an application to the function in the block containing
it. This is an approximation, but it should be good enough because
in the frontend, we invoke the compiler with blocks of strongly connected
components only.
We use this information to control inlining.
-/
recursive : Bool := false
/--
We set this flag to false during LCNF conversion if the Lean function
associated with this function was tagged as partial or unsafe. This
information affects how static analyzers treat function applications
of this kind. See `DefinitionSafety`.
`partial` and `unsafe` functions may not be terminating, but Lean
functions terminate, and some static analyzers exploit this
fact. So, we use the following semantics. Suppose whe hav a (large) natural
number `C`. We consider a nondeterministic model for computation of Lean expressions as
follows:
Each call to a partial/unsafe function uses up one "recursion token".
Prior to consuming `C` recursion tokens all partial functions must be called
as normal. Once the model has used up `C` recursion tokens, a subsequent call to
a partial function has the following nondeterministic options: it can either call
the function again, or return any value of the target type (even a noncomputable one).
Larger values of `C` yield less nondeterminism in the model, but even the intersection of
all choices of `C` yields nondeterminism where `def loop : A := loop` returns any value of type `A`.
The compiler fixes a choice for `C`. This is a fixed constant greater than 2^2^64,
which is allowed to be compiler and architecture dependent, and promises that it will
produce an execution consistent with every possible nondeterministic outcome of the `C`-model.
In the event that different nondeterministic executions disagree, the compiler is required to
exhaust resources or output a looping computation.
-/
safe : Bool := true
deriving Inhabited, BEq
def Decl.size (decl : Decl) : Nat :=
decl.value.size
def Decl.getArity (decl : Decl) : Nat :=
decl.params.size
/--
Return `some i` if `decl` is of the form
```
def f (a_0 ... a_i ...) :=
...
cases a_i
| ...
| ...
```
That is, `f` is a sequence of declarations followed by a `cases` on the parameter `i`.
We use this function to decide whether we should inline a declaration tagged with
`[inlineIfReduce]` or not.
-/
def Decl.isCasesOnParam? (decl : Decl) : Option Nat :=
go decl.value
where
go (code : Code) : Option Nat :=
match code with
| .let _ k | .jp _ k | .fun _ k => go k
| .cases c => decl.params.findIdx? fun param => param.fvarId == c.discr
| _ => none
def Decl.instantiateTypeLevelParams (decl : Decl) (us : List Level) : Expr :=
decl.type.instantiateLevelParams decl.levelParams us
def Decl.instantiateParamsLevelParams (decl : Decl) (us : List Level) : Array Param :=
decl.params.mapMono fun param => param.updateCore (param.type.instantiateLevelParams decl.levelParams us)
partial def Decl.instantiateValueLevelParams (decl : Decl) (us : List Level) : Code :=
instCode decl.value
where
instExpr (e : Expr) :=
e.instantiateLevelParams decl.levelParams us
instParams (ps : Array Param) :=
ps.mapMono fun p => p.updateCore (instExpr p.type)
instAlt (alt : Alt) :=
match alt with
| .default k => alt.updateCode (instCode k)
| .alt _ ps k => alt.updateAlt! (instParams ps) (instCode k)
instLetDecl (decl : LetDecl) :=
decl.updateCore (instExpr decl.type) (instExpr decl.value)
instFunDecl (decl : FunDecl) :=
decl.updateCore (instExpr decl.type) (instParams decl.params) (instCode decl.value)
instCode (code : Code) :=
match code with
| .let decl k => code.updateLet! (instLetDecl decl) (instCode k)
| .jp decl k | .fun decl k => code.updateFun! (instFunDecl decl) (instCode k)
| .cases c => code.updateCases! (instExpr c.resultType) c.discr (c.alts.mapMono instAlt)
| .jmp fvarId args => code.updateJmp! fvarId (args.mapMono instExpr)
| .return .. => code
| .unreach type => code.updateUnreach! (instExpr type)
mutual
partial def FunDeclCore.collectUsed (decl : FunDecl) (s : FVarIdSet := {}) : FVarIdSet :=
decl.value.collectUsed <| collectParams decl.params <| collectExpr decl.type s
private partial def collectParams (ps : Array Param) (s : FVarIdSet) : FVarIdSet :=
ps.foldl (init := s) fun s p => collectExpr p.type s
private partial def collectExprs (es : Array Expr) (s : FVarIdSet) : FVarIdSet :=
es.foldl (init := s) fun s e => collectExpr e s
private partial def collectExpr (e : Expr) : FVarIdSet → FVarIdSet :=
match e with
| .proj _ _ e => collectExpr e
| .forallE _ d b _ => collectExpr b ∘ collectExpr d
| .lam _ d b _ => collectExpr b ∘ collectExpr d
| .letE .. => unreachable!
| .app f a => collectExpr f ∘ collectExpr a
| .mdata _ b => collectExpr b
| .fvar fvarId => fun s => s.insert fvarId
| _ => id
partial def Code.collectUsed (code : Code) (s : FVarIdSet := {}) : FVarIdSet :=
match code with
| .let decl k => k.collectUsed <| collectExpr decl.value <| collectExpr decl.type s
| .jp decl k | .fun decl k => k.collectUsed <| decl.collectUsed s
| .cases c =>
let s := s.insert c.discr
let s := collectExpr c.resultType s
c.alts.foldl (init := s) fun s alt =>
match alt with
| .default k => k.collectUsed s
| .alt _ ps k => k.collectUsed <| collectParams ps s
| .return fvarId => s.insert fvarId
| .unreach type => collectExpr type s
| .jmp fvarId args => collectExprs args <| s.insert fvarId
end
abbrev collectUsedAtExpr (s : FVarIdSet) (e : Expr) : FVarIdSet :=
collectExpr e s
/--
Traverse the given block of potentially mutually recursive functions
and mark a declaration `f` as recursive if there is an application
`f ...` in the block.
This is an overapproximation, and relies on the fact that our frontend
computes strongly connected components.
See comment at `recursive` field.
-/
partial def markRecDecls (decls : Array Decl) : Array Decl :=
let (_, isRec) := go |>.run {}
decls.map fun decl =>
if isRec.contains decl.name then
{ decl with recursive := true }
else
decl
where
visit (code : Code) : StateM NameSet Unit := do
match code with
| .jp decl k | .fun decl k => visit decl.value; visit k
| .cases c => c.alts.forM fun alt => visit alt.getCode
| .unreach .. | .jmp .. | .return .. => return ()
| .let decl k =>
if let .const declName _ := decl.value.getAppFn then
if decls.any (·.name == declName) then
modify fun s => s.insert declName
visit k
go : StateM NameSet Unit :=
decls.forM fun decl => visit decl.value
end Lean.Compiler.LCNF
|
832075b272b71771781f30eff77ec61bf23ba7e0 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Compiler/IR.lean | 9fa99393158b03b7041fd348e15f88a71b0d8584 | [
"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,946 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Compiler.IR.Basic
import Lean.Compiler.IR.Format
import Lean.Compiler.IR.CompilerM
import Lean.Compiler.IR.PushProj
import Lean.Compiler.IR.ElimDeadVars
import Lean.Compiler.IR.SimpCase
import Lean.Compiler.IR.ResetReuse
import Lean.Compiler.IR.NormIds
import Lean.Compiler.IR.Checker
import Lean.Compiler.IR.Borrow
import Lean.Compiler.IR.Boxing
import Lean.Compiler.IR.RC
import Lean.Compiler.IR.ExpandResetReuse
import Lean.Compiler.IR.UnboxResult
import Lean.Compiler.IR.ElimDeadBranches
import Lean.Compiler.IR.EmitC
import Lean.Compiler.IR.CtorLayout
import Lean.Compiler.IR.Sorry
namespace Lean.IR
register_builtin_option compiler.reuse : Bool := {
defValue := true
descr := "heuristically insert reset/reuse instruction pairs"
}
private def compileAux (decls : Array Decl) : CompilerM Unit := do
logDecls `init decls
checkDecls decls
let mut decls ← elimDeadBranches decls
logDecls `elim_dead_branches decls
decls := decls.map Decl.pushProj
logDecls `push_proj decls
if compiler.reuse.get (← read) then
decls := decls.map Decl.insertResetReuse
logDecls `reset_reuse decls
decls := decls.map Decl.elimDead
logDecls `elim_dead decls
decls := decls.map Decl.simpCase
logDecls `simp_case decls
decls := decls.map Decl.normalizeIds
decls ← inferBorrow decls
logDecls `borrow decls
decls ← explicitBoxing decls
logDecls `boxing decls
decls ← explicitRC decls
logDecls `rc decls
if compiler.reuse.get (← read) then
decls := decls.map Decl.expandResetReuse
logDecls `expand_reset_reuse decls
decls := decls.map Decl.pushProj
logDecls `push_proj decls
decls ← updateSorryDep decls
logDecls `result decls
checkDecls decls
addDecls decls
@[export lean_ir_compile]
def compile (env : Environment) (opts : Options) (decls : Array Decl) : Log × (Except String Environment) :=
match (compileAux decls opts).run { env := env } with
| EStateM.Result.ok _ s => (s.log, Except.ok s.env)
| EStateM.Result.error msg s => (s.log, Except.error msg)
def addBoxedVersionAux (decl : Decl) : CompilerM Unit := do
let env ← getEnv
if !ExplicitBoxing.requiresBoxedVersion env decl then
pure ()
else
let decl := ExplicitBoxing.mkBoxedVersion decl
let decls : Array Decl := #[decl]
let decls ← explicitRC decls
decls.forM fun decl => modifyEnv fun env => addDeclAux env decl
pure ()
-- Remark: we are ignoring the `Log` here. This should be fine.
@[export lean_ir_add_boxed_version]
def addBoxedVersion (env : Environment) (decl : Decl) : Except String Environment :=
match (addBoxedVersionAux decl Options.empty).run { env := env } with
| EStateM.Result.ok _ s => Except.ok s.env
| EStateM.Result.error msg _ => Except.error msg
end Lean.IR
|
21adcc3e0332dd7881f8c3307bc8d0e242c0f5c0 | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/data/set/intervals/surj_on.lean | df51af5570fd15db2f9dbc442ad9528b3e4d0600 | [
"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 | 3,937 | lean | /-
Copyright (c) 2020 Heather Macbeth. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Heather Macbeth
-/
import data.set.intervals.basic
import data.set.function
/-!
# Monotone surjective functions are surjective on intervals
A monotone surjective function sends any interval in the domain onto the interval with corresponding
endpoints in the range. This is expressed in this file using `set.surj_on`, and provided for all
permutations of interval endpoints.
-/
variables {α : Type*} {β : Type*} [linear_order α] [partial_order β] {f : α → β}
open set function
lemma surj_on_Ioo_of_monotone_surjective
(h_mono : monotone f) (h_surj : function.surjective f) (a b : α) :
surj_on f (Ioo a b) (Ioo (f a) (f b)) :=
begin
classical,
intros p hp,
rcases h_surj p with ⟨x, rfl⟩,
refine ⟨x, mem_Ioo.2 _, rfl⟩,
by_contra h,
cases not_and_distrib.mp h with ha hb,
{ exact has_lt.lt.false (lt_of_lt_of_le hp.1 (h_mono (not_lt.mp ha))) },
{ exact has_lt.lt.false (lt_of_le_of_lt (h_mono (not_lt.mp hb)) hp.2) }
end
lemma surj_on_Ico_of_monotone_surjective
(h_mono : monotone f) (h_surj : function.surjective f) (a b : α) :
surj_on f (Ico a b) (Ico (f a) (f b)) :=
begin
obtain hab | hab := lt_or_le a b,
{ intros p hp,
rcases mem_Ioo_or_eq_left_of_mem_Ico hp with hp'|hp',
{ rw hp',
exact ⟨a, left_mem_Ico.mpr hab, rfl⟩ },
{ have := surj_on_Ioo_of_monotone_surjective h_mono h_surj a b hp',
cases this with x hx,
exact ⟨x, Ioo_subset_Ico_self hx.1, hx.2⟩ } },
{ rw Ico_eq_empty (h_mono hab).not_lt,
exact surj_on_empty f _ }
end
lemma surj_on_Ioc_of_monotone_surjective
(h_mono : monotone f) (h_surj : function.surjective f) (a b : α) :
surj_on f (Ioc a b) (Ioc (f a) (f b)) :=
begin
convert @surj_on_Ico_of_monotone_surjective _ _ _ _ _ h_mono.dual h_surj b a;
simp
end
-- to see that the hypothesis `a ≤ b` is necessary, consider a constant function
lemma surj_on_Icc_of_monotone_surjective
(h_mono : monotone f) (h_surj : function.surjective f) {a b : α} (hab : a ≤ b) :
surj_on f (Icc a b) (Icc (f a) (f b)) :=
begin
rcases lt_or_eq_of_le hab with hab|hab,
{ intros p hp,
rcases mem_Ioo_or_eq_endpoints_of_mem_Icc hp with hp'|⟨hp'|hp'⟩,
{ rw hp',
refine ⟨a, left_mem_Icc.mpr (le_of_lt hab), rfl⟩ },
{ rw hp',
refine ⟨b, right_mem_Icc.mpr (le_of_lt hab), rfl⟩ },
{ have := surj_on_Ioo_of_monotone_surjective h_mono h_surj a b hp',
cases this with x hx,
exact ⟨x, Ioo_subset_Icc_self hx.1, hx.2⟩ } },
{ simp only [hab, Icc_self],
intros _ hp,
exact ⟨b, mem_singleton _, (mem_singleton_iff.mp hp).symm⟩ }
end
lemma surj_on_Ioi_of_monotone_surjective
(h_mono : monotone f) (h_surj : function.surjective f) (a : α) :
surj_on f (Ioi a) (Ioi (f a)) :=
begin
classical,
intros p hp,
rcases h_surj p with ⟨x, rfl⟩,
refine ⟨x, _, rfl⟩,
simp only [mem_Ioi],
by_contra h,
exact has_lt.lt.false (lt_of_lt_of_le hp (h_mono (not_lt.mp h)))
end
lemma surj_on_Iio_of_monotone_surjective
(h_mono : monotone f) (h_surj : function.surjective f) (a : α) :
surj_on f (Iio a) (Iio (f a)) :=
@surj_on_Ioi_of_monotone_surjective _ _ _ _ _ h_mono.dual h_surj a
lemma surj_on_Ici_of_monotone_surjective
(h_mono : monotone f) (h_surj : function.surjective f) (a : α) :
surj_on f (Ici a) (Ici (f a)) :=
begin
intros p hp,
rw [mem_Ici, le_iff_lt_or_eq] at hp,
rcases hp with hp'|hp',
{ cases (surj_on_Ioi_of_monotone_surjective h_mono h_surj a hp') with x hx,
exact ⟨x, Ioi_subset_Ici_self hx.1, hx.2⟩ },
{ rw ← hp',
refine ⟨a, left_mem_Ici, rfl⟩ }
end
lemma surj_on_Iic_of_monotone_surjective
(h_mono : monotone f) (h_surj : function.surjective f) (a : α) :
surj_on f (Iic a) (Iic (f a)) :=
@surj_on_Ici_of_monotone_surjective _ _ _ _ _ h_mono.dual h_surj a
|
d5b7795a8da1b623f483d353feaccb740b111326 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/analysis/normed_space/multilinear.lean | 73546f022c1f38279fa10222a03b838b2921c4bc | [
"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 | 79,921 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.normed_space.operator_norm
import topology.algebra.module.multilinear
/-!
# Operator norm on the space of continuous multilinear maps
When `f` is a continuous multilinear map in finitely many variables, we define its norm `‖f‖` as the
smallest number such that `‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖` for all `m`.
We show that it is indeed a norm, and prove its basic properties.
## Main results
Let `f` be a multilinear map in finitely many variables.
* `exists_bound_of_continuous` asserts that, if `f` is continuous, then there exists `C > 0`
with `‖f m‖ ≤ C * ∏ i, ‖m i‖` for all `m`.
* `continuous_of_bound`, conversely, asserts that this bound implies continuity.
* `mk_continuous` constructs the associated continuous multilinear map.
Let `f` be a continuous multilinear map in finitely many variables.
* `‖f‖` is its norm, i.e., the smallest number such that `‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖` for
all `m`.
* `le_op_norm f m` asserts the fundamental inequality `‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖`.
* `norm_image_sub_le f m₁ m₂` gives a control of the difference `f m₁ - f m₂` in terms of
`‖f‖` and `‖m₁ - m₂‖`.
We also register isomorphisms corresponding to currying or uncurrying variables, transforming a
continuous multilinear function `f` in `n+1` variables into a continuous linear function taking
values in continuous multilinear functions in `n` variables, and also into a continuous multilinear
function in `n` variables taking values in continuous linear functions. These operations are called
`f.curry_left` and `f.curry_right` respectively (with inverses `f.uncurry_left` and
`f.uncurry_right`). They induce continuous linear equivalences between spaces of
continuous multilinear functions in `n+1` variables and spaces of continuous linear functions into
continuous multilinear functions in `n` variables (resp. continuous multilinear functions in `n`
variables taking values in continuous linear functions), called respectively
`continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`.
## Implementation notes
We mostly follow the API (and the proofs) of `operator_norm.lean`, with the additional complexity
that we should deal with multilinear maps in several variables. The currying/uncurrying
constructions are based on those in `multilinear.lean`.
From the mathematical point of view, all the results follow from the results on operator norm in
one variable, by applying them to one variable after the other through currying. However, this
is only well defined when there is an order on the variables (for instance on `fin n`) although
the final result is independent of the order. While everything could be done following this
approach, it turns out that direct proofs are easier and more efficient.
-/
noncomputable theory
open_locale big_operators nnreal
open finset metric
local attribute [instance, priority 1001]
add_comm_group.to_add_comm_monoid normed_add_comm_group.to_add_comm_group normed_space.to_module'
/-!
### Type variables
We use the following type variables in this file:
* `𝕜` : a `nontrivially_normed_field`;
* `ι`, `ι'` : finite index types with decidable equality;
* `E`, `E₁` : families of normed vector spaces over `𝕜` indexed by `i : ι`;
* `E'` : a family of normed vector spaces over `𝕜` indexed by `i' : ι'`;
* `Ei` : a family of normed vector spaces over `𝕜` indexed by `i : fin (nat.succ n)`;
* `G`, `G'` : normed vector spaces over `𝕜`.
-/
universes u v v' wE wE₁ wE' wEi wG wG'
variables {𝕜 : Type u} {ι : Type v} {ι' : Type v'} {n : ℕ}
{E : ι → Type wE} {E₁ : ι → Type wE₁} {E' : ι' → Type wE'} {Ei : fin n.succ → Type wEi}
{G : Type wG} {G' : Type wG'}
[fintype ι] [fintype ι'] [nontrivially_normed_field 𝕜]
[Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)]
[Π i, normed_add_comm_group (E₁ i)] [Π i, normed_space 𝕜 (E₁ i)]
[Π i, normed_add_comm_group (E' i)] [Π i, normed_space 𝕜 (E' i)]
[Π i, normed_add_comm_group (Ei i)] [Π i, normed_space 𝕜 (Ei i)]
[normed_add_comm_group G] [normed_space 𝕜 G] [normed_add_comm_group G'] [normed_space 𝕜 G']
/-!
### Continuity properties of multilinear maps
We relate continuity of multilinear maps to the inequality `‖f m‖ ≤ C * ∏ i, ‖m i‖`, in
both directions. Along the way, we prove useful bounds on the difference `‖f m₁ - f m₂‖`.
-/
namespace multilinear_map
variable (f : multilinear_map 𝕜 E G)
/-- If a multilinear map in finitely many variables on normed spaces satisfies the inequality
`‖f m‖ ≤ C * ∏ i, ‖m i‖` on a shell `ε i / ‖c i‖ < ‖m i‖ < ε i` for some positive numbers `ε i`
and elements `c i : 𝕜`, `1 < ‖c i‖`, then it satisfies this inequality for all `m`. -/
lemma bound_of_shell {ε : ι → ℝ} {C : ℝ} (hε : ∀ i, 0 < ε i) {c : ι → 𝕜} (hc : ∀ i, 1 < ‖c i‖)
(hf : ∀ m : Π i, E i, (∀ i, ε i / ‖c i‖ ≤ ‖m i‖) → (∀ i, ‖m i‖ < ε i) → ‖f m‖ ≤ C * ∏ i, ‖m i‖)
(m : Π i, E i) : ‖f m‖ ≤ C * ∏ i, ‖m i‖ :=
begin
rcases em (∃ i, m i = 0) with ⟨i, hi⟩|hm; [skip, push_neg at hm],
{ simp [f.map_coord_zero i hi, prod_eq_zero (mem_univ i), hi] },
choose δ hδ0 hδm_lt hle_δm hδinv using λ i, rescale_to_shell (hc i) (hε i) (hm i),
have hδ0 : 0 < ∏ i, ‖δ i‖, from prod_pos (λ i _, norm_pos_iff.2 (hδ0 i)),
simpa [map_smul_univ, norm_smul, prod_mul_distrib, mul_left_comm C, mul_le_mul_left hδ0]
using hf (λ i, δ i • m i) hle_δm hδm_lt,
end
/-- If a multilinear map in finitely many variables on normed spaces is continuous, then it
satisfies the inequality `‖f m‖ ≤ C * ∏ i, ‖m i‖`, for some `C` which can be chosen to be
positive. -/
theorem exists_bound_of_continuous (hf : continuous f) :
∃ (C : ℝ), 0 < C ∧ (∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) :=
begin
casesI is_empty_or_nonempty ι,
{ refine ⟨‖f 0‖ + 1, add_pos_of_nonneg_of_pos (norm_nonneg _) zero_lt_one, λ m, _⟩,
obtain rfl : m = 0, from funext (is_empty.elim ‹_›),
simp [univ_eq_empty, zero_le_one] },
obtain ⟨ε : ℝ, ε0 : 0 < ε, hε : ∀ m : Π i, E i, ‖m - 0‖ < ε → ‖f m - f 0‖ < 1⟩ :=
normed_add_comm_group.tendsto_nhds_nhds.1 (hf.tendsto 0) 1 zero_lt_one,
simp only [sub_zero, f.map_zero] at hε,
rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩,
have : 0 < (‖c‖ / ε) ^ fintype.card ι, from pow_pos (div_pos (zero_lt_one.trans hc) ε0) _,
refine ⟨_, this, _⟩,
refine f.bound_of_shell (λ _, ε0) (λ _, hc) (λ m hcm hm, _),
refine (hε m ((pi_norm_lt_iff ε0).2 hm)).le.trans _,
rw [← div_le_iff' this, one_div, ← inv_pow, inv_div, fintype.card, ← prod_const],
exact prod_le_prod (λ _ _, div_nonneg ε0.le (norm_nonneg _)) (λ i _, hcm i)
end
/-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂`
using the multilinearity. Here, we give a precise but hard to use version. See
`norm_image_sub_le_of_bound` for a less precise but more usable version. The bound reads
`‖f m - f m'‖ ≤
C * ‖m 1 - m' 1‖ * max ‖m 2‖ ‖m' 2‖ * max ‖m 3‖ ‖m' 3‖ * ... * max ‖m n‖ ‖m' n‖ + ...`,
where the other terms in the sum are the same products where `1` is replaced by any `i`. -/
lemma norm_image_sub_le_of_bound' [decidable_eq ι] {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) (m₁ m₂ : Πi, E i) :
‖f m₁ - f m₂‖ ≤
C * ∑ i, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖ :=
begin
have A : ∀(s : finset ι), ‖f m₁ - f (s.piecewise m₂ m₁)‖
≤ C * ∑ i in s, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖,
{ refine finset.induction (by simp) _,
assume i s his Hrec,
have I : ‖f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)‖
≤ C * ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖,
{ have A : ((insert i s).piecewise m₂ m₁)
= function.update (s.piecewise m₂ m₁) i (m₂ i) := s.piecewise_insert _ _ _,
have B : s.piecewise m₂ m₁ = function.update (s.piecewise m₂ m₁) i (m₁ i),
{ ext j,
by_cases h : j = i,
{ rw h, simp [his] },
{ simp [h] } },
rw [B, A, ← f.map_sub],
apply le_trans (H _) (mul_le_mul_of_nonneg_left _ hC),
refine prod_le_prod (λj hj, norm_nonneg _) (λj hj, _),
by_cases h : j = i,
{ rw h, simp },
{ by_cases h' : j ∈ s;
simp [h', h, le_refl] } },
calc ‖f m₁ - f ((insert i s).piecewise m₂ m₁)‖ ≤
‖f m₁ - f (s.piecewise m₂ m₁)‖ + ‖f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)‖ :
by { rw [← dist_eq_norm, ← dist_eq_norm, ← dist_eq_norm], exact dist_triangle _ _ _ }
... ≤ (C * ∑ i in s, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖)
+ C * ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖ :
add_le_add Hrec I
... = C * ∑ i in insert i s, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖ :
by simp [his, add_comm, left_distrib] },
convert A univ,
simp
end
/-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂`
using the multilinearity. Here, we give a usable but not very precise version. See
`norm_image_sub_le_of_bound'` for a more precise but less usable version. The bound is
`‖f m - f m'‖ ≤ C * card ι * ‖m - m'‖ * (max ‖m‖ ‖m'‖) ^ (card ι - 1)`. -/
lemma norm_image_sub_le_of_bound {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) (m₁ m₂ : Πi, E i) :
‖f m₁ - f m₂‖ ≤ C * (fintype.card ι) * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1) * ‖m₁ - m₂‖ :=
begin
letI := classical.dec_eq ι,
have A : ∀ (i : ι), ∏ j, (if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖)
≤ ‖m₁ - m₂‖ * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1),
{ assume i,
calc ∏ j, (if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖)
≤ ∏ j : ι, function.update (λ j, max ‖m₁‖ ‖m₂‖) i (‖m₁ - m₂‖) j :
begin
apply prod_le_prod,
{ assume j hj, by_cases h : j = i; simp [h, norm_nonneg] },
{ assume j hj,
by_cases h : j = i,
{ rw h, simp, exact norm_le_pi_norm (m₁ - m₂) i },
{ simp [h, max_le_max, norm_le_pi_norm (_ : Π i, E i)] } }
end
... = ‖m₁ - m₂‖ * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1) :
by { rw prod_update_of_mem (finset.mem_univ _), simp [card_univ_diff] } },
calc
‖f m₁ - f m₂‖
≤ C * ∑ i, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖ :
f.norm_image_sub_le_of_bound' hC H m₁ m₂
... ≤ C * ∑ i, ‖m₁ - m₂‖ * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1) :
mul_le_mul_of_nonneg_left (sum_le_sum (λi hi, A i)) hC
... = C * (fintype.card ι) * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1) * ‖m₁ - m₂‖ :
by { rw [sum_const, card_univ, nsmul_eq_mul], ring }
end
/-- If a multilinear map satisfies an inequality `‖f m‖ ≤ C * ∏ i, ‖m i‖`, then it is
continuous. -/
theorem continuous_of_bound (C : ℝ) (H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) :
continuous f :=
begin
let D := max C 1,
have D_pos : 0 ≤ D := le_trans zero_le_one (le_max_right _ _),
replace H : ∀ m, ‖f m‖ ≤ D * ∏ i, ‖m i‖,
{ assume m,
apply le_trans (H m) (mul_le_mul_of_nonneg_right (le_max_left _ _) _),
exact prod_nonneg (λ(i : ι) hi, norm_nonneg (m i)) },
refine continuous_iff_continuous_at.2 (λm, _),
refine continuous_at_of_locally_lipschitz zero_lt_one
(D * (fintype.card ι) * (‖m‖ + 1) ^ (fintype.card ι - 1)) (λm' h', _),
rw [dist_eq_norm, dist_eq_norm],
have : 0 ≤ (max ‖m'‖ ‖m‖), by simp,
have : (max ‖m'‖ ‖m‖) ≤ ‖m‖ + 1,
by simp [zero_le_one, norm_le_of_mem_closed_ball (le_of_lt h'), -add_comm],
calc
‖f m' - f m‖
≤ D * (fintype.card ι) * (max ‖m'‖ ‖m‖) ^ (fintype.card ι - 1) * ‖m' - m‖ :
f.norm_image_sub_le_of_bound D_pos H m' m
... ≤ D * (fintype.card ι) * (‖m‖ + 1) ^ (fintype.card ι - 1) * ‖m' - m‖ :
by apply_rules [mul_le_mul_of_nonneg_right, mul_le_mul_of_nonneg_left, mul_nonneg,
norm_nonneg, nat.cast_nonneg, pow_le_pow_of_le_left]
end
/-- Constructing a continuous multilinear map from a multilinear map satisfying a boundedness
condition. -/
def mk_continuous (C : ℝ) (H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) :
continuous_multilinear_map 𝕜 E G :=
{ cont := f.continuous_of_bound C H, ..f }
@[simp] lemma coe_mk_continuous (C : ℝ) (H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) :
⇑(f.mk_continuous C H) = f :=
rfl
/-- Given a multilinear map in `n` variables, if one restricts it to `k` variables putting `z` on
the other coordinates, then the resulting restricted function satisfies an inequality
`‖f.restr v‖ ≤ C * ‖z‖^(n-k) * Π ‖v i‖` if the original function satisfies `‖f v‖ ≤ C * Π ‖v i‖`. -/
lemma restr_norm_le {k n : ℕ} (f : (multilinear_map 𝕜 (λ i : fin n, G) G' : _))
(s : finset (fin n)) (hk : s.card = k) (z : G) {C : ℝ}
(H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) (v : fin k → G) :
‖f.restr s hk z v‖ ≤ C * ‖z‖ ^ (n - k) * ∏ i, ‖v i‖ :=
begin
rw [mul_right_comm, mul_assoc],
convert H _ using 2,
simp only [apply_dite norm, fintype.prod_dite, prod_const (‖z‖), finset.card_univ,
fintype.card_of_subtype sᶜ (λ x, mem_compl), card_compl, fintype.card_fin, hk, mk_coe,
← (s.order_iso_of_fin hk).symm.bijective.prod_comp (λ x, ‖v x‖)],
refl
end
end multilinear_map
/-!
### Continuous multilinear maps
We define the norm `‖f‖` of a continuous multilinear map `f` in finitely many variables as the
smallest number such that `‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖` for all `m`. We show that this
defines a normed space structure on `continuous_multilinear_map 𝕜 E G`.
-/
namespace continuous_multilinear_map
variables (c : 𝕜) (f g : continuous_multilinear_map 𝕜 E G) (m : Πi, E i)
theorem bound : ∃ (C : ℝ), 0 < C ∧ (∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) :=
f.to_multilinear_map.exists_bound_of_continuous f.2
open real
/-- The operator norm of a continuous multilinear map is the inf of all its bounds. -/
def op_norm := Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ‖f m‖ ≤ c * ∏ i, ‖m i‖}
instance has_op_norm : has_norm (continuous_multilinear_map 𝕜 E G) := ⟨op_norm⟩
/-- An alias of `continuous_multilinear_map.has_op_norm` with non-dependent types to help typeclass
search. -/
instance has_op_norm' : has_norm (continuous_multilinear_map 𝕜 (λ (i : ι), G) G') :=
continuous_multilinear_map.has_op_norm
lemma norm_def : ‖f‖ = Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ‖f m‖ ≤ c * ∏ i, ‖m i‖} := rfl
-- So that invocations of `le_cInf` make sense: we show that the set of
-- bounds is nonempty and bounded below.
lemma bounds_nonempty {f : continuous_multilinear_map 𝕜 E G} :
∃ c, c ∈ {c | 0 ≤ c ∧ ∀ m, ‖f m‖ ≤ c * ∏ i, ‖m i‖} :=
let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩
lemma bounds_bdd_below {f : continuous_multilinear_map 𝕜 E G} :
bdd_below {c | 0 ≤ c ∧ ∀ m, ‖f m‖ ≤ c * ∏ i, ‖m i‖} :=
⟨0, λ _ ⟨hn, _⟩, hn⟩
lemma op_norm_nonneg : 0 ≤ ‖f‖ :=
le_cInf bounds_nonempty (λ _ ⟨hx, _⟩, hx)
/-- The fundamental property of the operator norm of a continuous multilinear map:
`‖f m‖` is bounded by `‖f‖` times the product of the `‖m i‖`. -/
theorem le_op_norm : ‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖ :=
begin
have A : 0 ≤ ∏ i, ‖m i‖ := prod_nonneg (λj hj, norm_nonneg _),
cases A.eq_or_lt with h hlt,
{ rcases prod_eq_zero_iff.1 h.symm with ⟨i, _, hi⟩,
rw norm_eq_zero at hi,
have : f m = 0 := f.map_coord_zero i hi,
rw [this, norm_zero],
exact mul_nonneg (op_norm_nonneg f) A },
{ rw [← div_le_iff hlt],
apply le_cInf bounds_nonempty,
rintro c ⟨_, hc⟩, rw [div_le_iff hlt], apply hc }
end
theorem le_of_op_norm_le {C : ℝ} (h : ‖f‖ ≤ C) : ‖f m‖ ≤ C * ∏ i, ‖m i‖ :=
(f.le_op_norm m).trans $ mul_le_mul_of_nonneg_right h (prod_nonneg $ λ i _, norm_nonneg (m i))
lemma ratio_le_op_norm : ‖f m‖ / ∏ i, ‖m i‖ ≤ ‖f‖ :=
div_le_of_nonneg_of_le_mul (prod_nonneg $ λ i _, norm_nonneg _) (op_norm_nonneg _) (f.le_op_norm m)
/-- The image of the unit ball under a continuous multilinear map is bounded. -/
lemma unit_le_op_norm (h : ‖m‖ ≤ 1) : ‖f m‖ ≤ ‖f‖ :=
calc
‖f m‖ ≤ ‖f‖ * ∏ i, ‖m i‖ : f.le_op_norm m
... ≤ ‖f‖ * ∏ i : ι, 1 :
mul_le_mul_of_nonneg_left (prod_le_prod (λi hi, norm_nonneg _)
(λi hi, le_trans (norm_le_pi_norm (_ : Π i, E i) _) h)) (op_norm_nonneg f)
... = ‖f‖ : by simp
/-- If one controls the norm of every `f x`, then one controls the norm of `f`. -/
lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ m, ‖f m‖ ≤ M * ∏ i, ‖m i‖) :
‖f‖ ≤ M :=
cInf_le bounds_bdd_below ⟨hMp, hM⟩
/-- The operator norm satisfies the triangle inequality. -/
theorem op_norm_add_le : ‖f + g‖ ≤ ‖f‖ + ‖g‖ :=
cInf_le bounds_bdd_below
⟨add_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, by { rw add_mul,
exact norm_add_le_of_le (le_op_norm _ _) (le_op_norm _ _) }⟩
lemma op_norm_zero : ‖(0 : continuous_multilinear_map 𝕜 E G)‖ = 0 :=
(op_norm_nonneg _).antisymm' $ op_norm_le_bound 0 le_rfl $ λ m, by simp
/-- A continuous linear map is zero iff its norm vanishes. -/
theorem op_norm_zero_iff : ‖f‖ = 0 ↔ f = 0 :=
⟨λ h, by { ext m, simpa [h] using f.le_op_norm m }, by { rintro rfl, exact op_norm_zero }⟩
section
variables {𝕜' : Type*} [normed_field 𝕜'] [normed_space 𝕜' G] [smul_comm_class 𝕜 𝕜' G]
lemma op_norm_smul_le (c : 𝕜') : ‖c • f‖ ≤ ‖c‖ * ‖f‖ :=
(c • f).op_norm_le_bound
(mul_nonneg (norm_nonneg _) (op_norm_nonneg _))
begin
intro m,
erw [norm_smul, mul_assoc],
exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _)
end
lemma op_norm_neg : ‖-f‖ = ‖f‖ := by { rw norm_def, apply congr_arg, ext, simp }
/-- Continuous multilinear maps themselves form a normed space with respect to
the operator norm. -/
instance normed_add_comm_group : normed_add_comm_group (continuous_multilinear_map 𝕜 E G) :=
add_group_norm.to_normed_add_comm_group
{ to_fun := norm,
map_zero' := op_norm_zero,
neg' := op_norm_neg,
add_le' := op_norm_add_le,
eq_zero_of_map_eq_zero' := λ f, f.op_norm_zero_iff.1 }
/-- An alias of `continuous_multilinear_map.normed_add_comm_group` with non-dependent types to help
typeclass search. -/
instance normed_add_comm_group' :
normed_add_comm_group (continuous_multilinear_map 𝕜 (λ i : ι, G) G') :=
continuous_multilinear_map.normed_add_comm_group
instance normed_space : normed_space 𝕜' (continuous_multilinear_map 𝕜 E G) :=
⟨λ c f, f.op_norm_smul_le c⟩
/-- An alias of `continuous_multilinear_map.normed_space` with non-dependent types to help typeclass
search. -/
instance normed_space' : normed_space 𝕜' (continuous_multilinear_map 𝕜 (λ i : ι, G') G) :=
continuous_multilinear_map.normed_space
theorem le_op_norm_mul_prod_of_le {b : ι → ℝ} (hm : ∀ i, ‖m i‖ ≤ b i) : ‖f m‖ ≤ ‖f‖ * ∏ i, b i :=
(f.le_op_norm m).trans $ mul_le_mul_of_nonneg_left
(prod_le_prod (λ _ _, norm_nonneg _) (λ i _, hm i)) (norm_nonneg f)
theorem le_op_norm_mul_pow_card_of_le {b : ℝ} (hm : ∀ i, ‖m i‖ ≤ b) :
‖f m‖ ≤ ‖f‖ * b ^ fintype.card ι :=
by simpa only [prod_const] using f.le_op_norm_mul_prod_of_le m hm
theorem le_op_norm_mul_pow_of_le {Ei : fin n → Type*} [Π i, normed_add_comm_group (Ei i)]
[Π i, normed_space 𝕜 (Ei i)] (f : continuous_multilinear_map 𝕜 Ei G) (m : Π i, Ei i)
{b : ℝ} (hm : ‖m‖ ≤ b) :
‖f m‖ ≤ ‖f‖ * b ^ n :=
by simpa only [fintype.card_fin]
using f.le_op_norm_mul_pow_card_of_le m (λ i, (norm_le_pi_norm m i).trans hm)
/-- The fundamental property of the operator norm of a continuous multilinear map:
`‖f m‖` is bounded by `‖f‖` times the product of the `‖m i‖`, `nnnorm` version. -/
theorem le_op_nnnorm : ‖f m‖₊ ≤ ‖f‖₊ * ∏ i, ‖m i‖₊ :=
nnreal.coe_le_coe.1 $ by { push_cast, exact f.le_op_norm m }
theorem le_of_op_nnnorm_le {C : ℝ≥0} (h : ‖f‖₊ ≤ C) : ‖f m‖₊ ≤ C * ∏ i, ‖m i‖₊ :=
(f.le_op_nnnorm m).trans $ mul_le_mul' h le_rfl
lemma op_norm_prod (f : continuous_multilinear_map 𝕜 E G) (g : continuous_multilinear_map 𝕜 E G') :
‖f.prod g‖ = max (‖f‖) (‖g‖) :=
le_antisymm
(op_norm_le_bound _ (norm_nonneg (f, g)) (λ m,
have H : 0 ≤ ∏ i, ‖m i‖, from prod_nonneg $ λ _ _, norm_nonneg _,
by simpa only [prod_apply, prod.norm_def, max_mul_of_nonneg, H]
using max_le_max (f.le_op_norm m) (g.le_op_norm m))) $
max_le
(f.op_norm_le_bound (norm_nonneg _) $ λ m, (le_max_left _ _).trans ((f.prod g).le_op_norm _))
(g.op_norm_le_bound (norm_nonneg _) $ λ m, (le_max_right _ _).trans ((f.prod g).le_op_norm _))
lemma norm_pi {ι' : Type v'} [fintype ι'] {E' : ι' → Type wE'} [Π i', normed_add_comm_group (E' i')]
[Π i', normed_space 𝕜 (E' i')] (f : Π i', continuous_multilinear_map 𝕜 E (E' i')) :
‖pi f‖ = ‖f‖ :=
begin
apply le_antisymm,
{ refine (op_norm_le_bound _ (norm_nonneg f) (λ m, _)),
dsimp,
rw pi_norm_le_iff_of_nonneg,
exacts [λ i, (f i).le_of_op_norm_le m (norm_le_pi_norm f i),
mul_nonneg (norm_nonneg f) (prod_nonneg $ λ _ _, norm_nonneg _)] },
{ refine (pi_norm_le_iff_of_nonneg (norm_nonneg _)).2 (λ i, _),
refine (op_norm_le_bound _ (norm_nonneg _) (λ m, _)),
refine le_trans _ ((pi f).le_op_norm m),
convert norm_le_pi_norm (λ j, f j m) i }
end
section
variables (𝕜 G)
@[simp] lemma norm_of_subsingleton [subsingleton ι] [nontrivial G] (i' : ι) :
‖of_subsingleton 𝕜 G i'‖ = 1 :=
begin
apply le_antisymm,
{ refine op_norm_le_bound _ zero_le_one (λ m, _),
rw [fintype.prod_subsingleton _ i', one_mul, of_subsingleton_apply] },
{ obtain ⟨g, hg⟩ := exists_ne (0 : G),
rw ←norm_ne_zero_iff at hg,
have := (of_subsingleton 𝕜 G i').ratio_le_op_norm (λ _, g),
rwa [fintype.prod_subsingleton _ i', of_subsingleton_apply, div_self hg] at this },
end
@[simp] lemma nnnorm_of_subsingleton [subsingleton ι] [nontrivial G] (i' : ι) :
‖of_subsingleton 𝕜 G i'‖₊ = 1 :=
nnreal.eq $ norm_of_subsingleton _ _ _
variables {G} (E)
@[simp] lemma norm_const_of_is_empty [is_empty ι] (x : G) : ‖const_of_is_empty 𝕜 E x‖ = ‖x‖ :=
begin
apply le_antisymm,
{ refine op_norm_le_bound _ (norm_nonneg _) (λ x, _),
rw [fintype.prod_empty, mul_one, const_of_is_empty_apply], },
{ simpa using (const_of_is_empty 𝕜 E x).le_op_norm 0 }
end
@[simp] lemma nnnorm_const_of_is_empty [is_empty ι] (x : G) : ‖const_of_is_empty 𝕜 E x‖₊ = ‖x‖₊ :=
nnreal.eq $ norm_const_of_is_empty _ _ _
end
section
variables (𝕜 E E' G G')
/-- `continuous_multilinear_map.prod` as a `linear_isometry_equiv`. -/
def prodL :
(continuous_multilinear_map 𝕜 E G) × (continuous_multilinear_map 𝕜 E G') ≃ₗᵢ[𝕜]
continuous_multilinear_map 𝕜 E (G × G') :=
{ to_fun := λ f, f.1.prod f.2,
inv_fun := λ f, ((continuous_linear_map.fst 𝕜 G G').comp_continuous_multilinear_map f,
(continuous_linear_map.snd 𝕜 G G').comp_continuous_multilinear_map f),
map_add' := λ f g, rfl,
map_smul' := λ c f, rfl,
left_inv := λ f, by ext; refl,
right_inv := λ f, by ext; refl,
norm_map' := λ f, op_norm_prod f.1 f.2 }
/-- `continuous_multilinear_map.pi` as a `linear_isometry_equiv`. -/
def piₗᵢ {ι' : Type v'} [fintype ι'] {E' : ι' → Type wE'} [Π i', normed_add_comm_group (E' i')]
[Π i', normed_space 𝕜 (E' i')] :
@linear_isometry_equiv 𝕜 𝕜 _ _ (ring_hom.id 𝕜) _ _ _
(Π i', continuous_multilinear_map 𝕜 E (E' i')) (continuous_multilinear_map 𝕜 E (Π i, E' i)) _ _
(@pi.module ι' _ 𝕜 _ _ (λ i', infer_instance)) _ :=
{ to_linear_equiv :=
-- note: `pi_linear_equiv` does not unify correctly here, presumably due to issues with dependent
-- typeclass arguments.
{ map_add' := λ f g, rfl,
map_smul' := λ c f, rfl,
.. pi_equiv, },
norm_map' := norm_pi }
end
end
section restrict_scalars
variables {𝕜' : Type*} [nontrivially_normed_field 𝕜'] [normed_algebra 𝕜' 𝕜]
variables [normed_space 𝕜' G] [is_scalar_tower 𝕜' 𝕜 G]
variables [Π i, normed_space 𝕜' (E i)] [∀ i, is_scalar_tower 𝕜' 𝕜 (E i)]
@[simp] lemma norm_restrict_scalars : ‖f.restrict_scalars 𝕜'‖ = ‖f‖ :=
by simp only [norm_def, coe_restrict_scalars]
variable (𝕜')
/-- `continuous_multilinear_map.restrict_scalars` as a `continuous_multilinear_map`. -/
def restrict_scalars_linear :
continuous_multilinear_map 𝕜 E G →L[𝕜'] continuous_multilinear_map 𝕜' E G :=
linear_map.mk_continuous
{ to_fun := restrict_scalars 𝕜',
map_add' := λ m₁ m₂, rfl,
map_smul' := λ c m, rfl } 1 $ λ f, by simp
variable {𝕜'}
lemma continuous_restrict_scalars :
continuous (restrict_scalars 𝕜' : continuous_multilinear_map 𝕜 E G →
continuous_multilinear_map 𝕜' E G) :=
(restrict_scalars_linear 𝕜').continuous
end restrict_scalars
/-- The difference `f m₁ - f m₂` is controlled in terms of `‖f‖` and `‖m₁ - m₂‖`, precise version.
For a less precise but more usable version, see `norm_image_sub_le`. The bound reads
`‖f m - f m'‖ ≤
‖f‖ * ‖m 1 - m' 1‖ * max ‖m 2‖ ‖m' 2‖ * max ‖m 3‖ ‖m' 3‖ * ... * max ‖m n‖ ‖m' n‖ + ...`,
where the other terms in the sum are the same products where `1` is replaced by any `i`.-/
lemma norm_image_sub_le' [decidable_eq ι] (m₁ m₂ : Πi, E i) :
‖f m₁ - f m₂‖ ≤
‖f‖ * ∑ i, ∏ j, if j = i then ‖m₁ i - m₂ i‖ else max ‖m₁ j‖ ‖m₂ j‖ :=
f.to_multilinear_map.norm_image_sub_le_of_bound' (norm_nonneg _) f.le_op_norm _ _
/-- The difference `f m₁ - f m₂` is controlled in terms of `‖f‖` and `‖m₁ - m₂‖`, less precise
version. For a more precise but less usable version, see `norm_image_sub_le'`.
The bound is `‖f m - f m'‖ ≤ ‖f‖ * card ι * ‖m - m'‖ * (max ‖m‖ ‖m'‖) ^ (card ι - 1)`.-/
lemma norm_image_sub_le (m₁ m₂ : Πi, E i) :
‖f m₁ - f m₂‖ ≤ ‖f‖ * (fintype.card ι) * (max ‖m₁‖ ‖m₂‖) ^ (fintype.card ι - 1) * ‖m₁ - m₂‖ :=
f.to_multilinear_map.norm_image_sub_le_of_bound (norm_nonneg _) f.le_op_norm _ _
/-- Applying a multilinear map to a vector is continuous in both coordinates. -/
lemma continuous_eval :
continuous (λ p : continuous_multilinear_map 𝕜 E G × Π i, E i, p.1 p.2) :=
begin
apply continuous_iff_continuous_at.2 (λp, _),
apply continuous_at_of_locally_lipschitz zero_lt_one
((‖p‖ + 1) * (fintype.card ι) * (‖p‖ + 1) ^ (fintype.card ι - 1) + ∏ i, ‖p.2 i‖)
(λq hq, _),
have : 0 ≤ (max ‖q.2‖ ‖p.2‖), by simp,
have : 0 ≤ ‖p‖ + 1 := zero_le_one.trans ((le_add_iff_nonneg_left 1).2 $ norm_nonneg p),
have A : ‖q‖ ≤ ‖p‖ + 1 := norm_le_of_mem_closed_ball hq.le,
have : (max ‖q.2‖ ‖p.2‖) ≤ ‖p‖ + 1 :=
(max_le_max (norm_snd_le q) (norm_snd_le p)).trans (by simp [A, -add_comm, zero_le_one]),
have : ∀ (i : ι), i ∈ univ → 0 ≤ ‖p.2 i‖ := λ i hi, norm_nonneg _,
calc dist (q.1 q.2) (p.1 p.2)
≤ dist (q.1 q.2) (q.1 p.2) + dist (q.1 p.2) (p.1 p.2) : dist_triangle _ _ _
... = ‖q.1 q.2 - q.1 p.2‖ + ‖q.1 p.2 - p.1 p.2‖ : by rw [dist_eq_norm, dist_eq_norm]
... ≤ ‖q.1‖ * (fintype.card ι) * (max ‖q.2‖ ‖p.2‖) ^ (fintype.card ι - 1) * ‖q.2 - p.2‖
+ ‖q.1 - p.1‖ * ∏ i, ‖p.2 i‖ :
add_le_add (norm_image_sub_le _ _ _) ((q.1 - p.1).le_op_norm p.2)
... ≤ (‖p‖ + 1) * (fintype.card ι) * (‖p‖ + 1) ^ (fintype.card ι - 1) * ‖q - p‖
+ ‖q - p‖ * ∏ i, ‖p.2 i‖ :
by apply_rules [add_le_add, mul_le_mul, le_refl, le_trans (norm_fst_le q) A, nat.cast_nonneg,
mul_nonneg, pow_le_pow_of_le_left, pow_nonneg, norm_snd_le (q - p), norm_nonneg,
norm_fst_le (q - p), prod_nonneg]
... = ((‖p‖ + 1) * (fintype.card ι) * (‖p‖ + 1) ^ (fintype.card ι - 1)
+ (∏ i, ‖p.2 i‖)) * dist q p : by { rw dist_eq_norm, ring }
end
lemma continuous_eval_left (m : Π i, E i) :
continuous (λ p : continuous_multilinear_map 𝕜 E G, p m) :=
continuous_eval.comp (continuous_id.prod_mk continuous_const)
lemma has_sum_eval
{α : Type*} {p : α → continuous_multilinear_map 𝕜 E G} {q : continuous_multilinear_map 𝕜 E G}
(h : has_sum p q) (m : Π i, E i) : has_sum (λ a, p a m) (q m) :=
begin
dsimp [has_sum] at h ⊢,
convert ((continuous_eval_left m).tendsto _).comp h,
ext s,
simp
end
lemma tsum_eval {α : Type*} {p : α → continuous_multilinear_map 𝕜 E G} (hp : summable p)
(m : Π i, E i) : (∑' a, p a) m = ∑' a, p a m :=
(has_sum_eval hp.has_sum m).tsum_eq.symm
open_locale topology
open filter
/-- If the target space is complete, the space of continuous multilinear maps with its norm is also
complete. The proof is essentially the same as for the space of continuous linear maps (modulo the
addition of `finset.prod` where needed. The duplication could be avoided by deducing the linear
case from the multilinear case via a currying isomorphism. However, this would mess up imports,
and it is more satisfactory to have the simplest case as a standalone proof. -/
instance [complete_space G] : complete_space (continuous_multilinear_map 𝕜 E G) :=
begin
have nonneg : ∀ (v : Π i, E i), 0 ≤ ∏ i, ‖v i‖ :=
λ v, finset.prod_nonneg (λ i hi, norm_nonneg _),
-- We show that every Cauchy sequence converges.
refine metric.complete_of_cauchy_seq_tendsto (λ f hf, _),
-- We now expand out the definition of a Cauchy sequence,
rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩,
-- and establish that the evaluation at any point `v : Π i, E i` is Cauchy.
have cau : ∀ v, cauchy_seq (λ n, f n v),
{ assume v,
apply cauchy_seq_iff_le_tendsto_0.2 ⟨λ n, b n * ∏ i, ‖v i‖, λ n, _, _, _⟩,
{ exact mul_nonneg (b0 n) (nonneg v) },
{ assume n m N hn hm,
rw dist_eq_norm,
apply le_trans ((f n - f m).le_op_norm v) _,
exact mul_le_mul_of_nonneg_right (b_bound n m N hn hm) (nonneg v) },
{ simpa using b_lim.mul tendsto_const_nhds } },
-- We assemble the limits points of those Cauchy sequences
-- (which exist as `G` is complete)
-- into a function which we call `F`.
choose F hF using λv, cauchy_seq_tendsto_of_complete (cau v),
-- Next, we show that this `F` is multilinear,
let Fmult : multilinear_map 𝕜 E G :=
{ to_fun := F,
map_add' := λ _ v i x y, begin
resetI,
have A := hF (function.update v i (x + y)),
have B := (hF (function.update v i x)).add (hF (function.update v i y)),
simp at A B,
exact tendsto_nhds_unique A B
end,
map_smul' := λ _ v i c x, begin
resetI,
have A := hF (function.update v i (c • x)),
have B := filter.tendsto.smul (@tendsto_const_nhds _ ℕ _ c _) (hF (function.update v i x)),
simp at A B,
exact tendsto_nhds_unique A B
end },
-- and that `F` has norm at most `(b 0 + ‖f 0‖)`.
have Fnorm : ∀ v, ‖F v‖ ≤ (b 0 + ‖f 0‖) * ∏ i, ‖v i‖,
{ assume v,
have A : ∀ n, ‖f n v‖ ≤ (b 0 + ‖f 0‖) * ∏ i, ‖v i‖,
{ assume n,
apply le_trans ((f n).le_op_norm _) _,
apply mul_le_mul_of_nonneg_right _ (nonneg v),
calc ‖f n‖ = ‖(f n - f 0) + f 0‖ : by { congr' 1, abel }
... ≤ ‖f n - f 0‖ + ‖f 0‖ : norm_add_le _ _
... ≤ b 0 + ‖f 0‖ : begin
apply add_le_add_right,
simpa [dist_eq_norm] using b_bound n 0 0 (zero_le _) (zero_le _)
end },
exact le_of_tendsto (hF v).norm (eventually_of_forall A) },
-- Thus `F` is continuous, and we propose that as the limit point of our original Cauchy sequence.
let Fcont := Fmult.mk_continuous _ Fnorm,
use Fcont,
-- Our last task is to establish convergence to `F` in norm.
have : ∀ n, ‖f n - Fcont‖ ≤ b n,
{ assume n,
apply op_norm_le_bound _ (b0 n) (λ v, _),
have A : ∀ᶠ m in at_top, ‖(f n - f m) v‖ ≤ b n * ∏ i, ‖v i‖,
{ refine eventually_at_top.2 ⟨n, λ m hm, _⟩,
apply le_trans ((f n - f m).le_op_norm _) _,
exact mul_le_mul_of_nonneg_right (b_bound n m n le_rfl hm) (nonneg v) },
have B : tendsto (λ m, ‖(f n - f m) v‖) at_top (𝓝 (‖(f n - Fcont) v‖)) :=
tendsto.norm (tendsto_const_nhds.sub (hF v)),
exact le_of_tendsto B A },
erw tendsto_iff_norm_tendsto_zero,
exact squeeze_zero (λ n, norm_nonneg _) this b_lim,
end
end continuous_multilinear_map
/-- If a continuous multilinear map is constructed from a multilinear map via the constructor
`mk_continuous`, then its norm is bounded by the bound given to the constructor if it is
nonnegative. -/
lemma multilinear_map.mk_continuous_norm_le (f : multilinear_map 𝕜 E G) {C : ℝ} (hC : 0 ≤ C)
(H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) : ‖f.mk_continuous C H‖ ≤ C :=
continuous_multilinear_map.op_norm_le_bound _ hC (λm, H m)
/-- If a continuous multilinear map is constructed from a multilinear map via the constructor
`mk_continuous`, then its norm is bounded by the bound given to the constructor if it is
nonnegative. -/
lemma multilinear_map.mk_continuous_norm_le' (f : multilinear_map 𝕜 E G) {C : ℝ}
(H : ∀ m, ‖f m‖ ≤ C * ∏ i, ‖m i‖) : ‖f.mk_continuous C H‖ ≤ max C 0 :=
continuous_multilinear_map.op_norm_le_bound _ (le_max_right _ _) $
λ m, (H m).trans $ mul_le_mul_of_nonneg_right (le_max_left _ _)
(prod_nonneg $ λ _ _, norm_nonneg _)
namespace continuous_multilinear_map
/-- Given a continuous multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset
`s` of `k` of these variables, one gets a new continuous 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 : (G [×n]→L[𝕜] G' : _)) (s : finset (fin n)) (hk : s.card = k) (z : G) :
G [×k]→L[𝕜] G' :=
(f.to_multilinear_map.restr s hk z).mk_continuous
(‖f‖ * ‖z‖^(n-k)) $ λ v, multilinear_map.restr_norm_le _ _ _ _ f.le_op_norm _
lemma norm_restr {k n : ℕ} (f : G [×n]→L[𝕜] G') (s : finset (fin n)) (hk : s.card = k) (z : G) :
‖f.restr s hk z‖ ≤ ‖f‖ * ‖z‖ ^ (n - k) :=
begin
apply multilinear_map.mk_continuous_norm_le,
exact mul_nonneg (norm_nonneg _) (pow_nonneg (norm_nonneg _) _)
end
section
variables {𝕜 ι} {A : Type*} [normed_comm_ring A] [normed_algebra 𝕜 A]
@[simp]
lemma norm_mk_pi_algebra_le [nonempty ι] :
‖continuous_multilinear_map.mk_pi_algebra 𝕜 ι A‖ ≤ 1 :=
begin
have := λ f, @op_norm_le_bound 𝕜 ι (λ i, A) A _ _ _ _ _ _ f _ zero_le_one,
refine this _ _,
intros m,
simp only [continuous_multilinear_map.mk_pi_algebra_apply, one_mul],
exact norm_prod_le' _ univ_nonempty _,
end
lemma norm_mk_pi_algebra_of_empty [is_empty ι] :
‖continuous_multilinear_map.mk_pi_algebra 𝕜 ι A‖ = ‖(1 : A)‖ :=
begin
apply le_antisymm,
{ have := λ f, @op_norm_le_bound 𝕜 ι (λ i, A) A _ _ _ _ _ _ f _ (norm_nonneg (1 : A)),
refine this _ _,
simp, },
{ convert ratio_le_op_norm _ (λ _, (1 : A)),
simp [eq_empty_of_is_empty (univ : finset ι)], },
end
@[simp] lemma norm_mk_pi_algebra [norm_one_class A] :
‖continuous_multilinear_map.mk_pi_algebra 𝕜 ι A‖ = 1 :=
begin
casesI is_empty_or_nonempty ι,
{ simp [norm_mk_pi_algebra_of_empty] },
{ refine le_antisymm norm_mk_pi_algebra_le _,
convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance],
simp },
end
end
section
variables {𝕜 n} {A : Type*} [normed_ring A] [normed_algebra 𝕜 A]
lemma norm_mk_pi_algebra_fin_succ_le :
‖continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n.succ A‖ ≤ 1 :=
begin
have := λ f, @op_norm_le_bound 𝕜 (fin n.succ) (λ i, A) A _ _ _ _ _ _ f _ zero_le_one,
refine this _ _,
intros m,
simp only [continuous_multilinear_map.mk_pi_algebra_fin_apply, one_mul, list.of_fn_eq_map,
fin.prod_univ_def, multiset.coe_map, multiset.coe_prod],
refine (list.norm_prod_le' _).trans_eq _,
{ rw [ne.def, list.map_eq_nil, list.fin_range_eq_nil],
exact nat.succ_ne_zero _, },
rw list.map_map,
end
lemma norm_mk_pi_algebra_fin_le_of_pos (hn : 0 < n) :
‖continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A‖ ≤ 1 :=
begin
obtain ⟨n, rfl⟩ := nat.exists_eq_succ_of_ne_zero hn.ne',
exact norm_mk_pi_algebra_fin_succ_le
end
lemma norm_mk_pi_algebra_fin_zero :
‖continuous_multilinear_map.mk_pi_algebra_fin 𝕜 0 A‖ = ‖(1 : A)‖ :=
begin
refine le_antisymm _ _,
{ have := λ f, @op_norm_le_bound 𝕜 (fin 0) (λ i, A) A _ _ _ _ _ _ f _ (norm_nonneg (1 : A)),
refine this _ _,
simp, },
{ convert ratio_le_op_norm _ (λ _, (1 : A)),
simp }
end
@[simp] lemma norm_mk_pi_algebra_fin [norm_one_class A] :
‖continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A‖ = 1 :=
begin
cases n,
{ simp [norm_mk_pi_algebra_fin_zero] },
{ refine le_antisymm norm_mk_pi_algebra_fin_succ_le _,
convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance],
simp }
end
end
variables (𝕜 ι)
/-- The canonical continuous multilinear map on `𝕜^ι`, associating to `m` the product of all the
`m i` (multiplied by a fixed reference element `z` in the target module) -/
protected def mk_pi_field (z : G) : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G :=
multilinear_map.mk_continuous
(multilinear_map.mk_pi_ring 𝕜 ι z) (‖z‖)
(λ m, by simp only [multilinear_map.mk_pi_ring_apply, norm_smul, norm_prod,
mul_comm])
variables {𝕜 ι}
@[simp] lemma mk_pi_field_apply (z : G) (m : ι → 𝕜) :
(continuous_multilinear_map.mk_pi_field 𝕜 ι z : (ι → 𝕜) → G) m = (∏ i, m i) • z := rfl
lemma mk_pi_field_apply_one_eq_self (f : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) :
continuous_multilinear_map.mk_pi_field 𝕜 ι (f (λi, 1)) = f :=
to_multilinear_map_injective f.to_multilinear_map.mk_pi_ring_apply_one_eq_self
@[simp] lemma norm_mk_pi_field (z : G) : ‖continuous_multilinear_map.mk_pi_field 𝕜 ι z‖ = ‖z‖ :=
(multilinear_map.mk_continuous_norm_le _ (norm_nonneg z) _).antisymm $
by simpa using (continuous_multilinear_map.mk_pi_field 𝕜 ι z).le_op_norm (λ _, 1)
lemma mk_pi_field_eq_iff {z₁ z₂ : G} :
continuous_multilinear_map.mk_pi_field 𝕜 ι z₁ = continuous_multilinear_map.mk_pi_field 𝕜 ι z₂ ↔
z₁ = z₂ :=
begin
rw [← to_multilinear_map_injective.eq_iff],
exact multilinear_map.mk_pi_ring_eq_iff
end
lemma mk_pi_field_zero :
continuous_multilinear_map.mk_pi_field 𝕜 ι (0 : G) = 0 :=
by ext; rw [mk_pi_field_apply, smul_zero, continuous_multilinear_map.zero_apply]
lemma mk_pi_field_eq_zero_iff (z : G) :
continuous_multilinear_map.mk_pi_field 𝕜 ι z = 0 ↔ z = 0 :=
by rw [← mk_pi_field_zero, mk_pi_field_eq_iff]
variables (𝕜 ι G)
/-- Continuous multilinear maps on `𝕜^n` with values in `G` are in bijection with `G`, as such a
continuous multilinear map is completely determined by its value on the constant vector made of
ones. We register this bijection as a linear isometry in
`continuous_multilinear_map.pi_field_equiv`. -/
protected def pi_field_equiv : G ≃ₗᵢ[𝕜] (continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) :=
{ to_fun := λ z, continuous_multilinear_map.mk_pi_field 𝕜 ι 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_field_apply_one_eq_self,
norm_map' := norm_mk_pi_field }
end continuous_multilinear_map
namespace continuous_linear_map
lemma norm_comp_continuous_multilinear_map_le (g : G →L[𝕜] G')
(f : continuous_multilinear_map 𝕜 E G) :
‖g.comp_continuous_multilinear_map f‖ ≤ ‖g‖ * ‖f‖ :=
continuous_multilinear_map.op_norm_le_bound _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) $ λ m,
calc ‖g (f m)‖ ≤ ‖g‖ * (‖f‖ * ∏ i, ‖m i‖) : g.le_op_norm_of_le $ f.le_op_norm _
... = _ : (mul_assoc _ _ _).symm
variables (𝕜 E G G')
/-- `continuous_linear_map.comp_continuous_multilinear_map` as a bundled continuous bilinear map. -/
def comp_continuous_multilinear_mapL :
(G →L[𝕜] G') →L[𝕜] continuous_multilinear_map 𝕜 E G →L[𝕜] continuous_multilinear_map 𝕜 E G' :=
linear_map.mk_continuous₂
(linear_map.mk₂ 𝕜 comp_continuous_multilinear_map (λ f₁ f₂ g, rfl) (λ c f g, rfl)
(λ f g₁ g₂, by { ext1, apply f.map_add }) (λ c f g, by { ext1, simp }))
1 $ λ f g, by { rw one_mul, exact f.norm_comp_continuous_multilinear_map_le g }
variables {𝕜 G G'}
/-- `continuous_linear_map.comp_continuous_multilinear_map` as a bundled
continuous linear equiv. -/
def _root_.continuous_linear_equiv.comp_continuous_multilinear_mapL (g : G ≃L[𝕜] G') :
continuous_multilinear_map 𝕜 E G ≃L[𝕜] continuous_multilinear_map 𝕜 E G' :=
{ inv_fun := comp_continuous_multilinear_mapL 𝕜 _ _ _ g.symm.to_continuous_linear_map,
left_inv := begin
assume f,
ext1 m,
simp only [comp_continuous_multilinear_mapL, continuous_linear_equiv.coe_def_rev,
to_linear_map_eq_coe, linear_map.to_fun_eq_coe, coe_coe, linear_map.mk_continuous₂_apply,
linear_map.mk₂_apply, comp_continuous_multilinear_map_coe, continuous_linear_equiv.coe_coe,
function.comp_app, continuous_linear_equiv.symm_apply_apply],
end,
right_inv := begin
assume f,
ext1 m,
simp only [comp_continuous_multilinear_mapL, continuous_linear_equiv.coe_def_rev,
to_linear_map_eq_coe, linear_map.mk_continuous₂_apply, linear_map.mk₂_apply,
linear_map.to_fun_eq_coe, coe_coe, comp_continuous_multilinear_map_coe,
continuous_linear_equiv.coe_coe, function.comp_app, continuous_linear_equiv.apply_symm_apply],
end,
continuous_to_fun :=
(comp_continuous_multilinear_mapL 𝕜 _ _ _ g.to_continuous_linear_map).continuous,
continuous_inv_fun :=
(comp_continuous_multilinear_mapL 𝕜 _ _ _ g.symm.to_continuous_linear_map).continuous,
.. comp_continuous_multilinear_mapL 𝕜 _ _ _ g.to_continuous_linear_map }
@[simp] lemma _root_.continuous_linear_equiv.comp_continuous_multilinear_mapL_symm
(g : G ≃L[𝕜] G') :
(g.comp_continuous_multilinear_mapL E).symm = g.symm.comp_continuous_multilinear_mapL E := rfl
variables {E}
@[simp] lemma _root_.continuous_linear_equiv.comp_continuous_multilinear_mapL_apply
(g : G ≃L[𝕜] G') (f : continuous_multilinear_map 𝕜 E G) :
g.comp_continuous_multilinear_mapL E f = (g : G →L[𝕜] G').comp_continuous_multilinear_map f :=
rfl
/-- Flip arguments in `f : G →L[𝕜] continuous_multilinear_map 𝕜 E G'` to get
`continuous_multilinear_map 𝕜 E (G →L[𝕜] G')` -/
def flip_multilinear (f : G →L[𝕜] continuous_multilinear_map 𝕜 E G') :
continuous_multilinear_map 𝕜 E (G →L[𝕜] G') :=
multilinear_map.mk_continuous
{ to_fun := λ m, linear_map.mk_continuous
{ to_fun := λ x, f x m,
map_add' := λ x y, by simp only [map_add, continuous_multilinear_map.add_apply],
map_smul' := λ c x, by simp only [continuous_multilinear_map.smul_apply, map_smul,
ring_hom.id_apply] }
(‖f‖ * ∏ i, ‖m i‖) $ λ x,
by { rw mul_right_comm, exact (f x).le_of_op_norm_le _ (f.le_op_norm x) },
map_add' := λ _ m i x y,
by { ext1, simp only [add_apply, continuous_multilinear_map.map_add, linear_map.coe_mk,
linear_map.mk_continuous_apply]},
map_smul' := λ _ m i c x,
by { ext1, simp only [coe_smul', continuous_multilinear_map.map_smul, linear_map.coe_mk,
linear_map.mk_continuous_apply, pi.smul_apply]} }
‖f‖ $ λ m,
linear_map.mk_continuous_norm_le _
(mul_nonneg (norm_nonneg f) (prod_nonneg $ λ i hi, norm_nonneg (m i))) _
end continuous_linear_map
lemma linear_isometry.norm_comp_continuous_multilinear_map
(g : G →ₗᵢ[𝕜] G') (f : continuous_multilinear_map 𝕜 E G) :
‖g.to_continuous_linear_map.comp_continuous_multilinear_map f‖ = ‖f‖ :=
by simp only [continuous_linear_map.comp_continuous_multilinear_map_coe,
linear_isometry.coe_to_continuous_linear_map, linear_isometry.norm_map,
continuous_multilinear_map.norm_def]
open continuous_multilinear_map
namespace multilinear_map
/-- Given a map `f : G →ₗ[𝕜] multilinear_map 𝕜 E G'` and an estimate
`H : ∀ x m, ‖f x m‖ ≤ C * ‖x‖ * ∏ i, ‖m i‖`, construct a continuous linear
map from `G` to `continuous_multilinear_map 𝕜 E G'`.
In order to lift, e.g., a map `f : (multilinear_map 𝕜 E G) →ₗ[𝕜] multilinear_map 𝕜 E' G'`
to a map `(continuous_multilinear_map 𝕜 E G) →L[𝕜] continuous_multilinear_map 𝕜 E' G'`,
one can apply this construction to `f.comp continuous_multilinear_map.to_multilinear_map_linear`
which is a linear map from `continuous_multilinear_map 𝕜 E G` to `multilinear_map 𝕜 E' G'`. -/
def mk_continuous_linear (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') (C : ℝ)
(H : ∀ x m, ‖f x m‖ ≤ C * ‖x‖ * ∏ i, ‖m i‖) :
G →L[𝕜] continuous_multilinear_map 𝕜 E G' :=
linear_map.mk_continuous
{ to_fun := λ x, (f x).mk_continuous (C * ‖x‖) $ H x,
map_add' := λ x y, by { ext1, simp only [_root_.map_add], refl },
map_smul' := λ c x, by { ext1, simp only [smul_hom_class.map_smul], refl } }
(max C 0) $ λ x, ((f x).mk_continuous_norm_le' _).trans_eq $
by rw [max_mul_of_nonneg _ _ (norm_nonneg x), zero_mul]
lemma mk_continuous_linear_norm_le' (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') (C : ℝ)
(H : ∀ x m, ‖f x m‖ ≤ C * ‖x‖ * ∏ i, ‖m i‖) :
‖mk_continuous_linear f C H‖ ≤ max C 0 :=
begin
dunfold mk_continuous_linear,
exact linear_map.mk_continuous_norm_le _ (le_max_right _ _) _
end
lemma mk_continuous_linear_norm_le (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') {C : ℝ} (hC : 0 ≤ C)
(H : ∀ x m, ‖f x m‖ ≤ C * ‖x‖ * ∏ i, ‖m i‖) :
‖mk_continuous_linear f C H‖ ≤ C :=
(mk_continuous_linear_norm_le' f C H).trans_eq (max_eq_left hC)
/-- Given a map `f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)` and an estimate
`H : ∀ m m', ‖f m m'‖ ≤ C * ∏ i, ‖m i‖ * ∏ i, ‖m' i‖`, upgrade all `multilinear_map`s in the type to
`continuous_multilinear_map`s. -/
def mk_continuous_multilinear (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) (C : ℝ)
(H : ∀ m₁ m₂, ‖f m₁ m₂‖ ≤ C * (∏ i, ‖m₁ i‖) * ∏ i, ‖m₂ i‖) :
continuous_multilinear_map 𝕜 E (continuous_multilinear_map 𝕜 E' G) :=
mk_continuous
{ to_fun := λ m, mk_continuous (f m) (C * ∏ i, ‖m i‖) $ H m,
map_add' := λ _ m i x y, by { ext1, simp },
map_smul' := λ _ m i c x, by { ext1, simp } }
(max C 0) $ λ m, ((f m).mk_continuous_norm_le' _).trans_eq $
by { rw [max_mul_of_nonneg, zero_mul], exact prod_nonneg (λ _ _, norm_nonneg _) }
@[simp] lemma mk_continuous_multilinear_apply (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G))
{C : ℝ} (H : ∀ m₁ m₂, ‖f m₁ m₂‖ ≤ C * (∏ i, ‖m₁ i‖) * ∏ i, ‖m₂ i‖) (m : Π i, E i) :
⇑(mk_continuous_multilinear f C H m) = f m :=
rfl
lemma mk_continuous_multilinear_norm_le' (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) (C : ℝ)
(H : ∀ m₁ m₂, ‖f m₁ m₂‖ ≤ C * (∏ i, ‖m₁ i‖) * ∏ i, ‖m₂ i‖) :
‖mk_continuous_multilinear f C H‖ ≤ max C 0 :=
begin
dunfold mk_continuous_multilinear,
exact mk_continuous_norm_le _ (le_max_right _ _) _
end
lemma mk_continuous_multilinear_norm_le (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) {C : ℝ}
(hC : 0 ≤ C) (H : ∀ m₁ m₂, ‖f m₁ m₂‖ ≤ C * (∏ i, ‖m₁ i‖) * ∏ i, ‖m₂ i‖) :
‖mk_continuous_multilinear f C H‖ ≤ C :=
(mk_continuous_multilinear_norm_le' f C H).trans_eq (max_eq_left hC)
end multilinear_map
namespace continuous_multilinear_map
lemma norm_comp_continuous_linear_le (g : continuous_multilinear_map 𝕜 E₁ G)
(f : Π i, E i →L[𝕜] E₁ i) :
‖g.comp_continuous_linear_map f‖ ≤ ‖g‖ * ∏ i, ‖f i‖ :=
op_norm_le_bound _ (mul_nonneg (norm_nonneg _) $ prod_nonneg $ λ i hi, norm_nonneg _) $ λ m,
calc ‖g (λ i, f i (m i))‖ ≤ ‖g‖ * ∏ i, ‖f i (m i)‖ : g.le_op_norm _
... ≤ ‖g‖ * ∏ i, (‖f i‖ * ‖m i‖) :
mul_le_mul_of_nonneg_left
(prod_le_prod (λ _ _, norm_nonneg _) (λ i hi, (f i).le_op_norm (m i))) (norm_nonneg g)
... = (‖g‖ * ∏ i, ‖f i‖) * ∏ i, ‖m i‖ : by rw [prod_mul_distrib, mul_assoc]
lemma norm_comp_continuous_linear_isometry_le (g : continuous_multilinear_map 𝕜 E₁ G)
(f : Π i, E i →ₗᵢ[𝕜] E₁ i) :
‖g.comp_continuous_linear_map (λ i, (f i).to_continuous_linear_map)‖ ≤ ‖g‖ :=
begin
apply op_norm_le_bound _ (norm_nonneg _) (λ m, _),
apply (g.le_op_norm _).trans _,
simp only [continuous_linear_map.to_linear_map_eq_coe, continuous_linear_map.coe_coe,
linear_isometry.coe_to_continuous_linear_map, linear_isometry.norm_map]
end
lemma norm_comp_continuous_linear_isometry_equiv (g : continuous_multilinear_map 𝕜 E₁ G)
(f : Π i, E i ≃ₗᵢ[𝕜] E₁ i) :
‖g.comp_continuous_linear_map (λ i, (f i : E i →L[𝕜] E₁ i))‖ = ‖g‖ :=
begin
apply le_antisymm (g.norm_comp_continuous_linear_isometry_le (λ i, (f i).to_linear_isometry)),
have : g = (g.comp_continuous_linear_map (λ i, (f i : E i →L[𝕜] E₁ i)))
.comp_continuous_linear_map (λ i, ((f i).symm : E₁ i →L[𝕜] E i)),
{ ext1 m,
simp only [comp_continuous_linear_map_apply, linear_isometry_equiv.coe_coe'',
linear_isometry_equiv.apply_symm_apply] },
conv_lhs { rw this },
apply (g.comp_continuous_linear_map (λ i, (f i : E i →L[𝕜] E₁ i)))
.norm_comp_continuous_linear_isometry_le (λ i, (f i).symm.to_linear_isometry),
end
/-- `continuous_multilinear_map.comp_continuous_linear_map` as a bundled continuous linear map.
This implementation fixes `f : Π i, E i →L[𝕜] E₁ i`.
TODO: Actually, the map is multilinear in `f` but an attempt to formalize this failed because of
issues with class instances. -/
def comp_continuous_linear_mapL (f : Π i, E i →L[𝕜] E₁ i) :
continuous_multilinear_map 𝕜 E₁ G →L[𝕜] continuous_multilinear_map 𝕜 E G :=
linear_map.mk_continuous
{ to_fun := λ g, g.comp_continuous_linear_map f,
map_add' := λ g₁ g₂, rfl,
map_smul' := λ c g, rfl }
(∏ i, ‖f i‖) $ λ g, (norm_comp_continuous_linear_le _ _).trans_eq (mul_comm _ _)
@[simp] lemma comp_continuous_linear_mapL_apply
(g : continuous_multilinear_map 𝕜 E₁ G) (f : Π i, E i →L[𝕜] E₁ i) :
comp_continuous_linear_mapL f g = g.comp_continuous_linear_map f :=
rfl
lemma norm_comp_continuous_linear_mapL_le (f : Π i, E i →L[𝕜] E₁ i) :
‖@comp_continuous_linear_mapL 𝕜 ι E E₁ G _ _ _ _ _ _ _ _ f‖ ≤ (∏ i, ‖f i‖) :=
linear_map.mk_continuous_norm_le _ (prod_nonneg $ λ i _, norm_nonneg _) _
variable (G)
/-- `continuous_multilinear_map.comp_continuous_linear_map` as a bundled continuous linear equiv,
given `f : Π i, E i ≃L[𝕜] E₁ i`. -/
def comp_continuous_linear_map_equivL (f : Π i, E i ≃L[𝕜] E₁ i) :
continuous_multilinear_map 𝕜 E₁ G ≃L[𝕜] continuous_multilinear_map 𝕜 E G :=
{ inv_fun := comp_continuous_linear_mapL (λ i, ((f i).symm : E₁ i →L[𝕜] E i)),
continuous_to_fun := (comp_continuous_linear_mapL (λ i, (f i : E i →L[𝕜] E₁ i))).continuous,
continuous_inv_fun :=
(comp_continuous_linear_mapL (λ i, ((f i).symm : E₁ i →L[𝕜] E i))).continuous,
left_inv := begin
assume g,
ext1 m,
simp only [continuous_linear_map.to_linear_map_eq_coe, linear_map.to_fun_eq_coe,
continuous_linear_map.coe_coe, comp_continuous_linear_mapL_apply,
comp_continuous_linear_map_apply, continuous_linear_equiv.coe_coe,
continuous_linear_equiv.apply_symm_apply],
end,
right_inv := begin
assume g,
ext1 m,
simp only [continuous_linear_map.to_linear_map_eq_coe, comp_continuous_linear_mapL_apply,
linear_map.to_fun_eq_coe, continuous_linear_map.coe_coe, comp_continuous_linear_map_apply,
continuous_linear_equiv.coe_coe, continuous_linear_equiv.symm_apply_apply],
end,
.. comp_continuous_linear_mapL (λ i, (f i : E i →L[𝕜] E₁ i)) }
@[simp] lemma comp_continuous_linear_map_equivL_symm (f : Π i, E i ≃L[𝕜] E₁ i) :
(comp_continuous_linear_map_equivL G f).symm =
comp_continuous_linear_map_equivL G (λ (i : ι), (f i).symm) :=
rfl
variable {G}
@[simp] lemma comp_continuous_linear_map_equivL_apply
(g : continuous_multilinear_map 𝕜 E₁ G) (f : Π i, E i ≃L[𝕜] E₁ i) :
comp_continuous_linear_map_equivL G f g =
g.comp_continuous_linear_map (λ i, (f i : E i →L[𝕜] E₁ i)) := rfl
end continuous_multilinear_map
section smul
variables {R : Type*} [semiring R] [module R G] [smul_comm_class 𝕜 R G]
[has_continuous_const_smul R G]
instance : has_continuous_const_smul R (continuous_multilinear_map 𝕜 E G) :=
⟨λ c, (continuous_linear_map.comp_continuous_multilinear_mapL 𝕜 _ G G
(c • continuous_linear_map.id 𝕜 G)).2⟩
end smul
section currying
/-!
### Currying
We associate to a continuous multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two
curried functions, named `f.curry_left` (which is a continuous linear map on `E 0` taking values
in continuous multilinear maps in `n` variables) and `f.curry_right` (which is a continuous
multilinear map in `n` variables taking values in continuous linear maps on `E (last n)`).
The inverse operations are called `uncurry_left` and `uncurry_right`.
We also register continuous linear equiv versions of these correspondences, in
`continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`.
-/
open fin function
lemma continuous_linear_map.norm_map_tail_le
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) (m : Πi, Ei i) :
‖f (m 0) (tail m)‖ ≤ ‖f‖ * ∏ i, ‖m i‖ :=
calc
‖f (m 0) (tail m)‖ ≤ ‖f (m 0)‖ * ∏ i, ‖(tail m) i‖ : (f (m 0)).le_op_norm _
... ≤ (‖f‖ * ‖m 0‖) * ∏ i, ‖(tail m) i‖ :
mul_le_mul_of_nonneg_right (f.le_op_norm _) (prod_nonneg (λi hi, norm_nonneg _))
... = ‖f‖ * (‖m 0‖ * ∏ i, ‖(tail m) i‖) : by ring
... = ‖f‖ * ∏ i, ‖m i‖ : by { rw prod_univ_succ, refl }
lemma continuous_multilinear_map.norm_map_init_le
(f : continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G))
(m : Πi, Ei i) :
‖f (init m) (m (last n))‖ ≤ ‖f‖ * ∏ i, ‖m i‖ :=
calc
‖f (init m) (m (last n))‖ ≤ ‖f (init m)‖ * ‖m (last n)‖ : (f (init m)).le_op_norm _
... ≤ (‖f‖ * (∏ i, ‖(init m) i‖)) * ‖m (last n)‖ :
mul_le_mul_of_nonneg_right (f.le_op_norm _) (norm_nonneg _)
... = ‖f‖ * ((∏ i, ‖(init m) i‖) * ‖m (last n)‖) : mul_assoc _ _ _
... = ‖f‖ * ∏ i, ‖m i‖ : by { rw prod_univ_cast_succ, refl }
lemma continuous_multilinear_map.norm_map_cons_le
(f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (m : Π(i : fin n), Ei i.succ) :
‖f (cons x m)‖ ≤ ‖f‖ * ‖x‖ * ∏ i, ‖m i‖ :=
calc
‖f (cons x m)‖ ≤ ‖f‖ * ∏ i, ‖cons x m i‖ : f.le_op_norm _
... = (‖f‖ * ‖x‖) * ∏ i, ‖m i‖ : by { rw prod_univ_succ, simp [mul_assoc] }
lemma continuous_multilinear_map.norm_map_snoc_le
(f : continuous_multilinear_map 𝕜 Ei G) (m : Π(i : fin n), Ei i.cast_succ) (x : Ei (last n)) :
‖f (snoc m x)‖ ≤ ‖f‖ * (∏ i, ‖m i‖) * ‖x‖ :=
calc
‖f (snoc m x)‖ ≤ ‖f‖ * ∏ i, ‖snoc m x i‖ : f.le_op_norm _
... = ‖f‖ * (∏ i, ‖m i‖) * ‖x‖ : by { rw prod_univ_cast_succ, simp [mul_assoc] }
/-! #### Left currying -/
/-- Given a continuous linear map `f` from `E 0` to continuous multilinear maps on `n` variables,
construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating
the variables, given by `m ↦ f (m 0) (tail m)`-/
def continuous_linear_map.uncurry_left
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) :
continuous_multilinear_map 𝕜 Ei G :=
(@linear_map.uncurry_left 𝕜 n Ei G _ _ _ _ _
(continuous_multilinear_map.to_multilinear_map_linear.comp f.to_linear_map)).mk_continuous
(‖f‖) (λm, continuous_linear_map.norm_map_tail_le f m)
@[simp] lemma continuous_linear_map.uncurry_left_apply
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) (m : Πi, Ei i) :
f.uncurry_left m = f (m 0) (tail m) := rfl
/-- Given a continuous multilinear map `f` in `n+1` variables, split the first variable to obtain
a continuous linear map into continuous multilinear maps in `n` variables, given by
`x ↦ (m ↦ f (cons x m))`. -/
def continuous_multilinear_map.curry_left
(f : continuous_multilinear_map 𝕜 Ei G) :
Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G) :=
linear_map.mk_continuous
{ -- define a linear map into `n` continuous multilinear maps from an `n+1` continuous multilinear
-- map
to_fun := λx, (f.to_multilinear_map.curry_left x).mk_continuous
(‖f‖ * ‖x‖) (f.norm_map_cons_le x),
map_add' := λx y, by { ext m, exact f.cons_add m x y },
map_smul' := λc x, by { ext m, exact f.cons_smul m c x } }
-- then register its continuity thanks to its boundedness properties.
(‖f‖) (λx, multilinear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _)
@[simp] lemma continuous_multilinear_map.curry_left_apply
(f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (m : Π(i : fin n), Ei i.succ) :
f.curry_left x m = f (cons x m) := rfl
@[simp] lemma continuous_linear_map.curry_uncurry_left
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) :
f.uncurry_left.curry_left = f :=
begin
ext m x,
simp only [tail_cons, continuous_linear_map.uncurry_left_apply,
continuous_multilinear_map.curry_left_apply],
rw cons_zero
end
@[simp] lemma continuous_multilinear_map.uncurry_curry_left
(f : continuous_multilinear_map 𝕜 Ei G) : f.curry_left.uncurry_left = f :=
continuous_multilinear_map.to_multilinear_map_injective $ f.to_multilinear_map.uncurry_curry_left
variables (𝕜 Ei G)
/-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to
the space of continuous linear maps from `E 0` to the space of continuous multilinear maps on
`Π(i : fin n), E i.succ `, by separating the first variable. We register this isomorphism in
`continuous_multilinear_curry_left_equiv 𝕜 E E₂`. The algebraic version (without topology) is given
in `multilinear_curry_left_equiv 𝕜 E E₂`.
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 isometric equivs. -/
def continuous_multilinear_curry_left_equiv :
(Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) ≃ₗᵢ[𝕜]
(continuous_multilinear_map 𝕜 Ei G) :=
linear_isometry_equiv.of_bounds
{ to_fun := continuous_linear_map.uncurry_left,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, refl },
inv_fun := continuous_multilinear_map.curry_left,
left_inv := continuous_linear_map.curry_uncurry_left,
right_inv := continuous_multilinear_map.uncurry_curry_left }
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
(λ f, linear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
variables {𝕜 Ei G}
@[simp] lemma continuous_multilinear_curry_left_equiv_apply
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.succ) G)) (v : Π i, Ei i) :
continuous_multilinear_curry_left_equiv 𝕜 Ei G f v = f (v 0) (tail v) := rfl
@[simp] lemma continuous_multilinear_curry_left_equiv_symm_apply
(f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (v : Π i : fin n, Ei i.succ) :
(continuous_multilinear_curry_left_equiv 𝕜 Ei G).symm f x v = f (cons x v) := rfl
@[simp] lemma continuous_multilinear_map.curry_left_norm
(f : continuous_multilinear_map 𝕜 Ei G) : ‖f.curry_left‖ = ‖f‖ :=
(continuous_multilinear_curry_left_equiv 𝕜 Ei G).symm.norm_map f
@[simp] lemma continuous_linear_map.uncurry_left_norm
(f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) :
‖f.uncurry_left‖ = ‖f‖ :=
(continuous_multilinear_curry_left_equiv 𝕜 Ei G).norm_map f
/-! #### Right currying -/
/-- Given a continuous linear map `f` from continuous multilinear maps on `n` variables to
continuous linear maps on `E 0`, construct the corresponding continuous multilinear map on `n+1`
variables obtained by concatenating the variables, given by `m ↦ f (init m) (m (last n))`. -/
def continuous_multilinear_map.uncurry_right
(f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) :
continuous_multilinear_map 𝕜 Ei G :=
let f' : multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →ₗ[𝕜] G) :=
{ to_fun := λ m, (f m).to_linear_map,
map_add' := λ _ m i x y, by simp,
map_smul' := λ _ m i c x, by simp } in
(@multilinear_map.uncurry_right 𝕜 n Ei G _ _ _ _ _ f').mk_continuous
(‖f‖) (λm, f.norm_map_init_le m)
@[simp] lemma continuous_multilinear_map.uncurry_right_apply
(f : continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G))
(m : Πi, Ei i) :
f.uncurry_right m = f (init m) (m (last n)) := rfl
/-- Given a continuous multilinear map `f` in `n+1` variables, split the last variable to obtain
a continuous multilinear map in `n` variables into continuous linear maps, given by
`m ↦ (x ↦ f (snoc m x))`. -/
def continuous_multilinear_map.curry_right
(f : continuous_multilinear_map 𝕜 Ei G) :
continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G) :=
let f' : multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G) :=
{ to_fun := λm, (f.to_multilinear_map.curry_right m).mk_continuous
(‖f‖ * ∏ i, ‖m i‖) $ λx, f.norm_map_snoc_le m x,
map_add' := λ _ m i x y, by { simp, refl },
map_smul' := λ _ m i c x, by { simp, refl } } in
f'.mk_continuous (‖f‖) (λm, linear_map.mk_continuous_norm_le _
(mul_nonneg (norm_nonneg _) (prod_nonneg (λj hj, norm_nonneg _))) _)
@[simp] lemma continuous_multilinear_map.curry_right_apply
(f : continuous_multilinear_map 𝕜 Ei G) (m : Π i : fin n, Ei i.cast_succ) (x : Ei (last n)) :
f.curry_right m x = f (snoc m x) := rfl
@[simp] lemma continuous_multilinear_map.curry_uncurry_right
(f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) :
f.uncurry_right.curry_right = f :=
begin
ext m x,
simp only [snoc_last, continuous_multilinear_map.curry_right_apply,
continuous_multilinear_map.uncurry_right_apply],
rw init_snoc
end
@[simp] lemma continuous_multilinear_map.uncurry_curry_right
(f : continuous_multilinear_map 𝕜 Ei G) : f.curry_right.uncurry_right = f :=
by { ext m, simp }
variables (𝕜 Ei G)
/--
The space of continuous multilinear maps on `Π(i : fin (n+1)), Ei i` is canonically isomorphic to
the space of continuous multilinear maps on `Π(i : fin n), Ei i.cast_succ` with values in the space
of continuous linear maps on `Ei (last n)`, by separating the last variable. We register this
isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv 𝕜 Ei G`.
The algebraic version (without topology) is given in `multilinear_curry_right_equiv 𝕜 Ei G`.
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 isometric equivs.
-/
def continuous_multilinear_curry_right_equiv :
(continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) ≃ₗᵢ[𝕜]
(continuous_multilinear_map 𝕜 Ei G) :=
linear_isometry_equiv.of_bounds
{ to_fun := continuous_multilinear_map.uncurry_right,
map_add' := λf₁ f₂, by { ext m, refl },
map_smul' := λc f, by { ext m, refl },
inv_fun := continuous_multilinear_map.curry_right,
left_inv := continuous_multilinear_map.curry_uncurry_right,
right_inv := continuous_multilinear_map.uncurry_curry_right }
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
variables (n G')
/-- The space of continuous multilinear maps on `Π(i : fin (n+1)), G` is canonically isomorphic to
the space of continuous multilinear maps on `Π(i : fin n), G` with values in the space
of continuous linear maps on `G`, by separating the last variable. We register this
isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv' 𝕜 n G G'`.
For a version allowing dependent types, see `continuous_multilinear_curry_right_equiv`. When there
are no dependent types, use the primed version as it helps Lean a lot for unification.
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 isometric equivs. -/
def continuous_multilinear_curry_right_equiv' :
(G [×n]→L[𝕜] (G →L[𝕜] G')) ≃ₗᵢ[𝕜] (G [×n.succ]→L[𝕜] G') :=
continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin n.succ), G) G'
variables {n 𝕜 G Ei G'}
@[simp] lemma continuous_multilinear_curry_right_equiv_apply
(f : (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)))
(v : Π i, Ei i) :
(continuous_multilinear_curry_right_equiv 𝕜 Ei G) f v = f (init v) (v (last n)) := rfl
@[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply
(f : continuous_multilinear_map 𝕜 Ei G)
(v : Π (i : fin n), Ei i.cast_succ) (x : Ei (last n)) :
(continuous_multilinear_curry_right_equiv 𝕜 Ei G).symm f v x = f (snoc v x) := rfl
@[simp] lemma continuous_multilinear_curry_right_equiv_apply'
(f : G [×n]→L[𝕜] (G →L[𝕜] G')) (v : fin (n + 1) → G) :
continuous_multilinear_curry_right_equiv' 𝕜 n G G' f v = f (init v) (v (last n)) := rfl
@[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply'
(f : G [×n.succ]→L[𝕜] G') (v : fin n → G) (x : G) :
(continuous_multilinear_curry_right_equiv' 𝕜 n G G').symm f v x = f (snoc v x) := rfl
@[simp] lemma continuous_multilinear_map.curry_right_norm
(f : continuous_multilinear_map 𝕜 Ei G) : ‖f.curry_right‖ = ‖f‖ :=
(continuous_multilinear_curry_right_equiv 𝕜 Ei G).symm.norm_map f
@[simp] lemma continuous_multilinear_map.uncurry_right_norm
(f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) :
‖f.uncurry_right‖ = ‖f‖ :=
(continuous_multilinear_curry_right_equiv 𝕜 Ei G).norm_map f
/-!
#### Currying with `0` variables
The space of multilinear maps with `0` variables is trivial: such a multilinear map is just an
arbitrary constant (note that multilinear maps in `0` variables need not map `0` to `0`!).
Therefore, the space of continuous multilinear maps on `(fin 0) → G` with values in `E₂` is
isomorphic (and even isometric) to `E₂`. As this is the zeroth step in the construction of iterated
derivatives, we register this isomorphism. -/
section
variables {𝕜 G G'}
/-- Associating to a continuous multilinear map in `0` variables the unique value it takes. -/
def continuous_multilinear_map.uncurry0
(f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) G') : G' := f 0
variables (𝕜 G)
/-- Associating to an element `x` of a vector space `E₂` the continuous multilinear map in `0`
variables taking the (unique) value `x` -/
def continuous_multilinear_map.curry0 (x : G') : G [×0]→L[𝕜] G' :=
continuous_multilinear_map.const_of_is_empty 𝕜 _ x
variable {G}
@[simp] lemma continuous_multilinear_map.curry0_apply (x : G') (m : (fin 0) → G) :
continuous_multilinear_map.curry0 𝕜 G x m = x := rfl
variable {𝕜}
@[simp] lemma continuous_multilinear_map.uncurry0_apply (f : G [×0]→L[𝕜] G') :
f.uncurry0 = f 0 := rfl
@[simp] lemma continuous_multilinear_map.apply_zero_curry0 (f : G [×0]→L[𝕜] G') {x : fin 0 → G} :
continuous_multilinear_map.curry0 𝕜 G (f x) = f :=
by { ext m, simp [(subsingleton.elim _ _ : x = m)] }
lemma continuous_multilinear_map.uncurry0_curry0 (f : G [×0]→L[𝕜] G') :
continuous_multilinear_map.curry0 𝕜 G (f.uncurry0) = f :=
by simp
variables (𝕜 G)
@[simp] lemma continuous_multilinear_map.curry0_uncurry0 (x : G') :
(continuous_multilinear_map.curry0 𝕜 G x).uncurry0 = x := rfl
@[simp] lemma continuous_multilinear_map.curry0_norm (x : G') :
‖continuous_multilinear_map.curry0 𝕜 G x‖ = ‖x‖ :=
norm_const_of_is_empty _ _ _
variables {𝕜 G}
@[simp] lemma continuous_multilinear_map.fin0_apply_norm (f : G [×0]→L[𝕜] G') {x : fin 0 → G} :
‖f x‖ = ‖f‖ :=
begin
obtain rfl : x = 0 := subsingleton.elim _ _,
refine le_antisymm (by simpa using f.le_op_norm 0) _,
have : ‖continuous_multilinear_map.curry0 𝕜 G (f.uncurry0)‖ ≤ ‖f.uncurry0‖ :=
continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm,
by simp [-continuous_multilinear_map.apply_zero_curry0]),
simpa
end
lemma continuous_multilinear_map.uncurry0_norm (f : G [×0]→L[𝕜] G') : ‖f.uncurry0‖ = ‖f‖ :=
by simp
variables (𝕜 G G')
/-- The continuous linear isomorphism between elements of a normed space, and continuous multilinear
maps in `0` variables with values in this normed space.
The direct and inverse maps are `uncurry0` and `curry0`. Use these unless you need the full
framework of linear isometric equivs. -/
def continuous_multilinear_curry_fin0 : (G [×0]→L[𝕜] G') ≃ₗᵢ[𝕜] G' :=
{ to_fun := λf, continuous_multilinear_map.uncurry0 f,
inv_fun := λf, continuous_multilinear_map.curry0 𝕜 G f,
map_add' := λf g, rfl,
map_smul' := λc f, rfl,
left_inv := continuous_multilinear_map.uncurry0_curry0,
right_inv := continuous_multilinear_map.curry0_uncurry0 𝕜 G,
norm_map' := continuous_multilinear_map.uncurry0_norm }
variables {𝕜 G G'}
@[simp] lemma continuous_multilinear_curry_fin0_apply (f : G [×0]→L[𝕜] G') :
continuous_multilinear_curry_fin0 𝕜 G G' f = f 0 := rfl
@[simp] lemma continuous_multilinear_curry_fin0_symm_apply (x : G') (v : (fin 0) → G) :
(continuous_multilinear_curry_fin0 𝕜 G G').symm x v = x := rfl
end
/-! #### With 1 variable -/
variables (𝕜 G G')
/-- Continuous multilinear maps from `G^1` to `G'` are isomorphic with continuous linear maps from
`G` to `G'`. -/
def continuous_multilinear_curry_fin1 : (G [×1]→L[𝕜] G') ≃ₗᵢ[𝕜] (G →L[𝕜] G') :=
(continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin 1), G) G').symm.trans
(continuous_multilinear_curry_fin0 𝕜 G (G →L[𝕜] G'))
variables {𝕜 G G'}
@[simp] lemma continuous_multilinear_curry_fin1_apply (f : G [×1]→L[𝕜] G') (x : G) :
continuous_multilinear_curry_fin1 𝕜 G G' f x = f (fin.snoc 0 x) := rfl
@[simp] lemma continuous_multilinear_curry_fin1_symm_apply
(f : G →L[𝕜] G') (v : (fin 1) → G) :
(continuous_multilinear_curry_fin1 𝕜 G G').symm f v = f (v 0) := rfl
namespace continuous_multilinear_map
variables (𝕜 G G')
/-- An equivalence of the index set defines a linear isometric equivalence between the spaces
of multilinear maps. -/
def dom_dom_congr (σ : ι ≃ ι') :
continuous_multilinear_map 𝕜 (λ _ : ι, G) G' ≃ₗᵢ[𝕜]
continuous_multilinear_map 𝕜 (λ _ : ι', G) G' :=
linear_isometry_equiv.of_bounds
{ to_fun := λ f, (multilinear_map.dom_dom_congr σ f.to_multilinear_map).mk_continuous ‖f‖ $
λ m, (f.le_op_norm (λ i, m (σ i))).trans_eq $ by rw [← σ.prod_comp],
inv_fun := λ f, (multilinear_map.dom_dom_congr σ.symm f.to_multilinear_map).mk_continuous ‖f‖ $
λ m, (f.le_op_norm (λ i, m (σ.symm i))).trans_eq $ by rw [← σ.symm.prod_comp],
left_inv := λ f, ext $ λ m, congr_arg f $ by simp only [σ.symm_apply_apply],
right_inv := λ f, ext $ λ m, congr_arg f $ by simp only [σ.apply_symm_apply],
map_add' := λ f g, rfl,
map_smul' := λ c f, rfl }
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
variables {𝕜 G G'}
section
/-- A continuous multilinear map with variables indexed by `ι ⊕ ι'` defines a continuous multilinear
map with variables indexed by `ι` taking values in the space of continuous multilinear maps with
variables indexed by `ι'`. -/
def curry_sum (f : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G') :
continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G') :=
multilinear_map.mk_continuous_multilinear (multilinear_map.curry_sum f.to_multilinear_map) (‖f‖) $
λ m m', by simpa [fintype.prod_sum_type, mul_assoc] using f.le_op_norm (sum.elim m m')
@[simp] lemma curry_sum_apply (f : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G')
(m : ι → G) (m' : ι' → G) :
f.curry_sum m m' = f (sum.elim m m') :=
rfl
/-- A continuous multilinear map with variables indexed by `ι` taking values in the space of
continuous multilinear maps with variables indexed by `ι'` defines a continuous multilinear map with
variables indexed by `ι ⊕ ι'`. -/
def uncurry_sum
(f : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G')) :
continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G' :=
multilinear_map.mk_continuous
(to_multilinear_map_linear.comp_multilinear_map f.to_multilinear_map).uncurry_sum (‖f‖) $ λ m,
by simpa [fintype.prod_sum_type, mul_assoc]
using (f (m ∘ sum.inl)).le_of_op_norm_le (m ∘ sum.inr) (f.le_op_norm _)
@[simp] lemma uncurry_sum_apply
(f : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G'))
(m : ι ⊕ ι' → G) :
f.uncurry_sum m = f (m ∘ sum.inl) (m ∘ sum.inr) :=
rfl
variables (𝕜 ι ι' G G')
/-- Linear isometric equivalence between the space of continuous multilinear maps with variables
indexed by `ι ⊕ ι'` and the space of continuous multilinear maps with variables indexed by `ι`
taking values in the space of continuous multilinear maps with variables indexed by `ι'`.
The forward and inverse functions are `continuous_multilinear_map.curry_sum`
and `continuous_multilinear_map.uncurry_sum`. Use this definition only if you need
some properties of `linear_isometry_equiv`. -/
def curry_sum_equiv : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G' ≃ₗᵢ[𝕜]
continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G') :=
linear_isometry_equiv.of_bounds
{ to_fun := curry_sum,
inv_fun := uncurry_sum,
map_add' := λ f g, by { ext, refl },
map_smul' := λ c f, by { ext, refl },
left_inv := λ f, by { ext m, exact congr_arg f (sum.elim_comp_inl_inr m) },
right_inv := λ f, by { ext m₁ m₂, change f _ _ = f _ _,
rw [sum.elim_comp_inl, sum.elim_comp_inr] } }
(λ f, multilinear_map.mk_continuous_multilinear_norm_le _ (norm_nonneg f) _)
(λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _)
end
section
variables (𝕜 G G') {k l : ℕ} {s : finset (fin n)}
/-- If `s : finset (fin n)` is a finite set of cardinality `k` and its complement has cardinality
`l`, then the space of continuous multilinear maps `G [×n]→L[𝕜] G'` of `n` variables is isomorphic
to the space of continuous multilinear maps `G [×k]→L[𝕜] G [×l]→L[𝕜] G'` of `k` variables taking
values in the space of continuous multilinear maps of `l` variables. -/
def curry_fin_finset {k l n : ℕ} {s : finset (fin n)}
(hk : s.card = k) (hl : sᶜ.card = l) :
(G [×n]→L[𝕜] G') ≃ₗᵢ[𝕜] (G [×k]→L[𝕜] G [×l]→L[𝕜] G') :=
(dom_dom_congr 𝕜 G G' (fin_sum_equiv_of_finset hk hl).symm).trans
(curry_sum_equiv 𝕜 (fin k) (fin l) G G')
variables {𝕜 G G'}
@[simp] lemma curry_fin_finset_apply (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×n]→L[𝕜] G') (mk : fin k → G) (ml : fin l → G) :
curry_fin_finset 𝕜 G G' 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 (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (m : fin n → G) :
(curry_fin_finset 𝕜 G G' 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 (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (x y : G) :
(curry_fin_finset 𝕜 G G' hk hl).symm f (s.piecewise (λ _, x) (λ _, y)) = f (λ _, x) (λ _, y) :=
multilinear_map.curry_fin_finset_symm_apply_piecewise_const hk hl _ x y
@[simp] lemma curry_fin_finset_symm_apply_const (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (x : G) :
(curry_fin_finset 𝕜 G G' hk hl).symm f (λ _, x) = f (λ _, x) (λ _, x) :=
rfl
@[simp] lemma curry_fin_finset_apply_const (hk : s.card = k) (hl : sᶜ.card = l)
(f : G [×n]→L[𝕜] G') (x y : G) :
curry_fin_finset 𝕜 G G' 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_isometry_equiv.symm_apply_apply
end
end
end continuous_multilinear_map
end currying
|
449620dba9b451206c3b7964d6f6bea0eae13fe7 | 2d34dfb0a1cc250584282618dc10ea03d3fa858e | /src/for_mathlib/tsum.lean | 118fa233fa1d974d4d8a0abc59a2b1295f4b8d4e | [] | no_license | zeta1999/lean-liquid | 61e294ec5adae959d8ee1b65d015775484ff58c2 | 96bb0fa3afc3b451bcd1fb7d974348de2f290541 | refs/heads/master | 1,676,579,150,248 | 1,610,771,445,000 | 1,610,771,445,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,024 | lean | import topology.algebra.infinite_sum
import topology.instances.nnreal
open_locale big_operators nnreal
lemma tsum_abs_eq_coe_tsum_nnabs {α : Type*} (f : α → ℝ) :
(∑' i, abs (f i)) = ∑' i, real.nnabs (f i) :=
by simp only [nnreal.coe_nnabs]
open nnreal finset
lemma has_sum_nat_add_iff'' {f : ℕ → ℝ≥0} (k : ℕ) {a : ℝ≥0} :
has_sum (λ n, f (n + k)) a ↔ has_sum f (a + ∑ i in range k, f i) :=
begin
unfold has_sum,
rw ← tendsto_coe,
rw ← tendsto_coe,
simp only [coe_sum],
convert has_sum_nat_add_iff k,
refl,
classical,
rw nnreal.coe_add,
simp only [coe_sum],
apply_instance,
end
.
lemma sum_add_tsum_nat_add' {f : ℕ → ℝ≥0} (k : ℕ) (h : summable f) :
(∑ i in range k, f i) + (∑' i, f (i + k)) = (∑' i, f i) :=
by simpa [add_comm] using
((has_sum_nat_add_iff'' k).1 ((nnreal.summable_nat_add_iff k).2 h).has_sum).unique h.has_sum
lemma tsum_eq_zero {ι} (f : ι → ℝ≥0) (h : ∀ b, f b = 0) : (∑' b, f b) = 0 :=
by simp only [h, tsum_zero]
|
226574adda878094e88f09b3b49c5001cda98cdf | 4e0d7c3132ce31edc5829849735dd25db406b144 | /lean/love06_monads_demo.lean | e8fd27999ef80440f5110eb4f86a9bf1dd3828c1 | [] | no_license | gonzalgu/logical_verification_2020 | a0013a6c22ea254e9f4d245f2948f0f4d44df4bb | 724d0457dff2c3ff10f9ab2170388f4c5e958b75 | refs/heads/master | 1,660,886,374,533 | 1,589,859,641,000 | 1,589,859,641,000 | 256,069,971 | 0 | 0 | null | 1,586,997,430,000 | 1,586,997,429,000 | null | UTF-8 | Lean | false | false | 11,543 | lean | import .lovelib
/-! # LoVe Demo 6: Monads
We take a look at an important functional programming abstraction: monads.
Monads generalize computation with side effects. Haskell has shown that monads
can be used very successful to write imperative programs. For us, they are
interesting in three ways:
* They are a useful concept in their own right.
* They provide a nice example of axiomatic reasoning.
* They are useful for programming Lean itself (metaprogramming). -/
set_option pp.beta true
namespace LoVe
/-! ## Introductory Example
Consider the following programming task:
Implement a function `sum_2_5_7 ns` that sums up the second, fifth, and
seventh items of a list `ns` of natural numbers. Use `option ℕ` for the
result so that if the list has fewer than seven elements, you can return
`option.none`.
A straightforward solution follows: -/
def sum_2_5_7 (ns : list ℕ) : option ℕ :=
match list.nth ns 1 with
| option.none := option.none
| option.some n2 :=
match list.nth ns 4 with
| option.none := option.none
| option.some n5 :=
match list.nth ns 6 with
| option.none := option.none
| option.some n7 := option.some (n2 + n5 + n7)
end
end
end
/-! The code is ugly, because of all the pattern matching on options.
We can put all the ugliness in one function, which we call `bind_opt`: -/
def bind_opt {α : Type} {β : Type} :
option α → (α → option β) → option β
| option.none f := option.none
| (option.some a) f := f a
def sum_2_5_7₂ (ns : list ℕ) : option ℕ :=
bind_opt (list.nth ns 1)
(λn2, bind_opt (list.nth ns 4)
(λn5, bind_opt (list.nth ns 6)
(λn7, option.some (n2 + n5 + n7))))
/-! Instead of defining `bind_opt` ourselves, we can use Lean's predefined
general `bind` operation. We can also use `pure` instead of `option.some`: -/
#check bind
def sum_2_5_7₃ (ns : list ℕ) : option ℕ :=
bind (list.nth ns 1)
(λn2, bind (list.nth ns 4)
(λn5, bind (list.nth ns 6)
(λn7, pure (n2 + n5 + n7))))
/-! Syntactic sugar:
`ma >>= f` := `bind ma f` -/
#check (>>=)
def sum_2_5_7₄ (ns : list ℕ) : option ℕ :=
list.nth ns 1 >>=
λn2, list.nth ns 4 >>=
λn5, list.nth ns 6 >>=
λn7, pure (n2 + n5 + n7)
/-! Syntactic sugar:
`do a ← ma, t` := `ma >>= (λa, t)`
`do ma, t` := `ma >>= (λ_, t)` -/
def sum_2_5_7₅ (ns : list ℕ) : option ℕ :=
do n2 ← list.nth ns 1,
do n5 ← list.nth ns 4,
do n7 ← list.nth ns 6,
pure (n2 + n5 + n7)
/-! The `do`s can be combined: -/
def sum_2_5_7₆ (ns : list ℕ) : option ℕ :=
do
n2 ← list.nth ns 1,
n5 ← list.nth ns 4,
n7 ← list.nth ns 6,
pure (n2 + n5 + n7)
/-! Although the notation has an imperative flavor, the function is a pure
functional program.
## Two Operations and Three Laws
The `option` type constructor is an example of a monad.
In general, a __monad__ is a type constructor `m` that depends on some type
parameter `α` (i.e., `m α`) equipped with two distinguished operations:
`pure {α : Type} : α → m α`
`bind {α β : Type} : m α → (α → m β) → m β`
For `option`:
`pure` := `option.some`
`bind` := `bind_opt`
Intuitively, we can think of a monad as a "box":
* `pure` puts the data into the box.
* `bind` allows us to access the data in the box and modify it (possibly even
changing its type, since the result is an `m β` monad, not a `m α` monad).
There is no general way to extract the data from the monad, i.e., to obtain an
`α` from an `m α`.
To summarize, `pure a` provides no side effect and simply provides a box
containing the the value `a`, whereas `bind ma f` (also written `ma >>= f`)
executes `ma`, then executes `f` with the boxed result `a` of `ma`.
The option monad is only one instance among many.
Type | Effect
------------ | --------------------------------------------------------------
`id α` | no effect
`option α` | simple exceptions
`σ → α × σ` | threading through a state of type `σ`
`set α` | nondeterministic computation returning `α` values
`t → α` | reading elements of type `t` (e.g., a configuration)
`ℕ × α` | adjoining running time (e.g., to model algorithmic complexity)
`string × α` | adjoining text output (e.g., for logging)
`prob α` | probability (e.g., using random number generators)
`io α` | interaction with the operating system
`tactic α` | interaction with the proof assistant
All of the above are type constructors `m` are parameterized by a type `α`.
Some effects can be combined (e.g., `option (t → α)`).
Some effects are not executable (e.g., `set α`, `prob α`). They are nonetheless
useful for modeling programs abstractly in the logic.
Specific monads may provide a way to extract the boxed value stored in the monad
without `bind`'s requirement of putting it back in a monad.
Monads have several benefits, including:
* They provide the convenient and highly readable `do` notation.
* They support generic operations, such as
`mmap {α β : Type} : (α → m β) → list α → m (list β)`, which work uniformly
across all monads.
The `bind` and `pure` operations are normally required to obey three laws,
called the monad laws. Pure data as the first program can be simplified away:
do
a' ← pure a,
f a'
=
f a
Pure data as the second program can be simplified away:
do
a ← x,
pure a
=
x
Nested programs `x`, `f`, `g` can be linearized using this associativity rule:
do
b ← do {
a ← x,
f a },
g b
=
do
a ← x,
b ← f a,
g b
## A Type Class of Monads
Monads are a mathematical structure, so we use class to add them as a type class
(lecture 12). We can think of a type class as a structure that is parameterized
by a type—or here, by a type constructor `m : Type → Type`. -/
@[class] structure lawful_monad (m : Type → Type)
extends has_bind m, has_pure m : Type 1 :=
(pure_bind {α β : Type} (a : α) (f : α → m β) :
(pure a >>= f) = f a)
(bind_pure {α : Type} (ma : m α) :
(ma >>= pure) = ma)
(bind_assoc {α β γ : Type} (f : α → m β) (g : β → m γ)
(ma : m α) :
((ma >>= f) >>= g) = (ma >>= (λa, f a >>= g)))
#print monad
#print is_lawful_monad
/-! Step by step:
* We are creating a structure parameterized by a unary type constructor `m`.
* The structure inherits the fields, and any syntactic sugar, from structures
called `has_bind` and `has_pure`, which provide the `bind` and `pure`
operations on `m` and some syntactic sugar.
* `Type 1` is necessary for reasons that will become clear in lecture 11.
* The definition adds three fields to those already provided by `has_bind` and
`has_pure`, to store the proofs of the monad laws.
To instantiate this definition with a concrete monad, we must supply the type
constructor `m` (e.g., `option`), `bind` and `pure` operators, and
proofs of the monad laws.
(Lean's actual definition of monads is more complicated.)
## Identity -/
def id.pure {α : Type} : α → id α :=
id
def id.bind {α β : Type} : id α → (α → id β) → id β
| a f := f a
@[instance] def id.lawful_monad : lawful_monad (@id Type) :=
{ pure := @id.pure,
bind := @id.bind,
pure_bind :=
begin
intros α β a f,
refl
end,
bind_pure :=
begin
intros α m,
refl
end,
bind_assoc :=
begin
intros α β γ f g m,
refl
end }
/-! ## Exceptions -/
def option.pure {α : Type} : α → option α :=
option.some
def option.bind {α β : Type} :
option α → (α → option β) → option β
| option.none f := option.none
| (option.some a) f := f a
@[instance] def option.lawful_monad : lawful_monad option :=
{ pure := @option.pure,
bind := @option.bind,
pure_bind :=
begin
intros α β a f,
refl
end,
bind_pure :=
begin
intros α m,
cases m,
{ refl },
{ refl }
end,
bind_assoc :=
begin
intros α β γ f g m,
cases m,
{ refl },
{ refl }
end }
def option.throw {α : Type} : option α :=
option.none
def option.catch {α : Type} :
option α → option α → option α
| option.none ma' := ma'
| (option.some a) _ := option.some a
@[instance] def option.has_orelse : has_orelse option :=
{ orelse := @option.catch }
/-! ## Mutable State -/
def action (σ α : Type) :=
σ → α × σ
def action.read {σ : Type} : action σ σ
| s := (s, s)
def action.write {σ : Type} (s : σ) : action σ unit
| _ := ((), s)
def action.pure {σ α : Type} (a : α) : action σ α
| s := (a, s)
def action.bind {σ : Type} {α β : Type} (ma : action σ α)
(f : α → action σ β) :
action σ β
| s :=
match ma s with
| (a, s') := f a s'
end
@[instance] def action.lawful_monad {σ : Type} :
lawful_monad (action σ) :=
{ pure := @action.pure σ,
bind := @action.bind σ,
pure_bind :=
begin
intros α β a f,
apply funext,
intro s,
refl
end,
bind_pure :=
begin
intros α m,
apply funext,
intro s,
simp [action.bind],
cases m s,
refl
end,
bind_assoc :=
begin
intros α β γ f g m,
apply funext,
intro s,
simp [action.bind],
cases m s,
refl
end }
def diff_list : list ℕ → action ℕ (list ℕ)
| [] := pure []
| (n :: ns) :=
do
prev ← action.read,
if n < prev then
diff_list ns
else
do
action.write n,
ns' ← diff_list ns,
pure (n :: ns')
#eval diff_list [1, 2, 3, 2] 0
#eval diff_list [1, 2, 3, 2, 4, 5, 2] 0
/-! ## Nondeterminism -/
#check set
def set.pure {α : Type} : α → set α
| a := {a}
def set.bind {α β : Type} : set α → (α → set β) → set β
| A f := {b | ∃a, a ∈ A ∧ b ∈ f a}
@[instance] def set.lawful_monad : lawful_monad set :=
{ pure := @set.pure,
bind := @set.bind,
pure_bind :=
begin
intros α β a f,
simp [set.pure, set.bind]
end,
bind_pure :=
begin
intros α m,
simp [set.pure, set.bind]
end,
bind_assoc :=
begin
intros α β γ f g m,
simp [set.pure, set.bind],
apply set.ext,
simp,
tautology
end }
/-! `tautology` performs elimination of the logical symbols `∧`, `∨`, `↔`, and
`∃` in hypotheses and introduction of `∧`, `↔`, and `∃` in the conclusion, until
all the emerging subgoals can be trivially proved (e.g., by `refl`).
## A Generic Algorithm: Iteration over a List -/
def mmap {m : Type → Type} [lawful_monad m] {α β : Type}
(f : α → m β) :
list α → m (list β)
| [] := pure []
| (a :: as) :=
do
b ← f a,
bs ← mmap as,
pure (b :: bs)
lemma mmap_append {m : Type → Type} [lawful_monad m]
{α β : Type} (f : α → m β) :
∀as as' : list α, mmap f (as ++ as') =
do
bs ← mmap f as,
bs' ← mmap f as',
pure (bs ++ bs')
| [] _ :=
by simp [mmap, lawful_monad.bind_pure, lawful_monad.pure_bind]
| (a :: as) as' :=
by simp [mmap, mmap_append as as', lawful_monad.pure_bind,
lawful_monad.bind_assoc]
def nths {α : Type} (xss : list (list α)) (n : ℕ) :
option (list α) :=
mmap (λxs, list.nth xs n) xss
#eval nths
[[11, 12, 13, 14],
[21, 22, 23],
[31, 32, 33]] 2
end LoVe
|
d552ac502e1bf359f18f7a18e46098fb8c632cfc | 592ee40978ac7604005a4e0d35bbc4b467389241 | /Library/generated/mathscheme-lean/CommutativePointedMagma.lean | 88c4e8ed2cdbe9e3b9e164fc0a73092dcd021063 | [] | no_license | ysharoda/Deriving-Definitions | 3e149e6641fae440badd35ac110a0bd705a49ad2 | dfecb27572022de3d4aa702cae8db19957523a59 | refs/heads/master | 1,679,127,857,700 | 1,615,939,007,000 | 1,615,939,007,000 | 229,785,731 | 4 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,468 | lean | import init.data.nat.basic
import init.data.fin.basic
import data.vector
import .Prelude
open Staged
open nat
open fin
open vector
section CommutativePointedMagma
structure CommutativePointedMagma (A : Type) : Type :=
(op : (A → (A → A)))
(e : A)
(commutative_op : (∀ {x y : A} , (op x y) = (op y x)))
open CommutativePointedMagma
structure Sig (AS : Type) : Type :=
(opS : (AS → (AS → AS)))
(eS : AS)
structure Product (A : Type) : Type :=
(opP : ((Prod A A) → ((Prod A A) → (Prod A A))))
(eP : (Prod A A))
(commutative_opP : (∀ {xP yP : (Prod A A)} , (opP xP yP) = (opP yP xP)))
structure Hom {A1 : Type} {A2 : Type} (Co1 : (CommutativePointedMagma A1)) (Co2 : (CommutativePointedMagma A2)) : Type :=
(hom : (A1 → A2))
(pres_op : (∀ {x1 x2 : A1} , (hom ((op Co1) x1 x2)) = ((op Co2) (hom x1) (hom x2))))
(pres_e : (hom (e Co1)) = (e Co2))
structure RelInterp {A1 : Type} {A2 : Type} (Co1 : (CommutativePointedMagma A1)) (Co2 : (CommutativePointedMagma A2)) : Type 1 :=
(interp : (A1 → (A2 → Type)))
(interp_op : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((op Co1) x1 x2) ((op Co2) y1 y2))))))
(interp_e : (interp (e Co1) (e Co2)))
inductive CommutativePointedMagmaTerm : Type
| opL : (CommutativePointedMagmaTerm → (CommutativePointedMagmaTerm → CommutativePointedMagmaTerm))
| eL : CommutativePointedMagmaTerm
open CommutativePointedMagmaTerm
inductive ClCommutativePointedMagmaTerm (A : Type) : Type
| sing : (A → ClCommutativePointedMagmaTerm)
| opCl : (ClCommutativePointedMagmaTerm → (ClCommutativePointedMagmaTerm → ClCommutativePointedMagmaTerm))
| eCl : ClCommutativePointedMagmaTerm
open ClCommutativePointedMagmaTerm
inductive OpCommutativePointedMagmaTerm (n : ℕ) : Type
| v : ((fin n) → OpCommutativePointedMagmaTerm)
| opOL : (OpCommutativePointedMagmaTerm → (OpCommutativePointedMagmaTerm → OpCommutativePointedMagmaTerm))
| eOL : OpCommutativePointedMagmaTerm
open OpCommutativePointedMagmaTerm
inductive OpCommutativePointedMagmaTerm2 (n : ℕ) (A : Type) : Type
| v2 : ((fin n) → OpCommutativePointedMagmaTerm2)
| sing2 : (A → OpCommutativePointedMagmaTerm2)
| opOL2 : (OpCommutativePointedMagmaTerm2 → (OpCommutativePointedMagmaTerm2 → OpCommutativePointedMagmaTerm2))
| eOL2 : OpCommutativePointedMagmaTerm2
open OpCommutativePointedMagmaTerm2
def simplifyCl {A : Type} : ((ClCommutativePointedMagmaTerm A) → (ClCommutativePointedMagmaTerm A))
| (opCl x1 x2) := (opCl (simplifyCl x1) (simplifyCl x2))
| eCl := eCl
| (sing x1) := (sing x1)
def simplifyOpB {n : ℕ} : ((OpCommutativePointedMagmaTerm n) → (OpCommutativePointedMagmaTerm n))
| (opOL x1 x2) := (opOL (simplifyOpB x1) (simplifyOpB x2))
| eOL := eOL
| (v x1) := (v x1)
def simplifyOp {n : ℕ} {A : Type} : ((OpCommutativePointedMagmaTerm2 n A) → (OpCommutativePointedMagmaTerm2 n A))
| (opOL2 x1 x2) := (opOL2 (simplifyOp x1) (simplifyOp x2))
| eOL2 := eOL2
| (v2 x1) := (v2 x1)
| (sing2 x1) := (sing2 x1)
def evalB {A : Type} : ((CommutativePointedMagma A) → (CommutativePointedMagmaTerm → A))
| Co (opL x1 x2) := ((op Co) (evalB Co x1) (evalB Co x2))
| Co eL := (e Co)
def evalCl {A : Type} : ((CommutativePointedMagma A) → ((ClCommutativePointedMagmaTerm A) → A))
| Co (sing x1) := x1
| Co (opCl x1 x2) := ((op Co) (evalCl Co x1) (evalCl Co x2))
| Co eCl := (e Co)
def evalOpB {A : Type} {n : ℕ} : ((CommutativePointedMagma A) → ((vector A n) → ((OpCommutativePointedMagmaTerm n) → A)))
| Co vars (v x1) := (nth vars x1)
| Co vars (opOL x1 x2) := ((op Co) (evalOpB Co vars x1) (evalOpB Co vars x2))
| Co vars eOL := (e Co)
def evalOp {A : Type} {n : ℕ} : ((CommutativePointedMagma A) → ((vector A n) → ((OpCommutativePointedMagmaTerm2 n A) → A)))
| Co vars (v2 x1) := (nth vars x1)
| Co vars (sing2 x1) := x1
| Co vars (opOL2 x1 x2) := ((op Co) (evalOp Co vars x1) (evalOp Co vars x2))
| Co vars eOL2 := (e Co)
def inductionB {P : (CommutativePointedMagmaTerm → Type)} : ((∀ (x1 x2 : CommutativePointedMagmaTerm) , ((P x1) → ((P x2) → (P (opL x1 x2))))) → ((P eL) → (∀ (x : CommutativePointedMagmaTerm) , (P x))))
| popl pel (opL x1 x2) := (popl _ _ (inductionB popl pel x1) (inductionB popl pel x2))
| popl pel eL := pel
def inductionCl {A : Type} {P : ((ClCommutativePointedMagmaTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClCommutativePointedMagmaTerm A)) , ((P x1) → ((P x2) → (P (opCl x1 x2))))) → ((P eCl) → (∀ (x : (ClCommutativePointedMagmaTerm A)) , (P x)))))
| psing popcl pecl (sing x1) := (psing x1)
| psing popcl pecl (opCl x1 x2) := (popcl _ _ (inductionCl psing popcl pecl x1) (inductionCl psing popcl pecl x2))
| psing popcl pecl eCl := pecl
def inductionOpB {n : ℕ} {P : ((OpCommutativePointedMagmaTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpCommutativePointedMagmaTerm n)) , ((P x1) → ((P x2) → (P (opOL x1 x2))))) → ((P eOL) → (∀ (x : (OpCommutativePointedMagmaTerm n)) , (P x)))))
| pv popol peol (v x1) := (pv x1)
| pv popol peol (opOL x1 x2) := (popol _ _ (inductionOpB pv popol peol x1) (inductionOpB pv popol peol x2))
| pv popol peol eOL := peol
def inductionOp {n : ℕ} {A : Type} {P : ((OpCommutativePointedMagmaTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpCommutativePointedMagmaTerm2 n A)) , ((P x1) → ((P x2) → (P (opOL2 x1 x2))))) → ((P eOL2) → (∀ (x : (OpCommutativePointedMagmaTerm2 n A)) , (P x))))))
| pv2 psing2 popol2 peol2 (v2 x1) := (pv2 x1)
| pv2 psing2 popol2 peol2 (sing2 x1) := (psing2 x1)
| pv2 psing2 popol2 peol2 (opOL2 x1 x2) := (popol2 _ _ (inductionOp pv2 psing2 popol2 peol2 x1) (inductionOp pv2 psing2 popol2 peol2 x2))
| pv2 psing2 popol2 peol2 eOL2 := peol2
def stageB : (CommutativePointedMagmaTerm → (Staged CommutativePointedMagmaTerm))
| (opL x1 x2) := (stage2 opL (codeLift2 opL) (stageB x1) (stageB x2))
| eL := (Now eL)
def stageCl {A : Type} : ((ClCommutativePointedMagmaTerm A) → (Staged (ClCommutativePointedMagmaTerm A)))
| (sing x1) := (Now (sing x1))
| (opCl x1 x2) := (stage2 opCl (codeLift2 opCl) (stageCl x1) (stageCl x2))
| eCl := (Now eCl)
def stageOpB {n : ℕ} : ((OpCommutativePointedMagmaTerm n) → (Staged (OpCommutativePointedMagmaTerm n)))
| (v x1) := (const (code (v x1)))
| (opOL x1 x2) := (stage2 opOL (codeLift2 opOL) (stageOpB x1) (stageOpB x2))
| eOL := (Now eOL)
def stageOp {n : ℕ} {A : Type} : ((OpCommutativePointedMagmaTerm2 n A) → (Staged (OpCommutativePointedMagmaTerm2 n A)))
| (sing2 x1) := (Now (sing2 x1))
| (v2 x1) := (const (code (v2 x1)))
| (opOL2 x1 x2) := (stage2 opOL2 (codeLift2 opOL2) (stageOp x1) (stageOp x2))
| eOL2 := (Now eOL2)
structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type :=
(opT : ((Repr A) → ((Repr A) → (Repr A))))
(eT : (Repr A))
end CommutativePointedMagma |
7e1e6b186a3147c5f9cc629e141e94ec9bd13392 | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch4/ex0208.lean | 653b1009da54d740835230de9dfccd15b88a524f | [] | 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 | 229 | lean | variable α : Type
variables a b : α
variables f g : α → ℕ
variable h₁ : a = b
variable h₂ : f = g
example : f a = f b := congr_arg f h₁
example : f a = g a := congr_fun h₂ a
example : f a = g b := congr h₂ h₁
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.