pasfsiat.red 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. % ----------------------------------------------------------------------
  2. % $Id: pasfsiat.red,v 1.31 2003/12/16 07:45:34 lasaruk Exp $
  3. % ----------------------------------------------------------------------
  4. % Copyright (c) 2003 A. Dolzmann, A. Seidl, and T. Sturm
  5. % ----------------------------------------------------------------------
  6. % $Log: pasfsiat.red,v $
  7. % Revision 1.31 2003/12/16 07:45:34 lasaruk
  8. % Redlog normal form in the simplifier.
  9. %
  10. % Revision 1.30 2003/12/11 14:23:18 sturm
  11. % Do not use domain gcd in pasf_gcd(). Not relevant QE appears to spend
  12. % around 9% of time there.
  13. %
  14. % Revision 1.29 2003/11/07 12:07:52 sturm
  15. % Fixed a bug in pasf_mkpos.
  16. %
  17. % Revision 1.28 2003/11/05 13:56:19 lasaruk
  18. % Some more changes. pasf_content renamed to pasf_gcd with more
  19. % exact specificaton. lisp, symbolic and some "comments" are removed.
  20. %
  21. % Revision 1.27 2003/11/05 13:27:14 lasaruk
  22. % Some major redlog programming rules applied to the code.
  23. % Formulas are made positive acc. to the current kernel order.
  24. %
  25. % Revision 1.26 2003/10/28 09:59:11 dolzmann
  26. % Added correct content of fluids pasf_siat_rcsid!* and
  27. % pasf_siat_copyright!*.
  28. %
  29. % Revision 1.25 2003/10/28 09:56:36 dolzmann
  30. % Removed trailing spaces.
  31. % Changed true to 'true.
  32. %
  33. % Revision 1.24 2003/09/09 10:56:17 lasaruk
  34. % check for correct form improoved
  35. %
  36. % Revision 1.23 2003/08/28 15:30:40 lasaruk
  37. % Simplification verbose output done better. QE-Bug with truth values
  38. % corrected (will be done more effitient). Some fancy examples added.
  39. %
  40. % Revision 1.22 2003/08/27 16:10:04 lasaruk
  41. % Added switch rlpasfatfsimpvb to print out simplification steps if
  42. % simplification was really done. Check for correct PASF form added.
  43. %
  44. % Revision 1.21 2003/08/12 10:33:05 lasaruk
  45. % Value evaluation bug removed.
  46. %
  47. % Revision 1.20 2003/08/12 09:38:55 lasaruk
  48. % Absent atomic formula simplification cases added. Testfile
  49. % expanded. Testcases from Andreas checked.
  50. %
  51. % Revision 1.19 2003/08/05 12:05:14 lasaruk
  52. % Standard simplification completely rewritten.
  53. %
  54. % Revision 1.18 2003/08/05 08:57:17 seidl
  55. % Intermediate check-in.
  56. %
  57. % Revision 1.17 2003/07/22 08:45:03 seidl
  58. % Improved simplifiations of equations and negated equations. Still there
  59. % can be done more. Simplification of atomic formulas has to be thoroughly
  60. % revised.
  61. %
  62. % Revision 1.16 2003/07/10 07:54:30 seidl
  63. % Added cvs header and logs up to 1.15.
  64. %
  65. % ----------------------------------------------------------------------
  66. % revision 1.15
  67. % date: 2003/04/20 12:04:04; author: lasaruk; state: Exp; lines: +0 -3
  68. % Completely removed any reference to range predicates (in input
  69. % also). PNF made shorter.
  70. % ----------------------------
  71. % revision 1.14
  72. % date: 2003/04/14 10:11:39; author: lasaruk; state: Exp; lines: +4 -1
  73. % Changes to work with bounded quantifieres added . Simplification bug
  74. % (content) removed. Range predicates removed.
  75. % ----------------------------
  76. % revision 1.13
  77. % date: 2003/03/04 09:33:23; author: lasaruk; state: Exp; lines: +64 -30
  78. % Advanced simplification. PNF code attached but not used yet. Some code
  79. % migration. Documentation debugged.
  80. % ----------------------------
  81. % revision 1.12
  82. % date: 2003/02/28 11:55:40; author: lasaruk; state: Exp; lines: +55 -90
  83. % Simplifier congruence bug removed. Switch siatadv now actively used.
  84. % ----------------------------
  85. % revision 1.11
  86. % date: 2003/02/17 10:55:40; author: lasaruk; state: Exp; lines: +22 -15
  87. % Stable full featured version
  88. % ----------------------------
  89. % revision 1.10
  90. % date: 2003/01/21 17:39:14; author: lasaruk; state: Exp; lines: +13 -13
  91. % Switch rlsiatadv turned off. Bugs fixed.
  92. % ----------------------------
  93. % revision 1.9
  94. % date: 2003/01/06 18:20:32; author: lasaruk; state: Exp; lines: +5 -5
  95. % Bugs fixed
  96. % ----------------------------
  97. % revision 1.8
  98. % date: 2003/01/06 17:33:27; author: lasaruk; state: Exp; lines: +5 -7
  99. % Some simplifier bugs fixed. Alternating quantifier elimination attached.
  100. % ----------------------------
  101. % revision 1.7
  102. % date: 2003/01/05 15:55:05; author: lasaruk; state: Exp; lines: +7 -5
  103. % Simplification improoved. Expansion of range predicates added.
  104. % ----------------------------
  105. % revision 1.6
  106. % date: 2002/12/31 13:57:49; author: lasaruk; state: Exp; lines: +5 -5
  107. % Simplifier bugs fixed.
  108. % ----------------------------
  109. % revision 1.5
  110. % date: 2002/12/31 13:33:34; author: lasaruk; state: Exp; lines: +44 -21
  111. % Standard simplifier attached. Standard simplification of expressions
  112. % attached.
  113. % ----------------------------
  114. % revision 1.4
  115. % date: 2002/12/23 07:07:40; author: lasaruk; state: Exp; lines: +15 -15
  116. % Simplifier corrected
  117. % ----------------------------
  118. % revision 1.3
  119. % date: 2002/10/18 13:39:11; author: lasaruk; state: Exp; lines: +1 -1
  120. % QE one variable preparation added. No bounded quantifiers first.
  121. % ----------------------------
  122. % revision 1.2
  123. % date: 2002/10/10 09:09:20; author: lasaruk; state: Exp; lines: +15 -11
  124. % Range predicate implemented. Todo: logical negation of range predicate
  125. % ----------------------------
  126. % revision 1.1
  127. % date: 2002/10/02 14:31:19; author: lasaruk; state: Exp;
  128. % Initial check in. Only dummy methods for advanced simplification first.
  129. % ======================================================================
  130. lisp <<
  131. fluid '(pasf_siat_rcsid!* pasf_siat_copyright!*);
  132. pasf_siat_rcsid!* := "$Id: pasfsiat.red,v 1.31 2003/12/16 07:45:34 lasaruk Exp $";
  133. pasf_siat_copyright!* :=
  134. "Copyright (c) 2003 A. Dolzmann, A. Seidl, and T. Sturm"
  135. >>;
  136. module pasfsiat;
  137. % Presburger arithmetic standard form simplification. Submodule of
  138. % [pasf].
  139. % Datastructure <TNF> for term normal form.
  140. % <TMF> = (<CONST>,<COEF>,<VARS>)
  141. % <CONST> = a_0 is the integer constant part of the term
  142. % <COEF> = (a_1,a_2,a_3, ... , a_n)
  143. % <VARS> = (x_1,x_2,x_3, ... , x_n)
  144. % The interpretation of <TNF> as a term in normal form is
  145. % $t = a_0 + \sum_{i=1}^{n} a_i x_i$
  146. procedure tnf_atf2tnf(atf);
  147. % Term normal form constructor from an atomic formula. [atf] is an
  148. % atomic formula in redlog normal form. Returns a new <TNF>
  149. % structure for the left hand side of [atf].
  150. tnf_expr2tnf pasf_arg2l atf;
  151. procedure tnf_expr2tnf(exps);
  152. % Term normal form constructor from an expression. [exps] is an
  153. % expression. Returns a new <TNF> structure for the expression
  154. % [exps].
  155. begin scalar as,xs;
  156. while not domainp exps do <<
  157. if ldeg exps <> 1 then
  158. rederr {"termnf_atf2tnf : invalid PASF formula :",
  159. "polynome degree for",mvar exps,"is greater than 1"};
  160. if not domainp lc exps then
  161. rederr {"termnf_atf2tnf : invalid PASF formula :",
  162. "non constant leading coefitient"};
  163. as := (if lc exps then lc exps else 0) . as;
  164. xs := (mvar exps) . xs;
  165. exps := red exps
  166. >>;
  167. % Replacing "nil" by 0
  168. return {if exps then exps else 0,as,xs}
  169. end;
  170. procedure tnf_new(const,coef,vars);
  171. % Term normal form constructor. [const] ist the constant part of
  172. % the term; [coef] are the variable coefitients; [vars] are the
  173. % free variables. Returns a <TNF> representing the term.
  174. {if const then const else 0,coef,vars};
  175. procedure tnf_const(tnf);
  176. % Term normal form accessor for the constant part of the term.
  177. % [tnf] is a <TNF> structure. Returns the constant part of the
  178. % term.
  179. car tnf;
  180. procedure tnf_coef(tnf);
  181. % Term normal form accessor for the coefitients of the term. [tnf]
  182. % is a <TNF> structure. Returns the coefitients of the term.
  183. cadr tnf;
  184. procedure tnf_vars(tnf);
  185. % Term normal form accessor for the cariables of the term. [tnf] is
  186. % a <TNF> structure. Returns the variables of the term.
  187. caddr tnf;
  188. procedure tnf_tnf2expr(tnf);
  189. % Term normal form accessor for the term structure. [tnf] is
  190. % a <TNF> structure. Returns a term derived from the <TNF>.
  191. begin scalar term,vars;
  192. vars := tnf_vars tnf;
  193. for each coef in tnf_coef tnf do <<
  194. term := addf(term,multf(numr simp coef,numr simp car vars));
  195. vars := cdr vars
  196. >>;
  197. return addf(numr simp tnf_const tnf,term)
  198. end;
  199. procedure pasf_simplat1(atf,sop);
  200. % Presburger arithmetic standard form simplify atomic formula.
  201. % [atf] is an atomic formula; [sop] is the boolean operator [atf]
  202. % occurs with or [nil]. Accesses switches [rlsiatadv]. Returns a
  203. % quantifier-free formula that is a simplified equivalent of [atf].
  204. begin scalar tnf,m,tnfm,c;
  205. % Verbose check for simplification
  206. if !*rlpasfatfsimpvb then <<
  207. prin2t "Simplifying atomic formula";
  208. mathprint rl_mk!*fof atf
  209. >>;
  210. % Conversion to normal form (NF)
  211. atf := pasf_mkpos atf;
  212. tnf := tnf_atf2tnf atf;
  213. % Evaluation of variable free terms (VF)
  214. if null tnf_vars tnf then <<
  215. % Verbose check for simplification
  216. if !*rlpasfatfsimpvb then
  217. prin2!* "[VF]";
  218. return if pasf_evalatp(pasf_op atf,tnf_const tnf) then
  219. 'true
  220. else
  221. 'false;
  222. >>;
  223. % Congruences are treated differently as non-congruences
  224. if pasf_opn atf memq '(cong ncong) then <<
  225. m := pasf_m atf;
  226. tnf := pasf_mr(tnf,m);
  227. % Variable free terms are possible
  228. c := pasf_gcd tnf_coef tnf;
  229. % Now if the content is 0 then the whole list had only
  230. % zeros. Evaluating the formula
  231. if c = 0 then <<
  232. if !*rlpasfatfsimpvb then
  233. prin2!* "[VFs]";
  234. return if pasf_evalatp(pasf_op atf,tnf_const tnf) then
  235. 'true
  236. else
  237. 'false
  238. >>;
  239. tnfm := pasf_cecong(tnf,m);
  240. tnf := car tnfm;
  241. m := cdr tnfm
  242. >> else
  243. tnf := if pasf_opn atf memq '(equal neq) then
  244. pasf_ceeq tnf
  245. else
  246. pasf_cein(tnf,pasf_opn atf);
  247. % Advanced simplification
  248. if !*rlsiatadv then <<
  249. % Solvability of diophantine (in-)equations (SE-Rule)
  250. if pasf_opn atf eq 'equal and not pasf_se tnf then
  251. return 'false;
  252. if pasf_opn atf eq 'neq and not pasf_se tnf then
  253. return 'true;
  254. % Solvability of congruences
  255. if pasf_opn atf eq 'cong and pasf_sc(tnf,m) then
  256. return 'false;
  257. if pasf_opn atf eq 'ncong and pasf_sc(tnf,m) then
  258. return 'true
  259. >>;
  260. return if pasf_opn atf memq '(cong ncong) then
  261. pasf_mk2(pasf_mkop(pasf_opn atf,m),tnf_tnf2expr tnf,nil)
  262. else
  263. pasf_mk2(pasf_opn atf,tnf_tnf2expr tnf,nil)
  264. end;
  265. procedure pasf_mkpos(atf);
  266. % Presburger arithmetic standard form make atomic formula positive.
  267. % [atf] is an atomic formula. Returns an equivalent atomic formula
  268. % with a positive leading coefitient.
  269. if minusf pasf_arg2l atf then
  270. pasf_anegateat atf
  271. else
  272. atf;
  273. procedure pasf_mr(tnf,m);
  274. % Presburger arithmetic standard form modulo reduction. [tnf] is a
  275. % <TNF> structure. Returns a <TNF> that represents the modulo free
  276. % term.
  277. begin scalar c,as,xs,succ;
  278. % print tnf;
  279. c := tnf_const tnf;
  280. as := tnf_coef tnf;
  281. xs := tnf_vars tnf;
  282. % Verbose check for simplification
  283. for each a in as do
  284. if !*rlpasfatfsimpvb then
  285. if remainder(a,m) <> a then
  286. succ := 't;
  287. as := for each a in as collect
  288. remainder(a,m);
  289. % Verbose check for simplification
  290. if !*rlpasfatfsimpvb then
  291. if remainder(c,m) <> c then
  292. succ := 't;
  293. if succ then prin2!* "[MR]";
  294. return tnf_new(remainder(c,m),as,xs);
  295. end;
  296. procedure pasf_ceeq(tnf);
  297. % Presburger arithmetic standard form content elimination (CE) for
  298. % equalities. [tnf] is a <TNF> structure. Returns a <TNF> that
  299. % represents a term with eliminated content.
  300. begin scalar c,as,xs,g;
  301. c := tnf_const tnf;
  302. as := tnf_coef tnf;
  303. xs := tnf_vars tnf;
  304. % Computing the content of the coefitients list joined with the
  305. % constant part
  306. g := pasf_gcd(c . as);
  307. % Verbose check for simplification
  308. if !*rlpasfatfsimpvb then
  309. if g <> 1 then
  310. prin2!* "[CE(n)eq]";
  311. as := for each a in as collect
  312. a/g;
  313. return tnf_new(c/g,as,xs)
  314. end;
  315. procedure pasf_cein(tnf,op);
  316. % Presburger arithmetic standard form content elimination (CE) for
  317. % inequalities. [tnf] is a <TNF> structure; [op] is the inequality
  318. % operator. Returns a <TNF> that represents a term with eliminated
  319. % content.
  320. begin scalar c,as,xs,g;
  321. c := tnf_const tnf;
  322. as := tnf_coef tnf;
  323. xs := tnf_vars tnf;
  324. % Computing the content
  325. g := pasf_gcd as;
  326. % Verbose check for simplification
  327. if !*rlpasfatfsimpvb then
  328. if g <> 1 then
  329. prin2!* "[CEineq]";
  330. as := for each a in as collect
  331. a/g;
  332. return
  333. if op memq '(leq,greaterp) then
  334. tnf_new(negf pasf_floor(-c,g),as,xs)
  335. else if op memq '(geq,lessp) then
  336. tnf_new(negf pasf_ceil(-c,g),as,xs)
  337. else
  338. rederr {"pasf_cein : invalid inequality operator",op}
  339. end;
  340. procedure pasf_cecong(tnf,m);
  341. % Presburger arithmetic standard form content elimination (CE) for
  342. % congruences. [tnf] is a <TNF> structure; [m] is the
  343. % modulus. Returns a dotted pair $(<TNF> . modulus)$ that
  344. % represents a term with eliminated content.
  345. begin scalar c,as,xs,g,prime;
  346. c := tnf_const tnf;
  347. as := tnf_coef tnf;
  348. xs := tnf_vars tnf;
  349. % Computing if the modulus is prime. Is used not to call
  350. % "primep" twice
  351. prime := primep m;
  352. % Computing the content with modulus
  353. g := if prime then
  354. % In case of prime modulus the modulo ring is a field and so
  355. % content division is always possible
  356. pasf_gcd(c . as)
  357. else
  358. pasf_gcd(c . m . as);
  359. % Verbose check for simplification
  360. if !*rlpasfatfsimpvb then
  361. if g <> 1 then
  362. prin2!* "[CEcong]";
  363. as := for each a in as collect
  364. a/g;
  365. return if prime then
  366. (tnf_new(c/g,as,xs) . m)
  367. else
  368. (tnf_new(c/g,as,xs) . (m/g))
  369. end;
  370. procedure pasf_se(tnf);
  371. % Presburger arithmetic standard form (un-)solvability check for
  372. % (in-)equalities. [tnf] is a <TNF> structure. Returns ['t] if and
  373. % only if the $gcd(\{a_1, ..., a_n\} \setminus \{0\}) \mid a_0$.
  374. begin scalar c,as,xs,g;
  375. c := tnf_const tnf;
  376. as := tnf_coef tnf;
  377. xs := tnf_vars tnf;
  378. % Computing the content of the coefitients list
  379. g := pasf_gcd as;
  380. % Verbose check for simplification
  381. if !*rlpasfatfsimpvb then
  382. if remainder(c,g) <> 0 then
  383. prin2!* "[SE]";
  384. return (remainder(c,g) = 0)
  385. end;
  386. procedure pasf_sc(tnf,m);
  387. % Presburger arithmetic standard form (un-)solvability check for
  388. % (non-)congruences. [tnf] is a <TNF> structure. Returns ['t] if
  389. % and only if the $m \mid j * gcd(\{a_1, ..., a_n\} \setminus \{0\})
  390. % + a_0$ for all $0 <= j < m$.
  391. begin scalar c,as,xs,g,res;
  392. c := tnf_const tnf;
  393. as := tnf_coef tnf;
  394. xs := tnf_vars tnf;
  395. % Computing the content of the coefitients list
  396. g := pasf_gcd as;
  397. % Verbose check for simplification
  398. res := 'true;
  399. for j := 0 : m do
  400. res := res and (remainder(c + j*g,m) <> 0);
  401. if !*rlpasfatfsimpvb then
  402. if res then
  403. prin2!* "[SC]";
  404. return res
  405. end;
  406. procedure pasf_gcd(coefl);
  407. % Presburger arithmetic standard form list gcd computation. [coefl]
  408. % is a list of integers. Returns the $\gcd(coefl)$.
  409. if null coefl then 0 else gcdn(car coefl,pasf_gcd cdr coefl);
  410. procedure pasf_evalatp(rel,lhs);
  411. % Presburger arithmetic standard form evaluate atomic
  412. % formula. [rel] is a relation; [lhs] is a domain element; Returns
  413. % a truth value equivalent to $[rel]([lhs],0)$.
  414. if pairp rel and car rel memq '(cong ncong) then
  415. pasf_evalatpm(car rel,lhs,cdr rel)
  416. else
  417. pasf_evalatpm(rel,lhs,nil);
  418. procedure pasf_evalatpm(rel,lhs,m);
  419. % Presburger arithmetic standard form evaluate atomic formula
  420. % subroutine. [rel] is a relation; [lhs] is a domain element; [m]
  421. % is an optional modulus; Returns a truth value equivalent to
  422. % $[rel]([lhs],0)$.
  423. if rel eq 'equal then null lhs or lhs = 0
  424. else if rel eq 'neq then not (null lhs or lhs = 0)
  425. else if rel eq 'leq then minusf lhs or (null lhs or lhs = 0)
  426. else if rel eq 'geq then not minusf lhs
  427. else if rel eq 'lessp then minusf lhs
  428. else if rel eq 'greaterp then not (minusf lhs or null lhs or lhs = 0)
  429. else if rel eq 'cong then (null lhs or lhs = 0) or 0 = remainder(lhs,m)
  430. else if rel eq 'ncong then not ((null lhs or lhs = 0)
  431. or 0 = remainder(lhs,m))
  432. else rederr {"pasf_evalatp: unknown operator",rel};
  433. endmodule; % [pasfsiat]
  434. end; % of file