simplify.cc 119 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2011, 2012, 2013, 2014 Laboratoire de Recherche et
3
// Developpement 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
21
22
23
24
25
26
27
#include <iostream>
//#define TRACE
#ifdef TRACE
#define trace std::cerr
#else
#define trace while (0) std::cerr
#endif

28
29
30
31
#include "simplify.hh"
#include "misc/hash.hh"
#include "ltlast/allnodes.hh"
#include "ltlast/visitor.hh"
32
33
#include "ltlvisit/contain.hh"
#include "ltlvisit/tostring.hh"
34
#include "ltlvisit/snf.hh"
35
#include "tgba/formula2bdd.hh"
36
37
38
39
40
41
42
#include <cassert>

namespace spot
{
  namespace ltl
  {

43

44
45
46
    // The name of this class is public, but not its contents.
    class ltl_simplifier_cache
    {
47
48
49
50
      typedef std::unordered_map<const formula*, const formula*,
				 ptr_hash<formula>> f2f_map;
      typedef std::unordered_map<const formula*, bdd,
				 ptr_hash<formula>> f2b_map;
51
52
      typedef std::pair<const formula*, const formula*> pairf;
      typedef std::map<pairf, bool> syntimpl_cache_t;
53
    public:
54
      bdd_dict_ptr dict;
55
      ltl_simplifier_options options;
56
      language_containment_checker lcc;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

      ~ltl_simplifier_cache()
      {
	{
	  f2f_map::iterator i = simplified_.begin();
	  f2f_map::iterator end = simplified_.end();
	  while (i != end)
	    {
	      f2f_map::iterator old = i++;
	      old->second->destroy();
	      old->first->destroy();
	    }
	}
	{
	  f2f_map::iterator i = nenoform_.begin();
	  f2f_map::iterator end = nenoform_.end();
	  while (i != end)
	    {
	      f2f_map::iterator old = i++;
	      old->second->destroy();
	      old->first->destroy();
	    }
	}
	{
	  f2b_map::iterator i = as_bdd_.begin();
	  f2b_map::iterator end = as_bdd_.end();
	  while (i != end)
	    {
	      f2b_map::iterator old = i++;
	      old->first->destroy();
	    }
	}
89
90
91
92
93
94
95
96
97
98
	{
	  syntimpl_cache_t::iterator i = syntimpl_.begin();
	  syntimpl_cache_t::iterator end = syntimpl_.end();
	  while (i != end)
	    {
	      syntimpl_cache_t::iterator old = i++;
	      old->first.first->destroy();
	      old->first.second->destroy();
	    }
	}
99
100
101
102
103
104
105
106
107
108
	{
	  snf_cache::iterator i = snf_cache_.begin();
	  snf_cache::iterator end = snf_cache_.end();
	  while (i != end)
	    {
	      snf_cache::iterator old = i++;
	      old->second->destroy();
	      old->first->destroy();
	    }
	}
109
110
111
112
113
114
115
116
117
118
	{
	  snf_cache::iterator i = snfb_cache_.begin();
	  snf_cache::iterator end = snfb_cache_.end();
	  while (i != end)
	    {
	      snf_cache::iterator old = i++;
	      old->second->destroy();
	      old->first->destroy();
	    }
	}
119
120
121
122
123
124
125
126
127
128
	{
	  f2f_map::iterator i = bool_isop_.begin();
	  f2f_map::iterator end = bool_isop_.end();
	  while (i != end)
	    {
	      f2f_map::iterator old = i++;
	      old->second->destroy();
	      old->first->destroy();
	    }
	}
129

130
	dict->unregister_all_my_variables(this);
131
132
      }

133
      ltl_simplifier_cache(const bdd_dict_ptr& d)
134
	: dict(d), lcc(d, true, true, false, false)
135
136
137
      {
      }

138
139
      ltl_simplifier_cache(const bdd_dict_ptr& d,
			   const ltl_simplifier_options& opt)
140
	: dict(d), options(opt), lcc(d, true, true, false, false)
141
      {
142
	options.containment_checks |= options.containment_checks_stronger;
143
	options.event_univ |= options.favor_event_univ;
144
145
      }

146
147
148
149
150
151
152
      void
      print_stats(std::ostream& os) const
      {
	os << "simplified formulae:    " << simplified_.size() << " entries\n"
	   << "negative normal form:   " << nenoform_.size() << " entries\n"
	   << "syntactic implications: " << syntimpl_.size() << " entries\n"
	   << "boolean to bdd:         " << as_bdd_.size() << " entries\n"
153
154
	   << "star normal form:       " << snf_cache_.size() << " entries\n"
	   << "boolean isop:           " << bool_isop_.size() << " entries\n";
155
156
      }

157
158
159
160
161
162
163
164
165
166
167
168
169
      void
      clear_as_bdd_cache()
      {
	f2b_map::iterator i = as_bdd_.begin();
	f2b_map::iterator end = as_bdd_.end();
	while (i != end)
	  {
	    f2b_map::iterator old = i++;
	    old->first->destroy();
	  }
	as_bdd_.clear();
      }

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
      // Convert a Boolean formula into a BDD for easier comparison.
      bdd
      as_bdd(const formula* f)
      {
	// Lookup the result in case it has already been computed.
	f2b_map::const_iterator it = as_bdd_.find(f);
	if (it != as_bdd_.end())
	  return it->second;

	bdd result = bddfalse;

	switch (f->kind())
	  {
	  case formula::Constant:
	    if (f == constant::true_instance())
	      result = bddtrue;
	    else if (f == constant::false_instance())
187
	      result = bddfalse;
188
	    else
189
	      SPOT_UNIMPLEMENTED();
190
191
	    break;
	  case formula::AtomicProp:
192
	    result = bdd_ithvar(dict->register_proposition(f, this));
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
	    break;
	  case formula::UnOp:
	    {
	      const unop* uo = static_cast<const unop*>(f);
	      assert(uo->op() == unop::Not);
	      result = !as_bdd(uo->child());
	      break;
	    }
	  case formula::BinOp:
	    {
	      const binop* bo = static_cast<const binop*>(f);
	      int op = 0;
	      switch (bo->op())
		{
		case binop::Xor:
		  op = bddop_xor;
		  break;
		case binop::Implies:
		  op = bddop_imp;
		  break;
		case binop::Equiv:
		  op = bddop_biimp;
		  break;
		default:
217
		  SPOT_UNIMPLEMENTED();
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
		}
	      result = bdd_apply(as_bdd(bo->first()), as_bdd(bo->second()), op);
	      break;
	    }
	  case formula::MultOp:
	    {
	      const multop* mo = static_cast<const multop*>(f);
	      switch (mo->op())
		{
		case multop::And:
		  {
		    result = bddtrue;
		    unsigned s = mo->size();
		    for (unsigned n = 0; n < s; ++n)
		      result &= as_bdd(mo->nth(n));
		    break;
		  }
		case multop::Or:
		  {
		    result = bddfalse;
		    unsigned s = mo->size();
		    for (unsigned n = 0; n < s; ++n)
		      result |= as_bdd(mo->nth(n));
		    break;
		  }
243
		case multop::AndNLM:
244
245
		case multop::AndRat:
		case multop::OrRat:
246
247
		case multop::Concat:
		case multop::Fusion:
248
		  SPOT_UNIMPLEMENTED();
249
250
251
252
253
		  break;
		}
	      break;
	    }
	  case formula::BUnOp:
254
	    SPOT_UNIMPLEMENTED();
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
	    break;
	  }

	// Cache the result before returning.
	as_bdd_[f->clone()] = result;
	return result;
      }

      const formula*
      lookup_nenoform(const formula* f)
      {
	f2f_map::const_iterator i = nenoform_.find(f);
	if (i == nenoform_.end())
	  return 0;
	return i->second->clone();
      }

      void
      cache_nenoform(const formula* orig, const formula* nenoform)
      {
	nenoform_[orig->clone()] = nenoform->clone();
      }

278
279
280
281
282
      // Return true iff the option set (syntactic implication
      // or containment checks) allow to prove that f1 => f2.
      bool
      implication(const formula* f1, const formula* f2)
      {
283
284
285
286
287
288
289
290
291
292
	trace << "[->] does " << to_string(f1) << " implies "
	      << to_string(f2) << " ?" << std::endl;
	if ((options.synt_impl && syntactic_implication(f1, f2))
	    || (options.containment_checks && contained(f1, f2)))
	  {
	    trace << "[->] Yes" << std::endl;
	    return true;
	  }
	trace << "[->] No" << std::endl;
	return false;
293
294
295
      }

      // Return true if f1 => f2 syntactically
296
      bool
297
      syntactic_implication(const formula* f1, const formula* f2);
298
299
      bool
      syntactic_implication_aux(const formula* f1, const formula* f2);
300

301
302
303
      // Return true if f1 => f2
      bool
      contained(const formula* f1, const formula* f2)
304
      {
305
306
307
308
309
310
311
312
	if (!f1->is_psl_formula() || !f2->is_psl_formula())
	  return false;
	return lcc.contained(f1, f2);
      }

      // If right==false, true if !f1 => f2, false otherwise.
      // If right==true, true if f1 => !f2, false otherwise.
      bool
313
314
      syntactic_implication_neg(const formula* f1, const formula* f2,
				bool right);
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

      // Return true if f1 => !f2
      bool contained_neg(const formula* f1, const formula* f2)
      {
	if (!f1->is_psl_formula() || !f2->is_psl_formula())
	  return false;
	trace << "[CN] Does (" << to_string(f1) << ") implies !("
	      << to_string(f2) << ") ?" << std::endl;
	if (lcc.contained_neg(f1, f2))
	  {
	    trace << "[CN] Yes" << std::endl;
	    return true;
	  }
	else
	  {
	    trace << "[CN] No" << std::endl;
	    return false;
	  }
      }

      // Return true if f1 => !f2
      bool neg_contained(const formula* f1, const formula* f2)
      {
	if (!f1->is_psl_formula() || !f2->is_psl_formula())
	  return false;
	trace << "[NC] Does (" << to_string(f1) << ") implies !("
	      << to_string(f2) << ") ?" << std::endl;
	if (lcc.neg_contained(f1, f2))
	  {
	    trace << "[NC] Yes" << std::endl;
	    return true;
	  }
	else
	  {
	    trace << "[NC] No" << std::endl;
	    return false;
	  }
      }

      // Return true iff the option set (syntactic implication
      // or containment checks) allow to prove that
356
      //   - !f1 => f2   (case where right=false)
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
      //   - f1 => !f2   (case where right=true)
      bool
      implication_neg(const formula* f1, const formula* f2, bool right)
      {
	trace << "[IN] Does " << (right ? "(" : "!(")
	      << to_string(f1) << ") implies "
	      << (right ? "!(" : "(") << to_string(f2) << ") ?"
	      << std::endl;
	if ((options.synt_impl && syntactic_implication_neg(f1, f2, right))
	    || (options.containment_checks && right && contained_neg(f1, f2))
	    || (options.containment_checks && !right && neg_contained(f1, f2)))
	  {
	    trace << "[IN] Yes" << std::endl;
	    return true;
	  }
	else
	  {
	    trace << "[IN] No" << std::endl;
	    return false;
	  }
377
378
      }

379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
      const formula*
      lookup_simplified(const formula* f)
      {
	f2f_map::const_iterator i = simplified_.find(f);
	if (i == simplified_.end())
	  return 0;
	return i->second->clone();
      }

      void
      cache_simplified(const formula* orig, const formula* simplified)
      {
	simplified_[orig->clone()] = simplified->clone();
      }

394
395
396
397
398
399
      const formula*
      star_normal_form(const formula* f)
      {
	return ltl::star_normal_form(f, &snf_cache_);
      }

400
401
402
403
404
405
406
      const formula*
      star_normal_form_bounded(const formula* f)
      {
	return ltl::star_normal_form_bounded(f, &snfb_cache_);
      }


407
408
409
410
411
412
413
414
415
416
417
418
419
      const formula*
      boolean_to_isop(const formula* f)
      {
	f2f_map::const_iterator it = bool_isop_.find(f);
	if (it != bool_isop_.end())
	  return it->second->clone();

	assert(f->is_boolean());
	const formula* res = bdd_to_formula(as_bdd(f), dict);
	bool_isop_[f->clone()] = res->clone();
	return res;
      }

420
421
422
423
    private:
      f2b_map as_bdd_;
      f2f_map simplified_;
      f2f_map nenoform_;
424
      syntimpl_cache_t syntimpl_;
425
      snf_cache snf_cache_;
426
      snf_cache snfb_cache_;
427
      f2f_map bool_isop_;
428
429
430
431
432
    };


    namespace
    {
433
434
435
436
437
438
      //////////////////////////////////////////////////////////////////////
      //
      //  NEGATIVE_NORMAL_FORM_VISITOR
      //
      //////////////////////////////////////////////////////////////////////

439
440
441
442
443
444
445
446
447
      // Forward declaration.
      const formula*
      nenoform_recursively(const formula* f,
			   bool negated,
			   ltl_simplifier_cache* c);

      class negative_normal_form_visitor: public visitor
      {
      public:
448
449
	negative_normal_form_visitor(bool negated, ltl_simplifier_cache* c)
	  : negated_(negated), cache_(c)
450
451
452
453
454
455
456
457
	{
	}

	virtual
	~negative_normal_form_visitor()
	{
	}

458
	const formula* result() const
459
460
461
462
463
	{
	  return result_;
	}

	void
464
	visit(const atomic_prop* ap)
465
	{
466
	  const formula* f = ap->clone();
467
468
469
470
471
472
473
	  if (negated_)
	    result_ = unop::instance(unop::Not, f);
	  else
	    result_ = f;
	}

	void
474
	visit(const constant* c)
475
476
477
478
479
480
481
482
483
484
	{
	  // Negation of constants is taken care of in the constructor
	  // of unop::Not, so these cases should be caught by
	  // nenoform_recursively().
	  assert(!negated_);
	  result_ = c;
	  return;
	}

	void
485
	visit(const unop* uo)
486
	{
487
	  const formula* f = uo->child();
488
489
	  unop::type op = uo->op();
	  switch (op)
490
491
492
	    {
	    case unop::Not:
	      // "Not"s should be caught by nenoform_recursively().
493
	      SPOT_UNREACHABLE();
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
	    case unop::X:
	      /* !Xa == X!a */
	      result_ = unop::instance(unop::X, recurse(f));
	      return;
	    case unop::F:
	      /* !Fa == G!a */
	      result_ = unop::instance(negated_ ? unop::G : unop::F,
				       recurse(f));
	      return;
	    case unop::G:
	      /* !Ga == F!a */
	      result_ = unop::instance(negated_ ? unop::F : unop::G,
				       recurse(f));
	      return;
	    case unop::Closure:
	      result_ = unop::instance(negated_ ?
				       unop::NegClosure : unop::Closure,
				       recurse_(f, false));
	      return;
	    case unop::NegClosure:
514
	    case unop::NegClosureMarked:
515
	      result_ = unop::instance(negated_ ?
516
				       unop::Closure : op,
517
518
519
				       recurse_(f, false));
	      return;
	    }
520
	  SPOT_UNREACHABLE();
521
522
523
	}

	void
524
	visit(const bunop* bo)
525
	{
526
527
	  // !(a*) should never occur.
	  assert(!negated_);
528
529
530
531
	  result_ = bunop::instance(bo->op(), recurse_(bo->child(), false),
				    bo->min(), bo->max());
	}

532
533
534
	const formula* equiv_or_xor(bool equiv,
				    const formula* f1,
				    const formula* f2)
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
	{
	  // Rewrite a<=>b as (a&b)|(!a&!b)
	  if (equiv)
	    return
	      multop::instance(multop::Or,
			       multop::instance(multop::And,
						recurse_(f1, false),
						recurse_(f2, false)),
			       multop::instance(multop::And,
						recurse_(f1, true),
						recurse_(f2, true)));
	  else
	    // Rewrite a^b as (a&!b)|(!a&b)
	    return
	      multop::instance(multop::Or,
			       multop::instance(multop::And,
						recurse_(f1, false),
						recurse_(f2, true)),
			       multop::instance(multop::And,
						recurse_(f1, true),
						recurse_(f2, false)));
	}

558
	void
559
	visit(const binop* bo)
560
	{
561
562
	  const formula* f1 = bo->first();
	  const formula* f2 = bo->second();
563
564
565
	  switch (bo->op())
	    {
	    case binop::Xor:
566
567
	      // !(a ^ b) == a <=> b
	      result_ = equiv_or_xor(negated_, f1, f2);
568
569
	      return;
	    case binop::Equiv:
570
571
	      // !(a <=> b) == a ^ b
	      result_ = equiv_or_xor(!negated_, f1, f2);
572
573
574
	      return;
	    case binop::Implies:
	      if (negated_)
575
		// !(a => b) == a & !b
576
577
578
		result_ = multop::instance(multop::And,
					   recurse_(f1, false),
					   recurse_(f2, true));
579
580
581
582
	      else // a => b == !a | b
		result_ = multop::instance(multop::Or,
					   recurse_(f1, true),
					   recurse_(f2, false));
583
584
	      return;
	    case binop::U:
585
	      // !(a U b) == !a R !b
586
587
588
589
	      result_ = binop::instance(negated_ ? binop::R : binop::U,
					recurse(f1), recurse(f2));
	      return;
	    case binop::R:
590
	      // !(a R b) == !a U !b
591
592
593
594
	      result_ = binop::instance(negated_ ? binop::U : binop::R,
					recurse(f1), recurse(f2));
	      return;
	    case binop::W:
595
	      // !(a W b) == !a M !b
596
597
598
599
	      result_ = binop::instance(negated_ ? binop::M : binop::W,
					recurse(f1), recurse(f2));
	      return;
	    case binop::M:
600
	      // !(a M b) == !a W !b
601
602
603
604
	      result_ = binop::instance(negated_ ? binop::W : binop::M,
					recurse(f1), recurse(f2));
	      return;
	    case binop::UConcat:
605
	      // !(a []-> b) == a<>-> !b
606
607
608
609
610
	      result_ = binop::instance(negated_ ?
					binop::EConcat : binop::UConcat,
					recurse_(f1, false), recurse(f2));
	      return;
	    case binop::EConcat:
611
	      // !(a <>-> b) == a[]-> !b
612
613
614
615
616
	      result_ = binop::instance(negated_ ?
					binop::UConcat : binop::EConcat,
					recurse_(f1, false), recurse(f2));
	      return;
	    case binop::EConcatMarked:
617
	      // !(a <>-> b) == a[]-> !b
618
619
620
621
622
623
	      result_ = binop::instance(negated_ ?
					binop::UConcat :
					binop::EConcatMarked,
					recurse_(f1, false), recurse(f2));
	      return;
	    }
624
	  SPOT_UNREACHABLE();
625
626
627
	}

	void
628
	visit(const multop* mo)
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
	{
	  multop::type op = mo->op();
	  /* !(a & b & c) == !a | !b | !c  */
	  /* !(a | b | c) == !a & !b & !c  */
	  if (negated_)
	    switch (op)
	      {
	      case multop::And:
		op = multop::Or;
		break;
	      case multop::Or:
		op = multop::And;
		break;
	      case multop::Concat:
	      case multop::Fusion:
	      case multop::AndNLM:
645
646
	      case multop::OrRat:
	      case multop::AndRat:
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
		break;
	      }
	  multop::vec* res = new multop::vec;
	  unsigned mos = mo->size();
	  switch (op)
	    {
	    case multop::And:
	    case multop::Or:
	      {
		for (unsigned i = 0; i < mos; ++i)
		  res->push_back(recurse(mo->nth(i)));
		result_ = multop::instance(op, res);
		break;
	      }
	    case multop::Concat:
	    case multop::Fusion:
	    case multop::AndNLM:
664
665
	    case multop::AndRat:
	    case multop::OrRat:
666
667
668
669
670
671
672
673
674
	      {
		for (unsigned i = 0; i < mos; ++i)
		  res->push_back(recurse_(mo->nth(i), false));
		result_ = multop::instance(op, res);
		assert(!negated_);
	      }
	    }
	}

675
676
	const formula*
	recurse_(const formula* f, bool negated)
677
	{
678
	  return nenoform_recursively(f, negated, cache_);
679
680
	}

681
682
	const formula*
	recurse(const formula* f)
683
684
685
686
687
	{
	  return recurse_(f, negated_);
	}

      protected:
688
	const formula* result_;
689
690
691
692
693
694
695
696
697
698
	bool negated_;
	ltl_simplifier_cache* cache_;
      };


      const formula*
      nenoform_recursively(const formula* f,
			   bool negated,
			   ltl_simplifier_cache* c)
      {
699
	if (const unop* uo = is_Not(f))
700
	  {
701
702
	    negated = !negated;
	    f = uo->child();
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
	  }

	const formula* key = f;
	if (negated)
	  key = unop::instance(unop::Not, f->clone());
	const formula* result = c->lookup_nenoform(key);
	if (result)
	  goto done;

	if (key->is_in_nenoform()
	    || (c->options.nenoform_stop_on_boolean && key->is_boolean()))
	  {
	    result = key->clone();
	  }
	else
	  {
719
	    negative_normal_form_visitor v(negated, c);
720
	    f->accept(v);
721
722
723
724
725
726
727
728
729
730
731
	    result = v.result();
	  }

	c->cache_nenoform(key, result);
      done:
	if (negated)
	  key->destroy();

	return result;
      }

732
733
734
735
736
737
      //////////////////////////////////////////////////////////////////////
      //
      //  SIMPLIFY_VISITOR
      //
      //////////////////////////////////////////////////////////////////////

738
      // Forward declaration.
739
      const formula*
740
      simplify_recursively(const formula* f, ltl_simplifier_cache* c);
741

742

743

744
745
      // X(a) R b   or   X(a) M b
      // This returns a.
746
747
      const formula*
      is_XRM(const formula* f)
748
      {
749
	const binop* bo = is_binop(f, binop::R, binop::M);
750
	if (!bo)
751
	  return 0;
752
	const unop* uo = is_X(bo->first());
753
754
755
756
757
758
759
	if (!uo)
	  return 0;
	return uo->child();
      }

      // X(a) W b   or   X(a) U b
      // This returns a.
760
761
      const formula*
      is_XWU(const formula* f)
762
      {
763
	const binop* bo = is_binop(f, binop::W, binop::U);
764
	if (!bo)
765
	  return 0;
766
	const unop* uo = is_X(bo->first());
767
768
769
770
771
	if (!uo)
	  return 0;
	return uo->child();
      }

772
773
      // b & X(b W a)  or   b & X(b U a)
      // This returns (b W a) or (b U a).
774
775
      const binop*
      is_bXbWU(const formula* f)
776
      {
777
	const multop* mo = is_multop(f, multop::And);
778
779
780
781
782
	if (!mo)
	  return 0;
	unsigned s = mo->size();
	for (unsigned pos = 0; pos < s; ++pos)
	  {
783
	    const unop* u = is_X(mo->nth(pos));
784
785
	    if (!u)
	      continue;
786
	    const binop* bo = is_binop(u->child(), binop::U, binop::W);
787
788
	    if (!bo)
	      continue;
789
	    const formula* b = mo->all_but(pos);
790
791
792
793
794
795
796
797
798
799
	    bool result = (b == bo->first());
	    b->destroy();
	    if (result)
	      return bo;
	  }
	return 0;
      }

      // b | X(b R a)  or   b | X(b M a)
      // This returns (b R a) or (b M a).
800
801
      const binop*
      is_bXbRM(const formula* f)
802
      {
803
	const multop* mo = is_multop(f, multop::Or);
804
805
806
807
808
	if (!mo)
	  return 0;
	unsigned s = mo->size();
	for (unsigned pos = 0; pos < s; ++pos)
	  {
809
	    const unop* u = is_X(mo->nth(pos));
810
811
	    if (!u)
	      continue;
812
	    const binop* bo = is_binop(u->child(), binop::R, binop::M);
813
814
	    if (!bo)
	      continue;
815
	    const formula* b = mo->all_but(pos);
816
817
818
819
820
821
822
823
	    bool result = (b == bo->first());
	    b->destroy();
	    if (result)
	      return bo;
	  }
	return 0;
      }

824
      const formula*
825
826
827
828
829
      unop_multop(unop::type uop, multop::type mop, multop::vec* v)
      {
	return unop::instance(uop, multop::instance(mop, v));
      }

830
      const formula*
831
832
833
834
835
836
      unop_unop_multop(unop::type uop1, unop::type uop2, multop::type mop,
		       multop::vec* v)
      {
	return unop::instance(uop1, unop_multop(uop2, mop, v));
      }

837
838
      const formula*
      unop_unop(unop::type uop1, unop::type uop2, const formula* f)
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
      {
	return unop::instance(uop1, unop::instance(uop2, f));
      }

      struct mospliter
      {
	enum what { Split_GF = (1 << 0),
		    Strip_GF = (1 << 1) | (1 << 0),
		    Split_FG = (1 << 2),
		    Strip_FG = (1 << 3) | (1 << 2),
		    Split_F = (1 << 4),
		    Strip_F = (1 << 5) | (1 << 4),
		    Split_G = (1 << 6),
		    Strip_G = (1 << 7) | (1 << 6),
		    Strip_X = (1 << 8),
		    Split_U_or_W = (1 << 9),
		    Split_R_or_M = (1 << 10),
		    Split_EventUniv = (1 << 11),
857
858
859
		    Split_Event = (1 << 12),
		    Split_Univ = (1 << 13),
		    Split_Bool = (1 << 14)
860
861
862
863
864
865
866
867
868
869
870
	};

	void init()
	{
	  res_GF = (split_ & Split_GF) ? new multop::vec : 0;
	  res_FG = (split_ & Split_FG) ? new multop::vec : 0;
	  res_F = (split_ & Split_F) ? new multop::vec : 0;
	  res_G = (split_ & Split_G) ? new multop::vec : 0;
	  res_X = (split_ & Strip_X) ? new multop::vec : 0;
	  res_U_or_W = (split_ & Split_U_or_W) ? new multop::vec : 0;
	  res_R_or_M = (split_ & Split_R_or_M) ? new multop::vec : 0;
871
872
873
	  res_EventUniv = (split_ & Split_EventUniv) ? new multop::vec : 0;
	  res_Event = (split_ & Split_Event) ? new multop::vec : 0;
	  res_Univ = (split_ & Split_Univ) ? new multop::vec : 0;
874
875
876
877
	  res_Bool = (split_ & Split_Bool) ? new multop::vec : 0;
	  res_other = new multop::vec;
	}

878
	void process(const formula* f)
879
	{
880
881
882
	  bool e = f->is_eventual();
	  bool u = f->is_universal();
	  bool eu = res_EventUniv && e & u && c_->options.favor_event_univ;
883
884
885
886
	  switch (f->kind())
	    {
	    case formula::UnOp:
	      {
887
888
		const unop* uo = static_cast<const unop*>(f);
		const formula* c = uo->child();
889
890
891
		switch (uo->op())
		  {
		  case unop::X:
892
		    if (res_X && !eu)
893
894
895
896
		      {
			res_X->push_back(c->clone());
			return;
		      }
897
898
		    break;
		  case unop::F:
899
		    if (res_FG && u)
900
901
902
903
904
905
		      if (const unop* cc = is_G(c))
			{
			  res_FG->push_back(((split_ & Strip_FG) == Strip_FG
					     ? cc->child() : f)->clone());
			  return;
			}
906
		    if (res_F && !eu)
907
908
909
910
911
		      {
			res_F->push_back(((split_ & Strip_F) == Strip_F
					  ? c : f)->clone());
			return;
		      }
912
913
		    break;
		  case unop::G:
914
		    if (res_GF && e)
915
916
917
918
919
920
		      if (const unop* cc = is_F(c))
			{
			  res_GF->push_back(((split_ & Strip_GF) == Strip_GF
					     ? cc->child() : f)->clone());
			  return;
			}
921
		    if (res_G && !eu)
922
923
924
925
926
		      {
			res_G->push_back(((split_ & Strip_G) == Strip_G
					  ? c : f)->clone());
			return;
		      }
927
928
929
930
931
932
933
934
		    break;
		  default:
		    break;
		  }
	      }
	      break;
	    case formula::BinOp:
	      {
935
		const binop* bo = static_cast<const binop*>(f);
936
937
938
939
940
		switch (bo->op())
		  {
		  case binop::U:
		  case binop::W:
		    if (res_U_or_W)
941
942
943
944
		      {
			res_U_or_W->push_back(bo->clone());
			return;
		      }
945
946
947
948
		    break;
		  case binop::R:
		  case binop::M:
		    if (res_R_or_M)
949
950
951
952
		      {
			res_R_or_M->push_back(bo->clone());
			return;
		      }
953
954
955
956
957
958
959
960
		    break;
		  default:
		    break;
		  }
	      }
	      break;
	    default:
	      if (res_Bool && f->is_boolean())
961
962
963
964
		{
		  res_Bool->push_back(f->clone());
		  return;
		}
965
966
	      break;
	    }
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
	  if (c_->options.event_univ)
	    {
	      if (res_EventUniv && e && u)
		{
		  res_EventUniv->push_back(f->clone());
		  return;
		}
	      if (res_Event && e)
		{
		  res_Event->push_back(f->clone());
		  return;
		}
	      if (res_Univ && u)
		{
		  res_Univ->push_back(f->clone());
		  return;
		}
	    }

	  res_other->push_back(f->clone());
987
988
	}

989
990
	mospliter(unsigned split, multop::vec* v, ltl_simplifier_cache* cache)
	  : split_(split), c_(cache)
991
992
	{
	  init();
993
	  for (auto f: *v)
994
	    {
995
	      if (f) // skip null pointers left by previous simplifications
996
		{
997
998
		  process(f);
		  f->destroy();
999
		}
1000
1001
1002
1003
	    }
	  delete v;
	}

1004
1005
	mospliter(unsigned split, const multop* mo,
		  ltl_simplifier_cache* cache)
1006
	  : split_(split), c_(cache)
1007
1008
1009
1010
1011
	{
	  init();
	  unsigned mos = mo->size();
	  for (unsigned i = 0; i < mos; ++i)
	    {
1012
	      const formula* f = simplify_recursively(mo->nth(i), cache);
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
	      process(f);
	      f->destroy();
	    }
	  mo->destroy();
	}

	multop::vec* res_GF;
	multop::vec* res_FG;
	multop::vec* res_F;
	multop::vec* res_G;
	multop::vec* res_X;
	multop::vec* res_U_or_W;
	multop::vec* res_R_or_M;
	multop::vec* res_Event;
	multop::vec* res_Univ;
	multop::vec* res_EventUniv;
	multop::vec* res_Bool;
	multop::vec* res_other;
	unsigned split_;
1032
	ltl_simplifier_cache* c_;
1033
1034
      };

1035
1036
1037
      class simplify_visitor: public visitor
      {
      public:
1038

1039
1040
1041
1042
	simplify_visitor(ltl_simplifier_cache* cache)
	  : c_(cache), opt_(cache->options)
	{
	}
1043

1044
1045
1046
1047
	virtual ~simplify_visitor()
	{
	}

1048
	const formula*
1049
1050
1051
1052
1053
1054
	result() const
	{
	  return result_;
	}

	void
1055
	visit(const atomic_prop* ap)
1056
	{
1057
	  result_ = ap->clone();
1058
1059
1060
	}

	void
1061
	visit(const constant* c)
1062
1063
1064
1065
1066
	{
	  result_ = c;
	}

	void
1067
	visit(const bunop* bo)
1068
	{
1069
1070
	  bunop::type op = bo->op();
	  unsigned min = bo->min();
1071
	  const formula* h = recurse(bo->child());
1072
1073
1074
1075
1076
1077
1078
	  switch (op)
	    {
	    case bunop::Star:
	      if (h->accepts_eword())
		min = 0;
	      if (min == 0)
		{
1079
1080
1081
1082
		  const formula* s =
		    bo->max() == bunop::unbounded ?
		    c_->star_normal_form(h) :
		    c_->star_normal_form_bounded(h);
1083
		  h->destroy();
1084
		  h = s;
1085
1086
1087
1088
		}
	      result_ = bunop::instance(op, h, min, bo->max());
	      break;
	    }
1089
	}
1090

1091
1092
	// if !neg build c&X(c&X(...&X(tail))) with n occurences of c
	// if neg build !c|X(!c|X(...|X(tail))).
1093
1094
1095
	const formula*
	dup_b_x_tail(bool neg, const formula* c,
		     const formula* tail, unsigned n)
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
	{
	  c->clone();
	  multop::type mop;
	  if (neg)
	    {
	      c = unop::instance(unop::Not, c);
	      mop = multop::Or;
	    }
	  else
	    {
	      mop = multop::And;
	    }
	  while (n--)
	    {
	      tail = unop::instance(unop::X, tail);
	      tail = // b&X(tail) or !b|X(tail)
		multop::instance(mop, c->clone(), tail);
	    }
	  c->destroy();
	  return tail;
	}

1118
	void
1119
	visit(const unop* uo)
1120
1121
1122
	{
	  result_ = recurse(uo->child());

1123
1124
	  unop::type op = uo->op();
	  switch (op)
1125
	    {
1126
1127
1128
1129
	    case unop::Not:
	      break;

	    case unop::X:
1130
1131
1132
1133
1134
	      // X(constant) = constant is a trivial identity, but if
	      // the constant has been constructed by recurse() this
	      // identity has not been applied.
	      if (is_constant(result_))
		  return;
1135

1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
	      // Xf = f if f is both eventual and universal.
	      if (result_->is_universal() && result_->is_eventual())
		{
		  if (opt_.event_univ)
		    return;
		  // If EventUniv simplification is disabled, use
		  // only the following basic rewriting rules:
		  //   XGF(f) = GF(f) and XFG(f) = FG(f)
		  // The former comes from Somenzi&Bloem (CAV'00).
		  // It's not clear why they do not list the second.
1146
1147
		  if (opt_.reduce_basics &&
		      (is_GF(result_) || is_FG(result_)))
1148
1149
		    return;
		}
1150

1151
1152
1153
1154
1155
1156

	      // If Xa = a, keep only a.
	      if (opt_.containment_checks_stronger
		  && c_->lcc.equal(result_, uo))
		return;

1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
	      // X(f1 & GF(f2)) = X(f1) & GF(f2)
	      // X(f1 | GF(f2)) = X(f1) | GF(f2)
	      // X(f1 & FG(f2)) = X(f1) & FG(f2)
	      // X(f1 | FG(f2)) = X(f1) | FG(f2)
	      //
	      // The above usually make more sense when reversed (see
	      // them in the And and Or rewritings), except when we
	      // try to maximaze the size of subformula that do not
	      // have EventUniv formulae.
	      if (opt_.favor_event_univ)
		if (const multop* mo = is_multop(result_,
						 multop::Or, multop::And))
		  {
		    mospliter s(mospliter::Split_EventUniv, mo, c_);
		    multop::type op = mo->op();
		    s.res_EventUniv->push_back(unop_multop(unop::X, op,
							   s.res_other));
		    result_ = multop::instance(op, s.res_EventUniv);
		    if (result_ != uo)
		      result_ = recurse_destroy(result_);
		    return;
		  }
1179
1180
	      break;

1181
	    case unop::F:
1182
1183
1184
1185
1186
	      // F(constant) = constant is a trivial identity, but if
	      // the constant has been constructed by recurse() this
	      // identity has not been applied.
	      if (is_constant(result_))
		  return;
1187
1188
1189
1190
1191

	      // If f is a pure eventuality formula then F(f)=f.
	      if (opt_.event_univ && result_->is_eventual())
		return;

1192
1193
1194
	      if (opt_.reduce_basics)
		{
		  // F(a U b) = F(b)
1195
		  const binop* bo = is_U(result_);
1196
1197
		  if (bo)
		    {
1198
		      const formula* r =
1199
1200
1201
1202
1203
			unop::instance(unop::F, bo->second()->clone());
		      bo->destroy();
		      result_ = recurse_destroy(r);
		      return;
		    }
1204

1205
1206
1207
1208
		  // F(a M b) = F(a & b)
		  bo = is_M(result_);
		  if (bo)
		    {
1209
		      const formula* r =
1210
1211
1212
1213
1214
1215
1216
1217
1218
			unop::instance(unop::F,
				       multop::instance(multop::And,
							bo->first()->clone(),
							bo->second()->clone()));
		      bo->destroy();
		      result_ = recurse_destroy(r);
		      return;
		    }

1219
		  // FX(a) = XF(a)
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
		  if (const unop* u = is_X(result_))
		    {
		      const formula* res =
			unop_unop(unop::X, unop::F, u->child()->clone());
		      u->destroy();
		      // FXX(a) = XXF(a) ...
		      // FXG(a) = XFG(a) = FG(a) ...
		      result_ = recurse_destroy(res);
		      return;
		    }
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259

		  // FG(a & Xb) = FG(a & b)
		  // FG(a & Gb) = FG(a & b)
		  if (const unop* g = is_G(result_))
		    if (const multop* m = is_And(g->child()))
		      if (!m->is_boolean())
			{
			  m->clone();
			  mospliter s(mospliter::Strip_G | mospliter::Strip_X,
				      m, c_);
			  if (!s.res_G->empty() || !s.res_X->empty())
			    {
			      result_->destroy();
			      s.res_other->insert(s.res_other->begin(),
						  s.res_G->begin(),
						  s.res_G->end());
			      delete s.res_G;
			      s.res_other->insert(s.res_other->begin(),
						  s.res_X->begin(),
						  s.res_X->end());
			      delete s.res_X;
			      const formula* in =
				multop::instance(multop::And, s.res_other);
			      result_ =
				recurse_destroy(unop_unop(unop::F, unop::G,
							  in));
			      return;
			    }
			  else
			    {
1260
1261
1262
			      for (auto f: *s.res_other)
				if (f)
				  f->destroy();
1263
1264
1265
1266
1267
1268
			      delete s.res_other;
			      delete s.res_G;
			      delete s.res_X;
			      // and continue...
			    }
			}
1269
		}
1270
1271
1272
1273
1274

	      // if Fa => a, keep a.
	      if (opt_.containment_checks_stronger
		  && c_->lcc.contained(uo, result_))
		return;
1275

1276
1277
	      // Disabled by default:
	      //     F(f1 & GF(f2)) = F(f1) & GF(f2)
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
	      //
	      // As is, these two formulae are translated into
	      // equivalent Büchi automata so the rewriting is
	      // useless.
	      //
	      // However when taken in a larger formula such as F(f1
	      // & GF(f2)) | F(a & GF(b)), this rewriting used to
	      // produce (F(f1) & GF(f2)) | (F(a) & GF(b)), missing
	      // the opportunity to apply the F(E1)|F(E2) = F(E1|E2)
	      // rule which really helps the translation. F((f1 &
	      // GF(f2)) | (a & GF(b))) is indeed easier to translate.
	      //
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
	      // So we do not consider this rewriting rule by default.
	      // However if favor_event_univ is set, we want to move
	      // the GF out of the F.
	      if (opt_.favor_event_univ)
		// F(f1&f2&FG(f3)&FG(f4)&f5&f6) =
		//                        F(f1&f2) & FG(f3&f4) & f5 & f6
		// if f5 and f6 are both eventual and universal.
		if (const multop* mo = is_And(result_))
		  {
		    mo->clone();
		    mospliter s(mospliter::Strip_FG |
				mospliter::Split_EventUniv,
				mo, c_);
		    s.res_EventUniv->
		      push_back(unop_multop(unop::F, multop::And,
					    s.res_other));
		    s.res_EventUniv->
		      push_back(unop_unop_multop(unop::F, unop::G,
						 multop::And, s.res_FG));
		    result_ = multop::instance(multop::And, s.res_EventUniv);
		    if (result_ != uo)
		      {
			mo->destroy();
			result_ = recurse_destroy(result_);
			return;
		      }
		    else
		      {
			// Revert to the previous value of result_,
			// for the next simplification.
			result_->destroy();
			result_ = mo;
		      }
		  }
	      // If u3 and u4 are universal formulae and h is not:
	      // F(f1 | f2 | Fu3 | u4 | FGg | Fh)
	      //    = F(f1 | f2 | u3 | u4 | Gg | h)
	      // or
	      // F(f1 | f2 | Fu3 | u4 | FGg | Fh)
	      //    = F(f1 | f2 | h) | F(u3 | u4 | Gg)
	      // depending on whether favor_event_univ is set.
	      if (const multop* mo = is_Or(result_))
		{
		  mo->clone();
		  int w = mospliter::Strip_F;
		  if (opt_.favor_event_univ)
		    w |= mospliter::Split_Univ;
		  mospliter s(w, mo, c_);
		  s.res_other->insert(s.res_other->end(),
				      s.res_F->begin(), s.res_F->end());
		  delete s.res_F;
		  result_ = unop_multop(unop::F, multop::Or, s.res_other);
		  if (s.res_Univ)
		    {
		      // Strip any F.
1345
1346
		      for (auto& f: *s.res_Univ)
			if (const unop* u = is_F(f))
1347
			  {
1348
			    f = u->child()->clone();
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
			    u->destroy();
			  }
		      const formula* fu =
			unop_multop(unop::F, multop::Or, s.res_Univ);
		      result_ = multop::instance(multop::Or, result_, fu);
		    }
		  if (result_ != uo)
		    {
		      mo->destroy();
		      result_ = recurse_destroy(result_);
		      return;
		    }
		  else
		    {
		      // Revert to the previous value of result_,
		      // for the next simplification.
		      result_->destroy();
		      result_ = mo;
		    }
		}
1369
	      break;
1370
1371

	    case unop::G:
1372
1373
1374
1375
1376
	      // G(constant) = constant is a trivial identity, but if
	      // the constant has been constructed by recurse() this
	      // identity has not been applied.
	      if (is_constant(result_))
		  return;
1377

1378
1379
1380
1381
	      // If f is a pure universality formula then G(f)=f.
	      if (opt_.event_univ && result_->is_universal())
		return;

1382
	      if (opt_.reduce_basics)
1383
		{
1384
		  // G(a R b) = G(b)
1385
		  const binop* bo = is_R(result_);
1386
		  if (bo)
1387
		    {
1388
		      const formula* r =
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
			unop::instance(unop::G, bo->second()->clone());
		      bo->destroy();
		      result_ = recurse_destroy(r);
		      return;
		    }

		  // G(a W b) = G(a | b)
		  bo = is_W(result_);
		  if (bo)
		    {
1399
		      const formula* r =
1400
1401
1402
1403
1404
1405
1406
			unop::instance(unop::G,
				       multop::instance(multop::Or,
							bo->first()->clone(),
							bo->second()->clone()));
		      bo->destroy();
		      result_ = recurse_destroy(r);
		      return;
1407
1408
		    }

1409
		  // GX(a) = XG(a)
1410
		  if (const unop* u = is_X(result_))
1411
		    {
1412
1413
1414
1415
1416
1417
1418
		      const formula* res =
			unop_unop(unop::X, unop::G, u->child()->clone());
		      u->destroy();
		      // GXX(a) = XXG(a) ...
		      // GXF(a) = XGF(a) = GF(a) ...
		      result_ = recurse_destroy(res);
		      return;
1419
1420
		    }

1421
1422
1423
		  // G(f1|f2|GF(f3)|GF(f4)|f5|f6) =
		  //                        G(f1|f2) | GF(f3|f4) | f5 | f6
		  // if f5 and f6 are both eventual and universal.