-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
249 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/- | ||
Copyright (c) 2024 Lean FRO, LLC. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Henrik Böving | ||
-/ | ||
prelude | ||
import Init.Core | ||
import Init.System.IO | ||
import Init.System.Promise | ||
|
||
namespace Std | ||
namespace Internal | ||
namespace IO | ||
namespace Async | ||
|
||
/-- | ||
A `Task` that may resolve to a value or an `IO.Error`. | ||
-/ | ||
def AsyncTask (α : Type u) : Type u := Task (Except IO.Error α) | ||
|
||
namespace AsyncTask | ||
|
||
/-- | ||
Construct an `AsyncTask` that is already resolved with value `x`. | ||
-/ | ||
@[inline] | ||
protected def pure (x : α) : AsyncTask α := Task.pure <| .ok x | ||
|
||
instance : Pure AsyncTask where | ||
pure := AsyncTask.pure | ||
|
||
/-- | ||
Create a new `AsyncTask` that will run after `x` has finished. | ||
If `x`: | ||
- errors, return an `AsyncTask` that reolves to the error. | ||
- succeeds, run `f` on the result of `x` and return the `AsyncTask` produced by `f`. | ||
-/ | ||
@[inline] | ||
protected def bind (x : AsyncTask α) (f : α → AsyncTask β) : AsyncTask β := | ||
Task.bind x fun r => | ||
match r with | ||
| .ok a => f a | ||
| .error e => Task.pure <| .error e | ||
|
||
/-- | ||
Create a new `AsyncTask` that will run after `x` has finished. | ||
If `x`: | ||
- errors, return an `AsyncTask` that reolves to the error. | ||
- succeeds, return an `AsyncTask` that resolves to `f x`. | ||
-/ | ||
@[inline] | ||
def map (f : α → β) (x : AsyncTask α) : AsyncTask β := | ||
Task.map (x := x) fun r => | ||
match r with | ||
| .ok a => .ok (f a) | ||
| .error e => .error e | ||
|
||
/-- | ||
Similar to `bind`, however `f` has access to the `IO` monad. If `f` throws an error, the returned | ||
`AsyncTask` resolves to that error. | ||
-/ | ||
@[inline] | ||
def bindIO (x : AsyncTask α) (f : α → IO (AsyncTask β)) : BaseIO (AsyncTask β) := | ||
IO.bindTask x fun r => | ||
match r with | ||
| .ok a => f a | ||
| .error e => .error e | ||
|
||
/-- | ||
Similar to `bind`, however `f` has access to the `IO` monad. If `f` throws an error, the returned | ||
`AsyncTask` resolves to that error. | ||
-/ | ||
@[inline] | ||
def mapIO (f : α → IO β) (x : AsyncTask α) : BaseIO (AsyncTask β) := | ||
IO.mapTask (t := x) fun r => | ||
match r with | ||
| .ok a => f a | ||
| .error e => .error e | ||
|
||
/-- | ||
Block until the `AsyncTask` in `x` finishes. | ||
-/ | ||
def block (x : AsyncTask α) : IO α := do | ||
let res := x.get | ||
match res with | ||
| .ok a => return a | ||
| .error e => .error e | ||
|
||
/-- | ||
Create an `AsyncTask` that resolves to the value of `x`. | ||
-/ | ||
@[inline] | ||
def ofPromise (x : IO.Promise α) : AsyncTask α := | ||
x.result.map pure | ||
|
||
/-- | ||
Obtain the `IO.TaskState` of `x`. | ||
-/ | ||
@[inline] | ||
def getState (x : AsyncTask α) : BaseIO IO.TaskState := | ||
IO.getTaskState x | ||
|
||
end AsyncTask | ||
|
||
end Async | ||
end IO | ||
end Internal | ||
end Std |
Oops, something went wrong.