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 intentionally 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 MathFunctions (α : Type) :

Scalar transcendental functions used in activations and losses.

  • exp : αα
  • tanh : αα
  • cosh : αα
  • sqrt : αα
  • abs : αα
  • log : αα
  • pi : α
  • cos : αα
  • sin : αα
  • sinh : αα
Instances
    class Numbers (α : Type) :

    Common scalar constants used in model definitions.

    • neg_point_five : α
    • neg_one : α
    • pointone : α
    • pointfive : α
    • one : α
    • zero : α
    • two : α
    • three : α
    • four : α
    • five : α
    • ten : α
    • log10 : α
    • log10000 : α
    • epsilon : α
    • neg_thousand : α
    Instances

      Friendly aliases: these keep the original names (MathFunctions, Numbers) and give more descriptive names for scalar-facing code.

      @[reducible, inline]
      abbrev ScalarMath (α : Type) :

      Alias for MathFunctions.

      Instances For
        @[reducible, inline]
        abbrev ScalarConstants (α : Type) :

        Alias for Numbers.

        Instances For
          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.

              @[implicit_reducible]

              MathFunctions instance for Lean's Float (runtime-oriented backend).

              @[implicit_reducible]
              noncomputable instance instMathFunctionsReal :

              MathFunctions instance for (proof backend, noncomputable).

              @[implicit_reducible]

              Numbers literals for Float (runtime-oriented backend).

              @[implicit_reducible]
              noncomputable instance instNumbersReal :

              Numbers literals for (proof backend, noncomputable).

              @[implicit_reducible]

              Coerce naturals into Float using Float.ofNat.

              @[implicit_reducible]

              Coerce naturals into via the standard Nat-to-real coercion.

              @[implicit_reducible]

              Coerce naturals into via the standard Nat-to-rational coercion.

              A lightweight 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 lightweight 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 lightweight 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 lightweight 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).