coq-club AT inria.fr
Subject: The Coq mailing list
List archive
- From: mukesh tiwari <mukeshtiwari.iiitm AT gmail.com>
- To: coq-club AT inria.fr
- Subject: [Coq-Club] Directing the Coq extraction mechanism to extract a simple code
- Date: Tue, 14 Sep 2021 15:20:33 +1000
- Authentication-results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=mukeshtiwari.iiitm AT gmail.com; spf=Pass smtp.mailfrom=mukeshtiwari.iiitm AT gmail.com; spf=None smtp.helo=postmaster AT mail-ej1-f41.google.com
- Ironport-hdrordr: A9a23:HAboE64by5o3rKd4gwPXwZ+CI+orL9Y04lQ7vn1ZYQdec8yGm83rtOlz726htN9jYgBqpTnmAtjOfZq8z+883WB/B8bXYODZghrxEGgP1/qR/9SkIVy2ygayvZ0QJpSXJrXLfAZHZOzBkVaF+05K+qjUzEiQ7d2unEuEp2lRGvBdBn5Ce1am+y5NNX977PgCZe+hD5F81mHQI0j/B/7TbhJuMoStyb279+OFEHt2Y29AmXD+9ULSmcSKbmnlr2luIg+ng41SglQt9TaJoZlLnMvLlyM0vFWjq6i+9uGRuuerSPb8/fQ9G3HGihWhY4poMofy+UEYnAmQgmxa1uXkklMFN8R38W7LeCWLqQDs0wLk3CxG0Q60uAmlqEqmh9f4SjI8A88Ev4REaRHUr2oY1esMntNj40up86FaChvckD+43sXHW1VRj0Kxi3I/i+J7tQ0SbaIuLIRcqoQD8FgQKYoPEGbB8YgiKuNpF8203ocaTXqqK0vUuWFzzMfpZ2k8ElO9W0AHgNGczjQ+pgEy82IogPcSmX8c+Il4cZVe+OjeFaxtmNh1P41mQJ5VNaM7Tcy+F2DXBSjUOGa5KUj8GMg8SjG9gaLf0fEP6OajZZASiKEqkJD6WlVCuQcJCgKeIvGm7dlx/hfEXWmnGQ71wsVl+pBlttTHNfubGRFqQzoV4pKdSiokY7y9KomOEaMTBPmmJXDlGIZX02TFKsVvFUU=
- Ironport-phdr: A9a23:zNtyUhWnHkmemmN8YOaoPtRVsdvV8KxOVTF92v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G9KEF6MEW27P7mDDvwLcWeZJtJruj0CT7moDYE7hj+0swqvzqdkI/HUsjEZqpv51ZAh4vDQmAoy6T1rBt6clWCMTn1xtmwNTj4ymqt4pBoumR+4zaFkjqkARpRo7PRTX1JiXXYz5+N/AtH2HAnGe4XQIL5HatCvADV0Q9BohtFSOgByHNKtih2F1C2vUed9f1mjC5k986aa1H/0dZ4V9g==
How to direct the Coq extraction mechanism to avoid the unnecessary
wrapping and unwrapping of existentials and extract the Coq code as it
is?
In most of my work/research, I write Coq code, prove them correct, and
extract OCaml code. So far, I have used dependent types freely and the
extracted OCaml code is littered with "Obj.magic", which is fine
because those parts are not touched at the run time, but say my goal
is to avoid "Obj.magic", for mostly readability purposes. One way to
achieve this is writing a simple function, using Fixpoint or
Definition, but not every terminating function can be written this
way, especially if the termination measure is some combination of
input arguments. So recently, I have found "Program Fixpoint" which
basically solves my problem, except the extraction part. For example,
when I am extracting the "binary_gcd" function (see the code below), I
get two functions "binary_gcd" and "binary_gcd_func" [1]. However, I
feel that wrapping and unwrapping in existentials (ExistsT data type)
are unnecessary in these two functions, so my question is how can I
direct the extraction mechanism to give a code similar to this [2]
(extracted from a similar Coq but for termination it uses nat as
fuel)?
Best,
Mukesh
[1] https://gist.github.com/mukeshtiwari/5e03d736f662188d9b88660b800ef498
[2]
https://gist.github.com/mukeshtiwari/5e03d736f662188d9b88660b800ef498#file-gcd-ml-L99
Require Import Coq.NArith.BinNat.
Require Import Coq.Program.Wf.
Require Import Coq.ZArith.Zwf.
Local Open Scope positive_scope.
Lemma poslt_wf : well_founded Pos.lt.
Proof.
unfold well_founded.
assert (forall x a, x = Zpos a -> Acc Pos.lt a).
intros x. induction (Zwf_well_founded 1 x);
intros a Hxa. subst x.
constructor; intros y Hy.
apply H0 with y; trivial.
split; auto with zarith.
intros. apply H with a.
exact eq_refl.
Defined.
Definition poseven (a : positive) : bool :=
match a with
| xO _ => true
| _ => false
end.
Lemma poseven_div : forall p, true = poseven p -> Pos.div2 p < p.
Proof.
induction p; simpl; intro H; try (inversion H).
nia.
Qed.
Program Fixpoint binary_gcd (x y : positive) (u v g : positive) (a b c d : Z)
{measure (Pos.add u v) (Pos.lt)} : Z * Z * positive :=
match poseven u, poseven v with
| true, true => binary_gcd x y (Pos.div2 u) (Pos.div2 v) (Pos.mul 2 g) a b
c d
| true, false =>
match Z.even a, Z.even b with
| true, true => binary_gcd x y (Pos.div2 u) v g (Z.div a 2)
(Z.div b 2) c d
| _, _ => binary_gcd x y (Pos.div2 u) v g (Z.div (a + y) 2)
(Z.div (b - x) 2) c d
end
| false, true =>
match Z.even c, Z.even d with
| true, true => binary_gcd x y u (Pos.div2 v) g a b (Z.div c 2) (Z.div d
2)
| _, _ => binary_gcd x y u (Pos.div2 v) g a b (Z.div (c + y) 2)
(Z.div (d - x) 2)
end
| false, false =>
match u ?= v with
| Lt => binary_gcd x y u (Pos.sub v u) g a b (Z.sub c a) (Z.sub d b)
| Eq => (a, b, Pos.mul g v)
| Gt => binary_gcd x y (Pos.sub u v) v g (Z.sub a c) (Z.sub b d) c d
end
end.
Next Obligation.
apply Pos.add_lt_mono.
apply poseven_div; exact Heq_anonymous.
apply poseven_div; exact Heq_anonymous0.
Defined.
Next Obligation.
apply Pos.add_lt_mono_r.
apply poseven_div; exact Heq_anonymous1.
Defined.
Next Obligation.
apply Pos.add_lt_mono_r.
apply poseven_div; exact Heq_anonymous1.
Defined.
Next Obligation.
apply Pos.add_lt_mono_l.
apply poseven_div; exact Heq_anonymous2.
Defined.
Next Obligation.
apply Pos.add_lt_mono_l.
apply poseven_div; exact Heq_anonymous2.
Defined.
Next Obligation.
symmetry in Heq_anonymous.
pose proof (proj1 (Pos.compare_lt_iff u v) Heq_anonymous).
rewrite Pos.add_sub_assoc, Pos.add_comm, Pos.add_sub.
apply Pos.lt_add_r. exact H.
Defined.
Next Obligation.
symmetry in Heq_anonymous.
pose proof (proj1 (Pos.compare_gt_iff u v) Heq_anonymous).
nia.
Defined.
Next Obligation.
apply measure_wf.
apply poslt_wf.
Defined.
Extraction Language OCaml.
Recursive Extraction binary_gcd.
- [Coq-Club] Directing the Coq extraction mechanism to extract a simple code, mukesh tiwari, 09/14/2021
- Re: [Coq-Club] Directing the Coq extraction mechanism to extract a simple code, Dominique Larchey-Wendling, 09/14/2021
- Re: [Coq-Club] Directing the Coq extraction mechanism to extract a simple code, mukesh tiwari, 09/15/2021
- Re: [Coq-Club] Directing the Coq extraction mechanism to extract a simple code, Dominique Larchey-Wendling, 09/15/2021
- Re: [Coq-Club] Directing the Coq extraction mechanism to extract a simple code, Yannick Forster, 09/15/2021
- Re: [Coq-Club] Directing the Coq extraction mechanism to extract a simple code, Dominique Larchey-Wendling, 09/15/2021
- Re: [Coq-Club] Directing the Coq extraction mechanism to extract a simple code, Yannick Forster, 09/15/2021
- Re: [Coq-Club] Directing the Coq extraction mechanism to extract a simple code, Dominique Larchey-Wendling, 09/15/2021
- Re: [Coq-Club] Directing the Coq extraction mechanism to extract a simple code, mukesh tiwari, 09/15/2021
- Re: [Coq-Club] Directing the Coq extraction mechanism to extract a simple code, Dominique Larchey-Wendling, 09/14/2021
Archive powered by MHonArc 2.6.19+.