lean4-htt/Lake/Task.lean
2021-08-18 21:30:41 -04:00

60 lines
1.8 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
Copyright (c) 2021 Mac Malone. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mac Malone
-/
import Lake.Async
namespace Lake
-- # Tasks
instance : Monad Task where
map := Task.map
pure := Task.pure
bind := Task.bind
abbrev ETask ε α := ExceptT ε Task α
-- # IO Tasks
def IOTask α := ETask IO.Error α
instance : Monad IOTask := inferInstanceAs <| Monad (ETask IO.Error)
namespace IOTask
def spawn (act : IO α) (prio := Task.Priority.dedicated) : IO (IOTask α) :=
IO.asTask act prio
instance : Async IO IOTask := ⟨spawn⟩
def await (self : IOTask α) : IO α := do
IO.ofExcept (← IO.wait self)
instance : Await IOTask IO := ⟨await⟩
def mapAsync (f : α → IO β) (self : IOTask α) (prio := Task.Priority.dedicated) : IO (IOTask β) :=
IO.mapTask (fun x => do let x ← IO.ofExcept x; f x) self prio
instance : MapAsync IO IOTask := ⟨mapAsync⟩
def bindAsync (self : IOTask α) (f : α → IO (IOTask β)) (prio := Task.Priority.dedicated) : IO (IOTask β) :=
IO.bindTask self (fun x => do let x ← IO.ofExcept x; f x) prio
instance : BindAsync IO IOTask := ⟨bindAsync⟩
def seqLeftAsync (self : IOTask α) (act : IO β) (prio := Task.Priority.dedicated) : IO (IOTask α) :=
IO.mapTask (fun x => IO.ofExcept x <* act) self prio
instance : SeqLeftAsync IO IOTask := ⟨seqLeftAsync⟩
def seqRightAsync (self : IOTask α) (act : IO β) (prio := Task.Priority.dedicated) : IO (IOTask β) :=
IO.mapTask (fun x => IO.ofExcept x *> act) self prio
instance : SeqRightAsync IO IOTask := ⟨seqRightAsync⟩
instance : HAndThen (IOTask α) (IO β) (IO (IOTask β)) := ⟨seqRightAsync⟩
end IOTask
instance : HAndThen (List (IOTask α)) (IO β) (IO (IOTask β)) := ⟨afterTaskList⟩
instance : HAndThen (Array (IOTask α)) (IO β) (IO (IOTask β)) := ⟨afterTaskArray⟩