File: pcl_analysis.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 (373 lines) | stat: -rw-r--r-- 9,767 bytes parent folder | download | duplicates (2)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*-----------------------------------------------------------------------

File  : pcl_analysis.c

Author: Stephan Schulz

Contents

  Functions for performing some analysis on PCL files (primarily for
  reenabling the old learning code with the new output format).

  Copyright 2004 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> Tue Feb  3 23:28:50 CET 2004
    New

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

#include "pcl_analysis.h"



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


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


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


/*-----------------------------------------------------------------------
//
// Function: pcl_example_cmp()
//
//   Compare two PCL steps as follows: All proof steps are equal and
//   smaller than all non-proof steps. Non-proof steps are compared by
//   gen_ref/(sim_ref+1).
//
// Global Variables:
//
// Side Effects    :
//
/----------------------------------------------------------------------*/

int pcl_example_cmp(const void* ex1, const void* ex2)
{
   const IntOrP* e1 = (const IntOrP*) ex1;
   const IntOrP* e2 = (const IntOrP*) ex2;
   PCLStep_p s1, s2;
   double w1, w2;

   s1 = e1->p_val;
   s2 = e2->p_val;

   if(PCLStepQueryProp(s1, PCLIsProofStep))
   {
      if(PCLStepQueryProp(s2, PCLIsProofStep))
      {
         return 0;
      }
      return -1;
   }
   else if(PCLStepQueryProp(s2, PCLIsProofStep))
   {
      return 1;
   }
   assert(!PCLStepQueryProp(s2, PCLIsProofStep));
   assert(!PCLStepQueryProp(s1, PCLIsProofStep));
   w1 = s1->useless_gen_refs/(float)(s1->useless_simpl_refs+1);
   w2 = s2->useless_gen_refs/(float)(s2->useless_simpl_refs+1);
   if(w1<w2)
   {
      return 1;
   }
   if(w1>w2)
   {
      return -1;
   }
   return 0;
}


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


/*-----------------------------------------------------------------------
//
// Function: PCLExprProofDistance()
//
//   Find the longest inference chain from the nearest proof clause
//   referenced in the expression. If no proof clause is among its
//   ancestors, return LONG_MAX. Assumes that proof clauses are
//   marked!
//
// Global Variables: -
//
// Side Effects    : Sets proof_distance in parents (indirectly)
//
/----------------------------------------------------------------------*/

long PCLExprProofDistance(PCLProt_p prot, PCLExpr_p expr)
{
   long res = 0, tmp;
   PCLStep_p step;
   int i;

   switch(expr->op)
   {
   case PCLOpNoOp:
    assert(false);
    break;
   case PCLOpInitial:
         res = PCL_PROOF_DIST_DEFAULT;
    break;
   case PCLOpQuote:
    step = PCLProtFindStep(prot,PCLExprArg(expr,0));
         res = PCLStepProofDistance(prot, step);
    break;
   default:
    for(i=0; i<expr->arg_no; i++)
    {
            tmp = PCLExprProofDistance(prot, PCLExprArg(expr,i));
       res = MAX(res,tmp);
    }
         if(res!=PCL_PROOF_DIST_INFINITY)
         {
            res++;
         }
    break;
   }
   return res;
}


/*-----------------------------------------------------------------------
//
// Function: PCLStepProofDistance()
//
//   Find the longest inference chain from the nearest proof clause
//   referenced in the steps expression (or 0 if step is proof
//   step). If no proof clause is among its
//   ancestors, return LONG_MAX. Assumes that proof clauses are
//   marked! Non-proof initial clauses get PCL_PROOF_DIST_DEFAULT.
//
// Global Variables: -
//
// Side Effects    : Sets proof_distance in parents and step
//
/----------------------------------------------------------------------*/


long PCLStepProofDistance(PCLProt_p prot, PCLStep_p step)
{
   if(step->proof_distance == PCL_PROOF_DIST_UNKNOWN)
   {
      if(PCLStepQueryProp(step,PCLIsProofStep))
      {
         step->proof_distance = 0;
      }
      else
      {
         step->proof_distance = PCLExprProofDistance(prot,step->just);
      }
   }
   return step->proof_distance;
}



/*-----------------------------------------------------------------------
//
// Function: PCLProtProofDistance()
//
//   Compute the proof distance for all steps in protocol. Assumes
//   that proof steps are already identified.
//
// Global Variables: -
//
// Side Effects    : Sets proof_distance in all steps.
//
/----------------------------------------------------------------------*/

void PCLProtProofDistance(PCLProt_p prot)
{
   PCLStep_p step;
   PStackPointer i;

   PCLProtSerialize(prot);

   for(i=0; i<PStackGetSP(prot->in_order); i++)
   {
      step = PStackElementP(prot->in_order, i);
      PCLStepProofDistance(prot, step);
   }
}


/*-----------------------------------------------------------------------
//
// Function: PCLExprUpdateGRefs()
//
//   Update the reference counters in all parents of expr
//   appropriately.
//
// Global Variables: -
//
// Side Effects    : See above.
//
/----------------------------------------------------------------------*/

void PCLExprUpdateGRefs(PCLProt_p prot, PCLExpr_p expr, bool proofstep)
{
   int i;
   PCLStep_p parent;

   switch(expr->op)
   {
   case PCLOpInitial:
   case PCLOpQuote:
   case PCLOpClauseNormalize:
         break;
   case PCLOpParamod:
   case PCLOpSimParamod:
   case PCLOpEResolution:
   case PCLOpEFactoring:
   case PCLOpSplitClause:
         for(i=0; i<expr->arg_no; i++)
    {
            parent = PCLExprGetQuotedArg(prot,expr, i);
            if(parent)
            {
               if(proofstep)
               {
                  parent->contrib_gen_refs++;
               }
               else
               {
                  parent->useless_gen_refs++;
               }
            }
            else
            {
               PCLExprUpdateGRefs(prot, PCLExprArg(expr,i), proofstep);
            }
         }
         break;
   case PCLOpSimplifyReflect:
   case PCLOpContextSimplifyReflect:
   case PCLOpRewrite:
   case PCLOpURewrite:
   case PCLOpACResolution:
         PCLExprUpdateGRefs(prot, PCLExprArg(expr,0), proofstep);
         for(i=1; i<expr->arg_no; i++)
    {
            parent = PCLExprGetQuotedArg(prot,expr, i);
            if(parent)
            {
               if(proofstep)
               {
                  parent->contrib_simpl_refs++;
               }
               else
               {
                  parent->useless_simpl_refs++;
               }
            }
            else
            {
               PCLExprUpdateGRefs(prot, PCLExprArg(expr,i), proofstep);
            }
         }
         break;
   default:
         /* These are unknown inferences (typcally FOF), we ignore
            them. */
         break;
   }
}


/*-----------------------------------------------------------------------
//
// Function: PCLProtUpdateGRefs()
//
//   For all steps, mark how often they are used to generate or
//   simplify proof or non-proof clauses.  Assumes
//   that proof steps are already identified.
//
// Global Variables: -
//
// Side Effects    : Updates reference counters
//
/----------------------------------------------------------------------*/

void PCLProtUpdateGRefs(PCLProt_p prot)
{
   PCLStep_p step;
   PStackPointer i;

   PCLProtSerialize(prot);

   for(i=0; i<PStackGetSP(prot->in_order); i++)
   {
      step = PStackElementP(prot->in_order, i);
      PCLStepUpdateGRefs(prot, step);
   }
}


/*-----------------------------------------------------------------------
//
// Function: PCLProtSelectExamples()
//
//   Select examples for pattern-based learning. Selects all proof
//   clauses and up to neg_examples negative examples. Negative
//   examples are selected by ratio of generating to simplifying
//   applications (generating  bad, simplification good). Returns
//   number of steps selected.
//
// Global Variables: -
//
// Side Effects    : Temporarily rearranges the protocol, sets
//                   PCLIsExample in selected steps.
//
/----------------------------------------------------------------------*/

long PCLProtSelectExamples(PCLProt_p prot, long neg_examples)
{
   PCLStep_p step;
   PStackPointer i;

   PCLProtSerialize(prot); /* Ensure its all on the stack */
   prot->is_ordered = false;
   qsort(prot->in_order->stack,
         PStackGetSP(prot->in_order),
         sizeof(IntOrP),
         pcl_example_cmp);
   for(i=0; (i<PStackGetSP(prot->in_order))&&(neg_examples>0); i++)
   {
      step = PStackElementP(prot->in_order, i);
      if(PCLStepQueryProp(step, PCLIsFOFStep))
      {
         continue; /* We only consider clauses */
      }
      PCLStepSetProp(step, PCLIsExample);
      if(!PCLStepQueryProp(step, PCLIsProofStep))
      {
         neg_examples--;
      }
   }
   return i;
}

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