TorchLean API

NN.Tensor.Internal.Elab.Common

Shared elaboration infrastructure #

This module contains the common metaprogramming machinery used by every operation family. Literal term grammar lives in TorchLean.Tensor.Internal.Elab.Syntax. These internal utilities inspect dependent tensor types, preserve symbolic natural-number dimensions, construct heterogeneous tensor families, and emit proof-bearing checker certificates.

The helpers live in TorchLean.Tensor.Internal.Elab.Impl because they are implementation infrastructure rather than user API. Operation modules share them directly; there is no alternate tensor representation or compatibility elaborator.

Render a singular or plural label for a diagnostic source range.

Instances For

    Draw a caret marker beneath the source range selected by a diagnostic.

    Instances For

      Raise an elaboration error containing the pattern and its precise source range.

      Instances For

        Expose the elements of a statically known list while retaining symbolic element expressions.

        Read natural-number expressions only when every dimension is concrete.

        Instances For

          Read shape-expression spines as ordinary shapes only when every dimension reduces to a natural-number literal.

          Instances For

            Compute the length of a statically known list without inspecting its elements.

            Evaluate an integer expression when kernel reduction exposes a literal.

            Instances For

              Read a statically written list whose integer elements all reduce to literals.

              Read concrete unpack metadata as a statically written list of integer lists.

              Recover the element type, static shape, and storage selected by TorchLean.Tensor.

              The canonical public tensor abbreviation is unfolded before inspecting its packed representation. Arbitrary coordinate functions are not accepted as tensors.

              Instances For

                Elaborate one tensor and expose its static list structure while preserving symbolic dimension expressions.

                A nonempty list of equal-shaped tensors is accepted as a convenient stacked tensor and contributes its statically known list length as a leading axis.

                Instances For

                  Elaborate named supplementary axis lengths as natural-number expressions.

                  Instances For

                    Reify supplementary axis lengths when every supplied expression is concrete.

                    Instances For

                      Apply one explicit structure-constructor argument after checking its dependent field type.

                      Instances For

                        Seal a generated certificate in a kernel-checked auxiliary theorem.

                        The module-qualified theorem kind prevents exported terms in independently compiled modules from producing the same _einops_N declaration names.

                        Instances For
                          def TorchLean.Tensor.Internal.Elab.Impl.buildCertificate (constructorName : Lean.Name) (parameters fixedFields : Array Lean.Expr) (decidableFields : ) :

                          Build a proof-valued structure using explicit parameters, any fields that are already proved, and kernel-reduced decisions for the remaining fields.

                          Instances For

                            Run a focused tactic on a generated proposition and seal the resulting kernel certificate in an auxiliary theorem.

                            Callers choose the smallest tactic vocabulary appropriate for their invariant. Failure is reported as a missing user hypothesis rather than hidden behind an unchecked cast or axiom.

                            Instances For

                              Discharge a general symbolic shape invariant using the arithmetic and finite-family vocabulary shared by transformations and packing.

                              Instances For

                                Human-readable description of a logical transformation axis.

                                Instances For

                                  Look up the symbolic length currently assigned to a logical axis.

                                  Instances For

                                    Append a symbolic axis assignment unless that axis was assigned earlier.

                                    Instances For

                                      Construct a balanced-by-source-order natural-number product expression.

                                      Instances For

                                        Construct a source-order natural-number sum expression.

                                        Instances For

                                          Resolve a list of logical axes to their symbolic length expressions.

                                          Instances For

                                            Multiply the symbolic lengths assigned to a group of elementary axes.

                                            Instances For

                                              Flatten multiplication syntax after reducible normalization, preserving each factor expression for definitional matching.

                                              Flatten addition syntax after reducible normalization, preserving each summand expression for definitional matching.

                                              Remove one definitionally equal factor from a candidate multiset.

                                              Instances For

                                                Remove every requested factor by definitional equality, respecting multiplicity.

                                                Instances For

                                                  Recover the missing factor when the tensor type already displays a product.

                                                  For example, if the physical dimension is syntactically height * width and height is known, the remaining expression width is a valid axis length even when height = 0. Falling back immediately to natural-number division would unnecessarily demand positivity and would lose information deliberately present in the dependent tensor type.

                                                  Instances For

                                                    Recover the part of a packed length not occupied by known segments.

                                                    Addition is treated modulo association and order, but only definitionally equal summands are removed. This preserves expressions already present in the tensor type without asking the kernel to choose a subtraction normal form.

                                                    Instances For

                                                      Construct the type-level shape list represented by symbolic dimensions.

                                                      Instances For

                                                        Construct a list of already elaborated type-level shape expressions.

                                                        Instances For

                                                          Transport a generated tensor from a checker-indexed result shape to the compact dimension list computed by the surface operation.

                                                          The equality is definitional for generated plans, but the explicit transport prevents inferred declaration types from retaining the complete checker certificate.

                                                          Instances For

                                                            Construct the public tensor type corresponding to an internal list-shaped dimension expression.

                                                            Generated kernels use Rep directly, but inferred declaration types should retain the ordinary Tensor α [dims] spelling seen by users and editor tooling.

                                                            Instances For

                                                              Give an inferred tensor result its compact public type without changing its value or its checked implementation.

                                                              An explicit expected type remains authoritative. Otherwise the scalar and storage are recovered from the generated Rep, while shape is the compact shape expression computed by the public operation.

                                                              Instances For

                                                                Extract a tensor shape from an informative, non-metavariable expected type.

                                                                Instances For

                                                                  Extract the value from a checker result already proved to be successful.

                                                                  Instances For

                                                                    Register evidence that a reflected certificate is exactly the ordinary checker's result.

                                                                    The agreement is checked once as a named theorem. It is deliberately not embedded as a nondependent let in every generated value: doing so repeats a potentially large checker proposition during downstream type checking even though the proof has no computational or dependent use.

                                                                    Instances For
                                                                      partial def TorchLean.Tensor.Internal.Elab.Impl.buildTensorFamilyCases (scalarType storage : Lean.Expr) (inputShapes inputTensors : List Lean.Expr) (tensorIndex : Lean.Expr) :

                                                                      Eliminate a finite tensor index into one branch per heterogeneous operand, retaining each branch's exact dependent shape.

                                                                      Build the dependent Fin n-indexed family of heterogeneous input tensors.

                                                                      Instances For

                                                                        Elaborate a nonempty tensor family and convert mixed element types to one common type before operation-specific checking.

                                                                        Promotion folds from left to right. Each input conversion is composed during that fold and materialized at most once after the final common type is known. Homogeneous families retain their original tensors and storage exactly.

                                                                        Instances For

                                                                          Elaborate a common-scalar tensor family and share every required conversion around the generated consumer.

                                                                          The continuation sees local tensor variables rather than repeated conversion expressions. Each conversion is passed through nativeStage, whose no-inline boundary ensures native execution materializes it once before entering the consumer rather than sinking it into every downstream scalar read.

                                                                          Instances For