amzn-smt-ir 0.1.0

Intermediate representation for SMT problems
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
//! Traits for transforming [`Term`]s into other terms.
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use crate::{
    Command, CoreOp, Ctx, FunctionDec, IConst, ICoreOp, IIndex, ILet, IMatch, IOp, IQuantifier,
    ISort, ISymbol, IVar, Let, Logic, Quantifier, Sorted, Term, Void, IUF, UF,
};
pub use amzn_smt_ir_derive::Fold;

mod compose;
pub use compose::{Compose, TryComposed};
mod intra;
pub use intra::IntraLogicFolder;
mod inter;
pub use inter::InterLogicFolder;
use smallvec::SmallVec;
use smt2parser::Numeral;

/// A `Folder<T>` is a type that can be used to transform SMT terms in logic `T` to some other type.
/// For example, a folder could be used to partially evaluate a term or encode it into a simpler
/// logic (e.g. bit-blasting bit-vectors). Useful folders will often preserve equisatisfiability,
/// although this is not required.
///
/// **Note: When writing code generic over folders, be generic over `Folder<T, M>` instead of
/// `Folder<T>`, as the latter only includes types that directly implement `Folder`. See
/// [Emulating Specialization](#emulating-specialization) below for details.
///
/// [`IConst`], [`IVar`], [`CoreOp`], and [`IOp`] implement [`Fold`]; when [`Fold::fold_with`] is called
/// with a folder, it will recursively fold all of the subexpressions contained within. For types
/// that have corresponding hooks in [`Folder`] (e.g. [`Folder::fold_var`], [`Folder::fold_core_op`]),
/// [`Fold::fold_with`] will call the hook, which should then recursively fold subexpressions -- this
/// can be accomplished through a call to [`SuperFold::super_fold_with`].
///
/// # Emulating specialization
///
/// The [`IntraLogicFolder`] and [`InterLogicFolder`] traits are more specialized versions of
/// [`Folder`], providing default implementations for more `fold_*` hooks where possible. These
/// traits implement `Folder` through a blanket impl, which would produce conflicting trait
/// impls because [specialization](https://rust-lang.github.io/rfcs/1210-impl-specialization.html)
/// is not implemented. To work around this until some form of specialization lands, `Folder` has
/// a parameter `M` that acts as a *marker* of which `Folder`-family trait was directly implemented.
/// Types that directly implement `Folder` have a marker of `()`, while types that indirectly
/// implement it through a more specialized trait use marker types corresponding to that trait.
/// This makes the blanket impls produce distinct implementations `Folder<_, A>`, `Folder<_, B>`, etc.
///
/// # Examples
///
/// ### Partially evaluating `not` operations
///
/// ```
/// # fn main() {
/// use amzn_smt_ir::fold::{Fold, Folder, SuperFold};
/// use amzn_smt_ir::{CoreOp, ICoreOp, IVar, IUF, IOp, Term, ILet, IMatch, Logic, Void, IConst, IQuantifier};
///
/// struct PartiallyEvaluateNot;
///
/// impl<T: Logic> Folder<T> for PartiallyEvaluateNot {
///     type Output = Term<T>;
///     type Error = ();
///
///     fn fold_const(&mut self, constant: IConst) -> Result<Self::Output, Self::Error> {
///         Ok(constant.into())
///     }
///
///     fn fold_var(&mut self, var: IVar<T::Var>) -> Result<Self::Output, Self::Error> {
///         Ok(var.into())
///     }
///
///     fn fold_core_op(&mut self, op: ICoreOp<T>) -> Result<Self::Output, Self::Error> {
///         // Recursively fold arguments, then check if there's a `not` we can evaluate
///         Ok(match op.super_fold_with(self)? {
///             CoreOp::Not(Term::CoreOp(op)) if matches!(*op, CoreOp::True) => false.into(),
///             CoreOp::Not(Term::CoreOp(op)) if matches!(*op, CoreOp::False) => true.into(),
///             op => op.into(),
///         })
///     }
///
///     fn fold_theory_op(&mut self, op: IOp<T>) -> Result<Self::Output, Self::Error> {
///         op.super_fold_with(self).map(Into::into)
///     }
///
///     fn fold_uninterpreted_func(&mut self, uf: IUF<T>) -> Result<Self::Output, Self::Error> {
///         uf.super_fold_with(self).map(Into::into)
///     }
///
///     fn fold_let(&mut self, l: ILet<T>) -> Result<Self::Output, Self::Error> {
///         l.super_fold_with(self).map(Into::into)
///     }
///
///     fn fold_match(&mut self, m: IMatch<T>) -> Result<Self::Output, Self::Error> {
///         m.super_fold_with(self).map(Into::into)
///     }
///
///     fn fold_quantifier(&mut self, quantifier: IQuantifier<T>) -> Result<Self::Output, Self::Error> {
///         quantifier.super_fold_with(self).map(Into::into)
///     }
/// }
///
/// let f1: Term = !Term::CoreOp(true.into());
/// let f2 = f1.clone() & !Term::CoreOp(false.into());
/// let mut folder = PartiallyEvaluateNot;
/// assert_eq!(f1.fold_with(&mut folder), Ok(false.into()));
/// assert_eq!(f2.fold_with(&mut folder), Ok(CoreOp::And([false.into(), true.into()].into()).into()));
/// # }
/// ```
pub trait Folder<T: Logic, M = ()>: Compose {
    /// Type produced by the transformation.
    /// If `Output = Term<T>`, implement [`IntraLogicFolder`] instead.
    /// If `Output = Term<U>` for some `U: Logic`, implement [`InterLogicFolder`] instead.
    type Output;

    /// Type of errors that the folder may run into.
    type Error;

    /// Gets the context tracked by the folder, if applicable.
    /// Tracking context enables things like determining the sorts of expressions and which
    /// variables are free/bound, but a folder that doesn't need that information can leave
    /// this method defaulted, which will return `None`.
    fn context(&self) -> Option<&Ctx> {
        None
    }

    /// Gets a mutable reference to the context tracked by the folder, if applicable.
    /// Tracking context enables things like determining the sorts of expressions and which
    /// variables are free/bound, but a folder that doesn't need that information can leave
    /// this method defaulted, which will return `None`.
    fn context_mut(&mut self) -> Option<&mut Ctx> {
        debug_assert!(
            self.context().is_none(),
            "context() and context_mut() should be implemented together"
        );
        None
    }

    /// Transforms a term, recursively transforming its contents.
    /// By default, calls [`SuperFold::super_fold_with`].
    fn fold_term(&mut self, term: Term<T>) -> Result<Self::Output, Self::Error> {
        term.super_fold_with(self)
    }

    /// Transforms a constant.
    fn fold_const(&mut self, constant: IConst) -> Result<Self::Output, Self::Error>;

    /// Transforms a variable.
    fn fold_var(&mut self, var: IVar<T::Var>) -> Result<Self::Output, Self::Error>;

    /// Transforms an SMT-LIB Core operation, recursively folding its arguments.
    fn fold_core_op(&mut self, op: ICoreOp<T>) -> Result<Self::Output, Self::Error>;

    /// Transforms a non-core operation, recursively folding its arguments.
    fn fold_theory_op(&mut self, op: IOp<T>) -> Result<Self::Output, Self::Error>;

    /// Transforms an uninterpreted function, recursively folding its arguments.
    fn fold_uninterpreted_func(&mut self, func: IUF<T>) -> Result<Self::Output, Self::Error>;

    /// Transforms a let binder, recursively folding its arguments.
    fn fold_let(&mut self, l: ILet<T>) -> Result<Self::Output, Self::Error>;

    /// Transforms a match binder, recursively folding its arguments.
    fn fold_match(&mut self, m: IMatch<T>) -> Result<Self::Output, Self::Error>;

    /// Transforms a quantifier, recursively folding its arguments.
    fn fold_quantifier(&mut self, quantifier: IQuantifier<T>) -> Result<Self::Output, Self::Error>;

    /// Transforms a set-logic command.
    /// By default, returns as-is.
    fn fold_set_logic(&mut self, logic: ISymbol) -> Result<Command<Self::Output>, Self::Error> {
        Ok(Command::SetLogic { symbol: logic })
    }

    /// Transforms an asserted term.
    /// By default, calls [`Self::fold_term`].
    fn fold_assert(&mut self, asserted: Term<T>) -> Result<Command<Self::Output>, Self::Error> {
        self.fold_term(asserted)
            .map(|term| Command::Assert { term })
    }

    /// Transforms a constant declaration.
    /// By default, returns as-is.
    fn fold_declare_const(
        &mut self,
        symbol: ISymbol,
        sort: ISort,
    ) -> Result<Command<Self::Output>, Self::Error> {
        Ok(Command::DeclareConst { symbol, sort })
    }

    /// Transforms a function declaration.
    /// By default, returns as-is.
    fn fold_declare_fun(
        &mut self,
        symbol: ISymbol,
        parameters: Vec<ISort>,
        sort: ISort,
    ) -> Result<Command<Self::Output>, Self::Error> {
        Ok(Command::DeclareFun {
            symbol,
            parameters,
            sort,
        })
    }

    /// Transforms a sort declaration.
    /// By default, returns as-is.
    fn fold_declare_sort(
        &mut self,
        symbol: ISymbol,
        arity: Numeral,
    ) -> Result<Command<Self::Output>, Self::Error> {
        Ok(Command::DeclareSort { symbol, arity })
    }

    /// Transforms a function definition.
    /// By default, recursively transforms the function body with [`Self::fold_term`].
    fn fold_define_fun(
        &mut self,
        sig: FunctionDec,
        body: Term<T>,
    ) -> Result<Command<Self::Output>, Self::Error> {
        let term = body.fold_with(self)?;
        Ok(Command::DefineFun { sig, term })
    }

    /// Transforms a recursive function definition.
    /// By default, recursively transforms the function body with [`Self::fold_term`].
    fn fold_define_fun_rec(
        &mut self,
        sig: FunctionDec,
        body: Term<T>,
    ) -> Result<Command<Self::Output>, Self::Error> {
        let term = body.fold_with(self)?;
        Ok(Command::DefineFunRec { sig, term })
    }

    /// Transforms a set of recursive function definition.
    /// By default, recursively transforms each function body with [`Self::fold_term`].
    fn fold_define_funs_rec(
        &mut self,
        funs: Vec<(FunctionDec, Term<T>)>,
    ) -> Result<Command<Self::Output>, Self::Error> {
        let funs = funs
            .into_iter()
            .map(|(sig, body)| Ok((sig, body.fold_with(self)?)))
            .collect::<Result<_, _>>()?;
        Ok(Command::DefineFunsRec { funs })
    }

    /// Transforms a sort definition.
    /// By default, returns as-is.
    fn fold_define_sort(
        &mut self,
        symbol: ISymbol,
        parameters: Vec<ISymbol>,
        sort: ISort,
    ) -> Result<Command<Self::Output>, Self::Error> {
        Ok(Command::DefineSort {
            symbol,
            parameters,
            sort,
        })
    }

    /// Transforms a `get-value` call.
    /// By default, recursively transforms each term with [`Self::fold_term`].
    fn fold_get_value(
        &mut self,
        terms: Vec<Term<T>>,
    ) -> Result<Command<Self::Output>, Self::Error> {
        let terms = terms.fold_with(self)?;
        Ok(Command::GetValue { terms })
    }
}

/// `Fold` implements the logic to recursively fold the contents of a type using a folder (see [`Folder`]).
/// This should usually not be implemented manually; in particular, types used as operations
/// (i.e. the `T::Op` of a `T: Logic`) should derive `Fold` using the [`Fold`] derive macro.
///
/// # Examples
///
/// ## Deriving `Fold` for a simple enum.
///
/// ```
/// # fn main() {
/// use amzn_smt_ir::{fold::Fold, Term, Logic, Operation};
///
/// #[derive(Operation, Fold)]
/// enum AddOrSubtract<Term> {
///     Add(Vec<Term>),
///     Subtract(Term, Term),
/// }
/// # }
/// ```
pub trait Fold<T: Logic, Out> {
    /// Type output by the transformation.
    type Output;

    /// Transforms `self` using `folder`.
    ///
    /// If type inference is failing because it can't figure out which `T` to use, use
    /// [`Logic::fold`] to resolve the ambiguity.
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>;
}

/// `SuperFold` implements the logic to recursively fold the contents of types that have hooks in
/// [`Folder`] using a folder (see [`Folder`]).
/// This should usually not be implemented manually; in particular, types used as operations
/// (i.e. the `T::Op` of a `T: Logic`) should derive [`SuperFold`] using the [`Fold`] derive macro.
pub trait SuperFold<T: Logic, Out> {
    /// Type output by the transformation, e.g. `Foo<Out>` for an implementation for `Foo<U: Fold<T, Out>>`.
    type Output;

    /// Recursively transforms the contents of `self` using `folder.`
    fn super_fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>;
}

impl<T: Logic, Out> Fold<T, Out> for Term<T> {
    type Output = Out;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        folder.fold_term(self)
    }
}

impl<T: Logic, Out> Fold<T, Out> for &Term<T> {
    type Output = Out;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        folder.fold_term(self.clone())
    }
}

impl<T: Logic, Out> SuperFold<T, Out> for Term<T> {
    type Output = Out;

    fn super_fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        match self {
            Self::Constant(c) => c.fold_with(folder),
            Self::Variable(v) => v.fold_with(folder),
            Self::CoreOp(op) => op.fold_with(folder),
            Self::OtherOp(op) => op.fold_with(folder),
            Self::UF(uf) => uf.fold_with(folder),
            Self::Let(l) => l.fold_with(folder),
            Self::Match(m) => m.fold_with(folder),
            Self::Quantifier(q) => q.fold_with(folder),
        }
    }
}

impl<T: Logic, Out> SuperFold<T, Out> for &Term<T> {
    type Output = Out;

    fn super_fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        self.clone().super_fold_with(folder)
    }
}

impl<T: Logic, Out> Fold<T, Out> for IConst {
    type Output = Out;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        folder.fold_const(self)
    }
}

impl<T: Logic, Out> Fold<T, Out> for IVar<T::Var> {
    type Output = Out;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        folder.fold_var(self)
    }
}

// TODO: extend `Fold` derive to work for `CoreOp`
impl<T: Logic, Out> Fold<T, Out> for ICoreOp<T> {
    type Output = Out;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        folder.fold_core_op(self)
    }
}

// TODO: extend `Fold` derive to work for `CoreOp`
impl<T: Logic, Inner: Fold<T, Out>, Out> SuperFold<T, Out> for CoreOp<Inner> {
    type Output = CoreOp<Inner::Output>;
    fn super_fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        use CoreOp::*;
        const STACK_RED_ZONE: usize = 32 * 1024;
        const NEW_STACK_SIZE: usize = 1024 * 1024;
        let op = match self {
            True => True,
            False => False,
            Not(t) => Not(t.fold_with(folder)?),
            And(args) => And(stacker::maybe_grow(STACK_RED_ZONE, NEW_STACK_SIZE, || {
                args.fold_with(folder)
            })?),
            Or(args) => Or(stacker::maybe_grow(STACK_RED_ZONE, NEW_STACK_SIZE, || {
                args.fold_with(folder)
            })?),
            Xor(args) => Xor(args.fold_with(folder)?),
            Imp(args) => Imp(args.fold_with(folder)?),
            Eq(args) => Eq(args.fold_with(folder)?),
            Distinct(args) => Distinct(args.fold_with(folder)?),
            Ite(i, t, e) => Ite(
                i.fold_with(folder)?,
                t.fold_with(folder)?,
                e.fold_with(folder)?,
            ),
        };
        Ok(op)
    }
}

impl<L: Logic, Out> Fold<L, Out> for IOp<L> {
    type Output = Out;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<L, M, Output = Out>,
    {
        folder.fold_theory_op(self)
    }
}

impl<T: Logic, Out> Fold<T, Out> for IUF<T> {
    type Output = Out;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        folder.fold_uninterpreted_func(self)
    }
}

impl<T: Logic, Inner, Out> SuperFold<T, Out> for UF<Inner>
where
    Inner: Fold<T, Out>,
{
    type Output = UF<Inner::Output>;
    fn super_fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        let (func, args) = (self.func.fold_with(folder)?, self.args.fold_with(folder)?);
        Ok(UF { func, args })
    }
}

impl<T: Logic, Out> Fold<T, Out> for IQuantifier<T> {
    type Output = Out;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        folder.fold_quantifier(self)
    }
}

impl<T: Logic, Inner, Out> SuperFold<T, Out> for Quantifier<Inner>
where
    Inner: Fold<T, Out>,
{
    type Output = Quantifier<Inner::Output>;
    fn super_fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        use Quantifier::*;
        Ok(match self {
            Forall(vars, t) => Forall(vars.fold_with(folder)?, t.fold_with(folder)?),
            Exists(vars, t) => Exists(vars.fold_with(folder)?, t.fold_with(folder)?),
        })
    }
}

impl<T: Logic, Out> Fold<T, Out> for ILet<T> {
    type Output = Out;

    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        let old_bindings = if let Some(ctx) = folder.context_mut() {
            self.bindings
                .iter()
                .map(|(sym, t)| {
                    let sort = t.sort(ctx).ok();
                    let old_sort = ctx.local.bind_var(sym.clone(), sort);
                    (sym.clone(), old_sort)
                })
                .collect()
        } else {
            vec![]
        };
        let res = folder.fold_let(self);
        if let Some(ctx) = folder.context_mut() {
            for (sym, old_sort) in old_bindings {
                match old_sort {
                    None => ctx.local.unbind_var(&sym),
                    Some(sort) => {
                        ctx.local.bind_var(sym, sort);
                    }
                }
            }
        }
        res
    }
}

impl<T: Logic, Inner: Fold<T, Out>, Out> SuperFold<T, Out> for Let<Inner> {
    type Output = Let<Inner::Output>;

    fn super_fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        Ok(Let {
            bindings: self.bindings.fold_with(folder)?,
            term: self.term.fold_with(folder)?,
        })
    }
}

impl<T: Logic, Out> Fold<T, Out> for Void {
    type Output = Out;
    fn fold_with<F: Folder<T, M>, M>(self, _folder: &mut F) -> Result<Self::Output, F::Error> {
        match self {}
    }
}

impl<T: Logic, Out> SuperFold<T, Out> for Void {
    type Output = Void;
    fn super_fold_with<F: Folder<T, M>, M>(
        self,
        _folder: &mut F,
    ) -> Result<Self::Output, F::Error> {
        match self {}
    }
}

impl<T: Logic, Out> Fold<T, Out> for ISymbol {
    type Output = Self;
    fn fold_with<F, M>(self, _: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        Ok(self)
    }
}

impl<T: Logic, Out> Fold<T, Out> for ISort {
    type Output = Self;
    fn fold_with<F, M>(self, _: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        Ok(self)
    }
}

impl<T: Logic, Out, Inner: Fold<T, Out>> Fold<T, Out> for Vec<Inner> {
    type Output = Vec<<Inner as Fold<T, Out>>::Output>;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        self.into_iter().map(|t| t.fold_with(folder)).collect()
    }
}

impl<T: Logic, Out, Inner: Fold<T, Out>, const N: usize> Fold<T, Out> for SmallVec<[Inner; N]>
where
    [Inner; N]: smallvec::Array<Item = Inner>,
    [Inner::Output; N]: smallvec::Array<Item = Inner::Output>,
{
    type Output = SmallVec<[Inner::Output; N]>;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        self.into_iter().map(|t| t.fold_with(folder)).collect()
    }
}

impl<'a, T: Logic, Out, Inner, Output, const N: usize> Fold<T, Out> for &'a SmallVec<[Inner; N]>
where
    &'a Inner: Fold<T, Out, Output = Output>,
    [Inner; N]: smallvec::Array<Item = Inner>,
    [Output; N]: smallvec::Array<Item = Output>,
{
    type Output = SmallVec<[Output; N]>;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        self.iter().map(|t| t.fold_with(folder)).collect()
    }
}

impl<T: Logic, Out, Inner: Fold<T, Out>> Fold<T, Out> for Box<[Inner]> {
    type Output = Box<[<Inner as Fold<T, Out>>::Output]>;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        self.into_vec()
            .into_iter()
            .map(|t| t.fold_with(folder))
            .collect()
    }
}

impl<T: Logic, Out, const N: usize> Fold<T, Out> for [IIndex; N] {
    type Output = Self;
    fn fold_with<F, M>(self, _: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        Ok(self)
    }
}

impl<T: Logic, Out, Inner, Output> Fold<T, Out> for &[Inner]
where
    for<'a> &'a Inner: Fold<T, Out, Output = Output>,
{
    type Output = Vec<Output>;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        self.iter().map(|t| t.fold_with(folder)).collect()
    }
}

impl<T: Logic, Out, Inner, Output> Fold<T, Out> for &Vec<Inner>
where
    for<'a> &'a Inner: Fold<T, Out, Output = Output>,
{
    type Output = Vec<Output>;
    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<T, M, Output = Out>,
    {
        self.iter().map(|t| t.fold_with(folder)).collect()
    }
}

macro_rules! impl_fold_tuple {
    ($($x:ident),*) => {
        impl<T: Logic, Out, $($x),*> Fold<T, Out> for ($($x),*)
        where
            $($x: Fold<T, Out>),*
        {
            type Output = ($($x::Output),*);
            fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
            where
                F: Folder<T, M, Output = Out>,
            {
                #[allow(non_snake_case)]
                let ($($x),*) = self;
                Ok(($($x.fold_with(folder)?),*))
            }
        }
    };
}

impl_fold_tuple!(A, B);
impl_fold_tuple!(A, B, C);
impl_fold_tuple!(A, B, C, D);
impl_fold_tuple!(A, B, C, D, E);

impl<L: Logic, Out> Fold<L, Out> for Command<Term<L>> {
    type Output = Command<Out>;

    fn fold_with<F, M>(self, folder: &mut F) -> Result<Self::Output, F::Error>
    where
        F: Folder<L, M, Output = Out>,
    {
        use smt2parser::concrete::Command::*;
        if let Some(ctx) = folder.context_mut() {
            ctx.process(&self);
        }
        let command = match self {
            Assert { term } => folder.fold_assert(term)?,
            CheckSat => CheckSat,
            CheckSatAssuming { literals } => CheckSatAssuming { literals },
            DeclareConst { symbol, sort } => folder.fold_declare_const(symbol, sort)?,
            DeclareDatatype { symbol, datatype } => DeclareDatatype { symbol, datatype },
            DeclareDatatypes { datatypes } => DeclareDatatypes { datatypes },
            DeclareFun {
                symbol,
                parameters,
                sort,
            } => folder.fold_declare_fun(symbol, parameters, sort)?,
            DeclareSort { symbol, arity } => folder.fold_declare_sort(symbol, arity)?,
            DefineFun { sig, term } => folder.fold_define_fun(sig, term)?,
            DefineFunRec { sig, term } => folder.fold_define_fun_rec(sig, term)?,
            DefineFunsRec { funs } => folder.fold_define_funs_rec(funs)?,
            DefineSort {
                symbol,
                parameters,
                sort,
            } => folder.fold_define_sort(symbol, parameters, sort)?,
            Echo { message } => Echo { message },
            Exit => Exit,
            GetAssertions => GetAssertions,
            GetAssignment => GetAssignment,
            GetInfo { flag } => GetInfo { flag },
            GetModel => GetModel,
            GetOption { keyword } => GetOption { keyword },
            GetProof => GetProof,
            GetUnsatAssumptions => GetUnsatAssumptions,
            GetUnsatCore => GetUnsatCore,
            GetValue { terms } => folder.fold_get_value(terms)?,
            Pop { level } => Pop { level },
            Push { level } => Push { level },
            Reset => Reset,
            ResetAssertions => ResetAssertions,
            SetInfo { keyword, value } => SetInfo { keyword, value },
            SetLogic { symbol } => folder.fold_set_logic(symbol)?,
            SetOption { keyword, value } => SetOption { keyword, value },
        };
        if let Some(ctx) = folder.context_mut() {
            ctx.process(&command);
        }
        Ok(command)
    }
}