Second-order patterns¶
Patterns are terms of a signature with metavariables, the sides of equations and of judgment rules. This module sorts and checks them, matches them against terms, decides membership in Miller's pattern fragment, and solves higher-order pattern unification problems, and it reads written patterns against a signature (Equations and normalization §1).
patterns
¶
Second-order patterns: matching, instantiation, and Miller unification.
A pattern is a term of a binding signature that may contain
metavariables of context arity, written M[x, y]: an unknown term in
the scope of k extra variables, instantiated by second-order
substitution [@fiore2010second]. The left side of an equation or rule is
in Miller's pattern fragment when every metavariable occurrence is applied
to distinct variables bound inside the pattern [@miller1991logic]; then
matching a term against it has at most one solution and unification of two
patterns has a most general unifier when one exists.
Patterns use the de Bruijn discipline of quivers.structural.signature: a
PatternVariable refers to a binder of the pattern by index, counting
from the innermost. A metavariable's solution is a Solution, a term in
the context of the matched redex extended by the metavariable's arity, so
M[N] instantiates it with quivers.structural.binding.instantiate.
A metavariable may also stand at a data position, as the n of
Const(n, A): it is a data metavariable, of the data sort of that
position, and takes no arguments. Matching binds it to the data value
there, so its solution's body is that value; it unifies with a data value
by equality, type included; and instantiation puts the value back. A
metavariable is a data metavariable everywhere or nowhere: one used at a
data position and at a term position is refused.
Unification is computed in a locally nameless form: while it descends
under a binder it opens the binder's variables as FreeVariable names, so
a variable never changes its name as it moves between depths.
Pattern
¶
Pattern = PatternVariable | FreeVariable | Metavariable | PatternApplication
A second-order pattern.
PatternError
¶
Bases: ValueError
A pattern is ill-formed, ill-sorted, or outside the pattern fragment.
PatternVariable
didactic-model
¶
Bases: Model
A variable bound inside a pattern, by de Bruijn index.
| PARAMETER | DESCRIPTION |
|---|---|
index
|
The index, counting from the innermost enclosing binder.
TYPE:
|
FreeVariable
didactic-model
¶
Bases: Model
A variable opened by unification under a binder.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Its identity, unique in one unification problem.
TYPE:
|
Metavariable
didactic-model
¶
Bases: ValueModel
A metavariable applied to arguments: M[a_1, ..., a_k].
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The metavariable.
TYPE:
|
args
|
The terms its bound variables are replaced by.
TYPE:
|
PatternApplication
didactic-model
¶
PatternApplication(*, op: str, args: tuple = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: ValueModel
A constructor applied to patterns and data leaves.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor.
TYPE:
|
args
|
Its arguments; an argument that binds variables is a pattern in the extended scope.
TYPE:
|
MetaSort
didactic-model
¶
Bases: Model
The type of a metavariable: the sorts of its arguments and of its body.
| PARAMETER | DESCRIPTION |
|---|---|
arguments
|
The sort of each bound variable, outermost first.
TYPE:
|
result
|
The sort of the body.
TYPE:
|
Solution
didactic-model
¶
Bases: ValueModel
The value of a metavariable: a body in the scope of arity variables.
| PARAMETER | DESCRIPTION |
|---|---|
arity
|
The number of variables the body binds; the last is index 0.
TYPE:
|
body
|
The body, in the matched redex's context extended by
TYPE:
|
same
¶
same(other: Solution) -> bool
Whether two solutions are equal, a data value's type included.
| PARAMETER | DESCRIPTION |
|---|---|
other
|
The other solution.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/quivers/structural/patterns.py
UnificationProblem
¶
The state of one higher-order pattern unification.
Metavariables are solved by bodies that bind their arguments and mention no other free variable: a metavariable that may depend on a variable takes it as an argument (it is raised over it). Fresh metavariables are introduced by pruning. A data metavariable is solved by a data value, and unifies with one only when they are equal.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature.
TYPE:
|
sorts
|
The type of every metavariable.
TYPE:
|
Source code in src/quivers/structural/patterns.py
fresh_free
¶
fresh_free(sort: str) -> FreeVariable
A new free variable.
| PARAMETER | DESCRIPTION |
|---|---|
sort
|
Its sort.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FreeVariable
|
The variable. |
Source code in src/quivers/structural/patterns.py
open
¶
open(body: PatternArgument, values: Sequence[Pattern]) -> PatternArgument
Replace the innermost len(values) bound variables of a body.
values[-1] replaces index 0. The values must not contain
pattern variables, so nothing is shifted.
| PARAMETER | DESCRIPTION |
|---|---|
body
|
The body.
TYPE:
|
values
|
The replacements, outermost first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PatternArgument
|
The opened body. |
Source code in src/quivers/structural/patterns.py
close
¶
close(body: PatternArgument, names: Sequence[int]) -> PatternArgument
Bind free variables: the inverse of open.
| PARAMETER | DESCRIPTION |
|---|---|
body
|
The body.
TYPE:
|
names
|
The free variables to bind, outermost first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PatternArgument
|
The body with |
Source code in src/quivers/structural/patterns.py
resolve
¶
resolve(pattern: PatternArgument) -> PatternArgument
Instantiate a solved metavariable at the head, repeatedly.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PatternArgument
|
A pattern whose head is not a solved metavariable. |
Source code in src/quivers/structural/patterns.py
full
¶
full(pattern: PatternArgument) -> PatternArgument
Instantiate every solved metavariable of a pattern.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PatternArgument
|
The pattern with only unsolved metavariables. |
Source code in src/quivers/structural/patterns.py
fresh_meta
¶
A new metavariable.
| PARAMETER | DESCRIPTION |
|---|---|
arguments
|
The sorts of its arguments.
TYPE:
|
result
|
The sort of its body.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Its name. |
Source code in src/quivers/structural/patterns.py
unify
¶
unify(left: PatternArgument, right: PatternArgument) -> bool
Unify two patterns, extending the solution.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
The patterns, with every enclosing binder opened.
TYPE:
|
right
|
The patterns, with every enclosing binder opened.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether a unifier exists. |
Source code in src/quivers/structural/patterns.py
PatternSyntax
didactic-model
¶
PatternSyntax(*, kind: Literal['name', 'apply', 'instance', 'abstract', 'extend', 'leaf'], name: str = '', names: tuple[str, ...] = (), args: tuple[PatternSyntax, ...] = (), leaf: str | int | None = None, line: int = 0, col: int = 0)
Bases: Model
A pattern as written, before names are resolved.
kind is "name" for a bare name, "apply" for Op(...),
"instance" for M[...], "abstract" for x y. body,
"extend" for a context extension (ctx, x : A) (its name is
the context, names the entries and args their values), and
"leaf" for a literal.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
The form.
TYPE:
|
name
|
The name or head, empty for a leaf.
TYPE:
|
names
|
The names an abstraction binds.
TYPE:
|
args
|
The arguments, or the single body of an abstraction.
TYPE:
|
leaf
|
The literal of a leaf.
TYPE:
|
line
|
The source position, zero when unknown.
TYPE:
|
col
|
The source position, zero when unknown.
TYPE:
|
from_term
¶
Read a term as a pattern without metavariables.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Pattern
|
The same tree. |
Source code in src/quivers/structural/patterns.py
metavariables
¶
metavariables(pattern: PatternArgument) -> Iterator[Metavariable]
Every metavariable occurrence of a pattern, in prefix order.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
Metavariable
|
Each occurrence. |
Source code in src/quivers/structural/patterns.py
render_pattern
¶
render_pattern(pattern: PatternArgument, signature: Signature) -> str
The text of a pattern, with #i for a bound variable by index.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern.
TYPE:
|
signature
|
Its signature.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The text. |
Source code in src/quivers/structural/patterns.py
infer_metavariable_sorts
¶
infer_metavariable_sorts(pattern: Pattern, signature: Signature, sort: str | None = None) -> dict[str, MetaSort]
Infer the type of every metavariable of a pattern from its positions.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
A closed pattern: its variables are bound inside it.
TYPE:
|
signature
|
The signature.
TYPE:
|
sort
|
The sort the pattern must have, or
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict of str to MetaSort
|
The type of each metavariable. |
| RAISES | DESCRIPTION |
|---|---|
PatternError
|
If a constructor is undeclared or misapplied, a variable is out of scope or of the wrong sort, or a metavariable is used at two types. |
Source code in src/quivers/structural/patterns.py
check_pattern_sorts
¶
check_pattern_sorts(pattern: PatternArgument, signature: Signature, context: tuple[str, ...], sort: str | None, metas: dict[str, MetaSort]) -> str
Check a pattern's sorts, recording or checking metavariable types.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern.
TYPE:
|
signature
|
The signature.
TYPE:
|
context
|
The sorts of its bound variables in scope, outermost first.
TYPE:
|
sort
|
The sort it must have.
TYPE:
|
metas
|
Known metavariable types; new ones are added.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The pattern's sort. |
| RAISES | DESCRIPTION |
|---|---|
PatternError
|
If the pattern is ill-sorted. |
Source code in src/quivers/structural/patterns.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
check_miller_pattern
¶
Require a left side in Miller's pattern fragment.
The root is a constructor application, and every metavariable is applied to distinct variables bound inside the pattern.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The left side.
TYPE:
|
signature
|
The signature.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
PatternError
|
If the pattern is outside the fragment, naming the occurrence. |
Source code in src/quivers/structural/patterns.py
match
¶
match(pattern: Pattern, term: Term, signature: Signature, solutions: Mapping[str, Solution] | None = None) -> dict[str, Solution] | None
Match a term against a left side in the pattern fragment.
Matching a metavariable M[x_1, ..., x_k] succeeds when the term uses
no variable bound inside the pattern other than the x_i; its
solution abstracts them. A data metavariable binds the data value at
its position. A metavariable that occurs twice must match alpha-equal
terms, or equal data values of the same type. The term's own free
variables pass through.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The left side.
TYPE:
|
term
|
The term.
TYPE:
|
signature
|
The signature.
TYPE:
|
solutions
|
Metavariables already solved.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict of str to Solution or None
|
The unique solution, or |
Source code in src/quivers/structural/patterns.py
instantiate_pattern
¶
instantiate_pattern(pattern: PatternArgument, solutions: Mapping[str, Solution], signature: Signature, depth: int = 0) -> TermArg
The term a pattern denotes under metavariable solutions.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern, typically a right side.
TYPE:
|
solutions
|
A solution for every metavariable it uses.
TYPE:
|
signature
|
The signature.
TYPE:
|
depth
|
The number of pattern binders around it.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TermArg
|
The term, in the redex's context extended by |
| RAISES | DESCRIPTION |
|---|---|
PatternError
|
If a metavariable has no solution or the wrong number of arguments. |
Source code in src/quivers/structural/patterns.py
resolve
¶
resolve(syntax: PatternSyntax, signature: Signature, scope: tuple[str, ...] = ()) -> PatternArgument
Resolve the names of a written pattern against a signature.
A bare name is a bound variable when a binder in scope binds it, a
nullary constructor when the signature declares one, and a
metavariable of arity zero otherwise. M[...] is a metavariable
instantiation and Op(...) a constructor application, whose
abstractions must bind as many names as the argument's arity.
| PARAMETER | DESCRIPTION |
|---|---|
syntax
|
The written pattern.
TYPE:
|
signature
|
The signature.
TYPE:
|
scope
|
The names bound around it, outermost first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PatternArgument
|
The pattern. |
| RAISES | DESCRIPTION |
|---|---|
PatternError
|
If a constructor is applied to the wrong number of arguments, an abstraction has the wrong number of names, a binding argument has none, or a name in a metavariable's head is bound. |
Source code in src/quivers/structural/patterns.py
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 | |
parse_pattern
¶
parse_pattern(text: str) -> PatternSyntax
Read a pattern written in source syntax.
Op(a, x y. body) applies a constructor, M[a, b] instantiates a
metavariable, a bare name is resolved later by resolve, and string
and integer literals are data leaves.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The pattern.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PatternSyntax
|
The written pattern. |
| RAISES | DESCRIPTION |
|---|---|
PatternError
|
If the text is not a pattern. |
Source code in src/quivers/structural/patterns.py
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 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 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 | |
pattern
¶
Read and resolve a pattern.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The pattern in source syntax.
TYPE:
|
signature
|
The signature.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Pattern
|
The pattern. |
| RAISES | DESCRIPTION |
|---|---|
PatternError
|
If it does not parse or resolve. |
Source code in src/quivers/structural/patterns.py
generic_name
¶
The generic constant of a metavariable.
| PARAMETER | DESCRIPTION |
|---|---|
meta
|
The metavariable.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
|