Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] Program Fixpoint and a function involving two lists

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] Program Fixpoint and a function involving two lists


Chronological Thread 
  • From: "Lasser, Samuel M." <Samuel.Lasser AT tufts.edu>
  • To: "coq-club AT inria.fr" <coq-club AT inria.fr>
  • Subject: Re: [Coq-Club] Program Fixpoint and a function involving two lists
  • Date: Wed, 12 Aug 2020 18:05:57 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=tufts.edu; dmarc=pass action=none header.from=tufts.edu; dkim=pass header.d=tufts.edu; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Rx14UvczhaOUdEi56kZGUQkdZ/T2w/5Ji+0OUNCNr0c=; b=LOlc04iAqHrODqz40GUDWza3QY0infido0HLupbftYC/EhqG8Ld1p9nifRZ2I31wQ8HMavkTaslqT88HrdiBuea/lo8AdOjgFyaKufK7mADfrgiRAWtSVUv9toDc3rcdGUWXiZ2lksCrR6PUAmWQe5lxSWj/M4P2Q4cPw15WoqXmFDmzyXSzrT7L64w/MWq1VlOoDMKPykOCrN18eFjt+VExKz0kbWDxS+e6n2gdZAMZDiXDnjBHpe9NcwKx90MHwSKU/exzwg6UjhPI32cSLI1aLmVDFAptgqVjgJeU/isutpkpd7x+3S4V+u4bEkd+4Ry1Suuoz15Ceo0d6RhDpw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KprEPJ8M3jWlp2heN4Z0EwPTr0f3PsH1x0Ym7i7TFxi6MQoBtYbyu+MI/fvsm4yHrpqQ8z3gUEsycxdZ9jdefawk7OECFF+MBmV94/WE72S43G5KXavAq5z+P79f1NcpzQW+QUa6KDfFPJTG8JOR1KbS98S4YPLO9tZvLVGpfH2CptTyMLRrpyMOFWwAvdt6WMqUuS1ZCujjkbAX7ScJopUNO7gvE+k28cVUAXQ4kdWgayiWkjZe43M/uqIxEpZNG1B1TKGh3K2rVE3WkpKRcdkFpPwmzoaxzmmxmm0GScQPcqzwsSTw6r/RZ6OiTtvVVoFKlGWkQqu5zNwC0pehCQ==
  • Authentication-results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=Samuel.Lasser AT tufts.edu; spf=Pass smtp.mailfrom=Samuel.Lasser AT tufts.edu; spf=Pass smtp.helo=postmaster AT NAM04-CO1-obe.outbound.protection.outlook.com
  • Ironport-phdr: 9a23:eFezShVv/XyJ5pPn4+k5NegTtULV8LGtZVwlr6E/grcLSJyIuqrYbRKCt8tkgFKBZ4jH8fUM07OQ7/m+Hz1aqs/Y4DgrS99lb1c9k8IYnggtUoauKHbQC7rUVRE8B9lIT1R//nu2YgB/Ecf6YEDO8DXptWZBUhrwOhBoKevrB4Xck9q41/yo+53Ufg5EmCexbal9IRmrrAjdrM0bjZVtJqos1xfEoWZDdvhLy29vOV+dhQv36N2q/J5k/SRQuvYh+NBFXK7nYak2TqFWASo/PWwt68LlqRfMTQ2U5nsBSWoWiQZHAxLE7B7hQJj8tDbxu/dn1ymbOc32Sq00WSin4qx2RhLklDsLOjgk+2zRl8d+jr9UoAi5qhJ/3YDafZ2VOvR9cKPTf9waRHZOUclKWiNbHo+wc5ECA/YdMepFqYT2ulsArQG5BQmpHO7jxDlGiWXt3a0hzeshCxnK1xE9Ed0UtXTUscv6NKMPWu6y1qXEwyjIYvRL1zjn7YjIbw4uofWWUbx2a8XR0lIvGB3fjliLtYzlIj2V1vgWs2SB8eVvSP+vhnchpgpsrTeh2t0ihZPVhoIJ1F/E7yN5zZ41KNGmTEN3f8KpHpRQui+VOIV7QM0sTW9otis1zrALpJC2cDUIxZkk2RPSb/OJf5aG7B7+WuudPCp1iW57dL6hgRu57EuuyvXkW8S7zFpGtDdJnsXOu3wX1hHe7tKLRuZh8kqvwTqDyQ/e5vtFLEwpj6bWJIItzqQym5YNqUjPAC77lFn4gaKTa0ko5vOn5uXib7n4upORNIp5hwL9P6krn8GzH+Q1Pw4TVGaB4+u8zqfs/UjhTbVKkPI2lq7ZvYjCK8kHoaC1HwBY3p895himEjuqydMYkmIZI15ffxKHkpTpNErJIPDlC/ewnk6gkC9xx/DBIr3uHInCLmTCkLfme7Zx8UlcyBcvzdBb4JJUDbIBLOjvVU/2sdzUFh45MwqqzOb7ENhw2Z8SVXiSDqOFMq7eq16F6+Y1L+WReIMYti7xK/0/6P7viX85l0Udfa6s3ZYPdX63BPFmI1iHbnromNsPF30FvhElTOP0kl2PSiJTZ2u8X6Ig/TE3EpypApreRoy1mryOwD+7HoFKZmBBEl2DDXDod5ydV/gQbCKSP9RunycfVbmhTo8hzQuhuBX7y7phNOrU+zcXuYjt1NhvtKXvkkR4/jttSs+ZzmulTmdun2pOSSV8lPR0plU4wVOe24B5heZZHJpd/aUafB09MMuW6+VhEN3oHkr6d9KURVLuZ5PsSWUKR9cszttIR09mANi4pgze0TaxRbIZiurYV9QP7qvA0i2pdI5GwHHc2fx51gh0co50LWSjw5VH2U3WDo/NnV+ekv/zJ7gB0jLWsmqP0DjX5R0KYEtLSazAGEsnSA7Ot92gvxHeUrGyEvIqPhYTkZffeJsPUcXgiBB9fNmmONnaZD7uyUGZIE7Ug5irNc/tcWhb2zjBAk8ZlQxV5WyBKQU1GiampSTZESBqElXsJUjr9LsnpQ==

Hi Suneel,

Here's a pattern that I sometimes use to define these kinds of functions without Program Fixpoint. You can add, as an extra parameter, a proof term showing that (length l1 + length l2) is accessible in the lt relation.

===
Require Import List Omega.

Ltac inv H := inversion H; subst; clear H.

Fixpoint either' (l1 l2 : list nat)
                            (Ha : Acc lt (length l1 + length l2))
                            {struct Ha} : list nat.
  refine (match (l1, l2) as pr return (l1, l2) = pr -> _ with
               | (nil, _) => fun _ => nil
               | (_, nil) => fun _ => nil
               |(a1 :: l1', a2 :: l2') =>
                   fun Heq =>
                     match Nat.leb a1 a2 with
                     | true  => a1 :: either' l1' ((a2 - a1) :: l2') _
                     | false => a2 :: either' ((a1 - a2) :: l1') l2' _
                     end
               end eq_refl).
  - inv Heq.
    eapply Acc_inv; eauto.
  - inv Heq.
    eapply Acc_inv; eauto; simpl.
    omega.
Defined.
===

Then you can define a top-level function that calls either' with an appropriate initial Acc term:

===
Definition either (l1 l2 : list nat) : list nat := 
  either' l1 l2 (lt_wf (length l1 + length l2)). 
===

Best of luck,
Sam


From: coq-club-request AT inria.fr <coq-club-request AT inria.fr> on behalf of Suneel Sarswat <suneel.sarswat AT gmail.com>
Sent: Tuesday, August 11, 2020 3:26 AM
To: coq-club AT inria.fr <coq-club AT inria.fr>
Subject: [Coq-Club] Program Fixpoint and a function involving two lists
 
I am trying to write a function involving two lists. In each iteration, one of the lists decreases strictly. The Fixpoint doesn't work so tried Program Fixpoint with the measure on the sum of the length of the two lists. Here is a minimum working example

Require Import Wf.
Program Fixpoint either (l1:list nat) (l2: list nat) {measure (|l1| + |l2|)}:=
match (l1,l2) with
|(nil, _) => nil
|(_,nil) => nil
|(a1::l1',a2::l2') => match (Nat.leb a1 a2) with
  |true  => a1::either l1' ((a2 -a1)::l2')
  |false => a2::either ((a1-a2)::l1') l2'
  end
  end.

Next Obligation.
simpl. omega. Qed.

Lemma trivial1 (l1: list nat) (l2: list nat): (hd (either l1 l2)) = (hd l1)\/(hd (either l1 l2)) = (hd l2).
Proof. simpl. induction l1 as [| a1 l1']. left. auto. case l2 as [|a2 l2']. right. auto.
destruct (Nat.leb a1 a2) eqn:H1. right. unfold either. unfold either_func.    

After the last tactic, I got stuck. It gives me a huge goal which let alone proving, I don't even understand.
I tried finding this out on the internet. I got a post on stack exchange regarding this but that is not working. I got an error "fix_sub_eq_ext" not recognized when I run 'rewrite fix_sub_eq_ext' as per the instruction on the post. Besides this, I also did not understand how do these tactics work. Is there any tutorial or paper where I can learn more about Program Fixpoint related tactics with a lot of examples?

Besides this, is there an alternative way to write functions involving two or more lists where only one of them is strictly decreasing in each iteration. And prove their properties.

Thanks,
Suneel




Archive powered by MHonArc 2.6.19+.

Top of Page