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:

The scalar parameter serves several purposes:

Related work:

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 α, NatCast α, RatCast α, TorchLean.Numeric.QuotientArithmetic α :

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 decidableGT.

    Instances For
      def Context.stopGradient {α : Type} [Context α] (x : α) :
      α

      Clear a scalar's differentiation metadata, if its carrier has any.

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

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

        The instance has low priority so that a type's own decision procedure (Real.decidableLT, Float.decLt, ...) wins whenever one exists.

        Lawful contexts #

        Context α carries no laws: it only bundles operations. LawfulContext α records, for a scalar type that also carries a Mathlib ordered field structure, that the Context dictionary computes exactly the same operations. Generic proofs about scalar-polymorphic specifications can then rewrite the dictionary operations into the ordinary ring operations and finish with Mathlib.

        Only exact facts are recorded. Transcendental constants (lnTen, pi, ...) and the total power operation are backend specific and stay unconstrained.

        Compatibility of a Context dictionary with a linearly ordered field structure on the same type.

        Each field equates a Context projection (written with the explicit instance path Context.to*) with the corresponding Mathlib operation.

        • add_eq (x y : α) : x + y = x + y

          Dictionary addition is ring addition.

        • mul_eq (x y : α) : x * y = x * y

          Dictionary multiplication is ring multiplication.

        • sub_eq (x y : α) : x - y = x - y

          Dictionary subtraction is ring subtraction.

        • div_eq (x y : α) : x / y = x / y

          Dictionary division is field division.

        • neg_eq (x : α) : -x = -x

          Dictionary negation is ring negation.

        • The dictionary zero is the ring zero.

        • one_eq : One.one = One.one

          The dictionary one is the ring one.

        • lt_iff (x y : α) : x < y x < y

          The dictionary strict order is the field order.

        • le_iff (x y : α) : x y x y

          The dictionary order is the field order.

        • max_eq (x y : α) : xy = max x y

          Dictionary max is the lattice maximum.

        • min_eq (x y : α) : xy = min x y

          Dictionary min is the lattice minimum.

        • beq_iff (x y : α) : (x == y) = true x = y

          Boolean equality decides propositional equality.

        • natCast_eq (n : ) : n = n

          The natural-number cast is the semiring cast.

        • ratCast_eq (value : ) : value = value

          The rational-number cast is the field cast.

        • abs_eq (x : α) : MathFunctions.abs x = |x|

          The dictionary absolute value is the lattice absolute value.

        • defaultEpsilon_pos : 0 < Context.defaultEpsilon

          The backend tolerance is strictly positive.

        Instances
          @[instance_reducible, inline]

          Full Context instance for Float (runtime backend).

          @[instance_reducible, inline]

          Full Context instance for native binary32 execution.