ltl2tgba_fm.cc 36.1 KB
Newer Older
1
2
3
// Copyright (C) 2008, 2009, 2010 Laboratoire de Recherche et
// Dveloppement de l'Epita (LRDE).
// Copyright (C) 2003, 2004, 2005, 2006 Laboratoire
4
5
// d'Informatique de Paris 6 (LIP6), dpartement Systmes Rpartis
// Coopratifs (SRC), Universit Pierre et Marie Curie.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

24
#include "misc/hash.hh"
25
#include "misc/bddalloc.hh"
26
#include "misc/bddlt.hh"
27
#include "misc/minato.hh"
28
29
#include "ltlast/visitor.hh"
#include "ltlast/allnodes.hh"
30
31
32
#include "ltlvisit/lunabbrev.hh"
#include "ltlvisit/nenoform.hh"
#include "ltlvisit/tostring.hh"
33
#include "ltlvisit/postfix.hh"
34
#include "ltlvisit/apcollect.hh"
35
36
#include "ltlvisit/mark.hh"
#include "ltlvisit/tostring.hh"
37
#include <cassert>
38
#include <memory>
39
#include "ltl2tgba_fm.hh"
40
#include "ltlvisit/contain.hh"
41
42
#include "ltlvisit/consterm.hh"
#include "tgba/bddprint.hh"
43
44
45
46
47
48
49
50

namespace spot
{
  using namespace ltl;

  namespace
  {

51
52
    // Helper dictionary.  We represent formulae using BDDs to
    // simplify them, and then translate BDDs back into formulae.
53
54
55
56
57
    //
    // The name of the variables are inspired from Couvreur's FM paper.
    //   "a" variables are promises (written "a" in the paper)
    //   "next" variables are X's operands (the "r_X" variables from the paper)
    //   "var" variables are atomic propositions.
58
    class translate_dict
59
60
61
    {
    public:

62
63
      translate_dict(bdd_dict* dict)
	: dict(dict),
64
65
66
67
68
69
70
71
72
73
	  a_set(bddtrue),
	  var_set(bddtrue),
	  next_set(bddtrue)
      {
      }

      ~translate_dict()
      {
	fv_map::iterator i;
	for (i = next_map.begin(); i != next_map.end(); ++i)
74
	  i->first->destroy();
75
	dict->unregister_all_my_variables(this);
76
77
      }

78
79
      bdd_dict* dict;

80
81
      typedef bdd_dict::fv_map fv_map;
      typedef bdd_dict::vf_map vf_map;
82
83
84
85
86
87
88
89
90

      fv_map next_map;	       ///< Maps "Next" variables to BDD variables
      vf_map next_formula_map; ///< Maps BDD variables to "Next" variables

      bdd a_set;
      bdd var_set;
      bdd next_set;

      int
91
      register_proposition(const formula* f)
92
      {
93
	int num = dict->register_proposition(f, this);
94
95
96
97
98
	var_set &= bdd_ithvar(num);
	return num;
      }

      int
99
      register_a_variable(const formula* f)
100
      {
101
	int num = dict->register_acceptance_variable(f, this);
102
103
104
105
106
	a_set &= bdd_ithvar(num);
	return num;
      }

      int
107
      register_next_variable(const formula* f)
108
109
110
111
112
113
114
115
116
117
      {
	int num;
	// Do not build a Next variable that already exists.
	fv_map::iterator sii = next_map.find(f);
	if (sii != next_map.end())
	  {
	    num = sii->second;
	  }
	else
	  {
118
	    f = f->clone();
119
	    num = dict->register_anonymous_variables(1, this);
120
121
122
123
124
125
126
	    next_map[f] = num;
	    next_formula_map[num] = f;
	  }
	next_set &= bdd_ithvar(num);
	return num;
      }

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
      std::ostream&
      dump(std::ostream& os) const
      {
	fv_map::const_iterator fi;
	os << "Next Variables:" << std::endl;
	for (fi = next_map.begin(); fi != next_map.end(); ++fi)
	{
	  os << "  " << fi->second << ": Next[";
	  to_string(fi->first, os) << "]" << std::endl;
	}
	os << "Shared Dict:" << std::endl;
	dict->dump(os);
	return os;
      }

142
      formula*
143
144
145
146
      var_to_formula(int var) const
      {
	vf_map::const_iterator isi = next_formula_map.find(var);
	if (isi != next_formula_map.end())
147
	  return isi->second->clone();
148
149
	isi = dict->acc_formula_map.find(var);
	if (isi != dict->acc_formula_map.end())
150
	  return isi->second->clone();
151
152
	isi = dict->var_formula_map.find(var);
	if (isi != dict->var_formula_map.end())
153
	  return isi->second->clone();
154
	assert(0);
155
156
157
	// Never reached, but some GCC versions complain about
	// a missing return otherwise.
	return 0;
158
159
      }

160
      formula*
161
      conj_bdd_to_formula(bdd b) const
162
163
      {
	if (b == bddfalse)
164
165
	  return constant::false_instance();
	multop::vec* v = new multop::vec;
166
167
168
	while (b != bddtrue)
	  {
	    int var = bdd_var(b);
169
	    formula* res = var_to_formula(var);
170
171
172
	    bdd high = bdd_high(b);
	    if (high == bddfalse)
	      {
173
		res = unop::instance(unop::Not, res);
174
175
176
177
		b = bdd_low(b);
	      }
	    else
	      {
178
		assert(bdd_low(b) == bddfalse);
179
180
181
182
183
		b = high;
	      }
	    assert(b != bddfalse);
	    v->push_back(res);
	  }
184
	return multop::instance(multop::And, v);
185
186
      }

187
188
      const formula*
      bdd_to_formula(bdd f)
189
      {
190
	if (f == bddfalse)
191
	  return constant::false_instance();
192

193
194
195
196
197
198
199
200
201
	multop::vec* v = new multop::vec;

	minato_isop isop(f);
	bdd cube;
	while ((cube = isop.next()) != bddfalse)
	  v->push_back(conj_bdd_to_formula(cube));

	return multop::instance(multop::Or, v);
      }
202
203

      void
204
      conj_bdd_to_acc(tgba_explicit_formula* a, bdd b,
Pierre PARUTTO's avatar
Pierre PARUTTO committed
205
		      state_explicit_formula::transition* t)
206
207
208
209
210
211
212
213
      {
	assert(b != bddfalse);
	while (b != bddtrue)
	  {
	    int var = bdd_var(b);
	    bdd high = bdd_high(b);
	    if (high == bddfalse)
	      {
214
		// Simply ignore negated acceptance variables.
215
216
217
218
		b = bdd_low(b);
	      }
	    else
	      {
219
		formula* ac = var_to_formula(var);
220

221
		if (!a->has_acceptance_condition(ac))
222
		  a->declare_acceptance_condition(ac->clone());
223
		a->add_acceptance_condition(t, ac);
224
225
226
227
228
229
230
231
		b = high;
	      }
	    assert(b != bddfalse);
	  }
      }
    };


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    // Debugging function.
    std::ostream&
    trace_ltl_bdd(const translate_dict& d, bdd f)
    {
      minato_isop isop(f);
      bdd cube;
      while ((cube = isop.next()) != bddfalse)
	{
	  bdd label = bdd_exist(cube, d.next_set);
	  bdd dest_bdd = bdd_existcomp(cube, d.next_set);
	  const formula* dest = d.conj_bdd_to_formula(dest_bdd);
	  bdd_print_set(std::cerr, d.dict, label) << " => "
						  << to_string(dest)
						  << std::endl;
	  dest->destroy();
	}
      return std::cerr;
    }


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

    // Gather all promises of a formula.  These are the
    // right-hand sides of U or F operators.
    class ltl_promise_visitor: public postfix_visitor
    {
    public:
      ltl_promise_visitor(translate_dict& dict)
	: dict_(dict), res_(bddtrue)
      {
      }

      virtual
      ~ltl_promise_visitor()
      {
      }

      bdd
      result() const
      {
	return res_;
      }

      using postfix_visitor::doit;

      virtual void
      doit(unop* node)
      {
	if (node->op() == unop::F)
	  res_ &= bdd_ithvar(dict_.register_a_variable(node->child()));
      }

      virtual void
      doit(binop* node)
      {
	if (node->op() == binop::U)
	  res_ &= bdd_ithvar(dict_.register_a_variable(node->second()));
      }

    private:
      translate_dict& dict_;
      bdd res_;
    };

295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
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
    // Rewrite rule for rational operators.
    class ratexp_trad_visitor: public const_visitor
    {
    public:
      ratexp_trad_visitor(translate_dict& dict,
			  formula* to_concat = 0)
	: dict_(dict), to_concat_(to_concat)
      {
      }

      virtual
      ~ratexp_trad_visitor()
      {
	if (to_concat_)
	  to_concat_->destroy();
      }

      bdd
      result() const
      {
	return res_;
      }

      bdd next_to_concat()
      {
	if (!to_concat_)
	  to_concat_ = constant::empty_word_instance();
	int x = dict_.register_next_variable(to_concat_);
	return bdd_ithvar(x);
      }

      bdd now_to_concat()
      {
	if (to_concat_)
	  {
	    if (to_concat_ == constant::empty_word_instance())
	      return bddfalse;
	    bdd n = recurse(to_concat_);
	    return n;
	  }
	else
	  {
	    return bddfalse;
	  }
      }

      void
      visit(const atomic_prop* node)
      {
	res_ = (bdd_ithvar(dict_.register_proposition(node))
		& next_to_concat());
      }

      void
      visit(const constant* node)
      {
	switch (node->val())
	  {
	  case constant::True:
	    res_ = next_to_concat();
	    return;
	  case constant::False:
	    res_ = bddfalse;
	    return;
	  case constant::EmptyWord:
	    res_ = now_to_concat();
	    return;
	  }
	/* Unreachable code.  */
	assert(0);
      }

      void
      visit(const unop* node)
      {
	switch (node->op())
	  {
	  case unop::F:
	  case unop::G:
	  case unop::Not:
	  case unop::X:
	  case unop::Finish:
	    assert(!"not a rational operator");
	    return;
	  case unop::Star:
	    {
	      formula* f;
	      if (to_concat_)
		f = multop::instance(multop::Concat, node->clone(),
				     to_concat_->clone());
	      else
		f = node->clone();

	      res_ = recurse(node->child(), f) | now_to_concat();
	      return;
	    }
	  }
	/* Unreachable code.  */
	assert(0);
      }

      void
      visit(const binop*)
      {
	assert(!"not a rational operator");
      }

      void
      visit(const automatop*)
      {
	assert(!"not a rational operator");
      }

      void
      visit(const multop* node)
      {
	switch (node->op())
	  {
	  case multop::And:
	    {
	      res_ = bddtrue;
	      unsigned s = node->size();
	      for (unsigned n = 0; n < s; ++n)
	      {
		bdd res = recurse(node->nth(n));
		// trace_ltl_bdd(dict_, res);
		res_ &= res;
	      }

	      //std::cerr << "Pre-Concat:" << std::endl;
	      //trace_ltl_bdd(dict_, res_);

	      if (to_concat_)
		{
		  // If we have translated (a* & b*) in (a* & b*);c, we
		  // have to append ";c" to all destinations.

		  minato_isop isop(res_);
		  bdd cube;
		  res_ = bddfalse;
		  while ((cube = isop.next()) != bddfalse)
		    {
		      bdd label = bdd_exist(cube, dict_.next_set);
		      bdd dest_bdd = bdd_existcomp(cube, dict_.next_set);
		      formula* dest = dict_.conj_bdd_to_formula(dest_bdd);
		      formula* dest2;
		      int x;
		      if (dest == constant::empty_word_instance())
			{
			  res_ |= label & next_to_concat();
			}
		      else
			{
			  dest2 = multop::instance(multop::Concat, dest,
						   to_concat_->clone());
			  if (dest2 != constant::false_instance())
			    {
			      x = dict_.register_next_variable(dest2);
			      dest2->destroy();
			      res_ |= label & bdd_ithvar(x);
			    }
			  if (constant_term_as_bool(node))
			    res_ |= label & next_to_concat();
			}
		    }
		}

	      break;
	    }
	  case multop::Or:
	    {
	      res_ = bddfalse;
	      unsigned s = node->size();
	      if (to_concat_)
		for (unsigned n = 0; n < s; ++n)
		  res_ |= recurse(node->nth(n), to_concat_->clone());
	      else
		for (unsigned n = 0; n < s; ++n)
		  res_ |= recurse(node->nth(n));
	      break;
	    }
	  case multop::Concat:
	    {
	      multop::vec* v = new multop::vec;
	      unsigned s = node->size();
	      v->reserve(s);
	      for (unsigned n = 1; n < s; ++n)
		v->push_back(node->nth(n)->clone());
	      if (to_concat_)
		v->push_back(to_concat_->clone());
	      res_ = recurse(node->nth(0),
			     multop::instance(multop::Concat, v));
	      break;
	    }
	  }
      }

      bdd
      recurse(const formula* f, formula* to_concat = 0)
      {
	ratexp_trad_visitor v(dict_, to_concat);
	f->accept(v);
	return v.result();
      }


    private:
      translate_dict& dict_;
      bdd res_;
      formula* to_concat_;
    };

507

508
    // The rewrite rules used here are adapted from Jean-Michel
509
    // Couvreur's FM paper, augmented to support rational operators.
510
511
512
    class ltl_trad_visitor: public const_visitor
    {
    public:
513
514
      ltl_trad_visitor(translate_dict& dict, bool mark_all = false)
	: dict_(dict), rat_seen_(false), has_marked_(false), mark_all_(mark_all)
515
516
517
518
519
520
521
522
      {
      }

      virtual
      ~ltl_trad_visitor()
      {
      }

523
524
525
526
527
528
529
530
      void
      reset(bool mark_all)
      {
	rat_seen_ = false;
	has_marked_ = false;
	mark_all_ = mark_all;
      }

531
532
      bdd
      result() const
533
534
535
536
      {
	return res_;
      }

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
      const translate_dict&
      get_dict() const
      {
	return dict_;
      }

      bool
      has_rational() const
      {
	return rat_seen_;
      }

      bool
      has_marked() const
      {
	return has_marked_;
      }

555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
      void
      visit(const atomic_prop* node)
      {
	res_ = bdd_ithvar(dict_.register_proposition(node));
      }

      void
      visit(const constant* node)
      {
	switch (node->val())
	  {
	  case constant::True:
	    res_ = bddtrue;
	    return;
	  case constant::False:
	    res_ = bddfalse;
	    return;
572
	  case constant::EmptyWord:
573
574
	    assert(!"Not an LTL operator");
	    return;
575
576
577
578
579
580
581
582
583
584
585
586
587
	  }
	/* Unreachable code.  */
	assert(0);
      }

      void
      visit(const unop* node)
      {
	switch (node->op())
	  {
	  case unop::F:
	    {
	      // r(Fy) = r(y) + a(y)r(XFy)
588
589
590
	      const formula* child = node->child();
	      bdd y = recurse(child);
	      int a = dict_.register_a_variable(child);
591
592
	      int x = dict_.register_next_variable(node);
	      res_ = y | (bdd_ithvar(a) & bdd_ithvar(x));
593
	      break;
594
595
596
	    }
	  case unop::G:
	    {
597
598
599
600
601
602
603
604
605
606
607
608
	      // The paper suggests that we optimize GFy
	      // as
	      //   r(GFy) = (r(y) + a(y))r(XGFy)
	      // instead of
	      //   r(GFy) = (r(y) + a(y)r(XFy)).r(XGFy)
	      // but this is just a particular case
	      // of the "merge all states with the same
	      // symbolic rewriting" optimization we do later.
	      // (r(Fy).r(GFy) and r(GFy) have the same symbolic
	      // rewriting.)  Let's keep things simple here.

	      // r(Gy) = r(y)r(XGy)
609
	      const formula* child = node->child();
610
	      int x = dict_.register_next_variable(node);
611
612
	      bdd y = recurse(child);
	      res_ = y & bdd_ithvar(x);
613
	      break;
614
615
616
	    }
	  case unop::Not:
	    {
617
	      // r(!y) = !r(y)
618
	      res_ = bdd_not(recurse(node->child()));
619
	      break;
620
621
622
	    }
	  case unop::X:
	    {
623
	      // r(Xy) = Next[y]
624
625
	      int x = dict_.register_next_variable(node->child());
	      res_ = bdd_ithvar(x);
626
	      break;
627
	    }
628
629
	  case unop::Finish:
	    assert(!"unsupported operator");
630
631
632
633
	    break;
	  case unop::Star:
	    assert(!"Not an LTL operator");
	    break;
634
635
636
637
638
639
	  }
      }

      void
      visit(const binop* node)
      {
640
	binop::type op = node->op();
641

642
	switch (op)
643
	  {
644
	    // r(f1 logical-op f2) = r(f1) logical-op r(f2)
645
	  case binop::Xor:
646
647
648
649
650
651
	    {
	      bdd f1 = recurse(node->first());
	      bdd f2 = recurse(node->second());
	      res_ = bdd_apply(f1, f2, bddop_xor);
	      return;
	    }
652
	  case binop::Implies:
653
654
655
656
657
658
	    {
	      bdd f1 = recurse(node->first());
	      bdd f2 = recurse(node->second());
	      res_ = bdd_apply(f1, f2, bddop_imp);
	      return;
	    }
659
	  case binop::Equiv:
660
661
662
663
664
665
	    {
	      bdd f1 = recurse(node->first());
	      bdd f2 = recurse(node->second());
	      res_ = bdd_apply(f1, f2, bddop_biimp);
	      return;
	    }
666
667
	  case binop::U:
	    {
668
669
	      bdd f1 = recurse(node->first());
	      bdd f2 = recurse(node->second());
670
671
672
673
	      // r(f1 U f2) = r(f2) + a(f2)r(f1)r(X(f1 U f2))
	      int a = dict_.register_a_variable(node->second());
	      int x = dict_.register_next_variable(node);
	      res_ = f2 | (bdd_ithvar(a) & f1 & bdd_ithvar(x));
674
	      break;
675
	    }
676
677
	  case binop::W:
	    {
678
679
	      bdd f1 = recurse(node->first());
	      bdd f2 = recurse(node->second());
680
681
682
	      // r(f1 W f2) = r(f2) + r(f1)r(X(f1 U f2))
	      int x = dict_.register_next_variable(node);
	      res_ = f2 | (f1 & bdd_ithvar(x));
683
	      break;
684
	    }
685
686
	  case binop::R:
	    {
687
688
	      bdd f1 = recurse(node->first());
	      bdd f2 = recurse(node->second());
689
690
691
	      // r(f1 R f2) = r(f1)r(f2) + r(f2)r(X(f1 U f2))
	      int x = dict_.register_next_variable(node);
	      res_ = (f1 & f2) | (f2 & bdd_ithvar(x));
692
	      break;
693
	    }
694
695
	  case binop::M:
	    {
696
697
	      bdd f1 = recurse(node->first());
	      bdd f2 = recurse(node->second());
698
699
700
701
	      // r(f1 M f2) = r(f1)r(f2) + a(f1)r(f2)r(X(f1 M f2))
	      int a = dict_.register_a_variable(node->first());
	      int x = dict_.register_next_variable(node);
	      res_ = (f1 & f2) | (bdd_ithvar(a) & f2 & bdd_ithvar(x));
702
	      break;
703
	    }
704
705
706
	  case binop::EConcatMarked:
	    has_marked_ = true;
	    /* fall through */
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
707
	  case binop::EConcat:
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
	    rat_seen_ = true;
	    {
	      bdd f2 = recurse(node->second());
	      ratexp_trad_visitor v(dict_);
	      node->first()->accept(v);
	      bdd f1 = v.result();

	      if (mark_all_)
		{
		  op = binop::EConcatMarked;
		  has_marked_ = true;
		}

	      // Recognize f2 on transitions going to destinations
	      // that accept the empty word.
	      minato_isop isop(f1);
	      bdd cube;
	      res_ = bddfalse;
	      while ((cube = isop.next()) != bddfalse)
		{
		  bdd label = bdd_exist(cube, dict_.next_set);
		  bdd dest_bdd = bdd_existcomp(cube, dict_.next_set);
		  formula* dest = dict_.conj_bdd_to_formula(dest_bdd);
		  formula* dest2;
		  int x;
		  if (dest == constant::empty_word_instance())
		    {
		      res_ |= label & f2;
		    }
		  else
		    {
		      dest2 = binop::instance(op, dest,
					      node->second()->clone());
		      if (dest2 != constant::false_instance())
			{
			  x = dict_.register_next_variable(dest2);
			  dest2->destroy();
			  res_ |= label & bdd_ithvar(x);
			}
		      if (constant_term_as_bool(dest))
			res_ |= label & f2;
		    }
		}
	    }
	    break;

	  case binop::UConcat:
	    {
	      bdd f2 = recurse(node->second());
	      ratexp_trad_visitor v(dict_);
	      node->first()->accept(v);
	      bdd f1 = v.result();

	      // Transitions going to destinations accepting the empty
	      // word should recognize f2, and the automaton should be
	      // understood as universal.
	      minato_isop isop(f1);
	      bdd cube;
	      res_ = bddtrue;
	      while ((cube = isop.next()) != bddfalse)
		{
		  bdd label = bdd_exist(cube, dict_.next_set);
		  bdd dest_bdd = bdd_existcomp(cube, dict_.next_set);
		  formula* dest = dict_.conj_bdd_to_formula(dest_bdd);
		  formula* dest2;
		  bdd udest;

		  dest2 = binop::instance(op, dest,
					  node->second()->clone());
		  udest = bdd_ithvar(dict_.register_next_variable(dest2));

		  if (constant_term_as_bool(dest))
		    udest &= f2;

		  dest2->destroy();
		  label = bdd_apply(label, udest, bddop_imp);

		  res_ &= label;
		}
	    }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
788
	    break;
789
790
791
	  }
      }

792
793
794
795
796
797
      void
      visit(const automatop*)
      {
	assert(!"unsupported operator");
      }

798
799
800
801
802
803
      void
      visit(const multop* node)
      {
	switch (node->op())
	  {
	  case multop::And:
804
805
806
807
808
809
810
811
812
813
814
815
	    {
	      res_ = bddtrue;
	      unsigned s = node->size();
	      for (unsigned n = 0; n < s; ++n)
		{
		  bdd res = recurse(node->nth(n));
		  //std::cerr << "=== in And" << std::endl;
		  //trace_ltl_bdd(dict_, res);
		  res_ &= res;
		}
	      break;
	    }
816
	  case multop::Or:
817
818
819
820
821
822
823
	    {
	      res_ = bddfalse;
	      unsigned s = node->size();
	      for (unsigned n = 0; n < s; ++n)
		res_ |= recurse(node->nth(n));
	      break;
	    }
824
	  case multop::Concat:
825
826
	    assert(!"Not an LTL operator");
	    break;
827
	  }
828

829
830
831
832
833
      }

      bdd
      recurse(const formula* f)
      {
834
	ltl_trad_visitor v(dict_, mark_all_);
835
	f->accept(v);
836
837
	rat_seen_ |= v.has_rational();
	has_marked_ |= v.has_marked();
838
839
840
841
842
843
844
	return v.result();
      }


    private:
      translate_dict& dict_;
      bdd res_;
845
846
847
      bool rat_seen_;
      bool has_marked_;
      bool mark_all_;
848
849
    };

850

851
852
    // Check whether a formula has a R, W, or G operator at its
    // top-level (preceding logical operators do not count).
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
    class ltl_possible_fair_loop_visitor: public const_visitor
    {
    public:
      ltl_possible_fair_loop_visitor()
	: res_(false)
      {
      }

      virtual
      ~ltl_possible_fair_loop_visitor()
      {
      }

      bool
      result() const
      {
	return res_;
      }

      void
      visit(const atomic_prop*)
      {
      }

      void
      visit(const constant*)
      {
      }

      void
      visit(const unop* node)
      {
	if (node->op() == unop::G)
	  res_ = true;
      }

      void
      visit(const binop* node)
      {
	switch (node->op())
	  {
	    // r(f1 logical-op f2) = r(f1) logical-op r(f2)
	  case binop::Xor:
	  case binop::Implies:
	  case binop::Equiv:
	    node->first()->accept(*this);
	    if (!res_)
	      node->second()->accept(*this);
	    return;
	  case binop::U:
903
	  case binop::M:
904
905
	    return;
	  case binop::R:
906
	  case binop::W:
907
908
	    res_ = true;
	    return;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
909
910
	  case binop::UConcat:
	  case binop::EConcat:
911
	  case binop::EConcatMarked:
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
912
	    node->second()->accept(*this);
913
	    // FIXME: we might need to add Acc[1]
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
914
	    return;
915
916
917
918
919
	  }
	/* Unreachable code.  */
	assert(0);
      }

920
921
922
923
924
925
      void
      visit(const automatop*)
      {
	assert(!"unsupported operator");
      }

926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
      void
      visit(const multop* node)
      {
	unsigned s = node->size();
	for (unsigned n = 0; n < s && !res_; ++n)
	  {
	    node->nth(n)->accept(*this);
	  }
      }

    private:
      bool res_;
    };

    // Check whether a formula can be part of a fair loop.
    // Cache the result for efficiency.
    class possible_fair_loop_checker
    {
    public:
      bool
      check(const formula* f)
      {
948
949
	pfl_map::const_iterator i = pfl_.find(f);
	if (i != pfl_.end())
950
951
952
953
	  return i->second;
	ltl_possible_fair_loop_visitor v;
	f->accept(v);
	bool rel = v.result();
954
	pfl_[f] = rel;
955
956
957
958
	return rel;
      }

    private:
959
      typedef Sgi::hash_map<const formula*, bool, formula_ptr_hash> pfl_map;
960
      pfl_map pfl_;
961
962
    };

963
964
965
    class formula_canonizer
    {
    public:
966
      formula_canonizer(translate_dict& d,
967
			bool fair_loop_approx, bdd all_promises)
968
969
	: v_(d),
	  fair_loop_approx_(fair_loop_approx),
970
971
	  all_promises_(all_promises),
	  d_(d)
972
973
974
975
976
      {
	// For cosmetics, register 1 initially, so the algorithm will
	// not register an equivalent formula first.
	b2f_[bddtrue] = constant::true_instance();
      }
977

978
979
      ~formula_canonizer()
      {
980
	while (!f2b_.empty())
981
	  {
982
983
984
	    formula_to_bdd_map::iterator i = f2b_.begin();
	    const formula* f = i->first;
	    f2b_.erase(i);
985
	    f->destroy();
986
	  }
987
988
      }

989
990
991
992
993
994
995
996
      struct translated
      {
	bdd symbolic;
	bool has_rational:1;
	bool has_marked:1;
      };

      const translated&
997
      translate(const formula* f, bool* new_flag = 0)
998
999
1000
1001
1002
1003
      {
	// Use the cached result if available.
	formula_to_bdd_map::const_iterator i = f2b_.find(f);
	if (i != f2b_.end())
	  return i->second;

1004
1005
1006
	if (new_flag)
	  *new_flag = true;

1007
	// Perform the actual translation.
1008
	v_.reset(!has_mark(f));
1009
	f->accept(v_);
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
	translated t;
	t.symbolic = v_.result();
	t.has_rational = v_.has_rational();
	t.has_marked = v_.has_marked();

//	std::cerr << "-----" << std::endl;
//	std::cerr << "Formula: " << to_string(f) << std::endl;
//	std::cerr << "Rational: " << t.has_rational << std::endl;
//	std::cerr << "Marked: " << t.has_marked << std::endl;
//	std::cerr << "Mark all: " << !has_mark(f) << std::endl;
//	std::cerr << "Transitions:" << std::endl;
//	trace_ltl_bdd(v_.get_dict(), t.symbolic);

	if (t.has_rational)
	  {
	    bdd res = bddfalse;

	    minato_isop isop(t.symbolic);
	    bdd cube;
	    while ((cube = isop.next()) != bddfalse)
	      {
		bdd label = bdd_exist(cube, d_.next_set);
		bdd dest_bdd = bdd_existcomp(cube, d_.next_set);
		formula* dest =
		  d_.conj_bdd_to_formula(dest_bdd);

		// Handle a Miyano-Hayashi style unrolling for
		// rational operators.  Marked nodes correspond to
		// subformulae in the Miyano-Hayashi set.
		if (simplify_mark(dest))
		  {
		    // Make the promise that we will exit marked sets.
		    int a =
		      d_.register_a_variable(constant::true_instance());
		    label &= bdd_ithvar(a);
		  }
		else
		  {
		    // We have left marked operators, but still
		    // have other rational operator to check.
		    // Start a new marked cycle.
		    formula* dest2 = mark_concat_ops(dest);
		    dest->destroy();
		    dest = dest2;
		  }
		// Note that simplify_mark may have changed dest.
		dest_bdd = bdd_ithvar(d_.register_next_variable(dest));
		dest->destroy();
		res |= label & dest_bdd;
	      }
	    t.symbolic = res;
//	    std::cerr << "Marking rewriting:" << std::endl;
//	    trace_ltl_bdd(v_.get_dict(), t.symbolic);
	  }
1064
1065
1066
1067
1068
1069
1070
1071
1072

	// Apply the fair-loop approximation if requested.
	if (fair_loop_approx_)
	  {
	    // If the source cannot possibly be part of a fair
	    // loop, make all possible promises.
	    if (fair_loop_approx_
		&& f != constant::true_instance()
		&& !pflc_.check(f))
1073
	      t.symbolic &= all_promises_;
1074
1075
	  }

1076
	// Register the reverse mapping if it is not already done.
1077
1078
1079
1080
	if (b2f_.find(t.symbolic) == b2f_.end())
	  b2f_[t.symbolic] = f;

	return f2b_[f->clone()] = t;
1081
1082
1083
1084
1085
      }

      const formula*
      canonize(const formula* f)
      {
1086
	bool new_variable = false;
1087
	bdd b = translate(f, &new_variable).symbolic;
1088
1089

	bdd_to_formula_map::iterator i = b2f_.find(b);
1090
1091
	// Since we have just translated the formula, it is
	// necessarily in b2f_.
1092
1093
1094
	assert(i != b2f_.end());

	if (i->second != f)
1095
	  {
1096
	    // The translated bdd maps to an already seen formula.
1097
	    f->destroy();
1098
	    f = i->second->clone();
1099
	  }
1100
	return f;
1101
1102
      }

1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
    private:
      ltl_trad_visitor v_;
      // Map each formula to its associated bdd.  This speed things up when
      // the same formula is translated several times, which especially
      // occurs when canonize() is called repeatedly inside exprop.
      typedef std::map<bdd, const formula*, bdd_less_than> bdd_to_formula_map;
      bdd_to_formula_map b2f_;
      // Map a representation of successors to a canonical formula.
      // We do this because many formulae (such as `aR(bRc)' and
      // `aR(bRc).(bRc)') are equivalent, and are trivially identified
      // by looking at the set of successors.
1114
      typedef std::map<const formula*, translated> formula_to_bdd_map;
1115
      formula_to_bdd_map f2b_;
1116
1117
1118
1119

      possible_fair_loop_checker pflc_;
      bool fair_loop_approx_;
      bdd all_promises_;
1120
      translate_dict& d_;
1121
1122
1123
1124
1125
    };

  }

  typedef std::map<bdd, bdd, bdd_less_than> prom_map;
1126
  typedef Sgi::hash_map<const formula*, prom_map, formula_ptr_hash> dest_map;
1127
1128
1129
1130

  static void
  fill_dests(translate_dict& d, dest_map& dests, bdd label, const formula* dest)
  {
1131
    bdd conds = bdd_existcomp(label, d.var_set);
1132
1133
    bdd promises = bdd_existcomp(label, d.a_set);

1134
1135
1136
1137
1138
1139
1140
1141
    dest_map::iterator i = dests.find(dest);
    if (i == dests.end())
      {
	dests[dest][promises] = conds;
      }
    else
      {
	i->second[promises] |= conds;
1142
	dest->destroy();
1143
1144
1145
1146
      }
  }


Pierre PARUTTO's avatar
Pierre PARUTTO committed
1147
  tgba_explicit_formula*
1148
  ltl_to_tgba_fm(const formula* f, bdd_dict* dict,
1149
		 bool exprop, bool symb_merge, bool branching_postponement,
1150
		 bool fair_loop_approx, const atomic_prop_set* unobs,
1151
		 int reduce_ltl)
1152
1153
1154
1155
1156
  {
    // Normalize the formula.  We want all the negations on
    // the atomic propositions.  We also suppress logic
    // abbreviations such as <=>, =>, or XOR, since they
    // would involve negations at the BDD level.
1157
1158
    formula* f1 = unabbreviate_logic(f);
    formula* f2 = negative_normal_form(f1);
1159
    f1->destroy();
1160

1161
1162
1163
1164
    // Simplify the formula, if requested.
    if (reduce_ltl)
      {
	formula* tmp = reduce(f2, reduce_ltl);
1165
	f2->destroy();
1166
1167
1168
	f2 = tmp;
      }

1169
1170
    typedef std::set<const formula*, formula_ptr_less_than> set_type;
    set_type formulae_to_translate;
1171

1172
    translate_dict d(dict);
1173

1174
1175
    // Compute the set of all promises that can possibly occurre
    // inside the formula.
1176
    bdd all_promises = bddtrue;
1177
    if (fair_loop_approx || unobs)
1178
1179
1180
1181
1182
1183
      {
	ltl_promise_visitor pv(d);
	f2->accept(pv);
	all_promises = pv.result();
      }

1184
    formula_canonizer fc(d, fair_loop_approx, all_promises);
1185

1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
    // These are used when atomic propositions are interpreted as
    // events.  There are two kinds of events: observable events are
    // those used in the formula, and unobservable events or other
    // events that can occur at anytime.  All events exclude each
    // other.
    bdd observable_events = bddfalse;
    bdd unobservable_events = bddfalse;
    if (unobs)
      {
	bdd neg_events = bddtrue;
1196
1197
	std::auto_ptr<atomic_prop_set> aps(atomic_prop_collect(f));
	for (atomic_prop_set::const_iterator i = aps->begin();
1198
1199
1200
1201
1202
1203
1204
1205
	     i != aps->end(); ++i)
	  {
	    int p = d.register_proposition(*i);
	    bdd pos = bdd_ithvar(p);
	    bdd neg = bdd_nithvar(p);
	    observable_events = (observable_events & neg) | (neg_events & pos);
	    neg_events &= neg;
	  }
1206
	for (atomic_prop_set::const_iterator i = unobs->begin();
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
	     i != unobs->end(); ++i)
	  {
	    int p = d.register_proposition(*i);
	    bdd pos = bdd_ithvar(p);
	    bdd neg = bdd_nithvar(p);
	    unobservable_events = ((unobservable_events & neg)
				   | (neg_events & pos));
	    observable_events &= neg;
	    neg_events &= neg;
	  }
      }
    bdd all_events = observable_events | unobservable_events;

1220

1221
    tgba_explicit_formula* a = new tgba_explicit_formula(dict);
1222

1223
1224
1225
1226
    // This is in case the initial state is equivalent to true...
    if (symb_merge)
      f2 = const_cast<formula*>(fc.canonize(f2));

1227
1228
    formulae_to_translate.insert(f2);
    a->set_init_state(f2);
1229
1230
1231
1232

    while (!formulae_to_translate.empty())
      {
	// Pick one formula.
1233
	const formula* now = *formulae_to_translate.begin();
1234
1235
1236
	formulae_to_translate.erase(formulae_to_translate.begin());

	// Translate it into a BDD to simplify it.
1237
1238
	const formula_canonizer::translated& t = fc.translate(now);
	bdd res = t.symbolic;
1239

1240
1241
1242
1243
	// Handle exclusive events.
	if (unobs)
	  {
	    res &= observable_events;
1244
	    int n = d.register_next_variable(now);
1245
1246
1247
	    res |= unobservable_events & bdd_ithvar(n) & all_promises;
	  }

1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
	// We used to factor only Next and A variables while computing
	// prime implicants, with
	//    minato_isop isop(res, d.next_set & d.a_set);
	// in order to obtain transitions with formulae of atomic
	// proposition directly, but unfortunately this led to strange
	// factorizations.  For instance f U g was translated as
	//     r(f U g) = g + a(g).r(X(f U g)).(f + g)
	// instead of just
	//     r(f U g) = g + a(g).r(X(f U g)).f
	// Of course both formulae are logically equivalent, but the
	// latter is "more deterministic" than the former, so it should
	// be preferred.
	//
	// Therefore we now factor all variables.  This may lead to more
	// transitions than necessary (e.g.,  r(f + g) = f + g  will be
1263
1264
1265
	// coded as two transitions), but we later merge all transitions
	// with same source/destination and acceptance conditions.  This
	// is the goal of the `dests' hash.
1266
	//
1267
	// Note that this is still not optimal.  For instance it is
1268
	// better to encode `f U g' as
1269
	//     r(f U g) = g + a(g).r(X(f U g)).f.!g
1270
1271
1272
1273
	// because that leads to a deterministic automaton.  In order
	// to handle this, we take the conditions of any transition
	// going to true (it's `g' here), and remove it from the other
	// transitions.
1274
1275
1276
	//
	// In `exprop' mode, considering all possible combinations of
	// outgoing propositions generalizes the above trick.
1277
	dest_map dests;
1278

1279
	// Compute all outgoing arcs.
1280
1281
1282
1283
1284

	// If EXPROP is set, we will refine the symbolic
	// representation of the successors for all combinations of
	// the atomic properties involved in the formula.
	// VAR_SET is the set of these properties.
1285
	bdd var_set = bdd_existcomp(bdd_support(res), d.var_set);
1286
1287
1288
1289
1290
	// ALL_PROPS is the combinations we have yet to consider.
	// We used to start with `all_props = bddtrue', but it is
	// more efficient to start with the set of all satisfiable
	// variables combinations.
	bdd all_props = bdd_existcomp(res, d.var_set);
1291
	while (all_props != bddfalse)
1292
	  {
1293
1294
1295
	    bdd one_prop_set = bddtrue;
	    if (exprop)
	      one_prop_set = bdd_satoneset(all_props, var_set, bddtrue);
1296
	    all_props -= one_prop_set;
1297

1298
	    typedef std::map<bdd, const formula*, bdd_less_than> succ_map;
1299
1300
	    succ_map succs;

1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
	    // Compute prime implicants.
	    // The reason we use prime implicants and not bdd_satone()
	    // is that we do not want to get any negation in front of Next
	    // or Acc variables.  We wouldn't know what to do with these.
	    // We never added negations in front of these variables when
	    // we built the BDD, so prime implicants will not "invent" them.
	    //
	    // FIXME: minato_isop is quite expensive, and I (=adl)
	    // don't think we really care that much about getting the
	    // smalled sum of products that minato_isop strives to
	    // compute.  Given that Next and Acc variables should
	    // always be positive, maybe there is a faster way to
	    // compute the successors?  E.g. using bdd_satone() and
	    // ignoring negated Next and Acc variables.
1315
1316
1317
	    minato_isop isop(res & one_prop_set);
	    bdd cube;
	    while ((cube = isop.next()) != bddfalse)
1318
	      {
1319
		bdd label = bdd_exist(cube, d.next_set);
1320
		bdd dest_bdd = bdd_existcomp(cube, d.next_set);
1321
1322
		const formula* dest = d.conj_bdd_to_formula(dest_bdd);

1323
1324
1325
1326
		// Simplify the formula, if requested.
		if (reduce_ltl)
		  {
		    formula* tmp = reduce(dest, reduce_ltl);
1327
		    dest->destroy();
1328
1329
1330
1331
1332
1333
		    dest = tmp;
		    // Ignore the arc if the destination reduces to false.
		    if (dest == constant::false_instance())
		      continue;
		  }

1334
1335
1336
1337
		// If we already know a state with the same
		// successors, use it in lieu of the current one.
		if (symb_merge)
		  dest = fc.canonize(dest);
1338

1339
1340
1341
1342
1343
1344
		// If we are not postponing the branching, we can
		// declare the outgoing transitions immediately.
		// Otherwise, we merge transitions with identical
		// label, and declare the outgoing transitions in a
		// second loop.
		if (!branching_postponement)
1345
		  {
1346
		    fill_dests(d, dests, label, dest);
1347
1348
1349
		  }
		else
		  {
1350
1351
1352
1353
		    succ_map::iterator si = succs.find(label);
		    if (si == succs.end())
		      succs[label] = dest;
		    else
1354
1355
1356
1357
		      si->second =
			multop::instance(multop::Or,
					 const_cast<formula*>(si->second),
					 const_cast<formula*>(dest));
1358
1359
		  }
	      }
1360
1361
1362
	    if (branching_postponement)
	      for (succ_map::const_iterator si = succs.begin();
		   si != succs.end(); ++si)
1363
		fill_dests(d, dests, si->first, si->second);
1364
	  }
1365

1366
	// Check for an arc going to 1 (True).  Register it first, that
1367
	// way it will be explored before others during model checking.
1368
	dest_map::const_iterator i = dests.find(constant::true_instance());
1369
	// COND_FOR_TRUE is the conditions of the True arc, so we
1370
1371
1372
	// can remove them from all other arcs.  It might sounds that
	// this is not needed when exprop is used, but in fact it is
	// complementary.
1373
1374
	//
	// Consider
1375
	//   f = r(X(1) R p) = p.(1 + r(X(1) R p))
1376
	// with exprop the two outgoing arcs would be
1377
1378
        //         p               p
	//     f ----> 1       f ----> f
1379
1380
	//
	// where in fact we could output
1381
1382
        //         p
	//     f ----> 1
1383
	//
1384
	// because there is no point in looping on f if we can go to 1.
1385
	bdd cond_for_true = bddfalse;
1386
1387
	if (i != dests.end())
	  {
1388
	    // When translating LTL for an event-based logic with
1389
1390
	    // unobservable events, the 1 state should accept all events,
	    // even unobservable events.
1391
	    if (unobs && now == constant::true_instance())
1392
	      cond_for_true = all_events;
1393
1394
1395
1396
1397
	    else
	      {
		// There should be only one transition going to 1 (true) ...
		assert(i->second.size() == 1);
		prom_map::const_iterator j = i->second.begin();
1398
1399
1400
		// ... and it is not expected to make any promises (unless
		// fair loop approximations are used).
		assert(fair_loop_approx || j->first == bddtrue);
1401
1402
		cond_for_true = j->second;
	      }
1403
1404
	    if (!a->has_state(constant::true_instance()))
	      formulae_to_translate.insert(constant::true_instance());
Pierre PARUTTO's avatar
Pierre PARUTTO committed
1405
	    state_explicit_formula::transition* t =
1406
	      a->create_transition(now, constant::true_instance());
1407
	    a->add_condition(t, d.bdd_to_formula(cond_for_true));
1408
1409
1410
1411
1412
	  }
	// Register other transitions.
	for (i = dests.begin(); i != dests.end(); ++i)
	  {
	    const formula* dest = i->first;
1413
1414
1415
1416
	    // The cond_for_true optimization can cause some
	    // transitions to be removed.  So we have to remember
	    // whether a formula is actually reachable.
	    bool reachable = false;
1417

1418
1419
1420
	    // Will this be a new state?
	    bool seen = a->has_state(dest);

1421
1422
1423
1424
1425
	    if (dest != constant::true_instance())
	      {
		for (prom_map::const_iterator j = i->second.begin();
		     j != i->second.end(); ++j)
		  {