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

                Build subtraction e1 - e2 as e1 + (-e2) in Expr AST.

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

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