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