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


#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 42 43 44 45 46 47 48 49 50 51 52 53

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)
      {
	if (a & 1)
	  {
	    os << comma << level;
	    comma = ",";
	  }
	a >>= 1;
	++level;
      }
    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 66
    void default_set_printer(std::ostream& os, int v)
    {
      os << v;
    }

    template<bool html>
67 68
    static void
    print_code(std::ostream& os,
69 70
	       const acc_cond::acc_code& code, unsigned pos,
	       std::function<void(std::ostream&, int)> set_printer)
71 72 73 74 75 76 77 78
    {
      const char* op = " | ";
      auto& w = code[pos];
      const char* negated = "";
      bool top = pos == code.size() - 1;
      switch (w.op)
	{
	case acc_cond::acc_op::And:
79
	  op = html ? " &amp; " : " & ";
80 81 82 83 84 85 86 87 88 89 90 91 92
	case acc_cond::acc_op::Or:
	  {
	    unsigned sub = pos - w.size;
	    if (!top)
	      os << '(';
	    bool first = true;
	    while (sub < pos)
	      {
		--pos;
		if (first)
		  first = false;
		else
		  os << op;
93
		print_code<html>(os, code, pos, set_printer);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
		pos -= code[pos].size;
	      }
	    if (!top)
	      os << ')';
	  }
	  break;
	case acc_cond::acc_op::InfNeg:
	  negated = "!";
	case acc_cond::acc_op::Inf:
	  {
	    auto a = code[pos - 1].mark.id;
	    if (a == 0U)
	      {
		os << 't';
	      }
	    else
	      {
111 112 113
		if (!top)
		  // Avoid extra parentheses if there is only one set
		  top = code[pos - 1].mark.count() == 1;
114 115 116 117 118 119 120 121
		unsigned level = 0;
		const char* and_ = "";
		if (!top)
		  os << '(';
		while (a)
		  {
		    if (a & 1)
		      {
122 123 124 125
			os << and_ << "Inf(" << negated;
			set_printer(os, level);
			os << ')';
			and_ = html ? "&amp;" : "&";
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
		      }
		    a >>= 1;
		    ++level;
		  }
		if (!top)
		  os << ')';
	      }
	  }
	  break;
	case acc_cond::acc_op::FinNeg:
	  negated = "!";
	case acc_cond::acc_op::Fin:
	  {
	    auto a = code[pos - 1].mark.id;
	    if (a == 0U)
	      {
		os << 'f';
	      }
	    else
	      {
146 147 148
		if (!top)
		  // Avoid extra parentheses if there is only one set
		  top = code[pos - 1].mark.count() == 1;
149 150 151 152 153 154 155 156
		unsigned level = 0;
		const char* or_ = "";
		if (!top)
		  os << '(';
		while (a)
		  {
		    if (a & 1)
		      {
157 158 159
			os << or_ << "Fin(" << negated;
			set_printer(os, level);
			os << ')';
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
			or_ = "|";
		      }
		    a >>= 1;
		    ++level;
		  }
		if (!top)
		  os << ')';
	      }
	  }
	  break;
	}
    }


    static bool
175
    eval(acc_cond::mark_t inf, const acc_cond::acc_word* pos)
176
    {
177
      switch (pos->op)
178 179 180
	{
	case acc_cond::acc_op::And:
	  {
181
	    auto sub = pos - pos->size;
182 183 184
	    while (sub < pos)
	      {
		--pos;
185
		if (!eval(inf, pos))
186
		  return false;
187
		pos -= pos->size;
188 189 190 191 192
	      }
	    return true;
	  }
	case acc_cond::acc_op::Or:
	  {
193
	    auto sub = pos - pos->size;
194 195 196
	    while (sub < pos)
	      {
		--pos;
197
		if (eval(inf, pos))
198
		  return true;
199
		pos -= pos->size;
200 201 202 203
	      }
	    return false;
	  }
	case acc_cond::acc_op::Inf:
204
	  return (pos[-1].mark & inf) == pos[-1].mark;
205
	case acc_cond::acc_op::Fin:
206
	  return (pos[-1].mark & inf) != pos[-1].mark;
207 208 209 210 211 212 213
	case acc_cond::acc_op::FinNeg:
	case acc_cond::acc_op::InfNeg:
	  SPOT_UNREACHABLE();
	}
      SPOT_UNREACHABLE();
      return false;
    }
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
    static bool
    inf_eval(acc_cond::mark_t inf, const acc_cond::acc_word* pos)
    {
      switch (pos->op)
	{
	case acc_cond::acc_op::And:
	  {
	    auto sub = pos - pos->size;
	    while (sub < pos)
	      {
		--pos;
		if (!inf_eval(inf, pos))
		  return false;
		pos -= pos->size;
	      }
	    return true;
	  }
	case acc_cond::acc_op::Or:
	  {
	    auto sub = pos - pos->size;
	    while (sub < pos)
	      {
		--pos;
		if (inf_eval(inf, pos))
		  return true;
		pos -= pos->size;
	      }
	    return false;
	  }
	case acc_cond::acc_op::Inf:
	  return (pos[-1].mark & inf) == pos[-1].mark;
	case acc_cond::acc_op::Fin:
	  return true;
	case acc_cond::acc_op::FinNeg:
	case acc_cond::acc_op::InfNeg:
	  SPOT_UNREACHABLE();
	}
      SPOT_UNREACHABLE();
      return false;
    }

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
    static acc_cond::mark_t
    eval_sets(acc_cond::mark_t inf, const acc_cond::acc_word* pos)
    {
      switch (pos->op)
	{
	case acc_cond::acc_op::And:
	  {
	    auto sub = pos - pos->size;
	    acc_cond::mark_t m = 0U;
	    while (sub < pos)
	      {
		--pos;
		if (auto s = eval_sets(inf, pos))
		  m |= s;
		else
		  return 0U;
		pos -= pos->size;
	      }
	    return m;
	  }
	case acc_cond::acc_op::Or:
	  {
	    auto sub = pos - pos->size;
	    while (sub < pos)
	      {
		--pos;
		if (auto s = eval_sets(inf, pos))
		  return s;
		pos -= pos->size;
	      }
	    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();
	}
      SPOT_UNREACHABLE();
      return false;
    }
301 302
  }

303
  bool acc_cond::acc_code::accepting(mark_t inf) const
304
  {
305
    if (empty())
306
      return true;
307
    return eval(inf, &back());
308 309
  }

310
  bool acc_cond::acc_code::inf_satisfiable(mark_t inf) const
311
  {
312
    if (empty())
313
      return true;
314
    return inf_eval(inf, &back());
315 316
  }

317 318 319 320 321 322 323 324 325 326
  acc_cond::mark_t acc_cond::accepting_sets(mark_t inf) const
  {
    if (uses_fin_acceptance())
      throw std::runtime_error
	("Fin acceptance is not supported by this code path.");
    if (code_.empty())
      return 0U;
    return eval_sets(inf, &code_.back());
  }

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  bool acc_cond::check_fin_acceptance() const
  {
    if (code_.empty())
      return false;
    unsigned pos = code_.size();
    do
      {
	switch (code_[pos - 1].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:
	    pos -= 2;
	    break;
	  case acc_cond::acc_op::Fin:
345 346 347 348 349
	    if (code_[pos - 2].mark == 0U) // f
	      {
		pos -= 2;
		break;
	      }
350 351 352 353 354 355 356 357
	  case acc_cond::acc_op::FinNeg:
	    return true;
	  }
      }
    while (pos > 0);
    return false;
  }

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

  namespace
  {
    // Is Rabin or Streett, depending on highop and lowop.
    static bool
    is_rs(const acc_cond::acc_code& code,
	  acc_cond::acc_op highop,
	  acc_cond::acc_op lowop,
	  acc_cond::mark_t all_sets)
    {
      unsigned s = code.back().size;
      auto mainop = code.back().op;
      if (mainop == highop)
	{
	  // 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;
	  // Pretend we where in a unary highop.
	  s = 5;
	}
      acc_cond::mark_t seen_fin = 0U;
      acc_cond::mark_t seen_inf = 0U;
      while (s)
	{
	  if (code[--s].op != lowop)
	    return false;
	  auto o1 = code[--s].op;
	  auto m1 = code[--s].mark;
	  auto o2 = code[--s].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.id != (m1.id << 1))
	    return false;
	  seen_fin |= m1;
	  seen_inf |= m2;
	}

      return (!(seen_fin & seen_inf)
	      && (seen_fin | seen_inf) == all_sets);
    }
  }

417 418
  int acc_cond::is_rabin() const
  {
419
    if (code_.is_f())
420
      return num_ == 0 ? 0 : -1;
421
    if ((num_ & 1) || code_.is_t())
422
      return -1;
423 424 425 426

    if (is_rs(code_, acc_op::Or, acc_op::And, all_sets()))
      return num_ / 2;
    else
427 428 429 430 431
      return -1;
  }

  int acc_cond::is_streett() const
  {
432
    if (code_.is_t())
433
      return num_ == 0 ? 0 : -1;
434
    if ((num_ & 1) || code_.is_f())
435
      return -1;
436 437 438 439

    if (is_rs(code_, acc_op::And, acc_op::Or, all_sets()))
      return num_ / 2;
    else
440 441 442
      return -1;
  }

443
  // PAIRS contains the number of Inf in each pair.
444 445 446 447 448 449 450 451
  bool acc_cond::is_generalized_rabin(std::vector<unsigned>& pairs) const
  {
    pairs.clear();
    if (is_generalized_co_buchi())
      {
	pairs.resize(num_);
	return true;
      }
452
    if (code_.is_t()
453 454
	|| code_.back().op != acc_op::Or)
      return false;
455

456
    auto s = code_.back().size;
457 458 459 460 461
    acc_cond::mark_t seen_fin = 0U;
    acc_cond::mark_t seen_inf = 0U;
    // Each pairs is the position of a Fin followed
    // by the number of Inf.
    std::map<unsigned, unsigned> p;
462 463 464 465 466
    while (s)
      {
	--s;
	if (code_[s].op == acc_op::And)
	  {
467 468 469 470 471 472 473 474 475 476
	    auto o1 = code_[--s].op;
	    auto m1 = code_[--s].mark;
	    auto o2 = code_[--s].op;
	    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)
477
	      {
478 479
		std::swap(o1, o2);
		std::swap(m1, m2);
480
	      }
481 482 483 484

	    if (o1 != acc_cond::acc_op::Fin
		|| o2 != acc_cond::acc_op::Inf
		|| m1.count() != 1)
485
	      return false;
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

	    unsigned i = m2.count();
	    // If we have seen this pair already, it must have the
	    // same size.
	    if (p.emplace(m1.max_set(), i).first->second != i)
	      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;
501 502 503
	  }
	else if (code_[s].op == acc_op::Fin)
	  {
504
	    auto m1 = code_[--s].mark;
505 506 507 508 509
	    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;
510
	    seen_fin |= m1;
511 512 513 514 515 516
	  }
	else
	  {
	    return false;
	  }
      }
517 518 519 520
    for (auto i: p)
      pairs.push_back(i.second);
    return (!(seen_fin & seen_inf)
	    && (seen_fin | seen_inf) == all_sets());
521 522
  }

523 524 525
  acc_cond::acc_code
  acc_cond::acc_code::parity(bool max, bool odd, unsigned sets)
  {
526 527 528 529 530 531 532
    acc_cond::acc_code res;

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

533
    if (sets == 0)
534 535
      return res;

536 537 538 539 540 541 542 543 544 545
    // 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)
      {
	if ((i & 1) == odd)
546
	  res |= inf({(unsigned)i});
547
	else
548
	  res &= fin({(unsigned)i});
549 550 551 552
      }
    return res;
  }

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
  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)
      {
	if (drand() < 0.5)
	  codes.push_back(inf({i}));
	else
	  codes.push_back(fin({i}));
	if (reuse > 0.0 && drand() < reuse)
	  --i;
      }

    int s = codes.size();
    while (s > 1)
      {
	// Pick a random code and put it at the end
	int p1 = mrand(s--);
	std::swap(codes[p1], codes[s]);
	// and another one
	int p2 = mrand(s);

	if (drand() < 0.5)
585
	  codes[p2] |= std::move(codes.back());
586
	else
587
	  codes[p2] &= std::move(codes.back());
588 589 590 591 592 593

	codes.pop_back();
      }
    return codes[0];
  }

594 595
  namespace
  {
596
    bdd to_bdd_rec(const acc_cond::acc_word* c, const bdd* map)
597 598 599 600 601 602 603 604 605
    {
      auto sz = c->size;
      auto start = c - sz - 1;
      auto op = c->op;
      switch (op)
	{
	case acc_cond::acc_op::Or:
	  {
	    --c;
606
	    bdd res = bddfalse;
607 608
	    do
	      {
609
		res |= to_bdd_rec(c, map);
610 611 612 613 614 615 616 617
		c -= c->size + 1;
	      }
	    while (c > start);
	    return res;
	  }
	case acc_cond::acc_op::And:
	  {
	    --c;
618
	    bdd res = bddtrue;
619 620
	    do
	      {
621
		res &= to_bdd_rec(c, map);
622 623 624 625 626 627 628
		c -= c->size + 1;
	      }
	    while (c > start);
	    return res;
	  }
	case acc_cond::acc_op::Fin:
	  {
629
	    bdd res = bddfalse;
630
	    for (auto i: c[-1].mark.sets())
631 632
	      res |= !map[i];
	    return res;
633 634 635
	  }
	case acc_cond::acc_op::Inf:
	  {
636 637 638 639
	    bdd res = bddtrue;
	    for (auto i: c[-1].mark.sets())
	      res &= map[i];
	    return res;
640 641 642 643
	  }
	case acc_cond::acc_op::InfNeg:
	case acc_cond::acc_op::FinNeg:
	  SPOT_UNREACHABLE();
644
	  return bddfalse;
645 646
	}
      SPOT_UNREACHABLE();
647
      return bddfalse;
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

    static bool
    equiv_codes(const acc_cond::acc_code& lhs,
		const acc_cond::acc_code& rhs)
    {
      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)
	if (used.has(i))
	  r.push_back(bdd_ithvar(base++));
	else
	  r.push_back(bddfalse);
      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)
      {
677
	max = true;
678
	odd = is_t();
679
	return true;
680 681 682 683 684 685 686 687
      }
    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)
688 689 690 691
	{
	  max = false;	     // just so the value is not uninitialized
	  return false;
	}
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

    auto max_code = acc_code::parity(true, odd, sets);
    if (max_code == code_)
      {
	max = true;
	return true;
      }
    auto min_code = acc_code::parity(false, odd, sets);
    if (min_code == code_)
      {
	max = false;
	return true;
      }

    if (!equiv)
      return false;

    if (equiv_codes(code_, max_code))
      {
	max = true;
	return true;
      }
    if (equiv_codes(code_, min_code))
      {
	max = false;
	return true;
      }
    return false;
720 721 722 723
  }

  acc_cond::acc_code acc_cond::acc_code::to_dnf() const
  {
724
    if (empty() || size() == 2)
725
      return *this;
726 727 728

    auto used = acc_cond::acc_code::used_sets();
    unsigned c = used.count();
729
    unsigned max = used.max_set();
730 731 732

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
733
    assert(base == 0);
734 735
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
736
    for (unsigned i = 0; r.size() < max; ++i)
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
	if (used.has(i))
	  {
	    sets[base] = i;
	    r.push_back(bdd_ithvar(base++));
	  }
	else
	  {
	    r.push_back(bddfalse);
	  }
      }

    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)
      {
	mark_t i = 0U;
	acc_code c;
	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.
776
		auto a = fin(s) & std::move(c);
777 778 779 780 781 782 783 784
		std::swap(a, c);
	      }
	    else		// Positive variable? -> Inf
	      {
		i |= s;
		cube = h;
	      }
	  }
785
	c &= inf(i);
786
	// See comment above for the order.
787
	c |= std::move(rescode);
788
	std::swap(c, rescode);
789
      }
790

791
    return rescode;
792 793
  }

794 795 796 797 798 799 800
  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();
801
    unsigned max = used.max_set();
802 803 804

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
805
    assert(base == 0);
806 807
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
808
    for (unsigned i = 0; r.size() < max; ++i)
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
      {
	if (used.has(i))
	  {
	    sets[base] = i;
	    r.push_back(bdd_ithvar(base++));
	  }
	else
	  {
	    r.push_back(bddfalse);
	  }
      }

    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)
      {
	mark_t m = 0U;
	acc_code c = f();
	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.
848
		auto a = inf(s) | std::move(c);
849 850 851 852 853 854 855 856
		std::swap(a, c);
	      }
	    else		// Positive variable? -> Fin
	      {
		m |= s;
		cube = h;
	      }
	  }
857
	c |= fin(m);
858
	// See comment above for the order.
859
	c &= std::move(rescode);
860 861 862 863 864
	std::swap(c, rescode);
      }
    return rescode;
  }

865
  std::pair<bool, acc_cond::mark_t>
866
  acc_cond::unsat_mark() const
867
  {
868
    if (is_t())
869
      return {false, 0U};
870 871
    if (!uses_fin_acceptance())
      return {true, 0U};
872

873
    auto used = code_.used_sets();
874
    unsigned c = used.count();
875
    unsigned max = used.max_set();
876 877 878 879 880 881

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
    assert(base == 0);
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
882
    for (unsigned i = 0; r.size() < max; ++i)
883 884 885 886 887 888 889 890 891 892 893 894
      {
	if (used.has(i))
	  {
	    sets[base] = i;
	    r.push_back(bdd_ithvar(base++));
	  }
	else
	  {
	    r.push_back(bddfalse);
	  }
      }

895
    bdd res = to_bdd_rec(&code_.back(), &r[0]);
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

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

    bdd cube = bdd_satone(!res);
    mark_t i = 0U;
    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? -> skip
	  {
	    cube = bdd_low(cube);
	  }
	else		// Positive variable? -> Inf
	  {
	    i.set(s);
	    cube = h;
	  }
      }
    return {true, i};
  }

923 924 925 926 927 928 929 930 931 932 933 934 935 936
  std::vector<std::vector<int>>
  acc_cond::acc_code::missing(mark_t inf, bool accepting) const
  {
    if (empty())
      {
	if (accepting)
	  return {};
	else
	  return {
	    {}
	  };
      }
    auto used = acc_cond::acc_code::used_sets();
    unsigned c = used.count();
937
    unsigned max = used.max_set();
938 939 940 941 942 943 944

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
    assert(base == 0);
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
    bdd known = bddtrue;
945
    for (unsigned i = 0; r.size() < max; ++i)
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
      {
	if (used.has(i))
	  {
	    sets[base] = i;
	    bdd v = bdd_ithvar(base++);
	    r.push_back(v);
	    if (inf.has(i))
	      known &= v;
	  }
	else
	  {
	    r.push_back(bddfalse);
	  }
      }

    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)
      {
	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
	      {
		partial.push_back(s);
		cube = bdd_low(cube);
	      }
	    else		// Positive variable
	      {
		partial.push_back(-s - 1);
		cube = h;
	      }
	  }
	result.emplace_back(std::move(partial));
      }
    return result;
  }

999 1000
  bool acc_cond::acc_code::is_dnf() const
  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1001
    if (empty() || size() == 2)
1002 1003 1004
      return true;
    auto pos = &back();
    auto start = &front();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1005
    auto and_scope = pos + 1;
1006 1007 1008 1009 1010 1011 1012 1013 1014
    if (pos->op == acc_cond::acc_op::Or)
      --pos;
    while (pos > start)
      {
	switch (pos->op)
	  {
	  case acc_cond::acc_op::Or:
	    return false;
	  case acc_cond::acc_op::And:
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1015
	    and_scope = std::min(and_scope, pos - pos->size);
1016 1017 1018 1019
	    --pos;
	    break;
	  case acc_cond::acc_op::Fin:
	  case acc_cond::acc_op::FinNeg:
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1020 1021 1022 1023 1024
	    if (pos[-1].mark.count() > 1 && pos > and_scope)
	      return false;
	    /* fall through */
	  case acc_cond::acc_op::Inf:
	  case acc_cond::acc_op::InfNeg:
1025 1026 1027 1028 1029 1030 1031
	    pos -= 2;
	    break;
	  }
      }
    return true;
  }

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
  bool acc_cond::acc_code::is_cnf() const
  {
    if (empty() || size() == 2)
      return true;
    auto pos = &back();
    auto start = &front();
    auto or_scope = pos + 1;
    if (pos->op == acc_cond::acc_op::And)
      --pos;
    while (pos > start)
      {
	switch (pos->op)
	  {
	  case acc_cond::acc_op::And:
	    return false;
	  case acc_cond::acc_op::Or:
	    or_scope = std::min(or_scope, pos - pos->size);
	    --pos;
	    break;
	  case acc_cond::acc_op::Inf:
	  case acc_cond::acc_op::InfNeg:
	    if (pos[-1].mark.count() > 1 && pos > or_scope)
	      return false;
	    /* fall through */
	  case acc_cond::acc_op::Fin:
	  case acc_cond::acc_op::FinNeg:
	    pos -= 2;
	    break;
	  }
      }
    return true;
  }

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
  namespace
  {
    acc_cond::acc_code complement_rec(const acc_cond::acc_word* pos)
    {
      auto start = pos - pos->size;
      switch (pos->op)
	{
	case acc_cond::acc_op::And:
	  {
	    --pos;
1075
	    auto res = acc_cond::acc_code::f();
1076 1077
	    do
	      {
1078
		auto tmp = complement_rec(pos) | std::move(res);
1079 1080 1081 1082 1083 1084 1085 1086 1087
		std::swap(tmp, res);
		pos -= pos->size + 1;
	      }
	    while (pos > start);
	    return res;
	  }
	case acc_cond::acc_op::Or:
	  {
	    --pos;
1088
	    auto res = acc_cond::acc_code::t();
1089 1090
	    do
	      {
1091
		auto tmp = complement_rec(pos) & std::move(res);
1092 1093 1094 1095 1096 1097 1098
		std::swap(tmp, res);
		pos -= pos->size + 1;
	      }
	    while (pos > start);
	    return res;
	  }
	case acc_cond::acc_op::Fin:
1099
	  return acc_cond::acc_code::inf(pos[-1].mark);
1100
	case acc_cond::acc_op::Inf:
1101
	  return acc_cond::acc_code::fin(pos[-1].mark);
1102
	case acc_cond::acc_op::FinNeg:
1103
	  return acc_cond::acc_code::inf_neg(pos[-1].mark);
1104
	case acc_cond::acc_op::InfNeg:
1105
	  return acc_cond::acc_code::fin_neg(pos[-1].mark);
1106 1107
	}
      SPOT_UNREACHABLE();
1108
      return {};
1109 1110 1111 1112 1113 1114 1115
    }

  }


  acc_cond::acc_code acc_cond::acc_code::complement() const
  {
1116
    if (is_t())
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
      return acc_cond::acc_code::f();
    return complement_rec(&back());
  }

  namespace
  {
    static acc_cond::acc_code
    strip_rec(const acc_cond::acc_word* pos, acc_cond::mark_t rem, bool missing)
    {
      auto start = pos - pos->size;
      switch (pos->op)
	{
	case acc_cond::acc_op::And:
	  {
	    --pos;
	    auto res = acc_cond::acc_code::t();
	    do
	      {
1135
		auto tmp = strip_rec(pos, rem, missing) & std::move(res);
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
		std::swap(tmp, res);
		pos -= pos->size + 1;
	      }
	    while (pos > start);
	    return res;
	  }
	case acc_cond::acc_op::Or:
	  {
	    --pos;
	    auto res = acc_cond::acc_code::f();
	    do
	      {
1148
		auto tmp = strip_rec(pos, rem, missing) | std::move(res);
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
		std::swap(tmp, res);
		pos -= pos->size + 1;
	      }
	    while (pos > start);
	    return res;
	  }
	case acc_cond::acc_op::Fin:
	  if (missing && (pos[-1].mark & rem))
	    return acc_cond::acc_code::t();
	  return acc_cond::acc_code::fin(pos[-1].mark.strip(rem));
	case acc_cond::acc_op::Inf:
	  if (missing && (pos[-1].mark & rem))
	    return acc_cond::acc_code::f();
	  return acc_cond::acc_code::inf(pos[-1].mark.strip(rem));
	case acc_cond::acc_op::FinNeg:
	case acc_cond::acc_op::InfNeg:
	  SPOT_UNREACHABLE();
	  return {};
	}
      SPOT_UNREACHABLE();
      return {};
    }
  }

  acc_cond::acc_code
  acc_cond::acc_code::strip(acc_cond::mark_t rem, bool missing) const
  {
1176
    if (is_t() || is_f())
1177 1178 1179 1180 1181 1182 1183
      return *this;
    return strip_rec(&back(), rem, missing);
  }

  acc_cond::mark_t
  acc_cond::acc_code::used_sets() const
  {
1184
    if (is_t() || is_f())
1185 1186 1187 1188 1189
      return 0U;
    acc_cond::mark_t used_in_cond = 0U;
    auto pos = &back();
    auto end = &front();
    while (pos > end)
1190
      {
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	switch (pos->op)
	  {
	  case acc_cond::acc_op::And:
	  case acc_cond::acc_op::Or:
	    --pos;
	    break;
	  case acc_cond::acc_op::Fin:
	  case acc_cond::acc_op::Inf:
	  case acc_cond::acc_op::FinNeg:
	  case acc_cond::acc_op::InfNeg:
	    used_in_cond |= pos[-1].mark;
	    pos -= 2;
	    break;
	  }
1205
      }
1206
    return used_in_cond;
1207
  }
1208

1209 1210 1211
  std::pair<acc_cond::mark_t, acc_cond::mark_t>
  acc_cond::acc_code::used_inf_fin_sets() const
  {
1212
    if (is_t() || is_f())
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
      return {0U, 0U};

    acc_cond::mark_t used_fin = 0U;
    acc_cond::mark_t used_inf = 0U;
    auto pos = &back();
    auto end = &front();
    while (pos > end)
      {
	switch (pos->op)
	  {
	  case acc_cond::acc_op::And:
	  case acc_cond::acc_op::Or:
	    --pos;
	    break;
	  case acc_cond::acc_op::Fin:
	  case acc_cond::acc_op::FinNeg:
	    used_fin |= pos[-1].mark;
	    pos -= 2;
	    break;
	  case acc_cond::acc_op::Inf:
	  case acc_cond::acc_op::InfNeg:
	    used_inf |= pos[-1].mark;
	    pos -= 2;
	    break;
	  }
      }
    return {used_inf, used_fin};
  }

  std::ostream&
  acc_cond::acc_code::to_html(std::ostream& os,
			      std::function<void(std::ostream&, int)>
			      set_printer) const
1246
  {
1247
    if (empty())
1248 1249
      os << 't';
    else
1250 1251
      print_code<true>(os, *this, size() - 1,
		       set_printer ? set_printer : default_set_printer);
1252 1253
    return os;
  }
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

  std::ostream&
  acc_cond::acc_code::to_text(std::ostream& os,
			      std::function<void(std::ostream&, int)>
			      set_printer) const
  {
    if (empty())
      os << 't';
    else
      print_code<false>(os, *this, size() - 1,
			set_printer ? set_printer : default_set_printer);
    return os;
  }


  std::ostream& operator<<(std::ostream& os,
			   const spot::acc_cond::acc_code& code)
  {
    return code.to_text(os);
  }
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308



  namespace
  {

    /// acc ::= term | term "|" acc
    /// term := "t" | "f" | "Inf" "(" num ")"
    ///       | "Fin" "(" num ") " | "(" acc ")
    ///       | term "&" term

    static void skip_space(const char*& input)
    {
      while (std::isspace(*input))
	++input;
    }

    // Eat c and remove the following spaces.
    // Complain if there is no c.
    void expect(const char*& input, char c)
    {
      if (*input != c)
	{
	  std::ostringstream s;
	  s << "syntax error at '" << input << "', was expecting " << c << '.';
	  throw parse_error(s.str());
	}
      ++input;
      skip_space(input);
    }

    static acc_cond::acc_code parse_term(const char*& input);

    static acc_cond::acc_code parse_acc(const char*& input)
    {
1309
      auto res = parse_term(input);
1310 1311 1312 1313 1314
      skip_space(input);
      while (*input == '|')
	{
	  ++input;
	  skip_space(input);
1315 1316 1317
	  // Prepend instead of append, to preserve the input order.
	  auto tmp = parse_term(input);
	  std::swap(tmp, res);
1318
	  res |= std::move(tmp);
1319
	}
1320
      return res;
1321 1322 1323 1324 1325 1326 1327