Documentation

Lean.PostprocessTraces.Basic

Experimental: Trace Postprocessors and the postprocess_traces Command #

Trace messages of complex elaboration tasks can be very large, and finding the relevant part in the editor requires a lot of clicking and searching. This module provides trace postprocessors: functions that transform the trace of a command before it is reported, e.g. by filtering out irrelevant subtrees, hoisting the interesting ones, or pre-expanding the paths to matches.

A trace postprocessor (Lean.PostprocessTraces.TracePostprocessor) receives the array of trace roots of one trace message and returns the transformed roots. The Lean.PostprocessTraces namespace provides a small set of operations (filterSubtrees, hoist, exposeSubtrees, countNodes, selfTime) that compose left-to-right with >=>. The selecting operations take a pattern (Lean.PostprocessTraces.TracePattern), a predicate on trace subtrees; built-in patterns select by trace class (ofClass), text (containsString), result (succeeded, failed, errored, unsuccessful), and time (minTimeMs, minSelfTimeMs). Users can define their own postprocessors and patterns as ordinary functions.

Syntax: postprocess_traces post in cmd transforms the trace messages produced by cmd with post.

Traces are stored as MessageData (see MessageData.trace); TraceTree is a structured view of such messages that takes care of the context wrappers (MessageData.withContext etc.) around trace nodes.

Experimental. postprocess_traces and the library around it are expected to change in the future.

postprocess_traces post in cmd runs cmd and transforms every trace message it produces with the trace postprocessor post : Lean.PostprocessTraces.TracePostprocessor before it is reported.

The postprocessor receives the array of trace roots of each trace message and returns the transformed roots; returning an empty array drops the message entirely. The Lean.PostprocessTraces namespace (automatically opened in post) provides operations such as filterSubtrees, hoist, exposeSubtrees, and selfTime, which take patterns such as ofClass, containsString, and minTimeMs and compose left-to-right with >=>. User-defined postprocessors and patterns are ordinary functions.

For example, the following only shows the instance-synthesis steps that mention tryResolve, together with their ancestors:

set_option trace.Meta.synthInstance true in
postprocess_traces filter (containsString "tryResolve") in
example : Inhabited (List Nat) := inferInstance
Equations
  • One or more equations did not get rendered due to their size.
Instances For

    A structured view of a trace message (MessageData.trace), used by trace postprocessors (see TracePostprocessor).

    Instances For

      Reassembles the MessageData of a trace tree.

      @[reducible, inline]

      A trace postprocessor transforms the trace roots of a trace message before it is reported, e.g. by filtering out irrelevant subtrees or pre-expanding interesting nodes. Returning an empty array drops the trace message entirely.

      Traces are reported as one message per source range inside a command, and a postprocessor is applied to each of these messages separately; it therefore cannot move trace roots from one source range to another.

      Postprocessors are applied by the postprocess_traces post in cmd command and can be composed left-to-right with >=>.

      Equations
      Instances For
        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          @[reducible, inline]

          A pattern selects the trace subtrees that an operation acts on (see filter, hoist, and expand). Patterns are ordinary predicates: the built-in ones (such as containsString, unsuccessful, or minTimeMs) can be combined with custom conditions in a fun.

          Equations
          Instances For

            The TraceData of a trace node; none for leaf messages.

            Equations
            Instances For

              The trace class of a trace node; none for leaf messages.

              Equations
              Instances For

                Replaces the children of a trace node. Leaf messages are returned unchanged.

                Equations
                Instances For

                  Transforms the TraceData of a trace node. Leaf messages are returned unchanged.

                  Equations
                  Instances For

                    Elapsed time of this node in seconds; 0 if no profiling data is available.

                    Equations
                    Instances For

                      Elapsed time of this node that is not accounted for by its children, in seconds; 0 if no profiling data is available.

                      Equations
                      Instances For

                        The message of this node (without its children), formatted as a string. Useful for text-based filters but expensive.

                        Equations
                        Instances For

                          The TraceResult of a trace node; none for leaf messages and nodes without a result.

                          Equations
                          Instances For

                            Collects all maximal subtrees satisfying p in acc: adds t itself if p t holds, and otherwise recurses into the children. Matching subtrees are not searched for nested matches.

                            Prunes the tree to the subtrees satisfying p, keeping their ancestors for context; none if there is no match. The resulting tree consists of those nodes that either have a matching ancestor or transitive child. Matching subtrees are not searched for nested matches.

                            Matches the trace nodes with the exact trace class cls.

                            Equations
                            Instances For

                              Matches the subtrees whose trace class or head message contains pat as a substring. For large traces, this is an expensive pattern because all head messages need to be pretty-printed; to select nodes by their exact trace class, prefer the much cheaper ofClass.

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

                                Matches the trace nodes whose action succeeded (✅️, TraceResult.success). Nodes without a recorded result (e.g. from addTrace) do not match.

                                Equations
                                Instances For

                                  Matches the trace nodes whose action failed (❌️, TraceResult.failure).

                                  Equations
                                  Instances For

                                    Matches the trace nodes whose action threw an exception (💥️, TraceResult.error).

                                    Equations
                                    Instances For

                                      Matches the trace nodes whose action did not succeed, i.e. failed (❌️) or threw an exception (💥️). Nodes without a recorded result (e.g. from addTrace) do not match.

                                      Equations
                                      Instances For

                                        Matches the subtrees whose action took at least ms milliseconds. Timing information is only available with set_option trace.profiler true.

                                        Equations
                                        Instances For

                                          Matches the subtrees whose action took at least ms milliseconds outside of their child nodes. Timing information is only available with set_option trace.profiler true.

                                          Equations
                                          Instances For

                                            Keeps only the subtrees matching p, together with their ancestors for context; all other nodes are removed. Matching subtrees are kept in their entirety and not searched for nested matches (see TraceTree.filterSubtrees).

                                            Equations
                                            Instances For

                                              Hoists the subtrees matching p to the top level, so that every new trace root is a match; ancestors and unrelated subtrees are discarded. Matches nested inside other matches are not searched for.

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

                                                Expands all transitive ancestors of the subtrees matching p in the editor, so that the trace opens already showing all matches. No nodes are removed, and all other nodes, including the matches themselves, keep their expansion state. Matching subtrees are not searched for nested matches.

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

                                                  Appends the number of nodes inside each subtree to the subtree's head message.

                                                  Equations
                                                  Instances For

                                                    Appends the number of milliseconds spent inside each subtree but outside of its child nodes to the subtree's head message. Timing information is only available with set_option trace.profiler true; nodes without it are not annotated.

                                                    Equations
                                                    Instances For