irewrite.sl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. % {DSK}IREWRITE.PSL;2 6-JAN-83 10:08:06
  2. (FLUID '(unify-subst))
  3. (FLAG '(
  4. ADD-LEMMA
  5. ADD-LEMMA-LST
  6. Apply-subst
  7. Apply-subst-lst
  8. false
  9. one-way-unify
  10. one-way-unify1
  11. one-way-unify1-lst
  12. ptime
  13. rewrite
  14. rewrite-with-lemmas
  15. tautologyP
  16. tautp
  17. trans-of-implies
  18. trans-of-implies1
  19. truep
  20. ) 'InternalFunction)
  21. (DE ADD-LEMMA (TERM)
  22. (COND ((AND (NOT (ATOM TERM))
  23. (EQ (CAR TERM)
  24. 'EQUAL)
  25. (NOT (ATOM (CADR TERM))))
  26. (PUT (CAR (CADR TERM))
  27. 'LEMMAS
  28. (CONS TERM (GET (CAR (CADR TERM))
  29. 'LEMMAS))))
  30. (T (ERROR 0 (LIST 'ADD-LEMMA-DID-NOT-LIKE-TERM
  31. TERM)))))
  32. (DE ADD-LEMMA-LST (LST)
  33. (COND ((NULL LST)
  34. T)
  35. (T (ADD-LEMMA (CAR LST))
  36. (ADD-LEMMA-LST (CDR LST)))))
  37. % lmm 7-JUN-81 10:07
  38. (DE APPLY-SUBST (ALIST TERM)
  39. (COND ((NOT (PAIRP TERM))
  40. ((LAMBDA (TEM)
  41. (COND
  42. (TEM (CDR TEM))
  43. (T TERM)))
  44. (ASSOC TERM ALIST)))
  45. (T (CONS (CAR TERM)
  46. (MAPCAR (CDR TERM)
  47. (FUNCTION (LAMBDA (X)
  48. (APPLY-SUBST ALIST X))))))))
  49. (DE APPLY-SUBST-LST (ALIST LST)
  50. (COND ((NULL LST)
  51. NIL)
  52. (T (CONS (APPLY-SUBST ALIST (CAR LST))
  53. (APPLY-SUBST-LST ALIST (CDR LST))))))
  54. (DE FALSEP (X LST)
  55. (OR (EQUAL X '(F))
  56. (MEMBER X LST)))
  57. (DE ONE-WAY-UNIFY (TERM1 TERM2)
  58. (PROGN (SETQ UNIFY-SUBST NIL)
  59. (ONE-WAY-UNIFY1 TERM1 TERM2)))
  60. % lmm 7-JUN-81 09:47
  61. (DE ONE-WAY-UNIFY1 (TERM1 TERM2)
  62. (COND ((NOT (PAIRP TERM2))
  63. ((LAMBDA (TEM)
  64. (COND
  65. (TEM (EQUAL TERM1 (CDR TEM)))
  66. (T (SETQ UNIFY-SUBST (CONS (CONS TERM2 TERM1)
  67. UNIFY-SUBST))
  68. T)))
  69. (ASSOC TERM2 UNIFY-SUBST)))
  70. ((NOT (PAIRP TERM1))
  71. NIL)
  72. ((EQ (CAR TERM1)
  73. (CAR TERM2))
  74. (ONE-WAY-UNIFY1-LST (CDR TERM1)
  75. (CDR TERM2)))
  76. (T NIL)))
  77. (DE ONE-WAY-UNIFY1-LST (LST1 LST2)
  78. (COND ((NULL LST1)
  79. T)
  80. ((ONE-WAY-UNIFY1 (CAR LST1)
  81. (CAR LST2))
  82. (ONE-WAY-UNIFY1-LST (CDR LST1)
  83. (CDR LST2)))
  84. (T NIL)))
  85. (DE PTIME NIL
  86. (PROG (GCTM)
  87. (SETQ GCTM 0)
  88. (RETURN (CONS (time)
  89. GCTM))))
  90. % lmm 7-JUN-81 10:04
  91. (DE REWRITE (TERM)
  92. (COND ((NOT (PAIRP TERM))
  93. TERM)
  94. (T (REWRITE-WITH-LEMMAS (CONS (CAR TERM)
  95. (MAPCAR (CDR TERM)
  96. (FUNCTION REWRITE)))
  97. (GET (CAR TERM)
  98. 'LEMMAS)))))
  99. (DE REWRITE-WITH-LEMMAS (TERM LST)
  100. (COND ((NULL LST)
  101. TERM)
  102. ((ONE-WAY-UNIFY TERM (CADR (CAR LST)))
  103. (REWRITE (APPLY-SUBST UNIFY-SUBST (CADDR (CAR LST)))))
  104. (T (REWRITE-WITH-LEMMAS TERM (CDR LST)))))
  105. (DE SETUP NIL
  106. (ADD-LEMMA-LST
  107. '((EQUAL (COMPILE FORM)
  108. (REVERSE (CODEGEN (OPTIMIZE FORM)
  109. (NIL))))
  110. (EQUAL (EQP X Y)
  111. (EQUAL (FIX X)
  112. (FIX Y)))
  113. (EQUAL (GREATERP X Y)
  114. (LESSP Y X))
  115. (EQUAL (LESSEQP X Y)
  116. (NOT (LESSP Y X)))
  117. (EQUAL (GREATEREQP X Y)
  118. (NOT (LESSP X Y)))
  119. (EQUAL (BOOLEAN X)
  120. (OR (EQUAL X (T))
  121. (EQUAL X (F))))
  122. (EQUAL (IFF X Y)
  123. (AND (IMPLIES X Y)
  124. (IMPLIES Y X)))
  125. (EQUAL (EVEN1 X)
  126. (IF (ZEROP X)
  127. (T)
  128. (ODD (SUB1 X))))
  129. (EQUAL (COUNTPS- L PRED)
  130. (COUNTPS-LOOP L PRED (ZERO)))
  131. (EQUAL (FACT- I)
  132. (FACT-LOOP I 1))
  133. (EQUAL (REVERSE- X)
  134. (REVERSE-LOOP X (NIL)))
  135. (EQUAL (DIVIDES X Y)
  136. (ZEROP (REMAINDER Y X)))
  137. (EQUAL (ASSUME-TRUE VAR ALIST)
  138. (CONS (CONS VAR (T))
  139. ALIST))
  140. (EQUAL (ASSUME-FALSE VAR ALIST)
  141. (CONS (CONS VAR (F))
  142. ALIST))
  143. (EQUAL (TAUTOLOGY-CHECKER X)
  144. (TAUTOLOGYP (NORMALIZE X)
  145. (NIL)))
  146. (EQUAL (FALSIFY X)
  147. (FALSIFY1 (NORMALIZE X)
  148. (NIL)))
  149. (EQUAL (PRIME X)
  150. (AND (NOT (ZEROP X))
  151. (NOT (EQUAL X (ADD1 (ZERO))))
  152. (PRIME1 X (SUB1 X))))
  153. (EQUAL (AND P Q)
  154. (IF P (IF Q (T)
  155. (F))
  156. (F)))
  157. (EQUAL (OR P Q)
  158. (IF P (T)
  159. (IF Q (T)
  160. (F))
  161. (F)))
  162. (EQUAL (NOT P)
  163. (IF P (F)
  164. (T)))
  165. (EQUAL (IMPLIES P Q)
  166. (IF P (IF Q (T)
  167. (F))
  168. (T)))
  169. (EQUAL (FIX X)
  170. (IF (NUMBERP X)
  171. X
  172. (ZERO)))
  173. (EQUAL (IF (IF A B C)
  174. D E)
  175. (IF A (IF B D E)
  176. (IF C D E)))
  177. (EQUAL (ZEROP X)
  178. (OR (EQUAL X (ZERO))
  179. (NOT (NUMBERP X))))
  180. (EQUAL (PLUS (PLUS X Y)
  181. Z)
  182. (PLUS X (PLUS Y Z)))
  183. (EQUAL (EQUAL (PLUS A B)
  184. (ZERO))
  185. (AND (ZEROP A)
  186. (ZEROP B)))
  187. (EQUAL (DIFFERENCE X X)
  188. (ZERO))
  189. (EQUAL (EQUAL (PLUS A B)
  190. (PLUS A C))
  191. (EQUAL (FIX B)
  192. (FIX C)))
  193. (EQUAL (EQUAL (ZERO)
  194. (DIFFERENCE X Y))
  195. (NOT (LESSP Y X)))
  196. (EQUAL (EQUAL X (DIFFERENCE X Y))
  197. (AND (NUMBERP X)
  198. (OR (EQUAL X (ZERO))
  199. (ZEROP Y))))
  200. (EQUAL (MEANING (PLUS-TREE (APPEND X Y))
  201. A)
  202. (PLUS (MEANING (PLUS-TREE X)
  203. A)
  204. (MEANING (PLUS-TREE Y)
  205. A)))
  206. (EQUAL (MEANING (PLUS-TREE (PLUS-FRINGE X))
  207. A)
  208. (FIX (MEANING X A)))
  209. (EQUAL (APPEND (APPEND X Y)
  210. Z)
  211. (APPEND X (APPEND Y Z)))
  212. (EQUAL (REVERSE (APPEND A B))
  213. (APPEND (REVERSE B)
  214. (REVERSE A)))
  215. (EQUAL (TIMES X (PLUS Y Z))
  216. (PLUS (TIMES X Y)
  217. (TIMES X Z)))
  218. (EQUAL (TIMES (TIMES X Y)
  219. Z)
  220. (TIMES X (TIMES Y Z)))
  221. (EQUAL (EQUAL (TIMES X Y)
  222. (ZERO))
  223. (OR (ZEROP X)
  224. (ZEROP Y)))
  225. (EQUAL (EXEC (APPEND X Y)
  226. PDS ENVRN)
  227. (EXEC Y (EXEC X PDS ENVRN)
  228. ENVRN))
  229. (EQUAL (MC-FLATTEN X Y)
  230. (APPEND (FLATTEN X)
  231. Y))
  232. (EQUAL (MEMBER X (APPEND A B))
  233. (OR (MEMBER X A)
  234. (MEMBER X B)))
  235. (EQUAL (MEMBER X (REVERSE Y))
  236. (MEMBER X Y))
  237. (EQUAL (LENGTH (REVERSE X))
  238. (LENGTH X))
  239. (EQUAL (MEMBER A (INTERSECT B C))
  240. (AND (MEMBER A B)
  241. (MEMBER A C)))
  242. (EQUAL (NTH (ZERO)
  243. I)
  244. (ZERO))
  245. (EQUAL (EXP I (PLUS J K))
  246. (TIMES (EXP I J)
  247. (EXP I K)))
  248. (EQUAL (EXP I (TIMES J K))
  249. (EXP (EXP I J)
  250. K))
  251. (EQUAL (REVERSE-LOOP X Y)
  252. (APPEND (REVERSE X)
  253. Y))
  254. (EQUAL (REVERSE-LOOP X (NIL))
  255. (REVERSE X))
  256. (EQUAL (COUNT-LIST Z (SORT-LP X Y))
  257. (PLUS (COUNT-LIST Z X)
  258. (COUNT-LIST Z Y)))
  259. (EQUAL (EQUAL (APPEND A B)
  260. (APPEND A C))
  261. (EQUAL B C))
  262. (EQUAL (PLUS (REMAINDER X Y)
  263. (TIMES Y (QUOTIENT X Y)))
  264. (FIX X))
  265. (EQUAL (POWER-EVAL (BIG-PLUS1 L I BASE)
  266. BASE)
  267. (PLUS (POWER-EVAL L BASE)
  268. I))
  269. (EQUAL (POWER-EVAL (BIG-PLUS X Y I BASE)
  270. BASE)
  271. (PLUS I (PLUS (POWER-EVAL X BASE)
  272. (POWER-EVAL Y BASE))))
  273. (EQUAL (REMAINDER Y 1)
  274. (ZERO))
  275. (EQUAL (LESSP (REMAINDER X Y)
  276. Y)
  277. (NOT (ZEROP Y)))
  278. (EQUAL (REMAINDER X X)
  279. (ZERO))
  280. (EQUAL (LESSP (QUOTIENT I J)
  281. I)
  282. (AND (NOT (ZEROP I))
  283. (OR (ZEROP J)
  284. (NOT (EQUAL J 1)))))
  285. (EQUAL (LESSP (REMAINDER X Y)
  286. X)
  287. (AND (NOT (ZEROP Y))
  288. (NOT (ZEROP X))
  289. (NOT (LESSP X Y))))
  290. (EQUAL (POWER-EVAL (POWER-REP I BASE)
  291. BASE)
  292. (FIX I))
  293. (EQUAL (POWER-EVAL (BIG-PLUS (POWER-REP I BASE)
  294. (POWER-REP J BASE)
  295. (ZERO)
  296. BASE)
  297. BASE)
  298. (PLUS I J))
  299. (EQUAL (GCD X Y)
  300. (GCD Y X))
  301. (EQUAL (NTH (APPEND A B)
  302. I)
  303. (APPEND (NTH A I)
  304. (NTH B (DIFFERENCE I (LENGTH A)))))
  305. (EQUAL (DIFFERENCE (PLUS X Y)
  306. X)
  307. (FIX Y))
  308. (EQUAL (DIFFERENCE (PLUS Y X)
  309. X)
  310. (FIX Y))
  311. (EQUAL (DIFFERENCE (PLUS X Y)
  312. (PLUS X Z))
  313. (DIFFERENCE Y Z))
  314. (EQUAL (TIMES X (DIFFERENCE C W))
  315. (DIFFERENCE (TIMES C X)
  316. (TIMES W X)))
  317. (EQUAL (REMAINDER (TIMES X Z)
  318. Z)
  319. (ZERO))
  320. (EQUAL (DIFFERENCE (PLUS B (PLUS A C))
  321. A)
  322. (PLUS B C))
  323. (EQUAL (DIFFERENCE (ADD1 (PLUS Y Z))
  324. Z)
  325. (ADD1 Y))
  326. (EQUAL (LESSP (PLUS X Y)
  327. (PLUS X Z))
  328. (LESSP Y Z))
  329. (EQUAL (LESSP (TIMES X Z)
  330. (TIMES Y Z))
  331. (AND (NOT (ZEROP Z))
  332. (LESSP X Y)))
  333. (EQUAL (LESSP Y (PLUS X Y))
  334. (NOT (ZEROP X)))
  335. (EQUAL (GCD (TIMES X Z)
  336. (TIMES Y Z))
  337. (TIMES Z (GCD X Y)))
  338. (EQUAL (VALUE (NORMALIZE X)
  339. A)
  340. (VALUE X A))
  341. (EQUAL (EQUAL (FLATTEN X)
  342. (CONS Y (NIL)))
  343. (AND (NLISTP X)
  344. (EQUAL X Y)))
  345. (EQUAL (LISTP (GOPHER X))
  346. (LISTP X))
  347. (EQUAL (SAMEFRINGE X Y)
  348. (EQUAL (FLATTEN X)
  349. (FLATTEN Y)))
  350. (EQUAL (EQUAL (GREATEST-FACTOR X Y)
  351. (ZERO))
  352. (AND (OR (ZEROP Y)
  353. (EQUAL Y 1))
  354. (EQUAL X (ZERO))))
  355. (EQUAL (EQUAL (GREATEST-FACTOR X Y)
  356. 1)
  357. (EQUAL X 1))
  358. (EQUAL (NUMBERP (GREATEST-FACTOR X Y))
  359. (NOT (AND (OR (ZEROP Y)
  360. (EQUAL Y 1))
  361. (NOT (NUMBERP X)))))
  362. (EQUAL (TIMES-LIST (APPEND X Y))
  363. (TIMES (TIMES-LIST X)
  364. (TIMES-LIST Y)))
  365. (EQUAL (PRIME-LIST (APPEND X Y))
  366. (AND (PRIME-LIST X)
  367. (PRIME-LIST Y)))
  368. (EQUAL (EQUAL Z (TIMES W Z))
  369. (AND (NUMBERP Z)
  370. (OR (EQUAL Z (ZERO))
  371. (EQUAL W 1))))
  372. (EQUAL (GREATEREQPR X Y)
  373. (NOT (LESSP X Y)))
  374. (EQUAL (EQUAL X (TIMES X Y))
  375. (OR (EQUAL X (ZERO))
  376. (AND (NUMBERP X)
  377. (EQUAL Y 1))))
  378. (EQUAL (REMAINDER (TIMES Y X)
  379. Y)
  380. (ZERO))
  381. (EQUAL (EQUAL (TIMES A B)
  382. 1)
  383. (AND (NOT (EQUAL A (ZERO)))
  384. (NOT (EQUAL B (ZERO)))
  385. (NUMBERP A)
  386. (NUMBERP B)
  387. (EQUAL (SUB1 A)
  388. (ZERO))
  389. (EQUAL (SUB1 B)
  390. (ZERO))))
  391. (EQUAL (LESSP (LENGTH (DELETE X L))
  392. (LENGTH L))
  393. (MEMBER X L))
  394. (EQUAL (SORT2 (DELETE X L))
  395. (DELETE X (SORT2 L)))
  396. (EQUAL (DSORT X)
  397. (SORT2 X))
  398. (EQUAL (LENGTH (CONS X1 (CONS X2 (CONS X3 (CONS X4
  399. (CONS X5 (CONS X6 X7)))))))
  400. (PLUS 6 (LENGTH X7)))
  401. (EQUAL (DIFFERENCE (ADD1 (ADD1 X))
  402. 2)
  403. (FIX X))
  404. (EQUAL (QUOTIENT (PLUS X (PLUS X Y))
  405. 2)
  406. (PLUS X (QUOTIENT Y 2)))
  407. (EQUAL (SIGMA (ZERO)
  408. I)
  409. (QUOTIENT (TIMES I (ADD1 I))
  410. 2))
  411. (EQUAL (PLUS X (ADD1 Y))
  412. (IF (NUMBERP Y)
  413. (ADD1 (PLUS X Y))
  414. (ADD1 X)))
  415. (EQUAL (EQUAL (DIFFERENCE X Y)
  416. (DIFFERENCE Z Y))
  417. (IF (LESSP X Y)
  418. (NOT (LESSP Y Z))
  419. (IF (LESSP Z Y)
  420. (NOT (LESSP Y X))
  421. (EQUAL (FIX X)
  422. (FIX Z)))))
  423. (EQUAL (MEANING (PLUS-TREE (DELETE X Y))
  424. A)
  425. (IF (MEMBER X Y)
  426. (DIFFERENCE (MEANING (PLUS-TREE Y)
  427. A)
  428. (MEANING X A))
  429. (MEANING (PLUS-TREE Y)
  430. A)))
  431. (EQUAL (TIMES X (ADD1 Y))
  432. (IF (NUMBERP Y)
  433. (PLUS X (TIMES X Y))
  434. (FIX X)))
  435. (EQUAL (NTH (NIL)
  436. I)
  437. (IF (ZEROP I)
  438. (NIL)
  439. (ZERO)))
  440. (EQUAL (LAST (APPEND A B))
  441. (IF (LISTP B)
  442. (LAST B)
  443. (IF (LISTP A)
  444. (CONS (CAR (LAST A))
  445. B)
  446. B)))
  447. (EQUAL (EQUAL (LESSP X Y)
  448. Z)
  449. (IF (LESSP X Y)
  450. (EQUAL T Z)
  451. (EQUAL F Z)))
  452. (EQUAL (ASSIGNMENT X (APPEND A B))
  453. (IF (ASSIGNEDP X A)
  454. (ASSIGNMENT X A)
  455. (ASSIGNMENT X B)))
  456. (EQUAL (CAR (GOPHER X))
  457. (IF (LISTP X)
  458. (CAR (FLATTEN X))
  459. (ZERO)))
  460. (EQUAL (FLATTEN (CDR (GOPHER X)))
  461. (IF (LISTP X)
  462. (CDR (FLATTEN X))
  463. (CONS (ZERO)
  464. (NIL))))
  465. (EQUAL (QUOTIENT (TIMES Y X)
  466. Y)
  467. (IF (ZEROP Y)
  468. (ZERO)
  469. (FIX X)))
  470. (EQUAL (GET J (SET I VAL MEM))
  471. (IF (EQP J I)
  472. VAL
  473. (GET J MEM))))))
  474. % lmm 7-JUN-81 09:44
  475. (DE TAUTOLOGYP (X TRUE-LST FALSE-LST)
  476. (COND ((TRUEP X TRUE-LST)
  477. T)
  478. ((FALSEP X FALSE-LST)
  479. NIL)
  480. ((NOT (PAIRP X))
  481. NIL)
  482. ((EQ (CAR X)
  483. 'IF)
  484. (COND ((TRUEP (CADR X)
  485. TRUE-LST)
  486. (TAUTOLOGYP (CADDR X)
  487. TRUE-LST FALSE-LST))
  488. ((FALSEP (CADR X)
  489. FALSE-LST)
  490. (TAUTOLOGYP (CADDDR X)
  491. TRUE-LST FALSE-LST))
  492. (T (AND (TAUTOLOGYP (CADDR X)
  493. (CONS (CADR X)
  494. TRUE-LST)
  495. FALSE-LST)
  496. (TAUTOLOGYP (CADDDR X)
  497. TRUE-LST
  498. (CONS (CADR X)
  499. FALSE-LST))))))
  500. (T NIL)))
  501. (DE TAUTP (X)
  502. (TAUTOLOGYP (REWRITE X)
  503. NIL NIL))
  504. (DE TEST NIL
  505. (PROG (TM1 TM2 ANS TERM)
  506. (SETQ TM1 (PTIME))
  507. (SETQ TERM (APPLY-SUBST '((X F (PLUS (PLUS A B)
  508. (PLUS C (ZERO))))
  509. (Y F (TIMES (TIMES A B)
  510. (PLUS C D)))
  511. (Z F (REVERSE (APPEND (APPEND A B)
  512. (NIL))))
  513. (U EQUAL (PLUS A B)
  514. (DIFFERENCE X Y))
  515. (W LESSP (REMAINDER A B)
  516. (MEMBER A (LENGTH B))))
  517. '(IMPLIES (AND (IMPLIES X Y)
  518. (AND (IMPLIES Y Z)
  519. (AND (IMPLIES Z U)
  520. (IMPLIES U W))))
  521. (IMPLIES X W))))
  522. (SETQ ANS (TAUTP TERM))
  523. (SETQ TM2 (PTIME))
  524. (RETURN (LIST ANS (DIFFERENCE (CAR TM2)
  525. (CAR TM1))
  526. (DIFFERENCE (CDR TM2)
  527. (CDR TM1))))))
  528. (DE TRANS-OF-IMPLIES (N)
  529. (LIST 'IMPLIES
  530. (TRANS-OF-IMPLIES1 N)
  531. (LIST 'IMPLIES
  532. 0 N)))
  533. (DE TRANS-OF-IMPLIES1 (N)
  534. (COND ((EQUAL N 1)
  535. (LIST 'IMPLIES
  536. 0 1))
  537. (T (LIST 'AND
  538. (LIST 'IMPLIES
  539. (SUB1 N)
  540. N)
  541. (TRANS-OF-IMPLIES1 (SUB1 N))))))
  542. (DE TRUEP (X LST)
  543. (OR (EQUAL X '(T))
  544. (MEMBER X LST)))