-
Notifications
You must be signed in to change notification settings - Fork 19
/
UnderLetsPlus0.v
274 lines (240 loc) · 10.4 KB
/
UnderLetsPlus0.v
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
Require Import Coq.micromega.Lia.
Require Import Coq.ZArith.ZArith.
Require Import Coq.QArith.QArith.
Require Import Coq.Classes.Morphisms.
Require Import Coq.Setoids.Setoid.
Require Import Coq.Lists.List.
Require Import Coq.Strings.String.
Require Import Rewriter.Util.Option Rewriter.Util.Strings.ParseArithmetic.
Require Import Rewriter.Rewriter.Examples.PerfTesting.Harness.
Require Import Rewriter.Util.plugins.RewriterBuild.
Require Export Rewriter.Rewriter.Examples.PerfTesting.Settings.
Require Rewriter.Rewriter.Examples.PerfTesting.Sample.
Require Export Rewriter.Util.LetIn Rewriter.Rewriter.Examples.PerfTesting.ListRectInstances.
Import ListNotations.
Local Open Scope list_scope. Local Open Scope Z_scope.
Definition make_lets_def (n : nat) (v : Z) (acc : Z) :=
@nat_rect
(fun _ => Z * Z -> Z)
(fun '(v, acc) => acc + acc + v)
(fun _ rec '(v, acc) => dlet acc := acc + acc + v in rec (v, acc))
n
(v, acc).
Definition eval_Z_to_nat n : Z.to_nat ('n) = '(Z.to_nat n).
Proof. reflexivity. Qed.
Time Make myrew := Rewriter For (Z.add_0_r, eval_Z_to_nat, eval_rect nat, eval_rect prod).
Notation goal n := (forall acc, make_lets_def n 0 acc = acc) (only parsing).
Ltac start _ := cbv [make_lets_def]; intros.
Ltac verify_form start term :=
lazymatch term with
| (dlet x := start + start in ?rest)
=> let x' := fresh in
let __ := constr:(fun x' : Z
=> ltac:(let rest' := constr:(match x' return _ with x => rest end) in
verify_form x' rest';
exact I)) in
idtac
| start + start => idtac
end.
Ltac verify _ :=
lazymatch goal with
| [ |- ?lhs = ?acc ]
=> is_var acc; verify_form acc lhs
end.
#[global] Hint Rewrite Z.add_0_r : mydb.
Inductive rewrite_strat_kind := topdown | bottomup.
Inductive kind_of_rewrite := kind_rewrite_strat (_ : rewrite_strat_kind) | kind_setoid_rewrite | kind_rewrite_lhs_for.
Local Notation "'eta_kind' ( k' => f ) k"
:= match k with
| kind_rewrite_strat topdown
=> subst! (kind_rewrite_strat topdown) for k' in f
| kind_rewrite_strat bottomup
=> subst! (kind_rewrite_strat bottomup) for k' in f
| kind_setoid_rewrite => subst! kind_setoid_rewrite for k' in f
| kind_rewrite_lhs_for => subst! kind_rewrite_lhs_for for k' in f
end
(only parsing, at level 70, k' ident).
Local Lemma sanity : forall T f k, eta_kind (k => f k) k = f k :> T.
Proof. intros; repeat match goal with |- context[match ?e with _ => _ end] => destruct e end; reflexivity. Qed.
(*
:= match test_tac_n, s with
| 0, SuperFast => [(1, 70, 1)]
| 1, SuperFast => [(1, 70, 1)]
| 2, SuperFast => [(1, 30, 1)]
| 3, SuperFast => [(1, 30, 1)]
| 0, Fast => [(71, 5000, 1)]
| 1, Fast => [(71, 200, 1)]
| 2, Fast => [(31, 60, 1)]
| 3, Fast => [(31, 60, 1)]
| 1, Medium => [(201, 371, 1)]
| 2, Medium => [(61, 90, 1)]
| 3, Medium => [(61, 90, 1)]
| 1, Slow => [(372, 1000, 1)] (* ??? *)
| 2, Slow => [(91, 600, 1)] (* ??? *)
| 3, Slow => [(91, 600, 1)] (* ??? *)
| 1, VerySlow => [(1001, 5000, 1)]
| 2, VerySlow => [(601, 5000, 1)]
| 3, VerySlow => [(601, 5000, 1)]
| 0, _ => []
| _, _ => []
end.
*)
Local Notation parse x := (invert_Some (parseQ_arith_strict x)) (only parsing).
Local Notation parse_poly_expr p x := (invert_Some (parseQexpr_arith_with_vars [("x"%string, x)] p)) (only parsing).
Local Notation red_vm_compute x := (ltac:(let z := (eval vm_compute in x) in
exact z)) (only parsing).
Local Notation parse_poly p x := (invert_Some (eval_Qexpr_strict (red_vm_compute (parse_poly_expr p x)))) (only parsing).
Definition size_of_kind (k : kind_of_rewrite) (arg : Z) : Q
:= let x := inject_Z arg in
match k with
| kind_rewrite_strat bottomup
=> parse_poly "-9.07 + 0.926*x + -0.0169*x^2 + 9.36E-05*x^3"%string x
| kind_rewrite_strat topdown
=> parse_poly "-9.31 + 0.947*x + -0.0172*x^2 + 9.45E-05*x^3"%string x
| kind_setoid_rewrite
=> parse_poly "-0.074 + 7.79E-03*x + -7.08E-05*x^2 + 1.42E-06*x^3"%string x
| kind_rewrite_lhs_for
=> parse_poly "4.6006941637196e-07*x^2+0.000270526826521966*x+0.170329400639576"%string x
end%Q.
Definition max_input_of_kind (k : kind_of_rewrite) : option Z
:= match k with
| kind_rewrite_strat _
=> None
| kind_setoid_rewrite
=> None
| kind_rewrite_lhs_for
=> None
end%Z.
Definition args_of_size_by_sample' (k : kind_of_rewrite) (s : size) : list Z
:= Eval cbv beta iota in
eta_size
(s'
=> if match s' with Sanity => true | _ => false end
then [0; 1; 2; 3; 4]
else eta_kind
(k'
=> Sample.generate_inputs
(T:=Z)
1
(size_of_kind k')
(Qseconds_of_size s')
(Qstandard_max_seconds_of_size s')
Sample.default_max_points
(max_input_of_kind k'))
k)
s.
Local Set NativeCompute Profiling.
Local Set NativeCompute Timing.
(* Takes about 20 seconds *)
Time Definition args_of_size_by_sample (k : kind_of_rewrite) (s : size)
:= Eval native_compute in eta_size (s' => eta_kind (k' => args_of_size_by_sample' k' s') k) s.
Definition compat_args_of_size' (test_tac_n : nat) (s : size)
:= let ls
:= match test_tac_n, s with
| 0, SuperFast => [(1, 70, 1)]
| 1, SuperFast => [(1, 70, 1)]
| 2, SuperFast => [(1, 30, 1)]
| 3, SuperFast => [(1, 30, 1)]
| 0, Fast => [(71, 5000, 1)]
| 1, Fast => [(71, 200, 1)]
| 2, Fast => [(31, 60, 1)]
| 3, Fast => [(31, 60, 1)]
| 1, Medium => [(201, 371, 1)]
| 2, Medium => [(61, 90, 1)]
| 3, Medium => [(61, 90, 1)]
| 1, Slow => [(372, 1000, 1)] (* ??? *)
| 2, Slow => [(91, 600, 1)] (* ??? *)
| 3, Slow => [(91, 600, 1)] (* ??? *)
| 1, VerySlow => [(1001, 5000, 1)]
| 2, VerySlow => [(601, 5000, 1)]
| 3, VerySlow => [(601, 5000, 1)]
| 0, _ => []
| _, _ => []
end%nat in
List.flat_map (fun '(start, stop, step) => Sample.Zrange (Z.of_nat start) (inject_Z (Z.of_nat step)) (Z.of_nat stop)) ls.
Definition kind_to_compat (k : kind_of_rewrite) : option nat
:= Some
match k with
| kind_rewrite_lhs_for => 0
| kind_setoid_rewrite => 1
| kind_rewrite_strat topdown => 2
| kind_rewrite_strat bottomup => 2
end%nat.
Definition compat_args_of_size (k : kind_of_rewrite) (s : size)
:= match kind_to_compat k, s with
| _, (Sanity | Slow | VerySlow)
| None, _
=> args_of_size_by_sample k s
| Some n, _ => compat_args_of_size' n s
end.
Time Definition args_of_size (k : kind_of_rewrite) (s : size)
:= Eval native_compute in eta_size (s' => eta_kind (k' => compat_args_of_size k' s') k) s.
Ltac mkgoal kind n :=
let Z_to_nat := (eval vm_compute in Z.to_nat) in
constr:(goal (id Z_to_nat n)).
Ltac redgoal _ := start ().
Ltac describe_goal n := idtac "Params: n=" n.
Ltac time_solve_goal kind
:= let Z_to_nat := (eval cbv in Z.to_nat) in
let change_Z_to_nat _ := (change (id Z_to_nat) with Z.to_nat) in
lazymatch kind with
| kind_rewrite_strat topdown
=> fun n
=> time "rewrite_strat(topdown)-regression-exponential"
(cbv [nat_rect id]; repeat rewrite_strat topdown hints mydb)
| kind_rewrite_strat bottomup
=> fun n
=> time "rewrite_strat(bottomup)-regression-exponential"
(cbv [nat_rect id]; repeat rewrite_strat bottomup hints mydb)
| kind_setoid_rewrite
=> fun n
=> time "setoid_rewrite-regression-cubic"
(cbv [nat_rect id]; repeat setoid_rewrite Z.add_0_r)
| kind_rewrite_lhs_for
=> fun n
=> time "Rewrite_lhs_for-regression-quadratic"
(change_Z_to_nat (); Rewrite_lhs_for myrew)
end.
(**
<<<
#!/usr/bin/env python3
print(r'''(**
<<<
''')
print(open(__file__, 'r').read())
print(r'''>>>
*)''')
for i, c in enumerate(('kind_rewrite_strat topdown', 'kind_rewrite_strat bottomup', 'kind_setoid_rewrite', 'kind_rewrite_lhs_for')):
print(f'Ltac mkgoal{i} := mkgoal constr:({c}).\nLtac time_solve_goal{i} := time_solve_goal constr:({c}).\nLtac run{i} sz := Harness.runtests_verify_sanity (args_of_size ({c})) describe_goal mkgoal{i} redgoal time_solve_goal{i} verify sz.\n')
>>>
*)
Ltac mkgoal0 := mkgoal constr:(kind_rewrite_strat topdown).
Ltac time_solve_goal0 := time_solve_goal constr:(kind_rewrite_strat topdown).
Ltac run0 sz := Harness.runtests_verify_sanity (args_of_size (kind_rewrite_strat topdown)) describe_goal mkgoal0 redgoal time_solve_goal0 verify sz.
Ltac mkgoal1 := mkgoal constr:(kind_rewrite_strat bottomup).
Ltac time_solve_goal1 := time_solve_goal constr:(kind_rewrite_strat bottomup).
Ltac run1 sz := Harness.runtests_verify_sanity (args_of_size (kind_rewrite_strat bottomup)) describe_goal mkgoal1 redgoal time_solve_goal1 verify sz.
Ltac mkgoal2 := mkgoal constr:(kind_setoid_rewrite).
Ltac time_solve_goal2 := time_solve_goal constr:(kind_setoid_rewrite).
Ltac run2 sz := Harness.runtests_verify_sanity (args_of_size (kind_setoid_rewrite)) describe_goal mkgoal2 redgoal time_solve_goal2 verify sz.
Ltac mkgoal3 := mkgoal constr:(kind_rewrite_lhs_for).
Ltac time_solve_goal3 := time_solve_goal constr:(kind_rewrite_lhs_for).
Ltac run3 sz := Harness.runtests_verify_sanity (args_of_size (kind_rewrite_lhs_for)) describe_goal mkgoal3 redgoal time_solve_goal3 verify sz.
Local Transparent Let_In.
#[global] Hint Opaque Let_In Z.add : rewrite typeclass_instances.
Global Opaque Let_In Z.add.
Global Instance : forall {A}, Proper (eq ==> eq ==> Basics.flip Basics.impl) (@eq A) := _.
Global Instance : forall {A B x}, Proper (pointwise_relation _ eq ==> eq) (@Let_In A (fun _ => B) x)
:= _.
Global Instance : forall {A B x}, Proper (forall_relation (fun _ => eq) ==> eq) (@Let_In A (fun _ => B) x)
:= _.
Global Instance : forall {acc}, @ProperProxy Z (@eq Z) acc := _.
Global Open Scope Z_scope.
(*
Goal True.
let v := mkgoal3 3 in
assert v; [ redgoal () | ].
time_solve_goal3 ().
verify ().
run3 Sanity.
*)