Normalization by evaluation¶
Type-directed laws, eta for functions, surjective pairing for products, and the monad laws, are decided by evaluating a term into a semantic domain and reading it back at its type. The structures are recognized from the shapes of the declared laws (Equations and normalization §5).
nbe
¶
Typed normalization by evaluation for type-directed laws.
Some laws are not oriented rules: eta for functions, surjective pairing,
and the monad laws are type-directed, since whether t should become
Lam(x. App(t, x)) depends on t's type. A theory whose laws include
one of the structures below decides them by normalization by evaluation
[@berger1991inverse]: a term is evaluated into a semantic domain and read
back at its type, which yields the beta-eta-long normal form. Computations
use Filinski's residualizing monad [@filinski2001normalization]: a
computation is either a returned value or a neutral computation bound to a
continuation, which validates the three monad laws and eta for the monad.
The structures are recognized from the declared laws, never from constructor names. Each law is matched against the schemas below with the constructors as unknowns, and a structure is used only when every one of its laws is declared, since normalizing with an undeclared law would identify terms the theory does not:
- functions: beta
A(L(x. M[x]), N) = M[N]and etaL(x. A(F, x)) = F; - products:
P(Q(A, B)) = A,S(Q(A, B)) = B, and surjective pairingQ(P(X), S(X)) = X; - a monad:
B(R(A), x. K[x]) = K[A],B(M, x. R(x)) = M, andB(B(M, x. K[x]), y. L[y]) = B(M, x. B(K[x], y. L[y])).
The type former of each structure is read from the typing judgment: the type its introduction form's typing rule concludes. Types come from a derivation of the term in that judgment, so the normalizer reads every constant at the type its occurrence was given.
OPAQUE_TYPE
module-attribute
¶
The constructor an unsolved type variable is read as: an opaque base type.
SemanticComputation
¶
The value of a term of computation type.
SemanticValue
¶
SemanticValue = Residual | SemanticFunction | SemanticPair | Returned | Pending
A semantic value.
NormalizationError
¶
Bases: ValueError
A term cannot be normalized by evaluation.
FunctionStructure
didactic-model
¶
Bases: Model
The constructors of a function type and its type former.
| PARAMETER | DESCRIPTION |
|---|---|
lam
|
Abstraction and application.
TYPE:
|
app
|
Abstraction and application.
TYPE:
|
arrow
|
The function type former, or
TYPE:
|
ProductStructure
didactic-model
¶
Bases: Model
The constructors of a product type and its type former.
| PARAMETER | DESCRIPTION |
|---|---|
pair
|
Pairing and the projections.
TYPE:
|
first
|
Pairing and the projections.
TYPE:
|
second
|
Pairing and the projections.
TYPE:
|
product
|
The product type former, or
TYPE:
|
MonadStructure
didactic-model
¶
Bases: Model
The constructors of a monad and its type former.
| PARAMETER | DESCRIPTION |
|---|---|
unit
|
The return and the bind of the monad.
TYPE:
|
bind
|
The return and the bind of the monad.
TYPE:
|
monad
|
The computation type former, or
TYPE:
|
TypeDirectedStructure
didactic-model
¶
TypeDirectedStructure(*, function: FunctionStructure | None = None, product: ProductStructure | None = None, monad: MonadStructure | None = None, laws: tuple[str, ...] = ())
Bases: Model
The type-directed structures a theory's laws declare.
| PARAMETER | DESCRIPTION |
|---|---|
function
|
Functions with beta and eta.
TYPE:
|
product
|
Products with projections and surjective pairing.
TYPE:
|
monad
|
A monad with its three laws.
TYPE:
|
laws
|
The names of the laws the structures account for.
TYPE:
|
empty
property
¶
Whether no structure was found.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Residual
didactic-model
¶
Residual(*, at: Callable[[int], Term] = field(opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: ValueModel
Syntax as a function of the depth it is placed at (de Bruijn levels).
| PARAMETER | DESCRIPTION |
|---|---|
at
|
The term at each depth.
TYPE:
|
SemanticFunction
didactic-model
¶
SemanticFunction(*, apply: Callable[[SemanticValue], SemanticValue] = field(opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: ValueModel
The value of a term of function type.
| PARAMETER | DESCRIPTION |
|---|---|
apply
|
Application to a value.
TYPE:
|
SemanticPair
didactic-model
¶
SemanticPair(*, first: SemanticValue = field(opaque=True, extras=OPAQUE_BY_IDENTITY), second: SemanticValue = field(opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: ValueModel
The value of a term of product type.
| PARAMETER | DESCRIPTION |
|---|---|
first
|
The components.
TYPE:
|
second
|
The components.
TYPE:
|
Returned
didactic-model
¶
Returned(*, value: SemanticValue = field(opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: ValueModel
A computation that returns a value.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value.
TYPE:
|
Pending
didactic-model
¶
Pending(*, computation: Residual = field(opaque=True, extras=OPAQUE_BY_IDENTITY), result: Term = field(opaque=True, extras=OPAQUE_BY_VALUE), then: Callable[[SemanticValue], SemanticComputation] = field(opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: ValueModel
A neutral computation bound to a continuation: Filinski's residual bind.
| PARAMETER | DESCRIPTION |
|---|---|
computation
|
The neutral computation.
TYPE:
|
result
|
The type of the value it returns.
TYPE:
|
then
|
The rest of the computation, given that value.
TYPE:
|
Normalizer
¶
Normalizer(signature: Signature, structure: TypeDirectedStructure, system: JudgmentSystem, judgment: str)
Normalization by evaluation over a signature's typing judgment.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature.
TYPE:
|
structure
|
The recognized structures with their type formers.
TYPE:
|
system
|
The judgments.
TYPE:
|
judgment
|
The typing judgment: a context, the scoped term, and one output type.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
NormalizationError
|
If the judgment does not have that shape. |
Source code in src/quivers/structural/theories/nbe.py
derive
¶
derive(term: Term, context: Sequence[Term] = ()) -> tuple[Derivation, UnificationProblem]
The unique typing derivation of a term.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
context
|
The types of its free variables, outermost first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of Derivation and UnificationProblem
|
The derivation and the solution of its type variables. |
| RAISES | DESCRIPTION |
|---|---|
NormalizationError
|
If the term has no typing or more than one. |
Source code in src/quivers/structural/theories/nbe.py
normalize
¶
The beta-eta-long normal form of a term at its principal type.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
context
|
The types of its free variables, outermost first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
Its normal form. |
Source code in src/quivers/structural/theories/nbe.py
equal_modulo
¶
Whether two terms are equal modulo the type-directed laws.
Both are normalized at their unified principal type.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
The terms.
TYPE:
|
right
|
The terms.
TYPE:
|
context
|
The types of their free variables.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/quivers/structural/theories/nbe.py
evaluate
¶
evaluate(derivation: Derivation, problem: UnificationProblem, environment: tuple[SemanticValue, ...]) -> SemanticValue
Evaluate a typed term into the semantic domain.
| PARAMETER | DESCRIPTION |
|---|---|
derivation
|
The term's typing derivation, which follows its structure.
TYPE:
|
problem
|
The solution of its type variables.
TYPE:
|
environment
|
The values of its free variables, outermost first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SemanticValue
|
Its value. |
| RAISES | DESCRIPTION |
|---|---|
NormalizationError
|
If a non-structural constructor binds variables. |
Source code in src/quivers/structural/theories/nbe.py
reify
¶
reify(value: SemanticValue, type_: Term, depth: int) -> Term
Read a value back as a normal term at a type.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value.
TYPE:
|
type_
|
Its type.
TYPE:
|
depth
|
The number of variables in scope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The beta-eta-long normal form. |
| RAISES | DESCRIPTION |
|---|---|
NormalizationError
|
If the value does not fit the type. |
Source code in src/quivers/structural/theories/nbe.py
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 | |
reflect
¶
reflect(neutral: Residual, type_: Term) -> SemanticValue
Turn a neutral term into a value at a type, eta-expanding it.
| PARAMETER | DESCRIPTION |
|---|---|
neutral
|
The neutral term.
TYPE:
|
type_
|
Its type.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SemanticValue
|
Its value. |
Source code in src/quivers/structural/theories/nbe.py
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 | |
recognize
¶
recognize(laws: Sequence[Equation]) -> TypeDirectedStructure
Find the complete type-directed structures among a theory's laws.
Each law is tried in both orientations against each schema. A structure is returned only when every one of its laws is present and all of them agree on the constructors.
| PARAMETER | DESCRIPTION |
|---|---|
laws
|
The unoriented equations.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeDirectedStructure
|
The structures and the laws they account for. |
Source code in src/quivers/structural/theories/nbe.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
attach_types
¶
attach_types(structure: TypeDirectedStructure, system: JudgmentSystem, judgment: str) -> TypeDirectedStructure
Read each structure's type former from the typing judgment's rules.
The type former of a structure is the head of the type the typing rule of its introduction form concludes: abstraction for functions, pairing for products, and return for the monad.
| PARAMETER | DESCRIPTION |
|---|---|
structure
|
The recognized structure.
TYPE:
|
system
|
The judgments.
TYPE:
|
judgment
|
The typing judgment.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeDirectedStructure
|
The structure with its type formers. |
| RAISES | DESCRIPTION |
|---|---|
NormalizationError
|
If an introduction form has no typing rule, or rules that conclude different type formers. |
Source code in src/quivers/structural/theories/nbe.py
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 | |