File: cle_indexfunctions.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 (657 lines) | stat: -rw-r--r-- 17,423 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
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
/*-----------------------------------------------------------------------

  File  : cle_indexfunctions.c

  Author: Stephan Schulz

  Contents

  Functions and data types realizing simple index function for
  learning heuristics.

  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.

  Created: Wed Aug  4 18:10:31 MET DST 1999

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

#include "cle_indexfunctions.h"



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

char* IndexFunNames[] =
{
   "IndexNoIndex",
   "IndexDynamic",
   "IndexArity",
   "IndexSymbol",
   "IndexTop",
   "IndexAltTop",
   "IndexCSTop",
   "IndexESTop",
   "IndexIdentity",
   "IndexEmpty",
   NULL
};


static long index_counter = 0;

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


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

/*-----------------------------------------------------------------------
//
// Function: any_term_top()
//
//   Return a term top as specified by the parameters.
//
// Global Variables: -
//
// Side Effects    : By calling the TermTop-Functions.
//
/----------------------------------------------------------------------*/

static Term_p any_term_top(IndexType type, Term_p term, int depth,
                           VarBank_p freshvars)
{
   Term_p result;

   switch(type)
   {
   case IndexTop:
         result = TermTop(term, depth, freshvars);
         break;
   case IndexAltTop:
         result = AltTermTop(term, depth, freshvars);
         break;
   case IndexCSTop:
         result = CSTermTop(term, depth, freshvars);
         break;
   case IndexESTop:
         result = ESTermTop(term, depth, freshvars);
         break;
   default:
         assert(false && "Wrong index type!");
         result = NULL; /* Stiffle warning */
         break;
   }
   return result;
}


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

/*-----------------------------------------------------------------------
//
// Function: GetIndexType()
//
//   Given a string, return the proper index type or -1.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

int GetIndexType(char* name)
{
   int position = StringIndex(name, IndexFunNames);

   if(position < 1)
   {
      return position;
   }
   if(position == 1)
   {
      return IndexArity | IndexSymbol | IndexTop | IndexAltTop |
         IndexCSTop | IndexESTop | IndexIdentity;
   }
   return 1 << (position-2);
}


/*-----------------------------------------------------------------------
//
// Function: GetIndexName()
//
//   Return the name of an IndexType.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

char* GetIndexName(IndexType type)
{
   switch(type)
   {
   case IndexNoIndex:
         return IndexFunNames[0];
   case IndexArity:
         return IndexFunNames[2];
   case IndexSymbol:
         return IndexFunNames[3];
   case IndexTop:
         return IndexFunNames[4];
   case IndexAltTop:
         return IndexFunNames[5];
   case IndexCSTop:
         return IndexFunNames[6];
   case IndexESTop:
         return IndexFunNames[7];
   case IndexIdentity:
         return IndexFunNames[8];
   case IndexEmpty:
         return IndexFunNames[9];
   default:
         return IndexFunNames[1];
   }
}


/*-----------------------------------------------------------------------
//
// Function: IndexTermAlloc()
//
//   Return an initialized index term.
//
// Global Variables: -
//
// Side Effects    : Memory operations, gets reference on term
//
/----------------------------------------------------------------------*/

IndexTerm_p IndexTermAlloc(Term_p term, PatternSubst_p subst, long
                           key)
{
   IndexTerm_p handle = IndexTermCellAlloc();

   handle->term = term;
   handle->subst = subst;
   handle->key = key;

   return handle;
}


/*-----------------------------------------------------------------------
//
// Function: IndexTermFree()
//
//   Free a IndexTerm.
//
// Global Variables: -
//
// Side Effects    : Changes bank
//
/----------------------------------------------------------------------*/

void IndexTermFree(IndexTerm_p junk, TB_p bank)
{
   assert(junk);
   assert(bank);

   IndexTermCellFree(junk);
}


/*-----------------------------------------------------------------------
//
// Function: IndexTermCompareFun()
//
//   Compare two index terms (as patterns).
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

int IndexTermCompareFun(const void* term1, const void* term2)
{
   const IndexTerm_p t1 = (const IndexTerm_p) term1;
   const IndexTerm_p t2 = (const IndexTerm_p) term2;
   int res = 0;

   switch(PatternTermCompare(t1->subst, t1->term, t2->subst,
                             t2->term))
   {
   case to_uncomparable:
         assert(false && "Only total substitutions expected here!");
         break;
   case to_equal:
         break;
   case to_greater:
         res = 1;
         break;
   case to_lesser:
         res = -1;
         break;
   default:
         assert(false && "Unknown comparison result (?!?)");
         break;
   }
   return res;
}



/*-----------------------------------------------------------------------
//
// Function: TSMIndexAlloc()
//
//   Return an initialized index cell.
//
// Global Variables: -
//
// Side Effects    : Memory operations
//
/----------------------------------------------------------------------*/

TSMIndex_p TSMIndexAlloc(IndexType type, int depth, TB_p bank,
                         PatternSubst_p subst)
{
   TSMIndex_p handle = TSMIndexCellAlloc();
   assert(type != IndexNoIndex);

   handle->type = type;
   handle->depth = depth;
   handle->bank = bank;
   handle->subst = subst;
   handle->count = 0;
   handle->ident = index_counter++;

   switch(type)
   {
   case IndexArity:
         break;
   case IndexSymbol:
         assert(subst);
         handle->tree.n_index = NULL;
         break;
   case IndexTop:
   case IndexAltTop:
   case IndexCSTop:
   case IndexESTop:
         assert(depth > 0);
         assert(bank);
         assert(subst);
         handle->tree.t_index = NULL;
         break;
   case IndexIdentity:
         assert(bank);
         assert(subst);
         handle->tree.t_index = NULL;
         break;
   case IndexEmpty:
         break;
   default:
         assert(false && "Unknown index type");
         break;
   }
   return handle;
}


/*-----------------------------------------------------------------------
//
// Function: TSMIndexFree()
//
//   Free a TSMIndex.
//
// Global Variables: -
//
// Side Effects    : Memory operations, may change term bank.
//
/----------------------------------------------------------------------*/

void TSMIndexFree(TSMIndex_p junk)
{
   PStack_p stack;
   PTree_p  cell;
   assert(junk);

   switch(junk->type)
   {
   case IndexArity:
         break;
   case IndexSymbol:
         NumTreeFree(junk->tree.n_index);
         break;
   case IndexTop:
   case IndexAltTop:
   case IndexCSTop:
   case IndexESTop:
   case IndexIdentity:
         stack = PTreeTraverseInit(junk->tree.t_index);
         while((cell = PTreeTraverseNext(stack)))
         {
            IndexTermFree(cell->key, junk->bank);
         }
         PTreeTraverseExit(stack);
         PTreeFree(junk->tree.t_index);
         break;
   case IndexEmpty:
         break;
   default:
         assert(false && "Unknown index type");
         break;
   }
   TSMIndexCellFree(junk);
}


/*-----------------------------------------------------------------------
//
// Function: TSMIndexFind()
//
//   Return an index for term (-1 if no index exists);
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

long TSMIndexFind(TSMIndex_p index, Term_p term, PatternSubst_p subst)
{
   long          res = -1, key;
   IndexTermCell query;
   IndexTerm_p   found;
   NumTree_p     cell1;
   PTree_p       cell2;

   switch(index->type)
   {
   case IndexArity:
         res = term->arity;
         index->count = MAX(index->count, res+1);
         break;
   case IndexSymbol:
         /* printf("=Find in %ld =\n", index->ident);
            TermPrint(stdout, term, index->bank->sig, DEREF_NEVER);
            printf("\n");
            PatternTermPrint(stdout, subst, term,
            index->bank->sig); */
         if(TermIsVar(term) && VarIsAltVar(term))
         {
            key = term->f_code;
         }
         else
         {
            key = PatSymbValue(subst, term->f_code);
            assert(key);
         }
         cell1 = NumTreeFind(&(index->tree.n_index), key);
         if(cell1)
         {
            res = cell1->val1.i_val;
         }
         /* printf("\n===Key: %ld Res: %ld\n", key, res);  */
         break;
   case IndexTop:
   case IndexAltTop:
   case IndexCSTop:
   case IndexESTop:
         query.term  = any_term_top(index->type, term, index->depth,
                                    index->bank->vars);
         query.subst = subst;
         cell2 = PTreeObjFind(&(index->tree.t_index), &query,
                              IndexTermCompareFun);
         if(cell2)
         {
            found = cell2->key;
            res = found->key;
         }
         TermFree(query.term);
         break;
   case IndexIdentity:
         query.term  = term;
         query.subst = subst;
         cell2 = PTreeObjFind(&(index->tree.t_index), &query,
                              IndexTermCompareFun);
         if(cell2)
         {
            found = cell2->key;
            res = found->key;
         }
         break;
   case IndexEmpty:
         break;
   default:
         assert(false && "Unknown index type");
         break;
   }
   return res;
}


/*-----------------------------------------------------------------------
//
// Function: TSMIndexInsert()
//
//   Insert the term/index association into the index. This duplicates
//   some stuff from TSMIndexFind() to save on term copies. The
//   patterns substitution is taken to be consistent for all terms
//   inserted or to be inserted into index and is not passed
//   along. Returns index assigned.
//
// Global Variables: -
//
// Side Effects    : Changes index
//
/----------------------------------------------------------------------*/

long TSMIndexInsert(TSMIndex_p index, Term_p term)
{
   long          res=-1, key;
   IndexTermCell query;
   IndexTerm_p   entry;
   PTree_p       cell2;
   Term_p        shared;
   NumTree_p     cell, old;

   switch(index->type)
   {
   case IndexArity:
         res = term->arity;
         index->count = MAX(index->count, res+1);
         break;
   case IndexSymbol:
         /* printf("\n=Insert in %ld - term %ld=\n", index->ident, term->entry_no);
            TermPrint(stdout, term, index->bank->sig, DEREF_NEVER);
            printf("\n");
            PatternTermPrint(stdout, index->subst, term,
            index->bank->sig); */
         if(TermIsVar(term) && VarIsAltVar(term))
         {
            key = term->f_code;
         }
         else
         {
            key = PatSymbValue(index->subst, term->f_code);
            assert(key);
         }
         cell = NumTreeCellAlloc();
         cell->key = key;
         cell->val1.i_val = index->count;
         cell->val2.i_val = index->count;
         old = NumTreeInsert(&(index->tree.n_index), cell);
         if(old)
         {
            NumTreeCellFree(cell);
            res = old->val1.i_val;
         }
         else /* key was unknown, association stays...*/
         {
            res = index->count;
            index->count++;
         }
         /* printf("\n===Key: %ld Res: %ld\n", key, res); */
         break;
   case IndexTop:
   case IndexAltTop:
   case IndexCSTop:
   case IndexESTop:
         query.term  = any_term_top(index->type, term, index->depth,
                                    index->bank->vars);
         query.subst = index->subst;
         cell2 = PTreeObjFind(&(index->tree.t_index), &query,
                              IndexTermCompareFun);
         if(cell2)
         {
            entry = cell2->key;
            res = entry->key;
         }
         else
         {
            shared = TBInsert(index->bank, query.term, DEREF_NEVER);
            entry = IndexTermAlloc(shared, index->subst,
                                   index->count);
            res = index->count++;
            entry = PTreeObjStore(&(index->tree.t_index), entry,
                                  IndexTermCompareFun);
            assert(!entry);
         }
         TermFree(query.term);
         break;
   case IndexIdentity:
         query.term  = term;
         query.subst = index->subst;
         cell2 = PTreeObjFind(&(index->tree.t_index), &query,
                              IndexTermCompareFun);
         if(cell2)
         {
            entry = cell2->key;
            res = entry->key;
         }
         else
         {
            shared = TBInsert(index->bank, query.term, DEREF_NEVER);
            entry = IndexTermAlloc(shared, index->subst,
                                   index->count);
            res = index->count++;
            entry = PTreeObjStore(&(index->tree.t_index), entry,
                                  IndexTermCompareFun);
            assert(!entry);
         }
         break;
   case IndexEmpty:
         assert(false && "Cannot insert term into IndexEmpty index!");
         break;
   default:
         assert(false && "Unknown index type");
         res = 0; /* Stiffle warnings */
         break;
   }
   return res;
}


/*-----------------------------------------------------------------------
//
// Function: TSMIndexPrint()
//
//   Print a TSM-Index (as a comment) in the form "keyno:keyobj"*
//
// Global Variables: -
//
// Side Effects    : Output
//
/----------------------------------------------------------------------*/

void TSMIndexPrint(FILE* out, TSMIndex_p index, int depth)
{
   PStack_p stack;
   NumTree_p ncell;
   PTree_p   pcell;
   IndexTerm_p current;
   long i;
   long f_code;
   char pattern[10];
   char pattern2[70];

   sprintf(pattern, "%%%ds", 3*depth);
   sprintf(pattern2, pattern, "");

   switch(index->type)
   {
   case IndexArity:
         fprintf(out, "# %sIndex %ld is arity index!\n", pattern2, index->ident);
         break;
   case IndexSymbol:
         fprintf(out, "# %sIndex %ld is symbol index!\n",
                 pattern2,index->ident);
         fprintf(out, "# %sPSymbol         Index  FCode     (Symbol)\n", pattern2);
         stack = NumTreeTraverseInit(index->tree.n_index);
         i=0;
         while((ncell = NumTreeTraverseNext(stack)))
         {
            f_code = PatternSubstGetOriginalSymbol(index->subst,
                                                   ncell->key);

            fprintf(out, "# %s#%10ld :%7ld  %7ld     %s\n", pattern2,
                    ncell->key,
                    ncell->val1.i_val,
                    f_code,
                    ((f_code > 0))&&(f_code<=index->bank->sig->f_count)?
                    SigFindName(index->bank->sig, f_code):"variable");
            i++;
         }
         NumTreeTraverseExit(stack);
         fprintf(out, "# %s%ld alternatives in the index\n",
                 pattern2, i);
         break;
   case IndexTop:
   case IndexAltTop:
   case IndexCSTop:
   case IndexESTop:
   case IndexIdentity:
         fprintf(out, "# Index is %s index!\n",
                 GetIndexName(index->type));
         stack = PTreeTraverseInit(index->tree.t_index);
         i=0;
         while((pcell = PTreeTraverseNext(stack)))
         {
            current = pcell->key;
            fprintf(out, "# %3ld : ", current->key);
            PatternTermPrint(out, index->subst, current->term, index->bank->sig);
            /* TermPrint(out, current->term, index->bank->sig,
               DEREF_NEVER); */
            fputc('\n', out);
            i++;
         }
         PTreeTraverseExit(stack);
         fprintf(out, "# %ld alternatives in the index\n", i);
         break;
   case IndexEmpty:
         fprintf(out, "# Index is empty index!\n");
         break;
   default:
         assert(false && "Unknown index type");
         break;
   }
}


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