acc.hh 25.1 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2014, 2015 Laboratoire de Recherche et Développement de
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// l'Epita.
//
// 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/>.

20
#pragma once
21

22
23
24
25
#include <functional>
#include <unordered_map>
#include <sstream>
#include <vector>
26
#include <spot/tl/defaultenv.hh>
27
#include <iostream>
28
29
30

namespace spot
{
31
  class SPOT_API acc_cond
32
33
34
35
36
37
38
39
40
  {
  public:
    struct mark_t
    {
      typedef unsigned value_t;
      value_t id;

      mark_t() = default;

41
      mark_t(value_t id) noexcept
42
43
44
45
	: id(id)
      {
      }

46
      template<class iterator>
47
      mark_t(const iterator& begin, const iterator& end) noexcept
48
49
50
51
52
53
      {
	id = 0U;
	for (iterator i = begin; i != end; ++i)
	  set(*i);
      }

54
      mark_t(std::initializer_list<unsigned> vals) noexcept
55
56
57
58
	: mark_t(vals.begin(), vals.end())
      {
      }

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
      bool operator==(unsigned o) const
      {
	assert(o == 0U);
	return id == o;
      }

      bool operator!=(unsigned o) const
      {
	assert(o == 0U);
	return id != o;
      }

      bool operator==(mark_t o) const
      {
	return id == o.id;
      }

      bool operator!=(mark_t o) const
      {
	return id != o.id;
      }

      bool operator<(mark_t o) const
      {
	return id < o.id;
      }

      bool operator<=(mark_t o) const
      {
	return id <= o.id;
      }

      bool operator>(mark_t o) const
      {
	return id > o.id;
      }

      bool operator>=(mark_t o) const
      {
	return id >= o.id;
      }

      operator bool() const
      {
	return id != 0;
      }

      bool has(unsigned u) const
      {
	return id & (1U << u);
109
110
111
112
113
      }

      void set(unsigned u)
      {
	id |= (1U << u);
114
115
      }

116
117
118
119
120
      void clear(unsigned u)
      {
	id &= ~(1U << u);
      }

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
      mark_t& operator&=(mark_t r)
      {
	id &= r.id;
	return *this;
      }

      mark_t& operator|=(mark_t r)
      {
	id |= r.id;
	return *this;
      }

      mark_t& operator-=(mark_t r)
      {
	id &= ~r.id;
	return *this;
      }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
139
140
141
142
143
144
      mark_t& operator^=(mark_t r)
      {
	id ^= r.id;
	return *this;
      }

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
      mark_t operator&(mark_t r) const
      {
	return id & r.id;
      }

      mark_t operator|(mark_t r) const
      {
	return id | r.id;
      }

      mark_t operator-(mark_t r) const
      {
	return id & ~r.id;
      }

160
161
162
163
164
      mark_t operator~() const
      {
	return ~id;
      }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
165
166
167
168
169
      mark_t operator^(mark_t r) const
      {
	return id ^ r.id;
      }

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
      mark_t operator<<(int i) const
      {
	return id << i;
      }

      mark_t& operator<<=(int i)
      {
	id <<= i;
	return *this;
      }

      mark_t operator>>(int i) const
      {
	return id >> i;
      }

      mark_t& operator>>=(int i)
      {
	id >>= i;
	return *this;
      }

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
      mark_t strip(mark_t y) const
      {
	// strip every bit of id that is marked in y
	//       100101110100.strip(
	//       001011001000)
	//   ==  10 1  11 100
	//   ==      10111100

	auto xv = id;		// 100101110100
	auto yv = y.id;		// 001011001000

	while (yv && xv)
	  {
	    // Mask for everything after the last 1 in y
	    auto rm = (~yv) & (yv - 1);	// 000000000111
	    // Mask for everything before the last 1 in y
	    auto lm = ~(yv ^ (yv - 1));	// 111111110000
	    xv = ((xv & lm) >> 1) | (xv & rm);
	    yv = (yv & lm) >> 1;
	  }
	return xv;
      }

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
      // Number of bits sets.
      unsigned count() const
      {
#ifdef __GNUC__
	return __builtin_popcount(id);
#else
	unsigned c = 0U;
	auto v = id;
	while (v)
	  {
	    ++c;
	    v &= v - 1;
	  }
	return c;
#endif
      }

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
      // Return the number of the highest set used plus one.
      // So if no set is used, this returns 0,
      // but if the sets {1,3,8} are used, this returns 9.
      unsigned max_set() const
      {
	auto i = id;
	int res = 0;
	while (i)
	  {
	    ++res;
	    i >>= 1;
	  }
	return res;
      }

247
248
249
250
251
252
      // Return the lowest acceptance mark
      mark_t lowest() const
      {
	return id & -id;
      }

253
254
255
256
257
258
259
260
      // Remove n bits that where set
      mark_t& remove_some(unsigned n)
      {
	while (n--)
	  id &= id - 1;
	return *this;
      }

261
262
      template<class iterator>
      void fill(iterator here) const
263
      {
264
	auto a = id;
265
266
267
268
	unsigned level = 0;
	while (a)
	  {
	    if (a & 1)
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
	      *here++ = level;
	    ++level;
	    a >>= 1;
	  }
      }

      // FIXME: Return some iterable object without building a vector.
      std::vector<unsigned> sets() const
      {
	std::vector<unsigned> res;
	fill(std::back_inserter(res));
	return res;
      }

      SPOT_API
      friend std::ostream& operator<<(std::ostream& os, mark_t m);
    };

    // This encodes either an operator or set of acceptance sets.
288
    enum class acc_op : unsigned short
289
290
291
292
293
    { Inf, Fin, InfNeg, FinNeg, And, Or };
    union acc_word
    {
      mark_t mark;
      struct {
294
295
296
	acc_op op;	     // Operator
	unsigned short size; // Size of the subtree (number of acc_word),
			     // not counting this node.
297
298
299
      };
    };

300
    struct SPOT_API acc_code: public std::vector<acc_word>
301
    {
302
      bool operator==(const acc_code& other) const
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
      {
	unsigned pos = size();
	if (other.size() != pos)
	  return false;
	while (pos > 0)
	  {
	    auto op = (*this)[pos - 1].op;
	    auto sz = (*this)[pos - 1].size;
	    if (other[pos - 1].op != op ||
		other[pos - 1].size != sz)
	      return false;
	    switch (op)
	      {
	      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:
	      case acc_cond::acc_op::Fin:
	      case acc_cond::acc_op::FinNeg:
		pos -= 2;
		if (other[pos].mark != (*this)[pos].mark)
		  return false;
		break;
	      }
	  }
	return true;
      };

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
      bool operator<(const acc_code& other) const
      {
	unsigned pos = size();
	auto osize = other.size();
	if (pos < osize)
	  return true;
	if (pos > osize)
	  return false;
	while (pos > 0)
	  {
	    auto op = (*this)[pos - 1].op;
	    auto oop = other[pos - 1].op;
	    if (op < oop)
	      return true;
	    if (op > oop)
	      return false;
	    auto sz = (*this)[pos - 1].size;
	    auto osz = other[pos - 1].size;
	    if (sz < osz)
	      return true;
	    if (sz > osz)
	      return false;
	    switch (op)
	      {
	      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:
	      case acc_cond::acc_op::Fin:
	      case acc_cond::acc_op::FinNeg:
		pos -= 2;
		auto m = (*this)[pos].mark;
		auto om = other[pos].mark;
		if (m < om)
		  return true;
		if (m > om)
		  return false;
		break;
	      }
	  }
	return false;
      }

      bool operator>(const acc_code& other) const
      {
	return other < *this;
      }

      bool operator<=(const acc_code& other) const
      {
	return !(other < *this);
      }

      bool operator>=(const acc_code& other) const
      {
	return !(*this < other);
      }

      bool operator!=(const acc_code& other) const
394
395
396
397
      {
	return !(*this == other);
      }

398
      bool is_tt() const
399
400
401
402
403
404
      {
	unsigned s = size();
	return s == 0
	  || ((*this)[s - 1].op == acc_op::Inf && (*this)[s - 2].mark == 0U);
      }

405
      bool is_ff() const
406
407
408
409
410
411
      {
	unsigned s = size();
	return s > 1
	  && (*this)[s - 1].op == acc_op::Fin && (*this)[s - 2].mark == 0U;
      }

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
      static acc_code f()
      {
	acc_code res;
	res.resize(2);
	res[0].mark = 0U;
	res[1].op = acc_op::Fin;
	res[1].size = 1;
	return res;
      }

      static acc_code t()
      {
	return {};
      }

      static acc_code fin(mark_t m)
      {
	acc_code res;
	res.resize(2);
	res[0].mark = m;
	res[1].op = acc_op::Fin;
	res[1].size = 1;
	return res;
      }

437
438
439
440
441
      static acc_code fin(std::initializer_list<unsigned> vals)
      {
	return fin(mark_t(vals));
      }

442
443
444
445
446
447
448
449
450
451
      static acc_code fin_neg(mark_t m)
      {
	acc_code res;
	res.resize(2);
	res[0].mark = m;
	res[1].op = acc_op::FinNeg;
	res[1].size = 1;
	return res;
      }

452
453
454
455
456
      static acc_code fin_neg(std::initializer_list<unsigned> vals)
      {
	return fin_neg(mark_t(vals));
      }

457
458
459
460
461
462
463
464
465
466
      static acc_code inf(mark_t m)
      {
	acc_code res;
	res.resize(2);
	res[0].mark = m;
	res[1].op = acc_op::Inf;
	res[1].size = 1;
	return res;
      }

467
468
469
470
471
      static acc_code inf(std::initializer_list<unsigned> vals)
      {
	return inf(mark_t(vals));
      }

472
473
474
475
476
477
478
479
480
481
      static acc_code inf_neg(mark_t m)
      {
	acc_code res;
	res.resize(2);
	res[0].mark = m;
	res[1].op = acc_op::InfNeg;
	res[1].size = 1;
	return res;
      }

482
483
484
485
486
      static acc_code inf_neg(std::initializer_list<unsigned> vals)
      {
	return inf_neg(mark_t(vals));
      }

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
      static acc_code buchi()
      {
	return inf({0});
      }

      static acc_code cobuchi()
      {
	return fin({0});
      }

      static acc_code generalized_buchi(unsigned n)
      {
	acc_cond::mark_t m = (1U << n) - 1;
	return inf(m);
      }

      static acc_code generalized_co_buchi(unsigned n)
      {
	acc_cond::mark_t m = (1U << n) - 1;
	return fin(m);
      }

      // n is a number of pairs.
      static acc_code rabin(unsigned n)
      {
	acc_cond::acc_code res = f();
	while (n > 0)
	  {
	    acc_cond::acc_code pair = inf({2*n - 1});
	    pair.append_and(fin({2*n - 2}));
	    res.append_or(std::move(pair));
	    --n;
	  }
	return res;
      }

       // n is a number of pairs.
      static acc_code streett(unsigned n)
      {
	acc_cond::acc_code res = t();
	while (n > 0)
	  {
	    acc_cond::acc_code pair = inf({2*n - 1});
	    pair.append_or(fin({2*n - 2}));
	    res.append_and(std::move(pair));
	    --n;
	  }
	return res;
      }

537
538
      template<class Iterator>
      static acc_code generalized_rabin(Iterator begin, Iterator end)
539
540
541
      {
	acc_cond::acc_code res = f();
	unsigned n = 0;
542
	for (Iterator i = begin; i != end; ++i)
543
544
545
546
	  {
	    acc_cond::acc_code pair = fin({n++});
	    acc_cond::mark_t m = 0U;
	    for (unsigned ni = *i; ni > 0; --ni)
547
	      m.set(n++);
548
549
550
551
552
553
554
	    pair.append_and(inf(m));
	    std::swap(pair, res);
	    res.append_or(std::move(pair));
	  }
	return res;
      }

555
556
      static acc_code parity(bool max, bool odd, unsigned sets);

557
558
559
560
561
      // Number of acceptance sets to use, and probability to reuse
      // each set another time after it has been used in the
      // acceptance formula.
      static acc_code random(unsigned n, double reuse = 0.0);

562
563
      void append_and(acc_code&& r)
      {
564
	if (is_tt() || r.is_ff())
565
566
567
568
	  {
	    *this = std::move(r);
	    return;
	  }
569
	if (is_ff() || r.is_tt())
570
571
572
	  return;
	unsigned s = size() - 1;
	unsigned rs = r.size() - 1;
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
	// We want to group all Inf(x) operators:
	//   Inf(a) & Inf(b) = Inf(a & b)
	if (((*this)[s].op == acc_op::Inf && r[rs].op == acc_op::Inf)
	    || ((*this)[s].op == acc_op::InfNeg && r[rs].op == acc_op::InfNeg))
	  {
	    (*this)[s - 1].mark |= r[rs - 1].mark;
	    return;
	  }

	// In the more complex scenarios, left and right may both
	// be conjunctions, and Inf(x) might be a member of each
	// side.  Find it if it exists.
	// left_inf points to the left Inf mark if any.
	// right_inf points to the right Inf mark if any.
	acc_word* left_inf = nullptr;
	if ((*this)[s].op == acc_op::And)
	  {
	    auto start = &(*this)[s] - (*this)[s].size;
	    auto pos = &(*this)[s] - 1;
	    pop_back();
	    while (pos > start)
	      {
		if (pos->op == acc_op::Inf)
		  {
		    left_inf = pos - 1;
		    break;
		  }
		pos -= pos->size + 1;
	      }
	  }
	else if ((*this)[s].op == acc_op::Inf)
	  {
	    left_inf = &(*this)[s - 1];
	  }

	acc_word* right_inf = nullptr;
	auto right_end = &r.back();
	if (right_end->op == acc_op::And)
	  {
	    auto start = &r[0];
	    auto pos = --right_end;
	    while (pos > start)
	    {
	      if (pos->op == acc_op::Inf)
		{
		  right_inf = pos - 1;
		  break;
		}
	      pos -= pos->size + 1;
	    }
	  }
	else if (right_end->op == acc_op::Inf)
	  {
	    right_inf = right_end - 1;
	  }

	if (left_inf && right_inf)
	  {
	    left_inf->mark |= right_inf->mark;
	    insert(this->end(), &r[0], right_inf);
	    insert(this->end(), right_inf + 2, right_end + 1);
	  }
	else if (right_inf)
	  {
	    // Always insert Inf() at the very first entry.
	    insert(this->begin(), right_inf, right_inf + 2);
	    insert(this->end(), &r[0], right_inf);
	    insert(this->end(), right_inf + 2, right_end + 1);
	  }
	else
	  {
	    insert(this->end(), &r[0], right_end + 1);
	  }

	acc_word w;
	w.op = acc_op::And;
	w.size = size();
	push_back(w);
      }

      void append_and(const acc_code& r)
      {
655
	if (is_tt() || r.is_ff())
656
657
658
659
	  {
	    *this = r;
	    return;
	  }
660
	if (is_ff() || r.is_tt())
661
662
663
	  return;
	unsigned s = size() - 1;
	unsigned rs = r.size() - 1;
664
665
666
667
668
669
670
	// Inf(a) & Inf(b) = Inf(a & b)
	if (((*this)[s].op == acc_op::Inf && r[rs].op == acc_op::Inf)
	    || ((*this)[s].op == acc_op::InfNeg && r[rs].op == acc_op::InfNeg))
	  {
	    (*this)[s - 1].mark |= r[rs - 1].mark;
	    return;
	  }
671
672
673
674
675
676
677

	// In the more complex scenarios, left and right may both
	// be conjunctions, and Inf(x) might be a member of each
	// side.  Find it if it exists.
	// left_inf points to the left Inf mark if any.
	// right_inf points to the right Inf mark if any.
	acc_word* left_inf = nullptr;
678
	if ((*this)[s].op == acc_op::And)
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
	  {
	    auto start = &(*this)[s] - (*this)[s].size;
	    auto pos = &(*this)[s] - 1;
	    pop_back();
	    while (pos > start)
	      {
		if (pos->op == acc_op::Inf)
		  {
		    left_inf = pos - 1;
		    break;
		  }
		pos -= pos->size + 1;
	      }
	  }
	else if ((*this)[s].op == acc_op::Inf)
	  {
	    left_inf = &(*this)[s - 1];
	  }

	const acc_word* right_inf = nullptr;
	auto right_end = &r.back();
	if (right_end->op == acc_op::And)
	  {
	    auto start = &r[0];
	    auto pos = --right_end;
	    while (pos > start)
	    {
	      if (pos->op == acc_op::Inf)
		{
		  right_inf = pos - 1;
		  break;
		}
	      pos -= pos->size + 1;
	    }
	  }
	else if (right_end->op == acc_op::Inf)
	  {
	    right_inf = right_end - 1;
	  }

	if (left_inf && right_inf)
	  {
	    left_inf->mark |= right_inf->mark;
	    insert(this->end(), &r[0], right_inf);
	    insert(this->end(), right_inf + 2, right_end + 1);
	  }
	else if (right_inf)
	  {
	    // Always insert Inf() at the very first entry.
	    insert(this->begin(), right_inf, right_inf + 2);
	    insert(this->end(), &r[0], right_inf);
	    insert(this->end(), right_inf + 2, right_end + 1);
	  }
	else
	  {
	    insert(this->end(), &r[0], right_end + 1);
	  }

737
738
739
740
741
742
743
744
	acc_word w;
	w.op = acc_op::And;
	w.size = size();
	push_back(w);
      }

      void append_or(acc_code&& r)
      {
745
	if (is_tt() || r.is_ff())
746
	  return;
747
	if (is_ff() || r.is_tt())
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
	  {
	    *this = std::move(r);
	    return;
	  }
	unsigned s = size() - 1;
	unsigned rs = r.size() - 1;
	// Fin(a) | Fin(b) = Fin(a | b)
	if (((*this)[s].op == acc_op::Fin && r[rs].op == acc_op::Fin)
	    || ((*this)[s].op == acc_op::FinNeg && r[rs].op == acc_op::FinNeg))
	  {
	    (*this)[s - 1].mark |= r[rs - 1].mark;
	    return;
	  }
	if ((*this)[s].op == acc_op::Or)
	  pop_back();
763
764
	if (r.back().op == acc_op::Or)
	  r.pop_back();
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
	insert(this->end(), r.begin(), r.end());
	acc_word w;
	w.op = acc_op::Or;
	w.size = size();
	push_back(w);
      }

      void shift_left(unsigned sets)
      {
	if (empty())
	  return;
	unsigned pos = size();
	do
	  {
	    switch ((*this)[pos - 1].op)
780
	      {
781
782
783
784
785
786
787
788
789
790
791
	      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:
	      case acc_cond::acc_op::Fin:
	      case acc_cond::acc_op::FinNeg:
		pos -= 2;
		(*this)[pos].mark.id <<= sets;
		break;
792
793
	      }
	  }
794
	while (pos > 0);
795
      }
796

797
      bool is_dnf() const;
798
      bool is_cnf() const;
799

800
      acc_code to_dnf() const;
801
      acc_code to_cnf() const;
802

803
804
      acc_code complement() const;

805
806
807
808
809
810
811
812
813
814
815
816
      // Return a list of acceptance marks needed to close a cycle
      // that already visit INF infinitely often, so that the cycle is
      // accepting (ACCEPTING=true) or rejecting (ACCEPTING=false).
      // Positive values describe positive set.
      // A negative value x means the set -x-1 must be absent.
      std::vector<std::vector<int>>
	missing(mark_t inf, bool accepting) const;

      bool accepting(mark_t inf) const;

      bool inf_satisfiable(mark_t inf) const;

817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
      // Remove all the acceptance sets in rem.
      //
      // If MISSING is set, the acceptance sets are assumed to be
      // missing from the automaton, and the acceptance is updated to
      // reflect this.  For instance (Inf(1)&Inf(2))|Fin(3) will
      // become Fin(3) if we remove 2 because it is missing from this
      // automaton, because there is no way to fulfill Inf(1)&Inf(2)
      // in this case.  So essentially MISSING causes Inf(rem) to
      // become f, and Fin(rem) to become t.
      //
      // If MISSING is unset, Inf(rem) become t while Fin(rem) become
      // f.  Removing 2 from (Inf(1)&Inf(2))|Fin(3) would then give
      // Inf(1)|Fin(3).
      acc_code strip(acc_cond::mark_t rem, bool missing) const;

      // Return the set of sets appearing in the condition.
      acc_cond::mark_t used_sets() const;

835
836
837
838
      // Return (true, m) if there exist some m that does not satisfy
      // the acceptance condition.  Return (false, 0U) otherwise.
      std::pair<bool, acc_cond::mark_t> unsat_mark() const;

839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
      // Return the sets used as Inf or Fin in the acceptance condition
      std::pair<acc_cond::mark_t, acc_cond::mark_t> used_inf_fin_sets() const;

      // Print the acceptance as HTML.  The set_printer function can
      // be used to implement customized output for set numbers.
      std::ostream&
      to_html(std::ostream& os,
	      std::function<void(std::ostream&, int)>
	      set_printer = nullptr) const;

      // Print the acceptance as text.  The set_printer function can
      // be used to implement customized output for set numbers.
      std::ostream&
      to_text(std::ostream& os,
	      std::function<void(std::ostream&, int)>
	      set_printer = nullptr) const;

      // Calls to_text
857
858
      SPOT_API
      friend std::ostream& operator<<(std::ostream& os, const acc_code& code);
859
860
    };

861
862
    acc_cond(unsigned n_sets = 0)
      : num_(0U), all_(0U)
863
864
865
866
867
    {
      add_sets(n_sets);
    }

    acc_cond(const acc_cond& o)
868
      : num_(o.num_), all_(o.all_), code_(o.code_)
869
870
871
872
873
874
875
    {
    }

    ~acc_cond()
    {
    }

876
877
878
879
880
881
    void set_acceptance(const acc_code& code)
    {
      code_ = code;
      uses_fin_acceptance_ = check_fin_acceptance();
    }

882
    const acc_code& get_acceptance() const
883
884
885
886
    {
      return code_;
    }

887
888
889
890
891
    acc_code& get_acceptance()
    {
      return code_;
    }

892
893
894
895
896
    bool uses_fin_acceptance() const
    {
      return uses_fin_acceptance_;
    }

897
    bool is_tt() const
898
    {
899
      return code_.is_tt();
900
901
    }

902
    bool is_ff() const
903
    {
904
      return code_.is_ff();
905
906
907
908
909
910
911
912
913
914
915
916
917
918
    }

    bool is_buchi() const
    {
      unsigned s = code_.size();
      return num_ == 1 &&
	s == 2 && code_[1].op == acc_op::Inf && code_[0].mark == all_sets();
    }

    bool is_co_buchi() const
    {
      return num_ == 1 && is_generalized_co_buchi();
    }

919
920
921
922
923
924
925
926
927
928
929
930
    void set_generalized_buchi()
    {
      set_acceptance(inf(all_sets()));
    }

    bool is_generalized_buchi() const
    {
      unsigned s = code_.size();
      return (s == 0 && num_ == 0) ||
	(s == 2 && code_[1].op == acc_op::Inf && code_[0].mark == all_sets());
    }

931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
    bool is_generalized_co_buchi() const
    {
      unsigned s = code_.size();
      return (s == 2 &&
	      code_[1].op == acc_op::Fin && code_[0].mark == all_sets());
    }

    // Returns a number of pairs (>=0) if Rabin, or -1 else.
    int is_rabin() const;
    // Returns a number of pairs (>=0) if Streett, or -1 else.
    int is_streett() const;

    // Return the number of Inf in each pair.
    bool is_generalized_rabin(std::vector<unsigned>& pairs) const;

946
947
948
949
950
951
952
    // If EQUIV is false, this return true iff the acceptance
    // condition is a parity condition written in the canonical way
    // given in the HOA specifications.  If EQUIV is true, then we
    // check whether the condition is logically equivalent to some
    // parity acceptance condition.
    bool is_parity(bool& max, bool& odd, bool equiv = false) const;

953
954
955
956
957
958
959
960
    static acc_code generalized_buchi(unsigned n)
    {
      mark_t m((1U << n) - 1);
      if (n == 8 * sizeof(mark_t::value_t))
	m = mark_t(-1U);
      return acc_code::inf(m);
    }

961
962
963
  protected:
    bool check_fin_acceptance() const;

964
965
966
  public:
    acc_code inf(mark_t mark) const
    {
967
      return acc_code::inf(mark);
968
969
970
971
972
973
974
975
976
    }

    acc_code inf(std::initializer_list<unsigned> vals) const
    {
      return inf(marks(vals.begin(), vals.end()));
    }

    acc_code inf_neg(mark_t mark) const
    {
977
      return acc_code::inf_neg(mark);
978
979
980
981
982
983
984
985
986
    }

    acc_code inf_neg(std::initializer_list<unsigned> vals) const
    {
      return inf_neg(marks(vals.begin(), vals.end()));
    }

    acc_code fin(mark_t mark) const
    {
987
      return acc_code::fin(mark);
988
989
990
991
992
993
994
995
996
    }

    acc_code fin(std::initializer_list<unsigned> vals) const
    {
      return fin(marks(vals.begin(), vals.end()));
    }

    acc_code fin_neg(mark_t mark) const
    {
997
      return acc_code::fin_neg(mark);
998
999
1000
    }

    acc_code fin_neg(std::initializer_list<unsigned> vals) const