-
Notifications
You must be signed in to change notification settings - Fork 84
/
icing_optimisationsLib.sml
282 lines (273 loc) · 12.3 KB
/
icing_optimisationsLib.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
(*
Library defining HOL4 automation that builds an optimiser
correctness theorem for an optimisation plan.
*)
structure icing_optimisationsLib =
struct
open floatToRealProofsTheory icing_realIdProofsTheory source_to_source2ProofsTheory
listTheory Portable;
open preamble;
local
fun mk_single_rewriteFPexp_correct_thm th1 th2 =
let
val arglist =
th1 |> concl
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> rator |> rator |> rator |> rator |> rator |> rand;
val arg =
th2 |> concl
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> rator |> rator |> rator |> rator |> rator |> rand
|> listSyntax.dest_list |> fst |> hd (* TODO: Check length *);
in
SIMP_RULE std_ss [GSYM AND_IMP_INTRO] rewriteExp_compositional
|> SPECL [arglist,arg]
|> (fn th => MP th th1)
|> (fn th => MP th th2)
|> SIMP_RULE std_ss [APPEND]
end;
fun mk_rewriteFPexp_correct_thm_list thms correctthm =
case thms of
[] => correctthm
| th1::thms =>
mk_rewriteFPexp_correct_thm_list thms (mk_single_rewriteFPexp_correct_thm correctthm th1);
fun mk_rewriteFPexp_list_correct_thm th1 =
let
val arglist =
th1 |> concl
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> rator |> rator |> rator |> rator |> rator |> rand;
in
SPEC arglist lift_rewriteFPexp_correct_list
|> (fn th => MP th th1)
end;
val all_optimisation_proofs =
map (fn ((a,(b,c,d))) => (a,(b,c))) (DB.thy "icing_optimisationProofs");
(* Automatically prove a is_perform_rewrites_correct theorem for the given plan *)
fun mk_plan_correct_thm plan_list :(term * thm)=
case plan_list of
(* Base case: Empty plan -> No plan used in correctness theorem *)
[] => (“[]:opt_step list”, is_perform_rewrites_correct_empty_plan)
| p1 :: ps => (* Cons case *)
let
(* Recursive call *)
val (rec_plan, rec_thm) = mk_plan_correct_thm ps
(* Result plan, used for instantiation of theorems *)
val full_plan = Parse.Term ‘^p1 :: ^rec_plan’
(* Get the Datatype constructor used, can be "Label", "Expected" or "Apply" *)
val dtype_comb = rator p1
in
(* Simple case: "Label s" --> can be appended to the correctness theorem *)
if (Term.compare(dtype_comb, “source_to_source2$Label”) = EQUAL)
then
(full_plan,
MP (Q.SPECL [‘^(rand p1)’, ‘^rec_plan’] is_perform_rewrites_correct_label) rec_thm)
(* Simple case: "Expected e" --> can be appended to the correctness theorem *)
else if (Term.compare(dtype_comb, “source_to_source2$Expected”) = EQUAL)
then (full_plan,
MP (Q.SPECL [‘^(rand p1)’, ‘^rec_plan’] is_perform_rewrites_correct_expected) rec_thm)
else (* Must be an Apply (path, rws) now *)
let
val _ = if (Term.compare(dtype_comb, “source_to_source2$Apply”) <> EQUAL)
then raise Feedback.mk_HOL_ERR "" "" "Internal err, expected Apply" else ()
val (pth, rws) = rand p1 |> dest_pair
(* corr_thms = list of is_rewriteFPexp_correct theorems *)
val corr_thms:thm list = rand p1
|> dest_pair |> #2 (* extract the rewrites *)
|> listSyntax.dest_list |> #1
|> map (fn t => (t, DB.apropos_in t all_optimisation_proofs)) (* Look up correctness theorems *)
|> map (fn (t, thms) =>
(print_term t; print (" has thm :\n"); map (fn d => print_thm (#1 (#2 d))) thms; print ("\n\n"); thms))
|> map (fn datas => if (length datas <> 1)
then if (length datas = 0)
then raise Feedback.mk_HOL_ERR "" "" "Not enough matching theorems"
else raise Feedback.mk_HOL_ERR "" "" "Too many matching theorems"
else #1 (#2 (hd datas)))
(* |> (fn datas => if (length datas <> 1) then raise ERR "Too many matching theorems" ""
else datas)*)
(* Join the theorems into a single theorem about each of them*)
val all_rewrites_correct_thm = mk_rewriteFPexp_correct_thm_list corr_thms empty_rw_correct
(* lift the theorem to is_rewriteFPexp_list_correct *)
val all_rewrites_list_correct_thm = mk_rewriteFPexp_list_correct_thm all_rewrites_correct_thm
(* Extract argument list from theorem *)
val args = all_rewrites_list_correct_thm |> concl
|> dest_forall |> snd |> dest_forall |> snd
|> dest_forall |> snd |> dest_forall |> snd
|> dest_forall |> snd
|> rator |> rator |> rator |> rator |> rator |> rand
(* build a single perform_rewrites_correct theorem using modus-ponens *)
val perform_rw_correct_thm =
MP (Q.SPEC ‘^args’ is_rewriteFPexp_correct_lift_perform_rewrites)
all_rewrites_list_correct_thm
|> Q.SPEC ‘^(rand p1 |> dest_pair |> #1)’
(* finally, use the "CONS" theorem for plans to prepend the current Apply node *)
val final_perform_rw_thm =
let
val th1 = MP (Q.SPECL [‘^args’, ‘^(rand p1 |> dest_pair |> #1)’, ‘^rec_plan’]
is_perform_rewrites_correct_cons)
perform_rw_correct_thm
in
MP th1 rec_thm end
in (full_plan, final_perform_rw_thm)
end
end;
in
fun mk_stos_pass_correct_thm plan_list =
let
val (thePlan, plan_correct_perform_rewrites) = mk_plan_correct_thm plan_list;
val plan_correct_optimise_with_plan =
MP (Q.SPEC ‘^thePlan’ is_optimise_with_plan_correct_lift)
plan_correct_perform_rewrites
(* val stos_pass_correct_with_plan =
MP (Q.SPEC ‘[^thePlan]’ stos_pass_with_plans_correct)
(MP (Q.SPEC ‘^thePlan’ is_optimise_with_plan_correct_sing)
plan_correct_optimise_with_plan)
in stos_pass_correct_with_plan *)
in plan_correct_optimise_with_plan
end;
end;
(** Real-valued identity proof *)
local
fun mk_single_real_id_thm th1 th2 =
let
val arglist =
th1 |> concl
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> rator |> rator |> rator |> rator |> rator |> rand;
val arg =
th2 |> concl
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> rator |> rator |> rator |> rator |> rator |> rand
|> listSyntax.dest_list |> fst |> hd (* TODO: Check length *);
in
SIMP_RULE std_ss [GSYM AND_IMP_INTRO] real_valued_id_compositional
|> SPECL [arglist,arg]
|> (fn th => MP th th1)
|> (fn th => MP th th2)
|> SIMP_RULE std_ss [APPEND]
end;
fun mk_real_id_thm_list thms correctthm =
case thms of
[] => correctthm
| th1::thms =>
mk_real_id_thm_list thms (mk_single_real_id_thm correctthm th1);
fun mk_real_id_list_thm th1 =
let
val arglist =
th1 |> concl
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> dest_forall |> snd
|> rator |> rator |> rator |> rator |> rator |> rand;
in
SPEC arglist lift_real_id_exp_list_strong
|> (fn th => MP th th1)
end;
val all_realid_proofs =
map (fn ((a,(b,c,d))) => (a,(b,c))) (DB.thy "icing_realIdProofs");
(* Automatically prove a is_perform_rewrites_correct theorem for the given plan *)
fun mk_plan_correct_thm plan_list :(term * thm)=
case plan_list of
(* Base case: Empty plan -> No plan used in correctness theorem *)
[] => (“[]:opt_step list”, is_real_id_perform_rewrites_empty)
| p1 :: ps => (* Cons case *)
let
(* Recursive call *)
val (rec_plan, rec_thm) = mk_plan_correct_thm ps
(* Result plan, used for instantiation of theorems *)
val full_plan = Parse.Term ‘^p1 :: ^rec_plan’
(* Get the Datatype constructor used, can be "Label", "Expected" or "Apply" *)
val dtype_comb = rator p1
in
(* Simple case: "Label s" --> can be appended to the correctness theorem *)
if (Term.compare(dtype_comb, “source_to_source2$Label”) = EQUAL)
then
(full_plan,
MP (Q.SPECL [‘^(rand p1)’, ‘^rec_plan’] is_perform_rewrites_correct_label_real_id) rec_thm)
(* Simple case: "Expected e" --> can be appended to the correctness theorem *)
else if (Term.compare(dtype_comb, “source_to_source2$Expected”) = EQUAL)
then (full_plan,
MP (Q.SPECL [‘^(rand p1)’, ‘^rec_plan’] is_perform_rewrites_correct_expected_real_id) rec_thm)
else (* Must be an Apply (path, rws) now *)
let
val _ = if (Term.compare(dtype_comb, “source_to_source2$Apply”) <> EQUAL)
then raise Feedback.mk_HOL_ERR "" "" "Internal err, expected Apply" else ()
val (pth, rws) = rand p1 |> dest_pair
(* corr_thms = list of is_rewriteFPexp_correct theorems *)
val corr_thms:thm list = rand p1
|> dest_pair |> #2 (* extract the rewrites *)
|> listSyntax.dest_list |> #1
|> map (fn t => (t, DB.apropos_in t all_realid_proofs)) (* Look up correctness theorems *)
|> map (fn (t, thms) =>
(print_term t; print (":\n"); map (fn d => print_thm (#1 (#2 d))) thms; thms))
|> map (fn datas => if (length datas <> 1)
then if (length datas = 0)
then raise Feedback.mk_HOL_ERR "" "" "Not enough matching theorems"
else raise Feedback.mk_HOL_ERR "" "" "Too many matching theorems"
else #1 (#2 (hd datas)))
(* |> (fn datas => if (length datas <> 1) then raise ERR "Too many matching theorems" ""
else datas)*)
(* Join the theorems into a single theorem about each of them*)
val all_rewrites_correct_thm = mk_real_id_thm_list corr_thms empty_rw_real_id
(* lift the theorem to is_rewriteFPexp_list_correct *)
val all_rewrites_list_correct_thm = mk_real_id_list_thm all_rewrites_correct_thm
(* Extract argument list from theorem *)
val args = all_rewrites_list_correct_thm |> concl
|> dest_forall |> snd |> dest_forall |> snd
|> dest_forall |> snd |> dest_forall |> snd
|> dest_forall |> snd
|> rator |> rator |> rator |> rator |> rator |> rand
(* build a single perform_rewrites_correct theorem using modus-ponens *)
val perform_rw_correct_thm =
MP (Q.SPEC ‘^args’ is_real_id_list_perform_rewrites_lift)
all_rewrites_list_correct_thm
|> Q.SPEC ‘^(rand p1 |> dest_pair |> #1)’
(* finally, use the "CONS" theorem for plans to prepend the current Apply node *)
val final_perform_rw_thm =
let
val th1 = MP (Q.SPECL [‘^args’, ‘^(rand p1 |> dest_pair |> #1)’, ‘^rec_plan’]
is_perform_rewrites_correct_cons_real_id)
perform_rw_correct_thm
in
MP th1 rec_thm end
in (full_plan, final_perform_rw_thm)
end
end;
in
fun mk_stos_pass_real_id_thm plan_list =
let
val (thePlan, plan_correct_perform_rewrites) = mk_plan_correct_thm plan_list;
val plan_correct_optimise_with_plan =
MP (Q.SPEC ‘^thePlan’ is_real_id_perform_rewrites_optimise_with_plan_lift)
plan_correct_perform_rewrites
(* val stos_pass_correct_with_plan =
MP (Q.SPEC ‘[^thePlan]’ stos_pass_with_plans_real_id)
(MP (Q.SPEC ‘^thePlan’ is_optimise_with_plan_correct_sing_real_id)
plan_correct_optimise_with_plan)
in stos_pass_correct_with_plan *)
in plan_correct_optimise_with_plan
end;
end;
end