-
Notifications
You must be signed in to change notification settings - Fork 0
/
pplite_native_wrapper.hpp
2085 lines (1816 loc) · 58 KB
/
pplite_native_wrapper.hpp
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
#pragma once
#include <crab/config.h>
#ifdef HAVE_PPLITE
#include <crab/domains/abstract_domain.hpp>
#include <crab/domains/abstract_domain_params.hpp>
#include <crab/domains/abstract_domain_specialized_traits.hpp>
#include <crab/domains/intervals.hpp>
#include <crab/numbers/bignums.hpp>
#include <crab/support/debug.hpp>
#include <crab/support/stats.hpp>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include <pplite/pplite.hh>
// If needed, reset NDEBUG and include (again) cassert
#if defined(_DEBUG) && defined(NDEBUG)
# undef NDEBUG
# include <cassert>
#endif
namespace crab {
namespace domains {
namespace pplite_domains {
/*
This enumeration needs to be kept in sync with
enum pplite::dynamic::Abs_Poly::Kind
The listed enum values are up-to-date with PPLite 0.11.
Note: even values are for domains; odd values for _STATS variants.
*/
// enum pplite_domain_id_t : unsigned int {
// POLY, POLY_STATS,
// B_POLY, B_POLY_STATS,
// F_POLY, F_POLY_STATS,
// U_POLY, U_POLY_STATS,
// UF_POLY, UF_POLY_STATS,
// P_SET, P_SET_STATS,
// FP_SET, FP_SET_STATS
// };
} // namespace pplite_domains
} // namespace domains
} // namespace crab
#define PPLITE_DOMAIN_SCOPED_STATS(NAME) \
CRAB_DOMAIN_SCOPED_STATS(this, NAME, 0)
#define PPLITE_DOMAIN_SCOPED_STATS_ASSIGN_CTOR(NAME) \
CRAB_DOMAIN_SCOPED_STATS(&o, NAME, 0)
#define CHECK_INV(obj) \
assert((obj).check_inv());
#define NOISY_DEBUG false
#if NOISY_DEBUG
#define PRE() \
crab::outs() << "pre " << __func__ << " : "; \
CHECK_INV(*this); \
dump();
#define PRE1(title) \
crab::outs() << title << "\n"; \
PRE();
#define PRE2(title, code) \
crab::outs() << title << "\n"; \
do { code } while (false); \
PRE();
#define POST() \
crab::outs() << "post " << __func__ << " : "; \
CHECK_INV(*this); \
dump();
#define POST1(title) \
crab::outs() << title << "\n"; \
POST();
#define POST2(title, code) \
crab::outs() << title << "\n"; \
do { code } while (false); \
POST();
#define DUMP_RESULT(map, poly) \
crab::outs() << "\n result = "; \
dump(map, poly, crab::outs());
#else /* !NOISY_DEBUG */
#define PRE() CHECK_INV(*this)
#define PRE1(title) PRE()
#define PRE2(title, code) PRE()
#define POST() CHECK_INV(*this)
#define POST1(title) POST()
#define POST2(title, code) POST()
#define DUMP_RESULT(map, poly)
#endif /* !NOISY_DEBUG */
/**
* If template parameter Number is ikos::q_number then the PPLite
* domain will assume variables can take rational values.
* Otherwise, all variables are assumed to only take integral values.
**/
namespace crab {
namespace domains {
inline crab::crab_os&
operator<<(crab::crab_os& os, const pplite::dynamic::Dyn_Poly& p) {
// EZ FIXME: how to extract the standard ostream from crab_os?
if (&os == &crab::outs())
p.print(std::cout);
else if (&os == &crab::errs())
p.print(std::cerr);
else {
// os is based on a string stream
std::ostringstream ss;
p.print(ss);
os << ss.str();
}
return os;
}
template <typename Number, typename VariableName, pplite_domains::pplite_domain_id_t K,
class Params = PPLiteDefaultParams<Number>>
class pplite_domain final
: public abstract_domain_api<pplite_domain<Number, VariableName, K, Params>> {
public:
using number_t = Number;
using varname_t = VariableName;
using params_t = Params;
private:
using pplite_domain_t = pplite_domain<number_t, varname_t, K, params_t>;
using abstract_domain_t = abstract_domain_api<pplite_domain_t>;
public:
using typename abstract_domain_t::disjunctive_linear_constraint_system_t;
using typename abstract_domain_t::interval_t;
using typename abstract_domain_t::linear_constraint_system_t;
using typename abstract_domain_t::linear_constraint_t;
using typename abstract_domain_t::linear_expression_t;
using typename abstract_domain_t::reference_constraint_t;
using typename abstract_domain_t::variable_or_constant_t;
using typename abstract_domain_t::variable_t;
using typename abstract_domain_t::variable_vector_t;
using typename abstract_domain_t::variable_or_constant_vector_t;
private:
using dim_type = pplite::dim_type;
using dim_range = pplite::dim_range;
using Affine_Expr = pplite::Affine_Expr;
using Con = pplite::Con;
using Cons = pplite::Cons;
using Dims = pplite::Dims;
using Dyn_Poly = pplite::dynamic::Dyn_Poly;
using Dyn_Poly_Kind = pplite::dynamic::Abs_Poly::Kind;
using Index_Set = pplite::Index_Set;
using Integer = pplite::Integer;
using Itv = pplite::Itv;
using Linear_Expr = pplite::Linear_Expr;
using Rational = pplite::Rational;
using Spec_Elem = pplite::Spec_Elem;
using Topol = pplite::Topol;
using Var = pplite::Var;
static constexpr pplite_domains::pplite_domain_id_t domain_id = K;
static constexpr auto poly_kind = static_cast<Dyn_Poly_Kind>(domain_id);
// Trick to force library init code before building objects.
struct init_check_t {
init_check_t() {
static bool is_initialized = false;
if (not is_initialized) {
bool requires_stats = (domain_id % 2 == 1);
// Note: if (requires_stats == false), we do NOT reset
// PPLite's noisy stats to false, because it is a library-wide
// setting. This way, stats will be printed (at program exit)
// if there *exists* a template instance requiring them,
// no matter the order of template instantiation.
if (requires_stats) {
pplite::set_noisy_stats(requires_stats);
}
is_initialized = true;
}
}
}; // struct init_check_t
class var_map_t {
public:
using value_type = std::pair<const variable_t, dim_type>;
using opt_dim_type = boost::optional<dim_type>;
using inverse_t = std::vector<variable_t>;
private:
using map_t = std::unordered_map<variable_t, dim_type>;
map_t map_to_dim;
// optional: (re-) built on demand when needed
std::unique_ptr<inverse_t> map_to_var;
public:
var_map_t() = default;
var_map_t(var_map_t&& var_map) = default;
var_map_t& operator=(var_map_t&& var_map) = default;
~var_map_t() = default;
// define copy ctor and assignment (due to std::unique_ptr)
var_map_t(const var_map_t& other)
: map_to_dim(other.map_to_dim),
map_to_var() {
if (other.map_to_var)
map_to_var.reset(new inverse_t(*other.map_to_var));
}
var_map_t& operator=(const var_map_t& other) {
if (this != &other) {
map_to_dim = other.map_to_dim;
if (other.map_to_var)
map_to_var.reset(new inverse_t(*other.map_to_var));
else
map_to_var.reset();
}
return *this;
}
using iterator = typename map_t::iterator;
using const_iterator = typename map_t::const_iterator;
iterator begin() { return map_to_dim.begin(); }
iterator end() { return map_to_dim.end(); }
const_iterator begin() const { return map_to_dim.begin(); }
const_iterator end() const { return map_to_dim.end(); }
const_iterator cbegin() const { return map_to_dim.cbegin(); }
const_iterator cend() const { return map_to_dim.cend(); }
opt_dim_type get_var_dim(const variable_t& var) const {
auto it = map_to_dim.find(var);
return (it != map_to_dim.end())
? opt_dim_type(it->second)
: opt_dim_type();
}
bool has_variable(const variable_t& var) const {
return map_to_dim.find(var) != map_to_dim.end();
}
iterator find(const variable_t& var) {
return map_to_dim.find(var);
}
void insert(const variable_t& var, dim_type dim) {
insert(value_type(var, dim));
}
void insert(value_type&& cp) {
drop_inverse();
map_to_dim.insert(std::move(cp));
}
void erase(const variable_t& key) {
drop_inverse();
map_to_dim.erase(key);
}
void erase(const_iterator it) {
drop_inverse();
map_to_dim.erase(it);
}
bool set(const variable_t& key, dim_type val) {
auto it = map_to_dim.find(key);
if (it == map_to_dim.end())
return false;
drop_inverse();
it->second = val;
return true;
}
size_t size() const {
return map_to_dim.size();
}
const inverse_t& get_inverse() const {
maybe_regen_inverse();
return *map_to_var;
}
void swap(var_map_t& o) {
map_to_dim.swap(o.map_to_dim);
map_to_var.swap(o.map_to_var);
}
void print(crab::crab_os& os) const {
for (const auto& p : map_to_dim)
os << p.first << " --> " << p.second << "\n";
}
private:
void drop_inverse() {
map_to_var.reset();
}
void maybe_regen_inverse() const {
if (map_to_var == nullptr) {
auto& x = const_cast<var_map_t&>(*this);
x.regen_inverse();
}
}
void regen_inverse() {
auto sz = map_to_dim.size();
if (sz == 0) {
map_to_var.reset(new inverse_t());
return;
}
// needed because variable_t has no default ctor
const variable_t& var = map_to_dim.begin()->first;
map_to_var.reset(new inverse_t(sz, var));
for (const auto& e : map_to_dim)
(*map_to_var)[e.second] = e.first;
}
}; // class var_map_t
using poly_t = Dyn_Poly;
using interval_domain_t = ikos::interval_domain<number_t, varname_t>;
using bound_t = ikos::bound<number_t>;
using binding_t = typename var_map_t::value_type;
using opt_dim_type = typename var_map_t::opt_dim_type;
init_check_t m_init_check;
var_map_t m_var_map;
poly_t m_poly;
static bool is_real() {
return std::is_same<number_t, ikos::q_number>::value;
}
static bool is_integer() {
return not is_real();
}
static size_t get_dims(const poly_t& ph) { return ph.space_dim(); }
size_t get_dims() const { return get_dims(m_poly); }
/**** Conversion helpers: from Crab to PPLite ****/
static Integer to_Integer(const ikos::z_number& z) {
// EZ CHECKME: it is unclear why z_number::get_mpz_t()
// is non-const (and its use deprecated).
auto& zz = const_cast<ikos::z_number&>(z);
return Integer(zz.get_mpz_t());
}
// Note: correct rounding is up to the caller
static Integer to_Integer(const ikos::q_number& q) {
assert(q.denominator() == 1);
return to_Integer(q.numerator());
}
static Rational to_Rational(const ikos::z_number& z) {
assert(is_integer());
return Rational(to_Integer(z));
}
static Rational to_Rational(const ikos::q_number& q) {
assert(is_real());
return Rational(to_Integer(q.numerator()), to_Integer(q.denominator()));
}
// If v is in the map then it maps v to a dimension, otherwise null
static opt_dim_type
get_var_dim(const var_map_t& m, const variable_t& v) {
return m.get_var_dim(v);
}
opt_dim_type get_var_dim(const variable_t& v) const {
return m_var_map.get_var_dim(v);
}
dim_type get_or_insert_var_dim(const variable_t& v) const {
assert(m_var_map.size() == get_dims());
if (auto opt_dim = get_var_dim(v))
return *opt_dim;
dim_type dim = m_var_map.size();
// Semantically const
auto& obj = const_cast<pplite_domain_t&>(*this);
obj.m_var_map.insert(v, dim);
obj.m_poly.add_space_dims(1);
return dim;
}
Var to_Var(const variable_t& x) const {
return Var(get_or_insert_var_dim(x));
}
using z_linexpr = ikos::linear_expression<ikos::z_number, varname_t>;
using q_linexpr = ikos::linear_expression<ikos::q_number, varname_t>;
Affine_Expr
to_Affine_Expr(const z_linexpr& z_le) const {
assert(is_integer());
Affine_Expr res { to_Integer(z_le.constant()) };
for (const auto& p : z_le) {
auto z = to_Integer(p.first);
auto v = to_Var(p.second);
add_mul_assign(res.expr, z, v);
}
return res;
}
Affine_Expr
to_Affine_Expr(const q_linexpr& q_le) const {
Integer den = 1;
return to_Affine_Expr_with_den(q_le, den);
}
Affine_Expr
to_Affine_Expr_with_den(const z_linexpr& z_le, Integer& den) const {
assert(den == 1);
return to_Affine_Expr(z_le);
}
Affine_Expr
to_Affine_Expr_with_den(const q_linexpr& q_le, Integer& den) const {
assert(is_real());
// compute into den the lcm of denominators
den = to_Integer(q_le.constant().denominator());
for (const auto& p : q_le) {
const auto& q = p.first;
const auto& q_den = q.denominator();
if (q_den != 1)
lcm_assign(den, den, to_Integer(q_den));
}
const bool need_adjustment = (den != 1);
Integer adjust_factor;
auto convert_with_lcm = [&](const ikos::q_number& q) -> Integer {
auto res = to_Integer(q.numerator());
if (need_adjustment) {
auto q_den = to_Integer(q.denominator());
exact_div_assign(adjust_factor, den, q_den);
res *= adjust_factor;
}
return res;
};
Affine_Expr res;
res.inhomo = convert_with_lcm(q_le.constant());
for (const auto& p : q_le) {
if (p.first == 0)
continue;
auto z = convert_with_lcm(p.first);
auto v = to_Var(p.second);
add_mul_assign(res.expr, z, v);
}
return res;
}
Con to_Con(const linear_constraint_t& cst) const {
auto to_Type = [](typename linear_constraint_t::kind_t kind) {
assert(kind != linear_constraint_t::DISEQUATION);
switch (kind) {
case linear_constraint_t::EQUALITY:
return Con::EQUALITY;
case linear_constraint_t::INEQUALITY:
return Con::NONSTRICT_INEQUALITY;
case linear_constraint_t::STRICT_INEQUALITY:
return Con::STRICT_INEQUALITY;
case linear_constraint_t::DISEQUATION:
CRAB_ERROR("conversion to Con not supported for disequations");
}
#if defined(__GNUC__) || defined(__GNUG__)
__builtin_unreachable(); // to make gcc happy
#endif
};
// Note: no need to keep denominator (even when is_real() holds).
auto ae = to_Affine_Expr(cst.expression());
auto type = to_Type(cst.kind());
if (type != Con::EQUALITY) {
// Negation required (PPLite uses >= when Crab uses <=).
pplite::neg_assign(ae);
}
return Con(std::move(ae), type);
}
/**** Conversion helpers: from PPLite to Crab ****/
static void convert_Integer(const Integer& z_src, ikos::z_number& z_dst) {
mpz_class tmp = z_src;
mpz_swap(tmp.get_mpz_t(), z_dst.get_mpz_t());
}
static void convert_Integer(const Integer& z_src, ikos::q_number& q_dst) {
ikos::z_number z_dst;
convert_Integer(z_src, z_dst);
q_dst = z_dst;
}
// Note: proper rounding is up to the caller
static void convert_Rational(const Rational& q_src, ikos::z_number& z_dst) {
assert(q_src.get_den().is_one());
convert_Integer(q_src.get_num(), z_dst);
}
static void convert_Rational(const Rational& q_src, ikos::q_number& q_dst) {
mpq_class tmp = q_src;
q_dst = ikos::q_number::from_mpq_t(tmp.get_mpq_t());
}
static number_t to_number(const Integer& z) {
number_t res;
convert_Integer(z, res);
return res;
}
static number_t to_number(const Rational& q) {
number_t res;
convert_Rational(q, res);
return res;
}
// Note: integral refinement of itv (when safe) is up to the caller.
static interval_t to_interval(const Itv& itv) {
if (itv.is_empty())
return interval_t::bottom();
bound_t lb = itv.has_lb()
? bound_t(to_number(itv.lb))
: bound_t::minus_infinity();
bound_t ub = itv.has_ub()
? bound_t(to_number(itv.ub))
: bound_t::plus_infinity();
return interval_t(lb, ub);
}
linear_expression_t
to_linear_expression(const Linear_Expr& expr,
const Integer& inhomo) const {
linear_expression_t res = to_number(inhomo);
if (expr.space_dim() == 0)
return res;
const auto& vars = m_var_map.get_inverse();
for (auto d : dim_range(expr)) {
if (not expr[d].is_zero())
res = res + to_number(expr[d]) * vars[d];
}
return res;
}
linear_constraint_t
to_linear_constraint(const Con& con) const {
auto to_kind = [](typename Con::Type type) {
switch (type) {
case Con::EQUALITY:
return linear_constraint_t::EQUALITY;
case Con::NONSTRICT_INEQUALITY:
return linear_constraint_t::INEQUALITY;
case Con::STRICT_INEQUALITY:
return linear_constraint_t::STRICT_INEQUALITY;
}
#if defined(__GNUC__) || defined(__GNUG__)
__builtin_unreachable(); // to make gcc happy
#endif
};
auto expr = to_linear_expression(con.linear_expr(), con.inhomo_term());
auto kind = to_kind(con.type());
if (kind != linear_constraint_t::EQUALITY) {
// Negation required (PPLite uses >= when Crab uses <=).
expr = -expr;
}
return linear_constraint_t(expr, kind);
}
/**** End of conversion helpers ****/
// Checks the class invariant:
// m_var_map and m_poly should be consistent.
bool check_inv() const {
auto& os = crab::errs();
dim_type size = m_var_map.size();
dim_type dim = m_poly.space_dim();
if (size != dim) {
os << "pplite_domain: var map size and poly dim mismatch\n";
return false;
}
if (size == 0)
return true;
Index_Set dims;
for (const auto& p : m_var_map)
dims.set(p.second);
if (dims.size() != size) {
os << "pplite_domain: var map has index bigger than poly dim\n";
return false;
}
if (dims.last() != size - 1) {
os << "pplite_domain: var map is not injective\n";
return false;
}
// all checks passed
return true;
}
static void
remove_dimensions(const Index_Set& to_remove,
var_map_t& m, poly_t& p) {
assert(m.size() == get_dims(p));
if (to_remove.empty())
return;
// Note: copy of inverse map is meant!
// (m.set() and m.erase() invalidate inverse map)
auto inverse = m.get_inverse();
auto removed = 0;
for (auto d : dim_range(p)) {
const auto& var = inverse[d];
if (to_remove.test(d)) {
m.erase(var);
++removed;
} else if (removed > 0) {
m.set(var, d - removed);
}
}
p.remove_space_dims(to_remove);
assert(m.size() == get_dims(p));
}
static void
remove_some_unconstrained(const Index_Set& uncon,
const var_map_t& m, const poly_t& p) {
if (uncon.empty())
return;
// Here we remove some unconstrained space dims:
// we modify m and p, but we are semantically const.
auto& mm = const_cast<var_map_t&>(m);
auto& pp = const_cast<poly_t&>(p);
remove_dimensions(uncon, mm, pp);
}
void remove_unconstrained() const {
remove_some_unconstrained(m_poly.get_unconstrained(),
m_var_map, m_poly);
}
static void
merge_var_map(var_map_t& m_x, const var_map_t& m_y) {
for (const auto& p : m_y) {
if (not m_x.has_variable(p.first))
m_x.insert(p.first, m_x.size());
}
}
static void
extend_to_var_map(const var_map_t& m, poly_t& p) {
assert(m.size() >= get_dims(p));
dim_type new_dims = m.size() - get_dims(p);
p.add_space_dims(new_dims);
}
static void
adapt_to_var_map(const var_map_t& new_map,
const var_map_t& old_map, poly_t& p) {
const dim_type new_dim = new_map.size();
const dim_type old_dim = old_map.size();
assert(new_dim >= old_dim);
assert(old_dim == p.space_dim());
// add missing space dims
p.add_space_dims(new_dim - old_dim);
// permute p dimensions according to new_map
Dims perm(new_dim);
const auto& new_inverse = new_map.get_inverse();
auto add_d = old_dim;
for (auto d = 0; d != new_dim; ++d) {
const auto& v = new_inverse[d];
if (auto old_d = old_map.get_var_dim(v))
perm[*old_d] = d;
else {
perm[add_d] = d;
++add_d;
}
}
p.map_space_dims(perm);
}
// Dirty trick to possibly avoid copy of polyhedron
// (if no new dim has to be added, then adaptation can be done
// on the input polyhedron, because it is semantically const).
// We return a pair containing a reference and a smart pointer;
// the caller should use the reference; the smart pointer is
// only meant to ensure proper clean up (caller should not touch it).
static std::pair<poly_t&, std::unique_ptr<poly_t>>
maybe_copy_and_adapt(const var_map_t& new_map,
const var_map_t& old_map,
const poly_t& old_poly) {
const bool needs_copy = (new_map.size() > old_map.size());
if (needs_copy) {
std::unique_ptr<poly_t> ptr(new poly_t(old_poly));
auto& pp = *ptr;
adapt_to_var_map(new_map, old_map, pp);
return { pp, std::move(ptr) };
} else {
// semantically const: we only permute dims
std::unique_ptr<poly_t> ptr = nullptr;
auto& pp = const_cast<poly_t&>(old_poly);
adapt_to_var_map(new_map, old_map, pp);
auto& mm = const_cast<var_map_t&>(old_map);
mm = new_map;
return { pp, std::move(ptr) };
}
}
static bool
merge_var_map_leq(var_map_t& m_x,
const var_map_t& m_y, const poly_t& p_y) {
// Here we are checking if p_x <= p_y; if p_y constrains a variable
// which is not in m_x (hence, unconstrained in p_x), then
// containment does not hold and we eagerly return false.
for (const auto& p : m_y) {
if (not m_x.has_variable(p.first)) {
if (p_y.constrains(Var(p.second)))
return false;
m_x.insert(p.first, m_x.size());
}
}
return true;
}
// Helper for debugging purposes
static void
dump(const var_map_t& m, const poly_t& p,
crab::crab_os& os = crab::outs()) {
const char* spacer = " ";
auto sdim = p.space_dim();
os << "\n";
os << spacer << "m_poly space dim = " << sdim << "\n";
const auto& names = m.get_inverse();
os << spacer << "m_var_map = [ ";
for (auto i : pplite::index_range(names)) {
os << i << " -> " << names[i] << "; ";
}
os << "]\n";
auto var_printer = [&names](std::ostream& s, Var v) {
crab::crab_os c_os(&s);
c_os << names[v.id()];
};
Var::output_function.set(var_printer);
os << spacer << "m_poly = { " << p << " }\n";
}
void dump(crab::crab_os& os = crab::outs()) const {
dump(m_var_map, m_poly, os);
}
// FIXME: let it produce pplite::Cons
void
inequalities_from_disequation(const variable_t& x, const number_t& n,
linear_constraint_system_t& out) const {
interval_t xi = at(x);
interval_t ni(n);
interval_t new_xi = ikos::linear_interval_solver_impl
::trim_interval<interval_t>(xi, ni);
if (new_xi.is_bottom()) {
out += linear_constraint_t::get_false();
} else if (!new_xi.is_top() && (new_xi <= xi)) {
if (new_xi.lb().is_finite()) {
// strenghten lb
out += linear_constraint_t(x >= *(new_xi.lb().number()));
}
if (new_xi.ub().is_finite()) {
// strenghten ub
out += linear_constraint_t(x <= *(new_xi.ub().number()));
}
}
}
interval_t
compute_residual(const linear_expression_t &e,
const variable_t& pivot) const {
interval_t residual(-e.constant());
for (auto kv : e) {
const variable_t& v = kv.second;
if (v.index() != pivot.index()) {
residual = residual - (interval_t(kv.first) * at(v));
}
}
return residual;
}
// FIXME: let it produce pplite::Cons
void
inequalities_from_disequation(const linear_expression_t &e,
linear_constraint_system_t &o) const {
for (auto kv : e) {
const variable_t& pivot = kv.second;
interval_t i = compute_residual(e, pivot) / interval_t(kv.first);
if (auto k = i.singleton()) {
inequalities_from_disequation(pivot, *k, o);
}
}
}
private:
pplite_domain(var_map_t&& m, poly_t&& p, bool compact = true)
: m_var_map(std::move(m)), m_poly(std::move(p)) {
if (compact)
remove_unconstrained();
assert(check_inv());
}
public:
explicit pplite_domain(bool isBot = false)
: m_var_map(),
m_poly(isBot ? Spec_Elem::EMPTY : Spec_Elem::UNIVERSE,
0, Topol::CLOSED, poly_kind) {
assert(check_inv());
}
pplite_domain(pplite_domain_t&&) = default;
pplite_domain_t& operator=(pplite_domain_t&&) = default;
~pplite_domain() = default;
pplite_domain(const pplite_domain_t& o)
: m_var_map(o.m_var_map), m_poly(o.m_poly) {
PPLITE_DOMAIN_SCOPED_STATS(".copy");
}
pplite_domain_t& operator=(const pplite_domain_t& o) {
PPLITE_DOMAIN_SCOPED_STATS_ASSIGN_CTOR(".copy");
if (this != &o) {
m_var_map = o.m_var_map;
m_poly = o.m_poly;
}
return *this;
}
pplite_domain_t make_top() const override {
pplite_domain_t res(false);
return res;
}
pplite_domain_t make_bottom() const override {
pplite_domain_t res(true);
return res;
}
void set_to_top() override {
m_poly.set_universe();
}
void set_to_bottom() override {
m_poly.set_empty();
}
bool is_bottom() const override {
return m_poly.is_empty();
}
bool is_top() const override {
return m_poly.is_universe();
}
bool operator<=(const pplite_domain_t &o) const override {
PPLITE_DOMAIN_SCOPED_STATS(".leq");
PRE();
const auto& x = *this;
const auto& y = o;
if (x.is_bottom())
return true;
if (y.is_bottom())
return false;
if (y.is_top())
return true;
if (x.is_top())
return false;
auto m = x.m_var_map;
if (not merge_var_map_leq(m, y.m_var_map, y.m_poly))
return false;
auto xx = maybe_copy_and_adapt(m, x.m_var_map, x.m_poly);
auto yy = maybe_copy_and_adapt(m, y.m_var_map, y.m_poly);
return yy.first.contains(xx.first);
}
void operator|=(const pplite_domain_t& o) override {
PPLITE_DOMAIN_SCOPED_STATS(".join");
PRE();
auto& x = *this;
const auto& y = o;
if (x.is_top() || y.is_bottom())
return;
if (x.is_bottom()) {
x = y;
return;
}
if (y.is_top()) {
x.set_to_top();
return;
}
// Force compactness, as it improves efficiency.
x.remove_unconstrained();
y.remove_unconstrained();
merge_var_map(x.m_var_map, y.m_var_map);
extend_to_var_map(x.m_var_map, x.m_poly);
auto yy = maybe_copy_and_adapt(x.m_var_map, y.m_var_map, y.m_poly);
x.m_poly.join_assign(yy.first);
POST();
}
pplite_domain_t operator|(const pplite_domain_t& o) const override {
PPLITE_DOMAIN_SCOPED_STATS(".join");
PRE();
const auto& x = *this;
const auto& y = o;
if (x.is_bottom() || y.is_top())
return y;
if (y.is_bottom() || x.is_top())
return x;
// Compactness improves efficiency.
x.remove_unconstrained();
y.remove_unconstrained();
var_map_t m = x.m_var_map;
merge_var_map(m, y.m_var_map);
poly_t p = x.m_poly;
extend_to_var_map(m, p);
auto yy = maybe_copy_and_adapt(m, y.m_var_map, y.m_poly);
p.join_assign(yy.first);
auto res = pplite_domain_t(std::move(m), std::move(p));
CHECK_INV(res);
return res;
}
void operator&=(const pplite_domain_t& o) override {
PPLITE_DOMAIN_SCOPED_STATS(".meet");
PRE();
auto& x = *this;
const auto& y = o;
if (x.is_bottom() || y.is_top())
return;
if (x.is_top() || y.is_bottom()) {
x = y;
return;
}
// Compactness improves efficiency.
x.remove_unconstrained();
y.remove_unconstrained();
merge_var_map(x.m_var_map, y.m_var_map);
extend_to_var_map(x.m_var_map, x.m_poly);
auto yy = maybe_copy_and_adapt(x.m_var_map, y.m_var_map, y.m_poly);
x.m_poly.intersection_assign(yy.first);
POST();
}
pplite_domain_t operator&(const pplite_domain_t& o) const override {
PPLITE_DOMAIN_SCOPED_STATS(".meet");
PRE();
const auto& x = *this;
const auto& y = o;
if (x.is_bottom() || y.is_top())
return x;
if (y.is_bottom() || x.is_top())
return y;
// Compactness improves efficiency.
x.remove_unconstrained();
y.remove_unconstrained();
var_map_t m = x.m_var_map;
merge_var_map(m, y.m_var_map);
poly_t p = x.m_poly;
extend_to_var_map(m, p);
auto yy = maybe_copy_and_adapt(m, y.m_var_map, y.m_poly);
p.intersection_assign(yy.first);
DUMP_RESULT(m, p);
return pplite_domain_t(std::move(m), std::move(p));
}