Skip to content

Commit 2460645

Browse files
authored
docs(patterns): start with simple example (#2442)
## Description / Documentation Considerations Start the README with a simple example (taken from a test). Make the `PatternMatchers` interface that defines `M`'s methods easier to find. - Demote some material to the JSDoc of relevant types. - add various links - Demote some stuff to CONTRIBUTING. ### Security / Scaling / Testing / Compatibility / Upgrade Considerations n/a
2 parents 335bbba + f0b4117 commit 2460645

File tree

3 files changed

+126
-99
lines changed

3 files changed

+126
-99
lines changed

packages/patterns/CONTRIBUTING.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
## Invariants
3+
4+
Any Passable value is a possibly-empty tree of `passStyleOf`-level containers (CopyArray, CopyRecord, CopyTagged) in which each node may be extended with an arbitrary number of non-container Passable leaves (an isolated non-container Passable is a sole leaf of an empty tree).
5+
If no leaf is a Capability (i.e., a Remotable or Promise), then the Passable value is Data --- it carries only immutable information, without any connection to external references or unforgeable identity.
6+
7+
Guards do not yet exist as distinct kinds, so we ignore them for now. TODO: Expand this if kinds expand to include guards.
8+
9+
As mentioned above, `keyEQ` is pass-invariant: if passing `xa` from vatA to vatB arrives as `xb`, and likewise `ya` and `yb`, then `keyEQ(xa,ya)` iff `keyEQ(xb,yb)`. And because we do not wish to give Promises, Errors, or unrecognized CopyTagged values any useful pass-invariant equality, a Key may not include any of those.
10+
11+
These conditions all apply to Patterns as well. The differences are:
12+
* A Pattern can contain Matchers, but a Key cannot. All Keys are Patterns, but Patterns that include Matchers are not Keys.
13+
* A non-Key value (including a non-Key Pattern), cannot be an element of a CopySet or CopyBag, or a key of a CopyMap.
14+
15+
Patterns are pass-invariant Passable decidable synchronous predicates over Passables that may be used by mutually suspicious parties, and therefore cannot be user-extensible by code predicates. In several ways including this one, Patterns feel much like conventional types.
16+
17+
## Relationships between types
18+
19+
The set of all primitive values is a strict subset of Data, which is a strict subset of Keys.
20+
21+
The set of all primitive values is also a strict subset of the set of Scalars (which is the union of Primitives and Capabilities [i.e., Remotables and Promises]). The union of primitive values and Remotables is a strict subset of Keys.
22+
23+
Keys is a strict subset of Patterns, which is a strict subset of Passables.
24+
25+
TODO: Include a diagram visually demonstrating the following.
26+
27+
More precisely (using "∪" for union and "∖" for set difference):
28+
* Passables = Containable\<Keys, Patterns ∪ Promises ∪ Errors ∪ UnrecognizedTaggeds>
29+
* Patterns = Containable\<Keys, Keys ∪ Matchers>
30+
* Keys = Containable\<Primitives ∪ Remotables> = Containable\<Scalars ∖ Promises>
31+
* Data = Containable\<Primitives>
32+
* Scalars = Primitives ∪ Capabilities
33+
* Capabilities = Remotables ∪ Promises
34+
* Containable\<_K_> = Containable\<_K_, _K_>
35+
* Containable\<_K_, _V_> = Containable∞\<_K_, _V_>
36+
* Containable₀\<_K_, _V_> = _K__V_
37+
* Containableₙ₊₁\<_K_, _V_> = Containableₙ\<_K_, _V_> ∪ (**C**\<Containableₙ\<_K_, _V_>> where **C** is CopyArray or CopyRecord) ∪ (**C**\<Containableₙ\<_K_, _K_>> where **C** is CopySet or CopyBag) ∪ CopyMap\<Containableₙ\<_K_, _K_>, Containableₙ\<_K_, _V_>>

packages/patterns/README.md

+22-90
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,35 @@
11
# `@endo/patterns`
22

3-
Builds on [`@endo/pass-style`](https://www.npmjs.com/package/@endo/pass-style) as described in [`kindOf` and `passStyleOf` levels of abstraction](./docs/marshal-vs-patterns-level.md) to define higher level data types as individual refinements of Passable CopyTagged records (PassStyle "tagged"):
4-
- CopySet -- a collection of unique distinguishable Keys
5-
- CopyBag -- a collection of entries associating a unique distinguishable Key with a positive integer count (see [Multiset](https://en.wikipedia.org/wiki/Multiset)).
6-
- CopyMap -- a collection of entries associating a unique distinguishable Key with a Passable
7-
- Matcher -- a predicate characterizing a subset of Passables, such as "strings" or "8-bit unsigned integer numbers" or "CopyArrays of Remotables"
3+
The main export from the package is an `M` namespace object, for making a variety of Matchers (hence "M"). For example:
84

9-
In support of the above, there is also `compareKeys` and `keyEQ` exposing pass-invariant Key comparison, and two concepts with corresponding TypeScript types:
10-
- Key -- a Passable arbitrarily deep acyclic data structure in which each non-leaf node is a CopyArray, CopyRecord, CopySet, CopyBag, or CopyMap that is the child of at most one other internal node (forming a possibly-empty tree of containers), and each leaf is either an empty such container or a Passable primitive value or a Remotable (but the same Remotable `r` may be a child of multiple parents, e.g. `{ foo: r, bar: [r] }`). A Key is stable and stably comparable with other Keys via `keyEQ`. Key is the most general data type covering valid contents for CopySets and CopyBags and keys for CopyMaps (the last of which explains the "Key" name).
11-
- Pattern -- a Passable value that can be used to *match* some subset of Passables. Each Pattern is either a Key that matches itself (and any copy of itself --- `keyEQ` considers identity only for Remotables, where it is shared across all local Presences of the same Remotable), or a Key-like structure in which one or more leaves is a Matcher rather than a primitive or Remotable.
12-
13-
The main export from the package is an `M` namespace object, for making a variety of Matchers (hence "M").
14-
15-
`M` can also make _Guards_ that use Patterns to characterize dynamic behavior such as method argument/response signatures and promise awaiting. The [`@endo/exo`](https://www.npmjs.com/package/@endo/exo) package uses InterfaceGuards (each of which maps a collection of method names to their respective method guards) as the first level of defense for Exo objects against malformed input. For example:
165
```js
17-
const AsyncSerializerI = M.interface('AsyncSerializer', {
18-
// This interface has a single method, which is async as indicated by M.callWhen().
19-
// The method accepts a single argument, consumed with an implied `await` as indicated by M.await(),
20-
// and the result of that implied `await` is allowed to fulfill to any value per M.any().
21-
// The method result is a string as indicated by M.string(),
22-
// which is inherently wrapped in a promise by the async nature of the method.
23-
getStringOf: M.callWhen(M.await(M.any())).returns(M.string()),
24-
});
25-
const asyncSerializer = makeExo('AsyncSerializer', AsyncSerializerI, {
26-
// M.callWhen() delays invocation of this method implementation
27-
// while provided argument is in a pending state
28-
// (i.e., it is a promise that has not yet settled).
29-
getStringOf(val) { return String(val); },
30-
});
31-
32-
const stringP = asyncSerializer.getStringOf(Promise.resolve(42n));
33-
isPromise(stringP); // => true
34-
await stringP; // => "42"
35-
```
36-
37-
See [types.js](./src/types.js) for the definitions of these new types and (at typedefs `PatternMatchers` and `GuardMakers`) the methods of the exported `M` namespace object.
38-
39-
## Invariants
6+
import '@endo/init/debug.js';
7+
import { M, mustMatch } from '@endo/patterns';
408

41-
Any Passable value is a possibly-empty tree of `passStyleOf`-level containers (CopyArray, CopyRecord, CopyTagged) in which each node may be extended with an arbitrary number of non-container Passable leaves (an isolated non-container Passable is a sole leaf of an empty tree).
42-
If no leaf is a Capability (i.e., a Remotable or Promise), then the Passable value is Data --- it carries only immutable information, without any connection to external references or unforgeable identity.
9+
const specimen = harden({ foo: 3, bar: 4 });
4310

44-
Guards do not yet exist as distinct kinds, so we ignore them for now. TODO: Expand this if kinds expand to include guards.
11+
const pattern = M.splitRecord(
12+
{ foo: M.number() }, // required properties
13+
{ bar: M.string(), baz: M.number() }, // optional
14+
);
4515

46-
As mentioned above, `keyEQ` is pass-invariant: if passing `xa` from vatA to vatB arrives as `xb`, and likewise `ya` and `yb`, then `keyEQ(xa,ya)` iff `keyEQ(xb,yb)`. And because we do not wish to give Promises, Errors, or unrecognized CopyTagged values any useful pass-invariant equality, a Key may not include any of those.
47-
48-
These conditions all apply to Patterns as well. The differences are:
49-
* A Pattern can contain Matchers, but a Key cannot. All Keys are Patterns, but Patterns that include Matchers are not Keys.
50-
* A non-Key value (including a non-Key Pattern), cannot be an element of a CopySet or CopyBag, or a key of a CopyMap.
51-
52-
Patterns are pass-invariant Passable decidable synchronous predicates over Passables that may be used by mutually suspicious parties, and therefore cannot be user-extensible by code predicates. In several ways including this one, Patterns feel much like conventional types.
53-
54-
### Rank order and key order
55-
56-
The "key order" of `compareKeys` implements a partial order over Keys --- it defines relative position between two Keys but leaves some pairs incomparable (for example, subsets over sets is a partial order in which {} precedes {x} and {y}, which are mutually incomparable but both precede {x, y}).
57-
It is co-designed with the "rank order" (a total preorder) of `compareRank` from [`@endo/marshal`](https://www.npmjs.com/package/@endo/marshal) to support efficient range search for Key-based queries (for example, finding all entries in a map for which the key is a CopyRecord with particular fields can be implemented by selecting from rank-ordered keys those that are CopyRecords whose lexicographically greatest field is at least as big as the lexicographically greatest required field, and then filtering out matched keys that don't have the necessary shape).
58-
Both functions use `-1`, `0`, and `1` to respectively mean "less than", "equivalent to", and "greater than".
59-
`NaN` means "incomparable" --- the first key is not less than, equivalent to, or greater than the second.
60-
To keep the orders distinct when speaking informally, we use "earlier" and "later" for rank order, and "smaller" and "bigger" for key order.
61-
62-
The key ordering of `compareKeys` refines the rank ordering of `compareRank` but leaves gaps for which a more complete "full order" relies upon rank ordering:
63-
1. `compareKeys(X,Y) === 0` implies that `compareRank(X,Y) === 0` --- if X
64-
is equivalent to Y in key order, then X is equivalent to Y in rank order.
65-
But the converse does not hold; for example, Remotables `Far('X')` and
66-
`Far('Y')` are equivalent in rank order but incomparable in key order.
67-
2. `compareKeys(X,Y) < 0` implies that `compareRank(X,Y) < 0` --- if X is
68-
smaller than Y in key order, then X is earlier than Y in rank order.
69-
But the converse does not hold; for example, the record `{b: 3, a: 5}`
70-
is earlier than the record `{b: 5, a: 3}` in rank order but they are
71-
incomparable in key order.
72-
3. `compareRank(X,Y) === 0` implies that `compareKeys(X,Y)` is either
73-
0 or NaN --- Keys within the same rank are either equivalent to or
74-
incomparable to each other in key order. But the converse does not hold;
75-
for example, `Far('X')` and `{}` are incomparable in key order but not
76-
equivalent in rank order.
77-
4. `compareRank(X,Y) === 0` and `compareRank(X,Z) === 0` imply that
78-
`compareKeys(X,Y)` and `compareKeys(X,Z)` are the same --- all Keys within
79-
the same rank are either mutually equivalent or mutually incomparable, and
80-
in fact only in the mutually incomparable case can the rank be said to
81-
contain more than one key.
16+
mustMatch(specimen, pattern); // throws: 'bar?: number 4 - Must be a string'
17+
```
8218

83-
## Relationships between types
19+
See {@link PatternMatchers} for more on `M.splitRecord()`, `M.number()`, and other methods.
8420

85-
The set of all primitive values is a strict subset of Data, which is a strict subset of Keys.
21+
`M` also has {@link GuardMakers} methods to make {@link InterfaceGuard}s that use Patterns to characterize dynamic behavior such as method argument/response signatures and promise awaiting. The {@link @endo/exo!} package uses `InterfaceGuard`s as the first level of defense for Exo objects against malformed input.
8622

87-
The set of all primitive values is also a strict subset of the set of Scalars (which is the union of Primitives and Capabilities [i.e., Remotables and Promises]). The union of primitive values and Remotables is a strict subset of Keys.
23+
_For best rendering, use the [Endo reference docs](https://endojs.github.io/) site._
8824

89-
Keys is a strict subset of Patterns, which is a strict subset of Passables.
25+
## Key Equality, Containers
9026

91-
TODO: Include a diagram visually demonstrating the following.
27+
Builds on {@link @endo/pass-style!} as described in [`kindOf` and `passStyleOf` levels of abstraction](./docs/marshal-vs-patterns-level.md) to define higher level data types as individual refinements of Passable CopyTagged records (PassStyle "tagged"):
28+
- {@link CopySet} -- a collection of unique distinguishable {@link Key}s
29+
- {@link CopyBag} -- a collection of entries associating a unique distinguishable Key with a positive integer count (see [Multiset](https://en.wikipedia.org/wiki/Multiset)).
30+
- {@link CopyMap} -- a collection of entries associating a unique distinguishable Key with a Passable
31+
- {@link Matcher} -- a predicate characterizing a subset of Passables, such as "strings" or "8-bit unsigned integer numbers" or "CopyArrays of Remotables"
9232

93-
More precisely (using "∪" for union and "∖" for set difference):
94-
* Passables = Containable\<Keys, Patterns ∪ Promises ∪ Errors ∪ UnrecognizedTaggeds>
95-
* Patterns = Containable\<Keys, Keys ∪ Matchers>
96-
* Keys = Containable\<Primitives ∪ Remotables> = Containable\<Scalars ∖ Promises>
97-
* Data = Containable\<Primitives>
98-
* Scalars = Primitives ∪ Capabilities
99-
* Capabilities = Remotables ∪ Promises
100-
* Containable\<_K_> = Containable\<_K_, _K_>
101-
* Containable\<_K_, _V_> = Containable∞\<_K_, _V_>
102-
* Containable₀\<_K_, _V_> = _K__V_
103-
* Containableₙ₊₁\<_K_, _V_> = Containableₙ\<_K_, _V_> ∪ (**C**\<Containableₙ\<_K_, _V_>> where **C** is CopyArray or CopyRecord) ∪ (**C**\<Containableₙ\<_K_, _K_>> where **C** is CopySet or CopyBag) ∪ CopyMap\<Containableₙ\<_K_, _K_>, Containableₙ\<_K_, _V_>>
33+
In support of the above, there is also {@link compareKeys} and {@link keyEQ} exposing pass-invariant Key comparison, and two concepts with corresponding TypeScript types:
34+
- {@link Key} -- a Passable arbitrarily deep acyclic data structure in which each non-leaf node is a CopyArray, CopyRecord, CopySet, CopyBag, or CopyMap that is the child of at most one other internal node (forming a possibly-empty tree of containers), and each leaf is either an empty such container or a Passable primitive value or a Remotable (but the same Remotable `r` may be a child of multiple parents, e.g. `{ foo: r, bar: [r] }`). A Key is stable and stably comparable with other Keys via {@link keyEQ}. Key is the most general data type covering valid contents for CopySets and CopyBags and keys for CopyMaps (the last of which explains the "Key" name).
35+
- {@link Pattern} -- a Passable value that can be used to *match* some subset of Passables. Each Pattern is either a Key that matches itself (and any copy of itself --- `keyEQ` considers identity only for Remotables, where it is shared across all local Presences of the same Remotable), or a Key-like structure in which one or more leaves is a Matcher rather than a primitive or Remotable.

0 commit comments

Comments
 (0)