summaryrefslogtreecommitdiff
path: root/omegalib/omega/src/pres_beaut.cc
blob: c23962abd30c63ea94fde74aa556e06e220469b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
99
100
101
102
103
104
105
106
107
108
109
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <omega/pres_tree.h>
#include <omega/pres_conj.h>
#include <omega/Relation.h>
#include <omega/omega_i.h>

/////////////////////////
//                     //
// Beautify functions  //
//                     //
/////////////////////////


namespace omega {

//
// f & true = f
// f | false = f
// f1 & f2 & ... & fn = Conjunct(f1,f2,...,fn)
//

void Rel_Body::beautify() {
  assert(children().length()==1);
  set_parent(NULL,this);

  skip_finalization_check++;
  formula()->beautify();
  Formula *child = formula();
  if(child->node_type()==Op_And && child->children().empty()) {
    remove_child(child);
    delete child;
    add_conjunct();    
  }
  skip_finalization_check--;

  if(pres_debug) {
    fprintf(DebugFile, "\n=== Beautified TREE ===\n");
    prefix_print(DebugFile);
  }
  assert(children().length()==1);
}

void Formula::beautify() {
  // copy list of children, as they may be removed as we work
  List<Formula*> kiddies = myChildren;

  for(List_Iterator<Formula*> c(kiddies); c; c++)
    (*c)->beautify();
}

void F_Exists::beautify() {
  Formula::beautify();
  assert(children().length()==1);

  if(myLocals.empty()) {
    // exists( : ***)
    parent().remove_child(this);
    parent().add_child(children().remove_front());
    delete this;
  }
  else if (children()[1]->node_type() == Op_And && children()[1]->children().empty()) {
    // exists(*** : TRUE) --chun 6/4/2008
    parent().remove_child(this);
    parent().add_child(children().remove_front());
    delete this;
  }
  else {
    Formula *child = children().front();
    if(child->node_type() == Op_Conjunct || child->node_type() == Op_Exists) {
      child->push_exists(myLocals);
      parent().remove_child(this);
      parent().add_child(child);
      children().remove_front();
      delete this;
    }
  }
}

void F_Forall::beautify() {
  Formula::beautify();
  assert(children().length()==1);

  if(myLocals.empty()) {
    parent().remove_child(this);
    parent().add_child(children().remove_front());
    delete this;
  }
}


//
// The Pix-free versions of beautify for And and Or are a bit
// less efficient  than the previous code,  as we keep moving
// things from one list to another, but they do not depend on
// knowing that a Pix is valid after the list is updated, and
// they can always be optimized later if necessary.
// 

void F_Or::beautify() {
  Formula::beautify();

  List<Formula*> uglies, beauties;
  uglies.join(children());  assert(children().empty());
#if ! defined NDEBUG
  foreach(c,Formula*,uglies,c->set_parent(0));
#endif

  while(!uglies.empty()) {
    Formula *f = uglies.remove_front();
    if(f->node_type()==Op_And && f->children().empty() ) {
      // smth | true = true
      foreach(c,Formula*,uglies,delete c);
      foreach(c,Formula*,beauties,delete c);
      parent().remove_child(this);
      parent().add_and();
      delete f;
      delete this;
      return;
    }
    else if(f->node_type()==Op_Or) {
      // OR(f[1-m], OR(c[1-n])) = OR(f[1-m], c[1-n])
#if ! defined NDEBUG
      foreach(c,Formula*,f->children(),c->set_parent(0));
#endif
      uglies.join(f->children());
      delete f;
    }
    else
      beauties.prepend(f);
  }

  if(beauties.length()==1) {
    beauties.front()->set_parent(&parent());
    parent().remove_child(this);
    parent().add_child(beauties.remove_front());
    delete this;
  }
  else {
    foreach(c,Formula*,beauties,(c->set_parent(this),
                                 c->set_relation(relation())));
    assert(children().empty());
    children().join(beauties);
  }
}

void F_And::beautify() {
  Formula::beautify();

  Conjunct *conj = NULL;

  List<Formula*> uglies, beauties;
  uglies.join(children());  assert(children().empty());
#if ! defined NDEBUG
  foreach(c,Formula*,uglies,c->set_parent(0));
#endif

  while(!uglies.empty()) {
    Formula *f = uglies.remove_front();
    if (f->node_type() == Op_Conjunct) {
      if(conj==NULL)
        conj = f->really_conjunct();
      else {
        Conjunct *conj1 = merge_conjs(conj, f->really_conjunct(), MERGE_REGULAR);
        delete f;
        delete conj;
        conj = conj1;
      }
    }
    else if(f->node_type()==Op_Or && f->children().empty()) {
      // smth & false = false
      foreach(c,Formula*,uglies,delete c);
      foreach(c,Formula*,beauties,delete c);
      parent().remove_child(this);
      parent().add_or();
      delete f;
      delete conj;
      delete this;
      return;
    }
    else if(f->node_type()==Op_And) {
      //  AND(f[1-m], AND(c[1-n])) = AND(f[1-m], c[1-n])
#if ! defined NDEBUG
      foreach(c,Formula*,f->children(),c->set_parent(0));
#endif
      uglies.join(f->children());
      delete f;
    }
    else
      beauties.prepend(f);
  }

  if(conj!=NULL)
    beauties.prepend(conj);

  if(beauties.length()==1) {
    beauties.front()->set_parent(&parent());
    parent().remove_child(this);
    parent().add_child(beauties.remove_front());
    delete this;
  }
  else {
    foreach(c,Formula*,beauties,(c->set_parent(this),
                                 c->set_relation(relation())));
    assert(children().empty());
    children().join(beauties);
  }
}

void F_Not::beautify() {
  Formula::beautify();
  assert(children().length()==1);
  Formula *child = children().front();

  if(child->node_type()==Op_And && child->children().empty()) {
    // Not TRUE = FALSE
    parent().remove_child(this);
    parent().add_or();
    delete this;
  }
  else if (child->node_type()==Op_Or && child->children().empty()) {
    // Not FALSE = TRUE
    parent().remove_child(this);
    parent().add_and();
    delete this;
  }
}

void Conjunct::beautify() {
  if(is_true()) {
    parent().remove_child(this);
    parent().add_and();
    delete this;
  }
}

} // namespace