-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW15.agda
151 lines (129 loc) · 4.59 KB
/
HW15.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
module HW15 where
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl; trans; sym; cong; cong-app; subst)
open Eq.≡-Reasoning using (begin_; _≡⟨⟩_; step-≡; _∎)
open import Data.List using (List; []; _∷_; _++_; foldr)
module problem-1 where
++-assoc : ∀ {A : Set}
(xs ys zs : List A)
-----------------------------------
→ (xs ++ ys) ++ zs ≡ xs ++ (ys ++ zs)
++-assoc {A} [] ys zs = refl
++-assoc {A} (x ∷ xs) ys zs =
begin
x ∷ (xs ++ ys) ++ zs
≡⟨ cong (x ∷_) (++-assoc xs ys zs) ⟩
x ∷ xs ++ ys ++ zs
∎
-- tips: to input the superscript l (ˡ), type \^l and use your
-- arrow keys to select it (should be the fourth candidate).
++-identityˡ : ∀ {A : Set}
(xs : List A)
-------------
→ [] ++ xs ≡ xs
++-identityˡ xs = refl
-- you might have already guessed it: type \^r for superscript r (ʳ)
-- again, use your arrow keys to select it (the fourth candidate).
++-identityʳ : ∀ {A : Set}
(xs : List A)
→ xs ++ [] ≡ xs
++-identityʳ [] = refl
++-identityʳ (x ∷ xs) =
begin
x ∷ xs ++ []
≡⟨ cong (x ∷_) (++-identityʳ xs) ⟩
x ∷ xs
∎
module problem-2 where
-- tips: to input ⊗, type \otimes
foldr-++ : ∀ {A : Set}
(_⊗_ : A → A → A)
(e : A)
(xs ys : List A)
-----------------------------
→ foldr _⊗_ e (xs ++ ys)
≡ foldr _⊗_ (foldr _⊗_ e ys) xs
foldr-++ _⊗_ e [] ys = refl
foldr-++ _⊗_ e (x ∷ xs) ys =
begin
(x ⊗ foldr _⊗_ e (xs ++ ys))
≡⟨ cong (x ⊗_) (foldr-++ _⊗_ e xs ys) ⟩
(x ⊗ foldr _⊗_ (foldr _⊗_ e ys) xs)
∎
module problem-3 (
extensionality : ∀ {A : Set} {B : A → Set}
{f g : (x : A) → B x}
→ ((x : A) → f x ≡ g x)
---------------------
→ f ≡ g
) where
open import Function using (id; _∘_)
reverse : ∀ {A : Set} → List A → List A
reverse [] = []
reverse (x ∷ xs) = reverse xs ++ (x ∷ [])
-- hint: you might want to introduce an extra lemma to prove this.
reverse-tail : forall {A} -> (x : A) -> (xs : List A) -> reverse ( xs ++ x ∷ [] ) ≡ x ∷ (reverse xs)
reverse-tail x [] = refl
reverse-tail x (x₁ ∷ xs) = begin
reverse (xs ++ x ∷ []) ++ x₁ ∷ []
≡⟨ cong (_++ x₁ ∷ []) (reverse-tail x xs) ⟩
x ∷ reverse xs ++ x₁ ∷ []
∎
reverse-involutive-lemma : forall {A} -> (xs : List A) -> reverse (reverse xs) ≡ xs
reverse-involutive-lemma [] = refl
reverse-involutive-lemma (x ∷ xs) = begin
reverse (reverse xs ++ x ∷ [])
≡⟨ reverse-tail x (reverse xs) ⟩
x ∷ reverse (reverse xs)
≡⟨ cong (x ∷_) (reverse-involutive-lemma xs) ⟩
x ∷ xs
∎
reverse-involutive : ∀ {A : Set} → reverse {A} ∘ reverse {A} ≡ id
reverse-involutive = extensionality reverse-involutive-lemma
-- bonus: fast-reverse-involutive
-- this is only for practice, not part of the homework this week
fast-reverse : ∀ {A : Set} → List A → List A
fast-reverse = helper []
module FastReverse where
helper : ∀ {A : Set} → List A → List A → List A
helper res [] = res
helper res (x ∷ xs) = helper (x ∷ res) xs
reverse-helper : ∀ {A : Set} (xs ys : List A) -> FastReverse.helper {A} xs ys ≡ reverse ys ++ xs
reverse-helper {A} xs [] = refl
reverse-helper {A} xs (x ∷ ys) =
begin
FastReverse.helper {A} (x ∷ xs) ys
≡⟨ reverse-helper (x ∷ xs) ys ⟩
reverse ys ++ (x ∷ [] ++ xs)
≡⟨ sym (problem-1.++-assoc (reverse ys) (x ∷ []) xs) ⟩
(reverse ys ++ x ∷ []) ++ xs
∎
++-identityʳ : ∀ {A : Set} (xs : List A) → xs ++ [] ≡ xs
++-identityʳ [] = refl
++-identityʳ (x ∷ xs) =
begin
x ∷ (xs ++ [])
≡⟨ cong (x ∷_) (++-identityʳ xs) ⟩
x ∷ xs
∎
reverse-equal : ∀ {A : Set} (xs : List A) -> fast-reverse xs ≡ reverse xs
reverse-equal {A} xs =
begin
FastReverse.helper {A} [] xs
≡⟨ reverse-helper [] xs ⟩
reverse xs ++ []
≡⟨ ++-identityʳ (reverse xs) ⟩
reverse xs
∎
fast-reverse-involutive : ∀ {A : Set}
→ fast-reverse {A} ∘ fast-reverse {A} ≡ id
fast-reverse-involutive = extensionality λ xs ->
begin
fast-reverse (fast-reverse xs)
≡⟨ reverse-equal (fast-reverse xs) ⟩
reverse (fast-reverse xs)
≡⟨ cong reverse (reverse-equal xs) ⟩
reverse (reverse xs)
≡⟨ reverse-involutive-lemma xs ⟩
xs
∎