-- AGDA IN PADOVA, EXERCISE SHEET 3 -- DEFINITION OF EQUALITY AND BASIC LEMMAS ABOUT EQUALITY infixl 5 _≡_ data _≡_ {l} {A : Set l} (x : A) : A → Set l where refl : x ≡ x symm : {A : Set} {x y : A} → (x ≡ y) → (y ≡ x) symm {A} {x} {.x} refl = refl trans : {A : Set} {x y z : A} → (x ≡ y) → (y ≡ z) → (x ≡ z) trans {A} {x} {.x} {.x} refl refl = refl cong : {A B : Set} (f : A → B) {x y : A} → x ≡ y → f x ≡ f y cong {A} {B} f {x} {.x} refl = refl -- EQUATIONAL REASONING infix 3 _∎ infixr 2 _≡⟨_⟩_ _≡⟨⟩_ infix 1 begin_ _≡⟨_⟩_ : {A : Set} {y z : A} → (x : A) → x ≡ y → y ≡ z → x ≡ z x ≡⟨ p ⟩ q = trans p q _≡⟨⟩_ : {A : Set} {y : A} → (x : A) → (q : x ≡ y) → x ≡ y x ≡⟨⟩ q = q _∎ : {A : Set} → (x : A) → x ≡ x x ∎ = refl begin_ : {A : Set} {x y : A} → x ≡ y → x ≡ y begin p = p -- BASICS ABOUT NATURAL NUMBERS data ℕ : Set where zero : ℕ succ : ℕ → ℕ one : ℕ one = succ zero two : ℕ two = succ one pred : ℕ → ℕ -- predecessor pred zero = zero pred (succ k) = k infixl 6 _+_ _+_ : ℕ → ℕ → ℕ zero + m = m succ k + m = succ (k + m) lemma-+-zero : (n : ℕ) → ((n + zero) ≡ n) lemma-+-zero zero = refl lemma-+-zero (succ k) = cong succ (lemma-+-zero k) lemma-+-succ : (n m : ℕ) → ((n + succ m) ≡ succ (n + m)) lemma-+-succ zero m = refl lemma-+-succ (succ k) m = cong succ (lemma-+-succ k m) lemma-+-commutative : (n m : ℕ) → ((n + m) ≡ (m + n)) lemma-+-commutative zero m = symm (lemma-+-zero m) lemma-+-commutative (succ k) m = trans (cong succ (lemma-+-commutative k m)) (symm (lemma-+-succ m k)) infixl 7 _·_ _·_ : ℕ → ℕ → ℕ zero · m = zero succ n · m = m + n · m lemma-+-associative : (a b c : ℕ) → (a + (b + c) ≡ (a + b) + c) lemma-+-associative zero b c = refl lemma-+-associative (succ a) b c = cong succ (lemma-+-associative a b c) lemma-+-swap : (m n p : ℕ) → m + (n + p) ≡ n + (m + p) -- part of the exercise is to formalize the statement lemma-+-swap m n p = begin m + (n + p) ≡⟨ lemma-+-associative m n p ⟩ (m + n) + p ≡⟨ cong (λ z → z + p) (lemma-+-commutative m n) ⟩ (n + m) + p ≡⟨ symm (lemma-+-associative n m p) ⟩ n + (m + p) ∎ lemma-distributive : (a b c : ℕ) → (a + b) · c ≡ a · c + b · c lemma-distributive zero b c = refl lemma-distributive (succ a) b c = begin (succ a + b) · c ≡⟨⟩ succ (a + b) · c ≡⟨⟩ c + (a + b) · c ≡⟨ cong (_+_ c) (lemma-distributive a b c) ⟩ c + (a · c + b · c) ≡⟨ lemma-+-associative c (a · c) (b · c) ⟩ (c + a · c) + b · c ≡⟨⟩ succ a · c + b · c ∎ lemma-·-associative : (a b c : ℕ) → (a · (b · c) ≡ (a · b) · c) lemma-·-associative zero b c = refl lemma-·-associative (succ a) b c = begin (succ a) · (b · c) ≡⟨⟩ b · c + a · (b · c) ≡⟨ cong (_+_ (b · c)) (lemma-·-associative a b c) ⟩ b · c + (a · b) · c ≡⟨ symm (lemma-distributive b (a · b) c) ⟩ (b + a · b) · c ≡⟨⟩ ((succ a) · b) · c ∎ -- (succ a) · (b · c) ≡⟨⟩ -- a · (b · c) + b · c ≡⟨ cong (λ z → z + b · c) (lemma-·-associative a b c) ⟩ -- (a · b) · c + b · c ≡⟨ symm (lemma-distributive (a · b) b c) ⟩ -- (a · b + b) · c ≡⟨⟩ -- ((succ a) · b) · c ∎ lemma-·-zero : (n : ℕ) → (n · zero ≡ zero) lemma-·-zero zero = refl lemma-·-zero (succ n) = lemma-·-zero n lemma-·-succ : (n m : ℕ) → (n · succ m ≡ n + n · m) lemma-·-succ zero m = refl lemma-·-succ (succ n) m = cong succ (begin m + n · succ m ≡⟨ cong (_+_ m) (lemma-·-succ n m) ⟩ m + (n + n · m) ≡⟨ lemma-+-swap m n (n · m) ⟩ n + (m + n · m) ∎) lemma-·-commutative : (n m : ℕ) → (n · m ≡ m · n) lemma-·-commutative zero m = symm (lemma-·-zero m) lemma-·-commutative (succ n) m = begin (succ n) · m ≡⟨⟩ m + n · m ≡⟨ cong (_+_ m) (lemma-·-commutative n m) ⟩ m + m · n ≡⟨ symm (lemma-·-succ m n) ⟩ m · (succ n) ∎ -- VECTORS data Vector (A : Set) : ℕ → Set where [] : Vector A zero _∷_ : {n : ℕ} → A → Vector A n → Vector A (succ n) head : {A : Set} {n : ℕ} → Vector A (succ n) → A head (x ∷ xs) = x tail : {A : Set} {n : ℕ} → Vector A (succ n) → Vector A n tail (x ∷ xs) = xs map : {A B : Set} {n : ℕ} → (A → B) → Vector A n → Vector B n map f [] = [] map f (x ∷ xs) = f x ∷ map f xs zipWith : {A B C : Set} {n : ℕ} → (A → B → C) → Vector A n → Vector B n → Vector C n zipWith f [] [] = [] zipWith f (x ∷ xs) (y ∷ ys) = f x y ∷ zipWith f xs ys drop : {A : Set} {n : ℕ} (k : ℕ) → Vector A (k + n) → Vector A n drop zero xs = xs drop (succ k') (x ∷ xs') = drop k' xs' take : {A : Set} {n : ℕ} (k : ℕ) → Vector A (k + n) → Vector A k take zero xs = [] take (succ k') (x ∷ xs') = x ∷ take k' xs' _++_ : {A : Set} {n m : ℕ} → Vector A n → Vector A m → Vector A (n + m) [] ++ ys = ys (x ∷ xs') ++ ys = x ∷ (xs' ++ ys) snoc : {A : Set} {n : ℕ} → Vector A n → A → Vector A (succ n) snoc [] x = x ∷ [] snoc (y ∷ ys) x = y ∷ snoc ys x reverse : {A : Set} {n : ℕ} → Vector A n → Vector A n reverse [] = [] reverse (x ∷ xs) = snoc (reverse xs) x data _∈_ {A : Set} (x : A) : {n : ℕ} → Vector A n → Set where here : {n : ℕ} {xs : Vector A n} → x ∈ (x ∷ xs) elsewhere : {n : ℕ} {xs : Vector A n} {y : A} → x ∈ xs → x ∈ (y ∷ xs) data Fin : ℕ → Set where zero : {n : ℕ} → Fin (succ n) succ : {n : ℕ} → Fin n → Fin (succ n) -- "Fin n" is a type with exactly n inhabitants. _‼_ : {A : Set} {n : ℕ} → Vector A n → Fin n → A (x ∷ xs) ‼ zero = x (x ∷ xs) ‼ succ k = xs ‼ k concat : {A : Set} {n m : ℕ} → Vector (Vector A n) m → Vector A (m · n) concat [] = [] concat (xs ∷ xss) = xs ++ concat xss -- PROPERTIES OF VECTOR FUNCTIONS lemma-reverse-snoc : {A : Set} {n : ℕ} → (xs : Vector A n) (x : A) → reverse (snoc xs x) ≡ (x ∷ reverse xs) lemma-reverse-snoc [] x = refl lemma-reverse-snoc (y ∷ xs) x = cong (λ zs → snoc zs y) (lemma-reverse-snoc xs x) lemma-reverse-reverse : {A : Set} {n : ℕ} → (xs : Vector A n) → reverse (reverse xs) ≡ xs lemma-reverse-reverse [] = refl lemma-reverse-reverse (x ∷ xs) = trans (lemma-reverse-snoc (reverse xs) x) (cong (_∷_ x) (lemma-reverse-reverse xs)) lemma-take-drop : {A : Set} {n : ℕ} → (k : ℕ) → (xs : Vector A (k + n)) → take k xs ++ drop k xs ≡ xs lemma-take-drop zero xs = refl lemma-take-drop (succ k) (x ∷ xs) = cong (_∷_ x) (lemma-take-drop k xs) -- SORTING & STUFF