15
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 1
theory Solutions
172
+ − 2
imports Base "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
156
+ − 5
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
+ − 6
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 7
text {* \solution{fun:revsum} *}
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 8
69
+ − 9
ML{*fun rev_sum t =
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 10
let
130
+ − 11
fun dest_sum (Const (@{const_name plus}, _) $ u $ u') = u' :: dest_sum u
+ − 12
| dest_sum u = [u]
+ − 13
in
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 14
foldl1 (HOLogic.mk_binop @{const_name plus}) (dest_sum t)
130
+ − 15
end *}
15
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 16
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 17
text {* \solution{fun:makesum} *}
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 18
69
+ − 19
ML{*fun make_sum t1 t2 =
130
+ − 20
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
+ − 21
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 22
text {* \solution{ex:scancmts} *}
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 23
130
+ − 24
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>
diff
changeset
+ − 25
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 26
val scan_cmt =
168
+ − 27
let
+ − 28
val begin_cmt = Scan.this_string "(*"
+ − 29
val end_cmt = Scan.this_string "*)"
+ − 30
in
+ − 31
begin_cmt |-- Scan.repeat (Scan.unless end_cmt any) --| end_cmt
+ − 32
>> (enclose "(**" "**)" o implode)
+ − 33
end
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 34
130
+ − 35
val parser = Scan.repeat (scan_cmt || any)
+ − 36
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 37
val scan_all =
168
+ − 38
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>
diff
changeset
+ − 39
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 40
text {*
58
+ − 41
By using @{text "#> fst"} in the last line, the function
+ − 42
@{ML scan_all} retruns a string, instead of the pair a parser would
+ − 43
normally return. For example:
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 44
80
+ − 45
@{ML_response [display,gray]
+ − 46
"let
130
+ − 47
val input1 = (explode \"foo bar\")
+ − 48
val input2 = (explode \"foo (*test*) bar (*test*)\")
+ − 49
in
+ − 50
(scan_all input1, scan_all input2)
+ − 51
end"
+ − 52
"(\"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>
diff
changeset
+ − 53
*}
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 54
130
+ − 55
text {* \solution{ex:addsimproc} *}
+ − 56
+ − 57
ML{*fun dest_sum term =
+ − 58
case term of
+ − 59
(@{term "(op +):: nat \<Rightarrow> nat \<Rightarrow> nat"} $ t1 $ t2) =>
+ − 60
(snd (HOLogic.dest_number t1), snd (HOLogic.dest_number t2))
+ − 61
| _ => raise TERM ("dest_sum", [term])
+ − 62
+ − 63
fun get_sum_thm ctxt t (n1, n2) =
+ − 64
let
+ − 65
val sum = HOLogic.mk_number @{typ "nat"} (n1 + n2)
132
+ − 66
val goal = Logic.mk_equals (t, sum)
130
+ − 67
in
132
+ − 68
Goal.prove ctxt [] [] goal (K (arith_tac ctxt 1))
130
+ − 69
end
+ − 70
+ − 71
fun add_sp_aux ss t =
+ − 72
let
+ − 73
val ctxt = Simplifier.the_context ss
+ − 74
val t' = term_of t
+ − 75
in
+ − 76
SOME (get_sum_thm ctxt t' (dest_sum t'))
+ − 77
handle TERM _ => NONE
+ − 78
end*}
+ − 79
+ − 80
text {* The setup for the simproc is *}
+ − 81
177
+ − 82
simproc_setup %gray add_sp ("t1 + t2") = {* K add_sp_aux *}
130
+ − 83
+ − 84
text {* and a test case is the lemma *}
+ − 85
+ − 86
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>
diff
changeset
+ − 87
apply(tactic {* simp_tac (HOL_basic_ss addsimprocs [@{simproc add_sp}]) 1 *})
130
+ − 88
txt {*
+ − 89
where the simproc produces the goal state
+ − 90
+ − 91
\begin{minipage}{\textwidth}
+ − 92
@{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>
diff
changeset
+ − 93
\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>
diff
changeset
+ − 94
*}(*<*)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>
diff
changeset
+ − 95
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>
diff
changeset
+ − 96
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>
diff
changeset
+ − 97
168
+ − 98
text {*
+ − 99
The following code assumes the function @{ML dest_sum} from the previous
+ − 100
exercise.
+ − 101
*}
+ − 102
166
+ − 103
ML{*fun add_simple_conv ctxt ctrm =
+ − 104
let
+ − 105
val trm = Thm.term_of ctrm
+ − 106
in
+ − 107
get_sum_thm ctxt trm (dest_sum trm)
+ − 108
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>
diff
changeset
+ − 109
166
+ − 110
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>
diff
changeset
+ − 111
(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>
diff
changeset
+ − 112
@{term "(op +)::nat \<Rightarrow> nat \<Rightarrow> nat"} $ _ $ _ =>
166
+ − 113
(Conv.binop_conv (add_conv ctxt)
+ − 114
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>
diff
changeset
+ − 115
| _ $ _ => 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>
diff
changeset
+ − 116
(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>
diff
changeset
+ − 117
| 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>
diff
changeset
+ − 118
| _ => 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>
diff
changeset
+ − 119
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 120
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>
diff
changeset
+ − 121
CONVERSION
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 122
(Conv.params_conv ~1 (fn ctxt =>
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 123
(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>
diff
changeset
+ − 124
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>
diff
changeset
+ − 125
168
+ − 126
text {*
175
+ − 127
A test case for this conversion is as follows
168
+ − 128
*}
+ − 129
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>
diff
changeset
+ − 130
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>
diff
changeset
+ − 131
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>
diff
changeset
+ − 132
txt {*
175
+ − 133
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>
diff
changeset
+ − 134
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>
diff
changeset
+ − 135
\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>
diff
changeset
+ − 136
@{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>
diff
changeset
+ − 137
\end{minipage}\bigskip
130
+ − 138
*}(*<*)oops(*>*)
+ − 139
172
+ − 140
text {* \solution{ex:addconversion} *}
167
3e30ea95c7aa
added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 141
172
+ − 142
text {*
174
+ − 143
We use the timing function @{ML timing_wrapper} from Recipe~\ref{rec:timing}.
+ − 144
To measure any difference between the simproc and conversion, we will create
+ − 145
mechanically terms involving additions and then set up a goal to be
+ − 146
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>
diff
changeset
+ − 147
other parts of the simplifier do not interfere. For this we construct an
174
+ − 148
unprovable goal which, after simplification, we are going to ``prove'' with
192
+ − 149
the help of ``\isacommand{sorry}'', that is the method @{ML SkipProof.cheat_tac}
167
3e30ea95c7aa
added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 150
174
+ − 151
For constructing test cases, we first define a function that returns a
+ − 152
complete binary tree whose leaves are numbers and the nodes are
+ − 153
additions.
167
3e30ea95c7aa
added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 154
*}
3e30ea95c7aa
added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 155
172
+ − 156
ML{*fun term_tree n =
+ − 157
let
+ − 158
val count = ref 0;
167
3e30ea95c7aa
added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 159
172
+ − 160
fun term_tree_aux n =
+ − 161
case n of
+ − 162
0 => (count := !count + 1; HOLogic.mk_number @{typ nat} (!count))
+ − 163
| _ => Const (@{const_name "plus"}, @{typ "nat\<Rightarrow>nat\<Rightarrow>nat"})
+ − 164
$ (term_tree_aux (n - 1)) $ (term_tree_aux (n - 1))
+ − 165
in
+ − 166
term_tree_aux n
+ − 167
end*}
+ − 168
+ − 169
text {*
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 170
This function generates for example:
172
+ − 171
+ − 172
@{ML_response_fake [display,gray]
+ − 173
"warning (Syntax.string_of_term @{context} (term_tree 2))"
+ − 174
"(1 + 2) + (3 + 4)"}
+ − 175
174
+ − 176
The next function constructs a goal of the form @{text "P \<dots>"} with a term
+ − 177
produced by @{ML term_tree} filled in.
167
3e30ea95c7aa
added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 178
*}
3e30ea95c7aa
added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 179
172
+ − 180
ML{*fun goal n = HOLogic.mk_Trueprop (@{term "P::nat\<Rightarrow> bool"} $ (term_tree n))*}
+ − 181
+ − 182
text {*
+ − 183
Note that the goal needs to be wrapped in a @{term "Trueprop"}. Next we define
+ − 184
two tactics, @{text "c_tac"} and @{text "s_tac"}, for the conversion and simproc,
174
+ − 185
respectively. The idea is to first apply the conversion (respectively simproc) and
192
+ − 186
then prove the remaining goal using @{ML "cheat_tac" in SkipProof}.
167
3e30ea95c7aa
added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 187
*}
3e30ea95c7aa
added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 188
172
+ − 189
ML{*local
192
+ − 190
fun mk_tac tac = timing_wrapper (EVERY1 [tac, K (SkipProof.cheat_tac @{theory})])
172
+ − 191
in
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 192
val c_tac = mk_tac (add_tac @{context})
174
+ − 193
val s_tac = mk_tac (simp_tac (HOL_basic_ss addsimprocs [@{simproc add_sp}]))
172
+ − 194
end*}
+ − 195
+ − 196
text {*
175
+ − 197
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>
diff
changeset
+ − 198
*}
3e30ea95c7aa
added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 199
174
+ − 200
ML{*val _ = Goal.prove @{context} [] [] (goal 8) (K c_tac)
172
+ − 201
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>
diff
changeset
+ − 202
172
+ − 203
text {*
174
+ − 204
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>
diff
changeset
+ − 205
perform relatively similar with perhaps some advantages for the
174
+ − 206
simproc. That means the simplifier, even if much more complicated than
+ − 207
conversions, is quite efficient for tasks it is designed for. It usually does not
+ − 208
make sense to implement general-purpose rewriting using
172
+ − 209
conversions. Conversions only have clear advantages in special situations:
+ − 210
for example if you need to have control over innermost or outermost
174
+ − 211
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>
diff
changeset
+ − 212
*}
130
+ − 213
15
9da9ba2b095b
added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 214
end