1use std::fmt::Write as _;
5
6use dive_value::{Ctx, print};
7
8use crate::result::{CheckResult, TraceState, Verdict};
9
10#[must_use]
14pub fn format_trace_state(r: &CheckResult, s: &TraceState, ctx: Ctx) -> String {
15 if let Some(text) = &s.text {
16 return text.clone();
17 }
18 let vars: Vec<(&str, &dive_value::Value)> = r
19 .tlc_var_order
20 .iter()
21 .filter_map(|&i| Some((r.var_names.get(i)?.as_str(), s.values.get(i)?)))
22 .collect();
23 print::format_state(&vars, ctx).unwrap_or_else(|_| {
24 let mut out = String::new();
25 for (n, v) in &vars {
26 let _ = writeln!(out, "/\\ {n} = {}", print::to_tlc_string_lenient(v, ctx));
27 }
28 out
29 })
30}
31
32fn headline(v: &Verdict) -> String {
34 match v {
35 Verdict::Success => "Model checking completed. No error has been found.".to_owned(),
36 Verdict::InvariantViolated {
37 property,
38 initial: true,
39 } => format!("Error: Invariant {property} is violated by the initial state:"),
40 Verdict::InvariantViolated { property, .. } => {
41 format!("Error: Invariant {property} is violated.")
42 }
43 Verdict::PropertyViolatedInitially { property } => {
44 format!("Error: Property {property} is violated by the initial state:")
45 }
46 Verdict::ActionPropertyViolated { property } => {
47 format!("Error: Action property {property} is violated.")
48 }
49 Verdict::Deadlock => "Error: Deadlock reached.".to_owned(),
50 Verdict::AssumptionFalse { message, .. } | Verdict::ModelError { message, .. } => {
51 format!("Error: {message}")
52 }
53 Verdict::PostconditionFalse { message, .. }
54 | Verdict::NotCompletelySpecified { message, .. }
55 | Verdict::EvaluationError { message, .. } => format!("Error: {message}"),
56 Verdict::Unsupported { feature } => {
57 format!("unsupported: {feature}; no verdict produced")
58 }
59 Verdict::ResourceLimit { message } => {
60 format!("resource limit: {message}; no verdict produced")
61 }
62 Verdict::Stopped(stopped) => {
63 format!("{stopped}; no verdict produced ({})", stopped.hint())
64 }
65 Verdict::BudgetMeasurement { error } => format!("{error}; no verdict produced"),
66 Verdict::InternalError { message } => {
67 format!("internal error: {message}; no verdict produced (please report this)")
68 }
69 }
70}
71
72#[must_use]
74pub fn render_human(r: &CheckResult, ctx: Ctx) -> String {
75 let mut out = String::new();
76 let _ = writeln!(out, "{}", headline(&r.verdict));
77 if let Some(cx) = &r.counterexample {
78 let initial_only = matches!(
79 r.verdict,
80 Verdict::InvariantViolated { initial: true, .. }
81 | Verdict::PropertyViolatedInitially { .. }
82 );
83 if initial_only {
84 for s in &cx.states {
85 let _ = write!(out, "{}", format_trace_state(r, s, ctx));
86 }
87 } else {
88 let _ = writeln!(out, "Error: The behavior up to this point is:");
89 for (i, s) in cx.states.iter().enumerate() {
90 let _ = writeln!(out, "State {}: {}", i + 1, s.label);
91 let _ = writeln!(out, "{}", format_trace_state(r, s, ctx));
92 }
93 }
94 }
95 if r.verdict == Verdict::Success
96 && let Some(c) = &r.collision
97 {
98 let _ = writeln!(
99 out,
100 "The probability of a fingerprint collision is {} (calculated optimistically){}.",
101 c.optimistic,
102 c.actual.as_ref().map_or_else(String::new, |a| format!(
103 "; {a} based on the actual fingerprints"
104 ))
105 );
106 }
107 if let Some(s) = &r.stats
108 && r.tlc_prints_stats
109 {
110 let _ = writeln!(
111 out,
112 "{} states generated, {} distinct states found, {} states left on queue.",
113 s.generated, s.distinct, s.left_on_queue
114 );
115 let _ = writeln!(
116 out,
117 "The depth of the complete state graph search is {}.",
118 s.depth
119 );
120 }
121 if let Some(seen) = &r.seen {
122 let cleanup = seen
123 .after_cleanup
124 .is_some_and(|stats| stats.cleanup_completed);
125 let _ = writeln!(
126 out,
127 "Disk seen-state retention: {} admitted keys; actual inline worker {}; explicit cleanup completed: {cleanup}. This is not a total-memory budget.",
128 seen.admitted_keys, r.workers.used
129 );
130 if let Some(failure) = &seen.first_failure {
131 let _ = writeln!(
132 out,
133 "Seen storage {:?} failure: {}",
134 failure.phase, failure.error
135 );
136 }
137 }
138 for n in &r.notes {
139 let _ = writeln!(out, "note: {n}");
140 }
141 out
142}