Compare commits

...

6 Commits

Author SHA1 Message Date
Sofia Rodrigues
a360070fd7 fix: remove inhabited 2025-08-14 23:23:35 -03:00
Sofia Rodrigues
6c582cc004 fix: remove inhabited 2025-08-08 17:42:19 -03:00
Sofia Rodrigues
928dd63138 Merge branch 'master' of https://github.com/leanprover/lean4 into sofia/fix-async 2025-08-08 17:08:48 -03:00
Sofia Rodrigues
d9000f97e4 test: remove useless message about other issue 2025-07-28 09:22:38 -03:00
Sofia Rodrigues
6eb2480e01 test: add test 2025-07-28 09:18:04 -03:00
Sofia Rodrigues
0693bd3ad7 fix: background function and forIn 2025-07-24 16:22:10 -03:00
2 changed files with 35 additions and 11 deletions

View File

@@ -681,22 +681,30 @@ instance : MonadLift BaseAsync (EAsync ε) where
@[inline]
protected partial def forIn
{β : Type} [i : Inhabited ε] (init : β)
{β : Type} (init : β)
(f : Unit β EAsync ε (ForInStep β))
(prio := Task.Priority.default) :
EAsync ε β := do
have : Nonempty β := init
let promise IO.Promise.new
let rec @[specialize] loop (b : β) : EAsync ε (ETask ε Unit) := async (prio := prio) do
let rec @[specialize] loop (b : β) : BaseIO Unit := do
match f () b with
| ForInStep.done b => promise.resolve (.ok b)
| ForInStep.yield b => discard <| (loop b)
| MaybeTask.pure result =>
match result with
| .error e => promise.resolve (.error e)
| .ok (.done b) => promise.resolve (.ok b)
| .ok (.yield b) => loop b
| MaybeTask.ofTask task => BaseIO.chainTask (prio := prio) task fun
| .error e => promise.resolve (.error e)
| .ok (.done b) => promise.resolve (.ok b)
| .ok (.yield b) => loop b
discard <| loop init
loop init
.mk <| EAsync.ofETask promise.result!
.mk <| BaseAsync.ofTask promise.result!
instance [Inhabited ε] : ForIn (EAsync ε) Lean.Loop Unit where
instance : ForIn (EAsync ε) Lean.Loop Unit where
forIn _ := EAsync.forIn
end EAsync
@@ -734,8 +742,8 @@ export MonadAwait (await)
This function transforms the operation inside the monad `m` into a task and let it run in the background.
-/
@[inline, specialize]
def background [Monad m] [MonadAsync t m] (prio := Task.Priority.default) : m α m Unit :=
discard async (t := t) (prio := prio)
def background [Monad m] [MonadAsync t m] (action : m α) (prio := Task.Priority.default) : m Unit :=
discard (async (t := t) (prio := prio) action)
/--
Runs two computations concurrently and returns both results as a pair.

View File

@@ -20,7 +20,7 @@ def loop : Async Nat := do
while res < 10 do
res := res + 1
discard <| t
discard t
pure res
@@ -29,3 +29,19 @@ info: 10
-/
#guard_msgs in
#eval IO.println =<< ETask.block =<< loop.asTask
def loopExcept : Async Nat := do
let mut res := 0
while res < 10 do
throw (.userError "some error")
discard t
pure res
/--
error: some error
-/
#guard_msgs in
#eval IO.println =<< ETask.block =<< loopExcept.asTask