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
//! A more involved example.
//!
//! This example showcases print-time user-specified information but lacks a proper discussion,
//! because no one has asked for it yet.

// Parser library.
use std::io::Write;

use crate::{parse::*, print::*};

#[cfg(test)]
use crate::examples::get_solver;

use self::Var::*;

/// A type.
#[derive(Debug, Clone, Copy, PartialEq)]
enum Type {
    /// Integers.
    Int,
    /// Booleans.
    Bool,
    /// Reals.
    Real,
}
impl Type {
    /// String representation.
    pub fn to_str(self) -> &'static str {
        match self {
            Type::Int => "Int",
            Type::Bool => "Bool",
            Type::Real => "Real",
        }
    }
}
impl Sort2Smt for Type {
    fn sort_to_smt2<Writer: Write>(&self, writer: &mut Writer) -> SmtRes<()> {
        writer.write_all(self.to_str().as_bytes())?;
        Ok(())
    }
}

/// A symbol is a variable and an offset.
#[derive(Debug, Clone, PartialEq)]
pub struct Symbol<'a, 'b>(&'a Var, &'b Offset);

/// An offset gives the index of current and next step.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Offset(usize, usize);
impl Offset {
    /// Index of the current state.
    #[inline(always)]
    pub fn curr(&self) -> usize {
        self.0
    }
    /// Index of the next state.
    #[inline(always)]
    pub fn next(&self) -> usize {
        self.1
    }
}

/// An unrolled version of something.
#[derive(Debug, Clone, PartialEq)]
pub struct Unrolled<'a, T>(T, &'a Offset);

/// Under the hood a symbol is a string.
pub type Sym = String;

/// A variable wraps a symbol.
#[derive(Debug, Clone, PartialEq)]
pub enum Var {
    /// Variable constant in time (Non-Stateful Var: NSVar).
    NSVar(Sym),
    /// State variable in the current step.
    SVar0(Sym),
    /// State variable in the next step.
    SVar1(Sym),
}
impl Var {
    /// Symbol of a variable.
    pub fn sym(&self) -> &str {
        match *self {
            NSVar(ref s) => s,
            SVar0(ref s) => s,
            SVar1(ref s) => s,
        }
    }
    /// Non-stateful variable constructor.
    pub fn nsvar(s: &str) -> Self {
        NSVar(s.to_string())
    }
    /// State variable in the current step.
    pub fn svar0(s: &str) -> Self {
        SVar0(s.to_string())
    }
    /// State variable in the next step.
    pub fn svar1(s: &str) -> Self {
        SVar1(s.to_string())
    }

    /// Given an offset, an S-expression can be unrolled.
    pub fn unroll<'a, 'b>(&'a self, off: &'b Offset) -> Unrolled<'b, &'a Var> {
        Unrolled(self, off)
    }
}

impl<'a, V: AsRef<Var>> Expr2Smt<()> for Unrolled<'a, V> {
    fn expr_to_smt2<Writer: Write>(&self, writer: &mut Writer, _: ()) -> SmtRes<()> {
        self.0.as_ref().expr_to_smt2(writer, &self.1)
    }
}
impl<'a> Expr2Smt<&'a Offset> for Var {
    fn expr_to_smt2<Writer: Write>(&self, writer: &mut Writer, off: &'a Offset) -> SmtRes<()> {
        match *self {
            NSVar(ref sym) => write!(writer, "|{}|", sym)?,
            // SVar at 0, we use the index of the current step.
            SVar0(ref sym) => write!(writer, "|{}@{}|", sym, off.0)?,
            // SVar at 1, we use the index of the next step.
            SVar1(ref sym) => write!(writer, "|{}@{}|", sym, off.1)?,
        }
        Ok(())
    }
}

impl<'a> Sym2Smt<&'a Offset> for Var {
    fn sym_to_smt2<Writer: Write>(&self, writer: &mut Writer, off: &'a Offset) -> SmtRes<()> {
        self.expr_to_smt2(writer, off)
    }
}
impl<'a, 'b> Sym2Smt<()> for Unrolled<'a, &'b Var> {
    fn sym_to_smt2<Writer: Write>(&self, writer: &mut Writer, _: ()) -> SmtRes<()> {
        self.0.expr_to_smt2(writer, &self.1)
    }
}

/// A constant.
#[derive(Debug, Clone, PartialEq)]
pub enum Const {
    /// Boolean constant.
    BConst(bool),
    /// Integer constant.
    IConst(isize),
    /// Rational constant.
    RConst(isize, usize),
}

impl Expr2Smt<()> for Const {
    fn expr_to_smt2<Writer: Write>(&self, writer: &mut Writer, _: ()) -> SmtRes<()> {
        match *self {
            Const::BConst(b) => write!(writer, "{}", b)?,
            Const::IConst(i) => {
                let neg = i < 0;
                if neg {
                    write!(writer, "(- ")?
                }
                write!(writer, "{}", i.abs())?;
                if neg {
                    write!(writer, ")")?
                }
            }
            Const::RConst(num, den) => {
                let neg = num < 0;
                if neg {
                    write!(writer, "(- ")?
                }
                write!(writer, "(/ {} {})", num, den)?;
                if neg {
                    write!(writer, ")")?
                }
            }
        }
        Ok(())
    }
}

/// An S-expression.
#[derive(Debug, Clone, PartialEq)]
pub enum SExpr {
    /// A variable.
    Id(Var),
    /// A constant.
    Val(Const),
    /// An application of function symbol.
    App(Sym, Vec<SExpr>),
}

impl SExpr {
    /// Application constructor.
    pub fn app(sym: &str, args: Vec<SExpr>) -> Self {
        SExpr::App(sym.to_string(), args)
    }
    /// Given an offset, an S-expression can be unrolled.
    pub fn unroll<'a, 'b>(&'a self, off: &'b Offset) -> Unrolled<'b, &'a SExpr> {
        Unrolled(self, off)
    }
}

impl<'a> Expr2Smt<&'a Offset> for SExpr {
    fn expr_to_smt2<Writer: Write>(&self, writer: &mut Writer, off: &'a Offset) -> SmtRes<()> {
        match *self {
            SExpr::Id(ref var) => var.expr_to_smt2(writer, off),
            SExpr::Val(ref cst) => cst.expr_to_smt2(writer, ()),
            SExpr::App(ref sym, ref args) => {
                write!(writer, "({}", sym)?;
                for arg in args {
                    write!(writer, " ")?;
                    arg.expr_to_smt2(writer, off)?
                }
                write!(writer, ")")?;
                Ok(())
            }
        }
    }
}

impl<'a, 'b> Expr2Smt<()> for Unrolled<'a, &'b SExpr> {
    fn expr_to_smt2<Writer: Write>(&self, writer: &mut Writer, _: ()) -> SmtRes<()> {
        self.0.expr_to_smt2(writer, &self.1)
    }
}

/// Empty parser structure.
#[derive(Clone, Copy)]
pub struct Parser;

impl<'a> IdentParser<(Var, Option<usize>), Type, &'a str> for Parser {
    fn parse_ident(self, s: &'a str) -> SmtRes<(Var, Option<usize>)> {
        if s.len() <= 2 {
            bail!("not one of my idents...")
        }
        let s = &s[1..(s.len() - 1)]; // Removing surrounding pipes.
        let mut parts = s.split('@');
        let id = if let Some(id) = parts.next() {
            id.to_string()
        } else {
            bail!("nothing between my pipes!")
        };
        if let Some(index) = parts.next() {
            use std::str::FromStr;
            Ok((
                Var::SVar0(id),
                match usize::from_str(index) {
                    Ok(index) => Some(index),
                    Err(e) => bail!("while parsing the offset in `{}`: {}", s, e),
                },
            ))
        } else {
            Ok((Var::NSVar(id), None))
        }
    }
    fn parse_type(self, s: &'a str) -> SmtRes<Type> {
        match s {
            "Int" => Ok(Type::Int),
            "Bool" => Ok(Type::Bool),
            "Real" => Ok(Type::Real),
            _ => bail!(format!("unknown type `{}`", s)),
        }
    }
}

impl<'a, Br> ModelParser<(Var, Option<usize>), Type, Const, &'a mut SmtParser<Br>> for Parser
where
    Br: ::std::io::BufRead,
{
    fn parse_value(
        self,
        input: &'a mut SmtParser<Br>,
        _: &(Var, Option<usize>),
        _: &[((Var, Option<usize>), Type)],
        _: &Type,
    ) -> SmtRes<Const> {
        use std::str::FromStr;
        if let Some(b) = input.try_bool()? {
            Ok(Const::BConst(b))
        } else if let Some(int) = input.try_int(|int, pos| match isize::from_str(int) {
            Ok(int) => {
                if pos {
                    Ok(int)
                } else {
                    Ok(-int)
                }
            }
            Err(e) => Err(e),
        })? {
            Ok(Const::IConst(int))
        } else if let Some((num, den)) =
            input.try_rat(
                |num, den, pos| match (isize::from_str(num), usize::from_str(den)) {
                    (Ok(num), Ok(den)) => {
                        if pos {
                            Ok((num, den))
                        } else {
                            Ok((-num, den))
                        }
                    }
                    (Err(e), _) | (_, Err(e)) => Err(format!("{}", e)),
                },
            )?
        {
            Ok(Const::RConst(num, den))
        } else {
            input.fail_with("unexpected value")
        }
    }
}

#[test]
fn declare_non_nullary_fun() {
    let mut solver = get_solver(Parser);

    smtry!(
      solver.declare_fun(
        "my_fun", & [ "Int", "Real", "Bool" ], "Real"
      ), failwith "during function declaration: {:?}"
    );

    solver.kill().expect("kill")
}

#[test]
fn test_native() {
    use self::SExpr::*;

    let mut solver = get_solver(Parser);

    let nsv_0 = Var::nsvar("non-stateful var");
    let s_nsv_0 = Id(nsv_0.clone());
    let nsv_1 = Var::nsvar("also non-stateful");
    let s_nsv_1 = Id(nsv_1.clone());
    let sv_0 = Var::svar0("stateful var");
    let s_sv_0 = Id(sv_0.clone());
    let sv_1 = Var::svar1("also stateful");
    let s_sv_1 = Id(sv_1.clone());
    let app1 = SExpr::app("not", vec![s_sv_0.clone()]);
    let app2 = SExpr::app(">", vec![s_sv_1.clone(), Val(Const::IConst(7))]);
    let app3 = SExpr::app(
        "=",
        vec![
            Val(Const::RConst(-7, 3)),
            SExpr::app("+", vec![s_nsv_1.clone(), Val(Const::RConst(2, 1))]),
        ],
    );
    let app = SExpr::app("and", vec![s_nsv_0.clone(), app1, app2, app3]);
    let offset1 = Offset(0, 1);

    smtry!(
      solver.declare_const_with(& nsv_0, & "Bool", & offset1),
      failwith "declaration failed: {:?}"
    );

    smtry!(
      solver.declare_const_with(& nsv_1, & "Real", & offset1),
      failwith "declaration failed: {:?}"
    );

    smtry!(
      solver.declare_const_with(& sv_0, & "Bool", & offset1),
      failwith "declaration failed: {:?}"
    );

    smtry!(
      solver.declare_const_with(& sv_1, & "Int", & offset1),
      failwith "declaration failed: {:?}"
    );

    smtry!(
      solver.assert_with(& app, & offset1),
      failwith "assert failed: {:?}"
    );

    assert!(smtry!(
      solver.check_sat(),
      failwith "error in checksat: {:?}"
    ));

    let model = smtry!(
      solver.get_model(),
      failwith "while getting model: {:?}"
    );

    for ((var, off), _, typ, val) in model {
        if var.sym() == "stateful var" {
            assert_eq!(off, Some(0));
            assert_eq!(typ, Type::Bool);
            assert_eq!(val, Const::BConst(false))
        } else if var.sym() == "also stateful" {
            assert_eq!(off, Some(1));
            assert_eq!(typ, Type::Int);
            if let Const::IConst(val) = val {
                assert!(val > 7)
            } else {
                panic!("expected variable, got {:?}", val)
            }
        } else if var.sym() == "non-stateful var" {
            assert_eq!(off, None);
            assert_eq!(typ, Type::Bool);
            assert_eq!(val, Const::BConst(true))
        } else if var.sym() == "also non-stateful" {
            assert_eq!(off, None);
            assert_eq!(typ, Type::Real);
            if let Const::RConst(num, den) = val {
                assert_eq!(num * 3 + (2 * 3 * den as isize), (7 * den as isize))
            } else {
                panic!("expected variable, got {:?}", val)
            }
        }
    }

    solver.kill().expect("kill")
}

#[test]
fn test_unroll() {
    use self::SExpr::*;

    let mut solver = get_solver(Parser);

    let nsv_0 = Var::nsvar("non-stateful var");
    let s_nsv_0 = Id(nsv_0.clone());
    let nsv_1 = Var::nsvar("also non-stateful");
    let s_nsv_1 = Id(nsv_1.clone());
    let sv_0 = Var::svar0("stateful var");
    let s_sv_0 = Id(sv_0.clone());
    let sv_1 = Var::svar1("also stateful");
    let s_sv_1 = Id(sv_1.clone());
    let app1 = SExpr::app("not", vec![s_sv_0.clone()]);
    let app2 = SExpr::app(">", vec![s_sv_1.clone(), Val(Const::IConst(7))]);
    let app3 = SExpr::app(
        "=",
        vec![
            Val(Const::RConst(-7, 3)),
            SExpr::app("+", vec![s_nsv_1.clone(), Val(Const::RConst(2, 1))]),
        ],
    );
    let app = SExpr::app("and", vec![s_nsv_0.clone(), app1, app2, app3]);
    let offset1 = Offset(0, 1);

    let sym = nsv_0.unroll(&offset1);
    smtry!(
      solver.declare_const(& sym, & "Bool"),
      failwith "declaration failed: {:?}"
    );

    let sym = nsv_1.unroll(&offset1);
    smtry!(
      solver.declare_const(& sym, & "Real"),
      failwith "declaration failed: {:?}"
    );

    let sym = sv_0.unroll(&offset1);
    smtry!(
      solver.declare_const(& sym, & "Bool"),
      failwith "declaration failed: {:?}"
    );

    let sym = sv_1.unroll(&offset1);
    smtry!(
      solver.declare_const(& sym, & "Int"),
      failwith "declaration failed: {:?}"
    );

    let expr = app.unroll(&offset1);
    smtry!(
      solver.assert(& expr),
      failwith "assert failed: {:?}"
    );

    assert!(smtry!(
      solver.check_sat(),
      failwith "error in checksat: {:?}"
    ));

    let model = smtry!(
      solver.get_model(),
      failwith "while getting model: {:?}"
    );

    for ((var, off), _, typ, val) in model {
        if var.sym() == "stateful var" {
            assert_eq!(off, Some(0));
            assert_eq!(typ, Type::Bool);
            assert_eq!(val, Const::BConst(false))
        } else if var.sym() == "also stateful" {
            assert_eq!(off, Some(1));
            assert_eq!(typ, Type::Int);
            if let Const::IConst(val) = val {
                assert!(val > 7)
            } else {
                panic!("expected variable, got {:?}", val)
            }
        } else if var.sym() == "non-stateful var" {
            assert_eq!(off, None);
            assert_eq!(typ, Type::Bool);
            assert_eq!(val, Const::BConst(true))
        } else if var.sym() == "also non-stateful" {
            assert_eq!(off, None);
            assert_eq!(typ, Type::Real);
            if let Const::RConst(num, den) = val {
                assert_eq!(num * 3 + (2 * 3 * den as isize), (7 * den as isize))
            } else {
                panic!("expected variable, got {:?}", val)
            }
        }
    }

    solver.kill().expect("kill")
}