Skip to content

Commit 89fc732

Browse files
Update lean-toolchain for leanprover/lean4#9932
2 parents 1811271 + 3d15ca1 commit 89fc732

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1031
-1300
lines changed

Batteries/Classes/RatCast.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Copyright (c) 2014 Robert Lewis. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Robert Lewis, Leonardo de Moura, Johannes Hölzl, Mario Carneiro, Gabriel Ebner
55
-/
6-
import Batteries.Data.Rat.Basic
76

87
/-- Type class for the canonical homomorphism `Rat → K`. -/
98
class RatCast (K : Type u) where

Batteries/Control/Lemmas.lean

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/-
22
Copyright (c) 2023 François G. Dorais. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
4-
Authors: François G. Dorais
4+
Authors: François G. Dorais, Eric Wieser
55
-/
66

77
namespace ReaderT
@@ -14,6 +14,21 @@ attribute [ext] ReaderT.ext
1414
@[simp] theorem run_orElse [Monad m] [Alternative m] (x y : ReaderT ρ m α) (ctx : ρ) :
1515
(x <|> y).run ctx = (x.run ctx <|> y.run ctx) := rfl
1616

17+
@[simp] theorem run_throw [MonadExceptOf ε m] (e : ε) (ctx : ρ) :
18+
(throw e : ReaderT ρ m α).run ctx = throw e := rfl
19+
20+
@[simp] theorem run_throwThe [MonadExceptOf ε m] (e : ε) (ctx : ρ) :
21+
(throwThe ε e : ReaderT ρ m α).run ctx = throwThe ε e := rfl
22+
23+
@[simp] theorem run_tryCatch [MonadExceptOf ε m]
24+
(body : ReaderT ρ m α) (handler : ε → ReaderT ρ m α) (ctx : ρ) :
25+
(tryCatch body handler).run ctx = tryCatch (body.run ctx) (handler · |>.run ctx) := rfl
26+
27+
@[simp] theorem run_tryCatchThe [MonadExceptOf ε m]
28+
(body : ReaderT ρ m α) (handler : ε → ReaderT ρ m α) (ctx : ρ) :
29+
(tryCatchThe ε body handler).run ctx = tryCatchThe ε (body.run ctx) (handler · |>.run ctx) :=
30+
rfl
31+
1732
end ReaderT
1833

1934
namespace StateT
@@ -26,4 +41,18 @@ attribute [ext] StateT.ext
2641
@[simp] theorem run_orElse {α σ} [Monad m] [Alternative m] (x y : StateT σ m α) (s : σ) :
2742
(x <|> y).run s = (x.run s <|> y.run s) := rfl
2843

44+
@[simp] theorem run_throw [Monad m] [MonadExceptOf ε m] (e : ε) (s : σ) :
45+
(throw e : StateT σ m α).run s = (do let a ← throw e; pure (a, s)) := rfl
46+
47+
@[simp] theorem run_throwThe [Monad m] [MonadExceptOf ε m] (e : ε) (s : σ) :
48+
(throwThe ε e : StateT σ m α).run s = (do let a ← throwThe ε e; pure (a, s)) := rfl
49+
50+
@[simp] theorem run_tryCatch [Monad m] [MonadExceptOf ε m]
51+
(body : StateT σ m α) (handler : ε → StateT σ m α) (s : σ) :
52+
(tryCatch body handler).run s = tryCatch (body.run s) (handler · |>.run s) := rfl
53+
54+
@[simp] theorem run_tryCatchThe [Monad m] [MonadExceptOf ε m]
55+
(body : StateT σ m α) (handler : ε → StateT σ m α) (s : σ) :
56+
(tryCatchThe ε body handler).run s = tryCatchThe ε (body.run s) (handler · |>.run s) := rfl
57+
2958
end StateT

Batteries/Control/Nondet/Basic.lean

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ structure Nondet (m : Type → Type) [MonadBacktrack σ m] (α : Type) : Type wh
5050
toMLList : MLList m (α × σ)
5151

5252
namespace Nondet
53+
variable {m : TypeType}
5354

54-
variable {m : TypeType} [Monad m] [MonadBacktrack σ m]
55+
section Monad
56+
variable [Monad m] [MonadBacktrack σ m]
5557

5658
/-- The empty nondeterministic value. -/
5759
def nil : Nondet m α := .mk .nil
@@ -87,13 +89,10 @@ def singletonM (x : m α) : Nondet m α :=
8789
/-- Convert a value to the singleton nondeterministic value. -/
8890
def singleton (x : α) : Nondet m α := singletonM (pure x)
8991

90-
/-- `Nondet m` is a monad. -/
91-
instance : Monad (Nondet m) where
92+
/-- `Nondet m` is an alternative monad. -/
93+
instance : AlternativeMonad (Nondet m) where
9294
pure a := singletonM (pure a)
9395
bind := bind
94-
95-
/-- `Nondet m` is an alternative monad. -/
96-
instance : Alternative (Nondet m) where
9796
failure := .nil
9897
orElse x y := .mk <| x.toMLList.append fun _ => (y ()).toMLList
9998

@@ -164,21 +163,6 @@ All iterations of a non-deterministic function on an initial value.
164163
partial def iterate (f : α → Nondet m α) (a : α) : Nondet m α :=
165164
singleton a <|> (f a).bind (iterate f)
166165

167-
/--
168-
Find the first alternative in a nondeterministic value, as a monadic value.
169-
-/
170-
def head [Alternative m] (L : Nondet m α) : m α := do
171-
let (x, s) ← L.toMLList.head
172-
restoreState s
173-
return x
174-
175-
/--
176-
Find the value of a monadic function on the first alternative in a nondeterministic value
177-
where the function succeeds.
178-
-/
179-
def firstM [Alternative m] (L : Nondet m α) (f : α → m (Option β)) : m β :=
180-
L.filterMapM f |>.head
181-
182166
/--
183167
Convert a non-deterministic value into a lazy list, by discarding the backtrackable state.
184168
-/
@@ -194,6 +178,28 @@ Convert a non-deterministic value into a list in the monad, by discarding the ba
194178
-/
195179
def toList' (L : Nondet m α) : m (List α) := L.toMLList.map (·.1) |>.force
196180

181+
end Monad
182+
183+
section AlternativeMonad
184+
variable [AlternativeMonad m] [MonadBacktrack σ m]
185+
186+
/--
187+
Find the first alternative in a nondeterministic value, as a monadic value.
188+
-/
189+
def head (L : Nondet m α) : m α := do
190+
let (x, s) ← L.toMLList.head
191+
restoreState s
192+
return x
193+
194+
/--
195+
Find the value of a monadic function on the first alternative in a nondeterministic value
196+
where the function succeeds.
197+
-/
198+
def firstM (L : Nondet m α) (f : α → m (Option β)) : m β :=
199+
L.filterMapM f |>.head
200+
201+
end AlternativeMonad
202+
197203
end Nondet
198204

199205
/-- The `Id` monad is trivially backtrackable, with state `Unit`. -/

Batteries/Data/Array/Match.lean

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,66 @@ partial def Matcher.next? [BEq α] [Stream σ α] (m : Matcher α) (stream : σ)
101101
else
102102
next? { m with state } stream
103103

104+
namespace Matcher
105+
open Std.Iterators
106+
107+
/-- Iterator transformer for KMP matcher. -/
108+
protected structure Iterator (σ n α) [BEq α] (m : Matcher α) [Iterator σ n α] where
109+
/-- Inner iterator. -/
110+
inner : IterM (α := σ) n α
111+
/-- Matcher state. -/
112+
state : Fin (m.table.size + 1) := 0
113+
114+
private def modifyStep [BEq α] (m : Matcher α) [Iterator σ n α]
115+
(it : IterM (α := m.Iterator σ n α) n σ) :
116+
it.internalState.inner.Step (α := σ) → IterStep (IterM (α := m.Iterator σ n α) n σ) σ
117+
| .done _ => .done
118+
| .skip it' _ => .skip ⟨{it.internalState with inner := it'}⟩
119+
| .yield it' x _ =>
120+
let state := m.table.step x m.state
121+
if state = m.table.size then
122+
.yield ⟨{inner := it', state := state}⟩ it'.internalState
123+
else
124+
.skip ⟨{inner := it', state := state}⟩
125+
126+
instance [Monad n] [BEq α] (m : Matcher α) [Iterator σ n α] :
127+
Iterator (m.Iterator σ n α) n σ where
128+
IsPlausibleStep it step := ∃ step', m.modifyStep it step' = step
129+
step it := it.internalState.inner.step >>= fun step => pure ⟨m.modifyStep _ _, step, rfl⟩
130+
131+
private def finitenessRelation [Monad n] [BEq α] (m : Matcher α) [Iterator σ n α] [Finite σ n] :
132+
FinitenessRelation (m.Iterator σ n α) n where
133+
rel := InvImage IterM.IsPlausibleSuccessorOf fun it => it.internalState.inner
134+
wf := InvImage.wf _ Finite.wf
135+
subrelation {it it'} h := by
136+
obtain ⟨_, hsucc, step, rfl⟩ := h
137+
simp only [IterM.Step] at step
138+
cases step with simp only [IterStep.successor, modifyStep, reduceCtorEq] at hsucc
139+
| skip =>
140+
cases hsucc
141+
apply IterM.isPlausibleSuccessorOf_of_skip
142+
assumption
143+
| yield =>
144+
split at hsucc
145+
· next heq =>
146+
cases hsucc
147+
split at heq
148+
· cases heq
149+
apply IterM.isPlausibleSuccessorOf_of_yield
150+
assumption
151+
· contradiction
152+
· next heq =>
153+
cases hsucc
154+
split at heq
155+
· contradiction
156+
· cases heq
157+
apply IterM.isPlausibleSuccessorOf_of_yield
158+
assumption
159+
· contradiction
160+
161+
instance [Monad n] [BEq α] (m : Matcher α) [Iterator σ n α] [inst : Finite σ n] :
162+
Finite (m.Iterator σ n α) n (β := σ) := .of_finitenessRelation m.finitenessRelation
163+
164+
end Matcher
165+
104166
end Array

Batteries/Data/Array/Pairwise.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ instance (R : α → α → Prop) [DecidableRel R] (as) : Decidable (Pairwise R
3232
· intro h ⟨j, hj⟩ ⟨i, hlt⟩; exact h i j (Nat.lt_trans hlt hj) hj hlt
3333
decidable_of_iff _ this
3434

35-
@[grind]
35+
@[grind]
3636
theorem pairwise_empty : #[].Pairwise R := by
3737
unfold Pairwise; exact List.Pairwise.nil
3838

39-
@[grind]
39+
@[grind]
4040
theorem pairwise_singleton (R : α → α → Prop) (a) : #[a].Pairwise R := by
4141
unfold Pairwise; exact List.pairwise_singleton ..
4242

Batteries/Data/BitVec/Lemmas.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace BitVec
3636
simp only [BitVec.toInt, Int.ofBits, toNat_ofFnLE, Int.subNatNat_eq_coe]; rfl
3737

3838
-- TODO: consider these for global `grind` attributes.
39-
attribute [local grind] Fin.succ Fin.rev Fin.last Fin.zero_eta
39+
attribute [local grind =] Fin.succ Fin.rev Fin.last Fin.zero_eta
4040

4141
theorem getElem_ofFnLEAux (f : Fin n → Bool) (i) (h : i < n) (h' : i < m) :
4242
(ofFnLEAux m f)[i] = f ⟨i, h⟩ := by

Batteries/Data/ByteArray.lean

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,8 @@ theorem getElem_eq_data_getElem (a : ByteArray) (h : i < a.size) : a[i] = a.data
2222

2323
@[simp] theorem data_mkEmpty (cap) : (emptyWithCapacity cap).data = #[] := rfl
2424

25-
@[simp] theorem data_empty : empty.data = #[] := rfl
26-
27-
@[simp] theorem size_empty : empty.size = 0 := rfl
28-
2925
/-! ### push -/
3026

31-
@[simp] theorem data_push (a : ByteArray) (b : UInt8) : (a.push b).data = a.data.push b := rfl
32-
3327
@[simp] theorem size_push (a : ByteArray) (b : UInt8) : (a.push b).size = a.size + 1 :=
3428
Array.size_push ..
3529

@@ -68,15 +62,6 @@ theorem set_set (a : ByteArray) (i : Fin a.size) (v v' : UInt8) :
6862

6963
/-! ### append -/
7064

71-
@[simp] theorem append_eq (a b) : ByteArray.append a b = a ++ b := rfl
72-
73-
@[simp] theorem data_append (a b : ByteArray) : (a ++ b).data = a.data ++ b.data := by
74-
rw [←append_eq]; simp [ByteArray.append, size]
75-
rw [Array.extract_empty_of_stop_le_start (h:=Nat.le_add_right ..), Array.append_empty]
76-
77-
theorem size_append (a b : ByteArray) : (a ++ b).size = a.size + b.size := by
78-
simp only [size, append_eq, data_append]; exact Array.size_append ..
79-
8065
theorem get_append_left {a b : ByteArray} (hlt : i < a.size)
8166
(h : i < (a ++ b).size := size_append .. ▸ Nat.lt_of_lt_of_le hlt (Nat.le_add_right ..)) :
8267
(a ++ b)[i] = a[i] := by
@@ -89,17 +74,6 @@ theorem get_append_right {a b : ByteArray} (hle : a.size ≤ i) (h : i < (a ++ b
8974

9075
/-! ### extract -/
9176

92-
@[simp] theorem data_extract (a : ByteArray) (start stop) :
93-
(a.extract start stop).data = a.data.extract start stop := by
94-
simp [extract]
95-
match Nat.le_total start stop with
96-
| .inl h => simp [h, Nat.add_sub_cancel']
97-
| .inr h => simp [h, Nat.sub_eq_zero_of_le, Array.extract_empty_of_stop_le_start]
98-
99-
@[simp] theorem size_extract (a : ByteArray) (start stop) :
100-
(a.extract start stop).size = min stop a.size - start := by
101-
simp [size]
102-
10377
theorem get_extract_aux {a : ByteArray} {start stop} (h : i < (a.extract start stop).size) :
10478
start + i < a.size := by
10579
apply Nat.add_lt_of_lt_sub'; apply Nat.lt_of_lt_of_le h

Batteries/Data/Char/AsciiCasing.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Authors: François G. Dorais
55
-/
66
import Batteries.Data.Char.Basic
77
import Batteries.Tactic.Basic
8-
import Std.Classes.Ord.String
98

109
/-! # Lemmas for ASCII-casing
1110

0 commit comments

Comments
 (0)