acc.cc 55.2 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2015, 2016, 2017 Laboratoire de Recherche et
// Développement de l'Epita.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


#include <iostream>
22
#include <sstream>
23
#include <set>
24
25
#include <cctype>
#include <cstring>
26
#include <map>
27
28
29
30
#include <spot/twa/acc.hh>
#include "spot/priv/bddalloc.hh"
#include <spot/misc/minato.hh>
#include <spot/misc/random.hh>
31
32
33
34
35
36
37
38
39
40
41

namespace spot
{
  std::ostream& operator<<(std::ostream& os, spot::acc_cond::mark_t m)
  {
    auto a = m.id;
    os << '{';
    unsigned level = 0;
    const char* comma = "";
    while (a)
      {
42
43
44
45
46
47
48
        if (a & 1)
          {
            os << comma << level;
            comma = ",";
          }
        a >>= 1;
        ++level;
49
50
51
52
53
      }
    os << '}';
    return os;
  }

54
55
56
57
58
  std::ostream& operator<<(std::ostream& os, const acc_cond& acc)
  {
    return os << '(' << acc.num_sets() << ", " << acc.get_acceptance() << ')';
  }

59
60
  namespace
  {
61
62
63
64
65
    void default_set_printer(std::ostream& os, int v)
    {
      os << v;
    }

66
67
68
    enum code_output {HTML, TEXT, LATEX};

    template<enum code_output style>
69
70
    static void
    print_code(std::ostream& os,
71
72
               const acc_cond::acc_code& code, unsigned pos,
               std::function<void(std::ostream&, int)> set_printer)
73
    {
74
      const char* op_ = style == LATEX ? " \\lor " : " | ";
75
      auto& w = code[pos];
76
77
78
79
80
81
82
83
84
85
86
87
88
      const char* negated_pre = "";
      const char* negated_post = "";
      auto set_neg = [&]() {
        if (style == LATEX)
          {
            negated_pre = "\\overline{";
            negated_post = "}";
          }
        else
          {
            negated_pre = "!";
          }
      };
89
      bool top = pos == code.size() - 1;
90
      switch (w.sub.op)
91
92
        {
        case acc_cond::acc_op::And:
93
94
95
96
97
98
99
100
101
102
103
104
          switch (style)
            {
            case HTML:
              op_ = " &amp; ";
              break;
            case TEXT:
              op_ = " & ";
              break;
            case LATEX:
              op_ = " \\land ";
              break;
            }
105
          SPOT_FALLTHROUGH;
106
107
        case acc_cond::acc_op::Or:
          {
108
            unsigned sub = pos - w.sub.size;
109
110
111
112
113
114
115
116
117
            if (!top)
              os << '(';
            bool first = true;
            while (sub < pos)
              {
                --pos;
                if (first)
                  first = false;
                else
118
119
                  os << op_;
                print_code<style>(os, code, pos, set_printer);
120
                pos -= code[pos].sub.size;
121
122
123
124
125
126
              }
            if (!top)
              os << ')';
          }
          break;
        case acc_cond::acc_op::InfNeg:
127
          set_neg();
128
          SPOT_FALLTHROUGH;
129
130
131
132
133
        case acc_cond::acc_op::Inf:
          {
            auto a = code[pos - 1].mark.id;
            if (a == 0U)
              {
134
135
136
137
                if (style == LATEX)
                  os << "\\mathsf{t}";
                else
                  os << 't';
138
139
140
141
142
143
144
145
              }
            else
              {
                if (!top)
                  // Avoid extra parentheses if there is only one set
                  top = code[pos - 1].mark.count() == 1;
                unsigned level = 0;
                const char* and_ = "";
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
                const char* and_next_ = []() {
                  // The lack of surrounding space in HTML and
                  // TEXT is on purpose: we want to
                  // distinguish those grouped "Inf"s from
                  // other terms that are ANDed together.
                  switch (style)
                    {
                    case HTML:
                      return "&amp;";
                    case TEXT:
                      return "&";
                    case LATEX:
                      return " \\land ";
                    }
                }();
161
162
                if (!top)
                  os << '(';
163
                const char* inf_ = (style == LATEX) ? "\\mathsf{Inf}(" : "Inf(";
164
165
166
167
                while (a)
                  {
                    if (a & 1)
                      {
168
                        os << and_ << inf_ << negated_pre;
169
                        set_printer(os, level);
170
171
                        os << negated_post << ')';
                        and_ = and_next_;
172
173
174
175
176
177
178
179
180
181
                      }
                    a >>= 1;
                    ++level;
                  }
                if (!top)
                  os << ')';
              }
          }
          break;
        case acc_cond::acc_op::FinNeg:
182
          set_neg();
183
          SPOT_FALLTHROUGH;
184
185
186
187
188
        case acc_cond::acc_op::Fin:
          {
            auto a = code[pos - 1].mark.id;
            if (a == 0U)
              {
189
190
191
192
                if (style == LATEX)
                  os << "\\mathsf{f}";
                else
                  os << 'f';
193
194
195
196
197
198
199
200
201
202
              }
            else
              {
                if (!top)
                  // Avoid extra parentheses if there is only one set
                  top = code[pos - 1].mark.count() == 1;
                unsigned level = 0;
                const char* or_ = "";
                if (!top)
                  os << '(';
203
                const char* fin_ = (style == LATEX) ? "\\mathsf{Fin}(" : "Fin(";
204
205
206
207
                while (a)
                  {
                    if (a & 1)
                      {
208
                        os << or_ << fin_ << negated_pre;
209
                        set_printer(os, level);
210
211
212
213
214
215
                        os << negated_post << ')';
                        // The lack of surrounding space in HTML and
                        // TEXT is on purpose: we want to distinguish
                        // those grouped "Fin"s from other terms that
                        // are ORed together.
                        or_ = style == LATEX ? " \\lor " : "|";
216
217
218
219
220
221
222
223
224
225
                      }
                    a >>= 1;
                    ++level;
                  }
                if (!top)
                  os << ')';
              }
          }
          break;
        }
226
227
228
229
    }


    static bool
230
    eval(acc_cond::mark_t inf, const acc_cond::acc_word* pos)
231
    {
232
      switch (pos->sub.op)
233
234
235
        {
        case acc_cond::acc_op::And:
          {
236
            auto sub = pos - pos->sub.size;
237
238
239
240
241
            while (sub < pos)
              {
                --pos;
                if (!eval(inf, pos))
                  return false;
242
                pos -= pos->sub.size;
243
244
245
246
247
              }
            return true;
          }
        case acc_cond::acc_op::Or:
          {
248
            auto sub = pos - pos->sub.size;
249
250
251
252
253
            while (sub < pos)
              {
                --pos;
                if (eval(inf, pos))
                  return true;
254
                pos -= pos->sub.size;
255
256
257
258
259
260
261
262
263
264
265
              }
            return false;
          }
        case acc_cond::acc_op::Inf:
          return (pos[-1].mark & inf) == pos[-1].mark;
        case acc_cond::acc_op::Fin:
          return (pos[-1].mark & inf) != pos[-1].mark;
        case acc_cond::acc_op::FinNeg:
        case acc_cond::acc_op::InfNeg:
          SPOT_UNREACHABLE();
        }
266
267
268
      SPOT_UNREACHABLE();
      return false;
    }
269

270
271
272
273
    static trival
    partial_eval(acc_cond::mark_t infinitely_often,
                 acc_cond::mark_t always_present,
                 const acc_cond::acc_word* pos)
274
    {
275
      switch (pos->sub.op)
276
277
278
        {
        case acc_cond::acc_op::And:
          {
279
            auto sub = pos - pos->sub.size;
280
            trival res = true;
281
282
283
            while (sub < pos)
              {
                --pos;
284
285
286
287
                res = res &&
                  partial_eval(infinitely_often, always_present, pos);
                if (res.is_false())
                  return res;
288
                pos -= pos->sub.size;
289
              }
290
            return res;
291
292
293
          }
        case acc_cond::acc_op::Or:
          {
294
            auto sub = pos - pos->sub.size;
295
            trival res = false;
296
297
298
            while (sub < pos)
              {
                --pos;
299
300
301
302
                res = res ||
                  partial_eval(infinitely_often, always_present, pos);
                if (res.is_true())
                  return res;
303
                pos -= pos->sub.size;
304
              }
305
            return res;
306
307
          }
        case acc_cond::acc_op::Inf:
308
          return (pos[-1].mark & infinitely_often) == pos[-1].mark;
309
        case acc_cond::acc_op::Fin:
310
          if ((pos[-1].mark & always_present) == pos[-1].mark)
311
            return false;
312
          else if ((pos[-1].mark & infinitely_often) != pos[-1].mark)
313
            return true;
314
315
          else
            return trival::maybe();
316
317
318
319
        case acc_cond::acc_op::FinNeg:
        case acc_cond::acc_op::InfNeg:
          SPOT_UNREACHABLE();
        }
320
321
322
323
      SPOT_UNREACHABLE();
      return false;
    }

324
325
326
    static acc_cond::mark_t
    eval_sets(acc_cond::mark_t inf, const acc_cond::acc_word* pos)
    {
327
      switch (pos->sub.op)
328
329
330
        {
        case acc_cond::acc_op::And:
          {
331
            auto sub = pos - pos->sub.size;
332
333
334
335
336
337
338
339
            acc_cond::mark_t m = 0U;
            while (sub < pos)
              {
                --pos;
                if (auto s = eval_sets(inf, pos))
                  m |= s;
                else
                  return 0U;
340
                pos -= pos->sub.size;
341
342
343
344
345
              }
            return m;
          }
        case acc_cond::acc_op::Or:
          {
346
            auto sub = pos - pos->sub.size;
347
348
349
350
351
            while (sub < pos)
              {
                --pos;
                if (auto s = eval_sets(inf, pos))
                  return s;
352
                pos -= pos->sub.size;
353
354
355
356
357
358
359
360
361
362
363
364
365
              }
            return 0U;
          }
        case acc_cond::acc_op::Inf:
          if ((pos[-1].mark & inf) == pos[-1].mark)
            return pos[-1].mark;
          else
            return 0U;
        case acc_cond::acc_op::Fin:
        case acc_cond::acc_op::FinNeg:
        case acc_cond::acc_op::InfNeg:
          SPOT_UNREACHABLE();
        }
366
      SPOT_UNREACHABLE();
367
      return 0U;
368
    }
369
370
  }

371
  bool acc_cond::acc_code::accepting(mark_t inf) const
372
  {
373
    if (empty())
374
      return true;
375
    return eval(inf, &back());
376
377
  }

378
379
  trival acc_cond::acc_code::maybe_accepting(mark_t infinitely_often,
                                             mark_t always_present) const
380
  {
381
    if (empty())
382
      return true;
383
384
    return partial_eval(infinitely_often | always_present,
                        always_present, &back());
385
386
  }

387
388
389
390
391
392
  bool acc_cond::acc_code::inf_satisfiable(mark_t inf) const
  {
    return !maybe_accepting(inf, 0U).is_false();
  }


393
394
395
396
  acc_cond::mark_t acc_cond::accepting_sets(mark_t inf) const
  {
    if (uses_fin_acceptance())
      throw std::runtime_error
397
        ("Fin acceptance is not supported by this code path.");
398
399
400
401
402
    if (code_.empty())
      return 0U;
    return eval_sets(inf, &code_.back());
  }

403
404
405
406
407
408
409
  bool acc_cond::check_fin_acceptance() const
  {
    if (code_.empty())
      return false;
    unsigned pos = code_.size();
    do
      {
410
        switch (code_[pos - 1].sub.op)
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
          {
          case acc_cond::acc_op::And:
          case acc_cond::acc_op::Or:
            --pos;
            break;
          case acc_cond::acc_op::Inf:
          case acc_cond::acc_op::InfNeg:
            pos -= 2;
            break;
          case acc_cond::acc_op::Fin:
            if (code_[pos - 2].mark == 0U) // f
              {
                pos -= 2;
                break;
              }
426
            SPOT_FALLTHROUGH;
427
428
429
          case acc_cond::acc_op::FinNeg:
            return true;
          }
430
431
432
433
434
      }
    while (pos > 0);
    return false;
  }

435
436
437
438
439
440

  namespace
  {
    // Is Rabin or Streett, depending on highop and lowop.
    static bool
    is_rs(const acc_cond::acc_code& code,
441
442
443
          acc_cond::acc_op highop,
          acc_cond::acc_op lowop,
          acc_cond::mark_t all_sets)
444
    {
445
446
      unsigned s = code.back().sub.size;
      auto mainop = code.back().sub.op;
447
      if (mainop == highop)
448
449
450
451
452
453
454
455
456
        {
          // The size must be a multiple of 5.
          if ((s != code.size() - 1) || (s % 5))
            return false;
        }
      else                        // Single pair?
        {
          if (s != 4 || mainop != lowop)
            return false;
457
          // Pretend we were in a unary highop.
458
459
          s = 5;
        }
460
461
462
      acc_cond::mark_t seen_fin = 0U;
      acc_cond::mark_t seen_inf = 0U;
      while (s)
463
        {
464
          if (code[--s].sub.op != lowop)
465
            return false;
466
          auto o1 = code[--s].sub.op;
467
          auto m1 = code[--s].mark;
468
          auto o2 = code[--s].sub.op;
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
          auto m2 = code[--s].mark;

          // We assume
          //   Fin(n) lowop Inf(n+1)
          //   o1 (m1)       o2 (m2)
          // swap if it is the converse
          if (o2 == acc_cond::acc_op::Fin)
            {
              std::swap(o1, o2);
              std::swap(m1, m2);
            }
          if (o1 != acc_cond::acc_op::Fin
              || o2 != acc_cond::acc_op::Inf
              || m1.count() != 1
              || m2.id != (m1.id << 1))
            return false;
          seen_fin |= m1;
          seen_inf |= m2;
        }
488
489

      return (!(seen_fin & seen_inf)
490
              && (seen_fin | seen_inf) == all_sets);
491
    }
492
493
494
495
496
497

    // Is Rabin-like or Streett-like, depending on highop and lowop.
    static bool
    is_rs_like(const acc_cond::acc_code& code,
              acc_cond::acc_op highop,
              acc_cond::acc_op lowop,
498
              acc_cond::acc_op singleop,
499
500
501
502
503
504
505
506
              std::vector<acc_cond::rs_pair>& pairs)
    {
      assert(pairs.empty());
      unsigned s = code.back().sub.size;
      auto mainop = code.back().sub.op;

      if (mainop == acc_cond::acc_op::Fin || mainop == acc_cond::acc_op::Inf)
        {
507
          assert(code.size() == 2);
508
509

          auto m = code[0].mark;
510
          if (mainop == singleop && m.count() != 1)
511
512
513
514
            return false;

          acc_cond::mark_t fin(0U);
          acc_cond::mark_t inf(0U);
515
516
517
518
519
520
          for (unsigned mark: m.sets())
            {
              if (mainop == acc_cond::acc_op::Fin)
                fin = {mark};
              else
                inf = {mark};
521

522
523
              pairs.emplace_back(fin, inf);
            }
524
525
526
527
528
529
530
531
532
533
          return true;
        }
      else if (mainop == lowop)      // Single pair?
        {
          if (s != 4)
            return false;
          // Pretend we were in a unary highop.
          s = 5;
        }
      else if (mainop != highop)
534
535
536
        {
          return false;
        }
537
538
539
540
541
542
543
544
545
546
547
      while (s)
        {
          auto op = code[--s].sub.op;
          auto size = code[s].sub.size;
          if (op == acc_cond::acc_op::Fin
              || op == acc_cond::acc_op::Inf)
            {
              auto m = code[--s].mark;
              acc_cond::mark_t fin(0U);
              acc_cond::mark_t inf(0U);

548
              if (op == singleop && m.count() != 1)
549
550
551
552
                {
                  pairs.clear();
                  return false;
                }
553
554
555
556
557
558
559
560
              for (unsigned mark: m.sets())
                {
                  if (op == acc_cond::acc_op::Fin)
                    fin = {mark};
                  else //fin
                    inf = {mark};
                  pairs.emplace_back(fin, inf);
                }
561
562
563
564
            }
          else
            {
              if (op != lowop || size != 4)
565
566
567
568
                {
                  pairs.clear();
                  return false;
                }
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587

              auto o1 = code[--s].sub.op;
              auto m1 = code[--s].mark;
              auto o2 = code[--s].sub.op;
              auto m2 = code[--s].mark;

              // We assume
              //   Fin(n) lowop Inf(n+1)
              //   o1 (m1)       o2 (m2)
              // swap if it is the converse
              if (o2 == acc_cond::acc_op::Fin)
                {
                  std::swap(o1, o2);
                  std::swap(m1, m2);
                }
              if (o1 != acc_cond::acc_op::Fin
                  || o2 != acc_cond::acc_op::Inf
                  || m1.count() != 1
                  || m2.count() != 1)
588
589
590
591
                {
                  pairs.clear();
                  return false;
                }
592
593
594
595
596
597
              pairs.emplace_back(m1, m2);
            }
        }

      return true;
    }
598
599
  }

600
601
  int acc_cond::is_rabin() const
  {
602
    if (code_.is_f())
603
      return num_ == 0 ? 0 : -1;
604
    if ((num_ & 1) || code_.is_t())
605
      return -1;
606
607
608
609

    if (is_rs(code_, acc_op::Or, acc_op::And, all_sets()))
      return num_ / 2;
    else
610
611
612
613
614
      return -1;
  }

  int acc_cond::is_streett() const
  {
615
    if (code_.is_t())
616
      return num_ == 0 ? 0 : -1;
617
    if ((num_ & 1) || code_.is_f())
618
      return -1;
619
620
621
622

    if (is_rs(code_, acc_op::And, acc_op::Or, all_sets()))
      return num_ / 2;
    else
623
624
625
      return -1;
  }

626
627
628
629
630
631
632
633
634
635
  bool acc_cond::is_streett_like(std::vector<rs_pair>& pairs) const
  {
    pairs.clear();
    if (code_.is_t())
      return true;
    if (code_.is_f())
      {
        pairs.emplace_back(0U, 0U);
        return true;
      }
636
    return is_rs_like(code_, acc_op::And, acc_op::Or, acc_op::Fin, pairs);
637
638
639
640
641
642
643
644
645
646
647
648
  }

  bool acc_cond::is_rabin_like(std::vector<rs_pair>& pairs) const
  {
    pairs.clear();
    if (code_.is_f())
      return true;
    if (code_.is_t())
      {
        pairs.emplace_back(0U, 0U);
        return true;
      }
649
    return is_rs_like(code_, acc_op::Or, acc_op::And, acc_op::Inf, pairs);
650
651
  }

652

653
  // PAIRS contains the number of Inf in each pair.
654
655
656
657
658
  bool acc_cond::is_generalized_rabin(std::vector<unsigned>& pairs) const
  {
    pairs.clear();
    if (is_generalized_co_buchi())
      {
659
660
        pairs.resize(num_);
        return true;
661
      }
662
    if (code_.is_t()
663
        || code_.back().sub.op != acc_op::Or)
664
      return false;
665

666
    auto s = code_.back().sub.size;
667
668
    acc_cond::mark_t seen_fin = 0U;
    acc_cond::mark_t seen_inf = 0U;
669
    // Each pair is the position of a Fin followed
670
671
    // by the number of Inf.
    std::map<unsigned, unsigned> p;
672
673
    while (s)
      {
674
        --s;
675
        if (code_[s].sub.op == acc_op::And)
676
          {
677
            auto o1 = code_[--s].sub.op;
678
            auto m1 = code_[--s].mark;
679
            auto o2 = code_[--s].sub.op;
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
            auto m2 = code_[--s].mark;

            // We assume
            //   Fin(n) & Inf({n+1,n+2,...,n+i})
            //   o1 (m1)       o2 (m2)
            // swap if it is the converse
            if (o2 == acc_cond::acc_op::Fin)
              {
                std::swap(o1, o2);
                std::swap(m1, m2);
              }

            if (o1 != acc_cond::acc_op::Fin
                || o2 != acc_cond::acc_op::Inf
                || m1.count() != 1)
              return false;

            unsigned i = m2.count();
            // If we have seen this pair already, it must have the
            // same size.
700
            if (p.emplace(m1.max_set() - 1, i).first->second != i)
701
702
703
704
705
706
707
708
709
710
              return false;
            assert(i > 0);
            unsigned j = m1.max_set(); // == n+1
            do
              if (!m2.has(j++))
                return false;
            while (--i);
            seen_fin |= m1;
            seen_inf |= m2;
          }
711
        else if (code_[s].sub.op == acc_op::Fin)
712
713
714
715
716
          {
            auto m1 = code_[--s].mark;
            for (auto s: m1.sets())
              // If we have seen this pair already, it must have the
              // same size.
717
718
719
720
              {
                if (p.emplace(s, 0U).first->second != 0U)
                  return false;
              }
721
722
723
724
725
726
            seen_fin |= m1;
          }
        else
          {
            return false;
          }
727
      }
728
    for (auto i: p)
729
      pairs.emplace_back(i.second);
730
    return (!(seen_fin & seen_inf)
731
            && (seen_fin | seen_inf) == all_sets());
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
  // PAIRS contains the number of Inf in each pair.
  bool acc_cond::is_generalized_streett(std::vector<unsigned>& pairs) const
  {
    pairs.clear();
    if (is_generalized_buchi())
      {
        pairs.resize(num_);
        return true;
      }
    if (code_.is_f()
        || code_.back().sub.op != acc_op::And)
      return false;

    auto s = code_.back().sub.size;
    acc_cond::mark_t seen_fin = 0U;
    acc_cond::mark_t seen_inf = 0U;
    // Each pairs is the position of a Inf followed
    // by the number of Fin.
    std::map<unsigned, unsigned> p;
    while (s)
      {
        --s;
        if (code_[s].sub.op == acc_op::Or)
          {
            auto o1 = code_[--s].sub.op;
            auto m1 = code_[--s].mark;
            auto o2 = code_[--s].sub.op;
            auto m2 = code_[--s].mark;

            // We assume
            //   Inf(n) | Fin({n+1,n+2,...,n+i})
            //   o1 (m1)       o2 (m2)
            // swap if it is the converse
            if (o2 == acc_cond::acc_op::Inf)
              {
                std::swap(o1, o2);
                std::swap(m1, m2);
              }

            if (o1 != acc_cond::acc_op::Inf
                || o2 != acc_cond::acc_op::Fin
                || m1.count() != 1)
              return false;

            unsigned i = m2.count();
            // If we have seen this pair already, it must have the
            // same size.
781
            if (p.emplace(m1.max_set() - 1, i).first->second != i)
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
              return false;
            assert(i > 0);
            unsigned j = m1.max_set(); // == n+1
            do
              if (!m2.has(j++))
                return false;
            while (--i);

            seen_inf |= m1;
            seen_fin |= m2;
          }
        else if (code_[s].sub.op == acc_op::Inf)
          {
            auto m1 = code_[--s].mark;
            for (auto s: m1.sets())
              // If we have seen this pair already, it must have the
              // same size.
              if (p.emplace(s, 0U).first->second != 0U)
                return false;
            seen_inf |= m1;
          }
        else
          {
            return false;
          }
      }
    for (auto i: p)
      pairs.emplace_back(i.second);
    return (!(seen_inf & seen_fin)
            && (seen_inf | seen_fin) == all_sets());
  }

814
815
816
  acc_cond::acc_code
  acc_cond::acc_code::parity(bool max, bool odd, unsigned sets)
  {
817
818
819
820
821
    acc_cond::acc_code res;

    if (max)
      res = odd ? t() : f();
    else
822
      res = ((sets & 1) == odd) ? t() : f();
823

824
    if (sets == 0)
825
826
      return res;

827
828
829
830
831
832
833
834
835
    // When you look at something like
    //    acc-name: parity min even 5
    //    Acceptance: 5 Inf(0) | (Fin(1) & (Inf(2) | (Fin(3) & Inf(4))))
    // remember that we build it from right to left.
    int start = max ? 0 : sets - 1;
    int inc = max ? 1 : -1;
    int end = max ? sets : -1;
    for (int i = start; i != end; i += inc)
      {
836
837
838
839
        if ((i & 1) == odd)
          res |= inf({(unsigned)i});
        else
          res &= fin({(unsigned)i});
840
841
842
843
      }
    return res;
  }

844
845
846
847
848
849
850
851
852
853
854
855
856
857
  acc_cond::acc_code
  acc_cond::acc_code::random(unsigned n_accs, double reuse)
  {
    // With 0 acceptance sets, we always generate the true acceptance.
    // (Working with false is somehow pointless, and the formulas we
    // generate for n_accs>0 are always satisfiable, so it makes sense
    // that it should be satisfiable for n_accs=0 as well.)
    if (n_accs == 0)
      return {};

    std::vector<acc_cond::acc_code> codes;
    codes.reserve(n_accs);
    for (unsigned i = 0; i < n_accs; ++i)
      {
858
        codes.emplace_back(drand() < 0.5 ? inf({i}) : fin({i}));
859
860
        if (reuse > 0.0 && drand() < reuse)
          --i;
861
862
863
864
865
      }

    int s = codes.size();
    while (s > 1)
      {
866
867
        // Pick a random code and put it at the end
        int p1 = mrand(s--);
868
869
        if (p1 != s) // https://gcc.gnu.org/bugzilla//show_bug.cgi?id=59603
          std::swap(codes[p1], codes[s]);
870
871
872
873
874
875
876
877
878
        // and another one
        int p2 = mrand(s);

        if (drand() < 0.5)
          codes[p2] |= std::move(codes.back());
        else
          codes[p2] &= std::move(codes.back());

        codes.pop_back();
879
880
881
882
      }
    return codes[0];
  }

883
884
  namespace
  {
885
    bdd to_bdd_rec(const acc_cond::acc_word* c, const bdd* map)
886
    {
887
      auto sz = c->sub.size;
888
      auto start = c - sz - 1;
889
      auto op = c->sub.op;
890
      switch (op)
891
892
893
894
895
896
897
898
        {
        case acc_cond::acc_op::Or:
          {
            --c;
            bdd res = bddfalse;
            do
              {
                res |= to_bdd_rec(c, map);
899
                c -= c->sub.size + 1;
900
901
902
903
904
905
906
907
908
909
910
              }
            while (c > start);
            return res;
          }
        case acc_cond::acc_op::And:
          {
            --c;
            bdd res = bddtrue;
            do
              {
                res &= to_bdd_rec(c, map);
911
                c -= c->sub.size + 1;
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
              }
            while (c > start);
            return res;
          }
        case acc_cond::acc_op::Fin:
          {
            bdd res = bddfalse;
            for (auto i: c[-1].mark.sets())
              res |= !map[i];
            return res;
          }
        case acc_cond::acc_op::Inf:
          {
            bdd res = bddtrue;
            for (auto i: c[-1].mark.sets())
              res &= map[i];
            return res;
          }
        case acc_cond::acc_op::InfNeg:
        case acc_cond::acc_op::FinNeg:
          SPOT_UNREACHABLE();
          return bddfalse;
        }
935
      SPOT_UNREACHABLE();
936
      return bddfalse;
937
    }
938
939
940

    static bool
    equiv_codes(const acc_cond::acc_code& lhs,
941
                const acc_cond::acc_code& rhs)
942
943
944
945
946
947
948
949
950
951
952
    {
      auto used = lhs.used_sets() | rhs.used_sets();

      unsigned c = used.count();
      unsigned umax = used.max_set();

      bdd_allocator ba;
      int base = ba.allocate_variables(c);
      assert(base == 0);
      std::vector<bdd> r;
      for (unsigned i = 0; r.size() < umax; ++i)
953
        if (used.has(i))
954
          r.emplace_back(bdd_ithvar(base++));
955
        else
956
          r.emplace_back(bddfalse);
957
958
959
960
961
962
963
964
965
      return to_bdd_rec(&lhs.back(), &r[0]) == to_bdd_rec(&rhs.back(), &r[0]);
    }
  }

  bool acc_cond::is_parity(bool& max, bool& odd, bool equiv) const
  {
    unsigned sets = num_;
    if (sets == 0)
      {
966
967
968
        max = true;
        odd = is_t();
        return true;
969
970
971
972
973
974
975
976
      }
    acc_cond::mark_t u_inf;
    acc_cond::mark_t u_fin;
    std::tie(u_inf, u_fin) = code_.used_inf_fin_sets();

    odd = !u_inf.has(0);
    for (auto s: u_inf.sets())
      if ((s & 1) != odd)
977
978
979
980
        {
          max = false;             // just so the value is not uninitialized
          return false;
        }
981
982
983
984

    auto max_code = acc_code::parity(true, odd, sets);
    if (max_code == code_)
      {
985
986
        max = true;
        return true;
987
988
989
990
      }
    auto min_code = acc_code::parity(false, odd, sets);
    if (min_code == code_)
      {
991
992
        max = false;
        return true;
993
994
995
996
997
998
999
      }

    if (!equiv)
      return false;

    if (equiv_codes(code_, max_code))
      {
1000
1001
        max = true;
        return true;
1002
1003
1004
      }
    if (equiv_codes(code_, min_code))
      {
1005
1006
        max = false;
        return true;
1007
1008
      }
    return false;
1009
1010
1011
1012
  }

  acc_cond::acc_code acc_cond::acc_code::to_dnf() const
  {
1013
    if (empty() || size() == 2)
1014
      return *this;
1015
1016
1017

    auto used = acc_cond::acc_code::used_sets();
    unsigned c = used.count();
1018
    unsigned max = used.max_set();
1019
1020
1021

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
1022
    assert(base == 0);
1023
1024
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
1025
    for (unsigned i = 0; r.size() < max; ++i)
1026
      {
1027
1028
1029
        if (used.has(i))
          {
            sets[base] = i;
1030
            r.emplace_back(bdd_ithvar(base++));
1031
1032
1033
          }
        else
          {
1034
            r.emplace_back(bddfalse);
1035
          }
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
      }

    bdd res = to_bdd_rec(&back(), &r[0]);

    if (res == bddtrue)
      return t();
    if (res == bddfalse)
      return f();

    minato_isop isop(res);
    bdd cube;
    acc_code rescode = f();
    while ((cube = isop.next()) != bddfalse)
      {
1050
        mark_t i = 0U;
1051
        acc_code f;
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
        while (cube != bddtrue)
          {
            // The acceptance set associated to this BDD variable
            mark_t s = 0U;
            s.set(sets[bdd_var(cube)]);

            bdd h = bdd_high(cube);
            if (h == bddfalse)        // Negative variable? -> Fin
              {
                cube = bdd_low(cube);
                // The strange order here make sure we can smaller set
                // numbers at the end of the acceptance code, i.e., at
                // the front of the output.
1065
                f = fin(s) & f;
1066
1067
1068
1069
1070
1071
1072
1073
              }
            else                // Positive variable? -> Inf
              {
                i |= s;
                cube = h;
              }
          }
        // See comment above for the order.
1074
        rescode = (inf(i) & f) | rescode;
1075
      }
1076

1077
    return rescode;
1078
1079
  }

1080
1081
1082
1083
1084
1085
1086
  acc_cond::acc_code acc_cond::acc_code::to_cnf() const
  {
    if (empty() || size() == 2)
      return *this;

    auto used = acc_cond::acc_code::used_sets();
    unsigned c = used.count();
1087
    unsigned max = used.max_set();
1088
1089
1090

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
1091
    assert(base == 0);
1092
1093
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
1094
    for (unsigned i = 0; r.size() < max; ++i)
1095
      {
1096
1097
1098
        if (used.has(i))
          {
            sets[base] = i;
1099
            r.emplace_back(bdd_ithvar(base++));
1100
1101
1102
          }
        else
          {
1103
            r.emplace_back(bddfalse);
1104
          }
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
      }

    bdd res = to_bdd_rec(&back(), &r[0]);

    if (res == bddtrue)
      return t();
    if (res == bddfalse)
      return f();

    minato_isop isop(!res);
    bdd cube;
    acc_code rescode;
    while ((cube = isop.next()) != bddfalse)
      {
1119
        mark_t m = 0U;
1120
        acc_code i = f();
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
        while (cube != bddtrue)
          {
            // The acceptance set associated to this BDD variable
            mark_t s = 0U;
            s.set(sets[bdd_var(cube)]);

            bdd h = bdd_high(cube);
            if (h == bddfalse)        // Negative variable? -> Inf
              {
                cube = bdd_low(cube);
                // The strange order here make sure we can smaller set
                // numbers at the end of the acceptance code, i.e., at
                // the front of the output.
1134
                i = inf(s) | i;
1135
1136
1137
1138
1139
1140
1141
1142
              }
            else                // Positive variable? -> Fin
              {
                m |= s;
                cube = h;
              }
          }
        // See comment above for the order.
1143
        rescode = (fin(m) | i) & rescode;
1144
1145
1146
1147
      }
    return rescode;
  }

1148
  std::pair<bool, acc_cond::mark_t>
1149
  acc_cond::unsat_mark() const
1150
  {
1151
    if (is_t())
1152
      return {false, 0U};
1153
1154
    if (!uses_fin_acceptance())
      return {true, 0U};
1155

1156
    auto used = code_.used_sets();
1157
    unsigned c = used.count();
1158
    unsigned max = used.max_set();
1159
1160
1161
1162
1163
1164

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
    assert(base == 0);
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
1165
    for (unsigned i = 0; r.size() < max; ++i)
1166
      {
1167
1168
1169
        if (used.has(i))
          {
            sets[base] = i;
1170
            r.emplace_back(bdd_ithvar(base++));
1171
1172
1173
          }
        else
          {
1174
            r.emplace_back(bddfalse);
1175
          }
1176
1177
      }

1178
    bdd res = to_bdd_rec(&code_.back(), &r[0]);
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188

    if (res == bddtrue)
      return {false, 0U};
    if (res == bddfalse)
      return {true, 0U};

    bdd cube = bdd_satone(!res);
    mark_t i = 0U;
    while (cube != bddtrue)
      {
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
        // The acceptance set associated to this BDD variable
        int s = sets[bdd_var(cube)];

        bdd h = bdd_high(cube);
        if (h == bddfalse)        // Negative variable? -> skip
          {
            cube = bdd_low(cube);
          }
        else                // Positive variable? -> Inf
          {
            i.set(s);
            cube = h;
          }
1202
1203
1204
1205
      }
    return {true, i};
  }

1206
1207
1208
1209
1210
  std::vector<std::vector<int>>
  acc_cond::acc_code::missing(mark_t inf, bool accepting) const
  {
    if (empty())
      {
1211
1212
1213
1214
1215
1216
        if (accepting)
          return {};
        else
          return {
            {}
          };
1217
1218
1219
      }
    auto used = acc_cond::acc_code::used_sets();
    unsigned c = used.count();
1220
    unsigned max = used.max_set();
1221
1222
1223
1224
1225
1226
1227

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
    assert(base == 0);
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
    bdd known = bddtrue;
1228
    for (unsigned i = 0; r.size() < max; ++i)
1229
      {
1230
1231
1232
1233
        if (used.has(i))
          {
            sets[base] = i;
            bdd v = bdd_ithvar(base++);
1234
            r.emplace_back(v);
1235
1236
1237
1238
1239
            if (inf.has(i))
              known &= v;
          }
        else
          {
1240
            r.emplace_back(bddfalse);
1241
          }
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
      }

    bdd res = to_bdd_rec(&back(), &r[0]);

    res = bdd_restrict(res, known);

    if (accepting)
      res = !res;

    if (res == bddfalse)
      return {};

    minato_isop isop(res);
    bdd cube;
    std::vector<std::vector<int>> result;
    while ((cube = isop.next()) != bddfalse)
      {
1259
1260
1261
1262
1263
1264
1265
1266
1267
        std::vector<int> partial;
        while (cube != bddtrue)
          {
            // The acceptance set associated to this BDD variable
            int s = sets[bdd_var(cube)];

            bdd h = bdd_high(cube);
            if (h == bddfalse)        // Negative variable
              {
1268
                partial.emplace_back(s);
1269
1270
1271
1272
                cube = bdd_low(cube);
              }
            else                // Positive variable
              {
1273
                partial.emplace_back(-s - 1);
1274
1275
1276
1277
                cube = h;
              }
          }
        result.emplace_back(std::move(partial));
1278
1279
1280
1281
      }
    return result;
  }

1282
1283
  bool acc_cond::acc_code::is_dnf() const
  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1284
    if (empty() || size() == 2)
1285
1286
1287