1212(* 3.4.3. *)
1313(***************************************************************************** *)
1414
15+ From elpi Require Export derive.param2.
1516From mathcomp Require Import ssreflect ssrfun ssrbool eqtype ssrnat seq path.
1617From mathcomp Require Import zify.
17- From Param Require Export Param.
1818From Equations Require Import Equations.
1919
2020Set Implicit Arguments .
2121Unset Strict Implicit .
2222Unset 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
3636Local Lemma bool_R_refl b1 b2 : b1 = b2 -> bool_R b1 b2.
3737Proof . 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.
4545Proof . 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
7995Structure 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
137153End 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
177194Definition sort (xs : seq T) : R := sort_rec xs (size xs).
178195
179196End Abstract.
180197
181198(* Its parametricity *)
182- Parametricity sort.
199+ Elpi derive.param2 sort_rec.
200+ Elpi derive.param2 sort.
183201
184202End 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. *)
202220Lemma 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 *)
206228Lemma asort_catE : Abstract.sort cat (fun x : T => [:: x]) [::] =1 id.
207229Proof .
208230rewrite /Abstract.sort => xs; move: {-1}(size xs) (leqnn (size xs)) => n.
209231elim: 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.
211234Qed .
212235
213236End TopDown.
@@ -246,7 +269,9 @@ Proof. by apply_funelim (merge_pairs xs) => //= ? ? ? ->. Qed.
246269End 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
251276End Abstract.
252277
0 commit comments