stutter.cc 16.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// -*- coding: utf-8 -*-
// Copyright (C) 2014, 2015 Laboratoire de Recherche
// et Développement de l'Epita (LRDE).
//
// 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 3 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 this program.  If not, see <http://www.gnu.org/licenses/>.

#include "stutter.hh"
21
#include "twa/twa.hh"
22
23
24
25
26
27
#include "dupexp.hh"
#include "misc/hash.hh"
#include "misc/hashfunc.hh"
#include "ltlvisit/apcollect.hh"
#include "translate.hh"
#include "ltlast/unop.hh"
28
#include "ltlast/binop.hh"
29
#include "ltlvisit/remove_x.hh"
30
31
#include "twaalgos/product.hh"
#include "twaalgos/ltl2tgba_fm.hh"
32
33
#include "twaalgos/isdet.hh"
#include "twaalgos/dtgbacomp.hh"
34
35
#include "twa/twaproduct.hh"
#include "twa/bddprint.hh"
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace spot
{
  namespace
  {
    class state_tgbasl: public state
    {
    public:
      state_tgbasl(state* s, bdd cond) : s_(s), cond_(cond)
      {
      }

      virtual
      ~state_tgbasl()
      {
        s_->destroy();
      }

      virtual int
      compare(const state* other) const
      {
        const state_tgbasl* o =
          down_cast<const state_tgbasl*>(other);
        assert(o);
        int res = s_->compare(o->real_state());
        if (res != 0)
          return res;
        return cond_.id() - o->cond_.id();
      }

      virtual size_t
      hash() const
      {
        return wang32_hash(s_->hash()) ^ wang32_hash(cond_.id());
      }

      virtual
      state_tgbasl* clone() const
      {
        return new state_tgbasl(*this);
      }

      state*
      real_state() const
      {
        return s_;
      }

      bdd
      cond() const
      {
        return cond_;
      }

    private:
      state* s_;
      bdd cond_;
    };

99
    class twasl_succ_iterator : public twa_succ_iterator
100
101
    {
    public:
102
      twasl_succ_iterator(twa_succ_iterator* it, const state_tgbasl* state,
103
104
105
106
107
108
			   bdd_dict_ptr d, bdd atomic_propositions)
        : it_(it), state_(state), aps_(atomic_propositions), d_(d)
      {
      }

      virtual
109
      ~twasl_succ_iterator()
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
      {
        delete it_;
      }

      // iteration

      bool
      first()
      {
        loop_ = false;
        done_ = false;
        need_loop_ = true;
        if (it_->first())
          {
            cond_ = it_->current_condition();
            next_edge();
          }
        return true;
      }

      bool
      next()
      {
        if (cond_ != bddfalse)
          {
            next_edge();
            return true;
          }
        if (!it_->next())
          {
            if (loop_ || !need_loop_)
              done_ = true;
            loop_ = true;
            return !done_;
          }
        else
          {
            cond_ = it_->current_condition();
            next_edge();
            return true;
          }
      }

      bool
      done() const
      {
        return it_->done() && done_;
      }

      // inspection

      state_tgbasl*
      current_state() const
      {
        if (loop_)
          return new state_tgbasl(state_->real_state(), state_->cond());
        return new state_tgbasl(it_->current_state(), one_);
      }

      bdd
      current_condition() const
      {
        if (loop_)
          return state_->cond();
        return one_;
      }

      acc_cond::mark_t
      current_acceptance_conditions() const
      {
        if (loop_)
          return 0U;
        return it_->current_acceptance_conditions();
      }

    private:
      void
      next_edge()
      {
        one_ = bdd_satoneset(cond_, aps_, bddtrue);
        cond_ -= one_;
        if (need_loop_ && (state_->cond() == one_)
            && (state_ == it_->current_state()))
          need_loop_ = false;
      }

196
      twa_succ_iterator* it_;
197
198
199
200
201
202
203
204
205
206
207
      const state_tgbasl* state_;
      bdd cond_;
      bdd one_;
      bdd aps_;
      bdd_dict_ptr d_;
      bool loop_;
      bool need_loop_;
      bool done_;
    };


208
    class tgbasl final : public twa
209
210
    {
    public:
211
      tgbasl(const const_twa_ptr& a, bdd atomic_propositions)
212
	: twa(a->get_dict()), a_(a), aps_(atomic_propositions)
213
214
215
      {
	get_dict()->register_all_propositions_of(&a_, this);
	assert(acc_.num_sets() == 0);
216
	acc_.add_sets(a_->num_sets());
217
	acc_.set_generalized_buchi();
218
219
220
221
222
223
224
225
226
227
228
229
      }

      virtual ~tgbasl()
      {
	get_dict()->unregister_all_my_variables(this);
      }

      virtual state* get_init_state() const override
      {
	return new state_tgbasl(a_->get_init_state(), bddfalse);
      }

230
      virtual twa_succ_iterator* succ_iter(const state* state) const override
231
232
233
      {
	const state_tgbasl* s = down_cast<const state_tgbasl*>(state);
	assert(s);
234
	return new twasl_succ_iterator(a_->succ_iter(s->real_state()), s,
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
					a_->get_dict(), aps_);
      }

      virtual std::string format_state(const state* state) const override
      {
	const state_tgbasl* s = down_cast<const state_tgbasl*>(state);
	assert(s);
	return (a_->format_state(s->real_state())
		+ ", "
		+ bdd_format_formula(a_->get_dict(), s->cond()));
      }

    protected:
      virtual bdd compute_support_conditions(const state*) const override
      {
	return bddtrue;
      }

    private:
254
      const_twa_ptr a_;
255
256
257
258
259
      bdd aps_;
    };

    typedef std::shared_ptr<tgbasl> tgbasl_ptr;

260
    inline tgbasl_ptr make_tgbasl(const const_twa_ptr& aut, bdd ap)
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
    {
      return std::make_shared<tgbasl>(aut, ap);
    }



    typedef std::pair<unsigned, bdd> stutter_state;

    struct stutter_state_hash
    {
      size_t
      operator()(const stutter_state& s) const
      {
	return wang32_hash(s.first) ^ wang32_hash(s.second.id());
      }
    };

    // Associate the stutter state to its number.
    typedef std::unordered_map<stutter_state, unsigned,
			       stutter_state_hash> ss2num_map;

    // Queue of state to be processed.
    typedef std::deque<stutter_state> queue_t;
  }

286
  twa_graph_ptr
287
  sl(const twa_graph_ptr& a)
288
  {
289
    return sl(a, a->ap_var());
290
291
  }

292
  twa_graph_ptr
293
  sl2(const twa_graph_ptr& a)
294
  {
295
    return sl2(a, a->ap_var());
296
297
  }

298
299
  twa_graph_ptr
  sl(const const_twa_graph_ptr& a, bdd atomic_propositions)
300
301
  {
    // The result automaton uses numbered states.
302
    twa_graph_ptr res = make_twa_graph(a->get_dict());
303
304
    // We use the same BDD variables as the input.
    res->copy_ap_of(a);
305
    res->copy_acceptance_of(a);
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
    // These maps make it possible to convert stutter_state to number
    // and vice-versa.
    ss2num_map ss2num;

    queue_t todo;

    unsigned s0 = a->get_init_state_number();
    stutter_state s(s0, bddfalse);
    ss2num[s] = 0;
    res->new_state();
    todo.push_back(s);

    while (!todo.empty())
      {
	s = todo.front();
	todo.pop_front();
	unsigned src = ss2num[s];

	bool self_loop_needed = true;

	for (auto& t : a->out(s.first))
	  {
	    bdd all = t.cond;
	    while (all != bddfalse)
	      {
		bdd one = bdd_satoneset(all, atomic_propositions, bddtrue);
		all -= one;

		stutter_state d(t.dst, one);

		auto r = ss2num.emplace(d, ss2num.size());
		unsigned dest = r.first->second;

		if (r.second)
		  {
		    todo.push_back(d);
		    unsigned u = res->new_state();
		    assert(u == dest);
		    (void)u;
		  }

347
348
		// Create the edge.
		res->new_edge(src, dest, one, t.acc);
349
350
351
352
353
354
355

		if (src == dest)
		  self_loop_needed = false;
	      }
	  }

	if (self_loop_needed && s.second != bddfalse)
356
	  res->new_edge(src, src, s.second, 0U);
357
      }
358
    res->merge_edges();
359
360
361
    return res;
  }

362
363
  twa_graph_ptr
  sl2(twa_graph_ptr&& a, bdd atomic_propositions)
364
365
  {
    if (atomic_propositions == bddfalse)
366
      atomic_propositions = a->ap_var();
367
    unsigned num_states = a->num_states();
368
    unsigned num_edges = a->num_edges();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
369
370
371
372
    std::vector<bdd> selfloops(num_states, bddfalse);
    std::map<std::pair<unsigned, int>, unsigned> newstates;
    // Record all the conditions for which we can selfloop on each
    // state.
373
    for (auto& t: a->edges())
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
374
375
      if (t.src == t.dst)
	selfloops[t.src] |= t.cond;
376
    for (unsigned t = 1; t <= num_edges; ++t)
377
      {
378
379
	auto& td = a->edge_storage(t);
	if (a->is_dead_edge(td))
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
380
381
382
383
384
385
386
387
	  continue;

	unsigned src = td.src;
	unsigned dst = td.dst;
	if (src != dst)
	  {
	    bdd all = td.cond;
	    // If there is a self-loop with the whole condition on
388
	    // either end of the edge, do not bother with it.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
389
390
391
	    if (bdd_implies(all, selfloops[src])
		|| bdd_implies(all, selfloops[dst]))
	      continue;
392
	    // Do not use td in the loop because the new_edge()
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
393
394
395
396
397
398
399
400
401
402
403
404
405
406
	    // might invalidate it.
	    auto acc = td.acc;
	    while (all != bddfalse)
	      {
		bdd one = bdd_satoneset(all, atomic_propositions, bddtrue);
		all -= one;
		// Skip if there is a loop for this particular letter.
		if (bdd_implies(one, selfloops[src])
		    || bdd_implies(one, selfloops[dst]))
		  continue;
		auto p = newstates.emplace(std::make_pair(dst, one.id()), 0);
		if (p.second)
		  p.first->second = a->new_state();
		unsigned tmp = p.first->second; // intermediate state
407
408
409
410
		unsigned i = a->new_edge(src, tmp, one, acc);
		assert(i > num_edges);
		i = a->new_edge(tmp, tmp, one, 0U);
		assert(i > num_edges);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
411
		// No acceptance here to preserve the state-based property.
412
413
		i = a->new_edge(tmp, dst, one, 0U);
		assert(i > num_edges);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
414
415
416
		(void)i;
	      }
	  }
417
418
419
420
421
      }
    if (num_states != a->num_states())
      a->prop_keep({true,	// state_based
	            false,	// inherently_weak
	            false,	// deterministic
422
	            false,      // stutter inv.
423
	           });
424
    a->merge_edges();
425
426
427
    return a;
  }

428
429
  twa_graph_ptr
  sl2(const const_twa_graph_ptr& a, bdd atomic_propositions)
430
  {
431
    return sl2(make_twa_graph(a, twa::prop_set::all()),
432
433
434
435
	       atomic_propositions);
  }


436
437
  twa_graph_ptr
  closure(twa_graph_ptr&& a)
438
439
440
441
  {
    a->prop_keep({false,	// state_based
	          false,	// inherently_weak
	          false,	// deterministic
442
	          false,        // stutter inv.
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
	         });

    unsigned n = a->num_states();
    std::vector<unsigned> todo;
    std::vector<std::vector<unsigned> > dst2trans(n);

    for (unsigned state = 0; state < n; ++state)
      {
	auto trans = a->out(state);

	for (auto it = trans.begin(); it != trans.end(); ++it)
	  {
	    todo.push_back(it.trans());
	    dst2trans[it->dst].push_back(it.trans());
	  }

	while (!todo.empty())
	  {
461
	    auto t1 = a->edge_storage(todo.back());
462
463
464
465
466
467
468
469
470
471
472
	    todo.pop_back();

	    for (auto& t2 : a->out(t1.dst))
	      {
		bdd cond = t1.cond & t2.cond;
		if (cond != bddfalse)
		  {
                    bool need_new_trans = true;
		    acc_cond::mark_t acc = t1.acc | t2.acc;
                    for (auto& t: dst2trans[t2.dst])
                      {
473
                        auto& ts = a->edge_storage(t);
474
475
476
477
                        if (acc == ts.acc)
                          {
                            if (!bdd_implies(cond, ts.cond))
                              {
478
                                ts.cond |= cond;
479
480
481
482
483
                                if (std::find(todo.begin(), todo.end(), t)
                                    == todo.end())
                                  todo.push_back(t);
                              }
                            need_new_trans = false;
484
			    break;
485
                          }
486
487
488
489
490
491
492
493
494
495
496
497
498
			else if (cond == ts.cond)
			  {
			    acc |= ts.acc;
			    if (ts.acc != acc)
			      {
				ts.acc = acc;
				if (std::find(todo.begin(), todo.end(), t)
				    == todo.end())
				  todo.push_back(t);
			      }
                            need_new_trans = false;
			    break;
			  }
499
500
501
502
                      }
                    if (need_new_trans)
                      {
			// Load t2.dst first, because t2 can be
503
			// invalidated by new_edge().
504
			auto dst = t2.dst;
505
                        auto i = a->new_edge(state, dst, cond, acc);
506
507
508
509
510
511
512
513
514
515
516
517
                        dst2trans[dst].push_back(i);
                        todo.push_back(i);
                      }
		  }
	      }
	  }
        for (auto& it: dst2trans)
          it.clear();
      }
    return a;
  }

518
519
  twa_graph_ptr
  closure(const const_twa_graph_ptr& a)
520
  {
521
    return closure(make_twa_graph(a, {true, true, true, false}));
522
523
524
525
526
527
528
529
530
531
532
  }

  // The stutter check algorithm to use can be overridden via an
  // environment variable.
  static int default_stutter_check_algorithm()
  {
    static const char* stutter_check = getenv("SPOT_STUTTER_CHECK");
    if (stutter_check)
      {
	char* endptr;
	long res = strtol(stutter_check, &endptr, 10);
533
	if (*endptr || res < 0 || res > 9)
534
535
536
537
538
539
540
541
542
543
544
545
546
	  throw
	    std::runtime_error("invalid value for SPOT_STUTTER_CHECK.");
	return res;
      }
    else
      {
	return 8;     // The best variant, according to our benchmarks.
      }
  }

  bool
  is_stutter_invariant(const ltl::formula* f)
  {
547
    if (f->is_ltl_formula() && f->is_syntactic_stutter_invariant())
548
549
550
551
      return true;

    int algo = default_stutter_check_algorithm();

552
553
    if (algo == 0 || algo == 9)
      // Etessami's check via syntactic transformation.
554
555
556
557
558
559
      {
	if (!f->is_ltl_formula())
	  throw std::runtime_error("Cannot use the syntactic "
				   "stutter-invariance check "
				   "for non-LTL formulas");
	const ltl::formula* g = remove_x(f);
560
561
562
563
564
565
566
567
568
569
570
571
572
573
	bool res;
	if (algo == 0)		// Equivalence check
	  {
	    ltl::ltl_simplifier ls;
	    res = ls.are_equivalent(f, g);
	    g->destroy();
	  }
	else
	  {
	    const ltl::formula* h = ltl::binop::instance(ltl::binop::Xor,
							 f->clone(), g);
	    res = ltl_to_tgba_fm(h, make_bdd_dict())->is_empty();
	    h->destroy();
	  }
574
575
576
577
578
579
580
581
582
583
584
585
586
587
	return res;
      }

    // Prepare for an automata-based check.
    const ltl::formula* nf = ltl::unop::instance(ltl::unop::Not, f->clone());
    translator trans;
    auto aut_f = trans.run(f);
    auto aut_nf = trans.run(nf);
    bdd aps = atomic_prop_collect_as_bdd(f, aut_f);
    nf->destroy();
    return is_stutter_invariant(std::move(aut_f), std::move(aut_nf), aps, algo);
  }

  bool
588
589
  is_stutter_invariant(twa_graph_ptr&& aut_f,
                       twa_graph_ptr&& aut_nf, bdd aps, int algo)
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
  {
    if (algo == 0)
      algo = default_stutter_check_algorithm();

    switch (algo)
      {
      case 1: // sl(aut_f) x sl(aut_nf)
	return product(sl(std::move(aut_f), aps),
		       sl(std::move(aut_nf), aps))->is_empty();
      case 2: // sl(cl(aut_f)) x aut_nf
	return product(sl(closure(std::move(aut_f)), aps),
		       std::move(aut_nf))->is_empty();
      case 3: // (cl(sl(aut_f)) x aut_nf
	return product(closure(sl(std::move(aut_f), aps)),
		       std::move(aut_nf))->is_empty();
      case 4: // sl2(aut_f) x sl2(aut_nf)
	return product(sl2(std::move(aut_f), aps),
		       sl2(std::move(aut_nf), aps))->is_empty();
      case 5: // sl2(cl(aut_f)) x aut_nf
	return product(sl2(closure(std::move(aut_f)), aps),
		       std::move(aut_nf))->is_empty();
      case 6: // (cl(sl2(aut_f)) x aut_nf
	return product(closure(sl2(std::move(aut_f), aps)),
		       std::move(aut_nf))->is_empty();
      case 7: // on-the-fly sl(aut_f) x sl(aut_nf)
	return otf_product(make_tgbasl(aut_f, aps),
			   make_tgbasl(aut_nf, aps))->is_empty();
      case 8: // cl(aut_f) x cl(aut_nf)
	return product(closure(std::move(aut_f)),
		       closure(std::move(aut_nf)))->is_empty();
      default:
	throw std::runtime_error("invalid algorithm number for "
				 "is_stutter_invariant()");
	SPOT_UNREACHABLE();
      }
  }
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652

  bool
  check_stutter_invariance(const twa_graph_ptr& aut, const ltl::formula* f)
  {
    bool is_stut = aut->is_stutter_invariant();
    if (is_stut)
      return is_stut;

    twa_graph_ptr neg = nullptr;
    if (f)
      {
	auto* nf = ltl::unop::instance(ltl::unop::Not, f->clone());
	neg = translator(aut->get_dict()).run(nf);
	nf->destroy();
      }
    else
      {
	// If the automaton is deterministic, we
	// know how to complement it.
	aut->prop_deterministic(is_deterministic(aut));
	if (!aut->is_deterministic())
	  return false;

	neg = dtgba_complement(aut);
      }

    is_stut = is_stutter_invariant(make_twa_graph(aut, twa::prop_set::all()),
653
				   std::move(neg), aut->ap_var());
654
655
    aut->prop_stutter_invariant(is_stut);
    aut->prop_stutter_sensitive(!is_stut);
656
657
    return is_stut;
  }
658
}