Skip to content

Commit ca77768

Browse files
committed
Merge remote-tracking branch 'upstream/master' into treemap-equiv
2 parents 7ad0b96 + 6afa820 commit ca77768

File tree

259 files changed

+53706
-52023
lines changed

Some content is hidden

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

259 files changed

+53706
-52023
lines changed

.github/workflows/update-stage0.yml

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,24 @@ jobs:
4040
run: |
4141
git config --global user.name "Lean stage0 autoupdater"
4242
git config --global user.email "<>"
43-
# Would be nice, but does not work yet:
44-
# https://github.com/DeterminateSystems/magic-nix-cache/issues/39
45-
# This action does not run that often and building runs in a few minutes, so ok for now
46-
#- if: env.should_update_stage0 == 'yes'
47-
# uses: DeterminateSystems/magic-nix-cache-action@v2
48-
- if: env.should_update_stage0 == 'yes'
49-
name: Restore Build Cache
50-
uses: actions/cache/restore@v4
51-
with:
52-
path: nix-store-cache
53-
key: Nix Linux-nix-store-cache-${{ github.sha }}
54-
# fall back to (latest) previous cache
55-
restore-keys: |
56-
Nix Linux-nix-store-cache
57-
- if: env.should_update_stage0 == 'yes'
58-
name: Further Set Up Nix Cache
59-
shell: bash -euxo pipefail {0}
60-
run: |
61-
# Nix seems to mutate the cache, so make a copy
62-
cp -r nix-store-cache nix-store-cache-copy || true
6343
- if: env.should_update_stage0 == 'yes'
6444
name: Install Nix
6545
uses: DeterminateSystems/nix-installer-action@main
66-
with:
67-
extra-conf: |
68-
substituters = file://${{ github.workspace }}/nix-store-cache-copy?priority=10&trusted=true https://cache.nixos.org
46+
- name: Open Nix shell once
47+
if: env.should_update_stage0 == 'yes'
48+
run: true
49+
shell: 'nix develop -c bash -euxo pipefail {0}'
50+
- name: Set up NPROC
51+
if: env.should_update_stage0 == 'yes'
52+
run: |
53+
echo "NPROC=$(nproc 2>/dev/null || sysctl -n hw.logicalcpu 2>/dev/null || echo 4)" >> $GITHUB_ENV
54+
shell: 'nix develop -c bash -euxo pipefail {0}'
55+
- if: env.should_update_stage0 == 'yes'
56+
run: cmake --preset release
57+
shell: 'nix develop -c bash -euxo pipefail {0}'
6958
- if: env.should_update_stage0 == 'yes'
70-
run: nix run .#update-stage0-commit
59+
run: make -j$NPROC -C build/release update-stage0-commit
60+
shell: 'nix develop -c bash -euxo pipefail {0}'
7161
- if: env.should_update_stage0 == 'yes'
7262
run: git show --stat
7363
- if: env.should_update_stage0 == 'yes' && github.event_name == 'push'

src/Init/Control/Lawful/Basic.lean

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Authors: Sebastian Ullrich, Leonardo de Moura, Mario Carneiro
66
module
77

88
prelude
9+
import Init.Ext
910
import Init.SimpLemmas
1011
import Init.Meta
1112

@@ -241,13 +242,23 @@ theorem LawfulMonad.mk' (m : Type u → Type v) [Monad m]
241242

242243
namespace Id
243244

244-
@[simp] theorem map_eq (x : Id α) (f : α → β) : f <$> x = f x := rfl
245-
@[simp] theorem bind_eq (x : Id α) (f : α → id β) : x >>= f = f x := rfl
246-
@[simp] theorem pure_eq (a : α) : (pure a : Id α) = a := rfl
245+
@[ext] theorem ext {x y : Id α} (h : x.run = y.run) : x = y := h
247246

248247
instance : LawfulMonad Id := by
249248
refine LawfulMonad.mk' _ ?_ ?_ ?_ <;> intros <;> rfl
250249

250+
@[simp] theorem run_map (x : Id α) (f : α → β) : (f <$> x).run = f x.run := rfl
251+
@[simp] theorem run_bind (x : Id α) (f : α → Id β) : (x >>= f).run = (f x.run).run := rfl
252+
@[simp] theorem run_pure (a : α) : (pure a : Id α).run = a := rfl
253+
@[simp] theorem run_seqRight (x y : Id α) : (x *> y).run = y.run := rfl
254+
@[simp] theorem run_seqLeft (x y : Id α) : (x <* y).run = x.run := rfl
255+
@[simp] theorem run_seq (f : Id (α → β)) (x : Id α) : (f <*> x).run = f.run x.run := rfl
256+
257+
-- These lemmas are bad as they abuse the defeq of `Id α` and `α`
258+
@[deprecated run_map (since := "2025-03-05")] theorem map_eq (x : Id α) (f : α → β) : f <$> x = f x := rfl
259+
@[deprecated run_bind (since := "2025-03-05")] theorem bind_eq (x : Id α) (f : α → id β) : x >>= f = f x := rfl
260+
@[deprecated run_pure (since := "2025-03-05")] theorem pure_eq (a : α) : (pure a : Id α) = a := rfl
261+
251262
end Id
252263

253264
/-! # Option -/

src/Init/Data/Array/Basic.lean

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ Examples:
544544
-/
545545
@[inline]
546546
def modify (xs : Array α) (i : Nat) (f : α → α) : Array α :=
547-
Id.run <| modifyM xs i f
547+
Id.run <| modifyM xs i (pure <| f ·)
548548

549549
set_option linter.indexVariables false in -- Changing `idx` causes bootstrapping issues, haven't investigated.
550550
/--
@@ -1059,7 +1059,7 @@ Examples:
10591059
-/
10601060
@[inline]
10611061
def foldl {α : Type u} {β : Type v} (f : β → α → β) (init : β) (as : Array α) (start := 0) (stop := as.size) : β :=
1062-
Id.run <| as.foldlM f init start stop
1062+
Id.run <| as.foldlM (pure <| f · ·) init start stop
10631063

10641064
/--
10651065
Folds a function over an array from the right, accumulating a value starting with `init`. The
@@ -1076,7 +1076,7 @@ Examples:
10761076
-/
10771077
@[inline]
10781078
def foldr {α : Type u} {β : Type v} (f : α → β → β) (init : β) (as : Array α) (start := as.size) (stop := 0) : β :=
1079-
Id.run <| as.foldrM f init start stop
1079+
Id.run <| as.foldrM (pure <| f · ·) init start stop
10801080

10811081
/--
10821082
Computes the sum of the elements of an array.
@@ -1124,7 +1124,7 @@ Examples:
11241124
-/
11251125
@[inline]
11261126
def map {α : Type u} {β : Type v} (f : α → β) (as : Array α) : Array β :=
1127-
Id.run <| as.mapM f
1127+
Id.run <| as.mapM (pure <| f ·)
11281128

11291129
instance : Functor Array where
11301130
map := map
@@ -1139,7 +1139,7 @@ valid.
11391139
-/
11401140
@[inline]
11411141
def mapFinIdx {α : Type u} {β : Type v} (as : Array α) (f : (i : Nat) → α → (h : i < as.size) → β) : Array β :=
1142-
Id.run <| as.mapFinIdxM f
1142+
Id.run <| as.mapFinIdxM (pure <| f · · ·)
11431143

11441144
/--
11451145
Applies a function to each element of the array along with the index at which that element is found,
@@ -1150,7 +1150,7 @@ is valid.
11501150
-/
11511151
@[inline]
11521152
def mapIdx {α : Type u} {β : Type v} (f : Nat → α → β) (as : Array α) : Array β :=
1153-
Id.run <| as.mapIdxM f
1153+
Id.run <| as.mapIdxM (pure <| f · ·)
11541154

11551155
/--
11561156
Pairs each element of an array with its index, optionally starting from an index other than `0`.
@@ -1198,7 +1198,7 @@ some 10
11981198
-/
11991199
@[inline]
12001200
def findSome? {α : Type u} {β : Type v} (f : α → Option β) (as : Array α) : Option β :=
1201-
Id.run <| as.findSomeM? f
1201+
Id.run <| as.findSomeM? (pure <| f ·)
12021202

12031203
/--
12041204
Returns the first non-`none` result of applying the function `f` to each element of the
@@ -1232,7 +1232,7 @@ Examples:
12321232
-/
12331233
@[inline]
12341234
def findSomeRev? {α : Type u} {β : Type v} (f : α → Option β) (as : Array α) : Option β :=
1235-
Id.run <| as.findSomeRevM? f
1235+
Id.run <| as.findSomeRevM? (pure <| f ·)
12361236

12371237
/--
12381238
Returns the last element of the array for which the predicate `p` returns `true`, or `none` if no
@@ -1244,7 +1244,7 @@ Examples:
12441244
-/
12451245
@[inline]
12461246
def findRev? {α : Type} (p : α → Bool) (as : Array α) : Option α :=
1247-
Id.run <| as.findRevM? p
1247+
Id.run <| as.findRevM? (pure <| p ·)
12481248

12491249
/--
12501250
Returns the index of the first element for which `p` returns `true`, or `none` if there is no such
@@ -1383,7 +1383,7 @@ Examples:
13831383
-/
13841384
@[inline]
13851385
def any (as : Array α) (p : α → Bool) (start := 0) (stop := as.size) : Bool :=
1386-
Id.run <| as.anyM p start stop
1386+
Id.run <| as.anyM (pure <| p ·) start stop
13871387

13881388
/--
13891389
Returns `true` if `p` returns `true` for every element of `as`.
@@ -1401,7 +1401,7 @@ Examples:
14011401
-/
14021402
@[inline]
14031403
def all (as : Array α) (p : α → Bool) (start := 0) (stop := as.size) : Bool :=
1404-
Id.run <| as.allM p start stop
1404+
Id.run <| as.allM (pure <| p ·) start stop
14051405

14061406
/--
14071407
Checks whether `a` is an element of `as`, using `==` to compare elements.
@@ -1670,7 +1670,7 @@ Example:
16701670
-/
16711671
@[inline]
16721672
def filterMap (f : α → Option β) (as : Array α) (start := 0) (stop := as.size) : Array β :=
1673-
Id.run <| as.filterMapM f (start := start) (stop := stop)
1673+
Id.run <| as.filterMapM (pure <| f ·) (start := start) (stop := stop)
16741674

16751675
/--
16761676
Returns the largest element of the array, as determined by the comparison `lt`, or `none` if

src/Init/Data/Array/BasicAux.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@ pointer equality, and does not allocate a new array if the result of each functi
8888
pointer-equal to its argument.
8989
-/
9090
@[inline] def Array.mapMono (as : Array α) (f : α → α) : Array α :=
91-
Id.run <| as.mapMonoM f
91+
Id.run <| as.mapMonoM (pure <| f ·)

src/Init/Data/Array/BinSearch.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,6 @@ Examples:
129129
* `#[].binInsert (· < ·) 1 = #[1]`
130130
-/
131131
@[inline] def binInsert {α : Type u} (lt : α → α → Bool) (as : Array α) (k : α) : Array α :=
132-
Id.run <| binInsertM lt (fun _ => k) (fun _ => k) as k
132+
Id.run <| binInsertM lt (fun _ => pure k) (fun _ => pure k) as k
133133

134134
end Array

src/Init/Data/Array/Lemmas.lean

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -613,13 +613,13 @@ theorem anyM_loop_cons [Monad m] {p : α → m Bool} {a : α} {as : List α} {st
613613

614614
-- Auxiliary for `any_iff_exists`.
615615
theorem anyM_loop_iff_exists {p : α → Bool} {as : Array α} {start stop} (h : stop ≤ as.size) :
616-
anyM.loop (m := Id) p as stop h start = true ↔
616+
(anyM.loop (m := Id) (pure <| p ·) as stop h start).run = true ↔
617617
∃ (i : Nat) (_ : i < as.size), start ≤ i ∧ i < stop ∧ p as[i] = true := by
618618
unfold anyM.loop
619619
split <;> rename_i h₁
620620
· dsimp
621621
split <;> rename_i h₂
622-
· simp only [true_iff]
622+
· simp only [true_iff, Id.run_pure]
623623
refine ⟨start, by omega, by omega, by omega, h₂⟩
624624
· rw [anyM_loop_iff_exists]
625625
constructor
@@ -636,9 +636,9 @@ termination_by stop - start
636636
-- This could also be proved from `SatisfiesM_anyM_iff_exists` in `Batteries.Data.Array.Init.Monadic`
637637
theorem any_iff_exists {p : α → Bool} {as : Array α} {start stop} :
638638
as.any p start stop ↔ ∃ (i : Nat) (_ : i < as.size), start ≤ i ∧ i < stop ∧ p as[i] := by
639-
dsimp [any, anyM, Id.run]
639+
dsimp [any, anyM]
640640
split
641-
· rw [anyM_loop_iff_exists]
641+
· rw [anyM_loop_iff_exists (p := p)]
642642
· rw [anyM_loop_iff_exists]
643643
constructor
644644
· rintro ⟨i, hi, ge, _, h⟩
@@ -1370,17 +1370,17 @@ theorem mapM_eq_mapM_toList [Monad m] [LawfulMonad m] {f : α → m β} {xs : Ar
13701370

13711371
@[deprecated "Use `mapM_eq_foldlM` instead" (since := "2025-01-08")]
13721372
theorem mapM_map_eq_foldl {as : Array α} {f : α → β} {i : Nat} :
1373-
mapM.map (m := Id) f as i b = as.foldl (start := i) (fun acc a => acc.push (f a)) b := by
1373+
mapM.map (m := Id) (pure <| f ·) as i b = pure (as.foldl (start := i) (fun acc a => acc.push (f a)) b) := by
13741374
unfold mapM.map
13751375
split <;> rename_i h
1376-
· simp only [Id.bind_eq]
1377-
dsimp [foldl, Id.run, foldlM]
1376+
· ext : 1
1377+
dsimp [foldl, foldlM]
13781378
rw [mapM_map_eq_foldl, dif_pos (by omega), foldlM.loop, dif_pos h]
13791379
-- Calling `split` here gives a bad goal.
13801380
have : size as - i = Nat.succ (size as - i - 1) := by omega
13811381
rw [this]
1382-
simp [foldl, foldlM, Id.run, Nat.sub_add_eq]
1383-
· dsimp [foldl, Id.run, foldlM]
1382+
simp [foldl, foldlM, Nat.sub_add_eq]
1383+
· dsimp [foldl, foldlM]
13841384
rw [dif_pos (by omega), foldlM.loop, dif_neg h]
13851385
rfl
13861386
termination_by as.size - i
@@ -1602,8 +1602,8 @@ theorem filterMap_congr {as bs : Array α} (h : as = bs)
16021602
as.toList ++ List.filterMap f xs := ?_
16031603
exact this #[]
16041604
induction xs
1605-
· simp_all [Id.run]
1606-
· simp_all [Id.run, List.filterMap_cons]
1605+
· simp_all
1606+
· simp_all [List.filterMap_cons]
16071607
split <;> simp_all
16081608

16091609
@[grind] theorem toList_filterMap {f : α → Option β} {xs : Array α} :
@@ -3215,18 +3215,16 @@ theorem foldlM_push [Monad m] [LawfulMonad m] {xs : Array α} {a : α} {f : β
32153215
rw [foldr, foldrM_start_stop, ← foldrM_toList, List.foldrM_pure, foldr_toList, foldr, ← foldrM_start_stop]
32163216

32173217
theorem foldl_eq_foldlM {f : β → α → β} {b} {xs : Array α} {start stop : Nat} :
3218-
xs.foldl f b start stop = xs.foldlM (m := Id) f b start stop := by
3219-
simp [foldl, Id.run]
3218+
xs.foldl f b start stop = (xs.foldlM (m := Id) (pure <| f · ·) b start stop).run := rfl
32203219

32213220
theorem foldr_eq_foldrM {f : α → β → β} {b} {xs : Array α} {start stop : Nat} :
3222-
xs.foldr f b start stop = xs.foldrM (m := Id) f b start stop := by
3223-
simp [foldr, Id.run]
3221+
xs.foldr f b start stop = (xs.foldrM (m := Id) (pure <| f · ·) b start stop).run := rfl
32243222

32253223
@[simp] theorem id_run_foldlM {f : β → α → Id β} {b} {xs : Array α} {start stop : Nat} :
3226-
Id.run (xs.foldlM f b start stop) = xs.foldl f b start stop := foldl_eq_foldlM.symm
3224+
Id.run (xs.foldlM f b start stop) = xs.foldl (f · · |>.run) b start stop := rfl
32273225

32283226
@[simp] theorem id_run_foldrM {f : α → β → Id β} {b} {xs : Array α} {start stop : Nat} :
3229-
Id.run (xs.foldrM f b start stop) = xs.foldr f b start stop := foldr_eq_foldrM.symm
3227+
Id.run (xs.foldrM f b start stop) = xs.foldr (f · · |>.run) b start stop := rfl
32303228

32313229
/-- Variant of `foldlM_reverse` with a side condition for the `stop` argument. -/
32323230
@[simp] theorem foldlM_reverse' [Monad m] {xs : Array α} {f : β → α → m β} {b} {stop : Nat}
@@ -3526,17 +3524,16 @@ theorem foldrM_append [Monad m] [LawfulMonad m] {f : α → β → m β} {b} {xs
35263524

35273525
@[simp] theorem foldr_append' {f : α → β → β} {b} {xs ys : Array α} {start : Nat}
35283526
(w : start = xs.size + ys.size) :
3529-
(xs ++ ys).foldr f b start 0 = xs.foldr f (ys.foldr f b) := by
3530-
subst w
3531-
simp [foldr_eq_foldrM]
3527+
(xs ++ ys).foldr f b start 0 = xs.foldr f (ys.foldr f b) :=
3528+
foldrM_append' w
35323529

35333530
@[grind _=_]theorem foldl_append {β : Type _} {f : β → α → β} {b} {xs ys : Array α} :
3534-
(xs ++ ys).foldl f b = ys.foldl f (xs.foldl f b) := by
3535-
simp [foldl_eq_foldlM]
3531+
(xs ++ ys).foldl f b = ys.foldl f (xs.foldl f b) :=
3532+
foldlM_append
35363533

35373534
@[grind _=_] theorem foldr_append {f : α → β → β} {b} {xs ys : Array α} :
3538-
(xs ++ ys).foldr f b = xs.foldr f (ys.foldr f b) := by
3539-
simp [foldr_eq_foldrM]
3535+
(xs ++ ys).foldr f b = xs.foldr f (ys.foldr f b) :=
3536+
foldrM_append
35403537

35413538
@[simp] theorem foldl_flatten' {f : β → α → β} {b} {xss : Array (Array α)} {stop : Nat}
35423539
(w : stop = xss.flatten.size) :
@@ -3565,21 +3562,22 @@ theorem foldrM_append [Monad m] [LawfulMonad m] {f : α → β → m β} {b} {xs
35653562
/-- Variant of `foldl_reverse` with a side condition for the `stop` argument. -/
35663563
@[simp] theorem foldl_reverse' {xs : Array α} {f : β → α → β} {b} {stop : Nat}
35673564
(w : stop = xs.size) :
3568-
xs.reverse.foldl f b 0 stop = xs.foldr (fun x y => f y x) b := by
3569-
simp [w, foldl_eq_foldlM, foldr_eq_foldrM]
3565+
xs.reverse.foldl f b 0 stop = xs.foldr (fun x y => f y x) b :=
3566+
foldlM_reverse' w
35703567

35713568
/-- Variant of `foldr_reverse` with a side condition for the `start` argument. -/
35723569
@[simp] theorem foldr_reverse' {xs : Array α} {f : α → β → β} {b} {start : Nat}
35733570
(w : start = xs.size) :
3574-
xs.reverse.foldr f b start 0 = xs.foldl (fun x y => f y x) b := by
3575-
simp [w, foldl_eq_foldlM, foldr_eq_foldrM]
3571+
xs.reverse.foldr f b start 0 = xs.foldl (fun x y => f y x) b :=
3572+
foldrM_reverse' w
35763573

35773574
@[grind] theorem foldl_reverse {xs : Array α} {f : β → α → β} {b} :
3578-
xs.reverse.foldl f b = xs.foldr (fun x y => f y x) b := by simp [foldl_eq_foldlM, foldr_eq_foldrM]
3575+
xs.reverse.foldl f b = xs.foldr (fun x y => f y x) b :=
3576+
foldlM_reverse
35793577

35803578
@[grind] theorem foldr_reverse {xs : Array α} {f : α → β → β} {b} :
35813579
xs.reverse.foldr f b = xs.foldl (fun x y => f y x) b :=
3582-
(foldl_reverse ..).symm.trans <| by simp
3580+
foldrM_reverse
35833581

35843582
theorem foldl_eq_foldr_reverse {xs : Array α} {f : β → α → β} {b} :
35853583
xs.foldl f b = xs.reverse.foldr (fun x y => f y x) b := by simp
@@ -4094,15 +4092,16 @@ abbrev all_mkArray := @all_replicate
40944092
/-! ### modify -/
40954093

40964094
@[simp] theorem size_modify {xs : Array α} {i : Nat} {f : α → α} : (xs.modify i f).size = xs.size := by
4097-
unfold modify modifyM Id.run
4095+
unfold modify modifyM
40984096
split <;> simp
40994097

41004098
theorem getElem_modify {xs : Array α} {j i} (h : i < (xs.modify j f).size) :
41014099
(xs.modify j f)[i] = if j = i then f (xs[i]'(by simpa using h)) else xs[i]'(by simpa using h) := by
4102-
simp only [modify, modifyM, Id.run, Id.pure_eq]
4100+
simp only [modify, modifyM]
41034101
split
4104-
· simp only [Id.bind_eq, getElem_set]; split <;> simp [*]
4105-
· rw [if_neg (mt (by rintro rfl; exact h) (by simp_all))]
4102+
· simp only [getElem_set, Id.run_pure, Id.run_bind]; split <;> simp [*]
4103+
· simp only [Id.run_pure]
4104+
rw [if_neg (mt (by rintro rfl; exact h) (by simp_all))]
41064105

41074106
@[simp] theorem toList_modify {xs : Array α} {f : α → α} {i : Nat} :
41084107
(xs.modify i f).toList = xs.toList.modify i f := by
@@ -4643,12 +4642,12 @@ namespace Array
46434642
@[simp] theorem findSomeRev?_eq_findSome?_reverse {f : α → Option β} {xs : Array α} :
46444643
xs.findSomeRev? f = xs.reverse.findSome? f := by
46454644
cases xs
4646-
simp [findSomeRev?, Id.run]
4645+
simp [findSomeRev?]
46474646

46484647
@[simp] theorem findRev?_eq_find?_reverse {f : α → Bool} {xs : Array α} :
46494648
xs.findRev? f = xs.reverse.find? f := by
46504649
cases xs
4651-
simp [findRev?, Id.run]
4650+
simp [findRev?]
46524651

46534652
/-! ### unzip -/
46544653

0 commit comments

Comments
 (0)