coq-club AT inria.fr
Subject: The Coq mailing list
List archive
- From: Xuanrui Qi <xqi01 AT cs.tufts.edu>
- To: Klaus Ostermann <klaus.ostermann AT uni-tuebingen.de>, coq-club AT inria.fr
- Subject: Re: [Coq-Club] Certified insertion
- Date: Sat, 8 Dec 2018 18:36:09 +0000 (UTC)
- Authentication-results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=xqi01 AT cs.tufts.edu; spf=None smtp.mailfrom=xqi01 AT cs.tufts.edu; spf=None smtp.helo=postmaster AT vm-delivery1.eecs.tufts.edu
- Ironport-phdr: 9a23:DBV72h+Lzy322/9uRHKM819IXTAuvvDOBiVQ1KB32+gcTK2v8tzYMVDF4r011RmVBdWds6oMotGVmpioYXYH75eFvSJKW713fDhBt/8rmRc9CtWOE0zxIa2iRSU7GMNfSA0tpCnjYgBaF8nkelLdvGC54yIMFRXjLwp1Ifn+FpLPg8it2O2+557ebx9UiDahfLh/MAi4oQLNu8cMnIBsMLwxyhzHontJf+RZ22ZlLk+Nkhj/+8m94odt/zxftPw9+cFAV776f7kjQrxDEDsmKWE169b1uhTFUACC+2ETUmQSkhpPHgjF8BT3VYr/vyfmquZw3jSRMNboRr4oRzut86ZrSAfpiCgZMT457HrXgdF0gK5CvR6tuwBzz4vSbYqINvRxY7ndcMsUS2RBQMhfVC5OAo2hYYQAE+UPOv1VoJPhq1YUthSzHxWgCP3pxzRVhnH2x6o60+E5HA7HwQwvBdQOsG7JrNX0KawcSuG1x7TPwDTMdvNW2Cvx5JXTfx89uvGAR65wcdffyUkzDAPJlEufppH4Pz6M0OkGrmaV7+1lVe21im4nrRl8ojmpxsc2i4nGnJgZyl7e9Slhz4Y1JMG4SE5mYdG/CpdfqyaaN45uTsM/W2FnpiA6yrsctZGlYScK1ZIqzAPcZfyfa4WE/xzuWPqLLTp5i39pYqyzihio/UWuyODwTsm53EhQoiZYjNXBtmoB2wHd58SZUPdx40Ss1DSJ2gvO8O9LO1o0mrDeK5M5wr4/iJ4TsUPbEy/onUX6lbKZeVsg+uip9eTofK/mp5+BO4NulA7xLLghmtelDeQkLwgOUXSb+f+m2LL94EL5Xa1GjvwwkqbHrJDXPdkXq6ClDwNP04su5AyzAymo3dgCk3QKLkpJeBedgIjoP1HOLur4DfC6g1m0nzZr3vHGMqH8DpjWMnfDi63tfapg605S1Aoz1spT6IxJBbEcOP7zQFP+tMTEDh8lNAy52/roCNJk1o8HRW2PBrKZP7jJvF+T5uMvJvGMa5UPtDb8Lfgl/f/ugmUjlV8TZ6n6lacQPVO8F+l9a2KCfXf2h94HFy9evQ43Ufesj0aeXCRWbnC0d78653QnFY+sDIHMS4brjLHXmG+wGYQTbWRbAHiNF23pfsOKQaQiciWXd/98mzoeSLnpco5pgQG3tVej47F8aPbJ9DED85/vyY4mtKXoiRgu+GksXIym2GaXQjQxxztQHm5k7OVEuUV4j2y7/+19iv1cG8ZU4qoVAAwhc4LBwfBhTd3+R1CZJ4vbeBOdWtyjRAoJYJcp2dZXPxR2AJO+kx7fxGynD6JHz+XWVqxxybrV2j3KH+g4y3vC0/Nw3UcjB8dCMWy4i7Rurk7eCoXS1VmEmbqxM6kQwXyV+Q==
Hi Klaus,
Program Fixpoint or Function will probably do the trick. Usually, they will work much better than refine in the presence of subset types. There's a manual here: https://coq.inria.fr/refman/addendum/program.html.
You can also look at this program I've written, which uses subset types extensively: https://github.com/xuanruiqi/verified-data-structures/blob/master/twothree_dependent.v. This might give you some idea.
-Ray
On Sat, Dec 8, 2018 at 10:54 AM -0500, "Jason -Zhong Sheng- Hu" <fdhzs2010 AT hotmail.com> wrote:
Hi Klaus,
In Appel's Software Foundations Vol. 3, there are two chapters proving insertion sort correct. Not sure if you've taken a look? https://softwarefoundations.cis.upenn.edu/vfa-current/toc.html
Selection Sort, With Specification and Proof of Correctness ()Top; The Selection-Sort Program; Proof of Correctness of Selection sort; Recursive Functions That are Not Structurally Recursivesoftwarefoundations.cis.upenn.edu
Regarding your original problem, this can be solved without invoking K, but it's kind of annoying to do all these work manually. To provide an initial refinement:
Require Import Coq.Arith.Compare_dec.
Fixpoint insert(n: nat)(l: { q: list nat | Sorted q})
: { q: list nat | sortedOf (cons n (proj1_sig l)) q}.
Proof.
refine (let (l', sorted_l') as l0
return { q: list nat | sortedOf (cons n (proj1_sig l0)) q} := l
in match l' as l0
return Sorted l0 ->
{ q: list nat | sortedOf (cons n l0) q} with
| nil => fun _ => exist _ [n] _
| cons x xs =>
fun sorted_l' =>
match le_lt_dec n x with
| left n_le_x => exist _ (n :: x :: xs) _
| right x_lt_n => _
endend sorted_l').
To point out what's missing in your original solution:In the `right` case in `cons`, you need to do a recursive call, and you will have to use extended let to unpack the result again in order to expose the list and the evidence.
- Since dependent sum types in Coq has no definitional eta expansion, the extended let binding up front is needed to unpack the list and the evidence. The later is required in the recursive case. You originally attempted to use extended match directly, so it didn't work.
- Extended match then can be used to split cases. But Sorted evidence needs to be carried in.
- In the cons case, then you can use decision procedure (from Arith.Compare_dec) to decide ordering.
So it's not quite convenient. It's much better off to do the same as what Appel did in Vol 3. Additionally, you might fall into trouble proving additional theorems on this function, because `destruct` will have some trouble handling case analysis with the presence of dependent types. Another solution is to use Equations, which is much more friendly and more powerful in dependently typed settings. Equations also allows you to disable K, so if you are worried about your foundation, you are secured as well.
From: coq-club-request AT inria.fr <coq-club-request AT inria.fr> on behalf of Klaus Ostermann <klaus.ostermann AT uni-tuebingen.de>
Sent: December 8, 2018 9:22 AM
To: coq-club AT inria.fr
Subject: [Coq-Club] Certified insertionI'm making a few experiments with using subset types for "certified
programming".
I'm stuck with a certified variants of the "insert" function as used in
insertion sort, that is,
it's type should say that the input list and output list are sorted and
that the output list
contains both the input list and the element to be inserted.
Could somebody tell me how to do this idiomatically in Coq, preferably
without using
"dependent destruction" and other tactics that require additional
axioms? I'm stuck on
how to make the case distinction on whether the element to be inserted
is less than or eqal
to the first element. Presumably this requires a fancy "return" annotation?
Inductive Sorted : list nat -> Prop :=
| Sorted_nil : Sorted []
| Sorted_cons1 a : Sorted [a]
| Sorted_consn a b l :
Sorted (b :: l) -> a <= b -> Sorted (a :: b :: l).
Definition sortedOf(l: list nat)(q: list nat) := Sorted q /\ forall m,
In m l <-> In m q.
Fixpoint insert(n: nat)(l: { q: list nat | Sorted q}) : { q: list nat |
sortedOf (cons n (proj1_sig l)) q}.
refine( match l return { q: list nat | sortedOf (cons n (proj1_sig l))
q} with
| exist [] st => _
| exist (cons x xs) st => _
end).
- refine (exist _ [n] _). split. constructor. intros. split; intros.
simpl in H ; destruct H ; subst; constructor; auto. inversion H.
constructor. inversion H ; subst; auto. inversion H0.
- (* stuck here; how to make the case distinction on whether n <= x ? )
Klaus
- [Coq-Club] Certified insertion, Klaus Ostermann, 12/08/2018
- Re: [Coq-Club] Certified insertion, Jason -Zhong Sheng- Hu, 12/08/2018
- Re: [Coq-Club] Certified insertion, Xuanrui Qi, 12/08/2018
- Re: [Coq-Club] Certified insertion, Adam Chlipala, 12/08/2018
- Re: [Coq-Club] Certified insertion, Klaus Ostermann, 12/08/2018
- Re: [Coq-Club] Certified insertion, Xuanrui Qi, 12/08/2018
- Re: [Coq-Club] Certified insertion, Jonathan Leivent, 12/09/2018
- Re: [Coq-Club] Certified insertion, Klaus Ostermann, 12/08/2018
- Re: [Coq-Club] Certified insertion, David Holland, 12/19/2018
- Re: [Coq-Club] Certified insertion, Jason -Zhong Sheng- Hu, 12/08/2018
Archive powered by MHonArc 2.6.18.