Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] How do I manage data structures with invariants in Coq?

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] How do I manage data structures with invariants in Coq?


Chronological Thread 
  • From: mukesh tiwari <mukeshtiwari.iiitm AT gmail.com>
  • To: coq-club AT inria.fr
  • Subject: Re: [Coq-Club] How do I manage data structures with invariants in Coq?
  • Date: Wed, 11 Mar 2020 10:06:18 +1100
  • 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-il1-f180.google.com
  • Ironport-phdr: 9a23:dG1P6xFU1o8+CuxGLHOB4J1GYnF86YWxBRYc798ds5kLTJ78oc+wAkXT6L1XgUPTWs2DsrQY0raQ6vq5EjRbqb+681k8M7V0HycfjssXmwFySOWkMmbcaMDQUiohAc5ZX0Vk9XzoeWJcGcL5ekGA6ibqtW1aFRrwLxd6KfroEYDOkcu3y/qy+5rOaAlUmTaxe7x/IAi5oAnLq8Uan5ZuJqktxhbHv3BFZ/lYyWR0KF2cmBrx+t2+94N5/SRKvPIh+c9AUaHkcKk9ULdVEjcoPX0r6cPyrRXMQheB6XUaUmUNjxpHGBPF4w3gXpfwqST1qOxw0zSHMMLsTLA0XTOi77p3SBLtlSwKOSI1/H3Rh8dtiq9QvRCvqAFlw4PMb46VOvhxcKPTc90ZWGRPQNpeWjddAo+gdYYDE/YNMfpaooT7ulAArQG+BQ6pBO73zz9Im3z20rMh0+QhDArGwA0gHtwUv3TOrdX+KaAfUeWozKnL0zrDdPNW2Sv86InGaB8hu/CMUahxccrQyEkvCwbFg06fqYzgJTyV1+ANv3KH4OpnUOKikmgqoBxyrDi33soglJXFi4YPxl3H9Sh12ps5KNy5RUJhfNKpE4dcuieHPIVsWMwiWXtnuCMix70Gp5G7eC8KxYwixxHFavyHd5GE4hPnVOqMODt4im9pdbGjixqo/kigzer8Vsaw0FlUtCZKjt7MtnUV2xzS7MiIVOd981+/1TqT0w3f8OJJLEAumabGKpMsw6Q8mocQvEjfBiP2nV/5jK6SdkUq4Oio7OHnb63jpp+BNI97lBr+MqQymsy+D+U3KBIOX2mB9eS91b3j/FH5QLBRg/05l6nWqpHaJcABqqGlBA9V154v6wyjADe+zNQYgX4HIUpZdxKAlojlIk3BIPTlDfikmFmsizdqx/XePrL7GJnNL37DkK3gfbln8UJcxhAznphj4MdfDahEK/buUGfwssbZB1k3KV+a2eHiXdBg1Y4FWSqTA7CQKqKa5VqV5e80I/WNe4YPuXD8Kvk54tbhiHY4nRkWeqz/jshfU2yxAvkzexbRWnHrmNpUST5X7Dp7d/TjjRi5aRAWZ3u2WPhitDQyCYbjDIOaA4700OTH0yC8EZlbIGtBDwLUSCa6R8C/Q/4JLRmqDIpkmz0AW6KmTtZ4hx6rvQ7+jbFgK7iNo3FKhdfYzNFwotbru1Qq7zUtVpaS1miMSyd/mWZaHzI=

Hi Agnishom,

You can write your Definition in proof mode. I have wrapped your
definition (Gallina term?) in refine tactic, and you can discharge
your proof in tactic mode. Hope it helps.


Definition snoc (q : queue) (x: A) : queue.
refine (
let ans := exec (front q) (x :: rear q) (silly q) in
let s := snd ans in
let f := fst (fst ans) in
let r := snd (fst ans) in
let inv' := (exec_preserves_invariant q f r s x _)
in Build_queue f r s inv').
(* Write your tactic here to discharge the rest of the proof *)

A : Type
q : queue
x : A
ans := exec (front q) (x :: rear q) (silly q) : list A * list A * list A
s := snd ans : list A
f := fst (fst ans) : list A
r := snd (fst ans) : list A
============================
(f, r, s) = exec (front q) (x :: rear q) (silly q)

Best,
Mukesh

On Wed, Mar 11, 2020 at 9:26 AM Agnishom Chattopadhyay
<agnishom AT cmi.ac.in>
wrote:
>
> I am trying to implement a functional queue in Coq. My idea is to add the
> proof of the invariant as a part of the type. Like this:
>
> Require Import Coq.Lists.List.
> Import ListNotations.
> Require Import Lia.
>
> Section Queue.
>
> Variable A : Type.
>
> Record queue :=
> {
> front : list A;
> rear : list A;
> silly : list A;
> invariant : length front - length rear = length silly
> }.
>
> Definition empty : queue := Build_queue [] [] [] (eq_refl).
>
> Definition isEmpty (q : queue) : bool :=
> match (front q) with
> | [] => true
> | _ => false
> end.
>
> Fixpoint rotate (f : list A) (r : list A) (s : list A) : list A :=
> match f, r, s with
> | [], (y :: _), a => y :: a
> | (x :: xs), (y :: ys), a => x :: (rotate xs ys (y::a))
> | _, _, _ => []
> end.
>
> Fixpoint exec (f : list A) (r : list A) (s : list A) : list A * list A *
> list AUTO :=
> match f, r, s with
> | _, _, (x :: xs) => (f, r, xs)
> | _, _, [] => let f' := rotate f r [] in (f', [], f')
> end.
>
> The following Lemma is to support the next definition
>
> Lemma exec_preserves_invariant : forall (q : queue),
> forall f r s x ,
> (f, r, s) = exec (front q) (x :: rear q) (silly q)
> -> length f - length r = length s.
> Proof.
> intros.
> pose proof (invariant q).
> destruct (front q); destruct (silly q).
> - simpl in H. inversion H. simpl. lia.
> - inversion H0.
> - simpl in H. inversion H. simpl. lia.
> - simpl in H. inversion H. simpl.
> replace (length (a :: l)) with (S (length l)) in H0 by reflexivity.
> replace (length (a0 :: l0)) with (S (length l0)) in H0 by reflexivity.
> lia.
> Qed.
>
> However, I cannot figure out what would be the correct way to replace the
> underscore below.
>
> Definition snoc (q : queue) (x: A) : queue :=
> let ans := exec (front q) (x :: rear q) (silly q) in
> let s := snd ans in
> let f := fst (fst ans) in
> let r := snd (fst ans) in
> let inv' := (exec_preserves_invariant q f r s x _)
> in
> Build_queue f r s inv '.
>
> What is the correct way to define the invariant preserving functions such
> as snoc?
>
> In some languages like Lean, I have noticed that any term can be replaced
> by a tactic that synthesizes a similar term. So, in such a context, I could
> have replaced the underscore by a proof using tactics.
>
> --Agnishom



Archive powered by MHonArc 2.6.18.

Top of Page