basicreduce.cc 19.8 KB
Newer Older
1
2
// Copyright (C) 2008, 2009, 2010 Laboratoire de Recherche et
// Dveloppement de l'Epita (LRDE).
Guillaume Sadegh's avatar
Guillaume Sadegh committed
3
// Copyright (C) 2004, 2007 Laboratoire d'Informatique de
4
5
// Paris 6 (LIP6), dpartement Systmes Rpartis Coopratifs (SRC),
// Universit Pierre et Marie Curie.
martinez's avatar
martinez 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 "basicreduce.hh"
25
26
#include "ltlast/visitor.hh"
#include "ltlast/allnodes.hh"
martinez's avatar
martinez committed
27
28
29
30
31
32
#include <cassert>

namespace spot
{
  namespace ltl
  {
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    bool
    is_GF(const formula* f)
    {
      const unop* op = dynamic_cast<const unop*>(f);
      if (op && op->op() == unop::G)
	{
	  const unop* opchild = dynamic_cast<const unop*>(op->child());
	  if (opchild && opchild->op() == unop::F)
	    return true;
	}
      return false;
    }

    bool
    is_FG(const formula* f)
    {
      const unop* op = dynamic_cast<const unop*>(f);
      if (op && op->op() == unop::F)
	{
	  const unop* opchild = dynamic_cast<const unop*>(op->child());
	  if (opchild && opchild->op() == unop::G)
	    return true;
	}
      return false;
    }

59
    namespace
martinez's avatar
martinez committed
60
    {
61
62
63
      class basic_reduce_visitor: public visitor
      {
      public:
64

65
	basic_reduce_visitor(){}
66

67
	virtual ~basic_reduce_visitor(){}
martinez's avatar
martinez committed
68

69
70
71
72
73
	formula*
	result() const
	{
	  return result_;
	}
74

75
76
77
	void
	visit(atomic_prop* ap)
	{
78
	  formula* f = ap->clone();
79
80
	  result_ = f;
	}
81

82
83
84
85
86
	void
	visit(constant* c)
	{
	  result_ = c;
	}
87

88
89
90
91
92
93
94
95
96
	formula*
	param_case(multop* mo, unop::type op, multop::type op_child)
	{
	  formula* result;
	  multop::vec* res1 = new multop::vec;
	  multop::vec* resGF = new multop::vec;
	  unsigned mos = mo->size();
	  for (unsigned i = 0; i < mos; ++i)
	    if (is_GF(mo->nth(i)))
97
	      resGF->push_back(mo->nth(i)->clone());
98
	    else
99
	      res1->push_back(mo->nth(i)->clone());
100
	  mo->destroy();
101
102
103
104
	  multop::vec* res3 = new multop::vec;
	  if (!res1->empty())
	    res3->push_back(unop::instance(op,
					   multop::instance(op_child, res1)));
105
	  else
106
107
108
109
110
111
112
113
	    delete res1;
	  if (!resGF->empty())
	    res3->push_back(multop::instance(op_child, resGF));
	  else
	    delete resGF;
	  result = multop::instance(op_child, res3);
	  return result;
	}
114

115
116
117
118
119
120
121
122
123
124
125
126
	void
	visit(unop* uo)
	{
	  formula* f = uo->child();
	  result_ = basic_reduce(f);
	  multop* mo = 0;
	  unop* u = 0;
	  binop* bo = 0;
	  switch (uo->op())
	    {
	    case unop::Not:
	      result_ = unop::instance(unop::Not, result_);
127
	      return;
martinez's avatar
martinez committed
128

129
130
131
132
	    case unop::X:
	      // X(true) = true
	      // X(false) = false
	      if (dynamic_cast<constant*>(result_))
133
		return;
134

135
136
	      // XGF(f) = GF(f)
	      if (is_GF(result_))
137
		return;
138

139
140
141
142
143
144
145
146
	      // X(f1 & GF(f2)) = X(f1) & GF(F2)
	      // X(f1 | GF(f2)) = X(f1) | GF(F2)
	      mo = dynamic_cast<multop*>(result_);
	      if (mo)
		{
		  result_ = param_case(mo, unop::X, mo->op());
		  return;
		}
martinez's avatar
martinez committed
147

148
	      result_ = unop::instance(unop::X, result_);
martinez's avatar
martinez committed
149
150
	      return;

151
152
153
154
	    case unop::F:
	      // F(true) = true
	      // F(false) = false
	      if (dynamic_cast<constant*>(result_))
martinez's avatar
martinez committed
155
		return;
156

157
158
159
160
	      // FX(a) = XF(a)
	      u = dynamic_cast<unop*>(result_);
	      if (u && u->op() == unop::X)
		{
161
		  formula* res =
162
163
164
		    unop::instance(unop::X,
				   unop::instance(unop::F,
						  basic_reduce(u->child())));
165
		  u->destroy();
166
167
		  // FXX(a) = XXF(a) ...
		  result_ = basic_reduce(res);
168
		  res->destroy();
169
170
		  return;
		}
martinez's avatar
martinez committed
171

172
#if 0
173
	      // F(f1 & GF(f2)) = F(f1) & GF(F2)
174
175
176
177
178
179
180
181
182
183
184
185
186
	      //
	      // As is, these two formulae are translated into
	      // equivalent Bchi automata so the rewriting is
	      // useless.
	      //
	      // However when taken in a larger formulae such as F(f1
	      // & GF(f2)) | F(a & GF(b)), this rewriting used to
	      // produce (F(f1) & GF(f2)) | (F(a) & GF(b)), missing
	      // the opportunity to apply the F(E1)|F(E2) = F(E1|E2)
	      // rule which really helps the translation. F((f1 &
	      // GF(f2)) | (a & GF(b))) is indeed easier to translate.
	      //
	      // So let's not consider this rewriting rule.
187
188
189
190
191
192
	      mo = dynamic_cast<multop*>(result_);
	      if (mo && mo->op() == multop::And)
		{
		  result_ = param_case(mo, unop::F, multop::And);
		  return;
		}
193
#endif
194

195
196
	      result_ = unop::instance(unop::F, result_);
	      return;
197

198
199
200
201
	    case unop::G:
	      // G(true) = true
	      // G(false) = false
	      if (dynamic_cast<constant*>(result_))
202
		return;
martinez's avatar
martinez committed
203

204
205
206
207
208
209
	      // G(a R b) = G(b)
	      bo = dynamic_cast<binop*>(result_);
	      if (bo && bo->op() == binop::R)
		{
		  result_ = unop::instance(unop::G,
					   basic_reduce(bo->second()));
210
		  bo->destroy();
211
212
213
214
215
216
217
		  return;
		}

	      // GX(a) = XG(a)
	      u = dynamic_cast<unop*>(result_);
	      if (u && u->op() == unop::X)
		{
218
		  formula* res =
219
220
221
		    unop::instance(unop::X,
				   unop::instance(unop::G,
						  basic_reduce(u->child())));
222
		  u->destroy();
223
224
225
		  // GXX(a) = XXG(a) ...
		  // GXF(a) = XGF(a) = GF(a) ...
		  result_ = basic_reduce(res);
226
		  res->destroy();
227
228
229
230
231
232
233
234
235
236
237
238
239
		  return;
		}

	      // G(f1 | GF(f2)) = G(f1) | GF(F2)
	      mo = dynamic_cast<multop*>(result_);
	      if (mo && mo->op() == multop::Or)
		{
		  result_ = param_case(mo, unop::G, multop::Or);
		  return;
		}

	      result_ = unop::instance(unop::G, result_);
	      return;
240
241
242
243

	    case unop::Finish:
	      result_ = unop::instance(unop::Finish, result_);
	      return;
martinez's avatar
martinez committed
244
	    }
245
246
247
	  /* Unreachable code.  */
	  assert(0);
	}
248

249
250
251
252
253
	void
	visit(binop* bo)
	{
	  formula* f1 = bo->first();
	  formula* f2 = bo->second();
254
255
	  binop::type op = bo->op();
	  switch (op)
256
257
258
259
260
261
262
263
	    {
	    case binop::Xor:
	    case binop::Equiv:
	    case binop::Implies:
	      result_ = binop::instance(bo->op(),
					basic_reduce(f1),
					basic_reduce(f2));
	      return;
264
265
	    case binop::W:
	    case binop::M:
266
267
	    case binop::U:
	    case binop::R:
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
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
	      {
		f1 = basic_reduce(f1);
		f2 = basic_reduce(f2);

		// a W false = Ga
		if (op == binop::W && f2 == constant::false_instance())
		  {
		    result_ = unop::instance(unop::G, f1);
		    return;
		  }
		// a M true = Fa
		if (op == binop::M && f2 == constant::true_instance())
		  {
		    result_ = unop::instance(unop::F, f1);
		    return;
		  }
		// a U false = false
		// a U true = true
		// a R false = false
		// a R true = true
		// a W true = true
		// a M false = false
		if (dynamic_cast<constant*>(f2))
		  {
		    result_ = f2;
		    f1->destroy();
		    return;
		  }

		// X(a) U X(b) = X(a U b)
		// X(a) R X(b) = X(a R b)
		// X(a) W X(b) = X(a W b)
		// X(a) M X(b) = X(a M b)
		unop* fu1 = dynamic_cast<unop*>(f1);
		unop* fu2 = dynamic_cast<unop*>(f2);
		if (fu1 && fu2
		    && fu1->op() == unop::X
		    && fu2->op() == unop::X)
		  {
		    formula* ftmp = binop::instance(op,
						    basic_reduce(fu1->child()),
						    basic_reduce(fu2->child()));
		    result_ = unop::instance(unop::X, basic_reduce(ftmp));
		    f1->destroy();
		    f2->destroy();
		    ftmp->destroy();
		    return;
		  }

		if (op == binop::U || op == binop::W)
		  {
		    // a U Ga = Ga
		    // a W Ga = Ga
		    if (fu2 && fu2->op() == unop::G && fu2->child() == f1)
		      {
			result_ = f2;
			f1->destroy();
			return;
		      }

		    // a U (b | G(a)) = a W b
		    // a W (b | G(a)) = a W b
		    multop* fm2 = dynamic_cast<multop*>(f2);
		    if (fm2 && fm2->op() == multop::Or)
		      {
			int s = fm2->size();
			for (int i = 0; i < s; ++i)
			  {
			    unop* c = dynamic_cast<unop*>(fm2->nth(i));
			    if (c && c->op() == unop::G && c->child() == f1)
			      {
				multop::vec* v = new multop::vec;
				v->reserve(s - 1);
				int j;
				for (j = 0; j < i; ++j)
				  v->push_back(fm2->nth(j)->clone());
				// skip j=i
				for (++j; j < s; ++j)
				  v->push_back(fm2->nth(j)->clone());
				result_ =
				  binop::instance(binop::W, f1,
						  multop::instance(multop::Or,
								   v));
				f2->destroy();
				return;
			      }
			  }
		      }
		  }
357
358
359
360
361
362
363
364
365
366
		else if (op == binop::M || op == binop::R)
		  {
		    // a R Fa = Fa
		    // a M Fa = Fa
		    if (fu2 && fu2->op() == unop::F && fu2->child() == f1)
		      {
			result_ = f2;
			f1->destroy();
			return;
		      }
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
		    // a R (b & F(a)) = a M b
		    // a M (b & F(a)) = a M b
		    multop* fm2 = dynamic_cast<multop*>(f2);
		    if (fm2 && fm2->op() == multop::And)
		      {
			int s = fm2->size();
			for (int i = 0; i < s; ++i)
			  {
			    unop* c = dynamic_cast<unop*>(fm2->nth(i));
			    if (c && c->op() == unop::F && c->child() == f1)
			      {
				multop::vec* v = new multop::vec;
				v->reserve(s - 1);
				int j;
				for (j = 0; j < i; ++j)
				  v->push_back(fm2->nth(j)->clone());
				// skip j=i
				for (++j; j < s; ++j)
				  v->push_back(fm2->nth(j)->clone());
				result_ =
				  binop::instance(binop::M, f1,
						  multop::instance(multop::And,
								   v));
				f2->destroy();
				return;
			      }
			  }
		      }
		  }
397
398
399

		result_ = binop::instance(op, f1, f2);
		return;
martinez's avatar
martinez committed
400
	      }
401
402
403
404
405
	    }
	  /* Unreachable code.  */
	  assert(0);
	}

406
407
408
409
410
411
	void
	visit(automatop*)
	{
	  assert(0);
	}

412
413
414
415
416
417
418
419
420
421
422
	void
	visit(multop* mo)
	{
	  multop::type op = mo->op();
	  unsigned mos = mo->size();
	  multop::vec* res = new multop::vec;

	  multop::vec* tmpX = new multop::vec;
	  multop::vec* tmpU = new multop::vec;
	  multop::vec* tmpR = new multop::vec;
	  multop::vec* tmpFG = new multop::vec;
423
	  multop::vec* tmpF = new multop::vec;
424
	  multop::vec* tmpGF = new multop::vec;
425
	  multop::vec* tmpG = new multop::vec;
426
427
428
429
430
431
432
433
434
435
436
437

	  multop::vec* tmpOther = new multop::vec;

	  for (unsigned i = 0; i < mos; ++i)
	    res->push_back(basic_reduce(mo->nth(i)));

	  switch (op)
	    {
	    case multop::And:

	      for (multop::vec::iterator i = res->begin(); i != res->end(); i++)
		{
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
438
439
		  // An iteration of the loop may zero some later elements
		  // of the vector to mark them as redundant.  Skip them.
440
441
442
443
444
445
		  if (!*i)
		    continue;
		  unop* uo = dynamic_cast<unop*>(*i);
		  binop* bo = dynamic_cast<binop*>(*i);
		  if (uo)
		    {
446
		      if (uo->op() == unop::X)
447
448
			{
			  // Xa & Xb = X(a & b)
449
			  tmpX->push_back(uo->child()->clone());
450
451
452
453
454
			}
		      else if (is_FG(*i))
			{
			  // FG(a) & FG(b) = FG(a & b)
			  unop* uo2 = dynamic_cast<unop*>(uo->child());
455
			  tmpFG->push_back(uo2->child()->clone());
456
			}
457
458
		      else if (uo->op() == unop::G)
			{
459
			  // G(a) & G(b) = G(a & b)
460
461
			  tmpG->push_back(uo->child()->clone());
			}
462
463
464
465
		      else if (uo->op() == unop::F)
			{
			  // F(a) & (a R b) = a M b
			  // F(a) & (a M b) = a M b
466
467
			  // F(a) & (b W a) = b U a
			  // F(a) & (b U a) = b U a
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
			  formula* a = uo->child();
			  bool rewritten = false;
			  for (multop::vec::iterator j = i;
			       j != res->end(); ++j)
			    {
			      if (!*j)
				continue;
			      binop* b = dynamic_cast<binop*>(*j);
			      if (b && (b->op() == binop::R
					|| b->op() == binop::M)
				  && b->first() == a)
				{
				  binop* r =
				    binop::instance(binop::M,
						    a->clone(),
						    b->second()->clone());
				  tmpOther->push_back(r);
				  (*j)->destroy();
				  *j = 0;
				  rewritten = true;
				}
489
490
491
492
493
494
495
496
497
498
499
500
501
			      else if (b && (b->op() == binop::W
					     || b->op() == binop::U)
				       && b->second() == a)
				{
				  binop* r =
				    binop::instance(binop::U,
						    b->first()->clone(),
						    a->clone());
				  tmpOther->push_back(r);
				  (*j)->destroy();
				  *j = 0;
				  rewritten = true;
				}
502
503
504
505
			    }
			  if (!rewritten)
			    tmpOther->push_back(uo->clone());
			}
506
507
		      else
			{
508
			  tmpOther->push_back((*i)->clone());
509
510
511
512
			}
		    }
		  else if (bo)
		    {
513
		      if (bo->op() == binop::U || bo->op() == binop::W)
514
515
			{
			  // (a U b) & (c U b) = (a & c) U b
516
517
518
			  // (a U b) & (c W b) = (a & c) U b
			  // (a W b) & (c W b) = (a & c) W b
			  bool weak = true;
519
			  formula* ftmp = dynamic_cast<binop*>(*i)->second();
520
			  multop::vec* right = new multop::vec;
521
522
523
524
525
			  for (multop::vec::iterator j = i; j != res->end();
			       j++)
			    {
			      if (!*j)
				continue;
526
527
528
529
530
531
532
533
534
535
			      // (a U b) & Fb = a U b.
			      // (a W b) & Fb = a U b.
			      unop* uo2 = dynamic_cast<unop*>(*j);
			      if (uo2 && uo2->op() == unop::F
				  && uo2->child() == ftmp)
				{
				  (*j)->destroy();
				  *j = 0;
				  weak = false;
				}
536
			      binop* bo2 = dynamic_cast<binop*>(*j);
537
538
			      if (bo2 && (bo2->op() == binop::U
					  || bo2->op() == binop::W)
539
540
				  && ftmp == bo2->second())
				{
541
542
543
				  if (bo2->op() == binop::U)
				    weak = false;
				  right->push_back(bo2->first()->clone());
544
545
				  if (j != i)
				    {
546
				      (*j)->destroy();
547
548
549
550
551
				      *j = 0;
				    }
				}
			    }
			  tmpU
552
553
			    ->push_back(binop::instance(weak ?
							binop::W : binop::U,
554
555
							multop::
							instance(multop::
556
								 And, right),
557
							ftmp->clone()));
558
			}
559
		      else if (bo->op() == binop::R || bo->op() == binop::M)
560
561
			{
			  // (a R b) & (a R c) = a R (b & c)
562
563
			  // (a R b) & (a M c) = a M (b & c)
			  bool weak = true;
564
			  formula* ftmp = dynamic_cast<binop*>(*i)->first();
565
			  multop::vec* right = new multop::vec;
566
567
568
569
570
			  for (multop::vec::iterator j = i; j != res->end();
			       j++)
			    {
			      if (!*j)
				continue;
571
572
573
574
575
576
577
578
579
580
			      // (a R b) & Fa = a M b.
			      // (a M b) & Fa = a M b.
			      unop* uo2 = dynamic_cast<unop*>(*j);
			      if (uo2 && uo2->op() == unop::F
				  && uo2->child() == ftmp)
				{
				  (*j)->destroy();
				  *j = 0;
				  weak = false;
				}
581
			      binop* bo2 = dynamic_cast<binop*>(*j);
582
583
			      if (bo2 && (bo2->op() == binop::R
					  || bo2->op() == binop::M)
584
585
				  && ftmp == bo2->first())
				{
586
587
588
				  if (bo2->op() == binop::M)
				    weak = false;
				  right->push_back(bo2->second()->clone());
589
590
				  if (j != i)
				    {
591
				      (*j)->destroy();
592
593
594
595
596
				      *j = 0;
				    }
				}
			    }
			  tmpR
597
598
			    ->push_back(binop::instance(weak ?
							binop::R : binop::M,
599
							ftmp->clone(),
600
601
							multop::
							instance(multop::And,
602
								 right)));
603
604
605
			}
		      else
			{
606
			  tmpOther->push_back((*i)->clone());
607
608
609
610
			}
		    }
		  else
		    {
611
		      tmpOther->push_back((*i)->clone());
612
		    }
613
		  (*i)->destroy();
614
615
616
617
618
619
620
621
622
623
624
625
626
		}
	      break;

	    case multop::Or:

	      for (multop::vec::iterator i = res->begin(); i != res->end(); i++)
		{
		  if (!*i)
		    continue;
		  unop* uo = dynamic_cast<unop*>(*i);
		  binop* bo = dynamic_cast<binop*>(*i);
		  if (uo)
		    {
627
		      if (uo->op() == unop::X)
628
629
			{
			  // Xa | Xb = X(a | b)
630
			  tmpX->push_back(uo->child()->clone());
631
632
633
634
635
			}
		      else if (is_GF(*i))
			{
			  // GF(a) | GF(b) = GF(a | b)
			  unop* uo2 = dynamic_cast<unop*>(uo->child());
636
			  tmpGF->push_back(uo2->child()->clone());
637
			}
638
		      else if (uo->op() == unop::F)
639
			{
640
641
			  // F(a) | F(b) = F(a | b)
			  tmpF->push_back(uo->child()->clone());
642
			}
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
		      else if (uo->op() == unop::G)
			{
			  // G(a) | (a U b) = a W b
			  // G(a) | (a W b) = a W b
			  formula* a = uo->child();
			  bool rewritten = false;
			  for (multop::vec::iterator j = i;
			       j != res->end(); ++j)
			    {
			      if (!*j)
				continue;
			      binop* b = dynamic_cast<binop*>(*j);
			      if (b && (b->op() == binop::U
					|| b->op() == binop::W)
				  && b->first() == a)
				{
				  binop* r =
				    binop::instance(binop::W,
						    a->clone(),
						    b->second()->clone());
				  tmpOther->push_back(r);
				  (*j)->destroy();
				  *j = 0;
				  rewritten = true;
				}
			    }
			  if (!rewritten)
			    tmpOther->push_back(uo->clone());
			}
672
673
		      else
			{
674
			  tmpOther->push_back((*i)->clone());
675
676
677
678
			}
		    }
		  else if (bo)
		    {
679
		      if (bo->op() == binop::U || bo->op() == binop::W)
680
681
			{
			  // (a U b) | (a U c) = a U (b | c)
682
683
			  // (a W b) | (a U c) = a W (b | c)
			  bool weak = false;
684
			  formula* ftmp = bo->first();
685
			  multop::vec* right = new multop::vec;
686
687
688
689
690
			  for (multop::vec::iterator j = i; j != res->end();
			       j++)
			    {
			      if (!*j)
				continue;
691
692
693
694
695
696
697
698
699
700
			      // (a U b) | Ga = a W b.
			      // (a W b) | Ga = a W b.
			      unop* uo2 = dynamic_cast<unop*>(*j);
			      if (uo2 && uo2->op() == unop::G
				  && uo2->child() == ftmp)
				{
				  (*j)->destroy();
				  *j = 0;
				  weak = true;
				}
701
			      binop* bo2 = dynamic_cast<binop*>(*j);
702
703
			      if (bo2 && (bo2->op() == binop::U ||
					  bo2->op() == binop::W)
704
705
				  && ftmp == bo2->first())
				{
706
707
708
				  if (bo2->op() == binop::W)
				    weak = true;
				  right->push_back(bo2->second()->clone());
709
710
				  if (j != i)
				    {
711
				      (*j)->destroy();
712
713
714
715
				      *j = 0;
				    }
				}
			    }
716
717
			  tmpU->push_back(binop::instance(weak ?
							  binop::W : binop::U,
718
							  ftmp->clone(),
719
720
							  multop::
							  instance(multop::Or,
721
								   right)));
722
			}
723
		      else if (bo->op() == binop::R || bo->op() == binop::M)
724
725
			{
			  // (a R b) | (c R b) = (a | c) R b
726
727
728
			  // (a R b) | (c M b) = (a | c) R b
			  // (a M b) | (c M b) = (a | c) M b
			  bool weak = false;
729
			  formula* ftmp = dynamic_cast<binop*>(*i)->second();
730
			  multop::vec* right = new multop::vec;
731
732
733
734
735
			  for (multop::vec::iterator j = i; j != res->end();
			       j++)
			    {
			      if (!*j)
				continue;
736
737
738
739
740
741
742
743
744
745
			      // (a R b) | Gb = a R b.
			      // (a M b) | Gb = a R b.
			      unop* uo2 = dynamic_cast<unop*>(*j);
			      if (uo2 && uo2->op() == unop::G
				  && uo2->child() == ftmp)
				{
				  (*j)->destroy();
				  *j = 0;
				  weak = true;
				}
746
			      binop* bo2 = dynamic_cast<binop*>(*j);
747
748
			      if (bo2 && (bo2->op() == binop::R
					  || bo2->op() == binop::M)
749
750
				  && ftmp == bo2->second())
				{
751
752
753
				  if (bo2->op() == binop::R)
				    weak = true;
				  right->push_back(bo2->first()->clone());
754
755
				  if (j != i)
				    {
756
				      (*j)->destroy();
757
758
759
760
761
				      *j = 0;
				    }
				}
			    }
			  tmpR
762
763
			    ->push_back(binop::instance(weak ?
							binop::R : binop::M,
764
765
							multop::
							instance(multop::Or,
766
								 right),
767
							ftmp->clone()));
768
769
770
			}
		      else
			{
771
			  tmpOther->push_back((*i)->clone());
772
773
774
775
			}
		    }
		  else
		    {
776
		      tmpOther->push_back((*i)->clone());
777
		    }
778
		  (*i)->destroy();
779
780
781
782
		}

	      break;
	    }
783

784
785
	  res->clear();
	  delete res;
786

787

788
789
790
791
	  if (tmpX && !tmpX->empty())
	    tmpOther->push_back(unop::instance(unop::X,
					       multop::instance(mo->op(),
								tmpX)));
792
	  else
793
	    delete tmpX;
794

795
796
797
798
799
800
801
802
803
804
805
806
807
	  if (!tmpF->empty())
	    tmpOther->push_back(unop::instance(unop::F,
					       multop::instance(mo->op(),
								tmpF)));
	  else
	    delete tmpF;

	  if (!tmpG->empty())
	    tmpOther->push_back(unop::instance(unop::G,
					       multop::instance(mo->op(),
								tmpG)));
	  else
	    delete tmpG;
808

809
	  if (!tmpU->empty())
810
	    tmpOther->push_back(multop::instance(mo->op(), tmpU));
811
	  else
812
	    delete tmpU;
813

814
	  if (!tmpR->empty())
815
	    tmpOther->push_back(multop::instance(mo->op(), tmpR));
816
	  else
817
	    delete tmpR;
818

819
	  if (!tmpGF->empty())
820
821
822
823
824
825
826
827
	    {
	      formula* ftmp
		= unop::instance(unop::G,
				 unop::instance(unop::F,
						multop::instance(mo->op(),
								 tmpGF)));
	      tmpOther->push_back(ftmp);
	    }
828
	  else
829
	    delete tmpGF;
830
831


832
	  if (!tmpFG->empty())
833
834
835
836
837
838
839
840
841
842
843
844
845
846
	    {
	      formula* ftmp = 0;
	      if (mo->op() == multop::And)
		ftmp
		  = unop::instance(unop::F,
				   unop::instance(unop::G,
						  multop::instance(mo->op(),
								   tmpFG)));
	      else
		ftmp
		  = unop::instance(unop::F,
				   multop::instance(mo->op(), tmpFG));
	      tmpOther->push_back(ftmp);
	    }
847
	  else
848
	    delete tmpFG;
849

850

851
	  result_ = multop::instance(op, tmpOther);
martinez's avatar
martinez committed
852

853
854
	  return;
	}
855

856
857
858
859
      protected:
	formula* result_;
      };
    }
martinez's avatar
martinez committed
860

861
862
    formula*
    basic_reduce(const formula* f)
martinez's avatar
martinez committed
863
    {
864
      basic_reduce_visitor v;
martinez's avatar
martinez committed
865
866
867
868
869
870
      const_cast<formula*>(f)->accept(v);
      return v.result();
    }

  }
}