;; 2025-06-29.  solposgcd.scm

(set! COMMENT-FLAG #f)
(libload "nat.scm")
(exload "arith/gcd_nat.scm")
(libload "pos.scm")
(set! COMMENT-FLAG #t)

;;;; Command reference
;; (use …), in particular. (use "Truth")
;; (assume …)

;; (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.

;; (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]

;; (strip)
; Strips all variables/assumptions i.e. applies “assume” as long as possible

;; (fold-alltotal)
; Convert a goal of form ∀ n^(Total…1 n^ -> …2) to ∀ n (…2)


;;;; (b)

;;; Useful theorems
(pp "PosLtToLe")

;;; Useful definitions
(display-pconst "PosLog")
(display-alg "pos")

(set-goal "all p,q(p<=q -> PosLog p<=PosLog q)")
;; TODO
(save "PosLogMon")

;;;; (c)

;; TODO: remove the ellipses …
(add-program-constant "PosGcd" (py "pos=>pos=>pos"))
(add-computation-rules
 "PosGcd 1 p" "…"
 "PosGcd(SZero p)1" "…"
 "PosGcd(SZero p)(SZero q)" "…"
 "PosGcd(SZero p)(SOne q)" "…"
 "PosGcd(SOne p)1" "1"
 "PosGcd(SOne p)(SZero q)" "…"
 "PosGcd(SOne p)(SOne q)"
 "[if (p=q) …
      [if (p<q) (PosGcd(…)(q--p))
                (PosGcd(…)(…))]]")

;; Test your PosGcd  before proceeding to (d)
(pp (nt (pt "PosGcd 1 36"))) ; 1
(pp (nt (pt "PosGcd 1 5"))) ; 1
(pp (nt (pt "PosGcd 5 1"))) ; 1
(pp (nt (pt "PosGcd 36 1"))) ; 1
(pp (nt (pt "PosGcd 5 2"))) ; 1
(pp (nt (pt "PosGcd 2 5"))) ; 1
(pp (nt (pt "PosGcd 2 5"))) ; 1
(pp (nt (pt "PosGcd 5 5"))) ; 5
(pp (nt (pt "PosGcd 15 10"))) ; 5
(pp (nt (pt "PosGcd 10 15"))) ; 5
(pp (nt (pt "PosGcd 15 9"))) ; 3
(pp (nt (pt "PosGcd 9 15"))) ; 3

(pp (nt (pt "PosGcd(24*2123456789)(36*123456789001001)"))) ; 12

;;;; (d)

;;; Useful commands
;; use
;; assume
;; assert
;; ng
;; ind
;; cases
;; strip
;; fold-alltotal

;;; Useful theorems
(pp "EfTotalPos")
(pp "TotalVar")
(pp "TotalPosSZero")
(pp "TotalPosSOne")
(pp "NatLtTrans")
(pp "NatLeLtTrans")
(pp "BooleIfTotal")
(pp "PosLogMon")
(pp "Truth")

;;; Useful definitions
(display-pconst "PosLog")
(display-alg "nat")
(display-alg "pos")

(set-totality-goal "PosGcd")
(assert "all n,p,q(PosLog p+PosLog q<n -> TotalPos(PosGcd p q))")
;; TODO
(save-totality)
