(prog2$ (quote

(Improvements to Arithmetic-5  -- J Moore

Abstract - Arithmetic-5 can put the rewriter into loops.
But it has been in use for almost 20 years.  I have
fixed some loops.  My fixes can be enabledoptionally
to preserve old behavior.))


nil)

(brr t)
(set-gag-mode nil)
(set-evisc-tuple nil :iprint :same :sites :all)

(include-book "arithmetic-5/top" :dir :system)

; The problem: Arithmetic-5 can get into rewrite loops.
; But it's been in use for almost 20 years.
; I have fixed some of the loops.
; My fixes can be enabled optionally to preserve old behavior.

(quote (Example 1))
; Example 1: The following causes a hard error under the original rules:

(thm
 (implies (natp n)
          (equal (/ (expt 3 n) (expt 2 n))
                 (expt 3/2 n))))

(cw-gstack :frames 20)

; Here is Robert's version of |(expt 1/c n)|:
(quote
 (defthm |(expt 1/c n)|
    (implies (and (syntaxp (quotep x))
                  (syntaxp (rationalp (unquote x)))
                  (syntaxp (not (integerp (unquote x))))
                  ;(syntaxp (equal (numerator x) 1))
		  (syntaxp (< (abs x) 1))
		  )
             (equal (expt x n)
                    (expt (/ x) (- n))))))
(pe '|(expt 1/c n)|)


; But under the new rules the theorem is proved:

(thm
 (implies (natp n)
          (equal (/ (expt 3 n) (expt 2 n))
                 (expt 3/2 n)))
 :hints (("Goal" :in-theory (enable (:e use-new-arith-5-rules)))))

(quote (Example 2))
; Example 2: If we enable non-linear reasoning with

(set-default-hints '((nonlinearp-default-hint stable-under-simplificationp
                                              hist pspv)))

; the following causes a hard error under the original rules:

(thm (implies (natp n)
              (<= (* (expt 3 n) (expt 10 (- n))) 1)))

; Arithmetic-5 frequently rearranges polynomials with rules like

(pe 'SIMPLIFY-PRODUCTS-GATHER-EXPONENTS-EQUAL)

; There are about a dozen such rules.  But these are dangerous
; because you have to be sure the new products cancel terms out
; and sometimes rules are missing or other rules divert the
; intended course.

; But the proof succeeds under the new rules:

(thm (implies (natp n)
              (<= (* (expt 3 n) (expt 10 (- n))) 1))
     :hints (("Goal" :in-theory (enable (:e use-new-arith-5-rules)))))



(quote (example 3))

; Example 3:  The following causes a hard error under the original rules:

(thm
 (implies (and (integerp a) (not (equal a 0)))
          (not (equal (* (expt 3 a) (expt 2 (- a)))
                      (* (expt 2 a) (expt 3 (- a)))))))

(cw-gstack :frames 40)

; But the proof succeeds under the new rules:

(thm
 (implies (and (integerp a) (not (equal a 0)))
          (not (equal (* (expt 3 a) (expt 2 (- a)))
                      (* (expt 2 a) (expt 3 (- a))))))
 :hints (("Goal" :in-theory (enable (:e use-new-arith-5-rules)))))



(quote (the end))
