{"title": "The fiber machine",
 "producer": ["docs/site/guides/02-machine.md"],
 "format": "effect4-document/1",
 "body":
 [{"line":
   [{"text":
     "This guide explains the Effect4 fiber machine on one page. It defines how fibers execute, how stacks manage frames, how tapes supply decisions, how scopes finalize resources, and how runs advance."}],
   "kind": "para"},
  {"title": "What the machine is",
   "kind": "part",
   "key": "machine-overview",
   "body":
   [{"line":
     [{"text":
       "The fiber machine is an abstract state machine that executes compiled program code. Its state is represented by "},
      {"code": "RunMachine"},
      {"text": " ("},
      {"code": "src/Effect4/Machine/Fibers.lean"},
      {"text": ")."}],
     "kind": "para"},
    {"line": [{"text": "The machine holds:"}], "kind": "para"},
    {"lines":
     [[{"text": "Active and suspended fibers."}],
      [{"text":
        "Shared mutable stores for references, deferreds, and scopes."}],
      [{"text": "Fresh identifier counters."}],
      [{"text": "Queues for pending commands and external host answers."}],
      [{"text": "A trace of diagnostic execution events."}]],
     "kind": "items"},
    {"line":
     [{"text":
       "Every choice the machine cannot make alone is a decision on a tape. So a run is a function of its program and a tape that answers every decision. The scheduling rules are on the page "},
      {"link": "../../semantics/reactive-scheduling/",
       "label": "Reactive Scheduling"},
      {"text": "."}],
     "kind": "para"}]},
  {"title": "Fibers and the root",
   "kind": "part",
   "key": "fibers",
   "body":
   [{"line":
     [{"text":
       "A fiber is a thread of execution in the machine, identified by a "},
      {"code": "FiberId"},
      {"text":
       ". Each fiber holds its own continuation stack, its interruptibility and its context."}],
     "kind": "para"},
    {"line":
     [{"text":
       "A run begins with exactly one root fiber. The root fiber runs the loaded program ("},
      {"code": "Api.load"},
      {"text": ")."}],
     "kind": "para"},
    {"line":
     [{"text":
       "A running fiber can spawn child fibers using explicit fork operations. When a fiber terminates, it produces an exit value indicating success or failure with a cause."}],
     "kind": "para"}]},
  {"title": "Frames and continuation stacks",
   "kind": "part",
   "key": "frames",
   "body":
   [{"line":
     [{"text":
       "A continuation frame represents a defunctionalized step waiting for a sub-computation to finish. The machine stores frames in a first-order list on the fiber."}],
     "kind": "para"},
    {"line":
     [{"text":
       "When a fiber starts the first part of a sequence or of a handler, it pushes a frame that holds the rest. When that part finishes, the machine pops the top frame and gives it the result."}],
     "kind": "para"},
    {"line":
     [{"text":
       "Frames are first-order data, not host closures, so a machine state can be inspected and replayed."}],
     "kind": "para"}]},
  {"title": "Decisions and tapes",
   "kind": "part",
   "key": "decisions",
   "body":
   [{"line":
     [{"text": "A decision is one choice the host makes for the machine ("},
      {"code": "RunDecision"},
      {"text": ", "},
      {"code": "src/Effect4/Machine/Fibers.lean"},
      {"text":
       "). It evaluates a fiber, flushes the queued tasks, answers a host call, interrupts a fiber or advances the clock."}],
     "kind": "para"},
    {"line":
     [{"text":
       "A decision tape is the list of decisions, read in order. Because the decisions are data, a concurrent run can be replayed exactly."}],
     "kind": "para"},
    {"line":
     [{"text":
       "A program's full meaning is a relation over decision tapes. A tape that answers every decision fixes one run."}],
     "kind": "para"}]},
  {"title": "Live frontiers",
   "kind": "part",
   "key": "frontiers",
   "body":
   [{"line":
     [{"text":
       "A frontier is a machine state where the run waits. It waits for a budget, a host answer, a timer or a decision ("},
      {"code": "FrontierReason"},
      {"text": ")."}],
     "kind": "para"},
    {"line": [{"text": "A frontier is live:"}], "kind": "para"},
    {"lines":
     [[{"text": "A frontier is never a typed error."}],
      [{"text": "A frontier is never a defect or failure cause."}],
      [{"text": "A frontier is never a located refusal."}]],
     "kind": "items"},
    {"line":
     [{"text":
       "When a run reaches a frontier, the machine pauses cleanly with its state kept intact. Execution resumes whenever a caller provides additional budget or answers the pending choice."}],
     "kind": "para"}]},
  {"title": "Scopes and finalization",
   "kind": "part",
   "key": "scopes",
   "body":
   [{"line":
     [{"text": "A scope holds the finalizers that release its resources ("},
      {"code": "ScopeState"},
      {"text": ", "},
      {"code": "src/Effect4/Machine/Scope.lean"},
      {"text": "). The page "},
      {"link": "../../semantics/scope-lifetime-finalization/",
       "label": "Scope Lifetime and Finalization"},
      {"text": " gives its claims."}],
     "kind": "para"},
    {"line": [{"text": "When a scope closes:"}], "kind": "para"},
    {"lines":
     [[{"text": "it closes once, and a second close keeps the first exit;"}],
      [{"text":
        "a sequential scope runs its finalizers in the reverse order of their registration, each with the closing exit;"}],
      [{"text":
        "a finalizer registered after the close runs at once, with the closing exit."}]],
     "kind": "items"},
    {"line":
     [{"text":
       "A finalizer runs whether the work succeeded, failed or was interrupted, and it receives that exit."}],
     "kind": "para"}]},
  {"title": "Uninterruptible regions",
   "kind": "part",
   "key": "masks",
   "body":
   [{"line":
     [{"text":
       "An uninterruptible region saves the fiber's interruptibility, turns it off for the region, and restores the saved value at the region's end."}],
     "kind": "para"},
    {"line":
     [{"text":
       "An interrupt that arrives inside the region is recorded and deferred, never dropped. The fiber receives it when its interruptibility returns ("},
      {"code": "FrameFiber.deferredInterrupt"},
      {"text": ")."}],
     "kind": "para"},
    {"line":
     [{"text": "A "},
      {"code": "restore"},
      {"text":
       " inside the region gives its part the interruptibility saved at the region's entry. Inside an outer uninterruptible region, that part stays uninterruptible."}],
     "kind": "para"},
    {"line":
     [{"text":
       "An explicit close of a scope runs its finalizers uninterruptibly, as Effect 4.0.1 does (decisions row 359)."}],
     "kind": "para"}]},
  {"title": "How a run proceeds",
   "kind": "part",
   "key": "execution-flow",
   "body":
   [{"line":
     [{"text": "A run begins when "},
      {"code": "Api.load"},
      {"text":
       " places a program in the root fiber, with empty stores. The page "},
      {"link": "../../semantics/store-typing/", "label": "Store Typing"},
      {"text": " says what the stores may hold."}],
     "kind": "para"},
    {"line":
     [{"text":
       "At each step the tape's next decision says what the machine does, and the frame machine runs the chosen fiber's code."}],
     "kind": "para"},
    {"line":
     [{"text": "A run ends in one of three outcomes ("},
      {"code": "Outcome"},
      {"text": ", "},
      {"code": "src/Effect4/Api.lean"},
      {"text": "):"}],
     "kind": "para"},
    {"lines":
     [[{"text":
        "finished: the root fiber has exited, with a value or with a failure's cause;"}],
      [{"text":
        "a frontier: the run waits, live, for a budget, a host answer, a timer or a decision;"}],
      [{"text":
        "stuck: a halt on malformed code, which a typed program never reaches ("},
       {"code": "machineTyped_not_halted"},
       {"text": ")."}]],
     "kind": "items"}]}]}
