Bosatsu packages

Yichus/IO

Builtin package (resource io.bosatsu). This is the complete package source used by this build. The export list names its public API; definitions below give the types and behavior.

New to the API? Start with Make a data app or Build an API, then use this page to look up a definition.

package Yichus/IO

from Bosatsu/Predef import Int, String, List, Unit
from Zafu/Control/IterState import IterState

export (
  IO,
  pure, flat_map,
  capture, capture_formula,
  sequence, trace, random_int, current_time_millis,
  background, then_anyway, fold_until
)

exposes (Zafu/Control/IterState)

# IO produces a; it must not forget its result type at the FFI boundary.
external type IO[a: +*]

external def pure[a](value: a) -> IO[a]
external def flat_map[a, b](prog: IO[a], f: a -> IO[b]) -> IO[b]
external def capture[a](name: String, value: a) -> IO[a]
external def capture_formula[a](name: String, formula: String, value: a) -> IO[a]
external def sequence[a](progs: List[IO[a]]) -> IO[List[a]]
external def trace(unit: forall a. a) -> IO[String]
external def random_int(min: Int, max: Int) -> IO[Int]
external def current_time_millis(seed: Int) -> IO[Int]

# Running `background(work)` starts `work` and completes at once with
# Unit: the plan that ran it goes on without waiting for `work`, and
# `work`'s result is dropped. A failure in `work` is logged, never raised
# into the plan. Constructing `background(work)` runs nothing; it is an IO
# value like any other. `work` runs after the current step, concurrently
# with whatever runs next (later requests included), so a check of the
# program treats its operations as their own process.
external def background(work: IO[Unit]) -> IO[Unit]

# Running `then_anyway(first, next)` runs `first`, then runs `next` once
# `first` has finished or failed, and completes as `next` does. A failure
# in `first` is logged, never raised; a failure in `next` is raised as
# usual. Whether `next` runs does not depend on anything `first` did, which
# is what sets it apart from `flat_map(first, _ -> next)`: a check of the
# program does not count `first`'s effects as deciding that `next` runs.
# Constructing it runs nothing.
external def then_anyway(first: IO[Unit], next: IO[Unit]) -> IO[Unit]

# A bounded loop: running `fold_until(items, start, step)` runs
# `step(start, first)` for the first of `items`, then the step of each
# later item on the state the step before it carried on, and stops at the
# first step that answers Done(result), answering that Done(result). When
# every item has had its step and each answered Continue(state), it
# answers Continue with the last state, and Continue(start) when `items`
# is empty. So a loop runs at most one step per item, and `items` is its
# bound: `range(n)` for at most n steps, or the list a step reads one
# element of. Constructing it runs nothing. A check of the program reads
# `step` as the body every iteration runs, again until what the loop
# carries from one step to the next stops growing, with each iteration's
# effects after the iteration before it.
external def fold_until[a, c, d](items: List[a], start: c, step: (c, a) -> IO[IterState[c, d]]) -> IO[IterState[c, d]]