@@ -85,7 +85,7 @@ unsafe def fixl [Alternative m] (f : α → m (α × List β)) (s : α) : ListM
85
85
86
86
/-- Deconstruct a `ListM`, returning inside the monad an optional pair `α × ListM m α`
87
87
representing the head and tail of the list. -/
88
- unsafe def uncons {α : Type u} : ListM m α → m (Option (α × ListM m α))
88
+ unsafe def uncons : ListM m α → m (Option (α × ListM m α))
89
89
| nil => pure none
90
90
| cons l => do
91
91
let (x, xs) ← l
@@ -94,27 +94,27 @@ unsafe def uncons {α : Type u} : ListM m α → m (Option (α × ListM m α))
94
94
#align tactic.mllist.uncons ListM.uncons
95
95
96
96
/-- Compute, inside the monad, whether a `ListM` is empty. -/
97
- unsafe def isEmpty {α : Type u} (xs : ListM m α) : m (ULift Bool) :=
97
+ unsafe def isEmpty (xs : ListM m α) : m (ULift Bool) :=
98
98
(ULift.up ∘ Option.isSome) <$> uncons xs
99
99
#align tactic.mllist.empty ListM.isEmpty
100
100
101
101
/-- Convert a `List` to a `ListM`. -/
102
- unsafe def ofList {α : Type u} : List α → ListM m α
102
+ unsafe def ofList : List α → ListM m α
103
103
| [] => nil
104
104
| h :: t => cons (pure (h, ofList t))
105
105
#align tactic.mllist.of_list ListM.ofList
106
106
107
107
/-- The empty `ListM`. -/
108
- unsafe def empty {α : Type u} : ListM m α := ofList []
108
+ unsafe def empty : ListM m α := ofList []
109
109
110
110
/-- Convert a `List` of values inside the monad into a `ListM`. -/
111
- unsafe def ofListM {α : Type u} : List (m α) → ListM m α
111
+ unsafe def ofListM : List (m α) → ListM m α
112
112
| [] => nil
113
113
| h :: t => cons ((fun x => (x, ofListM t)) <$> some <$> h)
114
114
#align tactic.mllist.m_of_list ListM.ofListM
115
115
116
116
/-- Extract a list inside the monad from a `ListM`. -/
117
- unsafe def force {α} (L : ListM m α) : m (List α) := do
117
+ unsafe def force (L : ListM m α) : m (List α) := do
118
118
match ← uncons L with
119
119
| none => pure []
120
120
| some (x, xs) => (x :: ·) <$> force xs
@@ -142,7 +142,7 @@ unsafe def folds (f : β → α → β) (init : β) (L : ListM m α) : ListM m
142
142
L.foldsM (fun b a => pure (f b a)) init
143
143
144
144
/-- Take the first `n` elements, as a list inside the monad. -/
145
- unsafe def takeAsList {α} : ListM m α → Nat → m (List α)
145
+ unsafe def takeAsList : ListM m α → Nat → m (List α)
146
146
| _, 0 => pure []
147
147
| xs, n+1 => do
148
148
match ← uncons xs with
@@ -167,33 +167,33 @@ unsafe def drop : ListM m α → Nat → ListM m α
167
167
| none => return (none, empty)
168
168
169
169
/-- Apply a function which returns values in the monad to every element of a `ListM`. -/
170
- unsafe def mapM {α β : Type u} (f : α → m β) (L : ListM m α) : ListM m β :=
170
+ unsafe def mapM (f : α → m β) (L : ListM m α) : ListM m β :=
171
171
cons do match ← uncons L with
172
172
| some (x, xs) => return (← f x, mapM f xs)
173
173
| none => return (none, empty)
174
174
#align tactic.mllist.mmap ListM.mapM
175
175
176
176
/-- Apply a function to every element of a `ListM`. -/
177
- unsafe def map {α β : Type u} (f : α → β) (L : ListM m α) : ListM m β :=
177
+ unsafe def map (f : α → β) (L : ListM m α) : ListM m β :=
178
178
L.mapM fun a => pure (f a)
179
179
#align tactic.mllist.map ListM.map
180
180
181
181
/-- Filter a `ListM` using a monadic function. -/
182
- unsafe def filterM {α : Type u} (p : α → m (ULift Bool)) (L : ListM m α) : ListM m α :=
182
+ unsafe def filterM (p : α → m (ULift Bool)) (L : ListM m α) : ListM m α :=
183
183
cons do match ← uncons L with
184
184
| some (x, xs) => return (if (← p x).down then some x else none, filterM p xs)
185
185
| none => return (none, empty)
186
186
#align tactic.mllist.mfilter ListM.filterM
187
187
188
188
/-- Filter a `ListM`. -/
189
- unsafe def filter {α : Type u} (p : α → Bool) (L : ListM m α) : ListM m α :=
189
+ unsafe def filter (p : α → Bool) (L : ListM m α) : ListM m α :=
190
190
L.filterM fun a => pure <| .up (p a)
191
191
#align tactic.mllist.filter ListM.filter
192
192
193
193
/-- Filter and transform a `ListM` using a function that returns values inside the monad. -/
194
194
-- Note that the type signature has changed since Lean 3, when we allowed `f` to fail.
195
195
-- Use `try?` from `Mathlib.Control.Basic` to lift a possibly failing function to `Option`.
196
- unsafe def filterMapM {α β : Type u} (f : α → m (Option β)) (L : ListM m α) : ListM m β :=
196
+ unsafe def filterMapM (f : α → m (Option β)) (L : ListM m α) : ListM m β :=
197
197
cons do match ← uncons L with
198
198
| none => return (none, empty)
199
199
| some (x, xs) => match ← f x with
@@ -202,7 +202,7 @@ unsafe def filterMapM {α β : Type u} (f : α → m (Option β)) (L : ListM m
202
202
#align tactic.mllist.mfilter_map ListM.filterMapM
203
203
204
204
/-- Filter and transform a `ListM` using an `Option` valued function. -/
205
- unsafe def filterMap {α β : Type u} (f : α → Option β) : ListM m α → ListM m β :=
205
+ unsafe def filterMap (f : α → Option β) : ListM m α → ListM m β :=
206
206
filterMapM fun a => do pure (f a)
207
207
#align tactic.mllist.filter_map ListM.filterMap
208
208
@@ -217,14 +217,14 @@ unsafe def takeWhile (f : α → Bool) : ListM m α → ListM m α :=
217
217
takeWhileM fun a => pure (.up (f a))
218
218
219
219
/-- Concatenate two monadic lazy lists. -/
220
- unsafe def append {α : Type u} (L M : ListM m α) : ListM m α :=
220
+ unsafe def append (L M : ListM m α) : ListM m α :=
221
221
cons do match ← uncons L with
222
222
| none => return (none, M)
223
223
| some (x, xs) => return (some x, append xs M)
224
224
#align tactic.mllist.append ListM.append
225
225
226
226
/-- Join a monadic lazy list of monadic lazy lists into a single monadic lazy list. -/
227
- unsafe def join {α : Type u} (L : ListM m (ListM m α)) : ListM m α :=
227
+ unsafe def join (L : ListM m (ListM m α)) : ListM m α :=
228
228
cons do match ← uncons L with
229
229
| none => return (none, empty)
230
230
| some (x, xs) => match ← uncons x with
@@ -238,14 +238,14 @@ unsafe def squash (t : m (ListM m α)) : ListM m α :=
238
238
#align tactic.mllist.squash ListM.squash
239
239
240
240
/-- Enumerate the elements of a monadic lazy list, starting at a specified offset. -/
241
- unsafe def enum_from {α : Type u} (n : Nat) (L : ListM m α) : ListM m (Nat × α) :=
241
+ unsafe def enum_from (n : Nat) (L : ListM m α) : ListM m (Nat × α) :=
242
242
cons do match ← uncons L with
243
243
| none => return (none, empty)
244
244
| some (x, xs) => return (some (n, x), xs.enum_from (n+1 ))
245
245
#align tactic.mllist.enum_from ListM.enum_from
246
246
247
247
/-- Enumerate the elements of a monadic lazy list. -/
248
- unsafe def enum {α : Type u} : ListM m α → ListM m (Nat × α) :=
248
+ unsafe def enum : ListM m α → ListM m (Nat × α) :=
249
249
enum_from 0
250
250
#align tactic.mllist.enum ListM.enum
251
251
@@ -255,7 +255,7 @@ unsafe def range {m : Type → Type} [Alternative m] : ListM m Nat :=
255
255
#align tactic.mllist.range ListM.range
256
256
257
257
/-- Add one element to the end of a monadic lazy list. -/
258
- unsafe def concat {α : Type u} : ListM m α → α → ListM m α
258
+ unsafe def concat : ListM m α → α → ListM m α
259
259
| L, a => (ListM.ofList [L, ListM.ofList [a]]).join
260
260
#align tactic.mllist.concat ListM.concat
261
261
@@ -267,7 +267,7 @@ unsafe def zip (L : ListM m α) (M : ListM m β) : ListM m (α × β) :=
267
267
268
268
/-- Apply a function returning a monadic lazy list to each element of a monadic lazy list,
269
269
joining the results. -/
270
- unsafe def bind {α β : Type u} (L : ListM m α) (f : α → ListM m β) : ListM m β :=
270
+ unsafe def bind (L : ListM m α) (f : α → ListM m β) : ListM m β :=
271
271
cons do match ← uncons L with
272
272
| none => return (none, empty)
273
273
| some (x, xs) => match ← uncons (f x) with
@@ -287,13 +287,13 @@ unsafe def liftM [Monad n] [MonadLift m n] (L : ListM m α) : ListM n α :=
287
287
| some (a, L') => pure <| cons do pure (a, L'.liftM)
288
288
289
289
/-- Given a lazy list in a state monad, run it on some initial state, recording the states. -/
290
- unsafe def runState {σ α : Type u} (L : ListM (StateT.{u} σ m) α) (s : σ) : ListM m (α × σ) :=
290
+ unsafe def runState (L : ListM (StateT.{u} σ m) α) (s : σ) : ListM m (α × σ) :=
291
291
squash do match ← StateT.run (uncons L) s with
292
292
| (none, _) => pure empty
293
293
| (some (a, L'), s') => pure <| cons do pure (some (a, s'), L'.runState s')
294
294
295
295
/-- Given a lazy list in a state monad, run it on some initial state. -/
296
- unsafe def runState' {σ α : Type u} (L : ListM (StateT.{u} σ m) α) (s : σ) : ListM m α :=
296
+ unsafe def runState' (L : ListM (StateT.{u} σ m) α) (s : σ) : ListM m α :=
297
297
L.runState s |>.map (·.1 )
298
298
299
299
/-- Return the head of a monadic lazy list if it exists, as an `Option` in the monad. -/
0 commit comments