Skip to content

Commit

Permalink
Trigger CI for leanprover/lean4#6123
Browse files Browse the repository at this point in the history
  • Loading branch information
leanprover-community-mathlib4-bot committed Nov 30, 2024
2 parents 955b85b + 030100b commit 04c565b
Show file tree
Hide file tree
Showing 21 changed files with 585 additions and 455 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/labels-from-status.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
if: github.event.pull_request.state == 'closed'
uses: actions-ecosystem/action-remove-labels@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
labels: |
WIP
awaiting-author
Expand All @@ -48,6 +49,7 @@ jobs:
! contains(github.event.pull_request.labels.*.name, 'WIP')
uses: actions-ecosystem/action-add-labels@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
labels: WIP

- name: Label unlabeled other PR as awaiting-review
Expand All @@ -59,4 +61,5 @@ jobs:
! contains(github.event.pull_request.labels.*.name, 'WIP')
uses: actions-ecosystem/action-add-labels@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
labels: awaiting-review
18 changes: 2 additions & 16 deletions Batteries/Data/Array/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,9 @@ This will perform the update destructively provided that `a` has a reference cou
abbrev setN (a : Array α) (i : Nat) (x : α) (h : i < a.size := by get_elem_tactic) : Array α :=
a.set i x

/--
`swapN a i j hi hj` swaps two `Nat` indexed entries in an `Array α`.
Uses `get_elem_tactic` to supply a proof that the indices are in range.
`hi` and `hj` are both given a default argument `by get_elem_tactic`.
This will perform the update destructively provided that `a` has a reference count of 1 when called.
-/
abbrev swapN (a : Array α) (i j : Nat)
(hi : i < a.size := by get_elem_tactic) (hj : j < a.size := by get_elem_tactic) : Array α :=
Array.swap a ⟨i,hi⟩ ⟨j, hj⟩
@[deprecated (since := "2024-11-24")] alias swapN := swap

/--
`swapAtN a i h x` swaps the entry with index `i : Nat` in the vector for a new entry `x`.
The old entry is returned alongwith the modified vector.
Automatically generates proof of `i < a.size` with `get_elem_tactic` where feasible.
-/
abbrev swapAtN (a : Array α) (i : Nat) (x : α) (h : i < a.size := by get_elem_tactic) :
α × Array α := swapAt a ⟨i,h⟩ x
@[deprecated (since := "2024-11-24")] alias swapAtN := swapAt

@[deprecated (since := "2024-11-20")] alias eraseIdxN := eraseIdx

Expand Down
16 changes: 1 addition & 15 deletions Batteries/Data/Array/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Gabriel Ebner
-/
import Batteries.Data.List.Lemmas
import Batteries.Data.List.FinRange
import Batteries.Data.Array.Basic
import Batteries.Tactic.SeqFocus
import Batteries.Util.ProofWanted
Expand All @@ -25,14 +26,6 @@ theorem forIn_eq_forIn_toList [Monad m]
@[deprecated (since := "2024-09-09")] alias data_zipWith := toList_zipWith
@[deprecated (since := "2024-08-13")] alias zipWith_eq_zipWith_data := data_zipWith

@[simp] theorem size_zipWith (as : Array α) (bs : Array β) (f : α → β → γ) :
(as.zipWith bs f).size = min as.size bs.size := by
rw [size_eq_length_toList, toList_zipWith, List.length_zipWith]

@[simp] theorem size_zip (as : Array α) (bs : Array β) :
(as.zip bs).size = min as.size bs.size :=
as.size_zipWith bs Prod.mk

/-! ### filter -/

theorem size_filter_le (p : α → Bool) (l : Array α) :
Expand Down Expand Up @@ -81,11 +74,6 @@ theorem size_set! (a : Array α) (i v) : (a.set! i v).size = a.size := by simp

/-! ### map -/

theorem mapM_empty [Monad m] (f : α → m β) : mapM f #[] = pure #[] := by
rw [mapM, mapM.map]; rfl

theorem map_empty (f : α → β) : map f #[] = #[] := mapM_empty f

/-! ### mem -/

theorem mem_singleton : a ∈ #[b] ↔ a = b := by simp
Expand Down Expand Up @@ -150,13 +138,11 @@ theorem getElem_insertIdx_loop_gt {as : Array α} {i : Nat} {j : Nat} {hj : j <
have : j - 1 < j := by omega
rw [getElem_insertIdx_loop_gt w]
rw [getElem_swap]
simp only [Fin.getElem_fin]
split <;> rename_i h₂
· rw [if_neg (by omega), if_neg (by omega)]
have t : k ≤ j := by omega
simp [t]
· rw [getElem_swap]
simp only [Fin.getElem_fin]
rw [if_neg (by omega)]
split <;> rename_i h₃
· simp [h₃]
Expand Down
10 changes: 5 additions & 5 deletions Batteries/Data/BinaryHeap.lean
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def heapifyUp (lt : α → α → Bool) (a : Vector α sz) (i : Fin sz) :
match i with
| ⟨0, _⟩ => a
| ⟨i'+1, hi⟩ =>
let j := i'/2, by get_elem_tactic⟩
let j := i'/2
if lt a[j] a[i] then
heapifyUp lt (a.swap i j) j
heapifyUp lt (a.swap i j) ⟨j, by get_elem_tactic⟩
else a

/-- `O(1)`. Build a new empty heap. -/
Expand Down Expand Up @@ -107,7 +107,7 @@ def popMax (self : BinaryHeap α lt) : BinaryHeap α lt :=
if h0 : self.size = 0 then self else
have hs : self.size - 1 < self.size := Nat.pred_lt h0
have h0 : 0 < self.size := Nat.zero_lt_of_ne_zero h0
let v := self.vector.swap ⟨_, h0⟩ ⟨_, hs⟩ |>.pop
let v := self.vector.swap _ _ h0 hs |>.pop
if h : 0 < self.size - 1 then
⟨heapifyDown lt v ⟨0, h⟩ |>.toArray⟩
else
Expand Down Expand Up @@ -136,7 +136,7 @@ def insertExtractMax (self : BinaryHeap α lt) (x : α) : α × BinaryHeap α lt
| none => (x, self)
| some m =>
if lt x m then
let v := self.vector.set 0, size_pos_of_max e⟩ x
let v := self.vector.set 0 x (size_pos_of_max e)
(m, ⟨heapifyDown lt v ⟨0, size_pos_of_max e⟩ |>.toArray⟩)
else (x, self)

Expand All @@ -145,7 +145,7 @@ def replaceMax (self : BinaryHeap α lt) (x : α) : Option α × BinaryHeap α l
match e : self.max with
| none => (none, ⟨self.vector.push x |>.toArray⟩)
| some m =>
let v := self.vector.set 0, size_pos_of_max e⟩ x
let v := self.vector.set 0 x (size_pos_of_max e)
(some m, ⟨heapifyDown lt v ⟨0, size_pos_of_max e⟩ |>.toArray⟩)

/-- `O(log n)`. Replace the value at index `i` by `x`. Assumes that `x ≤ self.get i`. -/
Expand Down
1 change: 1 addition & 0 deletions Batteries/Data/Fin.lean
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import Batteries.Data.Fin.Basic
import Batteries.Data.Fin.Fold
import Batteries.Data.Fin.Lemmas
11 changes: 7 additions & 4 deletions Batteries/Data/Fin/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ Copyright (c) 2017 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis, Keeley Hoek, Mario Carneiro
-/
import Batteries.Tactic.Alias
import Batteries.Data.Array.Basic
import Batteries.Data.List.Basic

namespace Fin

/-- `min n m` as an element of `Fin (m + 1)` -/
def clamp (n m : Nat) : Fin (m + 1) := ⟨min n m, Nat.lt_succ_of_le (Nat.min_le_right ..)⟩

/-- `enum n` is the array of all elements of `Fin n` in order -/
def enum (n) : Array (Fin n) := Array.ofFn id
@[deprecated (since := "2024-11-15")]
alias enum := Array.finRange

/-- `list n` is the list of all elements of `Fin n` in order -/
def list (n) : List (Fin n) := (enum n).toList
@[deprecated (since := "2024-11-15")]
alias list := List.finRange
47 changes: 47 additions & 0 deletions Batteries/Data/Fin/Fold.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/-
Copyright (c) 2024 François G. Dorais. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: François G. Dorais
-/
import Batteries.Data.List.FinRange

namespace Fin

theorem foldlM_eq_foldlM_finRange [Monad m] (f : α → Fin n → m α) (x) :
foldlM n f x = (List.finRange n).foldlM f x := by
induction n generalizing x with
| zero => simp
| succ n ih =>
rw [foldlM_succ, List.finRange_succ, List.foldlM_cons]
congr; funext
rw [List.foldlM_map, ih]

@[deprecated (since := "2024-11-19")]
alias foldlM_eq_foldlM_list := foldlM_eq_foldlM_finRange

theorem foldrM_eq_foldrM_finRange [Monad m] [LawfulMonad m] (f : Fin n → α → m α) (x) :
foldrM n f x = (List.finRange n).foldrM f x := by
induction n with
| zero => simp
| succ n ih => rw [foldrM_succ, ih, List.finRange_succ, List.foldrM_cons, List.foldrM_map]

@[deprecated (since := "2024-11-19")]
alias foldrM_eq_foldrM_list := foldrM_eq_foldrM_finRange

theorem foldl_eq_foldl_finRange (f : α → Fin n → α) (x) :
foldl n f x = (List.finRange n).foldl f x := by
induction n generalizing x with
| zero => rw [foldl_zero, List.finRange_zero, List.foldl_nil]
| succ n ih => rw [foldl_succ, ih, List.finRange_succ, List.foldl_cons, List.foldl_map]

@[deprecated (since := "2024-11-19")]
alias foldl_eq_foldl_list := foldl_eq_foldl_finRange

theorem foldr_eq_foldr_finRange (f : Fin n → α → α) (x) :
foldr n f x = (List.finRange n).foldr f x := by
induction n with
| zero => rw [foldr_zero, List.finRange_zero, List.foldr_nil]
| succ n ih => rw [foldr_succ, ih, List.finRange_succ, List.foldr_cons, List.foldr_map]

@[deprecated (since := "2024-11-19")]
alias foldr_eq_foldr_list := foldr_eq_foldr_finRange
73 changes: 0 additions & 73 deletions Batteries/Data/Fin/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -13,76 +13,3 @@ attribute [norm_cast] val_last
/-! ### clamp -/

@[simp] theorem coe_clamp (n m : Nat) : (clamp n m : Nat) = min n m := rfl

/-! ### enum/list -/

@[simp] theorem size_enum (n) : (enum n).size = n := Array.size_ofFn ..

@[simp] theorem enum_zero : (enum 0) = #[] := by simp [enum, Array.ofFn, Array.ofFn.go]

@[simp] theorem getElem_enum (i) (h : i < (enum n).size) : (enum n)[i] = ⟨i, size_enum n ▸ h⟩ :=
Array.getElem_ofFn ..

@[simp] theorem length_list (n) : (list n).length = n := by simp [list]

@[simp] theorem getElem_list (i : Nat) (h : i < (list n).length) :
(list n)[i] = cast (length_list n) ⟨i, h⟩ := by
simp only [list]; rw [← Array.getElem_eq_getElem_toList, getElem_enum, cast_mk]

@[deprecated getElem_list (since := "2024-06-12")]
theorem get_list (i : Fin (list n).length) : (list n).get i = i.cast (length_list n) := by
simp [getElem_list]

@[simp] theorem list_zero : list 0 = [] := by simp [list]

theorem list_succ (n) : list (n+1) = 0 :: (list n).map Fin.succ := by
apply List.ext_get; simp; intro i; cases i <;> simp

theorem list_succ_last (n) : list (n+1) = (list n).map castSucc ++ [last n] := by
rw [list_succ]
induction n with
| zero => simp
| succ n ih =>
rw [list_succ, List.map_cons castSucc, ih]
simp [Function.comp_def, succ_castSucc]

theorem list_reverse (n) : (list n).reverse = (list n).map rev := by
induction n with
| zero => simp
| succ n ih =>
conv => lhs; rw [list_succ_last]
conv => rhs; rw [list_succ]
simp [← List.map_reverse, ih, Function.comp_def, rev_succ]

/-! ### foldlM -/

theorem foldlM_eq_foldlM_list [Monad m] (f : α → Fin n → m α) (x) :
foldlM n f x = (list n).foldlM f x := by
induction n generalizing x with
| zero => simp
| succ n ih =>
rw [foldlM_succ, list_succ, List.foldlM_cons]
congr; funext
rw [List.foldlM_map, ih]

/-! ### foldrM -/

theorem foldrM_eq_foldrM_list [Monad m] [LawfulMonad m] (f : Fin n → α → m α) (x) :
foldrM n f x = (list n).foldrM f x := by
induction n with
| zero => simp
| succ n ih => rw [foldrM_succ, ih, list_succ, List.foldrM_cons, List.foldrM_map]

/-! ### foldl -/

theorem foldl_eq_foldl_list (f : α → Fin n → α) (x) : foldl n f x = (list n).foldl f x := by
induction n generalizing x with
| zero => rw [foldl_zero, list_zero, List.foldl_nil]
| succ n ih => rw [foldl_succ, ih, list_succ, List.foldl_cons, List.foldl_map]

/-! ### foldr -/

theorem foldr_eq_foldr_list (f : Fin n → α → α) (x) : foldr n f x = (list n).foldr f x := by
induction n with
| zero => rw [foldr_zero, list_zero, List.foldr_nil]
| succ n ih => rw [foldr_succ, ih, list_succ, List.foldr_cons, List.foldr_map]
2 changes: 2 additions & 0 deletions Batteries/Data/List.lean
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Batteries.Data.List.Basic
import Batteries.Data.List.Count
import Batteries.Data.List.EraseIdx
import Batteries.Data.List.FinRange
import Batteries.Data.List.Init.Lemmas
import Batteries.Data.List.Lemmas
import Batteries.Data.List.Matcher
import Batteries.Data.List.Monadic
import Batteries.Data.List.OfFn
import Batteries.Data.List.Pairwise
Expand Down
32 changes: 32 additions & 0 deletions Batteries/Data/List/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1032,3 +1032,35 @@ where
loop : List α → List α → List α
| [], r => reverseAux (a :: r) [] -- Note: `reverseAux` is tail recursive.
| b :: l, r => bif p b then reverseAux (a :: r) (b :: l) else loop l (b :: r)

/-- `dropPrefix? l p` returns
`some r` if `l = p' ++ r` for some `p'` which is paiwise `==` to `p`,
and `none` otherwise. -/
def dropPrefix? [BEq α] : List α → List α → Option (List α)
| list, [] => some list
| [], _ :: _ => none
| a :: as, b :: bs => if a == b then dropPrefix? as bs else none

/-- `dropSuffix? l s` returns
`some r` if `l = r ++ s'` for some `s'` which is paiwise `==` to `s`,
and `none` otherwise. -/
def dropSuffix? [BEq α] (l s : List α) : Option (List α) :=
let (r, s') := l.splitAt (l.length - s.length)
if s' == s then some r else none

/-- `dropInfix? l i` returns
`some (p, s)` if `l = p ++ i' ++ s` for some `i'` which is paiwise `==` to `i`,
and `none` otherwise.
Note that this is an inefficient implementation, and if computation time is a concern you should be
using the Knuth-Morris-Pratt algorithm as implemented in `Batteries.Data.List.Matcher`.
-/
def dropInfix? [BEq α] (l i : List α) : Option (List α × List α) :=
go l []
where
/-- Inner loop for `dropInfix?`. -/
go : List α → List α → Option (List α × List α)
| [], acc => if i.isEmpty then some (acc.reverse, []) else none
| a :: as, acc => match (a :: as).dropPrefix? i with
| none => go as (a :: acc)
| some s => (acc.reverse, s)
26 changes: 26 additions & 0 deletions Batteries/Data/List/FinRange.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/-
Copyright (c) 2024 François G. Dorais. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: François G. Dorais
-/
import Batteries.Data.List.OfFn

namespace List

@[deprecated (since := "2024-11-19")]
alias length_list := length_finRange

@[deprecated (since := "2024-11-19")]
alias getElem_list := getElem_finRange

@[deprecated (since := "2024-11-19")]
alias list_zero := finRange_zero

@[deprecated (since := "2024-11-19")]
alias list_succ := finRange_succ

@[deprecated (since := "2024-11-19")]
alias list_succ_last := finRange_succ_last

@[deprecated (since := "2024-11-19")]
alias list_reverse := finRange_reverse
Loading

0 comments on commit 04c565b

Please sign in to comment.