/E/CLAUSES/ccl_eqnlist.c

# · C · 1624 lines · 700 code · 235 blank · 689 comment · 95 complexity · 961fdc13acc99d04f3aec8d4657011cd MD5 · raw file

  1. /*-----------------------------------------------------------------------
  2. File : ccl_eqnlist.c
  3. Author: Stephan Schulz
  4. Contents
  5. Functions for dealing with (singly linked) lists of equations as
  6. used in clauses
  7. Copyright 1998, 1999 by the author.
  8. This code is released under the GNU General Public Licence and
  9. the GNU Lesser General Public License.
  10. See the file COPYING in the main E directory for details..
  11. Run "eprover -h" for contact information.
  12. Changes
  13. <1> Fri Apr 10 21:11:18 MET DST 1998
  14. New
  15. -----------------------------------------------------------------------*/
  16. #include "ccl_eqnlist.h"
  17. /*---------------------------------------------------------------------*/
  18. /* Global Variables */
  19. /*---------------------------------------------------------------------*/
  20. /*---------------------------------------------------------------------*/
  21. /* Forward Declarations */
  22. /*---------------------------------------------------------------------*/
  23. /*---------------------------------------------------------------------*/
  24. /* Internal Functions */
  25. /*---------------------------------------------------------------------*/
  26. /*-----------------------------------------------------------------------
  27. //
  28. // Function: eqn_list_find_last()
  29. //
  30. // Find the last EqnRef in *list (may be list itself).
  31. //
  32. // Global Variables: -
  33. //
  34. // Side Effects : -
  35. //
  36. /----------------------------------------------------------------------*/
  37. EqnRef eqn_list_find_last(EqnRef list)
  38. {
  39. while(*list)
  40. {
  41. list = &((*list)->next);
  42. }
  43. return list;
  44. }
  45. /*---------------------------------------------------------------------*/
  46. /* Exported Functions */
  47. /*---------------------------------------------------------------------*/
  48. /*-----------------------------------------------------------------------
  49. //
  50. // Function: EqnListFree()
  51. //
  52. // Deallocate the list.
  53. //
  54. // Global Variables: -
  55. //
  56. // Side Effects : Memory Management
  57. //
  58. /----------------------------------------------------------------------*/
  59. void EqnListFree(Eqn_p list)
  60. {
  61. Eqn_p handle;
  62. while(list)
  63. {
  64. handle = list;
  65. list = list->next;
  66. EqnFree(handle);
  67. }
  68. }
  69. /*-----------------------------------------------------------------------
  70. //
  71. // Function: EqnListGCMarkTerms()
  72. //
  73. // Mark all terms in the eqnlist for the Garbage Collection.
  74. //
  75. // Global Variables:
  76. //
  77. // Side Effects :
  78. //
  79. /----------------------------------------------------------------------*/
  80. void EqnListGCMarkTerms(Eqn_p list)
  81. {
  82. while(list)
  83. {
  84. EqnGCMarkTerms(list);
  85. list = list->next;
  86. }
  87. }
  88. /*-----------------------------------------------------------------------
  89. //
  90. // Function: EqnListSetProp()
  91. //
  92. // Set the properties prop in all literals from list. Return the
  93. // lenght of the list.
  94. //
  95. // Global Variables: -
  96. //
  97. // Side Effects : -
  98. //
  99. /----------------------------------------------------------------------*/
  100. int EqnListSetProp(Eqn_p list, EqnProperties prop)
  101. {
  102. int res = 0;
  103. while(list)
  104. {
  105. EqnSetProp(list, prop);
  106. list = list->next;
  107. res++;
  108. }
  109. return res;
  110. }
  111. /*-----------------------------------------------------------------------
  112. //
  113. // Function: EqnListDelProp()
  114. //
  115. // Delete the properties prop in all literals from list. Return
  116. // lenght of the list.
  117. //
  118. // Global Variables: -
  119. //
  120. // Side Effects : -
  121. //
  122. /----------------------------------------------------------------------*/
  123. int EqnListDelProp(Eqn_p list, EqnProperties prop)
  124. {
  125. int res = 0;
  126. while(list)
  127. {
  128. EqnDelProp(list, prop);
  129. list = list->next;
  130. res++;
  131. }
  132. return res;
  133. }
  134. /*-----------------------------------------------------------------------
  135. //
  136. // Function: EqnListFlipProp()
  137. //
  138. // Delete the properties prop in all literals from list. Return
  139. // lenght of the list.
  140. //
  141. // Global Variables: -
  142. //
  143. // Side Effects : -
  144. //
  145. /----------------------------------------------------------------------*/
  146. int EqnListFlipProp(Eqn_p list, EqnProperties prop)
  147. {
  148. int res = 0;
  149. while(list)
  150. {
  151. EqnFlipProp(list, prop);
  152. list = list->next;
  153. res++;
  154. }
  155. return res;
  156. }
  157. /*-----------------------------------------------------------------------
  158. //
  159. // Function: EqnListQueryPropNumber()
  160. //
  161. // Return number of equations with props set.
  162. //
  163. // Global Variables: -
  164. //
  165. // Side Effects : -
  166. //
  167. /----------------------------------------------------------------------*/
  168. int EqnListQueryPropNumber(Eqn_p list, EqnProperties prop)
  169. {
  170. int res = 0;
  171. while(list)
  172. {
  173. if(EqnQueryProp(list, prop))
  174. {
  175. res++;
  176. }
  177. list = list->next;
  178. }
  179. return res;
  180. }
  181. /*-----------------------------------------------------------------------
  182. //
  183. // Function: EqnListLength()
  184. //
  185. // Return number of equations in the list.
  186. //
  187. // Global Variables: -
  188. //
  189. // Side Effects : -
  190. //
  191. /----------------------------------------------------------------------*/
  192. int EqnListLength(Eqn_p list)
  193. {
  194. int i=0;
  195. while(list)
  196. {
  197. i++;
  198. list = list->next;
  199. }
  200. return i;
  201. }
  202. /*-----------------------------------------------------------------------
  203. //
  204. // Function: EqnListFromArray()
  205. //
  206. // Convert an array of Eqn_p's into a list.
  207. //
  208. // Global Variables: -
  209. //
  210. // Side Effects : -
  211. //
  212. /----------------------------------------------------------------------*/
  213. Eqn_p EqnListFromArray(Eqn_p* array, int lenght)
  214. {
  215. int i;
  216. Eqn_p *handle, res;
  217. handle = &(res);
  218. for(i=0; i<lenght; i++)
  219. {
  220. *handle = array[i];
  221. handle = &((*handle)->next);
  222. }
  223. *handle = NULL;
  224. return res;
  225. }
  226. /*-----------------------------------------------------------------------
  227. //
  228. // Function: EqnListToStack()
  229. //
  230. // Push the literals onto a newly created stack and return it. Does
  231. // not copy anything! The caller has to free the stack.
  232. //
  233. // Global Variables:
  234. //
  235. // Side Effects :
  236. //
  237. /----------------------------------------------------------------------*/
  238. PStack_p EqnListToStack(Eqn_p list)
  239. {
  240. PStack_p stack = PStackAlloc();
  241. while(list)
  242. {
  243. PStackPushP(stack, list);
  244. list = list->next;
  245. }
  246. return stack;
  247. }
  248. /*-----------------------------------------------------------------------
  249. //
  250. // Function: EqnListExtractElement()
  251. //
  252. // Take the given element out of the list and return a pointer to
  253. // it.
  254. //
  255. // Global Variables: -
  256. //
  257. // Side Effects : Changes the list
  258. //
  259. /----------------------------------------------------------------------*/
  260. Eqn_p EqnListExtractElement(EqnRef element)
  261. {
  262. Eqn_p handle = *element;
  263. assert(handle);
  264. *element = handle->next;
  265. handle->next = NULL;
  266. return handle;
  267. }
  268. /*-----------------------------------------------------------------------
  269. //
  270. // Function: EqnListExtractByProp()
  271. //
  272. // Extract all equations with properties props (not) set (depending
  273. // on negate).
  274. //
  275. // Global Variables: -
  276. //
  277. // Side Effects : Changes list
  278. //
  279. /----------------------------------------------------------------------*/
  280. Eqn_p EqnListExtractByProps(EqnRef list, EqnProperties props, bool
  281. negate)
  282. {
  283. Eqn_p res = NULL, tmp;
  284. while(*list)
  285. {
  286. if(XOR(EqnQueryProp(*list, props),negate))
  287. {
  288. tmp = EqnListExtractFirst(list);
  289. EqnListInsertFirst(&res, tmp);
  290. }
  291. else
  292. {
  293. list = &((*list)->next);
  294. }
  295. }
  296. return res;
  297. }
  298. /*-----------------------------------------------------------------------
  299. //
  300. // Function: EqnListDeleteElement()
  301. //
  302. // Delete the given element from the list.
  303. //
  304. // Global Variables: -
  305. //
  306. // Side Effects : Via EqnListExtractElement() and EqnFree()
  307. //
  308. /----------------------------------------------------------------------*/
  309. void EqnListDeleteElement(EqnRef element)
  310. {
  311. Eqn_p handle;
  312. handle = EqnListExtractElement(element);
  313. EqnFree(handle);
  314. }
  315. /*-----------------------------------------------------------------------
  316. //
  317. // Function: EqnListInsertElement()
  318. //
  319. // Insert the element at the position defined by pos.
  320. //
  321. // Global Variables: -
  322. //
  323. // Side Effects : Changes the list
  324. //
  325. /----------------------------------------------------------------------*/
  326. void EqnListInsertElement(EqnRef pos, Eqn_p element)
  327. {
  328. element->next = *pos;
  329. *pos = element;
  330. }
  331. /*-----------------------------------------------------------------------
  332. //
  333. // Function: EqnListAppend()
  334. //
  335. // Append newpart at the end of *list.
  336. //
  337. // Global Variables: -
  338. //
  339. // Side Effects : -
  340. //
  341. /----------------------------------------------------------------------*/
  342. Eqn_p EqnListAppend(EqnRef list, Eqn_p newpart)
  343. {
  344. EqnRef result = list;
  345. list = eqn_list_find_last(list);
  346. assert(!(*list));
  347. *list = newpart;
  348. return *result;
  349. }
  350. /*-----------------------------------------------------------------------
  351. //
  352. // Function: EqnListFlatCopy()
  353. //
  354. // Return a flat copy of the given list, reusing the existing terms.
  355. //
  356. // Global Variables: -
  357. //
  358. // Side Effects : Memory operations
  359. //
  360. /----------------------------------------------------------------------*/
  361. Eqn_p EqnListFlatCopy(Eqn_p list)
  362. {
  363. Eqn_p newlist = NULL;
  364. EqnRef insert = &newlist;
  365. while(list)
  366. {
  367. *insert = EqnAlloc(list->lterm, list->rterm, list->bank,
  368. EqnIsPositive(list));
  369. insert = &((*insert)->next);
  370. list = list->next;
  371. }
  372. *insert = NULL;
  373. return newlist;
  374. }
  375. /*-----------------------------------------------------------------------
  376. //
  377. // Function: EqnListCopy()
  378. //
  379. // Return a copy of the given list, with new terms from the term
  380. // bank. Instantiated terms are copied as instantiations.
  381. //
  382. // Global Variables: -
  383. //
  384. // Side Effects : Memory operations
  385. //
  386. /----------------------------------------------------------------------*/
  387. Eqn_p EqnListCopy(Eqn_p list, TB_p bank)
  388. {
  389. Eqn_p newlist = NULL;
  390. EqnRef insert = &newlist;
  391. while(list)
  392. {
  393. *insert = EqnCopy(list, bank);
  394. insert = &((*insert)->next);
  395. list = list->next;
  396. }
  397. *insert = NULL;
  398. return newlist;
  399. }
  400. /*-----------------------------------------------------------------------
  401. //
  402. // Function: EqnListCopyExcept()
  403. //
  404. // Return a copy of the given list, except for the equation given in
  405. // except, with new terms from the term bank. Instantiated terms are
  406. // copied as instantiations.
  407. //
  408. // Global Variables: -
  409. //
  410. // Side Effects : Memory operations
  411. //
  412. /----------------------------------------------------------------------*/
  413. Eqn_p EqnListCopyExcept(Eqn_p list, Eqn_p except, TB_p bank)
  414. {
  415. Eqn_p newlist = NULL;
  416. EqnRef insert = &newlist;
  417. while(list)
  418. {
  419. if(list != except)
  420. {
  421. *insert = EqnCopy(list, bank);
  422. insert = &((*insert)->next);
  423. }
  424. list = list->next;
  425. }
  426. *insert = NULL;
  427. return newlist;
  428. }
  429. /*-----------------------------------------------------------------------
  430. //
  431. // Function: EqnListCopyOpt()
  432. //
  433. // Copy an Eqnlist with the optimizations possible if all terms
  434. // (source and target) are from the same term bank.
  435. //
  436. // Global Variables: -
  437. //
  438. // Side Effects : Memory operations
  439. //
  440. /----------------------------------------------------------------------*/
  441. Eqn_p EqnListCopyOpt(Eqn_p list)
  442. {
  443. Eqn_p newlist = NULL;
  444. EqnRef insert = &newlist;
  445. while(list)
  446. {
  447. *insert = EqnCopyOpt(list);
  448. insert = &((*insert)->next);
  449. list = list->next;
  450. }
  451. *insert = NULL;
  452. return newlist;
  453. }
  454. /*-----------------------------------------------------------------------
  455. //
  456. // Function: EqnListCopyOptExcept()
  457. //
  458. // Copy an Eqnlist with one exception using the optimizations
  459. // possible if all terms (source and target) are from the same term
  460. // bank.
  461. //
  462. // Global Variables: -
  463. //
  464. // Side Effects : Memory operations
  465. //
  466. /----------------------------------------------------------------------*/
  467. Eqn_p EqnListCopyOptExcept(Eqn_p list, Eqn_p except)
  468. {
  469. Eqn_p newlist = NULL;
  470. EqnRef insert = &newlist;
  471. while(list)
  472. {
  473. if(list != except)
  474. {
  475. *insert = EqnCopyOpt(list);
  476. insert = &((*insert)->next);
  477. }
  478. list = list->next;
  479. }
  480. *insert = NULL;
  481. return newlist;
  482. }
  483. /*-----------------------------------------------------------------------
  484. //
  485. // Function: EqnListCopyDisjoint()
  486. //
  487. // Create a copy of list with disjoint variables (using the even/odd
  488. // convention).
  489. //
  490. // Global Variables: -
  491. //
  492. // Side Effects : Memory operations
  493. //
  494. /----------------------------------------------------------------------*/
  495. Eqn_p EqnListCopyDisjoint(Eqn_p list)
  496. {
  497. Eqn_p newlist = NULL;
  498. EqnRef insert = &newlist;
  499. while(list)
  500. {
  501. *insert = EqnCopyDisjoint(list);
  502. insert = &((*insert)->next);
  503. list = list->next;
  504. }
  505. *insert = NULL;
  506. return newlist;
  507. }
  508. /*-----------------------------------------------------------------------
  509. //
  510. // Function: EqnListCopyRepl()
  511. //
  512. // Return a copy of the list with terms from bank, except that
  513. // all occurances of "old" are replaced with repl (which has to be
  514. // in bank).
  515. //
  516. // Global Variables: -
  517. //
  518. // Side Effects : Memory operations
  519. //
  520. /----------------------------------------------------------------------*/
  521. Eqn_p EqnListCopyRepl(Eqn_p list, TB_p bank, Term_p old, Term_p repl)
  522. {
  523. Eqn_p newlist = NULL;
  524. EqnRef insert = &newlist;
  525. while(list)
  526. {
  527. *insert = EqnCopyRepl(list, bank, old, repl);
  528. insert = &((*insert)->next);
  529. list = list->next;
  530. }
  531. *insert = NULL;
  532. return newlist;
  533. }
  534. /*-----------------------------------------------------------------------
  535. //
  536. // Function: EqnListNegateEqns()
  537. //
  538. // Negate all signs in the list.
  539. //
  540. // Global Variables: -
  541. //
  542. // Side Effects : Changes signs in the list
  543. //
  544. /----------------------------------------------------------------------*/
  545. Eqn_p EqnListNegateEqns(Eqn_p list)
  546. {
  547. Eqn_p handle = list;
  548. while(handle)
  549. {
  550. EqnFlipProp(handle, EPIsPositive);
  551. handle = handle->next;
  552. }
  553. return list;
  554. }
  555. /*-----------------------------------------------------------------------
  556. //
  557. // Function: EqnListRemoveDuplicates()
  558. //
  559. // Remove all but one copy of identical (modulo commutativity)
  560. // elements from the list. Return number of removed literals.
  561. //
  562. // Global Variables: -
  563. //
  564. // Side Effects : Changes the list
  565. //
  566. /----------------------------------------------------------------------*/
  567. int EqnListRemoveDuplicates(Eqn_p list, TermEqualTestFun EqualTest)
  568. {
  569. EqnRef handle;
  570. int removed = 0;
  571. while(list)
  572. {
  573. handle = &(list->next);
  574. while(*handle)
  575. {
  576. if(LiteralEqual(*handle, list, EqualTest))
  577. {
  578. EqnListDeleteElement(handle);
  579. removed++;
  580. }
  581. else
  582. {
  583. handle = &((*handle)->next);
  584. }
  585. }
  586. list = list->next;
  587. }
  588. return removed;
  589. }
  590. /*-----------------------------------------------------------------------
  591. //
  592. // Function: EqnListRemoveResolved()
  593. //
  594. // Remove trivially false equations.
  595. //
  596. // Global Variables: -
  597. //
  598. // Side Effects : Changes the list.
  599. //
  600. /----------------------------------------------------------------------*/
  601. int EqnListRemoveResolved(EqnRef list)
  602. {
  603. int removed = 0;
  604. while(*list)
  605. {
  606. if(EqnIsFalse(*list))
  607. {
  608. EqnListDeleteElement(list);
  609. removed++;
  610. }
  611. else
  612. {
  613. list = &((*list)->next);
  614. }
  615. }
  616. return removed;
  617. }
  618. /*-----------------------------------------------------------------------
  619. //
  620. // Function: EqnListRemoveACResolved()
  621. //
  622. // Remove negative equations implied by the current AC theory.
  623. //
  624. // Global Variables: -
  625. //
  626. // Side Effects : Changes the list.
  627. //
  628. /----------------------------------------------------------------------*/
  629. int EqnListRemoveACResolved(EqnRef list)
  630. {
  631. int removed = 0;
  632. while(*list)
  633. {
  634. if(!EqnIsPositive(*list) && EqnIsACTrivial(*list))
  635. {
  636. EqnListDeleteElement(list);
  637. removed++;
  638. }
  639. else
  640. {
  641. list = &((*list)->next);
  642. }
  643. }
  644. return removed;
  645. }
  646. /*-----------------------------------------------------------------------
  647. //
  648. // Function: EqnListRemoveSimpleAnswers()
  649. //
  650. // Remove all simple answer literals from the list
  651. //
  652. // Global Variables: -
  653. //
  654. // Side Effects : Changes the list.
  655. //
  656. /----------------------------------------------------------------------*/
  657. int EqnListRemoveSimpleAnswers(EqnRef list)
  658. {
  659. int removed = 0;
  660. while(*list)
  661. {
  662. if(EqnIsSimpleAnswer(*list))
  663. {
  664. EqnListDeleteElement(list);
  665. removed++;
  666. }
  667. else
  668. {
  669. list = &((*list)->next);
  670. }
  671. }
  672. return removed;
  673. }
  674. /*-----------------------------------------------------------------------
  675. //
  676. // Function: EqnListFindNegPureVarLit()
  677. //
  678. // Return a pointer to the first negative literal of the form X!=Y
  679. // (or NULL if no such literal exists).
  680. //
  681. // Global Variables: -
  682. //
  683. // Side Effects : -
  684. //
  685. /----------------------------------------------------------------------*/
  686. Eqn_p EqnListFindNegPureVarLit(Eqn_p list)
  687. {
  688. while(list)
  689. {
  690. if(EqnIsNegative(list)&&EqnIsPureVar(list))
  691. {
  692. break;
  693. }
  694. list = list->next;
  695. }
  696. return list;
  697. }
  698. /*-----------------------------------------------------------------------
  699. //
  700. // Function: EqnListFindTrue()
  701. //
  702. // Return the first "always true" literal, if any. Return false
  703. // otherwise.
  704. //
  705. // Global Variables:
  706. //
  707. // Side Effects :
  708. //
  709. /----------------------------------------------------------------------*/
  710. Eqn_p EqnListFindTrue(Eqn_p list)
  711. {
  712. while(list)
  713. {
  714. if(EqnIsTrue(list))
  715. {
  716. break;
  717. }
  718. list = list->next;
  719. }
  720. return list;
  721. }
  722. /*-----------------------------------------------------------------------
  723. //
  724. // Function: EqnListIsTrivial()
  725. //
  726. // Return true if the list contains two equal literals with
  727. // opposing signs or a literal that always evaluates to true.
  728. //
  729. // Global Variables: -
  730. //
  731. // Side Effects : -
  732. //
  733. /----------------------------------------------------------------------*/
  734. bool EqnListIsTrivial(Eqn_p list)
  735. {
  736. Eqn_p handle;
  737. while(list)
  738. {
  739. if(EqnIsTrue(list))
  740. {
  741. return true;
  742. }
  743. for(handle = list->next; handle; handle = handle->next)
  744. {
  745. if(!PropsAreEquiv(handle, list, EPIsPositive))
  746. {
  747. if(EqnEqual(handle, list, TBTermEqual))
  748. {
  749. return true;
  750. }
  751. }
  752. }
  753. list = list->next;
  754. }
  755. return false;
  756. }
  757. /*-----------------------------------------------------------------------
  758. //
  759. // Function: EqnListIsACTrivial()
  760. //
  761. // Return true if the list contains a positive AC-trivial
  762. // equation.
  763. //
  764. // Global Variables: -
  765. //
  766. // Side Effects : -
  767. //
  768. /----------------------------------------------------------------------*/
  769. bool EqnListIsACTrivial(Eqn_p list)
  770. {
  771. while(list)
  772. {
  773. if(EqnIsPositive(list) && EqnIsACTrivial(list))
  774. {
  775. return true;
  776. }
  777. list = list->next;
  778. }
  779. return false;
  780. }
  781. /*-----------------------------------------------------------------------
  782. //
  783. // Function: EqnListIsGround()
  784. //
  785. // Return true if all equations in list are true, false otherwise.
  786. //
  787. // Global Variables: -
  788. //
  789. // Side Effects : -
  790. //
  791. /----------------------------------------------------------------------*/
  792. bool EqnListIsGround(Eqn_p list)
  793. {
  794. bool res = true;
  795. while(list)
  796. {
  797. if(!EqnIsGround(list))
  798. {
  799. res = false;
  800. break;
  801. }
  802. list = list->next;
  803. }
  804. return res;
  805. }
  806. /*-----------------------------------------------------------------------
  807. //
  808. // Function: EqnListIsEquational()
  809. //
  810. // Return true if any literal in the list is a true equations.
  811. //
  812. // Global Variables: -
  813. //
  814. // Side Effects : -
  815. //
  816. /----------------------------------------------------------------------*/
  817. bool EqnListIsEquational(Eqn_p list)
  818. {
  819. while(list)
  820. {
  821. if(EqnIsEquLit(list))
  822. {
  823. return true;
  824. }
  825. list = list->next;
  826. }
  827. return false;
  828. }
  829. /*-----------------------------------------------------------------------
  830. //
  831. // Function: EqnListIsPureEquational()
  832. //
  833. // Return true if all literals in the list are true equations.
  834. //
  835. // Global Variables: -
  836. //
  837. // Side Effects : -
  838. //
  839. /----------------------------------------------------------------------*/
  840. bool EqnListIsPureEquational(Eqn_p list)
  841. {
  842. while(list)
  843. {
  844. if(!EqnIsEquLit(list))
  845. {
  846. return false;
  847. }
  848. list = list->next;
  849. }
  850. return true;
  851. }
  852. /*-----------------------------------------------------------------------
  853. //
  854. // Function: EqnListOrient()
  855. //
  856. // Orient all the equations in list. Equations already oriented are
  857. // not reoriented! Return number of swapped equations.
  858. //
  859. // Global Variables: -
  860. //
  861. // Side Effects : Orients equations
  862. //
  863. /----------------------------------------------------------------------*/
  864. int EqnListOrient(OCB_p ocb, Eqn_p list)
  865. {
  866. int res = 0;
  867. Eqn_p handle;
  868. for(handle = list; handle; handle = handle->next)
  869. {
  870. if(EqnOrient(ocb, handle))
  871. {
  872. res++;
  873. }
  874. }
  875. return res;
  876. }
  877. /*-----------------------------------------------------------------------
  878. //
  879. // Function: EqnListMaximalLiterals()
  880. //
  881. // Determine for each literal wether it is maximal or not. Returns
  882. // number of maximal literals. Also determines strictly maximal
  883. // literals.
  884. //
  885. // Global Variables: -
  886. //
  887. // Side Effects : Sets the maximal flags of literals.
  888. //
  889. /----------------------------------------------------------------------*/
  890. int EqnListMaximalLiterals(OCB_p ocb, Eqn_p list)
  891. {
  892. Eqn_p handle, stepper;
  893. CompareResult cmp;
  894. int res = 0;
  895. res = EqnListSetProp(list, EPIsMaximal|EPIsStrictlyMaximal);
  896. for(handle = list; handle; handle = handle->next)
  897. {
  898. for(stepper = handle->next; stepper; stepper = stepper->next)
  899. {
  900. if(EqnIsMaximal(stepper) && EqnIsMaximal(handle))
  901. {
  902. cmp = LiteralCompare(ocb, handle, stepper);
  903. switch(cmp)
  904. {
  905. case to_greater:
  906. EqnDelProp(stepper, EPIsMaximal);
  907. EqnDelProp(stepper, EPIsStrictlyMaximal);
  908. res--;
  909. break;
  910. case to_lesser:
  911. EqnDelProp(handle, EPIsMaximal);
  912. EqnDelProp(handle, EPIsStrictlyMaximal);
  913. res--;
  914. break;
  915. case to_equal:
  916. EqnDelProp(stepper, EPIsStrictlyMaximal);
  917. EqnDelProp(handle, EPIsStrictlyMaximal);
  918. break;
  919. default:
  920. break;
  921. }
  922. }
  923. }
  924. }
  925. return res;
  926. }
  927. /*-----------------------------------------------------------------------
  928. //
  929. // Function: EqnListEqnIsMaximal()
  930. //
  931. // Return true if eqn is maximal with respect to list (i.e. if there
  932. // are no equations that dominate it), false otherwise. As above,
  933. // details of this may need change if the calculus changes.
  934. //
  935. // Global Variables: -
  936. //
  937. // Side Effects : -
  938. //
  939. /----------------------------------------------------------------------*/
  940. bool EqnListEqnIsMaximal(OCB_p ocb, Eqn_p list, Eqn_p eqn)
  941. {
  942. Eqn_p handle;
  943. bool res = true;
  944. CompareResult cmp;
  945. for(handle=list; handle; handle = handle->next)
  946. {
  947. if(handle!=eqn && EqnIsMaximal(handle))
  948. {
  949. cmp = LiteralCompare(ocb, handle, eqn);
  950. if(cmp == to_greater)
  951. {
  952. res = false;
  953. break;
  954. }
  955. }
  956. }
  957. /* printf("\n");
  958. EqnPrintOriginal(stdout, eqn); printf(" is max in ");
  959. EqnListPrint(stdout, list, ";", normal, true);
  960. printf(" ResNormal: %d\n", res); */
  961. return res;
  962. }
  963. /*-----------------------------------------------------------------------
  964. //
  965. // Function: EqnListEqnIsStrictlyMaximal()
  966. //
  967. // Return true if eqn is strictly maximal with respect to list
  968. // (i.e. if there are no equations that dominate it), false
  969. // otherwise. As above, details of this may need change if the
  970. // calculus changes.
  971. //
  972. // Global Variables: -
  973. //
  974. // Side Effects : -
  975. //
  976. /----------------------------------------------------------------------*/
  977. bool EqnListEqnIsStrictlyMaximal(OCB_p ocb, Eqn_p list, Eqn_p eqn)
  978. {
  979. Eqn_p handle;
  980. bool res = true;
  981. for(handle=list; handle&&res; handle = handle->next)
  982. {
  983. if(handle!=eqn && EqnIsMaximal(handle))
  984. {
  985. switch(LiteralCompare(ocb, handle, eqn))
  986. {
  987. case to_equal:
  988. case to_greater:
  989. res = false;
  990. break;
  991. default:
  992. break;
  993. }
  994. }
  995. }
  996. /* printf("\n");
  997. EqnPrintOriginal(stdout, eqn); printf(" is max in ");
  998. EqnListPrint(stdout, list, ";", normal, true);
  999. printf("ResStrict: %d\n", res); */
  1000. return res;
  1001. }
  1002. /*-----------------------------------------------------------------------
  1003. //
  1004. // Function: EqnListDeleteTermProperties()
  1005. //
  1006. // Delete the given properties for all term occurences in the
  1007. // eqnlist.
  1008. //
  1009. // Global Variables: -
  1010. //
  1011. // Side Effects : Changes term bank
  1012. //
  1013. /----------------------------------------------------------------------*/
  1014. void EqnListDeleteTermProperties(Eqn_p list, TermProperties props)
  1015. {
  1016. while(list)
  1017. {
  1018. TBRefDelProp(list->bank, &(list->lterm), props);
  1019. TBRefDelProp(list->bank, &(list->rterm), props);
  1020. list = list->next;
  1021. }
  1022. }
  1023. /*-----------------------------------------------------------------------
  1024. //
  1025. // Function: EqnListPrint()
  1026. //
  1027. // Print the list. Separate elements with the given separator
  1028. // (usually "," oder ";"). If negated is true, negate equations
  1029. // before printing (to allow for easy printing of clauses in
  1030. // implicational form).
  1031. //
  1032. // Global Variables: -
  1033. //
  1034. // Side Effects : Output
  1035. //
  1036. /----------------------------------------------------------------------*/
  1037. void EqnListPrint(FILE* out, Eqn_p list, char* sep,
  1038. bool negated, bool fullterms)
  1039. {
  1040. Eqn_p handle = list;
  1041. if(handle)
  1042. {
  1043. EqnPrint(out, handle, negated, fullterms);
  1044. while(handle->next)
  1045. {
  1046. handle = handle->next;
  1047. fputs(sep, out);
  1048. EqnPrint(out, handle, negated, fullterms);
  1049. }
  1050. }
  1051. }
  1052. /*-----------------------------------------------------------------------
  1053. //
  1054. // Function: EqnListTSTPPrint()
  1055. //
  1056. // Same as above, but without negation and uses TSTP literal format.
  1057. //
  1058. // Global Variables: -
  1059. //
  1060. // Side Effects : Output
  1061. //
  1062. /----------------------------------------------------------------------*/
  1063. void EqnListTSTPPrint(FILE* out, Eqn_p list, char* sep, bool fullterms)
  1064. {
  1065. Eqn_p handle = list;
  1066. if(handle)
  1067. {
  1068. EqnTSTPPrint(out, handle, fullterms);
  1069. while(handle->next)
  1070. {
  1071. handle = handle->next;
  1072. fputs(sep, out);
  1073. EqnTSTPPrint(out, handle, fullterms);
  1074. }
  1075. }
  1076. }
  1077. /*-----------------------------------------------------------------------
  1078. //
  1079. // Function: EqnListParse()
  1080. //
  1081. // Parse a list of equations, separated by Tokens of type sep.
  1082. //
  1083. // Global Variables: -
  1084. //
  1085. // Side Effects : Input, memory operations, by EqnParse()
  1086. //
  1087. /----------------------------------------------------------------------*/
  1088. Eqn_p EqnListParse(Scanner_p in, TB_p bank, TokenType sep)
  1089. {
  1090. Eqn_p handle = NULL,
  1091. list = NULL;
  1092. if(((ScannerGetFormat(in) == TPTPFormat) &&
  1093. TestInpTok(in, Plus|Hyphen))
  1094. ||
  1095. ((ScannerGetFormat(in) == LOPFormat) &&
  1096. TestInpTok(in, TermStartToken|TildeSign))
  1097. ||
  1098. ((ScannerGetFormat(in) == TSTPFormat) &&
  1099. TestInpTok(in, TermStartToken|TildeSign)))
  1100. {
  1101. list = EqnParse(in, bank);
  1102. handle = list;
  1103. while(TestInpTok(in,sep))
  1104. {
  1105. NextToken(in);
  1106. handle->next = EqnParse(in, bank);
  1107. handle = handle->next;
  1108. }
  1109. }
  1110. return list;
  1111. }
  1112. /*-----------------------------------------------------------------------
  1113. //
  1114. // Function: NormSubstEqnListExcept()
  1115. //
  1116. // Instantiate all variables in eqnlist (except for terms from
  1117. // except) with fresh variables from vars. Returns the old value
  1118. // for vars->v_count, i.e. the number of the first
  1119. // fresh variable used.
  1120. //
  1121. // Global Variables: -
  1122. //
  1123. // Side Effects : Builds substitution, instantiates variables
  1124. //
  1125. /----------------------------------------------------------------------*/
  1126. FunCode NormSubstEqnListExcept(Eqn_p list, Eqn_p except, Subst_p
  1127. subst, VarBank_p vars)
  1128. {
  1129. Eqn_p handle;
  1130. FunCode res = VarBankGetVCount(vars);
  1131. for(handle = list; handle; handle = handle->next)
  1132. {
  1133. if(handle!= except)
  1134. {
  1135. SubstNormEqn(handle, subst, vars);
  1136. }
  1137. }
  1138. return res;
  1139. }
  1140. /*-----------------------------------------------------------------------
  1141. //
  1142. // Function: EqnListDepth()
  1143. //
  1144. // Return the depth of an eqn-list (i.e. the maximal depth of a
  1145. // term).
  1146. //
  1147. // Global Variables: -
  1148. //
  1149. // Side Effects : -
  1150. //
  1151. /----------------------------------------------------------------------*/
  1152. long EqnListDepth(Eqn_p list)
  1153. {
  1154. long maxdepth = 0, eqndepth;
  1155. while(list)
  1156. {
  1157. eqndepth = EqnDepth(list);
  1158. maxdepth = MAX(maxdepth,eqndepth);
  1159. list = list->next;
  1160. }
  1161. return maxdepth;
  1162. }
  1163. /*-----------------------------------------------------------------------
  1164. //
  1165. // Function: EqnListAddSymbolDistribution()
  1166. //
  1167. // Count the number of occurences of function symbols in list and
  1168. // add them to dist_array, which has to be a pointer to an array of
  1169. // long that is sufficiently long (and preferably adequatly
  1170. // initialized).
  1171. //
  1172. // Global Variables: -
  1173. //
  1174. // Side Effects : -
  1175. //
  1176. /----------------------------------------------------------------------*/
  1177. void EqnListAddSymbolDistribution(Eqn_p list, long *dist_array)
  1178. {
  1179. while(list)
  1180. {
  1181. EqnAddSymbolDistribution(list, dist_array);
  1182. list = list->next;
  1183. }
  1184. }
  1185. /*-----------------------------------------------------------------------
  1186. //
  1187. // Function: EqnListAddSymbolDistribExist()
  1188. //
  1189. // Count the number of occurences of function symbols in list and
  1190. // add them to dist_array, which has to be a pointer to an array of
  1191. // long that is sufficiently long (and preferably adequatly
  1192. // initialized). Push occuring symbols onto exists (once).
  1193. //
  1194. // Global Variables: -
  1195. //
  1196. // Side Effects : -
  1197. //
  1198. /----------------------------------------------------------------------*/
  1199. void EqnListAddSymbolDistExist(Eqn_p list, long *dist_array, PStack_p exist)
  1200. {
  1201. while(list)
  1202. {
  1203. EqnAddSymbolDistExist(list, dist_array, exist);
  1204. list = list->next;
  1205. }
  1206. }
  1207. /*-----------------------------------------------------------------------
  1208. //
  1209. // Function: EqnListAddSymbolFeatures()
  1210. //
  1211. // Update features in feature_array with all equation in list.
  1212. //
  1213. // Global Variables: -
  1214. //
  1215. // Side Effects : -
  1216. //
  1217. /----------------------------------------------------------------------*/
  1218. void EqnListAddSymbolFeatures(Eqn_p list, PStack_p mod_stack, long *feature_array)
  1219. {
  1220. while(list)
  1221. {
  1222. EqnAddSymbolFeatures(list, mod_stack, feature_array);
  1223. list = list->next;
  1224. }
  1225. }
  1226. /*-----------------------------------------------------------------------
  1227. //
  1228. // Function: EqnListComputeFunctionRanks()
  1229. //
  1230. // Compute the occurance rank for all function symbols in list.
  1231. //
  1232. // Global Variables:
  1233. //
  1234. // Side Effects :
  1235. //
  1236. /----------------------------------------------------------------------*/
  1237. void EqnListComputeFunctionRanks(Eqn_p list, long *rank_array,
  1238. long* count)
  1239. {
  1240. while(list)
  1241. {
  1242. EqnComputeFunctionRanks(list, rank_array, count);
  1243. list = list->next;
  1244. }
  1245. }
  1246. /*-----------------------------------------------------------------------
  1247. //
  1248. // Function: EqnListCollectVariables()
  1249. //
  1250. // Add all variables in list to tree. Return number of distinct
  1251. // variables.
  1252. //
  1253. // Global Variables: -
  1254. //
  1255. // Side Effects : Memory operations
  1256. //
  1257. /----------------------------------------------------------------------*/
  1258. long EqnListCollectVariables(Eqn_p list, PTree_p *tree)
  1259. {
  1260. long res = 0;
  1261. while(list)
  1262. {
  1263. res+=EqnCollectVariables(list, tree);
  1264. list = list->next;
  1265. }
  1266. return res;
  1267. }
  1268. /*-----------------------------------------------------------------------
  1269. //
  1270. // Function: EqnListAddFunOccs()
  1271. //
  1272. // For each symbol in literals that is not already marked in
  1273. // f_occur, push it onto res_stack and mark its entry. Return
  1274. // number of symbols found.
  1275. //
  1276. // Global Variables:
  1277. //
  1278. // Side Effects :
  1279. //
  1280. /----------------------------------------------------------------------*/
  1281. long EqnListAddFunOccs(Eqn_p list,
  1282. PDArray_p f_occur,
  1283. PStack_p res_stack)
  1284. {
  1285. long res = 0;
  1286. while(list)
  1287. {
  1288. res+=EqnAddFunOccs(list, f_occur, res_stack);
  1289. list = list->next;
  1290. }
  1291. return res;
  1292. }
  1293. /*-----------------------------------------------------------------------
  1294. //
  1295. // Function: EqnListTermSetProp()
  1296. //
  1297. // Set prop in all terms in list.
  1298. //
  1299. // Global Variables: -
  1300. //
  1301. // Side Effects : None beyond the purpose
  1302. //
  1303. /----------------------------------------------------------------------*/
  1304. void EqnListTermSetProp(Eqn_p list, TermProperties props)
  1305. {
  1306. while(list)
  1307. {
  1308. EqnTermSetProp(list, props);
  1309. list = list->next;
  1310. }
  1311. }
  1312. /*-----------------------------------------------------------------------
  1313. //
  1314. // Function: EqnListTBTermDelPropCount()
  1315. //
  1316. // Delete prop in all terms in list, return number of termcells in
  1317. // which prop was set.
  1318. //
  1319. // Global Variables: -
  1320. //
  1321. // Side Effects : As above
  1322. //
  1323. /----------------------------------------------------------------------*/
  1324. long EqnListTBTermDelPropCount(Eqn_p list, TermProperties props)
  1325. {
  1326. long count = 0;
  1327. while(list)
  1328. {
  1329. count += EqnTBTermDelPropCount(list, props);
  1330. list = list->next;
  1331. }
  1332. return count;
  1333. }
  1334. /*-----------------------------------------------------------------------
  1335. //
  1336. // Function: EqnListTermDelProp()
  1337. //
  1338. // Delete prop in all terms in list, return number of termcells in
  1339. // which prop was set.
  1340. //
  1341. // Global Variables: -
  1342. //
  1343. // Side Effects : As above
  1344. //
  1345. /----------------------------------------------------------------------*/
  1346. long EqnListTermDelProp(Eqn_p list, TermProperties props)
  1347. {
  1348. long count = 0;
  1349. while(list)
  1350. {
  1351. EqnTermDelProp(list, props);
  1352. list = list->next;
  1353. }
  1354. return count;
  1355. }
  1356. /*-----------------------------------------------------------------------
  1357. //
  1358. // Function: EqnListCollectSubterms()
  1359. //
  1360. // Collect all subterms of list onto collector. Assumes that
  1361. // TPOpFlag is set if and only if the term is already in the
  1362. // collection. Returns the number of new terms found.
  1363. //
  1364. // Global Variables: -
  1365. //
  1366. // Side Effects : Sets the OpFlag of newly collected terms.
  1367. //
  1368. /----------------------------------------------------------------------*/
  1369. long EqnListCollectSubterms(Eqn_p list, PStack_p collector)
  1370. {
  1371. long res = 0;
  1372. while(list)
  1373. {
  1374. res+= EqnCollectSubterms(list, collector);
  1375. list = list->next;
  1376. }
  1377. return res;
  1378. }
  1379. /*---------------------------------------------------------------------*/
  1380. /* End of File */
  1381. /*---------------------------------------------------------------------*/