logicaffeine-proof 0.10.1

Backward-chaining proof engine (certified SAT/CDCL, tactics, Socratic hints) plus the number-theory / cryptanalysis substrate (factoring, isogeny, lattice, order-finding)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
//! Standard proof-trace emission — **DRAT**, **LRAT**, and **DPR** text from our in-memory
//! [`ProofStep`] stream, so the certified refutations this crate produces are not merely
//! self-checked but replayable by the SAT community's independent (and in `cake_lpr`'s case
//! *formally verified*) checkers.
//!
//! Three formats, three trust reaches:
//!
//! - **DRAT** (Wetzler/Heule/Hunt 2014) — clause *additions* and *deletions*, no hints. The
//!   universal interchange format; checked by `drat-trim`. We emit it for any refutation whose
//!   added clauses are all RUP (our plain-CDCL fallback, the BVE/vivification RUP path).
//! - **LRAT** (Cruz-Filipe et al. 2017) — DRAT plus an explicit *hint chain* of antecedent
//!   clause IDs per step, so the checker replays in `O(hint)` rather than re-searching. This is
//!   what the CakeML-**verified** `cake_lpr` consumes. We synthesise the hints by instrumenting
//!   the very same unit-propagation core the RUP checker uses (`rup`), then ship an
//!   independent [`check_lrat`] that re-validates them.
//! - **DPR** (Heule/Kiesl/Biere 2017) — additions carrying an *assignment witness* for a
//!   propagation-redundant (model-removing) clause; checked by `dpr-trim`. This is the format
//!   `SaDiCaL` emits for its positive-reduct PR clauses, and the one our SDCL path lands in.
//!
//! **The honesty seam.** Our symmetry crusher certifies clauses with a *substitution* witness
//! under the **SR** (substitution-redundancy) criterion — strictly stronger than the PR that
//! `dpr-trim` checks (the PHP swap clause `¬x(i,h)` is SR-but-not-PR). So the DPR emitter does
//! not blindly transcribe a substitution: it [`try_assignment_witness`]es each one — reduce to a
//! candidate assignment, then **re-verify it against `pr::is_pr`** — and emits standard DPR
//! only when that genuinely holds. A substitution that is irreducibly SR makes the emitter
//! return [`EmitError::RequiresSubstitutionRedundancy`], never a bogus PR line. Fail-closed: we
//! would rather decline to emit than emit a proof an honest checker rejects.

use core::fmt::Write as _;

use crate::cdcl::Lit;
use crate::proof::{ProofStep, Witness};
use crate::rup::{lit_val, set_true};

/// Why a proof could not be emitted in a requested standard format.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EmitError {
    /// A step's added clause is not RUP w.r.t. the database at that point — the refutation does
    /// not actually check, so there is nothing sound to emit.
    StepNotRup { index: usize },
    /// The refutation does not end by deriving the empty clause (it is not a refutation).
    EmptyClauseNotRup,
    /// A `Pr` step carries a substitution witness that is irreducibly SR — it cannot be
    /// expressed as a standard PR assignment witness, so no `dpr-trim`-checkable line exists.
    RequiresSubstitutionRedundancy { index: usize },
    /// A `Pr` step appeared where the chosen format (DRAT/LRAT) admits only RUP additions.
    PrInRupOnlyFormat { index: usize },
    /// A streaming emitter's sink refused a write — it hit the caller's byte cap (see [`SizeSink`]).
    /// Surfaced only when measuring a proof's size against a bound; the `String`-returning emitters,
    /// whose sink never fails, cannot produce it.
    SizeCapExceeded,
}

impl From<core::fmt::Error> for EmitError {
    /// The only writer that returns [`core::fmt::Error`] here is a capped [`SizeSink`]; a `String`
    /// sink is infallible, so this conversion fires exactly when a size bound was crossed.
    fn from(_: core::fmt::Error) -> Self {
        EmitError::SizeCapExceeded
    }
}

/// A byte-counting write sink that measures a proof's serialized size **without materializing it**:
/// it keeps only a running length, discarding every chunk the moment its bytes are counted, and once
/// the total would exceed `cap` it refuses further writes (returning [`core::fmt::Error`], which the
/// streaming emitters map to [`EmitError::SizeCapExceeded`]). This lets us report our own proof size
/// on the benchmarks page in O(1) memory, and guarantees a pathological (super-linear) proof aborts
/// early rather than exhausting RAM — the streaming, capped counterpart to `emit_*(...).len()`.
pub struct SizeSink {
    bytes: u64,
    cap: u64,
    overflowed: bool,
}

impl SizeSink {
    /// A sink that counts up to `cap` bytes before it starts refusing writes.
    pub fn new(cap: u64) -> Self {
        Self { bytes: 0, cap, overflowed: false }
    }
    /// Bytes streamed through so far (the serialized proof size when emission ran to completion).
    pub fn bytes(&self) -> u64 {
        self.bytes
    }
    /// Whether the cap was hit — the measured proof is at least `cap` bytes, exact size unknown.
    pub fn overflowed(&self) -> bool {
        self.overflowed
    }
}

impl core::fmt::Write for SizeSink {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        self.bytes = self.bytes.saturating_add(s.len() as u64);
        if self.bytes > self.cap {
            self.overflowed = true;
            return Err(core::fmt::Error);
        }
        Ok(())
    }
}

/// DIMACS signed-integer encoding of a literal: variable `v` (0-based) → `±(v+1)`.
#[inline]
fn dimacs(l: Lit) -> i64 {
    let v = (l.var() + 1) as i64;
    if l.is_positive() {
        v
    } else {
        -v
    }
}

/// Append `lits` as space-separated DIMACS integers followed by the `0` terminator.
fn push_clause(out: &mut String, lits: &[Lit]) {
    for &l in lits {
        out.push_str(&dimacs(l).to_string());
        out.push(' ');
    }
    out.push_str("0");
}

/// Streaming twin of [`push_clause`]: write `lits` as space-separated DIMACS integers then the `0`
/// terminator into any [`core::fmt::Write`] sink, so an emitter can feed a [`SizeSink`] instead of a
/// growing `String`. Byte-for-byte identical to [`push_clause`].
fn wpush_clause<W: core::fmt::Write>(w: &mut W, lits: &[Lit]) -> Result<(), EmitError> {
    for &l in lits {
        write!(w, "{} ", dimacs(l))?;
    }
    w.write_str("0")?;
    Ok(())
}

/// Write a single literal as `"<dimacs> "` (integer then a trailing space) — the token shape the SR
/// witness/permutation sections use.
fn wput<W: core::fmt::Write>(w: &mut W, l: Lit) -> Result<(), EmitError> {
    write!(w, "{} ", dimacs(l))?;
    Ok(())
}

/// The canonical multiset key of a clause, for matching deletions against the database.
fn canon(c: &[Lit]) -> Vec<u32> {
    let mut k: Vec<u32> = c.iter().map(|l| l.var() * 2 + u32::from(!l.is_positive())).collect();
    k.sort_unstable();
    k.dedup();
    k
}

// ---------------------------------------------------------------------------------------------
// Witness reduction (SR substitution → re-verified PR assignment), fail-closed.
// ---------------------------------------------------------------------------------------------

/// Reduce a [`Witness`] to a standard PR **assignment** witness for `clause` against `db`,
/// returning `Some(ω)` only when `ω` genuinely satisfies `pr::is_pr` — so the caller can emit a
/// `dpr-trim`-checkable line — and `None` when the witness is irreducibly SR.
///
/// For an [`Witness::Assignment`] this just re-verifies the witness it already carries. For a
/// [`Witness::Substitution`] `σ` it tries the canonical reduction `ω = {¬σ(l) : l ∈ C}` and, since
/// that need not satisfy `C`, a small family of candidates seeded by each satisfying literal of
/// `C`. Every candidate is checked by `pr::is_pr` before being returned, so the function can never
/// hand back an unsound witness.
pub fn try_assignment_witness(
    num_vars: usize,
    db: &[Vec<Lit>],
    clause: &[Lit],
    witness: &Witness,
) -> Option<Vec<Lit>> {
    let accept = |w: &[Lit]| crate::pr::is_pr(num_vars, db, clause, &Witness::Assignment(w.to_vec()));
    match witness {
        Witness::Assignment(w) => {
            if accept(w) {
                Some(w.clone())
            } else {
                None
            }
        }
        Witness::Substitution(sigma) => {
            // The image of the falsified cube under σ — the textbook reduction.
            let canon_w: Vec<Lit> = clause.iter().map(|&l| sigma.apply(l).negated()).collect();
            if accept(&canon_w) {
                return Some(canon_w);
            }
            // Seed with a satisfying literal of C (PR requires ω ⊨ C), then layer the σ-image on
            // top without self-contradiction. Tries each pivot — cheap and often closes the gap.
            for &pivot in clause {
                let mut w = vec![pivot];
                for &l in &canon_w {
                    if !w.contains(&l) && !w.contains(&l.negated()) {
                        w.push(l);
                    }
                }
                if accept(&w) {
                    return Some(w);
                }
            }
            None
        }
    }
}

// ---------------------------------------------------------------------------------------------
// DRAT.
// ---------------------------------------------------------------------------------------------

/// Emit a **DRAT** proof (additions + deletions, no hints, no witnesses). Returns
/// [`EmitError::PrInRupOnlyFormat`] if any step is a `Pr` step — DRAT cannot carry a PR witness;
/// use [`emit_dpr`] for those. The trailing empty clause is appended iff it is RUP.
pub fn emit_drat(num_vars: usize, original: &[Vec<Lit>], steps: &[ProofStep]) -> Result<String, EmitError> {
    let mut db: Vec<Vec<Lit>> = original.to_vec();
    let mut out = String::new();
    for (i, step) in steps.iter().enumerate() {
        match step {
            ProofStep::Rup(c) => {
                if !crate::rup::is_rup(num_vars, &db, c) {
                    return Err(EmitError::StepNotRup { index: i });
                }
                push_clause(&mut out, c);
                out.push('\n');
                db.push(c.clone());
            }
            ProofStep::Delete(c) => {
                out.push_str("d ");
                push_clause(&mut out, c);
                out.push('\n');
                let key = canon(c);
                if let Some(pos) = db.iter().position(|d| canon(d) == key) {
                    db.swap_remove(pos);
                }
            }
            ProofStep::Pr { .. } => return Err(EmitError::PrInRupOnlyFormat { index: i }),
        }
    }
    if !crate::rup::is_rup(num_vars, &db, &[]) {
        return Err(EmitError::EmptyClauseNotRup);
    }
    out.push_str("0\n");
    Ok(out)
}

// ---------------------------------------------------------------------------------------------
// DPR.
// ---------------------------------------------------------------------------------------------

/// Emit a **DPR** proof: RUP additions as bare clauses, PR additions as `C 0 ω 0` (witness `ω`
/// re-verified and laid out pivot-first per the `dpr-trim` convention), deletions as `d C 0`. A
/// `Pr` step whose witness is irreducibly SR yields [`EmitError::RequiresSubstitutionRedundancy`].
pub fn emit_dpr(num_vars: usize, original: &[Vec<Lit>], steps: &[ProofStep]) -> Result<String, EmitError> {
    let mut db: Vec<Vec<Lit>> = original.to_vec();
    let mut out = String::new();
    for (i, step) in steps.iter().enumerate() {
        match step {
            ProofStep::Rup(c) => {
                if !crate::rup::is_rup(num_vars, &db, c) {
                    return Err(EmitError::StepNotRup { index: i });
                }
                push_clause(&mut out, c);
                out.push('\n');
                db.push(c.clone());
            }
            ProofStep::Pr { clause, witness } => {
                let omega = try_assignment_witness(num_vars, &db, clause, witness)
                    .ok_or(EmitError::RequiresSubstitutionRedundancy { index: i })?;
                // The witness must lead with the clause's first literal (the pivot): pick a
                // literal of C that ω satisfies and float it to the front of both.
                let pivot = clause
                    .iter()
                    .copied()
                    .find(|l| omega.contains(l))
                    .expect("a verified PR witness satisfies its clause");
                let mut c_ord = vec![pivot];
                c_ord.extend(clause.iter().copied().filter(|&l| l != pivot));
                let mut w_ord = vec![pivot];
                w_ord.extend(omega.iter().copied().filter(|&l| l != pivot));
                push_clause(&mut out, &c_ord);
                out.push(' ');
                // The witness literals, then their own terminating 0.
                for &l in &w_ord {
                    out.push_str(&dimacs(l).to_string());
                    out.push(' ');
                }
                out.push_str("0\n");
                db.push(clause.clone());
            }
            ProofStep::Delete(c) => {
                out.push_str("d ");
                push_clause(&mut out, c);
                out.push('\n');
                let key = canon(c);
                if let Some(pos) = db.iter().position(|d| canon(d) == key) {
                    db.swap_remove(pos);
                }
            }
        }
    }
    if !crate::rup::is_rup(num_vars, &db, &[]) {
        return Err(EmitError::EmptyClauseNotRup);
    }
    out.push_str("0\n");
    Ok(out)
}

// ---------------------------------------------------------------------------------------------
// SR (substitution redundancy) — Marijn Heule's `.sr` format, the input to `sr2drat`.
// ---------------------------------------------------------------------------------------------

/// Emit our refutation in **Marijn Heule's `.sr` (substitution-redundancy) proof format** — the input
/// `sr2drat` expands into plain DRAT for `drat-trim` to verify. This is the pipeline that makes our
/// marquee *symmetry* proofs externally machine-checkable, closing the "SR witnesses not yet exportable"
/// gap: a `Pr` step carrying a [`Witness::Substitution`] no longer dead-ends at
/// [`EmitError::RequiresSubstitutionRedundancy`] but is written as a substitution line `sr2drat` knows.
///
/// Per the `sr2drat` grammar (sr2drat.c) a lemma line is the clause, then — each delimited by a repeat
/// of the **pivot** (the clause's first literal) — the PR witness `ω`, then optionally the permutation
/// `σ` as `var image` pairs. RUP additions carry no witness (a bare `C 0` line `sr2drat` forwards to
/// DRAT). Each step is re-verified (`pr::is_pr` / RUP) before it is written, so a returned proof is
/// sound by our checker; the trailing empty clause closes it.
///
/// The decisive subtlety, matching the reference `.sr` shape: `sr2drat` expects the lemma's **pivot
/// variable to be held by the assignment `ω`, with `σ` permuting only the other variables**. For a unit
/// lemma `[p]` witnessed by a transposition that moves `p`, we therefore *decompose* the witness —
/// `ω = {p, ¬σ(p)}` pins both swapped copies in that coordinate and `σ′` (σ with `p`'s and `σ(p)`'s
/// variables fixed) carries the rest. This keeps the pivot out of the permutation entirely, which is
/// what makes the expansion check: **verified end-to-end through `sr2drat`→`drat-trim` for PHP(n) up to
/// n=18 (a 591k-line DRAT proof)**, the marquee symmetry refutations Kissat and CaDiCaL time out on.
pub fn emit_sr(num_vars: usize, original: &[Vec<Lit>], steps: &[ProofStep]) -> Result<String, EmitError> {
    let mut out = String::new();
    write_sr(&mut out, num_vars, original, steps)?;
    Ok(out)
}

/// Streaming core of [`emit_sr`]: write the SR proof into any [`core::fmt::Write`] sink rather than a
/// `String`, so its serialized size can be measured through a capped [`SizeSink`] in O(1) memory (and
/// bail early on a pathological proof) instead of building the whole text. Byte-for-byte identical to
/// [`emit_sr`]; the `String`-backed wrapper above simply cannot hit [`EmitError::SizeCapExceeded`].
pub fn write_sr<W: core::fmt::Write>(
    w: &mut W,
    num_vars: usize,
    original: &[Vec<Lit>],
    steps: &[ProofStep],
) -> Result<(), EmitError> {
    let mut db: Vec<Vec<Lit>> = original.to_vec();
    for (i, step) in steps.iter().enumerate() {
        match step {
            ProofStep::Rup(c) => {
                if !crate::rup::is_rup(num_vars, &db, c) {
                    return Err(EmitError::StepNotRup { index: i });
                }
                wpush_clause(w, c)?;
                w.write_str("\n")?;
                db.push(c.clone());
            }
            ProofStep::Delete(c) => {
                w.write_str("d ")?;
                wpush_clause(w, c)?;
                w.write_str("\n")?;
                let key = canon(c);
                if let Some(pos) = db.iter().position(|d| canon(d) == key) {
                    db.swap_remove(pos);
                }
            }
            ProofStep::Pr { clause, witness } => {
                if !crate::pr::is_pr(num_vars, &db, clause, witness) {
                    return Err(EmitError::StepNotRup { index: i });
                }
                match witness {
                    Witness::Assignment(omega) => {
                        let pivot = *clause.first().ok_or(EmitError::StepNotRup { index: i })?;
                        for &l in clause {
                            wput(w, l)?;
                        }
                        // Witness section opens with the pivot; then ω's other literals.
                        wput(w, pivot)?;
                        for &l in omega {
                            if l != pivot {
                                wput(w, l)?;
                            }
                        }
                        w.write_str("0\n")?;
                        db.push(clause.clone());
                    }
                    Witness::Substitution(sigma) => {
                        let pivot = *clause.first().ok_or(EmitError::StepNotRup { index: i })?;
                        // **Witness decomposition (sr2drat convention).** sr2drat wants the lemma's pivot
                        // variable held by the assignment ω, with σ permuting only the *other* variables
                        // (the pivot variable σ-fixed) — exactly how the reference `.sr` proofs are
                        // shaped. For a unit lemma `[p]` witnessed by a transposition that moves `p`'s
                        // variable, we split: ω = {p, ¬σ(p)} pins both swapped copies in this coordinate,
                        // and σ' = σ with `p`'s variable and `σ(p)`'s variable fixed carries the rest.
                        // This keeps the pivot out of the permutation (no delimiter collision) and out of
                        // the dropped-literal trap that breaks ≥3-variable orbits.
                        let (omega_extra, fixed_a, fixed_b): (Option<Lit>, u32, u32) =
                            if clause.len() == 1 {
                                let sp = sigma.apply(pivot);
                                (Some(sp.negated()), pivot.var(), sp.var())
                            } else {
                                (None, u32::MAX, u32::MAX)
                            };
                        for &l in clause {
                            wput(w, l)?;
                        }
                        // Witness ω opens with the pivot, then its decomposition partner.
                        wput(w, pivot)?;
                        if let Some(extra) = omega_extra {
                            if extra != pivot {
                                wput(w, extra)?;
                            }
                        }
                        // Permutation section: σ over the moved variables, with the pivot coordinate
                        // (both swapped copies) fixed when we decomposed a unit lemma.
                        wput(w, pivot)?;
                        for v in 0..num_vars as u32 {
                            if v == fixed_a || v == fixed_b {
                                continue; // held by ω, not permuted
                            }
                            let img = sigma.apply(Lit::pos(v));
                            if img == Lit::pos(v) {
                                continue; // fixed point
                            }
                            if img == pivot || Lit::pos(v) == pivot {
                                return Err(EmitError::RequiresSubstitutionRedundancy { index: i });
                            }
                            write!(w, "{} ", (v as i64) + 1)?;
                            wput(w, img)?;
                        }
                        w.write_str("0\n")?;
                        db.push(clause.clone());
                    }
                }
            }
        }
    }
    if !crate::rup::is_rup(num_vars, &db, &[]) {
        return Err(EmitError::EmptyClauseNotRup);
    }
    w.write_str("0\n")?;
    Ok(())
}

/// Parse a DRAT/DPR proof body back into [`ProofStep`]s — one clause per line. A bare `C 0` line
/// is a RUP addition; `C 0 ω 0` is a PR addition with an [`Witness::Assignment`]; `d C 0` is a
/// deletion. Used to round-trip our own emission through our own checker.
pub fn parse_dpr(text: &str) -> Result<Vec<ProofStep>, String> {
    let mut steps = Vec::new();
    for raw in text.lines() {
        let line = raw.trim();
        if line.is_empty() || line.starts_with('c') {
            continue;
        }
        let mut toks = line.split_whitespace().peekable();
        let deletion = matches!(toks.peek(), Some(&"d"));
        if deletion {
            toks.next();
        }
        // First field: literals up to the first 0.
        let mut clause = Vec::new();
        let mut saw_terminator = false;
        for tok in toks.by_ref() {
            let n: i64 = tok.parse().map_err(|_| format!("bad token {tok:?}"))?;
            if n == 0 {
                saw_terminator = true;
                break;
            }
            clause.push(Lit::new((n.unsigned_abs() - 1) as u32, n > 0));
        }
        if !saw_terminator {
            return Err(format!("unterminated clause in line {line:?}"));
        }
        if deletion {
            steps.push(ProofStep::Delete(clause));
            continue;
        }
        // A second field (the witness) may follow.
        let mut witness = Vec::new();
        let mut has_witness = false;
        for tok in toks.by_ref() {
            has_witness = true;
            let n: i64 = tok.parse().map_err(|_| format!("bad witness token {tok:?}"))?;
            if n == 0 {
                break;
            }
            witness.push(Lit::new((n.unsigned_abs() - 1) as u32, n > 0));
        }
        if has_witness {
            steps.push(ProofStep::Pr { clause, witness: Witness::Assignment(witness) });
        } else {
            steps.push(ProofStep::Rup(clause));
        }
    }
    Ok(steps)
}

// ---------------------------------------------------------------------------------------------
// LRAT (with synthesised hint chains) + an independent LRAT checker.
// ---------------------------------------------------------------------------------------------

/// Run RUP for `c` against the id-tagged `db`, returning the **LRAT hint chain** — the antecedent
/// clause ids in trail order, conflict clause last — or `None` if `c` is not RUP. The chain is
/// the reason-clauses reachable backward from the conflict, which is exactly what an LRAT checker
/// replays.
fn rup_hints(num_vars: usize, db: &[(u64, Vec<Lit>)], c: &[Lit]) -> Option<Vec<u64>> {
    let mut assign: Vec<Option<bool>> = vec![None; num_vars];
    let mut reason: Vec<Option<u64>> = vec![None; num_vars];
    let mut trail_pos: Vec<Option<usize>> = vec![None; num_vars];
    let mut next_pos = 0usize;
    // Assume ¬c. An immediate clash makes c trivially RUP (a tautological clause); no hints.
    for &l in c {
        let nl = l.negated();
        match lit_val(&assign, nl) {
            Some(true) => {}
            Some(false) => return Some(Vec::new()),
            None => {
                assign[nl.var() as usize] = Some(nl.is_positive());
                trail_pos[nl.var() as usize] = Some(next_pos);
                next_pos += 1;
            }
        }
    }
    loop {
        let mut changed = false;
        for (id, clause) in db {
            let mut satisfied = false;
            let mut unset: Vec<Lit> = Vec::new();
            for &l in clause {
                match lit_val(&assign, l) {
                    Some(true) => {
                        satisfied = true;
                        break;
                    }
                    Some(false) => {}
                    None => {
                        if unset.contains(&l.negated()) {
                            satisfied = true;
                            break;
                        }
                        if !unset.contains(&l) {
                            unset.push(l);
                        }
                    }
                }
            }
            if satisfied {
                continue;
            }
            if unset.is_empty() {
                return Some(build_chain(db, &reason, &trail_pos, clause, *id));
            }
            if unset.len() == 1 {
                let u = unset[0];
                assign[u.var() as usize] = Some(u.is_positive());
                reason[u.var() as usize] = Some(*id);
                trail_pos[u.var() as usize] = Some(next_pos);
                next_pos += 1;
                changed = true;
            }
        }
        if !changed {
            return None;
        }
    }
}

/// Backward-mark the reason clauses needed for the conflict at `conflict_clause`/`conflict_id`,
/// then order them by the trail position of the literal each forced (ascending) with the conflict
/// id last — a forward-replayable LRAT hint chain.
fn build_chain(
    db: &[(u64, Vec<Lit>)],
    reason: &[Option<u64>],
    trail_pos: &[Option<usize>],
    conflict_clause: &[Lit],
    conflict_id: u64,
) -> Vec<u64> {
    let by_id: std::collections::HashMap<u64, &Vec<Lit>> =
        db.iter().map(|(id, c)| (*id, c)).collect();
    let num_vars = reason.len();
    let mut visited = vec![false; num_vars];
    let mut needed: Vec<(usize, u64)> = Vec::new();
    let mut stack: Vec<u32> = conflict_clause.iter().map(|l| l.var()).collect();
    while let Some(v) = stack.pop() {
        if visited[v as usize] {
            continue;
        }
        visited[v as usize] = true;
        if let Some(rid) = reason[v as usize] {
            let pos = trail_pos[v as usize].expect("a forced literal has a trail position");
            needed.push((pos, rid));
            if let Some(rc) = by_id.get(&rid) {
                for &l in *rc {
                    stack.push(l.var());
                }
            }
        }
    }
    needed.sort_by_key(|&(pos, _)| pos);
    let mut chain: Vec<u64> = needed.into_iter().map(|(_, id)| id).collect();
    chain.push(conflict_id);
    chain
}

/// Emit an **LRAT** proof: each RUP addition as `<id> <clause> 0 <hints> 0`, deletions as
/// `<id> d <deleted-id> 0`, and a final empty-clause line. Returns
/// [`EmitError::PrInRupOnlyFormat`] for any `Pr` step (LRAT proper carries no PR witness).
pub fn emit_lrat(num_vars: usize, original: &[Vec<Lit>], steps: &[ProofStep]) -> Result<String, EmitError> {
    let mut db: Vec<(u64, Vec<Lit>)> =
        original.iter().enumerate().map(|(i, c)| (i as u64 + 1, c.clone())).collect();
    let mut next_id = original.len() as u64 + 1;
    let mut out = String::new();
    for (i, step) in steps.iter().enumerate() {
        match step {
            ProofStep::Rup(c) => {
                let hints = rup_hints(num_vars, &db, c).ok_or(EmitError::StepNotRup { index: i })?;
                let id = next_id;
                next_id += 1;
                out.push_str(&id.to_string());
                out.push(' ');
                push_clause(&mut out, c);
                out.push(' ');
                for h in &hints {
                    out.push_str(&h.to_string());
                    out.push(' ');
                }
                out.push_str("0\n");
                db.push((id, c.clone()));
            }
            ProofStep::Delete(c) => {
                let key = canon(c);
                if let Some(pos) = db.iter().rposition(|(_, d)| canon(d) == key) {
                    let (did, _) = db[pos];
                    out.push_str(&(next_id - 1).to_string());
                    out.push_str(" d ");
                    out.push_str(&did.to_string());
                    out.push_str(" 0\n");
                    db.remove(pos);
                }
            }
            ProofStep::Pr { .. } => return Err(EmitError::PrInRupOnlyFormat { index: i }),
        }
    }
    // The closing empty clause.
    let ehints = rup_hints(num_vars, &db, &[]).ok_or(EmitError::EmptyClauseNotRup)?;
    out.push_str(&next_id.to_string());
    out.push_str(" 0 ");
    for h in &ehints {
        out.push_str(&h.to_string());
        out.push(' ');
    }
    out.push_str("0\n");
    Ok(out)
}

/// Independently verify an **LRAT** proof against `original` — the small, naive checker whose
/// simplicity is the trust. Replays each addition's hint chain (every hinted clause must be unit
/// at its turn, the last must conflict) and accepts iff a line derives the empty clause. Rejects a
/// corrupted hint, a dangling id, or a chain that fails to conflict.
pub fn check_lrat(num_vars: usize, original: &[Vec<Lit>], lrat: &str) -> bool {
    let mut db: std::collections::HashMap<u64, Vec<Lit>> =
        original.iter().enumerate().map(|(i, c)| (i as u64 + 1, c.clone())).collect();
    for raw in lrat.lines() {
        let line = raw.trim();
        if line.is_empty() || line.starts_with('c') {
            continue;
        }
        let mut toks = line.split_whitespace();
        let Some(id_tok) = toks.next() else { continue };
        let Ok(id) = id_tok.parse::<u64>() else { return false };
        let rest: Vec<&str> = toks.collect();
        if rest.first() == Some(&"d") {
            for t in &rest[1..] {
                if let Ok(d) = t.parse::<u64>() {
                    if d != 0 {
                        db.remove(&d);
                    }
                }
            }
            continue;
        }
        // Split the line into the clause (up to the first 0) and the hint chain (up to the next).
        let mut clause = Vec::new();
        let mut k = 0usize;
        while k < rest.len() {
            let Ok(n) = rest[k].parse::<i64>() else { return false };
            k += 1;
            if n == 0 {
                break;
            }
            clause.push(Lit::new((n.unsigned_abs() - 1) as u32, n > 0));
        }
        let mut hints = Vec::new();
        while k < rest.len() {
            let Ok(n) = rest[k].parse::<i64>() else { return false };
            k += 1;
            if n == 0 {
                break;
            }
            hints.push(n as u64);
        }
        if !verify_rup_step(num_vars, &db, &clause, &hints) {
            return false;
        }
        if clause.is_empty() {
            return true;
        }
        db.insert(id, clause);
    }
    false
}

/// Replay one LRAT step: assume `¬clause`, then for each hint id (in order) the hinted clause must
/// be unit (propagate its literal) until the last hint conflicts.
fn verify_rup_step(
    num_vars: usize,
    db: &std::collections::HashMap<u64, Vec<Lit>>,
    clause: &[Lit],
    hints: &[u64],
) -> bool {
    let mut assign: Vec<Option<bool>> = vec![None; num_vars];
    for &l in clause {
        if !set_true(&mut assign, l.negated()) {
            return true; // ¬clause self-contradictory ⇒ trivially valid
        }
    }
    for (i, &h) in hints.iter().enumerate() {
        let Some(cl) = db.get(&h) else { return false };
        let mut satisfied = false;
        let mut unset: Vec<Lit> = Vec::new();
        for &l in cl {
            match lit_val(&assign, l) {
                Some(true) => {
                    satisfied = true;
                    break;
                }
                Some(false) => {}
                None => {
                    if unset.contains(&l.negated()) {
                        satisfied = true;
                        break;
                    }
                    if !unset.contains(&l) {
                        unset.push(l);
                    }
                }
            }
        }
        if satisfied {
            return false; // a satisfied clause is never a valid unit/conflict hint
        }
        if unset.is_empty() {
            return i == hints.len() - 1; // conflict — must be the final hint
        }
        if unset.len() == 1 {
            set_true(&mut assign, unset[0]);
        } else {
            return false; // not unit, not conflict ⇒ a bogus hint
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cdcl::Lit;
    use crate::proof::Perm;
    use crate::pr::check_pr_refutation;

    fn p(v: u32) -> Lit {
        Lit::pos(v)
    }
    fn n(v: u32) -> Lit {
        Lit::neg(v)
    }

    /// The canonical 2-variable all-clauses UNSAT instance, refuted by the resolvents `p`, `¬p`.
    fn pq_unsat() -> (usize, Vec<Vec<Lit>>, Vec<ProofStep>) {
        let f = vec![vec![p(0), p(1)], vec![p(0), n(1)], vec![n(0), p(1)], vec![n(0), n(1)]];
        let steps = vec![ProofStep::Rup(vec![p(0)]), ProofStep::Rup(vec![n(0)])];
        (2, f, steps)
    }

    #[test]
    fn drat_round_trips_through_our_checker() {
        let (nv, f, steps) = pq_unsat();
        let text = emit_drat(nv, &f, &steps).expect("emits");
        let parsed = parse_dpr(&text).expect("parses");
        assert!(check_pr_refutation(nv, &f, &parsed), "round-tripped DRAT must still refute");
    }

    #[test]
    fn drat_rejects_a_corrupted_addition() {
        // Over a SATISFIABLE base `(a ∨ b)`, a bolted-on unit `¬a` is not entailed (assuming `a`
        // satisfies the clause with no propagation), so it is not RUP — the parsed-back "proof"
        // must fail to verify. (In the full 4-clause UNSAT formula every clause is RUP, so the
        // base must be satisfiable for the corruption to bite.)
        let f = vec![vec![p(0), p(1)]];
        let bogus = parse_dpr("-1 0\n").expect("parses"); // the un-entailed unit `¬a`
        assert!(!check_pr_refutation(2, &f, &bogus));
    }

    #[test]
    fn lrat_hints_validate_and_reject_corruption() {
        let (nv, f, steps) = pq_unsat();
        let lrat = emit_lrat(nv, &f, &steps).expect("emits LRAT");
        assert!(check_lrat(nv, &f, &lrat), "our own LRAT checker accepts our hints");

        // Corrupt a hint id → the chain no longer conflicts where claimed → rejected.
        let mangled = lrat.replace(" 0\n", " 999 0\n");
        assert!(!check_lrat(nv, &f, &mangled), "a corrupted hint chain must be rejected");
    }

    #[test]
    fn lrat_checker_rejects_a_proof_with_no_empty_clause() {
        let (nv, f, _) = pq_unsat();
        // A single learned unit `p` with a valid hint, but never closing to the empty clause.
        let db: Vec<(u64, Vec<Lit>)> =
            f.iter().enumerate().map(|(i, c)| (i as u64 + 1, c.clone())).collect();
        let hints = rup_hints(nv, &db, &[p(0)]).expect("p is RUP");
        let mut line = String::from("5 1 ");
        for h in hints {
            line.push_str(&h.to_string());
            line.push(' ');
        }
        line.push_str("0\n");
        assert!(!check_lrat(nv, &f, &line), "no empty-clause line ⇒ not a refutation");
    }

    #[test]
    fn dpr_assignment_witness_line_round_trips_and_reverifies() {
        // The textbook model-removing PR clause: F=(a∨b), C=(¬a∨b) with ω={¬a,b}. Emit it as a
        // DPR witness line, parse it back, and confirm the parsed clause+witness still PR-checks.
        let (a, b) = (0u32, 1u32);
        let f = vec![vec![p(a), p(b)]];
        let c = vec![n(a), p(b)];
        let omega = vec![n(a), p(b)];
        let witness = Witness::Assignment(omega);
        assert!(crate::pr::is_pr(2, &f, &c, &witness));

        // emit_dpr requires a full refutation; here we test just the witness-line machinery, so
        // hand-format one line and parse it back.
        let w = try_assignment_witness(2, &f, &c, &witness).expect("reduces");
        let pivot = c.iter().copied().find(|l| w.contains(l)).unwrap();
        let mut out = String::new();
        let mut c_ord = vec![pivot];
        c_ord.extend(c.iter().copied().filter(|&l| l != pivot));
        push_clause(&mut out, &c_ord);
        out.push(' ');
        let mut w_ord = vec![pivot];
        w_ord.extend(w.iter().copied().filter(|&l| l != pivot));
        for &l in &w_ord {
            out.push_str(&dimacs(l).to_string());
            out.push(' ');
        }
        out.push_str("0\n");

        let parsed = parse_dpr(&out).expect("parses");
        match &parsed[0] {
            ProofStep::Pr { clause, witness } => {
                assert!(crate::pr::is_pr(2, &f, clause, witness), "parsed DPR line re-verifies");
            }
            other => panic!("expected a PR step, got {other:?}"),
        }
    }

    #[test]
    fn lrat_certifies_a_real_cdcl_refutation_of_php() {
        // Not a hand-built trace: the actual learned clauses from our CDCL solver refuting PHP(3),
        // emitted as LRAT and replayed by our independent (cake_lpr-style) checker.
        use crate::cdcl::{SolveResult, Solver};
        let (cnf, _) = crate::families::php(3);
        let nv = cnf.num_vars;
        let mut solver = Solver::new(nv);
        for c in &cnf.clauses {
            solver.add_clause(c.clone());
        }
        let steps: Vec<ProofStep> = match solver.solve() {
            SolveResult::Unsat => {
                solver.learned().iter().map(|c| ProofStep::Rup(c.lits.clone())).collect()
            }
            SolveResult::Sat(_) => panic!("PHP(3) is UNSAT"),
        };
        let lrat = emit_lrat(nv, &cnf.clauses, &steps).expect("a real CDCL refutation is RUP → LRAT");
        assert!(check_lrat(nv, &cnf.clauses, &lrat), "LRAT replay of a real CDCL refutation validates");

        // The same trace also round-trips as DRAT through our checker.
        let drat = emit_drat(nv, &cnf.clauses, &steps).expect("emits DRAT");
        let parsed = parse_dpr(&drat).expect("parses");
        assert!(check_pr_refutation(nv, &cnf.clauses, &parsed));
    }

    #[test]
    fn the_universal_lrat_guarantee_via_plain_cdcl() {
        // The fail-closed fallback is pure RUP, so EVERY UNSAT instance we close carries a
        // cake_lpr-checkable LRAT proof — independent of any symmetry/PR machinery.
        let (cnf, _) = crate::families::php(4);
        let nv = cnf.num_vars;
        let steps = crate::sdcl::plain_cdcl_refutation(nv, &cnf.clauses);
        let lrat = emit_lrat(nv, &cnf.clauses, &steps).expect("plain CDCL ⇒ RUP ⇒ LRAT");
        assert!(check_lrat(nv, &cnf.clauses, &lrat), "universal LRAT certificate validates");
    }

    #[test]
    fn external_sr2drat_drat_trim_certifies_our_php_symmetry_proof() {
        // The marquee cross-check: our *substitution-redundancy* refutation of PHP(n) — the symmetry
        // proof Kissat/CaDiCaL time out on — emitted as `.sr`, expanded by Marijn Heule's `sr2drat`,
        // and VERIFIED by the community's `drat-trim`. Independent of our solver and our SR checker.
        // Gated on `$SR2DRAT` + `$DRAT_TRIM` so the default suite stays green when the binaries are
        // absent (mirrors the Z3-optional pattern).
        let (Ok(sr2drat), Ok(drat_trim)) = (std::env::var("SR2DRAT"), std::env::var("DRAT_TRIM")) else {
            eprintln!("SR2DRAT/DRAT_TRIM unset — skipping external sr2drat→drat-trim cross-check");
            return;
        };
        for n in [4usize, 8] {
            let (cnf, _) = crate::families::php(n);
            let cert = crate::sym_certify::heule_php_refutation(n);
            assert!(cert.refuted, "PHP({n}) symmetry proof closes internally");
            let sr = emit_sr(cnf.num_vars, &cnf.clauses, &cert.steps).expect("emits .sr");
            let cnf_path = temp_write(&format!("logos_sr_php{n}.cnf"), &crate::dimacs::print(&cnf));
            let sr_path = temp_write(&format!("logos_sr_php{n}.sr"), &sr);
            let drat_path = cnf_path.with_extension("drat");
            // sr2drat expands the .sr into a plain DRAT proof (stdout → file).
            let drat = std::process::Command::new(&sr2drat)
                .arg(&cnf_path)
                .arg(&sr_path)
                .output()
                .expect("run sr2drat");
            std::fs::write(&drat_path, &drat.stdout).expect("write expanded DRAT");
            // drat-trim independently verifies the expansion against the original CNF.
            let out = std::process::Command::new(&drat_trim)
                .arg(&cnf_path)
                .arg(&drat_path)
                .output()
                .expect("run drat-trim");
            let s = format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
            assert!(
                s.contains("VERIFIED") && !s.contains("NOT VERIFIED"),
                "sr2drat→drat-trim must VERIFY our PHP({n}) SR proof; got:\n{s}"
            );
        }
    }

    #[test]
    fn size_sink_measures_the_sr_proof_lazily_and_matches_the_string_length() {
        // The benchmarks page reports our OWN proof size. It must be measured by streaming the SR
        // proof through a byte counter — never materializing a giant String — so the streamed count
        // equals the real serialized length exactly, and a tight cap aborts emission early
        // (fail-closed) instead of running to completion: the memory-safety guarantee for a
        // pathologically large proof.
        let (cnf, _) = crate::families::php(4);
        let cert = crate::sym_certify::heule_php_refutation(4);
        assert!(cert.refuted, "PHP(4) symmetry proof closes internally");
        let full = emit_sr(cnf.num_vars, &cnf.clauses, &cert.steps).expect("emits .sr");
        assert!(full.len() > 8, "the SR proof is more than a handful of bytes");

        let mut sink = SizeSink::new(64 << 20);
        write_sr(&mut sink, cnf.num_vars, &cnf.clauses, &cert.steps).expect("streams .sr");
        assert!(!sink.overflowed());
        assert_eq!(sink.bytes(), full.len() as u64, "streamed byte count = serialized length");

        let mut capped = SizeSink::new(8);
        let err = write_sr(&mut capped, cnf.num_vars, &cnf.clauses, &cert.steps);
        assert_eq!(err, Err(EmitError::SizeCapExceeded), "a tight cap aborts emission early");
        assert!(capped.overflowed());
        assert!(capped.bytes() >= 8, "counted up to the cap before bailing");
    }

    #[test]
    fn dpr_emits_or_honestly_declines_the_sdcl_proof() {
        // The SaDiCaL-grade path: solve a symmetric UNSAT instance with the certified SDCL solver
        // and export the proof as DPR. Either it is fully PR (a dpr-trim-checkable proof that
        // round-trips), or a step is irreducibly SR and the emitter declines honestly — never a
        // bogus line. In both cases the internal certificate holds.
        use crate::sdcl::{solve_certified, CertifiedOutcome};
        let (cnf, _) = crate::families::php(3);
        let nv = cnf.num_vars;
        match solve_certified(nv, &cnf.clauses) {
            CertifiedOutcome::Unsat { steps, .. } => {
                assert!(check_pr_refutation(nv, &cnf.clauses, &steps), "the SDCL proof self-checks");
                match emit_dpr(nv, &cnf.clauses, &steps) {
                    Ok(text) => {
                        let parsed = parse_dpr(&text).expect("parses");
                        assert!(check_pr_refutation(nv, &cnf.clauses, &parsed), "round-tripped DPR refutes");
                    }
                    Err(EmitError::RequiresSubstitutionRedundancy { .. }) => {}
                    Err(e) => panic!("unexpected emit error: {e:?}"),
                }
            }
            CertifiedOutcome::Sat(_) => panic!("PHP(3) is UNSAT"),
        }
    }

    /// Write `text` to a uniquely-named temp file and return its path.
    fn temp_write(name: &str, text: &str) -> std::path::PathBuf {
        let path = std::env::temp_dir().join(name);
        std::fs::write(&path, text).expect("write temp file");
        path
    }

    #[test]
    fn external_drat_trim_accepts_our_drat_proof() {
        // Gold-standard cross-check: the SAT community's own `drat-trim` must accept the DRAT we
        // emit for a real CDCL refutation of PHP(5). Gated on `$DRAT_TRIM` so the default suite
        // stays green when the binary is absent (mirrors the Z3-optional pattern).
        let Ok(bin) = std::env::var("DRAT_TRIM") else {
            eprintln!("DRAT_TRIM unset — skipping external drat-trim cross-check");
            return;
        };
        let (cnf, _) = crate::families::php(5);
        let nv = cnf.num_vars;
        let steps = crate::sdcl::plain_cdcl_refutation(nv, &cnf.clauses);
        let drat = emit_drat(nv, &cnf.clauses, &steps).expect("emits DRAT");
        let cnf_path = temp_write("logos_drat_php5.cnf", &crate::dimacs::print(&cnf));
        let drat_path = temp_write("logos_drat_php5.drat", &drat);
        let out = std::process::Command::new(&bin)
            .arg(&cnf_path)
            .arg(&drat_path)
            .output()
            .expect("run drat-trim");
        let s = format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
        assert!(s.contains("VERIFIED") && !s.contains("NOT VERIFIED"), "drat-trim must VERIFY our DRAT; got:\n{s}");
    }

    #[test]
    fn external_lrat_check_accepts_our_lrat_proof() {
        // The formally-verified-style checker `lrat-check` must accept the hint-carrying LRAT we
        // synthesise for a real CDCL refutation of PHP(5). Gated on `$LRAT_CHECK`.
        let Ok(bin) = std::env::var("LRAT_CHECK") else {
            eprintln!("LRAT_CHECK unset — skipping external lrat-check cross-check");
            return;
        };
        let (cnf, _) = crate::families::php(5);
        let nv = cnf.num_vars;
        let steps = crate::sdcl::plain_cdcl_refutation(nv, &cnf.clauses);
        let lrat = emit_lrat(nv, &cnf.clauses, &steps).expect("emits LRAT");
        let cnf_path = temp_write("logos_lrat_php5.cnf", &crate::dimacs::print(&cnf));
        let lrat_path = temp_write("logos_lrat_php5.lrat", &lrat);
        let out = std::process::Command::new(&bin)
            .arg(&cnf_path)
            .arg(&lrat_path)
            .output()
            .expect("run lrat-check");
        let s = format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
        assert!(s.contains("VERIFIED") && !s.contains("NOT VERIFIED"), "lrat-check must accept our LRAT; got:\n{s}");
    }

    #[test]
    fn try_assignment_witness_is_sound_on_php_substitutions() {
        // Over the real PHP crusher's substitution steps: whenever the reducer hands back an
        // assignment witness, pr::is_pr must independently bless it (the reducer never invents an
        // unsound witness). We also report how many genuinely reduce vs. stay irreducibly SR.
        let cr = crate::sym_certify::heule_php_refutation(4);
        let (cnf, _) = crate::families::php(4);
        let nv = cnf.num_vars;
        let mut db = cnf.clauses.clone();
        let mut reduced = 0usize;
        let mut sr_only = 0usize;
        for step in &cr.steps {
            match step {
                ProofStep::Pr { clause, witness } => {
                    if let Some(w) = try_assignment_witness(nv, &db, clause, witness) {
                        assert!(
                            crate::pr::is_pr(nv, &db, clause, &Witness::Assignment(w)),
                            "the reducer returned an unsound assignment witness"
                        );
                        reduced += 1;
                    } else {
                        sr_only += 1;
                    }
                    db.push(clause.clone());
                }
                ProofStep::Rup(c) => db.push(c.clone()),
                ProofStep::Delete(c) => {
                    let key = canon(c);
                    if let Some(pos) = db.iter().position(|d| canon(d) == key) {
                        db.swap_remove(pos);
                    }
                }
            }
        }
        // The honest classification: PHP's swap clauses are SR. The test's contract is soundness
        // of the reducer, not that everything reduces — but it must have actually run on PR steps.
        assert!(reduced + sr_only > 0, "the PHP refutation must contain PR steps to classify");
        eprintln!("PHP(4): {reduced} steps reduced to PR, {sr_only} remained irreducibly SR");
    }
}