Documentation

LeanCert.Meta.ToExpr

Metaprogram for Reifying Lean Expressions to LeanCert AST #

This module provides metaprogramming infrastructure to translate Lean kernel expressions (e.g., fun (x : ℝ) => x + 2) into LeanCert.Core.Expr terms (e.g., Expr.add (Expr.var 0) (Expr.const 2)).

Main definitions #

Usage #

#leancert_reflect (fun x => x * x + 1)
-- Output: Expr.add (Expr.mul (Expr.var 0) (Expr.var 0)) (Expr.const 1)

Context for the translation. Stores fvars of the lambda being traversed. vars[0] corresponds to var 0, vars[1] to var 1, etc.

  • Array of free variables from the lambda telescope

Instances For
    @[reducible, inline]

    The translation monad: ReaderT over MetaM with our Context.

    Equations
    Instances For

      Look up a Free Variable in the context. Returns its de Bruijn index if found.

      Equations
      Instances For

        Helper Functions: AST Constructors #

        These functions build LeanCert.Core.Expr syntax trees. They construct Lean expressions that represent LeanCert AST terms.

        Build LeanCert.Core.Expr.const q for a rational number.

        Equations
        Instances For

          ToExpr instance for ℚ (rationals)

          Equations

          Build LeanCert.Core.Expr.var idx for a variable index.

          Equations
          Instances For

            Build LeanCert.Core.Expr.add e1 e2.

            Equations
            Instances For

              Build LeanCert.Core.Expr.mul e1 e2.

              Equations
              Instances For

                Constant Extraction #

                Lean stores numbers as complex hierarchies of type classes (OfNat.ofNat, Rat.cast, etc.). We need a pattern matcher that digs out the actual number.

                Attempt to parse a Lean Expr as a constant rational number. Handles various encodings: Nat literals, Int literals, OfNat, Neg, etc.

                Try to match a numeric expression.

                Main Reification Loop #

                The recursive function that traverses the Lean expression tree and builds the corresponding LeanCert AST.

                Main recursive translation from Lean.Expr to LeanCert.Core.Expr (as Lean.Expr).

                Logic:

                1. Check if it's a variable in our context
                2. Check if it's a constant number
                3. Check if it's a known arithmetic operator (+, *, -, /)
                4. Check if it's a known transcendental (sin, cos, exp, log, etc.)
                5. Fail if unrecognized

                Try to match the expression against known patterns.

                Build a power expression using repeated multiplication. pow(base, k) = base * base * ... * base (k times) or 1 if k = 0.

                Entry Point #

                The main entry point that strips the leading lambda and initializes the context.

                Entry point: Takes a lambda fun x y ... => body and returns the LeanCert AST.

                This function uses lambdaTelescope to introduce the lambda-bound variables as free variables, then translates the body with those variables tracked in the context.

                Equations
                Instances For

                  Testing Infrastructure #

                  Elaborate a term and reify it to a LeanCert expression. Useful for debugging the reification process.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For