TorchLean API

NN.Spec.Core.Context

Context α: scalar interface for models + proofs #

TorchLean is designed to be scalar-polymorphic: the same model/layer definitions can be instantiated over many numeric backends:

Why we designed it this way:

Some relevant literature we were following / taking inspiration from:

Our Context α is the same engineering pattern in a Lean setting: one model/layer definition, many scalar interpretations, and explicit tradeoffs about semantics.

To make this practical, we collect the numeric operations required by neural networks into a single typeclass:

Context α

This is broader than a standard algebraic structure: it bundles arithmetic, ordering, and common transcendental functions (exp/tanh/log/sqrt) used by activations and losses.

Notes #

class Context (α : Type) extends Inhabited α, One α, Zero α, Add α, Sub α, Mul α, Div α, Neg α, Pow α α, Max α, Min α, BEq α, LT α, LE α, MathFunctions α, Numbers α, Coe α :

The full scalar interface required by spec‑level tensors and models.

Instances
    def Context.gtBool {α : Type} [Context α] (x y : α) :

    Decide x > y as a Bool using the Context's decidable_gt.

    Instances For
      @[implicit_reducible]
      instance instDecidableRelGt_nN {α : Type} [Context α] :
      DecidableRel fun (x1 x2 : α) => x1 > x2

      A Context includes a decidable > relation; expose it as a standard typeclass.

      Rational Backend #

      Context includes transcendental functions and real-valued exponentiation (Pow α α) because many models (softmax, tanh, etc.) need them when instantiated over Float / / interval scalars.

      For , most transcendental functions do not map rationals to rationals, so there is no canonical exact interpretation. TorchLean therefore does not install the rational Context globally. Purely algebraic tests can opt in explicitly with:

      open scoped NN.Spec.RationalAlgebraic
      

      Current policy:

      @[implicit_reducible]

      Pow ℚ ℚ instance used for the rational backend.

      Policy: support x^y only when y is an integer rational (y.den = 1); otherwise return 0. The instance is scoped so it is unavailable unless the caller explicitly opens NN.Spec.RationalAlgebraic.

      Instances For
        @[implicit_reducible]

        MathFunctions dictionary for the rational backend.

        Only abs is meaningful; other transcendental functions are defined as 0 in this scoped backend and should not be used for semantic claims. Keeping this scoped makes unsupported transcendental rational models fail at elaboration unless a file deliberately opts into the algebraic-test backend.

        Instances For
          @[implicit_reducible]

          Numbers dictionary for the rational backend.

          The transcendentals (log10, etc.) are defined as 0 in this scoped backend; the basic rational literals are exact.

          Instances For
            @[implicit_reducible]

            Full opt-in Context dictionary for exact rational algebraic fragments.

            Instances For
              @[implicit_reducible]

              Full Context instance for Float (runtime backend).

              @[implicit_reducible]
              noncomputable instance instContextReal :

              Full Context instance for (proof backend, noncomputable).