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 (https://github.com/leanprover-community/mathlib4/pull/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 <noreply@anthropic.com>
93 lines
2.1 KiB
Text
93 lines
2.1 KiB
Text
import Lean.CoreM
|
|
|
|
open Lean
|
|
|
|
structure MyState where
|
|
(trace_state : TraceState := {})
|
|
(s : Nat := 0)
|
|
|
|
abbrev M := CoreM
|
|
|
|
def tst1 : M Unit :=
|
|
do trace[module] (m!"hello" ++ MessageData.nest 9 (m!"\n" ++ "world"));
|
|
trace[module.aux] "another message";
|
|
pure ()
|
|
|
|
def tst2 (b : Bool) : M Unit :=
|
|
withTraceNode `module (fun _ => return "message") do
|
|
tst1;
|
|
trace[bughunt] "at test2";
|
|
if b then throwError "error";
|
|
tst1;
|
|
pure ()
|
|
|
|
partial def ack : Nat → Nat → Nat
|
|
| 0, n => n+1
|
|
| m+1, 0 => ack m 1
|
|
| m+1, n+1 => ack m (ack (m+1) n)
|
|
|
|
def slow (b : Bool) : Nat :=
|
|
ack 4 (cond b 0 1)
|
|
|
|
def tst3 (b : Bool) : M Unit :=
|
|
do withTraceNode `module.slow (fun _ => return m!"slow: {slow b}") do {
|
|
tst2 b;
|
|
tst1
|
|
};
|
|
trace[bughunt] "at end of tst3";
|
|
-- Messages are computed lazily. The following message will only be computed
|
|
-- if `trace.slow is active.
|
|
trace[slow] (m!"slow message: " ++ toString (slow b))
|
|
-- This is true even if it is a monad computation:
|
|
trace[slow] (m!"slow message: " ++ (← pure (toString (slow b))))
|
|
|
|
def run (x : M Unit) : M Unit :=
|
|
withReader
|
|
(fun ctx =>
|
|
-- Try commenting/uncommeting the following `setBool`s
|
|
let opts := ctx.options;
|
|
let opts := opts.set `trace.module true;
|
|
-- let opts := opts.set `trace.module.aux false;
|
|
let opts := opts.set `trace.bughunt true;
|
|
-- let opts := opts.set `trace.slow true;
|
|
{ ctx with options := opts })
|
|
(tryCatch (tryFinally x printTraces) (fun _ => IO.println "ERROR"))
|
|
|
|
/--
|
|
info: [module] 💥️ message
|
|
[module] hello
|
|
world
|
|
[bughunt] at test2
|
|
ERROR
|
|
---
|
|
trace: [module] 💥️ message
|
|
[module] hello
|
|
world
|
|
[bughunt] at test2
|
|
-/
|
|
#guard_msgs in
|
|
#eval run (tst3 true)
|
|
|
|
/--
|
|
info: [module] ✅️ message
|
|
[module] hello
|
|
world
|
|
[bughunt] at test2
|
|
[module] hello
|
|
world
|
|
[module] hello
|
|
world
|
|
[bughunt] at end of tst3
|
|
---
|
|
trace: [module] ✅️ message
|
|
[module] hello
|
|
world
|
|
[bughunt] at test2
|
|
[module] hello
|
|
world
|
|
[module] hello
|
|
world
|
|
[bughunt] at end of tst3
|
|
-/
|
|
#guard_msgs in
|
|
#eval run (tst3 false)
|