# HG changeset patch # User Shinji KONO # Date 1609395614 -32400 # Node ID 8207b69c500b750196984add39f45c3b6cb50170 # Parent 0d8a834c9c50fdabbfd83c8480fe59a9fa1261e8 ... diff -r 0d8a834c9c50 -r 8207b69c500b agda/gcd.agda --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agda/gcd.agda Thu Dec 31 15:20:14 2020 +0900 @@ -0,0 +1,235 @@ +{-# OPTIONS --allow-unsolved-metas #-} +module gcd where + +open import Data.Nat +open import Data.Nat.Properties +open import Data.Empty +open import Data.Unit using (⊤ ; tt) +open import Relation.Nullary +open import Relation.Binary.PropositionalEquality +open import Relation.Binary.Definitions + +even : (n : ℕ ) → Set +even zero = ⊤ +even (suc zero) = ⊥ +even (suc (suc n)) = even n + +even? : (n : ℕ ) → Dec ( even n ) +even? zero = yes tt +even? (suc zero) = no (λ ()) +even? (suc (suc n)) = even? n + +n+even : {n m : ℕ } → even n → even m → even ( n + m ) +n+even {zero} {zero} tt tt = tt +n+even {zero} {suc m} tt em = em +n+even {suc (suc n)} {m} en em = n+even {n} {m} en em + +n*even : {m n : ℕ } → even n → even ( m * n ) +n*even {zero} {n} en = tt +n*even {suc m} {n} en = n+even {n} {m * n} en (n*even {m} {n} en) + +even*n : {n m : ℕ } → even n → even ( n * m ) +even*n {n} {m} en = subst even (*-comm m n) (n*even {m} {n} en) + +gcd1 : ( i i0 j j0 : ℕ ) → ℕ +gcd1 zero i0 zero j0 with <-cmp i0 j0 +... | tri< a ¬b ¬c = j0 +... | tri≈ ¬a refl ¬c = i0 +... | tri> ¬a ¬b c = i0 +gcd1 zero i0 (suc zero) j0 = 1 +gcd1 zero zero (suc (suc j)) j0 = j0 +gcd1 zero (suc i0) (suc (suc j)) j0 = gcd1 i0 (suc i0) (suc j) (suc (suc j)) +gcd1 (suc zero) i0 zero j0 = 1 +gcd1 (suc (suc i)) i0 zero zero = i0 +gcd1 (suc (suc i)) i0 zero (suc j0) = gcd1 (suc i) (suc (suc i)) j0 (suc j0) +gcd1 (suc i) i0 (suc j) j0 = gcd1 i i0 j j0 + +gcd : ( i j : ℕ ) → ℕ +gcd i j = gcd1 i i j j + +even→gcd=2 : {n : ℕ} → even n → n > 0 → gcd n 2 ≡ 2 +even→gcd=2 {suc (suc zero)} en (s≤s z≤n) = refl +even→gcd=2 {suc (suc (suc (suc n)))} en (s≤s z≤n) = begin + gcd (suc (suc (suc (suc n)))) 2 + ≡⟨⟩ + gcd (suc (suc n)) 2 + ≡⟨ even→gcd=2 {suc (suc n)} en (s≤s z≤n) ⟩ + 2 + ∎ where open ≡-Reasoning + +open import nat + +-- gcd26 : { n m : ℕ} → n > 1 → m > 1 → n - m > 0 → gcd n m ≡ gcd (n - m) m +-- gcd27 : { n m : ℕ} → n > 1 → m > 1 → n - m > 0 → gcd n k ≡ k → k ≤ n + +gcd22 : ( i i0 o o0 : ℕ ) → gcd1 (suc i) i0 (suc o) o0 ≡ gcd1 i i0 o o0 +gcd22 zero i0 zero o0 = refl +gcd22 zero i0 (suc o) o0 = refl +gcd22 (suc i) i0 zero o0 = refl +gcd22 (suc i) i0 (suc o) o0 = refl + +-- gcd27 : ( i i0 : ℕ ) → gcd1 i i0 0 1 ≤ 1 +-- gcd27 zero zero = ≤-refl +-- gcd27 zero (suc i0) = {!!} +-- gcd27 (suc i) zero = {!!} +-- gcd27 (suc i) (suc i0) = {!!} + +gcd-kk : ( i : ℕ ) → gcd1 i i i i ≡ i +gcd-kk zero = refl +gcd-kk (suc i) = gcd-kk1 i i i i refl refl where + gcd-kk1 : (i i0 j j0 : ℕ) → i ≡ j → i0 ≡ j0 → gcd1 (suc i) (suc i0) (suc j) (suc j0) ≡ suc i0 + gcd-kk1 zero i0 zero j0 i=j i0=j0 with <-cmp (suc i0) (suc j0) + ... | tri< a ¬b ¬c = ⊥-elim (¬b (cong suc i0=j0)) + ... | tri≈ ¬a refl ¬c = refl + ... | tri> ¬a ¬b c = ⊥-elim (¬b (cong suc i0=j0)) + gcd-kk1 (suc i) i0 (suc j) j0 refl i0=j0 = + gcd-kk1 i i0 j j0 refl i0=j0 + +gcd26 : (n m i : ℕ) → 1 < n → 1 < m → gcd n i ≡ m → ¬ ( gcd n m ≡ 1 ) +gcd26 n m i 1 a a₁) +... | tri< a ¬b ¬c | tri≈ ¬a b ¬c₁ = ⊥-elim (nat-≡< (sym b) a) +... | tri< a ¬b ¬c | tri> ¬a ¬b₁ c = refl +... | tri≈ ¬a b ¬c | tri< a ¬b ¬c₁ = ⊥-elim (nat-≡< (sym b) a) +... | tri≈ ¬a refl ¬c | tri≈ ¬a₁ refl ¬c₁ = refl +... | tri≈ ¬a b ¬c | tri> ¬a₁ ¬b c = ⊥-elim (nat-≡< b c) +... | tri> ¬a ¬b c | tri< a ¬b₁ ¬c = refl +... | tri> ¬a ¬b c | tri≈ ¬a₁ b ¬c = ⊥-elim (nat-≡< b c) +... | tri> ¬a ¬b c | tri> ¬a₁ ¬b₁ c₁ = ⊥-elim (nat-<> c c₁) +gcdsym1 : ( i i0 j j0 : ℕ ) → gcd1 i i0 j j0 ≡ gcd1 j j0 i i0 +gcdsym1 zero zero zero zero = refl +gcdsym1 zero zero zero (suc j0) = refl +gcdsym1 zero (suc i0) zero zero = refl +gcdsym1 zero (suc i0) zero (suc j0) = gcdsym2 (suc i0) (suc j0) +gcdsym1 zero zero (suc zero) j0 = refl +gcdsym1 zero zero (suc (suc j)) j0 = refl +gcdsym1 zero (suc i0) (suc zero) j0 = refl +gcdsym1 zero (suc i0) (suc (suc j)) j0 = gcdsym1 i0 (suc i0) (suc j) (suc (suc j)) +gcdsym1 (suc zero) i0 zero j0 = refl +gcdsym1 (suc (suc i)) i0 zero zero = refl +gcdsym1 (suc (suc i)) i0 zero (suc j0) = gcdsym1 (suc i) (suc (suc i))j0 (suc j0) +gcdsym1 (suc i) i0 (suc j) j0 = subst₂ (λ j k → j ≡ k ) (sym (gcd22 i _ _ _)) (sym (gcd22 j _ _ _)) (gcdsym1 i i0 j j0 ) + +gcdsym : { n m : ℕ} → gcd n m ≡ gcd m n +gcdsym {n} {m} = gcdsym1 n n m m + +gcd24 : { n m k : ℕ} → n > 1 → m > 1 → k > 1 → gcd n k ≡ k → gcd m k ≡ k → ¬ ( gcd n m ≡ 1 ) +gcd24 {n} {m} {k} 1 ¬a ¬b c = 1 ¬a ¬b c = ⊥-elim ( nat-≡< gm 1 (subst (λ k → k ≤ 1 ) gm (gcd27 (suc j) (suc (suc j)))) 1 (suc (suc o0)) → gm = gnm → (suc (suc i0)) ≡ 1 + -- (suc (suc i0)) < (suc (suc o0)) → ? gcd1 (suc j) (suc (suc j)) (suc o0) (suc (suc o0)) ≡ (suc (suc o0)) + -- gcd1 (suc j) (suc (suc j)) (suc i0) (suc (suc i0)) ≡ 1 + gcd25 (suc (suc i0)) (suc (suc o0)) (suc j) j0 1 ¬a ¬b c = gcd26 {!!} {!!} {!!} {!!} {!!} {!!} {!!} + gcd21 zero i0 (suc j) j0 (suc zero) o0 1 ¬a ¬b c = ⊥-elim ( nat-≡< (sym eq) (s≤s (<-trans (<-plus c) (<-plus-0 (s≤s (<-plus c )))))) + diff -r 0d8a834c9c50 -r 8207b69c500b agda/root2.agda --- a/agda/root2.agda Thu Dec 31 03:21:05 2020 +0900 +++ b/agda/root2.agda Thu Dec 31 15:20:14 2020 +0900 @@ -8,200 +8,9 @@ open import Relation.Binary.PropositionalEquality open import Relation.Binary.Definitions -even : (n : ℕ ) → Set -even zero = ⊤ -even (suc zero) = ⊥ -even (suc (suc n)) = even n - -even? : (n : ℕ ) → Dec ( even n ) -even? zero = yes tt -even? (suc zero) = no (λ ()) -even? (suc (suc n)) = even? n - -n+even : {n m : ℕ } → even n → even m → even ( n + m ) -n+even {zero} {zero} tt tt = tt -n+even {zero} {suc m} tt em = em -n+even {suc (suc n)} {m} en em = n+even {n} {m} en em - -n*even : {m n : ℕ } → even n → even ( m * n ) -n*even {zero} {n} en = tt -n*even {suc m} {n} en = n+even {n} {m * n} en (n*even {m} {n} en) - -even*n : {n m : ℕ } → even n → even ( n * m ) -even*n {n} {m} en = subst even (*-comm m n) (n*even {m} {n} en) - -gcd1 : ( i i0 j j0 : ℕ ) → ℕ -gcd1 zero i0 zero j0 with <-cmp i0 j0 -... | tri< a ¬b ¬c = j0 -... | tri≈ ¬a refl ¬c = i0 -... | tri> ¬a ¬b c = i0 -gcd1 zero i0 (suc zero) j0 = 1 -gcd1 zero zero (suc (suc j)) j0 = j0 -gcd1 zero (suc i0) (suc (suc j)) j0 = gcd1 i0 (suc i0) (suc j) (suc (suc j)) -gcd1 (suc zero) i0 zero j0 = 1 -gcd1 (suc (suc i)) i0 zero zero = i0 -gcd1 (suc (suc i)) i0 zero (suc j0) = gcd1 (suc i) (suc (suc i)) j0 (suc j0) -gcd1 (suc i) i0 (suc j) j0 = gcd1 i i0 j j0 - -gcd : ( i j : ℕ ) → ℕ -gcd i j = gcd1 i i j j - -even→gcd=2 : {n : ℕ} → even n → n > 0 → gcd n 2 ≡ 2 -even→gcd=2 {suc (suc zero)} en (s≤s z≤n) = refl -even→gcd=2 {suc (suc (suc (suc n)))} en (s≤s z≤n) = begin - gcd (suc (suc (suc (suc n)))) 2 - ≡⟨⟩ - gcd (suc (suc n)) 2 - ≡⟨ even→gcd=2 {suc (suc n)} en (s≤s z≤n) ⟩ - 2 - ∎ where open ≡-Reasoning - +open import gcd open import nat --- gcd26 : { n m : ℕ} → n > 1 → m > 1 → n - m > 0 → gcd n m ≡ gcd (n - m) m --- gcd25 : { n m : ℕ} → n > 1 → m > 1 → n - m > 0 → gcd n m ≡ gcd m n --- gcd27 : { n m : ℕ} → n > 1 → m > 1 → n - m > 0 → gcd n k ≡ k → k ≤ n - -gcd22 : ( i i0 o o0 : ℕ ) → gcd1 (suc i) i0 (suc o) o0 ≡ gcd1 i i0 o o0 -gcd22 zero i0 zero o0 = refl -gcd22 zero i0 (suc o) o0 = refl -gcd22 (suc i) i0 zero o0 = refl -gcd22 (suc i) i0 (suc o) o0 = refl - -gcd27 : ( i i0 : ℕ ) → gcd1 i i0 0 1 ≤ 1 -gcd27 zero zero = ≤-refl -gcd27 zero (suc i0) = {!!} -gcd27 (suc i) zero = {!!} -gcd27 (suc i) (suc i0) = {!!} - -gcdsym0 : (i : ℕ) → gcd1 zero i zero i ≡ gcd1 zero i zero i -gcdsym0 i = refl -gcdsym2 : (i j : ℕ) → gcd1 zero i zero j ≡ gcd1 zero j zero i -gcdsym2 i j with <-cmp i j | <-cmp j i -... | tri< a ¬b ¬c | tri< a₁ ¬b₁ ¬c₁ = ⊥-elim (nat-<> a a₁) -... | tri< a ¬b ¬c | tri≈ ¬a b ¬c₁ = ⊥-elim (nat-≡< (sym b) a) -... | tri< a ¬b ¬c | tri> ¬a ¬b₁ c = refl -... | tri≈ ¬a b ¬c | tri< a ¬b ¬c₁ = ⊥-elim (nat-≡< (sym b) a) -... | tri≈ ¬a refl ¬c | tri≈ ¬a₁ refl ¬c₁ = refl -... | tri≈ ¬a b ¬c | tri> ¬a₁ ¬b c = ⊥-elim (nat-≡< b c) -... | tri> ¬a ¬b c | tri< a ¬b₁ ¬c = refl -... | tri> ¬a ¬b c | tri≈ ¬a₁ b ¬c = ⊥-elim (nat-≡< b c) -... | tri> ¬a ¬b c | tri> ¬a₁ ¬b₁ c₁ = ⊥-elim (nat-<> c c₁) -gcdsym1 : ( i i0 j j0 : ℕ ) → gcd1 i i0 j j0 ≡ gcd1 j j0 i i0 -gcdsym1 zero zero zero zero = refl -gcdsym1 zero zero zero (suc j0) = refl -gcdsym1 zero (suc i0) zero zero = refl -gcdsym1 zero (suc i0) zero (suc j0) = gcdsym2 (suc i0) (suc j0) -gcdsym1 zero zero (suc zero) j0 = refl -gcdsym1 zero zero (suc (suc j)) j0 = refl -gcdsym1 zero (suc i0) (suc zero) j0 = refl -gcdsym1 zero (suc i0) (suc (suc j)) j0 = gcdsym1 i0 (suc i0) (suc j) (suc (suc j)) -gcdsym1 (suc zero) i0 zero j0 = refl -gcdsym1 (suc (suc i)) i0 zero zero = refl -gcdsym1 (suc (suc i)) i0 zero (suc j0) = gcdsym1 (suc i) (suc (suc i))j0 (suc j0) -gcdsym1 (suc i) i0 (suc j) j0 = subst₂ (λ j k → j ≡ k ) (sym (gcd22 i _ _ _)) (sym (gcd22 j _ _ _)) (gcdsym1 i i0 j j0 ) - -gcdsym : { n m : ℕ} → gcd n m ≡ gcd m n -gcdsym {n} {m} = gcdsym1 n n m m - -gcd24 : { n m k : ℕ} → n > 1 → m > 1 → k > 1 → gcd n k ≡ k → gcd m k ≡ k → ¬ ( gcd n m ≡ 1 ) -gcd24 {n} {m} {k} 1 ¬a ¬b c = 1 ¬a ¬b c = ⊥-elim ( nat-≡< gm 1 (subst (λ k → k ≤ 1 ) gm (gcd27 (suc j) (suc (suc j)))) 1 ¬a ¬b c = ⊥-elim ( nat-≡< (sym eq) (s≤s (<-trans (<-plus c) (<-plus-0 (s≤s (<-plus c )))))) - record Rational : Set where field i j : ℕ