| 1 |
From Coq Require Import String. |
| 2 |
From Coq Require Import ZArith. |
| 3 |
Require Import Crypto.Util.ListUtil Coq.Lists.List. |
| 4 |
Require Import Crypto.Util.ZRange. |
| 5 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 6 |
Require Import Crypto.Language.PreExtra. |
| 7 |
Require Import Ltac2.Ltac2. |
| 8 |
Require Import Ltac2.Printf. |
| 9 |
Require Import Ltac2.Bool. |
| 10 |
Import Ltac2.Constr.Unsafe. |
| 11 |
Require Rewriter.Util.InductiveHList. |
| 12 |
Require Rewriter.Util.LetIn. |
| 13 |
Import InductiveHList.Notations. |
| 14 |
Require Import Rewriter.Util.Tactics2.DestCase. |
| 15 |
Require Import Ltac2.Printf. |
| 16 |
|
| 17 |
(* TODO: move to Util *) |
| 18 |
Ltac2 mkApp (f : constr) (args : constr list) := |
| 19 |
Constr.Unsafe.make (Constr.Unsafe.App f (Array.of_list args)). |
| 20 |
|
| 21 |
Ltac2 Set reify_preprocess_extra := |
| 22 |
fun ctx_tys term |
| 23 |
=> lazy_match! term with |
| 24 |
| @Crypto.Util.LetIn.Let_In ?a ?b ?x ?f |
| 25 |
=> mkApp '@Rewriter.Util.LetIn.Let_In [a; b; x; f] |
| 26 |
| ?term |
| 27 |
=> (* kludge around COQBUG(https://github.com/coq/coq/issues/16421) *) |
| 28 |
(* match ?x with ZRange.Build_zrange a b => @?f a b end |
| 29 |
=> let t := Constr.type term in |
| 30 |
'(@ZRange.zrange_rect_nodep $t $f $x) *) |
| 31 |
match Constr.Unsafe.kind term with |
| 32 |
| Constr.Unsafe.Case _ _ _ _ _ |
| 33 |
=> let (cinfo, ret_ty, cinv, x, branches) := destCase term in |
| 34 |
match Constr.Unsafe.kind ret_ty with |
| 35 |
| Constr.Unsafe.Lambda xb ret_ty |
| 36 |
=> let ty := Constr.Unsafe.substnl [x] 0 ret_ty in |
| 37 |
lazy_match! Constr.Binder.type xb with |
| 38 |
| zrange |
| 39 |
=> mkApp '@ZRange.zrange_rect_nodep [ty; Array.get branches 0; x] |
| 40 |
| _ => term |
| 41 |
end |
| 42 |
| _ => printf "Warning: non-Lambda case return type %t in %t" ret_ty term; |
| 43 |
term |
| 44 |
end |
| 45 |
| _ => term |
| 46 |
end |
| 47 |
end. |
| 48 |
|
| 49 |
Local Ltac2 Notation "red_flags:(" s(strategy) ")" := s. |
| 50 |
|
| 51 |
(* TODO: Move to util *) |
| 52 |
Ltac2 eval_cbv_beta (c : constr) := |
| 53 |
Std.eval_cbv (red_flags:(beta)) c. |
| 54 |
|
| 55 |
Ltac2 Set reify_ident_preprocess_extra := |
| 56 |
fun ctx_tys term |
| 57 |
=> lazy_match! term with |
| 58 |
| @ZRange.zrange_rect ?t0 |
| 59 |
=> lazy_match! eval_cbv_beta t0 with |
| 60 |
| fun _ => ?t => mkApp '@ZRange.zrange_rect_nodep [t] |
| 61 |
| ?t' => if Constr.equal t0 t' |
| 62 |
then term |
| 63 |
else mkApp '@ZRange.zrange_rect [t'] |
| 64 |
end |
| 65 |
| _ => term |
| 66 |
end. |
| 67 |
|
| 68 |
Definition var_like_idents : InductiveHList.hlist |
| 69 |
:= [@ident.literal |
| 70 |
; @Datatypes.nil |
| 71 |
; @Datatypes.cons |
| 72 |
; @Datatypes.pair |
| 73 |
; @Datatypes.fst |
| 74 |
; @Datatypes.snd |
| 75 |
; Datatypes.tt |
| 76 |
; Z.opp |
| 77 |
; ident.cast |
| 78 |
; ident.cast2 |
| 79 |
; Z.combine_at_bitwidth]%hlist. |
| 80 |
|
| 81 |
Definition base_type_list_named : InductiveHList.hlist |
| 82 |
:= [with_name Z BinInt.Z |
| 83 |
; with_name positive BinPos.positive |
| 84 |
; with_name bool Datatypes.bool |
| 85 |
; with_name nat Datatypes.nat |
| 86 |
; with_name zrange ZRange.zrange |
| 87 |
; with_name string String.string]%hlist. |
| 88 |
|
| 89 |
Definition all_ident_named_interped : InductiveHList.hlist |
| 90 |
:= [with_name ident_Literal (@ident.literal) |
| 91 |
; with_name ident_comment (@ident.comment) |
| 92 |
; with_name ident_comment_no_keep (@ident.comment_no_keep) |
| 93 |
; with_name ident_value_barrier (@Z.value_barrier) |
| 94 |
; with_name ident_Nat_succ Nat.succ |
| 95 |
; with_name ident_Nat_pred Nat.pred |
| 96 |
; with_name ident_Nat_max Nat.max |
| 97 |
; with_name ident_Nat_mul Nat.mul |
| 98 |
; with_name ident_Nat_add Nat.add |
| 99 |
; with_name ident_Nat_sub Nat.sub |
| 100 |
; with_name ident_Nat_eqb Nat.eqb |
| 101 |
; with_name ident_nil (@Datatypes.nil) |
| 102 |
; with_name ident_cons (@Datatypes.cons) |
| 103 |
; with_name ident_tt Datatypes.tt |
| 104 |
; with_name ident_pair (@Datatypes.pair) |
| 105 |
; with_name ident_fst (@Datatypes.fst) |
| 106 |
; with_name ident_snd (@Datatypes.snd) |
| 107 |
; with_name ident_prod_rect (@prod_rect_nodep) |
| 108 |
; with_name ident_bool_rect (@Thunked.bool_rect) |
| 109 |
; with_name ident_bool_rect_nodep (@bool_rect_nodep) |
| 110 |
; with_name ident_nat_rect (@Thunked.nat_rect) |
| 111 |
; with_name ident_eager_nat_rect (ident.eagerly (@Thunked.nat_rect)) |
| 112 |
; with_name ident_nat_rect_arrow (@nat_rect_arrow_nodep) |
| 113 |
; with_name ident_eager_nat_rect_arrow (ident.eagerly (@nat_rect_arrow_nodep)) |
| 114 |
; with_name ident_nat_rect_fbb_b (@nat_rect_fbb_b) |
| 115 |
; with_name ident_nat_rect_fbb_b_b (@nat_rect_fbb_b_b) |
| 116 |
; with_name ident_list_rect (@Thunked.list_rect) |
| 117 |
; with_name ident_eager_list_rect (ident.eagerly (@Thunked.list_rect)) |
| 118 |
; with_name ident_list_rect_arrow (@list_rect_arrow_nodep) |
| 119 |
; with_name ident_eager_list_rect_arrow (ident.eagerly (@list_rect_arrow_nodep)) |
| 120 |
; with_name ident_list_rect_fbb_b (@list_rect_fbb_b) |
| 121 |
; with_name ident_list_rect_fbb_b_b (@list_rect_fbb_b_b) |
| 122 |
; with_name ident_list_rect_fbb_b_b_b (@list_rect_fbb_b_b_b) |
| 123 |
; with_name ident_list_rect_fbb_b_b_b_b (@list_rect_fbb_b_b_b_b) |
| 124 |
; with_name ident_list_rect_fbb_b_b_b_b_b (@list_rect_fbb_b_b_b_b_b) |
| 125 |
; with_name ident_list_case (@Thunked.list_case) |
| 126 |
; with_name ident_List_length (@List.length) |
| 127 |
; with_name ident_List_seq (@List.seq) |
| 128 |
; with_name ident_List_firstn (@List.firstn) |
| 129 |
; with_name ident_List_skipn (@List.skipn) |
| 130 |
; with_name ident_List_repeat (@repeat) |
| 131 |
; with_name ident_List_combine (@List.combine) |
| 132 |
; with_name ident_List_map (@List.map) |
| 133 |
; with_name ident_List_app (@List.app) |
| 134 |
; with_name ident_List_rev (@List.rev) |
| 135 |
; with_name ident_List_flat_map (@List.flat_map) |
| 136 |
; with_name ident_List_partition (@List.partition) |
| 137 |
; with_name ident_List_filter (@List.filter) |
| 138 |
; with_name ident_List_fold_right (@List.fold_right) |
| 139 |
; with_name ident_List_update_nth (@update_nth) |
| 140 |
; with_name ident_List_nth_default (@nth_default) |
| 141 |
; with_name ident_eager_List_nth_default (ident.eagerly (@nth_default)) |
| 142 |
; with_name ident_Pos_add Pos.add |
| 143 |
; with_name ident_Pos_mul Pos.mul |
| 144 |
; with_name ident_Z_add Z.add |
| 145 |
; with_name ident_Z_mul Z.mul |
| 146 |
; with_name ident_Z_pow Z.pow |
| 147 |
; with_name ident_Z_sub Z.sub |
| 148 |
; with_name ident_Z_opp Z.opp |
| 149 |
; with_name ident_Z_div Z.div |
| 150 |
; with_name ident_Z_modulo Z.modulo |
| 151 |
; with_name ident_Z_eqb Z.eqb |
| 152 |
; with_name ident_Z_leb Z.leb |
| 153 |
; with_name ident_Z_ltb Z.ltb |
| 154 |
; with_name ident_Z_geb Z.geb |
| 155 |
; with_name ident_Z_gtb Z.gtb |
| 156 |
; with_name ident_Z_log2 Z.log2 |
| 157 |
; with_name ident_Z_log2_up Z.log2_up |
| 158 |
; with_name ident_Z_of_nat Z.of_nat |
| 159 |
; with_name ident_Z_to_nat Z.to_nat |
| 160 |
; with_name ident_Z_pos Z.pos |
| 161 |
; with_name ident_Z_to_pos Z.to_pos |
| 162 |
; with_name ident_Z_shiftr Z.shiftr |
| 163 |
; with_name ident_Z_shiftl Z.shiftl |
| 164 |
; with_name ident_Z_land Z.land |
| 165 |
; with_name ident_Z_lor Z.lor |
| 166 |
; with_name ident_Z_min Z.min |
| 167 |
; with_name ident_Z_max Z.max |
| 168 |
; with_name ident_Z_abs Z.abs |
| 169 |
; with_name ident_Z_mul_split Z.mul_split |
| 170 |
; with_name ident_Z_mul_high Z.mul_high |
| 171 |
; with_name ident_Z_add_get_carry Z.add_get_carry_full |
| 172 |
; with_name ident_Z_add_with_carry Z.add_with_carry |
| 173 |
; with_name ident_Z_add_with_get_carry Z.add_with_get_carry_full |
| 174 |
; with_name ident_Z_sub_get_borrow Z.sub_get_borrow_full |
| 175 |
; with_name ident_Z_sub_with_get_borrow Z.sub_with_get_borrow_full |
| 176 |
; with_name ident_Z_ltz Z.ltz |
| 177 |
; with_name ident_Z_zselect Z.zselect |
| 178 |
; with_name ident_Z_add_modulo Z.add_modulo |
| 179 |
; with_name ident_Z_truncating_shiftl Z.truncating_shiftl |
| 180 |
; with_name ident_Z_bneg Z.bneg |
| 181 |
; with_name ident_Z_lnot_modulo Z.lnot_modulo |
| 182 |
; with_name ident_Z_lxor Z.lxor |
| 183 |
; with_name ident_Z_rshi Z.rshi |
| 184 |
; with_name ident_Z_cc_m Z.cc_m |
| 185 |
; with_name ident_Z_combine_at_bitwidth Z.combine_at_bitwidth |
| 186 |
; with_name ident_Z_cast ident.cast |
| 187 |
; with_name ident_Z_cast2 ident.cast2 |
| 188 |
; with_name ident_Some (@Datatypes.Some) |
| 189 |
; with_name ident_None (@Datatypes.None) |
| 190 |
; with_name ident_option_rect (@Thunked.option_rect) |
| 191 |
; with_name ident_Build_zrange ZRange.Build_zrange |
| 192 |
; with_name ident_zrange_rect (@ZRange.zrange_rect_nodep) |
| 193 |
; with_name ident_fancy_add ident.fancy.add |
| 194 |
; with_name ident_fancy_addc ident.fancy.addc |
| 195 |
; with_name ident_fancy_sub ident.fancy.sub |
| 196 |
; with_name ident_fancy_subb ident.fancy.subb |
| 197 |
; with_name ident_fancy_mulll ident.fancy.mulll |
| 198 |
; with_name ident_fancy_mullh ident.fancy.mullh |
| 199 |
; with_name ident_fancy_mulhl ident.fancy.mulhl |
| 200 |
; with_name ident_fancy_mulhh ident.fancy.mulhh |
| 201 |
; with_name ident_fancy_rshi ident.fancy.rshi |
| 202 |
; with_name ident_fancy_selc ident.fancy.selc |
| 203 |
; with_name ident_fancy_selm ident.fancy.selm |
| 204 |
; with_name ident_fancy_sell ident.fancy.sell |
| 205 |
; with_name ident_fancy_addm ident.fancy.addm |
| 206 |
]%hlist. |
| 207 |
|
| 208 |
Definition scraped_data : ScrapedData.t |
| 209 |
:= {| ScrapedData.base_type_list_named := base_type_list_named |
| 210 |
; ScrapedData.all_ident_named_interped := all_ident_named_interped |}. |