;; 2025-05-01.  solquotrem.scm.  Based on
;; 2025-04-29.  examplesarithmod_nat.scm

(set! COMMENT-FLAG #f)
(libload "nat.scm")

;; IfTrue
(set-goal "all alpha_1,alpha_2 [if True alpha_1 alpha_2]eqd alpha_1")
(ng #t)
(strip)
(use "InitEqD")
;; Proof finished.
;; (cp)
(save "IfTrue")

;; IfFalse
(set-goal "all alpha_1,alpha_2 [if False alpha_1 alpha_2]eqd alpha_2")
(ng #t)
(strip)
(use "InitEqD")
;; Proof finished.
;; (cp)
(save "IfFalse")

(set! COMMENT-FLAG #t)

(add-var-name "q" "r" (py "nat"))
(add-var-name "nm" (py "nat yprod nat"))

(add-program-constant "NatQR" (py "nat=>nat=>nat yprod nat"))
(add-computation-rules
 "NatQR 0 m" "0 pair 0"
 "NatQR(Succ n)m"
 "([nm][if (Succ rht nm<m)      
           (lft nm pair Succ rht nm)
           (Succ lft nm pair 0)])
  (NatQR n m)")

(pp (nt (pt "NatQR 0 3")))
(pp (nt (pt "NatQR 1 3")))
(pp (nt (pt "NatQR 2 3")))
(pp (nt (pt "NatQR 3 3")))
(pp (nt (pt "NatQR 4 3")))
(pp (nt (pt "NatQR 5 3")))
(pp (nt (pt "NatQR 6 3")))
(pp (nt (pt "NatQR 7 3")))
(pp (nt (pt "NatQR 8 3")))
(pp (nt (pt "NatQR 9 3")))
(pp (nt (pt "NatQR 10 3")))
(pp (nt (pt "NatQR 11 3")))

(set-totality-goal "NatQR")
(assert
 "allnc m^(TotalNat m^ -> allnc n^(TotalNat n^ -> TotalYprod(NatQR n^ m^)))")
(fold-alltotal)
(assume "m")
(fold-alltotal)
(ind)
;; 7,8
(ng #t)
(use "TotalVar")
;; 8
(assume "n" "IH")
(ng #t)
(use "BooleIfTotal")
;; 12-14
(elim "IH")
(fold-alltotal)
(assume "m0")
(fold-alltotal)
(assume "n0")
(use "TotalVar")
;; 13
(elim "IH")
(fold-alltotal)
(assume "m0")
(fold-alltotal)
(assume "n0")
(ng #t)
(use "TotalVar")
;; 14
(elim "IH")
(fold-alltotal)
(assume "m0")
(fold-alltotal)
(assume "n0")
(ng #t)
(use "TotalVar")
;; Assertion proved.
;; 2
(search)
;; ok, ?_2 is proved by minimal quantifier logic.  Proof finished.
;; (cp)
(save-totality)

;; Given n,m with 0<m.  Let q:=lft(NatQR n m) and r:=rht(NatQR n m).
;; We show n=q*m+r andnc r<m

;; NatQRProp
(set-goal "all m(
  0<m -> all n(n=lft(NatQR n m)*m+rht(NatQR n m) andnc rht(NatQR n m)<m))")
(assume "m" "0<m")
(ind)
;; 3,4
(ng #t)
(split)
(use "Truth")
(use "0<m")
;; 4
(assume "n" "IH")
(ng #t)
(cases (pt "Succ rht(NatQR n m)<m"))
;; 10,11
(assume "LtH")
(simp "IfTrue")
(ng #t)
(split)
(use "IH")
(use "LtH")
;; 11
(assume "NegH")
(assert "m=Succ rht(NatQR n m)")
(use "NatLeAntiSym")
(use "NatNotLtToLe")
(use "NegH")
(use "NatLtToSuccLe")
(use "IH")
;; Assertion proved.
(assume "m=r+1")
(simp "IfFalse")
(ng #t)
(split)
(use "NatEqTrans" (pt "lft(NatQR n m)*m+Succ rht(NatQR n m)"))
(ng #t)
(use "IH")
(simp "<-" "m=r+1")
(use "Truth")
(use "0<m")
;; Proof finished.
;; (cp)
(save "NatQRProp")

;;;; Command help
;; (use …), in particular. (use "Truth")
; see previous exercises
;; (assume …)
; see previous exercises
;; (simp …), (simp "<-" …)
; Simp accepts the name of a theorem or a variable(*),
; which expresses an equality (i.e., the corresponding formula should
; be of the form "… eqd …" or "… = …") and applies
; this equality to the goal.
; The theorem or variable can also express a formula of the Form A → B → … → x=y.
; In that case x=y is used on the goal and A,B,… are added as additional goals.
; Sometimes
; If the string "<-" is passed as the first argument, the replacement
; is done from right to left instead of left to right.
; Regarding (*): Simp also accepts formulas directly, i.e., one can write
; (simp (pf "n+m=m+n"));
; "n+m=m+n" will be added as an additional goal.
; Furthermore, simp can replace computably true formulas with truth.
; For example, if a:l<n is in the context, then
; (simp a) performs the replacement.
;; (ind), (ind (pt "…"))
; If the goal has the form ∀ₓA(x), then (ind) initiates an induction proof over x.
; That is, (ind) uses the the axiom of the smallest fixpoint for the totality predicate
; of the type of x if applicable. Ofc this is only possible if x is total.
; (ind (pt "…"))
;; (cases), (cases (pt "…"))
; Similar to (ind), but only does case distinction.
;; (assert B)
; Replaces Goal A by the two goals B -> A and B
;; (pp …)
; Display a theorem
;; (ng #t)
; This command tries to normalize all terms in the goal as
; much as possible [note that it will not always do what you want]
;; (split)
; Splits a goal of the Form A ∧ B into subgoals A,B
;; (by-assume "∃ₓA" "x" "A")
; Splits an hypothesis from the context ∃ₓA into a variable x and a formula A with free x
; and adds them to the context.
;; (intro i …)
; Use the i-th introduction-axiom with arguments …

;;; Tips
; If the goal has the form A and there is a hypothesis w:A∧B in context, you can use (use "w")
; to proof the goal.

;; Examples
(set-goal "all n (exd m (m = 2*n) -> exd m (m = 2*n))")
(assume "n" "∃ₘm=2n")
(by-assume "∃ₘm=2n" "m" "m=2n")
(intro 0 (pt "m"))
(use "m=2n")

(add-pvar-name "A" "B" (make-arity))
(set-goal "A andd B -> B andd A")
(assume "w")
(split)
(use "w")
(use "w")

;;;; Exercise 17
;;; (a)
;; Now instead of starting with NatQR we prove an existential theorem.

;; by-assume
;; split
;; use works with terms containing and
;; assert

;;; Helpful commands:
;; assume
;; use
;; ng
;; ind
;; intro
;; simp
;; assert
;; cases
;; by-assume
;; split
;;; Helpful theorems:
(pp "NatLeAntiSym")
(pp "NatNotLtToLe")
(pp "NatLtToSuccLe")
(pp "NatEqTrans")

;; QR
(set-goal "all m(0<m -> all n exd q exl r(n=q*m+r andnc r<m))")
;; TODO
(save "QR")


;;; (b)
(define eterm (proof-to-extracted-term (theorem-name-to-proof "QR")))
(define neterm (rename-variables (nt eterm)))
(ppc neterm)

(pp (nt (mk-term-in-app-form neterm (pt "3") (pt "7"))))
(pp (nt (mk-term-in-app-form neterm (pt "6") (pt "7"))))
(pp (nt (mk-term-in-app-form neterm (pt "7") (pt "54"))))
(pp (nt (mk-term-in-app-form neterm (pt "7") (pt "754")))) 

;; > 2 pair 1
;; > 1 pair 1
;; > 7 pair 5
;; > 107 pair 5

;; Using add-sound we can automatically obtain a proof that neterm
;; realizes QR.

;; Remark.  Translation into scheme:
(define expr (term-to-scheme-expr neterm))

(((ev expr) 7) 754)
