Skip to content

Commit d702f18

Browse files
authored
Fix float ops (#768)
* Remove extraction hack for hexadecimalString/decimalString Fix string_of_float * Fix equality decision on primitive values
1 parent 928677a commit d702f18

File tree

9 files changed

+52
-46
lines changed

9 files changed

+52
-46
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,6 @@ test-suite/metacoq-config
362362
test-suite/plugin-demo/_CoqProject
363363
test-suite/plugin-demo/_PluginProject
364364
test-suite/plugin-demo/metacoq-config
365+
erasure/META
366+
safechecker/META
367+
template-coq/META

pcuic/theories/utils/PCUICPrimitive.v

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,38 +71,44 @@ Qed.
7171

7272
#[global]
7373
Instance reflect_eq_spec_float : ReflectEq SpecFloat.spec_float := EqDec_ReflectEq _.
74-
75-
(* #[program]
76-
#[global]
77-
Instance reflect_eq_float64 : ReflectEq float64_model :=
78-
{ eqb x y := eqb (proj1_sig x) (proj1_sig y) }.
79-
Next Obligation.
80-
cbn -[eqb].
81-
intros x y.
82-
elim: eqb_spec. constructor.
83-
now apply exist_irrel_eq.
84-
intros neq; constructor => H'; apply neq; now subst x.
85-
Qed. *)
8674

87-
(** Propositional UIP is needed below *)
88-
Set Equations With UIP.
89-
90-
#[global]
91-
Instance prim_model_eqdec {term} (*e : EqDec term*) : forall p : prim_tag, EqDec (prim_model term p).
92-
Proof. eqdec_proof. Qed.
75+
Equations eqb_prim_model {term} {t : prim_tag} (x y : prim_model term t) : bool :=
76+
| primIntModel x, primIntModel y := ReflectEq.eqb x y
77+
| primFloatModel x, primFloatModel y := ReflectEq.eqb x y.
78+
79+
#[global, program]
80+
Instance prim_model_reflecteq {term} {p : prim_tag} : ReflectEq (prim_model term p) :=
81+
{| ReflectEq.eqb := eqb_prim_model |}.
82+
Next Obligation.
83+
intros. depelim x; depelim y; simp eqb_prim_model.
84+
case: ReflectEq.eqb_spec; constructor; subst; auto. congruence.
85+
case: ReflectEq.eqb_spec; constructor; subst; auto. congruence.
86+
Qed.
9387

9488
#[global]
95-
Instance prim_tag_model_eqdec term : EqDec (prim_val term).
96-
Proof. eqdec_proof. Defined.
89+
Instance prim_model_eqdec {term} : forall p : prim_tag, EqDec (prim_model term p) := _.
90+
91+
Equations eqb_prim_val {term} (x y : prim_val term) : bool :=
92+
| (primInt; i), (primInt; i') := ReflectEq.eqb i i'
93+
| (primFloat; f), (primFloat; f') := ReflectEq.eqb f f'
94+
| x, y := false.
95+
96+
#[global, program]
97+
Instance prim_val_reflect_eq {term} : ReflectEq (prim_val term) :=
98+
{| ReflectEq.eqb := eqb_prim_val |}.
99+
Next Obligation.
100+
intros. funelim (eqb_prim_val x y); simp eqb_prim_val.
101+
case: ReflectEq.eqb_spec; constructor; subst; auto. intros H; noconf H. cbn in n. auto.
102+
constructor. intros H; noconf H; auto.
103+
constructor. intros H; noconf H; auto.
104+
case: ReflectEq.eqb_spec; constructor; subst; auto. intros H; noconf H; auto.
105+
Qed.
97106

98107
#[global]
99-
Instance prim_val_reflect_eq term : ReflectEq (prim_val term) := EqDec_ReflectEq _.
108+
Instance prim_tag_model_eqdec term : EqDec (prim_val term) := _.
100109

101110
(** Printing *)
102111

103-
Definition string_of_float64_model (f : float64_model) :=
104-
"<>".
105-
106112
Definition string_of_prim {term} (soft : term -> string) (p : prim_val term) : string :=
107113
match p.π2 return string with
108114
| primIntModel f => "(int: " ^ string_of_prim_int f ^ ")"

safechecker/theories/Extraction.v

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ From MetaCoq.SafeChecker Require Import PCUICSafeChecker PCUICSafeConversion
88
99
Any extracted code planning to link with the plugin's OCaml reifier
1010
should use these same directives for consistency.
11-
*)
12-
13-
(* Ignore [Decimal.int] before the extraction issue is solved:
14-
https://github.com/coq/coq/issues/7017. *)
15-
Extract Inductive Decimal.int => unit [ "(fun _ -> ())" "(fun _ -> ())" ] "(fun _ _ _ -> assert false)".
16-
Extract Inductive Hexadecimal.int => unit [ "(fun _ -> ())" "(fun _ -> ())" ] "(fun _ _ _ -> assert false)".
17-
Extract Inductive Number.int => unit [ "(fun _ -> ())" "(fun _ -> ())" ] "(fun _ _ _ -> assert false)".
11+
*)
1812

1913
(** Here we could extract uint63_from/to_model to the identity *)
2014

template-coq/_PluginProject

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ gen-src/mCReflect.mli
122122
gen-src/mCReflect.ml
123123
gen-src/primFloat.mli
124124
gen-src/primFloat.ml
125-
#gen-src/decimalString.mli
126-
#gen-src/decimalString.ml
125+
gen-src/hexadecimalString.mli
126+
gen-src/hexadecimalString.ml
127+
gen-src/decimalString.mli
128+
gen-src/decimalString.ml
127129
gen-src/mCString.ml
128130
gen-src/mCString.mli
129131
gen-src/mCUtils.ml

template-coq/gen-src/metacoq_template_plugin.mlpack

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Caml_byte
2525
ByteCompare
2626
ByteCompareSpec
2727
String0
28+
HexadecimalString
2829
Orders
2930
OrdersTac
3031
OrdersFacts
@@ -71,6 +72,7 @@ Zpower
7172
SpecFloat
7273
PrimFloat
7374
FloatOps
75+
DecimalString
7476
MCString
7577
MCUtils
7678
Signature

template-coq/theories/BasicAst.v

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,3 @@ Definition prec := 53%Z.
749749
Definition emax := 1024%Z.
750750
(** We consider valid binary encordings of floats as our model *)
751751
Definition float64_model := sig (SpecFloat.valid_binary prec emax).
752-
753-
Definition string_of_float64_model (i : float64_model) :=
754-
"<float>".

template-coq/theories/EnvironmentTyping.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ Module GlobalMaps (T: Term) (E: EnvironmentSig T) (TU : TermUtils T E) (ET: EnvT
11491149
Definition fresh_global (s : kername) (g : global_declarations) : Prop :=
11501150
Forall (fun g => g.1 <> s) g.
11511151

1152-
Record on_global_decls_data {cf:checker_flags} (univs : ContextSet.t) retro (Σ : global_declarations) (kn : kername) (d : global_decl) :=
1152+
Record on_global_decls_data (univs : ContextSet.t) retro (Σ : global_declarations) (kn : kername) (d : global_decl) :=
11531153
{
11541154
kn_fresh : fresh_global kn Σ ;
11551155
udecl := universes_decl_of_decl d ;

template-coq/theories/Extraction.v

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ From Coq Require Ascii Extraction ZArith NArith.
99
From MetaCoq.Template Require Import utils Ast Reflect Induction.
1010
From Coq Require Import FSets ExtrOcamlBasic ExtrOCamlFloats ExtrOCamlInt63.
1111

12-
(* Ignore [Decimal.int] before the extraction issue is solved:
13-
https://github.com/coq/coq/issues/7017. *)
14-
Extract Inductive Decimal.int => unit [ "(fun _ -> ())" "(fun _ -> ())" ] "(fun _ _ _ -> assert false)".
15-
Extract Inductive Hexadecimal.int => unit [ "(fun _ -> ())" "(fun _ -> ())" ] "(fun _ _ _ -> assert false)".
16-
Extract Inductive Number.int => unit [ "(fun _ -> ())" "(fun _ -> ())" ] "(fun _ _ _ -> assert false)".
17-
1812
Extract Inductive Equations.Init.sigma => "( * )" ["(,)"].
1913
Extract Constant Equations.Init.pr1 => "fst".
2014
Extract Constant Equations.Init.pr2 => "snd".

template-coq/theories/Primitive.v

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(* Primitive types *)
22

3-
From Coq Require Import Uint63 PrimFloat.
3+
From Coq Require Import Uint63 PrimFloat SpecFloat FloatOps ZArith HexadecimalString.
44
From MetaCoq.Template Require Import bytestring MCString.
55
Local Open Scope bs.
66

@@ -14,5 +14,13 @@ Definition string_of_prim_int (i:Uint63.int) : string :=
1414
(* Better? DecimalString.NilZero.string_of_uint (BinNat.N.to_uint (BinInt.Z.to_N (Int63.to_Z i))). ? *)
1515
string_of_Z (Uint63.to_Z i).
1616

17-
Definition string_of_float (f : PrimFloat.float) :=
18-
"<float>".
17+
Definition string_of_float (f : PrimFloat.float) : string :=
18+
match Prim2SF f with
19+
| S754_zero sign => if sign then "-0" else "0"
20+
| S754_infinity sign => if sign then "-INFINITY" else "INFINITY"
21+
| S754_nan => "NAN"
22+
| S754_finite sign p z =>
23+
let abs := "0x" ++ bytestring.String.of_string (Numbers.HexadecimalString.NilZero.string_of_uint (Pos.to_hex_uint p)) ++ "p" ++
24+
bytestring.String.of_string (Numbers.DecimalString.NilZero.string_of_int (Z.to_int z))
25+
in if sign then "-" ++ abs else abs
26+
end.

0 commit comments

Comments
 (0)