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