/E/TERMS/cte_idx_fp.c

# · C · 810 lines · 365 code · 132 blank · 313 comment · 25 complexity · d40d75bf60121344c35af92f4aeef5d8 MD5 · raw file

  1. /*-----------------------------------------------------------------------
  2. File : ctr_idx_fp.c
  3. Author: Stephan Schulz (schulz@eprover.org)
  4. Contents
  5. Compute and handle term fingerprints for indexing.
  6. Copyright 2010 by the author.
  7. This code is released under the GNU General Public Licence and
  8. the GNU Lesser General Public License.
  9. See the file COPYING in the main E directory for details..
  10. Run "eprover -h" for contact information.
  11. Changes
  12. <1> Wed Feb 24 01:28:18 EET 2010
  13. New
  14. -----------------------------------------------------------------------*/
  15. #include "cte_idx_fp.h"
  16. /*---------------------------------------------------------------------*/
  17. /* Global Variables */
  18. /*---------------------------------------------------------------------*/
  19. char* FPIndexNames[] =
  20. {
  21. "FP0",
  22. "FPfp",
  23. "FP1",
  24. "FP2",
  25. "FP3D",
  26. "FP3W",
  27. "FP4D",
  28. "FP4W",
  29. "FP4M",
  30. "FP5M",
  31. "FP6M",
  32. "FP7",
  33. "FP7M",
  34. "FP4X2_2",
  35. "FP3DFlex",
  36. "NPDT",
  37. "NoIndex",
  38. NULL
  39. };
  40. static FPIndexFunction fp_index_funs[] =
  41. {
  42. IndexFP0Create,
  43. IndexFPfpCreate,
  44. IndexFP1Create,
  45. IndexFP2Create,
  46. IndexFP3DCreate,
  47. IndexFP3WCreate,
  48. IndexFP4DCreate,
  49. IndexFP4WCreate,
  50. IndexFP4MCreate,
  51. IndexFP5MCreate,
  52. IndexFP6MCreate,
  53. IndexFP7Create,
  54. IndexFP7MCreate,
  55. IndexFP4X2_2Create,
  56. IndexFP3DFlexCreate,
  57. IndexDTCreate,
  58. NULL,
  59. NULL
  60. };
  61. /*---------------------------------------------------------------------*/
  62. /* Forward Declarations */
  63. /*---------------------------------------------------------------------*/
  64. /*---------------------------------------------------------------------*/
  65. /* Internal Functions */
  66. /*---------------------------------------------------------------------*/
  67. /*-----------------------------------------------------------------------
  68. //
  69. // Function: push_fcodes()
  70. //
  71. // Push the f_codes of the term (in depth first, LR order) onto the
  72. // stack.
  73. //
  74. // Global Variables: -
  75. //
  76. // Side Effects : Memory operations
  77. //
  78. /----------------------------------------------------------------------*/
  79. static void push_fcodes(PStack_p stack, Term_p t)
  80. {
  81. if(TermIsVar(t))
  82. {
  83. PStackPushInt(stack, ANY_VAR);
  84. }
  85. else
  86. {
  87. int i;
  88. PStackPushInt(stack, t->f_code);
  89. for(i=0; i<t->arity; i++)
  90. {
  91. push_fcodes(stack, t->args[i]);
  92. }
  93. }
  94. }
  95. /*---------------------------------------------------------------------*/
  96. /* Exported Functions */
  97. /*---------------------------------------------------------------------*/
  98. /*-----------------------------------------------------------------------
  99. //
  100. // Function: TermFPSample()
  101. //
  102. // Sample the term at the position described by the optional
  103. // arguments (encoding a (-1)-terminated position.
  104. //
  105. // Global Variables:
  106. //
  107. // Side Effects :
  108. //
  109. /----------------------------------------------------------------------*/
  110. FunCode TermFPSample(Term_p term, ...)
  111. {
  112. va_list ap;
  113. va_start(ap, term);
  114. int pos = 0;
  115. FunCode res = 0;
  116. for(pos = va_arg(ap, int); pos != -1; pos = va_arg(ap, int))
  117. {
  118. if(TermIsVar(term))
  119. {
  120. res = BELOW_VAR;
  121. break;
  122. }
  123. if(pos >= term->arity)
  124. {
  125. res = NOT_IN_TERM;
  126. break;
  127. }
  128. term = term->args[pos];
  129. }
  130. if(pos == -1)
  131. {
  132. res = TermIsVar(term)?ANY_VAR:term->f_code;
  133. }
  134. va_end(ap);
  135. return res;
  136. }
  137. /*-----------------------------------------------------------------------
  138. //
  139. // Function: TermFPFlexSample()
  140. //
  141. // Sample the term at the position described by the array at
  142. // pos. Update pos to point behind the end of the (-1)-terminated
  143. // position.
  144. //
  145. // Global Variables:
  146. //
  147. // Side Effects :
  148. //
  149. /----------------------------------------------------------------------*/
  150. FunCode TermFPFlexSample(Term_p term, IntOrP* *seq)
  151. {
  152. FunCode res = 0;
  153. long pos;
  154. while((pos=(*seq)->i_val)!=-1)
  155. {
  156. if(TermIsVar(term))
  157. {
  158. res = BELOW_VAR;
  159. break;
  160. }
  161. if(pos >= term->arity)
  162. {
  163. res = NOT_IN_TERM;
  164. break;
  165. }
  166. term = term->args[pos];
  167. (*seq)++;
  168. }
  169. if(pos == -1)
  170. {
  171. res = TermIsVar(term)?ANY_VAR:term->f_code;
  172. }
  173. else
  174. {
  175. /* Find the end of the position */
  176. while((pos=(*seq)->i_val)!=-1)
  177. {
  178. (*seq)++;
  179. }
  180. }
  181. /* We want to point beyond the end */
  182. (*seq)++;
  183. return res;
  184. }
  185. /*-----------------------------------------------------------------------
  186. //
  187. // Function: IndexFP0Create()
  188. //
  189. // Create a dummy fingerprint structure.
  190. //
  191. // Global Variables: -
  192. //
  193. // Side Effects : -
  194. //
  195. /----------------------------------------------------------------------*/
  196. IndexFP_p IndexFP0Create(Term_p t)
  197. {
  198. IndexFP_p res = SizeMalloc(sizeof(FunCode)*1);
  199. res[0] = 1;
  200. return res;
  201. }
  202. /*-----------------------------------------------------------------------
  203. //
  204. // Function: IndexFPfpCreate()
  205. //
  206. // Create a fingerprint structure using an abstraction to just avoid
  207. // function/predicate unifications/matches.
  208. //
  209. // Global Variables: -
  210. //
  211. // Side Effects : -
  212. //
  213. /----------------------------------------------------------------------*/
  214. IndexFP_p IndexFPfpCreate(Term_p t)
  215. {
  216. IndexFP_p res = SizeMalloc(sizeof(FunCode)*2);
  217. static FunCode f_rep = 0, p_rep = 0;
  218. res[0] = 2;
  219. res[1] = TermFPSample(t, -1);
  220. if(res[1] > 0)
  221. {
  222. if(TermCellQueryProp((t), TPPredPos))
  223. {
  224. if(!p_rep)
  225. {
  226. p_rep = res[1];
  227. }
  228. res[1] = p_rep;
  229. }
  230. else
  231. {
  232. if(!f_rep)
  233. {
  234. f_rep = res[1];
  235. }
  236. res[1] = f_rep;
  237. }
  238. }
  239. return res;
  240. }
  241. /*-----------------------------------------------------------------------
  242. //
  243. // Function: IndexFP1Create()
  244. //
  245. // Create a fingerprint structure representing top symbol hashing.
  246. //
  247. // Global Variables: -
  248. //
  249. // Side Effects : -
  250. //
  251. /----------------------------------------------------------------------*/
  252. IndexFP_p IndexFP1Create(Term_p t)
  253. {
  254. IndexFP_p res = SizeMalloc(sizeof(FunCode)*2);
  255. res[0] = 2;
  256. res[1] = TermFPSample(t, -1);
  257. return res;
  258. }
  259. /*-----------------------------------------------------------------------
  260. //
  261. // Function: IndexFP2Create()
  262. //
  263. // Create a fingerprint structure representing sampling at epsilon,
  264. // 0.
  265. //
  266. // Global Variables: -
  267. //
  268. // Side Effects : -
  269. //
  270. /----------------------------------------------------------------------*/
  271. IndexFP_p IndexFP2Create(Term_p t)
  272. {
  273. IndexFP_p res = SizeMalloc(sizeof(FunCode)*3);
  274. res[0] = 3;
  275. res[1] = TermFPSample(t, -1);
  276. res[2] = TermFPSample(t, 0, -1);
  277. return res;
  278. }
  279. /*-----------------------------------------------------------------------
  280. //
  281. // Function: IndexFP3DCreate()
  282. //
  283. // Create a fingerprint structure representing sampling at epsilon,
  284. // 0, 0.0.
  285. //
  286. // Global Variables: -
  287. //
  288. // Side Effects : -
  289. //
  290. /----------------------------------------------------------------------*/
  291. IndexFP_p IndexFP3DCreate(Term_p t)
  292. {
  293. IndexFP_p res = SizeMalloc(sizeof(FunCode)*4);
  294. res[0] = 4;
  295. res[1] = TermFPSample(t, -1);
  296. res[2] = TermFPSample(t, 0, -1);
  297. res[3] = TermFPSample(t, 0, 0, -1);
  298. return res;
  299. }
  300. /*-----------------------------------------------------------------------
  301. //
  302. // Function: IndexFP3WCreate()
  303. //
  304. // Create a fingerprint structure representing sampling at epsilon,
  305. // 0, 1.
  306. //
  307. // Global Variables: -
  308. //
  309. // Side Effects : -
  310. //
  311. /----------------------------------------------------------------------*/
  312. IndexFP_p IndexFP3WCreate(Term_p t)
  313. {
  314. IndexFP_p res = SizeMalloc(sizeof(FunCode)*4);
  315. res[0] = 4;
  316. res[1] = TermFPSample(t, -1);
  317. res[2] = TermFPSample(t, 0, -1);
  318. res[3] = TermFPSample(t, 1, -1);
  319. return res;
  320. }
  321. /*-----------------------------------------------------------------------
  322. //
  323. // Function: IndexFP4DCreate()
  324. //
  325. // Create a fingerprint structure representing sampling at epsilon,
  326. // 0, 0.0, 0.0.0
  327. //
  328. // Global Variables: -
  329. //
  330. // Side Effects : -
  331. //
  332. /----------------------------------------------------------------------*/
  333. IndexFP_p IndexFP4DCreate(Term_p t)
  334. {
  335. IndexFP_p res = SizeMalloc(sizeof(FunCode)*5);
  336. res[0] = 5;
  337. res[1] = TermFPSample(t, -1);
  338. res[2] = TermFPSample(t, 0, -1);
  339. res[3] = TermFPSample(t, 0, 0, -1);
  340. res[4] = TermFPSample(t, 0, 0,0, -1);
  341. return res;
  342. }
  343. /*-----------------------------------------------------------------------
  344. //
  345. // Function: IndexFP4WCreate()
  346. //
  347. // Create a fingerprint structure representing sampling at epsilon,
  348. // 0, 1, 2
  349. //
  350. // Global Variables: -
  351. //
  352. // Side Effects : -
  353. //
  354. /----------------------------------------------------------------------*/
  355. IndexFP_p IndexFP4WCreate(Term_p t)
  356. {
  357. IndexFP_p res = SizeMalloc(sizeof(FunCode)*5);
  358. res[0] = 5;
  359. res[1] = TermFPSample(t, -1);
  360. res[2] = TermFPSample(t, 0, -1);
  361. res[3] = TermFPSample(t, 1, -1);
  362. res[4] = TermFPSample(t, 2, -1);
  363. return res;
  364. }
  365. /*-----------------------------------------------------------------------
  366. //
  367. // Function: IndexFP4MCreate()
  368. //
  369. // Create a fingerprint structure representing sampling at epsilon,
  370. // 0, 1, 0.0
  371. //
  372. // Global Variables: -
  373. //
  374. // Side Effects : -
  375. //
  376. /----------------------------------------------------------------------*/
  377. IndexFP_p IndexFP4MCreate(Term_p t)
  378. {
  379. IndexFP_p res = SizeMalloc(sizeof(FunCode)*5);
  380. res[0] = 5;
  381. res[1] = TermFPSample(t, -1);
  382. res[2] = TermFPSample(t, 0, -1);
  383. res[3] = TermFPSample(t, 1, -1);
  384. res[4] = TermFPSample(t, 0, 0, -1);
  385. return res;
  386. }
  387. /*-----------------------------------------------------------------------
  388. //
  389. // Function: IndexFP5MCreate()
  390. //
  391. // Create a fingerprint structure representing sampling at epsilon,
  392. // 0, 1, 2, 0.0
  393. //
  394. // Global Variables: -
  395. //
  396. // Side Effects : -
  397. //
  398. /----------------------------------------------------------------------*/
  399. IndexFP_p IndexFP5MCreate(Term_p t)
  400. {
  401. IndexFP_p res = SizeMalloc(sizeof(FunCode)*6);
  402. res[0] = 6;
  403. res[1] = TermFPSample(t, -1);
  404. res[2] = TermFPSample(t, 0, -1);
  405. res[3] = TermFPSample(t, 1, -1);
  406. res[4] = TermFPSample(t, 2, -1);
  407. res[5] = TermFPSample(t, 0, 0, -1);
  408. return res;
  409. }
  410. /*-----------------------------------------------------------------------
  411. //
  412. // Function: IndexFP6MCreate()
  413. //
  414. // Create a fingerprint structure representing sampling at epsilon,
  415. // 0, 1, 2, 0.0, 0.1
  416. //
  417. // Global Variables: -
  418. //
  419. // Side Effects : -
  420. //
  421. /----------------------------------------------------------------------*/
  422. IndexFP_p IndexFP6MCreate(Term_p t)
  423. {
  424. IndexFP_p res = SizeMalloc(sizeof(FunCode)*7);
  425. res[0] = 7;
  426. res[1] = TermFPSample(t, -1);
  427. res[2] = TermFPSample(t, 0, -1);
  428. res[3] = TermFPSample(t, 1, -1);
  429. res[4] = TermFPSample(t, 2, -1);
  430. res[5] = TermFPSample(t, 0, 0, -1);
  431. res[6] = TermFPSample(t, 0, 1, -1);
  432. return res;
  433. }
  434. /*-----------------------------------------------------------------------
  435. //
  436. // Function: IndexFP7Create()
  437. //
  438. // Create a fingerprint structure with samples at positions epsilon,
  439. // 0, 1, 0.0, 0.1, 1.0, 1.1 (using E's internal numbering).
  440. //
  441. // Global Variables: -
  442. //
  443. // Side Effects : -
  444. //
  445. /----------------------------------------------------------------------*/
  446. IndexFP_p IndexFP7Create(Term_p t)
  447. {
  448. IndexFP_p res = SizeMalloc(sizeof(FunCode)*8);
  449. res[0] = 8;
  450. res[1] = TermFPSample(t, -1);
  451. res[2] = TermFPSample(t, 0, -1);
  452. res[3] = TermFPSample(t, 1, -1);
  453. res[4] = TermFPSample(t, 0, 0, -1);
  454. res[5] = TermFPSample(t, 0, 1, -1);
  455. res[6] = TermFPSample(t, 1, 0, -1);
  456. res[7] = TermFPSample(t, 1, 1, -1);
  457. return res;
  458. }
  459. /*-----------------------------------------------------------------------
  460. //
  461. // Function: IndexFP7MCreate()
  462. //
  463. // Create a fingerprint structure representing sampling at epsilon,
  464. // 0, 1, 2, 3, 0.0, 0.1
  465. //
  466. // Global Variables: -
  467. //
  468. // Side Effects : -
  469. //
  470. /----------------------------------------------------------------------*/
  471. IndexFP_p IndexFP7MCreate(Term_p t)
  472. {
  473. IndexFP_p res = SizeMalloc(sizeof(FunCode)*8);
  474. res[0] = 8;
  475. res[1] = TermFPSample(t, -1);
  476. res[2] = TermFPSample(t, 0, -1);
  477. res[3] = TermFPSample(t, 1, -1);
  478. res[4] = TermFPSample(t, 2, -1);
  479. res[5] = TermFPSample(t, 0, 0, -1);
  480. res[6] = TermFPSample(t, 3, -1);
  481. res[7] = TermFPSample(t, 0, 1, -1);
  482. return res;
  483. }
  484. /*-----------------------------------------------------------------------
  485. //
  486. // Function: IndexFP4X2_2Create()
  487. //
  488. // Create a fingerprint structure with samples at positions as
  489. // specified below.
  490. //
  491. // Global Variables: -
  492. //
  493. // Side Effects : -
  494. //
  495. /----------------------------------------------------------------------*/
  496. IndexFP_p IndexFP4X2_2Create(Term_p t)
  497. {
  498. IndexFP_p res = SizeMalloc(sizeof(FunCode)*17);
  499. res[ 0] = 17;
  500. res[ 1] = TermFPSample(t, -1);
  501. res[ 2] = TermFPSample(t, 0, -1);
  502. res[ 3] = TermFPSample(t, 1, -1);
  503. res[ 4] = TermFPSample(t, 2, -1);
  504. res[ 5] = TermFPSample(t, 3, -1);
  505. res[ 6] = TermFPSample(t, 0, 0, -1);
  506. res[ 7] = TermFPSample(t, 0, 1, -1);
  507. res[ 8] = TermFPSample(t, 0, 2, -1);
  508. res[9] = TermFPSample(t, 1, 0, -1);
  509. res[10] = TermFPSample(t, 1, 1, -1);
  510. res[11] = TermFPSample(t, 1, 2, -1);
  511. res[12] = TermFPSample(t, 2, 0, -1);
  512. res[13] = TermFPSample(t, 2, 1, -1);
  513. res[14] = TermFPSample(t, 2, 2, -1);
  514. res[15] = TermFPSample(t, 0, 0, 0, -1);
  515. res[16] = TermFPSample(t, 1, 0, 0, -1);
  516. return res;
  517. }
  518. /*-----------------------------------------------------------------------
  519. //
  520. // Function: IndexFPFlexCreate()
  521. //
  522. // Create a fingerprint of len elments, with the positions in pos.
  523. //
  524. // Global Variables:
  525. //
  526. // Side Effects :
  527. //
  528. /----------------------------------------------------------------------*/
  529. IndexFP_p IndexFPFlexCreate(Term_p t, PStack_p pos, int len)
  530. {
  531. IndexFP_p res = SizeMalloc(sizeof(FunCode)*(len+1));
  532. IntOrP *p = PStackBaseAddress(pos);
  533. int i;
  534. res[0] = (len+1);
  535. i=1;
  536. while((*p).i_val != -2)
  537. {
  538. res[i] = TermFPFlexSample(t, &p);
  539. i++;
  540. }
  541. return res;
  542. }
  543. /*-----------------------------------------------------------------------
  544. //
  545. // Function: IndexFP3DFlexCreate()
  546. //
  547. // Testfunction, equivalent to IndexFP3DCreate()
  548. //
  549. // Global Variables: -
  550. //
  551. // Side Effects : Memory operations
  552. //
  553. /----------------------------------------------------------------------*/
  554. IndexFP_p IndexFP3DFlexCreate(Term_p t)
  555. {
  556. IndexFP_p res;
  557. PStack_p pos = PStackAlloc();
  558. PStackPushInt(pos, -1);
  559. PStackPushInt(pos, 0);
  560. PStackPushInt(pos, -1);
  561. PStackPushInt(pos, 0);
  562. PStackPushInt(pos, 0);
  563. PStackPushInt(pos, -1);
  564. PStackPushInt(pos, -2);
  565. res = IndexFPFlexCreate(t, pos, 3);
  566. PStackFree(pos);
  567. return res;
  568. }
  569. /*-----------------------------------------------------------------------
  570. //
  571. // Function: IndexDTCreate()
  572. //
  573. // Create a fingerprint that samples t at all its positions (in
  574. // depths-first LR order) and no others. Building an FP-Tree with
  575. // these samples will not build an FP-Index, but a (non-perfect)
  576. // discrimination tree. This means that retrieval will require
  577. // special code, it cannot use simple FP-Index retrieval.
  578. //
  579. // Global Variables: -
  580. //
  581. // Side Effects : Memory operations
  582. //
  583. /----------------------------------------------------------------------*/
  584. IndexFP_p IndexDTCreate(Term_p t)
  585. {
  586. PStack_p stack = PStackAlloc();
  587. IndexFP_p res;
  588. PStackPointer sp;
  589. int i, len;
  590. push_fcodes(stack, t);
  591. len = PStackGetSP(stack);
  592. res = SizeMalloc(sizeof(FunCode)*(len+1));
  593. res[0] = len+1;
  594. for(sp = 0, i=1; sp<len; sp++, i++)
  595. {
  596. res[i] = PStackElementInt(stack, sp);
  597. }
  598. PStackFree(stack);
  599. return res;
  600. }
  601. /*-----------------------------------------------------------------------
  602. //
  603. // Function: IndexFPFree()
  604. //
  605. // Free an IndexFP data-structure (i.e. a self-describing FunCode
  606. // array).
  607. //
  608. // Global Variables: -
  609. //
  610. // Side Effects : Memory operations
  611. //
  612. /----------------------------------------------------------------------*/
  613. void IndexFPFree(IndexFP_p junk)
  614. {
  615. SizeFree(junk, sizeof(FunCode)*junk[0]);
  616. }
  617. /*-----------------------------------------------------------------------
  618. //
  619. // Function: GetFPIndexFunction()
  620. //
  621. // Given a name, return the corresponding index function, or NULL.
  622. //
  623. // Global Variables: fp_index_names, fp_index_funs
  624. //
  625. // Side Effects : -
  626. //
  627. /----------------------------------------------------------------------*/
  628. FPIndexFunction GetFPIndexFunction(char* name)
  629. {
  630. int i;
  631. for(i=0; FPIndexNames[i]; i++)
  632. {
  633. if(strcmp(FPIndexNames[i], name)==0)
  634. {
  635. return fp_index_funs[i];
  636. }
  637. }
  638. return NULL;
  639. }
  640. /*-----------------------------------------------------------------------
  641. //
  642. // Function: IndexFPPrint()
  643. //
  644. // Print a term fingerprint.
  645. //
  646. // Global Variables: -
  647. //
  648. // Side Effects : Output
  649. //
  650. /----------------------------------------------------------------------*/
  651. void IndexFPPrint(FILE* out, IndexFP_p fp)
  652. {
  653. int i, limit=fp[0];
  654. if(limit>=2)
  655. {
  656. fprintf(stdout, "<%ld", fp[1]);
  657. for(i=2; i<limit; i++)
  658. {
  659. fprintf(stdout, ",%ld", fp[i]);
  660. }
  661. fprintf(stdout, ">");
  662. }
  663. else
  664. {
  665. fprintf(stdout, "<>");
  666. }
  667. }
  668. /*---------------------------------------------------------------------*/
  669. /* End of File */
  670. /*---------------------------------------------------------------------*/