stutter.cc 18.1 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2014-2017 Laboratoire de Recherche et Développement de
// l'Epita (LRDE).
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
// 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/>.

20
21
22
23
24
25
26
27
28
29
#include <spot/twaalgos/stutter.hh>
#include <spot/twa/twa.hh>
#include <spot/misc/hash.hh>
#include <spot/misc/hashfunc.hh>
#include <spot/tl/apcollect.hh>
#include <spot/twaalgos/translate.hh>
#include <spot/tl/remove_x.hh>
#include <spot/twaalgos/product.hh>
#include <spot/twaalgos/ltl2tgba_fm.hh>
#include <spot/twaalgos/isdet.hh>
30
#include <spot/twaalgos/dualize.hh>
31
#include <spot/twaalgos/remfin.hh>
32
#include <spot/twaalgos/postproc.hh>
33
34
#include <spot/twa/twaproduct.hh>
#include <spot/twa/bddprint.hh>
35
36
37
38
39
40
41
42
43
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace spot
{
  namespace
  {
44
    class state_tgbasl final: public state
45
46
    {
    public:
47
      state_tgbasl(const state* s, bdd cond) : s_(s), cond_(cond)
48
49
50
51
52
53
54
55
56
57
      {
      }

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

      virtual int
58
      compare(const state* other) const override
59
60
61
62
63
64
65
66
67
68
      {
        const state_tgbasl* o =
          down_cast<const state_tgbasl*>(other);
        int res = s_->compare(o->real_state());
        if (res != 0)
          return res;
        return cond_.id() - o->cond_.id();
      }

      virtual size_t
69
      hash() const override
70
71
72
73
74
      {
        return wang32_hash(s_->hash()) ^ wang32_hash(cond_.id());
      }

      virtual
75
      state_tgbasl* clone() const override
76
77
78
79
      {
        return new state_tgbasl(*this);
      }

80
      const state*
81
82
83
84
85
86
87
88
89
90
91
92
      real_state() const
      {
        return s_;
      }

      bdd
      cond() const
      {
        return cond_;
      }

    private:
93
      const state* s_;
94
95
96
      bdd cond_;
    };

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

      virtual
107
      ~twasl_succ_iterator()
108
109
110
111
112
113
      {
        delete it_;
      }

      // iteration

114
115
      virtual bool
      first() override
116
117
118
119
120
121
      {
        loop_ = false;
        done_ = false;
        need_loop_ = true;
        if (it_->first())
          {
122
            cond_ = it_->cond();
123
124
125
126
127
            next_edge();
          }
        return true;
      }

128
129
      virtual bool
      next() override
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
      {
        if (cond_ != bddfalse)
          {
            next_edge();
            return true;
          }
        if (!it_->next())
          {
            if (loop_ || !need_loop_)
              done_ = true;
            loop_ = true;
            return !done_;
          }
        else
          {
145
            cond_ = it_->cond();
146
147
148
149
150
            next_edge();
            return true;
          }
      }

151
152
      virtual bool
      done() const override
153
154
155
156
157
158
      {
        return it_->done() && done_;
      }

      // inspection

159
160
      virtual state_tgbasl*
      dst() const override
161
162
163
      {
        if (loop_)
          return new state_tgbasl(state_->real_state(), state_->cond());
164
        return new state_tgbasl(it_->dst(), one_);
165
166
      }

167
168
      virtual bdd
      cond() const override
169
170
171
172
173
174
      {
        if (loop_)
          return state_->cond();
        return one_;
      }

175
176
      virtual acc_cond::mark_t
      acc() const override
177
178
179
      {
        if (loop_)
          return 0U;
180
        return it_->acc();
181
182
183
184
185
186
187
188
189
      }

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

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


206
    class tgbasl final : public twa
207
208
    {
    public:
209
      tgbasl(const const_twa_ptr& a, bdd atomic_propositions)
210
        : twa(a->get_dict()), a_(a), aps_(atomic_propositions)
211
      {
212
213
214
        get_dict()->register_all_propositions_of(&a_, this);
        assert(num_sets() == 0);
        set_generalized_buchi(a_->num_sets());
215
216
      }

217
      virtual const state* get_init_state() const override
218
      {
219
        return new state_tgbasl(a_->get_init_state(), bddfalse);
220
221
      }

222
      virtual twa_succ_iterator* succ_iter(const state* state) const override
223
      {
224
225
226
        const state_tgbasl* s = down_cast<const state_tgbasl*>(state);
        return new twasl_succ_iterator(a_->succ_iter(s->real_state()), s,
                                        a_->get_dict(), aps_);
227
228
229
230
      }

      virtual std::string format_state(const state* state) const override
      {
231
232
233
234
        const state_tgbasl* s = down_cast<const state_tgbasl*>(state);
        return (a_->format_state(s->real_state())
                + ", "
                + bdd_format_formula(a_->get_dict(), s->cond()));
235
236
237
      }

    private:
238
      const_twa_ptr a_;
239
240
241
242
243
      bdd aps_;
    };

    typedef std::shared_ptr<tgbasl> tgbasl_ptr;

244
    inline tgbasl_ptr make_tgbasl(const const_twa_ptr& aut, bdd ap)
245
246
247
248
249
250
251
252
253
254
255
256
257
    {
      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
      {
258
        return wang32_hash(s.first) ^ wang32_hash(s.second.id());
259
260
261
262
263
      }
    };

    // Associate the stutter state to its number.
    typedef std::unordered_map<stutter_state, unsigned,
264
                               stutter_state_hash> ss2num_map;
265
266
267
268
269

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

270
  twa_graph_ptr
271
  sl(const twa_graph_ptr& a)
272
  {
273
    return sl(a, a->ap_vars());
274
275
  }

276
  twa_graph_ptr
277
  sl2(const twa_graph_ptr& a)
278
  {
279
    return sl2(a, a->ap_vars());
280
281
  }

282
283
  twa_graph_ptr
  sl(const const_twa_graph_ptr& a, bdd atomic_propositions)
284
285
  {
    // The result automaton uses numbered states.
286
    twa_graph_ptr res = make_twa_graph(a->get_dict());
287
288
    // We use the same BDD variables as the input.
    res->copy_ap_of(a);
289
    res->copy_acceptance_of(a);
290
291
292
293
294
295
296
297
298
299
    // 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();
300
    todo.emplace_back(s);
301
302
303

    while (!todo.empty())
      {
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
        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)
                  {
325
                    todo.emplace_back(d);
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
                    unsigned u = res->new_state();
                    assert(u == dest);
                    (void)u;
                  }

                // Create the edge.
                res->new_edge(src, dest, one, t.acc);

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

        if (self_loop_needed && s.second != bddfalse)
          res->new_edge(src, src, s.second, 0U);
341
      }
342
    res->merge_edges();
343
344
345
    return res;
  }

346
347
  twa_graph_ptr
  sl2(twa_graph_ptr&& a, bdd atomic_propositions)
348
349
  {
    if (atomic_propositions == bddfalse)
350
      atomic_propositions = a->ap_vars();
351
    unsigned num_states = a->num_states();
352
    unsigned num_edges = a->num_edges();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
353
354
355
356
    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.
357
    for (auto& t: a->edges())
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
358
      if (t.src == t.dst)
359
        selfloops[t.src] |= t.cond;
360
    for (unsigned t = 1; t <= num_edges; ++t)
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
        auto& td = a->edge_storage(t);
        if (a->is_dead_edge(td))
          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
            // either end of the edge, do not bother with it.
            if (bdd_implies(all, selfloops[src])
                || bdd_implies(all, selfloops[dst]))
              continue;
            // Do not use td in the loop because the new_edge()
            // 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
                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);
                // No acceptance here to preserve the state-based property.
                i = a->new_edge(tmp, dst, one, 0U);
                assert(i > num_edges);
                (void)i;
              }
          }
401
402
      }
    if (num_states != a->num_states())
403
      a->prop_keep({true,         // state_based
404
                    false,        // inherently_weak
405
                    false, false, // deterministic
406
407
                    true,         // complete
                    false,        // stutter inv.
408
                   });
409
    a->merge_edges();
410
411
412
    return a;
  }

413
414
  twa_graph_ptr
  sl2(const const_twa_graph_ptr& a, bdd atomic_propositions)
415
  {
416
    return sl2(make_twa_graph(a, twa::prop_set::all()),
417
               atomic_propositions);
418
419
420
  }


421
422
  twa_graph_ptr
  closure(twa_graph_ptr&& a)
423
  {
424
425
    a->prop_keep({false,        // state_based
                  false,        // inherently_weak
426
                  false, false, // deterministic
427
                  true,         // complete
428
429
                  false,        // stutter inv.
                 });
430
431
432
433
434
435
436

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

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

        for (auto it = trans.begin(); it != trans.end(); ++it)
          {
441
442
            todo.emplace_back(it.trans());
            dst2trans[it->dst].emplace_back(it.trans());
443
444
445
446
447
448
449
450
451
452
453
454
          }

        while (!todo.empty())
          {
            auto t1 = a->edge_storage(todo.back());
            todo.pop_back();

            for (auto& t2 : a->out(t1.dst))
              {
                bdd cond = t1.cond & t2.cond;
                if (cond != bddfalse)
                  {
455
                    bool need_new_trans = true;
456
                    acc_cond::mark_t acc = t1.acc | t2.acc;
457
458
                    for (auto& t: dst2trans[t2.dst])
                      {
459
                        auto& ts = a->edge_storage(t);
460
461
462
463
                        if (acc == ts.acc)
                          {
                            if (!bdd_implies(cond, ts.cond))
                              {
464
                                ts.cond |= cond;
465
466
                                if (std::find(todo.begin(), todo.end(), t)
                                    == todo.end())
467
                                  todo.emplace_back(t);
468
469
                              }
                            need_new_trans = false;
470
                            break;
471
                          }
472
473
474
475
476
477
478
479
                        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())
480
                                  todo.emplace_back(t);
481
                              }
482
                            need_new_trans = false;
483
484
                            break;
                          }
485
486
487
                      }
                    if (need_new_trans)
                      {
488
489
490
                        // Load t2.dst first, because t2 can be
                        // invalidated by new_edge().
                        auto dst = t2.dst;
491
                        auto i = a->new_edge(state, dst, cond, acc);
492
493
                        dst2trans[dst].emplace_back(i);
                        todo.emplace_back(i);
494
                      }
495
496
497
                  }
              }
          }
498
499
500
501
502
503
        for (auto& it: dst2trans)
          it.clear();
      }
    return a;
  }

504
505
  twa_graph_ptr
  closure(const const_twa_graph_ptr& a)
506
  {
507
    return closure(make_twa_graph(a, twa::prop_set::all()));
508
509
510
511
512
513
514
515
516
  }

  // 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)
      {
517
518
519
520
521
522
        char* endptr;
        long res = strtol(stutter_check, &endptr, 10);
        if (*endptr || res < 0 || res > 9)
          throw
            std::runtime_error("invalid value for SPOT_STUTTER_CHECK.");
        return res;
523
524
525
      }
    else
      {
526
        return 8;     // The best variant, according to our benchmarks.
527
528
529
530
      }
  }

  bool
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
531
  is_stutter_invariant(formula f)
532
  {
533
    if (f.is_ltl_formula() && f.is_syntactic_stutter_invariant())
534
535
536
537
      return true;

    int algo = default_stutter_check_algorithm();

538
539
    if (algo == 0 || algo == 9)
      // Etessami's check via syntactic transformation.
540
      {
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
        if (!f.is_ltl_formula())
          throw std::runtime_error("Cannot use the syntactic "
                                   "stutter-invariance check "
                                   "for non-LTL formulas");
        formula g = remove_x(f);
        bool res;
        if (algo == 0)                // Equivalence check
          {
            tl_simplifier ls;
            res = ls.are_equivalent(f, g);
          }
        else
          {
            formula h = formula::Xor(f, g);
            res = ltl_to_tgba_fm(h, make_bdd_dict())->is_empty();
          }
        return res;
558
559
560
561
562
      }

    // Prepare for an automata-based check.
    translator trans;
    auto aut_f = trans.run(f);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
563
    auto aut_nf = trans.run(formula::Not(f));
564
565
566
567
568
    bdd aps = atomic_prop_collect_as_bdd(f, aut_f);
    return is_stutter_invariant(std::move(aut_f), std::move(aut_nf), aps, algo);
  }

  bool
569
570
  is_stutter_invariant(twa_graph_ptr&& aut_f,
                       twa_graph_ptr&& aut_nf, bdd aps, int algo)
571
572
573
574
575
576
577
  {
    if (algo == 0)
      algo = default_stutter_check_algorithm();

    switch (algo)
      {
      case 1: // sl(aut_f) x sl(aut_nf)
578
579
        return product(sl(std::move(aut_f), aps),
                       sl(std::move(aut_nf), aps))->is_empty();
580
      case 2: // sl(cl(aut_f)) x aut_nf
581
582
        return product(sl(closure(std::move(aut_f)), aps),
                       std::move(aut_nf))->is_empty();
583
      case 3: // (cl(sl(aut_f)) x aut_nf
584
585
        return product(closure(sl(std::move(aut_f), aps)),
                       std::move(aut_nf))->is_empty();
586
      case 4: // sl2(aut_f) x sl2(aut_nf)
587
588
        return product(sl2(std::move(aut_f), aps),
                       sl2(std::move(aut_nf), aps))->is_empty();
589
      case 5: // sl2(cl(aut_f)) x aut_nf
590
591
        return product(sl2(closure(std::move(aut_f)), aps),
                       std::move(aut_nf))->is_empty();
592
      case 6: // (cl(sl2(aut_f)) x aut_nf
593
594
        return product(closure(sl2(std::move(aut_f), aps)),
                       std::move(aut_nf))->is_empty();
595
      case 7: // on-the-fly sl(aut_f) x sl(aut_nf)
596
597
        return otf_product(make_tgbasl(aut_f, aps),
                           make_tgbasl(aut_nf, aps))->is_empty();
598
      case 8: // cl(aut_f) x cl(aut_nf)
599
600
        return product(closure(std::move(aut_f)),
                       closure(std::move(aut_nf)))->is_empty();
601
      default:
602
603
604
        throw std::runtime_error("invalid algorithm number for "
                                 "is_stutter_invariant()");
        SPOT_UNREACHABLE();
605
606
      }
  }
607

608
  trival
609
610
  check_stutter_invariance(const twa_graph_ptr& aut, formula f,
                           bool do_not_determinize)
611
  {
612
613
614
    trival is_stut = aut->prop_stutter_invariant();
    if (is_stut.is_known())
      return is_stut.is_true();
615
616
617
618

    twa_graph_ptr neg = nullptr;
    if (f)
      {
619
        neg = translator(aut->get_dict()).run(formula::Not(f));
620
621
622
      }
    else
      {
623
624
625
626
627
628
629
630
631
632
633
        twa_graph_ptr tmp = aut;
        if (!is_deterministic(aut))
          {
            if (do_not_determinize)
              return trival::maybe();
            spot::postprocessor p;
            p.set_type(spot::postprocessor::Generic);
            p.set_pref(spot::postprocessor::Deterministic);
            p.set_level(spot::postprocessor::Low);
            tmp = p.run(aut);
          }
634
        neg = dualize(tmp);
635
636
637
      }

    is_stut = is_stutter_invariant(make_twa_graph(aut, twa::prop_set::all()),
638
                                   std::move(neg), aut->ap_vars());
639
    aut->prop_stutter_invariant(is_stut);
640
641
    return is_stut;
  }
642
}