Skip to content

Commit c660824

Browse files
maxwell-thumParcly-Taxelthorimur
committed
feat: port Logic.Equiv.Array (#1733)
Co-authored-by: Parcly Taxel <reddeloostw@gmail.com> Co-authored-by: thorimur <68410468+thorimur@users.noreply.github.com>
1 parent 8a1d352 commit c660824

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

Mathlib.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,7 @@ import Mathlib.Logic.Embedding.Basic
21532153
import Mathlib.Logic.Embedding.Set
21542154
import Mathlib.Logic.Encodable.Basic
21552155
import Mathlib.Logic.Encodable.Lattice
2156+
import Mathlib.Logic.Equiv.Array
21562157
import Mathlib.Logic.Equiv.Basic
21572158
import Mathlib.Logic.Equiv.Defs
21582159
import Mathlib.Logic.Equiv.Embedding

Mathlib/Logic/Equiv/Array.lean

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/-
2+
Copyright (c) 2018 Mario Carneiro. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Mario Carneiro
5+
6+
! This file was ported from Lean 3 source module logic.equiv.array
7+
! leanprover-community/mathlib commit 1126441d6bccf98c81214a0780c73d499f6721fe
8+
! Please do not edit these lines, except to modify the commit id
9+
! if you have ported upstream changes.
10+
-/
11+
import Mathlib.Data.Vector.Basic
12+
import Mathlib.Logic.Equiv.List
13+
import Mathlib.Control.Traversable.Equiv
14+
15+
/-!
16+
# Equivalences involving `Array`
17+
-/
18+
19+
/-
20+
21+
Porting note:
22+
23+
The following commented-out definitions only made sense for the mathlib3 datatypes `d_array` and
24+
`array`. `d_array` (a dependent array) does not yet (as of Jun 27 2023) have a corresponding
25+
datatype in lean4/std4/mathlib4; `array` was length-indexed and therefore more similar to `Vector`,
26+
which may be reimplemented in terms of `Array` internally anyway in the future.
27+
28+
However, we have tried to align `array` with `Array` where possible nonetheless, and therefore we
29+
introduce the "right" equivalence for `Array` (`arrayEquivList`) and align the instances
30+
`array.encodable`, `array.countable` with `Array.encodable`, `Array.countable` respectively.
31+
32+
-/
33+
34+
namespace Equiv
35+
36+
-- /-- The natural equivalence between length-`n` heterogeneous arrays
37+
-- and dependent functions from `fin n`. -/
38+
-- def darrayEquivFin {n : ℕ} (α : Fin n → Type _) : DArray n α ≃ ∀ i, α i :=
39+
-- ⟨DArray.read, DArray.mk, fun ⟨f⟩ => rfl, fun f => rfl⟩
40+
#noalign equiv.d_array_equiv_fin
41+
42+
-- /-- The natural equivalence between length-`n` arrays and functions from `fin n`. -/
43+
-- def array'EquivFin (n : ℕ) (α : Type _) : Array' n α ≃ (Fin n → α) :=
44+
-- darrayEquivFin _
45+
#noalign equiv.array_equiv_fin
46+
47+
-- /-- The natural equivalence between length-`n` vectors and length-`n` arrays. -/
48+
-- def vectorEquivArray' (α : Type _) (n : ℕ) : Vector α n ≃ Array' n α :=
49+
-- (vectorEquivFin _ _).trans (array'EquivFin _ _).symm
50+
#noalign equiv.vector_equiv_array
51+
52+
/-- The natural equivalence between arrays and lists. -/
53+
def arrayEquivList (α : Type _) : Array α ≃ List α :=
54+
⟨Array.data, Array.mk, fun _ => rfl, fun _ => rfl⟩
55+
56+
end Equiv
57+
58+
/- Porting note: removed instances for what would be ported as `Traversable (Array α)` and
59+
`IsLawfulTraversable (Array α)`. These would
60+
61+
1. be implemented directly in terms of `Array` functionality for efficiency, rather than being the
62+
traversal of some other type transported along an equivalence to `Array α` (as the traversable
63+
instance for `array` was)
64+
65+
2. belong in `Mathlib.Control.Traversable.Instances` instead of this file. -/
66+
67+
-- namespace Array'
68+
69+
-- open Function
70+
71+
-- variable {n : ℕ}
72+
73+
-- instance : Traversable (Array' n) :=
74+
-- @Equiv.traversable (flip Vector n) _ (fun α => Equiv.vectorEquivArray α n) _
75+
76+
-- instance : IsLawfulTraversable (Array' n) :=
77+
-- @Equiv.isLawfulTraversable (flip Vector n) _ (fun α => Equiv.vectorEquivArray α n) _ _
78+
79+
-- end Array'
80+
81+
/-- If `α` is encodable, then so is `Array α`. -/
82+
instance Array.encodable {α} [Encodable α] : Encodable (Array α) :=
83+
Encodable.ofEquiv _ (Equiv.arrayEquivList _)
84+
#align array.encodable Array.encodable
85+
86+
/-- If `α` is countable, then so is `Array α`. -/
87+
instance Array.countable {α} [Countable α] : Countable (Array α) :=
88+
Countable.of_equiv _ (Equiv.arrayEquivList α).symm
89+
#align array.countable Array.countable

0 commit comments

Comments
 (0)