| 1 |
(** * Test the various specs of primitive floats for a sampling of inputs *) |
| 2 |
(** We enumerate a list of float and int parameters, a list of |
| 3 |
specs and a list of operators. Then two things are checked: |
| 4 |
1. Test the specs: |
| 5 |
Each spec is empirically checked against all |
| 6 |
instantiations of arguments from the float/int lists. |
| 7 |
This is done with vm_compute for the check to be fast. |
| 8 |
2. Test the evaluation mechanisms: |
| 9 |
Again on each float/int from the lists, each operator |
| 10 |
is evaluated in multiple evaluation mechanisms to check |
| 11 |
that their results agree with the one given by vm_compute. *) |
| 12 |
From Corelib Require Import ListDef BinNums PrimInt63 Uint63Axioms. |
| 13 |
From Corelib Require Import SpecFloat PrimFloat FloatOps FloatAxioms. |
| 14 |
From Ltac2 Require Import Ltac2 Printf. |
| 15 |
|
| 16 |
Notation "[ x ; y ; .. ; z ]" := (cons x (cons y .. (cons z nil) ..)) |
| 17 |
(format "[ '[' x ; '/' y ; '/' .. ; '/' z ']' ]") : list_scope. |
| 18 |
|
| 19 |
Open Scope list_scope. |
| 20 |
Open Scope float_scope. |
| 21 |
|
| 22 |
Section __WORK_AROUND_COQBUG_4790. |
| 23 |
(** *************************************************************************) |
| 24 |
(** * Specifying the arguments to test on *) |
| 25 |
(** EDIT HERE TO ADD MORE TESTS *) |
| 26 |
(** ** List of floats to instantiate spec and operator args *) |
| 27 |
Definition tricky_floats : list float |
| 28 |
:= Eval cbv in |
| 29 |
[infinity; neg_infinity; nan; 0; -0 |
| 30 |
; 0x1.fffffffffffffp+1023 (* largest finite value *) |
| 31 |
; -0x1.fffffffffffffp+1023 (* largest negative finite value *) |
| 32 |
; 0x0.1p-1070 (* smallest positive value *) |
| 33 |
; -0x0.1p-1070 (* non-zero negative value closest to zero *) |
| 34 |
; 1; -1; 0.5; 2; -0.5; -2 |
| 35 |
(* numbers from fma tests 264-266 from testsuite/tests/fma/fma.ml, |
| 36 |
cf |
| 37 |
https://github.com/rocq-prover/rocq/issues/17893#issuecomment-1654794043. These |
| 38 |
tests trigger the broken implementations of Cygwin64, mingw-w64 |
| 39 |
(x86_64) and VS2013-2017. *) |
| 40 |
; 0x3.bd5b7dde5fddap-496 |
| 41 |
; -0xd.fc352bc352bap-992 |
| 42 |
|
| 43 |
; 0x3.bd5b7dde5fddap-504 |
| 44 |
; -0xd.fc352bc352bap-1008 |
| 45 |
|
| 46 |
; 0x8p-540 |
| 47 |
; 0x4p-540 |
| 48 |
(* ; 0x4p-1076 *) (* same as 0x0.1p-1070 above *) |
| 49 |
(* constants from [add.v] *) |
| 50 |
; 3 |
| 51 |
(* ; Z.ldexp one 1023%Z *) (* same as largest finite (emax-prec) above *) |
| 52 |
; Z.ldexp one (Zneg (xI (xI (xI (xI (xI (xI (xI (xI (xI xH)))))))))); -Z.ldexp one (Zneg (xI (xI (xI (xI (xI (xI (xI (xI (xI xH)))))))))) |
| 53 |
(* constants from [classify.v] *) |
| 54 |
; Z.ldexp one (Zneg (xO (xO (xO (xO (xO (xO (xO (xO (xO (xO xH))))))))))); -Z.ldexp one (Zneg (xO (xO (xO (xO (xO (xO (xO (xO (xO (xO xH))))))))))) |
| 55 |
(* constants from [div.v] *) |
| 56 |
; 6 |
| 57 |
(* constants from [double_rounding.v] *) |
| 58 |
; Z.ldexp one (Zpos (xI (xO (xI (xO (xI xH)))))); Z.ldexp one (Zpos (xO (xO (xI (xO (xI xH)))))) |
| 59 |
; 1 + Z.ldexp 1 (Zneg (xO (xO (xI (xO (xI xH)))))) |
| 60 |
(* constants from [next_up_down.v] *) |
| 61 |
; 42; -42 |
| 62 |
; Z.ldexp one (Zneg (xO (xI (xI (xI (xI (xI (xI (xI (xI xH)))))))))); -Z.ldexp one (Zneg (xO (xI (xI (xI (xI (xI (xI (xI (xI xH)))))))))) |
| 63 |
; -0x1.ffffffffffffap+1023 |
| 64 |
; -0x1.fffffffffffff |
| 65 |
; -0x0.fffffffffffffp-1022 |
| 66 |
; -0x0.01p-1022 |
| 67 |
; 0x0.01p-1022 |
| 68 |
; 0x0.fffffffffffffp-1022 |
| 69 |
; 0x1.fffffffffffff |
| 70 |
; 0x1.ffffffffffffap+1023 |
| 71 |
(* constants from [normfr_mantissa.v] *) |
| 72 |
; 0.75 |
| 73 |
(* constants from [sqrt.v] *) |
| 74 |
; 9 |
| 75 |
]. |
| 76 |
Definition tricky_spec_floats := |
| 77 |
Eval cbv in ListDef.map Prim2SF tricky_floats. |
| 78 |
(** ** List of ints to instantiate spec and operator args *) |
| 79 |
Definition tricky_ints : list int |
| 80 |
:= Eval cbv in |
| 81 |
[0; 1 |
| 82 |
; max_int; PrimInt63.sub max_int 1 |
| 83 |
; Uint63Axioms.of_Z prec |
| 84 |
; Uint63Axioms.of_Z emax |
| 85 |
; Uint63Axioms.of_Z emin |
| 86 |
; 2; PrimInt63.div max_int 2 |
| 87 |
; PrimInt63.sub (PrimInt63.div max_int 2) 1; PrimInt63.add (PrimInt63.div max_int 2) 1 |
| 88 |
; PrimInt63.sub (Uint63Axioms.of_Z prec) 1; PrimInt63.add (Uint63Axioms.of_Z prec) 1 |
| 89 |
; PrimInt63.div (Uint63Axioms.of_Z prec) 2; PrimInt63.sub (PrimInt63.div (Uint63Axioms.of_Z prec) 2) 1; PrimInt63.sub (PrimInt63.div (Uint63Axioms.of_Z prec) 2) 1 |
| 90 |
; PrimInt63.sub (Uint63Axioms.of_Z emax) 1; PrimInt63.add (Uint63Axioms.of_Z emax) 1 |
| 91 |
; PrimInt63.div (Uint63Axioms.of_Z emax) 2; PrimInt63.sub (PrimInt63.div (Uint63Axioms.of_Z emax) 2) 1; PrimInt63.add (PrimInt63.div (Uint63Axioms.of_Z emax) 2) 1 |
| 92 |
; PrimInt63.sub (Uint63Axioms.of_Z emin) 1; PrimInt63.add (Uint63Axioms.of_Z emin) 1 |
| 93 |
; PrimInt63.div (Uint63Axioms.of_Z emin) 2; PrimInt63.sub (PrimInt63.div (Uint63Axioms.of_Z emin) 2) 1; PrimInt63.add (PrimInt63.div (Uint63Axioms.of_Z emin) 2) 1 |
| 94 |
(* constants from [ldexp.v] *) |
| 95 |
(* ; 9223372036854775807 *) (* max_int *) |
| 96 |
; 0x7ffffffffffff7ca%uint63 (* (-2102)%sint63 *) |
| 97 |
; 0x7ffffffffffffffd%uint63 (* (-3)%sint63 *) |
| 98 |
; 3 |
| 99 |
]%uint63. |
| 100 |
(** *************************************************************************) |
| 101 |
|
| 102 |
(** * Reflective machinery to instantiate specifications *) |
| 103 |
(** ** Variables we know how to instantiate *) |
| 104 |
Inductive SPEC_VAR_TYPE := INT | FLOAT | SPEC_FLOAT. |
| 105 |
Coercion denote_SPEC_VAR_TYPE (x : SPEC_VAR_TYPE) : Set |
| 106 |
:= match x with INT => int | FLOAT => float | SPEC_FLOAT => spec_float end. |
| 107 |
|
| 108 |
(** ** Fully-instantiated ("bare") specifications *) |
| 109 |
(** (Perhaps we want a better name than "bare" meaning "no binders"? *) |
| 110 |
(** As we'll see later, we check for [EQ l r] that [l] and [r] are |
| 111 |
[Constr.equal], and we'll check for [IFF (x = y) (x' = y')] that |
| 112 |
[Constr.equal x y] and [Constr.equal x' y'] are the same. (We |
| 113 |
don't currently support reporting results about [IFF A B] for [A] |
| 114 |
and [B] not equalities. *) |
| 115 |
Inductive BARE_SPEC := |
| 116 |
| EQ {T1 T2} (lhs : T1) (rhs : T2) |
| 117 |
| IFF {T1 T2} (lhs : T1) (rhs : T2). |
| 118 |
|
| 119 |
(** A [SPEC] is a [BARE_SPEC] prenex-quanified over known variable |
| 120 |
types. We hold the original proposition here so that we can |
| 121 |
pretty-print it easily *) |
| 122 |
Inductive SPEC := |
| 123 |
| BARE |
| 124 |
{U : Prop} (spec : U) (* for printing purposes *) |
| 125 |
(s : BARE_SPEC) |
| 126 |
| FORALL (T : SPEC_VAR_TYPE) |
| 127 |
(s : T -> SPEC). |
| 128 |
|
| 129 |
(** An [ANNOTATED_BARE_SPEC] holds the [BARE_SPEC] and also the |
| 130 |
propositional spec for pretty-printing of results. *) |
| 131 |
Definition ANNOTATED_BARE_SPEC : Type := BARE_SPEC * {P : Prop | P}. |
| 132 |
|
| 133 |
(* missing list functions *) |
| 134 |
Section FlatMap. |
| 135 |
Variables (A : Type) (B : Type). |
| 136 |
Variable f : A -> list B. |
| 137 |
Definition flat_map := |
| 138 |
fix flat_map (l:list A) : list B := |
| 139 |
match l with |
| 140 |
| nil => nil |
| 141 |
| cons x t => (f x)++(flat_map t) |
| 142 |
end. |
| 143 |
End FlatMap. |
| 144 |
Arguments flat_map [_ _]. |
| 145 |
|
| 146 |
Section ListPairs. |
| 147 |
Variables (A : Type) (B : Type). |
| 148 |
Fixpoint combine (l : list A) (l' : list B) : list (A*B) := |
| 149 |
match l,l' with |
| 150 |
| x::tl, y::tl' => (x,y)::(combine tl tl') |
| 151 |
| _, _ => nil |
| 152 |
end. |
| 153 |
End ListPairs. |
| 154 |
Arguments combine [_ _]. |
| 155 |
|
| 156 |
(** ** Machinery for instantiating specifications with all examples *) |
| 157 |
Fixpoint instantiate1_all_ways (s : SPEC) : list ANNOTATED_BARE_SPEC |
| 158 |
:= match s with |
| 159 |
| @BARE U spec s => cons (s, exist _ U spec) nil |
| 160 |
| @FORALL T s |
| 161 |
=> flat_map |
| 162 |
(fun v => instantiate1_all_ways (s v)) |
| 163 |
match T with |
| 164 |
| INT => tricky_ints |
| 165 |
| FLOAT => tricky_floats |
| 166 |
| SPEC_FLOAT => tricky_spec_floats |
| 167 |
end |
| 168 |
end. |
| 169 |
|
| 170 |
Definition instantiate_all_ways_nored (ls : list SPEC) : list ANNOTATED_BARE_SPEC |
| 171 |
:= flat_map instantiate1_all_ways ls. |
| 172 |
|
| 173 |
Definition instantiate_all_ways (ls : list SPEC) : list ANNOTATED_BARE_SPEC |
| 174 |
:= Eval cbv in instantiate_all_ways_nored ls. |
| 175 |
|
| 176 |
(** ** Some General Ltac2 Machinery *) |
| 177 |
Import Ltac2.Constr. |
| 178 |
Import Constr.Unsafe. |
| 179 |
Ltac2 Type exn ::= [ PrimFloat_Test_InternalError (message) | PrimFloat_SpecTest_Failed (message) ]. |
| 180 |
Ltac2 Type exn ::= [ Reification_error (message) | Reification_unhandled_kind (message, kind) ]. |
| 181 |
|
| 182 |
Ltac2 unify_bool (x : constr) (y : constr) : bool |
| 183 |
:= match Control.case (fun () => Std.unify x y) with |
| 184 |
| Val _ => true |
| 185 |
| Err _ => false |
| 186 |
end. |
| 187 |
|
| 188 |
Ltac2 lf () := String.make 1 (Char.of_int 10). |
| 189 |
|
| 190 |
Ltac2 rec count_prod (x : constr) : int := |
| 191 |
match kind x with |
| 192 |
| Cast x _ _ => count_prod x |
| 193 |
| Prod _ x => Int.add 1 (count_prod x) |
| 194 |
| _ => 0 |
| 195 |
end. |
| 196 |
Ltac2 mkApp f x := Unsafe.make (App f (Array.of_list x)). |
| 197 |
Ltac2 mkRel i := Unsafe.make (Rel i). |
| 198 |
Ltac2 mkLambda b body := Unsafe.make (Lambda b body). |
| 199 |
|
| 200 |
(** ** Reification of known variable types *) |
| 201 |
Ltac2 reify_var_type (t : constr) : constr |
| 202 |
:= match List.assoc_opt Constr.equal t |
| 203 |
[('spec_float, 'SPEC_FLOAT) |
| 204 |
; ('float, 'FLOAT) |
| 205 |
; ('int, 'INT)] |
| 206 |
with |
| 207 |
| Some v => v |
| 208 |
| None => Control.throw (Reification_error (fprintf "Unhandled type %t" t)) |
| 209 |
end. |
| 210 |
|
| 211 |
(** ** A kludgy hack we have to do to support some specifications that aren't equalities but have case statements out front *) |
| 212 |
(** turns [let '(x, y) := z in w = q] into [(let '(x, y) := z in w) = (let '(x, y) := z in q)] *) |
| 213 |
(** Does not run typechecking, and therefore works on open terms (with unbound rels) *) |
| 214 |
Ltac2 rec push_case_eq (tag : constr) (mkCase : constr (* retty *) -> constr -> constr) (branch : constr) : constr |
| 215 |
:= match kind branch with |
| 216 |
| Lambda b body => push_case_eq tag (fun retty body => mkCase retty (mkLambda b body)) body |
| 217 |
| App f args |
| 218 |
=> if Constr.equal f '@eq |
| 219 |
then let ty := Array.get args 0 in |
| 220 |
let x := Array.get args 1 in |
| 221 |
let y := Array.get args 2 in |
| 222 |
mkApp f [ty; mkCase ty x; mkCase ty y] |
| 223 |
else Control.throw (Reification_error (fprintf "Unrecognized under case %t (from %t from %t)" f branch tag)) |
| 224 |
| _ => Control.throw (Reification_error (fprintf "Unrecognized kind under case %t (from %t)" branch tag)) |
| 225 |
end. |
| 226 |
Ltac2 swap_case_eq (x : constr) : constr |
| 227 |
:= match kind x with |
| 228 |
| Case c (retty, rel) ci discr branches |
| 229 |
=> if Int.equal 1 (Array.length branches) |
| 230 |
then match kind retty with |
| 231 |
| Lambda retty_b rtProp |
| 232 |
=> if Constr.equal rtProp 'Prop |
| 233 |
then push_case_eq |
| 234 |
x |
| 235 |
(fun retty b => Unsafe.make (Case c (mkLambda retty_b (liftn 1 1 retty), rel) ci discr (Array.of_list [b]))) |
| 236 |
(Array.get branches 0) |
| 237 |
else x |
| 238 |
| _ => x |
| 239 |
end |
| 240 |
else x |
| 241 |
| _ => x |
| 242 |
end. |
| 243 |
|
| 244 |
(** ** Reification of specifications after binders have been removed *) |
| 245 |
(** Does not run typechecking, and therefore works on open terms (with unbound rels) *) |
| 246 |
Ltac2 reify_bare_spec (ty : constr) : constr |
| 247 |
:= let ty := swap_case_eq ty in |
| 248 |
match kind ty with |
| 249 |
| App f args |
| 250 |
=> if Constr.equal f '@eq |
| 251 |
then Unsafe.make (App (mkApp '@EQ [Array.get args 0]) args) |
| 252 |
else if Constr.equal f '@iff |
| 253 |
then Unsafe.make (App (mkApp '@IFF ['Prop; 'Prop]) args) |
| 254 |
else Control.throw (Reification_error (fprintf "Unhandled base spec app %t" ty)) |
| 255 |
| k => Control.throw (Reification_unhandled_kind (fprintf "Unhandled base spec %t" ty) k) |
| 256 |
end. |
| 257 |
(** ** Reification of specs, including binders *) |
| 258 |
(** [n] is how many binders are left to remove in [spec], and |
| 259 |
therefore which [Rel] the [spec] should be eventually applied to |
| 260 |
*) |
| 261 |
Ltac2 rec reify_spec' (ty : constr) (spec : constr) (n : int) : constr |
| 262 |
:= match kind ty with |
| 263 |
| Cast ty _ _ => reify_spec' ty spec n |
| 264 |
| Prod b body |
| 265 |
=> let ty := reify_var_type (Binder.type b) in |
| 266 |
let body := reify_spec' body (mkApp spec [mkRel n]) (Int.sub n 1) in |
| 267 |
mkApp 'FORALL [ty; mkLambda b body] |
| 268 |
| _ => let r := reify_bare_spec ty in |
| 269 |
mkApp '@BARE [ty; spec; r] |
| 270 |
end. |
| 271 |
Ltac2 reify_spec (spec : constr) : constr |
| 272 |
:= let ty := Constr.type spec in |
| 273 |
reify_spec' ty spec (count_prod ty). |
| 274 |
|
| 275 |
Notation "` x" := (ltac2:(let v := reify_spec (pretype x) in exact $v)) (only parsing, at level 10). |
| 276 |
|
| 277 |
(** * Machinery for reporting results *) |
| 278 |
Ltac2 report_result (red : string) (result : constr) (specTy : constr) (spec : constr) : message option |
| 279 |
:= let msg := |
| 280 |
lazy_match! result with |
| 281 |
| EQ ?x ?y |
| 282 |
=> if Constr.equal x y |
| 283 |
then None |
| 284 |
else (* if unify_bool x y (* commented out because of https://github.com/rocq-prover/rocq/pull/17899 *) |
| 285 |
then Some (fprintf "%s failed to fully reduce, leaving over %t (expected: %t), in %t %t" red x y spec specTy) |
| 286 |
else *) Some (fprintf "%s failed!%sGot: %t%sExpected: %t%sIn %t %t" red (lf ()) x (lf ()) y (lf ()) spec specTy) |
| 287 |
| IFF (?x = ?x') (?y = ?y') |
| 288 |
=> let (lhs, rhs) := lazy_match! result with |
| 289 |
| IFF ?lhs ?rhs => (lhs, rhs) |
| 290 |
| _ => Control.throw (PrimFloat_Test_InternalError (fprintf "Impossible! Result branch mismatch %t" result)) |
| 291 |
end in |
| 292 |
let descr := if unify_bool y y' then "should" else "should not" in |
| 293 |
if Bool.and (Bool.equal (Constr.equal x x') (Constr.equal y y')) |
| 294 |
(Bool.equal (unify_bool x x') (unify_bool y y')) |
| 295 |
then None |
| 296 |
else (* if Bool.equal (unify_bool x x') (unify_bool y y') (* commented out because of https://github.com/rocq-prover/rocq/pull/17899 *) |
| 297 |
then Some (fprintf "%s failed to fully reduce, leaving over %t (expected something equivalent to: %t), in %t %t" red lhs rhs spec specTy) |
| 298 |
else *) Some (fprintf "%s failed!%sGot: %t%sExpected something equivalent to: %t%s(both sides %s unify)%sIn %t %t" red (lf ()) lhs (lf ()) rhs (lf ()) descr (lf ()) spec specTy) |
| 299 |
| _ => Control.throw (PrimFloat_Test_InternalError (fprintf "Unhandled result %t (on %t : %t with %s)" result spec specTy red)) |
| 300 |
end in |
| 301 |
match msg with |
| 302 |
| Some msg => Message.print (Message.concat (Message.of_string "Test Error: ") msg) |
| 303 |
| None => () |
| 304 |
end; |
| 305 |
msg. |
| 306 |
|
| 307 |
Ltac2 rec report_results_gen (error_early : bool) (red : string) (results : constr) : unit |
| 308 |
:= lazy_match! results with |
| 309 |
| nil => () |
| 310 |
| cons (?res, exist _ ?specTy ?spec) ?results |
| 311 |
=> let err := report_result red res specTy spec in |
| 312 |
let check_rest () := report_results_gen error_early red results in |
| 313 |
let zero_err () := match err with |
| 314 |
| Some err => Control.zero (PrimFloat_SpecTest_Failed err) |
| 315 |
| None => () |
| 316 |
end in |
| 317 |
if error_early |
| 318 |
then (zero_err (); check_rest ()) |
| 319 |
else (check_rest (); zero_err ()) |
| 320 |
| cons ?v _ |
| 321 |
=> Control.throw (PrimFloat_Test_InternalError (fprintf "Invalid result format %t" v)) |
| 322 |
| _ |
| 323 |
=> let results' := Std.eval_hnf results in |
| 324 |
if Constr.equal results results' |
| 325 |
then Control.throw (PrimFloat_Test_InternalError (fprintf "Results must be a literal list, not %t" results)) |
| 326 |
else report_results_gen error_early red results' |
| 327 |
end. |
| 328 |
Ltac2 report_results red results := report_results_gen false red results. |
| 329 |
Ltac2 report_results_fast red results := report_results_gen true red results. |
| 330 |
|
| 331 |
(** *************************************************************************) |
| 332 |
(** * List of (reified) specifications *) |
| 333 |
(** EDIT HERE TO ADD MORE TESTS *) |
| 334 |
|
| 335 |
(* [Prim2SF_SF2Prim] has an hypothesis not handled by the above machinery |
| 336 |
so let's check something stronger in theory but equivalent in practice, |
| 337 |
since all test cases satisfy the hypothesis by construction. *) |
| 338 |
Axiom Prim2SF_SF2Prim' : forall x, (* valid_binary x = true -> *) Prim2SF (SF2Prim x) = x. |
| 339 |
|
| 340 |
Definition spec_list : list SPEC := |
| 341 |
[ `Prim2SF_valid |
| 342 |
; `SF2Prim_Prim2SF |
| 343 |
; `Prim2SF_SF2Prim' |
| 344 |
|
| 345 |
; `opp_spec |
| 346 |
; `abs_spec |
| 347 |
|
| 348 |
; `eqb_spec |
| 349 |
; `ltb_spec |
| 350 |
; `leb_spec |
| 351 |
|
| 352 |
; `compare_spec |
| 353 |
|
| 354 |
; `Leibniz.eqb_spec |
| 355 |
|
| 356 |
; `classify_spec |
| 357 |
; `mul_spec |
| 358 |
; `add_spec |
| 359 |
; `sub_spec |
| 360 |
; `div_spec |
| 361 |
; `sqrt_spec |
| 362 |
|
| 363 |
; `of_uint63_spec |
| 364 |
; `normfr_mantissa_spec |
| 365 |
|
| 366 |
; `frshiftexp_spec |
| 367 |
; `ldshiftexp_spec |
| 368 |
|
| 369 |
; `next_up_spec |
| 370 |
; `next_down_spec |
| 371 |
]. |
| 372 |
(* Spec to check that evaluation mechanisms agree for each operator. *) |
| 373 |
#[local] Notation reflspec1 f := (fun x => @eq_refl _ (f x)). |
| 374 |
#[local] Notation reflspec2 f := (fun x y => @eq_refl _ (f x y)). |
| 375 |
Definition op_spec_list : list SPEC := |
| 376 |
[ ` (reflspec1 PrimFloat.classify) |
| 377 |
; ` (reflspec1 PrimFloat.abs) |
| 378 |
; ` (reflspec1 PrimFloat.sqrt) |
| 379 |
; ` (reflspec1 PrimFloat.opp) |
| 380 |
; ` (reflspec2 PrimFloat.eqb) |
| 381 |
; ` (reflspec2 PrimFloat.ltb) |
| 382 |
; ` (reflspec2 PrimFloat.leb) |
| 383 |
; ` (reflspec2 PrimFloat.compare) |
| 384 |
; ` (reflspec2 PrimFloat.Leibniz.eqb) |
| 385 |
; ` (reflspec2 PrimFloat.mul) |
| 386 |
; ` (reflspec2 PrimFloat.add) |
| 387 |
; ` (reflspec2 PrimFloat.sub) |
| 388 |
; ` (reflspec2 PrimFloat.div) |
| 389 |
; ` (reflspec1 PrimFloat.of_uint63) |
| 390 |
; ` (reflspec1 PrimFloat.normfr_mantissa) |
| 391 |
; ` (reflspec1 PrimFloat.frshiftexp) |
| 392 |
; ` (reflspec2 PrimFloat.ldshiftexp) |
| 393 |
; ` (reflspec1 PrimFloat.next_up) |
| 394 |
; ` (reflspec1 PrimFloat.next_down) |
| 395 |
]. |
| 396 |
(** *************************************************************************) |
| 397 |
|
| 398 |
(** * Utility definitions for managing lists specifications *) |
| 399 |
(** We unfold standard library constants early to guarantee that we |
| 400 |
won't run afoul of constants that show up in the specs themselves *) |
| 401 |
Definition map_fst : list ANNOTATED_BARE_SPEC -> list BARE_SPEC |
| 402 |
:= Eval cbv in ListDef.map (@fst _ _). |
| 403 |
Definition combine_annotations (orig : list ANNOTATED_BARE_SPEC) (result : list BARE_SPEC) : list ANNOTATED_BARE_SPEC |
| 404 |
:= Eval cbv in ListDef.map (fun '((_, anno), v) => (v, anno)) (combine orig result). |
| 405 |
(** The native compiler is much slower if we feed it the precomputed |
| 406 |
instantiations of specs, whereas we want to make sure that [simpl] |
| 407 |
and [cbn] have as few places to take the wrong path as possible. |
| 408 |
Reductions like [cbv] and [lazy] and the [vm] are mostly |
| 409 |
indifferent. So we maintain both [_red] versions for [simpl] and |
| 410 |
[cbn] and non-[_red] versions for [native_compute]. *) |
| 411 |
(** We make [_red] definitions [Let] statements, to work around |
| 412 |
COQBUG(https://github.com/rocq-prover/rocq/issues/4790) and avoid stack |
| 413 |
overflows in COQNATIVE *) |
| 414 |
|
| 415 |
(** * 1. Test the specs *) |
| 416 |
Section TestSpecs. |
| 417 |
|
| 418 |
Time Let specs_red : list ANNOTATED_BARE_SPEC |
| 419 |
:= Eval cbv [spec_list instantiate_all_ways] in instantiate_all_ways spec_list. (* 0.911 secs *) |
| 420 |
|
| 421 |
Let bare_specs_red : list BARE_SPEC |
| 422 |
:= Eval cbv [map_fst specs_red] in map_fst specs_red. |
| 423 |
|
| 424 |
Time Let bare_specs_vm : list BARE_SPEC |
| 425 |
:= Eval vm_compute in bare_specs_red. (* 1.934 secs *) |
| 426 |
|
| 427 |
(** ** Fuse in the annotations so that we can report errors nicely *) |
| 428 |
Time Let results_vm : list ANNOTATED_BARE_SPEC |
| 429 |
:= Eval cbv [combine_annotations bare_specs_vm specs_red] in combine_annotations specs_red bare_specs_vm. (* 1.374 secs *) |
| 430 |
|
| 431 |
(** ** Report results *) |
| 432 |
Time Ltac2 Eval report_results "vm" 'results_vm. (* 0.634 secs *) |
| 433 |
|
| 434 |
End TestSpecs. |
| 435 |
|
| 436 |
(** Check that the machinery indeed fail, providing useful error messages, |
| 437 |
on some purposely-wrong spec. *) |
| 438 |
Section NegativeTest. |
| 439 |
|
| 440 |
Axiom wrong_spec : forall x, (- x)%float = PrimFloat.abs x. |
| 441 |
|
| 442 |
Definition wrong_spec_list : list SPEC := cons ( `wrong_spec ) nil. |
| 443 |
|
| 444 |
Let wrong_specs : list ANNOTATED_BARE_SPEC |
| 445 |
:= Eval cbv [wrong_spec_list instantiate_all_ways] in instantiate_all_ways wrong_spec_list. |
| 446 |
|
| 447 |
Let wrong_bare_specs : list BARE_SPEC |
| 448 |
:= Eval cbv [map_fst wrong_specs] in map_fst wrong_specs. |
| 449 |
|
| 450 |
Let wrong_bare_specs_vm : list BARE_SPEC |
| 451 |
:= Eval vm_compute in wrong_bare_specs. |
| 452 |
|
| 453 |
(** ** Fuse in the annotations so that we can report errors nicely *) |
| 454 |
Let wrong_results_vm : list ANNOTATED_BARE_SPEC |
| 455 |
:= Eval cbv [combine_annotations wrong_bare_specs_vm wrong_specs] in combine_annotations wrong_specs wrong_bare_specs_vm. |
| 456 |
|
| 457 |
(** ** Report results *) |
| 458 |
Fail Ltac2 Eval report_results "vm" 'wrong_results_vm. |
| 459 |
(* |
| 460 |
Test Error: vm failed! |
| 461 |
Got: neg_infinity |
| 462 |
Expected: infinity |
| 463 |
In (wrong_spec infinity) (- infinity = abs infinity) |
| 464 |
... |
| 465 |
*) |
| 466 |
|
| 467 |
End NegativeTest. |
| 468 |
|
| 469 |
(** * 2. Test the evaluation mechanisms *) |
| 470 |
|
| 471 |
Definition op_specs : list ANNOTATED_BARE_SPEC |
| 472 |
:= instantiate_all_ways_nored op_spec_list. |
| 473 |
Time Let op_specs_red : list ANNOTATED_BARE_SPEC |
| 474 |
:= Eval cbv [instantiate_all_ways op_spec_list] in instantiate_all_ways op_spec_list. (* 0.883 secs *) |
| 475 |
Definition op_bare_specs : list BARE_SPEC |
| 476 |
:= map fst op_specs. |
| 477 |
Let op_bare_specs_red : list BARE_SPEC |
| 478 |
:= Eval cbv [map_fst op_specs_red] in map_fst op_specs_red. |
| 479 |
|
| 480 |
(** Machinery for evaluating independently the LHS of specs *) |
| 481 |
(** To check that all evaluation mechanism agree, we will then |
| 482 |
0. evaluate [op_specs] with [vm_compute] |
| 483 |
1. [extract_lhs] of [op_specs] |
| 484 |
2. evaluate LHS with each mechanism |
| 485 |
3. [merge_lhs] with results of 2. and 0. *) |
| 486 |
Inductive hlist := hnil | hcons {T} (x : T) (_ : hlist). |
| 487 |
Fixpoint extract_lhs (ls : list BARE_SPEC) : hlist |
| 488 |
:= match ls with |
| 489 |
| nil => hnil |
| 490 |
| x :: xs |
| 491 |
=> let rest := extract_lhs xs in |
| 492 |
match x with EQ v _ | IFF v _ => hcons v rest end |
| 493 |
end. |
| 494 |
Fixpoint merge_lhs (ls : list BARE_SPEC) (result : hlist) : list BARE_SPEC |
| 495 |
:= match ls, result with |
| 496 |
| nil, _ | _, hnil => nil |
| 497 |
| x :: xs, hcons v vs |
| 498 |
=> match x with |
| 499 |
| EQ _ x' => EQ v x' |
| 500 |
| IFF _ x' => IFF v x' |
| 501 |
end :: merge_lhs xs vs |
| 502 |
end. |
| 503 |
|
| 504 |
(** 0. evaluate [op_specs] with [vm_compute] *) |
| 505 |
Let op_bare_specs_vm : list BARE_SPEC |
| 506 |
:= Eval vm_compute in op_bare_specs_red. |
| 507 |
|
| 508 |
(** 1. [extract_lhs] of [op_specs] *) |
| 509 |
Definition LHS_op : hlist |
| 510 |
:= extract_lhs op_bare_specs. |
| 511 |
Let LHS_op_red : hlist |
| 512 |
:= Eval cbv [op_bare_specs_red extract_lhs] in extract_lhs op_bare_specs_red. |
| 513 |
|
| 514 |
(** 2. evaluate LHS with each mechanism *) |
| 515 |
|
| 516 |
(** *************************************************************************) |
| 517 |
(** * Computing reduced expressions *) |
| 518 |
(** EDIT HERE TO ADD MORE REDUCTION STRATEGIES *) |
| 519 |
(** ** [vm_compute] is ommited as it is the reference *) |
| 520 |
(** ** [native_compute] *) |
| 521 |
(** Native is slow at compiling big code, so we start from smaller code *) |
| 522 |
Let LHS_op_native := Eval native_compute in extract_lhs op_bare_specs. |
| 523 |
|
| 524 |
(** ** [hnf] *) |
| 525 |
(** recursively applies hnf to all elements of the list *) |
| 526 |
Ltac2 rec eval_hnf_hlist (c : constr) : constr |
| 527 |
:= lazy_match! c with |
| 528 |
| hcons ?h ?t => |
| 529 |
let h := Std.eval_hnf h in |
| 530 |
let t := eval_hnf_hlist t in |
| 531 |
'(hcons $h $t) |
| 532 |
| hnil => 'hnil |
| 533 |
end. |
| 534 |
Time Let LHS_op_hnf := ltac2:(let l := Std.eval_hnf 'LHS_op_red in let x := eval_hnf_hlist l in exact $x). (* 16.309 secs *) |
| 535 |
|
| 536 |
(** ** [cbn] *) |
| 537 |
Time Let LHS_op_cbn := Eval cbn in ltac2:(let l := Std.eval_hnf 'LHS_op_red in exact $l). (* 0.25 secs *) |
| 538 |
|
| 539 |
(** ** [simpl] *) |
| 540 |
Time Let LHS_op_simpl := Eval simpl in ltac2:(let l := Std.eval_hnf 'LHS_op_red in exact $l). (* 0.296 secs *) |
| 541 |
|
| 542 |
(** ** [cbv] *) |
| 543 |
Time Let LHS_op_cbv := Eval cbv in ltac2:(let l := Std.eval_hnf 'LHS_op_red in exact $l). (* 0.292 secs *) |
| 544 |
|
| 545 |
(** ** [lazy] *) |
| 546 |
Time Let LHS_op_lazy := Eval lazy in ltac2:(let l := Std.eval_hnf 'LHS_op_red in exact $l). (* 0.259 secs *) |
| 547 |
|
| 548 |
(** 3. [merge_lhs] with results of 2. and 0. *) |
| 549 |
|
| 550 |
(** ** fuse the results of vm RHS (vm because it's fast) back into cbn/hnf/simpl LHS for comparison *) |
| 551 |
Let op_bare_specs_native : list BARE_SPEC |
| 552 |
:= Eval cbv [merge_lhs op_bare_specs_vm LHS_op_native] in merge_lhs op_bare_specs_vm LHS_op_native. |
| 553 |
Let op_bare_specs_hnf : list BARE_SPEC |
| 554 |
:= Eval cbv [merge_lhs op_bare_specs_vm LHS_op_hnf] in merge_lhs op_bare_specs_vm LHS_op_hnf. |
| 555 |
Let op_bare_specs_cbn : list BARE_SPEC |
| 556 |
:= Eval cbv [merge_lhs op_bare_specs_vm LHS_op_cbn] in merge_lhs op_bare_specs_vm LHS_op_cbn. |
| 557 |
Let op_bare_specs_simpl : list BARE_SPEC |
| 558 |
:= Eval cbv [merge_lhs op_bare_specs_vm LHS_op_simpl] in merge_lhs op_bare_specs_vm LHS_op_simpl. |
| 559 |
Let op_bare_specs_cbv : list BARE_SPEC |
| 560 |
:= Eval cbv [merge_lhs op_bare_specs_vm LHS_op_cbv] in merge_lhs op_bare_specs_vm LHS_op_cbv. |
| 561 |
Let op_bare_specs_lazy : list BARE_SPEC |
| 562 |
:= Eval cbv [merge_lhs op_bare_specs_vm LHS_op_lazy] in merge_lhs op_bare_specs_vm LHS_op_lazy. |
| 563 |
|
| 564 |
(** ** Fuse in the annotations so that we can report errors nicely *) |
| 565 |
Time Let op_results_native : list ANNOTATED_BARE_SPEC |
| 566 |
:= Eval cbv [combine_annotations op_specs_red op_bare_specs_native] in combine_annotations op_specs_red op_bare_specs_native. (* 0.826 secs *) |
| 567 |
Time Let op_results_hnf : list ANNOTATED_BARE_SPEC |
| 568 |
:= Eval cbv [combine_annotations op_specs_red op_bare_specs_hnf] in combine_annotations op_specs_red op_bare_specs_hnf. (* 0.83 secs *) |
| 569 |
Time Let op_results_cbn : list ANNOTATED_BARE_SPEC |
| 570 |
:= Eval cbv [combine_annotations op_specs_red op_bare_specs_cbn] in combine_annotations op_specs_red op_bare_specs_cbn. (* 0.83 secs *) |
| 571 |
Time Let op_results_simpl : list ANNOTATED_BARE_SPEC |
| 572 |
:= Eval cbv [combine_annotations op_specs_red op_bare_specs_simpl] in combine_annotations op_specs_red op_bare_specs_simpl. (* 0.865 secs *) |
| 573 |
Time Let op_results_cbv : list ANNOTATED_BARE_SPEC |
| 574 |
:= Eval cbv [combine_annotations op_specs_red op_bare_specs_cbv] in combine_annotations op_specs_red op_bare_specs_cbv. (* 0.845 secs *) |
| 575 |
Time Let op_results_lazy : list ANNOTATED_BARE_SPEC |
| 576 |
:= Eval cbv [combine_annotations op_specs_red op_bare_specs_lazy] in combine_annotations op_specs_red op_bare_specs_lazy. (* 0.812 secs *) |
| 577 |
|
| 578 |
(** ** Report results *) |
| 579 |
Set Printing Depth 100000000. |
| 580 |
Ltac2 Eval report_results "native" 'op_results_native. |
| 581 |
Ltac2 Eval report_results "hnf" 'op_results_hnf. |
| 582 |
Ltac2 Eval report_results "cbn" 'op_results_cbn. |
| 583 |
Ltac2 Eval report_results "simpl" 'op_results_simpl. |
| 584 |
Ltac2 Eval report_results "cbv" 'op_results_cbv. |
| 585 |
Ltac2 Eval report_results "lazy" 'op_results_lazy. |
| 586 |
|
| 587 |
End __WORK_AROUND_COQBUG_4790. |