ProgTutorial/Solutions.thy
author Christian Urban <urbanc@in.tum.de>
Thu, 19 Nov 2009 20:00:10 +0100
changeset 396 a2e49e0771b3
parent 391 ae2f0b40c840
child 402 a64f91de2eab
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
theory Solutions
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
     2
imports FirstSteps "Recipes/Timing"
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
begin
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
     5
(*<*)
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
     6
setup{*
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
     7
open_file_with_prelude 
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
     8
  "Solutions_Code.thy"
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
     9
  ["theory Solutions", "imports FirstSteps", "begin"]
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
    10
*}
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
    11
(*>*)
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
    12
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
    13
chapter {* Solutions to Most Exercises\label{ch:solutions} *}
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
text {* \solution{fun:revsum} *}
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
293
0a567f923b42 slightly changed exercises about rev_sum
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
    17
ML{*fun rev_sum ((p as Const (@{const_name plus}, _)) $ t $ u) = 
0a567f923b42 slightly changed exercises about rev_sum
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
    18
      p $ u $ rev_sum t
271
949957531f63 modified solution(s) for "revsum" example
griff
parents: 270
diff changeset
    19
  | rev_sum t = t *}
949957531f63 modified solution(s) for "revsum" example
griff
parents: 270
diff changeset
    20
293
0a567f923b42 slightly changed exercises about rev_sum
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
    21
text {* 
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    22
  An alternative solution using the function @{ML_ind mk_binop in HOLogic} is:
293
0a567f923b42 slightly changed exercises about rev_sum
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
    23
 *}
271
949957531f63 modified solution(s) for "revsum" example
griff
parents: 270
diff changeset
    24
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    25
ML{*fun rev_sum t =
47
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 35
diff changeset
    26
let
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    27
  fun dest_sum (Const (@{const_name plus}, _) $ u $ u') = u' :: dest_sum u
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    28
    | dest_sum u = [u]
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    29
in
47
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 35
diff changeset
    30
   foldl1 (HOLogic.mk_binop @{const_name plus}) (dest_sum t)
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    31
end *}
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
text {* \solution{fun:makesum} *}
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    35
ML{*fun make_sum t1 t2 =
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    36
      HOLogic.mk_nat (HOLogic.dest_nat t1 + HOLogic.dest_nat t2) *}
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
313
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    38
text {* \solution{ex:debruijn} *}
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    39
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    40
ML{*fun P n = @{term "P::nat \<Rightarrow> bool"} $ (HOLogic.mk_number @{typ "nat"} n) 
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    41
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    42
fun rhs 1 = P 1
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    43
  | rhs n = HOLogic.mk_conj (P n, rhs (n - 1))
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    44
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    45
fun lhs 1 n = HOLogic.mk_imp (HOLogic.mk_eq (P 1, P n), rhs n)
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    46
  | lhs m n = HOLogic.mk_conj (HOLogic.mk_imp 
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    47
                 (HOLogic.mk_eq (P (m - 1), P m), rhs n), lhs (m - 1) n)
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    48
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    49
fun de_bruijn n =
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    50
  HOLogic.mk_Trueprop (HOLogic.mk_imp (lhs n n, rhs n))*}
1ca2f41770cc polished the exercises about constructing terms
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    51
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    52
text {* \solution{ex:scancmts} *}
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    53
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    54
ML{*val any = Scan.one (Symbol.not_eof)
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    55
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    56
val scan_cmt =
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    57
let
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    58
  val begin_cmt = Scan.this_string "(*" 
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    59
  val end_cmt = Scan.this_string "*)"
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    60
in
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    61
  begin_cmt |-- Scan.repeat (Scan.unless end_cmt any) --| end_cmt 
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    62
  >> (enclose "(**" "**)" o implode)
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    63
end
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    64
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    65
val parser = Scan.repeat (scan_cmt || any)
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    66
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    67
val scan_all =
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    68
      Scan.finite Symbol.stopper parser >> implode #> fst *}
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    69
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    70
text {*
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    71
  By using @{text "#> fst"} in the last line, the function 
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    72
  @{ML scan_all} retruns a string, instead of the pair a parser would
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    73
  normally return. For example:
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    74
80
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    75
  @{ML_response [display,gray]
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    76
"let
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    77
  val input1 = (explode \"foo bar\")
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    78
  val input2 = (explode \"foo (*test*) bar (*test*)\")
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    79
in
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    80
  (scan_all input1, scan_all input2)
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    81
end"
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
    82
"(\"foo bar\", \"foo (**test**) bar (**test**)\")"}
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    83
*}
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    84
391
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    85
text {* \solution{ex:contextfree} *}
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    86
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    87
ML{*datatype expr = 
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    88
   Number of int
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    89
 | Mult of expr * expr 
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    90
 | Add of expr * expr
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
    91
391
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    92
fun parse_basic xs =
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    93
  (OuterParse.nat >> Number 
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    94
   || OuterParse.$$$ "(" |-- parse_expr --| OuterParse.$$$ ")") xs
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    95
and parse_factor xs =
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    96
  (parse_basic --| OuterParse.$$$ "*" -- parse_factor >> Mult 
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    97
   || parse_basic) xs
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    98
and parse_expr xs =
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
    99
  (parse_factor --| OuterParse.$$$ "+" -- parse_expr >> Add 
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
   100
   || parse_factor) xs*}
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
   101
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
   102
ae2f0b40c840 added exercise
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
   103
text {* \solution{ex:dyckhoff} 
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   104
  The axiom rule can be implemented with the function @{ML atac}. The other
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   105
  rules correspond to the theorems:
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   106
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   107
  \begin{center}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   108
  \begin{tabular}{cc}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   109
  \begin{tabular}{rl}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   110
  $\wedge_R$ & @{thm [source] conjI}\\
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   111
  $\vee_{R_1}$ & @{thm [source] disjI1}\\
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   112
  $\vee_{R_2}$ & @{thm [source] disjI2}\\
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   113
  $\longrightarrow_R$ & @{thm [source] impI}\\
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   114
  $=_R$ & @{thm [source] iffI}\\
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   115
  \end{tabular}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   116
  &
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   117
  \begin{tabular}{rl}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   118
  $False$ & @{thm [source] FalseE}\\
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   119
  $\wedge_L$ & @{thm [source] conjE}\\
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   120
  $\vee_L$ & @{thm [source] disjE}\\
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   121
  $=_L$ & @{thm [source] iffE}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   122
  \end{tabular}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   123
  \end{tabular}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   124
  \end{center}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   125
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   126
  For the other rules we need to prove the following lemmas.
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   127
*}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   128
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   129
lemma impE1:
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   130
  shows "\<lbrakk>A \<longrightarrow> B; A; B \<Longrightarrow> R\<rbrakk> \<Longrightarrow> R"
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   131
  by iprover
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   132
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   133
lemma impE2:
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   134
  shows "\<lbrakk>(C \<and> D) \<longrightarrow> B; C \<longrightarrow> (D \<longrightarrow>B) \<Longrightarrow> R\<rbrakk> \<Longrightarrow> R"
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   135
  by iprover
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   136
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   137
lemma impE3:
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   138
  shows "\<lbrakk>(C \<or> D) \<longrightarrow> B; \<lbrakk>C \<longrightarrow> B; D \<longrightarrow> B\<rbrakk> \<Longrightarrow> R\<rbrakk> \<Longrightarrow> R"
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   139
  by iprover
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   140
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   141
lemma impE4:
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   142
  shows "\<lbrakk>(C \<longrightarrow> D) \<longrightarrow> B; D \<longrightarrow> B \<Longrightarrow> C \<longrightarrow> D; B \<Longrightarrow> R\<rbrakk> \<Longrightarrow> R"
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   143
  by iprover
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   144
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   145
lemma impE5:
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   146
  shows "\<lbrakk>(C = D) \<longrightarrow> B; (C \<longrightarrow> D) \<longrightarrow> ((D \<longrightarrow> C) \<longrightarrow> B) \<Longrightarrow> R\<rbrakk> \<Longrightarrow> R"
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   147
  by iprover
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   148
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   149
text {*
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   150
  Now the tactic which applies a single rule can be implemented
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   151
  as follows.
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   152
*}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   153
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   154
ML %linenosgray{*val apply_tac =
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   155
let
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   156
  val intros = [@{thm conjI}, @{thm disjI1}, @{thm disjI2}, 
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   157
                @{thm impI}, @{thm iffI}]
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   158
  val elims  = [@{thm FalseE}, @{thm conjE}, @{thm disjE}, @{thm iffE},
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   159
                @{thm impE2}, @{thm impE3}, @{thm impE4}, @{thm impE5}]
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   160
in
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   161
  atac
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   162
  ORELSE' resolve_tac intros
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   163
  ORELSE' eresolve_tac elims
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   164
  ORELSE' (etac @{thm impE1} THEN' atac)
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   165
end*}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   166
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   167
text {*
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   168
  In Line 11 we apply the rule @{thm [source] impE1} in concjunction 
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   169
  with @{ML atac} in order to reduce the number of possibilities that
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   170
  need to be explored. You can use the tactic as follows.
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   171
*}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   172
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   173
lemma
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   174
  shows "((((P \<longrightarrow> Q) \<longrightarrow> P) \<longrightarrow> P) \<longrightarrow> Q) \<longrightarrow> Q"
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   175
apply(tactic {* (DEPTH_SOLVE o apply_tac) 1 *})
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   176
done
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   177
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   178
text {*
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   179
  We can use the tactic to prove or disprove automatically the
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   180
  de Bruijn formulae from Exercise \ref{ex:debruijn}.
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   181
*}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   182
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   183
ML{*fun de_bruijn_prove ctxt n =
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   184
let 
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   185
  val goal = HOLogic.mk_Trueprop (HOLogic.mk_imp (lhs n n, rhs n))
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   186
in
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   187
  Goal.prove ctxt ["P"] [] goal
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   188
   (fn _ => (DEPTH_SOLVE o apply_tac) 1)
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   189
end*}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   190
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   191
text {* 
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   192
  You can use this function to prove de Bruijn formulae.
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   193
*}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   194
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   195
ML{*de_bruijn_prove @{context} 3 *}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   196
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   197
text {* \solution{ex:addsimproc} *}
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   198
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   199
ML{*fun dest_sum term =
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   200
  case term of 
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   201
    (@{term "(op +):: nat \<Rightarrow> nat \<Rightarrow> nat"} $ t1 $ t2) =>
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   202
        (snd (HOLogic.dest_number t1), snd (HOLogic.dest_number t2))
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   203
  | _ => raise TERM ("dest_sum", [term])
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   204
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   205
fun get_sum_thm ctxt t (n1, n2) =  
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   206
let 
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   207
  val sum = HOLogic.mk_number @{typ "nat"} (n1 + n2)
132
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 130
diff changeset
   208
  val goal = Logic.mk_equals (t, sum)
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   209
in
214
7e04dc2368b0 updated to latest Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   210
  Goal.prove ctxt [] [] goal (K (Arith_Data.arith_tac ctxt 1))
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   211
end
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   212
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   213
fun add_sp_aux ss t =
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   214
let 
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   215
  val ctxt = Simplifier.the_context ss
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   216
  val t' = term_of t
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   217
in
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   218
  SOME (get_sum_thm ctxt t' (dest_sum t'))
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   219
  handle TERM _ => NONE
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   220
end*}
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   221
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   222
text {* The setup for the simproc is *}
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   223
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 175
diff changeset
   224
simproc_setup %gray add_sp ("t1 + t2") = {* K add_sp_aux *}
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   225
 
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   226
text {* and a test case is the lemma *}
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   227
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   228
lemma "P (Suc (99 + 1)) ((0 + 0)::nat) (Suc (3 + 3 + 3)) (4 + 1)"
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   229
  apply(tactic {* simp_tac (HOL_basic_ss addsimprocs [@{simproc add_sp}]) 1 *})
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   230
txt {* 
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   231
  where the simproc produces the goal state
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   232
  
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   233
  \begin{minipage}{\textwidth}
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   234
  @{subgoals [display]}
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   235
  \end{minipage}\bigskip
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   236
*}(*<*)oops(*>*)
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   237
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   238
text {* \solution{ex:addconversion} *}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   239
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   240
text {*
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   241
  The following code assumes the function @{ML dest_sum} from the previous
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   242
  exercise.
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   243
*}
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   244
166
00d153e32a53 improvments to the solutions suggested by Sacha B?hme
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   245
ML{*fun add_simple_conv ctxt ctrm =
00d153e32a53 improvments to the solutions suggested by Sacha B?hme
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   246
let
00d153e32a53 improvments to the solutions suggested by Sacha B?hme
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   247
  val trm =  Thm.term_of ctrm
00d153e32a53 improvments to the solutions suggested by Sacha B?hme
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   248
in 
00d153e32a53 improvments to the solutions suggested by Sacha B?hme
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   249
  get_sum_thm ctxt trm (dest_sum trm)
00d153e32a53 improvments to the solutions suggested by Sacha B?hme
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   250
end
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   251
166
00d153e32a53 improvments to the solutions suggested by Sacha B?hme
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   252
fun add_conv ctxt ctrm =
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   253
  (case Thm.term_of ctrm of
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   254
     @{term "(op +)::nat \<Rightarrow> nat \<Rightarrow> nat"} $ _ $ _ => 
166
00d153e32a53 improvments to the solutions suggested by Sacha B?hme
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   255
         (Conv.binop_conv (add_conv ctxt)
00d153e32a53 improvments to the solutions suggested by Sacha B?hme
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   256
          then_conv (Conv.try_conv (add_simple_conv ctxt))) ctrm         
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   257
    | _ $ _ => Conv.combination_conv 
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   258
                 (add_conv ctxt) (add_conv ctxt) ctrm
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   259
    | Abs _ => Conv.abs_conv (fn (_, ctxt) => add_conv ctxt) ctxt ctrm
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   260
    | _ => Conv.all_conv ctrm)
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   261
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   262
fun add_tac ctxt = CSUBGOAL (fn (goal, i) =>
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   263
  CONVERSION
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   264
    (Conv.params_conv ~1 (fn ctxt =>
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   265
       (Conv.prems_conv ~1 (add_conv ctxt) then_conv
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   266
          Conv.concl_conv ~1 (add_conv ctxt))) ctxt) i)*}
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   267
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   268
text {*
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   269
  A test case for this conversion is as follows
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   270
*}
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
   271
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   272
lemma "P (Suc (99 + 1)) ((0 + 0)::nat) (Suc (3 + 3 + 3)) (4 + 1)"
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   273
  apply(tactic {* add_tac @{context} 1 *})?
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   274
txt {* 
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   275
  where it produces the goal state
151
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   276
  
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   277
  \begin{minipage}{\textwidth}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   278
  @{subgoals [display]}
7e0bf13bf743 added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   279
  \end{minipage}\bigskip
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   280
*}(*<*)oops(*>*)
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   281
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   282
text {* \solution{ex:addconversion} *}
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   283
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   284
text {* 
174
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   285
  We use the timing function @{ML timing_wrapper} from Recipe~\ref{rec:timing}.
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   286
  To measure any difference between the simproc and conversion, we will create 
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   287
  mechanically terms involving additions and then set up a goal to be 
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   288
  simplified. We have to be careful to set up the goal so that
178
fb8f22dd8ad0 adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents: 177
diff changeset
   289
  other parts of the simplifier do not interfere. For this we construct an
174
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   290
  unprovable goal which, after simplification, we are going to ``prove'' with
351
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   291
  the help of ``\isacommand{sorry}'', that is the method @{ML Skip_Proof.cheat_tac}
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   292
174
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   293
  For constructing test cases, we first define a function that returns a 
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   294
  complete binary tree whose leaves are numbers and the nodes are 
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   295
  additions.
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   296
*}
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   297
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   298
ML{*fun term_tree n =
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   299
let
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   300
  val count = Unsynchronized.ref 0; 
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   301
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   302
  fun term_tree_aux n =
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   303
    case n of
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   304
      0 => (count := !count + 1; HOLogic.mk_number @{typ nat} (!count))
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   305
    | _ => Const (@{const_name "plus"}, @{typ "nat\<Rightarrow>nat\<Rightarrow>nat"}) 
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   306
             $ (term_tree_aux (n - 1)) $ (term_tree_aux (n - 1))
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   307
in
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   308
  term_tree_aux n
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   309
end*}
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   310
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   311
text {*
178
fb8f22dd8ad0 adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents: 177
diff changeset
   312
  This function generates for example:
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   313
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   314
  @{ML_response_fake [display,gray] 
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   315
  "writeln (string_of_term @{context} (term_tree 2))" 
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   316
  "(1 + 2) + (3 + 4)"} 
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   317
174
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   318
  The next function constructs a goal of the form @{text "P \<dots>"} with a term 
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   319
  produced by @{ML term_tree} filled in.
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   320
*}
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   321
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   322
ML{*fun goal n = HOLogic.mk_Trueprop (@{term "P::nat\<Rightarrow> bool"} $ (term_tree n))*}
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   323
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   324
text {*
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   325
  Note that the goal needs to be wrapped in a @{term "Trueprop"}. Next we define
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   326
  two tactics, @{text "c_tac"} and @{text "s_tac"}, for the conversion and simproc,
174
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   327
  respectively. The idea is to first apply the conversion (respectively simproc) and 
351
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   328
  then prove the remaining goal using @{ML "cheat_tac" in Skip_Proof}.
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   329
*}
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   330
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   331
ML{*local
225
7859fc59249a section for further material about simple inductive
Christian Urban <urbanc@in.tum.de>
parents: 214
diff changeset
   332
  fun mk_tac tac = 
351
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
   333
        timing_wrapper (EVERY1 [tac, K (Skip_Proof.cheat_tac @{theory})])
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   334
in
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   335
  val c_tac = mk_tac (add_tac @{context}) 
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   336
  val s_tac = mk_tac (simp_tac (HOL_basic_ss addsimprocs [@{simproc add_sp}]))
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   337
end*}
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   338
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   339
text {*
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 174
diff changeset
   340
  This is all we need to let the conversion run against the simproc:
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   341
*}
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   342
174
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   343
ML{*val _ = Goal.prove @{context} [] [] (goal 8) (K c_tac)
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   344
val _ = Goal.prove @{context} [] [] (goal 8) (K s_tac)*}
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   345
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   346
text {*
174
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   347
  If you do the exercise, you can see that both ways of simplifying additions
178
fb8f22dd8ad0 adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents: 177
diff changeset
   348
  perform relatively similar with perhaps some advantages for the
174
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   349
  simproc. That means the simplifier, even if much more complicated than
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   350
  conversions, is quite efficient for tasks it is designed for. It usually does not
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   351
  make sense to implement general-purpose rewriting using
172
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   352
  conversions. Conversions only have clear advantages in special situations:
ec47352e99c2 improved the solution for the simproc/conversion exercise
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
   353
  for example if you need to have control over innermost or outermost
174
a29b81d4fa88 some minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 172
diff changeset
   354
  rewriting, or when rewriting rules are lead to non-termination.
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   355
*}
130
a21d7b300616 polished the section about simprocs and added an exercise
Christian Urban <urbanc@in.tum.de>
parents: 80
diff changeset
   356
270
bfcabed9079e included an alternative solution for the "rev_sum" example as a comment
griff
parents: 225
diff changeset
   357
end