doc-src/TutorialI/Overview/FP1.thy
changeset 13238 a6cb18a25cbb
parent 12631 7648ac4a6b95
child 13250 efd5db7dc7cc
equal deleted inserted replaced
13237:493d61afa731 13238:a6cb18a25cbb
       
     1 (*<*)
     1 theory FP1 = Main:
     2 theory FP1 = Main:
     2 
     3 (*>*)
     3 subsection{*More Constructs*}
       
     4 
     4 
     5 lemma "if xs = ys
     5 lemma "if xs = ys
     6        then rev xs = rev ys
     6        then rev xs = rev ys
     7        else rev xs \<noteq> rev ys"
     7        else rev xs \<noteq> rev ys"
     8 by auto
     8 by auto
    26 lemma "sum n + sum n = n*(Suc n)"
    26 lemma "sum n + sum n = n*(Suc n)"
    27 apply(induct_tac n)
    27 apply(induct_tac n)
    28 apply(auto)
    28 apply(auto)
    29 done
    29 done
    30 
    30 
    31 lemma "\<lbrakk> \<not> m < n; m < n+1 \<rbrakk> \<Longrightarrow> m = n"
    31 text{*
       
    32 Some examples of linear arithmetic:
       
    33 *}
       
    34 
       
    35 lemma "\<lbrakk> \<not> m < n; m < n+(1::int) \<rbrakk> \<Longrightarrow> m = n"
    32 by(auto)
    36 by(auto)
    33 
    37 
    34 lemma "min i (max j k) = max (min k i) (min i (j::nat))"
    38 lemma "min i (max j k) = max (min k i) (min i (j::nat))"
    35 by(arith)
    39 by(arith)
    36 
    40 
    37 lemma "n*n = n \<Longrightarrow> n=0 \<or> n=1"
    41 text{*
    38 oops
    42 Not proved automatically because it involves multiplication:
       
    43 *}
       
    44 
       
    45 lemma "n*n = n \<Longrightarrow> n=0 \<or> n=(1::int)"
       
    46 (*<*)oops(*>*)
    39 
    47 
    40 
    48 
    41 subsubsection{*Pairs*}
    49 subsubsection{*Pairs*}
    42 
    50 
    43 lemma "fst(x,y) = snd(z,x)"
    51 lemma "fst(x,y) = snd(z,x)"
    65 subsubsection{*Simplification Rules*}
    73 subsubsection{*Simplification Rules*}
    66 
    74 
    67 lemma fst_conv[simp]: "fst(x,y) = x"
    75 lemma fst_conv[simp]: "fst(x,y) = x"
    68 by auto
    76 by auto
    69 
    77 
       
    78 text{*
       
    79 Setting and resetting the @{text simp} attribute:
       
    80 *}
       
    81 
    70 declare fst_conv[simp]
    82 declare fst_conv[simp]
    71 declare fst_conv[simp del]
    83 declare fst_conv[simp del]
    72 
    84 
    73 
    85 
    74 subsubsection{*The Simplification Method*}
    86 subsubsection{*The Simplification Method*}
    75 
    87 
    76 lemma "x*(y+1) = y*(x+1)"
    88 lemma "x*(y+1) = y*(x+1::nat)"
    77 apply simp
    89 apply simp
    78 oops
    90 (*<*)oops(*>*)
    79 
    91 
    80 
    92 
    81 subsubsection{*Adding and Deleting Simplification Rules*}
    93 subsubsection{*Adding and Deleting Simplification Rules*}
    82 
    94 
    83 lemma "\<forall>x::nat. x*(y+z) = r"
    95 lemma "\<forall>x::nat. x*(y+z) = r"
    84 apply (simp add: add_mult_distrib2)
    96 apply (simp add: add_mult_distrib2)
    85 oops
    97 (*<*)oops(*>*)text_raw {* \isanewline\isanewline *}
    86 
    98 
    87 lemma "rev(rev(xs @ [])) = xs"
    99 lemma "rev(rev(xs @ [])) = xs"
    88 apply (simp del: rev_rev_ident)
   100 apply (simp del: rev_rev_ident)
    89 oops
   101 (*<*)oops(*>*)
    90 
       
    91 
   102 
    92 subsubsection{*Assumptions*}
   103 subsubsection{*Assumptions*}
    93 
   104 
    94 lemma "\<lbrakk> xs @ zs = ys @ xs; [] @ xs = [] @ [] \<rbrakk> \<Longrightarrow> ys = zs"
   105 lemma "\<lbrakk> xs @ zs = ys @ xs; [] @ xs = [] @ [] \<rbrakk> \<Longrightarrow> ys = zs"
    95 apply simp
   106 by simp
    96 done
       
    97 
   107 
    98 lemma "\<forall>x. f x = g (f (g x)) \<Longrightarrow> f [] = f [] @ []"
   108 lemma "\<forall>x. f x = g (f (g x)) \<Longrightarrow> f [] = f [] @ []"
    99 apply(simp (no_asm))
   109 by(simp (no_asm))
   100 done
       
   101 
       
   102 
   110 
   103 subsubsection{*Rewriting with Definitions*}
   111 subsubsection{*Rewriting with Definitions*}
   104 
   112 
   105 lemma "xor A (\<not>A)"
   113 lemma "xor A (\<not>A)"
   106 apply(simp only: xor_def)
   114 apply(simp only: xor_def)
       
   115 apply simp
       
   116 done
       
   117 
       
   118 
       
   119 subsubsection{*Conditional Equations*}
       
   120 
       
   121 lemma hd_Cons_tl[simp]: "xs \<noteq> []  \<Longrightarrow>  hd xs # tl xs = xs"
       
   122 by(case_tac xs, simp_all)
       
   123 
       
   124 lemma "xs \<noteq> [] \<Longrightarrow> hd(rev xs) # tl(rev xs) = rev xs"
   107 by simp
   125 by simp
   108 
   126 
   109 
   127 
   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*}
   128 subsubsection{*Automatic Case Splits*}
   121 
   129 
   122 lemma "\<forall>xs. if xs = [] then A else B"
   130 lemma "\<forall>xs. if xs = [] then A else B"
   123 apply simp
   131 apply simp
   124 oops
   132 (*<*)oops(*>*)text_raw {* \isanewline\isanewline *}
   125 
   133 
   126 lemma "case xs @ [] of [] \<Rightarrow> P | y#ys \<Rightarrow> Q ys y"
   134 lemma "case xs @ [] of [] \<Rightarrow> P | y#ys \<Rightarrow> Q ys y"
   127 apply simp
   135 apply simp
   128 apply(simp split: list.split)
   136 apply(simp split: list.split)
   129 oops
   137 (*<*)oops(*>*)
   130 
   138 
   131 
   139 
   132 subsubsection{*Arithmetic*}
   140 subsubsection{*Arithmetic*}
   133 
   141 
   134 lemma "\<lbrakk> \<not> m < n; m < n+1 \<rbrakk> \<Longrightarrow> m = n"
   142 text{*
       
   143 Is simple enough for the default arithmetic:
       
   144 *}
       
   145 lemma "\<lbrakk> \<not> m < n; m < n+(1::nat) \<rbrakk> \<Longrightarrow> m = n"
   135 by simp
   146 by simp
   136 
   147 
   137 lemma "\<not> m < n \<and> m < n+1 \<Longrightarrow> m = n"
   148 text{*
       
   149 Contains boolean combinations and needs full arithmetic:
       
   150 *}
       
   151 lemma "\<not> m < n \<and> m < n+(1::nat) \<Longrightarrow> m = n"
   138 apply simp
   152 apply simp
   139 by(arith)
   153 by(arith)
   140 
   154 
   141 
   155 (*<*)
   142 subsubsection{*Tracing*}
   156 subsubsection{*Tracing*}
   143 
   157 
   144 lemma "rev [a] = []"
   158 lemma "rev [a] = []"
   145 apply(simp)
   159 apply(simp)
   146 oops
   160 oops
       
   161 (*>*)
   147 
   162 
   148 
   163 
   149 
   164 
   150 subsection{*Case Study: Compiling Expressions*}
   165 subsection{*Case Study: Compiling Expressions*}
   151 
   166 
   187 "comp (Cex v)       = [Const v]"
   202 "comp (Cex v)       = [Const v]"
   188 "comp (Vex a)       = [Load a]"
   203 "comp (Vex a)       = [Load a]"
   189 "comp (Bex f e1 e2) = (comp e2) @ (comp e1) @ [Apply f]"
   204 "comp (Bex f e1 e2) = (comp e2) @ (comp e1) @ [Apply f]"
   190 
   205 
   191 theorem "exec (comp e) s [] = [value e s]"
   206 theorem "exec (comp e) s [] = [value e s]"
   192 oops
   207 (*<*)oops(*>*)
   193 
   208 
   194 
   209 
   195 
   210 
   196 subsection{*Advanced Datatypes*}
   211 subsection{*Advanced Datatypes*}
   197 
   212 
   244 
   259 
   245 subsubsection{*Nested Recursion*}
   260 subsubsection{*Nested Recursion*}
   246 
   261 
   247 datatype tree = C "tree list"
   262 datatype tree = C "tree list"
   248 
   263 
   249 term "C[]"
   264 text{*Some trees:*}
   250 term "C[C[C[]],C[]]"
   265 
       
   266 term "C []"
       
   267 term "C [C [C []], C []]"
   251 
   268 
   252 consts
   269 consts
   253 mirror :: "tree \<Rightarrow> tree"
   270 mirror :: "tree \<Rightarrow> tree"
   254 mirrors:: "tree list \<Rightarrow> tree list"
   271 mirrors:: "tree list \<Rightarrow> tree list"
   255 
   272 
   260   "mirrors (t # ts) = mirrors ts @ [mirror t]"
   277   "mirrors (t # ts) = mirrors ts @ [mirror t]"
   261 
   278 
   262 lemma "mirror(mirror t) = t \<and> mirrors(mirrors ts) = ts"
   279 lemma "mirror(mirror t) = t \<and> mirrors(mirrors ts) = ts"
   263 apply(induct_tac t and ts)
   280 apply(induct_tac t and ts)
   264 apply simp_all
   281 apply simp_all
   265 oops
   282 (*<*)oops(*>*)
   266 
   283 
   267 text{*
   284 text{*
   268 \begin{exercise}
   285 \begin{exercise}
   269 Complete the above proof.
   286 Complete the above proof.
   270 \end{exercise}
   287 \end{exercise}
   273 
   290 
   274 subsubsection{*Datatypes Involving Functions*}
   291 subsubsection{*Datatypes Involving Functions*}
   275 
   292 
   276 datatype ('a,'i)bigtree = Tip | Br 'a "'i \<Rightarrow> ('a,'i)bigtree"
   293 datatype ('a,'i)bigtree = Tip | Br 'a "'i \<Rightarrow> ('a,'i)bigtree"
   277 
   294 
       
   295 text{*A big tree:*}
   278 term "Br 0 (\<lambda>i. Br i (\<lambda>n. Tip))"
   296 term "Br 0 (\<lambda>i. Br i (\<lambda>n. Tip))"
   279 
   297 
   280 consts map_bt :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a,'i)bigtree \<Rightarrow> ('b,'i)bigtree"
   298 consts map_bt :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a,'i)bigtree \<Rightarrow> ('b,'i)bigtree"
   281 primrec
   299 primrec
   282 "map_bt f Tip      = Tip"
   300 "map_bt f Tip      = Tip"
   285 lemma "map_bt (g o f) T = map_bt g (map_bt f T)"
   303 lemma "map_bt (g o f) T = map_bt g (map_bt f T)"
   286 apply(induct_tac T, rename_tac[2] F)
   304 apply(induct_tac T, rename_tac[2] F)
   287 apply simp_all
   305 apply simp_all
   288 done
   306 done
   289 
   307 
   290 (* This is NOT allowed:
   308 text{* This is \emph{not} allowed:
   291 datatype lambda = C "lambda \<Rightarrow> lambda"
   309 \begin{verbatim}
   292 *)
   310 datatype lambda = C "lambda => lambda"
       
   311 \end{verbatim}
       
   312 *}
   293 
   313 
   294 text{*
   314 text{*
   295 \begin{exercise}
   315 \begin{exercise}
   296 Define a datatype of ordinals and the ordinal $\Gamma_0$.
   316 Define a datatype of ordinals and the ordinal $\Gamma_0$.
   297 \end{exercise}
   317 \end{exercise}
   298 *}
   318 *}
   299 
   319 (*<*)
   300 end
   320 end
       
   321 (*>*)