From 7dff3e17e892a7d085fbe61004a1bebf7223a9fa Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Thu, 7 Oct 2021 18:15:03 -0500 Subject: [PATCH] finished hwk2! --- src/Homework2.agda | 67 ++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/src/Homework2.agda b/src/Homework2.agda index 2dd5e956..c03b4bf4 100644 --- a/src/Homework2.agda +++ b/src/Homework2.agda @@ -16,14 +16,8 @@ data Bin : Set where from : Bin → ℕ from O = 0 -from (b I) = 2 * (from b) + 1 -from (b II) = 2 * (from b) + 2 - -fromBin3 : from (O I I) ≡ 3 -fromBin3 = refl - -fromBin12 : from (O II I II) ≡ 12 -fromBin12 = refl +from (b I) = 1 + (from b) * 2 +from (b II) = 2 + (from b) * 2 ------------------------------------------------------- -- to @@ -31,32 +25,53 @@ fromBin12 = refl inc : Bin → Bin inc O = O I -inc (w I) = w II -inc (w II) = (inc w) I +inc (b I) = b II +inc (b II) = (inc b) I to : ℕ → Bin to 0 = O -to (suc n) with to n -... | O = O I -... | w I = w II -... | w II = (inc w) I - -to2 : to 2 ≡ O II -to2 = refl - -to3 : to 3 ≡ O I I -to3 = refl - -to4 : to 4 ≡ O I II -to4 = refl - -to5 : to 5 ≡ O II I -to5 = refl +to (suc n) = inc (to n) ------------------------------------------------------- -- from∘to ------------------------------------------------------- +from-inc : ∀ (b : Bin) → from (inc b) ≡ suc (from b) +from-inc O = refl +from-inc (n I) = refl +from-inc (n II) = cong suc (cong (_* 2) (from-inc n)) + from∘to : ∀ n → from (to n) ≡ n +from∘to zero = refl +from∘to (suc n) = trans (from-inc (to n)) (cong suc (from∘to n)) + +------------------------------------------------------- +-- to∘from +------------------------------------------------------- + +toI : ∀ (n : ℕ) → to (1 + n * 2) ≡ (to n) I +toI zero = refl +toI (suc n) = cong inc (cong inc (toI n)) to∘from : ∀ b → to (from b) ≡ b +to∘from O = refl +to∘from (n I) = + begin + to (1 + (from n) * 2) + ≡⟨ toI (from n) ⟩ + (to (from n)) I + ≡⟨ cong _I (to∘from n) ⟩ + n I + ∎ +to∘from (n II) = + begin + to (from (n II)) + ≡⟨⟩ + to (2 + (from n) * 2) + ≡⟨⟩ + inc (to (1 + (from n) * 2)) + ≡⟨ cong inc (toI (from n)) ⟩ + inc ((to (from n)) I) + ≡⟨ cong inc (cong _I (to∘from n)) ⟩ + n II + ∎ \ No newline at end of file