| 1 |
From mathcomp Require Import ssreflect ssrfun ssrbool eqtype ssrnat div seq. |
| 2 |
From mathcomp Require Import path choice fintype tuple finset bigop. |
| 3 |
|
| 4 |
From elpi Require Import elpi. |
| 5 |
Require Import test_param hrel ops. |
| 6 |
|
| 7 |
Set Implicit Arguments. |
| 8 |
Unset Strict Implicit. |
| 9 |
Unset Printing Implicit Defensive. |
| 10 |
|
| 11 |
Class refines X Y (R : X -> Y -> Type) x y : Type := Refines : R x y. |
| 12 |
Arguments refines {_ _} R%rel _ _. |
| 13 |
|
| 14 |
Lemma refines_apply |
| 15 |
{A B} {R : A -> B -> Type} |
| 16 |
{C D} {R' : C -> D -> Type} : |
| 17 |
forall (a : A) (b : B), refines R a b -> |
| 18 |
forall (c : A -> C) (d : B -> D), refines (R ==> R') c d -> |
| 19 |
refines R' (c a) (d b). |
| 20 |
Proof. by move=> a b Rab c d; apply. Qed. |
| 21 |
|
| 22 |
Cd "~/git/coq-elpi". |
| 23 |
Elpi Query param "with-TC-param (param {{O}} X Y)". |
| 24 |
|
| 25 |
Elpi Tactic coqeal. |
| 26 |
Elpi Accumulate File "paramX_lib.elpi" From elpi.apps.derive. |
| 27 |
Elpi Accumulate File "attic/coq-EAL.elpi". |
| 28 |
Elpi Typecheck coqeal. |
| 29 |
|
| 30 |
Section CoqEAL. |
| 31 |
|
| 32 |
Context (N : Type). |
| 33 |
Context (R : nat -> N -> Type). |
| 34 |
Context (zero one : N). |
| 35 |
Context (add : N -> N -> N). |
| 36 |
Context (N_of_bool : bool -> N). |
| 37 |
|
| 38 |
Context {Rzero : refines R 0 zero}. |
| 39 |
Context {Rone : refines R 1 one}. |
| 40 |
Context {Radd : refines (R ==> R ==> R)%rel addn add}. |
| 41 |
|
| 42 |
Context {Rfrombool : forall b, refines R (nat_of_bool b) (N_of_bool b)}. |
| 43 |
|
| 44 |
Lemma test_refines0 : { x : _ & refines R 1 x}. |
| 45 |
Proof. |
| 46 |
eexists. |
| 47 |
elpi coqeal; shelve_unifiable. |
| 48 |
Abort. |
| 49 |
|
| 50 |
Lemma test_refines0 : { x : _ & refines R true x}. |
| 51 |
Proof. |
| 52 |
eexists. |
| 53 |
elpi coqeal; shelve_unifiable. |
| 54 |
Qed. |
| 55 |
|
| 56 |
|
| 57 |
Lemma test_refines1 : { x : _ & refines (R ==> R) (addn 1) x}. |
| 58 |
Proof. |
| 59 |
eexists. |
| 60 |
elpi coqeal; shelve_unifiable. |
| 61 |
Qed. |
| 62 |
|
| 63 |
Lemma test_refines2 : { x : _ & refines R (addn 1 0) x}. |
| 64 |
Proof. |
| 65 |
eexists. |
| 66 |
elpi coqeal; shelve_unifiable. |
| 67 |
Qed. |
| 68 |
|
| 69 |
Lemma test_refines_big : { x : N & refines R (0 + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + (((((((((0 + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + (((((((0 + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + (((((0 + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + 1)) + 1) + 1)) + 1)) + 1) + 1)))))))) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + (((((0 + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + 1)) + 1) + 1)) + 1)) + 1) + 1)))))))) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + 1)) + 1) + 1)) + 1)) + 1) + 1)))))))) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + (((((((0 + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + (((((0 + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + true)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + 1)) + 1) + 1)) + 1)) + 1) + 1)))))))) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + (((((0 + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + 1)) + 1) + 1)) + 1)) + 1) + 1)))))))) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + 1)) + 1) + 1)) + 1)) + 1) + 1)))))))) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + (((((0 + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + 1)) + 1) + 1)) + 1)) + 1) + 1)))))))) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + (((((0 + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + 1)) + 1) + 1)) + 1)) + 1) + 1)))))))) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + ((0 + (((((0 + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + ((0 + ((0 + 1)) + 1) + 1)) + 1)) + 1) + 1)) + 1) + 1)) + 1)) + 1) + 1))))))))%nat x}. |
| 70 |
Proof. |
| 71 |
eexists. |
| 72 |
Time elpi coqeal. |
| 73 |
Show Proof. |
| 74 |
Qed. |
| 75 |
|
| 76 |
Elpi Query coqeal " |
| 77 |
coq.locate ""nat_ind"" (const GR), |
| 78 |
coq.env.typeof GR (prod N _ _), |
| 79 |
$term_to_string N S, |
| 80 |
S' is S ^ ""1"", |
| 81 |
coq.say S'. |
| 82 |
". |
| 83 |
|
| 84 |
End CoqEAL. |
| 85 |
|
| 86 |
|
| 87 |
Require Import ZArith pos. |
| 88 |
|
| 89 |
Section positive_op. |
| 90 |
|
| 91 |
Definition positive_of_pos (p : pos) : positive := Pos.of_nat (val p). |
| 92 |
Definition pos_of_positive (p : positive) : pos := insubd pos1 (Pos.to_nat p). |
| 93 |
|
| 94 |
Global Instance spec_positive : Op.spec_of positive pos := pos_of_positive. |
| 95 |
Global Instance implem_positive : Op.implem_of pos positive := positive_of_pos. |
| 96 |
Global Instance one_positive : Op.one_of positive := xH. |
| 97 |
Global Instance add_positive : Op.add_of positive := Pos.add. |
| 98 |
Global Instance sub_positive : Op.sub_of positive := Pos.sub. |
| 99 |
Global Instance mul_positive : Op.mul_of positive := Pos.mul. |
| 100 |
Global Instance le_positive : Op.leq_of positive := Pos.leb. |
| 101 |
Global Instance lt_positive : Op.lt_of positive := Pos.ltb. |
| 102 |
Global Instance eq_positive : Op.eq_of positive := Pos.eqb. |
| 103 |
Global Instance exp_positive : Op.exp_of positive positive := Pos.pow. |
| 104 |
End positive_op. |
| 105 |
|
| 106 |
Section positive_theory. |
| 107 |
|
| 108 |
Local Open Scope rel_scope. |
| 109 |
|
| 110 |
Lemma positive_of_posK : cancel positive_of_pos pos_of_positive. |
| 111 |
Proof. |
| 112 |
move=> n /=; rewrite /positive_of_pos /pos_of_positive /=. |
| 113 |
apply: val_inj; rewrite Nat2Pos.id ?insubdK -?topredE ?valP //. |
| 114 |
by apply/eqP; rewrite -lt0n valP. |
| 115 |
Qed. |
| 116 |
|
| 117 |
Lemma to_natE : forall (p : positive), Pos.to_nat p = nat_of_pos p. |
| 118 |
Proof. |
| 119 |
by elim=> //= p <-; |
| 120 |
rewrite ?Pos2Nat.inj_xI ?Pos2Nat.inj_xO NatTrec.trecE -mul2n. |
| 121 |
Qed. |
| 122 |
|
| 123 |
Lemma to_nat_gt0 p : 0 < Pos.to_nat p. |
| 124 |
Proof. |
| 125 |
by rewrite to_natE; elim: p => //= p; rewrite NatTrec.trecE double_gt0. |
| 126 |
Qed. |
| 127 |
Hint Resolve to_nat_gt0. |
| 128 |
|
| 129 |
Lemma pos_of_positiveK : cancel pos_of_positive positive_of_pos. |
| 130 |
Proof. |
| 131 |
move=> n /=; rewrite /positive_of_pos /pos_of_positive /=. |
| 132 |
by rewrite val_insubd to_nat_gt0 Pos2Nat.id. |
| 133 |
Qed. |
| 134 |
|
| 135 |
Definition Rpos := fun_hrel pos_of_positive. |
| 136 |
|
| 137 |
Lemma RposE (p : pos) (x : positive) : |
| 138 |
refines Rpos p x -> p = pos_of_positive x. |
| 139 |
Proof. by []. Qed. |
| 140 |
|
| 141 |
Lemma RposI (p : pos) (x : positive) : |
| 142 |
refines Rpos p x -> x = positive_of_pos p. |
| 143 |
Proof. by move=> /RposE ->; rewrite pos_of_positiveK. Qed. |
| 144 |
|
| 145 |
Global Instance Rpos_spec_pos_r x : refines Rpos (Op.spec x) x. |
| 146 |
Proof. by []. Qed. |
| 147 |
|
| 148 |
(* Global Instance Rpos_spec_pos_l : refines (Rpos ==> pos_R) spec_id spec. *) |
| 149 |
(* Proof. *) |
| 150 |
(* rewrite refinesE=> x x'. *) |
| 151 |
(* rewrite -[Rpos]refinesE=> rx. *) |
| 152 |
(* rewrite [spec _]RposE [y in pos_of_positive y]RposI positive_of_posK /spec_id. *) |
| 153 |
(* exact: pos_Rxx. *) |
| 154 |
(* Qed. *) |
| 155 |
|
| 156 |
Global Instance Rpos_spec : refines (Rpos ==> Logic.eq) Op.spec_id Op.spec. |
| 157 |
Proof. by rewrite []. Qed. |
| 158 |
|
| 159 |
Global Instance Rpos_implem : refines (Logic.eq ==> Rpos) Op.implem_id implem. |
| 160 |
Proof. |
| 161 |
rewrite refinesE=> _ x ->. |
| 162 |
case: x=> n ngt0. |
| 163 |
by rewrite /Rpos /fun_hrel positive_of_posK. |
| 164 |
Qed. |
| 165 |
|
| 166 |
Global Instance Rpos_1 : refines Rpos (pos1 : pos) (1%C : positive). |
| 167 |
Proof. by rewrite !refinesE; apply: val_inj; rewrite /= insubdK. Qed. |
| 168 |
|
| 169 |
Global Instance Rpos_add : refines (Rpos ==> Rpos ==> Rpos) add_pos +%C. |
| 170 |
Proof. |
| 171 |
rewrite refinesE => _ x <- _ y <-; apply: val_inj. |
| 172 |
rewrite !val_insubd Pos2Nat.inj_add. |
| 173 |
by move: (Pos2Nat.is_pos x) (Pos2Nat.is_pos y) => /leP -> /leP ->. |
| 174 |
Qed. |
| 175 |
|
| 176 |
Global Instance Rpos_mul : refines (Rpos ==> Rpos ==> Rpos) mul_pos *%C. |
| 177 |
Proof. |
| 178 |
rewrite refinesE => _ x <- _ y <-; apply: val_inj. |
| 179 |
rewrite !val_insubd Pos2Nat.inj_mul. |
| 180 |
by move: (Pos2Nat.is_pos x) (Pos2Nat.is_pos y) => /leP -> /leP ->. |
| 181 |
Qed. |
| 182 |
|
| 183 |
Global Instance Rpos_sub : refines (Rpos ==> Rpos ==> Rpos) sub_pos sub_op. |
| 184 |
Proof. |
| 185 |
rewrite refinesE => _ x <- _ y <-; apply: val_inj; rewrite !val_insubd. |
| 186 |
move: (Pos2Nat.is_pos x) (Pos2Nat.is_pos y) => /leP -> /leP ->. |
| 187 |
have [/leP h|/leP h] := (ltnP (Pos.to_nat y) (Pos.to_nat x)). |
| 188 |
by have := (Pos2Nat.inj_sub x y); rewrite Pos2Nat.inj_lt => ->. |
| 189 |
rewrite /sub_op /sub_positive Pos.sub_le ?Pos2Nat.inj_le //. |
| 190 |
by rewrite subn_gt0 !ltnNge; move/leP: h ->. |
| 191 |
Qed. |
| 192 |
|
| 193 |
Global Instance Rpos_leq : refines (Rpos ==> Rpos ==> bool_R) leq_pos Op.leq. |
| 194 |
Proof. |
| 195 |
rewrite refinesE=> _ x <- _ y <-; |
| 196 |
rewrite /leq_op /le_positive /leq_pos !val_insubd. |
| 197 |
move: (Pos2Nat.is_pos x) (Pos2Nat.is_pos y) => /leP -> /leP ->. |
| 198 |
by case: (Pos.leb_spec0 _ _); move /Pos2Nat.inj_le /leP; |
| 199 |
[move ->|rewrite -eqbF_neg; move/eqP ->]. |
| 200 |
Qed. |
| 201 |
|
| 202 |
Global Instance Rpos_lt : refines (Rpos ==> Rpos ==> bool_R) lt_pos Op.lt. |
| 203 |
Proof. |
| 204 |
rewrite refinesE => /= _ x <- _ y <-; rewrite /lt_pos !val_insubd. |
| 205 |
move: (Pos2Nat.is_pos x) (Pos2Nat.is_pos y) => /leP -> /leP ->. |
| 206 |
have -> : (Pos.to_nat x < Pos.to_nat y) = (x < y)%C. |
| 207 |
by apply/ltP/idP => [|h]; rewrite -Pos2Nat.inj_lt -Pos.ltb_lt. |
| 208 |
exact: bool_Rxx. |
| 209 |
Qed. |
| 210 |
|
| 211 |
Global Instance Rpos_eq : refines (Rpos ==> Rpos ==> bool_R) eq_pos Op.eq. |
| 212 |
Proof. |
| 213 |
rewrite refinesE=> _ x <- _ y <-; rewrite /eq_op /eq_positive /eq_pos. |
| 214 |
case: (Pos.eqb_spec _ _)=> [->|h]. |
| 215 |
by rewrite eqxx. |
| 216 |
suff H : (pos_of_positive x == pos_of_positive y) = false. |
| 217 |
by rewrite H. |
| 218 |
by apply/negP=> [/eqP /(can_inj pos_of_positiveK)]. |
| 219 |
Qed. |
| 220 |
|
| 221 |
Lemma pos2nat_inj_exp x y : Pos.to_nat (x ^ y) = Pos.to_nat x ^ Pos.to_nat y. |
| 222 |
Proof. |
| 223 |
have pos2nat_pow_xO a b |
| 224 |
(hab : Pos.to_nat (a ^ b) = Pos.to_nat a ^ Pos.to_nat b) : |
| 225 |
Pos.to_nat (a ^ b~0) = (Pos.to_nat a ^ Pos.to_nat b) ^ 2. |
| 226 |
by rewrite -Z2Nat.inj_pos Pos2Z.inj_pow Pos2Z.inj_xO Z.mul_comm Z.pow_mul_r |
| 227 |
// Z.pow_2_r -Pos2Z.inj_pow Z2Nat.inj_mul // Z2Nat.inj_pos multE |
| 228 |
hab mulnn. |
| 229 |
elim: y=> [y ihy|y ihy|]. |
| 230 |
by rewrite Pos2Nat.inj_xI multE expnS [in _ ^ _]mulnC expnM Pos.xI_succ_xO |
| 231 |
Pos.pow_succ_r Pos2Nat.inj_mul multE pos2nat_pow_xO. |
| 232 |
by rewrite Pos2Nat.inj_xO multE mulnC expnM pos2nat_pow_xO. |
| 233 |
by rewrite Pos2Nat.inj_1 expn1 Pos.pow_1_r. |
| 234 |
Qed. |
| 235 |
|
| 236 |
Global Instance Rpos_exp : refines (Rpos ==> Rpos ==> Rpos) exp_pos exp_op. |
| 237 |
Proof. |
| 238 |
rewrite refinesE /exp_op /exp_positive=> _ x <- _ y <-. |
| 239 |
apply: val_inj. |
| 240 |
by rewrite !val_insubd expn_gt0 !to_nat_gt0 pos2nat_inj_exp. |
| 241 |
Qed. |
| 242 |
|
| 243 |
End positive_theory. |
| 244 |
|
| 245 |
Typeclasses Opaque pos_of_positive positive_of_pos. |
| 246 |
Global Opaque pos_of_positive positive_of_pos. |
| 247 |
|
| 248 |
Section binnat_op. |
| 249 |
|
| 250 |
Global Instance zero_N : Op.zero_of N := N.zero. |
| 251 |
Global Instance one_N : Op.one_of N := N.one. |
| 252 |
Global Instance add_N : Op.add_of N := N.add. |
| 253 |
|
| 254 |
Definition succN (n : N) : N := (1 + n)%C. |
| 255 |
|
| 256 |
Global Instance sub_N : Op.sub_of N := N.sub. |
| 257 |
Global Instance exp_N : Op.exp_of N N := N.pow. |
| 258 |
Global Instance mul_N : Op.mul_of N := N.mul. |
| 259 |
Global Instance div_N : Op.div_of N := N.div. |
| 260 |
Global Instance mod_N : Op.mod_of N := N.modulo. |
| 261 |
Global Instance eq_N : Op.eq_of N := N.eqb. |
| 262 |
Global Instance leq_N : Op.leq_of N := N.leb. |
| 263 |
Global Instance lt_N : Op.lt_of N := N.ltb. |
| 264 |
|
| 265 |
Global Instance cast_positive_N : Op.cast_of positive N := Npos. |
| 266 |
Global Instance cast_N_positive : Op.cast_of N positive := |
| 267 |
fun n => if n is Npos p then p else 1%C. |
| 268 |
|
| 269 |
Global Instance spec_N : Op.spec_of N nat := nat_of_bin. |
| 270 |
Global Instance implem_N : Op.implem_of nat N := bin_of_nat. |
| 271 |
|
| 272 |
End binnat_op. |
| 273 |
|
| 274 |
Section binnat_theory. |
| 275 |
|
| 276 |
Local Open Scope rel_scope. |
| 277 |
|
| 278 |
Definition Rnat : nat -> N -> Type := fun_hrel nat_of_bin. |
| 279 |
Instance ref_Rnat : 'refinement Rnat. |
| 280 |
|
| 281 |
Lemma RnatE (n : nat) (x : N) : refines_rec Rnat n x -> n = x. |
| 282 |
Proof. by rewrite refinesE. Qed. |
| 283 |
|
| 284 |
Global Instance Rnat_spec_r x : refines Rnat (spec x) x. |
| 285 |
Proof. by rewrite refinesE. Qed. |
| 286 |
|
| 287 |
Global Instance Rnat_spec : refines (Rnat ==> Logic.eq) Op.spec_id spec. |
| 288 |
Proof. by rewrite refinesE. Qed. |
| 289 |
|
| 290 |
Global Instance Rnat_implem : refines (Logic.eq ==> Rnat) Op.implem_id implem. |
| 291 |
Proof. |
| 292 |
rewrite !refinesE => x _ <-. |
| 293 |
by rewrite /Rnat /fun_hrel /implem /implem_N bin_of_natK. |
| 294 |
Qed. |
| 295 |
|
| 296 |
Global Instance Rnat_0 : refines Rnat 0 0%C. |
| 297 |
Proof. by rewrite refinesE. Qed. |
| 298 |
|
| 299 |
Global Instance Rnat_1 : refines Rnat 1%nat 1%C. |
| 300 |
Proof. by rewrite refinesE. Qed. |
| 301 |
|
| 302 |
Global Instance Rnat_add : refines (Rnat ==> Rnat ==> Rnat) addn +%C. |
| 303 |
Proof. |
| 304 |
by rewrite refinesE => _ x <- _ y <-; rewrite /Rnat /fun_hrel nat_of_add_bin. |
| 305 |
Qed. |
| 306 |
|
| 307 |
(* Hint Extern 999 (@refinement _ _ _) => *) |
| 308 |
(* (once lazymatch goal with |- ?g => idtac "trying " g; fail 1 end) *) |
| 309 |
(* : typeclass_instances. *) |
| 310 |
|
| 311 |
|
| 312 |
Global Instance Rnat_S : refines (Rnat ==> Rnat) S succN. |
| 313 |
Proof. by refines_abstrE; rewrite -add1n; apply/spec_refines. Qed. |
| 314 |
|
| 315 |
Lemma nat_of_binK : forall x, N.of_nat (nat_of_bin x) = x. |
| 316 |
Proof. |
| 317 |
by case => //= p; apply: Nnat.N2Nat.inj; rewrite Nnat.Nat2N.id /= to_natE. |
| 318 |
Qed. |
| 319 |
|
| 320 |
Global Instance Rnat_sub : refines (Rnat ==> Rnat ==> Rnat) subn sub_op. |
| 321 |
Proof. |
| 322 |
rewrite refinesE => _ x <- _ y <-. |
| 323 |
by apply: Nnat.Nat2N.inj; rewrite Nnat.Nat2N.inj_sub !nat_of_binK. |
| 324 |
Qed. |
| 325 |
|
| 326 |
Global Instance Rnat_mul : refines (Rnat ==> Rnat ==> Rnat) muln mul_op. |
| 327 |
Proof. |
| 328 |
rewrite refinesE => _ x <- _ y <-. |
| 329 |
by rewrite /Rnat /fun_hrel /= nat_of_mul_bin. |
| 330 |
Qed. |
| 331 |
|
| 332 |
Global Instance Rnat_div_eucl : |
| 333 |
refines (Rnat ==> Rnat ==> prod_R Rnat Rnat) edivn N.div_eucl. |
| 334 |
Proof. |
| 335 |
rewrite refinesE /Rnat /fun_hrel=> _ x <- _ y <-. |
| 336 |
rewrite edivn_def /=. |
| 337 |
case: x=> [|x] /=; first by rewrite div0n mod0n. |
| 338 |
case: y=> [|y] //=. |
| 339 |
have hspec := N.pos_div_eucl_spec x (N.pos y). |
| 340 |
have hrem := N.pos_div_eucl_remainder x (N.pos y). |
| 341 |
destruct N.pos_div_eucl. |
| 342 |
rewrite -[nat_of_pos _]/(nat_of_bin (N.pos _)) hspec /= {hspec}. |
| 343 |
rewrite nat_of_add_bin nat_of_mul_bin. |
| 344 |
have rem_lt_div : (n0 < N.pos y)%N. |
| 345 |
have pos_ne0 : N.pos y <> 0%num by []. |
| 346 |
have /= := hrem pos_ne0. |
| 347 |
rewrite /N.lt Nnat.N2Nat.inj_compare /= to_natE. |
| 348 |
move/nat_compare_lt/ltP. |
| 349 |
case: n0 {hrem}=> //= p. |
| 350 |
by rewrite to_natE. |
| 351 |
rewrite modnMDl modn_small ?rem_lt_div // divnMDl /= -?to_natE ?to_nat_gt0 //. |
| 352 |
by rewrite divn_small ?addn0 // ?to_natE. |
| 353 |
Qed. |
| 354 |
|
| 355 |
Global Instance Rnat_div : refines (Rnat ==> Rnat ==> Rnat) divn div_op. |
| 356 |
Proof. refines_abstrE; rewrite /divn; apply/spec_refines. Qed. |
| 357 |
|
| 358 |
Global Instance Rnat_mod : refines (Rnat ==> Rnat ==> Rnat) modn mod_op. |
| 359 |
Proof. by refines_abstrE; rewrite modn_def; apply/spec_refines. Qed. |
| 360 |
|
| 361 |
Global Instance Rnat_exp : refines (Rnat ==> Rnat ==> Rnat) expn exp_op. |
| 362 |
Proof. |
| 363 |
rewrite refinesE => _ x <- _ y <-; rewrite /Rnat /fun_hrel /=. |
| 364 |
rewrite /exp_op /exp_N /N.pow. |
| 365 |
case: x y => [|x] [|y] //. |
| 366 |
rewrite exp0n //=; elim: y => //= p. |
| 367 |
by rewrite natTrecE double_gt0. |
| 368 |
have nat_of_binposE p : nat_of_bin (N.pos p) = Pos.to_nat p. |
| 369 |
elim: p=> [p ihp|p ihp|] /=; last (by rewrite Pos2Nat.inj_1); |
| 370 |
by rewrite ?(Pos2Nat.inj_xI, Pos2Nat.inj_xO) multE NatTrec.doubleE to_natE |
| 371 |
mul2n. |
| 372 |
by rewrite !nat_of_binposE pos2nat_inj_exp. |
| 373 |
Qed. |
| 374 |
|
| 375 |
Global Instance Rnat_eq : refines (Rnat ==> Rnat ==> bool_R) eqtype.eq_op Op.eq. |
| 376 |
Proof. |
| 377 |
rewrite refinesE=> _ x <- _ y <-; rewrite /eq_op /eq_N. |
| 378 |
case: (N.eqb_spec _ _) => [->|/eqP hneq]; first by rewrite eqxx. |
| 379 |
suff H : (nat_of_bin x == nat_of_bin y) = false by rewrite H. |
| 380 |
by apply/negP => [/eqP /(can_inj nat_of_binK)]; apply/eqP. |
| 381 |
Qed. |
| 382 |
|
| 383 |
Global Instance refines_spec_bool : |
| 384 |
refines (bool_R ==> eq) Op.spec_id id. |
| 385 |
Proof. by rewrite refinesE => ??[]. Qed. |
| 386 |
|
| 387 |
Global Instance Rnat_leq : refines (Rnat ==> Rnat ==> bool_R) ssrnat.leq Op.leq. |
| 388 |
Proof. |
| 389 |
rewrite refinesE=> _ x <- _ y <-; rewrite /leq_op /leq_N /leq. |
| 390 |
case: (N.leb_spec0 _ _)=> [/N.sub_0_le|] xBy. |
| 391 |
by rewrite [_ == _](coqeal unify) [sub_op _ _]xBy. |
| 392 |
suff H : (nat_of_bin x - nat_of_bin y == 0) = false. |
| 393 |
by rewrite /(_ <= _)%N H. |
| 394 |
apply/negP=> /eqP; rewrite [x - y]RnatE [0]RnatE. |
| 395 |
by move/(can_inj nat_of_binK)/N.sub_0_le. |
| 396 |
Qed. |
| 397 |
|
| 398 |
Global Instance Rnat_lt : refines (Rnat ==> Rnat ==> bool_R) ltn lt_op. |
| 399 |
Proof. |
| 400 |
refines_abstr; rewrite refinesE /ltn /= ltnNge; apply/eq_spec_refines. |
| 401 |
by rewrite /lt_op /lt_N N.ltb_antisym. |
| 402 |
Qed. |
| 403 |
|
| 404 |
Global Instance Rnat_cast_positive_N : |
| 405 |
refines (Rpos ==> Rnat) val (cast : positive -> N). |
| 406 |
Proof. |
| 407 |
rewrite refinesE /cast /Rnat /fun_hrel => x x' rx. |
| 408 |
by rewrite [x]RposE val_insubd to_nat_gt0 to_natE. |
| 409 |
Qed. |
| 410 |
|
| 411 |
Global Instance Rnat_cast_N_positive : |
| 412 |
refines (Rnat ==> Rpos) (insubd pos1) (cast : N -> positive). |
| 413 |
Proof. |
| 414 |
rewrite refinesE=> x x' rx; rewrite [x]RnatE. |
| 415 |
case: x' {x rx} => [|p] /=; last by rewrite -to_natE. |
| 416 |
rewrite /insubd insubF //= /cast. |
| 417 |
by apply (@spec_refinesP _ _ _ _ _ _ _ _ _). |
| 418 |
Qed. |
| 419 |
|
| 420 |
End binnat_theory. |
| 421 |
|
| 422 |
Typeclasses Opaque nat_of_bin bin_of_nat. |
| 423 |
Global Opaque nat_of_bin bin_of_nat. |
| 424 |
|
| 425 |
Section test. |
| 426 |
|
| 427 |
Instance : 'refinement Rnat. |
| 428 |
|
| 429 |
Lemma test : 10000%num * 10000%num * (99999999%num + 1) = |
| 430 |
10000000000000000%num. |
| 431 |
Proof. |
| 432 |
by rewrite [X in X = _](coqeal unify); reflexivity. |
| 433 |
Qed. |
| 434 |
|
| 435 |
Lemma test' : 10000%num * 10000%num * (99999999%num + 1) = |
| 436 |
10000000000000000%num. |
| 437 |
Proof. by apply/eqP; rewrite [_ == _](coqeal unify). Qed. |
| 438 |
|
| 439 |
End test. |
| 440 |
|
| 441 |
|