Skip to content

Commit 8aff6a3

Browse files
committed
move, cleanup
1 parent e9166bf commit 8aff6a3

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

Batteries.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Batteries.Control.ForInStep
1111
import Batteries.Control.ForInStep.Basic
1212
import Batteries.Control.ForInStep.Lemmas
1313
import Batteries.Control.Lemmas
14+
import Batteries.Control.Monad
1415
import Batteries.Control.Nondet.Basic
1516
import Batteries.Data.Array
1617
import Batteries.Data.AssocList

Batteries/Control/Monad.lean

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/-
2+
Copyright (c) 2025 Lean FRO, LLC. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Kim Morrison
5+
-/
6+
7+
-- Note: this will be replaced by `_root_.map_inj_right_of_nonempty` in nightly-2025-02-07
8+
theorem _root_.LawfulFunctor.map_inj_right_of_nonempty [Functor f] [LawfulFunctor f] [Nonempty α]
9+
{g : α → β} (h : ∀ {x y : α}, g x = g y → x = y) {x y : f α} :
10+
g <$> x = g <$> y ↔ x = y := by
11+
constructor
12+
· open Classical in
13+
let g' a := if h : ∃ b, g b = a then h.choose else Classical.ofNonempty
14+
have g'g a : g' (g a) = a := by
15+
simp only [exists_apply_eq_apply, ↓reduceDIte, g']
16+
exact h (_ : ∃ b, g b = g a).choose_spec
17+
intro h'
18+
simpa only [Functor.map_map, g'g, id_map'] using congrArg (g' <$> ·) h'
19+
· intro h'
20+
rw [h']
21+
22+
-- Note: this will be replaced by `_root_.map_inj_right` in nightly-2025-02-07
23+
theorem _root_.LawfulMonad.map_inj_right [Monad m] [LawfulMonad m]
24+
{f : α → β} (h : ∀ {x y : α}, f x = f y → x = y) {x y : m α} :
25+
f <$> x = f <$> y ↔ x = y := by
26+
by_cases hempty : Nonempty α
27+
· exact LawfulFunctor.map_inj_right_of_nonempty h
28+
· constructor
29+
· intro h'
30+
have (z : m α) : z = (do let a ← z; let b ← pure (f a); x) := by
31+
conv => lhs; rw [← bind_pure z]
32+
congr; funext a
33+
exact (hempty ⟨a⟩).elim
34+
rw [this x, this y]
35+
rw [← bind_assoc, ← map_eq_pure_bind, h', map_eq_pure_bind, bind_assoc]
36+
· intro h'
37+
rw [h']

Batteries/Data/Vector/Monadic.lean

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Kim Morrison
55
-/
66
import Batteries.Classes.SatisfiesM
7+
import Batteries.Control.Monad
78
import Batteries.Data.Array.Monadic
89

910
namespace Vector
@@ -29,36 +30,6 @@ theorem toArray_mapIdxM [Monad m] [LawfulMonad m] (a : Vector α n) (f : Nat →
2930
toArray <$> a.mapIdxM f = a.toArray.mapIdxM f := by
3031
exact toArray_mapFinIdxM _ _
3132

32-
theorem _root_.LawfulFunctor.map_inj_right_of_nonempty [Functor f] [LawfulFunctor f] [Nonempty α]
33-
{g : α → β} (h : ∀ {x y : α}, g x = g y → x = y) {x y : f α} :
34-
g <$> x = g <$> y ↔ x = y := by
35-
constructor
36-
· open Classical in
37-
let g' a := if h : ∃ b, g b = a then h.choose else Classical.ofNonempty
38-
have g'g a : g' (g a) = a := by
39-
simp only [exists_apply_eq_apply, ↓reduceDIte, g']
40-
exact h (_ : ∃ b, g b = g a).choose_spec
41-
intro h'
42-
simpa only [Functor.map_map, g'g, id_map'] using congrArg (g' <$> ·) h'
43-
· intro h'
44-
rw [h']
45-
46-
theorem _root_.LawfulMonad.map_inj_right [Monad m] [LawfulMonad m]
47-
{f : α → β} (h : ∀ {x y : α}, f x = f y → x = y) {x y : m α} :
48-
f <$> x = f <$> y ↔ x = y := by
49-
by_cases hempty : Nonempty α
50-
· exact LawfulFunctor.map_inj_right_of_nonempty h
51-
· constructor
52-
· intro h'
53-
have (z : m α) : z = (do let a ← z; let b ← pure (f a); x) := by
54-
conv => lhs; rw [← bind_pure z]
55-
congr; funext a
56-
exact (hempty ⟨a⟩).elim
57-
rw [this x, this y]
58-
rw [← bind_assoc, ← map_eq_pure_bind, h', map_eq_pure_bind, bind_assoc]
59-
· intro h'
60-
rw [h']
61-
6233
theorem mapM_mk [Monad m] [LawfulMonad m] [MonadSatisfying m]
6334
(a : Array α) (h : a.size = n) (f : α → m β) :
6435
(Vector.mk a h).mapM f =

0 commit comments

Comments
 (0)