Skip to content

Commit 879d2d8

Browse files
committed
Port to derive.param2 of Coq-Elpi
1 parent 442e96e commit 879d2d8

File tree

7 files changed

+132
-80
lines changed

7 files changed

+132
-80
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ slices in the input.
4747
- License: [CeCILL-B Free Software License Agreement](CeCILL-B)
4848
- Compatible Rocq/Coq versions: 8.19 or later
4949
- Additional dependencies:
50+
- [Coq-Elpi](https://github.com/LPCIC/coq-elpi)
5051
- [MathComp](https://math-comp.github.io) ssreflect 2.3.0 or later
51-
- [Paramcoq](https://github.com/coq-community/paramcoq) 1.1.3 or later
5252
- [Mczify](https://github.com/math-comp/mczify) (required only for `icfp25/`)
5353
- [Equations](https://github.com/mattam82/Coq-Equations) (required only for `icfp25/`)
5454
- Rocq/Coq namespace: `stablesort`

icfp25/Section3.v

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
(* 3.4.3. *)
1313
(******************************************************************************)
1414

15+
From elpi Require Export derive.param2.
1516
From mathcomp Require Import ssreflect ssrfun ssrbool eqtype ssrnat seq path.
1617
From mathcomp Require Import zify.
17-
From Param Require Export Param.
1818
From Equations Require Import Equations.
1919

2020
Set Implicit Arguments.
2121
Unset Strict Implicit.
2222
Unset Printing Implicit Defensive.
2323

24-
Global Ltac destruct_reflexivity :=
25-
intros; repeat match goal with
26-
| [ x : _ |- _ = _ ] => destruct x; reflexivity; fail
27-
end.
28-
29-
Global Parametricity Tactic := destruct_reflexivity.
30-
31-
Parametricity bool.
32-
Parametricity nat.
33-
Parametricity list.
34-
Parametricity merge.
24+
Elpi derive.param2 bool.
25+
Elpi derive.param2 nat.
26+
Elpi derive.param2 list.
27+
Elpi derive.param2 pred.
28+
Elpi derive.param2 rel.
29+
Elpi derive.param2 merge.
30+
Elpi derive.param2 size.
31+
Elpi derive.param2 take.
32+
Elpi derive.param2 drop.
33+
Elpi derive.param2 foldr.
34+
Elpi derive.param2 map.
3535

3636
Local Lemma bool_R_refl b1 b2 : b1 = b2 -> bool_R b1 b2.
3737
Proof. by case: b1 => <-; constructor. Qed.
@@ -44,6 +44,22 @@ Local Lemma rel_map_map A B (f : A -> B) (l : seq A) (fl : seq B) :
4444
list_R (fun x y => f x = y) l fl -> fl = map f l.
4545
Proof. by elim/list_R_ind: l fl / => //= ? ? <- ? ? _ ->. Qed.
4646

47+
(* We define a non-mutual-fixpoint alternative of ssrnat.half, so that *)
48+
(* derive.param2 accepts it. *)
49+
Fixpoint half' (n : nat) : nat :=
50+
match n with
51+
| n.+2 => (half' n).+1
52+
| _ => 0
53+
end.
54+
55+
Lemma half'E : half' =1 half.
56+
Proof.
57+
move=> n; have [m ltnm] := ubnP n.
58+
by elim: m n ltnm => // m IHm [|[|n]] //= /ltnW /IHm ->.
59+
Qed.
60+
61+
Elpi derive.param2 half'.
62+
4763
(******************************************************************************)
4864
(* Section 3.2: Characterization of stable non-tail-recursive mergesort *)
4965
(* functions *)
@@ -73,8 +89,8 @@ Definition asort_ty :=
7389
seq T -> (* input *)
7490
R. (* output *)
7591

76-
Parametricity sort_ty.
77-
Parametricity asort_ty.
92+
Elpi derive.param2 sort_ty.
93+
Elpi derive.param2 asort_ty.
7894

7995
Structure function := Pack {
8096
(* the sort function *)
@@ -132,7 +148,7 @@ Definition sort T R (merge : R -> R -> R) (singleton : T -> R) (empty : R) :=
132148
foldr (fun x => merge (singleton x)) empty.
133149

134150
(* Its parametricity *)
135-
Parametricity sort.
151+
Elpi derive.param2 sort.
136152

137153
End Abstract.
138154

@@ -172,14 +188,16 @@ Equations sort_rec (xs : seq T) (fuel : nat) : R by struct fuel :=
172188
sort_rec [:: x] _ => singleton x;
173189
sort_rec xs fuel.+1 =>
174190
let k := size xs in
175-
merge (sort_rec (take k./2 xs) fuel) (sort_rec (drop k./2 xs) fuel).
191+
merge (sort_rec (take (half' k) xs) fuel)
192+
(sort_rec (drop (half' k) xs) fuel).
176193

177194
Definition sort (xs : seq T) : R := sort_rec xs (size xs).
178195

179196
End Abstract.
180197

181198
(* Its parametricity *)
182-
Parametricity sort.
199+
Elpi derive.param2 sort_rec.
200+
Elpi derive.param2 sort.
183201

184202
End Abstract.
185203

@@ -200,14 +218,19 @@ Definition sort (xs : seq T) : seq T := sort_rec xs (size xs).
200218

201219
(* Equation (1) holds by definition. *)
202220
Lemma asort_mergeE : Abstract.sort (merge leT) (fun x => [:: x]) [::] =1 sort.
203-
Proof. by []. Qed.
221+
Proof.
222+
rewrite /sort /Abstract.sort => xs; move: (size xs) => n.
223+
elim: n xs => [|n IHn] [|x [|y xs]] //.
224+
by simp sort_rec; rewrite /= !half'E !IHn.
225+
Qed.
204226

205227
(* The proof of Equation (2) in Lemma 3.3 *)
206228
Lemma asort_catE : Abstract.sort cat (fun x : T => [:: x]) [::] =1 id.
207229
Proof.
208230
rewrite /Abstract.sort => xs; move: {-1}(size xs) (leqnn (size xs)) => n.
209231
elim: n xs => [|n IHn] [|x [|y xs]] //= Hxs; simp sort_rec; cbn zeta.
210-
rewrite !IHn ?cat_take_drop //= (size_drop, size_take) /=; last case: ifP; lia.
232+
rewrite !half'E !IHn ?cat_take_drop //= (size_drop, size_take) /=;
233+
last case: ifP; lia.
211234
Qed.
212235

213236
End TopDown.
@@ -246,7 +269,9 @@ Proof. by apply_funelim (merge_pairs xs) => //= ? ? ? ->. Qed.
246269
End Abstract.
247270

248271
(* Its parametricity *)
249-
Parametricity sort.
272+
Elpi derive.param2 merge_pairs.
273+
Elpi derive.param2 merge_all.
274+
Elpi derive.param2 sort.
250275

251276
End Abstract.
252277

icfp25/Section4_1.v

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ From mathcomp Require Import zify.
1010
From stablesort Require Import param stablesort.
1111
From Equations Require Import Equations.
1212

13+
Elpi derive.param2 fst.
14+
Elpi derive.param2 size.
15+
Elpi derive.param2 Nat.sub.
16+
Elpi derive.param2 subn.
17+
1318
Set Implicit Arguments.
1419
Unset Strict Implicit.
1520
Unset Printing Implicit Defensive.
@@ -18,6 +23,22 @@ Lemma if_nilp (T S : Type) (s : seq T) (x y : S) :
1823
(if nilp s then x else y) = if s is [::] then x else y.
1924
Proof. by case: s. Qed.
2025

26+
(* We define a non-mutual-fixpoint alternative of ssrnat.half, so that *)
27+
(* derive.param2 accepts it. *)
28+
Fixpoint half' (n : nat) : nat :=
29+
match n with
30+
| n.+2 => (half' n).+1
31+
| _ => 0
32+
end.
33+
34+
Lemma half'E : half' =1 half.
35+
Proof.
36+
move=> n; have [m ltnm] := ubnP n.
37+
by elim: m n ltnm => // m IHm [|[|n]] //= /ltnW /IHm ->.
38+
Qed.
39+
40+
Elpi derive.param2 half'.
41+
2142
Section Revmerge.
2243

2344
Context (T : Type) (leT : rel T).
@@ -60,7 +81,7 @@ Equations sort_rec (xs : seq T) (b : bool) (n fuel : nat) :
6081
(* end absurd cases *)
6182
sort_rec (x :: xs) _ 1 _ => (singleton x, xs);
6283
sort_rec xs b n fuel.+1 =>
63-
let n1 := n./2 in
84+
let n1 := half' n in
6485
let (s1, xs') := sort_rec xs (~~ b) n1 fuel in
6586
let (s2, xs'') := sort_rec xs' (~~ b) (n - n1) fuel in
6687
((if b then merge' s1 s2 else merge s1 s2), xs'').
@@ -71,7 +92,8 @@ Definition sort (xs : seq T) : R :=
7192

7293
End Abstract.
7394

74-
Parametricity sort.
95+
Elpi derive.param2 sort_rec.
96+
Elpi derive.param2 sort.
7597

7698
End Abstract.
7799

@@ -114,7 +136,7 @@ rewrite {}/rhs; move: {2 4}(size xs) => fuel.
114136
apply_funelim (sort_rec xs true (size xs) fuel);
115137
try by move=> *; case: (b in condrev b).
116138
move=> x {}xs b n {}fuel IHl IHr.
117-
rewrite Abstract.sort_rec_equation_5 /= {}IHl /= {IHr}(IHr [::]) /=.
139+
rewrite Abstract.sort_rec_equation_5 /= !half'E {}IHl /= {IHr}(IHr [::]) /=.
118140
case: (sort_rec (x :: xs)) => s1 xs' /=; case: sort_rec => s2 xs'' /=.
119141
by rewrite !revmergeE /condrev; case: b; rewrite /= !revK.
120142
Qed.
@@ -134,7 +156,7 @@ apply_funelim
134156
- by [].
135157
- by move=> x {}xs; rewrite /= take0 drop0.
136158
move=> x {}xs b n {}fuel IHl IHr; rewrite ltnS => n_lt_fuel.
137-
rewrite [LHS]/= {}IHl 1?{}(IHr [::]) 1?if_same; try lia.
159+
rewrite [LHS]/= {}IHl 1?{}(IHr [::]) 1?if_same half'E; try lia.
138160
rewrite -takeD drop_drop; congr (take _ _, drop _ _); lia.
139161
Qed.
140162

meta.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,15 @@ tested_coq_opam_versions:
7878
repo: 'mathcomp/mathcomp-dev'
7979

8080
dependencies:
81+
- opam:
82+
name: coq-elpi
83+
description: |-
84+
[Coq-Elpi](https://github.com/LPCIC/coq-elpi)
8185
- opam:
8286
name: coq-mathcomp-ssreflect
8387
version: '{>= "2.3.0"}'
8488
description: |-
8589
[MathComp](https://math-comp.github.io) ssreflect 2.3.0 or later
86-
- opam:
87-
name: coq-paramcoq
88-
version: '{>= "1.1.3"}'
89-
description: |-
90-
[Paramcoq](https://github.com/coq-community/paramcoq) 1.1.3 or later
9190
- opam:
9291
name: coq-mathcomp-zify
9392
version: '{with-test}'

rocq-stablesort.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ run-test: [make "-j%{jobs}%" "build-icfp25"]
4141
install: [make "install"]
4242
depends: [
4343
"coq" {>= "8.19"}
44+
"coq-elpi"
4445
"coq-mathcomp-ssreflect" {>= "2.3.0"}
45-
"coq-paramcoq" {>= "1.1.3"}
4646
"coq-mathcomp-zify" {with-test}
4747
"coq-equations" {with-test}
4848
]

theories/param.v

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1+
From elpi Require Export derive.param2.
12
From mathcomp Require Import ssreflect ssrfun ssrbool eqtype ssrnat seq path.
2-
From Param Require Export Param.
33

44
Set Implicit Arguments.
55
Unset Strict Implicit.
66
Unset Printing Implicit Defensive.
77

8-
Ltac destruct_reflexivity :=
9-
intros ; repeat match goal with
10-
| [ x : _ |- _ = _ ] => destruct x; reflexivity; fail
11-
end.
12-
13-
Global Parametricity Tactic := ((destruct_reflexivity; fail) || auto).
14-
15-
Parametricity False.
16-
Parametricity eq.
17-
Parametricity or.
18-
Parametricity Acc.
19-
Parametricity unit.
20-
Parametricity bool.
21-
Parametricity option.
22-
Parametricity prod.
23-
Parametricity nat.
24-
Parametricity list.
25-
Parametricity pred.
26-
Parametricity rel.
27-
Parametricity BinNums.positive.
28-
Parametricity BinNums.N.
29-
Parametricity merge.
30-
Parametricity rev.
8+
Elpi derive.param2 unit.
9+
Elpi derive.param2 bool.
10+
Elpi derive.param2 nat.
11+
Elpi derive.param2 option.
12+
Elpi derive.param2 prod.
13+
Elpi derive.param2 list.
14+
Elpi derive.param2 pred.
15+
Elpi derive.param2 rel.
16+
Elpi derive.param2 negb.
17+
Elpi derive.param2 addb.
18+
Elpi derive.param2 eqb.
19+
Elpi derive.param2 merge.
20+
Elpi derive.param2 catrev.
21+
Elpi derive.param2 rev.
22+
Elpi derive.param2 foldr.
3123

3224
Lemma bool_R_refl b1 b2 : b1 = b2 -> bool_R b1 b2.
3325
Proof. by case: b1 => <-; constructor. Qed.

0 commit comments

Comments
 (0)