mark.cc 7.22 KB
Newer Older
1
2
3
// -*- coding: utf-8 -*-
// Copyright (C) 2010, 2012, 2014 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
4
5
6
7
8
//
// 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
9
// the Free Software Foundation; either version 3 of the License, or
10
11
12
13
14
15
16
17
// (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
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21
22
23

#include "mark.hh"
#include "ltlast/allnodes.hh"
#include <cassert>
#include <algorithm>
24
25
#include <set>
#include <vector>
26
27
#include "ltlvisit/tostring.hh"
#include "misc/casts.hh"
28
29
30
31
32
33
34
35
36

namespace spot
{
  namespace ltl
  {
    namespace
    {
      class simplify_mark_visitor : public visitor
      {
37
	const formula* result_;
38
	mark_tools* tools_;
39
40

      public:
41
42
	simplify_mark_visitor(mark_tools* t)
	  : tools_(t)
43
44
45
46
47
48
49
	{
	}

	~simplify_mark_visitor()
	{
	}

50
	const formula*
51
52
53
54
55
56
	result()
	{
	  return result_;
	}

	void
57
	visit(const atomic_prop* ao)
58
59
60
61
62
	{
	  result_ = ao->clone();
	}

	void
63
	visit(const constant* c)
64
65
66
67
	{
	  result_ = c->clone();
	}

68
	void
69
	visit(const bunop* bo)
70
71
72
73
	{
	  result_ = bo->clone();
	}

74
	void
75
	visit(const unop* uo)
76
	{
77
	  result_ = uo->clone();
78
79
80
	}

	void
81
	visit(const multop* mo)
82
83
84
85
86
	{
	  unsigned mos = mo->size();
	  multop::vec* res = new multop::vec;
	  switch (mo->op())
	    {
87
88
89
	    case multop::OrRat:
	    case multop::AndNLM:
	    case multop::AndRat:
90
	    case multop::Concat:
91
	    case multop::Fusion:
92
	      SPOT_UNIMPLEMENTED();
93
	    case multop::Or:
94
95
96
97
98
	      for (unsigned i = 0; i < mos; ++i)
		res->push_back(recurse(mo->nth(i)));
	      break;
	    case multop::And:
	      {
99
100
		typedef std::set<std::pair<const formula*,
					   const formula*> > pset;
101
102
103
104
105
106
107
		pset empairs;
		typedef std::set<const formula*> sset;
		sset nmset;
		typedef std::vector<const binop*> unbinop;
		unbinop elist;
		typedef std::vector<const unop*> ununop;
		ununop nlist;
108
109
110

		for (unsigned i = 0; i < mos; ++i)
		  {
111
		    const formula* f = mo->nth(i);
112
		    if (const binop* bo = is_binop(f))
113
		      {
114
115
			switch (bo->op())
			  {
116
			  case binop::EConcatMarked:
117
			    empairs.emplace(bo->first(), bo->second());
118
			    // fall through
119
120
121
122
123
124
125
126
127
128
129
			  case binop::Xor:
			  case binop::Implies:
			  case binop::Equiv:
			  case binop::U:
			  case binop::W:
			  case binop::M:
			  case binop::R:
			  case binop::UConcat:
			    res->push_back(recurse(f));
			    break;
			  case binop::EConcat:
130
			    elist.push_back(bo);
131
			    break;
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
			  }
		      }
		    if (const unop* uo = is_unop(f))
		      {
			switch (uo->op())
			  {
			  case unop::NegClosureMarked:
			    nmset.insert(uo->child());
			    // fall through
			  case unop::Not:
			  case unop::X:
			  case unop::F:
			  case unop::G:
			  case unop::Finish:
			  case unop::Closure:
			    res->push_back(recurse(f));
			    break;
			  case unop::NegClosure:
			    nlist.push_back(uo);
151
152
153
			    break;
			  }
		      }
154
155
156
157
		    else
		      {
			res->push_back(recurse(f));
		      }
158
		  }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
		// Keep only the non-marked EConcat for which we
		// have not seen a similar EConcatMarked.
		for (unbinop::const_iterator i = elist.begin();
		     i != elist.end(); ++i)
		  if (empairs.find(std::make_pair((*i)->first(),
						  (*i)->second()))
		      == empairs.end())
		    res->push_back((*i)->clone());
		// Keep only the non-marked NegClosure for which we
		// have not seen a similar NegClosureMarked.
		for (ununop::const_iterator i = nlist.begin();
		     i != nlist.end(); ++i)
		  if (nmset.find((*i)->child()) == nmset.end())
		    res->push_back((*i)->clone());
173
174
175
176
177
178
	      }
	    }
	  result_ = multop::instance(mo->op(), res);
	}

	void
179
	visit(const binop* bo)
180
	{
181
	  result_ = bo->clone();
182
183
	}

184
185
	const formula*
	recurse(const formula* f)
186
	{
187
	  return tools_->simplify_mark(f);
188
189
190
191
192
193
	}
      };


      class mark_visitor : public visitor
      {
194
	const formula* result_;
195
196
	mark_tools* tools_;

197
      public:
198
199
	mark_visitor(mark_tools* t)
	  : tools_(t)
200
201
202
203
204
205
	{
	}
	~mark_visitor()
	{
	}

206
	const formula*
207
208
209
210
211
212
	result()
	{
	  return result_;
	}

	void
213
	visit(const atomic_prop* ap)
214
215
216
217
218
	{
	  result_ = ap->clone();
	}

	void
219
	visit(const constant* c)
220
221
222
223
	{
	  result_ = c->clone();
	}

224
	void
225
	visit(const bunop* bo)
226
227
228
229
	{
	  result_ = bo->clone();
	}

230
	void
231
	visit(const unop* uo)
232
	{
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
	  switch (uo->op())
	    {
	    case unop::Not:
	    case unop::X:
	    case unop::F:
	    case unop::G:
	    case unop::Finish:
	    case unop::Closure:
	    case unop::NegClosureMarked:
	      result_ = uo->clone();
	      return;
	    case unop::NegClosure:
	      result_ = unop::instance(unop::NegClosureMarked,
				       uo->child()->clone());
	      return;
	    }
249
	  SPOT_UNREACHABLE();
250
251
252
	}

	void
253
	visit(const multop* mo)
254
255
256
257
258
259
260
261
262
	{
	  multop::vec* res = new multop::vec;
	  unsigned mos = mo->size();
	  for (unsigned i = 0; i < mos; ++i)
	    res->push_back(recurse(mo->nth(i)));
	  result_ = multop::instance(mo->op(), res);
	}

	void
263
	visit(const binop* bo)
264
265
266
267
268
269
	{
	  switch (bo->op())
	    {
	    case binop::Xor:
	    case binop::Implies:
	    case binop::Equiv:
270
	      SPOT_UNIMPLEMENTED();
271
272
273
274
275
	    case binop::U:
	    case binop::W:
	    case binop::M:
	    case binop::R:
	    case binop::UConcat:
276
	    case binop::EConcatMarked:
277
278
279
280
	      result_ = bo->clone();
	      return;
	    case binop::EConcat:
	      {
281
		const formula* f1 = bo->first()->clone();
282
		const formula* f2 = bo->second()->clone();
283
284
285
286
		result_ = binop::instance(binop::EConcatMarked, f1, f2);
		return;
	      }
	    }
287
	  SPOT_UNREACHABLE();
288
289
	}

290
291
	const formula*
	recurse(const formula* f)
292
	{
293
	  return tools_->mark_concat_ops(f);
294
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
    mark_tools::mark_tools()
      : simpvisitor_(new simplify_mark_visitor(this)),
	markvisitor_(new mark_visitor(this))
    {
    }


    mark_tools::~mark_tools()
    {
      delete simpvisitor_;
      delete markvisitor_;
      {
	f2f_map::iterator i = simpmark_.begin();
	f2f_map::iterator end = simpmark_.end();
	while (i != end)
	  {
	    f2f_map::iterator old = i++;
	    old->second->destroy();
	    old->first->destroy();
	  }
      }
      {
	f2f_map::iterator i = markops_.begin();
	f2f_map::iterator end = markops_.end();
	while (i != end)
	  {
	    f2f_map::iterator old = i++;
	    old->second->destroy();
	    old->first->destroy();
	  }
      }
    }

332
    const formula*
333
    mark_tools::mark_concat_ops(const formula* f)
334
    {
335
336
337
338
      f2f_map::iterator i = markops_.find(f);
      if (i != markops_.end())
	return i->second->clone();

339
      f->accept(*markvisitor_);
340

341
      const formula* r = down_cast<mark_visitor*>(markvisitor_)->result();
342
343
      markops_[f->clone()] = r->clone();
      return r;
344
345
    }

346
    const formula*
347
    mark_tools::simplify_mark(const formula* f)
348
    {
349
350
351
352
353
354
355
      if (!f->is_marked())
	return f->clone();

      f2f_map::iterator i = simpmark_.find(f);
      if (i != simpmark_.end())
	return i->second->clone();

356
      f->accept(*simpvisitor_);
357

358
359
      const formula* r =
	down_cast<simplify_mark_visitor*>(simpvisitor_)->result();
360
361
      simpmark_[f->clone()] = r->clone();
      return r;
362
363
364
365
    }

  }
}