Scopes¶
The reference machine keeps the values of the locals
in scope in a Scope, a persistent map. Binding a local returns a new
scope that shares the old one's nodes, so a pending frame keeps its environment without a copy.
Binding and lookup walk one path of the trie and scan only the entries that share the key's full
hash. For well-distributed hashes, the path length grows logarithmically with the number of locals.
scope
¶
Persistent environments of the reference machine.
A run of the machine binds a local at every Bind, every call and every
case branch, and keeps the environment of each pending frame so the frame
can continue under it. A copied dictionary makes every bind cost the size
of the scope, so a program of n binds runs in time quadratic in n.
Scope is a persistent map instead: binding returns a new scope that
shares all but one path of the old one, so a frame keeps its environment
without a copy.
The map is a hash array mapped trie of branching 32. Binding and lookup
walk one path of the trie, whose depth is at most ceil(64 / 5) = 13
and, for a scope of n locals with well-spread hashes, about
log_32 n; each step makes at most one node of at most 32 entries. Two
locals whose 64-bit hashes are equal share a bucket, scanned in full.
Scope
¶
Bases: Mapping[K, V]
A persistent map from locals to the values they are bound to.
A scope never changes: bind and extend return a new scope sharing
the old one's nodes, so a frame that keeps a scope keeps it at no
cost. Each operation walks one path and inspects the entries sharing
the key's full hash. With well-distributed hashes, binding and lookup
take time logarithmic in the scope's size.
| PARAMETER | DESCRIPTION |
|---|---|
entries
|
The bindings to start from; none for the empty scope.
TYPE:
|
Source code in src/quivers/qiec/scope.py
of
classmethod
¶
of(entries: Mapping[K, V] | Iterable[tuple[K, V]]) -> Scope[K, V]
A scope with given bindings, reusing one that is already a scope.
| PARAMETER | DESCRIPTION |
|---|---|
entries
|
The bindings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Scope
|
|
Source code in src/quivers/qiec/scope.py
bind
¶
bind(key: K, value: V) -> Scope[K, V]
This scope with one local bound, or rebound.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The local.
TYPE:
|
value
|
Its value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Scope
|
The new scope; this one is unchanged. |
Source code in src/quivers/qiec/scope.py
extend
¶
extend(pairs: Iterable[tuple[K, V]]) -> Scope[K, V]
This scope with several locals bound in order.
| PARAMETER | DESCRIPTION |
|---|---|
pairs
|
The bindings; a later one of a key replaces an earlier.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Scope
|
The new scope; this one is unchanged. |
Source code in src/quivers/qiec/scope.py
__getitem__
¶
The value a local is bound to.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The local.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
V
|
Its value. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the local is not bound. |
Source code in src/quivers/qiec/scope.py
__iter__
¶
Every bound local.
| RETURNS | DESCRIPTION |
|---|---|
Iterator[K]
|
The locals, in an order fixed by their hashes. |
__len__
¶
__repr__
¶
The bindings, as a dictionary shows them.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The representation. |