formula.cc 54.7 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2015-2019 Laboratoire de Recherche et Développement
3
// de l'Epita (LRDE).
4 5 6 7 8
//
// 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
9
// the Free Software Foundation; either version 3 of the License, or
10 11 12 13 14 15 16 17
// (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
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
19

20
#include "config.h"
21 22
#include <spot/misc/common.hh>
#include <spot/tl/formula.hh>
23
#include <iostream>
24 25 26 27 28
#include <map>
#include <set>
#include <tuple>
#include <cstring>
#include <algorithm>
29 30
#include <spot/misc/bareword.hh>
#include <spot/tl/print.hh>
31

32 33 34 35 36
#ifndef HAVE_STRVERSCMP
// If the libc does not have this, a version is compiled in lib/.
extern "C" int strverscmp(const char *s1, const char *s2);
#endif

37 38
namespace spot
{
39 40
  namespace
  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
41
    typedef std::vector<const fnode*> vec;
42 43 44 45 46 47 48

    // Compare two formulas, by looking at their operators and
    // children.  This does not use id for the top-level operator,
    // because it is used to decide whether to reuse an equal existing
    // formula.
    struct formula_cmp
    {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
49
      bool operator()(const fnode* l, const fnode* r) const
50
      {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        op opl = l->kind();
        op opr = r->kind();
        if (opl != opr)
          return opl < opr;

        if (SPOT_UNLIKELY(opl == op::Star || opl == op::FStar))
          {
            {
              auto minl = l->min();
              auto minr = r->min();
              if (minl != minr)
                return minl < minr;
            }
            {
              auto maxl = l->max();
              auto maxr = r->max();
              if (maxl != maxr)
                return maxl < maxr;
            }
          }
        else
          {
            auto szl = l->size();
            auto szr = r->size();
            if (szl != szr)
              return szl < szr;
          }

        auto el = l->end();
        auto ir = r->begin();
        for (auto il = l->begin(); il != el; ++il, ++ir)
          if (*il != *ir)
            return (*il)->id() < (*ir)->id();

        return false;
86 87 88 89 90
      }
    };

    struct maps_t final
    {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
91
      std::map<std::string, const fnode*> name2ap;
92 93
      std::map<size_t, std::string> ap2name;

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
94
      std::set<const fnode*, formula_cmp> uniq;
95 96 97 98
    };
    static maps_t m;

    static void
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
99
    gather_bool(vec& v, op o)
100 101 102 103 104
    {
      // Gather all boolean terms.
      vec b;
      vec::iterator i = v.begin();
      while (i != v.end())
105 106 107
        {
          if ((*i)->is_boolean())
            {
108
              b.emplace_back(*i);
109 110 111 112 113 114 115
              i = v.erase(i);
            }
          else
            {
              ++i;
            }
        }
116 117 118 119 120 121 122
      // - AndNLM(Exps1...,Bool1,Exps2...,Bool2,Exps3...) =
      //    AndNLM(And(Bool1,Bool2),Exps1...,Exps2...,Exps3...)
      // - AndRat(Exps1...,Bool1,Exps2...,Bool2,Exps3...) =
      //    AndRat(And(Bool1,Bool2),Exps1...,Exps2...,Exps3...)
      // - OrRat(Exps1...,Bool1,Exps2...,Bool2,Exps3...) =
      //    AndRat(Or(Bool1,Bool2),Exps1...,Exps2...,Exps3...)
      if (!b.empty())
123
        v.insert(v.begin(), fnode::multop(o, std::move(b)));
124 125 126
    }
  }

127
  const fnode* fnode::unique(fnode* f)
128
  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
129 130 131
    auto ires = m.uniq.emplace(f);
    if (!ires.second)
      {
132 133 134
        //(*ires.first)->dump(std::cerr << "UNI: ") << '\n';
        for (auto c: *f)
          c->destroy();
135 136
        f->~fnode();
        ::operator delete(f);
137
        return (*ires.first)->clone();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
138 139 140 141
      }
    //f->dump(std::cerr << "INS: ") << '\n';
    return f;
  }
142

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
143 144 145 146 147
  void
  fnode::destroy_aux() const
  {
    if (SPOT_UNLIKELY(is(op::ap)))
      {
148 149 150 151 152
        auto i = m.ap2name.find(id());
        auto n = m.name2ap.erase(i->second);
        assert(n == 1);
        (void)n;
        m.ap2name.erase(i);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
153 154 155
      }
    else
      {
156 157 158 159 160
        auto n = m.uniq.erase(this);
        assert(n == 1);
        (void)n;
        for (auto c: *this)
          c->destroy();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
161
      }
162 163
    this->~fnode();
    ::operator delete(const_cast<fnode*>(this));
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
164
  }
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  void
  fnode::report_non_existing_child()
  {
    throw std::runtime_error("access to non-existing child");
  }

  void
  fnode::report_too_many_children()
  {
    throw std::runtime_error("too many children for formula");
  }

  void
  fnode::report_get_child_of_expecting_single_child_node()
  {
    throw std::invalid_argument
      ("get_child_of() expecting single-child node");
  }

  void
  fnode::report_min_invalid_arg()
  {
    throw std::invalid_argument
      ("min() only works on Star and FStar nodes");
  }

  void
  fnode::report_max_invalid_arg()
  {
    throw std::invalid_argument
      ("min() only works on Star and FStar nodes");
  }

  void
  formula::report_ap_invalid_arg()
  {
    throw std::invalid_argument("atomic propositions cannot be "
                                "constructed from arbitrary formulas");
  }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
206 207 208 209
  std::string fnode::kindstr() const
  {
    switch (op_)
      {
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
#define C(x)                                        \
        case op::x:                                \
          return #x;                                \
          break
        C(ff);
        C(tt);
        C(eword);
        C(ap);
        C(Not);
        C(X);
        C(F);
        C(G);
        C(Closure);
        C(NegClosure);
        C(NegClosureMarked);
        C(Xor);
        C(Implies);
        C(Equiv);
        C(U);
        C(R);
        C(W);
        C(M);
        C(EConcat);
        C(EConcatMarked);
        C(UConcat);
        C(Or);
        C(OrRat);
        C(And);
        C(AndRat);
        C(AndNLM);
        C(Concat);
        C(Fusion);
        C(Star);
        C(FStar);
244
#undef C
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
245 246 247
      }
    SPOT_UNREACHABLE();
  }
248

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
249 250 251 252 253 254 255
  const fnode*
  fnode::multop(op o, vec v)
  {
    // Inline children of same kind.
    //
    // When we construct a formula such as Multop(Op,X,Multop(Op,Y,Z))
    // we will want to inline it as Multop(Op,X,Y,Z).
256
    {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
257 258 259
      vec inlined;
      vec::iterator i = v.begin();
      while (i != v.end())
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        {
          // Some simplification routines erase terms using null
          // pointers that we must ignore.
          if ((*i) == nullptr)
            {
              // FIXME: For commutative operators we should replace
              // the pointer by the first non-null value at the end
              // of the array instead of calling erase.
              i = v.erase(i);
              continue;
            }
          if ((*i)->is(o))
            {
              unsigned ps = (*i)->size();
              for (unsigned n = 0; n < ps; ++n)
275
                inlined.emplace_back((*i)->nth(n)->clone());
276 277 278 279 280 281 282 283 284 285 286
              (*i)->destroy();
              // FIXME: Do not use erase.  See previous FIXME.
              i = v.erase(i);
              continue;
            }
          // All operators except "Concat" and "Fusion" are
          // commutative, so we just keep a list of the inlined
          // arguments that should later be added to the vector.
          // For concat we have to keep track of the order of
          // all the arguments.
          if (o == op::Concat || o == op::Fusion)
287
            inlined.emplace_back(*i);
288 289
          ++i;
        }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
290
      if (o == op::Concat || o == op::Fusion)
291
        v.swap(inlined);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
292
      else
293
        v.insert(v.end(), inlined.begin(), inlined.end());
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
294
    }
295

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
296 297
    if (o != op::Concat && o != op::Fusion)
      std::sort(v.begin(), v.end(), formula_ptr_less_than_bool_first());
298

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
299
    unsigned orig_size = v.size();
300

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
301 302 303 304 305 306 307 308
    const fnode* neutral;
    const fnode* neutral2;
    const fnode* abs;
    const fnode* abs2;
    const fnode* weak_abs;
    switch (o)
      {
      case op::And:
309 310 311 312 313 314
        neutral = tt();
        neutral2 = nullptr;
        abs = ff();
        abs2 = nullptr;
        weak_abs = nullptr;
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
315
      case op::AndRat:
316 317 318 319 320 321 322
        neutral = one_star();
        neutral2 = nullptr;
        abs = ff();
        abs2 = nullptr;
        weak_abs = eword();
        gather_bool(v, op::And);
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
323
      case op::AndNLM:
324 325 326 327 328 329 330
        neutral = eword();
        neutral2 = nullptr;
        abs = ff();
        abs2 = nullptr;
        weak_abs = tt();
        gather_bool(v, op::And);
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
331
      case op::Or:
332 333 334 335 336 337
        neutral = ff();
        neutral2 = nullptr;
        abs = tt();
        abs2 = nullptr;
        weak_abs = nullptr;
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
338
      case op::OrRat:
339 340 341 342 343 344 345
        neutral = ff();
        neutral2 = nullptr;
        abs = one_star();
        abs2 = nullptr;
        weak_abs = nullptr;
        gather_bool(v, op::Or);
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
346
      case op::Concat:
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
        neutral = eword();
        neutral2 = nullptr;
        abs = ff();
        abs2 = nullptr;
        weak_abs = nullptr;

        // - Concat(Exps1...,FExps2...,1[*],FExps3...,Exps4) =
        //     Concat(Exps1...,1[*],Exps4)
        // If FExps2... and FExps3 all accept [*0].
        {
          vec::iterator i = v.begin();
          const fnode* os = one_star();
          while (i != v.end())
            {
              while (i != v.end() && !(*i)->accepts_eword())
                ++i;
              if (i == v.end())
                break;
              vec::iterator b = i;
              // b is the first expressions that accepts [*0].
              // let's find more, and locate the position of
              // 1[*] at the same time.
              bool os_seen = false;
              do
                {
                  os_seen |= (*i == os);
                  ++i;
                }
              while (i != v.end() && (*i)->accepts_eword());

              if (os_seen) // [b..i) is a range that contains [*].
                {
                  // Place [*] at the start of the range, and erase
                  // all other formulae.
                  (*b)->destroy();
                  *b++ = os->clone();
                  for (vec::iterator c = b; c < i; ++c)
                    (*c)->destroy();
                  i = v.erase(b, i);
                }
            }
        }

        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
391
      case op::Fusion:
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
        neutral = tt();
        neutral2 = nullptr;
        abs = ff();
        abs2 = eword();
        weak_abs = nullptr;

        // Make a first pass to group adjacent Boolean formulae.
        // - Fusion(Exps1...,BoolExp1...BoolExpN,Exps2,Exps3...) =
        //   Fusion(Exps1...,And(BoolExp1...BoolExpN),Exps2,Exps3...)
        {
          vec::iterator i = v.begin();
          while (i != v.end())
            {
              if ((*i)->is_boolean())
                {
                  vec::iterator first = i;
                  ++i;
                  if (i == v.end())
                    break;
                  if (!(*i)->is_boolean())
                    {
                      ++i;
                      continue;
                    }
                  do
                    ++i;
                  while (i != v.end() && (*i)->is_boolean());
                  // We have at least two adjacent Boolean formulae.
                  // Replace the first one by the conjunction of all.
                  // FIXME: Investigate the removal of the temporary
                  // vector, by allowing range to be passed to
                  // multop() instead of entire containers.  We
                  // should then able to do
                  //   *first = multop(op::And, first, i);
                  //   i = v.erase(first + 1, i);
                  vec b;
                  b.insert(b.begin(), first, i);
                  i = v.erase(first + 1, i);
                  *first = multop(op::And, b);
                }
              else
                {
                  ++i;
                }
            }
        }

        break;
440

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
441
      default:
442 443 444 445 446 447
        neutral = nullptr;
        neutral2 = nullptr;
        abs = nullptr;
        abs2 = nullptr;
        weak_abs = nullptr;
        break;
448 449
      }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
450 451 452
    // Remove duplicates (except for Concat and Fusion).  We can't use
    // std::unique(), because we must destroy() any formula we drop.
    // Also ignore neutral elements and handle absorbent elements.
453
    {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
454 455 456 457
      const fnode* last = nullptr;
      vec::iterator i = v.begin();
      bool weak_abs_seen = false;
      while (i != v.end())
458 459 460 461 462 463 464 465 466 467
        {
          if ((*i == neutral) || (*i == neutral2) || (*i == last))
            {
              (*i)->destroy();
              i = v.erase(i);
            }
          else if (*i == abs || *i == abs2)
            {
              for (i = v.begin(); i != v.end(); ++i)
                (*i)->destroy();
468
              assert(abs);
469 470 471 472 473 474 475 476 477 478 479
              return abs->clone();
            }
          else
            {
              weak_abs_seen |= (*i == weak_abs);
              if (o != op::Concat && o != op::Fusion)
                // Don't remove duplicates
                last = *i;
              ++i;
            }
        }
480

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
481
      if (weak_abs_seen)
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
        {
          if (o == op::AndRat)
            {
              // We have    a* && [*0] && c  = 0
              //     and    a* && [*0] && c* = [*0]
              // So if [*0] has been seen, check if alls term
              // recognize the empty word.
              bool acc_eword = true;
              for (i = v.begin(); i != v.end(); ++i)
                {
                  acc_eword &= (*i)->accepts_eword();
                  (*i)->destroy();
                }
              if (acc_eword)
                return weak_abs;
              else
                return abs;
            }
          else
            {
              // Similarly,  a* & 1 & (c;d) = c;d
              //             a* & 1 & c* = 1
              assert(o == op::AndNLM);
              vec tmp;
              for (auto i: v)
                {
                  if (i == weak_abs)
                    continue;
                  if (i->accepts_eword())
                    {
                      i->destroy();
                      continue;
                    }
515
                  tmp.emplace_back(i);
516 517
                }
              if (tmp.empty())
518
                tmp.emplace_back(weak_abs);
519 520 521
              v.swap(tmp);
            }
        }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
522
      else if (o == op::Concat || o == op::Fusion)
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
        {
          // Perform an extra loop to merge starable items.
          //   f;f -> f[*2]
          //   f;f[*i..j] -> f[*i+1..j+1]
          //   f[*i..j];f -> f[*i+1..j+1]
          //   f[*i..j];f[*k..l] -> f[*i+k..j+l]
          // same for FStar:
          //   f:f -> f[:*2]
          //   f:f[*i..j] -> f[:*i+1..j+1]
          //   f[:*i..j];f -> f[:*i+1..j+1]
          //   f[:*i..j];f[:*k..l] -> f[:*i+k..j+l]
          op bop = o == op::Concat ? op::Star : op::FStar;
          i = v.begin();
          while (i != v.end())
            {
              vec::iterator fpos = i;
              const fnode* f;
              unsigned min;
              unsigned max;
              bool changed = false;
              if ((*i)->is(bop))
                {
                  f = (*i)->nth(0);
                  min = (*i)->min();
                  max = (*i)->max();
                }
              else
                {
                  f = *i;
                  min = max = 1;
                }

              ++i;
              while (i != v.end())
                {
                  const fnode* f2;
                  unsigned min2;
                  unsigned max2;
                  if ((*i)->is(bop))
                    {
                      f2 = (*i)->nth(0);
                      if (f2 != f)
                        break;
                      min2 = (*i)->min();
                      max2 = (*i)->max();
                    }
                  else
                    {
                      f2 = *i;
                      if (f2 != f)
                        break;
                      min2 = max2 = 1;
                    }
                  if (min2 == unbounded())
                    min = unbounded();
                  else if (min != unbounded())
                    min += min2;
                  if (max2 == unbounded())
                    max = unbounded();
                  else if (max != unbounded())
                    max += max2;
                  (*i)->destroy();
                  i = v.erase(i);
                  changed = true;
                }
              if (changed)
                {
                  const fnode* newfs = bunop(bop, f->clone(), min, max);
                  (*fpos)->destroy();
                  *fpos = newfs;
                }
            }
        }
596 597
    }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
598 599 600
    vec::size_type s = v.size();
    if (s == 0)
      {
601 602
        assert(neutral != nullptr);
        return neutral->clone();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
603 604 605
      }
    else if (s == 1)
      {
606 607 608 609 610 611 612 613 614 615 616 617
        // Simply replace Multop(Op,X) by X.
        // Except we should never reduce the
        // arguments of a Fusion operator to
        // a list with a single formula that
        // accepts [*0].
        const fnode* res = v[0];
        if (o != op::Fusion || orig_size == 1
            || !res->accepts_eword())
          return res;
        // If Fusion(f, ...) reduces to Fusion(f), emit Fusion(1,f).
        // to ensure that [*0] is not accepted.
        v.insert(v.begin(), tt());
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
618
      }
619

620 621
    auto mem = ::operator new(sizeof(fnode)
                              + (v.size() - 1)*sizeof(*children));
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
622 623
    return unique(new(mem) fnode(o, v.begin(), v.end()));
  }
624

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
625 626 627 628
  const fnode*
  fnode::bunop(op o, const fnode* child, uint8_t min, uint8_t max)
  {
    assert(min <= max);
629

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
630 631 632 633
    const fnode* neutral = nullptr;
    switch (o)
      {
      case op::Star:
634 635
        neutral = eword();
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
636
      case op::FStar:
637 638
        neutral = tt();
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
639
      default:
640
        SPOT_UNREACHABLE();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
641
      }
642 643


Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
644
    // common trivial simplifications
645

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
646 647 648 649
    //   - [*0][*min..max] = [*0]
    //   - [*0][:*0..max] = 1
    //   - [*0][:*min..max] = 0 if min > 0
    if (child->is_eword())
650
      switch (o)
651 652 653 654 655 656 657 658 659 660 661
        {
        case op::Star:
          return neutral;
        case op::FStar:
          if (min == 0)
            return neutral;
          else
            return ff();
        default:
          SPOT_UNREACHABLE();
        }
662

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
663 664 665 666 667
    //   - 0[*0..max] = [*0]
    //   - 0[*min..max] = 0 if min > 0
    //   - b[:*0..max] = 1
    //   - b[:*min..max] = 0 if min > 0
    if (child->is_ff()
668
        || (o == op::FStar && child->is_boolean()))
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
669
      {
670 671 672 673 674 675
        if (min == 0)
          {
            child->destroy();
            return neutral;
          }
        return child;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
676
      }
677

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
678 679 680 681
    //   - Exp[*0] = [*0]
    //   - Exp[:*0] = 1
    if (max == 0)
      {
682 683
        child->destroy();
        return neutral;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
684
      }
685

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
686 687 688 689
    //   - Exp[*1] = Exp
    //   - Exp[:*1] = Exp if Exp does not accept [*0]
    if (min == 1 && max == 1)
      if (o == op::Star || !child->accepts_eword())
690
        return child;
691

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
692 693 694 695
    //   - Exp[*i..j][*k..l] = Exp[*ik..jl] if i*(k+1)<=jk+1.
    //   - Exp[:*i..j][:*k..l] = Exp[:*ik..jl] if i*(k+1)<=jk+1.
    if (child->is(o))
      {
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
        unsigned i = child->min();
        unsigned j = child->max();

        // Exp has to be true between i*min and j*min
        //               then between i*(min+1) and j*(min+1)
        //               ...
        //            finally between i*max and j*max
        //
        // We can merge these intervals into [i*min..j*max] iff the
        // first are adjacent or overlap, i.e. iff
        //   i*(min+1) <= j*min+1.
        // (Because i<=j, this entails that the other intervals also
        // overlap).

        const fnode* exp = child->nth(0);
        if (j == unbounded())
          {
            min *= i;
            max = unbounded();

            // Exp[*min..max]
            exp->clone();
            child->destroy();
            child = exp;
          }
        else
          {
            if (i * (min + 1) <= (j * min) + 1)
              {
                min *= i;
                if (max != unbounded())
                  {
                    if (j == unbounded())
                      max = unbounded();
                    else
                      max *= j;
                  }
                exp->clone();
                child->destroy();
                child = exp;
              }
          }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
738 739 740 741 742 743 744 745 746 747 748 749 750
      }

    return unique(new fnode(o, child, min, max));
  }

  const fnode*
  fnode::unop(op o, const fnode* f)
  {
    // Some trivial simplifications.
    switch (o)
      {
      case op::F:
      case op::G:
751 752 753 754
        {
          // F and G are idempotent.
          if (f->is(o))
            return f;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
755

756 757 758 759
          // F(0) = G(0) = 0
          // F(1) = G(1) = 1
          if (f->is_ff() || f->is_tt())
            return f;
760

761 762 763
          assert(!f->is_eword());
        }
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
764 765

      case op::Not:
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
        {
          // !1 = 0
          if (f->is_tt())
            return ff();
          // !0 = 1
          if (f->is_ff())
            return tt();
          // ![*0] = 1[+]
          if (f->is_eword())
            return bunop(op::Star, tt(), 1);

          auto fop = f->kind();
          // "Not" is an involution.
          if (fop == o)
            {
              auto c = f->nth(0)->clone();
              f->destroy();
              return c;
            }
          // !Closure(Exp) = NegClosure(Exp)
          if (fop == op::Closure)
            {
              const fnode* c = unop(op::NegClosure, f->nth(0)->clone());
              f->destroy();
              return c;
            }
          // !NegClosure(Exp) = Closure(Exp)
          if (fop == op::NegClosure || fop == op::NegClosureMarked)
            {
              const fnode* c = unop(op::Closure, f->nth(0)->clone());
              f->destroy();
              return c;
            }
          break;
        }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
801
      case op::X:
802 803 804 805 806
        // X(1) = 1,  X(0) = 0
        if (f->is_tt() || f->is_ff())
          return f;
        assert(!f->is_eword());
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
807 808

      case op::Closure:
809 810 811 812 813 814 815
        // {0} = 0, {1} = 1,  {b} = b
        if (f->is_boolean())
          return f;
        // {[*0]} = 0
        if (f->is_eword())
          return ff();
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
816 817 818

      case op::NegClosure:
      case op::NegClosureMarked:
819 820 821 822 823 824 825 826 827 828
        // {1} = 0
        if (f->is_tt())
          return ff();
        // {0} = 1,  {[*0]} = 1
        if (f->is_ff() || f->is_eword())
          return tt();
        // {b} = !b
        if (f->is_boolean())
          return unop(op::Not, f);
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
829
      default:
830
        SPOT_UNREACHABLE();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
831
      }
832

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
833 834
    return unique(new fnode(o, {f}));
  }
835

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
836 837 838 839 840 841
  const fnode*
  fnode::binop(op o, const fnode* first, const fnode* second)
  {
    // Sort the operands of commutative operators, so that for
    // example the formula instance for 'a xor b' is the same as
    // that for 'b xor a'.
842

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
843 844 845 846
    // Trivial identities:
    switch (o)
      {
      case op::Xor:
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
        {
          // Xor is commutative: sort operands.
          formula_ptr_less_than_bool_first cmp;
          if (cmp(second, first))
            std::swap(second, first);
        }
        //   - (1 ^ Exp) = !Exp
        //   - (0 ^ Exp) = Exp
        if (first->is_tt())
          return unop(op::Not, second);
        if (first->is_ff())
          return second;
        if (first == second)
          {
            first->destroy();
            second->destroy();
            return ff();
          }
        // We expect constants to appear first, because they are
        // instantiated first.
        assert(!second->is_constant());
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
869
      case op::Equiv:
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
        {
          // Equiv is commutative: sort operands.
          formula_ptr_less_than_bool_first cmp;
          if (cmp(second, first))
            std::swap(second, first);
        }
        //   - (0 <=> Exp) = !Exp
        //   - (1 <=> Exp) = Exp
        //   - (Exp <=> Exp) = 1
        if (first->is_ff())
          return unop(op::Not, second);
        if (first->is_tt())
          return second;
        if (first == second)
          {
            first->destroy();
            second->destroy();
            return tt();
          }
        // We expect constants to appear first, because they are
        // instantiated first.
        assert(!second->is_constant());
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
893
      case op::Implies:
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
        //   - (1 => Exp) = Exp
        //   - (0 => Exp) = 1
        //   - (Exp => 1) = 1
        //   - (Exp => 0) = !Exp
        //   - (Exp => Exp) = 1
        if (first->is_tt())
          return second;
        if (first->is_ff())
          {
            second->destroy();
            return tt();
          }
        if (second->is_tt())
          {
            first->destroy();
            return second;
          }
        if (second->is_ff())
          return unop(op::Not, first);
        if (first == second)
          {
            first->destroy();
            second->destroy();
            return tt();
          }
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
920
      case op::U:
921 922 923 924 925 926 927 928 929 930 931 932 933
        //   - (Exp U 1) = 1
        //   - (Exp U 0) = 0
        //   - (0 U Exp) = Exp
        //   - (Exp U Exp) = Exp
        if (second->is_tt()
            || second->is_ff()
            || first->is_ff()
            || first == second)
          {
            first->destroy();
            return second;
          }
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
934
      case op::W:
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
        //   - (Exp W 1) = 1
        //   - (0 W Exp) = Exp
        //   - (1 W Exp) = 1
        //   - (Exp W Exp) = Exp
        if (second->is_tt()
            || first->is_ff()
            || first == second)
          {
            first->destroy();
            return second;
          }
        if (first->is_tt())
          {
            second->destroy();
            return first;
          }
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
952
      case op::R:
953 954 955 956 957 958 959 960 961 962 963 964 965
        //   - (Exp R 1) = 1
        //   - (Exp R 0) = 0
        //   - (1 R Exp) = Exp
        //   - (Exp R Exp) = Exp
        if (second->is_tt()
            || second->is_ff()
            || first->is_tt()
            || first == second)
          {
            first->destroy();
            return second;
          }
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
966
      case op::M:
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
        //   - (Exp M 0) = 0
        //   - (1 M Exp) = Exp
        //   - (0 M Exp) = 0
        //   - (Exp M Exp) = Exp
        if (second->is_ff()
            || first->is_tt()
            || first == second)
          {
            first->destroy();
            return second;
          }
        if (first->is_ff())
          {
            second->destroy();
            return first;
          }
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
984 985
      case op::EConcat:
      case op::EConcatMarked:
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
        //   - 0 <>-> Exp = 0
        //   - 1 <>-> Exp = Exp
        //   - [*0] <>-> Exp = 0
        //   - Exp <>-> 0 = 0
        //   - boolExp <>-> Exp = boolExp & Exp
        if (first->is_tt())
          return second;
        if (first->is_ff()
            || first->is_eword())
          {
            second->destroy();
            return ff();
          }
        if (second->is_ff())
          {
            first->destroy();
            return second;
          }
        if (first->is_boolean())
          return multop(op::And, {first, second});
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1007
      case op::UConcat:
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
        //   - 0 []-> Exp = 1
        //   - 1 []-> Exp = Exp
        //   - [*0] []-> Exp = 1
        //   - Exp []-> 1 = 1
        //   - boolExp []-> Exp = !boolExp | Exp
        if (first->is_tt())
          return second;
        if (first->is_ff()
            || first->is_eword())
          {
            second->destroy();
            return tt();
          }
        if (second->is_tt())
          {
            first->destroy();
            return second;
          }
        if (first->is_boolean())
          return multop(op::Or, {unop(op::Not, first), second});
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1029
      default:
1030
        SPOT_UNREACHABLE();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1031
      }
1032

1033
    auto mem = ::operator new(sizeof(fnode) + sizeof(*children));
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
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 1065 1066 1067 1068 1069 1070 1071 1072 1073
    return unique(new(mem) fnode(o, {first, second}));
  }

  const fnode*
  fnode::ap(const std::string& name)
  {
    auto ires = m.name2ap.emplace(name, nullptr);
    if (!ires.second)
      return ires.first->second->clone();
    // Name the formula before creating it, because
    // the constructor will call ap_name().
    m.ap2name.emplace(next_id_, name);
    // next_id_ is incremented by setup_props(), called by the
    // constructor of fnode
    return ires.first->second = new fnode(op::ap, {});
  }

  const std::string&
  fnode::ap_name() const
  {
    if (op_ != op::ap)
      throw std::runtime_error("ap_name() called on non-AP formula");
    auto i = m.ap2name.find(id());
    assert(i != m.ap2name.end());
    return i->second;
  }

  size_t fnode::next_id_ = 0U;
  const fnode* fnode::ff_ = new fnode(op::ff, {});
  const fnode* fnode::tt_ = new fnode(op::tt, {});
  const fnode* fnode::ew_ = new fnode(op::eword, {});
  const fnode* fnode::one_star_ = nullptr; // Only built when necessary.

  void fnode::setup_props(op o)
  {
    id_ = next_id_++;
    // If the counter of formulae ever loops, we want to skip the
    // first three values, because they are permanently associated
    // to constants, and it is convenient to have constants
    // smaller than all other formulas.
1074
    if (SPOT_UNLIKELY(next_id_ == 0))
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1075 1076
      next_id_ = 3;

1077
    switch (o)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1078 1079 1080
      {
      case op::ff:
      case op::tt:
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
        is_.boolean = true;
        is_.sugar_free_boolean = true;
        is_.in_nenoform = true;
        is_.syntactic_si = true; // for LTL (not PSL)
        is_.sugar_free_ltl = true;
        is_.ltl_formula = true;
        is_.psl_formula = true;
        is_.sere_formula = true;
        is_.finite = true;
        is_.eventual = true;
        is_.universal = true;
        is_.syntactic_safety = true;
        is_.syntactic_guarantee = true;
        is_.syntactic_obligation = true;
        is_.syntactic_recurrence = true;
        is_.syntactic_persistence = true;
        is_.not_marked = true;
        is_.accepting_eword = false;
        is_.lbt_atomic_props = true;
        is_.spin_atomic_props = true;
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1102
      case op::eword:
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
        is_.boolean = false;
        is_.sugar_free_boolean = false;
        is_.in_nenoform = true;
        is_.syntactic_si = true;
        is_.sugar_free_ltl = true;
        is_.ltl_formula = false;
        is_.psl_formula = false;
        is_.sere_formula = true;
        is_.finite = true;
        is_.eventual = false;
        is_.syntactic_safety = false;
        is_.syntactic_guarantee = false;
        is_.syntactic_obligation = false;
        is_.syntactic_recurrence = false;
        is_.syntactic_persistence = false;
        is_.universal = false;
        is_.not_marked = true;
        is_.accepting_eword = true;
        is_.lbt_atomic_props = true;
        is_.spin_atomic_props = true;
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1124
      case op::ap:
1125 1126 1127
        is_.boolean = true;
        is_.sugar_free_boolean = true;
        is_.in_nenoform = true;
1128 1129 1130
        is_.syntactic_si = true;        // Assuming LTL (for PSL, a Boolean
        // term that is not stared will be regarded as non-SI where
        // this matters.)
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
        is_.sugar_free_ltl = true;
        is_.ltl_formula = true;
        is_.psl_formula = true;
        is_.sere_formula = true;
        is_.finite = true;
        is_.eventual = false;
        is_.universal = false;
        is_.syntactic_safety = true;
        is_.syntactic_guarantee = true;
        is_.syntactic_obligation = true;
        is_.syntactic_recurrence = true;
        is_.syntactic_persistence = true;
        is_.not_marked = true;
        is_.accepting_eword = false;
        {
          // is_.lbt_atomic_props should be true if the name has the
          // form pNN where NN is any number of digit.
          const std::string& n = ap_name();
          std::string::const_iterator pos = n.begin();
          bool lbtap = (pos != n.end() && *pos++ == 'p');
          while (lbtap && pos != n.end())
            {
              char l = *pos++;
              lbtap = (l >= '0' && l <= '9');
            }
          is_.lbt_atomic_props = lbtap;
          is_.spin_atomic_props = lbtap || is_spin_ap(n.c_str());
        }
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1160
      case op::Not:
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
        props = children[0]->props;
        is_.not_marked = true;
        is_.eventual = children[0]->is_universal();
        is_.universal = children[0]->is_eventual();
        is_.in_nenoform = (children[0]->is(op::ap));
        is_.sere_formula = is_.boolean;

        is_.syntactic_safety = children[0]->is_syntactic_guarantee();
        is_.syntactic_guarantee = children[0]->is_syntactic_safety();
        // is_.syntactic_obligation inherited from child
        is_.syntactic_recurrence = children[0]->is_syntactic_persistence();
        is_.syntactic_persistence = children[0]->is_syntactic_recurrence();

        is_.accepting_eword = false;
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1176
      case op::X:
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
        props = children[0]->props;
        is_.not_marked = true;
        is_.boolean = false;
        is_.syntactic_si = false;
        is_.sere_formula = false;
        // is_.syntactic_safety inherited
        // is_.syntactic_guarantee inherited
        // is_.syntactic_obligation inherited
        // is_.syntactic_recurrence inherited
        // is_.syntactic_persistence inherited
        is_.accepting_eword = false;
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1189
      case op::F:
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
        props = children[0]->props;
        is_.not_marked = true;
        is_.boolean = false;
        is_.sere_formula = false;
        is_.finite = false;
        is_.sugar_free_ltl = false;
        is_.eventual = true;
        is_.syntactic_safety = false;
        // is_.syntactic_guarantee inherited
        is_.syntactic_obligation = is_.syntactic_guarantee;
        is_.syntactic_recurrence = is_.syntactic_guarantee;
        // is_.syntactic_persistence inherited
        is_.accepting_eword = false;
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1204
      case op::G:
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
        props = children[0]->props;
        is_.not_marked = true;
        is_.boolean = false;
        is_.sere_formula = false;
        is_.finite = false;
        is_.sugar_free_ltl = false;
        is_.universal = true;
        // is_.syntactic_safety inherited
        is_.syntactic_guarantee = false;
        is_.syntactic_obligation = is_.syntactic_safety;
        // is_.syntactic_recurrence inherited
        is_.syntactic_persistence = is_.syntactic_safety;
        is_.accepting_eword = false;
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1219 1220
      case op::NegClosure:
      case op::NegClosureMarked:
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
        props = children[0]->props;
        is_.not_marked = (op_ == op::NegClosure);
        is_.boolean = false;
        is_.ltl_formula = false;
        is_.psl_formula = true;
        is_.sere_formula = false;
        is_.syntactic_safety = is_.finite;
        is_.syntactic_guarantee = true;
        is_.syntactic_obligation = true;
        is_.syntactic_recurrence = true;
        is_.syntactic_persistence = true;
        is_.accepting_eword = false;
        assert(children[0]->is_sere_formula());
        assert(!children[0]->is_boolean());
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1236
      case op::Closure:
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
        props = children[0]->props;
        is_.not_marked = true;
        is_.boolean = false;
        is_.ltl_formula = false;
        is_.psl_formula = true;
        is_.sere_formula = false;
        is_.syntactic_safety = true;
        is_.syntactic_guarantee = is_.finite;
        is_.syntactic_obligation = true;
        is_.syntactic_recurrence = true;
        is_.syntactic_persistence = true;
        is_.accepting_eword = false;
        assert(children[0]->is_sere_formula());
        assert(!children[0]->is_boolean());
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1252 1253
      case op::Xor:
      case op::Equiv:
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
        props = children[0]->props & children[1]->props;
        is_.eventual = false;
        is_.universal = false;
        is_.sere_formula = is_.boolean;
        is_.sugar_free_boolean = false;
        is_.in_nenoform = false;
        // is_.syntactic_obligation inherited;
        is_.accepting_eword = false;
        if (is_.syntactic_obligation)
          {
            // Only formula that are in the intersection of
            // guarantee and safety are closed by Xor and <=>.
            bool sg = is_.syntactic_safety && is_.syntactic_guarantee;
            is_.syntactic_safety = sg;
            is_.syntactic_guarantee = sg;
            assert(is_.syntactic_recurrence == true);
            assert(is_.syntactic_persistence == true);
          }
        else
          {
            is_.syntactic_safety = false;
            is_.syntactic_guarantee = false;
            is_.syntactic_recurrence = false;
            is_.syntactic_persistence = false;
          }
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1280
      case op::Implies:
1281 1282 1283
        props = children[0]->props & children[1]->props;
        is_.eventual = false;
        is_.universal = false;
1284 1285
        is_.sere_formula = (children[0]->is_boolean()
                            && children[1]->is_sere_formula());
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
        is_.sugar_free_boolean = false;
        is_.in_nenoform = false;
        is_.syntactic_safety = (children[0]->is_syntactic_guarantee()
                                && children[1]->is_syntactic_safety());
        is_.syntactic_guarantee = (children[0]->is_syntactic_safety()
                                   && children[1]->is_syntactic_guarantee());
        // is_.syntactic_obligation inherited
        is_.syntactic_persistence = children[0]->is_syntactic_recurrence()
          && children[1]->is_syntactic_persistence();
        is_.syntactic_recurrence = children[0]->is_syntactic_persistence()
          && children[1]->is_syntactic_recurrence();
        is_.accepting_eword = false;
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1299 1300
      case op::EConcatMarked:
      case op::EConcat:
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
        props = children[0]->props & children[1]->props;
        is_.not_marked = (op_ != op::EConcatMarked);
        is_.ltl_formula = false;
        is_.boolean = false;
        is_.sere_formula = false;
        is_.accepting_eword = false;
        is_.psl_formula = true;

        is_.syntactic_guarantee = children[1]->is_syntactic_guarantee();
        is_.syntactic_persistence = children[1]->is_syntactic_persistence();
        if (children[0]->is_finite())
          {
            is_.syntactic_safety = children[1]->is_syntactic_safety();
            is_.syntactic_obligation = children[1]->is_syntactic_obligation();
            is_.syntactic_recurrence = children[1]->is_syntactic_recurrence();
          }
        else
          {
            is_.syntactic_safety = false;
1320 1321 1322
            bool g = children[1]->is_syntactic_guarantee();
            is_.syntactic_obligation = g;
            is_.syntactic_recurrence = g;
1323 1324 1325 1326 1327 1328
          }
        assert(children[0]->is_sere_formula());
        assert(children[1]->is_psl_formula());
        if (children[0]->is_boolean())
          is_.syntactic_si = false;
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1329
      case op::UConcat:
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
        props = children[0]->props & children[1]->props;
        is_.not_marked = true;
        is_.ltl_formula = false;
        is_.boolean = false;
        is_.sere_formula = false;
        is_.accepting_eword = false;
        is_.psl_formula = true;

        is_.syntactic_safety = children[1]->is_syntactic_safety();
        is_.syntactic_recurrence = children[1]->is_syntactic_recurrence();
        if (children[0]->is_finite())
          {
            is_.syntactic_guarantee = children[1]->is_syntactic_guarantee();
            is_.syntactic_obligation = children[1]->is_syntactic_obligation();
            is_.syntactic_persistence =
              children[1]->is_syntactic_persistence();
          }
        else
          {
            is_.syntactic_guarantee = false;
1350 1351 1352
            bool s = children[1]->is_syntactic_safety();
            is_.syntactic_obligation = s;
            is_.syntactic_persistence = s;
1353 1354 1355 1356 1357 1358
          }
        assert(children[0]->is_sere_formula());
        assert(children[1]->is_psl_formula());
        if (children[0]->is_boolean())
          is_.syntactic_si = false;
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1359
      case op::U:
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
        // Beware: (f U g) is a pure eventuality if both operands
        // are pure eventualities, unlike in the proceedings of
        // Concur'00.  (The revision of the paper available at
        // http://www.bell-labs.com/project/TMP/ is fixed.)  See
        // also http://arxiv.org/abs/1011.4214v2 for a discussion
        // about this problem.  (Which we fixed in 2005 thanks
        // to LBTT.)
        // This means that we can use the following line to handle
        // all cases of (f U g), (f R g), (f W g), (f M g) for
        // universality and eventuality.
        props = children[0]->props & children[1]->props;
        // The matter can be further refined because:
        //  (f U g) is a pure eventuality if
        //                g is a pure eventuality (regardless of f),
        //             or f == 1
        //  (g M f) is a pure eventuality if f and g are,
        //                                or f == 1
        //  (g R f) is purely universal if
        //                f is purely universal (regardless of g)
        //               or g == 0
        //  (f W g) is purely universal if f and g are
        //                              or g == 0
        is_.not_marked = true;
        // f U g is universal if g is eventual, or if f == 1.
        is_.eventual = children[1]->is_eventual();
        is_.eventual |= children[0]->is_tt();
        is_.boolean = false;
        is_.sere_formula = false;
        is_.finite = false;
        is_.accepting_eword = false;

        is_.syntactic_safety = false;
        // is_.syntactic_guarantee = Guarantee U Guarantee
        is_.syntactic_obligation = // Obligation U Guarantee
          children[0]->is_syntactic_obligation()
          && children[1]->is_syntactic_guarantee();
        is_.syntactic_recurrence = // Recurrence U Guarantee
          children[0]->is_syntactic_recurrence()
          && children[1]->is_syntactic_guarantee();
        // is_.syntactic_persistence = Persistence U Persistance
        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1401
      case op::W:
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
        // See comment for op::U.
        props = children[0]->props & children[1]->props;
        is_.not_marked = true;
        // f W g is universal if f and g are, or if g == 0.
        is_.universal |= children[1]->is_ff();
        is_.boolean = false;
        is_.sere_formula = false;
        is_.finite = false;
        is_.accepting_eword = false;

        // is_.syntactic_safety = Safety W Safety;
        is_.syntactic_guarantee = false;
        is_.syntactic_obligation = // Safety W Obligation
          children[0]->is_syntactic_safety()
          && children[1]->is_syntactic_obligation();
        // is_.syntactic_recurrence = Recurrence W Recurrence
        is_.syntactic_persistence = // Safety W Persistance
          children[0]->is_syntactic_safety()
          && children[1]->is_syntactic_persistence();

        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1423
      case op::R:
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
        // See comment for op::U.
        props = children[0]->props & children[1]->props;
        is_.not_marked = true;
        // g R f is universal if f is universal, or if g == 0.
        is_.universal = children[1]->is_universal();
        is_.universal |= children[0]->is_ff();
        is_.boolean = false;
        is_.sere_formula = false;
        is_.finite = false;
        is_.accepting_eword = false;

        // is_.syntactic_safety = Safety R Safety;
        is_.syntactic_guarantee = false;
        is_.syntactic_obligation = // Obligation R Safety
          children[0]->is_syntactic_obligation()
          && children[1]->is_syntactic_safety();
        //is_.syntactic_recurrence = Recurrence R Recurrence
        is_.syntactic_persistence = // Persistence R Safety
          children[0]->is_syntactic_persistence()
          && children[1]->is_syntactic_safety();

        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1446
      case op::M:
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
        // See comment for op::U.
        props = children[0]->props & children[1]->props;
        is_.not_marked = true;
        // g M f is eventual if both g and f are eventual, or if f == 1.
        is_.eventual |= children[1]->is_tt();
        is_.boolean = false;
        is_.sere_formula = false;
        is_.finite = false;
        is_.accepting_eword = false;

        is_.syntactic_safety = false;
        // is_.syntactic_guarantee = Guarantee M Guarantee
        is_.syntactic_obligation = // Guarantee M Obligation
          children[0]->is_syntactic_guarantee()
          && children[1]->is_syntactic_obligation();
        is_.syntactic_recurrence = // Guarantee M Recurrence
          children[0]->is_syntactic_guarantee()
          && children[1]->is_syntactic_recurrence();
        // is_.syntactic_persistence = Persistence M Persistance

        break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1468
      case op::Or:
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
        {
          props = children[0]->props;
          unsigned s = size_;
          bool ew = children[0]->accepts_eword();
          for (unsigned i = 1; i < s; ++i)
            {
              ew |= children[i]->accepts_eword();
              props &= children[i]->props;
            }
          is_.accepting_eword = ew;
          break;
        }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1481
      case op::OrRat:
1482 1483 1484 1485 1486 1487 1488