Skip to main content

rucc_verify/
verify.rs

1//! Turning a rule into a question, and the answers into a report.
2
3use std::fmt;
4
5use rucc_rules::{Error, Rule, Term, TermKind};
6
7use crate::model::{MEMORY_CONST, Model, Sort, Widths, rule_width};
8use crate::solver::{Answer, Solver};
9
10/// What became of one rule.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum Verdict {
13    /// Nothing makes the claim false.
14    Discharged,
15    /// Something does, and this is what the solver printed of it.
16    Refuted(String),
17    /// The claim holds at every width narrower than the rule's own, and the rule carries a
18    /// written reason for taking that as enough. A pass, and a counted one.
19    Bounded {
20        /// The widths it was proved at, narrowest first.
21        widths: Vec<u32>,
22        /// The reason the rule gives, which is what a reviewer signed for.
23        why: String,
24    },
25    /// The solver gave up. Not a pass.
26    Unknown,
27}
28
29impl Verdict {
30    /// Whether a rule with this verdict may enter the rule set.
31    #[must_use]
32    pub fn accepted(&self) -> bool {
33        matches!(self, Verdict::Discharged | Verdict::Bounded { .. })
34    }
35
36    /// Why it may not, as a sentence, or nothing when it may.
37    #[must_use]
38    pub fn refusal(&self) -> Option<String> {
39        match self {
40            Verdict::Discharged | Verdict::Bounded { .. } => None,
41            Verdict::Refuted(model) => {
42                Some(format!("this rule is not true, and here is what makes it false: {model}"))
43            }
44            Verdict::Unknown => Some(
45                "the solver could not settle this rule, and a rule nobody has proved does not \
46                 enter the rule set"
47                    .to_owned(),
48            ),
49        }
50    }
51}
52
53/// What became of a rule set.
54#[derive(Debug, Default, Clone, PartialEq, Eq)]
55pub struct Report {
56    /// One verdict per rule, in the order the rules were given.
57    pub verdicts: Vec<Verdict>,
58}
59
60impl Report {
61    /// How many rules were discharged at their own width.
62    #[must_use]
63    pub fn discharged(&self) -> usize {
64        self.verdicts.iter().filter(|v| **v == Verdict::Discharged).count()
65    }
66
67    /// How many rules got a bounded proof instead.
68    ///
69    /// `spec/15-testing.md` section 15.5 asks for this number to be reported rather than merely
70    /// known, because it going up is the signal that the rule set is drifting towards claims
71    /// nobody is checking at the width the compiler runs at.
72    #[must_use]
73    pub fn bounded(&self) -> usize {
74        self.verdicts.iter().filter(|v| matches!(v, Verdict::Bounded { .. })).count()
75    }
76
77    /// Whether every rule was discharged at its own width. A bounded proof is not one of these.
78    #[must_use]
79    pub fn all_discharged(&self) -> bool {
80        self.verdicts.iter().all(|v| *v == Verdict::Discharged)
81    }
82
83    /// Whether every rule may enter the rule set, which allows a bounded proof and allows
84    /// nothing else. A solver that gave up is not a pass, because "we could not tell" is not
85    /// "it is correct".
86    #[must_use]
87    pub fn accepted(&self) -> bool {
88        self.verdicts.iter().all(Verdict::accepted)
89    }
90}
91
92impl fmt::Display for Report {
93    /// One line, which is what a build prints and what a person reads in a log.
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        let refused = self.verdicts.len() - self.discharged() - self.bounded();
96        let rules = if self.verdicts.len() == 1 { "rule" } else { "rules" };
97        write!(
98            f,
99            "{} {rules}: {} discharged, {} by bounded proof, {} refused",
100            self.verdicts.len(),
101            self.discharged(),
102            self.bounded(),
103            refused
104        )
105    }
106}
107
108/// The SMT-LIB question one rule asks, at the width the rule works in.
109///
110/// This is separate from asking it so that the query can be read, kept in a test, and handed to
111/// a solver by hand when one is arguing with it.
112///
113/// # Errors
114///
115/// Anything the model cannot write out, which is any term nobody has said the meaning of.
116pub fn query(path: &str, rule: &Rule, model: &Model) -> Result<String, Error> {
117    query_at(path, rule, model, rule_width(&rule.pattern))
118}
119
120/// The same question asked at a width somebody chose.
121///
122/// This is what a bounded proof is made of: the rule's own claim, in narrower bitvectors than
123/// the ones it will run in. Every width in the rule scales by the one ratio, so a rule that
124/// converts between widths still converts between them here.
125///
126/// # Errors
127///
128/// Anything the model cannot write out, which is any term nobody has said the meaning of, and
129/// anything whose widths do not fit together.
130pub fn query_at(path: &str, rule: &Rule, model: &Model, width: u32) -> Result<String, Error> {
131    let widths = Widths::at(&rule.pattern, width);
132
133    // A rule that reaches memory needs the theory of arrays and a constant to stand for the
134    // memory it starts from, and a rule that reaches a float needs the theory of floats. A rule
135    // that does neither gets neither, so every rule written before effects and floats existed
136    // asks exactly the question it asked before.
137    let parts = [&rule.pattern, &rule.replacement, &rule.spec];
138    let memory = parts.iter().any(|term| model.touches_memory(term));
139    let floats = parts.iter().any(|term| model.touches_floats(term));
140    let logic = match (memory, floats) {
141        (false, false) => "QF_BV",
142        (true, false) => "QF_ABV",
143        (false, true) => "QF_FPBV",
144        (true, true) => "QF_ABVFP",
145    };
146    let mut out = format!("(set-logic {logic})\n");
147    if memory {
148        let sort = Sort::Memory.write(&widths);
149        out.push_str(&format!("(declare-const {MEMORY_CONST} {sort})\n"));
150    }
151
152    // Each name as the pattern binds it, which is not one thing for the whole rule: a rule that
153    // lowers a thirty two bit add of two sixty four bit registers has both widths in it and
154    // neither is the other, and a rule that lowers a float has a float and an address in it.
155    for (name, sort) in widths.names() {
156        out.push_str(&format!("(declare-const {name} {})\n", sort.write(&widths)));
157    }
158
159    if let Some(guard) = &rule.guard {
160        // An assumption, not part of the claim. A rule that only holds for some constants is
161        // only being asked about those constants.
162        out.push_str(&format!("(assert {})\n", model.write(path, guard, &widths)?.0));
163    }
164
165    // Two obligations, asked as one question. The first is the one that matters: what the
166    // pattern means and what the replacement means have to be the same thing, both read out of
167    // the model rather than out of anybody's description of them. The second is the rule's own
168    // `spec` clause, which is written by hand and so is worth checking rather than trusting: a
169    // rule whose stated claim is not what its pattern actually means would otherwise verify
170    // against its own mistake.
171    let (matched, over) = model.write(path, &rule.pattern, &widths)?;
172    let (produced, into) = model.write(path, &rule.replacement, &widths)?;
173    let same = agreement(path, &rule.replacement, &matched, &produced, over, into)?;
174    let substituted = substitute(&rule.spec, &produced);
175    let claim = model.write(path, &substituted, &widths.with(&produced, into))?.0;
176    out.push_str(&format!("(assert (not (and {same} {claim})))\n"));
177    out.push_str("(check-sat)\n(get-model)\n");
178    Ok(out)
179}
180
181/// What it takes for a machine term to compute what the IR term it replaces computes.
182///
183/// The same bitvector, when the two are the same width, which is every rule that does not
184/// convert. When the machine term is wider they have to agree on the bits the IR term has, which
185/// is what lowering a value into a register wider than the value means, and what the rest of the
186/// register holds is left to the rule's own `spec` clause to claim: on a target where a thirty
187/// two bit add sign extends into a sixty four bit register, that clause is the only place the
188/// sign extension is stated and so it is the only place it can be checked.
189///
190/// A machine term narrower than the IR term loses bits, and that is a mistake rather than a
191/// claim about anything.
192fn agreement(
193    path: &str,
194    at: &Term,
195    matched: &str,
196    produced: &str,
197    over: Sort,
198    into: Sort,
199) -> Result<String, Error> {
200    if over == into {
201        return Ok(format!("(= {matched} {produced})"));
202    }
203    let fail = |said: String| Error {
204        path: path.to_owned(),
205        line: at.line,
206        column: at.column,
207        message: said,
208    };
209    let (Sort::Bits(over), Sort::Bits(into)) = (over, into) else {
210        // The two are not both bitvectors and are not the same thing either, so there is no
211        // reading of this which is a mistake in the widths. Either one of them is a memory,
212        // which is a rule replacing something with an effect by something without one or the
213        // other way round, or one of them is a float, which is a rule computing a float out of
214        // bits or the other way round without saying which reading of those bits it means.
215        let said = if over == Sort::Memory || into == Sort::Memory {
216            "this replaces something that computes a value with something that computes a \
217             memory, or the other way round"
218                .to_owned()
219        } else {
220            format!(
221                "this replaces something {} with something {}",
222                over.describe(),
223                into.describe()
224            )
225        };
226        return Err(fail(said));
227    };
228    if into < over {
229        let said = format!(
230            "what this replaces is {over} bits wide and this is {into}, so it cannot compute it"
231        );
232        return Err(fail(said));
233    }
234    Ok(format!("(= {matched} ((_ extract {} 0) {produced}))", over - 1))
235}
236
237/// The widths a bounded proof is taken over, narrowest first.
238///
239/// Two of them rather than one, because a claim that holds at a single width can hold for
240/// reasons that are about that width. Both of them small, because the claims that need a
241/// bounded proof at all are the ones mixing multiplication with division, and one of those is
242/// as far out of reach at sixteen bits as it is at sixty four: the rule the tests use is
243/// answered in hundredths of a second at eight bits and not at all at sixteen. Only widths
244/// narrower than the rule's own are used, so a rule that already works in four bits has nothing
245/// to fall back to.
246pub const BOUNDED_WIDTHS: [u32; 2] = [4, 8];
247
248/// Ask about every rule.
249///
250/// A rule that the solver settles at its own width is discharged and that is the end of it. A
251/// rule it gives up on is asked again at [`BOUNDED_WIDTHS`], but only if the rule carries a
252/// written reason for taking narrow widths as enough, because a bounded proof is a judgement
253/// somebody makes and not a fallback a tool takes on its own.
254///
255/// # Errors
256///
257/// Anything the model cannot write out, and anything that stops the solver from running.
258pub fn verify(
259    path: &str,
260    rules: &[Rule],
261    model: &Model,
262    solver: &Solver,
263) -> Result<Report, Vec<Error>> {
264    let mut report = Report::default();
265    let mut errors = Vec::new();
266
267    for rule in rules {
268        let width = rule_width(&rule.pattern);
269        match ask(path, rule, model, solver, width) {
270            Err(error) => errors.push(error),
271            Ok(Answer::Unsat) => report.verdicts.push(Verdict::Discharged),
272            Ok(Answer::Sat(found)) => report.verdicts.push(Verdict::Refuted(found)),
273            Ok(Answer::Unknown) => match &rule.bounded {
274                None => report.verdicts.push(Verdict::Unknown),
275                Some(why) => match bounded(path, rule, model, solver, width, why) {
276                    Ok(verdict) => report.verdicts.push(verdict),
277                    Err(error) => errors.push(error),
278                },
279            },
280        }
281    }
282
283    if errors.is_empty() { Ok(report) } else { Err(errors) }
284}
285
286/// Verify a rule set and refuse the whole of it if anything in it cannot enter.
287///
288/// This is the gate `spec/17-milestones.md` asks for. It refuses the file rather than dropping
289/// the rules that failed, because a compiler built from the rules that happened to pass is a
290/// compiler nobody described: what it does with the terms the dropped rules matched is then a
291/// question about the order of the rest.
292///
293/// # Errors
294///
295/// One error per rule that may not enter, at the line the rule starts on, and anything that
296/// stopped the verification from happening at all.
297pub fn admit(
298    path: &str,
299    rules: &[Rule],
300    model: &Model,
301    solver: &Solver,
302) -> Result<Report, Vec<Error>> {
303    let report = verify(path, rules, model, solver)?;
304    let mut errors = Vec::new();
305    for (rule, verdict) in rules.iter().zip(&report.verdicts) {
306        if let Some(said) = verdict.refusal() {
307            errors.push(Error {
308                path: path.to_owned(),
309                line: rule.line,
310                column: rule.column,
311                message: said,
312            });
313        }
314    }
315    if errors.is_empty() { Ok(report) } else { Err(errors) }
316}
317
318/// Put one question to the solver.
319fn ask(
320    path: &str,
321    rule: &Rule,
322    model: &Model,
323    solver: &Solver,
324    width: u32,
325) -> Result<Answer, Error> {
326    let asked = query_at(path, rule, model, width)?;
327    solver.ask(&asked).map_err(|problem| Error {
328        path: path.to_owned(),
329        line: rule.line,
330        column: rule.column,
331        message: format!("the solver could not be run: {problem}"),
332    })
333}
334
335/// Ask the rule again at the narrow widths, once the real one has come back a shrug.
336///
337/// Every width has to come back `unsat`. A counterexample at a narrow width is reported as the
338/// refutation it looks like, named with the width it was found at, because the two things it
339/// can be are a rule that is wrong and a rule whose constants do not fit in four bits, and both
340/// are for a person to look at rather than for this to decide.
341fn bounded(
342    path: &str,
343    rule: &Rule,
344    model: &Model,
345    solver: &Solver,
346    width: u32,
347    why: &str,
348) -> Result<Verdict, Error> {
349    let mut proved = Vec::new();
350    for narrow in BOUNDED_WIDTHS.iter().copied().filter(|narrow| *narrow < width) {
351        match ask(path, rule, model, solver, narrow)? {
352            Answer::Unsat => proved.push(narrow),
353            Answer::Sat(found) => {
354                let said = format!("at {narrow} bits, where the rule works in {width}: {found}");
355                return Ok(Verdict::Refuted(said));
356            }
357            Answer::Unknown => return Ok(Verdict::Unknown),
358        }
359    }
360    if proved.is_empty() {
361        return Ok(Verdict::Unknown);
362    }
363    Ok(Verdict::Bounded { widths: proved, why: why.to_owned() })
364}
365
366/// Put the replacement's meaning where the specification says `(result)`.
367///
368/// This is a substitution on the written form rather than on the term, because what the
369/// replacement means is SMT-LIB text by the time it is known and there is nothing to put back
370/// into a term.
371fn substitute(spec: &Term, produced: &str) -> Term {
372    match &spec.kind {
373        TermKind::App { head, args } if head == "result" && args.is_empty() => {
374            Term { kind: TermKind::Var(produced.to_owned()), line: spec.line, column: spec.column }
375        }
376        TermKind::App { head, args } => Term {
377            kind: TermKind::App {
378                head: head.clone(),
379                args: args.iter().map(|arg| substitute(arg, produced)).collect(),
380            },
381            line: spec.line,
382            column: spec.column,
383        },
384        _ => spec.clone(),
385    }
386}