momba-explore 0.1.1

State space exploration engine for PTAs and MDPs augmented with variables.
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
//! Data structures for representing values.

use std::cmp;

use std::convert::TryInto;

use serde::{Deserialize, Serialize};

use ordered_float::NotNan;

use super::types::*;

use self::Value::*;

#[derive(Serialize, Deserialize, Eq, PartialEq, Hash, Clone, Debug)]
#[serde(untagged)]
#[repr(u8)]
pub enum Value {
    Int64(i64),
    Float64(NotNan<f64>),
    Bool(bool),
    Vector(Vec<Value>),
}

impl From<bool> for Value {
    fn from(value: bool) -> Self {
        Value::Bool(value)
    }
}

impl TryInto<bool> for Value {
    type Error = String;

    fn try_into(self) -> Result<bool, Self::Error> {
        match self {
            Value::Bool(value) => Ok(value),
            _ => Err(format!("Unable to convert {:?} to boolean.", self)),
        }
    }
}

impl From<i64> for Value {
    fn from(value: i64) -> Self {
        Value::Int64(value)
    }
}

impl TryInto<i64> for Value {
    type Error = String;

    fn try_into(self) -> Result<i64, Self::Error> {
        match self {
            Value::Int64(value) => Ok(value),
            _ => Err(format!("Unable to convert {:?} to integer.", self)),
        }
    }
}

impl TryInto<f64> for Value {
    type Error = String;

    fn try_into(self) -> Result<f64, Self::Error> {
        match self {
            Value::Float64(value) => Ok(value.into_inner()),
            _ => Err(format!("Unable to convert {:?} to float.", self)),
        }
    }
}

impl Value {
    pub fn get_type(&self) -> Type {
        match self {
            Value::Int64(_) => Type::Int64,
            Value::Float64(_) => Type::Float64,
            Value::Bool(_) => Type::Bool,
            Value::Vector(elements) => Type::Vector {
                element_type: Box::new(
                    // All elements are required to have the same type, hence,
                    // we just take the first element of the vector.
                    elements
                        .first()
                        .map(|element| element.get_type())
                        .unwrap_or(Type::Unknown),
                ),
            },
        }
    }

    pub fn is_int(&self) -> bool {
        matches!(self, Value::Int64(_))
    }

    pub fn is_float(&self) -> bool {
        matches!(self, Value::Float64(_))
    }

    pub fn is_numeric(&self) -> bool {
        matches!(self, Value::Int64(_) | Value::Float64(_))
    }

    pub fn is_bool(&self) -> bool {
        matches!(self, Value::Bool(_))
    }

    pub fn is_vector(&self) -> bool {
        matches!(self, Value::Vector(_))
    }

    pub fn unwrap_bool(&self) -> bool {
        match self {
            Value::Bool(value) => *value,
            _ => panic!("Value {:?} is not a Bool.", self),
        }
    }

    pub fn unwrap_int64(&self) -> i64 {
        match self {
            Value::Int64(value) => *value,
            _ => panic!("Value {:?} is not an Int64.", self),
        }
    }

    pub fn unwrap_float64(&self) -> NotNan<f64> {
        match self {
            Value::Float64(value) => *value,
            _ => panic!("Value {:?} is not a Float64.", self),
        }
    }

    pub fn unwrap_vector(&self) -> &Vec<Value> {
        match self {
            Value::Vector(vector) => vector,
            _ => panic!("Value {:?} is not a Vector.", self),
        }
    }

    #[inline(always)]
    pub fn apply_not(self) -> Value {
        match self {
            Bool(operand) => Bool(!operand),
            operand => panic!("Invalid operand in expression (! {:?}).", operand),
        }
    }

    #[inline(always)]
    pub fn apply_floor(self) -> Value {
        match self {
            Float64(operand) => Int64(operand.floor() as i64),
            Int64(operand) => Int64(operand),
            operand => panic!("Invalid operand in expression (floor {:?}).", operand),
        }
    }

    #[inline(always)]
    pub fn apply_ceil(self) -> Value {
        match self {
            Float64(operand) => Int64(operand.ceil() as i64),
            operand => panic!("Invalid operand in expression (ceil {:?}).", operand),
        }
    }

    #[inline(always)]
    pub fn apply_abs(self) -> Value {
        match self {
            Float64(operand) => Float64(NotNan::new(operand.abs()).unwrap()),
            Int64(operand) => Int64(operand.abs()),
            operand => panic!("Invalid operand in expression (abs {:?}).", operand),
        }
    }

    #[inline(always)]
    pub fn apply_sgn(self) -> Value {
        match self {
            Float64(operand) => Float64(NotNan::new(operand.signum()).unwrap()),
            Int64(operand) => Int64(operand.signum()),
            operand => panic!("Invalid operand in expression (sgn {:?}).", operand),
        }
    }

    #[inline(always)]
    pub fn apply_trc(self) -> Value {
        match self {
            Float64(operand) => Int64(operand.trunc() as i64),
            operand => panic!("Invalid operand in expression (trc {:?}).", operand),
        }
    }

    #[inline(always)]
    pub fn apply_minus(self) -> Value {
        match self {
            Int64(operand) => Int64(-operand),
            Float64(operand) => Float64(-operand),
            operand => panic!("Invalid operand in expression (- {:?}).", operand),
        }
    }

    #[inline(always)]
    pub fn apply_add(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => Int64(left + right),
            (Float64(left), Float64(right)) => Float64(left + right),
            (Int64(left), Float64(right)) => Float64(NotNan::new(left as f64).unwrap() + right),
            (Float64(left), Int64(right)) => Float64(left + NotNan::new(right as f64).unwrap()),
            (left, right) => panic!("Invalid operands in expression ({:?} + {:?}).", left, right),
        }
    }

    #[inline(always)]
    pub fn apply_sub(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => Int64(left - right),
            (Float64(left), Float64(right)) => Float64(left - right),
            (Int64(left), Float64(right)) => Float64(NotNan::new(left as f64).unwrap() - right),
            (Float64(left), Int64(right)) => Float64(left - NotNan::new(right as f64).unwrap()),
            (left, right) => panic!("Invalid operands in expression ({:?} - {:?}).", left, right),
        }
    }

    #[inline(always)]
    pub fn apply_mul(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => Int64(left * right),
            (Float64(left), Float64(right)) => Float64(left * right),
            (Int64(left), Float64(right)) => Float64(NotNan::new(left as f64).unwrap() * right),
            (Float64(left), Int64(right)) => Float64(left * NotNan::new(right as f64).unwrap()),
            (left, right) => panic!("Invalid operands in expression ({:?} * {:?}).", left, right),
        }
    }

    #[inline(always)]
    pub fn apply_floor_div(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => Int64(left.div_euclid(right)),
            (Float64(left), Float64(right)) => Int64((left / right).floor() as i64),
            (Int64(left), Float64(right)) => {
                Int64((NotNan::new(left as f64).unwrap() / right).floor() as i64)
            }
            (Float64(left), Int64(right)) => {
                Int64((left / (NotNan::new(right as f64).unwrap()).floor()).into_inner() as i64)
            }
            (left, right) => panic!(
                "Invalid operands in expression ({:?} // {:?}).",
                left, right
            ),
        }
    }

    #[inline(always)]
    pub fn apply_real_div(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => {
                Float64(NotNan::new((left as f64) / (right as f64)).unwrap())
            }
            (Float64(left), Float64(right)) => Float64(left / right),
            (left, right) => panic!("Invalid operands in expression ({:?} / {:?}).", left, right),
        }
    }

    #[inline(always)]
    pub fn apply_mod(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => Int64(left.rem_euclid(right)),
            (Float64(left), Float64(right)) => Float64(left % right),
            (left, right) => panic!("Invalid operands in expression ({:?} % {:?}).", left, right),
        }
    }

    #[inline(always)]
    pub fn apply_pow(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => {
                Float64(NotNan::new((left as f64).powf(right as f64)).unwrap())
            }
            (Float64(left), Float64(right)) => {
                Float64(NotNan::new(left.powf(right.into())).unwrap())
            }
            (left, right) => panic!(
                "Invalid operands in expression ({:?} ** {:?}).",
                left, right
            ),
        }
    }

    #[inline(always)]
    pub fn apply_log(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => {
                Float64(NotNan::new((left as f64).log(right as f64)).unwrap())
            }
            (Float64(left), Float64(right)) => {
                Float64(NotNan::new(left.log(right.into())).unwrap())
            }
            (left, right) => panic!(
                "Invalid operands in expression ({:?} log {:?}).",
                left, right
            ),
        }
    }

    #[inline(always)]
    pub fn apply_min(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => Int64(cmp::min(left, right)),
            (Float64(left), Float64(right)) => Float64(cmp::min(left, right)),
            (left, right) => panic!(
                "Invalid operands in expression ({:?} min {:?}).",
                left, right
            ),
        }
    }

    #[inline(always)]
    pub fn apply_max(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => Int64(cmp::max(left, right)),
            (Float64(left), Float64(right)) => Float64(cmp::max(left, right)),
            (left, right) => panic!(
                "Invalid operands in expression ({:?} max {:?}).",
                left, right
            ),
        }
    }

    #[inline(always)]
    pub fn apply_cmp_eq(self, other: Value) -> Value {
        Value::Bool(self == other)
    }

    #[inline(always)]
    pub fn apply_cmp_ne(self, other: Value) -> Value {
        Value::Bool(self != other)
    }

    #[inline(always)]
    pub fn apply_cmp_lt(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => Bool(left < right),
            (Float64(left), Float64(right)) => Bool(left < right),
            (Int64(left), Float64(right)) => Bool((left as f64) < right.into_inner()),
            (Float64(left), Int64(right)) => Bool(left.into_inner() < (right as f64)),
            (left, right) => panic!("Invalid operands in expression ({:?} < {:?}).", left, right),
        }
    }

    #[inline(always)]
    pub fn apply_cmp_le(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => Bool(left <= right),
            (Float64(left), Float64(right)) => Bool(left <= right),
            (Int64(left), Float64(right)) => Bool((left as f64) <= right.into_inner()),
            (Float64(left), Int64(right)) => Bool(left.into_inner() <= (right as f64)),
            (left, right) => panic!(
                "Invalid operands in expression ({:?} <= {:?}).",
                left, right
            ),
        }
    }

    #[inline(always)]
    pub fn apply_cmp_ge(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => Bool(left >= right),
            (Float64(left), Float64(right)) => Bool(left >= right),
            (Int64(left), Float64(right)) => Bool((left as f64) >= right.into_inner()),
            (Float64(left), Int64(right)) => Bool(left.into_inner() >= (right as f64)),
            (left, right) => panic!(
                "Invalid operands in expression ({:?} >= {:?}).",
                left, right
            ),
        }
    }

    #[inline(always)]
    pub fn apply_cmp_gt(self, other: Value) -> Value {
        match (self, other) {
            (Int64(left), Int64(right)) => Bool(left > right),
            (Float64(left), Float64(right)) => Bool(left > right),
            (Int64(left), Float64(right)) => Bool((left as f64) > right.into_inner()),
            (Float64(left), Int64(right)) => Bool(left.into_inner() > (right as f64)),
            (left, right) => panic!("Invalid operands in expression ({:?} > {:?}).", left, right),
        }
    }

    #[inline(always)]
    pub fn apply_sin(self) -> Value {
        match self {
            Float64(operand) => Float64(operand.sin().try_into().unwrap()),
            operand => panic!("Invalid operand in expression (sin {:?}).", operand),
        }
    }

    #[inline(always)]
    pub fn apply_cos(self) -> Value {
        match self {
            Float64(operand) => Float64(operand.cos().try_into().unwrap()),
            operand => panic!("Invalid operand in expression (sin {:?}).", operand),
        }
    }

    #[inline(always)]
    pub fn apply_tan(self) -> Value {
        match self {
            Float64(operand) => Float64(operand.tan().try_into().unwrap()),
            operand => panic!("Invalid operand in expression (sin {:?}).", operand),
        }
    }
}