Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] Proof in functional style

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] Proof in functional style


Chronological Thread 
  • From: Larry Lee <llee454 AT gmail.com>
  • To: coq-club AT inria.fr
  • Subject: Re: [Coq-Club] Proof in functional style
  • Date: Thu, 29 Nov 2018 21:14:02 -0800
  • Authentication-results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=llee454 AT gmail.com; spf=Pass smtp.mailfrom=llee454 AT gmail.com; spf=None smtp.helo=postmaster AT mail-pg1-f176.google.com
  • Ironport-phdr: 9a23:n6wuXhaJtjIyex+SOgJVCw//LSx+4OfEezUN459isYplN5qZoMi5bnLW6fgltlLVR4KTs6sC17KG9fi4EUU7or+5+EgYd5JNUxJXwe43pCcHRPC/NEvgMfTxZDY7FskRHHVs/nW8LFQHUJ2mPw6arXK99yMdFQviPgRpOOv1BpTSj8Oq3Oyu5pHfeQpFiCa+bL9oMBm6sRjau9ULj4dlNqs/0AbCrGFSe+RRy2NoJFaTkAj568yt4pNt8Dletuw4+cJYXqr0Y6o3TbpDDDQ7KG81/9HktQPCTQSU+HQRVHgdnwdSDAjE6BH6WYrxsjf/u+Fg1iSWIdH6QLYpUjmk8qxlSgLniD0fOjA67W/ZitJ+gqxZrxKvuxNxxIHbbZqPO/ZiZK7QZ8kXSXZDU8tXSidPApm8b4wKD+cZO+hXtZP9p0EOrBCjAwejGv3gwSJPi3/y2a01zfouHA7G0QEvBN8Ov3HUo8/0NKcWS+y60K7IzTDaYv5QxDzz5o/IchU7rvGNW7J9acXRyUgzFwPFk1WfspbpMC+S1uQItWWQ8uluVfq3hmI5tw18piKjy8Qsh4XTmI4Z11DJ+T9kzIs3JNC0UEF2bcK+HJdNuSyXM5F6Tt4/T212oio3zr4LtJimdyYQ0psn3QTQa/mffoiI/B3jUOGRLC99hH1/ebK/gw++8VCvyuHhT8W03ktGoyhEn9XWuXAN0BvT6seDSvRj5EuuxTGP1wXL5uFFJ0A7i7bbJoY/zrIskpcfq0fOEy/slEnokaObdl8o9vWq5unmernmo4WTN45wigHwKKQuncm/DPw8MggKQWeU5+ux2b3s8EDiT7VKi+c5kqjdsJzAOcsboau5DxdP0ok/8xa/Eyum0NMAkHYbK1JFYQuLgJTtO1HTO//1Fuy/glSpkDdz3f/KJLzhApPXLnjCirjtZ7h961QPgDY0mNtY/tdfDqwLCPP1QE748tLCXTEjNAnh5+/9EsQ18o4TETaLBqKUbP6OmVCN7+MrZeKLYdlG637GN/E56qu23jcCklgHcPzxhMpFWDWDBv1jZn6hTz/pi9YFH30Nu1NnHuPvgVyGFzVUYiTrBv5u1nQAEIujSLz7aMW1mrXYhXW0G5RXYiZNDVXeSS61JbXBYO8FbWepGuEkkjEAUuL/GYoo1BXrrACjjrQ+cbSS9SofupbuktNy4r+LmA==

Hi mukesh,

I hope this helps you identify the problem in your proof.

- Larry

Require Import List.
Import ListNotations.

Open Scope list_scope.

Fixpoint intersperse (A : Type) (c : A) (l : list A) : list A :=
      match l with
      | [] => []
      | [h] => [h]
      | h :: t => h :: c :: intersperse _ c t
      end.

Lemma map_intersperse_not_working :  forall (A B : Type) (f : A -> B) (c : A) (l : list A),
          map f (intersperse _ c l) = intersperse _ (f c) (map f l).
Proof.
  exact (
    fun (A B : Type) (f : A -> B) (c : A)
      => list_ind
           (fun l => map f (intersperse _ c l) = intersperse _ (f c) (map f l))
           (eq_refl [])
           (fun x0
             => list_ind
                  (fun xs => map f (intersperse _ c xs) = intersperse _ (f c) (map f xs) -> map f (intersperse _ c (x0 :: xs)) = intersperse _ (f c) (map f (x0 :: xs)))
                  (*
                     goal:
                     map f (intersperse _ c (x0 :: [])) = intersperse _ (f c) (map f (x0 :: []))
                     map f [x0] = intersperse _ (f c) [f x0]
                     [f x0] = [f x0]
                  *)
                  (fun _ => eq_refl [f x0])
                  (* 
                     goal:
                     map f (intersperse _ c (x0 :: y0 :: ys)) = intersperse _ (f c) (map f (x0 :: y0 :: ys))
                     map f (x0 :: c :: intersperse _ c (y0 :: ys)) = intersperse _ (f c) (f x0 :: f y0 :: (map f ys))
                     (f x0 :: f c :: map f (intersperse _ c (y0 :: ys)) = (f x0 :: f c :: intersperse _ (f c) (f y0 :: map f xs))
                  *)
                  (fun y0 ys _ (H : map f (intersperse _ c (y0 :: ys)) = intersperse _ (f c) (map f (y0 :: ys)))
                    => ((eq_ind
                         (map f (intersperse _ c (y0 :: ys)))
                         (fun zs => (f x0 :: f c :: map f (intersperse _ c (y0 :: ys))) = (f x0 :: f c :: zs))
                         (eq_refl (f x0 :: f c :: map f (intersperse _ c (y0 :: ys))))
                         (intersperse _ (f c) (map f (y0 :: ys)))
                         H))))).


On Fri, 2018-11-30 at 15:24 +1100, mukesh tiwari wrote:
Hi Everyone, I have written a function (basically Haskell intersperse function)

  Fixpoint intersperse (A : Type) (c : A) (l : list A) : list A :=
        match l with
        | [] => []
        | [h] => [h]
        | h :: t => h :: c :: intersperse _ c t
        end.

     
      Compute (intersperse _ 0 []).
      Compute (intersperse _ 0 [1]).
      Compute (intersperse _ 0 [1; 2; 3]).

and proof about it's correctness      

      Lemma map_intersperse : forall (A B : Type) (f : A -> B) (c : A) (l : list A),
          map f (intersperse _ c l) = intersperse _ (f c) (map f l).
      Proof.
        intros A B f c.
        induction l.
        + cbn; auto.
        + destruct l.
          ++ cbn; auto.
          ++ cbn in *. repeat f_equal.
               (* At this point, the induction hypothesis is [1], and assumption is able to discharge
                   the proof
*)
                assumption.
      Qed.

Now I rewrote the same proof in function style using refine tactic.

Lemma map_intersperse_not_working :  forall (A B : Type) (f : A -> B) (c : A) (l : list A),
          map f (intersperse _ c l) = intersperse _ (f c) (map f l).
        refine (fun A B f c =>
                  fix F l :=
                  match  l as l'
                         return  l = l' ->
                                 map f (intersperse A c l') =
                                 intersperse B (f c) (map f l') with
                                     
                  | [] => fun H => eq_refl
                  | [h] => fun H => eq_refl
                  | h1 :: h2 :: t => fun H => _      
                  end eq_refl).
        pose proof (F (h2 :: t)).
        cbn in *. repeat f_equal. Guarded.
        (* At this point assumption H0 is same as goal [2] so apply H0 should discharge the goal*)
        apply H0. Guarded. 
       (* I am getting Recursive definition of F is ill-formed. *)

Could some one point me why it's saying F is ill-formed ? I am calling it smaller argument (At least that is my understanding). 

I got working solution with help of Li-yao [3], but I am wondering why destruct t is making a difference in this solution which I am doing explicitly in previous one (matching on one element list [h])

  Lemma map_intersperse_not_working :  forall (A B : Type) (f : A -> B) (c : A) (l : list A),
          map f (intersperse _ c l) = intersperse _ (f c) (map f l).
        refine (fun A B f c =>
                  fix F l :=
                  match  l as l'
                         return  l = l' ->
                                 map f (intersperse A c l') =
                                 intersperse B (f c) (map f l') with
                                      
                  | [] => fun H => eq_refl
                  | h :: t => fun H => _     
                  end eq_refl).
        pose proof (F t).
        destruct t.
        + cbn. exact eq_refl.
        + cbn in *. repeat f_equal. apply H0. Guarded.
      Qed.

Best regards,
Mukesh Tiwari




Archive powered by MHonArc 2.6.18.

Top of Page