Structural conversion of sequential GraphSpec chains to DAG terms #
This module embeds each sequential primitive as a DAG primitive and lowers composition to explicit SSA-style let bindings.
Lowering: sequential chain → DAG term #
GraphSpec has two surface syntaxes:
NN.GraphSpec.Core: a sequential DSL (Chain+>>>), ideal for pure pipelines.NN.GraphSpec.DAG.Core: a general SSA/A-normal-form term language, ideal for sharing/skip connections.
The DAG term language is GraphSpec’s “general graph” core: it is the representation that can express sharing and skip connections.
Chain exists because it is the clearest way to write pipelines, and it has its own
direct Spec semantics (Interp.spec) and program translation (Chain.toProgram).
This lowering is still useful whenever you want to embed a sequential pipeline into the DAG world (e.g. to reuse DAG-only tooling, or to keep a single GraphSpec example surface that can export DAG models).
The declarations below provide a structural lowering:
Chain.toDAGTermproduces aDAG.Term (ps ++ [σ]) τ, i.e. a DAG term whose environment starts with the parameter listpsand ends with the (single) data inputσ. Notes:- The lowering is purely structural: it introduces
let1binders between stages to make the sequential flow explicit in SSA form. - Each sequential
Primitive ps σ τis embedded as a DAG primitive op with inputsps ++ [σ]. This embedding is generic: any custom GraphSpec primitive automatically becomes usable in the DAG world.
Lowering internals #
The definitions below (castTerm, toTerm, …) are adapters for the structural lowering. The
principal entry point is Chain.toDAGTerm.
Cast a DAG.Term across a proven equality of output shapes.
Instances For
Cast the environment of a DAG.Term across a proven equality of environments.
Instances For
Primitive embedding: Primitive → DAG.PrimOp #
Embed a sequential GraphSpec primitive as a DAG primitive op.
The resulting op has input shapes ps ++ [σ] (parameters followed by the data input).
Instances For
Building well-typed DAG arguments for a primitive call #
Build a typed DAG.Args list from an index-based family of argument terms.
This is the bridge from “arguments as a function of Fin ins.length” to the inductive DAG.Args
encoding used by DAG.Term.op.
Instances For
Append one final term to a typed DAG argument list.
Instances For
Reference the ith parameter block inside a larger environment layout.
The surrounding environment is split as pre ++ ps ++ post ++ extra; this helper returns the term
that points at parameter i : Fin ps.length while keeping the full ambient environment explicit.
Instances For
Lower a unary Primitive application into the DAG term language.
Parameters are read from the middle ps segment of the ambient environment, in the same order as
the primitive's parameter ABI, and the final data input is supplied by x.
Instances For
Chain lowering #
Lower a sequential Chain to an SSA-style DAG.Term, with parameters read from the
environment.
Instances For
Public API #
Lower a sequential Chain to a DAG term with environment ps ++ [σ].