Typed context indices #
A tape-style graph names its inputs and intermediates by position, so every node has to say "the
ith saved tensor" without losing the shape invariant that makes the node typecheck. Idx Γ s
is that name: a position in the context Γ bundled with a proof that the entry sitting there has
shape s.
The type carries no element type, which is why it lives here rather than beside any one soundness
development. The real-valued tape proofs, the CommSemiring-generic ones, and the
runtime-approximation graphs all index contexts the same way, and they used to do it through three
byte-identical copies of this structure. One definition means a lemma about indices proved in one
of those developments is usable in the others.
Alongside the structure are the two operations every graph construction needs:
Idx.weakenextends the context with more intermediates and keeps the index valid;Idx.lastnames the freshly appended entry ofΓ ++ ss ++ [τ].
Both are pure list arithmetic, and centralizing them keeps that boilerplate out of every op graph (LayerNorm, BatchNorm, attention, …).
A typed index into a heterogeneous context Γ, carrying a proof that the selected entry has the
expected shape s.
Position in the heterogeneous context.
Proof that the selected context entry has shape
s.
Instances For
Read a tensor out of a context at a typed index, casting along the shape equality the index carries.
The cast is what makes the result Tensor α s instead of Tensor α (Γ.get idx.i), so callers
never have to rewrite the ambient shape by hand.
Instances For
A shape-preserving pack map commutes with typed tensor selection.
Weaken a typed index when the context is extended by appending more shapes.
If idx : Idx Γ s, then weaken idx rest : Idx (Γ ++ rest) s.
Instances For
Typed index for the last element of an appended shape list.
Idx.last is the canonical index of τ in Γ ++ ss ++ [τ].
Instances For
Two packs agree when every typed tensor selection agrees.