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 is independent of tensor elements. The real-valued tape proofs, the
CommSemiring-generic ones, and the runtime-approximation graphs share this context indexing
and its lemmas.
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
Two typed indices with the same position are equal.
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.