TorchLean API

NN.Verification.ODE.Parse

Parse #

Hand-rolled parser for ODE RHS expressions.

Grammar (informal): expr := term (('+' | '-') term)* term := factor (('' | '/') factor) factor := unary ('^' nat)? unary := '-' unary | primary primary:= number | 't' | 'u' | ident '(' expr ')' | ident | '(' expr ')'

Supported unary functions: sin, cos, exp, log. Supported identifiers: pi.

Exponentiation is expanded into repeated multiplication when ^ n is given.

This parser is part of the executable ODE verifier. We keep the grammar direct and hand-written so that the accepted certificate syntax is visible in one file, with predictable errors and no hidden parser-combinator behavior.

The output AST is NN.Verification.ODE.Ast.Expr.

Parser state and low-level helpers #

Parser state for scanning a String by byte-position.

We track the source string s and the current raw byte index i.

Instances For
    @[inline]

    Peek at the current character, if any, without advancing.

    Instances For
      @[inline]

      Advance the current position by one character.

      Instances For
        @[inline]

        A fuel budget derived from the remaining input length (used to guarantee termination).

        Instances For

          Internal: ASCII whitespace predicate used by skipWs.

          Instances For

            Fuel-bounded whitespace skipping (implementation of skipWs).

            Instances For

              Skip ASCII whitespace (' ', '\t', '\n').

              Instances For

                Internal: fuel-bounded implementation of takeWhile.

                Instances For

                  Consume consecutive characters satisfying p, accumulating them into acc.

                  Returns the consumed text and the updated parser state.

                  Instances For

                    Parse a signed decimal number without exponent, e.g. -12.34.

                    Instances For

                      Parse a natural number (decimal digits) used for exponents ^ n.

                      Instances For

                        Parse an identifier consisting of letters/digits/underscore.

                        Instances For

                          Built-in constants and unary functions #

                          Internal: interpret built-in constants like pi / π.

                          Instances For

                            Interpret supported unary function names (e.g. sin, cos, exp, log).

                            Instances For

                              Recursive descent: expression/term/factor/unary/primary #

                              Internal: parse an expr (addition/subtraction chain), with an explicit fuel budget.

                              This is a plain def (not private) because the module is in a public section for export/doc tooling, and Lean 4.29 disallows referring to private declarations from public ones.

                              Instances For

                                Parse a term (multiplication/division chain), with an explicit fuel budget.

                                Instances For

                                  Parse a factor (unary with optional exponentiation), with an explicit fuel budget.

                                  Instances For

                                    Parse a unary (leading negations), with an explicit fuel budget.

                                    Instances For

                                      Parse a primary atom (number/variable/function-call/parentheses), with an explicit fuel budget.

                                      Instances For

                                        Parse an ODE RHS expression string into an AST.

                                        This is the user-facing entrypoint for the ODE verifier: it parses a string like "sin(t) + u^2" into an Expr (NN.Verification.ODE.Ast.Expr).

                                        Instances For