thys/MyFirst.thy
author fahadausaf <fahad.ausaf@icloud.com>
Mon, 06 Oct 2014 14:22:13 +0100
changeset 19 66c9c06c0f0e
parent 17 a5427713eef4
child 20 c11651bbebf5
permissions -rw-r--r--
abc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     1
theory MyFirst
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     2
imports Main
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     3
begin
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     4
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     5
datatype 'a list = Nil | Cons 'a "'a list"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     6
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     7
fun app :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     8
"app Nil ys = ys" |
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     9
"app (Cons x xs) ys = Cons x (app xs ys)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    10
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    11
fun rev :: "'a list \<Rightarrow> 'a list" where
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    12
"rev Nil = Nil" |
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    13
"rev (Cons x xs) = app (rev xs) (Cons x Nil)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    14
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    15
value "rev(Cons True (Cons False Nil))"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    16
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    17
value "1 + (2::nat)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    18
value "1 + (2::int)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    19
value "1 - (2::nat)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    20
value "1 - (2::int)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    21
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    22
lemma app_Nil2 [simp]: "app xs Nil = xs"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    23
apply(induction xs)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    24
apply(auto)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    25
done
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    26
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    27
lemma app_assoc [simp]: "app (app xs ys) zs = app xs (app ys zs)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    28
apply(induction xs)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    29
apply(auto)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    30
done
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    31
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    32
lemma rev_app [simp]: "rev(app xs ys) = app (rev ys) (rev xs)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    33
apply (induction xs)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    34
apply (auto)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    35
done
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    36
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    37
theorem rev_rev [simp]: "rev(rev xs) = xs"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    38
apply (induction xs)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    39
apply (auto)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    40
done
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    41
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    42
fun add :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    43
"add 0 n = n" |
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    44
"add (Suc m) n = Suc(add m n)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    45
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    46
lemma add_02: "add m 0 = m"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    47
apply(induction m)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    48
apply(auto)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    49
done
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    50
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    51
value "add 2 3"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    52
16
a92c10af61bd associative-commutative
fahadausaf <fahad.ausaf@icloud.com>
parents: 15
diff changeset
    53
(**commutative-associative**)
14
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    54
lemma add_04: "add m (add n k) = add k (add m n)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    55
apply(induct)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    56
apply(auto)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    57
oops
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    58
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    59
fun dub :: "nat \<Rightarrow> nat" where
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    60
"dub 0 = 0" |
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    61
"dub m = add m m"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    62
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    63
lemma dub_01: "dub 0 = 0"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    64
apply(induct)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    65
apply(auto)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    66
done
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    67
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    68
lemma dub_02: "dub m = add m m"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    69
apply(induction m)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    70
apply(auto)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    71
done
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    72
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    73
value "dub 2"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    74
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    75
fun trip :: "nat \<Rightarrow> nat" where
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    76
"trip 0 = 0" |
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    77
"trip m = add m (add m m)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    78
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    79
lemma trip_01: "trip 0 = 0"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    80
apply(induct)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    81
apply(auto)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    82
done
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    83
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    84
lemma trip_02: "trip m = add m (add m m)"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    85
apply(induction m)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    86
apply(auto)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    87
done
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    88
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    89
value "trip 1"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    90
value "trip 2"
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    91
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    92
fun mull :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    93
"mull 0 0 = 0" |
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    94
"mull m 0 = 0" |
15
bdabf71fa35b multiply
fahadausaf <fahad.ausaf@icloud.com>
parents: 14
diff changeset
    95
(**"mull m 1 = m" | **)
17
a5427713eef4 multiply 2
fahadausaf <fahad.ausaf@icloud.com>
parents: 16
diff changeset
    96
(**"mull m (1::nat) = m" | **)
a5427713eef4 multiply 2
fahadausaf <fahad.ausaf@icloud.com>
parents: 16
diff changeset
    97
(**"mull m (suc(0)) = m" | **)
a5427713eef4 multiply 2
fahadausaf <fahad.ausaf@icloud.com>
parents: 16
diff changeset
    98
"mull m n = mull m (n-(1::nat))" 
14
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    99
17
a5427713eef4 multiply 2
fahadausaf <fahad.ausaf@icloud.com>
parents: 16
diff changeset
   100
(**Define a function that counts the
a5427713eef4 multiply 2
fahadausaf <fahad.ausaf@icloud.com>
parents: 16
diff changeset
   101
number of occurrences of an element in a list **)
14
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   102
(**
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   103
fun count :: "'a\<Rightarrow>'a list\<Rightarrow>nat" where
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   104
"count  "
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   105
**)
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   106
19
fahadausaf <fahad.ausaf@icloud.com>
parents: 17
diff changeset
   107
fun sum :: "nat \<Rightarrow> nat" where
fahadausaf <fahad.ausaf@icloud.com>
parents: 17
diff changeset
   108
"sum n = 0 + \<dots> + n"
fahadausaf <fahad.ausaf@icloud.com>
parents: 17
diff changeset
   109
(* prove n = n * (n + 1) div 2  *)
14
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   110
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   111
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   112
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   113
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   114
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   115
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   116
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   117
5c6f9325327f calculator
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
   118
19
fahadausaf <fahad.ausaf@icloud.com>
parents: 17
diff changeset
   119
fahadausaf <fahad.ausaf@icloud.com>
parents: 17
diff changeset
   120