author | Chengsong |
Mon, 04 Jul 2022 12:43:03 +0100 | |
changeset 559 | 9d18f3eac484 |
parent 95 | a33d3040bf7e |
permissions | -rw-r--r-- |
14 | 1 |
theory MyFirst |
2 |
imports Main |
|
3 |
begin |
|
4 |
||
5 |
datatype 'a list = Nil | Cons 'a "'a list" |
|
6 |
||
7 |
fun app :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
8 |
"app Nil ys = ys" | |
|
9 |
"app (Cons x xs) ys = Cons x (app xs ys)" |
|
10 |
||
11 |
fun rev :: "'a list \<Rightarrow> 'a list" where |
|
12 |
"rev Nil = Nil" | |
|
13 |
"rev (Cons x xs) = app (rev xs) (Cons x Nil)" |
|
14 |
||
15 |
value "rev(Cons True (Cons False Nil))" |
|
16 |
||
17 |
value "1 + (2::nat)" |
|
18 |
value "1 + (2::int)" |
|
19 |
value "1 - (2::nat)" |
|
20 |
value "1 - (2::int)" |
|
21 |
||
22 |
lemma app_Nil2 [simp]: "app xs Nil = xs" |
|
23 |
apply(induction xs) |
|
24 |
apply(auto) |
|
25 |
done |
|
26 |
||
27 |
lemma app_assoc [simp]: "app (app xs ys) zs = app xs (app ys zs)" |
|
28 |
apply(induction xs) |
|
29 |
apply(auto) |
|
30 |
done |
|
31 |
||
32 |
lemma rev_app [simp]: "rev(app xs ys) = app (rev ys) (rev xs)" |
|
33 |
apply (induction xs) |
|
34 |
apply (auto) |
|
35 |
done |
|
36 |
||
37 |
theorem rev_rev [simp]: "rev(rev xs) = xs" |
|
38 |
apply (induction xs) |
|
39 |
apply (auto) |
|
40 |
done |
|
41 |
||
42 |
fun add :: "nat \<Rightarrow> nat \<Rightarrow> nat" where |
|
43 |
"add 0 n = n" | |
|
44 |
"add (Suc m) n = Suc(add m n)" |
|
45 |
||
46 |
lemma add_02: "add m 0 = m" |
|
47 |
apply(induction m) |
|
48 |
apply(auto) |
|
49 |
done |
|
50 |
||
22 | 51 |
value "add 2 3" |
52 |
||
53 |
||
16 | 54 |
(**commutative-associative**) |
20
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
55 |
lemma add_04: "add m (add n k) = add (add m n) k" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
56 |
apply(induct m) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
57 |
apply(simp_all) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
58 |
done |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
59 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
60 |
lemma add_zero: "add n 0 = n" |
21 | 61 |
apply(induct n) |
62 |
apply(auto) |
|
63 |
done |
|
20
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
64 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
65 |
lemma add_Suc: "add m (Suc n) = Suc (add m n)" |
21 | 66 |
apply(induct m) |
22 | 67 |
apply(metis add.simps(1)) |
21 | 68 |
apply(auto) |
69 |
done |
|
20
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
70 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
71 |
lemma add_comm: "add m n = add n m" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
72 |
apply(induct m) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
73 |
apply(simp add: add_zero) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
74 |
apply(simp add: add_Suc) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
75 |
done |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
76 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
77 |
lemma add_odd: "add m (add n k) = add k (add m n)" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
78 |
apply(subst add_04) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
79 |
apply(subst (2) add_comm) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
80 |
apply(simp) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
81 |
done |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
82 |
|
14 | 83 |
|
84 |
fun dub :: "nat \<Rightarrow> nat" where |
|
85 |
"dub 0 = 0" | |
|
86 |
"dub m = add m m" |
|
87 |
||
88 |
lemma dub_01: "dub 0 = 0" |
|
89 |
apply(induct) |
|
90 |
apply(auto) |
|
91 |
done |
|
92 |
||
93 |
lemma dub_02: "dub m = add m m" |
|
94 |
apply(induction m) |
|
95 |
apply(auto) |
|
96 |
done |
|
97 |
||
98 |
value "dub 2" |
|
99 |
||
100 |
fun trip :: "nat \<Rightarrow> nat" where |
|
101 |
"trip 0 = 0" | |
|
102 |
"trip m = add m (add m m)" |
|
103 |
||
104 |
lemma trip_01: "trip 0 = 0" |
|
105 |
apply(induct) |
|
106 |
apply(auto) |
|
107 |
done |
|
108 |
||
109 |
lemma trip_02: "trip m = add m (add m m)" |
|
110 |
apply(induction m) |
|
111 |
apply(auto) |
|
112 |
done |
|
113 |
||
114 |
value "trip 1" |
|
115 |
value "trip 2" |
|
116 |
||
20
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
117 |
fun sum :: "nat \<Rightarrow> nat" where |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
118 |
"sum 0 = 0" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
119 |
| "sum (Suc n) = (Suc n) + sum n" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
120 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
121 |
function sum1 :: "nat \<Rightarrow> nat" where |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
122 |
"sum1 0 = 0" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
123 |
| "n \<noteq> 0 \<Longrightarrow> sum1 n = n + sum1 (n - 1)" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
124 |
apply(auto) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
125 |
done |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
126 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
127 |
termination sum1 |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
128 |
by (smt2 "termination" diff_less less_than_iff not_gr0 wf_less_than zero_neq_one) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
129 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
130 |
lemma "sum n = sum1 n" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
131 |
apply(induct n) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
132 |
apply(auto) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
133 |
done |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
134 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
135 |
lemma "sum n = (\<Sum>i \<le> n. i)" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
136 |
apply(induct n) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
137 |
apply(simp_all) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
138 |
done |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
139 |
|
14 | 140 |
fun mull :: "nat \<Rightarrow> nat \<Rightarrow> nat" where |
141 |
"mull 0 0 = 0" | |
|
142 |
"mull m 0 = 0" | |
|
20
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
143 |
"mull m 1 = m" | |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
144 |
(**"mull m (1::nat) = m" | **) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
145 |
(**"mull m (suc(0)) = m" | **) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
146 |
"mull m n = mull m (n-(1::nat))" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
147 |
apply(pat_completeness) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
148 |
apply(auto) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
149 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
150 |
done |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
151 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
152 |
"mull 0 n = 0" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
153 |
| "mull (Suc m) n = add n (mull m n)" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
154 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
155 |
lemma test: "mull m n = m * n" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
156 |
sorry |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
157 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
158 |
fun poww :: "nat \<Rightarrow> nat \<Rightarrow> nat" where |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
159 |
"poww 0 n = 1" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
160 |
| "poww (Suc m) n = mull n (poww m n)" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
161 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
162 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
163 |
"mull 0 0 = 0" | |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
164 |
"mull m 0 = 0" | |
15 | 165 |
(**"mull m 1 = m" | **) |
17 | 166 |
(**"mull m (1::nat) = m" | **) |
167 |
(**"mull m (suc(0)) = m" | **) |
|
168 |
"mull m n = mull m (n-(1::nat))" |
|
14 | 169 |
|
17 | 170 |
(**Define a function that counts the |
171 |
number of occurrences of an element in a list **) |
|
14 | 172 |
(** |
173 |
fun count :: "'a\<Rightarrow>'a list\<Rightarrow>nat" where |
|
174 |
"count " |
|
175 |
**) |
|
176 |
||
20
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
177 |
|
19 | 178 |
(* prove n = n * (n + 1) div 2 *) |
14 | 179 |
|
180 |
||
181 |
||
182 |
||
183 |
||
184 |
||
185 |
||
186 |
||
187 |
||
19 | 188 |
|
189 |