Searching a list by List.filter

这一生的挚爱 提交于 2019-12-11 15:28:36

问题


In my program, I use List.filter to search a list for finding specific elements. I am proving if List.filter finds some elements in a list, then by appenindg another list we still get those elements that were in the first list before appending. I am a bit stuck in provingfilterKeepSameElementsAfterAppending. To make my program shorter, I changed my program's data to customType and mydata.

 Require Import  List Nat.
 Inductive customType : Type :=
  |Const1:  nat -> customType
  |Const2: list nat -> customType.

Inductive mydata : Set :=
   |Set1: customType * customType ->mydata
   |Set2: customType ->mydata.

   Fixpoint custome_Equal (c1 c2:customType) :bool:=
        match c1 with
            |Const1 nt => match c2 with 
                       |Const1 mt =>  eqb nt  mt
                       |Const2 (hm::lmt) => eqb nt hm
                      | _ => false
                                     end
           |Const2 (hn::lnt) => match c2 with                                                                            
                           |Const1 mt => eqb  hn  mt
                           |Const2 (hm:: lmt) => eqb hn  hm
                           | _ => false
                                     end
           | _ => false
          end.

 Fixpoint Search (l: mydata) (t:customType):  bool :=
    match l with
       |Set1 (a1, a2) =>  if (custome_Equal a2 t)  then  true else false
       | _=>false
    end.

Lemma filterKeepSameElementsAfterAppending(l1 l2: list mydata)(x:mydata)(ta:customType):
In x (filter (fun n => Search n ta) (l1)) -> In x (filter (fun n  => Search n ta) (l2++ l1)).
Proof.
intro.

回答1:


The filter_cat lemma should provide you some inspiration:

filter_cat
   forall (T : Type) (a : pred T) (s1 s2 : seq T),
   [seq x <- s1 ++ s2 | a x] = [seq x <- s1 | a x] ++ [seq x <- s2 | a x]

together with mem_cat should do what you want:

mem_cat
   forall (T : eqType) (x : T) (s1 s2 : seq T),
   (x \in s1 ++ s2) = (x \in s1) || (x \in s2)

Complete code:

From mathcomp Require Import all_ssreflect.

Lemma mem_filter_cat (T : eqType) p (l1 l2 : seq T) x :
  x \in filter p l1 -> x \in filter p (l1 ++ l2).
Proof. by rewrite filter_cat mem_cat => ->. Qed.


来源:https://stackoverflow.com/questions/54962302/searching-a-list-by-list-filter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!