Patterns¶
Pattern matching for QIEC values: each authored pattern is resolved against the type it is
checked against, the clauses are checked for exhaustiveness and redundancy, and the clause matrix
is compiled to a decision tree that lowers to the one-level core case. The
types reference describes the surface.
patterns
¶
Pattern matching for QIEC values: resolution, coverage, and compilation.
A case or destructuring let is elaborated in four steps, none of
which changes the QIEC core:
resolutionresolves each authored pattern against the type it is checked against, so a constructor name is read in the family the type selects and a bare name becomes either a nullary constructor or a variable;coveragechecks exhaustiveness and redundancy with guard trees, as in Lower Your Guards, reporting a witness for every missing case;decisioncompiles the clause matrix to a decision tree, as in Maranget's compilation of pattern matching, testing each value at most once on every path;- the elaborator lowers the tree to the existing one-level core
caseandif, sharing branch bodies through generated selector families when the tree would otherwise copy them.
The matching structure of a type (its alternatives, which of them a static
index rules out, and the types of their fields) comes from
structures, which other features
extend through its provider registry.
resolution
¶
Resolve authored patterns against the types they are checked against.
Patterns are checked, never synthesized: the type a pattern is matched against selects the alternatives its names refer to. A constructor name is read among the alternatives of that type, so two families may declare the same constructor name without a pattern ever being ambiguous; a bare name is a nullary alternative of that type when it declares one and a variable otherwise; a literal is admitted only by a type whose values are literals.
Pattern synonyms are expanded during resolution, in the environment of their use, so coverage and compilation see the patterns they stand for.
StructureOf
¶
StructureOf = Callable[[TypeExpr], MatchStructure]
Describe the matching structure of a type.
PatternError
¶
Bases: Exception
A pattern that cannot be resolved, located at an authored node.
| PARAMETER | DESCRIPTION |
|---|---|
node
|
The authored node to blame, carrying
TYPE:
|
message
|
What is wrong and what to write instead.
TYPE:
|
code
|
The diagnostic code.
TYPE:
|
Source code in src/quivers/dsl/patterns/resolution.py
ResolvedPattern
didactic-model
¶
Bases: TaggedUnion
A pattern whose names are resolved against its type.
RWildcard
didactic-model
¶
Bases: ResolvedPattern
Matches anything and binds nothing.
| PARAMETER | DESCRIPTION |
|---|---|
line
|
The source position.
TYPE:
|
col
|
The source position.
TYPE:
|
kind
|
The discriminator.
TYPE:
|
RVariable
didactic-model
¶
RVariable(*, name: str, annotation: QiecTypeExpr | None = None, line: int = 0, col: int = 0, kind: Literal['variable'] = 'variable')
Bases: ResolvedPattern
Matches anything and binds it to a name.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name bound.
TYPE:
|
annotation
|
The authored type the value must have, or
TYPE:
|
line
|
The source position.
TYPE:
|
col
|
The source position.
TYPE:
|
kind
|
The discriminator.
TYPE:
|
RAlternative
didactic-model
¶
RAlternative(*, alternative: Alternative, statics: tuple[str, ...] = (), arguments: tuple[ResolvedPattern, ...] = (), line: int = 0, col: int = 0, kind: Literal['alternative'] = 'alternative')
Bases: ResolvedPattern
Matches values built by one alternative, field by field.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
The alternative matched.
TYPE:
|
statics
|
The names given to the alternative's static binders, in order, or empty when the pattern names none.
TYPE:
|
arguments
|
One sub-pattern per field.
TYPE:
|
line
|
The source position.
TYPE:
|
col
|
The source position.
TYPE:
|
kind
|
The discriminator.
TYPE:
|
RLiteral
didactic-model
¶
Bases: ResolvedPattern
Matches one literal of an infinite literal domain.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The literal.
TYPE:
|
line
|
The source position.
TYPE:
|
col
|
The source position.
TYPE:
|
kind
|
The discriminator.
TYPE:
|
ROr
didactic-model
¶
ROr(*, alternatives: tuple[ResolvedPattern, ...], line: int = 0, col: int = 0, kind: Literal['or'] = 'or')
Bases: ResolvedPattern
Matches when one of its alternatives does, trying them in order.
| PARAMETER | DESCRIPTION |
|---|---|
alternatives
|
The alternatives.
TYPE:
|
line
|
The source position.
TYPE:
|
col
|
The source position.
TYPE:
|
kind
|
The discriminator.
TYPE:
|
RAs
didactic-model
¶
RAs(*, pattern: ResolvedPattern, name: str, line: int = 0, col: int = 0, kind: Literal['as'] = 'as')
Bases: ResolvedPattern
Matches a pattern and binds the whole value to a name.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern matched.
TYPE:
|
name
|
The name bound to the whole value.
TYPE:
|
line
|
The source position.
TYPE:
|
col
|
The source position.
TYPE:
|
kind
|
The discriminator.
TYPE:
|
RBinder
didactic-model
¶
RBinder(*, names: tuple[str, ...], variables: tuple[str, ...], body: ResolvedPattern, line: int = 0, col: int = 0, kind: Literal['binder'] = 'binder')
Bases: ResolvedPattern
Opens a field that binds variables, naming them.
| PARAMETER | DESCRIPTION |
|---|---|
names
|
The names given to the bound variables, in binding order.
TYPE:
|
variables
|
The keys of the alternatives that build each bound variable.
TYPE:
|
body
|
The pattern the field is matched against.
TYPE:
|
line
|
The source position.
TYPE:
|
col
|
The source position.
TYPE:
|
kind
|
The discriminator.
TYPE:
|
PatternResolver
didactic-model
¶
PatternResolver(*, structure_of: StructureOf = field(opaque=True, extras=OPAQUE_BY_IDENTITY), synonyms: Mapping[str, QiecPatternSynonymDecl] = field(default_factory=dict, opaque=True, extras=OPAQUE_BY_VALUE))
Bases: ValueModel
Resolves the authored patterns of one module.
| PARAMETER | DESCRIPTION |
|---|---|
structure_of
|
Describes the matching structure of a type.
TYPE:
|
synonyms
|
The module's pattern synonyms, by name.
TYPE:
|
resolve
¶
resolve(pattern: QiecPattern, type_: TypeExpr) -> ResolvedPattern
Resolve a pattern against the type it is checked against.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The authored pattern.
TYPE:
|
type_
|
The type of the values it matches.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ResolvedPattern
|
The pattern with every name resolved. |
| RAISES | DESCRIPTION |
|---|---|
PatternError
|
If a name is not an alternative of the type, a constructor is given the wrong number of fields or static names, a literal does not belong to the type, a binder pattern does not match the field's declared binding arity, the alternatives of an or-pattern bind different names or types, a name is bound twice, or a synonym is used wrongly. |
Source code in src/quivers/dsl/patterns/resolution.py
bound_types
¶
bound_types(pattern: ResolvedPattern, type_: TypeExpr) -> dict[str, TypeExpr]
The type of every variable a resolved pattern binds.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The resolved pattern.
TYPE:
|
type_
|
The type it matches.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, TypeExpr]
|
The variables and their types at resolution skolems. |
Source code in src/quivers/dsl/patterns/resolution.py
resolution_statics
¶
resolution_statics(structure: MatchStructure, alternative: Alternative) -> tuple[TypeExpr, ...]
The field types of an alternative at canonical resolution skolems.
| PARAMETER | DESCRIPTION |
|---|---|
structure
|
The structure of the type matched.
TYPE:
|
alternative
|
One of its alternatives.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[TypeExpr, ...]
|
The field types, with the alternative's statics as rigid variables of a scope derived from the type and the alternative. |
Source code in src/quivers/dsl/patterns/resolution.py
coverage
¶
Coverage and redundancy of pattern matches, after Lower Your Guards.
Each clause is desugared to a guard tree: a sequence of guards
x <- K(y1, ..., yn) (x was built by the alternative K, whose
fields are the fresh variables yi) ending in the clause's right-hand
side, with an or-pattern contributing one sequence per alternative and a
Boolean guard contributing a test whose outcome is unknown. Two functions
over refinement types follow the tree (Graf, Peyton Jones, and Scott 2020):
- the uncovered set: the values no clause matches. A match whose uncovered set is inhabited is not exhaustive, and an inhabitant is the witness reported;
- the reaching set of each right-hand side: the values that reach it. A clause whose reaching set is empty is redundant.
A refinement type is a disjunction of constraint sets, each recording for every variable either the alternative that built it (with its fields) or the alternatives it was not built by. Inhabitation is decided per variable by the matching structure of its type, which knows the static indices: an alternative the type's indices rule out never inhabits it, so a branch that is unreachable at a given index is not demanded. The test recurses into fields with bounded fuel and treats exhausted fuel as inhabited, so coverage is sound: a match is never reported exhaustive when it is not.
StructureUnder
¶
StructureUnder = Callable[[TypeExpr, tuple[IndexEquation, ...]], MatchStructure]
Describe a type's matching structure under index equations in force.
Clause
didactic-model
¶
Clause(*, pattern: ResolvedPattern, guarded: bool = False)
Bases: Model
One clause, as coverage sees it.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The resolved pattern.
TYPE:
|
guarded
|
Whether a guard may reject a value the pattern matches.
TYPE:
|
Redundancy
didactic-model
¶
Bases: Model
A clause, or one alternative of an or-pattern, no value reaches.
| PARAMETER | DESCRIPTION |
|---|---|
clause
|
The clause's position.
TYPE:
|
alternative
|
The choices of or-pattern alternatives that lead to the redundant path, or empty when the whole clause is redundant.
TYPE:
|
CoverageReport
didactic-model
¶
CoverageReport(*, witnesses: tuple[str, ...] = (), redundant: tuple[Redundancy, ...] = ())
Bases: Model
The outcome of checking a match.
| PARAMETER | DESCRIPTION |
|---|---|
witnesses
|
Values no clause matches, rendered as patterns; empty when the match is exhaustive.
TYPE:
|
redundant
|
The clauses and or-pattern alternatives no value reaches.
TYPE:
|
check_coverage
¶
check_coverage(clauses: tuple[Clause, ...], scrutinee: TypeExpr, structure_of: StructureUnder, assumptions: tuple[IndexEquation, ...] = ()) -> CoverageReport
Check a match for exhaustiveness and redundancy.
| PARAMETER | DESCRIPTION |
|---|---|
clauses
|
The clauses, in order.
TYPE:
|
scrutinee
|
The type of the value matched.
TYPE:
|
structure_of
|
Describes the matching structure of a type under index equations.
TYPE:
|
assumptions
|
The index equations the enclosing matches established.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CoverageReport
|
Witnesses of the values no clause matches, and the clauses and or-pattern alternatives no value reaches. |
Source code in src/quivers/dsl/patterns/coverage.py
decision
¶
Compile a clause matrix to a decision tree, after Maranget.
The clause matrix has one column per value still to be examined (an
occurrence, named by its path from the scrutinee) and one row per
clause. Compilation picks a column the first row needs, tests that value
once, and continues with the specialized matrix in each outcome: for an
alternative K, the rows whose pattern in the column is K(...) or a
wildcard, with K's fields as new columns; for the alternatives no row
names, the default matrix of the wildcard rows. A row whose remaining
patterns are all wildcards selects its clause, behind the clause's guard
when it has one (Maranget 2008).
Each value is tested at most once on any path from the root. The default subtree is compiled once and shared by every alternative that reaches it, so the result is a directed acyclic graph; the elaborator lowers a shared subtree once.
Occurrence
¶
A path from the scrutinee: each step names an alternative and a field.
ValueBinding
didactic-model
¶
ValueBinding(*, name: str, occurrence: Occurrence = (), variable: str | None = None, index: int = 0, annotation: QiecTypeExpr | None = None, line: int = 0, col: int = 0)
Bases: Model
A name a clause binds and where its value comes from.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name.
TYPE:
|
occurrence
|
The occurrence whose value it binds, when it binds one.
TYPE:
|
variable
|
For a name a binder pattern gives a bound variable, the key of the alternative building that variable.
TYPE:
|
index
|
The bound variable's de Bruijn index.
TYPE:
|
annotation
|
The authored type the value must have, or
TYPE:
|
line
|
Where the name is written.
TYPE:
|
col
|
Where the name is written.
TYPE:
|
StaticBinding
didactic-model
¶
StaticBinding(*, name: str, occurrence: Occurrence, key: str, position: int)
Bases: Model
A static name a clause binds to one of an alternative's statics.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name.
TYPE:
|
occurrence
|
The occurrence tested.
TYPE:
|
key
|
The alternative whose telescope binds it.
TYPE:
|
position
|
Its position in the telescope.
TYPE:
|
Row
didactic-model
¶
Row(*, patterns: tuple[ResolvedPattern, ...], arm: int, guarded: bool = False, values: tuple[ValueBinding, ...] = (), statics: tuple[StaticBinding, ...] = ())
Bases: Model
One row of a clause matrix.
| PARAMETER | DESCRIPTION |
|---|---|
patterns
|
One pattern per column.
TYPE:
|
arm
|
The clause the row belongs to.
TYPE:
|
guarded
|
Whether the clause has a guard.
TYPE:
|
values
|
The value names bound so far.
TYPE:
|
statics
|
The static names bound so far.
TYPE:
|
Leaf
didactic-model
¶
Leaf(*, id: int, arm: int, values: tuple[ValueBinding, ...] = (), statics: tuple[StaticBinding, ...] = (), guarded: bool = False, otherwise: int | None = None, kind: Literal['leaf'] = 'leaf')
Bases: Node
Select a clause.
| PARAMETER | DESCRIPTION |
|---|---|
id
|
The node's position in the tree's node table.
TYPE:
|
arm
|
The clause selected.
TYPE:
|
values
|
The value names the clause binds.
TYPE:
|
statics
|
The static names the clause binds.
TYPE:
|
guarded
|
Whether the clause's guard is tested first.
TYPE:
|
otherwise
|
The node continued at when the guard fails.
TYPE:
|
kind
|
The discriminator.
TYPE:
|
Branch
didactic-model
¶
Bases: Model
One outcome of a switch.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The alternative, for a finite switch.
TYPE:
|
literal
|
The literal, for a literal switch.
TYPE:
|
target
|
The node continued at.
TYPE:
|
Switch
didactic-model
¶
Switch(*, id: int, occurrence: Occurrence, branches: tuple[Branch, ...], default: int | None = None, kind: Literal['switch'] = 'switch')
Bases: Node
Test one occurrence.
| PARAMETER | DESCRIPTION |
|---|---|
id
|
The node's position in the tree's node table.
TYPE:
|
occurrence
|
The occurrence tested.
TYPE:
|
branches
|
One branch per possible alternative, or per literal written.
TYPE:
|
default
|
For a literal switch, the node continued at when no literal matches.
TYPE:
|
kind
|
The discriminator.
TYPE:
|
DecisionTree
didactic-model
¶
DecisionTree(*, nodes: tuple[Node, ...], root: int)
Bases: Model
A compiled match.
| PARAMETER | DESCRIPTION |
|---|---|
nodes
|
Every node, indexed by its
TYPE:
|
root
|
The root node.
TYPE:
|
node
¶
node(identity: int) -> Node
The node with an id.
| PARAMETER | DESCRIPTION |
|---|---|
identity
|
The id.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Node
|
The node. |
references
¶
How many edges point at each node.
| RETURNS | DESCRIPTION |
|---|---|
dict[int, int]
|
Incoming edge counts, with the root counted once. |
Source code in src/quivers/dsl/patterns/decision.py
step
¶
One step of an occurrence: a field of an alternative.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The alternative's key.
TYPE:
|
position
|
The field's position.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The step, |
Source code in src/quivers/dsl/patterns/decision.py
parse_step
¶
Split an occurrence step into its alternative and field.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
A step made by
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, int]
|
The alternative's key and the field's position. |
Source code in src/quivers/dsl/patterns/decision.py
compile_match
¶
compile_match(patterns: tuple[tuple[ResolvedPattern, bool], ...], scrutinee: TypeExpr, structure_of: StructureUnder, assumptions: tuple[IndexEquation, ...] = ()) -> DecisionTree
Compile clauses to a decision tree.
| PARAMETER | DESCRIPTION |
|---|---|
patterns
|
Each clause's resolved pattern and whether it is guarded, in order.
TYPE:
|
scrutinee
|
The type of the value matched.
TYPE:
|
structure_of
|
Describes the matching structure of a type under index equations.
TYPE:
|
assumptions
|
The index equations the enclosing matches established.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DecisionTree
|
The tree. Every possible value reaches a leaf, given that coverage has accepted the clauses. |
Source code in src/quivers/dsl/patterns/decision.py
leaf_counts
¶
leaf_counts(tree: DecisionTree, shared: frozenset[int]) -> dict[int, int]
How many times each clause body would be lowered.
| PARAMETER | DESCRIPTION |
|---|---|
tree
|
The tree.
TYPE:
|
shared
|
Nodes lowered once however many edges reach them.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[int, int]
|
For each clause, the number of leaves selecting it on the lowered tree, counting a node reached by several edges once per edge unless it is shared. |
Source code in src/quivers/dsl/patterns/decision.py
lowered_size
¶
lowered_size(tree: DecisionTree, shared: frozenset[int]) -> int
The number of nodes the lowered tree contains.
| PARAMETER | DESCRIPTION |
|---|---|
tree
|
The tree.
TYPE:
|
shared
|
Nodes lowered once however many edges reach them.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Nodes counted once per path to them, except shared nodes, which are counted once. |
Source code in src/quivers/dsl/patterns/decision.py
structures
¶
The matching structure of a QIEC value type.
A pattern is checked against a type, and what it may say about a value of
that type is fixed by the type's structure: the alternatives a value is
built from, which of them the type's static indices rule out, the types of
each alternative's fields, and how a test on the value is lowered to the
core. A MatchStructure
answers those questions for one type.
Structures come from providers. The built-in providers cover declared
families (generically, from their constructors), Bool, Unit,
tuples, and the literal domains Int and String. A feature that adds
value types registers its own provider with
register_structure_provider;
patterns over its types then resolve, are covered, and compile through
the same machinery with no change here.
TRUE
module-attribute
¶
TRUE = Alternative(key='true', name='true', arity=0, form='name')
The alternative true of Bool.
FALSE
module-attribute
¶
FALSE = Alternative(key='false', name='false', arity=0, form='name')
The alternative false of Bool.
UNIT_VALUE
module-attribute
¶
UNIT_VALUE = Alternative(key='unit', name='unit', arity=0, form='name')
The only alternative of Unit.
Lowering
¶
How a test on a value of the type is lowered to the core.
case is a core case over a declared family; boolean an if;
product needs no test and reads fields by projection; literal is a
chain of equality tests; opaque admits no test at all; custom defers
to the structure's own switch.
BranchBody
¶
BranchBody = Callable[[tuple[Local, ...]], tuple[Computation, TypeExpr]]
Lowers one arm of a custom switch, given the locals its fields are bound to.
StructureProvider
¶
StructureProvider = Callable[[TypeExpr, KernelRegistry, dict[ConstructorId, tuple[tuple[ConstructorId, ...], ...]]], MatchStructure | None]
Describe a type's matching structure, or return None to decline it.
The provider receives the type, the registry it is checked against, and the binding arities of the module's constructors.
Alternative
didactic-model
¶
Alternative(*, key: str, name: str, arity: int, form: Literal['constructor', 'tuple', 'record', 'name'] = 'constructor', labels: tuple[str, ...] = (), telescope: Telescope = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE), binders: tuple[tuple[str, ...], ...] = ())
Bases: ValueModel
One way a value of a type can be built.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
A stable key, unique among the type's alternatives.
TYPE:
|
name
|
The name a pattern uses for the alternative, and the name a witness prints.
TYPE:
|
arity
|
The number of fields.
TYPE:
|
form
|
How a witness prints the alternative: a constructor applied to its fields, a tuple, a record, or a bare name.
TYPE:
|
labels
|
The field labels, for an alternative whose fields are labelled.
TYPE:
|
telescope
|
The static binders the alternative brings into scope, which a pattern may name in brackets.
TYPE:
|
binders
|
For each field, the keys of the alternatives that build the variables the field binds, in binding order; empty for a field that binds nothing.
TYPE:
|
MatchStructure
¶
Bases: Protocol
What patterns may say about values of one type.
| ATTRIBUTE | DESCRIPTION |
|---|---|
type |
The type described.
TYPE:
|
lowering |
How a test on a value of the type is lowered.
TYPE:
|
alternatives
¶
alternatives() -> tuple[Alternative, ...]
Every alternative, in declaration order.
| RETURNS | DESCRIPTION |
|---|---|
tuple[Alternative, ...]
|
The alternatives; empty for a literal or opaque type. |
possible
¶
possible(alternative: Alternative) -> bool
Whether a value of this type can be built by an alternative.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
One of this structure's alternatives.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
False only when the type's static indices rule it out. |
Source code in src/quivers/dsl/patterns/structures.py
skolems
¶
skolems(alternative: Alternative, scope: StaticScopeId) -> tuple[StaticArgument, ...]
Rigid variables for an alternative's telescope in one scope.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
One of this structure's alternatives.
TYPE:
|
scope
|
The scope the variables are minted in.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[StaticArgument, ...]
|
One variable per telescope binder. |
Source code in src/quivers/dsl/patterns/structures.py
field_types
¶
field_types(alternative: Alternative, statics: tuple[StaticArgument, ...]) -> tuple[TypeExpr, ...]
The types of an alternative's fields at given statics.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
One of this structure's alternatives.
TYPE:
|
statics
|
Arguments for the alternative's telescope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[TypeExpr, ...]
|
One type per field. |
Source code in src/quivers/dsl/patterns/structures.py
project
¶
project(value: Value, alternative: Alternative, position: int) -> Value
Read one field of a product value.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A value of this type.
TYPE:
|
alternative
|
The structure's only alternative.
TYPE:
|
position
|
The field's position.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Value
|
The field, as a pure value. |
Source code in src/quivers/dsl/patterns/structures.py
switch
¶
switch(scrutinee: Value, branches: tuple[tuple[Alternative, BranchBody], ...]) -> tuple[Computation, TypeExpr]
Lower a test on a value for a custom structure.
| PARAMETER | DESCRIPTION |
|---|---|
scrutinee
|
The value tested.
TYPE:
|
branches
|
For every possible alternative, the function lowering its arm.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Computation, TypeExpr]
|
The test and the type every arm produces. |
Source code in src/quivers/dsl/patterns/structures.py
FamilyStructure
didactic-model
¶
FamilyStructure(*, type: TypeApplication = field(opaque=True, extras=OPAQUE_BY_VALUE), family: FamilyDecl = field(opaque=True, extras=OPAQUE_BY_VALUE), constructors: tuple[ConstructorDecl, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), bindings: dict[ConstructorId, tuple[tuple[ConstructorId, ...], ...]] = field(default_factory=dict, opaque=True, extras=OPAQUE_BY_VALUE), assumptions: tuple[IndexEquation, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: ValueModel
The structure of a declared family application.
| PARAMETER | DESCRIPTION |
|---|---|
type
|
The family application.
TYPE:
|
family
|
The family it applies.
TYPE:
|
constructors
|
The family's constructors, in declaration order.
TYPE:
|
bindings
|
For each constructor that has binder fields, the variable constructors each field binds.
TYPE:
|
assumptions
|
Equations between static terms that the enclosing matches have established, which decide what the application's indices admit.
TYPE:
|
constructor
¶
constructor(alternative: Alternative) -> ConstructorDecl
The constructor an alternative stands for.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
One of this structure's alternatives.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ConstructorDecl
|
The declaration. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the alternative is not one of this family's. |
Source code in src/quivers/dsl/patterns/structures.py
alternatives
¶
alternatives() -> tuple[Alternative, ...]
One alternative per constructor, in declaration order.
| RETURNS | DESCRIPTION |
|---|---|
tuple[Alternative, ...]
|
The alternatives. |
Source code in src/quivers/dsl/patterns/structures.py
possible
¶
possible(alternative: Alternative) -> bool
Whether the application's indices admit a constructor.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
One of this structure's alternatives.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
False only when the constructor's result indices cannot equal the application's under the structure's assumptions, the rule the core's coverage check applies. |
Source code in src/quivers/dsl/patterns/structures.py
refinement
¶
refinement(alternative: Alternative, statics: tuple[StaticArgument, ...]) -> BranchRefinement
What matching an alternative at given statics establishes.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
One of this structure's alternatives.
TYPE:
|
statics
|
Arguments for the constructor's own telescope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BranchRefinement
|
The branch's givens and whether it is reachable under the structure's assumptions. |
Source code in src/quivers/dsl/patterns/structures.py
equations
¶
equations(alternative: Alternative, statics: tuple[StaticArgument, ...]) -> tuple[IndexEquation, ...]
The equations matching an alternative establishes.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
One of this structure's alternatives.
TYPE:
|
statics
|
Arguments for the constructor's own telescope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[IndexEquation, ...]
|
Each of the application's indices equals the constructor's result index at the statics. |
Source code in src/quivers/dsl/patterns/structures.py
skolems
¶
skolems(alternative: Alternative, scope: StaticScopeId) -> tuple[StaticArgument, ...]
The constructor's rigid variables in a branch scope.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
One of this structure's alternatives.
TYPE:
|
scope
|
The branch scope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[StaticArgument, ...]
|
The canonical skolems the core expects of a branch. |
Source code in src/quivers/dsl/patterns/structures.py
field_types
¶
field_types(alternative: Alternative, statics: tuple[StaticArgument, ...]) -> tuple[TypeExpr, ...]
The constructor's field types at the application and statics.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
One of this structure's alternatives.
TYPE:
|
statics
|
Arguments for the constructor's own telescope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[TypeExpr, ...]
|
One type per field. |
Source code in src/quivers/dsl/patterns/structures.py
project
¶
project(value: Value, alternative: Alternative, position: int) -> Value
Families are not products.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Unused.
TYPE:
|
alternative
|
Unused.
TYPE:
|
position
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Value
|
Never returns. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
Always: a family value's fields are bound by a |
Source code in src/quivers/dsl/patterns/structures.py
switch
¶
switch(scrutinee: Value, branches: tuple[tuple[Alternative, BranchBody], ...]) -> tuple[Computation, TypeExpr]
Families are lowered by the elaborator's own case.
| PARAMETER | DESCRIPTION |
|---|---|
scrutinee
|
Unused.
TYPE:
|
branches
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Computation, TypeExpr]
|
Never returns. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
Always. |
Source code in src/quivers/dsl/patterns/structures.py
FiniteStructure
didactic-model
¶
FiniteStructure(*, type: TypeExpr = field(opaque=True, extras=OPAQUE_BY_VALUE), lowering: Lowering, choices: tuple[Alternative, ...])
Bases: ValueModel
A finite structure with no core family behind it.
Bool has the alternatives true and false, tested by if.
A tuple type has one alternative whose fields are its components, and
Unit one alternative with no fields; neither needs a test.
| PARAMETER | DESCRIPTION |
|---|---|
type
|
The type described.
TYPE:
|
lowering
|
TYPE:
|
choices
|
The alternatives.
TYPE:
|
alternatives
¶
alternatives() -> tuple[Alternative, ...]
The alternatives.
| RETURNS | DESCRIPTION |
|---|---|
tuple[Alternative, ...]
|
The structure's alternatives. |
possible
¶
possible(alternative: Alternative) -> bool
Every alternative is possible.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True. |
skolems
¶
skolems(alternative: Alternative, scope: StaticScopeId) -> tuple[StaticArgument, ...]
No alternative binds statics.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
Unused.
TYPE:
|
scope
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[StaticArgument, ...]
|
The empty tuple. |
Source code in src/quivers/dsl/patterns/structures.py
field_types
¶
field_types(alternative: Alternative, statics: tuple[StaticArgument, ...]) -> tuple[TypeExpr, ...]
A tuple's components, or no fields.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
One of this structure's alternatives.
TYPE:
|
statics
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[TypeExpr, ...]
|
The component types of a tuple, else the empty tuple. |
Source code in src/quivers/dsl/patterns/structures.py
project
¶
project(value: Value, alternative: Alternative, position: int) -> Value
Read a tuple component.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A tuple value.
TYPE:
|
alternative
|
The tuple alternative.
TYPE:
|
position
|
The component.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Value
|
The projection. |
Source code in src/quivers/dsl/patterns/structures.py
switch
¶
switch(scrutinee: Value, branches: tuple[tuple[Alternative, BranchBody], ...]) -> tuple[Computation, TypeExpr]
Finite structures are lowered by the elaborator.
| PARAMETER | DESCRIPTION |
|---|---|
scrutinee
|
Unused.
TYPE:
|
branches
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Computation, TypeExpr]
|
Never returns. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
Always. |
Source code in src/quivers/dsl/patterns/structures.py
LiteralStructure
didactic-model
¶
LiteralStructure(*, type: TypeExpr = field(opaque=True, extras=OPAQUE_BY_VALUE), lowering: Lowering)
Bases: ValueModel
An infinite domain matched by literals, Int or String.
A literal domain also stands for a type with no alternatives a pattern
can name (Real, a tensor, a rigid type variable), which is the
opaque lowering: only variables and wildcards match it.
| PARAMETER | DESCRIPTION |
|---|---|
type
|
The type described.
TYPE:
|
lowering
|
TYPE:
|
alternatives
¶
alternatives() -> tuple[Alternative, ...]
No finite list of alternatives.
| RETURNS | DESCRIPTION |
|---|---|
tuple[Alternative, ...]
|
The empty tuple. |
possible
¶
possible(alternative: Alternative) -> bool
No alternative is possible.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
False. |
skolems
¶
skolems(alternative: Alternative, scope: StaticScopeId) -> tuple[StaticArgument, ...]
No statics.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
Unused.
TYPE:
|
scope
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[StaticArgument, ...]
|
The empty tuple. |
Source code in src/quivers/dsl/patterns/structures.py
field_types
¶
field_types(alternative: Alternative, statics: tuple[StaticArgument, ...]) -> tuple[TypeExpr, ...]
No fields.
| PARAMETER | DESCRIPTION |
|---|---|
alternative
|
Unused.
TYPE:
|
statics
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[TypeExpr, ...]
|
The empty tuple. |
Source code in src/quivers/dsl/patterns/structures.py
project
¶
project(value: Value, alternative: Alternative, position: int) -> Value
Literal values have no fields.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Unused.
TYPE:
|
alternative
|
Unused.
TYPE:
|
position
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Value
|
Never returns. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
Always. |
Source code in src/quivers/dsl/patterns/structures.py
switch
¶
switch(scrutinee: Value, branches: tuple[tuple[Alternative, BranchBody], ...]) -> tuple[Computation, TypeExpr]
Literal tests are lowered by the elaborator.
| PARAMETER | DESCRIPTION |
|---|---|
scrutinee
|
Unused.
TYPE:
|
branches
|
Unused.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Computation, TypeExpr]
|
Never returns. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
Always. |
Source code in src/quivers/dsl/patterns/structures.py
register_structure_provider
¶
register_structure_provider(provider: StructureProvider) -> None
Add a provider consulted before the built-in ones.
| PARAMETER | DESCRIPTION |
|---|---|
provider
|
A function describing the types it knows and declining the rest.
TYPE:
|
Source code in src/quivers/dsl/patterns/structures.py
tuple_alternative
¶
tuple_alternative(arity: int) -> Alternative
The single alternative of a tuple type.
| PARAMETER | DESCRIPTION |
|---|---|
arity
|
The number of components.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Alternative
|
The alternative, keyed by its arity. |
Source code in src/quivers/dsl/patterns/structures.py
branch_equations
¶
branch_equations(structure: MatchStructure, alternative: Alternative, statics: tuple[StaticArgument, ...]) -> tuple[IndexEquation, ...]
The index equations matching an alternative establishes, if any.
| PARAMETER | DESCRIPTION |
|---|---|
structure
|
The structure of the type matched.
TYPE:
|
alternative
|
One of its alternatives.
TYPE:
|
statics
|
Arguments for the alternative's telescope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[IndexEquation, ...]
|
A family's index equations; nothing for any other structure, whose alternatives refine no indices. |
Source code in src/quivers/dsl/patterns/structures.py
structure_of
¶
structure_of(type_: TypeExpr, registry: KernelRegistry, bindings: dict[ConstructorId, tuple[tuple[ConstructorId, ...], ...]], assumptions: tuple[IndexEquation, ...] = ()) -> MatchStructure
Describe a type's matching structure.
| PARAMETER | DESCRIPTION |
|---|---|
type_
|
The type a pattern is checked against.
TYPE:
|
registry
|
The registry declaring the module's families.
TYPE:
|
bindings
|
The binding arities of constructors whose fields bind variables.
TYPE:
|
assumptions
|
Equations the enclosing matches established.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MatchStructure
|
The first registered provider's answer, else the built-in one:
a declared family, |
Source code in src/quivers/dsl/patterns/structures.py
statics
¶
Static variables of QIEC types, and abstracting them into binders.
StaticVariable
¶
StaticVariable = TypeVariable | IndexVariable | EffectVariable
A static variable: a rigid skolem or a telescope binder's variable.
free_statics
¶
free_statics(term: StaticArgument) -> tuple[StaticVariable, ...]
The static variables a term mentions, in order of first occurrence.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[StaticVariable, ...]
|
Each variable once. |
Source code in src/quivers/dsl/patterns/statics.py
binder_for
¶
binder_for(variable: StaticVariable, name: str) -> TelescopeBinder
A binder ranging over what a variable ranges over.
| PARAMETER | DESCRIPTION |
|---|---|
variable
|
The variable.
TYPE:
|
name
|
The binder's name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TelescopeBinder
|
A type binder of the variable's kind, an index binder of its sort, or an effect binder. |
Source code in src/quivers/dsl/patterns/statics.py
variable_for
¶
variable_for(binder: TelescopeBinder) -> StaticVariable
The variable a binder introduces, by name.
| PARAMETER | DESCRIPTION |
|---|---|
binder
|
The binder.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
StaticVariable
|
A variable with no identity, which substitution reaches by name. |
Source code in src/quivers/dsl/patterns/statics.py
abstract
¶
abstract(type_: TypeExpr, variables: tuple[StaticVariable, ...], names: tuple[str, ...]) -> TypeExpr
Replace variables in a type by variables of new names.
| PARAMETER | DESCRIPTION |
|---|---|
type_
|
The type.
TYPE:
|
variables
|
The variables to replace.
TYPE:
|
names
|
The name each becomes, a binder's name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeExpr
|
The type over the binders |
Source code in src/quivers/dsl/patterns/statics.py
unification
¶
First-order unification over QIEC static terms.
Constructor applications infer the static arguments they omit by
unification over the constructor's telescope (Pottier 2014, restricted to
static telescopes). The unknowns are metavariables: type, index, and
effect variables whose names begin with ?, which no authored name can
spell. A solution maps each metavariable's name to a static term.
Index arithmetic is solved only where it is a shift: a sum of one metavariable and a constant against a term the constant can be taken from. Everything else is structural.
Solution
¶
Solution = dict[str, StaticArgument]
Metavariable names and the static terms they are solved to.
metavariables
¶
metavariables(telescope: Telescope, prefix: str) -> tuple[StaticArgument, ...]
One fresh metavariable per binder of a telescope.
| PARAMETER | DESCRIPTION |
|---|---|
telescope
|
The binders.
TYPE:
|
prefix
|
Distinguishes these metavariables from any others in one problem.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[StaticArgument, ...]
|
A variable of each binder's class and classifier, named
|
Source code in src/quivers/dsl/patterns/unification.py
apply
¶
apply(term: StaticArgument, solution: Mapping[str, StaticArgument]) -> StaticArgument
Substitute a solution into a term.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
solution
|
The solved metavariables.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
StaticArgument
|
The term with every solved metavariable replaced, repeatedly, so a solution whose values mention other solved metavariables is fully applied. |
Source code in src/quivers/dsl/patterns/unification.py
unsolved
¶
unsolved(term: StaticArgument) -> tuple[str, ...]
The metavariables a term still mentions.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, ...]
|
Their names, in order of first occurrence. |
Source code in src/quivers/dsl/patterns/unification.py
unify
¶
unify(left: StaticArgument, right: StaticArgument, solution: Solution) -> bool
Extend a solution so two terms become equal.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
One term.
TYPE:
|
right
|
The other.
TYPE:
|
solution
|
The solution so far, extended in place on success. On failure it may hold bindings made before the clash.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether the terms unify. |
Source code in src/quivers/dsl/patterns/unification.py
elaboration
¶
Bidirectional elaboration of QIEC values and pattern matches.
Mixed into the QIEC elaborator, this module elaborates:
- constructor applications, where a constructor is an ordinary name. In
checking mode the expected type selects the family and fixes its static
arguments; in synthesis mode a name declared by one family selects it,
and the static arguments are inferred by unification over the
constructor's telescope. A name two families declare is refused with
every candidate unless qualified (
F.c) or checked against a type; - annotations
(e : T), which checkeagainstT; caseand destructuringlet: patterns are resolved against the scrutinee's type, checked for coverage and redundancy, compiled to a decision tree, and lowered to the core's one-levelcaseandif.
Nothing here adds a core term. Branch bodies the decision tree would copy
are lowered once each, behind a generated selector family: the tree
returns the selected branch and its bindings as a value, and one flat
case over that value runs the body.