Skip to content

feat: add structured TraceResult to TraceData - #12698

Merged
kim-em merged 9 commits into
leanprover:masterfrom
kim-em:kim/trace-result-field
Mar 10, 2026
Merged

kim-em merged 9 commits into
leanprover:masterfrom
kim-em:kim/trace-result-field

Conversation

@kim-em

@kim-em kim-em commented Feb 26, 2026 •

Copy link
Copy Markdown
Collaborator

This PR adds a result? : Option TraceResult field to TraceData and populates it in withTraceNode and withTraceNodeBefore, so that metaprograms walking trace trees can determine success/failure structurally instead of string-matching on emoji.

TraceResult has three cases: .success (checkEmoji), .failure (crossEmoji), and .error (bombEmoji, exception thrown). An ExceptToTraceResult typeclass converts Except results to TraceResult directly, with instances for Bool and Option. TraceResult.toEmoji converts back to emoji for display. This replaces the previous ExceptToEmoji typeclass — TraceResult is now the primary representation rather than being derived from emoji strings.

withTraceNodeBefore (used by isDefEq) uses ExceptToTraceResult.toTraceResult directly, correctly handling Bool (.ok false = failure) and Option (.ok none = failure), with Except.error mapping to .error.

For withTraceNode, result? defaults to none. Callers can pass mkResult? to provide structured results; when set, the corresponding emoji is auto-prepended to the message.

Motivated by mathlib's #defeq_abuse diagnostic tactic (leanprover-community/mathlib4#35750) which currently string-matches on emoji to determine trace node outcomes. See https://leanprover.zulipchat.com/#narrow/channel/113488-general/topic/backward.2EisDefEq.2ErespectTransparency

🤖 Prepared with Claude Code

Comment thread src/Lean/Message.lean Resolved
Comment thread src/Lean/Util/Trace.lean Resolved
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Feb 26, 2026
@mathlib-lean-pr-testing

mathlib-lean-pr-testing Bot commented Feb 26, 2026 •

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d4b560ec4ac4431e5439565f91e73eec1ce12c37 --onto c595413fccaa47ebdc399d9b817c17425687fc26. You can force Mathlib CI using the force-mathlib-ci label. (2026-02-26 03:07:24)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d4b560ec4ac4431e5439565f91e73eec1ce12c37 --onto 87ec768a509c45987a7d73258da0cc1ef047925c. You can force Mathlib CI using the force-mathlib-ci label. (2026-03-03 07:12:40)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d4b560ec4ac4431e5439565f91e73eec1ce12c37 --onto 333ab1c6f0ce11f33551d6a736054cb72812cad9. You can force Mathlib CI using the force-mathlib-ci label. (2026-03-06 01:16:19)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 71ff36621103dfab119dfbead39dea3995d5832d --onto e8048291010c815d0d30926924e7ad7afc18b1c0. You can force Mathlib CI using the force-mathlib-ci label. (2026-03-10 02:59:04)

@leanprover-bot

leanprover-bot commented Feb 26, 2026 •

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase d4b560ec4ac4431e5439565f91e73eec1ce12c37 --onto 8038a8b8904f89ad9542c8eda11379f8f006eab1. You can force reference manual CI using the force-manual-ci label. (2026-02-26 03:07:26)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase d4b560ec4ac4431e5439565f91e73eec1ce12c37 --onto cda84702e9b31165f1f83c657b532f36f34e0bd0. You can force reference manual CI using the force-manual-ci label. (2026-03-03 07:12:41)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 71ff36621103dfab119dfbead39dea3995d5832d --onto a165292462a973c20d3cc8c8b23a3ac2334a2a4a. You can force reference manual CI using the force-manual-ci label. (2026-03-10 02:59:06)

mathlib-bors Bot pushed a commit to leanprover-community/mathlib4 that referenced this pull request Mar 1, 2026
This PR adds `#defeq_abuse in`, a tactic/command combinator for diagnosing `backward.isDefEq.respectTransparency` failures. It runs the given tactic or command with the option both `true` and `false`, and when the strict setting fails, walks the trace tree to identify the specific `isDefEq` checks that are root causes.

Reusable trace-tree analysis utilities are extracted into `Mathlib.Lean.MessageData.Trace`:
- `TraceResult` / `traceResultOf` — structured access to the ✅/❌/💥 status encoding
- `foldTraceNodes` — generic fold over `MessageData` trace trees
- `stripTraceResultPrefix` / `dedupByString` — helpers for trace content comparison

Companion lean4 PRs that will simplify this further:
- leanprover/lean4#12698 — structured `TraceResult` field on `TraceData`
- leanprover/lean4#12699 — `Meta.synthInstance.apply` trace class

See https://leanprover.zulipchat.com/#narrow/channel/113488-general/topic/backward.2EisDefEq.2ErespectTransparency

🤖 Prepared with Claude Code

Co-authored-by: Thomas R. Murrills <[email protected]>
@kim-em
kim-em marked this pull request as ready for review March 2, 2026 02:08
@kim-em kim-em added the changelog-language Language features and metaprograms label Mar 2, 2026
@kim-em
kim-em requested a review from eric-wieser March 2, 2026 03:34
pfaffelh pushed a commit to pfaffelh/mathlib4 that referenced this pull request Mar 2, 2026
…ity#35750)

This PR adds `#defeq_abuse in`, a tactic/command combinator for diagnosing `backward.isDefEq.respectTransparency` failures. It runs the given tactic or command with the option both `true` and `false`, and when the strict setting fails, walks the trace tree to identify the specific `isDefEq` checks that are root causes.

Reusable trace-tree analysis utilities are extracted into `Mathlib.Lean.MessageData.Trace`:
- `TraceResult` / `traceResultOf` — structured access to the ✅/❌/💥 status encoding
- `foldTraceNodes` — generic fold over `MessageData` trace trees
- `stripTraceResultPrefix` / `dedupByString` — helpers for trace content comparison

Companion lean4 PRs that will simplify this further:
- leanprover/lean4#12698 — structured `TraceResult` field on `TraceData`
- leanprover/lean4#12699 — `Meta.synthInstance.apply` trace class

See https://leanprover.zulipchat.com/#narrow/channel/113488-general/topic/backward.2EisDefEq.2ErespectTransparency

🤖 Prepared with Claude Code

Co-authored-by: Thomas R. Murrills <[email protected]>
@kim-em
kim-em requested a review from thorimur March 4, 2026 00:01

@thorimur thorimur left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me after these changes! :) I'd of course be happy to give the one or two declarations of API I'm asking for a quick look, but I expect they'll be simple.

Comment thread src/Lean/Message.lean Resolved
Comment thread src/Lean/Util/Trace.lean Resolved
Comment thread src/Lean/Util/Trace.lean Resolved
Comment thread src/Lean/Util/Trace.lean Resolved
Comment thread src/Lean/Message.lean Resolved
Comment thread src/Lean/Util/Trace.lean Resolved
Comment thread src/Lean/Util/Trace.lean Resolved
@kim-em

kim-em commented Mar 5, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @thorimur, very helpful comments. I've made the changes.

@kim-em
kim-em requested a review from thorimur March 5, 2026 10:21
@thorimur

thorimur commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

@kim-em Great! (Though I'm guessing they're stuck on the other fork as before? :) )

@kim-em

kim-em commented Mar 6, 2026

Copy link
Copy Markdown
Collaborator Author

@kim-em Great! (Though I'm guessing they're stuck on the other fork as before? :) )

Thanks for your patience... It's a bug in my worktree creation script, sometimes not setting tracking branches for PRs correctly, and then Claude being "helpful". I think I have my script right now, hopefully this won't keep happening.

Commits now in the right place.

@thorimur thorimur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! (Minor cleanups suggested, one namespace fix)

I'm not 100% sure why Lean.SubExpr is changed in this PR (hmm, it has something to do with Except?) but it does also look good to me.

Comment thread src/Lean/Util/Trace.lean Resolved
Comment thread src/Lean/Util/Trace.lean Resolved
Comment thread src/Lean/Util/Trace.lean Resolved
kim-em and others added 9 commits March 10, 2026 01:39
Adds a `result? : Option TraceResult` field to `TraceData`, so that
`withTraceNode` and `withTraceNodeBefore` record structured
success/failure information alongside the existing emoji in the
rendered header.

This allows metaprograms that walk trace trees to determine success or
failure by checking `td.result?` instead of string-matching on emoji
characters in the rendered `MessageData`.

For `withTraceNodeBefore`, `result?` is derived from the emoji
computed by `ExceptToEmoji`, correctly handling `Bool` (where
`.ok false` = failure) and `Option` (where `.ok none` = failure).

For `withTraceNode`, `result?` defaults to `.ok _ => success`,
`.error _ => failure`. Callers needing different semantics can pass
`result?` explicitly.

Trace nodes not created by `withTraceNode`/`withTraceNodeBefore`
(e.g. from `addTrace` or diagnostic formatting) get `result? := none`.

Motivated by mathlib's `#defeq_abuse` diagnostic tactic
(mathlib4#35750) which currently has to string-match on emoji
to determine trace node outcomes.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
`withTraceNode` doesn't have access to `ExceptToEmoji`, so it can't
distinguish `Except.ok none` (failure) from `Except.ok (some _)`
(success). Defaulting to `none` is the honest choice; callers that
need structured results can pass `result?` explicitly.

`withTraceNodeBefore` is unaffected — it correctly derives from the
emoji string via `ExceptToEmoji`.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…raceNodeBefore

Both `withTraceNode` and `withTraceNodeBefore` now take a
`mkResult? : Except ε α → Option TraceResult` callback instead of a
fixed `result? : Option TraceResult`. This lets callers compute the
structured result from the actual `Except` outcome.

- `withTraceNode`: defaults to `fun _ => none` (callers opt in)
- `withTraceNodeBefore`: defaults to inferring from `ExceptToEmoji`
  (success if emoji == checkEmoji, failure otherwise)

This addresses the semantic mismatch where `Except.ok none` or
`Except.ok false` would need to be reported as failure, not success.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
The bomb emoji (💥) indicates an exception was thrown, which is
semantically different from a normal failure (❌). Lumping them
together as `.failure` loses this distinction.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
This replaces the `ExceptToEmoji` typeclass with `ExceptToTraceResult`, making
`TraceResult` the primary representation. `withTraceNodeBefore` now computes a
`TraceResult` directly via the typeclass instead of deriving it from emoji string
comparison. Adds `TraceResult.toEmoji` to convert back to emoji for display.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add `Except.toTraceResult`, `withTraceNode` auto-prepends emoji when
`mkResult?` yields `some`, remove redundant `mkResult` param from
`withTraceNodeBefore`, improve docstrings and examples.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Since withTraceNode now auto-prepends emoji via ExceptToTraceResult,
remove all manual emoji embedding from ~31 callers across 25 source
files. Remove the now-unused exceptEmoji, exceptBoolEmoji,
exceptOptionEmoji helpers and custom emoji functions in AppBuilder and
LibrarySearch. Update all test expected outputs to match.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@kim-em
kim-em force-pushed the kim/trace-result-field branch from a04cf13 to 6cf80c5 Compare March 10, 2026 02:13
@kim-em
kim-em added this pull request to the merge queue Mar 10, 2026
Merged via the queue into leanprover:master with commit e01cbf2 Mar 10, 2026
15 checks passed
xroblot pushed a commit to xroblot/mathlib4 that referenced this pull request Mar 10, 2026
…ity#35750)

This PR adds `#defeq_abuse in`, a tactic/command combinator for diagnosing `backward.isDefEq.respectTransparency` failures. It runs the given tactic or command with the option both `true` and `false`, and when the strict setting fails, walks the trace tree to identify the specific `isDefEq` checks that are root causes.

Reusable trace-tree analysis utilities are extracted into `Mathlib.Lean.MessageData.Trace`:
- `TraceResult` / `traceResultOf` — structured access to the ✅/❌/💥 status encoding
- `foldTraceNodes` — generic fold over `MessageData` trace trees
- `stripTraceResultPrefix` / `dedupByString` — helpers for trace content comparison

Companion lean4 PRs that will simplify this further:
- leanprover/lean4#12698 — structured `TraceResult` field on `TraceData`
- leanprover/lean4#12699 — `Meta.synthInstance.apply` trace class

See https://leanprover.zulipchat.com/#narrow/channel/113488-general/topic/backward.2EisDefEq.2ErespectTransparency

🤖 Prepared with Claude Code

Co-authored-by: Thomas R. Murrills <[email protected]>
kim-em added a commit that referenced this pull request Mar 16, 2026
This PR adds a `result? : Option TraceResult` field to `TraceData` and
populates it in `withTraceNode` and `withTraceNodeBefore`, so that
metaprograms walking trace trees can determine success/failure
structurally instead of string-matching on emoji.

`TraceResult` has three cases: `.success` (checkEmoji), `.failure`
(crossEmoji), and `.error` (bombEmoji, exception thrown). An
`ExceptToTraceResult` typeclass converts `Except` results to
`TraceResult` directly, with instances for `Bool` and `Option`.
`TraceResult.toEmoji` converts back to emoji for display. This replaces
the previous `ExceptToEmoji` typeclass — `TraceResult` is now the
primary representation rather than being derived from emoji strings.

`withTraceNodeBefore` (used by `isDefEq`) uses
`ExceptToTraceResult.toTraceResult` directly, correctly handling `Bool`
(`.ok false` = failure) and `Option` (`.ok none` = failure), with
`Except.error` mapping to `.error`.

For `withTraceNode`, `result?` defaults to `none`. Callers can pass
`mkResult?` to provide structured results; when set, the corresponding
emoji is auto-prepended to the message.

Motivated by mathlib's `#defeq_abuse` diagnostic tactic
(leanprover-community/mathlib4#35750) which
currently string-matches on emoji to determine trace node outcomes. See
https://leanprover.zulipchat.com/#narrow/channel/113488-general/topic/backward.2EisDefEq.2ErespectTransparency

🤖 Prepared with Claude Code

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
pevogam pushed a commit to pevogam/mathlib4 that referenced this pull request Mar 19, 2026
…ity#35750)

This PR adds `#defeq_abuse in`, a tactic/command combinator for diagnosing `backward.isDefEq.respectTransparency` failures. It runs the given tactic or command with the option both `true` and `false`, and when the strict setting fails, walks the trace tree to identify the specific `isDefEq` checks that are root causes.

Reusable trace-tree analysis utilities are extracted into `Mathlib.Lean.MessageData.Trace`:
- `TraceResult` / `traceResultOf` — structured access to the ✅/❌/💥 status encoding
- `foldTraceNodes` — generic fold over `MessageData` trace trees
- `stripTraceResultPrefix` / `dedupByString` — helpers for trace content comparison

Companion lean4 PRs that will simplify this further:
- leanprover/lean4#12698 — structured `TraceResult` field on `TraceData`
- leanprover/lean4#12699 — `Meta.synthInstance.apply` trace class

See https://leanprover.zulipchat.com/#narrow/channel/113488-general/topic/backward.2EisDefEq.2ErespectTransparency

🤖 Prepared with Claude Code

Co-authored-by: Thomas R. Murrills <[email protected]>
kim-em added a commit that referenced this pull request Mar 23, 2026
withTraceNode and withTraceNodeBefore previously baked TraceResult.toEmoji
into the header MessageData. Now that TraceData.result? exists (#12698),
the emoji is redundant in the stored header. Move it to the rendering
layer (formatAux, InteractiveDiagnostic) so programmatic consumers get
clean headers without string manipulation.

Rendered output is unchanged.

Closes #13069

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
kim-em added a commit that referenced this pull request Jun 11, 2026
withTraceNode and withTraceNodeBefore previously baked TraceResult.toEmoji
into the header MessageData. Now that TraceData.result? exists (#12698),
the emoji is redundant in the stored header. Move it to the rendering
layer (formatAux, InteractiveDiagnostic) so programmatic consumers get
clean headers without string manipulation.

Rendered output is unchanged.

Closes #13069

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
kim-em pushed a commit that referenced this pull request Aug 31, 2026
`exceptBoolEmoji` was removed when `TraceResult` replaced `ExceptToEmoji`
(#12698); `withTraceNode` now prefixes the emoji itself, so the header
message just drops it. `DiscrTree.values` moved to `Lean.Meta.DiscrTree.Util`
in the `DiscrTree` refactor (#11875), which `Lean.Meta.Tactic.Simp.SimpTheorems`
no longer pulls in, so import it directly.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01WGQ4cXuC9bBbZQ3psYLZUA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-language Language features and metaprograms toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants