thys/Chap03.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 14 Jan 2016 17:19:40 +0000
changeset 88 532bb9df225d
parent 54 45274393f28c
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     1
theory Chap03
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     2
imports Main
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     3
begin
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     4
54
45274393f28c no changes
fahad
parents: 52
diff changeset
     5
(* 2.5.6 Case Study: Boolean Expressions *)
45274393f28c no changes
fahad
parents: 52
diff changeset
     6
45274393f28c no changes
fahad
parents: 52
diff changeset
     7
datatype boolex = Const bool | Var nat | Neg boolex
45274393f28c no changes
fahad
parents: 52
diff changeset
     8
| And boolex boolex
45274393f28c no changes
fahad
parents: 52
diff changeset
     9
45274393f28c no changes
fahad
parents: 52
diff changeset
    10
primrec "value2" :: "boolex \<Rightarrow> (nat \<Rightarrow> bool) \<Rightarrow> bool" where
45274393f28c no changes
fahad
parents: 52
diff changeset
    11
"value2 (Const b)  env = b" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    12
"value2 (Var x)    env = env x" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    13
"value2 (Neg b)    env = (\<not> value2 b env)" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    14
"value2 (And b c)  env = (value2 b env \<and> value2 c env)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    15
45274393f28c no changes
fahad
parents: 52
diff changeset
    16
value "Const true"
45274393f28c no changes
fahad
parents: 52
diff changeset
    17
value "Var (Suc(0))"
45274393f28c no changes
fahad
parents: 52
diff changeset
    18
value "value2 (Const False) (\<lambda>x. False)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    19
value "value2 (Var 11) (\<lambda>x. if (x = 10 | x = 11) then True else False)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    20
value "value2 (Var 11) (\<lambda>x. True )"
45274393f28c no changes
fahad
parents: 52
diff changeset
    21
45274393f28c no changes
fahad
parents: 52
diff changeset
    22
definition
45274393f28c no changes
fahad
parents: 52
diff changeset
    23
  "en1 \<equiv>  (\<lambda>x. if x = 10 | x = 11 then True else False)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    24
  
45274393f28c no changes
fahad
parents: 52
diff changeset
    25
abbreviation
45274393f28c no changes
fahad
parents: 52
diff changeset
    26
  "en2 \<equiv>  (\<lambda>x. if x = 10 | x = 11 then True else False)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    27
  
45274393f28c no changes
fahad
parents: 52
diff changeset
    28
value "value2 (And (Var 10) (Var 11)) en2"
45274393f28c no changes
fahad
parents: 52
diff changeset
    29
45274393f28c no changes
fahad
parents: 52
diff changeset
    30
lemma "value2 (And (Var 10) (Var 11)) en2 = True"
45274393f28c no changes
fahad
parents: 52
diff changeset
    31
apply(simp)
45274393f28c no changes
fahad
parents: 52
diff changeset
    32
done
45274393f28c no changes
fahad
parents: 52
diff changeset
    33
45274393f28c no changes
fahad
parents: 52
diff changeset
    34
datatype ifex = 
45274393f28c no changes
fahad
parents: 52
diff changeset
    35
  CIF bool 
45274393f28c no changes
fahad
parents: 52
diff changeset
    36
| VIF nat 
45274393f28c no changes
fahad
parents: 52
diff changeset
    37
| IF ifex ifex ifex
45274393f28c no changes
fahad
parents: 52
diff changeset
    38
45274393f28c no changes
fahad
parents: 52
diff changeset
    39
primrec valif :: "ifex \<Rightarrow> (nat \<Rightarrow> bool) \<Rightarrow> bool" where
45274393f28c no changes
fahad
parents: 52
diff changeset
    40
"valif (CIF b)    env = b" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    41
"valif (VIF x)    env = env x" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    42
"valif (IF b t e) env = (if valif b env then valif t env else valif e env)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    43
45274393f28c no changes
fahad
parents: 52
diff changeset
    44
abbreviation "vif1 \<equiv> valif (CIF False) (\<lambda>x. False)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    45
abbreviation "vif2 \<equiv> valif (VIF 11) (\<lambda>x. False)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    46
abbreviation "vif3 \<equiv> valif (VIF 13) (\<lambda>x. True)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    47
45274393f28c no changes
fahad
parents: 52
diff changeset
    48
value "valif (CIF False) (\<lambda>x. False)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    49
value "valif (VIF 11) (\<lambda>x. True)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    50
value "valif (IF (CIF False) (CIF True) (CIF True))"
45274393f28c no changes
fahad
parents: 52
diff changeset
    51
45274393f28c no changes
fahad
parents: 52
diff changeset
    52
primrec bool2if :: "boolex \<Rightarrow> ifex" where
45274393f28c no changes
fahad
parents: 52
diff changeset
    53
"bool2if (Const b)  = CIF b" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    54
"bool2if (Var x)    = VIF x" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    55
"bool2if (Neg b)    = IF (bool2if b) (CIF False) (CIF True)" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    56
"bool2if (And b c)  = IF (bool2if b) (bool2if c) (CIF False)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    57
45274393f28c no changes
fahad
parents: 52
diff changeset
    58
lemma "valif (bool2if b) env = value2 b env"
45274393f28c no changes
fahad
parents: 52
diff changeset
    59
apply(induct_tac b)
45274393f28c no changes
fahad
parents: 52
diff changeset
    60
apply(auto)
45274393f28c no changes
fahad
parents: 52
diff changeset
    61
done
45274393f28c no changes
fahad
parents: 52
diff changeset
    62
45274393f28c no changes
fahad
parents: 52
diff changeset
    63
primrec normif :: "ifex \<Rightarrow> ifex \<Rightarrow> ifex \<Rightarrow> ifex" where
45274393f28c no changes
fahad
parents: 52
diff changeset
    64
"normif (CIF b)     t e = IF (CIF b) t e" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    65
"normif (VIF x)     t e = IF (VIF x) t e" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    66
"normif (IF b t e)  u f = normif b (normif t u f) (normif e u f)"
45274393f28c no changes
fahad
parents: 52
diff changeset
    67
45274393f28c no changes
fahad
parents: 52
diff changeset
    68
primrec norm :: "ifex \<Rightarrow> ifex" where
45274393f28c no changes
fahad
parents: 52
diff changeset
    69
"norm (CIF b)     = CIF b" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    70
"norm (VIF x)     = VIF x" |
45274393f28c no changes
fahad
parents: 52
diff changeset
    71
"norm (IF b t e)  = normif b (norm t) (norm e)"  
45274393f28c no changes
fahad
parents: 52
diff changeset
    72
45274393f28c no changes
fahad
parents: 52
diff changeset
    73
(***************   CHAPTER-3   ********************************)
45274393f28c no changes
fahad
parents: 52
diff changeset
    74
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    75
lemma "\<lbrakk> xs @ zs = ys @ xs; [] @ xs = [] @ [] \<rbrakk> \<Longrightarrow> ys = zs"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    76
apply simp
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    77
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    78
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    79
lemma "\<forall> x. f x = g (f (g x)) \<Longrightarrow> f [] = f [] @ []"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    80
apply (simp (no_asm))
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    81
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    82
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    83
definition xor :: "bool \<Rightarrow> bool \<Rightarrow> bool" where
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    84
"xor A B \<equiv> (A \<and> \<not>B) \<or> (\<not>A \<and> B)"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    85
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    86
lemma "xor A (\<not>A)"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    87
apply(simp only: xor_def)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    88
apply(simp add: xor_def)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    89
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    90
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    91
lemma "(let xs = [] in xs@ys@xs) = ys"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    92
apply(simp only: Let_def)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    93
apply(simp add: Let_def)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    94
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    95
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    96
(* 3.1.8 Conditioal Simplification Rules  *)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    97
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    98
lemma hd_Cons_tl: "xs \<noteq> [] \<Longrightarrow> hd xs # tl xs = xs"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    99
using [[simp_trace=true]]
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   100
apply(case_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   101
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   102
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   103
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   104
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   105
lemma "xs \<noteq> [] \<Longrightarrow> hd(rev xs) # tl(rev xs) = rev xs"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   106
apply(case_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   107
using [[simp_trace=true]]
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   108
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   109
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   110
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   111
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   112
(* 3.1.9 Automatic Case Splits  *)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   113
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   114
lemma "\<forall> xs. if xs = [] then rev xs = [] else rev xs \<noteq> []"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   115
apply(split split_if)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   116
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   117
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   118
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   119
lemma "(case xs of [] \<Rightarrow> zs | y#ys \<Rightarrow> y#(ys@zs)) = xs@zs"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   120
apply(split list.split)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   121
apply(simp split: list.split)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   122
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   123
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   124
lemma "if xs = [] then ys \<noteq> [] else ys = [] \<Longrightarrow> xs @ ys \<noteq> []"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   125
apply(split split_if_asm)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   126
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   127
apply(auto)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   128
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   129
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   130
(* 3.2 Induction Heuristics  *)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   131
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   132
primrec itrev :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   133
"itrev [] ys = ys" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   134
"itrev (x#xs) ys = itrev xs (x#ys)"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   135
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   136
lemma "itrev xs [] = rev xs"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   137
apply(induct_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   138
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   139
apply(auto)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   140
oops
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   141
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   142
lemma "itrev xs ys = rev xs @ ys"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   143
apply(induct_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   144
apply(simp_all)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   145
oops
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   146
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   147
lemma "\<forall> ys. itrev xs ys = rev xs @ ys"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   148
apply(induct_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   149
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   150
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   151
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   152
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   153
primrec add1 :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   154
"add1 m 0 = m" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   155
"add1 m (Suc n) = add1 (Suc m) n"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   156
51
fahad
parents: 50
diff changeset
   157
primrec add2 :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
fahad
parents: 50
diff changeset
   158
"add2 m 0 = m" |
fahad
parents: 50
diff changeset
   159
"add2 m (Suc n) = Suc (add2 m n)"
fahad
parents: 50
diff changeset
   160
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   161
value "add1 1 3"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   162
value "1 + 3"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   163
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   164
lemma abc [simp]: "add1 m 0 = m"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   165
apply(induction m)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   166
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   167
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   168
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   169
51
fahad
parents: 50
diff changeset
   170
lemma abc2: "\<forall>m. add1 m n = m + n"
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   171
apply(induction n)
51
fahad
parents: 50
diff changeset
   172
apply(simp)
fahad
parents: 50
diff changeset
   173
apply(simp del: add1.simps)
fahad
parents: 50
diff changeset
   174
apply(simp (no_asm) only: add1.simps)
fahad
parents: 50
diff changeset
   175
apply(simp only: )
fahad
parents: 50
diff changeset
   176
apply(simp)
fahad
parents: 50
diff changeset
   177
done
fahad
parents: 50
diff changeset
   178
fahad
parents: 50
diff changeset
   179
thm abc2
fahad
parents: 50
diff changeset
   180
fahad
parents: 50
diff changeset
   181
lemma abc3: "add1 m n = m + n"
fahad
parents: 50
diff changeset
   182
apply(induction n arbitrary: m)
fahad
parents: 50
diff changeset
   183
apply(simp)
fahad
parents: 50
diff changeset
   184
apply(simp del: add1.simps)
fahad
parents: 50
diff changeset
   185
apply(simp (no_asm) only: add1.simps)
fahad
parents: 50
diff changeset
   186
apply(simp only: )
fahad
parents: 50
diff changeset
   187
done
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   188
51
fahad
parents: 50
diff changeset
   189
thm abc3
fahad
parents: 50
diff changeset
   190
fahad
parents: 50
diff changeset
   191
lemma abc4: "add1 m n = add2 m n"
fahad
parents: 50
diff changeset
   192
apply(induction n arbitrary: m)
fahad
parents: 50
diff changeset
   193
apply(simp)
fahad
parents: 50
diff changeset
   194
apply(simp add: abc3)
fahad
parents: 50
diff changeset
   195
done
fahad
parents: 50
diff changeset
   196
54
45274393f28c no changes
fahad
parents: 52
diff changeset
   197
find_theorems "_ \<and> _ "
45274393f28c no changes
fahad
parents: 52
diff changeset
   198
45274393f28c no changes
fahad
parents: 52
diff changeset
   199
(* added anottest *)
51
fahad
parents: 50
diff changeset
   200
fahad
parents: 50
diff changeset
   201
lemma abc5: "add2 m n = m + n"
fahad
parents: 50
diff changeset
   202
apply(induction n)
fahad
parents: 50
diff changeset
   203
apply(simp)
fahad
parents: 50
diff changeset
   204
apply(simp)
fahad
parents: 50
diff changeset
   205
done
fahad
parents: 50
diff changeset
   206
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   207
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   208
(* 3.3 Case Study: Compiling Expressions  *)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   209
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   210
type_synonym 'v binop = "'v \<Rightarrow> 'v \<Rightarrow> 'v"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   211
datatype ('a, 'v)expr = 
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   212
  Cex 'v
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   213
| Vex 'a
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   214
| Bex "'v binop" "('a,'v)expr" "('a,'v)expr"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   215
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   216
primrec "value" :: "('a,'v)expr \<Rightarrow> ('a \<Rightarrow> 'v) \<Rightarrow> 'v" where
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   217
"value (Cex v) env = v" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   218
"value (Vex a) env = env a" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   219
"value (Bex f e1 e2) env = f (value e1 env) (value e2 env)"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   220
54
45274393f28c no changes
fahad
parents: 52
diff changeset
   221
value "value (Cex a) (\<lambda>x. True)"
45274393f28c no changes
fahad
parents: 52
diff changeset
   222
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   223
datatype ('a,'v)instr = 
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   224
  Const 'v
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   225
| Load 'a
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   226
| Apply "'v binop"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   227
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   228
primrec exec :: "('a,'v)instr list \<Rightarrow> ('a\<Rightarrow>'v) \<Rightarrow> 'v list \<Rightarrow> 'v list" where
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   229
"exec [] s vs = vs" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   230
"exec (i#is) s vs = (case i of
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   231
    Const v \<Rightarrow> exec is s (v#vs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   232
  | Load a \<Rightarrow> exec is s ((s a)#vs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   233
  | Apply f \<Rightarrow> exec is s ((f (hd vs) (hd(tl vs)))#(tl(tl vs))))"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   234
  
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   235
primrec compile :: "('a,'v)expr \<Rightarrow> ('a,'v)instr list" where
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   236
"compile (Cex v) = [Const v]" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   237
"compile (Vex a) = [Load a]" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   238
"compile (Bex f e1 e2) = (compile e2) @ (compile e1) @ [Apply f]"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   239
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   240
theorem "exec (compile e) s [] = [value e s]"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   241
(*the theorem needs to be generalized*)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   242
oops
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   243
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   244
(*more generalized theorem*)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   245
theorem "\<forall> vs. exec (compile e) s vs = (value e s) # vs"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   246
apply(induct_tac e)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   247
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   248
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   249
oops
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   250
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   251
lemma exec_app[simp]: "\<forall> vs. exec (xs@ys) s vs = exec ys s (exec xs s vs)"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   252
apply(induct_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   253
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   254
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   255
apply(simp split: instr.split)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   256
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   257
54
45274393f28c no changes
fahad
parents: 52
diff changeset
   258
(* 3.4 Advanced Datatypes  *)
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   259
54
45274393f28c no changes
fahad
parents: 52
diff changeset
   260
datatype 'a aexp = IF "'a bexp" "'a aexp" "'a aexp" 
45274393f28c no changes
fahad
parents: 52
diff changeset
   261
                | Sum "'a aexp" "'a aexp"
45274393f28c no changes
fahad
parents: 52
diff changeset
   262
                | Diff "'a aexp" "'a aexp"
45274393f28c no changes
fahad
parents: 52
diff changeset
   263
                | Var 'a
45274393f28c no changes
fahad
parents: 52
diff changeset
   264
                | Num nat
45274393f28c no changes
fahad
parents: 52
diff changeset
   265
and      'a bexp = Less "'a aexp" "'a aexp"
45274393f28c no changes
fahad
parents: 52
diff changeset
   266
                | And "'a bexp" "'a bexp"
45274393f28c no changes
fahad
parents: 52
diff changeset
   267
                | Neg "'a bexp"
45274393f28c no changes
fahad
parents: 52
diff changeset
   268
                
45274393f28c no changes
fahad
parents: 52
diff changeset
   269
(*  Total Recursive Functions: Fun  *)
45274393f28c no changes
fahad
parents: 52
diff changeset
   270
(*  3.5.1 Definition    *)
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   271
54
45274393f28c no changes
fahad
parents: 52
diff changeset
   272
fun fib :: "nat \<Rightarrow> nat" where
45274393f28c no changes
fahad
parents: 52
diff changeset
   273
"fib 0 = 0" |
45274393f28c no changes
fahad
parents: 52
diff changeset
   274
"fib (Suc 0) = 1" |
45274393f28c no changes
fahad
parents: 52
diff changeset
   275
"fib (Suc(Suc x)) = fib x + fib (Suc x)"
51
fahad
parents: 50
diff changeset
   276
54
45274393f28c no changes
fahad
parents: 52
diff changeset
   277
value "fib (Suc(Suc(Suc(Suc(Suc 0)))))"
45274393f28c no changes
fahad
parents: 52
diff changeset
   278
45274393f28c no changes
fahad
parents: 52
diff changeset
   279
fun sep :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
45274393f28c no changes
fahad
parents: 52
diff changeset
   280
"sep a [] = []" |
45274393f28c no changes
fahad
parents: 52
diff changeset
   281
"sep a [x] = [x]" |
45274393f28c no changes
fahad
parents: 52
diff changeset
   282
"sep a (x#y#zs) = x # a # sep a (y#zs)"
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   283
54
45274393f28c no changes
fahad
parents: 52
diff changeset
   284
fun last :: "'a list \<Rightarrow> 'a" where
45274393f28c no changes
fahad
parents: 52
diff changeset
   285
"last [x] = x" |
45274393f28c no changes
fahad
parents: 52
diff changeset
   286
"last (_#y#zs) = last (y#zs)"
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   287
54
45274393f28c no changes
fahad
parents: 52
diff changeset
   288
fun sep1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
45274393f28c no changes
fahad
parents: 52
diff changeset
   289
"sep1 a (x#y#zs) = x # a # sep1 a (y#zs)" |
45274393f28c no changes
fahad
parents: 52
diff changeset
   290
"sep1 _ xs = xs"
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   291
54
45274393f28c no changes
fahad
parents: 52
diff changeset
   292
fun swap12:: "'a list \<Rightarrow> 'a list" where
45274393f28c no changes
fahad
parents: 52
diff changeset
   293
"swap12 (x#y#zs) = y#x#zs" |
45274393f28c no changes
fahad
parents: 52
diff changeset
   294
"swap12 zs = zs"
45274393f28c no changes
fahad
parents: 52
diff changeset
   295