Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] Program Fixpoint and Nested fix expressions

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] Program Fixpoint and Nested fix expressions


Chronological Thread 
  • From: Castéran Pierre <pierre.casteran AT gmail.com>
  • To: coq-club AT inria.fr
  • Subject: Re: [Coq-Club] Program Fixpoint and Nested fix expressions
  • Date: Mon, 14 Sep 2020 10:53:35 +0200
  • Authentication-results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=pierre.casteran AT gmail.com; spf=Pass smtp.mailfrom=pierre.casteran AT gmail.com; spf=None smtp.helo=postmaster AT mail-wr1-f49.google.com
  • Ironport-phdr: 9a23:/xPkQB3YeS2PB3UjsmDT+DRfVm0co7zxezQtwd8ZseMSL/ad9pjvdHbS+e9qxAeQG9mCtbQd0bad4/mocFdDyK7JiGoFfp1IWk1NouQttCtkPvS4D1bmJuXhdS0wEZcKflZk+3amLRodQ56mNBXdrXKo8DEdBAj0OxZrKeTpAI7SiNm82/yv95HJbAhEmTuwbalvIBmoogjducobjZZ/Iast1xXFpWdFdf5Lzm1yP1KTmBj85sa0/JF99ilbpuws+c1dX6jkZqo0VbNXAigoPGAz/83rqALMTRCT6XsGU2UZiQRHDg7Y5xznRJjxsy/6tu1g2CmGOMD9UL45VSi+46ptVRTljjoMOTwk/2HNksF+jL5Urx28qRJxzY7afoOaOvl5cK7GZ9wWWW9BU9xRVyBdAI6xaZYEAeobPeZfqonwv0UDogGlBQmwHuTv0CVHhnnu0qMg0uQuDxvG3BY+ENkTt3nUtNT1O7kIUeCv0qbE1ynMYOlN2Tfh9ofIaAouofeXULJ/dMre00gvFwffglqMrozlOiqY2+IQuGeU8+RuT/igi3I7qw5vuDivwN8hh4bXi48VyF3J9Dl0zJorKNO3VUN2YNGpHYdQuiyeKYd7Q94vTm5qtSs5y7AKpYK2cSwLxZkoxBPSa/KJfouH7B/lSe2fLzB4hHd/d7K+gRa/6VCgyvf7Vsm7ylZFtC9FnsPCt3AD0RHY98uJSuNl80u/xTqC0xrf5+JELEwui6bXNYMtzqQ/m5cXqUjPADX5lFnrgKKTakor4fWk5uDib7jkvZCRN4p5hw/gPagyhsCyBOU1PhQSU2eH/Omx273u8lb3TbhPivA5iLTVv4rfKMkfu6G2HgtY34A+4BilFTimys4XnXwfIVJFZh2Hi4/pNknLIP/iDPe/h02gkTlxx/zbJ7HhDJrAI3zZnLfuerZ97EFcyA4twtxF+51UDbQBLOryWk/3qtPYEgc0PxKoz+vjEtlw1YMTVXiRDqOHLa/eq1CF6+Y3L+mJfoAVuTL9K/Y/5/7piH80gUcdfai30psNcnC3AO5qLFmDbnr3ntcOD30KsRA4TOHxh12CVCRcZ3e2X64m+j47D4emAZ/ZRo+xmLyBwDu7HppOa29aDVCMCG7keJmAW/cRcy2fOdRhkzwBVbi5UYAtzxCutAngy7pmNOXY4CMYtYiwnORysubUjFQ58SF+J8WbyWCECW9uzU0SQDpj96lkvc1nw1G06al1meZVXYhL5v5TSAp8PpfH0+FgAtbaVQfIf9PPQ1GjFIb1SQotR848loddK312HM+v20iagniaRoQNnrnOP6Qat6LR23+repR4wnfCkbY71hwoHpoJOmqhiapysQPUAtyRyhTLp+ORba0ZmRX12iKGxGuKsltfVVcpA6rAVHEbIEDRqIagvx+Qf/qVEb0idzB554uaMKITM4/miFxHQLHoP9GMO28=

Hi,

   Perhaps it’s not a direct answer to your question, bit did you try to use mutual inductive types ?
   You can easily define flatten with structural induction (see below). It is also possible to work with the list type, and define your own induction principles.
   
  Best regards,
  Pierre

Inductive arb : Type :=
| A : nat -> arb
| B : nat -> arbs  -> arb
with arbs : Type :=
  anil : arbs
| acons : nat -> arb -> arbs -> arbs.


Scheme arb_rect2 := Induction for arb Sort Type
  with   arbs_rect2 := Induction for arbs Sort Type.

Fixpoint flatten (a : arb) : list nat :=
  match a with
  | A n => [n]
  | B n l => n :: flattens l
  end
with flattens l : list nat
       :=
         match l with
           anil => nil
         | acons  p a l =>  (p::flatten a) ++ flattens l
         end.

Compute flatten (B 7 (acons 8 (A 3) anil)).



Le 14 sept. 2020 à 09:28, Tj Barclay <tjbarclay AT ku.edu> a écrit :

Enclosed is an arbritrary example (Coq 8.11.2) of an attempt to show termination of a recursive function on the size of a nested-recursive datatype.

Is it possible for the obligation generated by the recursive call to 'flatten' inside the nested fix, 'flatten_list', to also generate hypotheses relating the matched list 'l' and the parameter 'arbs'? In particular,  that 'arbs' will always be a sublist of 'l' or that the matched pair '(_,a')' has the property 'In (_,a') l'.

(* Example Start *)
Require Import Coq.Program.Wf.
Require Import List.
Import ListNotations.

Unset Elimination Schemes.
Inductive Forall' (A : Type) (P' : A -> Type) : list A -> Type :=
| Forall_nil' : Forall' A P' []
| Forall_cons' : forall (x : A) (l : list A), P' x -> Forall' A P' l -> Forall' A P' (x :: l).

Inductive arb : Type :=
| A : nat -> arb
| B : nat -> list (nat * arb) -> arb.

Definition arb_rect_helper (P : arb -> Type) (p : nat * arb) : Type :=
  match p with
  | (x, y) => P y
  end.

Fixpoint arb_rect (P : arb -> Type)
         (H1 : forall (n : nat), P (A n))
         (H2 : forall (n : nat) (l : list (nat * arb)), Forall' (nat * arb) (arb_rect_helper P) l -> P (B n l))
         (a : arb) : P a :=
  match a return (P a) with
  | A n => H1 n
  | B n l => let fix loop (l : list (nat * arb)) :=
                 match l with
                 | [] => Forall_nil' (nat * arb) (arb_rect_helper P)
                 | (n',a')::l' => Forall_cons' (nat * arb) (arb_rect_helper P) (n',a') l' (arb_rect P H1 H2 a') (loop l')
                 end
             in
             H2 n l (loop l)
  end.

Definition arb_rec (P : arb -> Set) := arb_rect P.
Definition arb_ind (P : arb -> Prop) := arb_rect P.
Set Eliminate Schemes.

Fixpoint size_arb (a : arb) : nat :=
  match a with
  | A _ => 1
  | B _ l => 1 + (fix size_list (l : list (nat * arb)) : nat :=
                   match l with
                   | [] => 0
                   | (_,a')::l' => size_arb a' + size_list l'
                   end) l
  end.

(* Function in question *)
Program Fixpoint flatten (a : arb) {measure (size_arb a)}: list nat :=
  match a with
  | A n => [n]
  | B n l => n :: (fix flatten_list (arbs : list (nat * arb)) : list nat :=
                   match arbs with
                   | [] => []
                   | (n',a')::arbs' => n' :: (flatten a' ++ flatten_list arbs')
                end) l
  end.
Obligation 1.
(* Example End *)


Best,
TJ
--
TJ Barclay
Electrcial Engineering & Computer Science, University of Kansas
+1 316 259 2250




Archive powered by MHonArc 2.6.19+.

Top of Page