Discharge¶
Discharge rewrites a computation that installs authored handlers to an equal one that installs none, with the answer of each handler clause in place of the request it answers. The compiled runtime and the transpile targets that render no handler read the result. The rewriting and its correctness are stated in the semantics of effects.
discharge
¶
Static discharge of authored handlers.
Discharge rewrites a checked computation to an equal one that installs no
handler. It evaluates the term with the reference machine's own rules and
symbolic values: a handle is a frame of an explicit continuation, a
request finds the innermost frame of its instance, the frame's clause body
runs with the continuation between the request and the frame captured, and
resume puts the captured continuation back with the parameters it names.
A value that a step computes is bound under a fresh name in the rewritten
term, a request no frame answers stays a request, and an if whose
condition is not a literal stays an if over the rewritten branches, which
meet again where both reach one continuation.
The four resumption grades are one rule. A clause that resumes twice puts its captured continuation back twice, and one that never resumes drops it. Surviving direct requests record the shot labels of the copied continuations that reach them and the ancestry of the linear operation clauses they run in. A surviving request inside a discharged shot keeps the computation on the reference machine if its operation has a checked unrestricted clause. A residual effectful call in a discharged clause keeps its reference continuation. A linear clause with effects can record the source sites of captured acyclic calls.
The result depends on no target and no runtime. The compiled runtime, the transpile planner, the static targets' projection, and the capability analysis read it, so a handler is discharged for all of them or for none.
DISCHARGE_STATEMENTS
module-attribute
¶
The most statements a discharged computation may have.
A clause that resumes m times copies the rest of the computation m
times, so n such requests in sequence yield m ** n copies, and a
conditional whose branches differ in k values is stated k times. A
computation past this bound is left as it is.
DISCHARGE_STEPS
module-attribute
¶
The most reduction steps one computation's discharge may take.
Discharge reduces a term without entering a recursive call, so it terminates. The bound stops a reduction whose copies produce no statement of their own.
ObstacleKind
¶
ObstacleKind = Literal['foreign', 'forwarding', 'recursion', 'case', 'growth', 'address', 'resumption-grade', 'tunnel']
What stops a handler from being discharged.
DischargeObstacle
didactic-model
¶
DischargeObstacle(*, computation: str, kind: ObstacleKind = field(opaque=True, extras=OPAQUE_BY_VALUE), reason: str, origin: SourceOrigin | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
Why a computation's handlers are not discharged.
| PARAMETER | DESCRIPTION |
|---|---|
computation
|
The computation left as it is.
TYPE:
|
kind
|
TYPE:
|
reason
|
The obstacle as a clause naming the handler and the clause, to follow "the computation".
TYPE:
|
origin
|
Where the obstacle is written, when the term records it.
TYPE:
|
DischargeRewrite
didactic-model
¶
DischargeRewrite(*, computation: ComputationId = field(opaque=True, extras=OPAQUE_BY_VALUE), method: Literal['handler-discharge', 'recursive-state', 'evidence-passing'], companions: tuple[ComputationId, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
A rewrite actually selected for a checked source computation.
| PARAMETER | DESCRIPTION |
|---|---|
computation
|
The computation whose body was replaced.
TYPE:
|
method
|
The checked transformation that replaced the body.
TYPE:
|
companions
|
First-order computations added by the recursive-state transformation, or callees specialized to evidence. Companions can also represent acyclic calls or shared pure transitions.
TYPE:
|
DischargedModule
didactic-model
¶
DischargedModule(*, module: QiecModule = field(opaque=True, extras=OPAQUE_BY_VALUE), obstacles: tuple[DischargeObstacle, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), rewrites: tuple[DischargeRewrite, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
A module whose computations install no handler that can be discharged.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The module with every dischargeable computation's body rewritten. A computation keeps its declared type, whose row may name an instance the rewritten body no longer requests.
TYPE:
|
obstacles
|
One obstacle per computation whose handlers remain.
TYPE:
|
rewrites
|
The checked rewrites actually applied, in source order.
TYPE:
|
obstacle
¶
obstacle(computation: str) -> DischargeObstacle | None
Why a computation's handlers remain.
| PARAMETER | DESCRIPTION |
|---|---|
computation
|
The computation's name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DischargeObstacle or None
|
The obstacle, or |
Source code in src/quivers/qiec/discharge.py
requester
¶
requester(request: EffectRequest) -> str | None
The handler clause a request is written in.
| PARAMETER | DESCRIPTION |
|---|---|
request
|
A request of a discharged body.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str or None
|
|
Source code in src/quivers/qiec/discharge.py
clause_requests
¶
clause_requests(handler: HandlerDef, names: Mapping[OperationId, str]) -> tuple[tuple[str, EffectRequest], ...]
The requests a handler's clause bodies make.
| PARAMETER | DESCRIPTION |
|---|---|
handler
|
The handler.
TYPE:
|
names
|
The source name of each operation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[tuple[str, EffectRequest], ...]
|
Each request with the clause making it, as |
Source code in src/quivers/qiec/discharge.py
installs_handler
¶
installs_handler(computation: NamedComputation) -> bool
Whether a computation's body installs a handler or makes an instance.
| PARAMETER | DESCRIPTION |
|---|---|
computation
|
The computation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True when a |
Source code in src/quivers/qiec/discharge.py
discharge_computation
¶
discharge_computation(computation: NamedComputation, module: QiecModule) -> NamedComputation | DischargeObstacle
Discharge one computation's handlers.
| PARAMETER | DESCRIPTION |
|---|---|
computation
|
A checked computation of the module.
TYPE:
|
module
|
The module, whose handlers and callees are read.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NamedComputation | DischargeObstacle
|
The computation with a body that installs no handler, or why its handlers remain. |
Source code in src/quivers/qiec/discharge.py
discharge_module
¶
discharge_module(module: QiecModule, *, evidence_passing: bool = True) -> DischargedModule
Discharge the handlers of every computation of a module.
Tail-resumptive handlers are compiled first, by evidence passing
(quivers.qiec.evidence_passing.pass_evidence), which needs no
continuation under any control. The handlers that remain are
discharged as follows. A shared acyclic call graph or a repeated pure
transition under an eligible state handler is specialized before
ordinary reduction can copy the bodies. The same companion transform
is available after a recursion obstacle.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The checked module.
TYPE:
|
evidence_passing
|
Whether tail-resumptive handlers are compiled by evidence passing. Without it every handler goes through the steps that follow, which is how the two compilations are compared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DischargedModule
|
The module itself when no computation installs a handler, else the module with every dischargeable computation rewritten and one obstacle per computation left as it is. |
Source code in src/quivers/qiec/discharge.py
Evidence passing¶
A tail-resumptive handler is compiled before the rewriting above: each request it answers becomes
the clause's code at the request site, and each computation the request is reached through is
specialized to the handler. continuation_captures lists every authored installation and every
resumption a module still contains, which is empty for a module whose handlers were all
tail-resumptive. The reference
describes when a handler qualifies.
evidence_passing
¶
Evidence-passing compilation of tail-resumptive handlers.
A handler clause is tail-resumptive when every path through it computes a
reply without performing an effect and then resumes once, in tail position,
with the handler's parameters unchanged. Such a clause never needs its
continuation as a value: answering the request and resuming is the same as
running the clause's code where the request was made and returning the
reply [@xie2020evidently; @xie2021generalized]. A handler whose clauses are
all tail-resumptive, and whose return clause makes no request, is compiled
by evidence passing: the installation binds the handler's parameters once,
each request it would answer becomes the clause's code inlined at the
request site, and each named computation the request is reached through is
specialized to that evidence, receiving the handler's parameters as extra
arguments. The compiled computation installs no handler and captures no
continuation, whatever its control: recursion, a case on a value known
only at run time, and loops are compiled alike.
Evidence passing is the first carrier class of the representing-monad classification: the carrier is the identity, read at the handler's parameters. It keeps the reference machine's outward requests and their addresses. An inlined clause allocates no call address, as a clause activation allocates none, and its calls advance the call counter exactly as the clause's own calls do. A specialized computation is entered at the call it replaces. Its call frame carries a new name, so evidence passing specializes only computations that make no outward request, where no address can observe the frame.
EvidenceObstacle
didactic-model
¶
EvidenceObstacle(*, computation: str, reason: str, origin: SourceOrigin | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
Why a computation's handlers are not compiled by evidence passing.
| PARAMETER | DESCRIPTION |
|---|---|
computation
|
The computation's name.
TYPE:
|
reason
|
The obstacle, as a clause to follow "the computation".
TYPE:
|
origin
|
Where the obstacle is written, when the term records it.
TYPE:
|
EvidenceRewrite
didactic-model
¶
EvidenceRewrite(*, computation: ComputationId = field(opaque=True, extras=OPAQUE_BY_VALUE), handlers: tuple[HandlerId, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), companions: tuple[ComputationId, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
One computation compiled by evidence passing.
| PARAMETER | DESCRIPTION |
|---|---|
computation
|
The computation whose body was rewritten.
TYPE:
|
handlers
|
The handlers it installed whose requests became inlined clause code.
TYPE:
|
companions
|
The computations specialized to that evidence, which the rewritten body and the companions themselves call.
TYPE:
|
EvidencePassing
didactic-model
¶
EvidencePassing(*, module: QiecModule = field(opaque=True, extras=OPAQUE_BY_VALUE), rewrites: tuple[EvidenceRewrite, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), obstacles: tuple[EvidenceObstacle, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
A module whose tail-resumptive handlers are compiled to direct code.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The module with every rewritten computation replaced and every specialized companion appended.
TYPE:
|
rewrites
|
One record per rewritten computation, in source order.
TYPE:
|
obstacles
|
One record per computation that installs a tail-resumptive handler and was left as it is.
TYPE:
|
ContinuationCapture
didactic-model
¶
Bases: QiecModel
One term of a module that can capture a continuation when it runs.
| PARAMETER | DESCRIPTION |
|---|---|
computation
|
The name of the computation the term is written in.
TYPE:
|
kind
|
TYPE:
|
handler
|
The installed handler's name, for a
TYPE:
|
evidence_obstacle
¶
evidence_obstacle(handler: HandlerDef) -> str | None
Why a handler is not compiled by evidence passing.
| PARAMETER | DESCRIPTION |
|---|---|
handler
|
A checked handler declaration.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str or None
|
|
Source code in src/quivers/qiec/evidence_passing.py
resumes_in_tail
¶
resumes_in_tail(handler: HandlerDef) -> bool
Whether every clause resumes once, in tail position, without effects.
Unlike is_tail_resumptive, a resumption may give the handler's
parameters new values, as a state handler's does.
| PARAMETER | DESCRIPTION |
|---|---|
handler
|
A checked handler declaration.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True when the handler is authored, introduces no effect, and every clause has grade 1 and ends every path in one tail resumption. |
Source code in src/quivers/qiec/evidence_passing.py
is_tail_resumptive
¶
is_tail_resumptive(handler: HandlerDef) -> bool
Whether a handler is compiled by evidence passing.
| PARAMETER | DESCRIPTION |
|---|---|
handler
|
A checked handler declaration.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True when |
Source code in src/quivers/qiec/evidence_passing.py
pass_evidence
¶
pass_evidence(module: QiecModule) -> EvidencePassing
Compile every tail-resumptive handler of a module by evidence passing.
Each computation that installs a tail-resumptive handler is compiled as a whole: every such installation becomes bound parameters and inlined clause code, and other installations stay. A computation for which some request, call, or function value cannot be compiled is left as it is, with the reason.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The checked module.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
EvidencePassing
|
The compiled module, the rewrites, and the obstacles. |
Source code in src/quivers/qiec/evidence_passing.py
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 | |
continuation_captures
¶
continuation_captures(module: QiecModule) -> tuple[ContinuationCapture, ...]
Every term of a module's computations that can capture a continuation.
A request captures the continuation up to the authored handler that answers it, and a resumption reinstates one. A module with no authored installation and no resumption in any computation captures none: every request it makes is answered by a foreign provider or leaves the module, and every clause it runs is inlined code.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The module.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[ContinuationCapture, ...]
|
One record per authored installation and per resumption, in computation order. |