File: ccl_unit_simplify.c

package info (click to toggle)
eprover 2.6%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 21,288 kB
  • sloc: ansic: 331,111; csh: 12,026; python: 10,178; awk: 5,825; makefile: 461; sh: 389
file content (343 lines) | stat: -rw-r--r-- 9,498 bytes parent folder | download
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
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
/*-----------------------------------------------------------------------

File  : ccl_unit_simplify.c

Author: Stephan Schulz

Contents

  Functions for doing unit cut-off and unit-subsumption with indexed
  mixed clause sets.

Copyright 1998-2011 by the author.
  This code is released under the GNU General Public Licence and
  the GNU Lesser General Public License.
  See the file COPYING in the main E directory for details..
  Run "eprover -h" for contact information.

Changes

<1> Sun Jun 23 02:00:52 CEST 2002
    New

-----------------------------------------------------------------------*/

#include "ccl_unit_simplify.h"



/*---------------------------------------------------------------------*/
/*                        Global Variables                             */
/*---------------------------------------------------------------------*/

char* UnitSimplifyNames[]=
{
   "NoSimplify",
   "TopSimplify",
   "FullSimplify",
   NULL
};

const SimplifyRes SIMPLIFY_FAILED = {.pos = NULL, .remaining_args = MATCH_FAILED};

/*---------------------------------------------------------------------*/
/*                      Forward Declarations                           */
/*---------------------------------------------------------------------*/


/*---------------------------------------------------------------------*/
/*                         Internal Functions                          */
/*---------------------------------------------------------------------*/





/*---------------------------------------------------------------------*/
/*                         Exported Functions                          */
/*---------------------------------------------------------------------*/

/*-----------------------------------------------------------------------
//
// Function: FindTopSimplifyingUnit()
//
//   Find a unit s=t (or s!=t) in units such that sigma(s)=t1 and
//   sigma(t)=t2 for some sigma.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

SimplifyRes FindTopSimplifyingUnit(ClauseSet_p units, Term_p t1,
               Term_p t2)
{
   Subst_p     subst = SubstAlloc();
   ClausePos_p pos;
   SimplifyRes res = SIMPLIFY_FAILED;

   assert(TermStandardWeight(t1) == TermWeight(t1,DEFAULT_VWEIGHT,DEFAULT_FWEIGHT));
   assert(TermStandardWeight(t2) == TermWeight(t2,DEFAULT_VWEIGHT,DEFAULT_FWEIGHT));
   assert(units && units->demod_index);

   PDTreeSearchInit(units->demod_index, t1, PDTREE_IGNORE_NF_DATE, false);

   MatchRes_p mi;
   while((mi = PDTreeFindNextDemodulator(units->demod_index, subst)))
   {
      pos = mi->pos;

      if(mi->remaining_args == 0 && SubstMatchComplete(ClausePosGetOtherSide(pos), t2, subst))
      {
        // if the problem is not HO, we match completely.
        assert(pos->clause->set == units);
        res = (SimplifyRes){.pos = pos, .remaining_args = mi->remaining_args};
        MatchResFree(mi);
        break;
      }
   }
   PDTreeSearchExit(units->demod_index);
   SubstDelete(subst);
   return res;
}


/*-----------------------------------------------------------------------
//
// Function: FindSignedTopSimplifyingUnit()
//
//   Find a unit s=t (or s!=t) in units such that sigma(s)=t1 and
//   sigma(t)=t2 for some sigma. Return only clauses with sign sign.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/
SimplifyRes FindSignedTopSimplifyingUnit(ClauseSet_p units, Term_p t1,
                Term_p t2, bool sign)
{
   Subst_p     subst = SubstAlloc();
   ClausePos_p pos;
   SimplifyRes res = SIMPLIFY_FAILED;

   assert(TermStandardWeight(t1) == TermWeight(t1,DEFAULT_VWEIGHT,DEFAULT_FWEIGHT));
   assert(TermStandardWeight(t2) == TermWeight(t2,DEFAULT_VWEIGHT,DEFAULT_FWEIGHT));
   assert(units && units->demod_index);

   PDTreeSearchInit(units->demod_index, t1, PDTREE_IGNORE_NF_DATE, false);

   MatchRes_p mi;
   while((mi = PDTreeFindNextDemodulator(units->demod_index, subst)))
   {
      pos = mi->pos;
      if( mi->remaining_args == 0
          && EQUIV(EqnIsPositive(pos->literal), sign)
          && (SubstMatchComplete(ClausePosGetOtherSide(pos), t2, subst)))
      {
        // if the problem is not HO, we match completely.
        assert(pos->clause->set == units);
        res = (SimplifyRes){.pos = pos, .remaining_args = mi->remaining_args};
        MatchResFree(mi);
        break;
      }
      MatchResFree(mi);
   }
   PDTreeSearchExit(units->demod_index);
   SubstDelete(subst);
   return res;
}



/*-----------------------------------------------------------------------
//
// Function: RemainingArgsSame()
//
//   Determines if the trailing arugments are the same in both of the
//   terms. 
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/
__inline__ bool RemainingArgsSame(Term_p t1, Term_p t2, SimplifyRes *res)
{
   int remains = res->remaining_args;
   assert(problemType != PROBLEM_FO || !remains);
   while(remains)
   {
      if(t1->args[t1->arity - remains] != t2->args[t2->arity - remains])
      {
         return false;
      }
      remains --;
   }
   assert(!SimplifyFailed(*res));
   return true;
}


/*-----------------------------------------------------------------------
//
// Function: FindSimplifyingUnit()
//
//   Return a unit clause with from set that can simplify or subsume
//   t1=t2.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

SimplifyRes FindSimplifyingUnit(ClauseSet_p set, Term_p t1, Term_p t2,
            bool positive_only)
{
   Term_p   tmp1, tmp2 = NULL;
   int      i;
   SimplifyRes res = SIMPLIFY_FAILED;

   if(positive_only)
   {
      res = FindSignedTopSimplifyingUnit(set, t1, t2, true);
   }
   else
   {
      res = FindTopSimplifyingUnit(set, t1, t2);
   }
   
   if(!SimplifyFailed(res))
   {
      return RemainingArgsSame(t1, t2, &res) ? res : SIMPLIFY_FAILED;
   }

   while(SimplifyFailed(res))
   {
      if(TermIsTopLevelVar(t1) || TermIsTopLevelVar(t2) || 
         t1->f_code != t2->f_code || !t1->arity)
      {
        break;
      }
      tmp1 = NULL; /* Used to determine if another position has
            already been found */

      assert(t1!=t2);
      for(i=0; i<t1->arity; i++)
      {
         if(t1->args[i] != t2->args[i])
         {
            if(tmp1)
            {
               tmp2 = NULL; /* Signal that more than one conflict
                     exists */
               break;
            }
            tmp1 = t1->args[i];
            tmp2 = t2->args[i];
         }
      }
      if(!tmp2)
      {
         break;
      }
      t1 = tmp1;
      t2 = tmp2;
      res = FindSignedTopSimplifyingUnit(set, t1, t2, true);
      if(problemType == PROBLEM_HO && !SimplifyFailed(res))
      {
         return RemainingArgsSame(t1, t2, &res) ? res : SIMPLIFY_FAILED;
      }
   }
   return res;
}





/*-----------------------------------------------------------------------
//
// Function: ClauseSimplifyWithUnitSet()
//
//   Simplify a clause with the (indexed) units from set. Performs
//   simplify-reflect and subsumption steps. simplify-reflect is
//   controlled by the value of how. If clause is subsumed by a unit,
//   return false, otherwise return true.
//
// Global Variables: -
//
// Side Effects    : Changes clause, may cause output.
//
/----------------------------------------------------------------------*/

bool ClauseSimplifyWithUnitSet(Clause_p clause, ClauseSet_p unit_set,
                               UnitSimplifyType how)
{
   Eqn_p *handle;
   SimplifyRes res;

   assert(clause);
   assert(unit_set && unit_set->demod_index);
   assert(how);

   handle = &(clause->literals);
   while(*handle)
   {
      if(how == TopLevelUnitSimplify)
      {
         res = FindTopSimplifyingUnit(unit_set,
                                       (*handle)->lterm,
                                       (*handle)->rterm);
      }
      else
      {
         res = FindSimplifyingUnit(unit_set,
                    (*handle)->lterm,
                    (*handle)->rterm, false);
      }
      if(!SimplifyFailed(res))
      {
         ClausePos_p pos = res.pos;
         assert(ClauseIsUnit(pos->clause));
         if(EQUIV(EqnIsPositive(*handle),
             EqnIsPositive(pos->literal)))
         {
            DocClauseQuote(GlobalOut, OutputLevel, 6, clause,
                 "subsumed by unprocessed unit",
                 pos->clause);
            if(!ClauseIsUnit(clause)&&
               ClauseStandardWeight(clause)==ClauseStandardWeight(pos->clause))
            {
               ClauseSetProp(pos->clause, CPIsProtected);
            }
            ClauseSetProp(pos->clause, ClauseQueryProp(clause, CPIsSOS));
            return false;
         }
         ClauseDelProp(clause, CPLimitedRW);
         ClauseRemoveLiteralRef(clause, handle);
         DocClauseModification(GlobalOut, OutputLevel, clause,
                     inf_simplify_reflect, pos->clause,
                     NULL, "cut with unprocessed unit");
      }
      else
      {
         handle = &((*handle)->next);
      }
   }
   return true;
}


/*---------------------------------------------------------------------*/
/*                        End of File                                  */
/*---------------------------------------------------------------------*/