| 
11235
 | 
     1  | 
theory FP1 = Main:
  | 
| 
 | 
     2  | 
  | 
| 
 | 
     3  | 
subsection{*More Constructs*}
 | 
| 
 | 
     4  | 
  | 
| 
 | 
     5  | 
lemma "if xs = ys
  | 
| 
 | 
     6  | 
       then rev xs = rev ys
  | 
| 
 | 
     7  | 
       else rev xs \<noteq> rev ys"
  | 
| 
 | 
     8  | 
by auto
  | 
| 
 | 
     9  | 
  | 
| 
 | 
    10  | 
lemma "case xs of
  | 
| 
 | 
    11  | 
         []   \<Rightarrow> tl xs = xs
  | 
| 
 | 
    12  | 
       | y#ys \<Rightarrow> tl xs \<noteq> xs"
  | 
| 
 | 
    13  | 
apply(case_tac xs)
  | 
| 
 | 
    14  | 
by auto
  | 
| 
 | 
    15  | 
  | 
| 
 | 
    16  | 
  | 
| 
 | 
    17  | 
subsection{*More Types*}
 | 
| 
 | 
    18  | 
  | 
| 
 | 
    19  | 
  | 
| 
 | 
    20  | 
subsubsection{*Natural Numbers*}
 | 
| 
 | 
    21  | 
  | 
| 
 | 
    22  | 
consts sum :: "nat \<Rightarrow> nat"
  | 
| 
 | 
    23  | 
primrec "sum 0 = 0"
  | 
| 
 | 
    24  | 
        "sum (Suc n) = Suc n + sum n"
  | 
| 
 | 
    25  | 
  | 
| 
 | 
    26  | 
lemma "sum n + sum n = n*(Suc n)";
  | 
| 
 | 
    27  | 
apply(induct_tac n);
  | 
| 
 | 
    28  | 
apply(auto);
  | 
| 
 | 
    29  | 
done
  | 
| 
 | 
    30  | 
  | 
| 
 | 
    31  | 
lemma "\<lbrakk> \<not> m < n; m < n+1 \<rbrakk> \<Longrightarrow> m = n"
  | 
| 
 | 
    32  | 
by(auto)
  | 
| 
 | 
    33  | 
  | 
| 
 | 
    34  | 
lemma "min i (max j k) = max (min k i) (min i (j::nat))";
  | 
| 
 | 
    35  | 
by(arith)
  | 
| 
 | 
    36  | 
  | 
| 
 | 
    37  | 
lemma "n*n = n \<Longrightarrow> n=0 \<or> n=1"
  | 
| 
 | 
    38  | 
oops
  | 
| 
 | 
    39  | 
  | 
| 
 | 
    40  | 
  | 
| 
 | 
    41  | 
subsubsection{*Pairs*}
 | 
| 
 | 
    42  | 
  | 
| 
 | 
    43  | 
lemma "fst(x,y) = snd(z,x)"
  | 
| 
 | 
    44  | 
by auto
  | 
| 
 | 
    45  | 
  | 
| 
 | 
    46  | 
  | 
| 
 | 
    47  | 
  | 
| 
 | 
    48  | 
subsection{*Definitions*}
 | 
| 
 | 
    49  | 
  | 
| 
 | 
    50  | 
consts xor :: "bool \<Rightarrow> bool \<Rightarrow> bool"
  | 
| 
 | 
    51  | 
defs xor_def: "xor x y \<equiv> x \<and> \<not>y \<or> \<not>x \<and> y"
  | 
| 
 | 
    52  | 
  | 
| 
 | 
    53  | 
constdefs nand :: "bool \<Rightarrow> bool \<Rightarrow> bool"
  | 
| 
 | 
    54  | 
         "nand x y \<equiv> \<not>(x \<and> y)"
  | 
| 
 | 
    55  | 
  | 
| 
 | 
    56  | 
lemma "\<not> xor x x"
  | 
| 
 | 
    57  | 
apply(unfold xor_def)
  | 
| 
 | 
    58  | 
by auto
  | 
| 
 | 
    59  | 
  | 
| 
 | 
    60  | 
  | 
| 
 | 
    61  | 
  | 
| 
 | 
    62  | 
subsection{*Simplification*}
 | 
| 
 | 
    63  | 
  | 
| 
 | 
    64  | 
  | 
| 
 | 
    65  | 
subsubsection{*Simplification Rules*}
 | 
| 
 | 
    66  | 
  | 
| 
 | 
    67  | 
lemma fst_conv[simp]: "fst(x,y) = x"
  | 
| 
 | 
    68  | 
by auto
  | 
| 
 | 
    69  | 
  | 
| 
 | 
    70  | 
declare fst_conv[simp]
  | 
| 
 | 
    71  | 
declare fst_conv[simp del]
  | 
| 
 | 
    72  | 
  | 
| 
 | 
    73  | 
  | 
| 
 | 
    74  | 
subsubsection{*The Simplification Method*}
 | 
| 
 | 
    75  | 
  | 
| 
 | 
    76  | 
lemma "x*(y+1) = y*(x+1)"
  | 
| 
 | 
    77  | 
apply simp
  | 
| 
 | 
    78  | 
oops
  | 
| 
 | 
    79  | 
  | 
| 
 | 
    80  | 
  | 
| 
 | 
    81  | 
subsubsection{*Adding and Deleting Simplification Rules*}
 | 
| 
 | 
    82  | 
  | 
| 
 | 
    83  | 
lemma "\<forall>x::nat. x*(y+z) = r"
  | 
| 
 | 
    84  | 
apply (simp add: add_mult_distrib2)
  | 
| 
 | 
    85  | 
oops
  | 
| 
 | 
    86  | 
  | 
| 
 | 
    87  | 
lemma "rev(rev(xs @ [])) = xs"
  | 
| 
 | 
    88  | 
apply (simp del: rev_rev_ident)
  | 
| 
 | 
    89  | 
oops
  | 
| 
 | 
    90  | 
  | 
| 
 | 
    91  | 
  | 
| 
 | 
    92  | 
subsubsection{*Assumptions*}
 | 
| 
 | 
    93  | 
  | 
| 
 | 
    94  | 
lemma "\<lbrakk> xs @ zs = ys @ xs; [] @ xs = [] @ [] \<rbrakk> \<Longrightarrow> ys = zs";
  | 
| 
 | 
    95  | 
apply simp;
  | 
| 
 | 
    96  | 
done
  | 
| 
 | 
    97  | 
  | 
| 
 | 
    98  | 
lemma "\<forall>x. f x = g (f (g x)) \<Longrightarrow> f [] = f [] @ []";
  | 
| 
 | 
    99  | 
apply(simp (no_asm));
  | 
| 
 | 
   100  | 
done
  | 
| 
 | 
   101  | 
  | 
| 
 | 
   102  | 
  | 
| 
 | 
   103  | 
subsubsection{*Rewriting with Definitions*}
 | 
| 
 | 
   104  | 
  | 
| 
 | 
   105  | 
lemma "xor A (\<not>A)";
  | 
| 
 | 
   106  | 
apply(simp only:xor_def);
  | 
| 
 | 
   107  | 
by simp
  | 
| 
 | 
   108  | 
  | 
| 
 | 
   109  | 
  | 
| 
 | 
   110  | 
subsubsection{*Conditional Equations*}
 | 
| 
 | 
   111  | 
  | 
| 
 | 
   112  | 
lemma hd_Cons_tl[simp]: "xs \<noteq> []  \<Longrightarrow>  hd xs # tl xs = xs"
  | 
| 
 | 
   113  | 
apply(case_tac xs, simp, simp)
  | 
| 
 | 
   114  | 
done
  | 
| 
 | 
   115  | 
  | 
| 
 | 
   116  | 
lemma "xs \<noteq> [] \<Longrightarrow> hd(rev xs) # tl(rev xs) = rev xs"
  | 
| 
 | 
   117  | 
by(simp)
  | 
| 
 | 
   118  | 
  | 
| 
 | 
   119  | 
  | 
| 
 | 
   120  | 
subsubsection{*Automatic Case Splits*}
 | 
| 
 | 
   121  | 
  | 
| 
 | 
   122  | 
lemma "\<forall>xs. if xs = [] then A else B";
  | 
| 
 | 
   123  | 
apply simp
  | 
| 
 | 
   124  | 
oops
  | 
| 
 | 
   125  | 
  | 
| 
11292
 | 
   126  | 
lemma "case xs @ [] of [] \<Rightarrow> P | y#ys \<Rightarrow> Q ys y";
  | 
| 
11235
 | 
   127  | 
apply simp
  | 
| 
 | 
   128  | 
apply(simp split: list.split)
  | 
| 
 | 
   129  | 
oops
  | 
| 
 | 
   130  | 
  | 
| 
 | 
   131  | 
  | 
| 
 | 
   132  | 
subsubsection{*Arithmetic*}
 | 
| 
 | 
   133  | 
  | 
| 
 | 
   134  | 
lemma "\<lbrakk> \<not> m < n; m < n+1 \<rbrakk> \<Longrightarrow> m = n"
  | 
| 
 | 
   135  | 
by simp
  | 
| 
 | 
   136  | 
  | 
| 
 | 
   137  | 
lemma "\<not> m < n \<and> m < n+1 \<Longrightarrow> m = n";
  | 
| 
 | 
   138  | 
apply simp
  | 
| 
 | 
   139  | 
by(arith)
  | 
| 
 | 
   140  | 
  | 
| 
 | 
   141  | 
  | 
| 
 | 
   142  | 
subsubsection{*Tracing*}
 | 
| 
 | 
   143  | 
  | 
| 
 | 
   144  | 
lemma "rev [a] = []"
  | 
| 
 | 
   145  | 
apply(simp)
  | 
| 
 | 
   146  | 
oops
  | 
| 
 | 
   147  | 
  | 
| 
 | 
   148  | 
  | 
| 
 | 
   149  | 
  | 
| 
 | 
   150  | 
subsection{*Case Study: Compiling Expressions*}
 | 
| 
 | 
   151  | 
  | 
| 
 | 
   152  | 
  | 
| 
 | 
   153  | 
subsubsection{*Expressions*}
 | 
| 
 | 
   154  | 
  | 
| 
 | 
   155  | 
types 'v binop = "'v \<Rightarrow> 'v \<Rightarrow> 'v";
  | 
| 
 | 
   156  | 
  | 
| 
 | 
   157  | 
datatype ('a,'v)expr = Cex 'v
 | 
| 
 | 
   158  | 
                     | Vex 'a
  | 
| 
 | 
   159  | 
                     | Bex "'v binop"  "('a,'v)expr"  "('a,'v)expr";
 | 
| 
 | 
   160  | 
  | 
| 
 | 
   161  | 
consts value :: "('a,'v)expr \<Rightarrow> ('a \<Rightarrow> 'v) \<Rightarrow> 'v";
 | 
| 
 | 
   162  | 
primrec
  | 
| 
 | 
   163  | 
"value (Cex v) env = v"
  | 
| 
 | 
   164  | 
"value (Vex a) env = env a"
  | 
| 
 | 
   165  | 
"value (Bex f e1 e2) env = f (value e1 env) (value e2 env)";
  | 
| 
 | 
   166  | 
  | 
| 
 | 
   167  | 
  | 
| 
 | 
   168  | 
subsubsection{*The Stack Machine*}
 | 
| 
 | 
   169  | 
  | 
| 
 | 
   170  | 
datatype ('a,'v) instr = Const 'v
 | 
| 
 | 
   171  | 
                       | Load 'a
  | 
| 
 | 
   172  | 
                       | Apply "'v binop";
  | 
| 
 | 
   173  | 
  | 
| 
 | 
   174  | 
consts exec :: "('a,'v)instr list \<Rightarrow> ('a\<Rightarrow>'v) \<Rightarrow> 'v list \<Rightarrow> 'v list";
 | 
| 
 | 
   175  | 
primrec
  | 
| 
 | 
   176  | 
"exec [] s vs = vs"
  | 
| 
 | 
   177  | 
"exec (i#is) s vs = (case i of
  | 
| 
 | 
   178  | 
    Const v  \<Rightarrow> exec is s (v#vs)
  | 
| 
 | 
   179  | 
  | Load a   \<Rightarrow> exec is s ((s a)#vs)
  | 
| 
 | 
   180  | 
  | Apply f  \<Rightarrow> exec is s ((f (hd vs) (hd(tl vs)))#(tl(tl vs))))";
  | 
| 
 | 
   181  | 
  | 
| 
 | 
   182  | 
  | 
| 
 | 
   183  | 
subsubsection{*The Compiler*}
 | 
| 
 | 
   184  | 
  | 
| 
 | 
   185  | 
consts comp :: "('a,'v)expr \<Rightarrow> ('a,'v)instr list";
 | 
| 
 | 
   186  | 
primrec
  | 
| 
 | 
   187  | 
"comp (Cex v)       = [Const v]"
  | 
| 
 | 
   188  | 
"comp (Vex a)       = [Load a]"
  | 
| 
 | 
   189  | 
"comp (Bex f e1 e2) = (comp e2) @ (comp e1) @ [Apply f]";
  | 
| 
 | 
   190  | 
  | 
| 
 | 
   191  | 
theorem "exec (comp e) s [] = [value e s]";
  | 
| 
 | 
   192  | 
oops
  | 
| 
 | 
   193  | 
  | 
| 
 | 
   194  | 
  | 
| 
 | 
   195  | 
  | 
| 
11236
 | 
   196  | 
subsection{*Advanced Datatypes*}
 | 
| 
11235
 | 
   197  | 
  | 
| 
 | 
   198  | 
  | 
| 
 | 
   199  | 
subsubsection{*Mutual Recursion*}
 | 
| 
 | 
   200  | 
  | 
| 
 | 
   201  | 
datatype 'a aexp = IF   "'a bexp" "'a aexp" "'a aexp"
  | 
| 
 | 
   202  | 
                 | Sum  "'a aexp" "'a aexp"
  | 
| 
 | 
   203  | 
                 | Var 'a
  | 
| 
 | 
   204  | 
                 | Num nat
  | 
| 
 | 
   205  | 
and      'a bexp = Less "'a aexp" "'a aexp"
  | 
| 
 | 
   206  | 
                 | And  "'a bexp" "'a bexp"
  | 
| 
 | 
   207  | 
                 | Neg  "'a bexp";
  | 
| 
 | 
   208  | 
  | 
| 
 | 
   209  | 
  | 
| 
 | 
   210  | 
consts  evala :: "'a aexp \<Rightarrow> ('a \<Rightarrow> nat) \<Rightarrow> nat"
 | 
| 
 | 
   211  | 
        evalb :: "'a bexp \<Rightarrow> ('a \<Rightarrow> nat) \<Rightarrow> bool";
 | 
| 
 | 
   212  | 
  | 
| 
 | 
   213  | 
primrec
  | 
| 
 | 
   214  | 
  "evala (IF b a1 a2) env =
  | 
| 
 | 
   215  | 
     (if evalb b env then evala a1 env else evala a2 env)"
  | 
| 
 | 
   216  | 
  "evala (Sum a1 a2) env = evala a1 env + evala a2 env"
  | 
| 
 | 
   217  | 
  "evala (Var v) env = env v"
  | 
| 
 | 
   218  | 
  "evala (Num n) env = n"
  | 
| 
 | 
   219  | 
  | 
| 
 | 
   220  | 
  "evalb (Less a1 a2) env = (evala a1 env < evala a2 env)"
  | 
| 
 | 
   221  | 
  "evalb (And b1 b2) env = (evalb b1 env \<and> evalb b2 env)"
  | 
| 
 | 
   222  | 
  "evalb (Neg b) env = (\<not> evalb b env)"
  | 
| 
 | 
   223  | 
  | 
| 
 | 
   224  | 
consts substa :: "('a \<Rightarrow> 'b aexp) \<Rightarrow> 'a aexp \<Rightarrow> 'b aexp"
 | 
| 
 | 
   225  | 
       substb :: "('a \<Rightarrow> 'b aexp) \<Rightarrow> 'a bexp \<Rightarrow> 'b bexp"
 | 
| 
 | 
   226  | 
  | 
| 
 | 
   227  | 
primrec
  | 
| 
 | 
   228  | 
  "substa s (IF b a1 a2) =
  | 
| 
 | 
   229  | 
     IF (substb s b) (substa s a1) (substa s a2)"
  | 
| 
 | 
   230  | 
  "substa s (Sum a1 a2) = Sum (substa s a1) (substa s a2)"
  | 
| 
 | 
   231  | 
  "substa s (Var v) = s v"
  | 
| 
 | 
   232  | 
  "substa s (Num n) = Num n"
  | 
| 
 | 
   233  | 
  | 
| 
 | 
   234  | 
  "substb s (Less a1 a2) = Less (substa s a1) (substa s a2)"
  | 
| 
 | 
   235  | 
  "substb s (And b1 b2) = And (substb s b1) (substb s b2)"
  | 
| 
 | 
   236  | 
  "substb s (Neg b) = Neg (substb s b)"
  | 
| 
 | 
   237  | 
  | 
| 
 | 
   238  | 
lemma substitution_lemma:
  | 
| 
 | 
   239  | 
 "evala (substa s a) env = evala a (\<lambda>x. evala (s x) env) \<and>
  | 
| 
 | 
   240  | 
  evalb (substb s b) env = evalb b (\<lambda>x. evala (s x) env)";
  | 
| 
 | 
   241  | 
apply(induct_tac a and b);
  | 
| 
 | 
   242  | 
by simp_all
  | 
| 
 | 
   243  | 
  | 
| 
 | 
   244  | 
  | 
| 
 | 
   245  | 
subsubsection{*Nested Recursion*}
 | 
| 
 | 
   246  | 
  | 
| 
 | 
   247  | 
datatype tree = C "tree list"
  | 
| 
 | 
   248  | 
  | 
| 
 | 
   249  | 
term "C[]"
  | 
| 
 | 
   250  | 
term "C[C[C[]],C[]]"
  | 
| 
 | 
   251  | 
  | 
| 
 | 
   252  | 
consts
  | 
| 
 | 
   253  | 
mirror :: "tree \<Rightarrow> tree"
  | 
| 
 | 
   254  | 
mirrors:: "tree list \<Rightarrow> tree list";
  | 
| 
 | 
   255  | 
  | 
| 
 | 
   256  | 
primrec
  | 
| 
 | 
   257  | 
  "mirror(C ts) = C(mirrors ts)"
  | 
| 
 | 
   258  | 
  | 
| 
 | 
   259  | 
  "mirrors [] = []"
  | 
| 
 | 
   260  | 
  "mirrors (t # ts) = mirrors ts @ [mirror t]"
  | 
| 
 | 
   261  | 
  | 
| 
 | 
   262  | 
lemma "mirror(mirror t) = t \<and> mirrors(mirrors ts) = ts"
  | 
| 
 | 
   263  | 
apply(induct_tac t and ts)
  | 
| 
 | 
   264  | 
apply simp_all
  | 
| 
 | 
   265  | 
oops
  | 
| 
 | 
   266  | 
  | 
| 
11236
 | 
   267  | 
text{*
 | 
| 
 | 
   268  | 
\begin{exercise}
 | 
| 
 | 
   269  | 
Complete the above proof.
  | 
| 
 | 
   270  | 
\end{exercise}
 | 
| 
 | 
   271  | 
*}
  | 
| 
11235
 | 
   272  | 
  | 
| 
 | 
   273  | 
  | 
| 
 | 
   274  | 
subsubsection{*Datatypes Involving Functions*}
 | 
| 
 | 
   275  | 
  | 
| 
 | 
   276  | 
datatype ('a,'i)bigtree = Tip | Br 'a "'i \<Rightarrow> ('a,'i)bigtree"
 | 
| 
 | 
   277  | 
  | 
| 
 | 
   278  | 
term "Br 0 (\<lambda>i. Br i (\<lambda>n. Tip))"
  | 
| 
 | 
   279  | 
  | 
| 
 | 
   280  | 
consts map_bt :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a,'i)bigtree \<Rightarrow> ('b,'i)bigtree"
 | 
| 
 | 
   281  | 
primrec
  | 
| 
 | 
   282  | 
"map_bt f Tip      = Tip"
  | 
| 
 | 
   283  | 
"map_bt f (Br a F) = Br (f a) (\<lambda>i. map_bt f (F i))"
  | 
| 
 | 
   284  | 
  | 
| 
 | 
   285  | 
lemma "map_bt (g o f) T = map_bt g (map_bt f T)"
  | 
| 
 | 
   286  | 
apply(induct_tac T, rename_tac[2] F)
  | 
| 
 | 
   287  | 
apply simp_all
  | 
| 
 | 
   288  | 
done
  | 
| 
 | 
   289  | 
  | 
| 
 | 
   290  | 
(* This is NOT allowed:
  | 
| 
 | 
   291  | 
datatype lambda = C "lambda \<Rightarrow> lambda"
  | 
| 
 | 
   292  | 
*)
  | 
| 
 | 
   293  | 
  | 
| 
11236
 | 
   294  | 
text{*
 | 
| 
 | 
   295  | 
\begin{exercise}
 | 
| 
11237
 | 
   296  | 
Define a datatype of ordinals and the ordinal $\Gamma_0$.
  | 
| 
11236
 | 
   297  | 
\end{exercise}
 | 
| 
 | 
   298  | 
*}
  | 
| 
 | 
   299  | 
  | 
| 
11235
 | 
   300  | 
end
  |