Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Data.Fin.Coding and Data.Fin.Enum #1007

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Batteries/Data/Fin.lean
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Batteries.Data.Fin.Basic
import Batteries.Data.Fin.Coding
import Batteries.Data.Fin.Enum
import Batteries.Data.Fin.Fold
import Batteries.Data.Fin.Lemmas
32 changes: 32 additions & 0 deletions Batteries/Data/Fin/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,35 @@ alias enum := Array.finRange

@[deprecated (since := "2024-11-15")]
alias list := List.finRange

/-- Sum of a list indexed by `Fin n`. -/
protected def sum [OfNat α (nat_lit 0)] [Add α] (x : Fin n → α) : α :=
foldr n (x · + ·) 0

/-- Product of a list indexed by `Fin n`. -/
protected def prod [OfNat α (nat_lit 1)] [Mul α] (x : Fin n → α) : α :=
foldr n (x · * ·) 1

/-- Count the number of true values of a decidable predicate on `Fin n`. -/
protected def count (P : Fin n → Prop) [DecidablePred P] : Nat :=
Fin.sum (if P · then 1 else 0)

/-- Find the first true value of a decidable predicate on `Fin n`, if there is one. -/
protected def find? (P : Fin n → Prop) [DecidablePred P] : Option (Fin n) :=
foldr n (fun i v => if P i then some i else v) none

/-- Custom recursor for `Fin (n+1)`. -/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this exactly the same as Fin.inductionOn?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it's also not needed here so it will eventually disappear.

def recZeroSuccOn {motive : Fin (n+1) → Sort _} (x : Fin (n+1))
(zero : motive 0) (succ : (x : Fin n) → motive x.castSucc → motive x.succ) : motive x :=
match x with
| 0 => zero
| ⟨x+1, hx⟩ =>
let x : Fin n := ⟨x, Nat.lt_of_succ_lt_succ hx⟩
succ x <| recZeroSuccOn x.castSucc zero succ

/-- Custom recursor for `Fin (n+1)`. -/
def casesZeroSuccOn {motive : Fin (n+1) → Sort _} (x : Fin (n+1))
(zero : motive 0) (succ : (x : Fin n) → motive x.succ) : motive x :=
match x with
| 0 => zero
| ⟨x+1, hx⟩ => succ ⟨x, Nat.lt_of_succ_lt_succ hx⟩
Loading
Loading