File: pcl_lemmas.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 (659 lines) | stat: -rw-r--r-- 17,318 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*-----------------------------------------------------------------------

File  : pcl_lemmas.c

Author: Stephan Schulz

Contents

  Data structures and functions for the detection of lemmas in PCL2
  protocol.

  Copyright 2003 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> Mon Jun 16 13:06:20 CEST 2003
    New

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

#include "pcl_lemmas.h"



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


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


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


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

/*-----------------------------------------------------------------------
//
// Function: LemmaParamAlloc()
//
//   Allocate an initialized parameter block for the lemma detection
//   algorithm.
//
// Global Variables: -
//
// Side Effects    : Memory allocation
//
/----------------------------------------------------------------------*/

LemmaParam_p LemmaParamAlloc(void)
{
   LemmaParam_p handle = LemmaParamCellAlloc();

   handle->tree_base_weight   = LEMMA_TREE_BASE_W;
   handle->act_pm_w           = LEMMA_ACT_PM_W;
   handle->o_gen_w            = LEMMA_O_GEN_W;
   handle->act_simpl_w        = LEMMA_ACT_SIMPL_W;
   handle->pas_simpl_w        = LEMMA_PAS_SIMPL_W;
   handle->proof_tree_w       = LEMMA_PROOF_TREE_W;
   handle->proof_dag_w        = LEMMA_PROOF_DAG_W;
   handle->size_base_weight   = LEMMA_SIZE_BASE_W;
   handle->horn_bonus         = LEMMA_HORN_BONUS_W;

   return handle;
}


/*-----------------------------------------------------------------------
//
// Function: InferenceWeightsAlloc()
//
//   Allocate an inference weight parameter data structure,
//   initialized with default values.
//
// Global Variables: -
//
// Side Effects    : Memory allocation
//
/----------------------------------------------------------------------*/

InferenceWeight_p InferenceWeightsAlloc(void)
{
   InferenceWeight_p handle = InferenceWeightCellAlloc();

   (*handle)[PCLOpNoOp]            = PCL_OP_NOOP_WEIGHT;
   (*handle)[PCLOpInitial]         = PCL_OP_INITIAL_WEIGHT;
   (*handle)[PCLOpQuote]           = PCL_OP_QUOTE_WEIGHT;
   (*handle)[PCLOpParamod]         = PCL_OP_PARAMOD_WEIGHT;
   (*handle)[PCLOpSimParamod]      = PCL_OP_SIM_PARAMOD_WEIGHT;
   (*handle)[PCLOpEvalGC]          = PCL_OP_EVALGC_WEIGHT;
   (*handle)[PCLOpEResolution]     = PCL_OP_ERESOLUTION_WEIGHT;
   (*handle)[PCLOpEFactoring]      = PCL_OP_EFACTORING_WEIGHT;
   (*handle)[PCLOpSimplifyReflect] = PCL_OP_SIMPLIFYREFLECT_WEIGHT;
   (*handle)[PCLOpContextSimplifyReflect] = PCL_OP_CONTEXTSIMPLIFYREFLECT_WEIGHT;
   (*handle)[PCLOpACResolution]    = PCL_OP_ACRESOLUTION_WEIGHT;
   (*handle)[PCLOpRewrite]         = PCL_OP_REWRITE_WEIGHT;
   (*handle)[PCLOpURewrite]        = PCL_OP_UREWRITE_WEIGHT;
   (*handle)[PCLOpClauseNormalize] = PCL_OP_CLAUSENORMALIZE_WEIGHT;
   (*handle)[PCLOpSplitClause]     = PCL_OP_SPLITCLAUSE_WEIGHT;

  return handle;
}


/*-----------------------------------------------------------------------
//
// Function: PCLExprUpdateRefs()
//
//   Given a PCL expression, update the counter in all leaves according
//   to the inferences they directly paricipate in.
//
// Global Variables: -
//
// Side Effects    : Updates those counters.
//
/----------------------------------------------------------------------*/

void PCLExprUpdateRefs(PCLProt_p prot, PCLExpr_p expr)
{
   PCLStep_p handle;
   int i;

   /* printf("PCLExprUpdateRefs(%p %p), %ld\n", prot, expr,
      expr->arg_no);    PCLExprPrint(stdout, expr, false); printf("\n"); */

   switch(expr->op)
   {
   case PCLOpNoOp:
   case PCLOpInitial:
   case PCLOpQuote:
    /* Do nothing */
    break;
   case PCLOpParamod:
   case PCLOpSimParamod:
    assert(expr->arg_no == 2);
    if((handle = PCLExprGetQuotedArg(prot,expr,0)))
    {
       handle->other_generating_refs++;
    }
         else
         {
            PCLExprUpdateRefs(prot, PCLExprArg(expr,0));
         }
    if((handle = PCLExprGetQuotedArg(prot,expr,1)))
    {
       handle->active_pm_refs++;
    }
         else
         {
            PCLExprUpdateRefs(prot, PCLExprArg(expr,1));
         }
    break;
   case PCLOpEResolution:
   case PCLOpEFactoring:
   case PCLOpSplitClause: /* No idea if this one belongs here. but I
              have no better idea */
    if((handle = PCLExprGetQuotedArg(prot,expr,0)))
    {
       handle->other_generating_refs++;
    }
         else
         {
            PCLExprUpdateRefs(prot, PCLExprArg(expr,0));
         }
    break;
   case PCLOpSimplifyReflect:
   case PCLOpACResolution:
   case PCLOpRewrite:
   case PCLOpURewrite:
   case PCLOpClauseNormalize:
    if((handle = PCLExprGetQuotedArg(prot,expr,0)))
    {
       handle->passive_simpl_refs++;
    }
         else
         {
            PCLExprUpdateRefs(prot, PCLExprArg(expr,0));
         }
    for(i=1; i < expr->arg_no; i++)
    {
       if((handle = PCLExprGetQuotedArg(prot,expr,i)))
       {
          handle->active_simpl_refs++;
       }
            else
            {
               PCLExprUpdateRefs(prot, PCLExprArg(expr,i));
            }
    }
    break;
   default:
         /* Nothing happens for the FOF inference types */
    /* assert(false); */
         break;
   }
}

/*-----------------------------------------------------------------------
//
// Function: PCLStepUpdateRefs()
//
//   Update reference counters from this step to its parents.
//
// Global Variables: -
//
// Side Effects    : Just those
//
/----------------------------------------------------------------------*/

void PCLStepUpdateRefs(PCLProt_p prot, PCLStep_p step)
{
   /* PCLStepPrint(stdout, step);printf("\n"); */
   if(step->just->op == PCLOpQuote)
   {
      PCLStep_p handle = PCLProtFindStep(prot, PCLExprArg(step->just,0));
      handle->pure_quote_refs++;
   }
   PCLExprUpdateRefs(prot, step->just);
}



/*-----------------------------------------------------------------------
//
// Function: PCLProtUpdateRefs()
//
//   For all steps in prot update the reference counters
//
// Global Variables: -
//
// Side Effects    : As described
//
/----------------------------------------------------------------------*/

void PCLProtUpdateRefs(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);
      PCLStepUpdateRefs(prot,step);
   }
}



/*-----------------------------------------------------------------------
//
// Function: PCLStepLemmaCmpWrapper()
//
//   Wrapper for PCLStepLemmaCmp in IntOrP's
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

int PCLStepLemmaCmpWrapper(const void* s1, const void* s2)
{
   const IntOrP* step1 = (const IntOrP*) s1;
   const IntOrP* step2 = (const IntOrP*) s2;

   return PCLStepLemmaCmp(step1->p_val, step2->p_val);
}


/*-----------------------------------------------------------------------
//
// Function: PCLStepLemmaCmp()
//
//   Compare the lemma rating of two PCL steps, returning -1, 0, 1
//   depending on outcome.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

int PCLStepLemmaCmp(PCLStep_p step1, PCLStep_p step2)
{
   float r1, r2;

   r1 = step1->lemma_quality;
   r2 = step2->lemma_quality;
   if(r1 < r2)
   {
      return -1;
   }
   if(r2 < r1)
   {
      return 1;
   }
   return 0;
}


/*-----------------------------------------------------------------------
//
// Function: PCLExprProofSize()
//
//   Compute the proof size of the expression (including proofs for
//   children). Assumes that all previous steps already have correct
//   weight.
//
// Global Variables: -
//
// Side Effects    : May reorganize step tree.
//
/----------------------------------------------------------------------*/

long PCLExprProofSize(PCLProt_p prot, PCLExpr_p expr, InferenceWeight_p iw,
            bool use_lemmas)
{
   long res = 0,i;
   PCLStep_p step;

   assert(prot);
   assert(expr);

   if(expr->op == PCLOpQuote)
   {
      step = PCLProtFindStep(prot, PCLExprArg(expr,0));
      if(step)
      {
    res = PCLStepProofSize(prot, step, iw, use_lemmas);
      }
      else
      {
    PCLExprPrint(stderr, expr, false); fprintf(stderr, "  ");
    Error("Reference to non-existing step", SYNTAX_ERROR);
      }
   }
   else if(expr->op == PCLOpInitial)
   {
      res = (*iw)[expr->op];
   }
   else
   {
      res = (*iw)[expr->op];
      for(i=0; i<expr->arg_no; i++)
      {
    res+=PCLExprProofSize(prot, PCLExprArg(expr,i), iw, use_lemmas);
      }
   }
   return res;
}


/*-----------------------------------------------------------------------
//
// Function: PCLStepProofSize()
//
//   Compute and return the proof size of step. Caches result in the
//   step. If use_lemmas is true, always return 0
//
// Global Variables: -
//
// Side Effects    : As noted
//
/----------------------------------------------------------------------*/

long PCLStepProofSize(PCLProt_p prot, PCLStep_p step, InferenceWeight_p iw,
            bool use_lemmas)
{
   assert(prot);
   assert(step);

   if(step->proof_tree_size == PCLNoWeight)
   {
      assert(step->just);
      step->proof_tree_size = PCLExprProofSize(prot, step->just, iw, use_lemmas);
   }
   if(use_lemmas && PCLStepQueryProp(step, PCLIsLemma))
   {
      return 0;
   }
   else
   {
      return step->proof_tree_size;
   }
}


/*-----------------------------------------------------------------------
//
// Function: PCLProtComputeProofSize()
//
//   Compute proof weight for all steps. If use_lemmas is true, assume
//   proof weight of lemmas is 0 (but still record it).
//
// Global Variables: -
//
// Side Effects    : As noted
//
/----------------------------------------------------------------------*/

void PCLProtComputeProofSize(PCLProt_p prot, InferenceWeight_p iw,
              bool use_lemmas)
{
   PCLStep_p step;
   PStackPointer i;

   PCLProtSerialize(prot);

   for(i=0; i<PStackGetSP(prot->in_order); i++)
   {
      step = PStackElementP(prot->in_order, i);
      assert(step);
      PCLStepProofSize(prot, step, iw, use_lemmas);
   }
}


/*-----------------------------------------------------------------------
//
// Function: PCLStepComputeLemmaWeight()
//
//   Compute the lemma quality of a PCL step based on the information
//   stored in it.
//
// Global Variables: -
//
// Side Effects    : Caches the result in step->lemma_quality
//
/----------------------------------------------------------------------*/

float PCLStepComputeLemmaWeight(PCLProt_p prot, PCLStep_p step,
            LemmaParam_p params)
{
   float res;

   if(PCLStepQueryProp(step, PCLIsFOFStep))
   {
      res = 0;
   }
   else
   {
      res =
         (1.0+
          params->tree_base_weight +
          step->active_pm_refs        * params->act_pm_w +
          step->other_generating_refs * params->o_gen_w +
          step->active_simpl_refs     * params->act_simpl_w +
          step->passive_simpl_refs    * params->pas_simpl_w)
         *
         (1.0+
          step->proof_tree_size
            )
         /
         (params->size_base_weight+ClauseStandardWeight(step->logic.clause));

      if(ClauseIsHorn(step->logic.clause))
      {
         res = res*params->horn_bonus;
      }

      if((step->passive_simpl_refs || step->pure_quote_refs)
         &&
         !(step->active_pm_refs+step->other_generating_refs+step->active_simpl_refs) )
      {
         res = 0;
      }
      else
      {
         if(ClauseIsTrivial(step->logic.clause))
         {
            res = 0;
         }
      }
   }
   step->lemma_quality = res;
   return res;
}

/*-----------------------------------------------------------------------
//
// Function: PCLProtComputeLemmaWeights()
//
//   Compute the lemma rating for all steps. Return the step with the
//   best lemma rating.
//
// Global Variables: -
//
// Side Effects    : Caches lemma rating in the steps.
//
/----------------------------------------------------------------------*/

PCLStep_p PCLProtComputeLemmaWeights(PCLProt_p prot, LemmaParam_p params)
{
   PCLStep_p step, res=NULL;
   float best_rating = -1, current_rating;
   PStackPointer i;

   PCLProtSerialize(prot);

   for(i=0; i<PStackGetSP(prot->in_order); i++)
   {
      step = PStackElementP(prot->in_order, i);
      current_rating = PCLStepComputeLemmaWeight(prot, step, params);
      if(current_rating > best_rating && !PCLStepQueryProp(step, PCLIsLemma))
      {
    best_rating = current_rating;
    res = step;
      }
   }
   return res;
}


/*-----------------------------------------------------------------------
//
// Function: PCLProtSeqFindLemmas()
//
//   Mark all lemmas in procol which have a lemma rating of at least
//   quality_limit, but not more than max_number. Goes from first to
//   last step, taking already marked lemmas into account. Assumes
//   topologically ordered protocol (otherwise lemma ratings might be
//   off). Returns number of lemmas found.
//
// Global Variables: -
//
// Side Effects    : Marks lemmata, changes weights in steps.
//
/----------------------------------------------------------------------*/


long PCLProtSeqFindLemmas(PCLProt_p prot, LemmaParam_p params,
           InferenceWeight_p iw, long max_number,
           float quality_limit)
{
   PStackPointer i;
   PCLStep_p step;
   long res = 0;

   PCLProtSerialize(prot);
   PCLProtResetTreeData(prot, false);
   PCLProtUpdateRefs(prot);

   for(i=0; i<PStackGetSP(prot->in_order); i++)
   {
      step = PStackElementP(prot->in_order, i);
      PCLStepProofSize(prot, step, iw, true);
      if(PCLStepComputeLemmaWeight(prot, step, params) >= quality_limit)
      {
    PCLStepSetProp(step, PCLIsLemma);
    res++;
    if(res > max_number)
    {
       break;
    }
      }
   }
   return res;
}


/*-----------------------------------------------------------------------
//
// Function: PCLProtRecFindLemmas()
//
//   Recursively mark lemmas in prot as follows: Find the globally
//   best one, mark it. Recalculate all weight. Repeat. Return number
//   of lemmas found. Terminate if max_number lemmas have
//   been found or quality drops below quality limit.
//
// Global Variables: -
//
// Side Effects    : Changes lemma weights.
//
/----------------------------------------------------------------------*/

long PCLProtRecFindLemmas(PCLProt_p prot, LemmaParam_p params,
             InferenceWeight_p iw, long max_number,
             float quality_limit)
{
   long i;
   PCLStep_p lemma = NULL;

   PCLProtResetTreeData(prot, false);
   PCLProtUpdateRefs(prot);
   for(i=0; i<max_number; i++)
   {
      PCLProtResetTreeData(prot, true);
      PCLProtComputeProofSize(prot, iw, true);
      lemma = PCLProtComputeLemmaWeights(prot, params);
      if(!lemma || (lemma->lemma_quality < quality_limit))
      {
    break;
      }
      PCLStepSetProp(lemma, PCLIsLemma);
   }
   return i;
}


/*-----------------------------------------------------------------------
//
// Function: PCLProtFlatFindLemmas()
//
//   Find lemmas by computing scores once, sorting (by score) and
//   picking the best lemmas (down to quality_limit). Returns number
//   of lemmas selected.
//
// Global Variables: -
//
// Side Effects    : Changes weight, reorders protocol.
//
/----------------------------------------------------------------------*/

long PCLProtFlatFindLemmas(PCLProt_p prot, LemmaParam_p params,
            InferenceWeight_p iw, long max_number,
            float quality_limit)
{
   long i;
   PCLStep_p step = NULL;

   PCLProtResetTreeData(prot, false);
   PCLProtUpdateRefs(prot);
   PCLProtComputeProofSize(prot, iw, true);
   PCLProtComputeLemmaWeights(prot, params);
   PCLProtSerialize(prot);
   prot->is_ordered = false;
   PStackSort(prot->in_order, PCLStepLemmaCmpWrapper);

   max_number = MIN(max_number, PStackGetSP(prot->in_order));
   for(i=0; i<max_number; i++)
   {
      step = PStackPopP(prot->in_order);
      if(step->lemma_quality < quality_limit)
      {
    break;
      }
      PCLStepSetProp(step, PCLIsLemma);
   }
   return i;
}

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