author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Mon, 06 Oct 2014 15:24:41 +0100 | |
changeset 20 | c11651bbebf5 |
parent 19 | 66c9c06c0f0e |
child 21 | 893f0314a88b |
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 |
||
51 |
value "add 2 3" |
|
52 |
||
20
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
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" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
61 |
sorry |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
62 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
63 |
lemma add_Suc: "add m (Suc n) = Suc (add m n)" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
64 |
sorry |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
65 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
66 |
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
|
67 |
apply(induct m) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
68 |
apply(simp add: add_zero) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
69 |
apply(simp add: add_Suc) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
70 |
done |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
71 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
72 |
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
|
73 |
apply(subst add_04) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
74 |
apply(subst (2) add_comm) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
75 |
apply(simp) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
76 |
done |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
77 |
|
14 | 78 |
|
79 |
fun dub :: "nat \<Rightarrow> nat" where |
|
80 |
"dub 0 = 0" | |
|
81 |
"dub m = add m m" |
|
82 |
||
83 |
lemma dub_01: "dub 0 = 0" |
|
84 |
apply(induct) |
|
85 |
apply(auto) |
|
86 |
done |
|
87 |
||
88 |
lemma dub_02: "dub m = add m m" |
|
89 |
apply(induction m) |
|
90 |
apply(auto) |
|
91 |
done |
|
92 |
||
93 |
value "dub 2" |
|
94 |
||
95 |
fun trip :: "nat \<Rightarrow> nat" where |
|
96 |
"trip 0 = 0" | |
|
97 |
"trip m = add m (add m m)" |
|
98 |
||
99 |
lemma trip_01: "trip 0 = 0" |
|
100 |
apply(induct) |
|
101 |
apply(auto) |
|
102 |
done |
|
103 |
||
104 |
lemma trip_02: "trip m = add m (add m m)" |
|
105 |
apply(induction m) |
|
106 |
apply(auto) |
|
107 |
done |
|
108 |
||
109 |
value "trip 1" |
|
110 |
value "trip 2" |
|
111 |
||
20
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
112 |
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
|
113 |
"sum 0 = 0" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
114 |
| "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
|
115 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
116 |
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
|
117 |
"sum1 0 = 0" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
118 |
| "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
|
119 |
apply(auto) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
120 |
done |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
121 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
122 |
termination sum1 |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
123 |
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
|
124 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
125 |
lemma "sum n = sum1 n" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
126 |
apply(induct n) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
127 |
apply(auto) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
128 |
done |
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 = (\<Sum>i \<le> n. i)" |
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(simp_all) |
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 |
|
14 | 135 |
fun mull :: "nat \<Rightarrow> nat \<Rightarrow> nat" where |
136 |
"mull 0 0 = 0" | |
|
137 |
"mull m 0 = 0" | |
|
20
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
138 |
"mull m 1 = m" | |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
139 |
(**"mull m (1::nat) = m" | **) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
140 |
(**"mull m (suc(0)) = m" | **) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
141 |
"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
|
142 |
apply(pat_completeness) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
143 |
apply(auto) |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
144 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
145 |
done |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
146 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
147 |
"mull 0 n = 0" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
148 |
| "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
|
149 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
150 |
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
|
151 |
sorry |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
152 |
|
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
153 |
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
|
154 |
"poww 0 n = 1" |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
155 |
| "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
|
156 |
|
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 |
"mull 0 0 = 0" | |
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
159 |
"mull m 0 = 0" | |
15 | 160 |
(**"mull m 1 = m" | **) |
17 | 161 |
(**"mull m (1::nat) = m" | **) |
162 |
(**"mull m (suc(0)) = m" | **) |
|
163 |
"mull m n = mull m (n-(1::nat))" |
|
14 | 164 |
|
17 | 165 |
(**Define a function that counts the |
166 |
number of occurrences of an element in a list **) |
|
14 | 167 |
(** |
168 |
fun count :: "'a\<Rightarrow>'a list\<Rightarrow>nat" where |
|
169 |
"count " |
|
170 |
**) |
|
171 |
||
20
c11651bbebf5
some small changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
19
diff
changeset
|
172 |
|
19 | 173 |
(* prove n = n * (n + 1) div 2 *) |
14 | 174 |
|
175 |
||
176 |
||
177 |
||
178 |
||
179 |
||
180 |
||
181 |
||
182 |
||
19 | 183 |
|
184 |