Skip to content

Commit 3f35515

Browse files
committed
Added tuple conversions for Vector and BitVec
1 parent 92a17c0 commit 3f35515

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

RecMath/SortingNetwork.lean

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,38 @@ variable {α : Type u} [LinearOrder α] {β : Type v} [LinearOrder β] {n : Nat}
1212

1313
def Tuple (α n) := Fin n -> α
1414

15+
def Tuple.ofVector : Vector α n -> Tuple α n := Vector.get
16+
def Tuple.toVector : Tuple α n -> Vector α n := Vector.ofFn
17+
18+
def Tuple.equivVector : Tuple α n ≃ Vector α n where
19+
toFun := Tuple.toVector
20+
invFun := Tuple.ofVector
21+
left_inv t := by funext i; exact Vector.get_ofFn t i
22+
right_inv v := by
23+
apply Function.rightInverse_of_injective_of_leftInverse
24+
· intro a b h
25+
unfold Tuple.ofVector at h
26+
ext i hi
27+
let fi := Fin.mk i hi
28+
rw [<- Vector.get_eq_getElem a fi, <- Vector.get_eq_getElem b fi, h]
29+
· intro t
30+
funext i
31+
exact Vector.get_ofFn t i
32+
33+
def Tuple.ofBitVec : BitVec n -> Tuple Bool n := BitVec.getLsb
34+
def Tuple.toBitVec : Tuple Bool n -> BitVec n := BitVec.ofFnLE
35+
36+
def Tuple.equivBitVec : Tuple Bool n ≃ BitVec n where
37+
toFun := Tuple.toBitVec
38+
invFun := Tuple.ofBitVec
39+
left_inv t := by
40+
funext i
41+
exact BitVec.getLsb_ofFnLE t i
42+
right_inv bv := by
43+
unfold ofBitVec
44+
unfold toBitVec
45+
grind only [= BitVec.getLsb_eq_getElem, = BitVec.getElem_ofFnLE, = Fin.getElem_fin]
46+
1547
-- Not using these anymore, but i can change my definitoins to use them if wanted
1648
-- def Sorted (t : Tuple α n) : Prop := Monotone t
1749
-- def SortingFunction (f : Tuple α n -> Tuple α n) : Prop := ∀ (t : Tuple α n), Sorted (f t)
@@ -49,6 +81,15 @@ def IndexPair.toPerm (t : Tuple α n) (p : IndexPair n) : Equiv.Perm (Fin n) :=
4981
def IndexPair.apply (p : IndexPair n) (t : Tuple α n) : Tuple α n :=
5082
t ∘ p.toPerm t
5183

84+
def IndexPair.vector_apply (p : IndexPair n) (v : Vector α n) : Vector α n :=
85+
if v.get p.i ≤ v.get p.j then
86+
v
87+
else
88+
v.swap p.i p.j
89+
90+
example (p : IndexPair n) (v : Vector α n) : p.vector_apply v = Tuple.toVector (p.apply (Tuple.ofVector v)) := by
91+
sorry
92+
5293
@[simp, grind =]
5394
theorem IndexPair.apply_i_eq_min {p : IndexPair n} {t : Tuple α n} :
5495
p.apply t p.i = min (t p.i) (t p.j) := by
@@ -79,7 +120,7 @@ theorem IndexPair.apply.monotoneOn_ij {p : IndexPair n} {t : Tuple α n} :
79120
subst i_eq_a j_eq_b
80121
exact apply_i_le_apply_j
81122

82-
@[simp, grind =]
123+
@[grind =]
83124
theorem IndexPair.apply_of_ne_of_ne {p : IndexPair n} {t : Tuple α n} {x : Fin n} :
84125
x ≠ p.i -> x ≠ p.j -> p.apply t x = t x := by
85126
intro x_ne_i x_ne_j
@@ -178,8 +219,6 @@ def ComparisonNetwork.upgrade.sortsOn_old {net : ComparisonNetwork n} {skip : Fi
178219
conv at h => intro t; rw [<- monotoneOn_univ]
179220
let := Fin.succAboveEmb skip
180221

181-
182-
183222
intro a ha b hb a_le_b
184223
rw [Set.mem_setOf_eq] at ha hb
185224
-- rw [upgrade, List.map_eq_foldr]
@@ -303,12 +342,13 @@ def extend_fin.equiv {n : Nat} : Fin n ≃ {x : Fin (n + 1) // x < n } where
303342
left_inv _ := rfl
304343
right_inv _ := rfl
305344

306-
theorem monotone_perm_eq_one {n : Nat} {p : Equiv.Perm (Fin n)} (h : Monotone p) : p = 1 := by
307-
unfold Monotone at h
345+
theorem monotone_perm_eq_one {n : Nat} {p : Equiv.Perm (Fin n)} (mono : Monotone p) : p = 1 := by
346+
-- unfold Monotone at mono
308347
-- Induct and try to transfer proofs using MonotoneOn then expand the set
309348
induction n with
310349
| zero => exact Subsingleton.allEq p 1
311350
| succ n h =>
351+
specialize @h 1
312352
change p = Equiv.refl _
313353
rw [<- Equiv.Perm.extendDomain_refl extend_fin.equiv]
314354
sorry
@@ -366,6 +406,7 @@ theorem ComparisonNetwork.zero_one_principle (net : ComparisonNetwork n) :
366406
simp
367407
push_neg
368408
simp [f, Bool.le_iff_imp] at fswap
409+
sorry
369410

370411
order
371412

@@ -382,7 +423,6 @@ theorem ComparisonNetwork.zero_one_principle (net : ComparisonNetwork n) :
382423

383424
-- sorry
384425

385-
sorry
386426

387427
-- Using the zero-one principle i could implement Decidable ComparisonNetwork.Sorts
388428
-- in O(2ⁿ) instead of O(n!).

lake-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "git",
66
"subDir": null,
77
"scope": "leanprover-community",
8-
"rev": "3da1779e02916f017d8007355ae936f6d49f7964",
8+
"rev": "b557810af9b33eb7a2cede79422cefb8c7c12bd2",
99
"name": "mathlib",
1010
"manifestFile": "lake-manifest.json",
1111
"inputRev": "master",

0 commit comments

Comments
 (0)