-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlatten_of_mirror.thy
158 lines (144 loc) · 4.31 KB
/
Flatten_of_mirror.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
(* Flatten_of_mirror.lean
-- Proofs of "flatten (mirror a) = reverse (flatten a)"
-- José A. Alonso <https://jaalonso.github.io>
-- Seville, August 28, 2024
-- ------------------------------------------------------------------ *)
(* ---------------------------------------------------------------------
-- The tree corresponding to
-- 3
-- / \
-- 2 4
-- / \
-- 1 5
-- can be represented by the term
-- Node 3 (Node 2 (Leaf 1) (Leaf 5)) (Leaf 4)
-- using the datatype defined by
-- datatype 'a tree = Leaf "'a"
-- | Node "'a" "'a tree" "'a tree"
--
-- The mirror image of the previous tree is
-- 3
-- / \
-- 4 2
-- / \
-- 5 1
-- and the list obtained by flattening it (traversing it in infix order)
-- is
-- [4, 3, 5, 2, 1]
--
-- The definition of the function that calculates the mirror image is
-- fun mirror :: "'a tree \<Rightarrow> 'a tree" where
-- "mirror (Leaf x) = (Leaf x)"
-- | "mirror (Node x i d) = (Node x (mirror d) (mirror i))"
-- and the one that flattens the tree is
-- fun flatten :: "'a tree \<Rightarrow> 'a list" where
-- "flatten (Leaf x) = [x]"
-- | "flatten (Node x i d) = (flatten i) @ [x] @ (flatten d)"
--
-- Prove that
-- flatten (mirror a) = rev (flatten a)
-- ------------------------------------------------------------------ *)
theory Flatten_of_mirror
imports Main
begin
datatype 'a tree = Leaf "'a"
| Node "'a" "'a tree" "'a tree"
fun mirror :: "'a tree \<Rightarrow> 'a tree" where
"mirror (Leaf x) = (Leaf x)"
| "mirror (Node x i d) = (Node x (mirror d) (mirror i))"
fun flatten :: "'a tree \<Rightarrow> 'a list" where
"flatten (Leaf x) = [x]"
| "flatten (Node x i d) = (flatten i) @ [x] @ (flatten d)"
(* Auxiliary lemma *)
(* =============== *)
(* Proof 1 of the auxiliary lemma *)
lemma "rev [x] = [x]"
proof -
have "rev [x] = rev [] @ [x]"
by (simp only: rev.simps(2))
also have "\<dots> = [] @ [x]"
by (simp only: rev.simps(1))
also have "\<dots> = [x]"
by (simp only: append.simps(1))
finally show ?thesis
by this
qed
(* Proof 2 of the auxiliary lemma *)
lemma rev_unit: "rev [x] = [x]"
by simp
(* Main lemma *)
(* ========== *)
(* Proof 1 *)
lemma
fixes a :: "'b tree"
shows "flatten (mirror a) = rev (flatten a)" (is "?P a")
proof (induct a)
fix x :: 'b
have "flatten (mirror (Leaf x)) = flatten (Leaf x)"
by (simp only: mirror.simps(1))
also have "\<dots> = [x]"
by (simp only: flatten.simps(1))
also have "\<dots> = rev [x]"
by (rule rev_unit [symmetric])
also have "\<dots> = rev (flatten (Leaf x))"
by (simp only: flatten.simps(1))
finally show "?P (Leaf x)"
by this
next
fix x :: 'b
fix i assume h1: "?P i"
fix d assume h2: "?P d"
show "?P (Node x i d)"
proof -
have "flatten (mirror (Node x i d)) =
flatten (Node x (mirror d) (mirror i))"
by (simp only: mirror.simps(2))
also have "\<dots> = (flatten (mirror d)) @ [x] @ (flatten (mirror i))"
by (simp only: flatten.simps(2))
also have "\<dots> = (rev (flatten d)) @ [x] @ (rev (flatten i))"
by (simp only: h1 h2)
also have "\<dots> = rev ((flatten i) @ [x] @ (flatten d))"
by (simp only: rev_append rev_unit append_assoc)
also have "\<dots> = rev (flatten (Node x i d))"
by (simp only: flatten.simps(2))
finally show ?thesis
by this
qed
qed
(* Proof 2 *)
lemma
fixes a :: "'b tree"
shows "flatten (mirror a) = rev (flatten a)" (is "?P a")
proof (induct a)
fix x
show "?P (Leaf x)" by simp
next
fix x
fix i assume h1: "?P i"
fix d assume h2: "?P d"
show "?P (Node x i d)"
proof -
have "flatten (mirror (Node x i d)) =
flatten (Node x (mirror d) (mirror i))" by simp
also have "\<dots> = (flatten (mirror d)) @ [x] @ (flatten (mirror i))"
by simp
also have "\<dots> = (rev (flatten d)) @ [x] @ (rev (flatten i))"
using h1 h2 by simp
also have "\<dots> = rev ((flatten i) @ [x] @ (flatten d))" by simp
also have "\<dots> = rev (flatten (Node x i d))" by simp
finally show ?thesis .
qed
qed
(* Proof 3 *)
lemma "flatten (mirror a) = rev (flatten a)"
proof (induct a)
case (Leaf x)
then show ?case by simp
next
case (Node x i d)
then show ?case by simp
qed
(* Proof 4 *)
lemma "flatten (mirror a) = rev (flatten a)"
by (induct a) simp_all
end