-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEquivalence_of_reverse_definitions.thy
183 lines (166 loc) · 5.5 KB
/
Equivalence_of_reverse_definitions.thy
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
(* Equivalence_of_reverse_definitions.thy
-- Equivalence of reverse definitions.
-- José A. Alonso <https://jaalonso.github.io>
-- Seville, August 19, 2024
-- ------------------------------------------------------------------ *)
(* ---------------------------------------------------------------------
-- In Isabelle/HOL, the function
-- rev :: "'a list \<Rightarrow> 'a list"
-- is defined such that (rev xs) is the list obtained by reversing
-- the order of elements in xs, such that the first element becomes the
-- last, the second element becomes the second to last, and so on,
-- resulting in a new list with the same elements but in the opposite
-- sequence. For example,
-- rev [3,2,5,1] = [1,5,2,3]
--
-- Its definition is
-- primrec rev :: "'a list \<Rightarrow> 'a list" where
-- "rev [] = []"
-- | "rev (x # xs) = rev xs @ [x]"
--
-- An alternative definition is
-- fun reverse_aux :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
-- "reverse_aux [] ys = ys"
-- | "reverse_aux (x#xs) ys = reverse_aux xs (x#ys)"
--
-- fun reverse :: "'a list \<Rightarrow> 'a list" where
-- "reverse xs = reverse_aux xs []"
--
-- Prove that the two definitions are equivalent; that is,
-- reverse xs = reverse2 xs
-- ------------------------------------------------------------------ *)
theory Equivalence_of_reverse_definitions
imports Main
begin
(* Alternative definition *)
(* ====================== *)
fun reverse_aux :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
"reverse_aux [] ys = ys"
| "reverse_aux (x#xs) ys = reverse_aux xs (x#ys)"
fun reverse :: "'a list \<Rightarrow> 'a list" where
"reverse xs = reverse_aux xs []"
(* Auxiliar lemma: reverse_aux xs ys = (rev xs) @ ys *)
(* ================================================= *)
(* Proof 1 of the auxiliary lemma *)
lemma
"reverse_aux xs ys = (rev xs) @ ys"
proof (induct xs arbitrary: ys)
fix ys :: "'a list"
have "reverse_aux [] ys = ys"
by (simp only: reverse_aux.simps(1))
also have "\<dots> = [] @ ys"
by (simp only: append.simps(1))
also have "\<dots> = rev [] @ ys"
by (simp only: rev.simps(1))
finally show "reverse_aux [] ys = rev [] @ ys"
by this
next
fix a ::'a and xs :: "'a list"
assume HI: "\<And>ys. reverse_aux xs ys = rev xs@ys"
show "\<And>ys. reverse_aux (a#xs) ys = rev (a#xs)@ys"
proof -
fix ys
have "reverse_aux (a#xs) ys = reverse_aux xs (a#ys)"
by (simp only: reverse_aux.simps(2))
also have "\<dots> = rev xs@(a#ys)"
by (simp only: HI)
also have "\<dots> = rev xs @ ([a] @ ys)"
by (simp only: append.simps)
also have "\<dots> = (rev xs @ [a]) @ ys"
by (simp only: append_assoc)
also have "\<dots> = rev (a # xs) @ ys"
by (simp only: rev.simps(2))
finally show "reverse_aux (a#xs) ys = rev (a#xs)@ys"
by this
qed
qed
(* Proof 2 of the auxiliary lemma *)
lemma
"reverse_aux xs ys = (rev xs) @ ys"
proof (induct xs arbitrary: ys)
fix ys :: "'a list"
have "reverse_aux [] ys = ys" by simp
also have "\<dots> = [] @ ys" by simp
also have "\<dots> = rev [] @ ys" by simp
finally show "reverse_aux [] ys = rev [] @ ys" .
next
fix a ::'a and xs :: "'a list"
assume HI: "\<And>ys. reverse_aux xs ys = rev xs@ys"
show "\<And>ys. reverse_aux (a#xs) ys = rev (a#xs)@ys"
proof -
fix ys
have "reverse_aux (a#xs) ys = reverse_aux xs (a#ys)" by simp
also have "\<dots> = rev xs@(a#ys)" using HI by simp
also have "\<dots> = rev xs @ ([a] @ ys)" by simp
also have "\<dots> = (rev xs @ [a]) @ ys" by simp
also have "\<dots> = rev (a # xs) @ ys" by simp
finally show "reverse_aux (a#xs) ys = rev (a#xs)@ys" .
qed
qed
(* Proof 3 of the auxiliary lemma *)
lemma
"reverse_aux xs ys = (rev xs) @ ys"
proof (induct xs arbitrary: ys)
fix ys :: "'a list"
show "reverse_aux [] ys = rev [] @ ys" by simp
next
fix a ::'a and xs :: "'a list"
assume HI: "\<And>ys. reverse_aux xs ys = rev xs@ys"
show "\<And>ys. reverse_aux (a#xs) ys = rev (a#xs)@ys"
proof -
fix ys
have "reverse_aux (a#xs) ys = rev xs@(a#ys)" using HI by simp
also have "\<dots> = rev (a # xs) @ ys" by simp
finally show "reverse_aux (a#xs) ys = rev (a#xs)@ys" .
qed
qed
(* Proof 4 of the auxiliary lemma *)
lemma
"reverse_aux xs ys = (rev xs) @ ys"
proof (induct xs arbitrary: ys)
show "\<And>ys. reverse_aux [] ys = rev [] @ ys" by simp
next
fix a ::'a and xs :: "'a list"
assume "\<And>ys. reverse_aux xs ys = rev xs@ys"
then show "\<And>ys. reverse_aux (a#xs) ys = rev (a#xs)@ys" by simp
qed
(* Proof 5 of the auxiliary lemma *)
lemma
"reverse_aux xs ys = (rev xs) @ ys"
proof (induct xs arbitrary: ys)
case Nil
then show ?case by simp
next
case (Cons a xs)
then show ?case by simp
qed
(* Proof 6 of the auxiliary lemma *)
lemma reverse_equiv:
"reverse_aux xs ys = (rev xs) @ ys"
by (induct xs arbitrary: ys) simp_all
(* Proofs of the main lemma *)
(* ======================== *)
(* Proof 1 *)
lemma "reverse xs = rev xs"
proof -
have "reverse xs = reverse_aux xs []"
by (rule reverse.simps)
also have "\<dots> = (rev xs) @ []"
by (rule reverse_equiv)
also have "\<dots> = rev xs"
by (rule append.right_neutral)
finally show "reverse xs = rev xs"
by this
qed
(* Proof 2 *)
lemma "reverse xs = rev xs"
proof -
have "reverse xs = reverse_aux xs []" by simp
also have "\<dots> = (rev xs) @ []" by (rule reverse_equiv)
also have "\<dots> = rev xs" by simp
finally show "reverse xs = rev xs" .
qed
(* Proof 3 *)
lemma "reverse xs = rev xs"
by (simp add: reverse_equiv)
end