thys/Chap03.thy
author fahad
Mon, 26 Jan 2015 16:01:58 +0000
changeset 52 d67149236738
parent 51 3810b37511cb
child 54 45274393f28c
permissions -rw-r--r--
test
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52
fahad
parents: 51
diff changeset
     1
(* test *)
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     2
theory Chap03
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     3
imports Main
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     4
begin
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     5
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     6
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
     7
apply simp
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     8
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     9
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    10
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
    11
apply (simp (no_asm))
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    12
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    13
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    14
definition xor :: "bool \<Rightarrow> bool \<Rightarrow> bool" where
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    15
"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
    16
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    17
lemma "xor A (\<not>A)"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    18
apply(simp only: xor_def)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    19
apply(simp add: xor_def)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    20
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    21
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    22
lemma "(let xs = [] in xs@ys@xs) = ys"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    23
apply(simp only: Let_def)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    24
apply(simp add: Let_def)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    25
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    26
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    27
(* 3.1.8 Conditioal Simplification Rules  *)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    28
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    29
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
    30
using [[simp_trace=true]]
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    31
apply(case_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    32
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    33
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    34
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    35
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    36
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
    37
apply(case_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    38
using [[simp_trace=true]]
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    39
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    40
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    41
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    42
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    43
(* 3.1.9 Automatic Case Splits  *)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    44
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    45
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
    46
apply(split split_if)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    47
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    48
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    49
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    50
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
    51
apply(split list.split)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    52
apply(simp split: list.split)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    53
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    54
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    55
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
    56
apply(split split_if_asm)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    57
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    58
apply(auto)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    59
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    60
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    61
(* 3.2 Induction Heuristics  *)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    62
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    63
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
    64
"itrev [] ys = ys" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    65
"itrev (x#xs) ys = itrev xs (x#ys)"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    66
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    67
lemma "itrev xs [] = rev xs"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    68
apply(induct_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    69
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    70
apply(auto)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    71
oops
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    72
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    73
lemma "itrev xs ys = rev xs @ ys"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    74
apply(induct_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    75
apply(simp_all)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    76
oops
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    77
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    78
lemma "\<forall> ys. itrev xs ys = rev xs @ ys"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    79
apply(induct_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    80
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    81
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    82
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    83
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    84
primrec add1 :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    85
"add1 m 0 = m" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    86
"add1 m (Suc n) = add1 (Suc m) n"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    87
51
fahad
parents: 50
diff changeset
    88
primrec add2 :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
fahad
parents: 50
diff changeset
    89
"add2 m 0 = m" |
fahad
parents: 50
diff changeset
    90
"add2 m (Suc n) = Suc (add2 m n)"
fahad
parents: 50
diff changeset
    91
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    92
value "add1 1 3"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    93
value "1 + 3"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    94
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    95
lemma abc [simp]: "add1 m 0 = m"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    96
apply(induction m)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    97
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    98
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    99
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   100
51
fahad
parents: 50
diff changeset
   101
lemma abc2: "\<forall>m. add1 m n = m + n"
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   102
apply(induction n)
51
fahad
parents: 50
diff changeset
   103
apply(simp)
fahad
parents: 50
diff changeset
   104
apply(simp del: add1.simps)
fahad
parents: 50
diff changeset
   105
apply(simp (no_asm) only: add1.simps)
fahad
parents: 50
diff changeset
   106
apply(simp only: )
fahad
parents: 50
diff changeset
   107
apply(simp)
fahad
parents: 50
diff changeset
   108
done
fahad
parents: 50
diff changeset
   109
fahad
parents: 50
diff changeset
   110
thm abc2
fahad
parents: 50
diff changeset
   111
fahad
parents: 50
diff changeset
   112
lemma abc3: "add1 m n = m + n"
fahad
parents: 50
diff changeset
   113
apply(induction n arbitrary: m)
fahad
parents: 50
diff changeset
   114
apply(simp)
fahad
parents: 50
diff changeset
   115
apply(simp del: add1.simps)
fahad
parents: 50
diff changeset
   116
apply(simp (no_asm) only: add1.simps)
fahad
parents: 50
diff changeset
   117
apply(simp only: )
fahad
parents: 50
diff changeset
   118
done
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   119
51
fahad
parents: 50
diff changeset
   120
thm abc3
fahad
parents: 50
diff changeset
   121
fahad
parents: 50
diff changeset
   122
lemma abc4: "add1 m n = add2 m n"
fahad
parents: 50
diff changeset
   123
apply(induction n arbitrary: m)
fahad
parents: 50
diff changeset
   124
apply(simp)
fahad
parents: 50
diff changeset
   125
apply(simp add: abc3)
fahad
parents: 50
diff changeset
   126
done
fahad
parents: 50
diff changeset
   127
fahad
parents: 50
diff changeset
   128
(* added test *)
fahad
parents: 50
diff changeset
   129
fahad
parents: 50
diff changeset
   130
lemma abc5: "add2 m n = m + n"
fahad
parents: 50
diff changeset
   131
apply(induction n)
fahad
parents: 50
diff changeset
   132
apply(simp)
fahad
parents: 50
diff changeset
   133
apply(simp)
fahad
parents: 50
diff changeset
   134
done
fahad
parents: 50
diff changeset
   135
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   136
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   137
(* 3.3 Case Study: Compiling Expressions  *)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   138
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   139
type_synonym 'v binop = "'v \<Rightarrow> 'v \<Rightarrow> 'v"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   140
datatype ('a, 'v)expr = 
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   141
  Cex 'v
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   142
| Vex 'a
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   143
| Bex "'v binop" "('a,'v)expr" "('a,'v)expr"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   144
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   145
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
   146
"value (Cex v) env = v" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   147
"value (Vex a) env = env a" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   148
"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
   149
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   150
datatype ('a,'v)instr = 
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   151
  Const 'v
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   152
| Load 'a
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   153
| Apply "'v binop"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   154
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   155
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
   156
"exec [] s vs = vs" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   157
"exec (i#is) s vs = (case i of
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   158
    Const v \<Rightarrow> exec is s (v#vs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   159
  | Load a \<Rightarrow> exec is s ((s a)#vs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   160
  | 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
   161
  
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   162
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
   163
"compile (Cex v) = [Const v]" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   164
"compile (Vex a) = [Load a]" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   165
"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
   166
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   167
theorem "exec (compile e) s [] = [value e s]"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   168
(*the theorem needs to be generalized*)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   169
oops
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   170
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   171
(*more generalized theorem*)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   172
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
   173
apply(induct_tac e)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   174
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   175
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   176
oops
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   177
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   178
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
   179
apply(induct_tac xs)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   180
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   181
apply(simp)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   182
apply(simp split: instr.split)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   183
done
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   184
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   185
(* 2.5.6 Case Study: Boolean Expressions *)
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   186
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   187
datatype boolex = Const bool | Var nat | Neg boolex
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   188
| And boolex boolex
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   189
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   190
primrec "value2" :: "boolex \<Rightarrow> (nat \<Rightarrow> bool) \<Rightarrow> bool" where
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   191
"value2 (Const b)  env = b" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   192
"value2 (Var x)    env = env x" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   193
"value2 (Neg b)    env = (\<not> value2 b env)" |
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   194
"value2 (And b c)  env = (value2 b env \<and> value2 c env)"
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   195
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   196
value "Const true"
51
fahad
parents: 50
diff changeset
   197
value "Var (Suc(0))"
fahad
parents: 50
diff changeset
   198
fahad
parents: 50
diff changeset
   199
fahad
parents: 50
diff changeset
   200
fahad
parents: 50
diff changeset
   201
value "value2 (Const False) (\<lambda>x. False)"
fahad
parents: 50
diff changeset
   202
value "value2 (Var 11) (\<lambda>x. if (x = 10 | x = 11) then True else False)"
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   203
51
fahad
parents: 50
diff changeset
   204
definition
fahad
parents: 50
diff changeset
   205
  "en1 \<equiv>  (\<lambda>x. if x = 10 | x = 11 then True else False)"
fahad
parents: 50
diff changeset
   206
  
fahad
parents: 50
diff changeset
   207
abbreviation
fahad
parents: 50
diff changeset
   208
  "en2 \<equiv>  (\<lambda>x. if x = 10 | x = 11 then True else False)"
fahad
parents: 50
diff changeset
   209
  
fahad
parents: 50
diff changeset
   210
value "value2 (And (Var 10) (Var 11)) en2"
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   211
51
fahad
parents: 50
diff changeset
   212
lemma "value2 (And (Var 10) (Var 11)) en2 = True"
fahad
parents: 50
diff changeset
   213
apply(simp)
fahad
parents: 50
diff changeset
   214
done
50
c603b27083f3 fahad's experiments
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   215