smt-scope 0.1.7

A library for parsing and analysing SMT traces.
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
use core::num::{NonZeroU32, NonZeroUsize};

use num::Num;

use crate::{
    items::*,
    parsers::z3::{VersionInfo, Z3LogParser},
    BigRational, BoxSlice, Error as E, IString, NonMaxU32, Result, StringTable,
};

use super::{inter_line::LineKind, smt2::EventLog, terms::Terms, Z3Parser};

impl Default for Z3Parser {
    fn default() -> Self {
        let mut strings = StringTable::with_hasher(fxhash::FxBuildHasher::default());
        Self {
            version_info: VersionInfo::default(),
            push_count: 0,
            terms: Terms::default(),
            synth_terms: Default::default(),
            quantifiers: Default::default(),
            insts: Default::default(),
            // ts: Default::default(),
            egraph: Default::default(),
            stack: Default::default(),
            lits: Default::default(),
            events: EventLog::new(&mut strings),
            comm: Default::default(),
            strings,
        }
    }
}

impl Z3Parser {
    pub(super) fn incremental_mode(&self) -> bool {
        self.push_count > 0
    }

    fn parse_new_full_id(&mut self, id: Option<&str>) -> Result<TermId> {
        let full_id = id.ok_or(E::UnexpectedNewline)?;
        TermId::parse(&mut self.strings, full_id)
    }

    fn parse_existing_app(&mut self, id: &str) -> Result<TermIdx> {
        self.terms
            .app_terms
            .parse_existing_id(&mut self.strings, id)
    }

    fn parse_existing_proof(&mut self, id: &str) -> Result<ProofIdx> {
        self.terms
            .proof_terms
            .parse_existing_id(&mut self.strings, id)
    }

    fn parse_existing_enode(&mut self, id: &str) -> Result<ENodeIdx> {
        let idx = self.parse_existing_app(id)?;
        self.get_existing_enode(idx)
    }

    fn get_existing_enode(&mut self, idx: TermIdx) -> Result<ENodeIdx> {
        let res = self.egraph.get_enode(idx, &self.stack);
        let can_error =
            self.version_info.is_ge_version(4, 8, 9) && self.version_info.is_le_version(4, 11, 2);
        if can_error && res.is_err() {
            // Very rarely in v4.8.17 & v4.11.2, an `[attach-enode]` is not
            // emitted. Create it here.
            // TODO: log somewhere when this happens.
            self.egraph
                .new_enode(ENodeBlame::Unknown, idx, NonMaxU32::ZERO, &self.stack)?;
            self.events.new_enode();
            return self.egraph.get_enode(idx, &self.stack);
        }
        res
    }

    fn new_term(
        &mut self,
        id: TermId,
        kind: TermKind,
        child_ids: BoxSlice<TermIdx>,
    ) -> Result<TermIdx> {
        let term = Term::new(id, kind, child_ids, |tidx| &self[tidx]);
        self.terms.app_terms.new_term(term)
    }

    fn parse_z3_generation<'a>(l: &mut impl Iterator<Item = &'a str>) -> Result<Option<NonMaxU32>> {
        if let Some(gen) = l.next() {
            let gen = gen.parse::<NonMaxU32>().map_err(E::InvalidGeneration)?;
            Ok(Some(gen))
        } else {
            Ok(None)
        }
    }

    fn map<T, U>(l: impl Iterator<Item = T>, f: impl FnMut(T) -> Result<U>) -> Result<BoxSlice<U>> {
        l.map(f).collect()
    }
    fn gobble_var_names_list<'a>(&mut self, l: impl Iterator<Item = &'a str>) -> Result<VarNames> {
        let mut t = Self::gobble_tuples::<true>(l);
        // TODO: if the list can be empty then remove the first `?` and
        // replace with default case.
        let (first, second) = t.next().ok_or(E::UnexpectedEnd)??;
        if first.is_empty() {
            let first = self.mk_string(second);
            let tuples = t.map(|t| match t? {
                ("", second) => self.mk_string(second),
                _ => Err(E::VarNamesListInconsistent),
            });
            let types = [first].into_iter().chain(tuples);
            Ok(VarNames::TypeOnly(types.collect::<Result<_>>()?))
        } else {
            fn strip_bars(
                strings: &mut StringTable,
                (first, second): (&str, &str),
            ) -> Result<(IString, IString)> {
                let first = first
                    .strip_prefix('|')
                    .ok_or(E::VarNamesNoBar)?
                    .strip_suffix('|')
                    .ok_or(E::VarNamesNoBar)?;
                let second = second
                    .strip_prefix('|')
                    .ok_or(E::VarNamesNoBar)?
                    .strip_suffix('|')
                    .ok_or(E::VarNamesNoBar)?;
                Ok((
                    IString(strings.get_or_intern(first)),
                    IString(strings.get_or_intern(second)),
                ))
            }
            let first = strip_bars(&mut self.strings, (first, second));
            let tuples = t.map(|t| strip_bars(&mut self.strings, t?));
            let names_and_types = [first].into_iter().chain(tuples);
            Ok(VarNames::NameAndType(
                names_and_types.collect::<Result<_>>()?,
            ))
        }
    }
    /// Gobble tuples with any of the following forms (`A` and `B` can be empty):
    ///  - `(A;B)`
    ///  - `(A B)`
    ///  - `(A ; B)`
    ///
    /// The resulting iterator will contain `None` for any tuples which it failed to parse.
    /// If `FORMS_EQUAL` is true, then it will return `None` for any tuples which have a different
    /// form to the first tuple.
    fn gobble_tuples<'a, const FORMS_EQUAL: bool>(
        mut l: impl Iterator<Item = &'a str>,
    ) -> impl Iterator<Item = Result<(&'a str, &'a str)>> {
        let mut spaces = None;
        let mut gobble = move || {
            let Some(first) = l.next() else {
                return Ok(None);
            };
            let (first, second) = if first.ends_with(')') {
                let spaces = *spaces.get_or_insert(0);
                if FORMS_EQUAL && spaces != 0 {
                    return Err(E::UnequalTupleForms(spaces, 0));
                }
                let mut l = first.split(';');
                (
                    l.next().ok_or(E::UnexpectedNewline)?,
                    l.next().ok_or(E::UnexpectedNewline)?,
                )
            } else {
                let middle = l.next().ok_or(E::UnexpectedNewline)?;
                if middle != ";" {
                    let spaces = *spaces.get_or_insert(1);
                    if FORMS_EQUAL && spaces != 1 {
                        return Err(E::UnequalTupleForms(spaces, 1));
                    }
                    (first, middle)
                } else {
                    let spaces = *spaces.get_or_insert(2);
                    if FORMS_EQUAL && spaces != 2 {
                        return Err(E::UnequalTupleForms(spaces, 2));
                    }
                    let second = l.next().ok_or(E::UnexpectedNewline)?;
                    (first, second)
                }
            };
            let t = (
                first.strip_prefix('(').ok_or(E::TupleMissingParens)?,
                second.strip_suffix(')').ok_or(E::TupleMissingParens)?,
            );
            Ok(Some(t))
        };
        let inverted_gobble = move |_| gobble().map_or_else(|err| Some(Err(err)), |ok| ok.map(Ok));
        std::iter::repeat(()).map_while(inverted_gobble)
    }
    fn append_trans_equality_tuples<'a>(
        &mut self,
        l: impl Iterator<Item = &'a str>,
        mut f: impl FnMut((ENodeIdx, ENodeIdx)) -> Result<()>,
    ) -> Result<()> {
        for t in Self::gobble_tuples::<true>(l) {
            let (from, to) = t?;
            let from = self.parse_existing_enode(from)?;
            let to = self.parse_existing_enode(to)?;
            f((from, to))?;
        }
        Ok(())
    }

    /// Create a new iterator which will only consume elements from `l` until
    /// it finds `end`. The element `end` will also be consumed but no other elements after that will.
    fn iter_until_eq<'a, 's>(
        l: &'a mut impl Iterator<Item = &'s str>,
        end: &'a str,
    ) -> impl Iterator<Item = &'s str> + 'a {
        l.take_while(move |elem| *elem != end)
    }
    fn expect_completed<'s>(mut l: impl Iterator<Item = &'s str>) -> Result<()> {
        l.next()
            .map_or(Ok(()), |more| Err(E::ExpectedNewline(more.to_string())))
    }

    fn mk_string(&mut self, s: &str) -> Result<IString> {
        Ok(IString(self.strings.try_get_or_intern(s)?))
    }

    fn quant_or_lamda(
        &mut self,
        full_id: TermId,
        child_ids: BoxSlice<TermIdx>,
        num_vars: NonMaxU32,
        kind: QuantKind,
    ) -> Result<()> {
        let qidx = self.quantifiers.next_key();
        let tidx = self.new_term(full_id, TermKind::Quant(qidx), child_ids)?;
        let q = Quantifier {
            num_vars,
            term: tidx,
            kind,
            vars: None,
            blame: None,
        };
        self.quantifiers.raw.try_reserve(1)?;
        let qidx2 = self.quantifiers.push_and_get_key(q);
        debug_assert_eq!(qidx, qidx2);
        Ok(())
    }

    fn parse_arith(meaning: &str) -> Result<BigRational> {
        let (rest, num) = Self::parse_arith_inner(meaning)?;
        debug_assert!(rest.is_empty());
        Ok(num.into())
    }
    fn parse_arith_inner(meaning: &str) -> Result<(&str, num::BigRational)> {
        let Some(meaning) = meaning.strip_prefix('(') else {
            // Find position of not a digit
            let end = meaning
                .bytes()
                .position(|b| !b.is_ascii_digit())
                .unwrap_or(meaning.len());
            assert_ne!(end, 0);
            let value = meaning[..end]
                .parse::<num::BigUint>()
                .map_err(E::ParseBigUintError)?;
            let value = num::BigRational::from(num::BigInt::from(value));
            return Ok((&meaning[end..], value));
        };
        let error = || E::ParseError(meaning.to_owned());
        let space = meaning.bytes().position(|b| b == b' ').ok_or_else(error)?;
        let (op, mut rest) = (&meaning[..space], &meaning[space..]);
        let mut arguments = Vec::new();
        while let Some(r) = rest.strip_prefix(' ') {
            let (r, num) = Self::parse_arith_inner(r)?;
            arguments.push(num);
            rest = r;
        }
        rest = rest.strip_prefix(')').ok_or_else(error)?;
        match op {
            "+" => Ok((rest, arguments.into_iter().sum())),
            "-" if arguments.len() == 1 => Ok((rest, -arguments.into_iter().next().unwrap())),
            "-" if arguments.len() == 2 => {
                let mut args = arguments.into_iter();
                Ok((rest, args.next().unwrap() - args.next().unwrap()))
            }
            "*" => Ok((rest, arguments.into_iter().product())),
            "/" if arguments.len() == 2 => {
                let mut args = arguments.into_iter();
                Ok((rest, args.next().unwrap() / args.next().unwrap()))
            }
            _ => Err(error()),
        }
    }

    fn parse_literal<'a>(
        &mut self,
        l: &mut impl Iterator<Item = &'a str>,
    ) -> Result<Option<(Assignment, Option<ENodeIdx>)>> {
        let Some(lit) = l.next() else {
            return Ok(None);
        };
        let (lit, value) = match lit {
            // Have never seen this, but it is possible according to z3 source.
            "true" | "false" => return Err(E::BoolLiteral),
            "(not" => {
                let lit = l.next().ok_or(E::UnexpectedNewline)?;
                let lit = lit.strip_suffix(')').ok_or(E::TupleMissingParens)?;
                (lit, false)
            }
            _ => (lit, true),
        };
        let literal = self.parse_existing_app(lit)?;
        let enode = self.get_existing_enode(literal).ok();
        Ok(Some((Assignment { literal, value }, enode)))
    }

    /// When logging `assign` z3 prints some literals as their "bool var idx"
    /// rather than translating them through its `m_bool_var2expr` to print a
    /// "#id".
    ///
    /// These look like `p123` and `(not p123)` in <= v4.8.9, or `123`
    /// and `-123` in >= v4.8.10. Unfortunately we don't have the
    /// `m_bool_var2expr` map so can't translate these back to a `TermIdx`. The
    /// literal is rarely also `true` or `false` (for which this returns `None`)
    fn parse_bool_literal<'a>(
        version_info: &VersionInfo,
        l: &mut impl Iterator<Item = &'a str>,
    ) -> Result<Option<(Option<NonZeroU32>, bool)>> {
        let Some(lit) = l.next() else {
            return Ok(None);
        };
        match lit {
            "" => {
                debug_assert_eq!(l.next().filter(|n| !n.is_empty()), None);
                return Ok(None);
            }
            "true" => return Ok(Some((None, true))),
            "false" => return Ok(Some((None, false))),
            _ => (),
        };

        let new_mode = version_info.is_ge_version(4, 8, 10);
        let (lit, value) = if new_mode {
            let noneg = lit.strip_prefix('-');
            (noneg.unwrap_or(lit), noneg.is_none())
        } else {
            let (lit, value) = if lit == "(not" {
                let lit = l.next().ok_or(E::UnexpectedNewline)?;
                let lit = lit.strip_suffix(')').ok_or(E::TupleMissingParens)?;
                (lit, false)
            } else {
                (lit, true)
            };
            (lit.strip_prefix('p').ok_or(E::BoolLiteralNotP)?, value)
        };
        let bool_lit = lit.parse::<NonZeroU32>().map_err(E::InvalidBoolLiteral)?;
        Ok(Some((Some(bool_lit), value)))
    }
}

impl Z3LogParser for Z3Parser {
    fn newline(&mut self) {
        self.comm.newline();
    }

    fn version_info<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let solver = l.next().ok_or(E::UnexpectedNewline)?.to_string();
        let version = l.next().ok_or(E::UnexpectedNewline)?;
        // Return if there is unexpectedly more data
        Self::expect_completed(l)?;
        let version = semver::Version::parse(version)?;
        eprintln!("{solver} {version}");
        self.version_info = VersionInfo::Present { solver, version };
        Ok(())
    }

    fn mk_quant<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let full_id = self.parse_new_full_id(l.next())?;
        let (quant_name, num_vars) = self.parse_qid(&mut l)?;
        let quant_name = QuantKind::parse(&mut self.strings, &quant_name);
        let child_ids = Self::map(l, |id| self.parse_existing_app(id))?;
        debug_assert!(!child_ids.is_empty());
        let child_id_names = || {
            child_ids[..child_ids.len() - 1]
                .iter()
                .map(|&id| self[id].app_name().map(|name| &self[name]))
        };
        debug_assert!(
            child_id_names().all(|name| name.is_some_and(|name| name == "pattern")),
            "Expected all but last child to be \"pattern\" but found {:?}",
            child_id_names().collect::<Vec<_>>()
        );
        self.quant_or_lamda(full_id, child_ids, num_vars, quant_name)
    }

    fn mk_lambda<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let full_id = self.parse_new_full_id(l.next())?;
        let lambda = l.next().ok_or(E::UnexpectedNewline)?;
        self.check_lambda_name(lambda)?;
        let num_vars = l.next().ok_or(E::UnexpectedNewline)?;
        let num_vars = num_vars
            .parse::<NonMaxU32>()
            .map_err(E::InvalidQVarInteger)?;
        let child_ids = Self::map(l, |id| self.parse_existing_proof(id))?;
        let kind = QuantKind::Lambda(child_ids);
        self.quant_or_lamda(full_id, Default::default(), num_vars, kind)
    }

    fn mk_var<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let full_id = self.parse_new_full_id(l.next())?;
        let kind = l.next().ok_or(E::UnexpectedNewline)?;
        let kind = TermKind::parse_var(kind)?;
        // Return if there is unexpectedly more data
        Self::expect_completed(l)?;
        self.new_term(full_id, kind, Default::default())?;
        Ok(())
    }

    fn mk_app<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let id_str = l.next().ok_or(E::UnexpectedNewline)?;
        let mut id = TermId::parse(&mut self.strings, id_str)?;
        let (name, child) = self.parse_app_name(&mut l)?;
        debug_assert!(id_str != "#1" || name == "true");
        debug_assert!(id_str != "#2" || name == "false");
        self.terms
            .check_get_model(&mut id, &name, &mut self.strings);
        let name = self.mk_string(&name)?;
        let child = child.into_iter().map(Ok);
        let child_ids = child.chain(l.map(|id| TermId::parse(&mut self.strings, id)));
        let child_ids = Self::map(child_ids, |id| self.terms.parse_app_child_id(id?))?;
        let tidx = self.new_term(id, TermKind::App(name), child_ids)?;
        self.events
            .new_term(tidx, &self.terms[tidx], &self.strings)?;
        Ok(())
    }

    fn mk_proof<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let full_id = self.parse_new_full_id(l.next())?;
        let name = l.next().ok_or(E::UnexpectedNewline)?;
        let kind = match name.parse() {
            Ok(kind) => kind,
            Err(_) => {
                debug_assert!(false, "Unknown proof step kind {name:?}");
                ProofStepKind::OTHER(self.mk_string(name)?)
            }
        };
        let mut next = l.next().ok_or(E::UnexpectedNewline)?;
        let mut prerequisites = Vec::new();
        for n in l {
            let curr = next;
            next = n;

            let prereq = self.parse_existing_proof(curr)?;
            prerequisites.push(prereq);
        }
        let result = self.parse_existing_app(next)?;

        let proof_step = ProofStep {
            id: full_id,
            kind,
            result,
            prerequisites: prerequisites.into(),
            frame: self.stack.active_frame(),
        };
        let proof_idx = self.terms.proof_terms.new_term(proof_step)?;
        self.terms
            .new_proof(&mut self.quantifiers, proof_idx, &self.strings)?;
        self.egraph
            .new_proof(result, proof_idx, &self.terms, &self.stack)?;
        self.events.new_proof_step(
            proof_idx,
            &self.terms[proof_idx],
            &self.terms,
            &self.strings,
        )
    }

    fn attach_meaning<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let id = l.next().ok_or(E::UnexpectedNewline)?;
        let theory = l.next().ok_or(E::UnexpectedNewline)?;
        let value = l.collect::<Vec<_>>().join(" ");
        let meaning = match theory {
            "arith" => {
                let num = Self::parse_arith(&value)?;
                Meaning::Arith(Box::new(num))
            }
            "bv" => {
                let (value, bits) = if let Some(data) = value.strip_prefix("#x") {
                    let value = num::BigUint::from_str_radix(data, 16);
                    (value, data.len() as u32 * 4)
                } else if let Some(data) = value.strip_prefix("#b") {
                    let value = num::BigUint::from_str_radix(data, 2);
                    (value, data.len() as u32)
                } else {
                    return Err(E::ParseError(value));
                };
                let value = BitVec {
                    value: value.map_err(E::ParseBigUintError)?.into(),
                    bits,
                };
                Meaning::BitVec(Box::new(value))
            }
            theory => {
                let theory = self.mk_string(theory)?;
                let value = self.mk_string(&value)?;
                Meaning::Unknown { theory, value }
            }
        };
        let idx = self.parse_existing_app(id)?;
        let idx = self.terms.new_meaning(idx, meaning)?;
        let meaning = self.terms.meaning(idx).unwrap();
        self.events.new_meaning(idx, meaning, &self.strings)
    }

    fn attach_var_names<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let id = l.next().ok_or(E::UnexpectedNewline)?;
        let var_names = self.gobble_var_names_list(l)?;
        let tidx = self.parse_existing_app(id)?;
        let qidx = self.terms.quant(tidx)?;
        let quant = &mut self.quantifiers[qidx];
        debug_assert_eq!(quant.num_vars.get() as usize, var_names.len());
        debug_assert!(quant.vars.is_none());
        quant.vars = Some(var_names);
        Ok(())
    }

    fn attach_enode<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let id = l.next().ok_or(E::UnexpectedNewline)?;
        let idx = self.parse_existing_app(id);
        let Ok(idx) = idx else {
            if self.version_info.is_version(4, 8, 7) {
                // Z3 4.8.7 seems to have a bug where it can emit a non-existent term id here.
                return Ok(());
            } else {
                return idx.map(|_| ());
            }
        };
        let z3_gen = Self::parse_z3_generation(&mut l)?.ok_or(E::UnexpectedNewline)?;
        // Return if there is unexpectedly more data
        Self::expect_completed(l)?;

        debug_assert!(self[idx].app_name().is_some());
        let iidx = self.active_inst(idx)?;
        let blame = self.egraph.get_blame(idx, iidx, &self.terms, &self.stack);
        let enode = self.egraph.new_enode(blame, idx, z3_gen, &self.stack)?;
        self.events.new_enode();
        if let Some(a) = self.insts.active_inst() {
            // If `None` then this is a ground term not created by an instantiation.
            a.yields.try_reserve(1)?;
            a.yields.push(enode);
            let idx = a.idx;
            debug_assert!(self.insts.insts[idx]
                .kind
                .z3_generation()
                .is_none_or(|g| g == z3_gen));
        }
        Ok(())
    }

    fn eq_expl<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let from = self.parse_existing_enode(l.next().ok_or(E::UnexpectedNewline)?)?;
        let kind = l.next().ok_or(E::UnexpectedNewline)?;
        let eq_expl = {
            let mut kind_dependent_info = Self::iter_until_eq(l.by_ref(), ";");
            match kind {
                "root" => EqualityExpl::Root { id: from },
                "lit" => {
                    let eq = kind_dependent_info.next().ok_or(E::UnexpectedEnd)?;
                    let eq = self.parse_existing_enode(eq)?;
                    Self::expect_completed(kind_dependent_info)?;
                    let to = self.parse_existing_enode(l.next().ok_or(E::UnexpectedNewline)?)?;

                    let eq = self.lits.get_assign(self[eq].owner, &self.stack);
                    let eq = eq.ok_or(E::UnknownEqLit)?;
                    // The equality must have been assigned to true
                    // TODO: why is this not always true?
                    // debug_assert!(self[eq].term.value);
                    EqualityExpl::Literal { from, eq, to }
                }
                "cg" => {
                    let mut arg_eqs = Vec::new();
                    for t in Self::gobble_tuples::<true>(kind_dependent_info) {
                        let (from, to) = t?;
                        let from = self.parse_existing_enode(from)?;
                        let to = self.parse_existing_enode(to)?;
                        arg_eqs.push((from, to));
                    }
                    let to = self.parse_existing_enode(l.next().ok_or(E::UnexpectedNewline)?)?;
                    EqualityExpl::Congruence {
                        from,
                        arg_eqs: arg_eqs.into_boxed_slice(),
                        to,
                        uses: Vec::new(),
                    }
                }
                "th" => {
                    let theory = kind_dependent_info.next().ok_or(E::UnexpectedEnd)?;
                    let theory = TheoryKind::from_name(theory, &mut self.strings);
                    Self::expect_completed(kind_dependent_info)?;
                    let to = self.parse_existing_enode(l.next().ok_or(E::UnexpectedNewline)?)?;
                    EqualityExpl::Theory { from, theory, to }
                }
                "ax" => {
                    Self::expect_completed(kind_dependent_info)?;
                    let to = self.parse_existing_enode(l.next().ok_or(E::UnexpectedNewline)?)?;
                    EqualityExpl::Axiom { from, to }
                }
                kind => {
                    let args = kind_dependent_info
                        .map(|s| self.mk_string(s))
                        .collect::<Result<_>>()?;
                    let to = self.parse_existing_enode(l.next().ok_or(E::UnexpectedNewline)?)?;
                    EqualityExpl::Unknown {
                        kind: self.mk_string(kind)?,
                        from,
                        args,
                        to,
                    }
                }
            }
        };
        // Return if there is unexpectedly more data
        Self::expect_completed(l)?;

        self.egraph.new_given_equality(from, eq_expl, &self.stack)?;
        Ok(())
    }

    fn new_match<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let fingerprint = l.next().ok_or(E::UnexpectedNewline)?;
        let fingerprint = Fingerprint::parse(fingerprint)?;
        let idx = l.next().ok_or(E::UnexpectedNewline)?;
        let idx = self.parse_existing_app(idx)?;
        let quant = self.terms.quant(idx)?;
        let pattern = l.next().ok_or(E::UnexpectedNewline)?;
        let pattern = self.parse_existing_app(pattern)?;
        let pattern = self
            .patterns(quant)
            .ok_or(E::NewMatchOnLambda(quant))?
            .position(|p| *p == pattern)
            .ok_or(E::UnknownPatternIdx(pattern))?;
        let bound_terms = Self::iter_until_eq(&mut l, ";");
        let is_axiom = fingerprint.is_zero();

        let kind = if is_axiom {
            let bound_terms = bound_terms
                .map(|id| self.parse_existing_app(id))
                .collect::<Result<BoxSlice<_>>>()?;
            debug_assert!(bound_terms
                .iter()
                .all(|&b| !self[b].has_var().unwrap_or(true)));
            MatchKind::Axiom {
                axiom: quant,
                pattern,
                bound_terms,
            }
        } else {
            let bound_terms = bound_terms
                .map(|id| self.parse_existing_enode(id))
                .collect::<Result<BoxSlice<_>>>()?;
            debug_assert!(bound_terms
                .iter()
                .all(|&b| !self[self[b].owner].has_var().unwrap_or(true)));
            MatchKind::Quantifier {
                quant,
                pattern,
                bound_terms,
            }
        };
        let mut blamed = Vec::new();
        let mut next = l.next();
        while let Some(word) = next.take() {
            let enode = self.parse_existing_enode(word)?;
            // `append_trans_equality_tuples` would gobble the next term otherwise, so capture it instead.
            let l = l.by_ref().take_while(|s| {
                let cond = s.as_bytes().first().is_some_and(|f| *f == b'(')
                    || s.as_bytes().last().is_some_and(|l| *l == b')');
                if !cond {
                    next = Some(*s);
                }
                cond
            });
            let mut equalities = Vec::new();
            self.append_trans_equality_tuples(l, |eq| {
                equalities.try_reserve(1)?;
                equalities.push(eq);
                Ok(())
            })?;

            blamed.try_reserve(1)?;
            blamed.push((blamed.len(), enode, equalities));
        }

        let pat = kind.quant_pat().unwrap();
        let pat = self.get_pattern(pat).unwrap();
        let (_correct_order, blamed) = self.make_blamed(&kind, blamed, pat)?;

        let match_ = Match {
            kind,
            blamed,
            frame: self.stack.active_frame(),
        };
        debug_assert_eq!(match_.pattern_matches().count(), self[pat].child_ids.len());
        debug_assert!(match_
            .pattern_matches()
            .zip(self[pat].child_ids.iter())
            .all(|(m, s)| self.check_match(&match_.kind, m, *s)));
        self.insts.new_match(fingerprint, match_)?;
        Ok(())
    }

    fn inst_discovered<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let method = l.next().ok_or(E::UnexpectedNewline)?;
        let fingerprint = Fingerprint::parse(l.next().ok_or(E::UnexpectedNewline)?)?;

        let (kind, blamed) = match method {
            "theory-solving" => {
                debug_assert!(
                    fingerprint.is_zero(),
                    "Theory solving should have zero fingerprint"
                );
                let axiom_id = l.next().ok_or(E::UnexpectedNewline)?;
                let axiom_id = TermId::parse(&mut self.strings, axiom_id)?;

                let bound_terms = Self::iter_until_eq(&mut l, ";")
                    .map(|id| self.parse_existing_app(id))
                    .collect::<Result<_>>()?;

                let mut blamed = Vec::new();
                let mut rewrite_of = None;
                for word in l.by_ref() {
                    let term = self.parse_existing_app(word)?;
                    if let Ok(enode) = self.get_existing_enode(term) {
                        if let Some(rewrite_of) = rewrite_of {
                            return Err(E::NonRewriteAxiomInvalidEnode(rewrite_of));
                        }
                        blamed.try_reserve(1)?;
                        // TODO: are the enodes in the correct order here?
                        blamed.push(Blame {
                            coupling: Coupling::Exact,
                            enode,
                            equalities: Default::default(),
                        });
                    } else {
                        if let Some(rewrite_of) = rewrite_of {
                            return Err(E::RewriteAxiomMultipleTerms1(rewrite_of));
                        }
                        if !blamed.is_empty() {
                            return Err(E::RewriteAxiomMultipleTerms2(blamed));
                        }
                        rewrite_of = Some(term);
                    }
                }

                let kind = MatchKind::TheorySolving {
                    axiom_id,
                    bound_terms,
                    rewrite_of,
                };
                (kind, blamed)
            }
            "MBQI" => {
                let quant = self.parse_existing_app(l.next().ok_or(E::UnexpectedNewline)?)?;
                let quant = self.terms.quant(quant)?;
                let bound_terms = l
                    .map(|id| self.parse_existing_enode(id))
                    .collect::<Result<_>>()?;
                let kind = MatchKind::MBQI { quant, bound_terms };
                (kind, Vec::new())
            }
            _ => return Err(E::UnknownInstMethod(method.to_string())),
        };
        let match_ = Match {
            kind,
            blamed: blamed.into(),
            frame: self.stack.active_frame(),
        };
        self.insts.new_match(fingerprint, match_)?;
        Ok(())
    }

    fn instance<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let fingerprint = l.next().ok_or(E::UnexpectedNewline)?;
        let fingerprint = Fingerprint::parse(fingerprint)?;
        let mut proof = Self::iter_until_eq(&mut l, ";");
        let proof_str = proof.next();
        Self::expect_completed(proof)?;
        let z3_generation = Self::parse_z3_generation(&mut l)?;
        let kind = if let Some(z3_generation) = z3_generation {
            debug_assert!(!fingerprint.is_zero());
            let proof = if let Some(proof) = proof_str {
                let proof = self.parse_existing_proof(proof)?;
                InstProofLink::HasProof(proof)
            } else {
                let last_term = self.terms.last_term_from_instance(&self.strings);
                InstProofLink::ProofsDisabled(last_term)
            };
            InstantiationKind::NonAxiom {
                fingerprint,
                z3_generation,
                proof,
            }
        } else {
            debug_assert!(fingerprint.is_zero());
            // It seems that for `0x0` fingerprints the proof term points to an
            // app term (usually an equality), while for "real" fingerprints it
            // points to a proof term.
            let proof = proof_str.expect("Axiom instantiations should have an associated term");
            let body = self.parse_existing_app(proof)?;
            InstantiationKind::Axiom { body }
        };
        let match_ = self
            .insts
            .get_match(fingerprint)
            .ok_or(E::UnknownFingerprint(fingerprint))?;
        let inst = Instantiation {
            match_,
            kind,
            yields_terms: Default::default(),
            frame: self.stack.active_frame(),
        };
        let body = self.instance_body(kind.proof(), match_);
        // I have very rarely seen duplicate `[instance]` lines with the same
        // fingerprint in >= v4.12.2. Allow these there and debug panic otherwise.
        let can_duplicate = self.version_info.is_ge_version(4, 12, 0);
        self.insts
            .new_inst(fingerprint, inst, body, &self.stack, can_duplicate)?;
        self.events.new_inst();
        Ok(())
    }

    fn end_of_instance<'a>(&mut self, l: impl Iterator<Item = &'a str>) -> Result<()> {
        self.insts.end_inst()?;
        Self::expect_completed(l)
    }

    fn eof(&mut self) {
        self.synth_terms.eof(self.terms().next_key());
        self.events.new_eof();
    }

    fn assign<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let created_by = self.insts.active_inst();
        let iidx = created_by.as_ref().map(|a| a.idx);

        let (assign, enode) = self.parse_literal(&mut l)?.ok_or(E::UnexpectedNewline)?;
        let lit = self.lits.new_literal(assign, iidx, enode, &self.stack)?;
        let mut justification = l.next().ok_or(E::UnexpectedNewline)?;
        let decision = justification == "decision";
        if decision {
            let llk = self.comm.prev().last_line_kind;
            // TODO: the bitvector-only solver in z3 doesn't use push/pop
            // debug_assert_eq!(llk, LineKind::Push);
            if matches!(llk, LineKind::Push) && self.stack.new_decision() {
                self.push_count -= 1;
                self.events.undo_push();
            }
            self.lits.cdcl.new_decision(lit, &self.stack)?;
            justification = l.next().ok_or(E::UnexpectedNewline)?;
            debug_assert_eq!(justification, "axiom");
        }
        // Now `l` contains
        match justification {
            // Either a "decision" or a non-interesting assignment by e.g.
            // internal z3 axioms.
            "axiom" => {
                Self::expect_completed(l)?;
                let kind = if decision {
                    JustificationKind::Decision
                } else {
                    JustificationKind::Axiom
                };
                self.lits.justification(lit, kind, core::iter::empty())?;
                Ok(())
            }
            // Same as clause, but binary?
            "bin" => {
                let other = Self::parse_bool_literal(&self.version_info, &mut l)?
                    .ok_or(E::UnexpectedNewline)?;
                Self::expect_completed(l)?;

                let _j = self.lits.justification(
                    lit,
                    JustificationKind::Propagate,
                    [Ok(other)].into_iter(),
                )?;
                // Issue 3: No way to convert LitId -> TermIdx
                #[cfg(any())]
                debug_assert_ne!(lit, j.blamed[0]);
                self.lits.cdcl.new_propagate(lit, &self.stack)?;
                Ok(())
            }
            // A propagated assignment: a clause only has one unassigned literal
            // left. The offending clause is also printed but since it's in
            // bool-var format we can't use it. Also <= v4.8.8 prints the text
            // name of each clause on subsequent newlines, we'll ignore those.
            // Later versions of z3 use `display_compact_j`.
            "clause" => {
                let _first = Self::parse_bool_literal(&self.version_info, &mut l)?
                    .ok_or(E::UnexpectedNewline)?;
                // All the other literals in the clause (which have already been assigned)
                let lits = core::iter::from_fn(|| {
                    Self::parse_bool_literal(&self.version_info, &mut l).transpose()
                });
                self.lits
                    .justification(lit, JustificationKind::Propagate, lits)?;

                // Issue 3: No way to convert LitId -> TermIdx
                #[cfg(any())]
                let c0 = self.lits.literal_to_term(first, false)?;
                #[cfg(any())]
                if lit != c0 {
                    eprintln!(
                        "Unexpected clause literal: {lit} != {c0} ({first:?}) / {:?}",
                        self.lits.lit_stack
                    );
                    return Err(E::UnexpectedEnd);
                }
                #[cfg(any())]
                debug_assert_eq!(lit, c0, "{first:?}");
                self.lits.cdcl.new_propagate(lit, &self.stack)?;
                Ok(())
            }
            "justification" => {
                let theory_id = l.next().ok_or(E::UnexpectedNewline)?;
                let theory_id = theory_id
                    .strip_suffix(':')
                    .ok_or(E::MissingColonJustification)?;
                let theory_id = theory_id.parse::<i32>().map_err(E::InvalidTheoryId)?;
                let theory = TheoryKind::from_id(theory_id);

                let lits = core::iter::from_fn(|| {
                    Self::parse_bool_literal(&self.version_info, &mut l).transpose()
                });
                self.lits
                    .justification(lit, JustificationKind::Theory(theory), lits)?;
                Ok(())
            }
            _ => Err(E::UnknownJustification(justification.to_string())),
        }
    }

    fn decide_and_or<'a>(&mut self, _l: impl Iterator<Item = &'a str>) -> Result<()> {
        self.comm.curr().last_line_kind = LineKind::DecideAndOr;
        Ok(())
    }

    fn conflict<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        self.comm.curr().last_line_kind = LineKind::Conflict;

        let mut cut = Vec::new();
        while let Some((assignment, _)) = self.parse_literal(&mut l)? {
            cut.try_reserve(1)?;
            cut.push(assignment);
        }
        let frame = self.stack.active_frame();
        debug_assert!(self.lits.curr_to_root_unique());
        self.lits.cdcl.new_conflict(cut.into_boxed_slice(), frame);
        // Backtracking will happen with the pop in the next line. We'll push
        // the new (opposite) decision there.
        Ok(())
    }

    fn push<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        self.comm.curr().last_line_kind = LineKind::Push;

        let scope = l.next().ok_or(E::UnexpectedNewline)?;
        let scope = scope.parse::<usize>().map_err(E::InvalidFrameInteger)?;
        // Return if there is unexpectedly more data
        Self::expect_completed(l)?;

        let from_cdcl = matches!(self.comm.prev().last_line_kind, LineKind::DecideAndOr);
        let from_cdcl = from_cdcl || self.stack.is_speculative();
        self.stack.new_frame(scope, from_cdcl)?;
        if !from_cdcl {
            self.push_count += 1;
        }
        self.events.new_push(from_cdcl)?;
        Ok(())
    }

    fn pop<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let num = l.next().ok_or(E::UnexpectedNewline)?;
        let num = num
            .parse::<NonZeroUsize>()
            .map_err(E::InvalidFrameInteger)?;
        let scope = l.next().ok_or(E::UnexpectedNewline)?;
        let scope = scope.parse::<usize>().map_err(E::InvalidFrameInteger)?;
        // Return if there is unexpectedly more data
        Self::expect_completed(l)?;

        let conflict = matches!(self.comm.prev().last_line_kind, LineKind::Conflict);
        debug_assert_eq!(conflict, self.lits.cdcl.has_conflict());
        let from_cdcl = self.stack.pop_frames(num, scope, conflict)?;
        // I think `from_cdcl && !conflict` => end of a check-sat
        if conflict {
            self.lits.cdcl.backtrack(&self.stack)?;
        }
        self.events.new_pop(num, from_cdcl)
    }

    fn begin_check<'a>(&mut self, mut l: impl Iterator<Item = &'a str>) -> Result<()> {
        let scope = l.next().ok_or(E::UnexpectedNewline)?;
        let scope = scope.parse::<usize>().map_err(E::InvalidFrameInteger)?;
        self.stack.ensure_height(scope)?;
        self.lits
            .cdcl
            .begin_check(self.incremental_mode(), &self.stack)?;
        self.events.new_begin_check()
    }
}