Skip to content

Commit

Permalink
feat: Array.join (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
kim-em authored Nov 28, 2023
1 parent c6e0dc3 commit d2d9ea1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Std/Data/Array/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ Unsafe implementation of `attach`, taking advantage of the fact that the represe
@[implemented_by attachImpl] def attach (xs : Array α) : Array {x // x ∈ xs} :=
⟨xs.data.pmap Subtype.mk fun _ => Array.Mem.mk⟩

/--
`O(|join L|)`. `join L` concatenates all the arrays in `L` into one array.
* `join #[#[a], #[], #[b, c], #[d, e, f]] = #[a, b, c, d, e, f]`
-/
@[inline] def join (l : Array (Array α)) : Array α := l.foldl (· ++ ·) #[]

end Array


Expand Down
29 changes: 29 additions & 0 deletions Std/Data/Array/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Authors: Mario Carneiro, Gabriel Ebner
-/
import Std.Data.Nat.Lemmas
import Std.Data.List.Lemmas
import Std.Data.Array.Basic
import Std.Tactic.HaveI
import Std.Tactic.Simpa

Expand Down Expand Up @@ -337,6 +338,34 @@ theorem forIn_eq_data_forIn [Monad m]
rw [loop (i := i)]; rw [← ij, Nat.succ_add]; rfl
simp [forIn, Array.forIn]; rw [loop (Nat.zero_add _)]; rfl

/-! ### join -/

@[simp] theorem join_data {l : Array (Array α)} : l.join.data = (l.data.map data).join := by
dsimp [join]
simp only [foldl_eq_foldl_data]
generalize l.data = l
have : ∀ a : Array α, (List.foldl ?_ a l).data = a.data ++ ?_ := ?_
exact this #[]
induction l with
| nil => simp
| cons h => induction h.data <;> simp [*]

theorem mem_join : ∀ {L : Array (Array α)}, a ∈ L.join ↔ ∃ l, l ∈ L ∧ a ∈ l := by
simp only [mem_def, join_data, List.mem_join, List.mem_map]
intro l
constructor
· rintro ⟨_, ⟨s, m, rfl⟩, h⟩
exact ⟨s, m, h⟩
· rintro ⟨s, h₁, h₂⟩
refine ⟨s.data, ⟨⟨s, h₁, rfl⟩, h₂⟩⟩

/-! ### append -/

@[simp] theorem mem_append {a : α} {s t : Array α} : a ∈ s ++ t ↔ a ∈ s ∨ a ∈ t := by
simp only [mem_def, append_data, List.mem_append]

/-! ### all -/

theorem all_iff_forall (p : α → Bool) (as : Array α) (start stop) :
all as p start stop ↔ ∀ i : Fin as.size, start ≤ i.1 ∧ i.1 < stop → p as[i] := by
have := SatisfiesM_anyM_iff_exists (m := Id) (fun a => ! p a) as start stop (! p as[·]) (by simp)
Expand Down

0 comments on commit d2d9ea1

Please sign in to comment.