Coverage¶
These functions refine indices in the branches of a case analysis over a GADT (generalized algebraic data type) (Xi et al., 2003) and check that the case covers every constructor. Matching an empty vector, for instance, fixes its length at zero. Coverage checking uses such refinements to tell a missing case from a branch the indices make impossible.
coverage
¶
GADT branch refinement and conservative coverage interfaces.
IndexEquation
¶
IndexEquation = tuple[StaticArgument, StaticArgument]
Two static terms an enclosing branch has shown equal.
CoverageStatus
¶
Bases: str, Enum
Whether a set of branch patterns covers a family.
Reachability
¶
Bases: str, Enum
Whether a branch can be entered, as far as its equalities decide.
BranchPattern
didactic-model
¶
BranchPattern(*, constructor: ConstructorId | None = field(opaque=True, extras=OPAQUE_BY_VALUE), guarded: bool = False)
Bases: QiecModel
The shape of one case branch, as coverage sees it.
| PARAMETER | DESCRIPTION |
|---|---|
constructor
|
The constructor the branch matches, or
TYPE:
|
guarded
|
Whether the branch carries a guard that may reject a matching value, which keeps it from counting toward completeness.
TYPE:
|
CoverageResult
didactic-model
¶
CoverageResult(*, status: CoverageStatus = field(opaque=True, extras=OPAQUE_BY_VALUE), missing: tuple[ConstructorId, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), duplicates: tuple[ConstructorId, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
The outcome of checking branch patterns against a family.
| PARAMETER | DESCRIPTION |
|---|---|
status
|
Whether the patterns cover the family.
TYPE:
|
missing
|
Constructors no pattern matches.
TYPE:
|
duplicates
|
Constructors more than one pattern matches.
TYPE:
|
BranchRefinement
didactic-model
¶
BranchRefinement(*, skolems: tuple[str, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), givens: tuple[BranchGiven, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), reachability: Reachability = field(default=UNKNOWN, opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
Rigid skolems and local equalities available inside one branch.
| PARAMETER | DESCRIPTION |
|---|---|
skolems
|
The names of the branch's rigid static variables.
TYPE:
|
givens
|
The equalities the branch's constructor makes available.
TYPE:
|
reachability
|
Whether the givens are jointly satisfiable, as far as the unifier decides.
TYPE:
|
given_equations
¶
given_equations(givens: tuple[BranchGiven, ...]) -> tuple[IndexEquation, ...]
The equations a context's branch givens state.
| PARAMETER | DESCRIPTION |
|---|---|
givens
|
Evidence introduced by enclosing case branches.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[IndexEquation, ...]
|
Each given's two sides. |
Source code in src/quivers/qiec/coverage.py
solve_equations
¶
solve_equations(equations: tuple[IndexEquation, ...]) -> StaticSubstitution | None
The most general unifier of static equations, or None if they contradict.
Every static variable, rigid or named, is an unknown. The result is
sound for both uses the checker makes of it: when it is None no
instantiation satisfies the equations, so a branch that requires
them is unreachable; and two terms it makes syntactically equal are
equal under every instantiation that satisfies them.
| PARAMETER | DESCRIPTION |
|---|---|
equations
|
The equations.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
StaticSubstitution or None
|
The unifier, applied transitively by |
Source code in src/quivers/qiec/coverage.py
apply_unifier
¶
apply_unifier(term: StaticArgument, unifier: StaticSubstitution) -> StaticArgument
A term with a unifier applied.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
unifier
|
A result of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
StaticArgument
|
The term with every solved variable replaced. |
Source code in src/quivers/qiec/coverage.py
equal_under
¶
equal_under(left: StaticArgument, right: StaticArgument, equations: tuple[IndexEquation, ...]) -> bool
Whether two static terms are equal given some equations.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
The terms.
TYPE:
|
right
|
The terms.
TYPE:
|
equations
|
Equations known to hold, such as a branch's givens.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True when the terms are syntactically equal, when the equations contradict (the position is unreachable), or when the equations' most general unifier makes them equal. |
Source code in src/quivers/qiec/coverage.py
check_coverage
¶
check_coverage(family: FamilyDecl, patterns: tuple[BranchPattern, ...], *, certified_impossible: frozenset[ConstructorId] = frozenset()) -> CoverageResult
Check constructor coverage without pretending opaque cases are total.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family being matched on.
TYPE:
|
patterns
|
The branch patterns written, in source order.
TYPE:
|
certified_impossible
|
Constructors an equality solver has proven cannot occur, which may therefore be omitted without making the match incomplete.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CoverageResult
|
The status, the constructors still missing, and any matched
twice. The status is |
Source code in src/quivers/qiec/coverage.py
refine_branch
¶
refine_branch(family: FamilyDecl, constructor: ConstructorDecl, scrutinee: TypeApplication, constructor_arguments: tuple[StaticArgument, ...], assumptions: tuple[IndexEquation, ...] = ()) -> BranchRefinement
Introduce constructor skolems and index equalities for a branch.
The branch's equations (each scrutinee index equals the constructor's
result index) are solved by first-order unification together with the
equations of the enclosing branches. Indices the enclosing equations
already make equal are reachable; equations with no solution, because
distinct constructors would have to be equal at some depth, are
impossible; all others remain UNKNOWN and still contribute branch
effects and coverage.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family being matched on.
TYPE:
|
constructor
|
The constructor this branch matches.
TYPE:
|
scrutinee
|
The applied family type being scrutinised, supplying the parameters and the indices to be refined.
TYPE:
|
constructor_arguments
|
Arguments instantiating the constructor's local telescope,
normally the rigid skolems from
TYPE:
|
assumptions
|
Equations the enclosing branches established, which the branch's own equations are solved together with.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BranchRefinement
|
The skolems in scope, the equalities the match makes available, and whether the branch is reachable. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the constructor belongs to another family, if the scrutinee does not inhabit the matched family, or if the scrutinee supplies the wrong number of arguments. |
Source code in src/quivers/qiec/coverage.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
constructor_skolems
¶
constructor_skolems(constructor: ConstructorDecl, scope: StaticScopeId) -> tuple[StaticArgument, ...]
Create rigid static variables for a constructor's local telescope.
| PARAMETER | DESCRIPTION |
|---|---|
constructor
|
The constructor whose telescope to instantiate.
TYPE:
|
scope
|
Identity of the branch scope. It enters each variable's identity, so skolems from two branches are never confused and cannot be mistaken for one another when a type escapes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[StaticArgument, ...]
|
One rigid variable per binder, in telescope order. |
Source code in src/quivers/qiec/coverage.py
check_indexed_coverage
¶
check_indexed_coverage(family: FamilyDecl, constructors: tuple[ConstructorDecl, ...], scrutinee: TypeApplication, patterns: tuple[BranchPattern, ...], assumptions: tuple[IndexEquation, ...] = ()) -> CoverageResult
Coverage with omissions justified by closed-index contradiction.
Only constructors that the small equality solver certifies as impossible may be omitted. An unknown result remains a required case.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family being matched on.
TYPE:
|
constructors
|
Declarations of the family's constructors, needed to refine each one against the scrutinee.
TYPE:
|
scrutinee
|
The applied family type being matched.
TYPE:
|
patterns
|
The branch patterns written.
TYPE:
|
assumptions
|
Equations the enclosing branches established.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CoverageResult
|
Coverage judged against the constructors that remain possible. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If a constructor belongs to another family, or the scrutinee does not inhabit the matched family. |
Source code in src/quivers/qiec/coverage.py
join_branch_rows
¶
join_branch_rows(branches: tuple[tuple[Reachability, EffectRow], ...]) -> EffectRow
Union effects of every possibly reachable branch.
UNKNOWN is intentionally treated as reachable. Dropping it would make
an incomplete equality solver unsound for effect inference.
| PARAMETER | DESCRIPTION |
|---|---|
branches
|
Each branch's reachability and the row its body performs.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
EffectRow
|
The union over every branch not certified impossible. Only a proven contradiction removes a branch's effects from the result. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If two reachable branches bind one instance to different interfaces, or their open tails cannot merge. |
Source code in src/quivers/qiec/coverage.py
Cited sources¶
Hongwei Xi, Chiyan Chen, and Gang Chen. Guarded Recursive Datatype Constructors. In Proceedings of the 30th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL), 224–235. 2003. doi:10.1145/604131.604150.