Test terms and values¶
A test term is a let expression the runner evaluates on the host;
TermEvaluator runs a call to an entry point on the
reference machine and builds a data value from a constructor application. The values a test
computes with, and how they compare and print, are in quivers.testing.values.
terms
¶
Evaluating test terms against a checked module.
A test term is a let expression evaluated on the host:
- a numeric literal is exact: an integer literal is an
int, a decimal literal the rational it spells (0.25is1/4); +,-and*keep integers integral, and/is exact division, so1/4is the rational one quarter;- comparisons,
&&,||andnotare the host's; - a name is a
letbinding or a property's variable, and otherwise a constructor without fields; - a call
f(...)runsfon the reference machine whenfis a computation or program of the module, and builds a data value whenfis a constructor. Arguments are positional or named; a named argument whose name is a static binder of the computation instantiates it.
Each argument is checked against the type it is passed at, which is how a nullary constructor of a parameterized family, or an integer passed as a real, gets its type. A run evaluates in the test's number domain: exactly, over the rationals, unless the test declares floating numbers.
FunctionBody
¶
FunctionBody = Callable[[tuple[Value, ...]], tuple[Computation, TypeExpr, EffectRow]]
The body of a function a test term names, from the values of its parameters: the computation, its result type, and its effect row.
HeldResult
didactic-model
¶
TermError
¶
Bases: ValueError
A test term that cannot be evaluated, located at its source.
| PARAMETER | DESCRIPTION |
|---|---|
message
|
What is wrong.
TYPE:
|
line
|
The 1-based source line, or 0 when unknown.
TYPE:
|
col
|
The 0-based source column.
TYPE:
|
Source code in src/quivers/testing/terms.py
RunSettings
didactic-model
¶
RunSettings(*, numbers: NumberDomain, fuel: int, seed: int)
Bases: Model
How a test runs the module's entry points.
| PARAMETER | DESCRIPTION |
|---|---|
numbers
|
The number domain of every run.
TYPE:
|
fuel
|
The step budget of each run.
TYPE:
|
seed
|
The seed of the reference generator for each run of a program.
TYPE:
|
TermEvaluator
¶
TermEvaluator(module: QiecModule, settings: RunSettings)
Evaluate test terms against one checked module.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The checked module whose entry points and constructors terms name.
TYPE:
|
settings
|
The number domain, step budget and seed of each run.
TYPE:
|
Source code in src/quivers/testing/terms.py
evaluate
¶
evaluate(term: LetExprNode, scope: Mapping[str, TestValue], expected: TypeExpr | None = None) -> TestValue
Evaluate one term.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
scope
|
The values of the names bound so far.
TYPE:
|
expected
|
The type the value is passed at, when it is an argument.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
The value. |
| RAISES | DESCRIPTION |
|---|---|
TermError
|
If the term is not a test term, names nothing, or a run fails. |
Source code in src/quivers/testing/terms.py
operands
¶
operands(term: LetExprBinOp, scope: Mapping[str, TestValue]) -> tuple[TestValue, TestValue]
Evaluate the two operands of a binary operation.
The right operand is read at the type of the left one when the left
is a data value, so reverse(xs) == Cons(2, Cons(1, Nil)) builds
the expected list at the type the run returned.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The operation.
TYPE:
|
scope
|
The bindings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[TestValue, TestValue]
|
The left and right values. |
Source code in src/quivers/testing/terms.py
function
¶
function(term: LetExprNode, expected: FunctionType, scope: Mapping[str, TestValue]) -> RuntimeClosure
A computation or constructor passed where a function is expected.
The name of a computation is a function value: applying it calls the computation with the argument, or with the argument's components when the computation takes several values. A computation with static arguments is used at the instance the expected function type fixes, as the elaborator reads a named computation used as a value. The name of a constructor stands for the function that applies it to its fields, at the family type the expected result fixes. A call of a computation that returns a function is run, and its result is the function.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The argument: the name of a computation or a constructor, or a call that returns a function.
TYPE:
|
expected
|
The function type of the parameter it is passed to.
TYPE:
|
scope
|
The bindings, for a call's arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RuntimeClosure
|
The function, closed over nothing. |
| RAISES | DESCRIPTION |
|---|---|
TermError
|
If the term names neither a computation nor a constructor, or the expected type does not determine its static arguments or is not its type. |
Source code in src/quivers/testing/terms.py
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 | |
machine_argument
¶
machine_argument(value: TestValue, type_: TypeExpr, term: LetExprNode | None) -> TestValue
A test value as a run of the test's domain takes it at a type.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value.
TYPE:
|
type_
|
The parameter's type.
TYPE:
|
term
|
The argument, for the diagnostic's location;
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
The value: an integer for an |
| RAISES | DESCRIPTION |
|---|---|
TermError
|
If a rational that is not an integer is passed as an |
Source code in src/quivers/testing/terms.py
machine_field
¶
machine_field(value: TestValue, type_: TypeExpr, term: LetExprNode | None) -> MachineValue
A field value as the machine holds it in a data value.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value.
TYPE:
|
type_
|
The field's type.
TYPE:
|
term
|
The field term, for diagnostics;
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MachineValue
|
The value converted by the field's type into the run's numbers. |
Source code in src/quivers/testing/terms.py
family_type
¶
family_type(family: FamilyDecl, expected: TypeExpr | None, line: int = 0, col: int = 0) -> TypeApplication
The type of a family's value, at the expected instance.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family.
TYPE:
|
expected
|
The type the value is passed at.
TYPE:
|
line
|
The source line, for diagnostics.
TYPE:
|
col
|
The source column, for diagnostics.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeApplication
|
|
| RAISES | DESCRIPTION |
|---|---|
TermError
|
If the family has parameters or indices and no instance of it is expected, or another type is. |
Source code in src/quivers/testing/terms.py
type_argument
¶
type_argument(argument: StaticArgument) -> TypeExpr | None
A static argument that is a type, as a type.
| PARAMETER | DESCRIPTION |
|---|---|
argument
|
A type, row, index, or effect.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeExpr | None
|
The argument when it is a type; |
Source code in src/quivers/testing/terms.py
values_equal
¶
Structural equality of two test values.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
One value.
TYPE:
|
right
|
The other.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Numbers by exact value, tuples componentwise, data values by
constructor and fields, anything else by |
Source code in src/quivers/testing/terms.py
values
¶
The values a test computes with, and how they compare and print.
A test runs on the host: its terms evaluate to host values, and a call to
an entry point of the module runs it on the reference machine and reads its
result back at the host boundary (quivers.qiec.numbers.host_value). An
exact run returns its reals as fractions.Fraction; a floating run returns
them as float. Integers are int, truth values bool, strings str,
products and tensors tuples, the unit value the empty tuple, and a value of
a data family the machine's
RuntimeConstructor.
canonical reads a value into a form that compares structurally (a data
value becomes its constructor's name and its fields), and render prints
that form in QVR's surface spelling.
TestValue
¶
TestValue = Fraction | int | float | bool | str | RuntimeConstructor | tuple['TestValue', ...]
A host value a test term evaluates to.
MachineValue
¶
MachineValue = Tensor | Fraction | int | float | bool | str | RuntimeConstructor | tuple['MachineValue', ...]
A value as the machine holds it: a tensor for a number or tensor of numbers, a tuple of machine values, and the host form of every other value.
Canonical
¶
Canonical = Fraction | int | float | bool | str | Constructed | tuple['Canonical', ...]
A test value in the form that compares structurally.
Constructed
didactic-model
¶
Constructed(*, constructor: str, fields: tuple[Canonical, ...] = field(opaque=True, default=(), extras=OPAQUE_BY_VALUE))
Bases: ValueModel
A data value read structurally: a constructor and its fields.
| PARAMETER | DESCRIPTION |
|---|---|
constructor
|
The constructor's source name.
TYPE:
|
fields
|
The fields, canonical.
TYPE:
|
UnreadableValue
¶
Bases: TypeError
A machine value with no host form a test can compare.
read_value
¶
read_value(value: MachineValue) -> TestValue
A machine result read as a test value.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A value the machine returned, or a host value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TestValue
|
The value at the host boundary: an exact rational tensor as
fractions, a floating tensor as floats, an integer or truth tensor
as |
| RAISES | DESCRIPTION |
|---|---|
UnreadableValue
|
If the value is neither a number, a tensor, a string, a tuple, nor a data value. |
Source code in src/quivers/testing/values.py
canonical
¶
canonical(value: TestValue, constructors: Mapping[ConstructorId, str]) -> Canonical
A test value in the form that compares structurally.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value.
TYPE:
|
constructors
|
The source name of each constructor of the module.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Canonical
|
Data values as |
Source code in src/quivers/testing/values.py
render
¶
render(value: Canonical) -> str
Print a canonical value in QVR's surface spelling.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
|
Source code in src/quivers/testing/values.py
to_json
¶
to_json(value: Canonical) -> str | int | float | bool | list | dict[str, str | list]
A canonical value as JSON data for a machine-readable report.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str | int | float | bool | list | dict
|
A rational as its |