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)
theorem LeanCert.Meta.max_eq_half_add_abs_sub (a b : ℝ) :
max a b = (a + b + |b + -a|) / 2

Closed-form rewrite for max on reals, used for reification via existing Expr nodes.

theorem LeanCert.Meta.min_eq_half_sub_abs_sub (a b : ℝ) :
min a b = (a + b - |b + -a|) / 2

Closed-form rewrite for min on reals, used for reification via existing Expr nodes.

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

    Mutable bookkeeping for one reification pass.

    • unfolded : Array Lean.Name

      Definitions delta-reduced while exposing supported arithmetic.

    • remainingUnfoldSteps : ℕ

      Remaining delta-reduction steps. This prevents recursive wrappers from making reification diverge.

    Instances For

      The result of reification together with information needed to bridge the reflected expression back to the user's original syntax.

      Instances For
        @[reducible, inline]

        The translation monad: a read-only variable context plus bounded reification bookkeeping over MetaM.

        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
              @[instance_reducible]

              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

                    Build subtraction through the named Expr.sub wrapper.

                    Expr.sub is definitionally implemented by addition and negation, so the executable evaluator is unchanged. Preserving the wrapper until bridge normalization lets Expr.eval_sub recover the user's lhs - rhs syntax and is essential for general root goals lhs = rhs.

                    Equations
                    Instances For

                      Build absolute value as sqrt (e * e).

                      Equations
                      Instances For

                        Build max(a,b) via (a + b + |b - a|) / 2 in existing Expr constructors.

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

                          Build min(a,b) via (a + b - |b - a|) / 2 in existing Expr constructors.

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

                            Constant Extraction #

                            Numeric parsing is shared in LeanCert.Meta.Numeral.toRat?.

                            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. Try to match a known operator/function (+, *, -, /, sin, cos, exp, etc.)
                            3. Check if it's a numeric constant (ℕ, ℤ, ℚ literals and casts)
                            4. Reduce with whnf and retry
                            5. Unfold definitions and retry

                            Important: Step 2 (operator matching) must come before step 3 (numeric constant). Otherwise toRat? eagerly constant-folds compound expressions like ↑(-2 : ℤ) + 3 into const(1), losing the syntactic structure needed by the bridge converter. With operators first, this reifies as add(const(-2), const(3)) which the bridge can match against the goal.

                            Try to match the expression against known patterns.

                            Unary head-symbol handlers.

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

                              Binary head-symbol handlers.

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

                                Table for unary operations.

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

                                  Last argument of an application (if present).

                                  Equations
                                  Instances For

                                    Last two arguments of an application (if present).

                                    Equations
                                    Instances For

                                      Rich unsupported-expression diagnostics (head symbol + arity).

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

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

                                        Build base ^ z for integer z using Expr.pow + Expr.inv.

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

                                          Build base ^ (n/2) for integer numerator n (half-integers).

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

                                            Try to decompose e as max a b using definitional equality. This is robust against internal projection names changing across Mathlib versions.

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

                                              Try to decompose e as min a b using definitional equality.

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

                                                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
                                                • One or more equations did not get rendered due to their size.
                                                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