simplify.cc 120 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
520
521
522
523
524
525
				       recurse_(f, false));
	      return;
	      /* !Finish(x), is not simplified */
	    case unop::Finish:
	      result_ = unop::instance(uo->op(), recurse_(f, false));
	      if (negated_)
		result_ = unop::instance(unop::Not, result_);
	      return;
	    }
526
	  SPOT_UNREACHABLE();
527
528
529
	}

	void
530
	visit(const bunop* bo)
531
	{
532
533
	  // !(a*) should never occur.
	  assert(!negated_);
534
535
536
537
	  result_ = bunop::instance(bo->op(), recurse_(bo->child(), false),
				    bo->min(), bo->max());
	}

538
539
540
	const formula* equiv_or_xor(bool equiv,
				    const formula* f1,
				    const formula* f2)
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
	{
	  // 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)));
	}

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

	void
634
	visit(const multop* mo)
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
	{
	  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:
651
652
	      case multop::OrRat:
	      case multop::AndRat:
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
		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:
670
671
	    case multop::AndRat:
	    case multop::OrRat:
672
673
674
675
676
677
678
679
680
	      {
		for (unsigned i = 0; i < mos; ++i)
		  res->push_back(recurse_(mo->nth(i), false));
		result_ = multop::instance(op, res);
		assert(!negated_);
	      }
	    }
	}

681
682
	const formula*
	recurse_(const formula* f, bool negated)
683
	{
684
	  return nenoform_recursively(f, negated, cache_);
685
686
	}

687
688
	const formula*
	recurse(const formula* f)
689
690
691
692
693
	{
	  return recurse_(f, negated_);
	}

      protected:
694
	const formula* result_;
695
696
697
698
699
700
701
702
703
704
	bool negated_;
	ltl_simplifier_cache* cache_;
      };


      const formula*
      nenoform_recursively(const formula* f,
			   bool negated,
			   ltl_simplifier_cache* c)
      {
705
	if (const unop* uo = is_Not(f))
706
	  {
707
708
	    negated = !negated;
	    f = uo->child();
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
	  }

	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
	  {
725
	    negative_normal_form_visitor v(negated, c);
726
	    f->accept(v);
727
728
729
730
731
732
733
734
735
736
737
	    result = v.result();
	  }

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

	return result;
      }

738
739
740
741
742
743
      //////////////////////////////////////////////////////////////////////
      //
      //  SIMPLIFY_VISITOR
      //
      //////////////////////////////////////////////////////////////////////

744
      // Forward declaration.
745
      const formula*
746
      simplify_recursively(const formula* f, ltl_simplifier_cache* c);
747

748

749

750
751
      // X(a) R b   or   X(a) M b
      // This returns a.
752
753
      const formula*
      is_XRM(const formula* f)
754
      {
755
	const binop* bo = is_binop(f, binop::R, binop::M);
756
	if (!bo)
757
	  return 0;
758
	const unop* uo = is_X(bo->first());
759
760
761
762
763
764
765
	if (!uo)
	  return 0;
	return uo->child();
      }

      // X(a) W b   or   X(a) U b
      // This returns a.
766
767
      const formula*
      is_XWU(const formula* f)
768
      {
769
	const binop* bo = is_binop(f, binop::W, binop::U);
770
	if (!bo)
771
	  return 0;
772
	const unop* uo = is_X(bo->first());
773
774
775
776
777
	if (!uo)
	  return 0;
	return uo->child();
      }

778
779
      // b & X(b W a)  or   b & X(b U a)
      // This returns (b W a) or (b U a).
780
781
      const binop*
      is_bXbWU(const formula* f)
782
      {
783
	const multop* mo = is_multop(f, multop::And);
784
785
786
787
788
	if (!mo)
	  return 0;
	unsigned s = mo->size();
	for (unsigned pos = 0; pos < s; ++pos)
	  {
789
	    const unop* u = is_X(mo->nth(pos));
790
791
	    if (!u)
	      continue;
792
	    const binop* bo = is_binop(u->child(), binop::U, binop::W);
793
794
	    if (!bo)
	      continue;
795
	    const formula* b = mo->all_but(pos);
796
797
798
799
800
801
802
803
804
805
	    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).
806
807
      const binop*
      is_bXbRM(const formula* f)
808
      {
809
	const multop* mo = is_multop(f, multop::Or);
810
811
812
813
814
	if (!mo)
	  return 0;
	unsigned s = mo->size();
	for (unsigned pos = 0; pos < s; ++pos)
	  {
815
	    const unop* u = is_X(mo->nth(pos));
816
817
	    if (!u)
	      continue;
818
	    const binop* bo = is_binop(u->child(), binop::R, binop::M);
819
820
	    if (!bo)
	      continue;
821
	    const formula* b = mo->all_but(pos);
822
823
824
825
826
827
828
829
	    bool result = (b == bo->first());
	    b->destroy();
	    if (result)
	      return bo;
	  }
	return 0;
      }

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

836
      const formula*
837
838
839
840
841
842
      unop_unop_multop(unop::type uop1, unop::type uop2, multop::type mop,
		       multop::vec* v)
      {
	return unop::instance(uop1, unop_multop(uop2, mop, v));
      }

843
844
      const formula*
      unop_unop(unop::type uop1, unop::type uop2, const formula* f)
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
      {
	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),
863
864
865
		    Split_Event = (1 << 12),
		    Split_Univ = (1 << 13),
		    Split_Bool = (1 << 14)
866
867
868
869
870
871
872
873
874
875
876
	};

	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;
877
878
879
	  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;
880
881
882
883
	  res_Bool = (split_ & Split_Bool) ? new multop::vec : 0;
	  res_other = new multop::vec;
	}

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

995
996
	mospliter(unsigned split, multop::vec* v, ltl_simplifier_cache* cache)
	  : split_(split), c_(cache)
997
998
	{
	  init();
999
	  for (auto f: *v)
1000
	    {
1001
	      if (f) // skip null pointers left by previous simplifications
1002
		{
1003
1004
		  process(f);
		  f->destroy();
1005
		}
1006
1007
1008
1009
	    }
	  delete v;
	}

1010
1011
	mospliter(unsigned split, const multop* mo,
		  ltl_simplifier_cache* cache)
1012
	  : split_(split), c_(cache)
1013
1014
1015
1016
1017
	{
	  init();
	  unsigned mos = mo->size();
	  for (unsigned i = 0; i < mos; ++i)
	    {
1018
	      const formula* f = simplify_recursively(mo->nth(i), cache);
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
	      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_;
1038
	ltl_simplifier_cache* c_;
1039
1040
      };

1041
1042
1043
      class simplify_visitor: public visitor
      {
      public:
1044

1045
1046
1047
1048
	simplify_visitor(ltl_simplifier_cache* cache)
	  : c_(cache), opt_(cache->options)
	{
	}
1049

1050
1051
1052
1053
	virtual ~simplify_visitor()
	{
	}

1054
	const formula*
1055
1056
1057
1058
1059
1060
	result() const
	{
	  return result_;
	}

	void
1061
	visit(const atomic_prop* ap)
1062
	{
1063
	  result_ = ap->clone();
1064
1065
1066
	}

	void
1067
	visit(const constant* c)
1068
1069
1070
1071
1072
	{
	  result_ = c;
	}

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

1097
1098
	// if !neg build c&X(c&X(...&X(tail))) with n occurences of c
	// if neg build !c|X(!c|X(...|X(tail))).
1099
1100
1101
	const formula*
	dup_b_x_tail(bool neg, const formula* c,
		     const formula* tail, unsigned n)
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
	{
	  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;
	}

1124
	void
1125
	visit(const unop* uo)
1126
1127
1128
	{
	  result_ = recurse(uo->child());

1129
1130
	  unop::type op = uo->op();
	  switch (op)
1131
	    {
1132
1133
1134
1135
	    case unop::Not:
	      break;

	    case unop::X:
1136
1137
1138
1139
1140
	      // 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;
1141

1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
	      // 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.
1152
1153
		  if (opt_.reduce_basics &&
		      (is_GF(result_) || is_FG(result_)))
1154
1155
		    return;
		}
1156

1157
1158
1159
1160
1161
1162

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

1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
	      // 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;
		  }
1185
1186
	      break;

1187
	    case unop::F:
1188
1189
1190
1191
1192
	      // 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;
1193
1194
1195
1196
1197

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

1198
1199
1200
	      if (opt_.reduce_basics)
		{
		  // F(a U b) = F(b)
1201
		  const binop* bo = is_U(result_);
1202
1203
		  if (bo)
		    {
1204
		      const formula* r =
1205
1206
1207
1208
1209
			unop::instance(unop::F, bo->second()->clone());
		      bo->destroy();
		      result_ = recurse_destroy(r);
		      return;
		    }
1210

1211
1212
1213
1214
		  // F(a M b) = F(a & b)
		  bo = is_M(result_);
		  if (bo)
		    {
1215
		      const formula* r =
1216
1217
1218
1219
1220
1221
1222
1223
1224
			unop::instance(unop::F,
				       multop::instance(multop::And,
							bo->first()->clone(),
							bo->second()->clone()));
		      bo->destroy();
		      result_ = recurse_destroy(r);
		      return;
		    }

1225
		  // FX(a) = XF(a)
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
		  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;
		    }
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265

		  // 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
			    {
1266
1267
1268
			      for (auto f: *s.res_other)
				if (f)
				  f->destroy();
1269
1270
1271
1272
1273
1274
			      delete s.res_other;
			      delete s.res_G;
			      delete s.res_X;
			      // and continue...
			    }
			}
1275
		}
1276
1277
1278
1279
1280

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

1282
1283
	      // Disabled by default:
	      //     F(f1 & GF(f2)) = F(f1) & GF(f2)
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
	      //
	      // 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.
	      //
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
1345
1346
1347
1348
1349
1350
	      // 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.
1351
1352
		      for (auto& f: *s.res_Univ)
			if (const unop* u = is_F(f))
1353
			  {
1354
			    f = u->child()->clone();
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
			    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;
		    }
		}
1375
	      break;
1376
1377

	    case unop::G:
1378
1379
1380
1381
1382
	      // 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;
1383

1384
1385
1386
1387
	      // If f is a pure universality formula then G(f)=f.
	      if (opt_.event_univ && result_->is_universal())
		return;

1388
	      if (opt_.reduce_basics)
1389
		{
1390
		  // G(a R b) = G(b)
1391
		  const binop* bo = is_R(result_);
1392
		  if (bo)
1393
		    {
1394
		      const formula* r =
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
			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)
		    {
1405
		      const formula* r =
1406
1407
1408
1409
1410
1411
1412
			unop::instance(unop::G,
				       multop::instance(multop::Or,
							bo->first()->clone(),
							bo->second()->clone()));
		      bo->destroy();
		      result_ = recurse_destroy(r);
		      return;
1413
1414
		    }

1415
		  // GX(a) = XG(a)
1416
		  if (const unop* u = is_X(result_))
1417
		    {
1418
1419
1420
1421
1422
1423
1424
		      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;
1425
1426
		    }

1427
1428
1429
		  // G(f1|f2|GF(f3)|GF(f4)|f5|f6) =
		  //