1use crate::lock::Lock;
7use crate::model::{CrateItems, ModelFn, SpecRef};
8use std::collections::BTreeMap;
9use std::fmt::Write;
10
11#[derive(Debug, Clone, PartialEq)]
12pub struct ObligationState {
13 pub label: String,
14 pub spec: String,
16 pub spec_lean: String,
18 pub status: String,
19 pub axioms: Vec<String>,
20 pub proof: String,
21 pub theorem: String,
23}
24
25#[derive(Debug, Clone, PartialEq)]
26pub struct ModelState {
27 pub rust: String,
28 pub lean: String,
29 pub file: String,
30 pub doc_file: String,
31 pub obligations: Vec<ObligationState>,
32 pub changed: Vec<String>,
34}
35
36pub fn states(
39 items: &CrateItems,
40 lock: &Lock,
41 keys: &BTreeMap<String, String>,
42 crate_ident: &str,
43) -> Vec<ModelState> {
44 items
45 .models
46 .iter()
47 .map(|f| model_state(f, items, lock, keys, crate_ident))
48 .collect()
49}
50
51fn model_state(
52 f: &ModelFn,
53 items: &CrateItems,
54 lock: &Lock,
55 keys: &BTreeMap<String, String>,
56 crate_ident: &str,
57) -> ModelState {
58 let entry = lock.model(&f.rust);
59 let instances: Vec<String> = f
60 .attr
61 .instances
62 .iter()
63 .map(crate::model::type_text)
64 .collect();
65 let changed: Vec<String> = match entry {
66 Some(m) => {
67 let mut c: Vec<String> = m
68 .items
69 .iter()
70 .filter(|i| items.hashes.get(&i.rust) != Some(&i.hash))
71 .map(|i| i.rust.clone())
72 .collect();
73 if m.instances != instances {
74 c.push(format!("instances of {}", f.rust));
75 }
76 c
77 }
78 None => Vec::new(),
79 };
80 let obligations = f
81 .attr
82 .obligation_labels()
83 .into_iter()
84 .map(|(label, spec, _)| {
85 let spec_text = match spec {
86 SpecRef::Path(p) => crate::model::path_string(p),
87 SpecRef::Lean(s) => s.clone(),
88 };
89 let recorded = entry.and_then(|m| m.obligation.iter().find(|o| o.label == label));
90 match (entry, recorded) {
91 (Some(m), Some(o)) => {
92 let spec_changed =
93 !o.spec_block.is_empty() && keys.get(&o.spec_block) != Some(&o.spec_hash);
94 let status = if !changed.is_empty() || spec_changed {
95 "stale".to_string()
96 } else {
97 o.status.clone()
98 };
99 ObligationState {
100 label: label.clone(),
101 spec: spec_text,
102 spec_lean: o.spec.clone(),
103 status,
104 axioms: o.axioms.clone(),
105 proof: o.proof.clone(),
106 theorem: o.theorem(&m.lean),
107 }
108 }
109 _ => ObligationState {
110 label: label.clone(),
111 spec: spec_text,
112 spec_lean: String::new(),
113 status: "unextracted".to_string(),
114 axioms: Vec::new(),
115 proof: String::new(),
116 theorem: String::new(),
117 },
118 }
119 })
120 .collect();
121 ModelState {
122 rust: f.rust.clone(),
123 lean: entry.map(|m| m.lean.clone()).unwrap_or_default(),
124 file: f.file.clone(),
125 doc_file: f.doc_file(crate_ident),
126 obligations,
127 changed,
128 }
129}
130
131pub fn doc_text(m: &ModelState) -> String {
133 let mut s = String::from("\n\n# Verification\n\n");
134 if m.lean.is_empty() {
135 s.push_str("Not extracted: run `cargo leanfran extract`.\n");
136 } else {
137 let _ = writeln!(s, "Lean model: `{}`.\n", m.lean);
138 }
139 if m.obligations.is_empty() {
140 s.push_str("No obligations: the model is for theorems written in Lean.\n");
141 } else {
142 s.push_str("| Obligation | Spec | State | Theorem | Axioms |\n|---|---|---|---|---|\n");
143 for o in &m.obligations {
144 let spec = if o.spec_lean.is_empty() {
145 format!("`{}`", o.spec)
146 } else {
147 format!("`{}`", o.spec_lean)
148 };
149 let theorem = if o.theorem.is_empty() {
150 String::new()
151 } else {
152 format!("`{}`", o.theorem)
153 };
154 let _ = writeln!(
155 s,
156 "| `{}` | {spec} | {} | {theorem} | {} |",
157 o.label,
158 o.status,
159 o.axioms.join(", ")
160 );
161 }
162 }
163 s
164}
165
166pub fn warnings(states: &[ModelState]) -> Vec<String> {
168 let mut out = Vec::new();
169 for m in states {
170 let stale = m.obligations.iter().filter(|o| o.status == "stale").count();
171 let unextracted = m
172 .obligations
173 .iter()
174 .filter(|o| o.status == "unextracted")
175 .count();
176 if m.lean.is_empty() {
177 out.push(format!(
178 "leanfran: {}: not extracted (run `cargo leanfran extract`)",
179 m.rust
180 ));
181 continue;
182 }
183 if stale > 0 {
184 let why = if m.changed.is_empty() {
185 "a spec changed".to_string()
186 } else {
187 format!("{} changed", m.changed.join(", "))
188 };
189 let n = if stale == 1 {
190 "1 obligation is".to_string()
191 } else {
192 format!("{stale} obligations are")
193 };
194 out.push(format!(
195 "leanfran: {}: {n} stale ({why} since `cargo leanfran extract`)",
196 m.rust
197 ));
198 }
199 if unextracted > 0 {
200 out.push(format!(
201 "leanfran: {}: {unextracted} obligations are not extracted (run `cargo leanfran extract`)",
202 m.rust
203 ));
204 }
205 }
206 out
207}
208
209pub fn not_proved(states: &[ModelState]) -> Vec<String> {
211 let mut out = Vec::new();
212 for m in states {
213 for o in &m.obligations {
214 if o.status != "proved" {
215 out.push(format!("{}: {} ({})", m.rust, o.label, o.status));
216 }
217 }
218 }
219 out
220}
221
222pub fn table(states: &[ModelState]) -> String {
224 let mut s = String::new();
225 for m in states {
226 let _ = writeln!(s, "{}", m.rust);
227 if m.obligations.is_empty() {
228 let state = if m.lean.is_empty() {
229 "unextracted"
230 } else {
231 "model only"
232 };
233 let _ = writeln!(s, " {state}");
234 }
235 let w = m
236 .obligations
237 .iter()
238 .map(|o| o.label.len())
239 .max()
240 .unwrap_or(0);
241 for o in &m.obligations {
242 let detail = match o.status.as_str() {
243 "proved" | "axioms" => format!("axioms: {}", o.axioms.join(", ")),
244 "sorry" | "mismatch" | "failed" => o.proof.clone(),
245 "missing" => format!("theorem {} not found", o.theorem),
246 _ => String::new(),
247 };
248 let _ = writeln!(s, " {:w$} {:8} {detail}", o.label, o.status, w = w);
249 }
250 }
251 s
252}
253
254pub fn json(states: &[ModelState]) -> serde_json::Value {
256 serde_json::Value::Array(
257 states
258 .iter()
259 .map(|m| {
260 serde_json::json!({
261 "rust": m.rust,
262 "lean": m.lean,
263 "file": m.file,
264 "changed": m.changed,
265 "obligations": m.obligations.iter().map(|o| serde_json::json!({
266 "label": o.label,
267 "spec": o.spec,
268 "spec_lean": o.spec_lean,
269 "status": o.status,
270 "axioms": o.axioms,
271 "proof": o.proof,
272 "theorem": o.theorem,
273 })).collect::<Vec<_>>(),
274 })
275 })
276 .collect(),
277 )
278}