ltl2tgba_fm.cc 25.8 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
#include <cassert>
36
#include <memory>
37
#include "ltl2tgba_fm.hh"
38
#include "ltlvisit/contain.hh"
39
40
41
42
43
44
45
46

namespace spot
{
  using namespace ltl;

  namespace
  {

47
48
    // Helper dictionary.  We represent formulae using BDDs to
    // simplify them, and then translate BDDs back into formulae.
49
50
51
52
53
    //
    // 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.
54
    class translate_dict
55
56
57
    {
    public:

58
59
      translate_dict(bdd_dict* dict)
	: dict(dict),
60
61
62
63
64
65
66
67
68
69
	  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)
70
	  i->first->destroy();
71
	dict->unregister_all_my_variables(this);
72
73
      }

74
75
      bdd_dict* dict;

76
77
      typedef bdd_dict::fv_map fv_map;
      typedef bdd_dict::vf_map vf_map;
78
79
80
81
82
83
84
85
86

      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
87
      register_proposition(const formula* f)
88
      {
89
	int num = dict->register_proposition(f, this);
90
91
92
93
94
	var_set &= bdd_ithvar(num);
	return num;
      }

      int
95
      register_a_variable(const formula* f)
96
      {
97
	int num = dict->register_acceptance_variable(f, this);
98
99
100
101
102
	a_set &= bdd_ithvar(num);
	return num;
      }

      int
103
      register_next_variable(const formula* f)
104
105
106
107
108
109
110
111
112
113
      {
	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
	  {
114
	    f = f->clone();
115
	    num = dict->register_anonymous_variables(1, this);
116
117
118
119
120
121
122
	    next_map[f] = num;
	    next_formula_map[num] = f;
	  }
	next_set &= bdd_ithvar(num);
	return num;
      }

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
      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;
      }

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

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

183
184
      const formula*
      bdd_to_formula(bdd f)
185
      {
186
	if (f == bddfalse)
187
	  return constant::false_instance();
188

189
190
191
192
193
194
195
196
197
	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);
      }
198
199

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

217
		if (!a->has_acceptance_condition(ac))
218
		  a->declare_acceptance_condition(ac->clone());
219
		a->add_acceptance_condition(t, ac);
220
221
222
223
224
225
226
227
		b = high;
	      }
	    assert(b != bddfalse);
	  }
      }
    };


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

    // 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_;
    };


272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    // The rewrite rules used here are adapted from Jean-Michel
    // Couvreur's FM paper.
    class ltl_trad_visitor: public const_visitor
    {
    public:
      ltl_trad_visitor(translate_dict& dict)
	: dict_(dict)
      {
      }

      virtual
      ~ltl_trad_visitor()
      {
      }

287
288
      bdd
      result() const
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
      {
	return res_;
      }

      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;
310
311
	  case constant::EmptyWord:
	    assert(!"unsupported operator");
312
313
314
315
316
317
318
319
320
321
322
323
324
	  }
	/* Unreachable code.  */
	assert(0);
      }

      void
      visit(const unop* node)
      {
	switch (node->op())
	  {
	  case unop::F:
	    {
	      // r(Fy) = r(y) + a(y)r(XFy)
325
326
327
	      const formula* child = node->child();
	      bdd y = recurse(child);
	      int a = dict_.register_a_variable(child);
328
329
330
331
332
333
	      int x = dict_.register_next_variable(node);
	      res_ = y | (bdd_ithvar(a) & bdd_ithvar(x));
	      return;
	    }
	  case unop::G:
	    {
334
335
336
337
338
339
340
341
342
343
344
345
	      // 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)
346
	      const formula* child = node->child();
347
	      int x = dict_.register_next_variable(node);
348
349
	      bdd y = recurse(child);
	      res_ = y & bdd_ithvar(x);
350
351
352
353
	      return;
	    }
	  case unop::Not:
	    {
354
	      // r(!y) = !r(y)
355
356
357
358
359
	      res_ = bdd_not(recurse(node->child()));
	      return;
	    }
	  case unop::X:
	    {
360
	      // r(Xy) = Next[y]
361
362
363
364
	      int x = dict_.register_next_variable(node->child());
	      res_ = bdd_ithvar(x);
	      return;
	    }
365
	  case unop::Finish:
366
	  case unop::Star:
367
	    assert(!"unsupported operator");
368
369
370
371
372
373
374
375
376
377
378
379
380
	  }
	/* Unreachable code.  */
	assert(0);
      }

      void
      visit(const binop* node)
      {
	bdd f1 = recurse(node->first());
	bdd f2 = recurse(node->second());

	switch (node->op())
	  {
381
	    // r(f1 logical-op f2) = r(f1) logical-op r(f2)
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
	  case binop::Xor:
	    res_ = bdd_apply(f1, f2, bddop_xor);
	    return;
	  case binop::Implies:
	    res_ = bdd_apply(f1, f2, bddop_imp);
	    return;
	  case binop::Equiv:
	    res_ = bdd_apply(f1, f2, bddop_biimp);
	    return;
	  case binop::U:
	    {
	      // 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));
	      return;
	    }
399
400
401
402
403
404
405
	  case binop::W:
	    {
	      // 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));
	      return;
	    }
406
407
408
409
410
411
412
	  case binop::R:
	    {
	      // 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));
	      return;
	    }
413
414
415
416
417
418
419
420
	  case binop::M:
	    {
	      // 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));
	      return;
	    }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
421
422
423
424
	  case binop::UConcat:
	  case binop::EConcat:
	    assert(!"unsupported operator");
	    break;
425
426
427
428
429
	  }
	/* Unreachable code.  */
	assert(0);
      }

430
431
432
433
434
435
      void
      visit(const automatop*)
      {
	assert(!"unsupported operator");
      }

436
437
438
439
440
441
442
443
444
445
446
447
448
449
      void
      visit(const multop* node)
      {
	int op = -1;
	switch (node->op())
	  {
	  case multop::And:
	    op = bddop_and;
	    res_ = bddtrue;
	    break;
	  case multop::Or:
	    op = bddop_or;
	    res_ = bddfalse;
	    break;
450
451
	  case multop::Concat:
	    assert(!"unsupported operator");
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
	  }
	assert(op != -1);
	unsigned s = node->size();
	for (unsigned n = 0; n < s; ++n)
	  {
	    res_ = bdd_apply(res_, recurse(node->nth(n)), op);
	  }
      }

      bdd
      recurse(const formula* f)
      {
	ltl_trad_visitor v(dict_);
	f->accept(v);
	return v.result();
      }


    private:
      translate_dict& dict_;
      bdd res_;
    };

475

476
477
    // Check whether a formula has a R, W, or G operator at its
    // top-level (preceding logical operators do not count).
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
    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:
528
	  case binop::M:
529
530
	    return;
	  case binop::R:
531
	  case binop::W:
532
533
	    res_ = true;
	    return;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
534
535
536
537
	  case binop::UConcat:
	  case binop::EConcat:
	    node->second()->accept(*this);
	    return;
538
539
540
541
542
	  }
	/* Unreachable code.  */
	assert(0);
      }

543
544
545
546
547
548
      void
      visit(const automatop*)
      {
	assert(!"unsupported operator");
      }

549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
      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)
      {
571
572
	pfl_map::const_iterator i = pfl_.find(f);
	if (i != pfl_.end())
573
574
575
576
	  return i->second;
	ltl_possible_fair_loop_visitor v;
	f->accept(v);
	bool rel = v.result();
577
	pfl_[f] = rel;
578
579
580
581
	return rel;
      }

    private:
582
      typedef Sgi::hash_map<const formula*, bool, formula_ptr_hash> pfl_map;
583
      pfl_map pfl_;
584
585
    };

586
587
588
    class formula_canonizer
    {
    public:
589
      formula_canonizer(translate_dict& d,
590
			bool fair_loop_approx, bdd all_promises)
591
592
	: v_(d),
	  fair_loop_approx_(fair_loop_approx),
593
	  all_promises_(all_promises)
594
595
596
597
598
      {
	// For cosmetics, register 1 initially, so the algorithm will
	// not register an equivalent formula first.
	b2f_[bddtrue] = constant::true_instance();
      }
599

600
601
      ~formula_canonizer()
      {
602
	while (!f2b_.empty())
603
	  {
604
605
606
	    formula_to_bdd_map::iterator i = f2b_.begin();
	    const formula* f = i->first;
	    f2b_.erase(i);
607
	    f->destroy();
608
	  }
609
610
611
      }

      bdd
612
      translate(const formula* f, bool* new_flag = 0)
613
614
615
616
617
618
      {
	// Use the cached result if available.
	formula_to_bdd_map::const_iterator i = f2b_.find(f);
	if (i != f2b_.end())
	  return i->second;

619
620
621
	if (new_flag)
	  *new_flag = true;

622
623
624
	// Perform the actual translation.
	f->accept(v_);
	bdd res = v_.result();
625
626
627
628
629
630
631
632
633
634
635
636

	// 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))
	      res &= all_promises_;
	  }

637
	f2b_[f->clone()] = res;
638
639
640
641
642
643
644
645
646
647

	// Register the reverse mapping if it is not already done.
	if (b2f_.find(res) == b2f_.end())
	  b2f_[res] = f;
	return res;
      }

      const formula*
      canonize(const formula* f)
      {
648
649
	bool new_variable = false;
	bdd b = translate(f, &new_variable);
650
651

	bdd_to_formula_map::iterator i = b2f_.find(b);
652
653
	// Since we have just translated the formula, it is
	// necessarily in b2f_.
654
655
656
	assert(i != b2f_.end());

	if (i->second != f)
657
	  {
658
	    // The translated bdd maps to an already seen formula.
659
	    f->destroy();
660
	    f = i->second->clone();
661
	  }
662
	return f;
663
664
      }

665
666
667
668
669
670
671
672
673
674
675
676
677
    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.
      typedef std::map<const formula*, bdd> formula_to_bdd_map;
      formula_to_bdd_map f2b_;
678
679
680
681

      possible_fair_loop_checker pflc_;
      bool fair_loop_approx_;
      bdd all_promises_;
682
683
684
685
686
    };

  }

  typedef std::map<bdd, bdd, bdd_less_than> prom_map;
687
  typedef Sgi::hash_map<const formula*, prom_map, formula_ptr_hash> dest_map;
688
689
690
691

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

695
696
697
698
699
700
701
702
    dest_map::iterator i = dests.find(dest);
    if (i == dests.end())
      {
	dests[dest][promises] = conds;
      }
    else
      {
	i->second[promises] |= conds;
703
	dest->destroy();
704
705
706
707
      }
  }


Pierre PARUTTO's avatar
Pierre PARUTTO committed
708
  tgba_explicit_formula*
709
  ltl_to_tgba_fm(const formula* f, bdd_dict* dict,
710
		 bool exprop, bool symb_merge, bool branching_postponement,
711
		 bool fair_loop_approx, const atomic_prop_set* unobs,
712
		 int reduce_ltl)
713
714
715
716
717
  {
    // 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.
718
719
    formula* f1 = unabbreviate_logic(f);
    formula* f2 = negative_normal_form(f1);
720
    f1->destroy();
721

722
723
724
725
    // Simplify the formula, if requested.
    if (reduce_ltl)
      {
	formula* tmp = reduce(f2, reduce_ltl);
726
	f2->destroy();
727
728
729
	f2 = tmp;
      }

730
731
    typedef std::set<const formula*, formula_ptr_less_than> set_type;
    set_type formulae_to_translate;
732

733
    translate_dict d(dict);
734

735
736
    // Compute the set of all promises that can possibly occurre
    // inside the formula.
737
    bdd all_promises = bddtrue;
738
    if (fair_loop_approx || unobs)
739
740
741
742
743
744
      {
	ltl_promise_visitor pv(d);
	f2->accept(pv);
	all_promises = pv.result();
      }

745
    formula_canonizer fc(d, fair_loop_approx, all_promises);
746

747
748
749
750
751
752
753
754
755
756
    // 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;
757
758
	std::auto_ptr<atomic_prop_set> aps(atomic_prop_collect(f));
	for (atomic_prop_set::const_iterator i = aps->begin();
759
760
761
762
763
764
765
766
	     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;
	  }
767
	for (atomic_prop_set::const_iterator i = unobs->begin();
768
769
770
771
772
773
774
775
776
777
778
779
780
	     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;

781

782
    tgba_explicit_formula* a = new tgba_explicit_formula(dict);
783

784
785
    formulae_to_translate.insert(f2);
    a->set_init_state(f2);
786
787
788
789

    while (!formulae_to_translate.empty())
      {
	// Pick one formula.
790
	const formula* now = *formulae_to_translate.begin();
791
792
793
	formulae_to_translate.erase(formulae_to_translate.begin());

	// Translate it into a BDD to simplify it.
794
	bdd res = fc.translate(now);
795

796
797
798
799
	// Handle exclusive events.
	if (unobs)
	  {
	    res &= observable_events;
800
	    int n = d.register_next_variable(now);
801
802
803
	    res |= unobservable_events & bdd_ithvar(n) & all_promises;
	  }

804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
	// 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
819
820
821
	// 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.
822
	//
823
	// Note that this is still not optimal.  For instance it is
824
	// better to encode `f U g' as
825
	//     r(f U g) = g + a(g).r(X(f U g)).f.!g
826
827
828
829
	// 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.
830
831
832
	//
	// In `exprop' mode, considering all possible combinations of
	// outgoing propositions generalizes the above trick.
833
	dest_map dests;
834

835
	// Compute all outgoing arcs.
836
837
838
839
840

	// 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.
841
	bdd var_set = bdd_existcomp(bdd_support(res), d.var_set);
842
843
844
845
846
	// 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);
847
	while (all_props != bddfalse)
848
	  {
849
850
851
	    bdd one_prop_set = bddtrue;
	    if (exprop)
	      one_prop_set = bdd_satoneset(all_props, var_set, bddtrue);
852
	    all_props -= one_prop_set;
853

854
	    typedef std::map<bdd, const formula*, bdd_less_than> succ_map;
855
856
	    succ_map succs;

857
858
859
860
861
862
863
864
865
866
867
868
869
870
	    // 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.
871
872
873
	    minato_isop isop(res & one_prop_set);
	    bdd cube;
	    while ((cube = isop.next()) != bddfalse)
874
	      {
875
		bdd label = bdd_exist(cube, d.next_set);
876
		bdd dest_bdd = bdd_existcomp(cube, d.next_set);
877
878
		const formula* dest = d.conj_bdd_to_formula(dest_bdd);

879
880
881
882
		// Simplify the formula, if requested.
		if (reduce_ltl)
		  {
		    formula* tmp = reduce(dest, reduce_ltl);
883
		    dest->destroy();
884
885
886
887
888
889
		    dest = tmp;
		    // Ignore the arc if the destination reduces to false.
		    if (dest == constant::false_instance())
		      continue;
		  }

890
891
892
893
		// 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);
894

895
896
897
898
899
900
		// 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)
901
		  {
902
		    fill_dests(d, dests, label, dest);
903
904
905
		  }
		else
		  {
906
907
908
909
		    succ_map::iterator si = succs.find(label);
		    if (si == succs.end())
		      succs[label] = dest;
		    else
910
911
912
913
		      si->second =
			multop::instance(multop::Or,
					 const_cast<formula*>(si->second),
					 const_cast<formula*>(dest));
914
915
		  }
	      }
916
917
918
	    if (branching_postponement)
	      for (succ_map::const_iterator si = succs.begin();
		   si != succs.end(); ++si)
919
		fill_dests(d, dests, si->first, si->second);
920
	  }
921

922
	// Check for an arc going to 1 (True).  Register it first, that
923
	// way it will be explored before others during model checking.
924
	dest_map::const_iterator i = dests.find(constant::true_instance());
925
	// COND_FOR_TRUE is the conditions of the True arc, so we
926
927
928
	// 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.
929
930
	//
	// Consider
931
	//   f = r(X(1) R p) = p.(1 + r(X(1) R p))
932
	// with exprop the two outgoing arcs would be
933
934
        //         p               p
	//     f ----> 1       f ----> f
935
936
	//
	// where in fact we could output
937
938
        //         p
	//     f ----> 1
939
	//
940
	// because there is no point in looping on f if we can go to 1.
941
	bdd cond_for_true = bddfalse;
942
943
	if (i != dests.end())
	  {
944
	    // When translating LTL for an event-based logic with
945
946
	    // unobservable events, the 1 state should accept all events,
	    // even unobservable events.
947
	    if (unobs && now == constant::true_instance())
948
	      cond_for_true = all_events;
949
950
951
952
953
	    else
	      {
		// There should be only one transition going to 1 (true) ...
		assert(i->second.size() == 1);
		prom_map::const_iterator j = i->second.begin();
954
955
956
		// ... and it is not expected to make any promises (unless
		// fair loop approximations are used).
		assert(fair_loop_approx || j->first == bddtrue);
957
958
		cond_for_true = j->second;
	      }
959
960
	    if (!a->has_state(constant::true_instance()))
	      formulae_to_translate.insert(constant::true_instance());
Pierre PARUTTO's avatar
Pierre PARUTTO committed
961
	    state_explicit_formula::transition* t =
962
	      a->create_transition(now, constant::true_instance());
963
	    a->add_condition(t, d.bdd_to_formula(cond_for_true));
964
965
966
967
968
	  }
	// Register other transitions.
	for (i = dests.begin(); i != dests.end(); ++i)
	  {
	    const formula* dest = i->first;
969
970
971
972
	    // 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;
973

974
975
976
	    // Will this be a new state?
	    bool seen = a->has_state(dest);

977
978
979
980
981
	    if (dest != constant::true_instance())
	      {
		for (prom_map::const_iterator j = i->second.begin();
		     j != i->second.end(); ++j)
		  {
982
983
984
		    bdd cond = j->second - cond_for_true;
		    if (cond == bddfalse) // Skip false transitions.
		      continue;
Pierre PARUTTO's avatar
Pierre PARUTTO committed
985
		    state_explicit_formula::transition* t =
986
		      a->create_transition(now, dest);
987
		    a->add_condition(t, d.bdd_to_formula(cond));
988
		    d.conj_bdd_to_acc(a, j->first, t);
989
		    reachable = true;
990
991
		  }
	      }
992
993
994
995
996
	    else
	      {
		// "1" is reachable.
		reachable = true;
	      }
997
998
	    if (reachable && !seen)
	      formulae_to_translate.insert(dest);
999
	    else
1000
	      dest->destroy();
1001
1002
1003
	  }
      }

1004
1005
    // Turn all promises into real acceptance conditions.
    a->complement_all_acceptance_conditions();
1006
1007
1008
1009
    return a;
  }

}