| 1 |
(** * Push-Button Synthesis of Primitives *) |
| 2 |
From Coq Require Import String. |
| 3 |
From Coq Require Import Lia. |
| 4 |
From Coq Require Import ZArith. |
| 5 |
From Coq Require Import MSetPositive. |
| 6 |
From Coq Require Import List. |
| 7 |
From Coq Require Import QArith_base Qround. |
| 8 |
From Coq Require Import Derive. |
| 9 |
Require Import Crypto.Util.ErrorT. |
| 10 |
Require Import Crypto.Util.ListUtil. |
| 11 |
Require Import Crypto.Util.OptionList. |
| 12 |
Require Import Crypto.Util.Strings.Decimal. |
| 13 |
Require Import Crypto.Util.ZRange. |
| 14 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 15 |
Require Import Crypto.Util.ZUtil.Zselect. |
| 16 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 17 |
Require Import Crypto.Util.FueledLUB. |
| 18 |
Require Import Crypto.Util.Tactics.HasBody. |
| 19 |
Require Import Crypto.Util.Tactics.Head. |
| 20 |
Require Import Crypto.Util.Tactics.ConstrFail. |
| 21 |
Require Import Rewriter.Language.Wf. |
| 22 |
Require Import Crypto.Language.WfExtra. |
| 23 |
Require Import Rewriter.Language.Language. |
| 24 |
Require Import Crypto.Language.API. |
| 25 |
Require Import Crypto.Stringification.Language. |
| 26 |
Require Import Crypto.Arithmetic.Core. |
| 27 |
Require Import Crypto.Arithmetic.ModOps. |
| 28 |
Require Import Crypto.Arithmetic.Partition. |
| 29 |
Require Import Crypto.Arithmetic.Primitives. |
| 30 |
Require Import Crypto.Arithmetic.WordByWordMontgomery. |
| 31 |
Require Import Crypto.Arithmetic.UniformWeight. |
| 32 |
Require Import Crypto.BoundsPipeline. |
| 33 |
Require Import Crypto.COperationSpecifications. |
| 34 |
Require Import Crypto.PushButtonSynthesis.ReificationCache. |
| 35 |
Require Import Crypto.Util.ErrorT.List. |
| 36 |
Require Import Crypto.Util.ListUtil.GroupAllBy. |
| 37 |
Require Import Crypto.Util.Strings.Show. |
| 38 |
Require Import Crypto.Util.DebugMonad. |
| 39 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 40 |
Require Crypto.Assembly.Parse. |
| 41 |
Require Import Crypto.Assembly.Equivalence. |
| 42 |
Import ListNotations. |
| 43 |
Local Open Scope string_scope. Local Open Scope Z_scope. Local Open Scope list_scope. Local Open Scope bool_scope. |
| 44 |
|
| 45 |
Import |
| 46 |
Language.Wf.Compilers |
| 47 |
Language.WfExtra.Compilers |
| 48 |
Language.Compilers |
| 49 |
API.Compilers |
| 50 |
Stringification.Language.Compilers |
| 51 |
Stringification.Language.Compilers.ToString. |
| 52 |
Import Compilers.API. |
| 53 |
|
| 54 |
Import COperationSpecifications.Primitives. |
| 55 |
Import COperationSpecifications.Solinas. (* for selectznz *) |
| 56 |
|
| 57 |
Import Associational Positional. |
| 58 |
|
| 59 |
Local Coercion Z.of_nat : nat >-> Z. |
| 60 |
Local Coercion QArith_base.inject_Z : Z >-> Q. |
| 61 |
Local Coercion Z.pos : positive >-> Z. |
| 62 |
|
| 63 |
Local Set Keyed Unification. (* needed for making [autorewrite] fast, c.f. COQBUG(https://github.com/coq/coq/issues/9283) *) |
| 64 |
(** |
| 65 |
<< |
| 66 |
#!/usr/bin/env python |
| 67 |
|
| 68 |
#!/usr/bin/env python |
| 69 |
|
| 70 |
indent = '' |
| 71 |
|
| 72 |
print((indent + '(' + r'''** |
| 73 |
<< |
| 74 |
%s |
| 75 |
>> |
| 76 |
*''' + ')\n') % open(__file__, 'r').read()) |
| 77 |
|
| 78 |
for (op, opmod) in (('id', '(@id (list Z))'), ('selectznz', 'Positional.select'), ('mulx', 'mulx'), ('addcarryx', 'addcarryx'), ('subborrowx', 'subborrowx'), ('value_barrier', 'Z.value_barrier'), ('cmovznz', 'cmovznz'), ('cmovznz_by_mul', 'cmovznz_by_mul')): |
| 79 |
print((rf'''{indent}Derive reified_{op}_gen |
| 80 |
SuchThat (is_reification_of reified_{op}_gen {opmod}) |
| 81 |
As reified_{op}_gen_correct. |
| 82 |
Proof. Time cache_reify (). Time Qed. |
| 83 |
Local Definition reified_{op}_gen_correct_proj1 := proj1 reified_{op}_gen_correct. |
| 84 |
Local Definition reified_{op}_gen_correct_proj2 := proj2 reified_{op}_gen_correct. |
| 85 |
#[global] |
| 86 |
Hint Extern 1 (_ = _) => apply_cached_reification {opmod} reified_{op}_gen_correct_proj1 : reify_cache_gen. |
| 87 |
#[global] |
| 88 |
Hint Immediate reified_{op}_gen_correct_proj2 : wf_gen_cache. |
| 89 |
#[global] |
| 90 |
Hint Rewrite reified_{op}_gen_correct_proj1 : interp_gen_cache. |
| 91 |
Local Opaque reified_{op}_gen. (* needed for making [autorewrite] not take a very long time *)''').replace('\n', '\n%s' % indent) + '\n') |
| 92 |
>> |
| 93 |
*) |
| 94 |
|
| 95 |
Derive reified_id_gen |
| 96 |
SuchThat (is_reification_of reified_id_gen (@id (list Z))) |
| 97 |
As reified_id_gen_correct. |
| 98 |
Proof. Time cache_reify (). Time Qed. |
| 99 |
Local Definition reified_id_gen_correct_proj1 := proj1 reified_id_gen_correct. |
| 100 |
Local Definition reified_id_gen_correct_proj2 := proj2 reified_id_gen_correct. |
| 101 |
#[global] |
| 102 |
Hint Extern 1 (_ = _) => apply_cached_reification (@id (list Z)) reified_id_gen_correct_proj1 : reify_cache_gen. |
| 103 |
#[global] |
| 104 |
Hint Immediate reified_id_gen_correct_proj2 : wf_gen_cache. |
| 105 |
#[global] |
| 106 |
Hint Rewrite reified_id_gen_correct_proj1 : interp_gen_cache. |
| 107 |
Local Opaque reified_id_gen. (* needed for making [autorewrite] not take a very long time *) |
| 108 |
|
| 109 |
Derive reified_selectznz_gen |
| 110 |
SuchThat (is_reification_of reified_selectznz_gen Positional.select) |
| 111 |
As reified_selectznz_gen_correct. |
| 112 |
Proof. Time cache_reify (). Time Qed. |
| 113 |
Local Definition reified_selectznz_gen_correct_proj1 := proj1 reified_selectznz_gen_correct. |
| 114 |
Local Definition reified_selectznz_gen_correct_proj2 := proj2 reified_selectznz_gen_correct. |
| 115 |
#[global] |
| 116 |
Hint Extern 1 (_ = _) => apply_cached_reification Positional.select reified_selectznz_gen_correct_proj1 : reify_cache_gen. |
| 117 |
#[global] |
| 118 |
Hint Immediate reified_selectznz_gen_correct_proj2 : wf_gen_cache. |
| 119 |
#[global] |
| 120 |
Hint Rewrite reified_selectznz_gen_correct_proj1 : interp_gen_cache. |
| 121 |
Local Opaque reified_selectznz_gen. (* needed for making [autorewrite] not take a very long time *) |
| 122 |
|
| 123 |
Derive reified_mulx_gen |
| 124 |
SuchThat (is_reification_of reified_mulx_gen mulx) |
| 125 |
As reified_mulx_gen_correct. |
| 126 |
Proof. Time cache_reify (). Time Qed. |
| 127 |
Local Definition reified_mulx_gen_correct_proj1 := proj1 reified_mulx_gen_correct. |
| 128 |
Local Definition reified_mulx_gen_correct_proj2 := proj2 reified_mulx_gen_correct. |
| 129 |
#[global] |
| 130 |
Hint Extern 1 (_ = _) => apply_cached_reification mulx reified_mulx_gen_correct_proj1 : reify_cache_gen. |
| 131 |
#[global] |
| 132 |
Hint Immediate reified_mulx_gen_correct_proj2 : wf_gen_cache. |
| 133 |
#[global] |
| 134 |
Hint Rewrite reified_mulx_gen_correct_proj1 : interp_gen_cache. |
| 135 |
Local Opaque reified_mulx_gen. (* needed for making [autorewrite] not take a very long time *) |
| 136 |
|
| 137 |
Derive reified_addcarryx_gen |
| 138 |
SuchThat (is_reification_of reified_addcarryx_gen addcarryx) |
| 139 |
As reified_addcarryx_gen_correct. |
| 140 |
Proof. Time cache_reify (). Time Qed. |
| 141 |
Local Definition reified_addcarryx_gen_correct_proj1 := proj1 reified_addcarryx_gen_correct. |
| 142 |
Local Definition reified_addcarryx_gen_correct_proj2 := proj2 reified_addcarryx_gen_correct. |
| 143 |
#[global] |
| 144 |
Hint Extern 1 (_ = _) => apply_cached_reification addcarryx reified_addcarryx_gen_correct_proj1 : reify_cache_gen. |
| 145 |
#[global] |
| 146 |
Hint Immediate reified_addcarryx_gen_correct_proj2 : wf_gen_cache. |
| 147 |
#[global] |
| 148 |
Hint Rewrite reified_addcarryx_gen_correct_proj1 : interp_gen_cache. |
| 149 |
Local Opaque reified_addcarryx_gen. (* needed for making [autorewrite] not take a very long time *) |
| 150 |
|
| 151 |
Derive reified_subborrowx_gen |
| 152 |
SuchThat (is_reification_of reified_subborrowx_gen subborrowx) |
| 153 |
As reified_subborrowx_gen_correct. |
| 154 |
Proof. Time cache_reify (). Time Qed. |
| 155 |
Local Definition reified_subborrowx_gen_correct_proj1 := proj1 reified_subborrowx_gen_correct. |
| 156 |
Local Definition reified_subborrowx_gen_correct_proj2 := proj2 reified_subborrowx_gen_correct. |
| 157 |
#[global] |
| 158 |
Hint Extern 1 (_ = _) => apply_cached_reification subborrowx reified_subborrowx_gen_correct_proj1 : reify_cache_gen. |
| 159 |
#[global] |
| 160 |
Hint Immediate reified_subborrowx_gen_correct_proj2 : wf_gen_cache. |
| 161 |
#[global] |
| 162 |
Hint Rewrite reified_subborrowx_gen_correct_proj1 : interp_gen_cache. |
| 163 |
Local Opaque reified_subborrowx_gen. (* needed for making [autorewrite] not take a very long time *) |
| 164 |
|
| 165 |
Derive reified_value_barrier_gen |
| 166 |
SuchThat (is_reification_of reified_value_barrier_gen Z.value_barrier) |
| 167 |
As reified_value_barrier_gen_correct. |
| 168 |
Proof. Time cache_reify (). Time Qed. |
| 169 |
Local Definition reified_value_barrier_gen_correct_proj1 := proj1 reified_value_barrier_gen_correct. |
| 170 |
Local Definition reified_value_barrier_gen_correct_proj2 := proj2 reified_value_barrier_gen_correct. |
| 171 |
#[global] |
| 172 |
Hint Extern 1 (_ = _) => apply_cached_reification Z.value_barrier reified_value_barrier_gen_correct_proj1 : reify_cache_gen. |
| 173 |
#[global] |
| 174 |
Hint Immediate reified_value_barrier_gen_correct_proj2 : wf_gen_cache. |
| 175 |
#[global] |
| 176 |
Hint Rewrite reified_value_barrier_gen_correct_proj1 : interp_gen_cache. |
| 177 |
Local Opaque reified_value_barrier_gen. (* needed for making [autorewrite] not take a very long time *) |
| 178 |
|
| 179 |
Derive reified_cmovznz_gen |
| 180 |
SuchThat (is_reification_of reified_cmovznz_gen cmovznz) |
| 181 |
As reified_cmovznz_gen_correct. |
| 182 |
Proof. Time cache_reify (). Time Qed. |
| 183 |
Local Definition reified_cmovznz_gen_correct_proj1 := proj1 reified_cmovznz_gen_correct. |
| 184 |
Local Definition reified_cmovznz_gen_correct_proj2 := proj2 reified_cmovznz_gen_correct. |
| 185 |
#[global] |
| 186 |
Hint Extern 1 (_ = _) => apply_cached_reification cmovznz reified_cmovznz_gen_correct_proj1 : reify_cache_gen. |
| 187 |
#[global] |
| 188 |
Hint Immediate reified_cmovznz_gen_correct_proj2 : wf_gen_cache. |
| 189 |
#[global] |
| 190 |
Hint Rewrite reified_cmovznz_gen_correct_proj1 : interp_gen_cache. |
| 191 |
Local Opaque reified_cmovznz_gen. (* needed for making [autorewrite] not take a very long time *) |
| 192 |
|
| 193 |
Derive reified_cmovznz_by_mul_gen |
| 194 |
SuchThat (is_reification_of reified_cmovznz_by_mul_gen cmovznz_by_mul) |
| 195 |
As reified_cmovznz_by_mul_gen_correct. |
| 196 |
Proof. Time cache_reify (). Time Qed. |
| 197 |
Local Definition reified_cmovznz_by_mul_gen_correct_proj1 := proj1 reified_cmovznz_by_mul_gen_correct. |
| 198 |
Local Definition reified_cmovznz_by_mul_gen_correct_proj2 := proj2 reified_cmovznz_by_mul_gen_correct. |
| 199 |
#[global] |
| 200 |
Hint Extern 1 (_ = _) => apply_cached_reification cmovznz_by_mul reified_cmovznz_by_mul_gen_correct_proj1 : reify_cache_gen. |
| 201 |
#[global] |
| 202 |
Hint Immediate reified_cmovznz_by_mul_gen_correct_proj2 : wf_gen_cache. |
| 203 |
#[global] |
| 204 |
Hint Rewrite reified_cmovznz_by_mul_gen_correct_proj1 : interp_gen_cache. |
| 205 |
Local Opaque reified_cmovznz_by_mul_gen. (* needed for making [autorewrite] not take a very long time *) |
| 206 |
|
| 207 |
(* needed for making [autorewrite] with [Set Keyed Unification] fast *) |
| 208 |
Local Opaque expr.Interp. |
| 209 |
|
| 210 |
Notation "'FromPipelineToString!' machine_wordsize prefix name result" |
| 211 |
:= (Pipeline.FromPipelineToString machine_wordsize prefix name result) |
| 212 |
(only parsing, at level 10, machine_wordsize at next level, prefix at next level, name at next level, result at next level). |
| 213 |
Notation "'FromPipelineToInternalString!' machine_wordsize prefix name result" |
| 214 |
:= (Pipeline.FromPipelineToInternalString machine_wordsize prefix name result) |
| 215 |
(only parsing, at level 10, machine_wordsize at next level, prefix at next level, name at next level, result at next level). |
| 216 |
|
| 217 |
(** Some utilities for defining [check_args] *) |
| 218 |
Definition request_present (requests : list string) (request : string) : bool |
| 219 |
:= List.existsb (String.eqb request) requests. |
| 220 |
(** [None] means that the condition should hold regardless of what requests were made *) |
| 221 |
Definition any_request_present (requests : list string) (search_for : option (list string)) : bool |
| 222 |
:= match search_for with |
| 223 |
| None => true |
| 224 |
| Some search_for => List.existsb (request_present requests) search_for |
| 225 |
end. |
| 226 |
(** Turns a list of [(precondition : bool, (check : bool, error))] |
| 227 |
into a [check_args] function. The [precondition] is something |
| 228 |
like "this function was requested" or "this option was passed". *) |
| 229 |
Definition check_args_of_list (check_args_descr : list (bool * (bool * Pipeline.ErrorMessage))) |
| 230 |
{T} (res : Pipeline.ErrorT T) |
| 231 |
: Pipeline.ErrorT T |
| 232 |
:= fold_right |
| 233 |
(fun '(precondition, (b, e)) k => if implb precondition b then k else Error e) |
| 234 |
res |
| 235 |
check_args_descr. |
| 236 |
Ltac prepare_use_curve_good _ := |
| 237 |
let curve_good := lazymatch goal with | curve_good : ?check_args (Success tt) = Success tt |- _ => curve_good end in |
| 238 |
let requests := lazymatch type of curve_good with ?check_args ?requests _ = Success _ => requests end in |
| 239 |
revert dependent requests; clear; intros; |
| 240 |
lazymatch type of curve_good with |
| 241 |
| ?check = Success ?v => let check' := (eval hnf in check) in change (check' = Success v) in curve_good |
| 242 |
end; |
| 243 |
cbv [machine_wordsize_opt] in *; |
| 244 |
cbv [check_args_of_list] in *; |
| 245 |
cbn [List.fold_right List.map List.app] in *; |
| 246 |
cbv [any_request_present] in *; |
| 247 |
cbn [List.existsb] in *; |
| 248 |
repeat first [ progress cbv beta iota in * |
| 249 |
| progress cbn [andb orb implb] in * |
| 250 |
| discriminate |
| 251 |
| match goal with |
| 252 |
| [ H : context[match ?b with true => _ | false => _ end ] |- _ ] => destruct b eqn:? |
| 253 |
| [ H : context[orb _ false] |- _ ] => rewrite Bool.orb_false_r in H |
| 254 |
| [ H : context[orb false _] |- _ ] => rewrite Bool.orb_false_l in H |
| 255 |
end |
| 256 |
| progress Reflect.reflect_hyps |
| 257 |
| assumption |
| 258 |
| progress destruct_head'_and |
| 259 |
| apply conj ]. |
| 260 |
Ltac use_requests_to_prove_curve_good_t_step := |
| 261 |
first [ progress intros |
| 262 |
| progress Reflect.reflect_hyps |
| 263 |
| exfalso; assumption |
| 264 |
| progress specialize_by auto 50 using or_introl, or_intror with nocore ]. |
| 265 |
Ltac prove_correctness' should_not_clear use_curve_good := |
| 266 |
let should_not_clear_requests H := lazymatch type of H with context[request_present] => idtac end in |
| 267 |
let Hres := match goal with H : _ = Success _ |- _ => H end in |
| 268 |
let H := fresh in |
| 269 |
pose proof use_curve_good as H; |
| 270 |
(* I want to just use [clear -H Hres], but then I can't use any lemmas in the section because of COQBUG(https://github.com/coq/coq/issues/8153) *) |
| 271 |
repeat match goal with |
| 272 |
| [ H' : _ |- _ ] |
| 273 |
=> tryif first [ has_body H' | constr_eq H' H | constr_eq H' Hres | should_not_clear_requests H' | should_not_clear H' ] |
| 274 |
then fail |
| 275 |
else clear H' |
| 276 |
end; |
| 277 |
cbv zeta in *; |
| 278 |
repeat first [ progress destruct_head'_and |
| 279 |
| progress specialize_by_assumption ]; |
| 280 |
let f := match type of Hres with ?f = _ => head f end in |
| 281 |
try cbv [f] in *; |
| 282 |
hnf; |
| 283 |
PipelineTactics.do_unfolding; |
| 284 |
try (let m := match goal with m := _ - Associational.eval _ |- _ => m end in |
| 285 |
cbv [m] in * ); |
| 286 |
intros; |
| 287 |
try split; PipelineTactics.use_compilers_correctness Hres; |
| 288 |
[ pose_proof_length_list_Z_bounded_by; |
| 289 |
repeat first [ match goal with |- ?x = ?x => reflexivity end |
| 290 |
| progress autorewrite with interp_extra interp_gen_cache |
| 291 |
| progress autorewrite with push_eval |
| 292 |
| reflexivity |
| 293 |
| erewrite select_eq |
| 294 |
| progress autounfold with push_eval |
| 295 |
| progress autorewrite with distr_length in * |
| 296 |
| typeclasses eauto |
| 297 |
| solve [ eauto with zarith ] ] |
| 298 |
| .. ]. |
| 299 |
|
| 300 |
Ltac prove_correctness use_curve_good := prove_correctness' ltac:(fun _ => fail) use_curve_good. |
| 301 |
Ltac prove_pipeline_wf _ := PipelineTactics.prove_pipeline_wf (). |
| 302 |
|
| 303 |
Module CorrectnessStringification. |
| 304 |
Module dyn_context. |
| 305 |
Inductive list := |
| 306 |
| nil |
| 307 |
| cons {T1 T2} (k : T1) (v : T2) (ctx : list). |
| 308 |
End dyn_context. |
| 309 |
|
| 310 |
Ltac strip_bounds_info correctness := |
| 311 |
let rec is_known_non_bounds_equality_type T := |
| 312 |
lazymatch T with |
| 313 |
| Z => true |
| 314 |
| list Z => true |
| 315 |
| prod ?A ?B |
| 316 |
=> let a := is_known_non_bounds_equality_type A in |
| 317 |
let b := is_known_non_bounds_equality_type B in |
| 318 |
(eval cbv in (andb a b)) |
| 319 |
| _ => false |
| 320 |
end in |
| 321 |
lazymatch correctness with |
| 322 |
| (fun x : ?T => ?f) |
| 323 |
=> let fx := fresh in |
| 324 |
constr:(fun x : T => match f return _ with |
| 325 |
| fx => ltac:(let fx := (eval cbv [fx] in fx) in |
| 326 |
let v := strip_bounds_info fx in |
| 327 |
exact v) |
| 328 |
end) |
| 329 |
| ((lower ?r <=? ?v) && (?v <=? upper ?r))%bool%Z = true -> ?T |
| 330 |
=> strip_bounds_info T |
| 331 |
| list_Z_bounded_by _ _ -> ?T |
| 332 |
=> strip_bounds_info T |
| 333 |
| (_ = List.map (fun z => (_ mod _) / _) (List.seq _ _)) /\ (?a <= ?b < ?c) -> ?T |
| 334 |
=> let T := strip_bounds_info T in |
| 335 |
constr:(a <= b < c -> T) |
| 336 |
| ?T /\ list_Z_bounded_by _ _ |
| 337 |
=> T |
| 338 |
| ?T /\ (match _ with pair _ _ => _ end = true) |
| 339 |
=> T |
| 340 |
| ?T /\ ((lower ?r <=? ?v) && (?v <=? upper ?r))%bool%Z = true |
| 341 |
=> T |
| 342 |
| iff _ _ |
| 343 |
=> correctness |
| 344 |
| ?x = ?y /\ (_ = List.map (fun z => (_ mod _) / _) (List.seq _ _)) /\ (?a <= ?b < ?c) |
| 345 |
=> constr:(x = y /\ a <= b < c) |
| 346 |
| (_ = List.map (fun z => (_ mod _) / _) (List.seq _ _)) /\ (?a <= ?b < ?c) |
| 347 |
=> constr:(a <= b < c) |
| 348 |
| ?A /\ ?B |
| 349 |
=> let A := strip_bounds_info A in |
| 350 |
let B := strip_bounds_info B in |
| 351 |
constr:(A /\ B) |
| 352 |
| _ = _ :> ?T |
| 353 |
=> lazymatch is_known_non_bounds_equality_type T with |
| 354 |
| true => correctness |
| 355 |
| false |
| 356 |
=> constr_fail_with ltac:(fun _ => idtac "Unrecognized bounds component:" correctness "; unrecognized equality type" T; |
| 357 |
fail 1 "Unrecognized bounds component:" correctness "; unrecognized equality type" T) |
| 358 |
end |
| 359 |
| forall x : ?T, ?f |
| 360 |
=> let fx := fresh in |
| 361 |
constr:(forall x : T, match f return _ with |
| 362 |
| fx => ltac:(let fx := (eval cbv [fx] in fx) in |
| 363 |
let v := strip_bounds_info fx in |
| 364 |
exact v) |
| 365 |
end) |
| 366 |
| let x : ?T := ?v in ?f |
| 367 |
=> let fx := fresh in |
| 368 |
constr:(let x : T := v in |
| 369 |
match f return _ with |
| 370 |
| fx => ltac:(let fx := (eval cbv [fx] in fx) in |
| 371 |
let v := strip_bounds_info fx in |
| 372 |
exact v) |
| 373 |
end) |
| 374 |
| match ?v with pair a b => ?f end |
| 375 |
=> let fx := fresh in |
| 376 |
constr:(match v return _ with |
| 377 |
| pair a b |
| 378 |
=> match f return _ with |
| 379 |
| fx => ltac:(let fx := (eval cbv [fx] in fx) in |
| 380 |
let v := strip_bounds_info fx in |
| 381 |
exact v) |
| 382 |
end |
| 383 |
end) |
| 384 |
| _ |
| 385 |
=> constr_fail_with ltac:(fun _ => idtac "Unrecognized bounds component:" correctness; |
| 386 |
fail 1 "Unrecognized bounds component:" correctness) |
| 387 |
end. |
| 388 |
Ltac with_assoc_list ctx correctness arg_var_names out_var_names cont := |
| 389 |
lazymatch correctness with |
| 390 |
| (fun x : ?T => ?f) |
| 391 |
=> let fx := fresh in |
| 392 |
let x' := fresh in |
| 393 |
constr:(fun x : T |
| 394 |
=> match x, f return _ with |
| 395 |
| x', fx |
| 396 |
=> ltac:(let fx' := (eval cbv delta [fx] in fx) in |
| 397 |
let x := (eval cbv delta [x'] in x') in |
| 398 |
clear fx x'; |
| 399 |
let ret := with_assoc_list |
| 400 |
(dyn_context.cons x out_var_names ctx) |
| 401 |
fx' |
| 402 |
arg_var_names |
| 403 |
() |
| 404 |
cont in |
| 405 |
exact ret) |
| 406 |
end) |
| 407 |
| _ |
| 408 |
=> let T := type of arg_var_names in |
| 409 |
lazymatch (eval hnf in T) with |
| 410 |
| prod _ _ |
| 411 |
=> lazymatch correctness with |
| 412 |
| (forall x : ?T, ?f) |
| 413 |
=> let fx := fresh in |
| 414 |
let x' := fresh in |
| 415 |
constr:(fun x : T |
| 416 |
=> match x, f return _ with |
| 417 |
| x', fx |
| 418 |
=> ltac:(let fx' := (eval cbv delta [fx] in fx) in |
| 419 |
let x := (eval cbv delta [x'] in x') in |
| 420 |
clear fx x'; |
| 421 |
let ret := with_assoc_list |
| 422 |
(dyn_context.cons x (fst arg_var_names) ctx) |
| 423 |
fx' |
| 424 |
(snd arg_var_names) |
| 425 |
out_var_names |
| 426 |
cont in |
| 427 |
exact ret) |
| 428 |
end) |
| 429 |
| ?T |
| 430 |
=> cont ctx T |
| 431 |
end |
| 432 |
| _ => cont ctx correctness |
| 433 |
end |
| 434 |
end. |
| 435 |
Ltac to_level lvl := |
| 436 |
lazymatch type of lvl with |
| 437 |
| Z => constr:(Level.level lvl) |
| 438 |
| _ => lvl |
| 439 |
end. |
| 440 |
|
| 441 |
Ltac maybe_parenthesize str natural cur_lvl := |
| 442 |
let natural := to_level natural in |
| 443 |
let cur_lvl := to_level cur_lvl in |
| 444 |
let should_paren := (eval cbv in (Level.ltb cur_lvl natural)) in |
| 445 |
lazymatch should_paren with |
| 446 |
| true => constr:(("(" ++ str ++ ")")%string) |
| 447 |
| false => str |
| 448 |
end. |
| 449 |
|
| 450 |
Ltac find_head_in_ctx' ctx x cont := |
| 451 |
let h := head x in |
| 452 |
lazymatch ctx with |
| 453 |
| context[dyn_context.cons h ?name _] => cont name |
| 454 |
| context[dyn_context.cons x ?name _] => cont name |
| 455 |
| _ => lazymatch x with |
| 456 |
| fst ?x |
| 457 |
=> find_head_in_ctx' ctx x ltac:(fun x => cont (fst x)) |
| 458 |
| snd ?x |
| 459 |
=> find_head_in_ctx' ctx x ltac:(fun x => cont (snd x)) |
| 460 |
| _ => constr:(@None string) |
| 461 |
end |
| 462 |
end. |
| 463 |
Ltac find_head_in_ctx ctx x := |
| 464 |
find_head_in_ctx' ctx x ltac:(fun x => constr:(Some x)). |
| 465 |
|
| 466 |
Ltac find_in_ctx' ctx x cont := |
| 467 |
lazymatch ctx with |
| 468 |
| context[dyn_context.cons x ?name _] => cont name |
| 469 |
| _ => lazymatch x with |
| 470 |
| fst ?x |
| 471 |
=> find_in_ctx' ctx x ltac:(fun x => cont (fst x)) |
| 472 |
| snd ?x |
| 473 |
=> find_in_ctx' ctx x ltac:(fun x => cont (snd x)) |
| 474 |
| _ => constr:(@None string) |
| 475 |
end |
| 476 |
end. |
| 477 |
Ltac find_in_ctx ctx x := |
| 478 |
find_in_ctx' ctx x ltac:(fun x => constr:(Some x)). |
| 479 |
|
| 480 |
Ltac test_is_var_or_const v := |
| 481 |
constr:(ltac:(tryif first [ is_var v | is_const v ] then exact true else exact false)). |
| 482 |
Local Open Scope string_scope. |
| 483 |
|
| 484 |
Ltac fresh_from' ctx check_list start_val := |
| 485 |
lazymatch check_list with |
| 486 |
| cons ?n ?check_list |
| 487 |
=> lazymatch ctx with |
| 488 |
| context[dyn_context.cons _ n] |
| 489 |
=> fresh_from' ctx check_list start_val |
| 490 |
| _ => n |
| 491 |
end |
| 492 |
| _ |
| 493 |
=> let n := (eval cbv in ("x" ++ Decimal.Z.to_string start_val)) in |
| 494 |
lazymatch ctx with |
| 495 |
| context[dyn_context.cons _ n] |
| 496 |
=> fresh_from' ctx check_list (Z.succ start_val) |
| 497 |
| _ => n |
| 498 |
end |
| 499 |
end. |
| 500 |
|
| 501 |
Ltac fresh_from ctx := fresh_from' ctx ["x"; "y"; "z"] 0%Z. |
| 502 |
|
| 503 |
Ltac stringify_function_binders ctx correctness stringify_body := |
| 504 |
lazymatch correctness with |
| 505 |
| (fun x : ?T => ?f) |
| 506 |
=> let fx := fresh in |
| 507 |
let x' := fresh in |
| 508 |
let xn := fresh_from ctx in |
| 509 |
lazymatch |
| 510 |
constr:( |
| 511 |
fun x : T |
| 512 |
=> match x, f return string with |
| 513 |
| x', fx |
| 514 |
=> ltac:( |
| 515 |
let fx' := (eval cbv delta [fx] in fx) in |
| 516 |
let x := (eval cbv delta [x'] in x') in |
| 517 |
clear fx x'; |
| 518 |
let res := stringify_function_binders |
| 519 |
(dyn_context.cons x xn ctx) |
| 520 |
fx' |
| 521 |
stringify_body in |
| 522 |
exact (" " ++ xn ++ res)) |
| 523 |
end) with |
| 524 |
| fun _ => ?f => f |
| 525 |
| ?F => constr_fail_with ltac:(fun _ => idtac "Failed to eliminate functional dependencies in" F; |
| 526 |
fail 1 "Failed to eliminate functional dependencies in" F) |
| 527 |
end |
| 528 |
| ?v => let res := stringify_body ctx v in |
| 529 |
constr:(", " ++ res) |
| 530 |
end. |
| 531 |
|
| 532 |
Ltac is_literal x := |
| 533 |
lazymatch x with |
| 534 |
| O => true |
| 535 |
| S ?x => is_literal x |
| 536 |
| _ => false |
| 537 |
end. |
| 538 |
Ltac eta_match v := |
| 539 |
let recr_if_not_same v' := |
| 540 |
lazymatch v' with |
| 541 |
| v => v' |
| 542 |
| _ => eta_match v' |
| 543 |
end in |
| 544 |
lazymatch v with |
| 545 |
| match ?x with pair a b => ?f end |
| 546 |
=> recr_if_not_same (match fst x, snd x with a, b => f end) |
| 547 |
| fst (?x, ?y) => eta_match x |
| 548 |
| snd (?x, ?y) => eta_match y |
| 549 |
| match ?x with true => (?ta, ?tb) | false => (?fa, ?fb) end |
| 550 |
=> eta_match (if x then ta else fa, if x then tb else fb) |
| 551 |
| @fst ?A ?B ?x |
| 552 |
=> let x := eta_match x in |
| 553 |
recr_if_not_same (@fst A B x) |
| 554 |
| @snd ?A ?B ?x |
| 555 |
=> let x := eta_match x in |
| 556 |
recr_if_not_same (@snd A B x) |
| 557 |
| _ => v |
| 558 |
end. |
| 559 |
|
| 560 |
Ltac stringify_rec0 ctx correctness lvl := |
| 561 |
let lvl := to_level lvl in |
| 562 |
let term_lvl := constr:(term_lvl) in |
| 563 |
let rel_lvl := constr:(rel_lvl) in |
| 564 |
let next_rel_lvl := constr:(Level.next rel_lvl) in |
| 565 |
let app_lvl := constr:(app_lvl) in |
| 566 |
let next_app_lvl := constr:(Level.next app_lvl) in |
| 567 |
let correctness := eta_match correctness in |
| 568 |
let recurse v lvl := stringify_rec0 ctx v lvl in |
| 569 |
let name_of_var := find_head_in_ctx ctx correctness in |
| 570 |
let stringify_if testv t f := |
| 571 |
let stest := recurse testv term_lvl in |
| 572 |
let st := recurse t term_lvl in |
| 573 |
let sf := recurse f term_lvl in |
| 574 |
maybe_parenthesize (("if " ++ stest ++ " then " ++ st ++ " else " ++ sf)%string) term_lvl lvl in |
| 575 |
let show_Z _ := constr:(Decimal.show_lvl_Z correctness lvl) in |
| 576 |
let show_nat _ := constr:(Decimal.show_lvl_nat correctness lvl) in |
| 577 |
let show_list_Z_Z _ := constr:(@show_lvl_list _ (@show_lvl_prod _ _ Decimal.show_lvl_Z Decimal.show_lvl_Z) correctness lvl) in |
| 578 |
let stringify_prefix f natural arg_lvl := |
| 579 |
lazymatch correctness with |
| 580 |
| ?F ?x |
| 581 |
=> let sx := recurse x arg_lvl in |
| 582 |
maybe_parenthesize (f ++ sx)%string natural lvl |
| 583 |
end in |
| 584 |
let stringify_postfix f natural arg_lvl := |
| 585 |
lazymatch correctness with |
| 586 |
| ?F ?x |
| 587 |
=> let sx := recurse x arg_lvl in |
| 588 |
maybe_parenthesize (sx ++ f)%string natural lvl |
| 589 |
end in |
| 590 |
let stringify_infix' lvl space f natural l_lvl r_lvl := |
| 591 |
lazymatch correctness with |
| 592 |
| ?F ?x ?y |
| 593 |
=> let sx := recurse x l_lvl in |
| 594 |
let sy := recurse y r_lvl in |
| 595 |
maybe_parenthesize (sx ++ space ++ f ++ space ++ sy)%string natural lvl |
| 596 |
end in |
| 597 |
let stringify_infix := stringify_infix' lvl " " in |
| 598 |
let stringify_infix_without_space := stringify_infix' lvl "" in |
| 599 |
let stringify_infix2 f1 f2 natural l_lvl c_lvl r_lvl := |
| 600 |
lazymatch correctness with |
| 601 |
| and (?F1 ?x ?y) (?F2 ?y ?z) |
| 602 |
=> let sx := recurse x l_lvl in |
| 603 |
let sy := recurse y c_lvl in |
| 604 |
let sz := recurse z r_lvl in |
| 605 |
maybe_parenthesize (sx ++ " " ++ f1 ++ " " ++ sy ++ " " ++ f2 ++ " " ++ sz)%string natural lvl |
| 606 |
end in |
| 607 |
let name_of_fun := |
| 608 |
lazymatch correctness with |
| 609 |
| ?f ?x => find_in_ctx ctx f |
| 610 |
| _ => constr:(@None string) |
| 611 |
end in |
| 612 |
lazymatch constr:((name_of_var, name_of_fun)) with |
| 613 |
| (Some ?name, _) |
| 614 |
=> maybe_parenthesize name (Level.level 1) lvl |
| 615 |
| (None, Some ?name) |
| 616 |
=> lazymatch correctness with |
| 617 |
| ?f ?x |
| 618 |
=> let sx := recurse x (Level.next app_lvl) in |
| 619 |
maybe_parenthesize ((name ++ " " ++ sx)%string) constr:(app_lvl) lvl |
| 620 |
end |
| 621 |
| (None, None) |
| 622 |
=> lazymatch correctness with |
| 623 |
| ?x = ?y :> ?T |
| 624 |
=> lazymatch (eval cbv in T) with |
| 625 |
| Z => let sx := recurse x next_rel_lvl in |
| 626 |
let sy := recurse y next_rel_lvl in |
| 627 |
maybe_parenthesize ((sx ++ " = " ++ sy)%string) rel_lvl lvl |
| 628 |
| list Z |
| 629 |
=> let sx := recurse x next_rel_lvl in |
| 630 |
let sy := recurse y next_rel_lvl in |
| 631 |
maybe_parenthesize ((sx ++ " = " ++ sy)%string) rel_lvl lvl |
| 632 |
| prod ?A ?B |
| 633 |
=> let v := (eval cbn [fst snd] in (fst x = fst y /\ snd x = snd y)) in |
| 634 |
recurse v lvl |
| 635 |
| ?T' => constr_fail_with ltac:(fun _ => idtac "Error: Unrecognized type for equality:" T'; |
| 636 |
fail 1 "Error: Unrecognized type for equality:" T') |
| 637 |
end |
| 638 |
| eval (weight 8 1) _ ?v |
| 639 |
=> let sv := recurse v 9 in |
| 640 |
maybe_parenthesize (("bytes_eval " ++ sv)%string) app_lvl lvl |
| 641 |
| Associational.eval ?c |
| 642 |
=> let sc := recurse c 9 in |
| 643 |
maybe_parenthesize (("Associational.eval " ++ sc)%string) app_lvl lvl |
| 644 |
| uweight ?machine_wordsize ?v |
| 645 |
=> recurse (2^(machine_wordsize * Z.of_nat v)) lvl |
| 646 |
| weight 8 1 ?i |
| 647 |
=> recurse (2^(8 * Z.of_nat i)) lvl |
| 648 |
| List.map ?x ?y |
| 649 |
=> let sx := recurse x next_app_lvl in |
| 650 |
let sy := recurse y next_app_lvl in |
| 651 |
maybe_parenthesize (("map " ++ sx ++ " " ++ sy)%string) app_lvl lvl |
| 652 |
| match ?testv with true => ?t | false => ?f end |
| 653 |
=> stringify_if testv t f |
| 654 |
| match ?testv with or_introl _ => ?t | or_intror _ => ?f end |
| 655 |
=> stringify_if testv t f |
| 656 |
| match ?testv with left _ => ?t | right _ => ?f end |
| 657 |
=> stringify_if testv t f |
| 658 |
| Crypto.Util.Decidable.dec ?p |
| 659 |
=> recurse p lvl |
| 660 |
| Z.odd ?x |
| 661 |
=> let sx := recurse x next_app_lvl in |
| 662 |
maybe_parenthesize ((sx ++ " is odd")%string) app_lvl lvl |
| 663 |
| Z0 => show_Z () |
| 664 |
| Zpos _ => show_Z () |
| 665 |
| Zneg _ => show_Z () |
| 666 |
| O => show_nat () |
| 667 |
| S ?x |
| 668 |
=> let is_lit := is_literal x in |
| 669 |
lazymatch is_lit with |
| 670 |
| true => show_nat () |
| 671 |
| false => recurse (x + 1)%nat lvl |
| 672 |
end |
| 673 |
| Z.of_nat ?x => recurse x lvl |
| 674 |
| ?x <= ?y < ?z => stringify_infix2 "≤" "<" rel_lvl next_rel_lvl next_rel_lvl next_rel_lvl |
| 675 |
| ?x <= ?y <= ?z => stringify_infix2 "≤" "≤" rel_lvl next_rel_lvl next_rel_lvl next_rel_lvl |
| 676 |
| ?x < ?y <= ?z => stringify_infix2 "<" "≤" rel_lvl next_rel_lvl next_rel_lvl next_rel_lvl |
| 677 |
| ?x < ?y < ?z => stringify_infix2 "<" "<" rel_lvl next_rel_lvl next_rel_lvl next_rel_lvl |
| 678 |
| iff _ _ => stringify_infix "↔" 95 94 94 |
| 679 |
| and _ _ => stringify_infix "∧" 80 80 80 |
| 680 |
| andb _ _ => stringify_infix "∧" 80 80 80 |
| 681 |
| Z.modulo _ _ => stringify_infix "mod" 41 39 39 |
| 682 |
| Z.mul _ _ => stringify_infix "*" 40 40 39 |
| 683 |
| Z.pow _ _ => stringify_infix_without_space "^" 30 29 30 |
| 684 |
| Z.add _ _ => stringify_infix "+" 50 50 49 |
| 685 |
| Z.sub _ _ => stringify_infix "-" 50 50 49 |
| 686 |
| Z.opp _ => stringify_prefix "-" 35 35 |
| 687 |
| Z.le _ _ => stringify_infix "≤" rel_lvl next_rel_lvl next_rel_lvl |
| 688 |
| Z.lt _ _ => stringify_infix "<" rel_lvl next_rel_lvl next_rel_lvl |
| 689 |
| Z.leb _ _ => stringify_infix "≤" rel_lvl next_rel_lvl next_rel_lvl |
| 690 |
| Z.ltb _ _ => stringify_infix "<" rel_lvl next_rel_lvl next_rel_lvl |
| 691 |
| Nat.mul _ _ => stringify_infix "*" 40 40 39 |
| 692 |
| Nat.pow _ _ => stringify_infix "^" 30 29 30 |
| 693 |
| Nat.add _ _ => stringify_infix "+" 50 50 49 |
| 694 |
| Nat.sub _ _ => stringify_infix "-â„•" 50 50 49 |
| 695 |
| Z.div _ _ |
| 696 |
=> let res := stringify_infix' next_rel_lvl " " "/" 40 40 39 in |
| 697 |
maybe_parenthesize ("⌊" ++ res ++ "⌋") next_app_lvl lvl |
| 698 |
| List.seq ?x ?y |
| 699 |
=> let sx := recurse x next_app_lvl in |
| 700 |
let sy := recurse (pred y) next_app_lvl in |
| 701 |
constr:("[" ++ sx ++ ".." ++ sy ++ "]") |
| 702 |
| pred ?n |
| 703 |
=> let iv := test_is_var_or_const n in |
| 704 |
let il := is_literal n in |
| 705 |
lazymatch (eval cbv in (orb il iv)) with |
| 706 |
| true => show_nat () |
| 707 |
| false |
| 708 |
=> recurse (n - 1)%nat lvl |
| 709 |
end |
| 710 |
| fun x : ?T => ?f |
| 711 |
=> let slam := stringify_function_binders ctx correctness ltac:(fun ctx body => stringify_rec0 ctx body term_lvl) in |
| 712 |
maybe_parenthesize ("λ" ++ slam) term_lvl lvl |
| 713 |
| ?v |
| 714 |
=> let iv := test_is_var_or_const v in |
| 715 |
lazymatch iv with |
| 716 |
| true |
| 717 |
=> let T := type of v in |
| 718 |
lazymatch (eval hnf in T) with |
| 719 |
| Z => show_Z () |
| 720 |
| nat => show_nat () |
| 721 |
| list (Z * Z) => show_list_Z_Z () |
| 722 |
| _ |
| 723 |
=> constr_fail_with ltac:(fun _ => idtac "Error: Unrecognized var:" v " in " ctx; |
| 724 |
fail 1 "Error: Unrecognized var:" v " in " ctx) |
| 725 |
end |
| 726 |
| false |
| 727 |
=> constr_fail_with ltac:(fun _ => idtac "Error: Unrecognized term:" v " in " ctx; |
| 728 |
fail 1 "Error: Unrecognized term:" v " in " ctx) |
| 729 |
end |
| 730 |
end |
| 731 |
end. |
| 732 |
|
| 733 |
Ltac stringify_preconditions ctx so_far_rev correctness := |
| 734 |
let correctness := eta_match correctness in |
| 735 |
let recurse so_far_rev v := stringify_preconditions ctx so_far_rev v in |
| 736 |
lazymatch correctness with |
| 737 |
| ?A -> ?B |
| 738 |
=> let sA := stringify_rec0 ctx A constr:(term_lvl) in |
| 739 |
recurse (sA :: so_far_rev) B |
| 740 |
| ?T |
| 741 |
=> let so_far := (eval cbv [List.rev] in (List.rev so_far_rev)) in |
| 742 |
constr:((so_far, T)) |
| 743 |
end. |
| 744 |
|
| 745 |
Ltac stringify_postconditions ctx correctness := |
| 746 |
let correctness := eta_match correctness in |
| 747 |
let recurse v := stringify_postconditions ctx v in |
| 748 |
let default _ := let v := stringify_rec0 ctx correctness constr:(term_lvl) in |
| 749 |
constr:(v::nil) in |
| 750 |
lazymatch correctness with |
| 751 |
| _ <= _ < _ => default () |
| 752 |
| _ <= _ <= _ => default () |
| 753 |
| _ < _ <= _ => default () |
| 754 |
| _ < _ < _ => default () |
| 755 |
| and ?A ?B |
| 756 |
=> let sA := recurse A in |
| 757 |
let sB := recurse B in |
| 758 |
(eval cbv [List.app] in (List.app sA sB)) |
| 759 |
| ?x = ?y :> prod ?A ?B |
| 760 |
=> let v := (eval cbn [fst snd] in (fst x = fst y /\ snd x = snd y)) in |
| 761 |
recurse v |
| 762 |
| _ |
| 763 |
=> default () |
| 764 |
end. |
| 765 |
|
| 766 |
Ltac stringify_rec ctx correctness := |
| 767 |
let pre_rest := stringify_preconditions ctx (@nil string) correctness in |
| 768 |
lazymatch pre_rest with |
| 769 |
| (?preconditions, ?correctness) |
| 770 |
=> let postconditions := stringify_postconditions ctx correctness in |
| 771 |
let preconditions_list_string |
| 772 |
:= lazymatch preconditions with |
| 773 |
| nil => constr:(@nil string) |
| 774 |
| _ => constr:((["Preconditions:"] |
| 775 |
++ List.map (fun s => " " ++ s)%string preconditions)%list%string) |
| 776 |
end in |
| 777 |
let postconditions_list_string |
| 778 |
:= constr:((["Postconditions:"] |
| 779 |
++ List.map (fun s => " " ++ s)%string postconditions)%list%string) in |
| 780 |
(eval cbv [List.map List.app] in |
| 781 |
([""] ++ preconditions_list_string ++ postconditions_list_string ++ [""])%list%string) |
| 782 |
end. |
| 783 |
|
| 784 |
Ltac strip_lambdas v := |
| 785 |
lazymatch v with |
| 786 |
| fun _ => ?f => strip_lambdas f |
| 787 |
| ?v => v |
| 788 |
end. |
| 789 |
|
| 790 |
Ltac stringify ctx correctness fname arg_var_data out_var_data := |
| 791 |
let G := match goal with |- ?G => G end in |
| 792 |
let correctness := (eval hnf in correctness) in |
| 793 |
let correctness := (eval cbv [Partition.partition WordByWordMontgomery.valid WordByWordMontgomery.small] in correctness) in |
| 794 |
let correctness := strip_bounds_info correctness in |
| 795 |
let arg_var_names := constr:(type.map_for_each_lhs_of_arrow (@ToString.OfPHOAS.names_of_var_data) arg_var_data) in |
| 796 |
let out_var_names := constr:(ToString.OfPHOAS.names_of_base_var_data out_var_data) in |
| 797 |
let res := with_assoc_list |
| 798 |
ctx |
| 799 |
correctness |
| 800 |
arg_var_names |
| 801 |
out_var_names |
| 802 |
ltac:( |
| 803 |
fun ctx T |
| 804 |
=> let v := stringify_rec ctx T in refine v |
| 805 |
) in |
| 806 |
let res := strip_lambdas res in |
| 807 |
res. |
| 808 |
|
| 809 |
Notation docstring_with_summary_from_lemma_with_ctx ctx summary correctness |
| 810 |
:= (fun fname arg_var_data out_var_data |
| 811 |
=> match ctx, summary%list, correctness return _ with |
| 812 |
| ctx', summary', correctness' |
| 813 |
=> ltac:(let ctx := (eval cbv delta [ctx'] in ctx') in |
| 814 |
let summary := (eval cbv delta [summary'] in summary') in |
| 815 |
let correctness := (eval cbv delta [correctness'] in correctness') in |
| 816 |
clear ctx' summary' correctness'; |
| 817 |
let res := stringify ctx correctness fname arg_var_data out_var_data in |
| 818 |
refine (List.app (summary fname) res)) |
| 819 |
end) (only parsing). |
| 820 |
Notation docstring_with_summary_from_lemma summary correctness |
| 821 |
:= (match dyn_context.nil with |
| 822 |
| ctx' => docstring_with_summary_from_lemma_with_ctx ctx' summary correctness |
| 823 |
end) |
| 824 |
(only parsing). |
| 825 |
End CorrectnessStringification. |
| 826 |
Notation "'docstring_with_summary_from_lemma_with_ctx!' ctx summary correctness" |
| 827 |
:= (CorrectnessStringification.docstring_with_summary_from_lemma_with_ctx ctx summary correctness) (only parsing, at level 10, ctx at next level, summary at next level, correctness at next level). |
| 828 |
Notation "'docstring_with_summary_from_lemma!' summary correctness" |
| 829 |
:= (CorrectnessStringification.docstring_with_summary_from_lemma summary correctness) (only parsing, at level 10, summary at next level, correctness at next level). |
| 830 |
(** Used to sigma up the output of stringified pipelines *) |
| 831 |
Notation wrap_s v := (fun s => existT (fun t => prod string (Pipeline.M (Pipeline.ExtendedSynthesisResult t))) _ (v s)) (only parsing). |
| 832 |
|
| 833 |
(** We stick this in a module to work around COQBUG(https://github.com/coq/coq/issues/16677) *) |
| 834 |
Module Export Options. |
| 835 |
Export Assembly.Equivalence.Options. |
| 836 |
(** The options which are passed from higher levels for synthesizing |
| 837 |
individual pipeline operations as an AST *) |
| 838 |
Class PipelineOptions := |
| 839 |
{ absint_opts : AbstractInterpretation.Options |
| 840 |
; widen_carry : widen_carry_opt |
| 841 |
; widen_bytes : widen_bytes_opt |
| 842 |
; unfold_value_barrier : unfold_value_barrier_opt |
| 843 |
; should_split_multiret : should_split_multiret_opt |
| 844 |
; should_split_mul : should_split_mul_opt |
| 845 |
; output_options : output_options_opt |
| 846 |
; only_signed : only_signed_opt |
| 847 |
; no_select : no_select_opt |
| 848 |
; low_level_rewriter_method : low_level_rewriter_method_opt |
| 849 |
; debug_rewriting : debug_rewriting_opt |
| 850 |
}. |
| 851 |
Definition default_PipelineOptions : PipelineOptions |
| 852 |
:= let _ := Pipeline.default_BoundsPipelineOptions in |
| 853 |
{| widen_carry := false |
| 854 |
; widen_bytes := false |
| 855 |
; should_split_multiret := false |
| 856 |
; should_split_mul := false |
| 857 |
; no_select := false |
| 858 |
; output_options := default_output_options |}. |
| 859 |
(** The additional options which are passed from higher levels for |
| 860 |
stringifying synthesized operations. We do not include |
| 861 |
[OutputLanguageAPI] for lack of a good default. *) |
| 862 |
Class PipelineToStringOptions := |
| 863 |
{ static : static_opt |
| 864 |
; language_naming_conventions : language_naming_conventions_opt |
| 865 |
; internal_static : internal_static_opt |
| 866 |
; inline_internal : inline_internal_opt |
| 867 |
; inline : inline_opt |
| 868 |
; documentation_options : documentation_options_opt }. |
| 869 |
Definition default_PipelineToStringOptions : PipelineToStringOptions := |
| 870 |
{| static := false |
| 871 |
; language_naming_conventions := default_language_naming_conventions |
| 872 |
; internal_static := false |
| 873 |
; inline_internal := false |
| 874 |
; inline := false |
| 875 |
; documentation_options := default_documentation_options |}. |
| 876 |
(** The additional options used for invoking [Synthesize], the entire |
| 877 |
field pipeline *) |
| 878 |
Class SynthesisOptions := |
| 879 |
{ use_mul_for_cmovznz : use_mul_for_cmovznz_opt |
| 880 |
; package_namev : package_name_opt |
| 881 |
; ignore_unique_asm_names : ignore_unique_asm_names_opt |
| 882 |
; error_on_unused_assembly_functions : error_on_unused_assembly_functions_opt |
| 883 |
; emit_primitives : emit_primitives_opt |
| 884 |
; class_namev : class_name_opt |
| 885 |
; assembly_hints_lines : assembly_hints_lines_opt |
| 886 |
; equivalence_checker_options : equivalence_checker_options_opt }. |
| 887 |
Definition default_SynthesisOptions : SynthesisOptions := |
| 888 |
{| use_mul_for_cmovznz := false |
| 889 |
; package_namev := None |
| 890 |
; ignore_unique_asm_names := true |
| 891 |
; error_on_unused_assembly_functions := true |
| 892 |
; emit_primitives := true |
| 893 |
; class_namev := None |
| 894 |
; assembly_hints_lines := [] |
| 895 |
; equivalence_checker_options := default_equivalence_checker_options |}. |
| 896 |
End Options. |
| 897 |
Module Import PipelineHints. |
| 898 |
Export EquivalenceCheckerHints. |
| 899 |
Global Existing Instances |
| 900 |
Build_PipelineOptions |
| 901 |
Build_PipelineToStringOptions |
| 902 |
Build_SynthesisOptions |
| 903 |
absint_opts |
| 904 |
widen_carry |
| 905 |
widen_bytes |
| 906 |
unfold_value_barrier |
| 907 |
should_split_multiret |
| 908 |
should_split_mul |
| 909 |
output_options |
| 910 |
only_signed |
| 911 |
no_select |
| 912 |
low_level_rewriter_method |
| 913 |
debug_rewriting |
| 914 |
static |
| 915 |
language_naming_conventions |
| 916 |
internal_static |
| 917 |
inline_internal |
| 918 |
inline |
| 919 |
documentation_options |
| 920 |
use_mul_for_cmovznz |
| 921 |
package_namev |
| 922 |
ignore_unique_asm_names |
| 923 |
error_on_unused_assembly_functions |
| 924 |
emit_primitives |
| 925 |
class_namev |
| 926 |
assembly_hints_lines |
| 927 |
equivalence_checker_options |
| 928 |
. |
| 929 |
#[global] |
| 930 |
Hint Cut [ |
| 931 |
( _ * ) |
| 932 |
(absint_opts |
| 933 |
| widen_carry |
| 934 |
| widen_bytes |
| 935 |
| unfold_value_barrier |
| 936 |
| should_split_multiret |
| 937 |
| should_split_mul |
| 938 |
| output_options |
| 939 |
| only_signed |
| 940 |
| no_select |
| 941 |
| low_level_rewriter_method |
| 942 |
| debug_rewriting |
| 943 |
| static |
| 944 |
| language_naming_conventions |
| 945 |
| internal_static |
| 946 |
| inline_internal |
| 947 |
| inline |
| 948 |
| documentation_options |
| 949 |
| use_mul_for_cmovznz |
| 950 |
| package_namev |
| 951 |
| ignore_unique_asm_names |
| 952 |
| error_on_unused_assembly_functions |
| 953 |
| emit_primitives |
| 954 |
| class_namev |
| 955 |
| assembly_hints_lines |
| 956 |
| equivalence_checker_options |
| 957 |
) ( _ * ) |
| 958 |
(Build_PipelineOptions |
| 959 |
| Build_PipelineToStringOptions |
| 960 |
| Build_SynthesisOptions |
| 961 |
) |
| 962 |
] : typeclass_instances. |
| 963 |
End PipelineHints. |
| 964 |
|
| 965 |
Section __. |
| 966 |
Context {output_language_api : ToString.OutputLanguageAPI} |
| 967 |
{pipeline_opts : PipelineOptions} |
| 968 |
{pipeline_to_string_opts : PipelineToStringOptions} |
| 969 |
{synthesis_opts : SynthesisOptions} |
| 970 |
(n : nat) |
| 971 |
(machine_wordsize : machine_wordsize_opt). |
| 972 |
|
| 973 |
Definition word_bound : ZRange.type.interp base.type.Z |
| 974 |
:= r[0 ~> 2^machine_wordsize-1]%zrange. |
| 975 |
Definition saturated_bounds : list (ZRange.type.option.interp base.type.Z) |
| 976 |
:= List.repeat (Some word_bound) n. |
| 977 |
(* We include [0], so that even after bounds relaxation, we can |
| 978 |
notice where the constant 0s are, and remove them. *) |
| 979 |
Definition possible_values_of_machine_wordsize |
| 980 |
:= prefix_with_carry [machine_wordsize; 2 * machine_wordsize]%Z. |
| 981 |
Definition possible_values_of_machine_wordsize_with_bytes |
| 982 |
:= prefix_with_carry_bytes [machine_wordsize; 2 * machine_wordsize]%Z. |
| 983 |
Let possible_values := possible_values_of_machine_wordsize. |
| 984 |
Let possible_values_with_bytes := possible_values_of_machine_wordsize_with_bytes. |
| 985 |
Local Existing Instance default_translate_to_fancy. |
| 986 |
Local Instance no_select_size : no_select_size_opt := no_select_size_of_no_select machine_wordsize. |
| 987 |
Local Instance split_mul_to : split_mul_to_opt := split_mul_to_of_should_split_mul machine_wordsize possible_values. |
| 988 |
Local Instance split_multiret_to : split_multiret_to_opt := split_multiret_to_of_should_split_multiret machine_wordsize possible_values. |
| 989 |
(** We override this instance with a version that does not avoid |
| 990 |
uint1, so that the primitive operations, which in fact do use |
| 991 |
(>>) to get carries, don't have issues *) |
| 992 |
Local Instance absint_opts_with_no_shiftr_avoid_uint1 : AbstractInterpretation.Options |
| 993 |
:= {| AbstractInterpretation.shiftr_avoid_uint1 := false |}. |
| 994 |
Local Notation adc_sbb_return_carry_range s |
| 995 |
:= ((if List.existsb (Z.eqb s) relax_adc_sbb_return_carry_to_bitwidth then r[0~>2^s%Z-1] else r[0~>1])%zrange). |
| 996 |
Lemma length_saturated_bounds : List.length saturated_bounds = n. |
| 997 |
Proof using Type. cbv [saturated_bounds]; now autorewrite with distr_length. Qed. |
| 998 |
Hint Rewrite length_saturated_bounds : distr_length. |
| 999 |
Local Notation dummy_weight := (weight 0 0). |
| 1000 |
Local Notation evalf := (eval dummy_weight n). |
| 1001 |
Local Notation notations_for_docstring |
| 1002 |
:= (CorrectnessStringification.dyn_context.cons |
| 1003 |
evalf "eval" |
| 1004 |
CorrectnessStringification.dyn_context.nil)%string. |
| 1005 |
Local Notation "'docstring_with_summary_from_lemma!' summary correctness" |
| 1006 |
:= (docstring_with_summary_from_lemma_with_ctx! |
| 1007 |
notations_for_docstring |
| 1008 |
summary |
| 1009 |
correctness) |
| 1010 |
(only parsing, at level 10, summary at next level, correctness at next level). |
| 1011 |
Definition selectznz |
| 1012 |
:= Pipeline.BoundsPipeline |
| 1013 |
false (* subst01 *) |
| 1014 |
possible_values_with_bytes |
| 1015 |
reified_selectznz_gen |
| 1016 |
(Some r[0~>1], (Some saturated_bounds, (Some saturated_bounds, tt)))%zrange |
| 1017 |
(Some saturated_bounds). |
| 1018 |
Definition sselectznz (prefix : string) |
| 1019 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 1020 |
:= Eval cbv beta in |
| 1021 |
FromPipelineToString! |
| 1022 |
machine_wordsize prefix "selectznz" selectznz |
| 1023 |
(docstring_with_summary_from_lemma! |
| 1024 |
(fun fname : string => [text_before_function_name ++ fname ++ " is a multi-limb conditional select."]%string) |
| 1025 |
(selectznz_correct saturated_bounds)). |
| 1026 |
|
| 1027 |
Definition mulx (s : Z) |
| 1028 |
:= Pipeline.BoundsPipeline |
| 1029 |
false (* subst01 *) |
| 1030 |
possible_values_with_bytes |
| 1031 |
(reified_mulx_gen |
| 1032 |
@ GallinaReify.Reify s) |
| 1033 |
(Some r[0~>2^s-1], (Some r[0~>2^s-1], tt))%zrange |
| 1034 |
(Some r[0~>2^s-1], Some r[0~>2^s-1])%zrange. |
| 1035 |
Definition smulx (prefix : string) (s : Z) |
| 1036 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 1037 |
:= Eval cbv beta in |
| 1038 |
FromPipelineToInternalString! |
| 1039 |
machine_wordsize prefix ("mulx_u" ++ Decimal.Z.to_string s)%string (mulx s) |
| 1040 |
(docstring_with_summary_from_lemma! |
| 1041 |
(fun fname : string => [text_before_function_name ++ fname ++ " is a multiplication, returning the full double-width result."]%string) |
| 1042 |
(mulx_correct s)). |
| 1043 |
Definition addcarryx (s : Z) |
| 1044 |
:= Pipeline.BoundsPipeline |
| 1045 |
false (* subst01 *) |
| 1046 |
possible_values_with_bytes |
| 1047 |
(reified_addcarryx_gen |
| 1048 |
@ GallinaReify.Reify s) |
| 1049 |
(Some r[0~>1], (Some r[0~>2^s-1], (Some r[0~>2^s-1], tt)))%zrange |
| 1050 |
(Some r[0~>2^s-1], Some (adc_sbb_return_carry_range s))%zrange. |
| 1051 |
Definition saddcarryx (prefix : string) (s : Z) |
| 1052 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 1053 |
:= Eval cbv beta in |
| 1054 |
FromPipelineToInternalString! |
| 1055 |
machine_wordsize prefix ("addcarryx_u" ++ Decimal.Z.to_string s)%string (addcarryx s) |
| 1056 |
(docstring_with_summary_from_lemma! |
| 1057 |
(fun fname : string => [text_before_function_name ++ fname ++ " is an addition with carry."]%string) |
| 1058 |
(addcarryx_correct relax_adc_sbb_return_carry_to_bitwidth s)). |
| 1059 |
|
| 1060 |
Definition subborrowx (s : Z) |
| 1061 |
:= Pipeline.BoundsPipeline |
| 1062 |
false (* subst01 *) |
| 1063 |
possible_values_with_bytes |
| 1064 |
(reified_subborrowx_gen |
| 1065 |
@ GallinaReify.Reify s) |
| 1066 |
(Some r[0~>1], (Some r[0~>2^s-1], (Some r[0~>2^s-1], tt)))%zrange |
| 1067 |
(Some r[0~>2^s-1], Some (adc_sbb_return_carry_range s))%zrange. |
| 1068 |
Definition ssubborrowx (prefix : string) (s : Z) |
| 1069 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 1070 |
:= Eval cbv beta in |
| 1071 |
FromPipelineToInternalString! |
| 1072 |
machine_wordsize prefix ("subborrowx_u" ++ Decimal.Z.to_string s)%string (subborrowx s) |
| 1073 |
(docstring_with_summary_from_lemma! |
| 1074 |
(fun fname : string => [text_before_function_name ++ fname ++ " is a subtraction with borrow."]%string) |
| 1075 |
(subborrowx_correct relax_adc_sbb_return_carry_to_bitwidth s)). |
| 1076 |
|
| 1077 |
Definition value_barrier (s : int.type) |
| 1078 |
:= Pipeline.BoundsPipeline |
| 1079 |
false (* subst01 *) |
| 1080 |
possible_values_with_bytes |
| 1081 |
reified_value_barrier_gen |
| 1082 |
(Some (int.to_zrange s), tt) |
| 1083 |
(Some (int.to_zrange s)). |
| 1084 |
Definition svalue_barrier (prefix : string) (s : int.type) |
| 1085 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 1086 |
:= Eval cbv beta in |
| 1087 |
FromPipelineToInternalString! |
| 1088 |
machine_wordsize prefix ("value_barrier_" ++ (if int.is_unsigned s then "u" else "") ++ Decimal.Z.to_string (int.bitwidth_of s))%string (value_barrier s) |
| 1089 |
(docstring_with_summary_from_lemma! |
| 1090 |
(fun fname : string => [text_before_function_name ++ fname ++ " is a single-word conditional move."]%string) |
| 1091 |
(value_barrier_correct (int.is_signed s) (int.bitwidth_of s))). |
| 1092 |
|
| 1093 |
Definition cmovznz (s : Z) |
| 1094 |
:= Pipeline.BoundsPipeline |
| 1095 |
false (* subst01 *) |
| 1096 |
possible_values_with_bytes |
| 1097 |
(reified_cmovznz_gen |
| 1098 |
@ GallinaReify.Reify s) |
| 1099 |
(Some r[0~>1], (Some r[0~>2^s-1], (Some r[0~>2^s-1], tt)))%zrange |
| 1100 |
(Some r[0~>2^s-1])%zrange. |
| 1101 |
Definition scmovznz (prefix : string) (s : Z) |
| 1102 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 1103 |
:= Eval cbv beta in |
| 1104 |
FromPipelineToInternalString! |
| 1105 |
machine_wordsize prefix ("cmovznz_u" ++ Decimal.Z.to_string s)%string (cmovznz s) |
| 1106 |
(docstring_with_summary_from_lemma! |
| 1107 |
(fun fname : string => [text_before_function_name ++ fname ++ " is a single-word conditional move."]%string) |
| 1108 |
(cmovznz_correct false s)). |
| 1109 |
Definition cmovznz_by_mul (s : Z) |
| 1110 |
:= Pipeline.BoundsPipeline |
| 1111 |
false (* subst01 *) |
| 1112 |
possible_values_with_bytes |
| 1113 |
(reified_cmovznz_by_mul_gen |
| 1114 |
@ GallinaReify.Reify s) |
| 1115 |
(Some r[0~>1], (Some r[0~>2^s-1], (Some r[0~>2^s-1], tt)))%zrange |
| 1116 |
(Some r[0~>2^s-1])%zrange. |
| 1117 |
Definition scmovznz_by_mul (prefix : string) (s : Z) |
| 1118 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 1119 |
:= Eval cbv beta in |
| 1120 |
FromPipelineToInternalString! |
| 1121 |
machine_wordsize prefix ("cmovznz_u" ++ Decimal.Z.to_string s)%string (cmovznz_by_mul s) |
| 1122 |
(docstring_with_summary_from_lemma! |
| 1123 |
(fun fname : string => [text_before_function_name ++ fname ++ " is a single-word conditional move."]%string) |
| 1124 |
(cmovznz_correct false s)). |
| 1125 |
|
| 1126 |
Definition copy |
| 1127 |
:= Pipeline.BoundsPipeline |
| 1128 |
false (* subst01 *) |
| 1129 |
possible_values |
| 1130 |
reified_id_gen |
| 1131 |
(Some saturated_bounds, tt)%zrange |
| 1132 |
(Some saturated_bounds). |
| 1133 |
Definition scopy (prefix : string) |
| 1134 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 1135 |
:= Eval cbv beta in |
| 1136 |
FromPipelineToString! |
| 1137 |
machine_wordsize prefix "copy" copy |
| 1138 |
(docstring_with_summary_from_lemma! |
| 1139 |
(fun fname : string => [text_before_function_name ++ fname ++ " is a multi-limb identity function."]%string) |
| 1140 |
(copy_correct saturated_bounds)). |
| 1141 |
|
| 1142 |
Local Ltac solve_extra_bounds_side_conditions := |
| 1143 |
cbn [lower upper fst snd machine_wordsize_opt] in *; Bool.split_andb; Z.ltb_to_lt; lia. |
| 1144 |
Hint Rewrite |
| 1145 |
eval_select |
| 1146 |
select_eq |
| 1147 |
Arithmetic.Primitives.mulx_correct |
| 1148 |
Arithmetic.Primitives.addcarryx_correct |
| 1149 |
Arithmetic.Primitives.subborrowx_correct |
| 1150 |
Arithmetic.Primitives.cmovznz_correct |
| 1151 |
Arithmetic.Primitives.cmovznz_by_mul_correct |
| 1152 |
Z.zselect_correct |
| 1153 |
using solve [ auto | congruence | solve_extra_bounds_side_conditions ] : push_eval. |
| 1154 |
Strategy -1000 [mulx]. (* if we don't tell the kernel to unfold this early, then [Qed] seems to run off into the weeds *) |
| 1155 |
Lemma mulx_correct s' res |
| 1156 |
(Hres : mulx s' = Success res) |
| 1157 |
: mulx_correct s' (Interp res). |
| 1158 |
Proof using Type. prove_correctness I. Qed. |
| 1159 |
|
| 1160 |
Lemma Wf_mulx s' res (Hres : mulx s' = Success res) : Wf res. |
| 1161 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 1162 |
Strategy -1000 [addcarryx]. (* if we don't tell the kernel to unfold this early, then [Qed] seems to run off into the weeds *) |
| 1163 |
Lemma addcarryx_correct s' res |
| 1164 |
(Hres : addcarryx s' = Success res) |
| 1165 |
: addcarryx_correct relax_adc_sbb_return_carry_to_bitwidth s' (Interp res). |
| 1166 |
Proof using Type. prove_correctness I. Qed. |
| 1167 |
|
| 1168 |
Lemma Wf_addcarryx s' res (Hres : addcarryx s' = Success res) : Wf res. |
| 1169 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 1170 |
|
| 1171 |
Strategy -1000 [subborrowx]. (* if we don't tell the kernel to unfold this early, then [Qed] seems to run off into the weeds *) |
| 1172 |
Lemma subborrowx_correct s' res |
| 1173 |
(Hres : subborrowx s' = Success res) |
| 1174 |
: subborrowx_correct relax_adc_sbb_return_carry_to_bitwidth s' (Interp res). |
| 1175 |
Proof using Type. prove_correctness I. Qed. |
| 1176 |
|
| 1177 |
Lemma Wf_subborrowx s' res (Hres : subborrowx s' = Success res) : Wf res. |
| 1178 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 1179 |
|
| 1180 |
Strategy -1000 [value_barrier]. (* if we don't tell the kernel to unfold this early, then [Qed] seems to run off into the weeds *) |
| 1181 |
Lemma value_barrier_correct s' res |
| 1182 |
(Hres : value_barrier s' = Success res) |
| 1183 |
: value_barrier_correct (int.is_signed s') (int.bitwidth_of s') (Interp res). |
| 1184 |
Proof using Type. prove_correctness I. Qed. |
| 1185 |
|
| 1186 |
Lemma Wf_value_barrier s' res (Hres : value_barrier s' = Success res) : Wf res. |
| 1187 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 1188 |
|
| 1189 |
Strategy -1000 [cmovznz]. (* if we don't tell the kernel to unfold this early, then [Qed] seems to run off into the weeds *) |
| 1190 |
Lemma cmovznz_correct s' res |
| 1191 |
(Hres : cmovznz s' = Success res) |
| 1192 |
: cmovznz_correct false s' (Interp res). |
| 1193 |
Proof using Type. prove_correctness I. Qed. |
| 1194 |
|
| 1195 |
Lemma Wf_cmovznz s' res (Hres : cmovznz s' = Success res) : Wf res. |
| 1196 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 1197 |
|
| 1198 |
Strategy -1000 [cmovznz_by_mul]. (* if we don't tell the kernel to unfold this early, then [Qed] seems to run off into the weeds *) |
| 1199 |
Lemma cmovznz_by_mul_correct s' res |
| 1200 |
(Hres : cmovznz_by_mul s' = Success res) |
| 1201 |
: COperationSpecifications.Primitives.cmovznz_correct false s' (Interp res). |
| 1202 |
Proof using Type. prove_correctness I. Qed. |
| 1203 |
|
| 1204 |
Lemma Wf_cmovznz_by_mul s' res (Hres : cmovznz_by_mul s' = Success res) : Wf res. |
| 1205 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 1206 |
|
| 1207 |
Lemma selectznz_correct res |
| 1208 |
(Hres : selectznz = Success res) |
| 1209 |
: selectznz_correct saturated_bounds (Interp res). |
| 1210 |
Proof using Type. prove_correctness I. Qed. |
| 1211 |
|
| 1212 |
Lemma Wf_selectznz res (Hres : selectznz = Success res) : Wf res. |
| 1213 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 1214 |
|
| 1215 |
Lemma copy_correct res |
| 1216 |
(Hres : copy = Success res) |
| 1217 |
: copy_correct saturated_bounds (Interp res). |
| 1218 |
Proof using Type. prove_correctness I. Qed. |
| 1219 |
|
| 1220 |
Lemma Wf_copy res (Hres : copy = Success res) : Wf res. |
| 1221 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 1222 |
|
| 1223 |
Section for_stringification. |
| 1224 |
Context (valid_names : string) |
| 1225 |
(known_functions : list (string |
| 1226 |
* (string |
| 1227 |
-> { t : _ |
| 1228 |
& string * Pipeline.M (Pipeline.ExtendedSynthesisResult t) }%type))) |
| 1229 |
(extra_special_synthesis : string -> |
| 1230 |
list |
| 1231 |
(option |
| 1232 |
{ t : _ |
| 1233 |
& string * Pipeline.M (Pipeline.ExtendedSynthesisResult t) }%type)). |
| 1234 |
|
| 1235 |
Definition aggregate_infos (ls : list (string * Pipeline.M { t : _ & Pipeline.ExtendedSynthesisResult t })) : ToString.ident_infos |
| 1236 |
:= fold_right |
| 1237 |
ToString.ident_info_union |
| 1238 |
ToString.ident_info_empty |
| 1239 |
(List.map |
| 1240 |
(fun '(_, res) => match Debug.eval_result res with |
| 1241 |
| Success (existT _ v) => v.(Pipeline.ident_infos) |
| 1242 |
| Error _ => ToString.ident_info_empty |
| 1243 |
end) |
| 1244 |
ls). |
| 1245 |
|
| 1246 |
Definition push_sig_type (v : { t : _ & string * Pipeline.M (Pipeline.ExtendedSynthesisResult t) }%type) |
| 1247 |
: string * Pipeline.M { t : _ & Pipeline.ExtendedSynthesisResult t } |
| 1248 |
:= let '(existT t (n, v)) := v in |
| 1249 |
(n, (v <- v; Debug.ret (v <- v; Success (existT _ t v))%error)%debugM). |
| 1250 |
|
| 1251 |
Definition extra_synthesis (function_name_prefix : string) (infos : ToString.ident_infos) |
| 1252 |
: list { t : _ & string * Pipeline.M (Pipeline.ExtendedSynthesisResult t) }%type * ToString.ident_infos |
| 1253 |
:= let infos := ToString.strip_special_infos machine_wordsize infos in |
| 1254 |
let mk := existT (fun t => string * Pipeline.M (Pipeline.ExtendedSynthesisResult t))%type in |
| 1255 |
let ls_addcarryx := List.flat_map |
| 1256 |
(fun lg_split:positive |
| 1257 |
=> [mk _ (saddcarryx function_name_prefix lg_split) |
| 1258 |
; mk _ (ssubborrowx function_name_prefix lg_split)]) |
| 1259 |
(PositiveSet.elements (ToString.addcarryx_lg_splits infos)) in |
| 1260 |
let ls_mulx := List.map |
| 1261 |
(fun lg_split:positive => mk _ (smulx function_name_prefix lg_split)) |
| 1262 |
(PositiveSet.elements (ToString.mulx_lg_splits infos)) in |
| 1263 |
let ls_value_barrier |
| 1264 |
:= List.map |
| 1265 |
(fun bw => mk _ (svalue_barrier function_name_prefix bw)) |
| 1266 |
(IntSet.elements (value_barrier_bitwidths infos)) in |
| 1267 |
let ls_cmov := List.map |
| 1268 |
(fun ty:int.type |
| 1269 |
=> mk _ ((if use_mul_for_cmovznz then scmovznz_by_mul else scmovznz) function_name_prefix (int.bitwidth_of ty))) |
| 1270 |
(IntSet.elements (cmovznz_bitwidths infos)) in |
| 1271 |
let ls := ls_addcarryx ++ ls_mulx ++ ls_value_barrier ++ ls_cmov in |
| 1272 |
let infos := aggregate_infos (List.map push_sig_type ls) in |
| 1273 |
(ls, infos). |
| 1274 |
|
| 1275 |
(** Since some of the extra functions (such as [cmovznz]) are the |
| 1276 |
only users of other extra functions (such as [value_barrier]), |
| 1277 |
we iterate extra synthesis until we reach a fixedpoint on the |
| 1278 |
info used *) |
| 1279 |
Definition union_extra_infos_of_extra_synthesis (infos : ToString.ident_infos) |
| 1280 |
: ToString.ident_infos |
| 1281 |
:= let '(fs, _) := extra_synthesis "" infos in |
| 1282 |
let fuel := (1 + List.length fs)%nat in |
| 1283 |
snd |
| 1284 |
(fueled_lub |
| 1285 |
ident_infos_equal ident_info_union fuel |
| 1286 |
(fun infos => snd (extra_synthesis "" infos)) |
| 1287 |
infos). |
| 1288 |
|
| 1289 |
Definition synthesize_of_name (function_name_prefix : string) (name : string) |
| 1290 |
: string * Pipeline.M { t : _ & Pipeline.ExtendedSynthesisResult t }%type |
| 1291 |
:= fold_right |
| 1292 |
(fun v default |
| 1293 |
=> match v with |
| 1294 |
| Some (existT t (n, res)) |
| 1295 |
=> (n, (res <- res; Debug.ret (res <- res; Success (existT _ t res))%error)%debugM) |
| 1296 |
| None => default |
| 1297 |
end) |
| 1298 |
(name, |
| 1299 |
Debug.ret |
| 1300 |
(Error |
| 1301 |
(Pipeline.Invalid_argument |
| 1302 |
("Unrecognized request to synthesize """ ++ name ++ """; valid names are " ++ valid_names ++ ".")))) |
| 1303 |
((List.map |
| 1304 |
(fun '(expected_name, resf) |
| 1305 |
=> if (name =? expected_name)%string |
| 1306 |
then Some (resf function_name_prefix) |
| 1307 |
else None) |
| 1308 |
known_functions) |
| 1309 |
++ extra_special_synthesis name). |
| 1310 |
|
| 1311 |
Fixpoint parse_asm_files_lines (ls : list (string (* fname *) * list string)) |
| 1312 |
: Pipeline.ErrorT (list (string (* fname *) * (Assembly.Syntax.Lines (* prefix *) * list (string (* function name *) * Assembly.Syntax.Lines)))) |
| 1313 |
:= match ls with |
| 1314 |
| [] => Success [] |
| 1315 |
| (fname, lines) :: ls |
| 1316 |
=> match Assembly.Parse.parse_validated lines with |
| 1317 |
| Error err => Error (Pipeline.Assembly_parsing_error fname err) |
| 1318 |
| Success v |
| 1319 |
=> (vs <- parse_asm_files_lines ls; |
| 1320 |
Success ((fname, Assembly.Parse.split_code_to_global_functions v) :: vs)) |
| 1321 |
end |
| 1322 |
end%error. |
| 1323 |
|
| 1324 |
Definition parse_asm_hints |
| 1325 |
: Pipeline.ErrorT (list (string (* fname *) * (Assembly.Syntax.Lines (* prefix *) * list (string (* function name *) * Assembly.Syntax.Lines)))) |
| 1326 |
:= parse_asm_files_lines assembly_hints_lines. |
| 1327 |
|
| 1328 |
Definition split_to_assembly_functions {A B} (assembly_data : list (string (* file name *) * list (string * A))) (normal_data : list (string * B)) |
| 1329 |
: list (string * (list (string (* file name *) * A) * B)) * list (string * B) * list (string (* file name *) * list (string (* function name *) * A)) * list (string (* file name of files with no globals *)) |
| 1330 |
:= let assembly_files_without_globals := List.filter (fun '(_, ls) => (List.length ls =? 0)%nat) assembly_data in |
| 1331 |
let assembly_files_without_globals := List.map (fun '(fname, _) => fname) assembly_files_without_globals in |
| 1332 |
let assembly_data := List.filter (fun '(_, ls) => negb (List.length ls =? 0)%nat) assembly_data in |
| 1333 |
if (ignore_unique_asm_names && (forallb (fun '(_, data) => length data =? 1) assembly_data) && (length normal_data =? 1))%nat%bool |
| 1334 |
then match normal_data with |
| 1335 |
| [(n, b)] |
| 1336 |
=> ([(n, (List.flat_map (fun '(fname, asm_ls) => List.map (fun '(_, a) => (fname, a)) asm_ls) assembly_data, b))], [], [], assembly_files_without_globals) |
| 1337 |
| _ => ([], normal_data, assembly_data, assembly_files_without_globals) |
| 1338 |
end |
| 1339 |
else |
| 1340 |
(* move the function name first, flatten the functions associated to each file *) |
| 1341 |
let assembly_data : list (string (* function name *) * (string (* file name *) * A)) := List.flat_map (fun '(fname, asm_ls) => List.map (fun '(func_name, a) => (func_name, (fname, a))) asm_ls) assembly_data in |
| 1342 |
(* now group the asesmbly functions by their function name *) |
| 1343 |
let assembly_data := List.groupAllBy (fun '(n1, _) '(n2, _) => (n1 =? n2)%string) assembly_data in |
| 1344 |
(* now pull out the function name to top level *) |
| 1345 |
let assembly_data : list (string (* function name *) * list (string (* file name *) * A)) |
| 1346 |
:= Option.List.map |
| 1347 |
(fun ls |
| 1348 |
=> match ls with |
| 1349 |
| [] => None |
| 1350 |
| (n, _) :: _ => Some (n, List.map snd ls) |
| 1351 |
end) |
| 1352 |
assembly_data in |
| 1353 |
(* combine normal data with corresponding assembly data *) |
| 1354 |
let cmp longer_string shorter_string := Assembly.Parse.string_matches_loose assembly_labels_fuzzy_prefixes assembly_labels_fuzzy_suffixes longer_string shorter_string in |
| 1355 |
let ls := List.map (fun '(n, normal_data) |
| 1356 |
=> ((n, normal_data), List.find (fun '(n_asm, _) => cmp n_asm n) assembly_data)) |
| 1357 |
normal_data in |
| 1358 |
(* split out the normal data that has assembly data from the normal data that doesn't *) |
| 1359 |
let '(lsAB, lsB) := List.partition (fun '(_, o) => match o with Some _ => true | None => false end) ls in |
| 1360 |
(* get the assembly functions that have no corresponding normal function *) |
| 1361 |
let lsA := List.filter (fun '(n_asm, _) => negb (List.existsb (fun '(n, _) => cmp n_asm n) normal_data)) assembly_data in |
| 1362 |
(* flatten out assembly functions and move the file names first *) |
| 1363 |
let lsA := List.flat_map (fun '(function_name_asm, ls) => List.map (fun '(file_name, v) => (file_name, (function_name_asm, v))) ls) lsA in |
| 1364 |
(* group by files for assembly data *) |
| 1365 |
let lsA := List.groupAllBy (fun '(n1_asm, _) '(n2_asm, _) => (n1_asm =? n2_asm)%string) lsA in |
| 1366 |
let lsA |
| 1367 |
:= Option.List.map |
| 1368 |
(fun ls |
| 1369 |
=> match ls with |
| 1370 |
| [] => None |
| 1371 |
| (n, _) :: _ => Some (n, List.map snd ls) |
| 1372 |
end) |
| 1373 |
lsA in |
| 1374 |
(Option.List.map |
| 1375 |
(fun '((n, normal_data), assembly_data) |
| 1376 |
=> match assembly_data with |
| 1377 |
| Some (_n_asm, assembly_data) => Some (n, (assembly_data, normal_data)) |
| 1378 |
| None => (* should not happen *) None |
| 1379 |
end) |
| 1380 |
lsAB, |
| 1381 |
List.map (@fst _ _) lsB, |
| 1382 |
lsA, |
| 1383 |
assembly_files_without_globals). |
| 1384 |
|
| 1385 |
(** Note: If you change the name or type signature of this |
| 1386 |
function, you will need to update the code in CLI.v *) |
| 1387 |
Definition Synthesize |
| 1388 |
(all_typedefs : list ToString.typedef_info) |
| 1389 |
(check_args : list string -> Pipeline.ErrorT (list string) -> Pipeline.ErrorT (list string)) |
| 1390 |
(comment_header : list string) (function_name_prefix : string) (requests : list string) |
| 1391 |
: list (synthesis_output_kind * string * Pipeline.M (list string)) |
| 1392 |
:= let requests := match requests with |
| 1393 |
| nil => List.map (@fst _ _) known_functions |
| 1394 |
| _ => requests |
| 1395 |
end in |
| 1396 |
let ls := List.map (synthesize_of_name function_name_prefix) requests in |
| 1397 |
let '(asm_ls, ls) |
| 1398 |
:= match parse_asm_hints with |
| 1399 |
| Success [] => ([], ls) |
| 1400 |
| Success asm_hints |
| 1401 |
=> (* TODO: Currently we just pass the prefix/header through unchanged; maybe we should instead generate a better header? *) |
| 1402 |
let asm_ls_prefix |
| 1403 |
:= List.map (fun '(fname, (prefix, _)) => (assembly_output, "header-prefix for " ++ fname, Pipeline.M.ret (Show.show_lines prefix)))%string asm_hints in |
| 1404 |
let function_asms := List.map (fun '(fname, (_prefix, function_asms)) => (fname, function_asms)) asm_hints in |
| 1405 |
let valid_function_names := List.map fst ls in |
| 1406 |
let '(asm_ls, ls, unused_asm_ls, asm_missing_globals) := split_to_assembly_functions function_asms ls in |
| 1407 |
let asm_ls_check |
| 1408 |
:= if negb (List.length asm_missing_globals =? 0)%nat |
| 1409 |
then [(assembly_output, "check", Pipeline.M.err (Pipeline.Assembly_without_any_global_labels asm_missing_globals))] |
| 1410 |
else [] in |
| 1411 |
let asm_ls_check |
| 1412 |
:= asm_ls_check |
| 1413 |
++ if (error_on_unused_assembly_functions && (0 <? List.length unused_asm_ls))%nat |
| 1414 |
then [(assembly_output, "check", Pipeline.M.err (Pipeline.Unused_global_assembly_labels |
| 1415 |
(List.map (fun '(file_name, labels) => (file_name, List.map (@fst _ _) labels)) unused_asm_ls) |
| 1416 |
valid_function_names))] |
| 1417 |
else [] in |
| 1418 |
let asm_ls |
| 1419 |
:= List.map |
| 1420 |
(fun '(name, (asm_fname_and_lines, synthesis_res)) |
| 1421 |
=> (name, |
| 1422 |
(synthesis_res <- synthesis_res; |
| 1423 |
let 'existT _ synthesis_res := synthesis_res in |
| 1424 |
match Assembly.Equivalence.generate_assembly_of_hinted_expr |
| 1425 |
asm_fname_and_lines |
| 1426 |
(synthesis_res.(Pipeline.expr)) |
| 1427 |
(synthesis_res.(Pipeline.arg_bounds)) |
| 1428 |
(synthesis_res.(Pipeline.out_bounds)) |
| 1429 |
with |
| 1430 |
| Success lines => Pipeline.M.ret (List.flat_map (fun '(_fname, lines) => Show.show_lines lines) lines) |
| 1431 |
| Error (Some (fname, asm_lines), err) |
| 1432 |
=> Pipeline.M.err |
| 1433 |
(Pipeline.Equivalence_checking_failure |
| 1434 |
(synthesis_res.(Pipeline.expr)) |
| 1435 |
fname |
| 1436 |
asm_lines |
| 1437 |
(synthesis_res.(Pipeline.arg_bounds)) |
| 1438 |
err) |
| 1439 |
| Error (None, err) |
| 1440 |
=> Pipeline.M.err |
| 1441 |
(Pipeline.Equivalence_checking_failure_pre_asm |
| 1442 |
(synthesis_res.(Pipeline.expr)) |
| 1443 |
(synthesis_res.(Pipeline.arg_bounds)) |
| 1444 |
err) |
| 1445 |
end)%pipelineM)) |
| 1446 |
asm_ls in |
| 1447 |
let asm_ls := List.map (fun '(name, lines) => (assembly_output, name, lines)) asm_ls in |
| 1448 |
(match asm_ls with |
| 1449 |
| [] => asm_ls_check |
| 1450 |
| _ => asm_ls_check ++ asm_ls_prefix ++ asm_ls |
| 1451 |
end, |
| 1452 |
ls) |
| 1453 |
| Error err => ([(assembly_output, "parsing", Pipeline.M.err err)], ls) |
| 1454 |
end in |
| 1455 |
let infos := aggregate_infos ls in |
| 1456 |
let infos := union_extra_infos_of_extra_synthesis infos in |
| 1457 |
let '(extra_ls, extra_infos) := extra_synthesis function_name_prefix infos in |
| 1458 |
let extra_bit_widths := ToString.bitwidths_used extra_infos in |
| 1459 |
let res := (if emit_primitives then List.map push_sig_type extra_ls else nil) ++ ls in |
| 1460 |
let res := List.map (fun '(name, res) => (normal_output, name, (res <- res; Pipeline.M.ret (projT2 res).(Pipeline.lines))%pipelineM)) res in |
| 1461 |
let infos := ToString.ident_info_union |
| 1462 |
infos |
| 1463 |
(ToString.ident_info_of_bitwidths_used extra_bit_widths) in |
| 1464 |
let header := |
| 1465 |
(comment_header |
| 1466 |
++ ToString.header machine_wordsize (orb internal_static static) static function_name_prefix infos all_typedefs) in |
| 1467 |
let footer := |
| 1468 |
ToString.footer machine_wordsize (orb internal_static static) static function_name_prefix infos in |
| 1469 |
[(normal_output, |
| 1470 |
"check_args" ++ String.NewLine ++ String.concat String.NewLine header, |
| 1471 |
Debug.ret (check_args requests (ErrorT.Success header)))%string] |
| 1472 |
++ asm_ls |
| 1473 |
++ res |
| 1474 |
++ match footer with |
| 1475 |
| nil => nil |
| 1476 |
| _ => [(normal_output, "footer", Pipeline.M.ret footer)] |
| 1477 |
end. |
| 1478 |
End for_stringification. |
| 1479 |
End __. |
| 1480 |
|
| 1481 |
Notation "'all_typedefs!'" := |
| 1482 |
(match ((fun r => Option.value (relax_zrange_gen only_signed (possible_values_of_machine_wordsize_with_bytes machine_wordsize) r) r) |
| 1483 |
: relax_zrange_opt) |
| 1484 |
return _ with |
| 1485 |
| relax_zrange |
| 1486 |
=> Pipeline.all_typedefs |
| 1487 |
end) (only parsing). |
| 1488 |
|
| 1489 |
Module Export Hints. |
| 1490 |
Export PipelineHints. |
| 1491 |
#[global] |
| 1492 |
Hint Opaque |
| 1493 |
mulx |
| 1494 |
addcarryx |
| 1495 |
subborrowx |
| 1496 |
value_barrier |
| 1497 |
cmovznz |
| 1498 |
cmovznz_by_mul |
| 1499 |
selectznz |
| 1500 |
copy |
| 1501 |
: wf_op_cache. |
| 1502 |
#[global] |
| 1503 |
Hint Immediate |
| 1504 |
Wf_mulx |
| 1505 |
Wf_addcarryx |
| 1506 |
Wf_subborrowx |
| 1507 |
Wf_value_barrier |
| 1508 |
Wf_cmovznz |
| 1509 |
Wf_cmovznz_by_mul |
| 1510 |
Wf_selectznz |
| 1511 |
Wf_copy |
| 1512 |
: wf_op_cache. |
| 1513 |
End Hints. |