chars int64 40 24k | content_type stringclasses 9
values | id stringlengths 13 64 | source stringclasses 5
values | subset stringclasses 31
values | text stringlengths 40 24k |
|---|---|---|---|---|---|
192 | leanexplore_declaration | leanexplore:387016 | leanexplore_sqlite | leanexplore_declarations | /-- `toContT` is the unique interpreter extending `contInterp`. -/
theorem toContT_unique {α : Type u} (g : FreeCont r α → ContT r Id α)
(h : Interprets contInterp g) : g = toContT := h.eq |
190 | leanexplore_declaration | leanexplore:387017 | leanexplore_sqlite | leanexplore_declarations | /-- Run a continuation computation with the given continuation. -/
def run : FreeCont r α → (α → r) → r
| .pure a, k => k a
| .liftBind (.callCC g) cont, k => g (fun a => run (cont a) k) |
452 | leanexplore_declaration | leanexplore:387018 | leanexplore_sqlite | leanexplore_declarations | /--
The canonical interpreter `toContT` derived from `liftM` agrees with the hand-written
recursive interpreter `run` for `FreeCont`.
-/
@[simp]
theorem run_toContT {α : Type u} (comp : FreeCont r α) (k : α → r) :
(toContT comp).run k = pure (run comp k) := by
induction comp with
| pure a => rfl
| liftBind op... |
92 | leanexplore_declaration | leanexplore:387019 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma run_pure (a : α) (k : α → r) :
run (.pure a : FreeCont r α) k = k a := rfl |
276 | leanexplore_declaration | leanexplore:387020 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma run_bind (x : FreeCont r α) (f : α → FreeCont r β) (k : β → r) :
run (x.bind f) k = run x (fun i => run (f i) k) := by
induction x generalizing k with
| pure a => rfl
| liftBind op cont ih =>
rw [FreeM.liftBind_bind]
cases op
simp [run, ih] |
170 | leanexplore_declaration | leanexplore:387021 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma run_liftBind_callCC (g : (α → r) → r)
(cont : α → FreeCont r β) (k : β → r) :
run (liftBind (.callCC g) cont) k = g (fun a => run (cont a) k) := rfl |
250 | leanexplore_declaration | leanexplore:387022 | leanexplore_sqlite | leanexplore_declarations | /-- Call with current continuation for the Free continuation monad. -/
def callCC (f : MonadCont.Label α (FreeCont r) β → FreeCont r α) :
FreeCont r α :=
liftBind (.callCC fun k => run (f ⟨fun x => liftBind (.callCC fun _ => k x) pure⟩) k) pure |
201 | leanexplore_declaration | leanexplore:387023 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma callCC_def (f : MonadCont.Label α (FreeCont r) β → FreeCont r α) :
callCC f =
liftBind (.callCC fun k => run (f ⟨fun x => liftBind (.callCC fun _ => k x) pure⟩) k) pure :=
rfl |
59 | leanexplore_declaration | leanexplore:387024 | leanexplore_sqlite | leanexplore_declarations | instance : MonadCont (FreeCont r) where
callCC := .callCC |
313 | leanexplore_declaration | leanexplore:387025 | leanexplore_sqlite | leanexplore_declarations | /-- `run` of a `callCC` node simplifies to running the handler with the current continuation. -/
@[simp]
lemma run_callCC (f : MonadCont.Label α (FreeCont r) β → FreeCont r α) (k : α → r) :
run (callCC f) k = run (f ⟨fun x => liftBind (.callCC fun _ => k x) pure⟩) k := by
simp [callCC, run_liftBind_callCC] |
124 | leanexplore_declaration | leanexplore:387026 | leanexplore_sqlite | leanexplore_declarations | /-- Type constructor for reader operations. -/
inductive ReaderF (σ : Type u) : Type u → Type u where
| read : ReaderF σ σ |
84 | leanexplore_declaration | leanexplore:387028 | leanexplore_sqlite | leanexplore_declarations | /-- Reader monad via the `FreeM` monad -/
abbrev FreeReader (σ) := FreeM (ReaderF σ) |
69 | leanexplore_declaration | leanexplore:387029 | leanexplore_sqlite | leanexplore_declarations | instance : MonadReaderOf σ (FreeReader σ) where
read := .lift .read |
69 | leanexplore_declaration | leanexplore:387030 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma read_def : (read : FreeReader σ σ) = .lift .read := rfl |
56 | leanexplore_declaration | leanexplore:387031 | leanexplore_sqlite | leanexplore_declarations | instance : MonadReader σ (FreeReader σ) := inferInstance |
141 | leanexplore_declaration | leanexplore:387032 | leanexplore_sqlite | leanexplore_declarations | /-- Interpret `ReaderF` operations into `ReaderM`. -/
def readInterp {α : Type u} : ReaderF σ α → ReaderM σ α
| .read => MonadReaderOf.read |
219 | leanexplore_declaration | leanexplore:387033 | leanexplore_sqlite | leanexplore_declarations | /-- Convert a `FreeReader` computation into a `ReaderM` computation. This is the canonical
interpreter derived from `liftM`. -/
def toReaderM {α : Type u} (comp : FreeReader σ α) : ReaderM σ α :=
comp.liftM readInterp |
199 | leanexplore_declaration | leanexplore:387034 | leanexplore_sqlite | leanexplore_declarations | /-- `toReaderM` is the unique interpreter extending `readInterp`. -/
theorem toReaderM_unique {α : Type u} (g : FreeReader σ α → ReaderM σ α)
(h : Interprets readInterp g) : g = toReaderM := h.eq |
160 | leanexplore_declaration | leanexplore:387035 | leanexplore_sqlite | leanexplore_declarations | /-- Run a reader computation -/
def run (comp : FreeReader σ α) (s₀ : σ) : α :=
match comp with
| .pure a => a
| .liftBind ReaderF.read a => run (a s₀) s₀ |
374 | leanexplore_declaration | leanexplore:387036 | leanexplore_sqlite | leanexplore_declarations | /--
The canonical interpreter `toReaderM` derived from `liftM` agrees with the hand-written
recursive interpreter `run` for `FreeReader` -/
@[simp]
theorem run_toReaderM {α : Type u} (comp : FreeReader σ α) (s : σ) :
(toReaderM comp).run s = pure (run comp s) := by
induction comp generalizing s with
| pure a =>... |
90 | leanexplore_declaration | leanexplore:387037 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma run_pure (a : α) (s₀ : σ) :
run (.pure a : FreeReader σ α) s₀ = a := rfl |
111 | leanexplore_declaration | leanexplore:387038 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma run_read (k : σ → FreeReader σ α) (s₀ : σ) :
run (liftBind .read k) s₀ = run (k s₀) s₀ := rfl |
235 | leanexplore_declaration | leanexplore:387039 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma run_bind (x : FreeReader σ α) (f : α → FreeReader σ β) (s₀ : σ) :
run (x.bind f) s₀ = run (f <| run x s₀) s₀ := by
induction x generalizing s₀ with
| pure a => rfl
| liftBind op cont ih =>
cases op; apply ih |
265 | leanexplore_declaration | leanexplore:387040 | leanexplore_sqlite | leanexplore_declarations | instance instMonadWithReaderOf : MonadWithReaderOf σ (FreeReader σ) where
withReader {α} f m :=
let rec go : FreeReader σ α → FreeReader σ α
| .pure a => .pure a
| .liftBind .read cont =>
.liftBind .read fun s => go (cont (f s))
go m |
154 | leanexplore_declaration | leanexplore:387041 | leanexplore_sqlite | leanexplore_declarations | let rec go : FreeReader σ α → FreeReader σ α
| .pure a => .pure a
| .liftBind .read cont =>
.liftBind .read fun s => go (cont (f s)) |
290 | leanexplore_declaration | leanexplore:387042 | leanexplore_sqlite | leanexplore_declarations | @[simp] theorem run_withReader (f : σ → σ) (m : FreeReader σ α) (s : σ) :
run (withTheReader σ f m) s = run m (f s) := by
induction m generalizing s with
| pure a => rfl
| liftBind op cont ih =>
cases op
simpa [withTheReader, instMonadWithReaderOf, run] using (ih (f s) s) |
251 | leanexplore_declaration | leanexplore:387043 | leanexplore_sqlite | leanexplore_declarations | /-- Fold function for the `FreeM` monad -/
def foldFreeM
(onValue : α → β)
(onEffect : {ι : Type u} → F ι → (ι → β) → β) :
FreeM F α → β
| .pure a => onValue a
| .liftBind op k => onEffect op (fun x => foldFreeM onValue onEffect (k x)) |
172 | leanexplore_declaration | leanexplore:387044 | leanexplore_sqlite | leanexplore_declarations | @[simp]
theorem foldFreeM_pure
(onValue : α → β)
(onEffect : {ι : Type u} → F ι → (ι → β) → β)
(a : α) : foldFreeM onValue onEffect (.pure a) = onValue a := rfl |
262 | leanexplore_declaration | leanexplore:387045 | leanexplore_sqlite | leanexplore_declarations | @[simp]
theorem foldFreeM_liftBind
(onValue : α → β)
(onEffect : {ι : Type u} → F ι → (ι → β) → β)
(op : F ι) (k : ι → FreeM F α) :
foldFreeM onValue onEffect (.liftBind op k)
= onEffect op (fun x => foldFreeM onValue onEffect (k x)) := rfl |
673 | leanexplore_declaration | leanexplore:387046 | leanexplore_sqlite | leanexplore_declarations | /--
**Universal Property**: If `h : FreeM F α → β` satisfies:
* `h (.pure a) = onValue a`
* `h (.liftBind op k) = onEffect op (fun x => h (k x))`
then `h` is equal to `foldFreeM onValue onEffect`.
-/
theorem foldFreeM_unique
(onValue : α → β)
(onEffect : {ι : Type u} → F ι → (ι → β) → β)
(h : FreeM F α → β... |
370 | leanexplore_declaration | leanexplore:387047 | leanexplore_sqlite | leanexplore_declarations | /--
A structure for bidirectionally-infinite Turing machine tapes
that eventually take on blank `none` values
-/
structure BiTape (Symbol : Type) where
/-- The symbol currently under the tape head -/
head : Option Symbol
/-- The contents to the left of the head -/
left : StackTape Symbol
/-- The contents to t... |
65 | leanexplore_declaration | leanexplore:387048 | leanexplore_sqlite | leanexplore_declarations | /-- The empty `BiTape` -/
def nil : BiTape Symbol := ⟨none, ∅, ∅⟩ |
59 | leanexplore_declaration | leanexplore:387049 | leanexplore_sqlite | leanexplore_declarations | instance : Inhabited (BiTape Symbol) where
default := nil |
53 | leanexplore_declaration | leanexplore:387050 | leanexplore_sqlite | leanexplore_declarations | instance : EmptyCollection (BiTape Symbol) :=
⟨nil⟩ |
61 | leanexplore_declaration | leanexplore:387051 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma empty_eq_nil : (∅ : BiTape Symbol) = nil := rfl |
345 | leanexplore_declaration | leanexplore:387052 | leanexplore_sqlite | leanexplore_declarations | /--
Given a `List` of `Symbol`s, construct a `BiTape` by mapping the list to `some` elements
and laying them out to the right side,
with the head under the first element of the list if it exists.
-/
def mk₁ (l : List Symbol) : BiTape Symbol :=
match l with
| [] => ∅
| h :: t => { head := some h, left := ∅, right ... |
185 | leanexplore_declaration | leanexplore:387053 | leanexplore_sqlite | leanexplore_declarations | /--
Move the head left by shifting the left StackTape under the head.
-/
def move_left (t : BiTape Symbol) : BiTape Symbol :=
⟨t.left.head, t.left.tail, StackTape.cons t.head t.right⟩ |
189 | leanexplore_declaration | leanexplore:387054 | leanexplore_sqlite | leanexplore_declarations | /--
Move the head right by shifting the right StackTape under the head.
-/
def move_right (t : BiTape Symbol) : BiTape Symbol :=
⟨t.right.head, StackTape.cons t.head t.left, t.right.tail⟩ |
178 | leanexplore_declaration | leanexplore:387055 | leanexplore_sqlite | leanexplore_declarations | /--
Move the head to the left or right, shifting the tape underneath it.
-/
def move (t : BiTape Symbol) : Dir → BiTape Symbol
| .left => t.move_left
| .right => t.move_right |
163 | leanexplore_declaration | leanexplore:387056 | leanexplore_sqlite | leanexplore_declarations | /--
Optionally perform a `move`, or do nothing if `none`.
-/
def optionMove : BiTape Symbol → Option Dir → BiTape Symbol
| t, none => t
| t, some d => t.move d |
120 | leanexplore_declaration | leanexplore:387057 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma move_left_move_right (t : BiTape Symbol) : t.move_left.move_right = t := by
simp [move_right, move_left] |
120 | leanexplore_declaration | leanexplore:387058 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma move_right_move_left (t : BiTape Symbol) : t.move_right.move_left = t := by
simp [move_left, move_right] |
142 | leanexplore_declaration | leanexplore:387059 | leanexplore_sqlite | leanexplore_declarations | /--
Write a value under the head of the `BiTape`.
-/
def write (t : BiTape Symbol) (a : Option Symbol) : BiTape Symbol := { t with head := a } |
247 | leanexplore_declaration | leanexplore:387060 | leanexplore_sqlite | leanexplore_declarations | /--
The space used by a `BiTape` is the number of symbols
between and including the head, and leftmost and rightmost non-blank symbols on the `BiTape`.
-/
@[scoped grind]
def space_used (t : BiTape Symbol) : ℕ := 1 + t.left.length + t.right.length |
133 | leanexplore_declaration | leanexplore:387061 | leanexplore_sqlite | leanexplore_declarations | @[simp, grind =]
lemma space_used_write (t : BiTape Symbol) (a : Option Symbol) :
(t.write a).space_used = t.space_used := by rfl |
257 | leanexplore_declaration | leanexplore:387062 | leanexplore_sqlite | leanexplore_declarations | lemma space_used_mk₁ (l : List Symbol) :
(mk₁ l).space_used = max 1 l.length := by
cases l with
| nil => simp [mk₁, space_used, nil, StackTape.length_nil]
| cons h t => simp [mk₁, space_used, StackTape.length_nil, StackTape.length_map_some]; omega |
222 | leanexplore_declaration | leanexplore:387063 | leanexplore_sqlite | leanexplore_declarations | lemma space_used_move (t : BiTape Symbol) (d : Dir) :
(t.move d).space_used ≤ t.space_used + 1 := by
cases d <;> grind [move_left, move_right, move,
space_used, StackTape.length_tail_le, StackTape.length_cons_le] |
417 | leanexplore_declaration | leanexplore:387064 | leanexplore_sqlite | leanexplore_declarations | /-- A `FinFun` is a function `fn` with a finite `support`.
This is similar to `Finsupp` in Mathlib, but definitions are computable. -/
structure FinFun (α β : Type*) [Zero β] where
/-- The underlying function. -/
fn : α → β
/-- The finite support of the function. -/
support : Finset α
/-- Proof that `support... |
383 | leanexplore_declaration | leanexplore:387065 | leanexplore_sqlite | leanexplore_declarations | @[inherit_doc]
scoped infixr:25 " →₀ " => FinFun
/-- Constructs a `FinFun` by restricting a function to a given support, filtering out all elements
not mapped to 0 in the support. -/
@[scoped grind .]
def fromFun {α β : Type*} [Zero β] [DecidableEq α]
[∀ y : β, Decidable (y = 0)] (fn : α → β) (support : Finset α) : ... |
401 | leanexplore_declaration | leanexplore:387066 | leanexplore_sqlite | leanexplore_declarations | /-- Constructs a `FinFun` by restricting a function to a given support, filtering out all elements
not mapped to 0 in the support. -/
@[scoped grind .]
def fromFun {α β : Type*} [Zero β] [DecidableEq α]
[∀ y : β, Decidable (y = 0)] (fn : α → β) (support : Finset α) : α →₀ β where
fn := (fun a => if a ∈ support then... |
155 | leanexplore_declaration | leanexplore:387067 | leanexplore_sqlite | leanexplore_declarations | @[inherit_doc]
scoped notation f:25 "↾₀" support:51 => FinFun.fromFun f support
instance instFunLike [Zero β] : FunLike (α →₀ β) α β where
coe f := f.fn |
152 | leanexplore_declaration | leanexplore:387068 | leanexplore_sqlite | leanexplore_declarations | instance instFunLike [Zero β] : FunLike (α →₀ β) α β where
coe f := f.fn
coe_injective' := by
rintro ⟨_, _⟩ ⟨_, _⟩
simp_all [Finset.ext_iff] |
101 | leanexplore_declaration | leanexplore:387069 | leanexplore_sqlite | leanexplore_declarations | @[scoped grind =]
theorem coe_fn [Zero β] {f : α →₀ β} : (f : α → β) = f.fn := by simp [DFunLike.coe] |
100 | leanexplore_declaration | leanexplore:387070 | leanexplore_sqlite | leanexplore_declarations | @[scoped grind =]
theorem coe_eq_fn [Zero β] {f : α →₀ β} : f a = f.fn a := by
simp [DFunLike.coe] |
169 | leanexplore_declaration | leanexplore:387071 | leanexplore_sqlite | leanexplore_declarations | /-- Extensional equality for `FinFun`. -/
@[scoped grind ←=]
theorem ext [Zero β] {f g : α →₀ β} (h : ∀ (a : α), f a = g a) : f = g :=
DFunLike.ext (f := f) (g := g) h |
110 | leanexplore_declaration | leanexplore:387072 | leanexplore_sqlite | leanexplore_declarations | @[scoped grind _=_]
theorem mem_support_not_zero [Zero β] {f : α →₀ β} : a ∈ f.support ↔ f a ≠ 0 := by
grind |
110 | leanexplore_declaration | leanexplore:387073 | leanexplore_sqlite | leanexplore_declarations | @[scoped grind _=_]
theorem not_mem_support_zero [Zero β] {f : α →₀ β} : a ∉ f.support ↔ f a = 0 := by
grind |
208 | leanexplore_declaration | leanexplore:387074 | leanexplore_sqlite | leanexplore_declarations | /-- If two `FinFun`s are equal, their underlying functions and supports are equal. -/
@[scoped grind .]
theorem eq_fields_eq [Zero β] {f g : α →₀ β} : f = g → f.fn = g.fn ∧ f.support = g.support :=
by grind |
214 | leanexplore_declaration | leanexplore:387075 | leanexplore_sqlite | leanexplore_declarations | /-- If two functions are equal, two `FinFun`s respectively using them as underlying functions
are equal. -/
@[scoped grind .]
theorem fn_eq_eq [Zero β] {f g : α →₀ β} (h : f.fn = g.fn) : f = g :=
ext (congrFun h) |
108 | leanexplore_declaration | leanexplore:387076 | leanexplore_sqlite | leanexplore_declarations | @[scoped grind =>]
theorem congrFinFun [Zero β] {f g : α →₀ β} (h : f = g) (a : α) : f a = g a :=
by grind |
197 | leanexplore_declaration | leanexplore:387077 | leanexplore_sqlite | leanexplore_declarations | @[scoped grind =]
theorem fromFun_eq [Zero β] [DecidableEq α] [∀ y : β, Decidable (y = 0)]
(f : α → β) (support : Finset α) (h : ∀ a, a ∉ support → f a = 0) :
(f ↾₀ support) = f := by grind |
209 | leanexplore_declaration | leanexplore:387078 | leanexplore_sqlite | leanexplore_declarations | @[scoped grind =]
theorem fromFun_fn [Zero β] [DecidableEq α] [∀ y : β, Decidable (y = 0)]
(f : α → β) (support : Finset α) :
(f ↾₀ support).fn = (fun a => if a ∈ support then f a else 0) := by
grind |
202 | leanexplore_declaration | leanexplore:387079 | leanexplore_sqlite | leanexplore_declarations | @[scoped grind =]
theorem fromFun_support [Zero β] [DecidableEq α] [∀ y : β, Decidable (y = 0)]
(f : α → β) (support : Finset α) :
(f ↾₀ support).support = support.filter (f · ≠ 0) := by
grind |
259 | leanexplore_declaration | leanexplore:387080 | leanexplore_sqlite | leanexplore_declarations | /-- Restricting a function twice to the same support is idempotent. -/
@[scoped grind =]
theorem fromFun_idem [Zero β] [DecidableEq α]
[∀ y : β, Decidable (y = 0)] {f : α → β} {support : Finset α} :
(f ↾₀ support) ↾₀ support = f ↾₀ support := by grind |
212 | leanexplore_declaration | leanexplore:387081 | leanexplore_sqlite | leanexplore_declarations | /-- Restricting a `FinFun` to its own support is the identity. -/
@[scoped grind =]
theorem coe_fromFun_id [Zero β] [DecidableEq α] [∀ y : β, Decidable (y = 0)] {f : α →₀ β} :
(f ↾₀ f.support) = f := by grind |
314 | leanexplore_declaration | leanexplore:387082 | leanexplore_sqlite | leanexplore_declarations | /-- Restricting a function twice to two supports is equal to restricting to their intersection. -/
@[scoped grind =]
theorem fromFun_inter [Zero β] [DecidableEq α]
[∀ y : β, Decidable (y = 0)] {f : α → β} {support1 support2 : Finset α} :
(f ↾₀ support1) ↾₀ support2 = f ↾₀ (support1 ∩ support2) := by grind |
304 | leanexplore_declaration | leanexplore:387083 | leanexplore_sqlite | leanexplore_declarations | /-- Restricting a function is commutative. -/
@[scoped grind =]
theorem fromFun_comm [Zero β] [DecidableEq α]
[∀ y : β, Decidable (y = 0)] {f : α → β} {support1 support2 : Finset α} :
(f ↾₀ support1) ↾₀ support2 = (f ↾₀ support2) ↾₀ support1 := by
grind only [= coe_eq_fn, = fromFun_fn, ←= ext] |
258 | leanexplore_declaration | leanexplore:387084 | leanexplore_sqlite | leanexplore_declarations | /-- Decidable equality -/
instance instDecidableEq [Zero β] [DecidableEq α] [DecidableEq β] : DecidableEq (α →₀ β) :=
fun f g =>
if h : ∀ a ∈ f.support ∪ g.support, f a = g a then
isTrue <| ext fun a => by grind
else
isFalse <| by grind |
383 | leanexplore_declaration | leanexplore:387085 | leanexplore_sqlite | leanexplore_declarations | /-- A type `α` has a computable `fresh` function if it is always possible, for any finite set
of `α`, to compute a fresh element not in the set. -/
class HasFresh (α : Type u) where
/-- Given a finite set, returns an element not in the set. -/
fresh : Finset α → α
/-- Proof that `fresh` returns a fresh element fo... |
223 | leanexplore_declaration | leanexplore:387086 | leanexplore_sqlite | leanexplore_declarations | /-- An existential version of the `HasFresh` typeclass. This is useful for the sake of brevity
in proofs. -/
theorem HasFresh.fresh_exists {α : Type u} [HasFresh α] (s : Finset α) : ∃ a, a ∉ s :=
⟨fresh s, fresh_notMem s⟩ |
293 | leanexplore_declaration | leanexplore:387087 | leanexplore_sqlite | leanexplore_declarations | /-- Configuration for the `free_union` term elaborator. -/
structure FreeUnionConfig where
/-- For `free_union Var`, include all `x : Var`. Defaults to true. -/
singleton : Bool := true
/-- For `free_union Var`, include all `xs : Finset Var`. Defaults to true. -/
finset : Bool := true |
91 | leanexplore_declaration | leanexplore:387088 | leanexplore_sqlite | leanexplore_declarations | /-- Elaborate a FreeUnionConfig. -/
declare_config_elab elabFreeUnionConfig FreeUnionConfig |
948 | leanexplore_declaration | leanexplore:387089 | leanexplore_sqlite | leanexplore_declarations | /--
Given a `DecidableEq Var` instance, this elaborator automatically constructs
the union of any variables, finite sets of variables, and optionally the
results of provided functions mapping to variables. This is configurable with
optional boolean boolean arguments `singleton` and `finset`.
As an example, c... |
1,322 | leanexplore_declaration | leanexplore:387090 | leanexplore_sqlite | leanexplore_declarations | /-- Elaborator for `free_union`. -/
@[term_elab freeUnion]
def HasFresh.freeUnion : TermElab := fun stx _ => do
match stx with
| `(free_union $cfg $[[$maps,*]]? $var:term) =>
let cfg ← elabFreeUnionConfig cfg |>.run { elaborator := .anonymous } |>.run' { goals := [] }
-- the type of our variables
let v... |
211 | leanexplore_declaration | leanexplore:387091 | leanexplore_sqlite | leanexplore_declarations | /-- `HasFresh α` implies a computably infinite type. -/
instance HasFresh.to_infinite (α : Type u) [HasFresh α] : Infinite α := by
apply Infinite.of_not_fintype
rintro ⟨elems, _⟩
grind [fresh_notMem elems] |
334 | leanexplore_declaration | leanexplore:387092 | leanexplore_sqlite | leanexplore_declarations | /-- All infinite types have an associated (at least noncomputable) fresh function.
This, in conjunction with `HasFresh.to_infinite`, characterizes `HasFresh`. -/
noncomputable instance HasFresh.of_infinite (α : Type u) [Infinite α] : HasFresh α where
fresh s := Infinite.exists_notMem_finset s |>.choose
fresh_notMem... |
400 | leanexplore_declaration | leanexplore:387093 | leanexplore_sqlite | leanexplore_declarations | /-- Construct a fresh element from an embedding of `ℕ` using `Nat.find`. -/
@[implicit_reducible]
def HasFresh.ofNatEmbed {α : Type u} [DecidableEq α] (e : ℕ ↪ α) : HasFresh α where
fresh s := e (Nat.find (p := fun n ↦ e n ∉ s) ⟨(s.preimage e e.2.injOn).max.succ,
fun h ↦ not_lt_of_ge (le_max <| mem_preimage.2 h) ... |
430 | leanexplore_declaration | leanexplore:387094 | leanexplore_sqlite | leanexplore_declarations | /-- Construct a fresh element given a function `f` with `x < f x`. -/
@[implicit_reducible]
def HasFresh.ofSucc {α : Type u} [Inhabited α] [SemilatticeSup α] (f : α → α) (hf : ∀ x, x < f x) :
HasFresh α where
fresh s := if hs : s.Nonempty then f (s.sup' hs id) else default
fresh_notMem s h := if hs : s.Nonempty... |
101 | leanexplore_declaration | leanexplore:387095 | leanexplore_sqlite | leanexplore_declarations | /-- `ℕ` has a computable fresh function. -/
instance : HasFresh ℕ :=
.ofSucc (· + 1) Nat.lt_add_one |
98 | leanexplore_declaration | leanexplore:387096 | leanexplore_sqlite | leanexplore_declarations | /-- `ℤ` has a computable fresh function. -/
instance : HasFresh ℤ :=
.ofSucc (· + 1) Int.lt_succ |
124 | leanexplore_declaration | leanexplore:387097 | leanexplore_sqlite | leanexplore_declarations | /-- `ℚ` has a computable fresh function. -/
instance : HasFresh ℚ :=
.ofSucc (· + 1) fun x ↦ lt_add_of_pos_right x one_pos |
234 | leanexplore_declaration | leanexplore:387098 | leanexplore_sqlite | leanexplore_declarations | /-- If `α` has a computable fresh function, then so does `Finset α`. -/
instance {α : Type u} [DecidableEq α] [HasFresh α] : HasFresh (Finset α) :=
.ofSucc (fun s ↦ insert (fresh s) s) fun s ↦ Finset.ssubset_insert <| fresh_notMem s |
225 | leanexplore_declaration | leanexplore:387099 | leanexplore_sqlite | leanexplore_declarations | /-- If `α` is inhabited, then `Multiset α` has a computable fresh function. -/
instance {α : Type u} [DecidableEq α] [Inhabited α] : HasFresh (Multiset α) :=
.ofSucc (fun s ↦ default ::ₘ s) fun _ ↦ Multiset.lt_cons_self _ _ |
175 | leanexplore_declaration | leanexplore:387100 | leanexplore_sqlite | leanexplore_declarations | /-- `ℕ → ℕ` has a computable fresh function. -/
instance : HasFresh (ℕ → ℕ) :=
.ofSucc (fun f x ↦ f x + 1) fun _ ↦ Pi.lt_def.2 ⟨fun _ ↦ Nat.le_succ _, 0, Nat.lt_succ_self _⟩ |
229 | leanexplore_declaration | leanexplore:387101 | leanexplore_sqlite | leanexplore_declarations | /-- The `f`-segment of `k`, where `f : ℕ → ℕ` will be assumed to be at least StrictMono. -/
@[scoped grind]
noncomputable def Nat.segment (f : ℕ → ℕ) (k : ℕ) : ℕ :=
open scoped Classical in
Nat.count (· ∈ range f) (k + 1) - 1 |
192 | leanexplore_declaration | leanexplore:387102 | leanexplore_sqlite | leanexplore_declarations | /-- Any strictly monotonic function `f : ℕ → ℕ` has an infinite range. -/
theorem strictMono_infinite (hm : StrictMono f) :
(range f).Infinite :=
infinite_range_of_injective hm.injective |
249 | leanexplore_declaration | leanexplore:387103 | leanexplore_sqlite | leanexplore_declarations | /-- Any infinite subset of `ℕ` is the range of a strictly monotonic function. -/
theorem infinite_strictMono {ns : Set ℕ} (h : ns.Infinite) :
∃ f : ℕ → ℕ, StrictMono f ∧ range f = ns :=
⟨nth (· ∈ ns), nth_strictMono h, range_nth_of_infinite h⟩ |
534 | leanexplore_declaration | leanexplore:387104 | leanexplore_sqlite | leanexplore_declarations | /-- There is a gap between two successive occurrences of a predicate `p : ℕ → Prop`,
assuming `p` (as a set) is infinite. -/
theorem nth_succ_gap {p : ℕ → Prop} (hf : (setOf p).Infinite) (n : ℕ) :
∀ k < nth p (n + 1) - nth p n, k > 0 → ¬ p (k + nth p n) := by
classical
intro k h_k1 h_k0 h_p_k
let m := count p... |
450 | leanexplore_declaration | leanexplore:387105 | leanexplore_sqlite | leanexplore_declarations | /-- For a strictly monotonic function `f : ℕ → ℕ`, `f n` is exactly the n-th
element of the range of `f`. -/
theorem nth_of_strictMono (hm : StrictMono f) (n : ℕ) :
f n = nth (· ∈ range f) n := by
rw [← nth_comp_of_strictMono hm]
· simp
· simp
· #adaptation_note
/-- A grind regression found moving to ni... |
251 | leanexplore_declaration | leanexplore:387106 | leanexplore_sqlite | leanexplore_declarations | /-- If `f 0 = 0`, then `0` is below any `n` not in the range of `f`. -/
theorem count_notMem_range_pos (h0 : f 0 = 0) (n : ℕ) (hn : n ∉ range f) :
count (· ∈ range f) n > 0 := by
have := count_monotone (· ∈ range f) (show 1 ≤ n by grind)
grind |
571 | leanexplore_declaration | leanexplore:387107 | leanexplore_sqlite | leanexplore_declarations | /-- For a strictly monotonic function `f : ℕ → ℕ`, no number (strictly) between
`f m` and ` f (m + 1)` is in the range of `f`. -/
theorem strictMono_range_gap (hm : StrictMono f) {m k : ℕ}
(hl : f m < k) (hu : k < f (m + 1)) : k ∉ range f := by
rw [nth_of_strictMono hm m] at hl
rw [nth_of_strictMono hm (m + 1)]... |
311 | leanexplore_declaration | leanexplore:387108 | leanexplore_sqlite | leanexplore_declarations | /-- For a strictly monotonic function `f : ℕ → ℕ`, the segment of `f k` is `k`. -/
@[simp]
theorem segment_idem (hm : StrictMono f) (k : ℕ) :
segment f (f k) = k := by
classical
have := count_nth_of_infinite (p := (· ∈ range f)) <| strictMono_infinite hm
have := nth_of_strictMono hm
grind [segment] |
398 | leanexplore_declaration | leanexplore:387109 | leanexplore_sqlite | leanexplore_declarations | /-- For a strictly monotonic function `f : ℕ → ℕ`, `segment f k = 0` for all `k < f 0`. -/
@[scoped grind =]
theorem segment_pre_zero (hm : StrictMono f) {k : ℕ} (h : k < f 0) :
segment f k = 0 := by
classical
have h1 : count (· ∈ range f) (k + 1) = 0 := by
apply count_of_forall_not
rintro n h_n ⟨i, rfl... |
274 | leanexplore_declaration | leanexplore:387110 | leanexplore_sqlite | leanexplore_declarations | /-- For a strictly monotonic function `f : ℕ → ℕ` with `f 0 = 0`, `segment f 0 = 0`. -/
@[scoped grind =]
theorem segment_zero (hm : StrictMono f) (h0 : f 0 = 0) :
segment f 0 = 0 := by
calc _ = segment f (f 0) := by simp [h0]
_ = _ := by simp [segment_idem hm] |
307 | leanexplore_declaration | leanexplore:387111 | leanexplore_sqlite | leanexplore_declarations | /-- A slight restatement of the definition of `segment` which has proven useful. -/
theorem segment_plus_one (h0 : f 0 = 0) (k : ℕ) :
segment f k + 1 = count (· ∈ range f) (k + 1) := by
suffices _ : count (· ∈ range f) (k + 1) ≠ 0 by unfold segment; omega
apply count_ne_iff_exists.mpr; use 0; grind |
431 | leanexplore_declaration | leanexplore:387112 | leanexplore_sqlite | leanexplore_declarations | /-- For a strictly monotonic function `f : ℕ → ℕ` with `f 0 = 0`,
`k < f (segment f k + 1)` for all `k : ℕ`. -/
theorem segment_upper_bound (hm : StrictMono f) (h0 : f 0 = 0) (k : ℕ) :
k < f (segment f k + 1) := by
classical
rw [nth_of_strictMono hm (segment f k + 1), segment_plus_one h0 k]
suffices _ : k + 1... |
665 | leanexplore_declaration | leanexplore:387113 | leanexplore_sqlite | leanexplore_declarations | /-- For a strictly monotonic function `f : ℕ → ℕ` with `f 0 = 0`,
`f (segment f k) ≤ k` for all `k : ℕ`. -/
theorem segment_lower_bound (hm : StrictMono f) (h0 : f 0 = 0) (k : ℕ) :
f (segment f k) ≤ k := by
classical
rw [nth_of_strictMono hm (segment f k), segment]
rcases Classical.em (k ∈ range f) with h_k |... |
1,131 | leanexplore_declaration | leanexplore:387114 | leanexplore_sqlite | leanexplore_declarations | /-- For a strictly monotonic function `f : ℕ → ℕ`, all `k` satisfying `f m ≤ k < f (m + 1)`
has `segment f k = m`. -/
theorem segment_range_val (hm : StrictMono f) {m k : ℕ}
(hl : f m ≤ k) (hu : k < f (m + 1)) : segment f k = m := by
classical
obtain (rfl | hu') := show f m = k ∨ f m < k by omega
· exact segm... |
552 | leanexplore_declaration | leanexplore:387115 | leanexplore_sqlite | leanexplore_declarations | /-- For a strictly monotonic function `f : ℕ → ℕ` with `f 0 = 0`,
`f` and `segment f` form a Galois connection. -/
theorem segment_galois_connection (hm : StrictMono f) (h0 : f 0 = 0) :
GaloisConnection f (segment f) := by
intro m k; constructor
· intro h
by_contra! h_con
have h1 : segment f k + 1 ≤ m :... |
220 | leanexplore_declaration | leanexplore:387116 | leanexplore_sqlite | leanexplore_declarations | /-- `segment'` is a helper function that will be proved to be equal to `segment`.
It facilitates the proofs of some theorems below. -/
noncomputable def segment' (f : ℕ → ℕ) (k : ℕ) : ℕ :=
segment (f · - f 0) (k - f 0) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.