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