ltl2tgba_fm.cc 16.6 KB
Newer Older
1
// Copyright (C) 2003, 2004  Laboratoire d'Informatique de Paris 6 (LIP6),
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// dpartement Systmes Rpartis Coopratifs (SRC), Universit Pierre
// et Marie Curie.
//
// 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.

22
#include "misc/hash.hh"
23
#include "misc/bddalloc.hh"
24
#include "misc/bddlt.hh"
25
#include "misc/minato.hh"
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "ltlast/visitor.hh"
#include "ltlast/allnodes.hh"
#include "ltlvisit/lunabbrev.hh"
#include "ltlvisit/nenoform.hh"
#include "ltlvisit/destroy.hh"
#include "ltlvisit/tostring.hh"
#include <cassert>

#include "tgba/tgbabddconcretefactory.hh"
#include "ltl2tgba_fm.hh"

namespace spot
{
  using namespace ltl;

  namespace
  {

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

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

71
72
      bdd_dict* dict;

73
      /// Formula-to-BDD-variable maps.
74
75
      typedef Sgi::hash_map<const formula*, int,
			    ptr_hash<formula> > fv_map;
76
      /// BDD-variable-to-formula maps.
77
      typedef Sgi::hash_map<int, const formula*> 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
114
      {
	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
	  {
	    f = clone(f);
115
	    num = dict->register_anonymous_variables(1, this);
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
	    next_map[f] = num;
	    next_formula_map[num] = f;
	  }
	next_set &= bdd_ithvar(num);
	return num;
      }

      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;
	}
133
134
	os << "Shared Dict:" << std::endl;
	dict->dump(os);
135
136
137
	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 clone(isi->second);
144
145
	isi = dict->acc_formula_map.find(var);
	if (isi != dict->acc_formula_map.end())
146
	  return clone(isi->second);
147
148
	isi = dict->var_formula_map.find(var);
	if (isi != dict->var_formula_map.end())
149
	  return clone(isi->second);
150
151
152
	assert(0);
      }

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

180
181
      const formula*
      bdd_to_formula(bdd f)
182
      {
183
	if (f == bddfalse)
184
	  return constant::false_instance();
185

186
187
188
189
190
191
192
193
194
	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);
      }
195
196
197
198
199
200
201
202
203
204
205

      void
      conj_bdd_to_acc(tgba_explicit* a, bdd b, tgba_explicit::transition* t)
      {
	assert(b != bddfalse);
	while (b != bddtrue)
	  {
	    int var = bdd_var(b);
	    bdd high = bdd_high(b);
	    if (high == bddfalse)
	      {
206
		// Simply ignore negated acceptance variables.
207
208
209
210
		b = bdd_low(b);
	      }
	    else
	      {
211
		formula* ac = var_to_formula(var);
212

213
214
215
		if (! a->has_acceptance_condition(ac))
		  a->declare_acceptance_condition(clone(ac));
		a->add_acceptance_condition(t, ac);
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
272
273
		b = high;
	      }
	    assert(b != bddfalse);
	  }
      }
    };


    // 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()
      {
      }

      bdd result() const
      {
	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;
	  }
	/* Unreachable code.  */
	assert(0);
      }

      void
      visit(const unop* node)
      {
	switch (node->op())
	  {
	  case unop::F:
	    {
	      // r(Fy) = r(y) + a(y)r(XFy)
274
275
276
	      const formula* child = node->child();
	      bdd y = recurse(child);
	      int a = dict_.register_a_variable(child);
277
278
279
280
281
282
	      int x = dict_.register_next_variable(node);
	      res_ = y | (bdd_ithvar(a) & bdd_ithvar(x));
	      return;
	    }
	  case unop::G:
	    {
283
284
285
286
287
288
289
290
291
292
293
294
	      // 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)
295
	      const formula* child = node->child();
296
	      int x = dict_.register_next_variable(node);
297
298
	      bdd y = recurse(child);
	      res_ = y & bdd_ithvar(x);
299
300
301
302
	      return;
	    }
	  case unop::Not:
	    {
303
	      // r(!y) = !r(y)
304
305
306
307
308
	      res_ = bdd_not(recurse(node->child()));
	      return;
	    }
	  case unop::X:
	    {
309
	      // r(Xy) = Next[y]
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
	      int x = dict_.register_next_variable(node->child());
	      res_ = bdd_ithvar(x);
	      return;
	    }
	  }
	/* Unreachable code.  */
	assert(0);
      }

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

	switch (node->op())
	  {
327
	    // r(f1 logical-op f2) = r(f1) logical-op r(f2)
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
	  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;
	    }
	  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;
	    }
	  }
	/* Unreachable code.  */
	assert(0);
      }

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

  }

  tgba_explicit*
397
398
  ltl_to_tgba_fm(const formula* f, bdd_dict* dict,
		 bool exprop, bool symb_merge)
399
400
401
402
403
  {
    // 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.
404
405
406
    formula* f1 = unabbreviate_logic(f);
    formula* f2 = negative_normal_form(f1);
    destroy(f1);
407

408
409
    std::set<const formula*> formulae_seen;
    std::set<const formula*> formulae_to_translate;
410
411
    // Map a representation of successors to a canonical formula.
    // We do this because many formulae (such as `aR(bRc)' and
412
    // `aR(bRc).(bRc)') are equivalent, and are trivially identified
413
414
415
416
    // by looking at the set of successors.
    typedef std::map<bdd, const formula*, bdd_less_than> succ_to_formula;
    succ_to_formula canonical_succ;

417
    translate_dict d(dict);
418
    ltl_trad_visitor v(d);
419
420
421
422
423
424
425
426
427
428
429

    formulae_seen.insert(f2);
    formulae_to_translate.insert(f2);

    tgba_explicit* a = new tgba_explicit(dict);

    a->set_init_state(to_string(f2));

    while (!formulae_to_translate.empty())
      {
	// Pick one formula.
430
	const formula* f = *formulae_to_translate.begin();
431
432
433
	formulae_to_translate.erase(formulae_to_translate.begin());

	// Translate it into a BDD to simplify it.
434
435
436
	// FIXME: Currently the same formula can be converted into a
	// BDD twice.  Once in the symb_merge block below, and then
	// once here.
437
438
	f->accept(v);
	bdd res = v.result();
439
440
441
	succ_to_formula::iterator cs = canonical_succ.find(res);
	if (cs == canonical_succ.end())
	  canonical_succ[res] = clone(f);
442
443
444

	std::string now = to_string(f);

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
	// 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
460
461
462
	// 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.
463
	//
464
	// Note that this is still not optimal.  For instance it is
465
	// better to encode `f U g' as
466
	//     r(f U g) = g + a(g).r(X(f U g)).f.!g
467
468
469
470
	// 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.
471
472
473
	//
	// In `exprop' mode, considering all possible combinations of
	// outgoing propositions generalizes the above trick.
474
475
476
477
478

	typedef std::map<bdd, bdd, bdd_less_than> prom_map;
	typedef Sgi::hash_map<const formula*, prom_map, ptr_hash<formula> >
	  dest_map;
	dest_map dests;
479

480
	// Compute all outgoing arcs.
481
482
483
484
485

	// 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.
486
	bdd var_set = bdd_existcomp(bdd_support(res), d.var_set);
487
488
489
490
491
	// 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);
492
	while (all_props != bddfalse)
493
	  {
494
495
496
497
498
499
	    bdd one_prop_set =
	      exprop ? bdd_satoneset(all_props, var_set, bddtrue) : bddtrue;
	    all_props -= one_prop_set;
	    minato_isop isop(res & one_prop_set);
	    bdd cube;
	    while ((cube = isop.next()) != bddfalse)
500
	      {
501
502
503
504
505
506
507
		const formula* dest =
		  d.conj_bdd_to_formula(bdd_existcomp(cube, d.next_set));

		// If we already know a state with the same successors,
		// use it in lieu of the current one.  (See the comments
		// for canonical_succ.)  We need to do this only for new
		// destinations.
508
509
		if (symb_merge
		    && formulae_seen.find(dest) == formulae_seen.end())
510
		  {
511
512
513
514
515
516
517
518
519
520
521
		    dest->accept(v);
		    bdd succbdd = v.result();
		    succ_to_formula::iterator cs =
		      canonical_succ.find(succbdd);
		    if (cs != canonical_succ.end())
		      {
			destroy(dest);
			dest = clone(cs->second);
		      }
		    else
		      {
522
			canonical_succ[succbdd] = clone(dest);
523
524
525
526
527
528
529
530
531
532
533
		      }
		  }

		bdd promises = bdd_existcomp(cube, d.a_set);
		bdd conds =
		  exprop ? one_prop_set : bdd_existcomp(cube, var_set);

		dest_map::iterator i = dests.find(dest);
		if (i == dests.end())
		  {
		    dests[dest][promises] = conds;
534
535
536
		  }
		else
		  {
537
538
		    i->second[promises] |= conds;
		    destroy(dest);
539
540
		  }
	      }
541
	  }
542

543
	// Check for an arc going to 1 (True).  Register it first, that
544
545
546
	// way it will be explored before the other during the model
	// checking.
	dest_map::const_iterator i = dests.find(constant::true_instance());
547
548
549
550
	// COND_FOR_TRUE is the conditions of the True arc, so when
	// 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.
551
552
	//
	// Consider
553
	//   f = r(X(1) R p) = p.(1 + r(X(1) R p))
554
	// with exprop the two outgoing arcs would be
555
556
        //         p                  p
	//     f ----> 1       f ----------> 1
557
558
	//
	// where in fact we could output
559
560
        //         p
	//     f ----> 1
561
	//
562
	// because there is no point in looping on f if we can go to 1.
563
	bdd cond_for_true = bddfalse;
564
565
	if (i != dests.end())
	  {
566
567
	    // Transitions going to 1 (true) are not expected to make
	    // any promises.
568
569
570
571
	    assert(i->second.size() == 1);
	    prom_map::const_iterator j = i->second.find(bddtrue);
	    assert(j != i->second.end());

572
	    cond_for_true = j->second;
573
574
	    tgba_explicit::transition* t =
	      a->create_transition(now, constant::true_instance()->val_name());
575
	    a->add_condition(t, d.bdd_to_formula(cond_for_true));
576
577
578
579
580
	  }
	// Register other transitions.
	for (i = dests.begin(); i != dests.end(); ++i)
	  {
	    const formula* dest = i->first;
581
582
583
584
	    // 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;
585

586
587
588
589
590
591
	    if (dest != constant::true_instance())
	      {
		std::string next = to_string(dest);
		for (prom_map::const_iterator j = i->second.begin();
		     j != i->second.end(); ++j)
		  {
592
593
594
		    bdd cond = j->second - cond_for_true;
		    if (cond == bddfalse) // Skip false transitions.
		      continue;
595
596
		    tgba_explicit::transition* t =
		      a->create_transition(now, next);
597
		    a->add_condition(t, d.bdd_to_formula(cond));
598
		    d.conj_bdd_to_acc(a, j->first, t);
599
		    reachable = true;
600
601
		  }
	      }
602
603
604
605
606
607
608
	    else
	      {
		// "1" is reachable.
		reachable = true;
	      }
	    if (reachable
		&& formulae_seen.find(dest) == formulae_seen.end())
609
610
611
612
613
614
	      {
		formulae_seen.insert(dest);
		formulae_to_translate.insert(dest);
	      }
	    else
	      {
615
		destroy(dest);
616
617
618
619
620
	      }
	  }
      }

    // Free all formulae.
621
    for (std::set<const formula*>::iterator i = formulae_seen.begin();
622
	 i != formulae_seen.end(); ++i)
623
      destroy(*i);
624
625
626
    for (succ_to_formula::iterator i = canonical_succ.begin();
     	 i != canonical_succ.end(); ++i)
      destroy(i->second);
627

628
629
    // Turn all promises into real acceptance conditions.
    a->complement_all_acceptance_conditions();
630
631
632
633
    return a;
  }

}